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