Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef NDN_DYNAMICUCHARARRAY_H |
| 7 | #define NDN_DYNAMICUCHARARRAY_H |
| 8 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 9 | #include "../errors.h" |
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 { |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 17 | unsigned char *array; /**< the allocated array buffer */ |
| 18 | unsigned int length; /**< the length of the allocated array buffer */ |
Jeff Thompson | 4f93888 | 2013-07-01 16:01:09 -0700 | [diff] [blame] | 19 | unsigned char * (*realloc)(unsigned char *array, unsigned int length); /**< a pointer to a function that reallocates array and returns a new pointer to a buffer of |
| 20 | * length bytes, or 0 for error. On success, the contents of the old buffer are copied to the new one. |
| 21 | * On success, the original array pointer will no longer be used. |
| 22 | * This function pointer may be 0 (which causes an error if a reallocate is necessary). */ |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Initialize an ndn_DynamicUCharArray struct with the given array buffer. |
| 27 | * @param self pointer to the ndn_DynamicUCharArray struct |
| 28 | * @param array the allocated array buffer |
| 29 | * @param length the length of the allocated array buffer |
| 30 | * @param reallocFunction see ndn_DynamicUCharArray_ensureLength. This may be 0. |
| 31 | */ |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 32 | static inline void ndn_DynamicUCharArray_init |
Jeff Thompson | 4f93888 | 2013-07-01 16:01:09 -0700 | [diff] [blame] | 33 | (struct ndn_DynamicUCharArray *self, unsigned char *array, unsigned int length, unsigned char * (*reallocFunction)(unsigned char *, unsigned int)) |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 34 | { |
| 35 | self->array = array; |
| 36 | self->length = length; |
| 37 | self->realloc = reallocFunction; |
| 38 | } |
| 39 | |
Jeff Thompson | 4f93888 | 2013-07-01 16:01:09 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Do the work of ndn_DynamicUCharArray_ensureLength if realloc is necessary. |
| 42 | * If the self->realloc function pointer is null, then return an error. |
| 43 | * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length). |
| 44 | * @param self pointer to the ndn_DynamicUCharArray struct |
| 45 | * @param length the needed minimum size for self->length |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 46 | * @return 0 for success, else an error code if can't reallocate the array |
Jeff Thompson | 4f93888 | 2013-07-01 16:01:09 -0700 | [diff] [blame] | 47 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 48 | ndn_Error ndn_DynamicUCharArray_reallocArray(struct ndn_DynamicUCharArray *self, unsigned int length); |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 49 | |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Ensure that self->length is greater than or equal to length. If it is, just return 0 for success. |
| 52 | * Otherwise, if the self->realloc function pointer is null, then return an error. |
| 53 | * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length). |
| 54 | * @param self pointer to the ndn_DynamicUCharArray struct |
| 55 | * @param length the needed minimum size for self->length |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 56 | * @return 0 for success, else an error code if need to reallocate the array but can't |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 57 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 58 | static inline ndn_Error ndn_DynamicUCharArray_ensureLength(struct ndn_DynamicUCharArray *self, unsigned int length) |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 59 | { |
| 60 | if (self->length >= length) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 61 | return (ndn_Error)0; |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 62 | |
| 63 | return ndn_DynamicUCharArray_reallocArray(self, length); |
| 64 | } |
| 65 | |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Copy value into self->array at offset, using ndn_DynamicUCharArray_ensureLength to make sure self->array has enough length. |
| 68 | * @param self pointer to the ndn_DynamicUCharArray struct |
| 69 | * @param value the buffer to copy from |
| 70 | * @param valueLength the length of the value buffer |
| 71 | * @param offset the offset in self->array to copy to |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 72 | * @return 0 for success, else an error code if need to reallocate the array but can't |
Jeff Thompson | 519bcab | 2013-06-28 20:07:50 -0700 | [diff] [blame] | 73 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 74 | static inline ndn_Error ndn_DynamicUCharArray_set |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 75 | (struct ndn_DynamicUCharArray *self, unsigned char *value, unsigned int valueLength, unsigned int offset) |
| 76 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 77 | ndn_Error error; |
Jeff Thompson | 76de4a0 | 2013-06-28 19:32:39 -0700 | [diff] [blame] | 78 | if (error = ndn_DynamicUCharArray_ensureLength(self, valueLength + offset)) |
| 79 | return error; |
| 80 | ndn_memcpy(self->array + offset, value, valueLength); |
| 81 | }; |
| 82 | |
| 83 | #ifdef __cplusplus |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | #endif |
| 88 | |