Fossil SCM

merged in JSON_parser license change (went to the wrong branch).

stephan 2013-09-08 07:14 trunk
Commit 10666aadddbd5cce69b76acf705c42ac65078d5d
1 file changed +126 -124
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -26,15 +26,11 @@
2626
#else
2727
# define JSON_PARSER_DLL_API
2828
#endif
2929
3030
/* Determine the integer type use to parse non-floating point numbers */
31
-#ifdef _WIN32
32
-typedef __int64 JSON_int_t;
33
-#define JSON_PARSER_INTEGER_SSCANF_TOKEN "%I64d"
34
-#define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%I64d"
35
-#elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1)
31
+#if __STDC_VERSION__ >= 199901L || HAVE_LONG_LONG == 1
3632
typedef long long JSON_int_t;
3733
#define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld"
3834
#define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld"
3935
#else
4036
typedef long JSON_int_t;
@@ -103,11 +99,11 @@
10399
JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned
104100
as zero-terminated C strings.
105101
106102
\return Non-zero if parsing should continue, else zero.
107103
*/
108
-typedef int (*JSON_parser_callback)(void* ctx, int type, const struct JSON_value_struct* value);
104
+typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value);
109105
110106
111107
/**
112108
A typedef for allocator functions semantically compatible with malloc().
113109
*/
@@ -233,11 +229,11 @@
233229
234230
#endif /* JSON_PARSER_H */
235231
/* end file parser/JSON_parser.h */
236232
/* begin file parser/JSON_parser.c */
237233
/*
238
-Copyright (c) 2005 JSON.org
234
+Copyright (c) 2007-2013 Jean Gressmann ([email protected])
239235
240236
Permission is hereby granted, free of charge, to any person obtaining a copy
241237
of this software and associated documentation files (the "Software"), to deal
242238
in the Software without restriction, including without limitation the rights
243239
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -245,12 +241,10 @@
245241
furnished to do so, subject to the following conditions:
246242
247243
The above copyright notice and this permission notice shall be included in all
248244
copies or substantial portions of the Software.
249245
250
-The Software shall be used for Good, not Evil.
251
-
252246
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
253247
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
254248
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
255249
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
256250
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
@@ -257,49 +251,52 @@
257251
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
258252
SOFTWARE.
259253
*/
260254
261255
/*
262
- Callbacks, comments, Unicode handling by Jean Gressmann ([email protected]), 2007-2010.
263
-
264
-
265256
Changelog:
257
+ 2013-09-08
258
+ Updated license to to be compatible with Debian license requirements.
259
+
260
+ 2012-06-06
261
+ Fix for invalid UTF16 characters and some comment fixex ([email protected]).
262
+
266263
2010-11-25
267264
Support for custom memory allocation ([email protected]).
268
-
265
+
269266
2010-05-07
270
- Added error handling for memory allocation failure ([email protected]).
267
+ Added error handling for memory allocation failure ([email protected]).
271268
Added diagnosis errors for invalid JSON.
272
-
269
+
273270
2010-03-25
274271
Fixed buffer overrun in grow_parse_buffer & cleaned up code.
275
-
272
+
276273
2009-10-19
277
- Replaced long double in JSON_value_struct with double after reports
274
+ Replaced long double in JSON_value_struct with double after reports
278275
of strtold being broken on some platforms ([email protected]).
279
-
280
- 2009-05-17
276
+
277
+ 2009-05-17
281278
Incorporated [email protected] fix for UTF16 decoding.
282
-
283
- 2009-05-14
279
+
280
+ 2009-05-14
284281
Fixed float parsing bug related to a locale being set that didn't
285282
use '.' as decimal point character ([email protected]).
286
-
287
- 2008-10-14
283
+
284
+ 2008-10-14
288285
Renamed states.IN to states.IT to avoid name clash which IN macro
289286
defined in windef.h ([email protected])
290
-
291
- 2008-07-19
287
+
288
+ 2008-07-19
292289
Removed some duplicate code & debugging variable ([email protected])
293
-
294
- 2008-05-28
295
- Made JSON_value structure ansi C compliant. This bug was report by
290
+
291
+ 2008-05-28
292
+ Made JSON_value structure ansi C compliant. This bug was report by
296293
[email protected]
297
-
298
- 2008-05-20
299
- Fixed bug reported by [email protected] where the switching
300
- from static to dynamic parse buffer did not copy the static parse
294
+
295
+ 2008-05-20
296
+ Fixed bug reported by [email protected] where the switching
297
+ from static to dynamic parse buffer did not copy the static parse
301298
buffer's content.
302299
*/
303300
304301
305302
@@ -362,11 +359,11 @@
362359
char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE];
363360
JSON_malloc_t malloc;
364361
JSON_free_t free;
365362
};
366363
367
-#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
364
+#define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
368365
369366
/*
370367
Characters are mapped into these character classes. This allows for
371368
a significant reduction in the size of the state transition table.
372369
*/
@@ -403,11 +400,11 @@
403400
C_LOW_T, /* t */
404401
C_LOW_U, /* u */
405402
C_ABCDF, /* ABCDF */
406403
C_E, /* E */
407404
C_ETC, /* everything else */
408
- C_STAR, /* * */
405
+ C_STAR, /* * */
409406
NR_CLASSES
410407
};
411408
412409
static const signed char ascii_class[128] = {
413410
/*
@@ -419,11 +416,11 @@
419416
__, C_WHITE, C_WHITE, __, __, C_WHITE, __, __,
420417
__, __, __, __, __, __, __, __,
421418
__, __, __, __, __, __, __, __,
422419
423420
C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
424
- C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,
421
+ C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,
425422
C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
426423
C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
427424
428425
C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC,
429426
C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
@@ -536,11 +533,11 @@
536533
/*false F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
537534
/*nu N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__},
538535
/*nul N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__},
539536
/*null N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__},
540537
/*/ C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2},
541
-/*/* 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},
538
+/*/star 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},
542539
/** 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},
543540
/*_. FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
544541
/*\ D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
545542
/*\ D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__},
546543
};
@@ -548,27 +545,27 @@
548545
549546
/*
550547
These modes can be pushed on the stack.
551548
*/
552549
enum modes {
553
- MODE_ARRAY = 1,
554
- MODE_DONE = 2,
555
- MODE_KEY = 3,
550
+ MODE_ARRAY = 1,
551
+ MODE_DONE = 2,
552
+ MODE_KEY = 3,
556553
MODE_OBJECT = 4
557554
};
558555
559556
static void set_error(JSON_parser jc)
560557
{
561558
switch (jc->state) {
562559
case GO:
563560
switch (jc->current_char) {
564
- case '{': case '}': case '[': case ']':
561
+ case '{': case '}': case '[': case ']':
565562
jc->error = JSON_E_UNBALANCED_COLLECTION;
566563
break;
567564
default:
568565
jc->error = JSON_E_INVALID_CHAR;
569
- break;
566
+ break;
570567
}
571568
break;
572569
case OB:
573570
jc->error = JSON_E_EXPECTED_KEY;
574571
break;
@@ -604,11 +601,11 @@
604601
{
605602
/*
606603
Push a mode onto the stack. Return false if there is overflow.
607604
*/
608605
assert(jc->top <= jc->stack_capacity);
609
-
606
+
610607
if (jc->depth < 0) {
611608
if (jc->top == jc->stack_capacity) {
612609
const size_t bytes_to_copy = jc->stack_capacity * sizeof(jc->stack[0]);
613610
const size_t new_capacity = jc->stack_capacity * 2;
614611
const size_t bytes_to_allocate = new_capacity * sizeof(jc->stack[0]);
@@ -653,17 +650,17 @@
653650
#define parse_buffer_clear(jc) \
654651
do {\
655652
jc->parse_buffer_count = 0;\
656653
jc->parse_buffer[0] = 0;\
657654
} while (0)
658
-
655
+
659656
#define parse_buffer_pop_back_char(jc)\
660657
do {\
661658
assert(jc->parse_buffer_count >= 1);\
662659
--jc->parse_buffer_count;\
663660
jc->parse_buffer[jc->parse_buffer_count] = 0;\
664
- } while (0)
661
+ } while (0)
665662
666663
667664
668665
void delete_JSON_parser(JSON_parser jc)
669666
{
@@ -673,25 +670,25 @@
673670
}
674671
if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
675672
jc->free((void*)jc->parse_buffer);
676673
}
677674
jc->free((void*)jc);
678
- }
675
+ }
679676
}
680677
681678
int JSON_parser_reset(JSON_parser jc)
682679
{
683680
if (NULL == jc) {
684681
return false;
685682
}
686
-
683
+
687684
jc->state = GO;
688685
jc->top = -1;
689686
690687
/* parser has been used previously? */
691688
if (NULL == jc->parse_buffer) {
692
-
689
+
693690
/* Do we want non-bound stack? */
694691
if (jc->depth > 0) {
695692
jc->stack_capacity = jc->depth;
696693
if (jc->depth <= (int)COUNTOF(jc->static_stack)) {
697694
jc->stack = &jc->static_stack[0];
@@ -705,20 +702,20 @@
705702
} else {
706703
jc->stack_capacity = (int)COUNTOF(jc->static_stack);
707704
jc->depth = -1;
708705
jc->stack = &jc->static_stack[0];
709706
}
710
-
707
+
711708
/* set up the parse buffer */
712709
jc->parse_buffer = &jc->static_parse_buffer[0];
713710
jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer);
714711
}
715
-
712
+
716713
/* set parser to start */
717714
push(jc, MODE_DONE);
718715
parse_buffer_clear(jc);
719
-
716
+
720717
return true;
721718
}
722719
723720
JSON_parser
724721
new_JSON_parser(JSON_config const * config)
@@ -735,29 +732,29 @@
735732
736733
int use_std_malloc = false;
737734
JSON_config default_config;
738735
JSON_parser jc;
739736
JSON_malloc_t alloc;
740
-
737
+
741738
/* set to default configuration if none was provided */
742739
if (NULL == config) {
743740
/* initialize configuration */
744741
init_JSON_config(&default_config);
745742
config = &default_config;
746743
}
747
-
744
+
748745
/* use std malloc if either the allocator or deallocator function isn't set */
749746
use_std_malloc = NULL == config->malloc || NULL == config->free;
750
-
747
+
751748
alloc = use_std_malloc ? malloc : config->malloc;
752
-
753
- jc = JSON_parser_malloc(alloc, sizeof(*jc), "parser");
754
-
749
+
750
+ jc = (JSON_parser)JSON_parser_malloc(alloc, sizeof(*jc), "parser");
751
+
755752
if (NULL == jc) {
756753
return NULL;
757754
}
758
-
755
+
759756
/* configure the parser */
760757
memset(jc, 0, sizeof(*jc));
761758
jc->malloc = alloc;
762759
jc->free = use_std_malloc ? free : config->free;
763760
jc->callback = config->callback;
@@ -765,42 +762,42 @@
765762
jc->allow_comments = (signed char)(config->allow_comments != 0);
766763
jc->handle_floats_manually = (signed char)(config->handle_floats_manually != 0);
767764
jc->decimal_point = *localeconv()->decimal_point;
768765
/* We need to be able to push at least one object */
769766
jc->depth = config->depth == 0 ? 1 : config->depth;
770
-
767
+
771768
/* reset the parser */
772769
if (!JSON_parser_reset(jc)) {
773770
jc->free(jc);
774771
return NULL;
775772
}
776
-
773
+
777774
return jc;
778775
}
779776
780777
static int parse_buffer_grow(JSON_parser jc)
781778
{
782779
const size_t bytes_to_copy = jc->parse_buffer_count * sizeof(jc->parse_buffer[0]);
783780
const size_t new_capacity = jc->parse_buffer_capacity * 2;
784781
const size_t bytes_to_allocate = new_capacity * sizeof(jc->parse_buffer[0]);
785782
void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "parse buffer");
786
-
783
+
787784
if (mem == NULL) {
788785
jc->error = JSON_E_OUT_OF_MEMORY;
789786
return false;
790787
}
791
-
788
+
792789
assert(new_capacity > 0);
793790
memcpy(mem, jc->parse_buffer, bytes_to_copy);
794
-
791
+
795792
if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
796793
jc->free(jc->parse_buffer);
797794
}
798
-
795
+
799796
jc->parse_buffer = (char*)mem;
800797
jc->parse_buffer_capacity = new_capacity;
801
-
798
+
802799
return true;
803800
}
804801
805802
static int parse_buffer_reserve_for(JSON_parser jc, unsigned chars)
806803
{
@@ -808,11 +805,11 @@
808805
if (!parse_buffer_grow(jc)) {
809806
assert(jc->error == JSON_E_OUT_OF_MEMORY);
810807
return false;
811808
}
812809
}
813
-
810
+
814811
return true;
815812
}
816813
817814
#define parse_buffer_has_space_for(jc, count) \
818815
(jc->parse_buffer_count + (count) + 1 <= jc->parse_buffer_capacity)
@@ -830,27 +827,27 @@
830827
jc->type == JSON_T_FALSE || \
831828
jc->type == JSON_T_TRUE || \
832829
jc->type == JSON_T_FLOAT || \
833830
jc->type == JSON_T_INTEGER || \
834831
jc->type == JSON_T_STRING)
835
-
832
+
836833
837834
static int parse_parse_buffer(JSON_parser jc)
838835
{
839836
if (jc->callback) {
840837
JSON_value value, *arg = NULL;
841
-
838
+
842839
if (jc->type != JSON_T_NONE) {
843840
assert_is_non_container_type(jc);
844
-
841
+
845842
switch(jc->type) {
846843
case JSON_T_FLOAT:
847844
arg = &value;
848845
if (jc->handle_floats_manually) {
849846
value.vu.str.value = jc->parse_buffer;
850847
value.vu.str.length = jc->parse_buffer_count;
851
- } else {
848
+ } else {
852849
/* not checking with end pointer b/c there may be trailing ws */
853850
value.vu.float_value = strtod(jc->parse_buffer, NULL);
854851
}
855852
break;
856853
case JSON_T_INTEGER:
@@ -861,19 +858,19 @@
861858
arg = &value;
862859
value.vu.str.value = jc->parse_buffer;
863860
value.vu.str.length = jc->parse_buffer_count;
864861
break;
865862
}
866
-
863
+
867864
if (!(*jc->callback)(jc->ctx, jc->type, arg)) {
868865
return false;
869866
}
870867
}
871868
}
872
-
869
+
873870
parse_buffer_clear(jc);
874
-
871
+
875872
return true;
876873
}
877874
878875
#define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800)
879876
#define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00)
@@ -884,35 +881,39 @@
884881
{
885882
int i;
886883
unsigned uc = 0;
887884
char* p;
888885
int trail_bytes;
889
-
886
+
890887
assert(jc->parse_buffer_count >= 6);
891
-
888
+
892889
p = &jc->parse_buffer[jc->parse_buffer_count - 4];
893
-
890
+
894891
for (i = 12; i >= 0; i -= 4, ++p) {
895892
unsigned x = *p;
896
-
893
+
897894
if (x >= 'a') {
898895
x -= ('a' - 10);
899896
} else if (x >= 'A') {
900897
x -= ('A' - 10);
901898
} else {
902899
x &= ~0x30u;
903900
}
904
-
901
+
905902
assert(x < 16);
906
-
903
+
907904
uc |= x << i;
908905
}
909
-
906
+
910907
/* clear UTF-16 char from buffer */
911908
jc->parse_buffer_count -= 6;
912909
jc->parse_buffer[jc->parse_buffer_count] = 0;
913
-
910
+
911
+ if (uc == 0xffff || uc == 0xfffe) {
912
+ return false;
913
+ }
914
+
914915
/* attempt decoding ... */
915916
if (jc->utf16_high_surrogate) {
916917
if (IS_LOW_SURROGATE(uc)) {
917918
uc = DECODE_SURROGATE_PAIR(jc->utf16_high_surrogate, uc);
918919
trail_bytes = 3;
@@ -935,26 +936,26 @@
935936
return false;
936937
} else {
937938
trail_bytes = 2;
938939
}
939940
}
940
-
941
+
941942
jc->parse_buffer[jc->parse_buffer_count++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]);
942
-
943
+
943944
for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) {
944945
jc->parse_buffer[jc->parse_buffer_count++] = (char) (((uc >> i) & 0x3F) | 0x80);
945946
}
946947
947948
jc->parse_buffer[jc->parse_buffer_count] = 0;
948
-
949
+
949950
return true;
950951
}
951952
952953
static int add_escaped_char_to_parse_buffer(JSON_parser jc, int next_char)
953954
{
954955
assert(parse_buffer_has_space_for(jc, 1));
955
-
956
+
956957
jc->escaped = 0;
957958
/* remove the backslash */
958959
parse_buffer_pop_back_char(jc);
959960
switch(next_char) {
960961
case 'b':
@@ -996,22 +997,22 @@
996997
{
997998
if (!parse_buffer_reserve_for(jc, 1)) {
998999
assert(JSON_E_OUT_OF_MEMORY == jc->error);
9991000
return false;
10001001
}
1001
-
1002
+
10021003
if (jc->escaped) {
10031004
if (!add_escaped_char_to_parse_buffer(jc, next_char)) {
10041005
jc->error = JSON_E_INVALID_ESCAPE_SEQUENCE;
1005
- return false;
1006
+ return false;
10061007
}
10071008
} else if (!jc->comment) {
10081009
if ((jc->type != JSON_T_NONE) | !((next_class == C_SPACE) | (next_class == C_WHITE)) /* non-white-space */) {
10091010
parse_buffer_push_back_char(jc, (char)next_char);
10101011
}
10111012
}
1012
-
1013
+
10131014
return true;
10141015
}
10151016
10161017
#define assert_type_isnt_string_null_or_bool(jc) \
10171018
assert(jc->type != JSON_T_FALSE); \
@@ -1031,13 +1032,13 @@
10311032
*/
10321033
int next_class, next_state;
10331034
10341035
/*
10351036
Store the current char for error handling
1036
-*/
1037
+*/
10371038
jc->current_char = next_char;
1038
-
1039
+
10391040
/*
10401041
Determine the character's class.
10411042
*/
10421043
if (next_char < 0) {
10431044
jc->error = JSON_E_INVALID_CHAR;
@@ -1050,15 +1051,15 @@
10501051
if (next_class <= __) {
10511052
set_error(jc);
10521053
return false;
10531054
}
10541055
}
1055
-
1056
+
10561057
if (!add_char_to_parse_buffer(jc, next_char, next_class)) {
10571058
return false;
10581059
}
1059
-
1060
+
10601061
/*
10611062
Get the next state from the state transition table.
10621063
*/
10631064
next_state = state_transition_table[jc->state][next_class];
10641065
if (next_state >= 0) {
@@ -1069,11 +1070,11 @@
10691070
} else {
10701071
/*
10711072
Or perform one of the actions.
10721073
*/
10731074
switch (next_state) {
1074
-/* Unicode character */
1075
+/* Unicode character */
10751076
case UC:
10761077
if(!decode_unicode_char(jc)) {
10771078
jc->error = JSON_E_INVALID_UNICODE_SEQUENCE;
10781079
return false;
10791080
}
@@ -1091,78 +1092,78 @@
10911092
break;
10921093
/* integer detected by minus */
10931094
case MX:
10941095
jc->type = JSON_T_INTEGER;
10951096
jc->state = MI;
1096
- break;
1097
-/* integer detected by zero */
1097
+ break;
1098
+/* integer detected by zero */
10981099
case ZX:
10991100
jc->type = JSON_T_INTEGER;
11001101
jc->state = ZE;
1101
- break;
1102
-/* integer detected by 1-9 */
1102
+ break;
1103
+/* integer detected by 1-9 */
11031104
case IX:
11041105
jc->type = JSON_T_INTEGER;
11051106
jc->state = IT;
1106
- break;
1107
-
1107
+ break;
1108
+
11081109
/* floating point number detected by exponent*/
11091110
case DE:
11101111
assert_type_isnt_string_null_or_bool(jc);
11111112
jc->type = JSON_T_FLOAT;
11121113
jc->state = E1;
1113
- break;
1114
-
1114
+ break;
1115
+
11151116
/* floating point number detected by fraction */
11161117
case DF:
11171118
assert_type_isnt_string_null_or_bool(jc);
11181119
if (!jc->handle_floats_manually) {
11191120
/*
1120
- Some versions of strtod (which underlies sscanf) don't support converting
1121
+ Some versions of strtod (which underlies sscanf) don't support converting
11211122
C-locale formated floating point values.
1122
-*/
1123
+*/
11231124
assert(jc->parse_buffer[jc->parse_buffer_count-1] == '.');
11241125
jc->parse_buffer[jc->parse_buffer_count-1] = jc->decimal_point;
1125
- }
1126
+ }
11261127
jc->type = JSON_T_FLOAT;
11271128
jc->state = FX;
1128
- break;
1129
+ break;
11291130
/* string begin " */
11301131
case SB:
11311132
parse_buffer_clear(jc);
11321133
assert(jc->type == JSON_T_NONE);
11331134
jc->type = JSON_T_STRING;
11341135
jc->state = ST;
1135
- break;
1136
-
1136
+ break;
1137
+
11371138
/* n */
11381139
case NU:
11391140
assert(jc->type == JSON_T_NONE);
11401141
jc->type = JSON_T_NULL;
11411142
jc->state = N1;
1142
- break;
1143
+ break;
11431144
/* f */
11441145
case FA:
11451146
assert(jc->type == JSON_T_NONE);
11461147
jc->type = JSON_T_FALSE;
11471148
jc->state = F1;
1148
- break;
1149
+ break;
11491150
/* t */
11501151
case TR:
11511152
assert(jc->type == JSON_T_NONE);
11521153
jc->type = JSON_T_TRUE;
11531154
jc->state = T1;
1154
- break;
1155
-
1155
+ break;
1156
+
11561157
/* closing comment */
11571158
case CE:
11581159
jc->comment = 0;
11591160
assert(jc->parse_buffer_count == 0);
11601161
assert(jc->type == JSON_T_NONE);
11611162
jc->state = jc->before_comment_state;
1162
- break;
1163
-
1163
+ break;
1164
+
11641165
/* opening comment */
11651166
case CB:
11661167
if (!jc->allow_comments) {
11671168
return false;
11681169
}
@@ -1172,11 +1173,11 @@
11721173
}
11731174
assert(jc->parse_buffer_count == 0);
11741175
assert(jc->type != JSON_T_STRING);
11751176
switch (jc->stack[jc->top]) {
11761177
case MODE_ARRAY:
1177
- case MODE_OBJECT:
1178
+ case MODE_OBJECT:
11781179
switch(jc->state) {
11791180
case VA:
11801181
case AR:
11811182
jc->before_comment_state = jc->state;
11821183
break;
@@ -1192,11 +1193,11 @@
11921193
jc->type = JSON_T_NONE;
11931194
jc->state = C1;
11941195
jc->comment = 1;
11951196
break;
11961197
/* empty } */
1197
- case -9:
1198
+ case -9:
11981199
parse_buffer_clear(jc);
11991200
if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
12001201
return false;
12011202
}
12021203
if (!pop(jc, MODE_KEY)) {
@@ -1231,11 +1232,11 @@
12311232
}
12321233
if (!pop(jc, MODE_ARRAY)) {
12331234
jc->error = JSON_E_UNBALANCED_COLLECTION;
12341235
return false;
12351236
}
1236
-
1237
+
12371238
jc->type = JSON_T_NONE;
12381239
jc->state = OK;
12391240
break;
12401241
12411242
/* { */ case -6:
@@ -1267,11 +1268,11 @@
12671268
switch (jc->stack[jc->top]) {
12681269
case MODE_KEY:
12691270
assert(jc->type == JSON_T_STRING);
12701271
jc->type = JSON_T_NONE;
12711272
jc->state = CO;
1272
-
1273
+
12731274
if (jc->callback) {
12741275
JSON_value value;
12751276
value.vu.str.value = jc->parse_buffer;
12761277
value.vu.str.length = jc->parse_buffer_count;
12771278
if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
@@ -1357,29 +1358,29 @@
13571358
13581359
13591360
int JSON_parser_is_legal_white_space_string(const char* s)
13601361
{
13611362
int c, char_class;
1362
-
1363
+
13631364
if (s == NULL) {
13641365
return false;
13651366
}
1366
-
1367
- for (; *s; ++s) {
1367
+
1368
+ for (; *s; ++s) {
13681369
c = *s;
1369
-
1370
+
13701371
if (c < 0 || c >= 128) {
13711372
return false;
13721373
}
1373
-
1374
+
13741375
char_class = ascii_class[c];
1375
-
1376
+
13761377
if (char_class != C_SPACE && char_class != C_WHITE) {
13771378
return false;
13781379
}
13791380
}
1380
-
1381
+
13811382
return true;
13821383
}
13831384
13841385
int JSON_parser_get_last_error(JSON_parser jc)
13851386
{
@@ -1389,16 +1390,17 @@
13891390
13901391
void init_JSON_config(JSON_config* config)
13911392
{
13921393
if (config) {
13931394
memset(config, 0, sizeof(*config));
1394
-
1395
+
13951396
config->depth = JSON_PARSER_STACK_SIZE - 1;
13961397
config->malloc = malloc;
13971398
config->free = free;
13981399
}
13991400
}
1401
+
14001402
/* end file parser/JSON_parser.c */
14011403
/* begin file ./cson.c */
14021404
#include <assert.h>
14031405
#include <stdlib.h> /* malloc()/free() */
14041406
#include <string.h>
14051407
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -26,15 +26,11 @@
26 #else
27 # define JSON_PARSER_DLL_API
28 #endif
29
30 /* Determine the integer type use to parse non-floating point numbers */
31 #ifdef _WIN32
32 typedef __int64 JSON_int_t;
33 #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%I64d"
34 #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%I64d"
35 #elif (__STDC_VERSION__ >= 199901L) || (HAVE_LONG_LONG == 1)
36 typedef long long JSON_int_t;
37 #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld"
38 #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld"
39 #else
40 typedef long JSON_int_t;
@@ -103,11 +99,11 @@
103 JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned
104 as zero-terminated C strings.
105
106 \return Non-zero if parsing should continue, else zero.
107 */
108 typedef int (*JSON_parser_callback)(void* ctx, int type, const struct JSON_value_struct* value);
109
110
111 /**
112 A typedef for allocator functions semantically compatible with malloc().
113 */
@@ -233,11 +229,11 @@
233
234 #endif /* JSON_PARSER_H */
235 /* end file parser/JSON_parser.h */
236 /* begin file parser/JSON_parser.c */
237 /*
238 Copyright (c) 2005 JSON.org
239
240 Permission is hereby granted, free of charge, to any person obtaining a copy
241 of this software and associated documentation files (the "Software"), to deal
242 in the Software without restriction, including without limitation the rights
243 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -245,12 +241,10 @@
245 furnished to do so, subject to the following conditions:
246
247 The above copyright notice and this permission notice shall be included in all
248 copies or substantial portions of the Software.
249
250 The Software shall be used for Good, not Evil.
251
252 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
253 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
254 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
255 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
256 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
@@ -257,49 +251,52 @@
257 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
258 SOFTWARE.
259 */
260
261 /*
262 Callbacks, comments, Unicode handling by Jean Gressmann ([email protected]), 2007-2010.
263
264
265 Changelog:
 
 
 
 
 
 
266 2010-11-25
267 Support for custom memory allocation ([email protected]).
268
269 2010-05-07
270 Added error handling for memory allocation failure ([email protected]).
271 Added diagnosis errors for invalid JSON.
272
273 2010-03-25
274 Fixed buffer overrun in grow_parse_buffer & cleaned up code.
275
276 2009-10-19
277 Replaced long double in JSON_value_struct with double after reports
278 of strtold being broken on some platforms ([email protected]).
279
280 2009-05-17
281 Incorporated [email protected] fix for UTF16 decoding.
282
283 2009-05-14
284 Fixed float parsing bug related to a locale being set that didn't
285 use '.' as decimal point character ([email protected]).
286
287 2008-10-14
288 Renamed states.IN to states.IT to avoid name clash which IN macro
289 defined in windef.h ([email protected])
290
291 2008-07-19
292 Removed some duplicate code & debugging variable ([email protected])
293
294 2008-05-28
295 Made JSON_value structure ansi C compliant. This bug was report by
296 [email protected]
297
298 2008-05-20
299 Fixed bug reported by [email protected] where the switching
300 from static to dynamic parse buffer did not copy the static parse
301 buffer's content.
302 */
303
304
305
@@ -362,11 +359,11 @@
362 char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE];
363 JSON_malloc_t malloc;
364 JSON_free_t free;
365 };
366
367 #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
368
369 /*
370 Characters are mapped into these character classes. This allows for
371 a significant reduction in the size of the state transition table.
372 */
@@ -403,11 +400,11 @@
403 C_LOW_T, /* t */
404 C_LOW_U, /* u */
405 C_ABCDF, /* ABCDF */
406 C_E, /* E */
407 C_ETC, /* everything else */
408 C_STAR, /* * */
409 NR_CLASSES
410 };
411
412 static const signed char ascii_class[128] = {
413 /*
@@ -419,11 +416,11 @@
419 __, C_WHITE, C_WHITE, __, __, C_WHITE, __, __,
420 __, __, __, __, __, __, __, __,
421 __, __, __, __, __, __, __, __,
422
423 C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
424 C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,
425 C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
426 C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
427
428 C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC,
429 C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
@@ -536,11 +533,11 @@
536 /*false F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
537 /*nu N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__},
538 /*nul N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__},
539 /*null N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__},
540 /*/ C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2},
541 /*/* 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},
542 /** 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},
543 /*_. FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
544 /*\ D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
545 /*\ D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__},
546 };
@@ -548,27 +545,27 @@
548
549 /*
550 These modes can be pushed on the stack.
551 */
552 enum modes {
553 MODE_ARRAY = 1,
554 MODE_DONE = 2,
555 MODE_KEY = 3,
556 MODE_OBJECT = 4
557 };
558
559 static void set_error(JSON_parser jc)
560 {
561 switch (jc->state) {
562 case GO:
563 switch (jc->current_char) {
564 case '{': case '}': case '[': case ']':
565 jc->error = JSON_E_UNBALANCED_COLLECTION;
566 break;
567 default:
568 jc->error = JSON_E_INVALID_CHAR;
569 break;
570 }
571 break;
572 case OB:
573 jc->error = JSON_E_EXPECTED_KEY;
574 break;
@@ -604,11 +601,11 @@
604 {
605 /*
606 Push a mode onto the stack. Return false if there is overflow.
607 */
608 assert(jc->top <= jc->stack_capacity);
609
610 if (jc->depth < 0) {
611 if (jc->top == jc->stack_capacity) {
612 const size_t bytes_to_copy = jc->stack_capacity * sizeof(jc->stack[0]);
613 const size_t new_capacity = jc->stack_capacity * 2;
614 const size_t bytes_to_allocate = new_capacity * sizeof(jc->stack[0]);
@@ -653,17 +650,17 @@
653 #define parse_buffer_clear(jc) \
654 do {\
655 jc->parse_buffer_count = 0;\
656 jc->parse_buffer[0] = 0;\
657 } while (0)
658
659 #define parse_buffer_pop_back_char(jc)\
660 do {\
661 assert(jc->parse_buffer_count >= 1);\
662 --jc->parse_buffer_count;\
663 jc->parse_buffer[jc->parse_buffer_count] = 0;\
664 } while (0)
665
666
667
668 void delete_JSON_parser(JSON_parser jc)
669 {
@@ -673,25 +670,25 @@
673 }
674 if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
675 jc->free((void*)jc->parse_buffer);
676 }
677 jc->free((void*)jc);
678 }
679 }
680
681 int JSON_parser_reset(JSON_parser jc)
682 {
683 if (NULL == jc) {
684 return false;
685 }
686
687 jc->state = GO;
688 jc->top = -1;
689
690 /* parser has been used previously? */
691 if (NULL == jc->parse_buffer) {
692
693 /* Do we want non-bound stack? */
694 if (jc->depth > 0) {
695 jc->stack_capacity = jc->depth;
696 if (jc->depth <= (int)COUNTOF(jc->static_stack)) {
697 jc->stack = &jc->static_stack[0];
@@ -705,20 +702,20 @@
705 } else {
706 jc->stack_capacity = (int)COUNTOF(jc->static_stack);
707 jc->depth = -1;
708 jc->stack = &jc->static_stack[0];
709 }
710
711 /* set up the parse buffer */
712 jc->parse_buffer = &jc->static_parse_buffer[0];
713 jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer);
714 }
715
716 /* set parser to start */
717 push(jc, MODE_DONE);
718 parse_buffer_clear(jc);
719
720 return true;
721 }
722
723 JSON_parser
724 new_JSON_parser(JSON_config const * config)
@@ -735,29 +732,29 @@
735
736 int use_std_malloc = false;
737 JSON_config default_config;
738 JSON_parser jc;
739 JSON_malloc_t alloc;
740
741 /* set to default configuration if none was provided */
742 if (NULL == config) {
743 /* initialize configuration */
744 init_JSON_config(&default_config);
745 config = &default_config;
746 }
747
748 /* use std malloc if either the allocator or deallocator function isn't set */
749 use_std_malloc = NULL == config->malloc || NULL == config->free;
750
751 alloc = use_std_malloc ? malloc : config->malloc;
752
753 jc = JSON_parser_malloc(alloc, sizeof(*jc), "parser");
754
755 if (NULL == jc) {
756 return NULL;
757 }
758
759 /* configure the parser */
760 memset(jc, 0, sizeof(*jc));
761 jc->malloc = alloc;
762 jc->free = use_std_malloc ? free : config->free;
763 jc->callback = config->callback;
@@ -765,42 +762,42 @@
765 jc->allow_comments = (signed char)(config->allow_comments != 0);
766 jc->handle_floats_manually = (signed char)(config->handle_floats_manually != 0);
767 jc->decimal_point = *localeconv()->decimal_point;
768 /* We need to be able to push at least one object */
769 jc->depth = config->depth == 0 ? 1 : config->depth;
770
771 /* reset the parser */
772 if (!JSON_parser_reset(jc)) {
773 jc->free(jc);
774 return NULL;
775 }
776
777 return jc;
778 }
779
780 static int parse_buffer_grow(JSON_parser jc)
781 {
782 const size_t bytes_to_copy = jc->parse_buffer_count * sizeof(jc->parse_buffer[0]);
783 const size_t new_capacity = jc->parse_buffer_capacity * 2;
784 const size_t bytes_to_allocate = new_capacity * sizeof(jc->parse_buffer[0]);
785 void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "parse buffer");
786
787 if (mem == NULL) {
788 jc->error = JSON_E_OUT_OF_MEMORY;
789 return false;
790 }
791
792 assert(new_capacity > 0);
793 memcpy(mem, jc->parse_buffer, bytes_to_copy);
794
795 if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
796 jc->free(jc->parse_buffer);
797 }
798
799 jc->parse_buffer = (char*)mem;
800 jc->parse_buffer_capacity = new_capacity;
801
802 return true;
803 }
804
805 static int parse_buffer_reserve_for(JSON_parser jc, unsigned chars)
806 {
@@ -808,11 +805,11 @@
808 if (!parse_buffer_grow(jc)) {
809 assert(jc->error == JSON_E_OUT_OF_MEMORY);
810 return false;
811 }
812 }
813
814 return true;
815 }
816
817 #define parse_buffer_has_space_for(jc, count) \
818 (jc->parse_buffer_count + (count) + 1 <= jc->parse_buffer_capacity)
@@ -830,27 +827,27 @@
830 jc->type == JSON_T_FALSE || \
831 jc->type == JSON_T_TRUE || \
832 jc->type == JSON_T_FLOAT || \
833 jc->type == JSON_T_INTEGER || \
834 jc->type == JSON_T_STRING)
835
836
837 static int parse_parse_buffer(JSON_parser jc)
838 {
839 if (jc->callback) {
840 JSON_value value, *arg = NULL;
841
842 if (jc->type != JSON_T_NONE) {
843 assert_is_non_container_type(jc);
844
845 switch(jc->type) {
846 case JSON_T_FLOAT:
847 arg = &value;
848 if (jc->handle_floats_manually) {
849 value.vu.str.value = jc->parse_buffer;
850 value.vu.str.length = jc->parse_buffer_count;
851 } else {
852 /* not checking with end pointer b/c there may be trailing ws */
853 value.vu.float_value = strtod(jc->parse_buffer, NULL);
854 }
855 break;
856 case JSON_T_INTEGER:
@@ -861,19 +858,19 @@
861 arg = &value;
862 value.vu.str.value = jc->parse_buffer;
863 value.vu.str.length = jc->parse_buffer_count;
864 break;
865 }
866
867 if (!(*jc->callback)(jc->ctx, jc->type, arg)) {
868 return false;
869 }
870 }
871 }
872
873 parse_buffer_clear(jc);
874
875 return true;
876 }
877
878 #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800)
879 #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00)
@@ -884,35 +881,39 @@
884 {
885 int i;
886 unsigned uc = 0;
887 char* p;
888 int trail_bytes;
889
890 assert(jc->parse_buffer_count >= 6);
891
892 p = &jc->parse_buffer[jc->parse_buffer_count - 4];
893
894 for (i = 12; i >= 0; i -= 4, ++p) {
895 unsigned x = *p;
896
897 if (x >= 'a') {
898 x -= ('a' - 10);
899 } else if (x >= 'A') {
900 x -= ('A' - 10);
901 } else {
902 x &= ~0x30u;
903 }
904
905 assert(x < 16);
906
907 uc |= x << i;
908 }
909
910 /* clear UTF-16 char from buffer */
911 jc->parse_buffer_count -= 6;
912 jc->parse_buffer[jc->parse_buffer_count] = 0;
913
 
 
 
 
914 /* attempt decoding ... */
915 if (jc->utf16_high_surrogate) {
916 if (IS_LOW_SURROGATE(uc)) {
917 uc = DECODE_SURROGATE_PAIR(jc->utf16_high_surrogate, uc);
918 trail_bytes = 3;
@@ -935,26 +936,26 @@
935 return false;
936 } else {
937 trail_bytes = 2;
938 }
939 }
940
941 jc->parse_buffer[jc->parse_buffer_count++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]);
942
943 for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) {
944 jc->parse_buffer[jc->parse_buffer_count++] = (char) (((uc >> i) & 0x3F) | 0x80);
945 }
946
947 jc->parse_buffer[jc->parse_buffer_count] = 0;
948
949 return true;
950 }
951
952 static int add_escaped_char_to_parse_buffer(JSON_parser jc, int next_char)
953 {
954 assert(parse_buffer_has_space_for(jc, 1));
955
956 jc->escaped = 0;
957 /* remove the backslash */
958 parse_buffer_pop_back_char(jc);
959 switch(next_char) {
960 case 'b':
@@ -996,22 +997,22 @@
996 {
997 if (!parse_buffer_reserve_for(jc, 1)) {
998 assert(JSON_E_OUT_OF_MEMORY == jc->error);
999 return false;
1000 }
1001
1002 if (jc->escaped) {
1003 if (!add_escaped_char_to_parse_buffer(jc, next_char)) {
1004 jc->error = JSON_E_INVALID_ESCAPE_SEQUENCE;
1005 return false;
1006 }
1007 } else if (!jc->comment) {
1008 if ((jc->type != JSON_T_NONE) | !((next_class == C_SPACE) | (next_class == C_WHITE)) /* non-white-space */) {
1009 parse_buffer_push_back_char(jc, (char)next_char);
1010 }
1011 }
1012
1013 return true;
1014 }
1015
1016 #define assert_type_isnt_string_null_or_bool(jc) \
1017 assert(jc->type != JSON_T_FALSE); \
@@ -1031,13 +1032,13 @@
1031 */
1032 int next_class, next_state;
1033
1034 /*
1035 Store the current char for error handling
1036 */
1037 jc->current_char = next_char;
1038
1039 /*
1040 Determine the character's class.
1041 */
1042 if (next_char < 0) {
1043 jc->error = JSON_E_INVALID_CHAR;
@@ -1050,15 +1051,15 @@
1050 if (next_class <= __) {
1051 set_error(jc);
1052 return false;
1053 }
1054 }
1055
1056 if (!add_char_to_parse_buffer(jc, next_char, next_class)) {
1057 return false;
1058 }
1059
1060 /*
1061 Get the next state from the state transition table.
1062 */
1063 next_state = state_transition_table[jc->state][next_class];
1064 if (next_state >= 0) {
@@ -1069,11 +1070,11 @@
1069 } else {
1070 /*
1071 Or perform one of the actions.
1072 */
1073 switch (next_state) {
1074 /* Unicode character */
1075 case UC:
1076 if(!decode_unicode_char(jc)) {
1077 jc->error = JSON_E_INVALID_UNICODE_SEQUENCE;
1078 return false;
1079 }
@@ -1091,78 +1092,78 @@
1091 break;
1092 /* integer detected by minus */
1093 case MX:
1094 jc->type = JSON_T_INTEGER;
1095 jc->state = MI;
1096 break;
1097 /* integer detected by zero */
1098 case ZX:
1099 jc->type = JSON_T_INTEGER;
1100 jc->state = ZE;
1101 break;
1102 /* integer detected by 1-9 */
1103 case IX:
1104 jc->type = JSON_T_INTEGER;
1105 jc->state = IT;
1106 break;
1107
1108 /* floating point number detected by exponent*/
1109 case DE:
1110 assert_type_isnt_string_null_or_bool(jc);
1111 jc->type = JSON_T_FLOAT;
1112 jc->state = E1;
1113 break;
1114
1115 /* floating point number detected by fraction */
1116 case DF:
1117 assert_type_isnt_string_null_or_bool(jc);
1118 if (!jc->handle_floats_manually) {
1119 /*
1120 Some versions of strtod (which underlies sscanf) don't support converting
1121 C-locale formated floating point values.
1122 */
1123 assert(jc->parse_buffer[jc->parse_buffer_count-1] == '.');
1124 jc->parse_buffer[jc->parse_buffer_count-1] = jc->decimal_point;
1125 }
1126 jc->type = JSON_T_FLOAT;
1127 jc->state = FX;
1128 break;
1129 /* string begin " */
1130 case SB:
1131 parse_buffer_clear(jc);
1132 assert(jc->type == JSON_T_NONE);
1133 jc->type = JSON_T_STRING;
1134 jc->state = ST;
1135 break;
1136
1137 /* n */
1138 case NU:
1139 assert(jc->type == JSON_T_NONE);
1140 jc->type = JSON_T_NULL;
1141 jc->state = N1;
1142 break;
1143 /* f */
1144 case FA:
1145 assert(jc->type == JSON_T_NONE);
1146 jc->type = JSON_T_FALSE;
1147 jc->state = F1;
1148 break;
1149 /* t */
1150 case TR:
1151 assert(jc->type == JSON_T_NONE);
1152 jc->type = JSON_T_TRUE;
1153 jc->state = T1;
1154 break;
1155
1156 /* closing comment */
1157 case CE:
1158 jc->comment = 0;
1159 assert(jc->parse_buffer_count == 0);
1160 assert(jc->type == JSON_T_NONE);
1161 jc->state = jc->before_comment_state;
1162 break;
1163
1164 /* opening comment */
1165 case CB:
1166 if (!jc->allow_comments) {
1167 return false;
1168 }
@@ -1172,11 +1173,11 @@
1172 }
1173 assert(jc->parse_buffer_count == 0);
1174 assert(jc->type != JSON_T_STRING);
1175 switch (jc->stack[jc->top]) {
1176 case MODE_ARRAY:
1177 case MODE_OBJECT:
1178 switch(jc->state) {
1179 case VA:
1180 case AR:
1181 jc->before_comment_state = jc->state;
1182 break;
@@ -1192,11 +1193,11 @@
1192 jc->type = JSON_T_NONE;
1193 jc->state = C1;
1194 jc->comment = 1;
1195 break;
1196 /* empty } */
1197 case -9:
1198 parse_buffer_clear(jc);
1199 if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
1200 return false;
1201 }
1202 if (!pop(jc, MODE_KEY)) {
@@ -1231,11 +1232,11 @@
1231 }
1232 if (!pop(jc, MODE_ARRAY)) {
1233 jc->error = JSON_E_UNBALANCED_COLLECTION;
1234 return false;
1235 }
1236
1237 jc->type = JSON_T_NONE;
1238 jc->state = OK;
1239 break;
1240
1241 /* { */ case -6:
@@ -1267,11 +1268,11 @@
1267 switch (jc->stack[jc->top]) {
1268 case MODE_KEY:
1269 assert(jc->type == JSON_T_STRING);
1270 jc->type = JSON_T_NONE;
1271 jc->state = CO;
1272
1273 if (jc->callback) {
1274 JSON_value value;
1275 value.vu.str.value = jc->parse_buffer;
1276 value.vu.str.length = jc->parse_buffer_count;
1277 if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
@@ -1357,29 +1358,29 @@
1357
1358
1359 int JSON_parser_is_legal_white_space_string(const char* s)
1360 {
1361 int c, char_class;
1362
1363 if (s == NULL) {
1364 return false;
1365 }
1366
1367 for (; *s; ++s) {
1368 c = *s;
1369
1370 if (c < 0 || c >= 128) {
1371 return false;
1372 }
1373
1374 char_class = ascii_class[c];
1375
1376 if (char_class != C_SPACE && char_class != C_WHITE) {
1377 return false;
1378 }
1379 }
1380
1381 return true;
1382 }
1383
1384 int JSON_parser_get_last_error(JSON_parser jc)
1385 {
@@ -1389,16 +1390,17 @@
1389
1390 void init_JSON_config(JSON_config* config)
1391 {
1392 if (config) {
1393 memset(config, 0, sizeof(*config));
1394
1395 config->depth = JSON_PARSER_STACK_SIZE - 1;
1396 config->malloc = malloc;
1397 config->free = free;
1398 }
1399 }
 
1400 /* end file parser/JSON_parser.c */
1401 /* begin file ./cson.c */
1402 #include <assert.h>
1403 #include <stdlib.h> /* malloc()/free() */
1404 #include <string.h>
1405
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -26,15 +26,11 @@
26 #else
27 # define JSON_PARSER_DLL_API
28 #endif
29
30 /* Determine the integer type use to parse non-floating point numbers */
31 #if __STDC_VERSION__ >= 199901L || HAVE_LONG_LONG == 1
 
 
 
 
32 typedef long long JSON_int_t;
33 #define JSON_PARSER_INTEGER_SSCANF_TOKEN "%lld"
34 #define JSON_PARSER_INTEGER_SPRINTF_TOKEN "%lld"
35 #else
36 typedef long JSON_int_t;
@@ -103,11 +99,11 @@
99 JSON_T_NULL, JSON_T_TRUE, and JSON_T_FALSE. String values are always returned
100 as zero-terminated C strings.
101
102 \return Non-zero if parsing should continue, else zero.
103 */
104 typedef int (*JSON_parser_callback)(void* ctx, int type, const JSON_value* value);
105
106
107 /**
108 A typedef for allocator functions semantically compatible with malloc().
109 */
@@ -233,11 +229,11 @@
229
230 #endif /* JSON_PARSER_H */
231 /* end file parser/JSON_parser.h */
232 /* begin file parser/JSON_parser.c */
233 /*
234 Copyright (c) 2007-2013 Jean Gressmann ([email protected])
235
236 Permission is hereby granted, free of charge, to any person obtaining a copy
237 of this software and associated documentation files (the "Software"), to deal
238 in the Software without restriction, including without limitation the rights
239 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -245,12 +241,10 @@
241 furnished to do so, subject to the following conditions:
242
243 The above copyright notice and this permission notice shall be included in all
244 copies or substantial portions of the Software.
245
 
 
246 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
247 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
249 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
250 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
@@ -257,49 +251,52 @@
251 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
252 SOFTWARE.
253 */
254
255 /*
 
 
 
256 Changelog:
257 2013-09-08
258 Updated license to to be compatible with Debian license requirements.
259
260 2012-06-06
261 Fix for invalid UTF16 characters and some comment fixex ([email protected]).
262
263 2010-11-25
264 Support for custom memory allocation ([email protected]).
265
266 2010-05-07
267 Added error handling for memory allocation failure ([email protected]).
268 Added diagnosis errors for invalid JSON.
269
270 2010-03-25
271 Fixed buffer overrun in grow_parse_buffer & cleaned up code.
272
273 2009-10-19
274 Replaced long double in JSON_value_struct with double after reports
275 of strtold being broken on some platforms ([email protected]).
276
277 2009-05-17
278 Incorporated [email protected] fix for UTF16 decoding.
279
280 2009-05-14
281 Fixed float parsing bug related to a locale being set that didn't
282 use '.' as decimal point character ([email protected]).
283
284 2008-10-14
285 Renamed states.IN to states.IT to avoid name clash which IN macro
286 defined in windef.h ([email protected])
287
288 2008-07-19
289 Removed some duplicate code & debugging variable ([email protected])
290
291 2008-05-28
292 Made JSON_value structure ansi C compliant. This bug was report by
293 [email protected]
294
295 2008-05-20
296 Fixed bug reported by [email protected] where the switching
297 from static to dynamic parse buffer did not copy the static parse
298 buffer's content.
299 */
300
301
302
@@ -362,11 +359,11 @@
359 char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE];
360 JSON_malloc_t malloc;
361 JSON_free_t free;
362 };
363
364 #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
365
366 /*
367 Characters are mapped into these character classes. This allows for
368 a significant reduction in the size of the state transition table.
369 */
@@ -403,11 +400,11 @@
400 C_LOW_T, /* t */
401 C_LOW_U, /* u */
402 C_ABCDF, /* ABCDF */
403 C_E, /* E */
404 C_ETC, /* everything else */
405 C_STAR, /* * */
406 NR_CLASSES
407 };
408
409 static const signed char ascii_class[128] = {
410 /*
@@ -419,11 +416,11 @@
416 __, C_WHITE, C_WHITE, __, __, C_WHITE, __, __,
417 __, __, __, __, __, __, __, __,
418 __, __, __, __, __, __, __, __,
419
420 C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
421 C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,
422 C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
423 C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
424
425 C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC,
426 C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
@@ -536,11 +533,11 @@
533 /*false F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
534 /*nu N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__},
535 /*nul N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__},
536 /*null N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__},
537 /*/ C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2},
538 /*/star 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},
539 /** 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},
540 /*_. FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
541 /*\ D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
542 /*\ D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__},
543 };
@@ -548,27 +545,27 @@
545
546 /*
547 These modes can be pushed on the stack.
548 */
549 enum modes {
550 MODE_ARRAY = 1,
551 MODE_DONE = 2,
552 MODE_KEY = 3,
553 MODE_OBJECT = 4
554 };
555
556 static void set_error(JSON_parser jc)
557 {
558 switch (jc->state) {
559 case GO:
560 switch (jc->current_char) {
561 case '{': case '}': case '[': case ']':
562 jc->error = JSON_E_UNBALANCED_COLLECTION;
563 break;
564 default:
565 jc->error = JSON_E_INVALID_CHAR;
566 break;
567 }
568 break;
569 case OB:
570 jc->error = JSON_E_EXPECTED_KEY;
571 break;
@@ -604,11 +601,11 @@
601 {
602 /*
603 Push a mode onto the stack. Return false if there is overflow.
604 */
605 assert(jc->top <= jc->stack_capacity);
606
607 if (jc->depth < 0) {
608 if (jc->top == jc->stack_capacity) {
609 const size_t bytes_to_copy = jc->stack_capacity * sizeof(jc->stack[0]);
610 const size_t new_capacity = jc->stack_capacity * 2;
611 const size_t bytes_to_allocate = new_capacity * sizeof(jc->stack[0]);
@@ -653,17 +650,17 @@
650 #define parse_buffer_clear(jc) \
651 do {\
652 jc->parse_buffer_count = 0;\
653 jc->parse_buffer[0] = 0;\
654 } while (0)
655
656 #define parse_buffer_pop_back_char(jc)\
657 do {\
658 assert(jc->parse_buffer_count >= 1);\
659 --jc->parse_buffer_count;\
660 jc->parse_buffer[jc->parse_buffer_count] = 0;\
661 } while (0)
662
663
664
665 void delete_JSON_parser(JSON_parser jc)
666 {
@@ -673,25 +670,25 @@
670 }
671 if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
672 jc->free((void*)jc->parse_buffer);
673 }
674 jc->free((void*)jc);
675 }
676 }
677
678 int JSON_parser_reset(JSON_parser jc)
679 {
680 if (NULL == jc) {
681 return false;
682 }
683
684 jc->state = GO;
685 jc->top = -1;
686
687 /* parser has been used previously? */
688 if (NULL == jc->parse_buffer) {
689
690 /* Do we want non-bound stack? */
691 if (jc->depth > 0) {
692 jc->stack_capacity = jc->depth;
693 if (jc->depth <= (int)COUNTOF(jc->static_stack)) {
694 jc->stack = &jc->static_stack[0];
@@ -705,20 +702,20 @@
702 } else {
703 jc->stack_capacity = (int)COUNTOF(jc->static_stack);
704 jc->depth = -1;
705 jc->stack = &jc->static_stack[0];
706 }
707
708 /* set up the parse buffer */
709 jc->parse_buffer = &jc->static_parse_buffer[0];
710 jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer);
711 }
712
713 /* set parser to start */
714 push(jc, MODE_DONE);
715 parse_buffer_clear(jc);
716
717 return true;
718 }
719
720 JSON_parser
721 new_JSON_parser(JSON_config const * config)
@@ -735,29 +732,29 @@
732
733 int use_std_malloc = false;
734 JSON_config default_config;
735 JSON_parser jc;
736 JSON_malloc_t alloc;
737
738 /* set to default configuration if none was provided */
739 if (NULL == config) {
740 /* initialize configuration */
741 init_JSON_config(&default_config);
742 config = &default_config;
743 }
744
745 /* use std malloc if either the allocator or deallocator function isn't set */
746 use_std_malloc = NULL == config->malloc || NULL == config->free;
747
748 alloc = use_std_malloc ? malloc : config->malloc;
749
750 jc = (JSON_parser)JSON_parser_malloc(alloc, sizeof(*jc), "parser");
751
752 if (NULL == jc) {
753 return NULL;
754 }
755
756 /* configure the parser */
757 memset(jc, 0, sizeof(*jc));
758 jc->malloc = alloc;
759 jc->free = use_std_malloc ? free : config->free;
760 jc->callback = config->callback;
@@ -765,42 +762,42 @@
762 jc->allow_comments = (signed char)(config->allow_comments != 0);
763 jc->handle_floats_manually = (signed char)(config->handle_floats_manually != 0);
764 jc->decimal_point = *localeconv()->decimal_point;
765 /* We need to be able to push at least one object */
766 jc->depth = config->depth == 0 ? 1 : config->depth;
767
768 /* reset the parser */
769 if (!JSON_parser_reset(jc)) {
770 jc->free(jc);
771 return NULL;
772 }
773
774 return jc;
775 }
776
777 static int parse_buffer_grow(JSON_parser jc)
778 {
779 const size_t bytes_to_copy = jc->parse_buffer_count * sizeof(jc->parse_buffer[0]);
780 const size_t new_capacity = jc->parse_buffer_capacity * 2;
781 const size_t bytes_to_allocate = new_capacity * sizeof(jc->parse_buffer[0]);
782 void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "parse buffer");
783
784 if (mem == NULL) {
785 jc->error = JSON_E_OUT_OF_MEMORY;
786 return false;
787 }
788
789 assert(new_capacity > 0);
790 memcpy(mem, jc->parse_buffer, bytes_to_copy);
791
792 if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
793 jc->free(jc->parse_buffer);
794 }
795
796 jc->parse_buffer = (char*)mem;
797 jc->parse_buffer_capacity = new_capacity;
798
799 return true;
800 }
801
802 static int parse_buffer_reserve_for(JSON_parser jc, unsigned chars)
803 {
@@ -808,11 +805,11 @@
805 if (!parse_buffer_grow(jc)) {
806 assert(jc->error == JSON_E_OUT_OF_MEMORY);
807 return false;
808 }
809 }
810
811 return true;
812 }
813
814 #define parse_buffer_has_space_for(jc, count) \
815 (jc->parse_buffer_count + (count) + 1 <= jc->parse_buffer_capacity)
@@ -830,27 +827,27 @@
827 jc->type == JSON_T_FALSE || \
828 jc->type == JSON_T_TRUE || \
829 jc->type == JSON_T_FLOAT || \
830 jc->type == JSON_T_INTEGER || \
831 jc->type == JSON_T_STRING)
832
833
834 static int parse_parse_buffer(JSON_parser jc)
835 {
836 if (jc->callback) {
837 JSON_value value, *arg = NULL;
838
839 if (jc->type != JSON_T_NONE) {
840 assert_is_non_container_type(jc);
841
842 switch(jc->type) {
843 case JSON_T_FLOAT:
844 arg = &value;
845 if (jc->handle_floats_manually) {
846 value.vu.str.value = jc->parse_buffer;
847 value.vu.str.length = jc->parse_buffer_count;
848 } else {
849 /* not checking with end pointer b/c there may be trailing ws */
850 value.vu.float_value = strtod(jc->parse_buffer, NULL);
851 }
852 break;
853 case JSON_T_INTEGER:
@@ -861,19 +858,19 @@
858 arg = &value;
859 value.vu.str.value = jc->parse_buffer;
860 value.vu.str.length = jc->parse_buffer_count;
861 break;
862 }
863
864 if (!(*jc->callback)(jc->ctx, jc->type, arg)) {
865 return false;
866 }
867 }
868 }
869
870 parse_buffer_clear(jc);
871
872 return true;
873 }
874
875 #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800)
876 #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00)
@@ -884,35 +881,39 @@
881 {
882 int i;
883 unsigned uc = 0;
884 char* p;
885 int trail_bytes;
886
887 assert(jc->parse_buffer_count >= 6);
888
889 p = &jc->parse_buffer[jc->parse_buffer_count - 4];
890
891 for (i = 12; i >= 0; i -= 4, ++p) {
892 unsigned x = *p;
893
894 if (x >= 'a') {
895 x -= ('a' - 10);
896 } else if (x >= 'A') {
897 x -= ('A' - 10);
898 } else {
899 x &= ~0x30u;
900 }
901
902 assert(x < 16);
903
904 uc |= x << i;
905 }
906
907 /* clear UTF-16 char from buffer */
908 jc->parse_buffer_count -= 6;
909 jc->parse_buffer[jc->parse_buffer_count] = 0;
910
911 if (uc == 0xffff || uc == 0xfffe) {
912 return false;
913 }
914
915 /* attempt decoding ... */
916 if (jc->utf16_high_surrogate) {
917 if (IS_LOW_SURROGATE(uc)) {
918 uc = DECODE_SURROGATE_PAIR(jc->utf16_high_surrogate, uc);
919 trail_bytes = 3;
@@ -935,26 +936,26 @@
936 return false;
937 } else {
938 trail_bytes = 2;
939 }
940 }
941
942 jc->parse_buffer[jc->parse_buffer_count++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]);
943
944 for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) {
945 jc->parse_buffer[jc->parse_buffer_count++] = (char) (((uc >> i) & 0x3F) | 0x80);
946 }
947
948 jc->parse_buffer[jc->parse_buffer_count] = 0;
949
950 return true;
951 }
952
953 static int add_escaped_char_to_parse_buffer(JSON_parser jc, int next_char)
954 {
955 assert(parse_buffer_has_space_for(jc, 1));
956
957 jc->escaped = 0;
958 /* remove the backslash */
959 parse_buffer_pop_back_char(jc);
960 switch(next_char) {
961 case 'b':
@@ -996,22 +997,22 @@
997 {
998 if (!parse_buffer_reserve_for(jc, 1)) {
999 assert(JSON_E_OUT_OF_MEMORY == jc->error);
1000 return false;
1001 }
1002
1003 if (jc->escaped) {
1004 if (!add_escaped_char_to_parse_buffer(jc, next_char)) {
1005 jc->error = JSON_E_INVALID_ESCAPE_SEQUENCE;
1006 return false;
1007 }
1008 } else if (!jc->comment) {
1009 if ((jc->type != JSON_T_NONE) | !((next_class == C_SPACE) | (next_class == C_WHITE)) /* non-white-space */) {
1010 parse_buffer_push_back_char(jc, (char)next_char);
1011 }
1012 }
1013
1014 return true;
1015 }
1016
1017 #define assert_type_isnt_string_null_or_bool(jc) \
1018 assert(jc->type != JSON_T_FALSE); \
@@ -1031,13 +1032,13 @@
1032 */
1033 int next_class, next_state;
1034
1035 /*
1036 Store the current char for error handling
1037 */
1038 jc->current_char = next_char;
1039
1040 /*
1041 Determine the character's class.
1042 */
1043 if (next_char < 0) {
1044 jc->error = JSON_E_INVALID_CHAR;
@@ -1050,15 +1051,15 @@
1051 if (next_class <= __) {
1052 set_error(jc);
1053 return false;
1054 }
1055 }
1056
1057 if (!add_char_to_parse_buffer(jc, next_char, next_class)) {
1058 return false;
1059 }
1060
1061 /*
1062 Get the next state from the state transition table.
1063 */
1064 next_state = state_transition_table[jc->state][next_class];
1065 if (next_state >= 0) {
@@ -1069,11 +1070,11 @@
1070 } else {
1071 /*
1072 Or perform one of the actions.
1073 */
1074 switch (next_state) {
1075 /* Unicode character */
1076 case UC:
1077 if(!decode_unicode_char(jc)) {
1078 jc->error = JSON_E_INVALID_UNICODE_SEQUENCE;
1079 return false;
1080 }
@@ -1091,78 +1092,78 @@
1092 break;
1093 /* integer detected by minus */
1094 case MX:
1095 jc->type = JSON_T_INTEGER;
1096 jc->state = MI;
1097 break;
1098 /* integer detected by zero */
1099 case ZX:
1100 jc->type = JSON_T_INTEGER;
1101 jc->state = ZE;
1102 break;
1103 /* integer detected by 1-9 */
1104 case IX:
1105 jc->type = JSON_T_INTEGER;
1106 jc->state = IT;
1107 break;
1108
1109 /* floating point number detected by exponent*/
1110 case DE:
1111 assert_type_isnt_string_null_or_bool(jc);
1112 jc->type = JSON_T_FLOAT;
1113 jc->state = E1;
1114 break;
1115
1116 /* floating point number detected by fraction */
1117 case DF:
1118 assert_type_isnt_string_null_or_bool(jc);
1119 if (!jc->handle_floats_manually) {
1120 /*
1121 Some versions of strtod (which underlies sscanf) don't support converting
1122 C-locale formated floating point values.
1123 */
1124 assert(jc->parse_buffer[jc->parse_buffer_count-1] == '.');
1125 jc->parse_buffer[jc->parse_buffer_count-1] = jc->decimal_point;
1126 }
1127 jc->type = JSON_T_FLOAT;
1128 jc->state = FX;
1129 break;
1130 /* string begin " */
1131 case SB:
1132 parse_buffer_clear(jc);
1133 assert(jc->type == JSON_T_NONE);
1134 jc->type = JSON_T_STRING;
1135 jc->state = ST;
1136 break;
1137
1138 /* n */
1139 case NU:
1140 assert(jc->type == JSON_T_NONE);
1141 jc->type = JSON_T_NULL;
1142 jc->state = N1;
1143 break;
1144 /* f */
1145 case FA:
1146 assert(jc->type == JSON_T_NONE);
1147 jc->type = JSON_T_FALSE;
1148 jc->state = F1;
1149 break;
1150 /* t */
1151 case TR:
1152 assert(jc->type == JSON_T_NONE);
1153 jc->type = JSON_T_TRUE;
1154 jc->state = T1;
1155 break;
1156
1157 /* closing comment */
1158 case CE:
1159 jc->comment = 0;
1160 assert(jc->parse_buffer_count == 0);
1161 assert(jc->type == JSON_T_NONE);
1162 jc->state = jc->before_comment_state;
1163 break;
1164
1165 /* opening comment */
1166 case CB:
1167 if (!jc->allow_comments) {
1168 return false;
1169 }
@@ -1172,11 +1173,11 @@
1173 }
1174 assert(jc->parse_buffer_count == 0);
1175 assert(jc->type != JSON_T_STRING);
1176 switch (jc->stack[jc->top]) {
1177 case MODE_ARRAY:
1178 case MODE_OBJECT:
1179 switch(jc->state) {
1180 case VA:
1181 case AR:
1182 jc->before_comment_state = jc->state;
1183 break;
@@ -1192,11 +1193,11 @@
1193 jc->type = JSON_T_NONE;
1194 jc->state = C1;
1195 jc->comment = 1;
1196 break;
1197 /* empty } */
1198 case -9:
1199 parse_buffer_clear(jc);
1200 if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
1201 return false;
1202 }
1203 if (!pop(jc, MODE_KEY)) {
@@ -1231,11 +1232,11 @@
1232 }
1233 if (!pop(jc, MODE_ARRAY)) {
1234 jc->error = JSON_E_UNBALANCED_COLLECTION;
1235 return false;
1236 }
1237
1238 jc->type = JSON_T_NONE;
1239 jc->state = OK;
1240 break;
1241
1242 /* { */ case -6:
@@ -1267,11 +1268,11 @@
1268 switch (jc->stack[jc->top]) {
1269 case MODE_KEY:
1270 assert(jc->type == JSON_T_STRING);
1271 jc->type = JSON_T_NONE;
1272 jc->state = CO;
1273
1274 if (jc->callback) {
1275 JSON_value value;
1276 value.vu.str.value = jc->parse_buffer;
1277 value.vu.str.length = jc->parse_buffer_count;
1278 if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
@@ -1357,29 +1358,29 @@
1358
1359
1360 int JSON_parser_is_legal_white_space_string(const char* s)
1361 {
1362 int c, char_class;
1363
1364 if (s == NULL) {
1365 return false;
1366 }
1367
1368 for (; *s; ++s) {
1369 c = *s;
1370
1371 if (c < 0 || c >= 128) {
1372 return false;
1373 }
1374
1375 char_class = ascii_class[c];
1376
1377 if (char_class != C_SPACE && char_class != C_WHITE) {
1378 return false;
1379 }
1380 }
1381
1382 return true;
1383 }
1384
1385 int JSON_parser_get_last_error(JSON_parser jc)
1386 {
@@ -1389,16 +1390,17 @@
1390
1391 void init_JSON_config(JSON_config* config)
1392 {
1393 if (config) {
1394 memset(config, 0, sizeof(*config));
1395
1396 config->depth = JSON_PARSER_STACK_SIZE - 1;
1397 config->malloc = malloc;
1398 config->free = free;
1399 }
1400 }
1401
1402 /* end file parser/JSON_parser.c */
1403 /* begin file ./cson.c */
1404 #include <assert.h>
1405 #include <stdlib.h> /* malloc()/free() */
1406 #include <string.h>
1407

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button