From 949c083315357633fe32f33fd014e42861e36624 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 27 Apr 2017 02:48:28 +0200 Subject: [PATCH] Move 'hooks' parameter into buffers (parse/print) --- cJSON.c | 132 ++++++++++++++++++++++--------------------- tests/parse_array.c | 10 ++-- tests/parse_number.c | 2 +- tests/parse_object.c | 10 ++-- tests/parse_string.c | 10 ++-- tests/parse_value.c | 6 +- tests/print_array.c | 15 +++-- tests/print_number.c | 5 +- tests/print_object.c | 15 +++-- tests/print_string.c | 5 +- tests/print_value.c | 10 ++-- 11 files changed, 122 insertions(+), 98 deletions(-) diff --git a/cJSON.c b/cJSON.c index f47a94a..4ab110f 100644 --- a/cJSON.c +++ b/cJSON.c @@ -203,6 +203,7 @@ typedef struct size_t length; size_t offset; size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; } parse_buffer; /* check if the given size is left to read in a given parse buffer (starting with 1) */ @@ -318,10 +319,11 @@ typedef struct size_t depth; /* current nesting depth (for formatted printing) */ cJSON_bool noalloc; cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; } printbuffer; /* realloc printbuffer if necessary to have at least "needed" bytes more */ -static unsigned char* ensure(printbuffer * const p, size_t needed, const internal_hooks * const hooks) +static unsigned char* ensure(printbuffer * const p, size_t needed) { unsigned char *newbuffer = NULL; size_t newsize = 0; @@ -371,18 +373,18 @@ static unsigned char* ensure(printbuffer * const p, size_t needed, const interna newsize = needed * 2; } - if (hooks->reallocate != NULL) + if (p->hooks.reallocate != NULL) { /* reallocate with realloc if available */ - newbuffer = (unsigned char*)hooks->reallocate(p->buffer, newsize); + newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); } else { /* otherwise reallocate manually */ - newbuffer = (unsigned char*)hooks->allocate(newsize); + newbuffer = (unsigned char*)p->hooks.allocate(newsize); if (!newbuffer) { - hooks->deallocate(p->buffer); + p->hooks.deallocate(p->buffer); p->length = 0; p->buffer = NULL; @@ -392,7 +394,7 @@ static unsigned char* ensure(printbuffer * const p, size_t needed, const interna { memcpy(newbuffer, p->buffer, p->offset + 1); } - hooks->deallocate(p->buffer); + p->hooks.deallocate(p->buffer); } p->length = newsize; p->buffer = newbuffer; @@ -435,7 +437,7 @@ static int trim_trailing_zeroes(const unsigned char * const number, int length, } /* Render the number nicely from the given item into a string. */ -static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) { unsigned char *output_pointer = NULL; double d = item->valuedouble; @@ -487,7 +489,7 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out } /* reserve appropriate space in the output */ - output_pointer = ensure(output_buffer, (size_t)length, hooks); + output_pointer = ensure(output_buffer, (size_t)length); if (output_pointer == NULL) { return false; @@ -671,7 +673,7 @@ fail: } /* Parse the input text into an unescaped cinput, and populate item. */ -static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks) +static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) { const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; @@ -710,7 +712,7 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu /* This is at most how much we need for the output */ allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; - output = (unsigned char*)hooks->allocate(allocation_length + sizeof("")); + output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); if (output == NULL) { goto fail; /* allocation failure */ @@ -788,7 +790,7 @@ static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_bu fail: if (output != NULL) { - hooks->deallocate(output); + input_buffer->hooks.deallocate(output); } if (input_pointer != NULL) @@ -800,7 +802,7 @@ fail: } /* Render the cstring provided to an escaped version that can be printed. */ -static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) { const unsigned char *input_pointer = NULL; unsigned char *output = NULL; @@ -817,7 +819,7 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe /* empty string */ if (input == NULL) { - output = ensure(output_buffer, sizeof("\"\""), hooks); + output = ensure(output_buffer, sizeof("\"\"")); if (output == NULL) { return false; @@ -853,7 +855,7 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe } output_length = (size_t)(input_pointer - input) + escape_characters; - output = ensure(output_buffer, output_length + sizeof("\"\""), hooks); + output = ensure(output_buffer, output_length + sizeof("\"\"")); if (output == NULL) { return false; @@ -922,18 +924,18 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffe } /* Invoke print_string_ptr (which is useful) on an item. */ -static cJSON_bool print_string(const cJSON * const item, printbuffer * const p, const internal_hooks * const hooks) +static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) { - return print_string_ptr((unsigned char*)item->valuestring, p, hooks); + return print_string_ptr((unsigned char*)item->valuestring, p); } /* Predeclare these prototypes. */ -static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks); -static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks); -static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks); -static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks); -static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks); -static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks); +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); /* Utility to jump whitespace and cr/lf */ static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) @@ -959,7 +961,7 @@ static parse_buffer *buffer_skip_whitespace(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) { - parse_buffer buffer = { 0, 0, 0, 0 }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; cJSON *item = NULL; /* reset error position */ @@ -974,6 +976,7 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return buffer.content = (const unsigned char*)value; buffer.length = strlen((const char*)value) + sizeof(""); buffer.offset = 0; + buffer.hooks = global_hooks; item = cJSON_New_Item(&global_hooks); if (item == NULL) /* memory fail */ @@ -981,7 +984,7 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return goto fail; } - if (!parse_value(item, buffer_skip_whitespace(&buffer), &global_hooks)) + if (!parse_value(item, buffer_skip_whitespace(&buffer))) { /* parse failure. ep is set. */ goto fail; @@ -1055,13 +1058,14 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, const i /* create buffer */ buffer->buffer = (unsigned char*) hooks->allocate(256); buffer->format = format; + buffer->hooks = *hooks; if (buffer->buffer == NULL) { goto fail; } /* print the value */ - if (!print_value(item, buffer, hooks)) + if (!print_value(item, buffer)) { goto fail; } @@ -1119,7 +1123,7 @@ CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) { - printbuffer p = { 0, 0, 0, 0, 0, 0 }; + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; if (prebuffer < 0) { @@ -1136,8 +1140,9 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON p.offset = 0; p.noalloc = false; p.format = fmt; + p.hooks = global_hooks; - if (!print_value(item, &p, &global_hooks)) + if (!print_value(item, &p)) { return NULL; } @@ -1147,7 +1152,7 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt) { - printbuffer p = { 0, 0, 0, 0, 0, 0 }; + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; if (len < 0) { @@ -1159,12 +1164,13 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const i p.offset = 0; p.noalloc = true; p.format = fmt; + p.hooks = global_hooks; - return print_value(item, &p, &global_hooks); + return print_value(item, &p); } /* Parser core - when encountering text, process appropriately. */ -static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks) +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) { if ((input_buffer == NULL) || (input_buffer->content == NULL)) { @@ -1197,7 +1203,7 @@ static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buf /* string */ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) { - return parse_string(item, input_buffer, hooks); + return parse_string(item, input_buffer); } /* number */ if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) @@ -1207,12 +1213,12 @@ static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buf /* array */ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) { - return parse_array(item, input_buffer, hooks); + return parse_array(item, input_buffer); } /* object */ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) { - return parse_object(item, input_buffer, hooks); + return parse_object(item, input_buffer); } @@ -1220,7 +1226,7 @@ static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buf } /* Render a value to text. */ -static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) { unsigned char *output = NULL; @@ -1232,7 +1238,7 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp switch ((item->type) & 0xFF) { case cJSON_NULL: - output = ensure(output_buffer, 5, hooks); + output = ensure(output_buffer, 5); if (output == NULL) { return false; @@ -1241,7 +1247,7 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp return true; case cJSON_False: - output = ensure(output_buffer, 6, hooks); + output = ensure(output_buffer, 6); if (output == NULL) { return false; @@ -1250,7 +1256,7 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp return true; case cJSON_True: - output = ensure(output_buffer, 5, hooks); + output = ensure(output_buffer, 5); if (output == NULL) { return false; @@ -1259,7 +1265,7 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp return true; case cJSON_Number: - return print_number(item, output_buffer, hooks); + return print_number(item, output_buffer); case cJSON_Raw: { @@ -1268,13 +1274,13 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp { if (!output_buffer->noalloc) { - hooks->deallocate(output_buffer->buffer); + output_buffer->hooks.deallocate(output_buffer->buffer); } return false; } raw_length = strlen(item->valuestring) + sizeof(""); - output = ensure(output_buffer, raw_length, hooks); + output = ensure(output_buffer, raw_length); if (output == NULL) { return false; @@ -1284,13 +1290,13 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp } case cJSON_String: - return print_string(item, output_buffer, hooks); + return print_string(item, output_buffer); case cJSON_Array: - return print_array(item, output_buffer, hooks); + return print_array(item, output_buffer); case cJSON_Object: - return print_object(item, output_buffer, hooks); + return print_object(item, output_buffer); default: return false; @@ -1298,7 +1304,7 @@ static cJSON_bool print_value(const cJSON * const item, printbuffer * const outp } /* Build an array from input text. */ -static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks) +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) { cJSON *head = NULL; /* head of the linked list */ cJSON *current_item = NULL; @@ -1336,7 +1342,7 @@ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buf do { /* allocate next item */ - cJSON *new_item = cJSON_New_Item(hooks); + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); if (new_item == NULL) { goto fail; /* allocation failure */ @@ -1359,7 +1365,7 @@ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buf /* parse next value */ input_buffer->offset++; buffer_skip_whitespace(input_buffer); - if (!parse_value(current_item, input_buffer, hooks)) + if (!parse_value(current_item, input_buffer)) { goto fail; /* failed to parse value */ } @@ -1392,7 +1398,7 @@ fail: } /* Render an array to text */ -static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) { unsigned char *output_pointer = NULL; size_t length = 0; @@ -1405,7 +1411,7 @@ static cJSON_bool print_array(const cJSON * const item, printbuffer * const outp /* Compose the output array. */ /* opening square bracket */ - output_pointer = ensure(output_buffer, 1, hooks); + output_pointer = ensure(output_buffer, 1); if (output_pointer == NULL) { return false; @@ -1417,7 +1423,7 @@ static cJSON_bool print_array(const cJSON * const item, printbuffer * const outp while (current_element != NULL) { - if (!print_value(current_element, output_buffer, hooks)) + if (!print_value(current_element, output_buffer)) { return false; } @@ -1425,7 +1431,7 @@ static cJSON_bool print_array(const cJSON * const item, printbuffer * const outp if (current_element->next) { length = (size_t) (output_buffer->format ? 2 : 1); - output_pointer = ensure(output_buffer, length + 1, hooks); + output_pointer = ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; @@ -1441,7 +1447,7 @@ static cJSON_bool print_array(const cJSON * const item, printbuffer * const outp current_element = current_element->next; } - output_pointer = ensure(output_buffer, 2, hooks); + output_pointer = ensure(output_buffer, 2); if (output_pointer == NULL) { return false; @@ -1454,7 +1460,7 @@ static cJSON_bool print_array(const cJSON * const item, printbuffer * const outp } /* Build an object from the text. */ -static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer, const internal_hooks * const hooks) +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) { cJSON *head = NULL; /* linked list head */ cJSON *current_item = NULL; @@ -1490,7 +1496,7 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu do { /* allocate next item */ - cJSON *new_item = cJSON_New_Item(hooks); + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); if (new_item == NULL) { goto fail; /* allocation failure */ @@ -1513,7 +1519,7 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu /* parse the name of the child */ input_buffer->offset++; buffer_skip_whitespace(input_buffer); - if (!parse_string(current_item, input_buffer, hooks)) + if (!parse_string(current_item, input_buffer)) { goto fail; /* faile to parse name */ } @@ -1531,7 +1537,7 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu /* parse the value */ input_buffer->offset++; buffer_skip_whitespace(input_buffer); - if (!parse_value(current_item, input_buffer, hooks)) + if (!parse_value(current_item, input_buffer)) { goto fail; /* failed to parse value */ } @@ -1563,7 +1569,7 @@ fail: } /* Render an object to text. */ -static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks) +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) { unsigned char *output_pointer = NULL; size_t length = 0; @@ -1576,7 +1582,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out /* Compose the output: */ length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ - output_pointer = ensure(output_buffer, length + 1, hooks); + output_pointer = ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; @@ -1595,7 +1601,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out if (output_buffer->format) { size_t i; - output_pointer = ensure(output_buffer, output_buffer->depth, hooks); + output_pointer = ensure(output_buffer, output_buffer->depth); if (output_pointer == NULL) { return false; @@ -1608,14 +1614,14 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out } /* print key */ - if (!print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks)) + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) { return false; } update_offset(output_buffer); length = (size_t) (output_buffer->format ? 2 : 1); - output_pointer = ensure(output_buffer, length, hooks); + output_pointer = ensure(output_buffer, length); if (output_pointer == NULL) { return false; @@ -1628,7 +1634,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out output_buffer->offset += length; /* print value */ - if (!print_value(current_item, output_buffer, hooks)) + if (!print_value(current_item, output_buffer)) { return false; } @@ -1636,7 +1642,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out /* print comma if not last */ length = (size_t) ((output_buffer->format ? 1 : 0) + (current_item->next ? 1 : 0)); - output_pointer = ensure(output_buffer, length + 1, hooks); + output_pointer = ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; @@ -1656,7 +1662,7 @@ static cJSON_bool print_object(const cJSON * const item, printbuffer * const out current_item = current_item->next; } - output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2, hooks); + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); if (output_pointer == NULL) { return false; diff --git a/tests/parse_array.c b/tests/parse_array.c index 335d245..69e0f41 100644 --- a/tests/parse_array.c +++ b/tests/parse_array.c @@ -44,21 +44,23 @@ static void assert_is_array(cJSON *array_item) static void assert_not_array(const char *json) { - parse_buffer buffer = { 0, 0, 0, 0 }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; buffer.content = (const unsigned char*)json; buffer.length = strlen(json) + sizeof(""); + buffer.hooks = global_hooks; - TEST_ASSERT_FALSE(parse_array(item, &buffer, &global_hooks)); + TEST_ASSERT_FALSE(parse_array(item, &buffer)); assert_is_invalid(item); } static void assert_parse_array(const char *json) { - parse_buffer buffer = { 0, 0, 0, 0 }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; buffer.content = (const unsigned char*)json; buffer.length = strlen(json) + sizeof(""); + buffer.hooks = global_hooks; - TEST_ASSERT_TRUE(parse_array(item, &buffer, &global_hooks)); + TEST_ASSERT_TRUE(parse_array(item, &buffer)); assert_is_array(item); } diff --git a/tests/parse_number.c b/tests/parse_number.c index 29175bd..f499ab6 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 }; + parse_buffer buffer = { 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 622254d..0985845 100644 --- a/tests/parse_object.c +++ b/tests/parse_object.c @@ -52,22 +52,24 @@ 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 }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)json; parsebuffer.length = strlen(json) + sizeof(""); + parsebuffer.hooks = global_hooks; - TEST_ASSERT_FALSE(parse_object(item, &parsebuffer, &global_hooks)); + TEST_ASSERT_FALSE(parse_object(item, &parsebuffer)); assert_is_invalid(item); reset(item); } static void assert_parse_object(const char *json) { - parse_buffer parsebuffer = { 0, 0, 0, 0 }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)json; parsebuffer.length = strlen(json) + sizeof(""); + parsebuffer.hooks = global_hooks; - TEST_ASSERT_TRUE(parse_object(item, &parsebuffer, &global_hooks)); + TEST_ASSERT_TRUE(parse_object(item, &parsebuffer)); assert_is_object(item); } diff --git a/tests/parse_string.c b/tests/parse_string.c index d03d945..ceb1a89 100644 --- a/tests/parse_string.c +++ b/tests/parse_string.c @@ -45,11 +45,12 @@ 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 }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); + buffer.hooks = global_hooks; - TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer, &global_hooks), "Couldn't parse string."); + TEST_ASSERT_TRUE_MESSAGE(parse_string(item, &buffer), "Couldn't parse string."); assert_is_string(item); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, item->valuestring, "The parsed result isn't as expected."); global_hooks.deallocate(item->valuestring); @@ -58,11 +59,12 @@ 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 }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; buffer.content = (const unsigned char*)string; buffer.length = strlen(string) + sizeof(""); + buffer.hooks = global_hooks; - TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer, &global_hooks), "Malformed string should not be accepted."); + TEST_ASSERT_FALSE_MESSAGE(parse_string(item, &buffer), "Malformed string should not be accepted."); assert_is_invalid(item); } diff --git a/tests/parse_value.c b/tests/parse_value.c index 4910af9..08ec3e7 100644 --- a/tests/parse_value.c +++ b/tests/parse_value.c @@ -43,10 +43,12 @@ 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 }; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; buffer.content = (const unsigned char*) string; buffer.length = strlen(string) + sizeof(""); - TEST_ASSERT_TRUE(parse_value(item, &buffer, &global_hooks)); + buffer.hooks = global_hooks; + + TEST_ASSERT_TRUE(parse_value(item, &buffer)); assert_is_value(item, type); } diff --git a/tests/print_array.c b/tests/print_array.c index 45a076b..4bee17f 100644 --- a/tests/print_array.c +++ b/tests/print_array.c @@ -31,34 +31,37 @@ 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 }; - printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0 }; + printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - parse_buffer parsebuffer = { 0, 0, 0, 0 }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; parsebuffer.content = (const unsigned char*)input; parsebuffer.length = strlen(input) + sizeof(""); + parsebuffer.hooks = global_hooks; /* buffer for formatted printing */ formatted_buffer.buffer = printed_formatted; formatted_buffer.length = sizeof(printed_formatted); formatted_buffer.offset = 0; formatted_buffer.noalloc = true; + formatted_buffer.hooks = global_hooks; /* buffer for unformatted printing */ unformatted_buffer.buffer = printed_unformatted; unformatted_buffer.length = sizeof(printed_unformatted); unformatted_buffer.offset = 0; unformatted_buffer.noalloc = true; + unformatted_buffer.hooks = global_hooks; memset(item, 0, sizeof(item)); - TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer, &global_hooks), "Failed to parse array."); + TEST_ASSERT_TRUE_MESSAGE(parse_array(item, &parsebuffer), "Failed to parse array."); unformatted_buffer.format = false; - TEST_ASSERT_TRUE_MESSAGE(print_array(item, &unformatted_buffer, &global_hooks), "Failed to print unformatted string."); + TEST_ASSERT_TRUE_MESSAGE(print_array(item, &unformatted_buffer), "Failed to print unformatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted array is not correct."); formatted_buffer.format = true; - TEST_ASSERT_TRUE_MESSAGE(print_array(item, &formatted_buffer, &global_hooks), "Failed to print formatted string."); + TEST_ASSERT_TRUE_MESSAGE(print_array(item, &formatted_buffer), "Failed to print formatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted array is not correct."); reset(item); diff --git a/tests/print_number.c b/tests/print_number.c index 68ce4dc..8b5ae1f 100644 --- a/tests/print_number.c +++ b/tests/print_number.c @@ -28,16 +28,17 @@ 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 }; + printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; buffer.noalloc = true; + buffer.hooks = global_hooks; memset(item, 0, sizeof(item)); cJSON_SetNumberValue(item, input); - TEST_ASSERT_TRUE_MESSAGE(print_number(item, &buffer, &global_hooks), "Failed to print number."); + TEST_ASSERT_TRUE_MESSAGE(print_number(item, &buffer), "Failed to print number."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, buffer.buffer, "Printed number is not as expected."); } diff --git a/tests/print_object.c b/tests/print_object.c index d26fd14..70bbb43 100644 --- a/tests/print_object.c +++ b/tests/print_object.c @@ -31,35 +31,38 @@ static void assert_print_object(const char * const expected, const char * const cJSON item[1]; - printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0 }; - printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0 }; - parse_buffer parsebuffer = { 0, 0, 0, 0 }; + printbuffer formatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; /* buffer for parsing */ parsebuffer.content = (const unsigned char*)input; parsebuffer.length = strlen(input) + sizeof(""); + parsebuffer.hooks = global_hooks; /* buffer for formatted printing */ formatted_buffer.buffer = printed_formatted; formatted_buffer.length = sizeof(printed_formatted); formatted_buffer.offset = 0; formatted_buffer.noalloc = true; + formatted_buffer.hooks = global_hooks; /* buffer for unformatted printing */ unformatted_buffer.buffer = printed_unformatted; unformatted_buffer.length = sizeof(printed_unformatted); unformatted_buffer.offset = 0; unformatted_buffer.noalloc = true; + unformatted_buffer.hooks = global_hooks; memset(item, 0, sizeof(item)); - TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer, &global_hooks), "Failed to parse object."); + TEST_ASSERT_TRUE_MESSAGE(parse_object(item, &parsebuffer), "Failed to parse object."); unformatted_buffer.format = false; - TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer, &global_hooks), "Failed to print unformatted string."); + TEST_ASSERT_TRUE_MESSAGE(print_object(item, &unformatted_buffer), "Failed to print unformatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, printed_unformatted, "Unformatted object is not correct."); formatted_buffer.format = true; - TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer, &global_hooks), "Failed to print formatted string."); + TEST_ASSERT_TRUE_MESSAGE(print_object(item, &formatted_buffer), "Failed to print formatted string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed_formatted, "Formatted ojbect is not correct."); reset(item); diff --git a/tests/print_string.c b/tests/print_string.c index 4416d24..83de1e7 100644 --- a/tests/print_string.c +++ b/tests/print_string.c @@ -27,13 +27,14 @@ static void assert_print_string(const char *expected, const char *input) { unsigned char printed[1024]; - printbuffer buffer = { 0, 0, 0, 0, 0, 0 }; + printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; buffer.noalloc = true; + buffer.hooks = global_hooks; - TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer, &global_hooks), "Failed to print string."); + TEST_ASSERT_TRUE_MESSAGE(print_string_ptr((const unsigned char*)input, &buffer), "Failed to print string."); TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, printed, "The printed string isn't as expected."); } diff --git a/tests/print_value.c b/tests/print_value.c index 3e11e5a..d590823 100644 --- a/tests/print_value.c +++ b/tests/print_value.c @@ -32,21 +32,23 @@ static void assert_print_value(const char *input) { unsigned char printed[1024]; cJSON item[1]; - printbuffer buffer = { 0, 0, 0, 0, 0, 0 }; - parse_buffer parsebuffer = { 0, 0, 0, 0 }; + printbuffer buffer = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0 } }; buffer.buffer = printed; buffer.length = sizeof(printed); buffer.offset = 0; buffer.noalloc = true; + buffer.hooks = global_hooks; parsebuffer.content = (const unsigned char*)input; parsebuffer.length = strlen(input) + sizeof(""); + parsebuffer.hooks = global_hooks; memset(item, 0, sizeof(item)); - TEST_ASSERT_TRUE_MESSAGE(parse_value(item, &parsebuffer, &global_hooks), "Failed to parse value."); + TEST_ASSERT_TRUE_MESSAGE(parse_value(item, &parsebuffer), "Failed to parse value."); - TEST_ASSERT_TRUE_MESSAGE(print_value(item, &buffer, &global_hooks), "Failed to print value."); + TEST_ASSERT_TRUE_MESSAGE(print_value(item, &buffer), "Failed to print value."); TEST_ASSERT_EQUAL_STRING_MESSAGE(input, buffer.buffer, "Printed value is not as expected."); reset(item);