diff options
author | shadchin <shadchin@yandex-team.com> | 2022-07-25 13:53:51 +0300 |
---|---|---|
committer | shadchin <shadchin@yandex-team.com> | 2022-07-25 13:53:51 +0300 |
commit | 3935c4ab2dab91b36d5432b0394025fd59fbf656 (patch) | |
tree | 653351e29ae0975d222d7f4c0e73847ec8f04698 /contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/external | |
parent | d4143bfea714226001e5e03652411d994bb6b54d (diff) | |
download | ydb-3935c4ab2dab91b36d5432b0394025fd59fbf656.tar.gz |
Update contrib/libs/aws-sdk-cpp to 1.8.187
Diffstat (limited to 'contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/external')
-rw-r--r-- | contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/external/cjson/cJSON.cpp | 409 |
1 files changed, 286 insertions, 123 deletions
diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/external/cjson/cJSON.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/external/cjson/cJSON.cpp index cc5636cbf8..d21a2e7d86 100644 --- a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/external/cjson/cJSON.cpp +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/external/cjson/cJSON.cpp @@ -43,6 +43,7 @@ #include <stdlib.h> #include <limits.h> #include <ctype.h> +#include <float.h> #ifdef ENABLE_LOCALES #include <locale.h> @@ -58,9 +59,28 @@ #include <aws/core/external/cjson/cJSON.h> /* define our own boolean type */ +// #ifdef true +// #undef true +// #endif // #define true ((cJSON_AS4CPP_bool)1) + +// #ifdef false +// #undef false +// #endif // #define false ((cJSON_AS4CPP_bool)0) +/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ +#ifndef isinf +#define isinf(d) (isnan((d - d)) && !isnan(d)) +#endif +#ifndef isnan +#define isnan(d) (d != d) +#endif + +#ifndef NAN +#define NAN 0.0/0.0 +#endif + typedef struct { const unsigned char *json; size_t position; @@ -72,16 +92,28 @@ CJSON_AS4CPP_PUBLIC(const char *) cJSON_AS4CPP_GetErrorPtr(void) return (const char*) (global_error.json + global_error.position); } -CJSON_AS4CPP_PUBLIC(char *) cJSON_AS4CPP_GetStringValue(cJSON *item) { - if (!cJSON_AS4CPP_IsString(item)) { +CJSON_AS4CPP_PUBLIC(char *) cJSON_AS4CPP_GetStringValue(const cJSON * const item) +{ + if (!cJSON_AS4CPP_IsString(item)) + { return NULL; } return item->valuestring; } +CJSON_AS4CPP_PUBLIC(double) cJSON_AS4CPP_GetNumberValue(const cJSON * const item) +{ + if (!cJSON_AS4CPP_IsNumber(item)) + { + return (double) NAN; + } + + return item->valuedouble; +} + /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ -#if (CJSON_AS4CPP_VERSION_MAJOR != 1) || (CJSON_AS4CPP_VERSION_MINOR != 7) || (CJSON_AS4CPP_VERSION_PATCH != 7) +#if (CJSON_AS4CPP_VERSION_MAJOR != 1) || (CJSON_AS4CPP_VERSION_MINOR != 7) || (CJSON_AS4CPP_VERSION_PATCH != 14) #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #endif @@ -119,22 +151,22 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned typedef struct internal_hooks { - void *(*allocate)(size_t size); - void (*deallocate)(void *pointer); - void *(*reallocate)(void *pointer, size_t size); + void *(CJSON_AS4CPP_CDECL *allocate)(size_t size); + void (CJSON_AS4CPP_CDECL *deallocate)(void *pointer); + void *(CJSON_AS4CPP_CDECL *reallocate)(void *pointer, size_t size); } internal_hooks; #if defined(_MSC_VER) -/* work around MSVC error C2322: '...' address of dillimport '...' is not static */ -static void *internal_malloc(size_t size) +/* work around MSVC error C2322: '...' address of dllimport '...' is not static */ +static void * CJSON_AS4CPP_CDECL internal_malloc(size_t size) { return malloc(size); } -static void internal_free(void *pointer) +static void CJSON_AS4CPP_CDECL internal_free(void *pointer) { free(pointer); } -static void *internal_realloc(void *pointer, size_t size) +static void * CJSON_AS4CPP_CDECL internal_realloc(void *pointer, size_t size) { return realloc(pointer, size); } @@ -144,6 +176,9 @@ static void *internal_realloc(void *pointer, size_t size) #define internal_realloc realloc #endif +/* strlen of character literals resolved at compile time */ +#define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) + static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; static unsigned char* cJSON_AS4CPP_strdup(const unsigned char* string, const internal_hooks * const hooks) @@ -335,7 +370,7 @@ loop_end: { item->valueint = INT_MAX; } - else if (number <= INT_MIN) + else if (number <= (double)INT_MIN) { item->valueint = INT_MIN; } @@ -357,7 +392,7 @@ CJSON_AS4CPP_PUBLIC(double) cJSON_AS4CPP_SetNumberHelper(cJSON *object, double n { object->valueint = INT_MAX; } - else if (number <= INT_MIN) + else if (number <= (double)INT_MIN) { object->valueint = INT_MIN; } @@ -369,6 +404,33 @@ CJSON_AS4CPP_PUBLIC(double) cJSON_AS4CPP_SetNumberHelper(cJSON *object, double n return object->valuedouble = number; } +CJSON_AS4CPP_PUBLIC(char*) cJSON_AS4CPP_SetValuestring(cJSON *object, const char *valuestring) +{ + char *copy = NULL; + /* if object's type is not cJSON_AS4CPP_String or is cJSON_AS4CPP_IsReference, it should not set valuestring */ + if (!(object->type & cJSON_AS4CPP_String) || (object->type & cJSON_AS4CPP_IsReference)) + { + return NULL; + } + if (strlen(valuestring) <= strlen(object->valuestring)) + { + memcpy(object->valuestring, valuestring, strlen(valuestring) + sizeof("")); + return object->valuestring; + } + copy = (char*) cJSON_AS4CPP_strdup((const unsigned char*)valuestring, &global_hooks); + if (copy == NULL) + { + return NULL; + } + if (object->valuestring != NULL) + { + cJSON_AS4CPP_free(object->valuestring); + } + object->valuestring = copy; + + return copy; +} + typedef struct { unsigned char *buffer; @@ -481,6 +543,13 @@ static void update_offset(printbuffer * const buffer) buffer->offset += strlen((const char*)buffer_pointer); } +/* securely comparison of floating-point variables */ +static cJSON_AS4CPP_bool compare_double(double a, double b) +{ + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); +} + /* Render the number nicely from the given item into a string. */ static cJSON_AS4CPP_bool print_number(const cJSON * const item, printbuffer * const output_buffer) { @@ -488,9 +557,9 @@ static cJSON_AS4CPP_bool print_number(const cJSON * const item, printbuffer * co double d = item->valuedouble; int length = 0; size_t i = 0; - unsigned char number_buffer[26]; /* temporary buffer to print the number into */ + unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ unsigned char decimal_point = get_decimal_point(); - double test; + double test = 0.0; if (output_buffer == NULL) { @@ -503,7 +572,7 @@ static cJSON_AS4CPP_bool print_number(const cJSON * const item, printbuffer * co length = sprintf((char*)number_buffer, "%s", item->valuestring); } /* This checks for NaN and Infinity */ - else if ((d * 0) != 0) + else if (isnan(d) || isinf(d)) { length = sprintf((char*)number_buffer, "null"); } @@ -513,7 +582,7 @@ static cJSON_AS4CPP_bool print_number(const cJSON * const item, printbuffer * co length = sprintf((char*)number_buffer, "%1.15g", d); /* Check whether the original double can be recovered */ - if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((double)test != d)) + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) { /* If not, print with 17 decimal places of precision */ length = sprintf((char*)number_buffer, "%1.17g", d); @@ -862,7 +931,7 @@ static cJSON_AS4CPP_bool print_string_ptr(const unsigned char * const input, pri { return false; } - strcpy((char*)output, "\"\""); + memcpy(output, "\"\"", sizeof("\"\"")); return true; } @@ -983,6 +1052,11 @@ static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) return NULL; } + if (cannot_access_at_index(buffer, 0)) + { + return buffer; + } + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) { buffer->offset++; @@ -1012,9 +1086,24 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) return buffer; } -/* Parse an object - create a new root, and populate. */ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_AS4CPP_bool require_null_terminated) { + size_t buffer_length; + + if (NULL == value) + { + return NULL; + } + + /* Adding null character size due to require_null_terminated. */ + buffer_length = strlen(value) + sizeof(""); + + return cJSON_AS4CPP_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); +} + +/* Parse an object - create a new root, and populate. */ +CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_AS4CPP_bool require_null_terminated) +{ parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; cJSON *item = NULL; @@ -1022,13 +1111,13 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_ParseWithOpts(const char *value, const global_error.json = NULL; global_error.position = 0; - if (value == NULL) + if (value == NULL || 0 == buffer_length) { goto fail; } buffer.content = (const unsigned char*)value; - buffer.length = strlen((const char*)value) + sizeof(""); + buffer.length = buffer_length; buffer.offset = 0; buffer.hooks = global_hooks; @@ -1098,7 +1187,12 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_Parse(const char *value) return cJSON_AS4CPP_ParseWithOpts(value, 0, 0); } -#define cjson_min(a, b) ((a < b) ? a : b) +CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_ParseWithLength(const char *value, size_t buffer_length) +{ + return cJSON_AS4CPP_ParseWithLengthOpts(value, buffer_length, 0, 0); +} + +#define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) static unsigned char *print(const cJSON * const item, cJSON_AS4CPP_bool format, const internal_hooks * const hooks) { @@ -1205,20 +1299,20 @@ CJSON_AS4CPP_PUBLIC(char *) cJSON_AS4CPP_PrintBuffered(const cJSON *item, int pr return (char*)p.buffer; } -CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_AS4CPP_bool fmt) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_AS4CPP_bool format) { printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - if ((len < 0) || (buf == NULL)) + if ((length < 0) || (buffer == NULL)) { return false; } - p.buffer = (unsigned char*)buf; - p.length = (size_t)len; + p.buffer = (unsigned char*)buffer; + p.length = (size_t)length; p.offset = 0; p.noalloc = true; - p.format = fmt; + p.format = format; p.hooks = global_hooks; return print_value(item, &p); @@ -1297,7 +1391,7 @@ static cJSON_AS4CPP_bool print_value(const cJSON * const item, printbuffer * con { return false; } - strcpy((char*)output, "null"); + memcpy(output, "null", sizeof("null")); return true; case cJSON_AS4CPP_False: @@ -1306,7 +1400,7 @@ static cJSON_AS4CPP_bool print_value(const cJSON * const item, printbuffer * con { return false; } - strcpy((char*)output, "false"); + memcpy(output, "false", sizeof("false")); return true; case cJSON_AS4CPP_True: @@ -1315,7 +1409,7 @@ static cJSON_AS4CPP_bool print_value(const cJSON * const item, printbuffer * con { return false; } - strcpy((char*)output, "true"); + memcpy(output, "true", sizeof("true")); return true; case cJSON_AS4CPP_Number: @@ -1431,6 +1525,10 @@ static cJSON_AS4CPP_bool parse_array(cJSON * const item, parse_buffer * const in success: input_buffer->depth--; + if (head != NULL) { + head->prev = current_item; + } + item->type = cJSON_AS4CPP_Array; item->child = head; @@ -1603,6 +1701,10 @@ static cJSON_AS4CPP_bool parse_object(cJSON * const item, parse_buffer * const i success: input_buffer->depth--; + if (head != NULL) { + head->prev = current_item; + } + item->type = cJSON_AS4CPP_Object; item->child = head; @@ -1691,7 +1793,7 @@ static cJSON_AS4CPP_bool print_object(const cJSON * const item, printbuffer * co update_offset(output_buffer); /* print comma if not last */ - length = (size_t) ((output_buffer->format ? 1 : 0) + (current_item->next ? 1 : 0)); + length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); output_pointer = ensure(output_buffer, length + 1); if (output_pointer == NULL) { @@ -1797,7 +1899,7 @@ static cJSON *get_object_item(const cJSON * const object, const char * const nam current_element = object->child; if (case_sensitive) { - while ((current_element != NULL) && (strcmp(name, current_element->string) != 0)) + while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) { current_element = current_element->next; } @@ -1810,6 +1912,10 @@ static cJSON *get_object_item(const cJSON * const object, const char * const nam } } + if ((current_element == NULL) || (current_element->string == NULL)) { + return NULL; + } + return current_element; } @@ -1861,35 +1967,39 @@ static cJSON_AS4CPP_bool add_item_to_array(cJSON *array, cJSON *item) { cJSON *child = NULL; - if ((item == NULL) || (array == NULL)) + if ((item == NULL) || (array == NULL) || (array == item)) { return false; } child = array->child; - + /* + * To find the last item in array quickly, we use prev in array + */ if (child == NULL) { /* list is empty, start new one */ array->child = item; + item->prev = item; + item->next = NULL; } else { /* append to the end */ - while (child->next) + if (child->prev) { - child = child->next; + suffix_object(child->prev, item); + array->child->prev = item; } - suffix_object(child, item); } return true; } /* Add item to array/object. */ -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_AddItemToArray(cJSON *array, cJSON *item) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_AddItemToArray(cJSON *array, cJSON *item) { - add_item_to_array(array, item); + return add_item_to_array(array, item); } #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) @@ -1913,7 +2023,7 @@ static cJSON_AS4CPP_bool add_item_to_object(cJSON * const object, const char * c char *new_key = NULL; int new_type = cJSON_AS4CPP_Invalid; - if ((object == NULL) || (string == NULL) || (item == NULL)) + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) { return false; } @@ -1945,35 +2055,35 @@ static cJSON_AS4CPP_bool add_item_to_object(cJSON * const object, const char * c return add_item_to_array(object, item); } -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_AddItemToObject(cJSON *object, const char *string, cJSON *item) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_AddItemToObject(cJSON *object, const char *string, cJSON *item) { - add_item_to_object(object, string, item, &global_hooks, false); + return add_item_to_object(object, string, item, &global_hooks, false); } /* Add an item to an object with constant string as key */ -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) { - add_item_to_object(object, string, item, &global_hooks, true); + return add_item_to_object(object, string, item, &global_hooks, true); } -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_AddItemReferenceToArray(cJSON *array, cJSON *item) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_AddItemReferenceToArray(cJSON *array, cJSON *item) { if (array == NULL) { - return; + return false; } - add_item_to_array(array, create_reference(item, &global_hooks)); + return add_item_to_array(array, create_reference(item, &global_hooks)); } -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) { if ((object == NULL) || (string == NULL)) { - return; + return false; } - add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); } CJSON_AS4CPP_PUBLIC(cJSON*) cJSON_AS4CPP_AddNullToObject(cJSON * const object, const char * const name) @@ -2091,7 +2201,7 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_DetachItemViaPointer(cJSON *parent, cJ return NULL; } - if (item->prev != NULL) + if (item != parent->child) { /* not the first element */ item->prev->next = item->next; @@ -2107,6 +2217,12 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_DetachItemViaPointer(cJSON *parent, cJ /* first element */ parent->child = item->next; } + else if (item->next == NULL) + { + /* last element */ + parent->child->prev = item->prev; + } + /* make sure the detached item doesn't point anywhere anymore */ item->prev = NULL; item->next = NULL; @@ -2154,20 +2270,19 @@ CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_DeleteItemFromObjectCaseSensitive(cJSON * } /* Replace array/object items with new ones. */ -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_InsertItemInArray(cJSON *array, int which, cJSON *newitem) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_InsertItemInArray(cJSON *array, int which, cJSON *newitem) { cJSON *after_inserted = NULL; if (which < 0) { - return; + return false; } after_inserted = get_array_item(array, (size_t)which); if (after_inserted == NULL) { - add_item_to_array(array, newitem); - return; + return add_item_to_array(array, newitem); } newitem->next = after_inserted; @@ -2181,6 +2296,7 @@ CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_InsertItemInArray(cJSON *array, int which { newitem->prev->next = newitem; } + return true; } CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) @@ -2202,14 +2318,28 @@ CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_ReplaceItemViaPointer(cJSON { replacement->next->prev = replacement; } - if (replacement->prev != NULL) - { - replacement->prev->next = replacement; - } if (parent->child == item) { + if (parent->child->prev == parent->child) + { + replacement->prev = replacement; + } parent->child = replacement; } + else + { /* + * To find the last item in array quickly, we use prev in array. + * We can't modify the last item's next pointer where this item was the parent's child + */ + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (replacement->next == NULL) + { + parent->child->prev = replacement; + } + } item->next = NULL; item->prev = NULL; @@ -2218,14 +2348,14 @@ CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_ReplaceItemViaPointer(cJSON return true; } -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { if (which < 0) { - return; + return false; } - cJSON_AS4CPP_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); + return cJSON_AS4CPP_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); } static cJSON_AS4CPP_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_AS4CPP_bool case_sensitive) @@ -2243,19 +2373,17 @@ static cJSON_AS4CPP_bool replace_item_in_object(cJSON *object, const char *strin replacement->string = (char*)cJSON_AS4CPP_strdup((const unsigned char*)string, &global_hooks); replacement->type &= ~cJSON_AS4CPP_StringIsConst; - cJSON_AS4CPP_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); - - return true; + return cJSON_AS4CPP_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); } -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) { - replace_item_in_object(object, string, newitem, false); + return replace_item_in_object(object, string, newitem, false); } -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) +CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) { - replace_item_in_object(object, string, newitem, true); + return replace_item_in_object(object, string, newitem, true); } /* Create basic types: */ @@ -2292,12 +2420,12 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateFalse(void) return item; } -CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateBool(cJSON_AS4CPP_bool b) +CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateBool(cJSON_AS4CPP_bool boolean) { cJSON *item = cJSON_AS4CPP_New_Item(&global_hooks); if(item) { - item->type = b ? cJSON_AS4CPP_True : cJSON_AS4CPP_False; + item->type = boolean ? cJSON_AS4CPP_True : cJSON_AS4CPP_False; } return item; @@ -2316,7 +2444,7 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateNumber(double num) { item->valueint = INT_MAX; } - else if (num <= INT_MIN) + else if (num <= (double)INT_MIN) { item->valueint = INT_MIN; } @@ -2485,6 +2613,7 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateIntArray(const int *numbers, int } p = n; } + a->child->prev = n; return a; } @@ -2521,6 +2650,7 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateFloatArray(const float *numbers, } p = n; } + a->child->prev = n; return a; } @@ -2557,11 +2687,12 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateDoubleArray(const double *number } p = n; } + a->child->prev = n; return a; } -CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateStringArray(const char **strings, int count) +CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateStringArray(const char *const *strings, int count) { size_t i = 0; cJSON *n = NULL; @@ -2593,6 +2724,7 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_CreateStringArray(const char **strings } p = n; } + a->child->prev = n; return a; } @@ -2665,6 +2797,10 @@ CJSON_AS4CPP_PUBLIC(cJSON *) cJSON_AS4CPP_Duplicate(const cJSON *item, cJSON_AS4 } child = child->next; } + if (newitem && newitem->child) + { + newitem->child->prev = newchild; + } return newitem; @@ -2677,69 +2813,96 @@ fail: return NULL; } -CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_Minify(char *json) +static void skip_oneline_comment(char **input) { - unsigned char *into = (unsigned char*)json; + *input += static_strlen("//"); - if (json == NULL) + for (; (*input)[0] != '\0'; ++(*input)) { - return; + if ((*input)[0] == '\n') { + *input += static_strlen("\n"); + return; + } } +} + +static void skip_multiline_comment(char **input) +{ + *input += static_strlen("/*"); - while (*json) + for (; (*input)[0] != '\0'; ++(*input)) { - if (*json == ' ') - { - json++; - } - else if (*json == '\t') - { - /* Whitespace characters. */ - json++; - } - else if (*json == '\r') - { - json++; - } - else if (*json=='\n') + if (((*input)[0] == '*') && ((*input)[1] == '/')) { - json++; + *input += static_strlen("*/"); + return; } - else if ((*json == '/') && (json[1] == '/')) - { - /* double-slash comments, to end of line. */ - while (*json && (*json != '\n')) - { - json++; - } + } +} + +static void minify_string(char **input, char **output) { + (*output)[0] = (*input)[0]; + *input += static_strlen("\""); + *output += static_strlen("\""); + + + for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { + (*output)[0] = (*input)[0]; + + if ((*input)[0] == '\"') { + (*output)[0] = '\"'; + *input += static_strlen("\""); + *output += static_strlen("\""); + return; + } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { + (*output)[1] = (*input)[1]; + *input += static_strlen("\""); + *output += static_strlen("\""); } - else if ((*json == '/') && (json[1] == '*')) + } +} + +CJSON_AS4CPP_PUBLIC(void) cJSON_AS4CPP_Minify(char *json) +{ + char *into = json; + + if (json == NULL) + { + return; + } + + while (json[0] != '\0') + { + switch (json[0]) { - /* multiline comments. */ - while (*json && !((*json == '*') && (json[1] == '/'))) - { + case ' ': + case '\t': + case '\r': + case '\n': json++; - } - json += 2; - } - else if (*json == '\"') - { - /* string literals, which are \" sensitive. */ - *into++ = (unsigned char)*json++; - while (*json && (*json != '\"')) - { - if (*json == '\\') + break; + + case '/': + if (json[1] == '/') { - *into++ = (unsigned char)*json++; + skip_oneline_comment(&json); } - *into++ = (unsigned char)*json++; - } - *into++ = (unsigned char)*json++; - } - else - { - /* All other characters. */ - *into++ = (unsigned char)*json++; + else if (json[1] == '*') + { + skip_multiline_comment(&json); + } else { + json++; + } + break; + + case '\"': + minify_string(&json, (char**)&into); + break; + + default: + into[0] = json[0]; + json++; + into++; } } @@ -2886,7 +3049,7 @@ CJSON_AS4CPP_PUBLIC(cJSON_AS4CPP_bool) cJSON_AS4CPP_Compare(const cJSON * const return true; case cJSON_AS4CPP_Number: - if (a->valuedouble == b->valuedouble) + if (compare_double(a->valuedouble, b->valuedouble)) { return true; } |