1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
38
39
40
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
86
87
88
92
93
94
98
99
108
109
118
119
120
121
122
123
124
131
132
133
134
135
139
140
144
145
152
153
154
155
156
162
163
164
178
181
182
183
184
185
186
187
188
189
190
191
192
193
197
198
199
200
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
225
226
227
231
232
233
234
238
239
240
241
251
252
253
254
255
256
257
258
259
263
268
273
274
275
276
277
278
279
280
281
282
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
319
320
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
344
348
350
351
352
353
354
355
356
360
361
362
363
364
368
372
376
377
378
379
380
381
382
383
384
385
386
390
394
398
399
400
401
402
403
404
405
406
407
411
412
416
421
422
426
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
456
457
462
463
468
469
470
474
475
478
479
480
492
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
540
541
542
543
544
545
546
551
552
553
554
555
556
557
558
559
560
561
562
563
567
568
569
573
577
578
579
580
581
582
583
588
589
590
591
595
596
597
598
602
603
605
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
635
639
643
647
648
653
654
655
656
657
658
660
661
662
663
664
665
666
667
668
669
670
675
676
677
678
679
680
684
685
686
687
688
689
690
691
692
697
698
703
704
705
706
707
712
713
714
715
716
717
722
723
726
731
737
743
749
754
755
756
762
763
767
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
793
794
795
796
797
798
813
817
818
819
820
821
825
826
827
828
829
830
831
835
836
837
838
839
843
844
845
846
849
852
855
858
861
862
863
868
877
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
901
902
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
924
925
926
927
928
929
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
953
960
961
962
963
964
965
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
991
992
993
994
995
996
997
1000
1003
1006
1009
1012
1015
1018
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1053
1054
1058
1059
1063
1064
1068
1069
1070
1071
1072
1073
1074
1075
1079
1080
1084
1085
1086
1087
1088
1089
1090
1091
1092
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1117
1118
1119
1120
1121
1122
1123
1124
1128
1129
1134
1135
1136
1144
1148
1149
1150
1151
1152
1156
1157
1158
1159
1160
1161
1162
1163
1167
1171
1172
1176
1177
1178
1179
1180
1181
1182
1183
1184
1188
1189
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1213
1214
1215
1219
1220
1221
1222
1230
1231
1232
1233
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1253
1254
1259
1260
1261
1262
1263
1264
1268
1269
1273
1274
1275
1276
1277
1278
1282
1283
1284
1288
1289
1290
1291
1292
1293
1294
1295
1301
1302
1303
1304
1305
1306
1307
1308
1309
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1331
1332
1333
1334
1340
1341
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1360
1361
1365
1366
1370
1371
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1388
1389
1390
1391
1400
1409
1418
1421
1422
1423
1424
1428
1429
1430
1431
1435
1436
1437
1438
1439
1442
1445
1448
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1463
1464
1465
1470
1471
1472
1473
1478
1479
1480
1485
1486
1487
1488
1489
1490
1491
1492
1493
1497
1498
1499
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1519
1520
1521
1522
1523
1527
1528
1529
1530
1531
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1562
1563
1564
1565
1566
1570
1571
1572
1573
1574
1575
1576
1577
1581
1582
1583
1584
1585
1586
1590
1591
1595
1596
1597
1598
1599
1600
1601
1602
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1623
1624
1625
1629
1630
1631
1632
1636
1637
1638
1643
1644
1645
1646
1647
1648
1649
1650
1651
1655
1656
1657
1662
1663
1664
1665
1666
1667
1668
1669
1670
1674
1675
1676
1677
1678
1682
1683
1684
1685
1686
1687
1688
1692
1693
1694
1695
1696
1700
1701
1702
1703
1704
1708
1709
1710
1711
1712
1715
1716
1717
1718
1719
1720
1721
1722
1723
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1742
1743
1744
1745
1746
1750
1751
1752
1753
1757
1758
1759
1760
1761
1762
1763
1764
1765
1769
1773
1774
1775
1776
1777
1781
1782
1783
1784
1785
1789
1790
1794
1795
1796
1797
1801
1802
1803
1804
1805
1806
1810
1814
1815
1819
1820
1821
1822
1823
1824
1825
1826
1830
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1855
1856
1857
1858
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1877
1878
1879
1884
1885
1886
1887
1888
1897
1898
1899
1900
1901
1902
1906
1907
1908
1915
1922
1923
1926
1927
1928
1929
1930
1934
1935
1939
1940
1944
1945
1946
1951
1952
1953
1954
1955
1956
1960
1961
1962
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1982
1983
1984
1987
1988
1989
1990
1991
1992
1993
1994
2003
2004
2005
2006
2007
2008
2012
2013
2014
2015
2016
2017
2018
2019
2020
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2038
2039
2044
2045
2046
2047
2051
2052
2053
2054
2055
2059
2060
2061
2062
2063
2064
2065
2066
2070
2071
2072
2076
2077
2086
2087
2096
2097
2098
2099
2100
2104
2105
2106
2107
2108
2109
2110
2111
2112
2116
2117
2118
2119
2120
2121
2122
2123
2124
2128
2129
2130
2131
2132
2133
2134
2135
2136
2140
2141
2142
2143
2144
2145
2146
2147
2148
2152
2153
2154
2155
2156
2157
2158
2159
2160
2164
2165
2166
2167
2168
2169
2170
2171
2172
2176
2177
2178
2179
2180
2181
2182
2183
2184
2188
2189
2190
2191
2192
2193
2194
2195
2196
2200
2201
2202
2203
2204
2205
2206
2207
2211
2212
2217
2222
2223
2228
2233
2234
2235
2236
2237
2238
2239
2240
2241
2250
2251
2255
2256
2262
2263
2269
2270
2274
2275
2279
2280
2281
2282
2283
2284
2285
2289
2290
2291
2295
2296
2300
2301
2302
2303
2304
2308
2312
2313
2314
2315
2316
2317
2321
2322
2326
2327
2328
2329
2330
2334
2342
2356
2357
2358
2359
2360
2361
2362
2363
2364
2373
2374
2375
2376
2380
2381
2382
2386
2387
2391
2392
2393
2394
2395
2396
2397
2401
2402
2406
2407
2408
2409
2410
2411
2415
2416
2417
2418
2419
2420
2421
2422
2426
2427
2428
2429
2430
2431
2432
2433
2437
2438
2439
2440
2441
2442
2443
2444
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2465
2469
2473
2474
2475
2476
2477
2478
2479
2480
2481
2491
2492
2493
2494
2495
2496
2497
2498
2503
2504
2505
2506
2507
2508
2509
2510
2514
2515
2516
2517
2518
2527
2528
2529
2530
2531
2541
2542
2543
2544
2545
2546
2547
2548
2552
2553
2554
2555
2556
2557
2558
2559
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2579
2580
2581
2582
2583
2584
2585
2590
2594
2598
2599
2600
2601
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2619
2620
2621
2622
2623
2624
2625
2630
2634
2638
2639
2640
2641
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2659
2660
2661
2662
2663
2664
2665
2670
2674
2678
2679
2680
2681
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2699
2700
2701
2702
2703
2704
2705
2710
2714
2718
2719
2720
2721
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2741
2742
2743
2747
2748
2749
2750
2751
2759
2767
2768
2772
2773
2774
2775
2776
2777
2781
2782
2783
2784
2785
2786
2787
2788
2794
2795
2796
2800
2801
2802
2803
2804
2808
2809
2810
2811
2812
2824
2825
2838
2839
2840
2841
2842
2843
2844
2845
2859
2860
2861
2862
2863
2864
2865
2869
2870
2871
2872
2873
2874
2875
2876
2877
2881
2894
2898
2902
2903
2904
2905
2906
2907
2908
2909
2918
2919
2928
2929
2938
2939
2940
2949
2958
2959
2968
2969
2978
2979
2988
2989
2998
2999
3008
3009
3010
3011
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3029
3031
3032
3033
3034
3038
3039
3040
3041
3042
3043
3044
3047
3054
3055
3067
3068
3069
3070
3071
3072
3073
3074
3078
3079
3080
3081
3082
3083
3084
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3103
3104
3108
3109
3110
3112
3113
3114
3115
3119
3120
3124
3125
3126
3127
3128
3129
3131
3132
3133
3134
3138
3139
/* ... */
#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
#define _CRT_SECURE_NO_DEPRECATE
#endif
#ifdef __GNUC__
#pragma GCC visibility push(default)
#endif
#if defined(_MSC_VER)
#pragma warning (push)
#pragma warning (disable : 4001)/* ... */
#endif
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <float.h>7 includes
#ifdef ENABLE_LOCALES
#include <locale.h>
#endif
#if defined(_MSC_VER)
#pragma warning (pop)
#endif
#ifdef __GNUC__
#pragma GCC visibility pop
#endif
#include "cJSON.h"
#ifdef true
#undef true
#endif
#define true ((cJSON_bool)1)
#ifdef false
#undef false
#endif
#define false ((cJSON_bool)0)
#ifndef isinf
#define isinf(d) (isnan((d - d)) && !isnan(d))
#endif
#ifndef isnan
#define isnan(d) (d != d)
#endif
#ifndef NAN
#ifdef _WIN32
#define NAN sqrt(-1.0)
#else
#define NAN 0.0/0.0
#endif/* ... */
#endif
typedef struct {
const unsigned char *json;
size_t position;
}{...} error;
static error global_error = { NULL, 0 };
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
{
return (const char*) (global_error.json + global_error.position);
}{...}
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item)
{
if (!cJSON_IsString(item))
{
return NULL;
}{...}
return item->valuestring;
}{...}
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item)
{
if (!cJSON_IsNumber(item))
{
return (double) NAN;
}{...}
return item->valuedouble;
}{...}
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 18)
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
#endif
CJSON_PUBLIC(const char*) cJSON_Version(void)
{
static char version[15];
sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);
return version;
}{...}
static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
{
if ((string1 == NULL) || (string2 == NULL))
{
return 1;
}{...}
if (string1 == string2)
{
return 0;
}{...}
for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
{
if (*string1 == '\0')
{
return 0;
}{...}
}{...}
return tolower(*string1) - tolower(*string2);
}{...}
typedef struct internal_hooks
{
void *(CJSON_CDECL *allocate)(size_t size);
void (CJSON_CDECL *deallocate)(void *pointer);
void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
}{...} internal_hooks;
#if defined(_MSC_VER)
static void * CJSON_CDECL internal_malloc(size_t size)
{
return malloc(size);
}{...}
static void CJSON_CDECL internal_free(void *pointer)
{
free(pointer);
}{...}
static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
{
return realloc(pointer, size);
}{...}
/* ... */#else
#define internal_malloc malloc
#define internal_free free
#define internal_realloc realloc/* ... */
#endif
#define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc };
static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
{
size_t length = 0;
unsigned char *copy = NULL;
if (string == NULL)
{
return NULL;
}{...}
length = strlen((const char*)string) + sizeof("");
copy = (unsigned char*)hooks->allocate(length);
if (copy == NULL)
{
return NULL;
}{...}
memcpy(copy, string, length);
return copy;
}{...}
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
{
if (hooks == NULL)
{
global_hooks.allocate = malloc;
global_hooks.deallocate = free;
global_hooks.reallocate = realloc;
return;
}{...}
global_hooks.allocate = malloc;
if (hooks->malloc_fn != NULL)
{
global_hooks.allocate = hooks->malloc_fn;
}{...}
global_hooks.deallocate = free;
if (hooks->free_fn != NULL)
{
global_hooks.deallocate = hooks->free_fn;
}{...}
global_hooks.reallocate = NULL;
if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free))
{
global_hooks.reallocate = realloc;
}{...}
}{...}
static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
{
cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON));
if (node)
{
memset(node, '\0', sizeof(cJSON));
}{...}
return node;
}{...}
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
{
cJSON *next = NULL;
while (item != NULL)
{
next = item->next;
if (!(item->type & cJSON_IsReference) && (item->child != NULL))
{
cJSON_Delete(item->child);
}{...}
if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
{
global_hooks.deallocate(item->valuestring);
item->valuestring = NULL;
}{...}
if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
{
global_hooks.deallocate(item->string);
item->string = NULL;
}{...}
global_hooks.deallocate(item);
item = next;
}{...}
}{...}
static unsigned char get_decimal_point(void)
{
#ifdef ENABLE_LOCALES
struct lconv *lconv = localeconv();
return (unsigned char) lconv->decimal_point[0];/* ... */
#else
return '.';
#endif
}{...}
typedef struct
{
const unsigned char *content;
size_t length;
size_t offset;
size_t depth;
internal_hooks hooks;
}{...} parse_buffer;
#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
{
double number = 0;
unsigned char *after_end = NULL;
unsigned char number_c_string[64];
unsigned char decimal_point = get_decimal_point();
size_t i = 0;
if ((input_buffer == NULL) || (input_buffer->content == NULL))
{
return false;
}{...}
/* ... */
for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
{
switch (buffer_at_offset(input_buffer)[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '+':
case '-':
case 'e':
case 'E':
number_c_string[i] = buffer_at_offset(input_buffer)[i];
break;
...
case '.':
number_c_string[i] = decimal_point;
break;
...
default:
goto loop_end;...
}{...}
}{...}
loop_end:
number_c_string[i] = '\0';
number = strtod((const char*)number_c_string, (char**)&after_end);
if (number_c_string == after_end)
{
return false;
}{...}
item->valuedouble = number;
if (number >= INT_MAX)
{
item->valueint = INT_MAX;
}{...}
else if (number <= (double)INT_MIN)
{
item->valueint = INT_MIN;
}{...}
else
{
item->valueint = (int)number;
}{...}
item->type = cJSON_Number;
input_buffer->offset += (size_t)(after_end - number_c_string);
return true;
}{...}
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
{
if (number >= INT_MAX)
{
object->valueint = INT_MAX;
}{...}
else if (number <= (double)INT_MIN)
{
object->valueint = INT_MIN;
}{...}
else
{
object->valueint = (int)number;
}{...}
return object->valuedouble = number;
}{...}
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{
char *copy = NULL;
if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference))
{
return NULL;
}{...}
if (object->valuestring == NULL || valuestring == NULL)
{
return NULL;
}{...}
if (strlen(valuestring) <= strlen(object->valuestring))
{
strcpy(object->valuestring, valuestring);
return object->valuestring;
}{...}
copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks);
if (copy == NULL)
{
return NULL;
}{...}
if (object->valuestring != NULL)
{
cJSON_free(object->valuestring);
}{...}
object->valuestring = copy;
return copy;
}{...}
typedef struct
{
unsigned char *buffer;
size_t length;
size_t offset;
size_t depth;
cJSON_bool noalloc;
cJSON_bool format;
internal_hooks hooks;
}{...} printbuffer;
static unsigned char* ensure(printbuffer * const p, size_t needed)
{
unsigned char *newbuffer = NULL;
size_t newsize = 0;
if ((p == NULL) || (p->buffer == NULL))
{
return NULL;
}{...}
if ((p->length > 0) && (p->offset >= p->length))
{
return NULL;
}{...}
if (needed > INT_MAX)
{
return NULL;
}{...}
needed += p->offset + 1;
if (needed <= p->length)
{
return p->buffer + p->offset;
}{...}
if (p->noalloc) {
return NULL;
}{...}
if (needed > (INT_MAX / 2))
{
if (needed <= INT_MAX)
{
newsize = INT_MAX;
}{...}
else
{
return NULL;
}{...}
}{...}
else
{
newsize = needed * 2;
}{...}
if (p->hooks.reallocate != NULL)
{
newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
if (newbuffer == NULL)
{
p->hooks.deallocate(p->buffer);
p->length = 0;
p->buffer = NULL;
return NULL;
}{...}
}{...}
else
{
newbuffer = (unsigned char*)p->hooks.allocate(newsize);
if (!newbuffer)
{
p->hooks.deallocate(p->buffer);
p->length = 0;
p->buffer = NULL;
return NULL;
}{...}
memcpy(newbuffer, p->buffer, p->offset + 1);
p->hooks.deallocate(p->buffer);
}{...}
p->length = newsize;
p->buffer = newbuffer;
return newbuffer + p->offset;
}{...}
static void update_offset(printbuffer * const buffer)
{
const unsigned char *buffer_pointer = NULL;
if ((buffer == NULL) || (buffer->buffer == NULL))
{
return;
}{...}
buffer_pointer = buffer->buffer + buffer->offset;
buffer->offset += strlen((const char*)buffer_pointer);
}{...}
static cJSON_bool compare_double(double a, double b)
{
double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b);
return (fabs(a - b) <= maxVal * DBL_EPSILON);
}{...}
static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer)
{
unsigned char *output_pointer = NULL;
double d = item->valuedouble;
int length = 0;
size_t i = 0;
unsigned char number_buffer[26] = {0};
unsigned char decimal_point = get_decimal_point();
double test = 0.0;
if (output_buffer == NULL)
{
return false;
}{...}
if (isnan(d) || isinf(d))
{
length = sprintf((char*)number_buffer, "null");
}{...}
else if(d == (double)item->valueint)
{
length = sprintf((char*)number_buffer, "%d", item->valueint);
}{...}
else
{
length = sprintf((char*)number_buffer, "%1.15g", d);
if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d))
{
length = sprintf((char*)number_buffer, "%1.17g", d);
}{...}
}{...}
if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
{
return false;
}{...}
output_pointer = ensure(output_buffer, (size_t)length + sizeof(""));
if (output_pointer == NULL)
{
return false;
}{...}
/* ... */
for (i = 0; i < ((size_t)length); i++)
{
if (number_buffer[i] == decimal_point)
{
output_pointer[i] = '.';
continue;
}{...}
output_pointer[i] = number_buffer[i];
}{...}
output_pointer[i] = '\0';
output_buffer->offset += (size_t)length;
return true;
}{...}
static unsigned parse_hex4(const unsigned char * const input)
{
unsigned int h = 0;
size_t i = 0;
for (i = 0; i < 4; i++)
{
if ((input[i] >= '0') && (input[i] <= '9'))
{
h += (unsigned int) input[i] - '0';
}{...}
else if ((input[i] >= 'A') && (input[i] <= 'F'))
{
h += (unsigned int) 10 + input[i] - 'A';
}{...}
else if ((input[i] >= 'a') && (input[i] <= 'f'))
{
h += (unsigned int) 10 + input[i] - 'a';
}{...}
else
{
return 0;
}{...}
if (i < 3)
{
h = h << 4;
}{...}
}{...}
return h;
}{...}
/* ... */
static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
{
long unsigned int codepoint = 0;
unsigned int first_code = 0;
const unsigned char *first_sequence = input_pointer;
unsigned char utf8_length = 0;
unsigned char utf8_position = 0;
unsigned char sequence_length = 0;
unsigned char first_byte_mark = 0;
if ((input_end - first_sequence) < 6)
{
goto fail;
}{...}
first_code = parse_hex4(first_sequence + 2);
if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
{
goto fail;
}{...}
if ((first_code >= 0xD800) && (first_code <= 0xDBFF))
{
const unsigned char *second_sequence = first_sequence + 6;
unsigned int second_code = 0;
sequence_length = 12;
if ((input_end - second_sequence) < 6)
{
goto fail;
}{...}
if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
{
goto fail;
}{...}
second_code = parse_hex4(second_sequence + 2);
if ((second_code < 0xDC00) || (second_code > 0xDFFF))
{
goto fail;
}{...}
codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF));
}{...}
else
{
sequence_length = 6;
codepoint = first_code;
}{...}
/* ... */
if (codepoint < 0x80)
{
utf8_length = 1;
}{...}
else if (codepoint < 0x800)
{
utf8_length = 2;
first_byte_mark = 0xC0;
}{...}
else if (codepoint < 0x10000)
{
utf8_length = 3;
first_byte_mark = 0xE0;
}{...}
else if (codepoint <= 0x10FFFF)
{
utf8_length = 4;
first_byte_mark = 0xF0;
}{...}
else
{
goto fail;
}{...}
for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
{
(*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
codepoint >>= 6;
}{...}
if (utf8_length > 1)
{
(*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
}{...}
else
{
(*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
}{...}
*output_pointer += utf8_length;
return sequence_length;
fail:
return 0;
}{...}
static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer)
{
const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
unsigned char *output_pointer = NULL;
unsigned char *output = NULL;
if (buffer_at_offset(input_buffer)[0] != '\"')
{
goto fail;
}{...}
{
size_t allocation_length = 0;
size_t skipped_bytes = 0;
while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
{
if (input_end[0] == '\\')
{
if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
{
goto fail;
}{...}
skipped_bytes++;
input_end++;
}{...}
input_end++;
}{...}
if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
{
goto fail;
}{...}
allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
if (output == NULL)
{
goto fail;
}{...}
}{...}
output_pointer = output;
while (input_pointer < input_end)
{
if (*input_pointer != '\\')
{
*output_pointer++ = *input_pointer++;
}{...}
else
{
unsigned char sequence_length = 2;
if ((input_end - input_pointer) < 1)
{
goto fail;
}{...}
switch (input_pointer[1])
{
case 'b':
*output_pointer++ = '\b';
break;...
case 'f':
*output_pointer++ = '\f';
break;...
case 'n':
*output_pointer++ = '\n';
break;...
case 'r':
*output_pointer++ = '\r';
break;...
case 't':
*output_pointer++ = '\t';
break;...
case '\"':
case '\\':
case '/':
*output_pointer++ = input_pointer[1];
break;
...
case 'u':
sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
if (sequence_length == 0)
{
goto fail;
}{...}
break;
...
default:
goto fail;...
}{...}
input_pointer += sequence_length;
}{...}
}{...}
*output_pointer = '\0';
item->type = cJSON_String;
item->valuestring = (char*)output;
input_buffer->offset = (size_t) (input_end - input_buffer->content);
input_buffer->offset++;
return true;
fail:
if (output != NULL)
{
input_buffer->hooks.deallocate(output);
output = NULL;
}{...}
if (input_pointer != NULL)
{
input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
}{...}
return false;
}{...}
static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer)
{
const unsigned char *input_pointer = NULL;
unsigned char *output = NULL;
unsigned char *output_pointer = NULL;
size_t output_length = 0;
size_t escape_characters = 0;
if (output_buffer == NULL)
{
return false;
}{...}
if (input == NULL)
{
output = ensure(output_buffer, sizeof("\"\""));
if (output == NULL)
{
return false;
}{...}
strcpy((char*)output, "\"\"");
return true;
}{...}
for (input_pointer = input; *input_pointer; input_pointer++)
{
switch (*input_pointer)
{
case '\"':
case '\\':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
escape_characters++;
break;...
default:
if (*input_pointer < 32)
{
escape_characters += 5;
}{...}
break;...
}{...}
}{...}
output_length = (size_t)(input_pointer - input) + escape_characters;
output = ensure(output_buffer, output_length + sizeof("\"\""));
if (output == NULL)
{
return false;
}{...}
if (escape_characters == 0)
{
output[0] = '\"';
memcpy(output + 1, input, output_length);
output[output_length + 1] = '\"';
output[output_length + 2] = '\0';
return true;
}{...}
output[0] = '\"';
output_pointer = output + 1;
for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
{
if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
{
*output_pointer = *input_pointer;
}{...}
else
{
*output_pointer++ = '\\';
switch (*input_pointer)
{
case '\\':
*output_pointer = '\\';
break;...
case '\"':
*output_pointer = '\"';
break;...
case '\b':
*output_pointer = 'b';
break;...
case '\f':
*output_pointer = 'f';
break;...
case '\n':
*output_pointer = 'n';
break;...
case '\r':
*output_pointer = 'r';
break;...
case '\t':
*output_pointer = 't';
break;...
default:
sprintf((char*)output_pointer, "u%04x", *input_pointer);
output_pointer += 4;
break;...
}{...}
}{...}
}{...}
output[output_length + 1] = '\"';
output[output_length + 2] = '\0';
return true;
}{...}
static cJSON_bool print_string(const cJSON * const item, printbuffer * const p)
{
return print_string_ptr((unsigned char*)item->valuestring, p);
}{...}
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer);
static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer);
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer);
static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer);
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer);
static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer);
static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
{
if ((buffer == NULL) || (buffer->content == NULL))
{
return NULL;
}{...}
if (cannot_access_at_index(buffer, 0))
{
return buffer;
}{...}
while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
{
buffer->offset++;
}{...}
if (buffer->offset == buffer->length)
{
buffer->offset--;
}{...}
return buffer;
}{...}
static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
{
if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0))
{
return NULL;
}{...}
if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0))
{
buffer->offset += 3;
}{...}
return buffer;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
{
size_t buffer_length;
if (NULL == value)
{
return NULL;
}{...}
buffer_length = strlen(value) + sizeof("");
return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated);
}{...}
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated)
{
parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
cJSON *item = NULL;
global_error.json = NULL;
global_error.position = 0;
if (value == NULL || 0 == buffer_length)
{
goto fail;
}{...}
buffer.content = (const unsigned char*)value;
buffer.length = buffer_length;
buffer.offset = 0;
buffer.hooks = global_hooks;
item = cJSON_New_Item(&global_hooks);
if (item == NULL)
{
goto fail;
}{...}
if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
{
goto fail;
}{...}
if (require_null_terminated)
{
buffer_skip_whitespace(&buffer);
if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
{
goto fail;
}{...}
}{...}
if (return_parse_end)
{
*return_parse_end = (const char*)buffer_at_offset(&buffer);
}{...}
return item;
fail:
if (item != NULL)
{
cJSON_Delete(item);
}{...}
if (value != NULL)
{
error local_error;
local_error.json = (const unsigned char*)value;
local_error.position = 0;
if (buffer.offset < buffer.length)
{
local_error.position = buffer.offset;
}{...}
else if (buffer.length > 0)
{
local_error.position = buffer.length - 1;
}{...}
if (return_parse_end != NULL)
{
*return_parse_end = (const char*)local_error.json + local_error.position;
}{...}
global_error = local_error;
}{...}
return NULL;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
{
return cJSON_ParseWithOpts(value, 0, 0);
}{...}
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length)
{
return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0);
}{...}
#define cjson_min(a, b) (((a) < (b)) ? (a) : (b))
static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
{
static const size_t default_buffer_size = 256;
printbuffer buffer[1];
unsigned char *printed = NULL;
memset(buffer, 0, sizeof(buffer));
buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
buffer->length = default_buffer_size;
buffer->format = format;
buffer->hooks = *hooks;
if (buffer->buffer == NULL)
{
goto fail;
}{...}
if (!print_value(item, buffer))
{
goto fail;
}{...}
update_offset(buffer);
if (hooks->reallocate != NULL)
{
printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
if (printed == NULL) {
goto fail;
}{...}
buffer->buffer = NULL;
}{...}
else
{
printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
if (printed == NULL)
{
goto fail;
}{...}
memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
printed[buffer->offset] = '\0';
hooks->deallocate(buffer->buffer);
buffer->buffer = NULL;
}{...}
return printed;
fail:
if (buffer->buffer != NULL)
{
hooks->deallocate(buffer->buffer);
buffer->buffer = NULL;
}{...}
if (printed != NULL)
{
hooks->deallocate(printed);
printed = NULL;
}{...}
return NULL;
}{...}
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item)
{
return (char*)print(item, true, &global_hooks);
}{...}
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
{
return (char*)print(item, false, &global_hooks);
}{...}
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
{
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
if (prebuffer < 0)
{
return NULL;
}{...}
p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer);
if (!p.buffer)
{
return NULL;
}{...}
p.length = (size_t)prebuffer;
p.offset = 0;
p.noalloc = false;
p.format = fmt;
p.hooks = global_hooks;
if (!print_value(item, &p))
{
global_hooks.deallocate(p.buffer);
p.buffer = NULL;
return NULL;
}{...}
return (char*)p.buffer;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format)
{
printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
if ((length < 0) || (buffer == NULL))
{
return false;
}{...}
p.buffer = (unsigned char*)buffer;
p.length = (size_t)length;
p.offset = 0;
p.noalloc = true;
p.format = format;
p.hooks = global_hooks;
return print_value(item, &p);
}{...}
static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer)
{
if ((input_buffer == NULL) || (input_buffer->content == NULL))
{
return false;
}{...}
if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
{
item->type = cJSON_NULL;
input_buffer->offset += 4;
return true;
}{...}
if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
{
item->type = cJSON_False;
input_buffer->offset += 5;
return true;
}{...}
if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
{
item->type = cJSON_True;
item->valueint = 1;
input_buffer->offset += 4;
return true;
}{...}
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
{
return parse_string(item, input_buffer);
}{...}
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'))))
{
return parse_number(item, input_buffer);
}{...}
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
{
return parse_array(item, input_buffer);
}{...}
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
{
return parse_object(item, input_buffer);
}{...}
return false;
}{...}
static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer)
{
unsigned char *output = NULL;
if ((item == NULL) || (output_buffer == NULL))
{
return false;
}{...}
switch ((item->type) & 0xFF)
{
case cJSON_NULL:
output = ensure(output_buffer, 5);
if (output == NULL)
{
return false;
}{...}
strcpy((char*)output, "null");
return true;
...
case cJSON_False:
output = ensure(output_buffer, 6);
if (output == NULL)
{
return false;
}{...}
strcpy((char*)output, "false");
return true;
...
case cJSON_True:
output = ensure(output_buffer, 5);
if (output == NULL)
{
return false;
}{...}
strcpy((char*)output, "true");
return true;
...
case cJSON_Number:
return print_number(item, output_buffer);
...
case cJSON_Raw:
{
size_t raw_length = 0;
if (item->valuestring == NULL)
{
return false;
}{...}
raw_length = strlen(item->valuestring) + sizeof("");
output = ensure(output_buffer, raw_length);
if (output == NULL)
{
return false;
}{...}
memcpy(output, item->valuestring, raw_length);
return true;
}{...}
...
case cJSON_String:
return print_string(item, output_buffer);
...
case cJSON_Array:
return print_array(item, output_buffer);
...
case cJSON_Object:
return print_object(item, output_buffer);
...
default:
return false;...
}{...}
}{...}
static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer)
{
cJSON *head = NULL;
cJSON *current_item = NULL;
if (input_buffer->depth >= CJSON_NESTING_LIMIT)
{
return false;
}{...}
input_buffer->depth++;
if (buffer_at_offset(input_buffer)[0] != '[')
{
goto fail;
}{...}
input_buffer->offset++;
buffer_skip_whitespace(input_buffer);
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']'))
{
goto success;
}{...}
if (cannot_access_at_index(input_buffer, 0))
{
input_buffer->offset--;
goto fail;
}{...}
input_buffer->offset--;
do
{
cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
if (new_item == NULL)
{
goto fail;
}{...}
if (head == NULL)
{
current_item = head = new_item;
}{...}
else
{
current_item->next = new_item;
new_item->prev = current_item;
current_item = new_item;
}{...}
input_buffer->offset++;
buffer_skip_whitespace(input_buffer);
if (!parse_value(current_item, input_buffer))
{
goto fail;
}{...}
buffer_skip_whitespace(input_buffer);
}{...}
while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
{
goto fail;
}{...}
success:
input_buffer->depth--;
if (head != NULL) {
head->prev = current_item;
}{...}
item->type = cJSON_Array;
item->child = head;
input_buffer->offset++;
return true;
fail:
if (head != NULL)
{
cJSON_Delete(head);
}{...}
return false;
}{...}
static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer)
{
unsigned char *output_pointer = NULL;
size_t length = 0;
cJSON *current_element = item->child;
if (output_buffer == NULL)
{
return false;
}{...}
output_pointer = ensure(output_buffer, 1);
if (output_pointer == NULL)
{
return false;
}{...}
*output_pointer = '[';
output_buffer->offset++;
output_buffer->depth++;
while (current_element != NULL)
{
if (!print_value(current_element, output_buffer))
{
return false;
}{...}
update_offset(output_buffer);
if (current_element->next)
{
length = (size_t) (output_buffer->format ? 2 : 1);
output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
return false;
}{...}
*output_pointer++ = ',';
if(output_buffer->format)
{
*output_pointer++ = ' ';
}{...}
*output_pointer = '\0';
output_buffer->offset += length;
}{...}
current_element = current_element->next;
}{...}
output_pointer = ensure(output_buffer, 2);
if (output_pointer == NULL)
{
return false;
}{...}
*output_pointer++ = ']';
*output_pointer = '\0';
output_buffer->depth--;
return true;
}{...}
static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer)
{
cJSON *head = NULL;
cJSON *current_item = NULL;
if (input_buffer->depth >= CJSON_NESTING_LIMIT)
{
return false;
}{...}
input_buffer->depth++;
if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
{
goto fail;
}{...}
input_buffer->offset++;
buffer_skip_whitespace(input_buffer);
if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}'))
{
goto success;
}{...}
if (cannot_access_at_index(input_buffer, 0))
{
input_buffer->offset--;
goto fail;
}{...}
input_buffer->offset--;
do
{
cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
if (new_item == NULL)
{
goto fail;
}{...}
if (head == NULL)
{
current_item = head = new_item;
}{...}
else
{
current_item->next = new_item;
new_item->prev = current_item;
current_item = new_item;
}{...}
if (cannot_access_at_index(input_buffer, 1))
{
goto fail;
}{...}
input_buffer->offset++;
buffer_skip_whitespace(input_buffer);
if (!parse_string(current_item, input_buffer))
{
goto fail;
}{...}
buffer_skip_whitespace(input_buffer);
current_item->string = current_item->valuestring;
current_item->valuestring = NULL;
if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
{
goto fail;
}{...}
input_buffer->offset++;
buffer_skip_whitespace(input_buffer);
if (!parse_value(current_item, input_buffer))
{
goto fail;
}{...}
buffer_skip_whitespace(input_buffer);
}{...}
while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
{
goto fail;
}{...}
success:
input_buffer->depth--;
if (head != NULL) {
head->prev = current_item;
}{...}
item->type = cJSON_Object;
item->child = head;
input_buffer->offset++;
return true;
fail:
if (head != NULL)
{
cJSON_Delete(head);
}{...}
return false;
}{...}
static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer)
{
unsigned char *output_pointer = NULL;
size_t length = 0;
cJSON *current_item = item->child;
if (output_buffer == NULL)
{
return false;
}{...}
length = (size_t) (output_buffer->format ? 2 : 1);
output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
return false;
}{...}
*output_pointer++ = '{';
output_buffer->depth++;
if (output_buffer->format)
{
*output_pointer++ = '\n';
}{...}
output_buffer->offset += length;
while (current_item)
{
if (output_buffer->format)
{
size_t i;
output_pointer = ensure(output_buffer, output_buffer->depth);
if (output_pointer == NULL)
{
return false;
}{...}
for (i = 0; i < output_buffer->depth; i++)
{
*output_pointer++ = '\t';
}{...}
output_buffer->offset += output_buffer->depth;
}{...}
if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
{
return false;
}{...}
update_offset(output_buffer);
length = (size_t) (output_buffer->format ? 2 : 1);
output_pointer = ensure(output_buffer, length);
if (output_pointer == NULL)
{
return false;
}{...}
*output_pointer++ = ':';
if (output_buffer->format)
{
*output_pointer++ = '\t';
}{...}
output_buffer->offset += length;
if (!print_value(current_item, output_buffer))
{
return false;
}{...}
update_offset(output_buffer);
length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0));
output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
return false;
}{...}
if (current_item->next)
{
*output_pointer++ = ',';
}{...}
if (output_buffer->format)
{
*output_pointer++ = '\n';
}{...}
*output_pointer = '\0';
output_buffer->offset += length;
current_item = current_item->next;
}{...}
output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
if (output_pointer == NULL)
{
return false;
}{...}
if (output_buffer->format)
{
size_t i;
for (i = 0; i < (output_buffer->depth - 1); i++)
{
*output_pointer++ = '\t';
}{...}
}{...}
*output_pointer++ = '}';
*output_pointer = '\0';
output_buffer->depth--;
return true;
}{...}
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
{
cJSON *child = NULL;
size_t size = 0;
if (array == NULL)
{
return 0;
}{...}
child = array->child;
while(child != NULL)
{
size++;
child = child->next;
}{...}
return (int)size;
}{...}
static cJSON* get_array_item(const cJSON *array, size_t index)
{
cJSON *current_child = NULL;
if (array == NULL)
{
return NULL;
}{...}
current_child = array->child;
while ((current_child != NULL) && (index > 0))
{
index--;
current_child = current_child->next;
}{...}
return current_child;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
{
if (index < 0)
{
return NULL;
}{...}
return get_array_item(array, (size_t)index);
}{...}
static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
{
cJSON *current_element = NULL;
if ((object == NULL) || (name == NULL))
{
return NULL;
}{...}
current_element = object->child;
if (case_sensitive)
{
while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0))
{
current_element = current_element->next;
}{...}
}{...}
else
{
while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
{
current_element = current_element->next;
}{...}
}{...}
if ((current_element == NULL) || (current_element->string == NULL)) {
return NULL;
}{...}
return current_element;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
{
return get_object_item(object, string, false);
}{...}
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string)
{
return get_object_item(object, string, true);
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
{
return cJSON_GetObjectItem(object, string) ? 1 : 0;
}{...}
static void suffix_object(cJSON *prev, cJSON *item)
{
prev->next = item;
item->prev = prev;
}{...}
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
{
cJSON *reference = NULL;
if (item == NULL)
{
return NULL;
}{...}
reference = cJSON_New_Item(hooks);
if (reference == NULL)
{
return NULL;
}{...}
memcpy(reference, item, sizeof(cJSON));
reference->string = NULL;
reference->type |= cJSON_IsReference;
reference->next = reference->prev = NULL;
return reference;
}{...}
static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
{
cJSON *child = NULL;
if ((item == NULL) || (array == NULL) || (array == item))
{
return false;
}{...}
child = array->child;
/* ... */
if (child == NULL)
{
array->child = item;
item->prev = item;
item->next = NULL;
}{...}
else
{
if (child->prev)
{
suffix_object(child->prev, item);
array->child->prev = item;
}{...}
}{...}
return true;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item)
{
return add_item_to_array(array, item);
}{...}
#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
#pragma GCC diagnostic push
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wcast-qual"
#endif
static void* cast_away_const(const void* string)
{
return (void*)string;
}{...}
#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
#pragma GCC diagnostic pop
#endif
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)
{
char *new_key = NULL;
int new_type = cJSON_Invalid;
if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item))
{
return false;
}{...}
if (constant_key)
{
new_key = (char*)cast_away_const(string);
new_type = item->type | cJSON_StringIsConst;
}{...}
else
{
new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks);
if (new_key == NULL)
{
return false;
}{...}
new_type = item->type & ~cJSON_StringIsConst;
}{...}
if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
{
hooks->deallocate(item->string);
}{...}
item->string = new_key;
item->type = new_type;
return add_item_to_array(object, item);
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
{
return add_item_to_object(object, string, item, &global_hooks, false);
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
{
return add_item_to_object(object, string, item, &global_hooks, true);
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
{
if (array == NULL)
{
return false;
}{...}
return add_item_to_array(array, create_reference(item, &global_hooks));
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
{
if ((object == NULL) || (string == NULL))
{
return false;
}{...}
return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name)
{
cJSON *null = cJSON_CreateNull();
if (add_item_to_object(object, name, null, &global_hooks, false))
{
return null;
}{...}
cJSON_Delete(null);
return NULL;
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name)
{
cJSON *true_item = cJSON_CreateTrue();
if (add_item_to_object(object, name, true_item, &global_hooks, false))
{
return true_item;
}{...}
cJSON_Delete(true_item);
return NULL;
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name)
{
cJSON *false_item = cJSON_CreateFalse();
if (add_item_to_object(object, name, false_item, &global_hooks, false))
{
return false_item;
}{...}
cJSON_Delete(false_item);
return NULL;
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean)
{
cJSON *bool_item = cJSON_CreateBool(boolean);
if (add_item_to_object(object, name, bool_item, &global_hooks, false))
{
return bool_item;
}{...}
cJSON_Delete(bool_item);
return NULL;
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number)
{
cJSON *number_item = cJSON_CreateNumber(number);
if (add_item_to_object(object, name, number_item, &global_hooks, false))
{
return number_item;
}{...}
cJSON_Delete(number_item);
return NULL;
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)
{
cJSON *string_item = cJSON_CreateString(string);
if (add_item_to_object(object, name, string_item, &global_hooks, false))
{
return string_item;
}{...}
cJSON_Delete(string_item);
return NULL;
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw)
{
cJSON *raw_item = cJSON_CreateRaw(raw);
if (add_item_to_object(object, name, raw_item, &global_hooks, false))
{
return raw_item;
}{...}
cJSON_Delete(raw_item);
return NULL;
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name)
{
cJSON *object_item = cJSON_CreateObject();
if (add_item_to_object(object, name, object_item, &global_hooks, false))
{
return object_item;
}{...}
cJSON_Delete(object_item);
return NULL;
}{...}
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name)
{
cJSON *array = cJSON_CreateArray();
if (add_item_to_object(object, name, array, &global_hooks, false))
{
return array;
}{...}
cJSON_Delete(array);
return NULL;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
{
if ((parent == NULL) || (item == NULL))
{
return NULL;
}{...}
if (item != parent->child)
{
item->prev->next = item->next;
}{...}
if (item->next != NULL)
{
item->next->prev = item->prev;
}{...}
if (item == parent->child)
{
parent->child = item->next;
}{...}
else if (item->next == NULL)
{
parent->child->prev = item->prev;
}{...}
item->prev = NULL;
item->next = NULL;
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
{
if (which < 0)
{
return NULL;
}{...}
return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
}{...}
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
{
cJSON_Delete(cJSON_DetachItemFromArray(array, which));
}{...}
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
{
cJSON *to_detach = cJSON_GetObjectItem(object, string);
return cJSON_DetachItemViaPointer(object, to_detach);
}{...}
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string)
{
cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string);
return cJSON_DetachItemViaPointer(object, to_detach);
}{...}
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
{
cJSON_Delete(cJSON_DetachItemFromObject(object, string));
}{...}
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
{
cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string));
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
{
cJSON *after_inserted = NULL;
if (which < 0 || newitem == NULL)
{
return false;
}{...}
after_inserted = get_array_item(array, (size_t)which);
if (after_inserted == NULL)
{
return add_item_to_array(array, newitem);
}{...}
if (after_inserted != array->child && after_inserted->prev == NULL) {
return false;
}{...}
newitem->next = after_inserted;
newitem->prev = after_inserted->prev;
after_inserted->prev = newitem;
if (after_inserted == array->child)
{
array->child = newitem;
}{...}
else
{
newitem->prev->next = newitem;
}{...}
return true;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
{
if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL))
{
return false;
}{...}
if (replacement == item)
{
return true;
}{...}
replacement->next = item->next;
replacement->prev = item->prev;
if (replacement->next != NULL)
{
replacement->next->prev = replacement;
}{...}
if (parent->child == item)
{
if (parent->child->prev == parent->child)
{
replacement->prev = replacement;
}{...}
parent->child = replacement;
}{...}
else
{
/* ... */
if (replacement->prev != NULL)
{
replacement->prev->next = replacement;
}{...}
if (replacement->next == NULL)
{
parent->child->prev = replacement;
}{...}
}{...}
item->next = NULL;
item->prev = NULL;
cJSON_Delete(item);
return true;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
{
if (which < 0)
{
return false;
}{...}
return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem);
}{...}
static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
{
if ((replacement == NULL) || (string == NULL))
{
return false;
}{...}
if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
{
cJSON_free(replacement->string);
}{...}
replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
if (replacement->string == NULL)
{
return false;
}{...}
replacement->type &= ~cJSON_StringIsConst;
return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
{
return replace_item_in_object(object, string, newitem, false);
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem)
{
return replace_item_in_object(object, string, newitem, true);
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
{
item->type = cJSON_NULL;
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
{
item->type = cJSON_True;
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
{
item->type = cJSON_False;
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
{
item->type = boolean ? cJSON_True : cJSON_False;
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
{
item->type = cJSON_Number;
item->valuedouble = num;
if (num >= INT_MAX)
{
item->valueint = INT_MAX;
}{...}
else if (num <= (double)INT_MIN)
{
item->valueint = INT_MIN;
}{...}
else
{
item->valueint = (int)num;
}{...}
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
{
item->type = cJSON_String;
item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
if(!item->valuestring)
{
cJSON_Delete(item);
return NULL;
}{...}
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if (item != NULL)
{
item->type = cJSON_String | cJSON_IsReference;
item->valuestring = (char*)cast_away_const(string);
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if (item != NULL) {
item->type = cJSON_Object | cJSON_IsReference;
item->child = (cJSON*)cast_away_const(child);
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) {
cJSON *item = cJSON_New_Item(&global_hooks);
if (item != NULL) {
item->type = cJSON_Array | cJSON_IsReference;
item->child = (cJSON*)cast_away_const(child);
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
{
item->type = cJSON_Raw;
item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks);
if(!item->valuestring)
{
cJSON_Delete(item);
return NULL;
}{...}
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
{
item->type=cJSON_Array;
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if (item)
{
item->type = cJSON_Object;
}{...}
return item;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
{
size_t i = 0;
cJSON *n = NULL;
cJSON *p = NULL;
cJSON *a = NULL;
if ((count < 0) || (numbers == NULL))
{
return NULL;
}{...}
a = cJSON_CreateArray();
for(i = 0; a && (i < (size_t)count); i++)
{
n = cJSON_CreateNumber(numbers[i]);
if (!n)
{
cJSON_Delete(a);
return NULL;
}{...}
if(!i)
{
a->child = n;
}{...}
else
{
suffix_object(p, n);
}{...}
p = n;
}{...}
if (a && a->child) {
a->child->prev = n;
}{...}
return a;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
{
size_t i = 0;
cJSON *n = NULL;
cJSON *p = NULL;
cJSON *a = NULL;
if ((count < 0) || (numbers == NULL))
{
return NULL;
}{...}
a = cJSON_CreateArray();
for(i = 0; a && (i < (size_t)count); i++)
{
n = cJSON_CreateNumber((double)numbers[i]);
if(!n)
{
cJSON_Delete(a);
return NULL;
}{...}
if(!i)
{
a->child = n;
}{...}
else
{
suffix_object(p, n);
}{...}
p = n;
}{...}
if (a && a->child) {
a->child->prev = n;
}{...}
return a;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
{
size_t i = 0;
cJSON *n = NULL;
cJSON *p = NULL;
cJSON *a = NULL;
if ((count < 0) || (numbers == NULL))
{
return NULL;
}{...}
a = cJSON_CreateArray();
for(i = 0; a && (i < (size_t)count); i++)
{
n = cJSON_CreateNumber(numbers[i]);
if(!n)
{
cJSON_Delete(a);
return NULL;
}{...}
if(!i)
{
a->child = n;
}{...}
else
{
suffix_object(p, n);
}{...}
p = n;
}{...}
if (a && a->child) {
a->child->prev = n;
}{...}
return a;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count)
{
size_t i = 0;
cJSON *n = NULL;
cJSON *p = NULL;
cJSON *a = NULL;
if ((count < 0) || (strings == NULL))
{
return NULL;
}{...}
a = cJSON_CreateArray();
for (i = 0; a && (i < (size_t)count); i++)
{
n = cJSON_CreateString(strings[i]);
if(!n)
{
cJSON_Delete(a);
return NULL;
}{...}
if(!i)
{
a->child = n;
}{...}
else
{
suffix_object(p,n);
}{...}
p = n;
}{...}
if (a && a->child) {
a->child->prev = n;
}{...}
return a;
}{...}
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
{
cJSON *newitem = NULL;
cJSON *child = NULL;
cJSON *next = NULL;
cJSON *newchild = NULL;
if (!item)
{
goto fail;
}{...}
newitem = cJSON_New_Item(&global_hooks);
if (!newitem)
{
goto fail;
}{...}
newitem->type = item->type & (~cJSON_IsReference);
newitem->valueint = item->valueint;
newitem->valuedouble = item->valuedouble;
if (item->valuestring)
{
newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks);
if (!newitem->valuestring)
{
goto fail;
}{...}
}{...}
if (item->string)
{
newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks);
if (!newitem->string)
{
goto fail;
}{...}
}{...}
if (!recurse)
{
return newitem;
}{...}
child = item->child;
while (child != NULL)
{
newchild = cJSON_Duplicate(child, true);
if (!newchild)
{
goto fail;
}{...}
if (next != NULL)
{
next->next = newchild;
newchild->prev = next;
next = newchild;
}{...}
else
{
newitem->child = newchild;
next = newchild;
}{...}
child = child->next;
}{...}
if (newitem && newitem->child)
{
newitem->child->prev = newchild;
}{...}
return newitem;
fail:
if (newitem != NULL)
{
cJSON_Delete(newitem);
}{...}
return NULL;
}{...}
static void skip_oneline_comment(char **input)
{
*input += static_strlen("//");
for (; (*input)[0] != '\0'; ++(*input))
{
if ((*input)[0] == '\n') {
*input += static_strlen("\n");
return;
}{...}
}{...}
}{...}
static void skip_multiline_comment(char **input)
{
*input += static_strlen("/*");
for (; (*input)[0] != '\0'; ++(*input))
{
if (((*input)[0] == '*') && ((*input)[1] == '/'))
{
*input += static_strlen("*/");
return;
}{...}
}{...}
}{...}
static void minify_string(char **input, char **output) {
(*output)[0] = (*input)[0];
*input += static_strlen("\"");
*output += static_strlen("\"");
for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) {
(*output)[0] = (*input)[0];
if ((*input)[0] == '\"') {
(*output)[0] = '\"';
*input += static_strlen("\"");
*output += static_strlen("\"");
return;
}{...} else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) {
(*output)[1] = (*input)[1];
*input += static_strlen("\"");
*output += static_strlen("\"");
}{...}
}{...}
}{...}
CJSON_PUBLIC(void) cJSON_Minify(char *json)
{
char *into = json;
if (json == NULL)
{
return;
}{...}
while (json[0] != '\0')
{
switch (json[0])
{
case ' ':
case '\t':
case '\r':
case '\n':
json++;
break;
...
case '/':
if (json[1] == '/')
{
skip_oneline_comment(&json);
}{...}
else if (json[1] == '*')
{
skip_multiline_comment(&json);
}{...} else {
json++;
}{...}
break;
...
case '\"':
minify_string(&json, (char**)&into);
break;
...
default:
into[0] = json[0];
json++;
into++;...
}{...}
}{...}
*into = '\0';
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xFF) == cJSON_Invalid;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xFF) == cJSON_False;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xff) == cJSON_True;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & (cJSON_True | cJSON_False)) != 0;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xFF) == cJSON_NULL;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xFF) == cJSON_Number;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xFF) == cJSON_String;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xFF) == cJSON_Array;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xFF) == cJSON_Object;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
{
if (item == NULL)
{
return false;
}{...}
return (item->type & 0xFF) == cJSON_Raw;
}{...}
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive)
{
if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
{
return false;
}{...}
switch (a->type & 0xFF)
{
case cJSON_False:
case cJSON_True:
case cJSON_NULL:
case cJSON_Number:
case cJSON_String:
case cJSON_Raw:
case cJSON_Array:
case cJSON_Object:
break;
...
default:
return false;...
}{...}
if (a == b)
{
return true;
}{...}
switch (a->type & 0xFF)
{
case cJSON_False:
case cJSON_True:
case cJSON_NULL:
return true;
...
case cJSON_Number:
if (compare_double(a->valuedouble, b->valuedouble))
{
return true;
}{...}
return false;
...
case cJSON_String:
case cJSON_Raw:
if ((a->valuestring == NULL) || (b->valuestring == NULL))
{
return false;
}{...}
if (strcmp(a->valuestring, b->valuestring) == 0)
{
return true;
}{...}
return false;
...
case cJSON_Array:
{
cJSON *a_element = a->child;
cJSON *b_element = b->child;
for (; (a_element != NULL) && (b_element != NULL);)
{
if (!cJSON_Compare(a_element, b_element, case_sensitive))
{
return false;
}{...}
a_element = a_element->next;
b_element = b_element->next;
}{...}
if (a_element != b_element) {
return false;
}{...}
return true;
}{...}
...
case cJSON_Object:
{
cJSON *a_element = NULL;
cJSON *b_element = NULL;
cJSON_ArrayForEach(a_element, a)
{
b_element = get_object_item(b, a_element->string, case_sensitive);
if (b_element == NULL)
{
return false;
}{...}
if (!cJSON_Compare(a_element, b_element, case_sensitive))
{
return false;
}{...}
}{...}
/* ... */
cJSON_ArrayForEach(b_element, b)
{
a_element = get_object_item(a, b_element->string, case_sensitive);
if (a_element == NULL)
{
return false;
}{...}
if (!cJSON_Compare(b_element, a_element, case_sensitive))
{
return false;
}{...}
}{...}
return true;
}{...}
...
default:
return false;...
}{...}
}{...}
CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
{
return global_hooks.allocate(size);
}{...}
CJSON_PUBLIC(void) cJSON_free(void *object)
{
global_hooks.deallocate(object);
object = NULL;
}{...}