blob: 6548662bddfc0b522f25d1bce79b47845a05cc5d [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
Jeff Thompson10ad12a2013-09-24 16:19:11 -070017struct ndn_DynamicUInt8Array {
18 uint8_t *array; /**< the allocated array buffer */
Jeff Thompson97223af2013-09-24 17:01:27 -070019 size_t length; /**< the length of the allocated array buffer */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070020 uint8_t * (*realloc)
Jeff Thompson97223af2013-09-24 17:01:27 -070021 (struct ndn_DynamicUInt8Array *self, uint8_t *array, size_t length); /**< a pointer to a function that reallocates array and returns a new pointer to a buffer of
Jeff Thompson9254d742013-08-12 12:04:44 -070022 * 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.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070024 * self is a pointer to the struct ndn_DynamicUInt8Array which is calling realloc.
Jeff Thompson9254d742013-08-12 12:04:44 -070025 * 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/**
Jeff Thompson10ad12a2013-09-24 16:19:11 -070029 * Initialize an ndn_DynamicUInt8Array struct with the given array buffer.
30 * @param self pointer to the ndn_DynamicUInt8Array struct
Jeff Thompson519bcab2013-06-28 20:07:50 -070031 * @param array the allocated array buffer
32 * @param length the length of the allocated array buffer
Jeff Thompson10ad12a2013-09-24 16:19:11 -070033 * @param reallocFunction see ndn_DynamicUInt8Array_ensureLength. This may be 0.
Jeff Thompson519bcab2013-06-28 20:07:50 -070034 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070035static inline void ndn_DynamicUInt8Array_initialize
Jeff Thompson97223af2013-09-24 17:01:27 -070036 (struct ndn_DynamicUInt8Array *self, uint8_t *array, size_t length,
37 uint8_t * (*reallocFunction)(struct ndn_DynamicUInt8Array *self, uint8_t *, size_t))
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/**
Jeff Thompson10ad12a2013-09-24 16:19:11 -070045 * Do the work of ndn_DynamicUInt8Array_ensureLength if realloc is necessary.
Jeff Thompson4f938882013-07-01 16:01:09 -070046 * 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).
Jeff Thompson10ad12a2013-09-24 16:19:11 -070048 * @param self pointer to the ndn_DynamicUInt8Array struct
Jeff Thompson4f938882013-07-01 16:01:09 -070049 * @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 Thompson97223af2013-09-24 17:01:27 -070052ndn_Error ndn_DynamicUInt8Array_reallocArray(struct ndn_DynamicUInt8Array *self, size_t 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).
Jeff Thompson10ad12a2013-09-24 16:19:11 -070058 * @param self pointer to the ndn_DynamicUInt8Array struct
Jeff Thompson519bcab2013-06-28 20:07:50 -070059 * @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 Thompson97223af2013-09-24 17:01:27 -070062static inline ndn_Error ndn_DynamicUInt8Array_ensureLength(struct ndn_DynamicUInt8Array *self, size_t 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
Jeff Thompson10ad12a2013-09-24 16:19:11 -070067 return ndn_DynamicUInt8Array_reallocArray(self, length);
Jeff Thompson76de4a02013-06-28 19:32:39 -070068}
69
Jeff Thompson519bcab2013-06-28 20:07:50 -070070/**
Jeff Thompson10ad12a2013-09-24 16:19:11 -070071 * Copy value into self->array at offset, using ndn_DynamicUInt8Array_ensureLength to make sure self->array has enough length.
72 * @param self pointer to the ndn_DynamicUInt8Array struct
Jeff Thompson519bcab2013-06-28 20:07:50 -070073 * @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 Thompson10ad12a2013-09-24 16:19:11 -070078static inline ndn_Error ndn_DynamicUInt8Array_set
Jeff Thompson97223af2013-09-24 17:01:27 -070079 (struct ndn_DynamicUInt8Array *self, uint8_t *value, size_t valueLength, size_t offset)
Jeff Thompson76de4a02013-06-28 19:32:39 -070080{
Jeff Thompson8b666002013-07-08 01:16:26 -070081 ndn_Error error;
Jeff Thompson10ad12a2013-09-24 16:19:11 -070082 if ((error = ndn_DynamicUInt8Array_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