blob: 67060fa2f29031cd9d28af87fefeac34a03ee4a2 [file] [log] [blame]
Jeff Thompson76de4a02013-06-28 19:32:39 -07001/*
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 Thompson2630dd02013-06-28 19:40:18 -070010#include "ndn_memory.h"
11
Jeff Thompson76de4a02013-06-28 19:32:39 -070012#ifdef __cplusplus
13extern "C" {
14#endif
15
16struct ndn_DynamicUCharArray {
Jeff Thompson519bcab2013-06-28 20:07:50 -070017 unsigned char *array; /**< the allocated array buffer */
18 unsigned int length; /**< the length of the allocated array buffer */
19 unsigned char (*realloc)(unsigned char *array, unsigned int length); /**< a pointer to a function that reallocates array and returns a new buffer of
20 * length bytes or 0 for error. The original array pointer is no longer used.
21 * This 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
32 (struct ndn_DynamicUCharArray *self, unsigned char *array, unsigned int length, unsigned char (*reallocFunction)(unsigned char *, unsigned int))
33{
34 self->array = array;
35 self->length = length;
36 self->realloc = reallocFunction;
37}
38
39char *ndn_DynamicUCharArray_reallocArray(struct ndn_DynamicUCharArray *self, unsigned int length);
40
Jeff Thompson519bcab2013-06-28 20:07:50 -070041/**
42 * Ensure that self->length is greater than or equal to length. If it is, just return 0 for success.
43 * Otherwise, if the self->realloc function pointer is null, then return an error.
44 * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length).
45 * @param self pointer to the ndn_DynamicUCharArray struct
46 * @param length the needed minimum size for self->length
47 * @return 0 for success, else an error string if need to reallocate the array but can't
48 */
Jeff Thompson76de4a02013-06-28 19:32:39 -070049static inline char *ndn_DynamicUCharArray_ensureLength(struct ndn_DynamicUCharArray *self, unsigned int length)
50{
51 if (self->length >= length)
52 return 0;
53
54 return ndn_DynamicUCharArray_reallocArray(self, length);
55}
56
Jeff Thompson519bcab2013-06-28 20:07:50 -070057/**
58 * Copy value into self->array at offset, using ndn_DynamicUCharArray_ensureLength to make sure self->array has enough length.
59 * @param self pointer to the ndn_DynamicUCharArray struct
60 * @param value the buffer to copy from
61 * @param valueLength the length of the value buffer
62 * @param offset the offset in self->array to copy to
63 * @return 0 for success, else an error string if need to reallocate the array but can't
64 */
Jeff Thompson76de4a02013-06-28 19:32:39 -070065static inline char *ndn_DynamicUCharArray_set
66 (struct ndn_DynamicUCharArray *self, unsigned char *value, unsigned int valueLength, unsigned int offset)
67{
68 char *error;
69 if (error = ndn_DynamicUCharArray_ensureLength(self, valueLength + offset))
70 return error;
71 ndn_memcpy(self->array + offset, value, valueLength);
72};
73
74#ifdef __cplusplus
75}
76#endif
77
78#endif
79