Lines Matching +full:json +full:- +full:buffer
2 Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
24 /* JSON parser in C. */
78 #define isinf(d) (isnan((d - d)) && !isnan(d))
86 #define NAN sqrt(-1.0)
93 const unsigned char *json; member
98 CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) { return (const char *)(global_error.json + glob… in cJSON_GetErrorPtr()
105 return item->valuestring; in loader_cJSON_GetStringValue()
113 return item->valuedouble; in loader_cJSON_GetNumberValue()
116 /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
137 return tolower(*string1) - tolower(*string2); in case_insensitive_strcmp()
141 #define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
147 node->pAllocator = pAllocator; in cJSON_New_Item()
156 next = item->next; in CJSON_PUBLIC()
157 if (!(item->type & cJSON_IsReference) && (item->child != NULL)) { in CJSON_PUBLIC()
158 loader_cJSON_Delete(item->child); in CJSON_PUBLIC()
160 if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) { in CJSON_PUBLIC()
161 loader_free(item->pAllocator, item->valuestring); in CJSON_PUBLIC()
162 item->valuestring = NULL; in CJSON_PUBLIC()
164 if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) { in CJSON_PUBLIC()
165 loader_free(item->pAllocator, item->string); in CJSON_PUBLIC()
166 item->string = NULL; in CJSON_PUBLIC()
168 loader_free(item->pAllocator, item); in CJSON_PUBLIC()
177 return (unsigned char)lconv->decimal_point[0]; in get_decimal_point()
191 /* check if the given size is left to read in a given parse buffer (starting with 1) */
192 #define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) argument
193 /* check if the buffer can be accessed at the given index (starting with 0) */
194 #define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buff… argument
195 #define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) argument
196 /* get a pointer to the buffer at the position */
197 #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) argument
207 if ((input_buffer == NULL) || (input_buffer->content == NULL)) { in parse_number()
211 /* copy the number into a temporary buffer and replace '.' with the decimal point in parse_number()
214 for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) { in parse_number()
227 case '-': in parse_number()
249 item->valuedouble = number; in parse_number()
253 item->valueint = INT_MAX; in parse_number()
255 item->valueint = INT_MIN; in parse_number()
257 item->valueint = (int)number; in parse_number()
260 item->type = cJSON_Number; in parse_number()
262 input_buffer->offset += (size_t)(after_end - number_c_string); in parse_number()
267 unsigned char *buffer; member
281 if ((p == NULL) || (p->buffer == NULL)) { in ensure()
285 if ((p->length > 0) && (p->offset >= p->length)) { in ensure()
295 needed += p->offset + 1; in ensure()
296 if (needed <= p->length) { in ensure()
297 return p->buffer + p->offset; in ensure()
300 if (p->noalloc) { in ensure()
304 /* calculate new buffer size */ in ensure()
316 …newbuffer = (unsigned char *)loader_realloc(p->pAllocator, p->buffer, p->length, newsize, VK_SYSTE… in ensure()
319 loader_free(p->pAllocator, p->buffer); in ensure()
320 p->length = 0; in ensure()
321 p->buffer = NULL; in ensure()
326 p->length = newsize; in ensure()
327 p->buffer = newbuffer; in ensure()
329 return newbuffer + p->offset; in ensure()
333 static void update_offset(printbuffer *const buffer) { in update_offset() argument
335 if ((buffer == NULL) || (buffer->buffer == NULL)) { in update_offset()
338 buffer_pointer = buffer->buffer + buffer->offset; in update_offset()
340 buffer->offset += strlen((const char *)buffer_pointer); in update_offset()
343 /* securely comparison of floating-point variables */
346 return (fabs(a - b) <= maxVal * DBL_EPSILON); in compare_double()
352 double d = item->valuedouble; in print_number()
355 unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ in print_number()
366 } else if (d == (double)item->valueint) { in print_number()
367 length = snprintf((char *)number_buffer, 26, "%d", item->valueint); in print_number()
379 /* snprintf failed or buffer overrun occurred */ in print_number()
380 if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) { in print_number()
402 output_buffer->offset += (size_t)length; in print_number()
415 h += (unsigned int)input[i] - '0'; in parse_hex4()
417 h += (unsigned int)10 + input[i] - 'A'; in parse_hex4()
419 h += (unsigned int)10 + input[i] - 'a'; in parse_hex4()
434 /* converts a UTF-16 literal to UTF-8
446 if ((input_end - first_sequence) < 6) { in utf16_literal_to_utf8()
465 if ((input_end - second_sequence) < 6) { in utf16_literal_to_utf8()
490 /* encode as UTF-8 in utf16_literal_to_utf8()
514 for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) { in utf16_literal_to_utf8()
550 …while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'… in parse_string()
553 if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) { in parse_string()
554 /* prevent buffer overflow when last input character is a backslash */ in parse_string()
562 …if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))… in parse_string()
567 allocation_length = (size_t)(input_end - buffer_at_offset(input_buffer)) - skipped_bytes; in parse_string()
568 … output = (unsigned char *)loader_calloc(input_buffer->pAllocator, allocation_length + sizeof(""), in parse_string()
585 if ((input_end - input_pointer) < 1) { in parse_string()
611 /* UTF-16 literal */ in parse_string()
615 /* failed to convert UTF16-literal to UTF-8 */ in parse_string()
630 item->type = cJSON_String; in parse_string()
631 item->valuestring = (char *)output; in parse_string()
633 input_buffer->offset = (size_t)(input_end - input_buffer->content); in parse_string()
634 input_buffer->offset++; in parse_string()
640 loader_free(input_buffer->pAllocator, output); in parse_string()
645 input_buffer->offset = (size_t)(input_pointer - input_buffer->content); in parse_string()
689 /* UTF-16 escape sequence uXXXX */ in print_string_ptr()
695 output_length = (size_t)(input_pointer - input) + escape_characters; in print_string_ptr()
717 …// Loader specific modification - don't add a backslash because that will 'double up' any existing… in print_string_ptr()
746 …snprintf((char *)output_pointer, output_length - (size_t)(output_pointer - output), "u%04x", *inpu… in print_string_ptr()
759 return print_string_ptr((unsigned char *)item->valuestring, p, out_of_memory); in print_string()
771 static parse_buffer *buffer_skip_whitespace(parse_buffer *const buffer) { in buffer_skip_whitespace() argument
772 if ((buffer == NULL) || (buffer->content == NULL)) { in buffer_skip_whitespace()
776 if (cannot_access_at_index(buffer, 0)) { in buffer_skip_whitespace()
777 return buffer; in buffer_skip_whitespace()
780 while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) { in buffer_skip_whitespace()
781 buffer->offset++; in buffer_skip_whitespace()
784 if (buffer->offset == buffer->length) { in buffer_skip_whitespace()
785 buffer->offset--; in buffer_skip_whitespace()
788 return buffer; in buffer_skip_whitespace()
791 /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */
792 static parse_buffer *skip_utf8_bom(parse_buffer *const buffer) { in skip_utf8_bom() argument
793 if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) { in skip_utf8_bom()
797 …if (can_access_at_index(buffer, 4) && (strncmp((const char *)buffer_at_offset(buffer), "\xEF\xBB\x… in skip_utf8_bom()
798 buffer->offset += 3; in skip_utf8_bom()
801 return buffer; in skip_utf8_bom()
820 /* Parse an object - create a new root, and populate. */
824 parse_buffer buffer = {0, 0, 0, 0, 0}; in loader_cJSON_ParseWithLengthOpts() local
828 global_error.json = NULL; in loader_cJSON_ParseWithLengthOpts()
835 buffer.content = (const unsigned char *)value; in loader_cJSON_ParseWithLengthOpts()
836 buffer.length = buffer_length; in loader_cJSON_ParseWithLengthOpts()
837 buffer.offset = 0; in loader_cJSON_ParseWithLengthOpts()
838 buffer.pAllocator = pAllocator; in loader_cJSON_ParseWithLengthOpts()
847 if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)), out_of_memory)) { in loader_cJSON_ParseWithLengthOpts()
852 …/* if we require null-terminated JSON without appended garbage, skip and then check for a null ter… in loader_cJSON_ParseWithLengthOpts()
854 buffer_skip_whitespace(&buffer); in loader_cJSON_ParseWithLengthOpts()
855 if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') { in loader_cJSON_ParseWithLengthOpts()
860 *return_parse_end = (const char *)buffer_at_offset(&buffer); in loader_cJSON_ParseWithLengthOpts()
872 local_error.json = (const unsigned char *)value; in loader_cJSON_ParseWithLengthOpts()
875 if (buffer.offset < buffer.length) { in loader_cJSON_ParseWithLengthOpts()
876 local_error.position = buffer.offset; in loader_cJSON_ParseWithLengthOpts()
877 } else if (buffer.length > 0) { in loader_cJSON_ParseWithLengthOpts()
878 local_error.position = buffer.length - 1; in loader_cJSON_ParseWithLengthOpts()
882 *return_parse_end = (const char *)local_error.json + local_error.position; in loader_cJSON_ParseWithLengthOpts()
906 printbuffer buffer[1]; in print() local
909 memset(buffer, 0, sizeof(buffer)); in print()
911 /* create buffer */ in print()
912 …buffer->buffer = (unsigned char *)loader_calloc(item->pAllocator, default_buffer_size, VK_SYSTEM_A… in print()
913 buffer->length = default_buffer_size; in print()
914 buffer->format = format; in print()
915 buffer->pAllocator = item->pAllocator; in print()
916 if (buffer->buffer == NULL) { in print()
922 if (!print_value(item, buffer, out_of_memory)) { in print()
925 update_offset(buffer); in print()
927 …printed = (unsigned char *)loader_realloc(item->pAllocator, buffer->buffer, buffer->length, buffer… in print()
933 buffer->buffer = NULL; in print()
938 if (buffer->buffer != NULL) { in print()
939 loader_free(item->pAllocator, buffer->buffer); in print()
940 buffer->buffer = NULL; in print()
944 loader_free(item->pAllocator, printed); in print()
968 …p.buffer = (unsigned char *)loader_alloc(item->pAllocator, (size_t)prebuffer, VK_SYSTEM_ALLOCATION… in loader_cJSON_PrintBuffered()
969 if (!p.buffer) { in loader_cJSON_PrintBuffered()
977 p.pAllocator = item->pAllocator; in loader_cJSON_PrintBuffered()
980 loader_free(item->pAllocator, p.buffer); in loader_cJSON_PrintBuffered()
981 p.buffer = NULL; in loader_cJSON_PrintBuffered()
985 return (char *)p.buffer; in loader_cJSON_PrintBuffered()
989 loader_cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format… in loader_cJSON_PrintPreallocated() argument
992 if ((length < 0) || (buffer == NULL)) { in loader_cJSON_PrintPreallocated()
996 p.buffer = (unsigned char *)buffer; in loader_cJSON_PrintPreallocated()
1001 p.pAllocator = item->pAllocator; in loader_cJSON_PrintPreallocated()
1006 /* Parser core - when encountering text, process appropriately. */
1008 if ((input_buffer == NULL) || (input_buffer->content == NULL)) { in parse_value()
1015 item->type = cJSON_NULL; in parse_value()
1016 input_buffer->offset += 4; in parse_value()
1021 item->type = cJSON_False; in parse_value()
1022 input_buffer->offset += 5; in parse_value()
1027 item->type = cJSON_True; in parse_value()
1028 item->valueint = 1; in parse_value()
1029 input_buffer->offset += 4; in parse_value()
1038 ((buffer_at_offset(input_buffer)[0] == '-') || in parse_value()
1062 switch ((item->type) & 0xFF) { in print_value()
1092 if (item->valuestring == NULL) { in print_value()
1096 raw_length = strlen(item->valuestring) + sizeof(""); in print_value()
1101 memcpy(output, item->valuestring, raw_length); in print_value()
1124 if (input_buffer->depth >= CJSON_NESTING_LIMIT) { in parse_array()
1127 input_buffer->depth++; in parse_array()
1134 input_buffer->offset++; in parse_array()
1141 /* check if we skipped to the end of the buffer */ in parse_array()
1143 input_buffer->offset--; in parse_array()
1148 input_buffer->offset--; in parse_array()
1152 cJSON *new_item = cJSON_New_Item(input_buffer->pAllocator); in parse_array()
1164 current_item->next = new_item; in parse_array()
1165 new_item->prev = current_item; in parse_array()
1170 input_buffer->offset++; in parse_array()
1183 input_buffer->depth--; in parse_array()
1186 head->prev = current_item; in parse_array()
1189 item->type = cJSON_Array; in parse_array()
1190 item->child = head; in parse_array()
1192 input_buffer->offset++; in parse_array()
1208 cJSON *current_element = item->child; in print_array()
1222 output_buffer->offset++; in print_array()
1223 output_buffer->depth++; in print_array()
1230 if (current_element->next) { in print_array()
1231 length = (size_t)(output_buffer->format ? 2 : 1); in print_array()
1237 if (output_buffer->format) { in print_array()
1241 output_buffer->offset += length; in print_array()
1243 current_element = current_element->next; in print_array()
1252 output_buffer->depth--; in print_array()
1262 if (input_buffer->depth >= CJSON_NESTING_LIMIT) { in parse_object()
1265 input_buffer->depth++; in parse_object()
1271 input_buffer->offset++; in parse_object()
1277 /* check if we skipped to the end of the buffer */ in parse_object()
1279 input_buffer->offset--; in parse_object()
1284 input_buffer->offset--; in parse_object()
1288 cJSON *new_item = cJSON_New_Item(input_buffer->pAllocator); in parse_object()
1300 current_item->next = new_item; in parse_object()
1301 new_item->prev = current_item; in parse_object()
1310 input_buffer->offset++; in parse_object()
1318 current_item->string = current_item->valuestring; in parse_object()
1319 current_item->valuestring = NULL; in parse_object()
1326 input_buffer->offset++; in parse_object()
1339 input_buffer->depth--; in parse_object()
1342 head->prev = current_item; in parse_object()
1345 item->type = cJSON_Object; in parse_object()
1346 item->child = head; in parse_object()
1348 input_buffer->offset++; in parse_object()
1363 cJSON *current_item = item->child; in print_object()
1370 length = (size_t)(output_buffer->format ? 2 : 1); /* fmt: {\n */ in print_object()
1377 output_buffer->depth++; in print_object()
1378 if (output_buffer->format) { in print_object()
1381 output_buffer->offset += length; in print_object()
1384 if (output_buffer->format) { in print_object()
1386 output_pointer = ensure(output_buffer, output_buffer->depth, out_of_memory); in print_object()
1390 for (i = 0; i < output_buffer->depth; i++) { in print_object()
1393 output_buffer->offset += output_buffer->depth; in print_object()
1397 … if (!print_string_ptr((unsigned char *)current_item->string, output_buffer, out_of_memory)) { in print_object()
1402 length = (size_t)(output_buffer->format ? 2 : 1); in print_object()
1408 if (output_buffer->format) { in print_object()
1411 output_buffer->offset += length; in print_object()
1420 length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); in print_object()
1425 if (current_item->next) { in print_object()
1429 if (output_buffer->format) { in print_object()
1433 output_buffer->offset += length; in print_object()
1435 current_item = current_item->next; in print_object()
1438 …output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2, out… in print_object()
1442 if (output_buffer->format) { in print_object()
1444 for (i = 0; i < (output_buffer->depth - 1); i++) { in print_object()
1450 output_buffer->depth--; in print_object()
1464 child = array->child; in loader_cJSON_GetArraySize()
1468 child = child->next; in loader_cJSON_GetArraySize()
1483 current_child = array->child; in get_array_item()
1485 index--; in get_array_item()
1486 current_child = current_child->next; in get_array_item()
1507 current_element = object->child; in get_object_item()
1509 …hile ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_elem… in get_object_item()
1510 current_element = current_element->next; in get_object_item()
1514 …tive_strcmp((const unsigned char *)name, (const unsigned char *)(current_element->string)) != 0)) { in get_object_item()
1515 current_element = current_element->next; in get_object_item()
1519 if ((current_element == NULL) || (current_element->string == NULL)) { in get_object_item()
1581 CJSON_PUBLIC(void) loader_cJSON_Minify(char *json) { in loader_cJSON_Minify() argument
1582 char *into = json; in loader_cJSON_Minify()
1584 if (json == NULL) { in loader_cJSON_Minify()
1588 while (json[0] != '\0') { in loader_cJSON_Minify()
1589 switch (json[0]) { in loader_cJSON_Minify()
1594 json++; in loader_cJSON_Minify()
1598 if (json[1] == '/') { in loader_cJSON_Minify()
1599 skip_oneline_comment(&json); in loader_cJSON_Minify()
1600 } else if (json[1] == '*') { in loader_cJSON_Minify()
1601 skip_multiline_comment(&json); in loader_cJSON_Minify()
1603 json++; in loader_cJSON_Minify()
1608 minify_string(&json, (char **)&into); in loader_cJSON_Minify()
1612 into[0] = json[0]; in loader_cJSON_Minify()
1613 json++; in loader_cJSON_Minify()
1618 /* and null-terminate. */ in loader_cJSON_Minify()
1627 return (item->type & 0xFF) == cJSON_Invalid; in loader_cJSON_IsInvalid()
1635 return (item->type & 0xFF) == cJSON_False; in loader_cJSON_IsFalse()
1643 return (item->type & 0xff) == cJSON_True; in loader_cJSON_IsTrue()
1651 return (item->type & (cJSON_True | cJSON_False)) != 0; in loader_cJSON_IsBool()
1658 return (item->type & 0xFF) == cJSON_NULL; in loader_cJSON_IsNull()
1666 return (item->type & 0xFF) == cJSON_Number; in loader_cJSON_IsNumber()
1674 return (item->type & 0xFF) == cJSON_String; in loader_cJSON_IsString()
1682 return (item->type & 0xFF) == cJSON_Array; in loader_cJSON_IsArray()
1690 return (item->type & 0xFF) == cJSON_Object; in cJSON_IsObject()
1698 return (item->type & 0xFF) == cJSON_Raw; in loader_cJSON_IsRaw()
1702 if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) { in loader_cJSON_Compare()
1707 switch (a->type & 0xFF) { in loader_cJSON_Compare()
1727 switch (a->type & 0xFF) { in loader_cJSON_Compare()
1735 if (compare_double(a->valuedouble, b->valuedouble)) { in loader_cJSON_Compare()
1742 if ((a->valuestring == NULL) || (b->valuestring == NULL)) { in loader_cJSON_Compare()
1745 if (strcmp(a->valuestring, b->valuestring) == 0) { in loader_cJSON_Compare()
1752 cJSON *a_element = a->child; in loader_cJSON_Compare()
1753 cJSON *b_element = b->child; in loader_cJSON_Compare()
1760 a_element = a_element->next; in loader_cJSON_Compare()
1761 b_element = b_element->next; in loader_cJSON_Compare()
1777 b_element = get_object_item(b, a_element->string, case_sensitive); in loader_cJSON_Compare()
1790 a_element = get_object_item(a, b_element->string, case_sensitive); in loader_cJSON_Compare()