-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.h
3403 lines (2820 loc) · 107 KB
/
json.h
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
/*
The latest version of this library is available on GitHub;
https://github.com/sheredom/json.h.
*/
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>.
*/
#ifndef SHEREDOM_JSON_H_INCLUDED
#define SHEREDOM_JSON_H_INCLUDED
#if defined(_MSC_VER)
#pragma warning(push)
/* disable warning: no function prototype given: converting '()' to '(void)' */
#pragma warning(disable : 4255)
/* disable warning: '__cplusplus' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif' */
#pragma warning(disable : 4668)
/* disable warning: 'bytes padding added after construct' */
#pragma warning(disable : 4820)
#endif
#include <stddef.h>
#include <string.h>
#if defined(_MSC_VER)
#define json_weak __inline
#elif defined(__clang__) || defined(__GNUC__)
#define json_weak __attribute__((weak))
#else
#error Non clang, non gcc, non MSVC compiler found!
#endif
#ifdef __cplusplus
extern "C" {
#endif
struct json_value_s;
struct json_parse_result_s;
enum json_parse_flags_e {
json_parse_flags_default = 0,
/* allow trailing commas in objects and arrays. For example, both [true,] and
{"a" : null,} would be allowed with this option on. */
json_parse_flags_allow_trailing_comma = 0x1,
/* allow unquoted keys for objects. For example, {a : null} would be allowed
with this option on. */
json_parse_flags_allow_unquoted_keys = 0x2,
/* allow a global unbracketed object. For example, a : null, b : true, c : {}
would be allowed with this option on. */
json_parse_flags_allow_global_object = 0x4,
/* allow objects to use '=' instead of ':' between key/value pairs. For
example, a = null, b : true would be allowed with this option on. */
json_parse_flags_allow_equals_in_object = 0x8,
/* allow that objects don't have to have comma separators between key/value
pairs. */
json_parse_flags_allow_no_commas = 0x10,
/* allow c-style comments (either variants) to be ignored in the input JSON
file. */
json_parse_flags_allow_c_style_comments = 0x20,
/* deprecated flag, unused. */
json_parse_flags_deprecated = 0x40,
/* record location information for each value. */
json_parse_flags_allow_location_information = 0x80,
/* allow strings to be 'single quoted'. */
json_parse_flags_allow_single_quoted_strings = 0x100,
/* allow numbers to be hexadecimal. */
json_parse_flags_allow_hexadecimal_numbers = 0x200,
/* allow numbers like +123 to be parsed. */
json_parse_flags_allow_leading_plus_sign = 0x400,
/* allow numbers like .0123 or 123. to be parsed. */
json_parse_flags_allow_leading_or_trailing_decimal_point = 0x800,
/* allow Infinity, -Infinity, NaN, -NaN. */
json_parse_flags_allow_inf_and_nan = 0x1000,
/* allow multi line string values. */
json_parse_flags_allow_multi_line_strings = 0x2000,
/* allow simplified JSON to be parsed. Simplified JSON is an enabling of a set
of other parsing options. */
json_parse_flags_allow_simplified_json =
(json_parse_flags_allow_trailing_comma |
json_parse_flags_allow_unquoted_keys |
json_parse_flags_allow_global_object |
json_parse_flags_allow_equals_in_object |
json_parse_flags_allow_no_commas),
/* allow JSON5 to be parsed. JSON5 is an enabling of a set of other parsing
options. */
json_parse_flags_allow_json5 =
(json_parse_flags_allow_trailing_comma |
json_parse_flags_allow_unquoted_keys |
json_parse_flags_allow_c_style_comments |
json_parse_flags_allow_single_quoted_strings |
json_parse_flags_allow_hexadecimal_numbers |
json_parse_flags_allow_leading_plus_sign |
json_parse_flags_allow_leading_or_trailing_decimal_point |
json_parse_flags_allow_inf_and_nan |
json_parse_flags_allow_multi_line_strings)
};
/* Parse a JSON text file, returning a pointer to the root of the JSON
* structure. json_parse performs 1 call to malloc for the entire encoding.
* Returns 0 if an error occurred (malformed JSON input, or malloc failed). */
json_weak struct json_value_s *json_parse(const void *src, size_t src_size);
/* Parse a JSON text file, returning a pointer to the root of the JSON
* structure. json_parse performs 1 call to alloc_func_ptr for the entire
* encoding. Returns 0 if an error occurred (malformed JSON input, or malloc
* failed). If an error occurred, the result struct (if not NULL) will explain
* the type of error, and the location in the input it occurred. If
* alloc_func_ptr is null then malloc is used. */
json_weak struct json_value_s *
json_parse_ex(const void *src, size_t src_size, size_t flags_bitset,
void *(*alloc_func_ptr)(void *, size_t), void *user_data,
struct json_parse_result_s *result);
/* Extracts a value and all the data that makes it up into a newly created
* value. json_extract_value performs 1 call to malloc for the entire encoding.
*/
json_weak struct json_value_s *
json_extract_value(const struct json_value_s *value);
/* Extracts a value and all the data that makes it up into a newly created
* value. json_extract_value performs 1 call to alloc_func_ptr for the entire
* encoding. If alloc_func_ptr is null then malloc is used. */
json_weak struct json_value_s *
json_extract_value_ex(const struct json_value_s *value,
void *(*alloc_func_ptr)(void *, size_t), void *user_data);
/* Write out a minified JSON utf-8 string. This string is an encoding of the
* minimal string characters required to still encode the same data.
* json_write_minified performs 1 call to malloc for the entire encoding. Return
* 0 if an error occurred (malformed JSON input, or malloc failed). The out_size
* parameter is optional as the utf-8 string is null terminated. */
json_weak void *json_write_minified(const struct json_value_s *value,
size_t *out_size);
/* Write out a pretty JSON utf-8 string. This string is encoded such that the
* resultant JSON is pretty in that it is easily human readable. The indent and
* newline parameters allow a user to specify what kind of indentation and
* newline they want (two spaces / three spaces / tabs? \r, \n, \r\n ?). Both
* indent and newline can be NULL, indent defaults to two spaces (" "), and
* newline defaults to linux newlines ('\n' as the newline character).
* json_write_pretty performs 1 call to malloc for the entire encoding. Return 0
* if an error occurred (malformed JSON input, or malloc failed). The out_size
* parameter is optional as the utf-8 string is null terminated. */
json_weak void *json_write_pretty(const struct json_value_s *value,
const char *indent, const char *newline,
size_t *out_size);
/* Reinterpret a JSON value as a string. Returns null is the value was not a
* string. */
json_weak struct json_string_s *
json_value_as_string(struct json_value_s *const value);
/* Reinterpret a JSON value as a number. Returns null is the value was not a
* number. */
json_weak struct json_number_s *
json_value_as_number(struct json_value_s *const value);
/* Reinterpret a JSON value as an object. Returns null is the value was not an
* object. */
json_weak struct json_object_s *
json_value_as_object(struct json_value_s *const value);
/* Reinterpret a JSON value as an array. Returns null is the value was not an
* array. */
json_weak struct json_array_s *
json_value_as_array(struct json_value_s *const value);
/* Whether the value is true. */
json_weak int json_value_is_true(const struct json_value_s *const value);
/* Whether the value is false. */
json_weak int json_value_is_false(const struct json_value_s *const value);
/* Whether the value is null. */
json_weak int json_value_is_null(const struct json_value_s *const value);
/* The various types JSON values can be. Used to identify what a value is. */
enum json_type_e {
json_type_string,
json_type_number,
json_type_object,
json_type_array,
json_type_true,
json_type_false,
json_type_null
};
/* A JSON string value. */
struct json_string_s {
/* utf-8 string */
const char *string;
/* The size (in bytes) of the string */
size_t string_size;
};
/* A JSON string value (extended). */
struct json_string_ex_s {
/* The JSON string this extends. */
struct json_string_s string;
/* The character offset for the value in the JSON input. */
size_t offset;
/* The line number for the value in the JSON input. */
size_t line_no;
/* The row number for the value in the JSON input, in bytes. */
size_t row_no;
};
/* A JSON number value. */
struct json_number_s {
/* ASCII string containing representation of the number. */
const char *number;
/* the size (in bytes) of the number. */
size_t number_size;
};
/* an element of a JSON object. */
struct json_object_element_s {
/* the name of this element. */
struct json_string_s *name;
/* the value of this element. */
struct json_value_s *value;
/* the next object element (can be NULL if the last element in the object). */
struct json_object_element_s *next;
};
/* a JSON object value. */
struct json_object_s {
/* a linked list of the elements in the object. */
struct json_object_element_s *start;
/* the number of elements in the object. */
size_t length;
};
/* an element of a JSON array. */
struct json_array_element_s {
/* the value of this element. */
struct json_value_s *value;
/* the next array element (can be NULL if the last element in the array). */
struct json_array_element_s *next;
};
/* a JSON array value. */
struct json_array_s {
/* a linked list of the elements in the array. */
struct json_array_element_s *start;
/* the number of elements in the array. */
size_t length;
};
/* a JSON value. */
struct json_value_s {
/* a pointer to either a json_string_s, json_number_s, json_object_s, or. */
/* json_array_s. Should be cast to the appropriate struct type based on what.
*/
/* the type of this value is. */
void *payload;
/* must be one of json_type_e. If type is json_type_true, json_type_false, or.
*/
/* json_type_null, payload will be NULL. */
size_t type;
};
/* a JSON value (extended). */
struct json_value_ex_s {
/* the JSON value this extends. */
struct json_value_s value;
/* the character offset for the value in the JSON input. */
size_t offset;
/* the line number for the value in the JSON input. */
size_t line_no;
/* the row number for the value in the JSON input, in bytes. */
size_t row_no;
};
/* a parsing error code. */
enum json_parse_error_e {
/* no error occurred (huzzah!). */
json_parse_error_none = 0,
/* expected either a comma or a closing '}' or ']' to close an object or. */
/* array! */
json_parse_error_expected_comma_or_closing_bracket,
/* colon separating name/value pair was missing! */
json_parse_error_expected_colon,
/* expected string to begin with '"'! */
json_parse_error_expected_opening_quote,
/* invalid escaped sequence in string! */
json_parse_error_invalid_string_escape_sequence,
/* invalid number format! */
json_parse_error_invalid_number_format,
/* invalid value! */
json_parse_error_invalid_value,
/* reached end of buffer before object/array was complete! */
json_parse_error_premature_end_of_buffer,
/* string was malformed! */
json_parse_error_invalid_string,
/* a call to malloc, or a user provider allocator, failed. */
json_parse_error_allocator_failed,
/* the JSON input had unexpected trailing characters that weren't part of the.
*/
/* JSON value. */
json_parse_error_unexpected_trailing_characters,
/* catch-all error for everything else that exploded (real bad chi!). */
json_parse_error_unknown
};
/* error report from json_parse_ex(). */
struct json_parse_result_s {
/* the error code (one of json_parse_error_e). */
size_t error;
/* the character offset for the error in the JSON input. */
size_t error_offset;
/* the line number for the error in the JSON input. */
size_t error_line_no;
/* the row number for the error, in bytes. */
size_t error_row_no;
};
#ifdef __cplusplus
} /* extern "C". */
#endif
#include <stdlib.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(_MSC_VER) && (_MSC_VER < 1920)
#define json_uintmax_t unsigned __int64
#else
#include <inttypes.h>
#define json_uintmax_t uintmax_t
#endif
#if defined(_MSC_VER)
#define json_strtoumax _strtoui64
#else
#define json_strtoumax strtoumax
#endif
#if defined(__cplusplus) && (__cplusplus >= 201103L)
#define json_null nullptr
#else
#define json_null 0
#endif
#if defined(__clang__)
#pragma clang diagnostic push
/* we do one big allocation via malloc, then cast aligned slices of this for. */
/* our structures - we don't have a way to tell the compiler we know what we. */
/* are doing, so disable the warning instead! */
#pragma clang diagnostic ignored "-Wcast-align"
/* We use C style casts everywhere. */
#pragma clang diagnostic ignored "-Wold-style-cast"
/* We need long long for strtoull. */
#pragma clang diagnostic ignored "-Wc++11-long-long"
/* Who cares if nullptr doesn't work with C++98, we don't use it there! */
#pragma clang diagnostic ignored "-Wc++98-compat"
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#elif defined(_MSC_VER)
#pragma warning(push)
/* disable 'function selected for inline expansion' warning. */
#pragma warning(disable : 4711)
/* disable '#pragma warning: there is no warning number' warning. */
#pragma warning(disable : 4619)
/* disable 'warning number not a valid compiler warning' warning. */
#pragma warning(disable : 4616)
/* disable 'Compiler will insert Spectre mitigation for memory load if
* /Qspectre. */
/* switch specified' warning. */
#pragma warning(disable : 5045)
#endif
struct json_parse_state_s {
const char *src;
size_t size;
size_t offset;
size_t flags_bitset;
char *data;
char *dom;
size_t dom_size;
size_t data_size;
size_t line_no; /* line counter for error reporting. */
size_t line_offset; /* (offset-line_offset) is the character number (in
bytes). */
size_t error;
};
json_weak int json_hexadecimal_digit(const char c);
int json_hexadecimal_digit(const char c) {
if ('0' <= c && c <= '9') {
return c - '0';
}
if ('a' <= c && c <= 'f') {
return c - 'a' + 10;
}
if ('A' <= c && c <= 'F') {
return c - 'A' + 10;
}
return -1;
}
json_weak int json_hexadecimal_value(const char *c, const unsigned long size,
unsigned long *result);
int json_hexadecimal_value(const char *c, const unsigned long size,
unsigned long *result) {
const char *p;
int digit;
if (size > sizeof(unsigned long) * 2) {
return 0;
}
*result = 0;
for (p = c; (unsigned long)(p - c) < size; ++p) {
*result <<= 4;
digit = json_hexadecimal_digit(*p);
if (digit < 0 || digit > 15) {
return 0;
}
*result |= (unsigned char)digit;
}
return 1;
}
json_weak int json_skip_whitespace(struct json_parse_state_s *state);
int json_skip_whitespace(struct json_parse_state_s *state) {
size_t offset = state->offset;
const size_t size = state->size;
const char *const src = state->src;
/* the only valid whitespace according to ECMA-404 is ' ', '\n', '\r' and
* '\t'. */
switch (src[offset]) {
default:
return 0;
case ' ':
case '\r':
case '\t':
case '\n':
break;
}
do {
switch (src[offset]) {
default:
/* Update offset. */
state->offset = offset;
return 1;
case ' ':
case '\r':
case '\t':
break;
case '\n':
state->line_no++;
state->line_offset = offset;
break;
}
offset++;
} while (offset < size);
/* Update offset. */
state->offset = offset;
return 1;
}
json_weak int json_skip_c_style_comments(struct json_parse_state_s *state);
int json_skip_c_style_comments(struct json_parse_state_s *state) {
/* do we have a comment?. */
if ('/' == state->src[state->offset]) {
/* skip '/'. */
state->offset++;
if ('/' == state->src[state->offset]) {
/* we had a comment of the form //. */
/* skip second '/'. */
state->offset++;
while (state->offset < state->size) {
switch (state->src[state->offset]) {
default:
/* skip the character in the comment. */
state->offset++;
break;
case '\n':
/* if we have a newline, our comment has ended! Skip the newline. */
state->offset++;
/* we entered a newline, so move our line info forward. */
state->line_no++;
state->line_offset = state->offset;
return 1;
}
}
/* we reached the end of the JSON file! */
return 1;
} else if ('*' == state->src[state->offset]) {
/* we had a comment in the C-style long form. */
/* skip '*'. */
state->offset++;
while (state->offset + 1 < state->size) {
if (('*' == state->src[state->offset]) &&
('/' == state->src[state->offset + 1])) {
/* we reached the end of our comment! */
state->offset += 2;
return 1;
} else if ('\n' == state->src[state->offset]) {
/* we entered a newline, so move our line info forward. */
state->line_no++;
state->line_offset = state->offset;
}
/* skip character within comment. */
state->offset++;
}
/* Comment wasn't ended correctly which is a failure. */
return 1;
}
}
/* we didn't have any comment, which is ok too! */
return 0;
}
json_weak int json_skip_all_skippables(struct json_parse_state_s *state);
int json_skip_all_skippables(struct json_parse_state_s *state) {
/* skip all whitespace and other skippables until there are none left. note
* that the previous version suffered from read past errors should. the
* stream end on json_skip_c_style_comments eg. '{"a" ' with comments flag.
*/
int did_consume = 0;
const size_t size = state->size;
if (json_parse_flags_allow_c_style_comments & state->flags_bitset) {
do {
if (state->offset == size) {
state->error = json_parse_error_premature_end_of_buffer;
return 1;
}
did_consume = json_skip_whitespace(state);
/* This should really be checked on access, not in front of every call.
*/
if (state->offset == size) {
state->error = json_parse_error_premature_end_of_buffer;
return 1;
}
did_consume |= json_skip_c_style_comments(state);
} while (0 != did_consume);
} else {
do {
if (state->offset == size) {
state->error = json_parse_error_premature_end_of_buffer;
return 1;
}
did_consume = json_skip_whitespace(state);
} while (0 != did_consume);
}
if (state->offset == size) {
state->error = json_parse_error_premature_end_of_buffer;
return 1;
}
return 0;
}
json_weak int json_get_value_size(struct json_parse_state_s *state,
int is_global_object);
json_weak int json_get_string_size(struct json_parse_state_s *state,
size_t is_key);
int json_get_string_size(struct json_parse_state_s *state, size_t is_key) {
size_t offset = state->offset;
const size_t size = state->size;
size_t data_size = 0;
const char *const src = state->src;
const int is_single_quote = '\'' == src[offset];
const char quote_to_use = is_single_quote ? '\'' : '"';
const size_t flags_bitset = state->flags_bitset;
unsigned long codepoint;
unsigned long high_surrogate = 0;
if ((json_parse_flags_allow_location_information & flags_bitset) != 0 &&
is_key != 0) {
state->dom_size += sizeof(struct json_string_ex_s);
} else {
state->dom_size += sizeof(struct json_string_s);
}
if ('"' != src[offset]) {
/* if we are allowed single quoted strings check for that too. */
if (!((json_parse_flags_allow_single_quoted_strings & flags_bitset) &&
is_single_quote)) {
state->error = json_parse_error_expected_opening_quote;
state->offset = offset;
return 1;
}
}
/* skip leading '"' or '\''. */
offset++;
while ((offset < size) && (quote_to_use != src[offset])) {
/* add space for the character. */
data_size++;
switch (src[offset]) {
default:
break;
case '\0':
case '\t':
state->error = json_parse_error_invalid_string;
state->offset = offset;
return 1;
}
if ('\\' == src[offset]) {
/* skip reverse solidus character. */
offset++;
if (offset == size) {
state->error = json_parse_error_premature_end_of_buffer;
state->offset = offset;
return 1;
}
switch (src[offset]) {
default:
state->error = json_parse_error_invalid_string_escape_sequence;
state->offset = offset;
return 1;
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
/* all valid characters! */
offset++;
break;
case 'u':
if (!(offset + 5 < size)) {
/* invalid escaped unicode sequence! */
state->error = json_parse_error_invalid_string_escape_sequence;
state->offset = offset;
return 1;
}
codepoint = 0;
if (!json_hexadecimal_value(&src[offset + 1], 4, &codepoint)) {
/* escaped unicode sequences must contain 4 hexadecimal digits! */
state->error = json_parse_error_invalid_string_escape_sequence;
state->offset = offset;
return 1;
}
/* Valid sequence!
* see: https://en.wikipedia.org/wiki/UTF-8#Invalid_code_points.
* 1 7 U + 0000 U + 007F 0xxxxxxx.
* 2 11 U + 0080 U + 07FF 110xxxxx
* 10xxxxxx.
* 3 16 U + 0800 U + FFFF 1110xxxx
* 10xxxxxx 10xxxxxx.
* 4 21 U + 10000 U + 10FFFF 11110xxx
* 10xxxxxx 10xxxxxx 10xxxxxx.
* Note: the high and low surrogate halves used by UTF-16 (U+D800
* through U+DFFF) and code points not encodable by UTF-16 (those after
* U+10FFFF) are not legal Unicode values, and their UTF-8 encoding must
* be treated as an invalid byte sequence. */
if (high_surrogate != 0) {
/* we previously read the high half of the \uxxxx\uxxxx pair, so now
* we expect the low half. */
if (codepoint >= 0xdc00 &&
codepoint <= 0xdfff) { /* low surrogate range. */
data_size += 3;
high_surrogate = 0;
} else {
state->error = json_parse_error_invalid_string_escape_sequence;
state->offset = offset;
return 1;
}
} else if (codepoint <= 0x7f) {
data_size += 0;
} else if (codepoint <= 0x7ff) {
data_size += 1;
} else if (codepoint >= 0xd800 &&
codepoint <= 0xdbff) { /* high surrogate range. */
/* The codepoint is the first half of a "utf-16 surrogate pair". so we
* need the other half for it to be valid: \uHHHH\uLLLL. */
if (offset + 11 > size || '\\' != src[offset + 5] ||
'u' != src[offset + 6]) {
state->error = json_parse_error_invalid_string_escape_sequence;
state->offset = offset;
return 1;
}
high_surrogate = codepoint;
} else if (codepoint >= 0xd800 &&
codepoint <= 0xdfff) { /* low surrogate range. */
/* we did not read the other half before. */
state->error = json_parse_error_invalid_string_escape_sequence;
state->offset = offset;
return 1;
} else {
data_size += 2;
}
/* escaped codepoints after 0xffff are supported in json through utf-16
* surrogate pairs: \uD83D\uDD25 for U+1F525. */
offset += 5;
break;
}
} else if (('\r' == src[offset]) || ('\n' == src[offset])) {
if (!(json_parse_flags_allow_multi_line_strings & flags_bitset)) {
/* invalid escaped unicode sequence! */
state->error = json_parse_error_invalid_string_escape_sequence;
state->offset = offset;
return 1;
}
offset++;
} else {
/* skip character (valid part of sequence). */
offset++;
}
}
/* If the offset is equal to the size, we had a non-terminated string! */
if (offset == size) {
state->error = json_parse_error_premature_end_of_buffer;
state->offset = offset - 1;
return 1;
}
/* skip trailing '"' or '\''. */
offset++;
/* add enough space to store the string. */
state->data_size += data_size;
/* one more byte for null terminator ending the string! */
state->data_size++;
/* update offset. */
state->offset = offset;
return 0;
}
json_weak int is_valid_unquoted_key_char(const char c);
int is_valid_unquoted_key_char(const char c) {
return (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') || ('_' == c));
}
json_weak int json_get_key_size(struct json_parse_state_s *state);
int json_get_key_size(struct json_parse_state_s *state) {
const size_t flags_bitset = state->flags_bitset;
if (json_parse_flags_allow_unquoted_keys & flags_bitset) {
size_t offset = state->offset;
const size_t size = state->size;
const char *const src = state->src;
size_t data_size = state->data_size;
/* if we are allowing unquoted keys, first grok for a quote... */
if ('"' == src[offset]) {
/* ... if we got a comma, just parse the key as a string as normal. */
return json_get_string_size(state, 1);
} else if ((json_parse_flags_allow_single_quoted_strings & flags_bitset) &&
('\'' == src[offset])) {
/* ... if we got a comma, just parse the key as a string as normal. */
return json_get_string_size(state, 1);
} else {
while ((offset < size) && is_valid_unquoted_key_char(src[offset])) {
offset++;
data_size++;
}
/* one more byte for null terminator ending the string! */
data_size++;
if (json_parse_flags_allow_location_information & flags_bitset) {
state->dom_size += sizeof(struct json_string_ex_s);
} else {
state->dom_size += sizeof(struct json_string_s);
}
/* update offset. */
state->offset = offset;
/* update data_size. */
state->data_size = data_size;
return 0;
}
} else {
/* we are only allowed to have quoted keys, so just parse a string! */
return json_get_string_size(state, 1);
}
}
json_weak int json_get_object_size(struct json_parse_state_s *state,
int is_global_object);
int json_get_object_size(struct json_parse_state_s *state,
int is_global_object) {
const size_t flags_bitset = state->flags_bitset;
const char *const src = state->src;
const size_t size = state->size;
size_t elements = 0;
int allow_comma = 0;
int found_closing_brace = 0;
if (is_global_object) {
/* if we found an opening '{' of an object, we actually have a normal JSON
* object at the root of the DOM... */
if (!json_skip_all_skippables(state) && '{' == state->src[state->offset]) {
/* . and we don't actually have a global object after all! */
is_global_object = 0;
}
}
if (!is_global_object) {
if ('{' != src[state->offset]) {
state->error = json_parse_error_unknown;
return 1;
}
/* skip leading '{'. */
state->offset++;
}
state->dom_size += sizeof(struct json_object_s);
if ((state->offset == size) && !is_global_object) {
state->error = json_parse_error_premature_end_of_buffer;
return 1;
}
do {
if (!is_global_object) {
if (json_skip_all_skippables(state)) {
state->error = json_parse_error_premature_end_of_buffer;
return 1;
}
if ('}' == src[state->offset]) {
/* skip trailing '}'. */
state->offset++;
found_closing_brace = 1;
/* finished the object! */
break;
}
} else {
/* we don't require brackets, so that means the object ends when the input
* stream ends! */
if (json_skip_all_skippables(state)) {
break;
}
}
/* if we parsed at least once element previously, grok for a comma. */
if (allow_comma) {
if (',' == src[state->offset]) {
/* skip comma. */
state->offset++;
allow_comma = 0;
} else if (json_parse_flags_allow_no_commas & flags_bitset) {
/* we don't require a comma, and we didn't find one, which is ok! */
allow_comma = 0;
} else {
/* otherwise we are required to have a comma, and we found none. */
state->error = json_parse_error_expected_comma_or_closing_bracket;
return 1;
}
if (json_parse_flags_allow_trailing_comma & flags_bitset) {
continue;
} else {
if (json_skip_all_skippables(state)) {
state->error = json_parse_error_premature_end_of_buffer;
return 1;
}
}
}
if (json_get_key_size(state)) {
/* key parsing failed! */
state->error = json_parse_error_invalid_string;
return 1;
}
if (json_skip_all_skippables(state)) {
state->error = json_parse_error_premature_end_of_buffer;
return 1;
}
if (json_parse_flags_allow_equals_in_object & flags_bitset) {
const char current = src[state->offset];
if ((':' != current) && ('=' != current)) {
state->error = json_parse_error_expected_colon;
return 1;
}
} else {
if (':' != src[state->offset]) {
state->error = json_parse_error_expected_colon;
return 1;
}
}
/* skip colon. */
state->offset++;
if (json_skip_all_skippables(state)) {