Put buffer_size into internal_configuration

This commit is contained in:
Max Bruckner 2018-02-01 00:00:36 +01:00
parent 27977adc93
commit 7030dc7c5b
12 changed files with 37 additions and 46 deletions

41
cJSON.c
View File

@ -118,6 +118,7 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned
typedef struct internal_configuration typedef struct internal_configuration
{ {
size_t buffer_size;
cJSON_bool format; cJSON_bool format;
void *(*allocate)(size_t size); void *(*allocate)(size_t size);
void (*deallocate)(void *pointer); void (*deallocate)(void *pointer);
@ -144,7 +145,13 @@ static void *internal_realloc(void *pointer, size_t size)
#define internal_realloc realloc #define internal_realloc realloc
#endif #endif
static internal_configuration global_configuration = { true, internal_malloc, internal_free, internal_realloc }; static internal_configuration global_configuration = {
256, /* default buffer size */
true, /* enable formatting by default */
internal_malloc,
internal_free,
internal_realloc
};
static unsigned char* custom_strdup(const unsigned char* string, const internal_configuration * const configuration) static unsigned char* custom_strdup(const unsigned char* string, const internal_configuration * const configuration)
{ {
@ -994,7 +1001,7 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
/* Parse an object - create a new root, and populate. */ /* 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) 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, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
cJSON *item = NULL; cJSON *item = NULL;
/* reset error position */ /* reset error position */
@ -1081,15 +1088,14 @@ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
static unsigned char *print(const cJSON * const item, const internal_configuration * const configuration) static unsigned char *print(const cJSON * const item, const internal_configuration * const configuration)
{ {
static const size_t default_buffer_size = 256;
printbuffer buffer[1]; printbuffer buffer[1];
unsigned char *printed = NULL; unsigned char *printed = NULL;
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
/* create buffer */ /* create buffer */
buffer->buffer = (unsigned char*) configuration->allocate(default_buffer_size); buffer->buffer = (unsigned char*) configuration->allocate(configuration->buffer_size);
buffer->length = default_buffer_size; buffer->length = configuration->buffer_size;
buffer->configuration = *configuration; buffer->configuration = *configuration;
if (buffer->buffer == NULL) if (buffer->buffer == NULL)
{ {
@ -1159,37 +1165,22 @@ CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool format) CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool format)
{ {
printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; internal_configuration configuration = global_configuration;
if (prebuffer < 0) if (prebuffer < 0)
{ {
return NULL; return NULL;
} }
p.buffer = (unsigned char*)global_configuration.allocate((size_t)prebuffer); configuration.buffer_size = (size_t)prebuffer;
if (!p.buffer) configuration.format = format;
{
return NULL;
}
p.length = (size_t)prebuffer; return (char*)print(item, &configuration);
p.offset = 0;
p.noalloc = false;
p.configuration = global_configuration;
p.configuration.format = format;
if (!print_value(item, &p))
{
global_configuration.deallocate(p.buffer);
return NULL;
}
return (char*)p.buffer;
} }
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) 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 } }; printbuffer p = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
if ((length < 0) || (buffer == NULL)) if ((length < 0) || (buffer == NULL))
{ {

View File

@ -418,7 +418,7 @@ static void *failing_realloc(void *pointer, size_t size)
static void ensure_should_fail_on_failed_realloc(void) static void ensure_should_fail_on_failed_realloc(void)
{ {
printbuffer buffer = {NULL, 10, 0, 0, false, {false, &malloc, &free, &failing_realloc}}; printbuffer buffer = {NULL, 10, 0, 0, false, {256, false, &malloc, &free, &failing_realloc}};
buffer.buffer = (unsigned char*)malloc(100); buffer.buffer = (unsigned char*)malloc(100);
TEST_ASSERT_NOT_NULL(buffer.buffer); TEST_ASSERT_NOT_NULL(buffer.buffer);
@ -428,7 +428,7 @@ static void ensure_should_fail_on_failed_realloc(void)
static void skip_utf8_bom_should_skip_bom(void) static void skip_utf8_bom_should_skip_bom(void)
{ {
const unsigned char string[] = "\xEF\xBB\xBF{}"; const unsigned char string[] = "\xEF\xBB\xBF{}";
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.content = string; buffer.content = string;
buffer.length = sizeof(string); buffer.length = sizeof(string);
buffer.configuration = global_configuration; buffer.configuration = global_configuration;
@ -440,7 +440,7 @@ static void skip_utf8_bom_should_skip_bom(void)
static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void) static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
{ {
const unsigned char string[] = " \xEF\xBB\xBF{}"; const unsigned char string[] = " \xEF\xBB\xBF{}";
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.content = string; buffer.content = string;
buffer.length = sizeof(string); buffer.length = sizeof(string);
buffer.configuration = global_configuration; buffer.configuration = global_configuration;

View File

@ -44,7 +44,7 @@ static void assert_is_array(cJSON *array_item)
static void assert_not_array(const char *json) static void assert_not_array(const char *json)
{ {
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.content = (const unsigned char*)json; buffer.content = (const unsigned char*)json;
buffer.length = strlen(json) + sizeof(""); buffer.length = strlen(json) + sizeof("");
buffer.configuration = global_configuration; buffer.configuration = global_configuration;
@ -55,7 +55,7 @@ static void assert_not_array(const char *json)
static void assert_parse_array(const char *json) static void assert_parse_array(const char *json)
{ {
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.content = (const unsigned char*)json; buffer.content = (const unsigned char*)json;
buffer.length = strlen(json) + sizeof(""); buffer.length = strlen(json) + sizeof("");
buffer.configuration = global_configuration; buffer.configuration = global_configuration;

View File

@ -45,7 +45,7 @@ static void assert_is_number(cJSON *number_item)
static void assert_parse_number(const char *string, int integer, double real) static void assert_parse_number(const char *string, int integer, double real)
{ {
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.content = (const unsigned char*)string; buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof(""); buffer.length = strlen(string) + sizeof("");

View File

@ -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) static void assert_not_object(const char *json)
{ {
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)json; parsebuffer.content = (const unsigned char*)json;
parsebuffer.length = strlen(json) + sizeof(""); parsebuffer.length = strlen(json) + sizeof("");
parsebuffer.configuration = global_configuration; parsebuffer.configuration = global_configuration;
@ -64,7 +64,7 @@ static void assert_not_object(const char *json)
static void assert_parse_object(const char *json) static void assert_parse_object(const char *json)
{ {
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)json; parsebuffer.content = (const unsigned char*)json;
parsebuffer.length = strlen(json) + sizeof(""); parsebuffer.length = strlen(json) + sizeof("");
parsebuffer.configuration = global_configuration; parsebuffer.configuration = global_configuration;

View File

@ -45,7 +45,7 @@ static void assert_is_string(cJSON *string_item)
static void assert_parse_string(const char *string, const char *expected) static void assert_parse_string(const char *string, const char *expected)
{ {
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.content = (const unsigned char*)string; buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof(""); buffer.length = strlen(string) + sizeof("");
buffer.configuration = global_configuration; 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) static void assert_not_parse_string(const char * const string)
{ {
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.content = (const unsigned char*)string; buffer.content = (const unsigned char*)string;
buffer.length = strlen(string) + sizeof(""); buffer.length = strlen(string) + sizeof("");
buffer.configuration = global_configuration; buffer.configuration = global_configuration;

View File

@ -43,7 +43,7 @@ static void assert_is_value(cJSON *value_item, int type)
static void assert_parse_value(const char *string, int type) static void assert_parse_value(const char *string, int type)
{ {
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.content = (const unsigned char*) string; buffer.content = (const unsigned char*) string;
buffer.length = strlen(string) + sizeof(""); buffer.length = strlen(string) + sizeof("");
buffer.configuration = global_configuration; buffer.configuration = global_configuration;

View File

@ -31,10 +31,10 @@ static void assert_print_array(const char * const expected, const char * const i
cJSON item[1]; cJSON item[1];
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; 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 } }; printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
parsebuffer.content = (const unsigned char*)input; parsebuffer.content = (const unsigned char*)input;
parsebuffer.length = strlen(input) + sizeof(""); parsebuffer.length = strlen(input) + sizeof("");
parsebuffer.configuration = global_configuration; parsebuffer.configuration = global_configuration;

View File

@ -28,7 +28,7 @@ static void assert_print_number(const char *expected, double input)
{ {
unsigned char printed[1024]; unsigned char printed[1024];
cJSON item[1]; cJSON item[1];
printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.buffer = printed; buffer.buffer = printed;
buffer.length = sizeof(printed); buffer.length = sizeof(printed);
buffer.offset = 0; buffer.offset = 0;

View File

@ -31,9 +31,9 @@ static void assert_print_object(const char * const expected, const char * const
cJSON item[1]; cJSON item[1];
printbuffer formatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; 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 } }; printbuffer unformatted_buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
/* buffer for parsing */ /* buffer for parsing */
parsebuffer.content = (const unsigned char*)input; parsebuffer.content = (const unsigned char*)input;

View File

@ -27,7 +27,7 @@
static void assert_print_string(const char *expected, const char *input) static void assert_print_string(const char *expected, const char *input)
{ {
unsigned char printed[1024]; unsigned char printed[1024];
printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.buffer = printed; buffer.buffer = printed;
buffer.length = sizeof(printed); buffer.length = sizeof(printed);
buffer.offset = 0; buffer.offset = 0;

View File

@ -32,8 +32,8 @@ static void assert_print_value(const char *input)
{ {
unsigned char printed[1024]; unsigned char printed[1024];
cJSON item[1]; cJSON item[1];
printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0 } }; printbuffer buffer = { 0, 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0 } }; parse_buffer parsebuffer = { 0, 0, 0, 0, { 0, 0, 0, 0, 0 } };
buffer.buffer = printed; buffer.buffer = printed;
buffer.length = sizeof(printed); buffer.length = sizeof(printed);
buffer.offset = 0; buffer.offset = 0;