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