From 47f4337604ed904035c263d365c1397d7edc8e7b Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 1 Feb 2018 01:24:36 +0100 Subject: [PATCH] cJSON_ParseWithOpts: Extract pasrse with internal_configuration Also introduces a allow_data_after_json property in the internal configuration. --- cJSON.c | 69 ++++++++++++++++++++++++++------------------ cJSON.h | 4 +-- tests/misc_tests.c | 6 ++-- tests/parse_array.c | 4 +-- tests/parse_number.c | 2 +- tests/parse_object.c | 4 +-- tests/parse_string.c | 4 +-- tests/parse_value.c | 2 +- tests/print_array.c | 6 ++-- tests/print_number.c | 2 +- tests/print_object.c | 6 ++-- tests/print_string.c | 2 +- tests/print_value.c | 4 +-- 13 files changed, 64 insertions(+), 51 deletions(-) diff --git a/cJSON.c b/cJSON.c index f133f8c..ceb0a48 100644 --- a/cJSON.c +++ b/cJSON.c @@ -122,6 +122,7 @@ typedef struct internal_configuration { size_t buffer_size; cJSON_bool format; + cJSON_bool allow_data_after_json; void *(*allocate)(size_t size); void (*deallocate)(void *pointer); void *(*reallocate)(void *pointer, size_t size); @@ -150,6 +151,7 @@ static void *internal_realloc(void *pointer, size_t size) static internal_configuration global_configuration = { 256, /* default buffer size */ true, /* enable formatting by default */ + true, /* allow data after the JSON by default */ internal_malloc, internal_free, internal_realloc @@ -1007,39 +1009,38 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) } /* Parse an object - create a new root, and populate. */ -CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +static cJSON *parse(const char * const json, const internal_configuration * const configuration, size_t *end_position) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; cJSON *item = NULL; - /* reset error position */ + /* reset global error position */ global_error.json = NULL; global_error.position = 0; - if (value == NULL) + if (json == NULL) { goto fail; } - buffer.content = (const unsigned char*)value; - buffer.length = strlen((const char*)value) + sizeof(""); + buffer.content = (const unsigned char*)json; + buffer.length = strlen((const char*)json) + sizeof(""); buffer.offset = 0; - buffer.configuration = global_configuration; + buffer.configuration = *configuration; - item = create_item(&global_configuration); - if (item == NULL) /* memory fail */ + item = create_item(configuration); + if (item == NULL) { goto fail; } if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) { - /* parse failure. ep is set. */ + /* parse failure. error position is set. */ goto fail; } - /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ - if (require_null_terminated) + if (!configuration->allow_data_after_json) { buffer_skip_whitespace(&buffer); if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') @@ -1047,23 +1048,20 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return goto fail; } } - if (return_parse_end) - { - *return_parse_end = (const char*)buffer_at_offset(&buffer); - } + *end_position = buffer.offset; return item; fail: if (item != NULL) { - delete_item(item, &global_configuration); + delete_item(item, configuration); } - if (value != NULL) + if (json != NULL) { error local_error; - local_error.json = (const unsigned char*)value; + local_error.json = (const unsigned char*)json; local_error.position = 0; if (buffer.offset < buffer.length) @@ -1075,21 +1073,36 @@ fail: local_error.position = buffer.length - 1; } - if (return_parse_end != NULL) - { - *return_parse_end = (const char*)local_error.json + local_error.position; - } - + *end_position = local_error.position; global_error = local_error; } return NULL; } -/* Default options for cJSON_Parse */ -CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +/* Parse an object - create a new root, and populate. */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *json, const char **return_parse_end, cJSON_bool require_null_terminated) { - return cJSON_ParseWithOpts(value, 0, 0); + size_t end_position = 0; + internal_configuration configuration = global_configuration; + cJSON *item = NULL; + + configuration.allow_data_after_json = !require_null_terminated; + item = parse(json, &configuration, &end_position); + + if (return_parse_end != NULL) + { + *return_parse_end = json + end_position; + } + + return item; +} + +/* Default options for cJSON_Parse */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *json) +{ + size_t end_position = 0; + return parse(json, &global_configuration, &end_position); } #define cjson_min(a, b) ((a < b) ? a : b) @@ -1188,7 +1201,7 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) { - printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; if ((length < 0) || (buffer == NULL)) { diff --git a/cJSON.h b/cJSON.h index 26e5be9..980d9b9 100644 --- a/cJSON.h +++ b/cJSON.h @@ -137,10 +137,10 @@ CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); /* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */ /* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ -CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *json); /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ /* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */ -CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *json, const char **return_parse_end, cJSON_bool require_null_terminated); /* Render a cJSON entity to text for transfer/storage. */ CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 4542a22..f63fc85 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -419,7 +419,7 @@ static void *failing_realloc(void *pointer, size_t size) static void ensure_should_fail_on_failed_realloc(void) { - printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, &malloc, &free, &failing_realloc}}; + printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, true, &malloc, &free, &failing_realloc}}; buffer.buffer = (unsigned char*)malloc(100); TEST_ASSERT_NOT_NULL(buffer.buffer); @@ -429,7 +429,7 @@ static void ensure_should_fail_on_failed_realloc(void) static void skip_utf8_bom_should_skip_bom(void) { const unsigned char string[] = "\xEF\xBB\xBF{}"; - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.content = string; buffer.length = sizeof(string); buffer.configuration = global_configuration; @@ -441,7 +441,7 @@ static void skip_utf8_bom_should_skip_bom(void) static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void) { const unsigned char string[] = " \xEF\xBB\xBF{}"; - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.content = string; buffer.length = sizeof(string); buffer.configuration = global_configuration; diff --git a/tests/parse_array.c b/tests/parse_array.c index c8f1fcf..d6503d2 100644 --- a/tests/parse_array.c +++ b/tests/parse_array.c @@ -44,7 +44,7 @@ static void assert_is_array(cJSON *array_item) static void assert_not_array(const char *json) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)json; buffer.length = strlen(json) + sizeof(""); buffer.configuration = global_configuration; @@ -55,7 +55,7 @@ static void assert_not_array(const char *json) static void assert_parse_array(const char *json) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)json; buffer.length = strlen(json) + sizeof(""); buffer.configuration = global_configuration; diff --git a/tests/parse_number.c b/tests/parse_number.c index 69fcfd1..6bcf9fe 100644 --- a/tests/parse_number.c +++ b/tests/parse_number.c @@ -45,7 +45,7 @@ static void assert_is_number(cJSON *number_item) static void assert_parse_number(const char *string, int integer, double real) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); diff --git a/tests/parse_object.c b/tests/parse_object.c index 8b927f1..eef951b 100644 --- a/tests/parse_object.c +++ b/tests/parse_object.c @@ -52,7 +52,7 @@ static void assert_is_child(cJSON *child_item, const char *name, int type) static void assert_not_object(const char *json) { - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)json; parsebuffer.length = strlen(json) + sizeof(""); parsebuffer.configuration = global_configuration; @@ -64,7 +64,7 @@ static void assert_not_object(const char *json) static void assert_parse_object(const char *json) { - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)json; parsebuffer.length = strlen(json) + sizeof(""); parsebuffer.configuration = global_configuration; diff --git a/tests/parse_string.c b/tests/parse_string.c index 2eefc64..30ecd65 100644 --- a/tests/parse_string.c +++ b/tests/parse_string.c @@ -45,7 +45,7 @@ static void assert_is_string(cJSON *string_item) static void assert_parse_string(const char *string, const char *expected) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); buffer.configuration = global_configuration; @@ -59,7 +59,7 @@ static void assert_parse_string(const char *string, const char *expected) static void assert_not_parse_string(const char * const string) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); buffer.configuration = global_configuration; diff --git a/tests/parse_value.c b/tests/parse_value.c index 18e4101..dddf75a 100644 --- a/tests/parse_value.c +++ b/tests/parse_value.c @@ -43,7 +43,7 @@ static void assert_is_value(cJSON *value_item, int type) static void assert_parse_value(const char *string, int type) { - parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.content = (const unsigned char*) string; buffer.length = strlen(string) + sizeof(""); buffer.configuration = global_configuration; diff --git a/tests/print_array.c b/tests/print_array.c index 5b6cd1b..239c2c9 100644 --- a/tests/print_array.c +++ b/tests/print_array.c @@ -31,10 +31,10 @@ static void assert_print_array(const char * const expected, const char * const i cJSON item[1]; - printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; - printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; + printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)input; parsebuffer.length = strlen(input) + sizeof(""); parsebuffer.configuration = global_configuration; diff --git a/tests/print_number.c b/tests/print_number.c index c2780a1..02e850b 100644 --- a/tests/print_number.c +++ b/tests/print_number.c @@ -28,7 +28,7 @@ static void assert_print_number(const char *expected, double input) { unsigned char printed[1024]; cJSON item[1]; - printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; diff --git a/tests/print_object.c b/tests/print_object.c index ca24580..b374913 100644 --- a/tests/print_object.c +++ b/tests/print_object.c @@ -31,9 +31,9 @@ static void assert_print_object(const char * const expected, const char * const cJSON item[1]; - printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; - printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; + printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; /* buffer for parsing */ parsebuffer.content = (const unsigned char*)input; diff --git a/tests/print_string.c b/tests/print_string.c index cd7d17a..6b0e6b2 100644 --- a/tests/print_string.c +++ b/tests/print_string.c @@ -27,7 +27,7 @@ static void assert_print_string(const char *expected, const char *input) { unsigned char printed[1024]; - printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; diff --git a/tests/print_value.c b/tests/print_value.c index 8b6e4ea..a887151 100644 --- a/tests/print_value.c +++ b/tests/print_value.c @@ -32,8 +32,8 @@ static void assert_print_value(const char *input) { unsigned char printed[1024]; cJSON item[1]; - printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } }; + printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0;