Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_DYNAMICUCHARARRAY_H |
| 8 | #define NDN_DYNAMICUCHARARRAY_H |
| 9 | |
Jeff Thompson | 2630dd0 | 2013-06-28 19:40:18 -0700 | [diff] [blame^] | 10 | #include "ndn_memory.h" |
| 11 | |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 12 | #ifdef __cplusplus |
| 13 | extern "C" { |
| 14 | #endif |
| 15 | |
| 16 | struct ndn_DynamicUCharArray { |
| 17 | unsigned char *array; |
| 18 | unsigned int length; |
| 19 | unsigned char (*realloc)(unsigned char *array, unsigned int length); |
| 20 | }; |
| 21 | |
| 22 | static inline void ndn_DynamicUCharArray_init |
| 23 | (struct ndn_DynamicUCharArray *self, unsigned char *array, unsigned int length, unsigned char (*reallocFunction)(unsigned char *, unsigned int)) |
| 24 | { |
| 25 | self->array = array; |
| 26 | self->length = length; |
| 27 | self->realloc = reallocFunction; |
| 28 | } |
| 29 | |
| 30 | char *ndn_DynamicUCharArray_reallocArray(struct ndn_DynamicUCharArray *self, unsigned int length); |
| 31 | |
| 32 | static inline char *ndn_DynamicUCharArray_ensureLength(struct ndn_DynamicUCharArray *self, unsigned int length) |
| 33 | { |
| 34 | if (self->length >= length) |
| 35 | return 0; |
| 36 | |
| 37 | return ndn_DynamicUCharArray_reallocArray(self, length); |
| 38 | } |
| 39 | |
| 40 | static inline char *ndn_DynamicUCharArray_set |
| 41 | (struct ndn_DynamicUCharArray *self, unsigned char *value, unsigned int valueLength, unsigned int offset) |
| 42 | { |
| 43 | char *error; |
| 44 | if (error = ndn_DynamicUCharArray_ensureLength(self, valueLength + offset)) |
| 45 | return error; |
| 46 | ndn_memcpy(self->array + offset, value, valueLength); |
| 47 | }; |
| 48 | |
| 49 | #ifdef __cplusplus |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | #endif |
| 54 | |