blob: 94f852f13056e000dd7ebda115938b3523b534c9 [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
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_DYNAMICUCHARARRAY_H
Jeff Thompson76de4a02013-06-28 19:32:39 -07008
Jeff Thompson8b666002013-07-08 01:16:26 -07009#include "../errors.h"
Jeff Thompson2630dd02013-06-28 19:40:18 -070010#include "ndn_memory.h"
11
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070012#ifdef __cplusplus
Jeff Thompson76de4a02013-06-28 19:32:39 -070013extern "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 */
Jeff Thompson9254d742013-08-12 12:04:44 -070019 unsigned char * (*realloc)
20 (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
21 * length bytes, or 0 for error. On success, the contents of the old buffer are copied to the new one.
22 * On success, the original array pointer will no longer be used.
23 * self is a pointer to the struct ndn_DynamicUCharArray which is calling realloc.
24 * This function pointer may be 0 (which causes an error if a reallocate is necessary). */
Jeff Thompson76de4a02013-06-28 19:32:39 -070025};
26
Jeff Thompson519bcab2013-06-28 20:07:50 -070027/**
28 * Initialize an ndn_DynamicUCharArray struct with the given array buffer.
29 * @param self pointer to the ndn_DynamicUCharArray struct
30 * @param array the allocated array buffer
31 * @param length the length of the allocated array buffer
32 * @param reallocFunction see ndn_DynamicUCharArray_ensureLength. This may be 0.
33 */
Jeff Thompsond1427fb2013-08-29 17:20:32 -070034static inline void ndn_DynamicUCharArray_initialize
Jeff Thompson9254d742013-08-12 12:04:44 -070035 (struct ndn_DynamicUCharArray *self, unsigned char *array, unsigned int length,
36 unsigned char * (*reallocFunction)(struct ndn_DynamicUCharArray *self, unsigned char *, unsigned int))
Jeff Thompson76de4a02013-06-28 19:32:39 -070037{
38 self->array = array;
39 self->length = length;
40 self->realloc = reallocFunction;
41}
42
Jeff Thompson4f938882013-07-01 16:01:09 -070043/**
44 * Do the work of ndn_DynamicUCharArray_ensureLength if realloc is necessary.
45 * If the self->realloc function pointer is null, then return an error.
46 * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length).
47 * @param self pointer to the ndn_DynamicUCharArray struct
48 * @param length the needed minimum size for self->length
Jeff Thompson8b666002013-07-08 01:16:26 -070049 * @return 0 for success, else an error code if can't reallocate the array
Jeff Thompson4f938882013-07-01 16:01:09 -070050 */
Jeff Thompson8b666002013-07-08 01:16:26 -070051ndn_Error ndn_DynamicUCharArray_reallocArray(struct ndn_DynamicUCharArray *self, unsigned int length);
Jeff Thompson76de4a02013-06-28 19:32:39 -070052
Jeff Thompson519bcab2013-06-28 20:07:50 -070053/**
54 * Ensure that self->length is greater than or equal to length. If it is, just return 0 for success.
55 * Otherwise, if the self->realloc function pointer is null, then return an error.
56 * If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length).
57 * @param self pointer to the ndn_DynamicUCharArray struct
58 * @param length the needed minimum size for self->length
Jeff Thompson8b666002013-07-08 01:16:26 -070059 * @return 0 for success, else an error code if need to reallocate the array but can't
Jeff Thompson519bcab2013-06-28 20:07:50 -070060 */
Jeff Thompson8b666002013-07-08 01:16:26 -070061static inline ndn_Error ndn_DynamicUCharArray_ensureLength(struct ndn_DynamicUCharArray *self, unsigned int length)
Jeff Thompson76de4a02013-06-28 19:32:39 -070062{
63 if (self->length >= length)
Jeff Thompsonf2349af2013-08-08 14:05:37 -070064 return NDN_ERROR_success;
Jeff Thompson76de4a02013-06-28 19:32:39 -070065
66 return ndn_DynamicUCharArray_reallocArray(self, length);
67}
68
Jeff Thompson519bcab2013-06-28 20:07:50 -070069/**
70 * Copy value into self->array at offset, using ndn_DynamicUCharArray_ensureLength to make sure self->array has enough length.
71 * @param self pointer to the ndn_DynamicUCharArray struct
72 * @param value the buffer to copy from
73 * @param valueLength the length of the value buffer
74 * @param offset the offset in self->array to copy to
Jeff Thompson8b666002013-07-08 01:16:26 -070075 * @return 0 for success, else an error code if need to reallocate the array but can't
Jeff Thompson519bcab2013-06-28 20:07:50 -070076 */
Jeff Thompson8b666002013-07-08 01:16:26 -070077static inline ndn_Error ndn_DynamicUCharArray_set
Jeff Thompson76de4a02013-06-28 19:32:39 -070078 (struct ndn_DynamicUCharArray *self, unsigned char *value, unsigned int valueLength, unsigned int offset)
79{
Jeff Thompson8b666002013-07-08 01:16:26 -070080 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070081 if ((error = ndn_DynamicUCharArray_ensureLength(self, valueLength + offset)))
Jeff Thompson76de4a02013-06-28 19:32:39 -070082 return error;
83 ndn_memcpy(self->array + offset, value, valueLength);
Jeff Thompsonadaf9232013-08-08 14:30:29 -070084 return NDN_ERROR_success;
Jeff Thompson76de4a02013-06-28 19:32:39 -070085};
86
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070087#ifdef __cplusplus
Jeff Thompson76de4a02013-06-28 19:32:39 -070088}
89#endif
90
91#endif
92