From 2777a04ed2ba4fd36138b991d66a32a283361f7e Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Thu, 1 May 2008 16:34:46 +0000 Subject: Import parser construction utility library svn path=/trunk/libparserutils/; revision=4111 --- src/utils/Makefile | 49 +++++++++++++++++ src/utils/buffer.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils/errors.c | 70 ++++++++++++++++++++++++ src/utils/utils.h | 28 ++++++++++ 4 files changed, 303 insertions(+) create mode 100644 src/utils/Makefile create mode 100644 src/utils/buffer.c create mode 100644 src/utils/errors.c create mode 100644 src/utils/utils.h (limited to 'src/utils') diff --git a/src/utils/Makefile b/src/utils/Makefile new file mode 100644 index 0000000..e053673 --- /dev/null +++ b/src/utils/Makefile @@ -0,0 +1,49 @@ +# Child makefile fragment +# +# Toolchain is provided by top-level makefile +# +# Variables provided by top-level makefile +# +# COMPONENT The name of the component +# EXPORT The location of the export directory +# TOP The location of the source tree root +# RELEASEDIR The place to put release objects +# DEBUGDIR The place to put debug objects +# +# do_include Canned command sequence to include a child makefile +# +# Variables provided by parent makefile: +# +# DIR The name of the directory we're in, relative to $(TOP) +# +# Variables we can manipulate: +# +# ITEMS_CLEAN The list of items to remove for "make clean" +# ITEMS_DISTCLEAN The list of items to remove for "make distclean" +# TARGET_TESTS The list of target names to run for "make test" +# +# SOURCES The list of sources to build for $(COMPONENT) +# +# Plus anything from the toolchain + +# Push parent directory onto the directory stack +sp := $(sp).x +dirstack_$(sp) := $(d) +d := $(DIR) + +# Manipulate include paths +override CFLAGS := $(CFLAGS) -I$(d) + +# Sources +SRCS_$(d) := buffer.c errors.c + +# Append to sources for component +SOURCES += $(addprefix $(d), $(SRCS_$(d))) + +# Now include any children we may have +MAKE_INCLUDES := $(wildcard $(d)*/Makefile) +$(eval $(foreach INC, $(MAKE_INCLUDES), $(call do_include,$(INC)))) + +# Finally, pop off the directory stack +d := $(dirstack_$(sp)) +sp := $(basename $(sp)) diff --git a/src/utils/buffer.c b/src/utils/buffer.c new file mode 100644 index 0000000..21c47fc --- /dev/null +++ b/src/utils/buffer.c @@ -0,0 +1,156 @@ +/* + * This file is part of LibParserUtils. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2008 John-Mark Bell + */ + +#include + +#include + +#define DEFAULT_SIZE (4096) + +/** + * Create a memory buffer + * + * \param alloc Memory (de)allocation function + * \param pw Pointer to client-specific private data + * \return Pointer to memory buffer, or NULL on memory exhaustion + */ +parserutils_buffer *parserutils_buffer_create(parserutils_alloc alloc, void *pw) +{ + parserutils_buffer *buffer = + alloc(NULL, sizeof(parserutils_buffer), pw); + + if (buffer == NULL) + return NULL; + + buffer->data = alloc(NULL, DEFAULT_SIZE, pw); + if (buffer->data == NULL) { + alloc(buffer, 0, pw); + return NULL; + } + + buffer->length = 0; + buffer->allocated = DEFAULT_SIZE; + + buffer->alloc = alloc; + buffer->pw = pw; + + return buffer; +} + +/** + * Destroy a memory buffer + * + * \param buffer The buffer to destroy + */ +void parserutils_buffer_destroy(parserutils_buffer *buffer) +{ + if (buffer == NULL) + return; + + buffer->alloc(buffer->data, 0, buffer->pw); + buffer->alloc(buffer, 0, buffer->pw); +} + +/** + * Append data to a memory buffer + * + * \param buffer The buffer to append to + * \param data The data to append + * \param len The length, in bytes, of the data to append + * \return PARSERUTILS_OK on success, appropriate error otherwise. + */ +parserutils_error parserutils_buffer_append(parserutils_buffer *buffer, + const uint8_t *data, size_t len) +{ + while (len >= buffer->allocated - buffer->length) { + parserutils_error error = parserutils_buffer_grow(buffer); + if (error != PARSERUTILS_OK) + return error; + } + + memcpy(buffer->data + buffer->length, data, len); + + buffer->length += len; + + return PARSERUTILS_OK; +} + +/** + * Insert data into a memory buffer + * + * \param buffer The buffer to insert into + * \param offset The offset into the buffer to insert at + * \param data The data to insert + * \param len The length, in bytes, of the data to insert + * \return PARSERUTILS_OK on success, appropriate error otherwise + */ +parserutils_error parserutils_buffer_insert(parserutils_buffer *buffer, + size_t offset, const uint8_t *data, size_t len) +{ + if (offset > buffer->length) + return PARSERUTILS_BADPARM; + + if (offset == buffer->length) + return parserutils_buffer_append(buffer, data, len); + + while (len >= buffer->allocated - buffer->length) { + parserutils_error error = parserutils_buffer_grow(buffer); + if (error != PARSERUTILS_OK) + return error; + } + + memmove(buffer->data + buffer->length + len, + buffer->data + offset, buffer->length - offset); + + memcpy(buffer->data + offset, data, len); + + buffer->length += len; + + return PARSERUTILS_OK; +} + +/** + * Discard a section of a memory buffer + * + * \param buffer The buffer to discard data from + * \param offset The offset into the buffer of the start of the section + * \param len The number of bytes to discard + * \return PARSERUTILS_OK on success, appropriate error otherwise. + */ +parserutils_error parserutils_buffer_discard(parserutils_buffer *buffer, + size_t offset, size_t len) +{ + if (offset >= buffer->length || offset + len > buffer->length) + return PARSERUTILS_BADPARM; + + memmove(buffer->data + offset, buffer->data + offset + len, + buffer->length - len); + + buffer->length -= len; + + return PARSERUTILS_OK; +} + +/** + * Extend the amount of space allocated for a memory buffer + * + * \param buffer The buffer to extend + * \return PARSERUTILS_OK on success, appropriate error otherwise. + */ +parserutils_error parserutils_buffer_grow(parserutils_buffer *buffer) +{ + uint8_t *temp = buffer->alloc(buffer->data, + buffer->allocated * 2, buffer->pw); + if (temp == NULL) + return PARSERUTILS_NOMEM; + + buffer->data = temp; + buffer->allocated *= 2; + + return PARSERUTILS_OK; +} + diff --git a/src/utils/errors.c b/src/utils/errors.c new file mode 100644 index 0000000..353cda1 --- /dev/null +++ b/src/utils/errors.c @@ -0,0 +1,70 @@ +/* + * This file is part of LibParserUtils. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 John-Mark Bell + */ + +#include + +#include + +/** + * Convert a parserutils error code to a string + * + * \param error The error code to convert + * \return Pointer to string representation of error, or NULL if unknown. + */ +const char *parserutils_error_to_string(parserutils_error error) +{ + const char *result = NULL; + + switch (error) { + case PARSERUTILS_OK: + result = "No error"; + break; + case PARSERUTILS_NOMEM: + result = "Insufficient memory"; + break; + case PARSERUTILS_BADPARM: + result = "Bad parameter"; + break; + case PARSERUTILS_INVALID: + result = "Invalid input"; + break; + case PARSERUTILS_FILENOTFOUND: + result = "File not found"; + break; + case PARSERUTILS_NEEDDATA: + result = "Insufficient data"; + break; + } + + return result; +} + +/** + * Convert a string representation of an error name to a parserutils error code + * + * \param str String containing error name + * \param len Length of string (bytes) + * \return Error code, or PARSERUTILS_OK if unknown + */ +parserutils_error parserutils_error_from_string(const char *str, size_t len) +{ + if (strncmp(str, "PARSERUTILS_OK", len) == 0) { + return PARSERUTILS_OK; + } else if (strncmp(str, "PARSERUTILS_NOMEM", len) == 0) { + return PARSERUTILS_NOMEM; + } else if (strncmp(str, "PARSERUTILS_BADPARM", len) == 0) { + return PARSERUTILS_BADPARM; + } else if (strncmp(str, "PARSERUTILS_INVALID", len) == 0) { + return PARSERUTILS_INVALID; + } else if (strncmp(str, "PARSERUTILS_FILENOTFOUND", len) == 0) { + return PARSERUTILS_FILENOTFOUND; + } else if (strncmp(str, "PARSERUTILS_NEEDDATA", len) == 0) { + return PARSERUTILS_NEEDDATA; + } + + return PARSERUTILS_OK; +} diff --git a/src/utils/utils.h b/src/utils/utils.h new file mode 100644 index 0000000..5162945 --- /dev/null +++ b/src/utils/utils.h @@ -0,0 +1,28 @@ +/* + * This file is part of LibParserUtils. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 John-Mark Bell + */ + +#ifndef parserutils_utils_h_ +#define parserutils_utils_h_ + +#ifndef max +#define max(a,b) ((a)>(b)?(a):(b)) +#endif + +#ifndef min +#define min(a,b) ((a)<(b)?(a):(b)) +#endif + +#ifndef SLEN +/* Calculate length of a string constant */ +#define SLEN(s) (sizeof((s)) - 1) /* -1 for '\0' */ +#endif + +#ifndef UNUSED +#define UNUSED(x) ((x)=(x)) +#endif + +#endif -- cgit v1.2.3