blob: d8d0d234b6b9e5f2b739b3d3bf39f722fb9f2477 [file] [log] [blame]
/**
* @author: Jeff Thompson
* See COPYING for copyright and distribution information.
*/
#ifndef NDN_DYNAMICUCHARARRAY_H
#define NDN_DYNAMICUCHARARRAY_H
#include "../errors.h"
#include "ndn_memory.h"
#ifdef __cplusplus
extern "C" {
#endif
struct ndn_DynamicUCharArray {
unsigned char *array; /**< the allocated array buffer */
unsigned int length; /**< the length of the allocated array buffer */
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
* length bytes, or 0 for error. On success, the contents of the old buffer are copied to the new one.
* On success, the original array pointer will no longer be used.
* This function pointer may be 0 (which causes an error if a reallocate is necessary). */
};
/**
* Initialize an ndn_DynamicUCharArray struct with the given array buffer.
* @param self pointer to the ndn_DynamicUCharArray struct
* @param array the allocated array buffer
* @param length the length of the allocated array buffer
* @param reallocFunction see ndn_DynamicUCharArray_ensureLength. This may be 0.
*/
static inline void ndn_DynamicUCharArray_init
(struct ndn_DynamicUCharArray *self, unsigned char *array, unsigned int length, unsigned char * (*reallocFunction)(unsigned char *, unsigned int))
{
self->array = array;
self->length = length;
self->realloc = reallocFunction;
}
/**
* Do the work of ndn_DynamicUCharArray_ensureLength if realloc is necessary.
* If the self->realloc function pointer is null, then return an error.
* If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length).
* @param self pointer to the ndn_DynamicUCharArray struct
* @param length the needed minimum size for self->length
* @return 0 for success, else an error code if can't reallocate the array
*/
ndn_Error ndn_DynamicUCharArray_reallocArray(struct ndn_DynamicUCharArray *self, unsigned int length);
/**
* Ensure that self->length is greater than or equal to length. If it is, just return 0 for success.
* Otherwise, if the self->realloc function pointer is null, then return an error.
* If not null, call self->realloc to reallocate self->array, and update self->length (which may be greater than length).
* @param self pointer to the ndn_DynamicUCharArray struct
* @param length the needed minimum size for self->length
* @return 0 for success, else an error code if need to reallocate the array but can't
*/
static inline ndn_Error ndn_DynamicUCharArray_ensureLength(struct ndn_DynamicUCharArray *self, unsigned int length)
{
if (self->length >= length)
return (ndn_Error)0;
return ndn_DynamicUCharArray_reallocArray(self, length);
}
/**
* Copy value into self->array at offset, using ndn_DynamicUCharArray_ensureLength to make sure self->array has enough length.
* @param self pointer to the ndn_DynamicUCharArray struct
* @param value the buffer to copy from
* @param valueLength the length of the value buffer
* @param offset the offset in self->array to copy to
* @return 0 for success, else an error code if need to reallocate the array but can't
*/
static inline ndn_Error ndn_DynamicUCharArray_set
(struct ndn_DynamicUCharArray *self, unsigned char *value, unsigned int valueLength, unsigned int offset)
{
ndn_Error error;
if (error = ndn_DynamicUCharArray_ensureLength(self, valueLength + offset))
return error;
ndn_memcpy(self->array + offset, value, valueLength);
};
#ifdef __cplusplus
}
#endif
#endif