Debian Codegraph @

crt/aws-c-common/source/external/cJSON.c

raw symbols SCIP
1 /*
2 Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 /*
24 * This file has been modified from its original version by Amazon:
25 * (1) Remove cJSON_GetErrorPtr and global_error as they are not thread-safe.
26 * (2) Add NOLINTBEGIN/NOLINTEND so clang-tidy ignores file.
27 * (3) Replace sprintf() with snprintf() to make compilers happier.
28 */
29 /* NOLINTBEGIN */
30
31 /* cJSON */
32 /* JSON parser in C. */
33
34 /* disable warnings about old C89 functions in MSVC */
35 #if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
36 #define _CRT_SECURE_NO_DEPRECATE
37 #endif
38
39 #ifdef __GNUC__
40 #pragma GCC visibility push(default)
41 #endif
42 #if defined(_MSC_VER)
43 #pragma warning (push)
44 /* disable warning about single line comments in system headers */
45 #pragma warning (disable : 4001)
46 #endif
47
48 #include <string.h>
49 #include <stdio.h>
50 #include <math.h>
51 #include <stdlib.h>
52 #include <limits.h>
53 #include <ctype.h>
54 #include <float.h>
55
56 #ifdef ENABLE_LOCALES
57 #include <locale.h>
58 #endif
59
60 #if defined(_MSC_VER)
61 #pragma warning (pop)
62 #endif
63 #ifdef __GNUC__
64 #pragma GCC visibility pop
65 #endif
66
67 #include "cJSON.h"
68
69 /* define our own boolean type */
70 #ifdef true
71 #undef true
72 #endif
73 #define true ((cJSON_bool)1)
74
75 #ifdef false
76 #undef false
77 #endif
78 #define false ((cJSON_bool)0)
79
80 /* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */
81 #ifndef isinf
82 #define isinf(d) (isnan((d - d)) && !isnan(d))
83 #endif
84 #ifndef isnan
85 #define isnan(d) (d != d)
86 #endif
87
88 #ifndef NAN
89 #ifdef _WIN32
90 #define NAN sqrt(-1.0)
91 #else
92 #define NAN 0.0/0.0
93 #endif
94 #endif
95
96 typedef struct {
97 const unsigned char *json;
98 size_t position;
99 } error;
100 #if 0 /* Amazon edit */
101 static error global_error = { NULL, 0 };
102
103 CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
104 {
105 return (const char*) (global_error.json + global_error.position);
106 }
107 #endif /* Amazon edit */
108
109 CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item)
110 {
111 if (!cJSON_IsString(item))
112 {
113 return NULL;
114 }
115
116 return item->valuestring;
117 }
118
119 CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item)
120 {
121 if (!cJSON_IsNumber(item))
122 {
123 return (double) NAN;
124 }
125
126 return item->valuedouble;
127 }
128
129 /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
130 #if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 19)
131 #error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
132 #endif
133
134 CJSON_PUBLIC(const char*) cJSON_Version(void)
135 {
136 static char version[15];
137 snprintf(version, sizeof(version), "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); /* Amazon edit */
138
139 return version;
140 }
141
142 /* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
143 static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
144 {
145 if ((string1 == NULL) || (string2 == NULL))
146 {
147 return 1;
148 }
149
150 if (string1 == string2)
151 {
152 return 0;
153 }
154
155 for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
156 {
157 if (*string1 == '\0')
158 {
159 return 0;
160 }
161 }
162
163 return tolower(*string1) - tolower(*string2);
164 }
165
166 typedef struct internal_hooks
167 {
168 void *(CJSON_CDECL *allocate)(size_t size);
169 void (CJSON_CDECL *deallocate)(void *pointer);
170 void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
171 } internal_hooks;
172
173 #if defined(_MSC_VER)
174 /* work around MSVC error C2322: '...' address of dllimport '...' is not static */
175 static void * CJSON_CDECL internal_malloc(size_t size)
176 {
177 return malloc(size);
178 }
179 static void CJSON_CDECL internal_free(void *pointer)
180 {
181 free(pointer);
182 }
183 static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
184 {
185 return realloc(pointer, size);
186 }
187 #else
188 #define internal_malloc malloc
189 #define internal_free free
190 #define internal_realloc realloc
191 #endif
192
193 /* strlen of character literals resolved at compile time */
194 #define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
195
196 static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc };
197
198 static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
199 {
200 size_t length = 0;
201 unsigned char *copy = NULL;
202
203 if (string == NULL)
204 {
205 return NULL;
206 }
207
208 length = strlen((const char*)string) + sizeof("");
209 copy = (unsigned char*)hooks->allocate(length);
210 if (copy == NULL)
211 {
212 return NULL;
213 }
214 memcpy(copy, string, length);
215
216 return copy;
217 }
218
219 CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
220 {
221 if (hooks == NULL)
222 {
223 /* Reset hooks */
224 global_hooks.allocate = malloc;
225 global_hooks.deallocate = free;
226 global_hooks.reallocate = realloc;
227 return;
228 }
229
230 global_hooks.allocate = malloc;
231 if (hooks->malloc_fn != NULL)
232 {
233 global_hooks.allocate = hooks->malloc_fn;
234 }
235
236 global_hooks.deallocate = free;
237 if (hooks->free_fn != NULL)
238 {
239 global_hooks.deallocate = hooks->free_fn;
240 }
241
242 /* use realloc only if both free and malloc are used */
243 global_hooks.reallocate = NULL;
244 if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
245 {
246 global_hooks.reallocate = realloc;
247 }
248 }
249
250 /* Internal constructor. */
251 static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
252 {
253 cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
254 if (node)
255 {
256 memset(node, '\0', sizeof(cJSON));
257 }
258
259 return node;
260 }
261
262 /* Delete a cJSON structure. */
263 CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
264 {
265 cJSON *next = NULL;
266 while (item != NULL)
267 {
268 next = item->next;
269 if (!(item->type & cJSON_IsReference) && (item->child != NULL))
270 {
271 cJSON_Delete(item->child);
272 }
273 if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
274 {
275 global_hooks.deallocate(item->valuestring);
276 item->valuestring = NULL;
277 }
278 if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
279 {
280 global_hooks.deallocate(item->string);
281 item->string = NULL;
282 }
283 global_hooks.deallocate(item);
284 item = next;
285 }
286 }
287
288 /* get the decimal point character of the current locale */
289 static unsigned char get_decimal_point(void)
290 {
291 #ifdef ENABLE_LOCALES
292 struct lconv *lconv = localeconv();
293 return (unsigned char) lconv->decimal_point[0];
294 #else
295 return '.';
296 #endif
297 }
298
299 typedef struct
300 {
301 const unsigned char *content;
302 size_t length;
303 size_t offset;
304 size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
305 internal_hooks hooks;
306 } parse_buffer;
307
308 /* check if the given size is left to read in a given parse buffer (starting with 1) */
309 #define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
310 /* check if the buffer can be accessed at the given index (starting with 0) */
311 #define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
312 #define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
313 /* get a pointer to the buffer at the position */
314 #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
315
316 /* Parse the input text to generate a number, and populate the result into item. */
317 static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
318 {
319 double number = 0;
320 unsigned char *after_end = NULL;
321 unsigned char *number_c_string;
322 unsigned char decimal_point = get_decimal_point();
323 size_t i = 0;
324 size_t number_string_length = 0;
325 cJSON_bool has_decimal_point = false;
326
327 if ((input_buffer == NULL) || (input_buffer->content == NULL))
328 {
329 return false;
330 }
331
332 /* copy the number into a temporary buffer and replace '.' with the decimal point
333 * of the current locale (for strtod)
334 * This also takes care of '\0' not necessarily being available for marking the end of the input */
335 for (i = 0; can_access_at_index(input_buffer, i); i++)
336 {
337 switch (buffer_at_offset(input_buffer)[i])
338 {
339 case '0':
340 case '1':
341 case '2':
342 case '3':
343 case '4':
344 case '5':
345 case '6':
346 case '7':
347 case '8':
348 case '9':
349 case '+':
350 case '-':
351 case 'e':
352 case 'E':
353 number_string_length++;
354 break;
355
356 case '.':
357 number_string_length++;
358 has_decimal_point = true;
359 break;
360
361 default:
362 goto loop_end;
363 }
364 }
365 loop_end:
366 /* malloc for temporary buffer, add 1 for '\0' */
367 number_c_string = (unsigned char *) input_buffer->hooks.allocate(number_string_length + 1);
368 if (number_c_string == NULL)
369 {
370 return false; /* allocation failure */
371 }
372
373 memcpy(number_c_string, buffer_at_offset(input_buffer), number_string_length);
374 number_c_string[number_string_length] = '\0';
375
376 if (has_decimal_point)
377 {
378 for (i = 0; i < number_string_length; i++)
379 {
380 if (number_c_string[i] == '.')
381 {
382 /* replace '.' with the decimal point of the current locale (for strtod) */
383 number_c_string[i] = decimal_point;
384 }
385 }
386 }
387
388 number = strtod((const char*)number_c_string, (char**)&after_end);
389 if (number_c_string == after_end)
390 {
391 /* free the temporary buffer */
392 input_buffer->hooks.deallocate(number_c_string);
393 return false; /* parse_error */
394 }
395
396 item->valuedouble = number;
397
398 /* use saturation in case of overflow */
399 if (number >= INT_MAX)
400 {
401 item->valueint = INT_MAX;
402 }
403 else if (number <= (double)INT_MIN)
404 {
405 item->valueint = INT_MIN;
406 }
407 else
408 {
409 item->valueint = (int)number;
410 }
411
412 item->type = cJSON_Number;
413
414 input_buffer->offset += (size_t)(after_end - number_c_string);
415 /* free the temporary buffer */
416 input_buffer->hooks.deallocate(number_c_string);
417 return true;
418 }
419
420 /* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
421 CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
422 {
423 if (number >= INT_MAX)
424 {
425 object->valueint = INT_MAX;
426 }
427 else if (number <= (double)INT_MIN)
428 {
429 object->valueint = INT_MIN;
430 }
431 else
432 {
433 object->valueint = (int)number;
434 }
435
436 return object->valuedouble = number;
437 }
438
439 /* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */
440 CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
441 {
442 char *copy = NULL;
443 size_t v1_len;
444 size_t v2_len;
445 /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
446 if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference))
447 {
448 return NULL;
449 }
450 /* return NULL if the object is corrupted or valuestring is NULL */
451 if (object->valuestring == NULL || valuestring == NULL)
452 {
453 return NULL;
454 }
455
456 v1_len = strlen(valuestring);
457 v2_len = strlen(object->valuestring);
458
459 if (v1_len <= v2_len)
460 {
461 /* strcpy does not handle overlapping string: [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */
462 if (!( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring ))
463 {
464 return NULL;
465 }
466 strcpy(object->valuestring, valuestring);
467 return object->valuestring;
468 }
469 copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks);
470 if (copy == NULL)
471 {
472 return NULL;
473 }
474 if (object->valuestring != NULL)
475 {
476 cJSON_free(object->valuestring);
477 }
478 object->valuestring = copy;
479
480 return copy;
481 }
482
483 typedef struct
484 {
485 unsigned char *buffer;
486 size_t length;
487 size_t offset;
488 size_t depth; /* current nesting depth (for formatted printing) */
489 cJSON_bool noalloc;
490 cJSON_bool format; /* is this print a formatted print */
491 internal_hooks hooks;
492 } printbuffer;
493
494 /* realloc printbuffer if necessary to have at least "needed" bytes more */
495 static unsigned char* ensure(printbuffer * const p, size_t needed)
496 {
497 unsigned char *newbuffer = NULL;
498 size_t newsize = 0;
499
500 if ((p == NULL) || (p->buffer == NULL))
501 {
502 return NULL;
503 }
504
505 if ((p->length > 0) && (p->offset >= p->length))
506 {
507 /* make sure that offset is valid */
508 return NULL;
509 }
510
511 if (needed > INT_MAX)
512 {
513 /* sizes bigger than INT_MAX are currently not supported */
514 return NULL;
515 }
516
517 needed += p->offset + 1;
518 if (needed <= p->length)
519 {
520 return p->buffer + p->offset;
521 }
522
523 if (p->noalloc) {
524 return NULL;
525 }
526
527 /* calculate new buffer size */
528 if (needed > (INT_MAX / 2))
529 {
530 /* overflow of int, use INT_MAX if possible */
531 if (needed <= INT_MAX)
532 {
533 newsize = INT_MAX;
534 }
535 else
536 {
537 return NULL;
538 }
539 }
540 else
541 {
542 newsize = needed * 2;
543 }
544
545 if (p->hooks.reallocate != NULL)
546 {
547 /* reallocate with realloc if available */
548 newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
549 if (newbuffer == NULL)
550 {
551 p->hooks.deallocate(p->buffer);
552 p->length = 0;
553 p->buffer = NULL;
554
555 return NULL;
556 }
557 }
558 else
559 {
560 /* otherwise reallocate manually */
561 newbuffer = (unsigned char*)p->hooks.allocate(newsize);
562 if (!newbuffer)
563 {
564 p->hooks.deallocate(p->buffer);
565 p->length = 0;
566 p->buffer = NULL;
567
568 return NULL;
569 }
570
571 memcpy(newbuffer, p->buffer, p->offset + 1);
572 p->hooks.deallocate(p->buffer);
573 }
574 p->length = newsize;
575 p->buffer = newbuffer;
576
577 return newbuffer + p->offset;
578 }
579
580 /* calculate the new length of the string in a printbuffer and update the offset */
581 static void update_offset(printbuffer * const buffer)
582 {
583 const unsigned char *buffer_pointer = NULL;
584 if ((buffer == NULL) || (buffer->buffer == NULL))
585 {
586 return;
587 }
588 buffer_pointer = buffer->buffer + buffer->offset;
589
590 buffer->offset += strlen((const char*)buffer_pointer);
591 }
592
593 /* securely comparison of floating-point variables */
594 static cJSON_bool compare_double(double a, double b)
595 {
596 double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
597 return (fabs(a - b) <= maxVal * DBL_EPSILON);
598 }
599
600 /* Render the number nicely from the given item into a string. */
601 static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer)
602 {
603 unsigned char *output_pointer = NULL;
604 double d = item->valuedouble;
605 int length = 0;
606 size_t i = 0;
607 unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */
608 unsigned char decimal_point = get_decimal_point();
609 double test = 0.0;
610
611 if (output_buffer == NULL)
612 {
613 return false;
614 }
615
616 /* This checks for NaN and Infinity */
617 if (isnan(d) || isinf(d))
618 {
619 length = snprintf((char*)number_buffer, sizeof(number_buffer), "null"); /* Amazon edit */
620 }
621 else if(d == (double)item->valueint)
622 {
623 length = snprintf((char*)number_buffer, sizeof(number_buffer), "%d", item->valueint); /* Amazon edit */
624 }
625 else
626 {
627 /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
628 length = snprintf((char*)number_buffer, sizeof(number_buffer), "%1.15g", d); /* Amazon edit */
629
630 /* Check whether the original double can be recovered */
631 if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
632 {
633 /* If not, print with 17 decimal places of precision */
634 length = snprintf((char*)number_buffer, sizeof(number_buffer), "%1.17g", d); /* Amazon edit */
635 }
636 }
637
638 /* sprintf failed or buffer overrun occurred */
639 if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
640 {
641 return false;
642 }
643
644 /* reserve appropriate space in the output */
645 output_pointer = ensure(output_buffer, (size_t)length + sizeof(""));
646 if (output_pointer == NULL)
647 {
648 return false;
649 }
650
651 /* copy the printed number to the output and replace locale
652 * dependent decimal point with '.' */
653 for (i = 0; i < ((size_t)length); i++)
654 {
655 if (number_buffer[i] == decimal_point)
656 {
657 output_pointer[i] = '.';
658 continue;
659 }
660
661 output_pointer[i] = number_buffer[i];
662 }
663 output_pointer[i] = '\0';
664
665 output_buffer->offset += (size_t)length;
666
667 return true;
668 }
669
670 /* parse 4 digit hexadecimal number */
671 static unsigned parse_hex4(const unsigned char * const input)
672 {
673 unsigned int h = 0;
674 size_t i = 0;
675
676 for (i = 0; i < 4; i++)
677 {
678 /* parse digit */
679 if ((input[i] >= '0') && (input[i] <= '9'))
680 {
681 h += (unsigned int) input[i] - '0';
682 }
683 else if ((input[i] >= 'A') && (input[i] <= 'F'))
684 {
685 h += (unsigned int) 10 + input[i] - 'A';
686 }
687 else if ((input[i] >= 'a') && (input[i] <= 'f'))
688 {
689 h += (unsigned int) 10 + input[i] - 'a';
690 }
691 else /* invalid */
692 {
693 return 0;
694 }
695
696 if (i < 3)
697 {
698 /* shift left to make place for the next nibble */
699 h = h << 4;
700 }
701 }
702
703 return h;
704 }
705
706 /* converts a UTF-16 literal to UTF-8
707 * A literal can be one or two sequences of the form \uXXXX */
708 static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
709 {
710 long unsigned int codepoint = 0;
711 unsigned int first_code = 0;
712 const unsigned char *first_sequence = input_pointer;
713 unsigned char utf8_length = 0;
714 unsigned char utf8_position = 0;
715 unsigned char sequence_length = 0;
716 unsigned char first_byte_mark = 0;
717
718 if ((input_end - first_sequence) < 6)
719 {
720 /* input ends unexpectedly */
721 goto fail;
722 }
723
724 /* get the first utf16 sequence */
725 first_code = parse_hex4(first_sequence + 2);
726
727 /* check that the code is valid */
728 if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
729 {
730 goto fail;
731 }
732
733 /* UTF16 surrogate pair */
734 if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
735 {
736 const unsigned char *second_sequence = first_sequence + 6;
737 unsigned int second_code = 0;
738 sequence_length = 12; /* \uXXXX\uXXXX */
739
740 if ((input_end - second_sequence) < 6)
741 {
742 /* input ends unexpectedly */
743 goto fail;
744 }
745
746 if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
747 {
748 /* missing second half of the surrogate pair */
749 goto fail;
750 }
751
752 /* get the second utf16 sequence */
753 second_code = parse_hex4(second_sequence + 2);
754 /* check that the code is valid */
755 if ((second_code < 0xDC00) || (second_code > 0xDFFF))
756 {
757 /* invalid second half of the surrogate pair */
758 goto fail;
759 }
760
761
762 /* calculate the unicode codepoint from the surrogate pair */
763 codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
764 }
765 else
766 {
767 sequence_length = 6; /* \uXXXX */
768 codepoint = first_code;
769 }
770
771 /* encode as UTF-8
772 * takes at maximum 4 bytes to encode:
773 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
774 if (codepoint < 0x80)
775 {
776 /* normal ascii, encoding 0xxxxxxx */
777 utf8_length = 1;
778 }
779 else if (codepoint < 0x800)
780 {
781 /* two bytes, encoding 110xxxxx 10xxxxxx */
782 utf8_length = 2;
783 first_byte_mark = 0xC0; /* 11000000 */
784 }
785 else if (codepoint < 0x10000)
786 {
787 /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
788 utf8_length = 3;
789 first_byte_mark = 0xE0; /* 11100000 */
790 }
791 else if (codepoint <= 0x10FFFF)
792 {
793 /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
794 utf8_length = 4;
795 first_byte_mark = 0xF0; /* 11110000 */
796 }
797 else
798 {
799 /* invalid unicode codepoint */
800 goto fail;
801 }
802
803 /* encode as utf8 */
804 for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
805 {
806 /* 10xxxxxx */
807 (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
808 codepoint >>= 6;
809 }
810 /* encode first byte */
811 if (utf8_length > 1)
812 {
813 (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
814 }
815 else
816 {
817 (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
818 }
819
820 *output_pointer += utf8_length;
821
822 return sequence_length;
823
824 fail:
825 return 0;
826 }
827
828 /* Parse the input text into an unescaped cinput, and populate item. */
829 static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer)
830 {
831 const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
832 const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
833 unsigned char *output_pointer = NULL;
834 unsigned char *output = NULL;
835
836 /* not a string */
837 if (buffer_at_offset(input_buffer)[0] != '\"')
838 {
839 goto fail;
840 }
841
842 {
843 /* calculate approximate size of the output (overestimate) */
844 size_t allocation_length = 0;
845 size_t skipped_bytes = 0;
846 while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
847 {
848 /* is escape sequence */
849 if (input_end[0] == '\\')
850 {
851 if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
852 {
853 /* prevent buffer overflow when last input character is a backslash */
854 goto fail;
855 }
856 skipped_bytes++;
857 input_end++;
858 }
859 input_end++;
860 }
861 if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
862 {
863 goto fail; /* string ended unexpectedly */
864 }
865
866 /* This is at most how much we need for the output */
867 allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
868 output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
869 if (output == NULL)
870 {
871 goto fail; /* allocation failure */
872 }
873 }
874
875 output_pointer = output;
876 /* loop through the string literal */
877 while (input_pointer < input_end)
878 {
879 if (*input_pointer != '\\')
880 {
881 *output_pointer++ = *input_pointer++;
882 }
883 /* escape sequence */
884 else
885 {
886 unsigned char sequence_length = 2;
887 if ((input_end - input_pointer) < 1)
888 {
889 goto fail;
890 }
891
892 switch (input_pointer[1])
893 {
894 case 'b':
895 *output_pointer++ = '\b';
896 break;
897 case 'f':
898 *output_pointer++ = '\f';
899 break;
900 case 'n':
901 *output_pointer++ = '\n';
902 break;
903 case 'r':
904 *output_pointer++ = '\r';
905 break;
906 case 't':
907 *output_pointer++ = '\t';
908 break;
909 case '\"':
910 case '\\':
911 case '/':
912 *output_pointer++ = input_pointer[1];
913 break;
914
915 /* UTF-16 literal */
916 case 'u':
917 sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
918 if (sequence_length == 0)
919 {
920 /* failed to convert UTF16-literal to UTF-8 */
921 goto fail;
922 }
923 break;
924
925 default:
926 goto fail;
927 }
928 input_pointer += sequence_length;
929 }
930 }
931
932 /* zero terminate the output */
933 *output_pointer = '\0';
934
935 item->type = cJSON_String;
936 item->valuestring = (char*)output;
937
938 input_buffer->offset = (size_t) (input_end - input_buffer->content);
939 input_buffer->offset++;
940
941 return true;
942
943 fail:
944 if (output != NULL)
945 {
946 input_buffer->hooks.deallocate(output);
947 output = NULL;
948 }
949
950 if (input_pointer != NULL)
951 {
952 input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
953 }
954
955 return false;
956 }
957
958 /* Render the cstring provided to an escaped version that can be printed. */
959 static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer)
960 {
961 const unsigned char *input_pointer = NULL;
962 unsigned char *output = NULL;
963 unsigned char *output_pointer = NULL;
964 size_t output_length = 0;
965 /* numbers of additional characters needed for escaping */
966 size_t escape_characters = 0;
967
968 if (output_buffer == NULL)
969 {
970 return false;
971 }
972
973 /* empty string */
974 if (input == NULL)
975 {
976 output = ensure(output_buffer, sizeof("\"\""));
977 if (output == NULL)
978 {
979 return false;
980 }
981 strcpy((char*)output, "\"\"");
982
983 return true;
984 }
985
986 /* set "flag" to 1 if something needs to be escaped */
987 for (input_pointer = input; *input_pointer; input_pointer++)
988 {
989 switch (*input_pointer)
990 {
991 case '\"':
992 case '\\':
993 case '\b':
994 case '\f':
995 case '\n':
996 case '\r':
997 case '\t':
998 /* one character escape sequence */
999 escape_characters++;
1000 break;
1001 default:
1002 if (*input_pointer < 32)
1003 {
1004 /* UTF-16 escape sequence uXXXX */
1005 escape_characters += 5;
1006 }
1007 break;
1008 }
1009 }
1010 output_length = (size_t)(input_pointer - input) + escape_characters;
1011
1012 output = ensure(output_buffer, output_length + sizeof("\"\""));
1013 if (output == NULL)
1014 {
1015 return false;
1016 }
1017
1018 /* no characters have to be escaped */
1019 if (escape_characters == 0)
1020 {
1021 output[0] = '\"';
1022 memcpy(output + 1, input, output_length);
1023 output[output_length + 1] = '\"';
1024 output[output_length + 2] = '\0';
1025
1026 return true;
1027 }
1028
1029 output[0] = '\"';
1030 output_pointer = output + 1;
1031 /* copy the string */
1032 for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
1033 {
1034 if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
1035 {
1036 /* normal character, copy */
1037 *output_pointer = *input_pointer;
1038 }
1039 else
1040 {
1041 /* character needs to be escaped */
1042 *output_pointer++ = '\\';
1043 switch (*input_pointer)
1044 {
1045 case '\\':
1046 *output_pointer = '\\';
1047 break;
1048 case '\"':
1049 *output_pointer = '\"';
1050 break;
1051 case '\b':
1052 *output_pointer = 'b';
1053 break;
1054 case '\f':
1055 *output_pointer = 'f';
1056 break;
1057 case '\n':
1058 *output_pointer = 'n';
1059 break;
1060 case '\r':
1061 *output_pointer = 'r';
1062 break;
1063 case '\t':
1064 *output_pointer = 't';
1065 break;
1066 default:
1067 /* escape and print as unicode codepoint */
1068 snprintf((char*)output_pointer, 6, "u%04x", *input_pointer); /* Amazon edit */
1069 output_pointer += 4;
1070 break;
1071 }
1072 }
1073 }
1074 output[output_length + 1] = '\"';
1075 output[output_length + 2] = '\0';
1076
1077 return true;
1078 }
1079
1080 /* Invoke print_string_ptr (which is useful) on an item. */
1081 static cJSON_bool print_string(const cJSON * const item, printbuffer * const p)
1082 {
1083 return print_string_ptr((unsigned char*)item->valuestring, p);
1084 }
1085
1086 /* Predeclare these prototypes. */
1087 static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer);
1088 static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer);
1089 static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer);
1090 static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer);
1091 static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer);
1092 static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer);
1093
1094 /* Utility to jump whitespace and cr/lf */
1095 static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
1096 {
1097 if ((buffer == NULL) || (buffer->content == NULL))
1098 {
1099 return NULL;
1100 }
1101
1102 if (cannot_access_at_index(buffer, 0))
1103 {
1104 return buffer;
1105 }
1106
1107 while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
1108 {
1109 buffer->offset++;
1110 }
1111
1112 if (buffer->offset == buffer->length)
1113 {
1114 buffer->offset--;
1115 }
1116
1117 return buffer;
1118 }
1119
1120 /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */
1121 static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
1122 {
1123 if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0))
1124 {
1125 return NULL;
1126 }
1127
1128 if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0))
1129 {
1130 buffer->offset += 3;
1131 }
1132
1133 return buffer;
1134 }
1135
1136 CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
1137 {
1138 size_t buffer_length;
1139
1140 if (NULL == value)
1141 {
1142 return NULL;
1143 }
1144
1145 /* Adding null character size due to require_null_terminated. */
1146 buffer_length = strlen(value) + sizeof("");
1147
1148 return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated);
1149 }
1150
1151 /* Parse an object - create a new root, and populate. */
1152 CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated)
1153 {
1154 parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
1155 cJSON *item = NULL;
1156
1157 #if 0 /* Amazon edit */
1158 /* reset error position */
1159 global_error.json = NULL;
1160 global_error.position = 0;
1161 #endif /* Amazon edit */
1162
1163 if (value == NULL || 0 == buffer_length)
1164 {
1165 goto fail;
1166 }
1167
1168 buffer.content = (const unsigned char*)value;
1169 buffer.length = buffer_length;
1170 buffer.offset = 0;
1171 buffer.hooks = global_hooks;
1172
1173 item = cJSON_New_Item(&global_hooks);
1174 if (item == NULL) /* memory fail */
1175 {
1176 goto fail;
1177 }
1178
1179 if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
1180 {
1181 /* parse failure. ep is set. */
1182 goto fail;
1183 }
1184
1185 /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
1186 if (require_null_terminated)
1187 {
1188 buffer_skip_whitespace(&buffer);
1189 if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
1190 {
1191 goto fail;
1192 }
1193 }
1194 if (return_parse_end)
1195 {
1196 *return_parse_end = (const char*)buffer_at_offset(&buffer);
1197 }
1198
1199 return item;
1200
1201 fail:
1202 if (item != NULL)
1203 {
1204 cJSON_Delete(item);
1205 }
1206
1207 if (value != NULL)
1208 {
1209 error local_error;
1210 local_error.json = (const unsigned char*)value;
1211 local_error.position = 0;
1212
1213 if (buffer.offset < buffer.length)
1214 {
1215 local_error.position = buffer.offset;
1216 }
1217 else if (buffer.length > 0)
1218 {
1219 local_error.position = buffer.length - 1;
1220 }
1221
1222 if (return_parse_end != NULL)
1223 {
1224 *return_parse_end = (const char*)local_error.json + local_error.position;
1225 }
1226
1227 #if 0 /* Amazon edit */
1228 global_error = local_error;
1229 #endif /* Amazon edit */
1230 }
1231
1232 return NULL;
1233 }
1234
1235 /* Default options for cJSON_Parse */
1236 CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
1237 {
1238 return cJSON_ParseWithOpts(value, 0, 0);
1239 }
1240
1241 CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length)
1242 {
1243 return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0);
1244 }
1245
1246 #define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
1247
1248 static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
1249 {
1250 static const size_t default_buffer_size = 256;
1251 printbuffer buffer[1];
1252 unsigned char *printed = NULL;
1253
1254 memset(buffer, 0, sizeof(buffer));
1255
1256 /* create buffer */
1257 buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
1258 buffer->length = default_buffer_size;
1259 buffer->format = format;
1260 buffer->hooks = *hooks;
1261 if (buffer->buffer == NULL)
1262 {
1263 goto fail;
1264 }
1265
1266 /* print the value */
1267 if (!print_value(item, buffer))
1268 {
1269 goto fail;
1270 }
1271 update_offset(buffer);
1272
1273 /* check if reallocate is available */
1274 if (hooks->reallocate != NULL)
1275 {
1276 printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
1277 if (printed == NULL) {
1278 goto fail;
1279 }
1280 buffer->buffer = NULL;
1281 }
1282 else /* otherwise copy the JSON over to a new buffer */
1283 {
1284 printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
1285 if (printed == NULL)
1286 {
1287 goto fail;
1288 }
1289 memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1290 printed[buffer->offset] = '\0'; /* just to be sure */
1291
1292 /* free the buffer */
1293 hooks->deallocate(buffer->buffer);
1294 buffer->buffer = NULL;
1295 }
1296
1297 return printed;
1298
1299 fail:
1300 if (buffer->buffer != NULL)
1301 {
1302 hooks->deallocate(buffer->buffer);
1303 buffer->buffer = NULL;
1304 }
1305
1306 if (printed != NULL)
1307 {
1308 hooks->deallocate(printed);
1309 printed = NULL;
1310 }
1311
1312 return NULL;
1313 }
1314
1315 /* Render a cJSON item/entity/structure to text. */
1316 CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
1317 {
1318 return (char*)print(item, true, &global_hooks);
1319 }
1320
1321 CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
1322 {
1323 return (char*)print(item, false, &global_hooks);
1324 }
1325
1326 CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
1327 {
1328 printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
1329
1330 if (prebuffer < 0)
1331 {
1332 return NULL;
1333 }
1334
1335 p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
1336 if (!p.buffer)
1337 {
1338 return NULL;
1339 }
1340
1341 p.length = (size_t)prebuffer;
1342 p.offset = 0;
1343 p.noalloc = false;
1344 p.format = fmt;
1345 p.hooks = global_hooks;
1346
1347 if (!print_value(item, &p))
1348 {
1349 global_hooks.deallocate(p.buffer);
1350 p.buffer = NULL;
1351 return NULL;
1352 }
1353
1354 return (char*)p.buffer;
1355 }
1356
1357 CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format)
1358 {
1359 printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
1360
1361 if ((length < 0) || (buffer == NULL))
1362 {
1363 return false;
1364 }
1365
1366 p.buffer = (unsigned char*)buffer;
1367 p.length = (size_t)length;
1368 p.offset = 0;
1369 p.noalloc = true;
1370 p.format = format;
1371 p.hooks = global_hooks;
1372
1373 return print_value(item, &p);
1374 }
1375
1376 /* Parser core - when encountering text, process appropriately. */
1377 static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer)
1378 {
1379 if ((input_buffer == NULL) || (input_buffer->content == NULL))
1380 {
1381 return false; /* no input */
1382 }
1383
1384 /* parse the different types of values */
1385 /* null */
1386 if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
1387 {
1388 item->type = cJSON_NULL;
1389 input_buffer->offset += 4;
1390 return true;
1391 }
1392 /* false */
1393 if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
1394 {
1395 item->type = cJSON_False;
1396 input_buffer->offset += 5;
1397 return true;
1398 }
1399 /* true */
1400 if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
1401 {
1402 item->type = cJSON_True;
1403 item->valueint = 1;
1404 input_buffer->offset += 4;
1405 return true;
1406 }
1407 /* string */
1408 if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
1409 {
1410 return parse_string(item, input_buffer);
1411 }
1412 /* number */
1413 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'))))
1414 {
1415 return parse_number(item, input_buffer);
1416 }
1417 /* array */
1418 if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
1419 {
1420 return parse_array(item, input_buffer);
1421 }
1422 /* object */
1423 if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
1424 {
1425 return parse_object(item, input_buffer);
1426 }
1427
1428 return false;
1429 }
1430
1431 /* Render a value to text. */
1432 static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer)
1433 {
1434 unsigned char *output = NULL;
1435
1436 if ((item == NULL) || (output_buffer == NULL))
1437 {
1438 return false;
1439 }
1440
1441 switch ((item->type) & 0xFF)
1442 {
1443 case cJSON_NULL:
1444 output = ensure(output_buffer, 5);
1445 if (output == NULL)
1446 {
1447 return false;
1448 }
1449 strcpy((char*)output, "null");
1450 return true;
1451
1452 case cJSON_False:
1453 output = ensure(output_buffer, 6);
1454 if (output == NULL)
1455 {
1456 return false;
1457 }
1458 strcpy((char*)output, "false");
1459 return true;
1460
1461 case cJSON_True:
1462 output = ensure(output_buffer, 5);
1463 if (output == NULL)
1464 {
1465 return false;
1466 }
1467 strcpy((char*)output, "true");
1468 return true;
1469
1470 case cJSON_Number:
1471 return print_number(item, output_buffer);
1472
1473 case cJSON_Raw:
1474 {
1475 size_t raw_length = 0;
1476 if (item->valuestring == NULL)
1477 {
1478 return false;
1479 }
1480
1481 raw_length = strlen(item->valuestring) + sizeof("");
1482 output = ensure(output_buffer, raw_length);
1483 if (output == NULL)
1484 {
1485 return false;
1486 }
1487 memcpy(output, item->valuestring, raw_length);
1488 return true;
1489 }
1490
1491 case cJSON_String:
1492 return print_string(item, output_buffer);
1493
1494 case cJSON_Array:
1495 return print_array(item, output_buffer);
1496
1497 case cJSON_Object:
1498 return print_object(item, output_buffer);
1499
1500 default:
1501 return false;
1502 }
1503 }
1504
1505 /* Build an array from input text. */
1506 static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer)
1507 {
1508 cJSON *head = NULL; /* head of the linked list */
1509 cJSON *current_item = NULL;
1510
1511 if (input_buffer->depth >= CJSON_NESTING_LIMIT)
1512 {
1513 return false; /* to deeply nested */
1514 }
1515 input_buffer->depth++;
1516
1517 if (buffer_at_offset(input_buffer)[0] != '[')
1518 {
1519 /* not an array */
1520 goto fail;
1521 }
1522
1523 input_buffer->offset++;
1524 buffer_skip_whitespace(input_buffer);
1525 if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']'))
1526 {
1527 /* empty array */
1528 goto success;
1529 }
1530
1531 /* check if we skipped to the end of the buffer */
1532 if (cannot_access_at_index(input_buffer, 0))
1533 {
1534 input_buffer->offset--;
1535 goto fail;
1536 }
1537
1538 /* step back to character in front of the first element */
1539 input_buffer->offset--;
1540 /* loop through the comma separated array elements */
1541 do
1542 {
1543 /* allocate next item */
1544 cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
1545 if (new_item == NULL)
1546 {
1547 goto fail; /* allocation failure */
1548 }
1549
1550 /* attach next item to list */
1551 if (head == NULL)
1552 {
1553 /* start the linked list */
1554 current_item = head = new_item;
1555 }
1556 else
1557 {
1558 /* add to the end and advance */
1559 current_item->next = new_item;
1560 new_item->prev = current_item;
1561 current_item = new_item;
1562 }
1563
1564 /* parse next value */
1565 input_buffer->offset++;
1566 buffer_skip_whitespace(input_buffer);
1567 if (!parse_value(current_item, input_buffer))
1568 {
1569 goto fail; /* failed to parse value */
1570 }
1571 buffer_skip_whitespace(input_buffer);
1572 }
1573 while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
1574
1575 if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
1576 {
1577 goto fail; /* expected end of array */
1578 }
1579
1580 success:
1581 input_buffer->depth--;
1582
1583 if (head != NULL) {
1584 head->prev = current_item;
1585 }
1586
1587 item->type = cJSON_Array;
1588 item->child = head;
1589
1590 input_buffer->offset++;
1591
1592 return true;
1593
1594 fail:
1595 if (head != NULL)
1596 {
1597 cJSON_Delete(head);
1598 }
1599
1600 return false;
1601 }
1602
1603 /* Render an array to text */
1604 static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer)
1605 {
1606 unsigned char *output_pointer = NULL;
1607 size_t length = 0;
1608 cJSON *current_element = item->child;
1609
1610 if (output_buffer == NULL)
1611 {
1612 return false;
1613 }
1614
1615 /* Compose the output array. */
1616 /* opening square bracket */
1617 output_pointer = ensure(output_buffer, 1);
1618 if (output_pointer == NULL)
1619 {
1620 return false;
1621 }
1622
1623 *output_pointer = '[';
1624 output_buffer->offset++;
1625 output_buffer->depth++;
1626
1627 while (current_element != NULL)
1628 {
1629 if (!print_value(current_element, output_buffer))
1630 {
1631 return false;
1632 }
1633 update_offset(output_buffer);
1634 if (current_element->next)
1635 {
1636 length = (size_t) (output_buffer->format ? 2 : 1);
1637 output_pointer = ensure(output_buffer, length + 1);
1638 if (output_pointer == NULL)
1639 {
1640 return false;
1641 }
1642 *output_pointer++ = ',';
1643 if(output_buffer->format)
1644 {
1645 *output_pointer++ = ' ';
1646 }
1647 *output_pointer = '\0';
1648 output_buffer->offset += length;
1649 }
1650 current_element = current_element->next;
1651 }
1652
1653 output_pointer = ensure(output_buffer, 2);
1654 if (output_pointer == NULL)
1655 {
1656 return false;
1657 }
1658 *output_pointer++ = ']';
1659 *output_pointer = '\0';
1660 output_buffer->depth--;
1661
1662 return true;
1663 }
1664
1665 /* Build an object from the text. */
1666 static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer)
1667 {
1668 cJSON *head = NULL; /* linked list head */
1669 cJSON *current_item = NULL;
1670
1671 if (input_buffer->depth >= CJSON_NESTING_LIMIT)
1672 {
1673 return false; /* to deeply nested */
1674 }
1675 input_buffer->depth++;
1676
1677 if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
1678 {
1679 goto fail; /* not an object */
1680 }
1681
1682 input_buffer->offset++;
1683 buffer_skip_whitespace(input_buffer);
1684 if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}'))
1685 {
1686 goto success; /* empty object */
1687 }
1688
1689 /* check if we skipped to the end of the buffer */
1690 if (cannot_access_at_index(input_buffer, 0))
1691 {
1692 input_buffer->offset--;
1693 goto fail;
1694 }
1695
1696 /* step back to character in front of the first element */
1697 input_buffer->offset--;
1698 /* loop through the comma separated array elements */
1699 do
1700 {
1701 /* allocate next item */
1702 cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
1703 if (new_item == NULL)
1704 {
1705 goto fail; /* allocation failure */
1706 }
1707
1708 /* attach next item to list */
1709 if (head == NULL)
1710 {
1711 /* start the linked list */
1712 current_item = head = new_item;
1713 }
1714 else
1715 {
1716 /* add to the end and advance */
1717 current_item->next = new_item;
1718 new_item->prev = current_item;
1719 current_item = new_item;
1720 }
1721
1722 if (cannot_access_at_index(input_buffer, 1))
1723 {
1724 goto fail; /* nothing comes after the comma */
1725 }
1726
1727 /* parse the name of the child */
1728 input_buffer->offset++;
1729 buffer_skip_whitespace(input_buffer);
1730 if (!parse_string(current_item, input_buffer))
1731 {
1732 goto fail; /* failed to parse name */
1733 }
1734 buffer_skip_whitespace(input_buffer);
1735
1736 /* swap valuestring and string, because we parsed the name */
1737 current_item->string = current_item->valuestring;
1738 current_item->valuestring = NULL;
1739
1740 if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
1741 {
1742 goto fail; /* invalid object */
1743 }
1744
1745 /* parse the value */
1746 input_buffer->offset++;
1747 buffer_skip_whitespace(input_buffer);
1748 if (!parse_value(current_item, input_buffer))
1749 {
1750 goto fail; /* failed to parse value */
1751 }
1752 buffer_skip_whitespace(input_buffer);
1753 }
1754 while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
1755
1756 if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
1757 {
1758 goto fail; /* expected end of object */
1759 }
1760
1761 success:
1762 input_buffer->depth--;
1763
1764 if (head != NULL) {
1765 head->prev = current_item;
1766 }
1767
1768 item->type = cJSON_Object;
1769 item->child = head;
1770
1771 input_buffer->offset++;
1772 return true;
1773
1774 fail:
1775 if (head != NULL)
1776 {
1777 cJSON_Delete(head);
1778 }
1779
1780 return false;
1781 }
1782
1783 /* Render an object to text. */
1784 static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer)
1785 {
1786 unsigned char *output_pointer = NULL;
1787 size_t length = 0;
1788 cJSON *current_item = item->child;
1789
1790 if (output_buffer == NULL)
1791 {
1792 return false;
1793 }
1794
1795 /* Compose the output: */
1796 length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
1797 output_pointer = ensure(output_buffer, length + 1);
1798 if (output_pointer == NULL)
1799 {
1800 return false;
1801 }
1802
1803 *output_pointer++ = '{';
1804 output_buffer->depth++;
1805 if (output_buffer->format)
1806 {
1807 *output_pointer++ = '\n';
1808 }
1809 output_buffer->offset += length;
1810
1811 while (current_item)
1812 {
1813 if (output_buffer->format)
1814 {
1815 size_t i;
1816 output_pointer = ensure(output_buffer, output_buffer->depth);
1817 if (output_pointer == NULL)
1818 {
1819 return false;
1820 }
1821 for (i = 0; i < output_buffer->depth; i++)
1822 {
1823 *output_pointer++ = '\t';
1824 }
1825 output_buffer->offset += output_buffer->depth;
1826 }
1827
1828 /* print key */
1829 if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
1830 {
1831 return false;
1832 }
1833 update_offset(output_buffer);
1834
1835 length = (size_t) (output_buffer->format ? 2 : 1);
1836 output_pointer = ensure(output_buffer, length);
1837 if (output_pointer == NULL)
1838 {
1839 return false;
1840 }
1841 *output_pointer++ = ':';
1842 if (output_buffer->format)
1843 {
1844 *output_pointer++ = '\t';
1845 }
1846 output_buffer->offset += length;
1847
1848 /* print value */
1849 if (!print_value(current_item, output_buffer))
1850 {
1851 return false;
1852 }
1853 update_offset(output_buffer);
1854
1855 /* print comma if not last */
1856 length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0));
1857 output_pointer = ensure(output_buffer, length + 1);
1858 if (output_pointer == NULL)
1859 {
1860 return false;
1861 }
1862 if (current_item->next)
1863 {
1864 *output_pointer++ = ',';
1865 }
1866
1867 if (output_buffer->format)
1868 {
1869 *output_pointer++ = '\n';
1870 }
1871 *output_pointer = '\0';
1872 output_buffer->offset += length;
1873
1874 current_item = current_item->next;
1875 }
1876
1877 output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
1878 if (output_pointer == NULL)
1879 {
1880 return false;
1881 }
1882 if (output_buffer->format)
1883 {
1884 size_t i;
1885 for (i = 0; i < (output_buffer->depth - 1); i++)
1886 {
1887 *output_pointer++ = '\t';
1888 }
1889 }
1890 *output_pointer++ = '}';
1891 *output_pointer = '\0';
1892 output_buffer->depth--;
1893
1894 return true;
1895 }
1896
1897 /* Get Array size/item / object item. */
1898 CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
1899 {
1900 cJSON *child = NULL;
1901 size_t size = 0;
1902
1903 if (array == NULL)
1904 {
1905 return 0;
1906 }
1907
1908 child = array->child;
1909
1910 while(child != NULL)
1911 {
1912 size++;
1913 child = child->next;
1914 }
1915
1916 /* FIXME: Can overflow here. Cannot be fixed without breaking the API */
1917
1918 return (int)size;
1919 }
1920
1921 static cJSON* get_array_item(const cJSON *array, size_t index)
1922 {
1923 cJSON *current_child = NULL;
1924
1925 if (array == NULL)
1926 {
1927 return NULL;
1928 }
1929
1930 current_child = array->child;
1931 while ((current_child != NULL) && (index > 0))
1932 {
1933 index--;
1934 current_child = current_child->next;
1935 }
1936
1937 return current_child;
1938 }
1939
1940 CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
1941 {
1942 if (index < 0)
1943 {
1944 return NULL;
1945 }
1946
1947 return get_array_item(array, (size_t)index);
1948 }
1949
1950 static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
1951 {
1952 cJSON *current_element = NULL;
1953
1954 if ((object == NULL) || (name == NULL))
1955 {
1956 return NULL;
1957 }
1958
1959 current_element = object->child;
1960 if (case_sensitive)
1961 {
1962 while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0))
1963 {
1964 current_element = current_element->next;
1965 }
1966 }
1967 else
1968 {
1969 while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
1970 {
1971 current_element = current_element->next;
1972 }
1973 }
1974
1975 if ((current_element == NULL) || (current_element->string == NULL)) {
1976 return NULL;
1977 }
1978
1979 return current_element;
1980 }
1981
1982 CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
1983 {
1984 return get_object_item(object, string, false);
1985 }
1986
1987 CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string)
1988 {
1989 return get_object_item(object, string, true);
1990 }
1991
1992 CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
1993 {
1994 return cJSON_GetObjectItem(object, string) ? 1 : 0;
1995 }
1996
1997 /* Utility for array list handling. */
1998 static void suffix_object(cJSON *prev, cJSON *item)
1999 {
2000 prev->next = item;
2001 item->prev = prev;
2002 }
2003
2004 /* Utility for handling references. */
2005 static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
2006 {
2007 cJSON *reference = NULL;
2008 if (item == NULL)
2009 {
2010 return NULL;
2011 }
2012
2013 reference = cJSON_New_Item(hooks);
2014 if (reference == NULL)
2015 {
2016 return NULL;
2017 }
2018
2019 memcpy(reference, item, sizeof(cJSON));
2020 reference->string = NULL;
2021 reference->type |= cJSON_IsReference;
2022 reference->next = reference->prev = NULL;
2023 return reference;
2024 }
2025
2026 static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
2027 {
2028 cJSON *child = NULL;
2029
2030 if ((item == NULL) || (array == NULL) || (array == item))
2031 {
2032 return false;
2033 }
2034
2035 child = array->child;
2036 /*
2037 * To find the last item in array quickly, we use prev in array
2038 */
2039 if (child == NULL)
2040 {
2041 /* list is empty, start new one */
2042 array->child = item;
2043 item->prev = item;
2044 item->next = NULL;
2045 }
2046 else
2047 {
2048 /* append to the end */
2049 if (child->prev)
2050 {
2051 suffix_object(child->prev, item);
2052 array->child->prev = item;
2053 }
2054 }
2055
2056 return true;
2057 }
2058
2059 /* Add item to array/object. */
2060 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item)
2061 {
2062 return add_item_to_array(array, item);
2063 }
2064
2065 #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
2066 #pragma GCC diagnostic push
2067 #endif
2068 #ifdef __GNUC__
2069 #pragma GCC diagnostic ignored "-Wcast-qual"
2070 #endif
2071 /* helper function to cast away const */
2072 static void* cast_away_const(const void* string)
2073 {
2074 return (void*)string;
2075 }
2076 #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
2077 #pragma GCC diagnostic pop
2078 #endif
2079
2080
2081 static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key)
2082 {
2083 char *new_key = NULL;
2084 int new_type = cJSON_Invalid;
2085
2086 if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item))
2087 {
2088 return false;
2089 }
2090
2091 if (constant_key)
2092 {
2093 new_key = (char*)cast_away_const(string);
2094 new_type = item->type | cJSON_StringIsConst;
2095 }
2096 else
2097 {
2098 new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks);
2099 if (new_key == NULL)
2100 {
2101 return false;
2102 }
2103
2104 new_type = item->type & ~cJSON_StringIsConst;
2105 }
2106
2107 if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
2108 {
2109 hooks->deallocate(item->string);
2110 }
2111
2112 item->string = new_key;
2113 item->type = new_type;
2114
2115 return add_item_to_array(object, item);
2116 }
2117
2118 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
2119 {
2120 return add_item_to_object(object, string, item, &global_hooks, false);
2121 }
2122
2123 /* Add an item to an object with constant string as key */
2124 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
2125 {
2126 return add_item_to_object(object, string, item, &global_hooks, true);
2127 }
2128
2129 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
2130 {
2131 if (array == NULL)
2132 {
2133 return false;
2134 }
2135
2136 return add_item_to_array(array, create_reference(item, &global_hooks));
2137 }
2138
2139 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
2140 {
2141 if ((object == NULL) || (string == NULL))
2142 {
2143 return false;
2144 }
2145
2146 return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
2147 }
2148
2149 CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name)
2150 {
2151 cJSON *null = cJSON_CreateNull();
2152 if (add_item_to_object(object, name, null, &global_hooks, false))
2153 {
2154 return null;
2155 }
2156
2157 cJSON_Delete(null);
2158 return NULL;
2159 }
2160
2161 CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name)
2162 {
2163 cJSON *true_item = cJSON_CreateTrue();
2164 if (add_item_to_object(object, name, true_item, &global_hooks, false))
2165 {
2166 return true_item;
2167 }
2168
2169 cJSON_Delete(true_item);
2170 return NULL;
2171 }
2172
2173 CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name)
2174 {
2175 cJSON *false_item = cJSON_CreateFalse();
2176 if (add_item_to_object(object, name, false_item, &global_hooks, false))
2177 {
2178 return false_item;
2179 }
2180
2181 cJSON_Delete(false_item);
2182 return NULL;
2183 }
2184
2185 CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean)
2186 {
2187 cJSON *bool_item = cJSON_CreateBool(boolean);
2188 if (add_item_to_object(object, name, bool_item, &global_hooks, false))
2189 {
2190 return bool_item;
2191 }
2192
2193 cJSON_Delete(bool_item);
2194 return NULL;
2195 }
2196
2197 CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number)
2198 {
2199 cJSON *number_item = cJSON_CreateNumber(number);
2200 if (add_item_to_object(object, name, number_item, &global_hooks, false))
2201 {
2202 return number_item;
2203 }
2204
2205 cJSON_Delete(number_item);
2206 return NULL;
2207 }
2208
2209 CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)
2210 {
2211 cJSON *string_item = cJSON_CreateString(string);
2212 if (add_item_to_object(object, name, string_item, &global_hooks, false))
2213 {
2214 return string_item;
2215 }
2216
2217 cJSON_Delete(string_item);
2218 return NULL;
2219 }
2220
2221 CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw)
2222 {
2223 cJSON *raw_item = cJSON_CreateRaw(raw);
2224 if (add_item_to_object(object, name, raw_item, &global_hooks, false))
2225 {
2226 return raw_item;
2227 }
2228
2229 cJSON_Delete(raw_item);
2230 return NULL;
2231 }
2232
2233 CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name)
2234 {
2235 cJSON *object_item = cJSON_CreateObject();
2236 if (add_item_to_object(object, name, object_item, &global_hooks, false))
2237 {
2238 return object_item;
2239 }
2240
2241 cJSON_Delete(object_item);
2242 return NULL;
2243 }
2244
2245 CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name)
2246 {
2247 cJSON *array = cJSON_CreateArray();
2248 if (add_item_to_object(object, name, array, &global_hooks, false))
2249 {
2250 return array;
2251 }
2252
2253 cJSON_Delete(array);
2254 return NULL;
2255 }
2256
2257 CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
2258 {
2259 if ((parent == NULL) || (item == NULL) || (item != parent->child && item->prev == NULL))
2260 {
2261 return NULL;
2262 }
2263
2264 if (item != parent->child)
2265 {
2266 /* not the first element */
2267 item->prev->next = item->next;
2268 }
2269 if (item->next != NULL)
2270 {
2271 /* not the last element */
2272 item->next->prev = item->prev;
2273 }
2274
2275 if (item == parent->child)
2276 {
2277 /* first element */
2278 parent->child = item->next;
2279 }
2280 else if (item->next == NULL)
2281 {
2282 /* last element */
2283 parent->child->prev = item->prev;
2284 }
2285
2286 /* make sure the detached item doesn't point anywhere anymore */
2287 item->prev = NULL;
2288 item->next = NULL;
2289
2290 return item;
2291 }
2292
2293 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
2294 {
2295 if (which < 0)
2296 {
2297 return NULL;
2298 }
2299
2300 return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
2301 }
2302
2303 CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
2304 {
2305 cJSON_Delete(cJSON_DetachItemFromArray(array, which));
2306 }
2307
2308 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
2309 {
2310 cJSON *to_detach = cJSON_GetObjectItem(object, string);
2311
2312 return cJSON_DetachItemViaPointer(object, to_detach);
2313 }
2314
2315 CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string)
2316 {
2317 cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string);
2318
2319 return cJSON_DetachItemViaPointer(object, to_detach);
2320 }
2321
2322 CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
2323 {
2324 cJSON_Delete(cJSON_DetachItemFromObject(object, string));
2325 }
2326
2327 CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
2328 {
2329 cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string));
2330 }
2331
2332 /* Replace array/object items with new ones. */
2333 CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
2334 {
2335 cJSON *after_inserted = NULL;
2336
2337 if (which < 0 || newitem == NULL)
2338 {
2339 return false;
2340 }
2341
2342 after_inserted = get_array_item(array, (size_t)which);
2343 if (after_inserted == NULL)
2344 {
2345 return add_item_to_array(array, newitem);
2346 }
2347
2348 if (after_inserted != array->child && after_inserted->prev == NULL) {
2349 /* return false if after_inserted is a corrupted array item */
2350 return false;
2351 }
2352
2353 newitem->next = after_inserted;
2354 newitem->prev = after_inserted->prev;
2355 after_inserted->prev = newitem;
2356 if (after_inserted == array->child)
2357 {
2358 array->child = newitem;
2359 }
2360 else
2361 {
2362 newitem->prev->next = newitem;
2363 }
2364 return true;
2365 }
2366
2367 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
2368 {
2369 if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL))
2370 {
2371 return false;
2372 }
2373
2374 if (replacement == item)
2375 {
2376 return true;
2377 }
2378
2379 replacement->next = item->next;
2380 replacement->prev = item->prev;
2381
2382 if (replacement->next != NULL)
2383 {
2384 replacement->next->prev = replacement;
2385 }
2386 if (parent->child == item)
2387 {
2388 if (parent->child->prev == parent->child)
2389 {
2390 replacement->prev = replacement;
2391 }
2392 parent->child = replacement;
2393 }
2394 else
2395 { /*
2396 * To find the last item in array quickly, we use prev in array.
2397 * We can't modify the last item's next pointer where this item was the parent's child
2398 */
2399 if (replacement->prev != NULL)
2400 {
2401 replacement->prev->next = replacement;
2402 }
2403 if (replacement->next == NULL)
2404 {
2405 parent->child->prev = replacement;
2406 }
2407 }
2408
2409 item->next = NULL;
2410 item->prev = NULL;
2411 cJSON_Delete(item);
2412
2413 return true;
2414 }
2415
2416 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
2417 {
2418 if (which < 0)
2419 {
2420 return false;
2421 }
2422
2423 return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem);
2424 }
2425
2426 static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
2427 {
2428 if ((replacement == NULL) || (string == NULL))
2429 {
2430 return false;
2431 }
2432
2433 /* replace the name in the replacement */
2434 if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
2435 {
2436 cJSON_free(replacement->string);
2437 }
2438 replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
2439 if (replacement->string == NULL)
2440 {
2441 return false;
2442 }
2443
2444 replacement->type &= ~cJSON_StringIsConst;
2445
2446 return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
2447 }
2448
2449 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
2450 {
2451 return replace_item_in_object(object, string, newitem, false);
2452 }
2453
2454 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem)
2455 {
2456 return replace_item_in_object(object, string, newitem, true);
2457 }
2458
2459 /* Create basic types: */
2460 CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
2461 {
2462 cJSON *item = cJSON_New_Item(&global_hooks);
2463 if(item)
2464 {
2465 item->type = cJSON_NULL;
2466 }
2467
2468 return item;
2469 }
2470
2471 CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
2472 {
2473 cJSON *item = cJSON_New_Item(&global_hooks);
2474 if(item)
2475 {
2476 item->type = cJSON_True;
2477 }
2478
2479 return item;
2480 }
2481
2482 CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
2483 {
2484 cJSON *item = cJSON_New_Item(&global_hooks);
2485 if(item)
2486 {
2487 item->type = cJSON_False;
2488 }
2489
2490 return item;
2491 }
2492
2493 CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean)
2494 {
2495 cJSON *item = cJSON_New_Item(&global_hooks);
2496 if(item)
2497 {
2498 item->type = boolean ? cJSON_True : cJSON_False;
2499 }
2500
2501 return item;
2502 }
2503
2504 CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
2505 {
2506 cJSON *item = cJSON_New_Item(&global_hooks);
2507 if(item)
2508 {
2509 item->type = cJSON_Number;
2510 item->valuedouble = num;
2511
2512 /* use saturation in case of overflow */
2513 if (num >= INT_MAX)
2514 {
2515 item->valueint = INT_MAX;
2516 }
2517 else if (num <= (double)INT_MIN)
2518 {
2519 item->valueint = INT_MIN;
2520 }
2521 else
2522 {
2523 item->valueint = (int)num;
2524 }
2525 }
2526
2527 return item;
2528 }
2529
2530 CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
2531 {
2532 cJSON *item = cJSON_New_Item(&global_hooks);
2533 if(item)
2534 {
2535 item->type = cJSON_String;
2536 item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
2537 if(!item->valuestring)
2538 {
2539 cJSON_Delete(item);
2540 return NULL;
2541 }
2542 }
2543
2544 return item;
2545 }
2546
2547 CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
2548 {
2549 cJSON *item = cJSON_New_Item(&global_hooks);
2550 if (item != NULL)
2551 {
2552 item->type = cJSON_String | cJSON_IsReference;
2553 item->valuestring = (char*)cast_away_const(string);
2554 }
2555
2556 return item;
2557 }
2558
2559 CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child)
2560 {
2561 cJSON *item = cJSON_New_Item(&global_hooks);
2562 if (item != NULL) {
2563 item->type = cJSON_Object | cJSON_IsReference;
2564 item->child = (cJSON*)cast_away_const(child);
2565 }
2566
2567 return item;
2568 }
2569
2570 CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) {
2571 cJSON *item = cJSON_New_Item(&global_hooks);
2572 if (item != NULL) {
2573 item->type = cJSON_Array | cJSON_IsReference;
2574 item->child = (cJSON*)cast_away_const(child);
2575 }
2576
2577 return item;
2578 }
2579
2580 CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
2581 {
2582 cJSON *item = cJSON_New_Item(&global_hooks);
2583 if(item)
2584 {
2585 item->type = cJSON_Raw;
2586 item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
2587 if(!item->valuestring)
2588 {
2589 cJSON_Delete(item);
2590 return NULL;
2591 }
2592 }
2593
2594 return item;
2595 }
2596
2597 CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
2598 {
2599 cJSON *item = cJSON_New_Item(&global_hooks);
2600 if(item)
2601 {
2602 item->type=cJSON_Array;
2603 }
2604
2605 return item;
2606 }
2607
2608 CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void)
2609 {
2610 cJSON *item = cJSON_New_Item(&global_hooks);
2611 if (item)
2612 {
2613 item->type = cJSON_Object;
2614 }
2615
2616 return item;
2617 }
2618
2619 /* Create Arrays: */
2620 CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
2621 {
2622 size_t i = 0;
2623 cJSON *n = NULL;
2624 cJSON *p = NULL;
2625 cJSON *a = NULL;
2626
2627 if ((count < 0) || (numbers == NULL))
2628 {
2629 return NULL;
2630 }
2631
2632 a = cJSON_CreateArray();
2633
2634 for(i = 0; a && (i < (size_t)count); i++)
2635 {
2636 n = cJSON_CreateNumber(numbers[i]);
2637 if (!n)
2638 {
2639 cJSON_Delete(a);
2640 return NULL;
2641 }
2642 if(!i)
2643 {
2644 a->child = n;
2645 }
2646 else
2647 {
2648 suffix_object(p, n);
2649 }
2650 p = n;
2651 }
2652
2653 if (a && a->child) {
2654 a->child->prev = n;
2655 }
2656
2657 return a;
2658 }
2659
2660 CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
2661 {
2662 size_t i = 0;
2663 cJSON *n = NULL;
2664 cJSON *p = NULL;
2665 cJSON *a = NULL;
2666
2667 if ((count < 0) || (numbers == NULL))
2668 {
2669 return NULL;
2670 }
2671
2672 a = cJSON_CreateArray();
2673
2674 for(i = 0; a && (i < (size_t)count); i++)
2675 {
2676 n = cJSON_CreateNumber((double)numbers[i]);
2677 if(!n)
2678 {
2679 cJSON_Delete(a);
2680 return NULL;
2681 }
2682 if(!i)
2683 {
2684 a->child = n;
2685 }
2686 else
2687 {
2688 suffix_object(p, n);
2689 }
2690 p = n;
2691 }
2692
2693 if (a && a->child) {
2694 a->child->prev = n;
2695 }
2696
2697 return a;
2698 }
2699
2700 CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
2701 {
2702 size_t i = 0;
2703 cJSON *n = NULL;
2704 cJSON *p = NULL;
2705 cJSON *a = NULL;
2706
2707 if ((count < 0) || (numbers == NULL))
2708 {
2709 return NULL;
2710 }
2711
2712 a = cJSON_CreateArray();
2713
2714 for(i = 0; a && (i < (size_t)count); i++)
2715 {
2716 n = cJSON_CreateNumber(numbers[i]);
2717 if(!n)
2718 {
2719 cJSON_Delete(a);
2720 return NULL;
2721 }
2722 if(!i)
2723 {
2724 a->child = n;
2725 }
2726 else
2727 {
2728 suffix_object(p, n);
2729 }
2730 p = n;
2731 }
2732
2733 if (a && a->child) {
2734 a->child->prev = n;
2735 }
2736
2737 return a;
2738 }
2739
2740 CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count)
2741 {
2742 size_t i = 0;
2743 cJSON *n = NULL;
2744 cJSON *p = NULL;
2745 cJSON *a = NULL;
2746
2747 if ((count < 0) || (strings == NULL))
2748 {
2749 return NULL;
2750 }
2751
2752 a = cJSON_CreateArray();
2753
2754 for (i = 0; a && (i < (size_t)count); i++)
2755 {
2756 n = cJSON_CreateString(strings[i]);
2757 if(!n)
2758 {
2759 cJSON_Delete(a);
2760 return NULL;
2761 }
2762 if(!i)
2763 {
2764 a->child = n;
2765 }
2766 else
2767 {
2768 suffix_object(p,n);
2769 }
2770 p = n;
2771 }
2772
2773 if (a && a->child) {
2774 a->child->prev = n;
2775 }
2776
2777 return a;
2778 }
2779
2780 /* Duplication */
2781 cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse);
2782
2783 CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
2784 {
2785 return cJSON_Duplicate_rec(item, 0, recurse );
2786 }
2787
2788 cJSON * cJSON_Duplicate_rec(const cJSON *item, size_t depth, cJSON_bool recurse)
2789 {
2790 cJSON *newitem = NULL;
2791 cJSON *child = NULL;
2792 cJSON *next = NULL;
2793 cJSON *newchild = NULL;
2794
2795 /* Bail on bad ptr */
2796 if (!item)
2797 {
2798 goto fail;
2799 }
2800 /* Create new item */
2801 newitem = cJSON_New_Item(&global_hooks);
2802 if (!newitem)
2803 {
2804 goto fail;
2805 }
2806 /* Copy over all vars */
2807 newitem->type = item->type & (~cJSON_IsReference);
2808 newitem->valueint = item->valueint;
2809 newitem->valuedouble = item->valuedouble;
2810 if (item->valuestring)
2811 {
2812 newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
2813 if (!newitem->valuestring)
2814 {
2815 goto fail;
2816 }
2817 }
2818 if (item->string)
2819 {
2820 newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
2821 if (!newitem->string)
2822 {
2823 goto fail;
2824 }
2825 }
2826 /* If non-recursive, then we're done! */
2827 if (!recurse)
2828 {
2829 return newitem;
2830 }
2831 /* Walk the ->next chain for the child. */
2832 child = item->child;
2833 while (child != NULL)
2834 {
2835 if(depth >= CJSON_CIRCULAR_LIMIT) {
2836 goto fail;
2837 }
2838 newchild = cJSON_Duplicate_rec(child, depth + 1, true); /* Duplicate (with recurse) each item in the ->next chain */
2839 if (!newchild)
2840 {
2841 goto fail;
2842 }
2843 if (next != NULL)
2844 {
2845 /* If newitem->child already set, then crosswire ->prev and ->next and move on */
2846 next->next = newchild;
2847 newchild->prev = next;
2848 next = newchild;
2849 }
2850 else
2851 {
2852 /* Set newitem->child and move to it */
2853 newitem->child = newchild;
2854 next = newchild;
2855 }
2856 child = child->next;
2857 }
2858 if (newitem && newitem->child)
2859 {
2860 newitem->child->prev = newchild;
2861 }
2862
2863 return newitem;
2864
2865 fail:
2866 if (newitem != NULL)
2867 {
2868 cJSON_Delete(newitem);
2869 }
2870
2871 return NULL;
2872 }
2873
2874 static void skip_oneline_comment(char **input)
2875 {
2876 *input += static_strlen("//");
2877
2878 for (; (*input)[0] != '\0'; ++(*input))
2879 {
2880 if ((*input)[0] == '\n') {
2881 *input += static_strlen("\n");
2882 return;
2883 }
2884 }
2885 }
2886
2887 static void skip_multiline_comment(char **input)
2888 {
2889 *input += static_strlen("/*");
2890
2891 for (; (*input)[0] != '\0'; ++(*input))
2892 {
2893 if (((*input)[0] == '*') && ((*input)[1] == '/'))
2894 {
2895 *input += static_strlen("*/");
2896 return;
2897 }
2898 }
2899 }
2900
2901 static void minify_string(char **input, char **output) {
2902 (*output)[0] = (*input)[0];
2903 *input += static_strlen("\"");
2904 *output += static_strlen("\"");
2905
2906
2907 for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) {
2908 (*output)[0] = (*input)[0];
2909
2910 if ((*input)[0] == '\"') {
2911 (*output)[0] = '\"';
2912 *input += static_strlen("\"");
2913 *output += static_strlen("\"");
2914 return;
2915 } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) {
2916 (*output)[1] = (*input)[1];
2917 *input += static_strlen("\"");
2918 *output += static_strlen("\"");
2919 }
2920 }
2921 }
2922
2923 CJSON_PUBLIC(void) cJSON_Minify(char *json)
2924 {
2925 char *into = json;
2926
2927 if (json == NULL)
2928 {
2929 return;
2930 }
2931
2932 while (json[0] != '\0')
2933 {
2934 switch (json[0])
2935 {
2936 case ' ':
2937 case '\t':
2938 case '\r':
2939 case '\n':
2940 json++;
2941 break;
2942
2943 case '/':
2944 if (json[1] == '/')
2945 {
2946 skip_oneline_comment(&json);
2947 }
2948 else if (json[1] == '*')
2949 {
2950 skip_multiline_comment(&json);
2951 } else {
2952 json++;
2953 }
2954 break;
2955
2956 case '\"':
2957 minify_string(&json, (char**)&into);
2958 break;
2959
2960 default:
2961 into[0] = json[0];
2962 json++;
2963 into++;
2964 }
2965 }
2966
2967 /* and null-terminate. */
2968 *into = '\0';
2969 }
2970
2971 CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item)
2972 {
2973 if (item == NULL)
2974 {
2975 return false;
2976 }
2977
2978 return (item->type & 0xFF) == cJSON_Invalid;
2979 }
2980
2981 CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item)
2982 {
2983 if (item == NULL)
2984 {
2985 return false;
2986 }
2987
2988 return (item->type & 0xFF) == cJSON_False;
2989 }
2990
2991 CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item)
2992 {
2993 if (item == NULL)
2994 {
2995 return false;
2996 }
2997
2998 return (item->type & 0xff) == cJSON_True;
2999 }
3000
3001
3002 CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item)
3003 {
3004 if (item == NULL)
3005 {
3006 return false;
3007 }
3008
3009 return (item->type & (cJSON_True | cJSON_False)) != 0;
3010 }
3011 CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
3012 {
3013 if (item == NULL)
3014 {
3015 return false;
3016 }
3017
3018 return (item->type & 0xFF) == cJSON_NULL;
3019 }
3020
3021 CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
3022 {
3023 if (item == NULL)
3024 {
3025 return false;
3026 }
3027
3028 return (item->type & 0xFF) == cJSON_Number;
3029 }
3030
3031 CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
3032 {
3033 if (item == NULL)
3034 {
3035 return false;
3036 }
3037
3038 return (item->type & 0xFF) == cJSON_String;
3039 }
3040
3041 CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
3042 {
3043 if (item == NULL)
3044 {
3045 return false;
3046 }
3047
3048 return (item->type & 0xFF) == cJSON_Array;
3049 }
3050
3051 CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
3052 {
3053 if (item == NULL)
3054 {
3055 return false;
3056 }
3057
3058 return (item->type & 0xFF) == cJSON_Object;
3059 }
3060
3061 CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
3062 {
3063 if (item == NULL)
3064 {
3065 return false;
3066 }
3067
3068 return (item->type & 0xFF) == cJSON_Raw;
3069 }
3070
3071 CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive)
3072 {
3073 if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
3074 {
3075 return false;
3076 }
3077
3078 /* check if type is valid */
3079 switch (a->type & 0xFF)
3080 {
3081 case cJSON_False:
3082 case cJSON_True:
3083 case cJSON_NULL:
3084 case cJSON_Number:
3085 case cJSON_String:
3086 case cJSON_Raw:
3087 case cJSON_Array:
3088 case cJSON_Object:
3089 break;
3090
3091 default:
3092 return false;
3093 }
3094
3095 /* identical objects are equal */
3096 if (a == b)
3097 {
3098 return true;
3099 }
3100
3101 switch (a->type & 0xFF)
3102 {
3103 /* in these cases and equal type is enough */
3104 case cJSON_False:
3105 case cJSON_True:
3106 case cJSON_NULL:
3107 return true;
3108
3109 case cJSON_Number:
3110 if (compare_double(a->valuedouble, b->valuedouble))
3111 {
3112 return true;
3113 }
3114 return false;
3115
3116 case cJSON_String:
3117 case cJSON_Raw:
3118 if ((a->valuestring == NULL) || (b->valuestring == NULL))
3119 {
3120 return false;
3121 }
3122 if (strcmp(a->valuestring, b->valuestring) == 0)
3123 {
3124 return true;
3125 }
3126
3127 return false;
3128
3129 case cJSON_Array:
3130 {
3131 cJSON *a_element = a->child;
3132 cJSON *b_element = b->child;
3133
3134 for (; (a_element != NULL) && (b_element != NULL);)
3135 {
3136 if (!cJSON_Compare(a_element, b_element, case_sensitive))
3137 {
3138 return false;
3139 }
3140
3141 a_element = a_element->next;
3142 b_element = b_element->next;
3143 }
3144
3145 /* one of the arrays is longer than the other */
3146 if (a_element != b_element) {
3147 return false;
3148 }
3149
3150 return true;
3151 }
3152
3153 case cJSON_Object:
3154 {
3155 cJSON *a_element = NULL;
3156 cJSON *b_element = NULL;
3157 cJSON_ArrayForEach(a_element, a)
3158 {
3159 /* TODO This has O(n^2) runtime, which is horrible! */
3160 b_element = get_object_item(b, a_element->string, case_sensitive);
3161 if (b_element == NULL)
3162 {
3163 return false;
3164 }
3165
3166 if (!cJSON_Compare(a_element, b_element, case_sensitive))
3167 {
3168 return false;
3169 }
3170 }
3171
3172 /* doing this twice, once on a and b to prevent true comparison if a subset of b
3173 * TODO: Do this the proper way, this is just a fix for now */
3174 cJSON_ArrayForEach(b_element, b)
3175 {
3176 a_element = get_object_item(a, b_element->string, case_sensitive);
3177 if (a_element == NULL)
3178 {
3179 return false;
3180 }
3181
3182 if (!cJSON_Compare(b_element, a_element, case_sensitive))
3183 {
3184 return false;
3185 }
3186 }
3187
3188 return true;
3189 }
3190
3191 default:
3192 return false;
3193 }
3194 }
3195
3196 CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
3197 {
3198 return global_hooks.allocate(size);
3199 }
3200
3201 CJSON_PUBLIC(void) cJSON_free(void *object)
3202 {
3203 global_hooks.deallocate(object);
3204 object = NULL;
3205 }
3206 /* Amazon edit */
3207 /* NOLINTEND */