-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSON_parser.c
executable file
·1028 lines (908 loc) · 31.5 KB
/
JSON_parser.c
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
36
37
38
39
40
41
42
43
44
45
46
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Definitions for JSON_Parser */
#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include "JSON_parser.h"
#ifdef _MSC_VER
# if _MSC_VER >= 1400
# pragma warning(disable:4996)
# pragma warning(disable:4127)
# endif
#endif
#define true 1
#define false 0
#define __ -1
#ifndef JSON_PARSER_STACK_SIZE
# define JSON_PARSER_STACK_SIZE 128
#endif
#ifndef JSON_PARSER_PARSE_BUFFER_SIZE
# define JSON_PARSER_PARSE_BUFFER_SIZE 3500
#endif
typedef void* (*JSON_debug_malloc_t)(size_t bytes, const char* reason);
#ifdef JSON_PARSER_DEBUG_MALLOC
# define JSON_parser_malloc(func, bytes, reason) ((JSON_debug_malloc_t)func)(bytes, reason)
#else
# define JSON_parser_malloc(func, bytes, reason) func(bytes)
#endif
typedef unsigned short UTF16;
struct JSON_parser_struct {
JSON_parser_callback callback;
void* ctx;
signed char state, before_comment_state, type, escaped, comment, allow_comments, handle_floats_manually, error;
char decimal_point;
UTF16 utf16_high_surrogate;
int current_char;
int depth;
int top;
int stack_capacity;
signed char* stack;
char* parse_buffer;
size_t parse_buffer_capacity;
size_t parse_buffer_count;
signed char static_stack[JSON_PARSER_STACK_SIZE];
char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE];
JSON_malloc_t malloc;
JSON_free_t free;
};
#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
enum classes {
C_SPACE, /* space */
C_WHITE, /* other whitespace */
C_LCURB, /* { */
C_RCURB, /* } */
C_LSQRB, /* [ */
C_RSQRB, /* ] */
C_COLON, /* : */
C_COMMA, /* , */
C_QUOTE, /* " */
C_BACKS, /* \ */
C_SLASH, /* / */
C_PLUS, /* + */
C_MINUS, /* - */
C_POINT, /* . */
C_ZERO , /* 0 */
C_DIGIT, /* 123456789 */
C_LOW_A, /* a */
C_LOW_B, /* b */
C_LOW_C, /* c */
C_LOW_D, /* d */
C_LOW_E, /* e */
C_LOW_F, /* f */
C_LOW_L, /* l */
C_LOW_N, /* n */
C_LOW_R, /* r */
C_LOW_S, /* s */
C_LOW_T, /* t */
C_LOW_U, /* u */
C_ABCDF, /* ABCDF */
C_E, /* E */
C_ETC, /* everything else */
C_STAR, /* * */
NR_CLASSES
};
static const signed char ascii_class[128] = {
__, __, __, __, __, __, __, __,
__, C_WHITE, C_WHITE, __, __, C_WHITE, __, __,
__, __, __, __, __, __, __, __,
__, __, __, __, __, __, __, __,
C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,
C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_LSQRB, C_BACKS, C_RSQRB, C_ETC, C_ETC,
C_ETC, C_LOW_A, C_LOW_B, C_LOW_C, C_LOW_D, C_LOW_E, C_LOW_F, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_LOW_L, C_ETC, C_LOW_N, C_ETC,
C_ETC, C_ETC, C_LOW_R, C_LOW_S, C_LOW_T, C_LOW_U, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_LCURB, C_ETC, C_RCURB, C_ETC, C_ETC
};
enum states {
GO, /* start */
OK, /* ok */
OB, /* object */
KE, /* key */
CO, /* colon */
VA, /* value */
AR, /* array */
ST, /* string */
ES, /* escape */
U1, /* u1 */
U2, /* u2 */
U3, /* u3 */
U4, /* u4 */
MI, /* minus */
ZE, /* zero */
IT, /* integer */
FR, /* fraction */
E1, /* e */
E2, /* ex */
E3, /* exp */
T1, /* tr */
T2, /* tru */
T3, /* true */
F1, /* fa */
F2, /* fal */
F3, /* fals */
F4, /* false */
N1, /* nu */
N2, /* nul */
N3, /* null */
C1, /* / */
C2, /* / * */
C3, /* * */
FX, /* *.* *eE* */
D1, /* second UTF-16 character decoding started by \ */
D2, /* second UTF-16 character proceeded by u */
NR_STATES
};
enum actions
{
CB = -10, /* comment begin */
CE = -11, /* comment end */
FA = -12, /* false */
TR = -13, /* false */
NU = -14, /* null */
DE = -15, /* double detected by exponent e E */
DF = -16, /* double detected by fraction . */
SB = -17, /* string begin */
MX = -18, /* integer detected by minus */
ZX = -19, /* integer detected by zero */
IX = -20, /* integer detected by 1-9 */
EX = -21, /* next char is escaped */
UC = -22 /* Unicode character read */
};
static const signed char state_transition_table[NR_STATES][NR_CLASSES] = {
/*
white 1-9 ABCDF etc
space | { } [ ] : , " \ / + - . 0 | a b c d e f l n r s t u | E | * */
/*start GO*/ {GO,GO,-6,__,-5,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*ok OK*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*object OB*/ {OB,OB,__,-9,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*key KE*/ {KE,KE,__,__,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*colon CO*/ {CO,CO,__,__,__,__,-2,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*value VA*/ {VA,VA,-6,__,-5,__,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
/*array AR*/ {AR,AR,-6,__,-5,-7,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
/*string ST*/ {ST,__,ST,ST,ST,ST,ST,ST,-4,EX,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST},
/*escape ES*/ {__,__,__,__,__,__,__,__,ST,ST,ST,__,__,__,__,__,__,ST,__,__,__,ST,__,ST,ST,__,ST,U1,__,__,__,__},
/*u1 U1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U2,U2,U2,U2,U2,U2,U2,U2,__,__,__,__,__,__,U2,U2,__,__},
/*u2 U2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U3,U3,U3,U3,U3,U3,U3,U3,__,__,__,__,__,__,U3,U3,__,__},
/*u3 U3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U4,U4,U4,U4,U4,U4,U4,U4,__,__,__,__,__,__,U4,U4,__,__},
/*u4 U4*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,UC,UC,UC,UC,UC,UC,UC,UC,__,__,__,__,__,__,UC,UC,__,__},
/*minus MI*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,ZE,IT,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*zero ZE*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*int IT*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,IT,IT,__,__,__,__,DE,__,__,__,__,__,__,__,__,DE,__,__},
/*frac FR*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
/*e E1*/ {__,__,__,__,__,__,__,__,__,__,__,E2,E2,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*ex E2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*exp E3*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*tr T1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T2,__,__,__,__,__,__,__},
/*tru T2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T3,__,__,__,__},
/*true T3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
/*fa F1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*fal F2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F3,__,__,__,__,__,__,__,__,__},
/*fals F3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F4,__,__,__,__,__,__},
/*false F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
/*nu N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__},
/*nul N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__},
/*null N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__},
/*/ C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2},
/*/* C2*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3},
/** C3*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,CE,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3},
/*_. FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
/*\ D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
/*\ D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__},
};
enum modes {
MODE_ARRAY = 1,
MODE_DONE = 2,
MODE_KEY = 3,
MODE_OBJECT = 4
};
static void set_error(JSON_parser jc)
{
switch (jc->state) {
case GO:
switch (jc->current_char) {
case '{': case '}': case '[': case ']':
jc->error = JSON_E_UNBALANCED_COLLECTION;
break;
default:
jc->error = JSON_E_INVALID_CHAR;
break;
}
break;
case OB:
jc->error = JSON_E_EXPECTED_KEY;
break;
case AR:
jc->error = JSON_E_UNBALANCED_COLLECTION;
break;
case CO:
jc->error = JSON_E_EXPECTED_COLON;
break;
case KE:
jc->error = JSON_E_EXPECTED_KEY;
break;
/* \uXXXX\uYYYY */
case U1: case U2: case U3: case U4: case D1: case D2:
jc->error = JSON_E_INVALID_UNICODE_SEQUENCE;
break;
/* true, false, null */
case T1: case T2: case T3: case F1: case F2: case F3: case F4: case N1: case N2: case N3:
jc->error = JSON_E_INVALID_KEYWORD;
break;
/* minus, integer, fraction, exponent */
case MI: case ZE: case IT: case FR: case E1: case E2: case E3:
jc->error = JSON_E_INVALID_NUMBER;
break;
default:
jc->error = JSON_E_INVALID_CHAR;
break;
}
}
static int
push(JSON_parser jc, int mode)
{
assert(jc->top <= jc->stack_capacity);
if (jc->depth < 0) {
if (jc->top == jc->stack_capacity) {
const size_t bytes_to_copy = jc->stack_capacity * sizeof(jc->stack[0]);
const size_t new_capacity = jc->stack_capacity * 2;
const size_t bytes_to_allocate = new_capacity * sizeof(jc->stack[0]);
void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "stack");
if (!mem) {
jc->error = JSON_E_OUT_OF_MEMORY;
return false;
}
jc->stack_capacity = (int)new_capacity;
memcpy(mem, jc->stack, bytes_to_copy);
if (jc->stack != &jc->static_stack[0]) {
jc->free(jc->stack);
}
jc->stack = (signed char*)mem;
}
} else {
if (jc->top == jc->depth) {
jc->error = JSON_E_NESTING_DEPTH_REACHED;
return false;
}
}
jc->stack[++jc->top] = (signed char)mode;
return true;
}
static int
pop(JSON_parser jc, int mode)
{
if (jc->top < 0 || jc->stack[jc->top] != mode) {
return false;
}
jc->top -= 1;
return true;
}
#define parse_buffer_clear(jc) \
do {\
jc->parse_buffer_count = 0;\
jc->parse_buffer[0] = 0;\
} while (0)
#define parse_buffer_pop_back_char(jc)\
do {\
assert(jc->parse_buffer_count >= 1);\
--jc->parse_buffer_count;\
jc->parse_buffer[jc->parse_buffer_count] = 0;\
} while (0)
void delete_JSON_parser(JSON_parser jc)
{
if (jc) {
if (jc->stack != &jc->static_stack[0]) {
jc->free((void*)jc->stack);
}
if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
jc->free((void*)jc->parse_buffer);
}
jc->free((void*)jc);
}
}
int JSON_parser_reset(JSON_parser jc)
{
if (NULL == jc) {
return false;
}
jc->state = GO;
jc->top = -1;
/* Has the parser has been used previously? */
if (NULL == jc->parse_buffer) {
/* Do we want a non-bound stack? */
if (jc->depth > 0) {
jc->stack_capacity = jc->depth;
if (jc->depth <= (int)COUNTOF(jc->static_stack)) {
jc->stack = &jc->static_stack[0];
} else {
const size_t bytes_to_alloc = jc->stack_capacity * sizeof(jc->stack[0]);
jc->stack = (signed char*)JSON_parser_malloc(jc->malloc, bytes_to_alloc, "stack");
if (jc->stack == NULL) {
return false;
}
}
} else {
jc->stack_capacity = (int)COUNTOF(jc->static_stack);
jc->depth = -1;
jc->stack = &jc->static_stack[0];
jc->type = JSON_T_NONE;
jc->escaped = 0;
jc->before_comment_state = 0;
jc->error = 0;
}
/* Set up the parse buffer */
jc->parse_buffer = &jc->static_parse_buffer[0];
jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer);
}
/* Set parser to start */
push(jc, MODE_DONE);
parse_buffer_clear(jc);
return true;
}
JSON_parser
new_JSON_parser(JSON_config const * config)
{
int use_std_malloc = false;
JSON_config default_config;
JSON_parser jc;
JSON_malloc_t alloc;
/* Set to default configuration if none was provided */
if (NULL == config) {
/* initialize configuration */
init_JSON_config(&default_config);
config = &default_config;
}
/* Use std malloc if either the allocator or deallocator function isn't set */
use_std_malloc = NULL == config->malloc || NULL == config->free;
alloc = use_std_malloc ? malloc : config->malloc;
jc = (JSON_parser)JSON_parser_malloc(alloc, sizeof(*jc), "parser");
if (NULL == jc) {
return NULL;
}
/* Configure the parser */
memset(jc, 0, sizeof(*jc));
jc->malloc = alloc;
jc->free = use_std_malloc ? free : config->free;
jc->callback = config->callback;
jc->ctx = config->callback_ctx;
jc->allow_comments = (signed char)(config->allow_comments != 0);
jc->handle_floats_manually = (signed char)(config->handle_floats_manually != 0);
jc->decimal_point = *localeconv()->decimal_point;
/* We need to be able to push at least one object */
jc->depth = config->depth == 0 ? 1 : config->depth;
/* Reset the parser */
if (!JSON_parser_reset(jc)) {
jc->free(jc);
return NULL;
}
return jc;
}
static int parse_buffer_grow(JSON_parser jc)
{
const size_t bytes_to_copy = jc->parse_buffer_count * sizeof(jc->parse_buffer[0]);
const size_t new_capacity = jc->parse_buffer_capacity * 2;
const size_t bytes_to_allocate = new_capacity * sizeof(jc->parse_buffer[0]);
void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "parse buffer");
if (mem == NULL) {
jc->error = JSON_E_OUT_OF_MEMORY;
return false;
}
assert(new_capacity > 0);
memcpy(mem, jc->parse_buffer, bytes_to_copy);
if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
jc->free(jc->parse_buffer);
}
jc->parse_buffer = (char*)mem;
jc->parse_buffer_capacity = new_capacity;
return true;
}
static int parse_buffer_reserve_for(JSON_parser jc, unsigned chars)
{
while (jc->parse_buffer_count + chars + 1 > jc->parse_buffer_capacity) {
if (!parse_buffer_grow(jc)) {
assert(jc->error == JSON_E_OUT_OF_MEMORY);
return false;
}
}
return true;
}
#define parse_buffer_has_space_for(jc, count) \
(jc->parse_buffer_count + (count) + 1 <= jc->parse_buffer_capacity)
#define parse_buffer_push_back_char(jc, c)\
do {\
assert(parse_buffer_has_space_for(jc, 1)); \
jc->parse_buffer[jc->parse_buffer_count++] = c;\
jc->parse_buffer[jc->parse_buffer_count] = 0;\
} while (0)
#define assert_is_non_container_type(jc) \
assert( \
jc->type == JSON_T_NULL || \
jc->type == JSON_T_FALSE || \
jc->type == JSON_T_TRUE || \
jc->type == JSON_T_FLOAT || \
jc->type == JSON_T_INTEGER || \
jc->type == JSON_T_STRING)
static int parse_parse_buffer(JSON_parser jc)
{
if (jc->callback) {
JSON_value value, *arg = NULL;
if (jc->type != JSON_T_NONE) {
assert_is_non_container_type(jc);
switch(jc->type) {
case JSON_T_FLOAT:
arg = &value;
if (jc->handle_floats_manually) {
value.vu.str.value = jc->parse_buffer;
value.vu.str.length = jc->parse_buffer_count;
} else {
value.vu.float_value = strtod(jc->parse_buffer, NULL);
}
break;
case JSON_T_INTEGER:
arg = &value;
sscanf(jc->parse_buffer, JSON_PARSER_INTEGER_SSCANF_TOKEN, &value.vu.integer_value);
break;
case JSON_T_STRING:
arg = &value;
value.vu.str.value = jc->parse_buffer;
value.vu.str.length = jc->parse_buffer_count;
break;
}
if (!(*jc->callback)(jc->ctx, jc->type, arg)) {
return false;
}
}
}
parse_buffer_clear(jc);
return true;
}
#define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800)
#define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00)
#define DECODE_SURROGATE_PAIR(hi,lo) ((((hi) & 0x3FF) << 10) + ((lo) & 0x3FF) + 0x10000)
static const unsigned char utf8_lead_bits[4] = { 0x00, 0xC0, 0xE0, 0xF0 };
static int decode_unicode_char(JSON_parser jc)
{
int i;
unsigned uc = 0;
char* p;
int trail_bytes;
assert(jc->parse_buffer_count >= 6);
p = &jc->parse_buffer[jc->parse_buffer_count - 4];
for (i = 12; i >= 0; i -= 4, ++p) {
unsigned x = *p;
if (x >= 'a') {
x -= ('a' - 10);
} else if (x >= 'A') {
x -= ('A' - 10);
} else {
x &= ~0x30u;
}
assert(x < 16);
uc |= x << i;
}
jc->parse_buffer_count -= 6;
jc->parse_buffer[jc->parse_buffer_count] = 0;
if (jc->utf16_high_surrogate) {
if (IS_LOW_SURROGATE(uc)) {
uc = DECODE_SURROGATE_PAIR(jc->utf16_high_surrogate, uc);
trail_bytes = 3;
jc->utf16_high_surrogate = 0;
} else {
return false;
}
} else {
if (uc < 0x80) {
trail_bytes = 0;
} else if (uc < 0x800) {
trail_bytes = 1;
} else if (IS_HIGH_SURROGATE(uc)) {
jc->utf16_high_surrogate = (UTF16)uc;
return true;
} else if (IS_LOW_SURROGATE(uc)) {
return false;
} else {
trail_bytes = 2;
}
}
jc->parse_buffer[jc->parse_buffer_count++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]);
for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) {
jc->parse_buffer[jc->parse_buffer_count++] = (char) (((uc >> i) & 0x3F) | 0x80);
}
jc->parse_buffer[jc->parse_buffer_count] = 0;
return true;
}
static int add_escaped_char_to_parse_buffer(JSON_parser jc, int next_char)
{
assert(parse_buffer_has_space_for(jc, 1));
jc->escaped = 0;
/* Remove the backslash */
parse_buffer_pop_back_char(jc);
switch(next_char) {
case 'b':
parse_buffer_push_back_char(jc, '\b');
break;
case 'f':
parse_buffer_push_back_char(jc, '\f');
break;
case 'n':
parse_buffer_push_back_char(jc, '\n');
break;
case 'r':
parse_buffer_push_back_char(jc, '\r');
break;
case 't':
parse_buffer_push_back_char(jc, '\t');
break;
case '"':
parse_buffer_push_back_char(jc, '"');
break;
case '\\':
parse_buffer_push_back_char(jc, '\\');
break;
case '/':
parse_buffer_push_back_char(jc, '/');
break;
case 'u':
parse_buffer_push_back_char(jc, '\\');
parse_buffer_push_back_char(jc, 'u');
break;
default:
return false;
}
return true;
}
static int add_char_to_parse_buffer(JSON_parser jc, int next_char, int next_class)
{
if (!parse_buffer_reserve_for(jc, 1)) {
assert(JSON_E_OUT_OF_MEMORY == jc->error);
return false;
}
if (jc->escaped) {
if (!add_escaped_char_to_parse_buffer(jc, next_char)) {
jc->error = JSON_E_INVALID_ESCAPE_SEQUENCE;
return false;
}
} else if (!jc->comment) {
if ((jc->type != JSON_T_NONE) | !((next_class == C_SPACE) | (next_class == C_WHITE)) /* non-white-space */) {
parse_buffer_push_back_char(jc, (char)next_char);
}
}
return true;
}
#define assert_type_isnt_string_null_or_bool(jc) \
assert(jc->type != JSON_T_FALSE); \
assert(jc->type != JSON_T_TRUE); \
assert(jc->type != JSON_T_NULL); \
assert(jc->type != JSON_T_STRING)
int
JSON_parser_char(JSON_parser jc, int next_char)
{
int next_class, next_state;
jc->current_char = next_char;
if (next_char < 0) {
jc->error = JSON_E_INVALID_CHAR;
return false;
}
if (next_char >= 128) {
next_class = C_ETC;
} else {
next_class = ascii_class[next_char];
if (next_class <= __) {
set_error(jc);
return false;
}
}
if (!add_char_to_parse_buffer(jc, next_char, next_class)) {
return false;
}
next_state = state_transition_table[jc->state][next_class];
if (next_state >= 0) {
jc->state = (signed char)next_state;
} else {
switch (next_state) {
case UC:
if(!decode_unicode_char(jc)) {
jc->error = JSON_E_INVALID_UNICODE_SEQUENCE;
return false;
}
/* check if we need to read a second UTF-16 char */
if (jc->utf16_high_surrogate) {
jc->state = D1;
} else {
jc->state = ST;
}
break;
/* escaped char */
case EX:
jc->escaped = 1;
jc->state = ES;
break;
/* integer detected by minus */
case MX:
jc->type = JSON_T_INTEGER;
jc->state = MI;
break;
/* integer detected by zero */
case ZX:
jc->type = JSON_T_INTEGER;
jc->state = ZE;
break;
/* integer detected by 1-9 */
case IX:
jc->type = JSON_T_INTEGER;
jc->state = IT;
break;
/* floating point number detected by exponent*/
case DE:
assert_type_isnt_string_null_or_bool(jc);
jc->type = JSON_T_FLOAT;
jc->state = E1;
break;
/* floating point number detected by fraction */
case DF:
assert_type_isnt_string_null_or_bool(jc);
if (!jc->handle_floats_manually) {
/*
Some versions of strtod (which underlies sscanf) don't support converting
C-locale formated floating point values.
*/
assert(jc->parse_buffer[jc->parse_buffer_count-1] == '.');
jc->parse_buffer[jc->parse_buffer_count-1] = jc->decimal_point;
}
jc->type = JSON_T_FLOAT;
jc->state = FX;
break;
/* string begin " */
case SB:
parse_buffer_clear(jc);
assert(jc->type == JSON_T_NONE);
jc->type = JSON_T_STRING;
jc->state = ST;
break;
/* n */
case NU:
assert(jc->type == JSON_T_NONE);
jc->type = JSON_T_NULL;
jc->state = N1;
break;
/* f */
case FA:
assert(jc->type == JSON_T_NONE);
jc->type = JSON_T_FALSE;
jc->state = F1;
break;
/* t */
case TR:
assert(jc->type == JSON_T_NONE);
jc->type = JSON_T_TRUE;
jc->state = T1;
break;
/* closing comment */
case CE:
jc->comment = 0;
assert(jc->parse_buffer_count == 0);
assert(jc->type == JSON_T_NONE);
jc->state = jc->before_comment_state;
break;
/* opening comment */
case CB:
if (!jc->allow_comments) {
return false;
}
parse_buffer_pop_back_char(jc);
if (!parse_parse_buffer(jc)) {
return false;
}
assert(jc->parse_buffer_count == 0);
assert(jc->type != JSON_T_STRING);
switch (jc->stack[jc->top]) {
case MODE_ARRAY:
case MODE_OBJECT:
switch(jc->state) {
case VA:
case AR:
jc->before_comment_state = jc->state;
break;
default:
jc->before_comment_state = OK;
break;
}
break;
default:
jc->before_comment_state = jc->state;
break;
}
jc->type = JSON_T_NONE;
jc->state = C1;
jc->comment = 1;
break;
/* empty } */
case -9:
parse_buffer_clear(jc);
if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
return false;
}
if (!pop(jc, MODE_KEY)) {
return false;
}
jc->state = OK;
break;
/* } */ case -8:
parse_buffer_pop_back_char(jc);
if (!parse_parse_buffer(jc)) {
return false;
}
if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
return false;
}
if (!pop(jc, MODE_OBJECT)) {
jc->error = JSON_E_UNBALANCED_COLLECTION;
return false;
}
jc->type = JSON_T_NONE;
jc->state = OK;
break;
/* ] */ case -7:
parse_buffer_pop_back_char(jc);
if (!parse_parse_buffer(jc)) {
return false;
}
if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_ARRAY_END, NULL)) {
return false;
}
if (!pop(jc, MODE_ARRAY)) {
jc->error = JSON_E_UNBALANCED_COLLECTION;
return false;
}
jc->type = JSON_T_NONE;
jc->state = OK;
break;
/* { */ case -6:
parse_buffer_pop_back_char(jc);
if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_BEGIN, NULL)) {
return false;
}
if (!push(jc, MODE_KEY)) {
return false;
}
assert(jc->type == JSON_T_NONE);
jc->state = OB;
break;
/* [ */ case -5:
parse_buffer_pop_back_char(jc);
if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_ARRAY_BEGIN, NULL)) {
return false;
}
if (!push(jc, MODE_ARRAY)) {
return false;
}
assert(jc->type == JSON_T_NONE);
jc->state = AR;
break;
/* string end " */ case -4:
parse_buffer_pop_back_char(jc);
switch (jc->stack[jc->top]) {
case MODE_KEY:
assert(jc->type == JSON_T_STRING);
jc->type = JSON_T_NONE;
jc->state = CO;
if (jc->callback) {
JSON_value value;
value.vu.str.value = jc->parse_buffer;
value.vu.str.length = jc->parse_buffer_count;
if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
return false;
}
}
parse_buffer_clear(jc);
break;
case MODE_ARRAY:
case MODE_OBJECT:
assert(jc->type == JSON_T_STRING);
if (!parse_parse_buffer(jc)) {
return false;
}
jc->type = JSON_T_NONE;
jc->state = OK;
break;
default:
return false;
}
break;
/* , */ case -3:
parse_buffer_pop_back_char(jc);
if (!parse_parse_buffer(jc)) {
return false;
}
switch (jc->stack[jc->top]) {
case MODE_OBJECT:
/*
A comma causes a flip from object mode to key mode.
*/
if (!pop(jc, MODE_OBJECT) || !push(jc, MODE_KEY)) {
return false;
}
assert(jc->type != JSON_T_STRING);
jc->type = JSON_T_NONE;
jc->state = KE;
break;
case MODE_ARRAY:
assert(jc->type != JSON_T_STRING);
jc->type = JSON_T_NONE;
jc->state = VA;
break;
default:
return false;
}
break;
/* : */ case -2:
/*
A colon causes a flip from key mode to object mode.
*/
parse_buffer_pop_back_char(jc);
if (!pop(jc, MODE_KEY) || !push(jc, MODE_OBJECT)) {
return false;
}
assert(jc->type == JSON_T_NONE);
jc->state = VA;
break;
/*
Bad action.
*/
default:
set_error(jc);
return false;
}
}
return true;
}
int
JSON_parser_done(JSON_parser jc)
{
if ((jc->state == OK || jc->state == GO) && pop(jc, MODE_DONE))
{
return true;
}
jc->error = JSON_E_UNBALANCED_COLLECTION;
return false;
}
int JSON_parser_is_legal_white_space_string(const char* s)
{
int c, char_class;
if (s == NULL) {
return false;
}
for (; *s; ++s) {
c = *s;
if (c < 0 || c >= 128) {
return false;