| 1 |
/* Copyright (C) 2008 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
#define KATE_INTERNAL |
| 11 |
#include "kate_internal.h" |
| 12 |
|
| 13 |
#ifdef HAVE_STDLIB_H |
| 14 |
#include <stdlib.h> |
| 15 |
#endif |
| 16 |
#include <string.h> |
| 17 |
#include "kate/kate.h" |
| 18 |
|
| 19 |
/** |
| 20 |
\ingroup packet |
| 21 |
Returns a kate_packet wrapping the given data. |
| 22 |
The data is not copied, so should stay valid throughout the use of the |
| 23 |
kate_packet. The kate_packet should not be cleared. |
| 24 |
\param kp the kate_packet to wrap |
| 25 |
\param nbytes the number of bytes in the data to wrap |
| 26 |
\param data a pointer to the data to wrap |
| 27 |
\returns 0 success |
| 28 |
\returns KATE_E_* error |
| 29 |
*/ |
| 30 |
int kate_packet_wrap(kate_packet *kp,size_t nbytes,const void *data) |
| 31 |
{ |
| 32 |
if (!kp) return KATE_E_INVALID_PARAMETER; |
| 33 |
if (!data && nbytes>0) return KATE_E_INVALID_PARAMETER; |
| 34 |
|
| 35 |
kp->nbytes=nbytes; |
| 36 |
kp->data=(void*)data; |
| 37 |
/* kp->data is not const as it may be freed; however, here, data will |
| 38 |
always be const as it may be a const buffer given by the user, in |
| 39 |
which case it won't be freed */ |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
\ingroup packet |
| 46 |
Returns a kate_packet created from the given data. |
| 47 |
The data is copied, so can be freed once the kate_packet is created. |
| 48 |
The kate_packet should be cleared when not needed anymore. |
| 49 |
\param kp the kate_packet to create |
| 50 |
\param nbytes the number of bytes in the data to wrap |
| 51 |
\param data a pointer to the data to wrap |
| 52 |
\returns 0 success |
| 53 |
\returns KATE_E_* error |
| 54 |
*/ |
| 55 |
int kate_packet_init(kate_packet *kp,size_t nbytes,const void *data) |
| 56 |
{ |
| 57 |
void *dup_data; |
| 58 |
|
| 59 |
if (!kp) return KATE_E_INVALID_PARAMETER; |
| 60 |
if (!data && nbytes>0) return KATE_E_INVALID_PARAMETER; |
| 61 |
|
| 62 |
dup_data=kate_malloc(nbytes); |
| 63 |
if (!dup_data) return KATE_E_OUT_OF_MEMORY; |
| 64 |
if (data) memcpy(dup_data,data,nbytes); |
| 65 |
|
| 66 |
return kate_packet_wrap(kp,nbytes,dup_data); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
\ingroup packet |
| 71 |
Clears a kate_packet previously initialized by kate_packet_init |
| 72 |
\param kp the kate_packet to clear |
| 73 |
\returns 0 success |
| 74 |
\returns KATE_E_* error |
| 75 |
*/ |
| 76 |
int kate_packet_clear(kate_packet *kp) |
| 77 |
{ |
| 78 |
if (!kp) return KATE_E_INVALID_PARAMETER; |
| 79 |
|
| 80 |
if (kp->data) { |
| 81 |
kate_free(kp->data); |
| 82 |
kp->data=NULL; |
| 83 |
} |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
|