blob: 14f06e549e46d4a1aa68165d27993d80e97b9b40 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompson76de4a02013-06-28 19:32:39 -07005 */
6
7#ifndef NDN_DYNAMICUCHARARRAY_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_DYNAMICUCHARARRAY_H
Jeff Thompson76de4a02013-06-28 19:32:39 -07009
Jeff Thompson8b666002013-07-08 01:16:26 -070010#include "../errors.h"
Jeff Thompson2630dd02013-06-28 19:40:18 -070011#include "ndn_memory.h"
12
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070013#ifdef __cplusplus
Jeff Thompson76de4a02013-06-28 19:32:39 -070014extern "C" {
15#endif
16
17struct ndn_DynamicUCharArray {
Jeff Thompson519bcab2013-06-28 20:07:50 -070018 unsigned char *array; /**< the allocated array buffer */
19 unsigned int length; /**< the length of the allocated array buffer */
Jeff Thompson9254d742013-08-12 12:04:44 -070020 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 Thompson76de4a02013-06-28 19:32:39 -070026};
27
Jeff Thompson519bcab2013-06-28 20:07:50 -070028/**
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 Thompsond1427fb2013-08-29 17:20:32 -070035static inline void ndn_DynamicUCharArray_initialize
Jeff Thompson9254d742013-08-12 12:04:44 -070036 (struct ndn_DynamicUCharArray *self, unsigned char *array, unsigned int length,
37 unsigned char * (*reallocFunction)(struct ndn_DynamicUCharArray *self, unsigned char *, unsigned int))
Jeff Thompson76de4a02013-06-28 19:32:39 -070038{
39 self->array = array;
40 self->length = length;
41 self->realloc = reallocFunction;
42}
43
Jeff Thompson4f938882013-07-01 16:01:09 -070044/**
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 Thompson8b666002013-07-08 01:16:26 -070050 * @return 0 for success, else an error code if can't reallocate the array
Jeff Thompson4f938882013-07-01 16:01:09 -070051 */
Jeff Thompson8b666002013-07-08 01:16:26 -070052ndn_Error ndn_DynamicUCharArray_reallocArray(struct ndn_DynamicUCharArray *self, unsigned int length);
Jeff Thompson76de4a02013-06-28 19:32:39 -070053
Jeff Thompson519bcab2013-06-28 20:07:50 -070054/**
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 Thompson8b666002013-07-08 01:16:26 -070060 * @return 0 for success, else an error code if need to reallocate the array but can't
Jeff Thompson519bcab2013-06-28 20:07:50 -070061 */
Jeff Thompson8b666002013-07-08 01:16:26 -070062static inline ndn_Error ndn_DynamicUCharArray_ensureLength(struct ndn_DynamicUCharArray *self, unsigned int length)
Jeff Thompson76de4a02013-06-28 19:32:39 -070063{
64 if (self->length >= length)
Jeff Thompsonf2349af2013-08-08 14:05:37 -070065 return NDN_ERROR_success;
Jeff Thompson76de4a02013-06-28 19:32:39 -070066
67 return ndn_DynamicUCharArray_reallocArray(self, length);
68}
69
Jeff Thompson519bcab2013-06-28 20:07:50 -070070/**
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 Thompson8b666002013-07-08 01:16:26 -070076 * @return 0 for success, else an error code if need to reallocate the array but can't
Jeff Thompson519bcab2013-06-28 20:07:50 -070077 */
Jeff Thompson8b666002013-07-08 01:16:26 -070078static inline ndn_Error ndn_DynamicUCharArray_set
Jeff Thompson76de4a02013-06-28 19:32:39 -070079 (struct ndn_DynamicUCharArray *self, unsigned char *value, unsigned int valueLength, unsigned int offset)
80{
Jeff Thompson8b666002013-07-08 01:16:26 -070081 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070082 if ((error = ndn_DynamicUCharArray_ensureLength(self, valueLength + offset)))
Jeff Thompson76de4a02013-06-28 19:32:39 -070083 return error;
84 ndn_memcpy(self->array + offset, value, valueLength);
Jeff Thompsonadaf9232013-08-08 14:30:29 -070085 return NDN_ERROR_success;
Jeff Thompson76de4a02013-06-28 19:32:39 -070086};
87
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070088#ifdef __cplusplus
Jeff Thompson76de4a02013-06-28 19:32:39 -070089}
90#endif
91
92#endif
93