-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathjson_lib.c
2161 lines (1821 loc) · 57.3 KB
/
json_lib.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
/* Copyright (c) 2016, 2022, MariaDB Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
#include <my_global.h>
#include <string.h>
#include <m_ctype.h>
#include "json_lib.h"
/*
JSON escaping lets user specify UTF16 codes of characters.
So we're going to need the UTF16 charset capabilities. Let's import
them from the utf16 charset.
*/
int my_utf16_uni(CHARSET_INFO *cs,
my_wc_t *pwc, const uchar *s, const uchar *e);
int my_uni_utf16(CHARSET_INFO *cs, my_wc_t wc, uchar *s, uchar *e);
void json_string_set_str(json_string_t *s,
const uchar *str, const uchar *end)
{
s->c_str= str;
s->str_end= end;
}
void json_string_set_cs(json_string_t *s, CHARSET_INFO *i_cs)
{
s->cs= i_cs;
s->error= 0;
s->wc= i_cs->cset->mb_wc;
}
static void json_string_setup(json_string_t *s,
CHARSET_INFO *i_cs, const uchar *str,
const uchar *end)
{
json_string_set_cs(s, i_cs);
json_string_set_str(s, str, end);
}
enum json_char_classes {
C_EOS, /* end of string */
C_LCURB, /* { */
C_RCURB, /* } */
C_LSQRB, /* [ */
C_RSQRB, /* ] */
C_COLON, /* : */
C_COMMA, /* , */
C_QUOTE, /* " */
C_DIGIT, /* -0123456789 */
C_LOW_F, /* 'f' (for "false") */
C_LOW_N, /* 'n' (for "null") */
C_LOW_T, /* 't' (for "true") */
C_ETC, /* everything else */
C_ERR, /* character disallowed in JSON */
C_BAD, /* invalid character, charset handler cannot read it */
NR_C_CLASSES, /* Counter for classes that handled with functions. */
C_SPACE /* space. Doesn't need specific handlers, so after the counter.*/
};
/*
This array maps first 128 Unicode Code Points into classes.
The remaining Unicode characters should be mapped to C_ETC.
*/
static enum json_char_classes json_chr_map[128] = {
C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR,
C_ERR, C_SPACE, C_SPACE, C_ERR, C_ERR, C_SPACE, C_ERR, C_ERR,
C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR,
C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR, C_ERR,
C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_COMMA, C_DIGIT, C_ETC, C_ETC,
C_DIGIT, 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_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_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_LSQRB, C_ETC, C_RSQRB, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_LOW_F, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_LOW_N, C_ETC,
C_ETC, C_ETC, C_ETC, C_ETC, C_LOW_T, C_ETC, C_ETC, C_ETC,
C_ETC, C_ETC, C_ETC, C_LCURB, C_ETC, C_RCURB, C_ETC, C_ETC
};
/*
JSON parser actually has more states than the 'enum json_states'
declares. But the rest of the states aren't seen to the user so let's
specify them here to avoid confusion.
*/
enum json_all_states {
JST_DONE= NR_JSON_USER_STATES, /* ok to finish */
JST_OBJ_CONT= NR_JSON_USER_STATES+1, /* object continues */
JST_ARRAY_CONT= NR_JSON_USER_STATES+2, /* array continues */
JST_READ_VALUE= NR_JSON_USER_STATES+3, /* value is being read */
NR_JSON_STATES= NR_JSON_USER_STATES+4
};
typedef int (*json_state_handler)(json_engine_t *);
/* The string is broken. */
static int unexpected_eos(json_engine_t *j)
{
j->s.error= JE_EOS;
return 1;
}
/* This symbol here breaks the JSON syntax. */
static int syntax_error(json_engine_t *j)
{
j->s.error= JE_SYN;
return 1;
}
/* Value of object. */
static int mark_object(json_engine_t *j)
{
j->state= JST_OBJ_START;
if (++j->stack_p < JSON_DEPTH_LIMIT)
{
j->stack[j->stack_p]= JST_OBJ_CONT;
return 0;
}
j->s.error= JE_DEPTH;
return 1;
}
/* Read value of object. */
static int read_obj(json_engine_t *j)
{
j->state= JST_OBJ_START;
j->value_type= JSON_VALUE_OBJECT;
j->value= j->value_begin;
if (++j->stack_p < JSON_DEPTH_LIMIT)
{
j->stack[j->stack_p]= JST_OBJ_CONT;
return 0;
}
j->s.error= JE_DEPTH;
return 1;
}
/* Value of array. */
static int mark_array(json_engine_t *j)
{
j->state= JST_ARRAY_START;
if (++j->stack_p < JSON_DEPTH_LIMIT)
{
j->stack[j->stack_p]= JST_ARRAY_CONT;
j->value= j->value_begin;
return 0;
}
j->s.error= JE_DEPTH;
return 1;
}
/* Read value of object. */
static int read_array(json_engine_t *j)
{
j->state= JST_ARRAY_START;
j->value_type= JSON_VALUE_ARRAY;
j->value= j->value_begin;
if (++j->stack_p < JSON_DEPTH_LIMIT)
{
j->stack[j->stack_p]= JST_ARRAY_CONT;
return 0;
}
j->s.error= JE_DEPTH;
return 1;
}
/*
Character classes inside the JSON string constant.
We mostly need this to parse escaping properly.
Escapings available in JSON are:
\" - quotation mark
\\ - backslash
\b - backspace UNICODE 8
\f - formfeed UNICODE 12
\n - newline UNICODE 10
\r - carriage return UNICODE 13
\t - horizontal tab UNICODE 9
\u{four-hex-digits} - code in UCS16 character set
*/
enum json_string_char_classes {
S_0= 0,
S_1= 1,
S_2= 2,
S_3= 3,
S_4= 4,
S_5= 5,
S_6= 6,
S_7= 7,
S_8= 8,
S_9= 9,
S_A= 10,
S_B= 11,
S_C= 12,
S_D= 13,
S_E= 14,
S_F= 15,
S_ETC= 36, /* rest of characters. */
S_QUOTE= 37,
S_BKSL= 38, /* \ */
S_ERR= 100, /* disallowed */
};
/* This maps characters to their types inside a string constant. */
static enum json_string_char_classes json_instr_chr_map[128] = {
S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR,
S_ETC, S_ETC, S_QUOTE, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_0, S_1, S_2, S_3, S_4, S_5, S_6, S_7,
S_8, S_9, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_A, S_B, S_C, S_D, S_E, S_F, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_BKSL, S_ETC, S_ETC, S_ETC,
S_ETC, S_A, S_B, S_C, S_D, S_E, S_F, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC,
S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC, S_ETC
};
static int read_4_hexdigits(json_string_t *s, uchar *dest)
{
int i, t, c_len;
for (i=0; i<4; i++)
{
if ((c_len= json_next_char(s)) <= 0)
return s->error= json_eos(s) ? JE_EOS : JE_BAD_CHR;
if (s->c_next >= 128 || (t= json_instr_chr_map[s->c_next]) > S_F)
return s->error= JE_SYN;
s->c_str+= c_len;
dest[i/2]+= (i % 2) ? t : t*16;
}
return 0;
}
static int json_handle_esc(json_string_t *s)
{
int t, c_len;
if ((c_len= json_next_char(s)) <= 0)
return s->error= json_eos(s) ? JE_EOS : JE_BAD_CHR;
s->c_str+= c_len;
switch (s->c_next)
{
case 'b':
s->c_next= 8;
return 0;
case 'f':
s->c_next= 12;
return 0;
case 'n':
s->c_next= 10;
return 0;
case 'r':
s->c_next= 13;
return 0;
case 't':
s->c_next= 9;
return 0;
}
if (s->c_next < 128 && (t= json_instr_chr_map[s->c_next]) == S_ERR)
{
s->c_str-= c_len;
return s->error= JE_ESCAPING;
}
if (s->c_next != 'u')
return 0;
{
/*
Read the four-hex-digits code.
If symbol is not in the Basic Multilingual Plane, we're reading
the string for the next four digits to compose the UTF-16 surrogate pair.
*/
uchar code[4]= {0,0,0,0};
if (read_4_hexdigits(s, code))
return 1;
if ((c_len= my_utf16_uni(0, &s->c_next, code, code+2)) == 2)
return 0;
if (c_len != MY_CS_TOOSMALL4)
return s->error= JE_BAD_CHR;
if ((c_len= json_next_char(s)) <= 0)
return s->error= json_eos(s) ? JE_EOS : JE_BAD_CHR;
if (s->c_next != '\\')
return s->error= JE_SYN;
s->c_str+= c_len;
if ((c_len= json_next_char(s)) <= 0)
return s->error= json_eos(s) ? JE_EOS : JE_BAD_CHR;
if (s->c_next != 'u')
return s->error= JE_SYN;
s->c_str+= c_len;
if (read_4_hexdigits(s, code+2))
return 1;
if ((c_len= my_utf16_uni(0, &s->c_next, code, code+4)) == 4)
return 0;
}
return s->error= JE_BAD_CHR;
}
int json_read_string_const_chr(json_string_t *js)
{
int c_len;
if ((c_len= json_next_char(js)) > 0)
{
js->c_str+= c_len;
return (js->c_next == '\\') ? json_handle_esc(js) : 0;
}
js->error= json_eos(js) ? JE_EOS : JE_BAD_CHR;
return 1;
}
static int skip_str_constant(json_engine_t *j)
{
int t, c_len;
for (;;)
{
if ((c_len= json_next_char(&j->s)) > 0)
{
j->s.c_str+= c_len;
if (j->s.c_next >= 128 || ((t=json_instr_chr_map[j->s.c_next]) <= S_ETC))
continue;
if (j->s.c_next == '"')
break;
if (j->s.c_next == '\\')
{
j->value_escaped= 1;
if (json_handle_esc(&j->s))
return 1;
continue;
}
/* Symbol not allowed in JSON. */
return j->s.error= JE_NOT_JSON_CHR;
}
else
return j->s.error= json_eos(&j->s) ? JE_EOS : JE_BAD_CHR;
}
j->state= j->stack[j->stack_p];
return 0;
}
/* Scalar string. */
static int v_string(json_engine_t *j)
{
return skip_str_constant(j) || json_scan_next(j);
}
/* Read scalar string. */
static int read_strn(json_engine_t *j)
{
j->value= j->s.c_str;
j->value_type= JSON_VALUE_STRING;
j->value_escaped= 0;
if (skip_str_constant(j))
return 1;
j->state= j->stack[j->stack_p];
j->value_len= (int)(j->s.c_str - j->value) - j->s.c_next_len;
return 0;
}
/*
We have dedicated parser for numeric constants. It's similar
to the main JSON parser, we similarly define character classes,
map characters to classes and implement the state-per-class
table. Though we don't create functions that handle
particular classes, just specify what new state should parser
get in this case.
*/
enum json_num_char_classes {
N_MINUS,
N_PLUS,
N_ZERO,
N_DIGIT,
N_POINT,
N_E,
N_END,
N_EEND,
N_ERR,
N_NUM_CLASSES
};
static enum json_num_char_classes json_num_chr_map[128] = {
N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR,
N_ERR, N_END, N_END, N_ERR, N_ERR, N_END, N_ERR, N_ERR,
N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR,
N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR, N_ERR,
N_END, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_PLUS, N_END, N_MINUS, N_POINT, N_EEND,
N_ZERO, N_DIGIT, N_DIGIT, N_DIGIT, N_DIGIT, N_DIGIT, N_DIGIT, N_DIGIT,
N_DIGIT, N_DIGIT, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_E, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_END, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_E, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_EEND,
N_EEND, N_EEND, N_EEND, N_EEND, N_EEND, N_END, N_EEND, N_EEND,
};
enum json_num_states {
NS_OK, /* Number ended. */
NS_GO, /* Initial state. */
NS_GO1, /* If the number starts with '-'. */
NS_Z, /* If the number starts with '0'. */
NS_Z1, /* If the numbers starts with '-0'. */
NS_INT, /* Integer part. */
NS_FRAC,/* Fractional part. */
NS_EX, /* Exponential part begins. */
NS_EX1, /* Exponential part continues. */
NS_NUM_STATES
};
static int json_num_states[NS_NUM_STATES][N_NUM_CLASSES]=
{
/* - + 0 1..9 POINT E END_OK ERROR */
/*OK*/ { JE_SYN, JE_SYN, JE_SYN, JE_SYN, JE_SYN, JE_SYN, JE_SYN, JE_BAD_CHR },
/*GO*/ { NS_GO1, JE_SYN, NS_Z, NS_INT, JE_SYN, JE_SYN, JE_SYN, JE_BAD_CHR },
/*GO1*/ { JE_SYN, JE_SYN, NS_Z1, NS_INT, JE_SYN, JE_SYN, JE_SYN, JE_BAD_CHR },
/*ZERO*/ { JE_SYN, JE_SYN, JE_SYN, JE_SYN, NS_FRAC, NS_EX, NS_OK, JE_BAD_CHR },
/*ZE1*/ { JE_SYN, JE_SYN, JE_SYN, JE_SYN, NS_FRAC, NS_EX, NS_OK, JE_BAD_CHR },
/*INT*/ { JE_SYN, JE_SYN, NS_INT, NS_INT, NS_FRAC, NS_EX, NS_OK, JE_BAD_CHR },
/*FRAC*/ { JE_SYN, JE_SYN, NS_FRAC, NS_FRAC,JE_SYN, NS_EX, NS_OK, JE_BAD_CHR },
/*EX*/ { NS_EX, NS_EX, NS_EX1, NS_EX1, JE_SYN, JE_SYN, JE_SYN, JE_BAD_CHR },
/*EX1*/ { JE_SYN, JE_SYN, NS_EX1, NS_EX1, JE_SYN, JE_SYN, NS_OK, JE_BAD_CHR }
};
static uint json_num_state_flags[NS_NUM_STATES]=
{
/*OK*/ 0,
/*GO*/ 0,
/*GO1*/ JSON_NUM_NEG,
/*ZERO*/ 0,
/*ZE1*/ 0,
/*INT*/ 0,
/*FRAC*/ JSON_NUM_FRAC_PART,
/*EX*/ JSON_NUM_EXP,
/*EX1*/ 0,
};
static int skip_num_constant(json_engine_t *j)
{
int state= json_num_states[NS_GO][json_num_chr_map[j->s.c_next]];
int c_len;
j->num_flags= 0;
for (;;)
{
j->num_flags|= json_num_state_flags[state];
if ((c_len= json_next_char(&j->s)) > 0 && j->s.c_next < 128)
{
if ((state= json_num_states[state][json_num_chr_map[j->s.c_next]]) > 0)
{
j->s.c_str+= c_len;
continue;
}
break;
}
if ((j->s.error=
json_eos(&j->s) ? json_num_states[state][N_END] : JE_BAD_CHR) < 0)
return 1;
else
break;
}
j->state= j->stack[j->stack_p];
return 0;
}
/* Scalar numeric. */
static int v_number(json_engine_t *j)
{
return skip_num_constant(j) || json_scan_next(j);
}
/* Read numeric constant. */
static int read_num(json_engine_t *j)
{
j->value= j->value_begin;
if (skip_num_constant(j) == 0)
{
j->value_type= JSON_VALUE_NUMBER;
j->value_len= (int)(j->s.c_str - j->value_begin);
return 0;
}
return 1;
}
/* Check that the JSON string matches the argument and skip it. */
static int skip_string_verbatim(json_string_t *s, const char *str)
{
int c_len;
while (*str)
{
if ((c_len= json_next_char(s)) > 0)
{
if (s->c_next == (my_wc_t) *(str++))
{
s->c_str+= c_len;
continue;
}
return s->error= JE_SYN;
}
return s->error= json_eos(s) ? JE_EOS : JE_BAD_CHR;
}
return 0;
}
/* Scalar false. */
static int v_false(json_engine_t *j)
{
if (skip_string_verbatim(&j->s, "alse"))
return 1;
j->state= j->stack[j->stack_p];
return json_scan_next(j);
}
/* Scalar null. */
static int v_null(json_engine_t *j)
{
if (skip_string_verbatim(&j->s, "ull"))
return 1;
j->state= j->stack[j->stack_p];
return json_scan_next(j);
}
/* Scalar true. */
static int v_true(json_engine_t *j)
{
if (skip_string_verbatim(&j->s, "rue"))
return 1;
j->state= j->stack[j->stack_p];
return json_scan_next(j);
}
/* Read false. */
static int read_false(json_engine_t *j)
{
j->value_type= JSON_VALUE_FALSE;
j->value= j->value_begin;
j->state= j->stack[j->stack_p];
j->value_len= 5;
return skip_string_verbatim(&j->s, "alse");
}
/* Read null. */
static int read_null(json_engine_t *j)
{
j->value_type= JSON_VALUE_NULL;
j->value= j->value_begin;
j->state= j->stack[j->stack_p];
j->value_len= 4;
return skip_string_verbatim(&j->s, "ull");
}
/* Read true. */
static int read_true(json_engine_t *j)
{
j->value_type= JSON_VALUE_TRUE;
j->value= j->value_begin;
j->state= j->stack[j->stack_p];
j->value_len= 4;
return skip_string_verbatim(&j->s, "rue");
}
/* Disallowed character. */
static int not_json_chr(json_engine_t *j)
{
j->s.error= JE_NOT_JSON_CHR;
return 1;
}
/* Bad character. */
static int bad_chr(json_engine_t *j)
{
j->s.error= JE_BAD_CHR;
return 1;
}
/* Correct finish. */
static int done(json_engine_t *j __attribute__((unused)))
{
return 1;
}
/* End of the object. */
static int end_object(json_engine_t *j)
{
j->stack_p--;
j->state= JST_OBJ_END;
return 0;
}
/* End of the array. */
static int end_array(json_engine_t *j)
{
j->stack_p--;
j->state= JST_ARRAY_END;
return 0;
}
/* Start reading key name. */
static int read_keyname(json_engine_t *j)
{
j->state= JST_KEY;
return 0;
}
static void get_first_nonspace(json_string_t *js, int *t_next, int *c_len)
{
do
{
if ((*c_len= json_next_char(js)) <= 0)
*t_next= json_eos(js) ? C_EOS : C_BAD;
else
{
*t_next= (js->c_next < 128) ? json_chr_map[js->c_next] : C_ETC;
js->c_str+= *c_len;
}
} while (*t_next == C_SPACE);
}
/* Next key name. */
static int next_key(json_engine_t *j)
{
int t_next, c_len;
get_first_nonspace(&j->s, &t_next, &c_len);
if (t_next == C_QUOTE)
{
j->state= JST_KEY;
return 0;
}
j->s.error= (t_next == C_EOS) ? JE_EOS :
((t_next == C_BAD) ? JE_BAD_CHR :
JE_SYN);
return 1;
}
/* Forward declarations. */
static int skip_colon(json_engine_t *j);
static int skip_key(json_engine_t *j);
static int struct_end_cb(json_engine_t *j);
static int struct_end_qb(json_engine_t *j);
static int struct_end_cm(json_engine_t *j);
static int struct_end_eos(json_engine_t *j);
static int next_item(json_engine_t *j)
{
j->state= JST_VALUE;
return 0;
}
static int array_item(json_engine_t *j)
{
j->state= JST_VALUE;
j->s.c_str-= j->sav_c_len;
return 0;
}
static json_state_handler json_actions[NR_JSON_STATES][NR_C_CLASSES]=
/*
EOS { } [ ]
: , " -0..9 f
n t ETC ERR BAD
*/
{
{/*VALUE*/
unexpected_eos, mark_object, syntax_error, mark_array, syntax_error,
syntax_error, syntax_error,v_string, v_number, v_false,
v_null, v_true, syntax_error, not_json_chr, bad_chr},
{/*KEY*/
unexpected_eos, skip_key, skip_key, skip_key, skip_key,
skip_key, skip_key, skip_colon, skip_key, skip_key,
skip_key, skip_key, skip_key, not_json_chr, bad_chr},
{/*OBJ_START*/
unexpected_eos, syntax_error, end_object, syntax_error, syntax_error,
syntax_error, syntax_error, read_keyname, syntax_error, syntax_error,
syntax_error, syntax_error, syntax_error, not_json_chr, bad_chr},
{/*OBJ_END*/
struct_end_eos, syntax_error, struct_end_cb, syntax_error, struct_end_qb,
syntax_error, struct_end_cm,syntax_error, syntax_error, syntax_error,
syntax_error, syntax_error, syntax_error, not_json_chr, bad_chr},
{/*ARRAY_START*/
unexpected_eos, array_item, syntax_error, array_item, end_array,
syntax_error, syntax_error, array_item, array_item, array_item,
array_item, array_item, syntax_error, not_json_chr, bad_chr},
{/*ARRAY_END*/
struct_end_eos, syntax_error, struct_end_cb, syntax_error, struct_end_qb,
syntax_error, struct_end_cm, syntax_error, syntax_error, syntax_error,
syntax_error, syntax_error, syntax_error, not_json_chr, bad_chr},
{/*DONE*/
done, syntax_error, syntax_error, syntax_error, syntax_error,
syntax_error, syntax_error, syntax_error, syntax_error, syntax_error,
syntax_error, syntax_error, syntax_error, not_json_chr, bad_chr},
{/*OBJ_CONT*/
unexpected_eos, syntax_error, end_object, syntax_error, syntax_error,
syntax_error, next_key, syntax_error, syntax_error, syntax_error,
syntax_error, syntax_error, syntax_error, not_json_chr, bad_chr},
{/*ARRAY_CONT*/
unexpected_eos, syntax_error, syntax_error, syntax_error, end_array,
syntax_error, next_item, syntax_error, syntax_error, syntax_error,
syntax_error, syntax_error, syntax_error, not_json_chr, bad_chr},
{/*READ_VALUE*/
unexpected_eos, read_obj, syntax_error, read_array, syntax_error,
syntax_error, syntax_error, read_strn, read_num, read_false,
read_null, read_true, syntax_error, not_json_chr, bad_chr},
};
int json_scan_start(json_engine_t *je,
CHARSET_INFO *i_cs, const uchar *str, const uchar *end)
{
static const uchar no_time_to_die= 0;
json_string_setup(&je->s, i_cs, str, end);
je->stack[0]= JST_DONE;
je->stack_p= 0;
je->state= JST_VALUE;
je->killed_ptr = (uchar*)&no_time_to_die;
return 0;
}
/* Skip colon and the value. */
static int skip_colon(json_engine_t *j)
{
int t_next, c_len;
get_first_nonspace(&j->s, &t_next, &c_len);
if (t_next == C_COLON)
{
get_first_nonspace(&j->s, &t_next, &c_len);
return json_actions[JST_VALUE][t_next](j);
}
j->s.error= (t_next == C_EOS) ? JE_EOS :
((t_next == C_BAD) ? JE_BAD_CHR:
JE_SYN);
return 1;
}
/* Skip colon and the value. */
static int skip_key(json_engine_t *j)
{
int t_next, c_len;
if (j->s.c_next<128 && json_instr_chr_map[j->s.c_next] == S_BKSL &&
json_handle_esc(&j->s))
return 1;
while (json_read_keyname_chr(j) == 0) {}
if (j->s.error)
return 1;
get_first_nonspace(&j->s, &t_next, &c_len);
return json_actions[JST_VALUE][t_next](j);
}
/*
Handle EOS after the end of an object or array.
To do that we should pop the stack to see if
we are inside an object, or an array, and
run our 'state machine' accordingly.
*/
static int struct_end_eos(json_engine_t *j)
{ return json_actions[j->stack[j->stack_p]][C_EOS](j); }
/*
Handle '}' after the end of an object or array.
To do that we should pop the stack to see if
we are inside an object, or an array, and
run our 'state machine' accordingly.
*/
static int struct_end_cb(json_engine_t *j)
{ return json_actions[j->stack[j->stack_p]][C_RCURB](j); }
/*
Handle ']' after the end of an object or array.
To do that we should pop the stack to see if
we are inside an object, or an array, and
run our 'state machine' accordingly.
*/
static int struct_end_qb(json_engine_t *j)
{ return json_actions[j->stack[j->stack_p]][C_RSQRB](j); }
/*
Handle ',' after the end of an object or array.
To do that we should pop the stack to see if
we are inside an object, or an array, and
run our 'state machine' accordingly.
*/
static int struct_end_cm(json_engine_t *j)
{ return json_actions[j->stack[j->stack_p]][C_COMMA](j); }
int json_read_keyname_chr(json_engine_t *j)
{
int c_len, t;
if ((c_len= json_next_char(&j->s)) > 0)
{
j->s.c_str+= c_len;
if (j->s.c_next>= 128 || (t= json_instr_chr_map[j->s.c_next]) <= S_ETC)
return 0;
switch (t)
{
case S_QUOTE:
for (;;) /* Skip spaces until ':'. */
{
if ((c_len= json_next_char(&j->s)) > 0)
{
if (j->s.c_next == ':')
{
j->s.c_str+= c_len;
j->state= JST_VALUE;
return 1;
}
if (j->s.c_next < 128 && json_chr_map[j->s.c_next] == C_SPACE)
{
j->s.c_str+= c_len;
continue;
}
j->s.error= JE_SYN;
break;
}
j->s.error= json_eos(&j->s) ? JE_EOS : JE_BAD_CHR;
break;
}
return 1;
case S_BKSL:
return json_handle_esc(&j->s);
case S_ERR:
j->s.c_str-= c_len;
j->s.error= JE_STRING_CONST;
return 1;
}
}
j->s.error= json_eos(&j->s) ? JE_EOS : JE_BAD_CHR;
return 1;
}
int json_read_value(json_engine_t *j)
{
int t_next, c_len, res;
j->value_type= JSON_VALUE_UNINITIALIZED;
if (j->state == JST_KEY)
{
while (json_read_keyname_chr(j) == 0) {}
if (j->s.error)
return 1;
}
get_first_nonspace(&j->s, &t_next, &c_len);
j->value_begin= j->s.c_str-c_len;
res= json_actions[JST_READ_VALUE][t_next](j);
j->value_end= j->s.c_str;
return res;
}
int json_scan_next(json_engine_t *j)
{
int t_next;
get_first_nonspace(&j->s, &t_next, &j->sav_c_len);
return *j->killed_ptr || json_actions[j->state][t_next](j);
}
enum json_path_chr_classes {
P_EOS, /* end of string */
P_USD, /* $ */
P_ASTER, /* * */
P_LSQRB, /* [ */
P_T, /* t (for to) */
P_RSQRB, /* ] */
P_POINT, /* . */
P_NEG, /* hyphen (for negative index in path) */
P_ZERO, /* 0 */
P_DIGIT, /* 123456789 */
P_L, /* l (for "lax") */
P_S, /* s (for "strict") */
P_SPACE, /* space */
P_BKSL, /* \ */
P_QUOTE, /* " */
P_ETC, /* everything else */
P_ERR, /* character disallowed in JSON*/
P_BAD, /* invalid character */
N_PATH_CLASSES,