blob: 66476803c1c086c944cdc58b66365ae3291a9efa [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson76de4a02013-06-28 19:32:39 -07004 */
5
6#ifndef NDN_DYNAMICUCHARARRAY_H
7#define NDN_DYNAMICUCHARARRAY_H
8
Jeff Thompson2630dd02013-06-28 19:40:18 -07009#include "ndn_memory.h"
10
Jeff Thompson76de4a02013-06-28 19:32:39 -070011#ifdef __cplusplus
12extern "C" {
13#endif
14
15struct ndn_DynamicUCharArray {
Jeff Thompson519bcab2013-06-28 20:07:50 -070016 unsigned char *array; /**< the allocated array buffer */
17 unsigned int length; /**< the length of the allocated array buffer */
Jeff Thompson4f938882013-07-01 16:01:09 -070018 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
19 * length bytes, or 0 for error. On success, the contents of the old buffer are copied to the new one.
20 * On success, the original array pointer will no longer be used.
21 * This function pointer may be 0 (which causes an error if a reallocate is necessary). */
Jeff Thompson76de4a02013-06-28 19:32:39 -070022};
23
Jeff Thompson519bcab2013-06-28 20:07:50 -070024/**
25 * Initialize an ndn_DynamicUCharArray struct with the given array buffer.
26 * @param self pointer to the ndn_DynamicUCharArray struct
27 * @param array the allocated array buffer
28 * @param length the length of the allocated array buffer
29 * @param reallocFunction see ndn_DynamicUCharArray_ensureLength. This may be 0.
30 */
Jeff Thompson76de4a02013-06-28 19:32:39 -070031static inline void ndn_DynamicUCharArray_init
Jeff Thompson4f938882013-07-01 16:01:09 -070032 (struct ndn_DynamicUCharArray *self, unsigned char *array, unsigned int length, unsigned char * (*reallocFunction)(unsigned char *, unsigned int))
Jeff Thompson76de4a02013-06-28 19:32:39 -070033{
34 self->array = array;
35 self->length = length;
36 self->realloc = reallocFunction;
37}
38
Jeff Thompson4f938882013-07-01 16:01:09 -070039/**
40 * Do the work of ndn_DynamicUCharArray_ensureLength if realloc is necessary.
41 * If the self->realloc function pointer is null, then return an error.
42 * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length).
43 * @param self pointer to the ndn_DynamicUCharArray struct
44 * @param length the needed minimum size for self->length
45 * @return 0 for success, else an error string if can't reallocate the array
46 */
Jeff Thompson76de4a02013-06-28 19:32:39 -070047char *ndn_DynamicUCharArray_reallocArray(struct ndn_DynamicUCharArray *self, unsigned int length);
48
Jeff Thompson519bcab2013-06-28 20:07:50 -070049/**
50 * Ensure that self->length is greater than or equal to length. If it is, just return 0 for success.
51 * Otherwise, if the self->realloc function pointer is null, then return an error.
52 * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length).
53 * @param self pointer to the ndn_DynamicUCharArray struct
54 * @param length the needed minimum size for self->length
55 * @return 0 for success, else an error string if need to reallocate the array but can't
56 */
Jeff Thompson76de4a02013-06-28 19:32:39 -070057static inline char *ndn_DynamicUCharArray_ensureLength(struct ndn_DynamicUCharArray *self, unsigned int length)
58{
59 if (self->length >= length)
60 return 0;
61
62 return ndn_DynamicUCharArray_reallocArray(self, length);
63}
64
Jeff Thompson519bcab2013-06-28 20:07:50 -070065/**
66 * Copy value into self->array at offset, using ndn_DynamicUCharArray_ensureLength to make sure self->array has enough length.
67 * @param self pointer to the ndn_DynamicUCharArray struct
68 * @param value the buffer to copy from
69 * @param valueLength the length of the value buffer
70 * @param offset the offset in self->array to copy to
71 * @return 0 for success, else an error string if need to reallocate the array but can't
72 */
Jeff Thompson76de4a02013-06-28 19:32:39 -070073static inline char *ndn_DynamicUCharArray_set
74 (struct ndn_DynamicUCharArray *self, unsigned char *value, unsigned int valueLength, unsigned int offset)
75{
76 char *error;
77 if (error = ndn_DynamicUCharArray_ensureLength(self, valueLength + offset))
78 return error;
79 ndn_memcpy(self->array + offset, value, valueLength);
80};
81
82#ifdef __cplusplus
83}
84#endif
85
86#endif
87