diff --git a/Makefile b/Makefile index 48c6c0e2..20facb18 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ TINYCBOR_SOURCES = \ src/cborencoder.c \ src/cborencoder_close_container_checked.c \ src/cborparser.c \ + src/cborparser_dup_string.c \ src/cborpretty.c \ src/cbortojson.c \ $(if $(open_memstream-pass),,src/open_memstream.c) \ diff --git a/Makefile.nmake b/Makefile.nmake index cf35bd5c..abf50da5 100644 --- a/Makefile.nmake +++ b/Makefile.nmake @@ -6,12 +6,13 @@ TINYCBOR_SOURCES = \ src\cborencoder.c \ src\cborencoder_close_container_checked.c \ src\cborparser.c \ + src\cborparser_dup_string.c \ src\cborpretty.c TINYCBOR_OBJS = \ src\cborerrorstrings.obj \ src\cborencoder.obj \ src\cborencoder_close_container_checked.obj \ - src\cborparser.obj \ + src\cborparser_dup_string.obj \ src\cborpretty.obj all: lib\tinycbor.lib diff --git a/src/cborparser.c b/src/cborparser.c index b03e03a2..f62727bd 100644 --- a/src/cborparser.c +++ b/src/cborparser.c @@ -29,7 +29,6 @@ #include "extract_number_p.h" #include -#include #include #include "assert_p.h" /* Always include last */ @@ -426,71 +425,6 @@ CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len return _cbor_value_copy_string(value, NULL, len, NULL); } -/** - * \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next) - * - * Allocates memory for the string pointed by \a value and copies it into this - * buffer. The pointer to the buffer is stored in \a buffer and the number of - * bytes copied is stored in \a len (those variables must not be NULL). - * - * If \c malloc returns a NULL pointer, this function will return error - * condition \ref CborErrorOutOfMemory. - * - * On success, \c{*buffer} will contain a valid pointer that must be freed by - * calling \c{free()}. This is the case even for zero-length strings. - * - * The \a next pointer, if not null, will be updated to point to the next item - * after this string. If \a value points to the last item, then \a next will be - * invalid. - * - * \note This function does not perform UTF-8 validation on the incoming text - * string. - * - * \sa cbor_value_copy_text_string(), cbor_value_dup_byte_string() - */ - -/** - * \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next) - * - * Allocates memory for the string pointed by \a value and copies it into this - * buffer. The pointer to the buffer is stored in \a buffer and the number of - * bytes copied is stored in \a len (those variables must not be NULL). - * - * If \c malloc returns a NULL pointer, this function will return error - * condition \ref CborErrorOutOfMemory. - * - * On success, \c{*buffer} will contain a valid pointer that must be freed by - * calling \c{free()}. This is the case even for zero-length strings. - * - * The \a next pointer, if not null, will be updated to point to the next item - * after this string. If \a value points to the last item, then \a next will be - * invalid. - * - * \sa cbor_value_copy_byte_string(), cbor_value_dup_text_string() - */ -CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next) -{ - assert(buffer); - assert(buflen); - *buflen = SIZE_MAX; - CborError err = _cbor_value_copy_string(value, NULL, buflen, NULL); - if (err) - return err; - - ++*buflen; - *buffer = malloc(*buflen); - if (!*buffer) { - /* out of memory */ - return CborErrorOutOfMemory; - } - err = _cbor_value_copy_string(value, *buffer, buflen, next); - if (err) { - free(*buffer); - return err; - } - return CborNoError; -} - /* We return uintptr_t so that we can pass memcpy directly as the iteration * function. The choice is to optimize for memcpy, which is used in the base * parser API (cbor_value_copy_string), while memcmp is used in convenience API diff --git a/src/cborparser_dup_string.c b/src/cborparser_dup_string.c new file mode 100644 index 00000000..1ffb11ce --- /dev/null +++ b/src/cborparser_dup_string.c @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Intel Corporation +** +** Permission is hereby granted, free of charge, to any person obtaining a copy +** of this software and associated documentation files (the "Software"), to deal +** in the Software without restriction, including without limitation the rights +** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +** copies of the Software, and to permit persons to whom the Software is +** furnished to do so, subject to the following conditions: +** +** The above copyright notice and this permission notice shall be included in +** all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +** THE SOFTWARE. +** +****************************************************************************/ + +#define _BSD_SOURCE 1 +#include "cbor.h" +#include + +/** + * \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next) + * + * Allocates memory for the string pointed by \a value and copies it into this + * buffer. The pointer to the buffer is stored in \a buffer and the number of + * bytes copied is stored in \a len (those variables must not be NULL). + * + * If \c malloc returns a NULL pointer, this function will return error + * condition \ref CborErrorOutOfMemory. + * + * On success, \c{*buffer} will contain a valid pointer that must be freed by + * calling \c{free()}. This is the case even for zero-length strings. + * + * The \a next pointer, if not null, will be updated to point to the next item + * after this string. If \a value points to the last item, then \a next will be + * invalid. + * + * \note This function does not perform UTF-8 validation on the incoming text + * string. + * + * \sa cbor_value_copy_text_string(), cbor_value_dup_byte_string() + */ + +/** + * \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next) + * + * Allocates memory for the string pointed by \a value and copies it into this + * buffer. The pointer to the buffer is stored in \a buffer and the number of + * bytes copied is stored in \a len (those variables must not be NULL). + * + * If \c malloc returns a NULL pointer, this function will return error + * condition \ref CborErrorOutOfMemory. + * + * On success, \c{*buffer} will contain a valid pointer that must be freed by + * calling \c{free()}. This is the case even for zero-length strings. + * + * The \a next pointer, if not null, will be updated to point to the next item + * after this string. If \a value points to the last item, then \a next will be + * invalid. + * + * \sa cbor_value_copy_byte_string(), cbor_value_dup_text_string() + */ +CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next) +{ + assert(buffer); + assert(buflen); + *buflen = SIZE_MAX; + CborError err = _cbor_value_copy_string(value, NULL, buflen, NULL); + if (err) + return err; + + ++*buflen; + *buffer = malloc(*buflen); + if (!*buffer) { + /* out of memory */ + return CborErrorOutOfMemory; + } + err = _cbor_value_copy_string(value, *buffer, buflen, next); + if (err) { + free(*buffer); + return err; + } + return CborNoError; +} diff --git a/src/src.pri b/src/src.pri index fcf33c46..5e799d59 100644 --- a/src/src.pri +++ b/src/src.pri @@ -3,6 +3,7 @@ SOURCES += \ $$PWD/cborencoder_close_container_checked.c \ $$PWD/cborerrorstrings.c \ $$PWD/cborparser.c \ + $$PWD/cborparser_dup_string.c \ $$PWD/cborpretty.c \ $$PWD/cbortojson.c \ diff --git a/tests/cpp/tst_cpp.cpp b/tests/cpp/tst_cpp.cpp index 48e017b7..21fd58a6 100644 --- a/tests/cpp/tst_cpp.cpp +++ b/tests/cpp/tst_cpp.cpp @@ -24,12 +24,13 @@ #include "../../src/cborencoder.c" #include "../../src/cborparser.c" +#include "../../src/cborparser_dup_string.c" #include "../../src/cborerrorstrings.c" #include // This is a compilation-only test. -// All it does is verify that the three source files above +// All it does is verify that the four source files above // compile as C++ without errors. class tst_Cpp : public QObject {