The R Project SVN R-packages

Rev

Rev 7747 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7747 Rev 8509
1
/* JSON_parser.c */
1
/* JSON_parser.c */
2
 
2
 
3
/* 2007-08-24 */
3
/* 2007-08-24 */
4
 
4
 
5
/*
5
/*
6
Copyright (c) 2005 JSON.org
6
Copyright (c) 2005 JSON.org
7
 
7
 
8
Permission is hereby granted, free of charge, to any person obtaining a copy
8
Permission is hereby granted, free of charge, to any person obtaining a copy
9
of this software and associated documentation files (the "Software"), to deal
9
of this software and associated documentation files (the "Software"), to deal
10
in the Software without restriction, including without limitation the rights
10
in the Software without restriction, including without limitation the rights
11
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
copies of the Software, and to permit persons to whom the Software is
12
copies of the Software, and to permit persons to whom the Software is
13
furnished to do so, subject to the following conditions:
13
furnished to do so, subject to the following conditions:
14
 
14
 
15
The above copyright notice and this permission notice shall be included in all
15
The above copyright notice and this permission notice shall be included in all
16
copies or substantial portions of the Software.
16
copies or substantial portions of the Software.
17
 
17
 
18
The Software shall be used for Good, not Evil.
18
The Software shall be used for Good, not Evil.
19
 
19
 
20
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
SOFTWARE.
26
SOFTWARE.
27
*/
27
*/
28
 
28
 
29
/*
29
/*
30
    Callbacks, comments, Unicode handling by Jean Gressmann (jean@0x42.de), 2007-2008.
30
    Callbacks, comments, Unicode handling by Jean Gressmann (jean@0x42.de), 2007-2008.
31
    
31
    
32
    For the added features the license above applies also.
32
    For the added features the license above applies also.
33
    
33
    
34
    Changelog:
34
    Changelog:
35
        2008/10/14 
35
        2008/10/14 
36
            - Renamed states.IN to states.IT to avoid name clash which IN macro
36
            - Renamed states.IN to states.IT to avoid name clash which IN macro
37
              defined in windef.h (alexey.pelykh@gmail.com)
37
              defined in windef.h (alexey.pelykh@gmail.com)
38
            
38
            
39
        2008/07/19 
39
        2008/07/19 
40
            - Removed some duplicate code & debugging variable (Charles.Kerr@noaa.gov)
40
            - Removed some duplicate code & debugging variable (Charles.Kerr@noaa.gov)
41
        
41
        
42
        2008/05/28 
42
        2008/05/28 
43
            - Made JSON_value structure ansi C compliant. This bug was report by 
43
            - Made JSON_value structure ansi C compliant. This bug was report by 
44
              trisk@acm.jhu.edu
44
              trisk@acm.jhu.edu
45
        
45
        
46
        2008/05/20 
46
        2008/05/20 
47
            - Fixed bug reported by Charles.Kerr@noaa.gov where the switching 
47
            - Fixed bug reported by Charles.Kerr@noaa.gov where the switching 
48
              from static to dynamic parse buffer did not copy the static parse 
48
              from static to dynamic parse buffer did not copy the static parse 
49
              buffer's content.
49
              buffer's content.
50
*/
50
*/
51
 
51
 
52
 
52
 
53
 
53
 
54
#include <assert.h>
54
#include <assert.h>
55
#include <ctype.h>
55
#include <ctype.h>
56
#include <float.h>
56
#include <float.h>
57
#include <stddef.h>
57
#include <stddef.h>
58
#include <stdio.h>
58
#include <stdio.h>
59
#include <stdlib.h>
59
#include <stdlib.h>
60
#include <string.h>
60
#include <string.h>
-
 
61
#include <stdbool.h>
61
 
62
 
62
#include "JSON_parser.h"
63
#include "JSON_parser.h"
63
#include "ConvertUTF.h"
64
#include "ConvertUTF.h"
64
 
65
 
65
 
66
 
66
#if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
67
#if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
67
#	pragma warning(disable:4996) // unsecure sscanf
68
#	pragma warning(disable:4996) // unsecure sscanf
68
#endif
69
#endif
69
 
70
 
70
 
71
/*
71
#define true  1
72
#define true  1
72
#define false 0
73
#define false 0
-
 
74
*/
73
#define __   -1     /* the universal error code */
75
#define __   -1     /* the universal error code */
74
 
76
 
75
/* values chosen so that the object size is approx equal to one page (4K) */
77
/* values chosen so that the object size is approx equal to one page (4K) */
76
#ifndef JSON_PARSER_STACK_SIZE
78
#ifndef JSON_PARSER_STACK_SIZE
77
#   define JSON_PARSER_STACK_SIZE 128
79
#   define JSON_PARSER_STACK_SIZE 128
78
#endif
80
#endif
79
 
81
 
80
#ifndef JSON_PARSER_PARSE_BUFFER_SIZE
82
#ifndef JSON_PARSER_PARSE_BUFFER_SIZE
81
#   define JSON_PARSER_PARSE_BUFFER_SIZE 3500
83
#   define JSON_PARSER_PARSE_BUFFER_SIZE 3500
82
#endif
84
#endif
83
 
85
 
84
 
86
 
85
typedef struct JSON_parser_struct {
87
typedef struct JSON_parser_struct {
86
    JSON_parser_callback callback;
88
    JSON_parser_callback callback;
87
    void* ctx;
89
    void* ctx;
88
    signed char state, before_comment_state, type, escaped, comment, allow_comments, handle_floats_manually;
90
    signed char state, before_comment_state, type, escaped, comment, allow_comments, handle_floats_manually;
89
    UTF16 utf16_decode_buffer[2];
91
    UTF16 utf16_decode_buffer[2];
90
    long depth;
92
    long depth;
91
    long top;
93
    long top;
92
    signed char* stack;
94
    signed char* stack;
93
    long stack_capacity;
95
    long stack_capacity;
94
    signed char static_stack[JSON_PARSER_STACK_SIZE];
96
    signed char static_stack[JSON_PARSER_STACK_SIZE];
95
    char* parse_buffer;
97
    char* parse_buffer;
96
    size_t parse_buffer_capacity;
98
    size_t parse_buffer_capacity;
97
    size_t parse_buffer_count;
99
    size_t parse_buffer_count;
98
    size_t comment_begin_offset;
100
    size_t comment_begin_offset;
99
    char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE];
101
    char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE];
100
} * JSON_parser;
102
} * JSON_parser;
101
 
103
 
102
#define COUNTOF(x) (sizeof(x)/sizeof(x[0])) 
104
#define COUNTOF(x) (sizeof(x)/sizeof(x[0])) 
103
 
105
 
104
/*
106
/*
105
    Characters are mapped into these 31 character classes. This allows for
107
    Characters are mapped into these 31 character classes. This allows for
106
    a significant reduction in the size of the state transition table.
108
    a significant reduction in the size of the state transition table.
107
*/
109
*/
108
 
110
 
109
 
111
 
110
 
112
 
111
enum classes {
113
enum classes {
112
    C_SPACE,  /* space */
114
    C_SPACE,  /* space */
113
    C_WHITE,  /* other whitespace */
115
    C_WHITE,  /* other whitespace */
114
    C_LCURB,  /* {  */
116
    C_LCURB,  /* {  */
115
    C_RCURB,  /* } */
117
    C_RCURB,  /* } */
116
    C_LSQRB,  /* [ */
118
    C_LSQRB,  /* [ */
117
    C_RSQRB,  /* ] */
119
    C_RSQRB,  /* ] */
118
    C_COLON,  /* : */
120
    C_COLON,  /* : */
119
    C_COMMA,  /* , */
121
    C_COMMA,  /* , */
120
    C_QUOTE,  /* " */
122
    C_QUOTE,  /* " */
121
    C_BACKS,  /* \ */
123
    C_BACKS,  /* \ */
122
    C_SLASH,  /* / */
124
    C_SLASH,  /* / */
123
    C_PLUS,   /* + */
125
    C_PLUS,   /* + */
124
    C_MINUS,  /* - */
126
    C_MINUS,  /* - */
125
    C_POINT,  /* . */
127
    C_POINT,  /* . */
126
    C_ZERO ,  /* 0 */
128
    C_ZERO ,  /* 0 */
127
    C_DIGIT,  /* 123456789 */
129
    C_DIGIT,  /* 123456789 */
128
    C_LOW_A,  /* a */
130
    C_LOW_A,  /* a */
129
    C_LOW_B,  /* b */
131
    C_LOW_B,  /* b */
130
    C_LOW_C,  /* c */
132
    C_LOW_C,  /* c */
131
    C_LOW_D,  /* d */
133
    C_LOW_D,  /* d */
132
    C_LOW_E,  /* e */
134
    C_LOW_E,  /* e */
133
    C_LOW_F,  /* f */
135
    C_LOW_F,  /* f */
134
    C_LOW_L,  /* l */
136
    C_LOW_L,  /* l */
135
    C_LOW_N,  /* n */
137
    C_LOW_N,  /* n */
136
    C_LOW_R,  /* r */
138
    C_LOW_R,  /* r */
137
    C_LOW_S,  /* s */
139
    C_LOW_S,  /* s */
138
    C_LOW_T,  /* t */
140
    C_LOW_T,  /* t */
139
    C_LOW_U,  /* u */
141
    C_LOW_U,  /* u */
140
    C_ABCDF,  /* ABCDF */
142
    C_ABCDF,  /* ABCDF */
141
    C_E,      /* E */
143
    C_E,      /* E */
142
    C_ETC,    /* everything else */
144
    C_ETC,    /* everything else */
143
    C_STAR,   /* * */   
145
    C_STAR,   /* * */   
144
    NR_CLASSES
146
    NR_CLASSES
145
};
147
};
146
 
148
 
147
static int ascii_class[128] = {
149
static int ascii_class[128] = {
148
/*
150
/*
149
    This array maps the 128 ASCII characters into character classes.
151
    This array maps the 128 ASCII characters into character classes.
150
    The remaining Unicode characters should be mapped to C_ETC.
152
    The remaining Unicode characters should be mapped to C_ETC.
151
    Non-whitespace control characters are errors.
153
    Non-whitespace control characters are errors.
152
*/
154
*/
153
    __,      __,      __,      __,      __,      __,      __,      __,
155
    __,      __,      __,      __,      __,      __,      __,      __,
154
    __,      C_WHITE, C_WHITE, __,      __,      C_WHITE, __,      __,
156
    __,      C_WHITE, C_WHITE, __,      __,      C_WHITE, __,      __,
155
    __,      __,      __,      __,      __,      __,      __,      __,
157
    __,      __,      __,      __,      __,      __,      __,      __,
156
    __,      __,      __,      __,      __,      __,      __,      __,
158
    __,      __,      __,      __,      __,      __,      __,      __,
157
 
159
 
158
    C_SPACE, C_ETC,   C_QUOTE, C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
160
    C_SPACE, C_ETC,   C_QUOTE, C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
159
    C_ETC,   C_ETC,   C_STAR,   C_PLUS,  C_COMMA, C_MINUS, C_POINT, C_SLASH,
161
    C_ETC,   C_ETC,   C_STAR,   C_PLUS,  C_COMMA, C_MINUS, C_POINT, C_SLASH,
160
    C_ZERO,  C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
162
    C_ZERO,  C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
161
    C_DIGIT, C_DIGIT, C_COLON, C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
163
    C_DIGIT, C_DIGIT, C_COLON, C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
162
 
164
 
163
    C_ETC,   C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E,     C_ABCDF, C_ETC,
165
    C_ETC,   C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E,     C_ABCDF, C_ETC,
164
    C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
166
    C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
165
    C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
167
    C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
166
    C_ETC,   C_ETC,   C_ETC,   C_LSQRB, C_BACKS, C_RSQRB, C_ETC,   C_ETC,
168
    C_ETC,   C_ETC,   C_ETC,   C_LSQRB, C_BACKS, C_RSQRB, C_ETC,   C_ETC,
167
 
169
 
168
    C_ETC,   C_LOW_A, C_LOW_B, C_LOW_C, C_LOW_D, C_LOW_E, C_LOW_F, C_ETC,
170
    C_ETC,   C_LOW_A, C_LOW_B, C_LOW_C, C_LOW_D, C_LOW_E, C_LOW_F, C_ETC,
169
    C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_LOW_L, C_ETC,   C_LOW_N, C_ETC,
171
    C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_LOW_L, C_ETC,   C_LOW_N, C_ETC,
170
    C_ETC,   C_ETC,   C_LOW_R, C_LOW_S, C_LOW_T, C_LOW_U, C_ETC,   C_ETC,
172
    C_ETC,   C_ETC,   C_LOW_R, C_LOW_S, C_LOW_T, C_LOW_U, C_ETC,   C_ETC,
171
    C_ETC,   C_ETC,   C_ETC,   C_LCURB, C_ETC,   C_RCURB, C_ETC,   C_ETC
173
    C_ETC,   C_ETC,   C_ETC,   C_LCURB, C_ETC,   C_RCURB, C_ETC,   C_ETC
172
};
174
};
173
 
175
 
174
 
176
 
175
/*
177
/*
176
    The state codes.
178
    The state codes.
177
*/
179
*/
178
enum states {
180
enum states {
179
    GO,  /* start    */
181
    GO,  /* start    */
180
    OK,  /* ok       */
182
    OK,  /* ok       */
181
    OB,  /* object   */
183
    OB,  /* object   */
182
    KE,  /* key      */
184
    KE,  /* key      */
183
    CO,  /* colon    */
185
    CO,  /* colon    */
184
    VA,  /* value    */
186
    VA,  /* value    */
185
    AR,  /* array    */
187
    AR,  /* array    */
186
    ST,  /* string   */
188
    ST,  /* string   */
187
    ES,  /* escape   */
189
    ES,  /* escape   */
188
    U1,  /* u1       */
190
    U1,  /* u1       */
189
    U2,  /* u2       */
191
    U2,  /* u2       */
190
    U3,  /* u3       */
192
    U3,  /* u3       */
191
    U4,  /* u4       */
193
    U4,  /* u4       */
192
    MI,  /* minus    */
194
    MI,  /* minus    */
193
    ZE,  /* zero     */
195
    ZE,  /* zero     */
194
    IT,  /* integer  */
196
    IT,  /* integer  */
195
    FR,  /* fraction */
197
    FR,  /* fraction */
196
    E1,  /* e        */
198
    E1,  /* e        */
197
    E2,  /* ex       */
199
    E2,  /* ex       */
198
    E3,  /* exp      */
200
    E3,  /* exp      */
199
    T1,  /* tr       */
201
    T1,  /* tr       */
200
    T2,  /* tru      */
202
    T2,  /* tru      */
201
    T3,  /* true     */
203
    T3,  /* true     */
202
    F1,  /* fa       */
204
    F1,  /* fa       */
203
    F2,  /* fal      */
205
    F2,  /* fal      */
204
    F3,  /* fals     */
206
    F3,  /* fals     */
205
    F4,  /* false    */
207
    F4,  /* false    */
206
    N1,  /* nu       */
208
    N1,  /* nu       */
207
    N2,  /* nul      */
209
    N2,  /* nul      */
208
    N3,  /* null     */
210
    N3,  /* null     */
209
    C1,  /* /        */
211
    C1,  /* /        */
210
    C2,  /* / *     */
212
    C2,  /* / *     */
211
    C3,  /* *        */
213
    C3,  /* *        */
212
    FX,  /* *.* *eE* */
214
    FX,  /* *.* *eE* */
213
    D1,  /* second UTF-16 character decoding started by \ */
215
    D1,  /* second UTF-16 character decoding started by \ */
214
    D2,  /* second UTF-16 character proceeded by u */
216
    D2,  /* second UTF-16 character proceeded by u */
215
    NR_STATES
217
    NR_STATES
216
};
218
};
217
 
219
 
218
enum actions
220
enum actions
219
{
221
{
220
    CB = -10, /* comment begin */
222
    CB = -10, /* comment begin */
221
    CE = -11, /* comment end */
223
    CE = -11, /* comment end */
222
    FA = -12, /* false */
224
    FA = -12, /* false */
223
    TR = -13, /* false */
225
    TR = -13, /* false */
224
    NU = -14, /* null */
226
    NU = -14, /* null */
225
    DE = -15, /* double detected by exponent e E */
227
    DE = -15, /* double detected by exponent e E */
226
    DF = -16, /* double detected by fraction . */
228
    DF = -16, /* double detected by fraction . */
227
    SB = -17, /* string begin */
229
    SB = -17, /* string begin */
228
    MX = -18, /* integer detected by minus */
230
    MX = -18, /* integer detected by minus */
229
    ZX = -19, /* integer detected by zero */
231
    ZX = -19, /* integer detected by zero */
230
    IX = -20, /* integer detected by 1-9 */
232
    IX = -20, /* integer detected by 1-9 */
231
    EX = -21, /* next char is escaped */
233
    EX = -21, /* next char is escaped */
232
    UC = -22, /* Unicode character read */
234
    UC = -22, /* Unicode character read */
233
};
235
};
234
 
236
 
235
 
237
 
236
static int state_transition_table[NR_STATES][NR_CLASSES] = {
238
static int state_transition_table[NR_STATES][NR_CLASSES] = {
237
/*
239
/*
238
    The state transition table takes the current state and the current symbol,
240
    The state transition table takes the current state and the current symbol,
239
    and returns either a new state or an action. An action is represented as a
241
    and returns either a new state or an action. An action is represented as a
240
    negative number. A JSON text is accepted if at the end of the text the
242
    negative number. A JSON text is accepted if at the end of the text the
241
    state is OK and if the mode is MODE_DONE.
243
    state is OK and if the mode is MODE_DONE.
242
 
244
 
243
                 white                                      1-9                                   ABCDF  etc
245
                 white                                      1-9                                   ABCDF  etc
244
             space |  {  }  [  ]  :  ,  "  \  /  +  -  .  0  |  a  b  c  d  e  f  l  n  r  s  t  u  |  E  |  * */
246
             space |  {  }  [  ]  :  ,  "  \  /  +  -  .  0  |  a  b  c  d  e  f  l  n  r  s  t  u  |  E  |  * */
245
/*start  GO*/ {GO,GO,-6,__,-5,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
247
/*start  GO*/ {GO,GO,-6,__,-5,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
246
/*ok     OK*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
248
/*ok     OK*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
247
/*object OB*/ {OB,OB,__,-9,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
249
/*object OB*/ {OB,OB,__,-9,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
248
/*key    KE*/ {KE,KE,__,__,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
250
/*key    KE*/ {KE,KE,__,__,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
249
/*colon  CO*/ {CO,CO,__,__,__,__,-2,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
251
/*colon  CO*/ {CO,CO,__,__,__,__,-2,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
250
/*value  VA*/ {VA,VA,-6,__,-5,__,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
252
/*value  VA*/ {VA,VA,-6,__,-5,__,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
251
/*array  AR*/ {AR,AR,-6,__,-5,-7,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
253
/*array  AR*/ {AR,AR,-6,__,-5,-7,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
252
/*string ST*/ {ST,__,ST,ST,ST,ST,ST,ST,-4,EX,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST},
254
/*string ST*/ {ST,__,ST,ST,ST,ST,ST,ST,-4,EX,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST},
253
/*escape ES*/ {__,__,__,__,__,__,__,__,ST,ST,ST,__,__,__,__,__,__,ST,__,__,__,ST,__,ST,ST,__,ST,U1,__,__,__,__},
255
/*escape ES*/ {__,__,__,__,__,__,__,__,ST,ST,ST,__,__,__,__,__,__,ST,__,__,__,ST,__,ST,ST,__,ST,U1,__,__,__,__},
254
/*u1     U1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U2,U2,U2,U2,U2,U2,U2,U2,__,__,__,__,__,__,U2,U2,__,__},
256
/*u1     U1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U2,U2,U2,U2,U2,U2,U2,U2,__,__,__,__,__,__,U2,U2,__,__},
255
/*u2     U2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U3,U3,U3,U3,U3,U3,U3,U3,__,__,__,__,__,__,U3,U3,__,__},
257
/*u2     U2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U3,U3,U3,U3,U3,U3,U3,U3,__,__,__,__,__,__,U3,U3,__,__},
256
/*u3     U3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U4,U4,U4,U4,U4,U4,U4,U4,__,__,__,__,__,__,U4,U4,__,__},
258
/*u3     U3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U4,U4,U4,U4,U4,U4,U4,U4,__,__,__,__,__,__,U4,U4,__,__},
257
/*u4     U4*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,UC,UC,UC,UC,UC,UC,UC,UC,__,__,__,__,__,__,UC,UC,__,__},
259
/*u4     U4*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,UC,UC,UC,UC,UC,UC,UC,UC,__,__,__,__,__,__,UC,UC,__,__},
258
/*minus  MI*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,ZE,IT,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
260
/*minus  MI*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,ZE,IT,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
259
/*zero   ZE*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
261
/*zero   ZE*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
260
/*int    IT*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,IT,IT,__,__,__,__,DE,__,__,__,__,__,__,__,__,DE,__,__},
262
/*int    IT*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,IT,IT,__,__,__,__,DE,__,__,__,__,__,__,__,__,DE,__,__},
261
/*frac   FR*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
263
/*frac   FR*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
262
/*e      E1*/ {__,__,__,__,__,__,__,__,__,__,__,E2,E2,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
264
/*e      E1*/ {__,__,__,__,__,__,__,__,__,__,__,E2,E2,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
263
/*ex     E2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
265
/*ex     E2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
264
/*exp    E3*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
266
/*exp    E3*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
265
/*tr     T1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T2,__,__,__,__,__,__,__},
267
/*tr     T1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T2,__,__,__,__,__,__,__},
266
/*tru    T2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T3,__,__,__,__},
268
/*tru    T2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T3,__,__,__,__},
267
/*true   T3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
269
/*true   T3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
268
/*fa     F1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
270
/*fa     F1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
269
/*fal    F2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F3,__,__,__,__,__,__,__,__,__},
271
/*fal    F2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F3,__,__,__,__,__,__,__,__,__},
270
/*fals   F3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F4,__,__,__,__,__,__},
272
/*fals   F3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F4,__,__,__,__,__,__},
271
/*false  F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
273
/*false  F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
272
/*nu     N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__},
274
/*nu     N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__},
273
/*nul    N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__},
275
/*nul    N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__},
274
/*null   N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__},
276
/*null   N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__},
275
/*/      C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2},
277
/*/      C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2},
276
/*/*     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},
278
/*/*     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},
277
/**      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},
279
/**      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},
278
/*_.     FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
280
/*_.     FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
279
/*\      D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
281
/*\      D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
280
/*\      D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__},
282
/*\      D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__},
281
};
283
};
282
 
284
 
283
 
285
 
284
/*
286
/*
285
    These modes can be pushed on the stack.
287
    These modes can be pushed on the stack.
286
*/
288
*/
287
enum modes {
289
enum modes {
288
    MODE_ARRAY = 1, 
290
    MODE_ARRAY = 1, 
289
    MODE_DONE = 2,  
291
    MODE_DONE = 2,  
290
    MODE_KEY = 3,   
292
    MODE_KEY = 3,   
291
    MODE_OBJECT = 4
293
    MODE_OBJECT = 4
292
};
294
};
293
 
295
 
294
static int
296
static int
295
push(JSON_parser jc, int mode)
297
push(JSON_parser jc, int mode)
296
{
298
{
297
/*
299
/*
298
    Push a mode onto the stack. Return false if there is overflow.
300
    Push a mode onto the stack. Return false if there is overflow.
299
*/
301
*/
300
    jc->top += 1;
302
    jc->top += 1;
301
    if (jc->depth < 0) {
303
    if (jc->depth < 0) {
302
        if (jc->top >= jc->stack_capacity) {
304
        if (jc->top >= jc->stack_capacity) {
303
            size_t bytes_to_allocate;
305
            size_t bytes_to_allocate;
304
            jc->stack_capacity *= 2;
306
            jc->stack_capacity *= 2;
305
            bytes_to_allocate = jc->stack_capacity * sizeof(jc->static_stack[0]);
307
            bytes_to_allocate = jc->stack_capacity * sizeof(jc->static_stack[0]);
306
            if (jc->stack == &jc->static_stack[0]) {
308
            if (jc->stack == &jc->static_stack[0]) {
307
                jc->stack = (signed char*)malloc(bytes_to_allocate);
309
                jc->stack = (signed char*)malloc(bytes_to_allocate);
308
                memcpy(jc->stack, jc->static_stack, sizeof(jc->static_stack));
310
                memcpy(jc->stack, jc->static_stack, sizeof(jc->static_stack));
309
            } else {
311
            } else {
310
                jc->stack = (signed char*)realloc(jc->stack, bytes_to_allocate);
312
                jc->stack = (signed char*)realloc(jc->stack, bytes_to_allocate);
311
            }
313
            }
312
        }
314
        }
313
    } else {
315
    } else {
314
        if (jc->top >= jc->depth) {
316
        if (jc->top >= jc->depth) {
315
            return false;
317
            return false;
316
        }
318
        }
317
    }
319
    }
318
    
320
    
319
    jc->stack[jc->top] = (signed char)mode;
321
    jc->stack[jc->top] = (signed char)mode;
320
    return true;
322
    return true;
321
}
323
}
322
 
324
 
323
 
325
 
324
static int
326
static int
325
pop(JSON_parser jc, int mode)
327
pop(JSON_parser jc, int mode)
326
{
328
{
327
/*
329
/*
328
    Pop the stack, assuring that the current mode matches the expectation.
330
    Pop the stack, assuring that the current mode matches the expectation.
329
    Return false if there is underflow or if the modes mismatch.
331
    Return false if there is underflow or if the modes mismatch.
330
*/
332
*/
331
    if (jc->top < 0 || jc->stack[jc->top] != mode) {
333
    if (jc->top < 0 || jc->stack[jc->top] != mode) {
332
        return false;
334
        return false;
333
    }
335
    }
334
    jc->top -= 1;
336
    jc->top -= 1;
335
    return true;
337
    return true;
336
}
338
}
337
 
339
 
338
 
340
 
339
#define parse_buffer_clear(jc) \
341
#define parse_buffer_clear(jc) \
340
    do {\
342
    do {\
341
        jc->parse_buffer_count = 0;\
343
        jc->parse_buffer_count = 0;\
342
        jc->parse_buffer[0] = 0;\
344
        jc->parse_buffer[0] = 0;\
343
    } while (0)
345
    } while (0)
344
    
346
    
345
#define parse_buffer_pop_back_char(jc)\
347
#define parse_buffer_pop_back_char(jc)\
346
    do {\
348
    do {\
347
        assert(jc->parse_buffer_count >= 1);\
349
        assert(jc->parse_buffer_count >= 1);\
348
        --jc->parse_buffer_count;\
350
        --jc->parse_buffer_count;\
349
        jc->parse_buffer[jc->parse_buffer_count] = 0;\
351
        jc->parse_buffer[jc->parse_buffer_count] = 0;\
350
    } while (0)    
352
    } while (0)    
351
    
353
    
352
void delete_JSON_parser(JSON_parser jc)
354
void delete_JSON_parser(JSON_parser jc)
353
{
355
{
354
    if (jc) {
356
    if (jc) {
355
        if (jc->stack != &jc->static_stack[0]) {
357
        if (jc->stack != &jc->static_stack[0]) {
356
            free((void*)jc->stack);
358
            free((void*)jc->stack);
357
        }
359
        }
358
        if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
360
        if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
359
            free((void*)jc->parse_buffer);
361
            free((void*)jc->parse_buffer);
360
        }
362
        }
361
        free((void*)jc);
363
        free((void*)jc);
362
     }   
364
     }   
363
}
365
}
364
 
366
 
365
 
367
 
366
JSON_parser
368
JSON_parser
367
new_JSON_parser(JSON_config* config)
369
new_JSON_parser(JSON_config* config)
368
{
370
{
369
/*
371
/*
370
    new_JSON_parser starts the checking process by constructing a JSON_parser
372
    new_JSON_parser starts the checking process by constructing a JSON_parser
371
    object. It takes a depth parameter that restricts the level of maximum
373
    object. It takes a depth parameter that restricts the level of maximum
372
    nesting.
374
    nesting.
373
 
375
 
374
    To continue the process, call JSON_parser_char for each character in the
376
    To continue the process, call JSON_parser_char for each character in the
375
    JSON text, and then call JSON_parser_done to obtain the final result.
377
    JSON text, and then call JSON_parser_done to obtain the final result.
376
    These functions are fully reentrant.
378
    These functions are fully reentrant.
377
*/
379
*/
378
 
380
 
379
    int depth = 0;
381
    int depth = 0;
380
    JSON_config default_config;
382
    JSON_config default_config;
381
    
383
    
382
    JSON_parser jc = malloc(sizeof(struct JSON_parser_struct));
384
    JSON_parser jc = malloc(sizeof(struct JSON_parser_struct));
383
    
385
    
384
    memset(jc, 0, sizeof(*jc));
386
    memset(jc, 0, sizeof(*jc));
385
    
387
    
386
    
388
    
387
    /* initialize configuration */
389
    /* initialize configuration */
388
    init_JSON_config(&default_config);
390
    init_JSON_config(&default_config);
389
    
391
    
390
    /* set to default configuration if none was provided */
392
    /* set to default configuration if none was provided */
391
    if (config == NULL) {
393
    if (config == NULL) {
392
        config = &default_config;
394
        config = &default_config;
393
    }
395
    }
394
 
396
 
395
    depth = config->depth;
397
    depth = config->depth;
396
    
398
    
397
    /* We need to be able to push at least one object */
399
    /* We need to be able to push at least one object */
398
    if (depth == 0) {
400
    if (depth == 0) {
399
        depth = 1;
401
        depth = 1;
400
    }
402
    }
401
    
403
    
402
    jc->state = GO;
404
    jc->state = GO;
403
    jc->top = -1;
405
    jc->top = -1;
404
    
406
    
405
    /* Do we want non-bound stack? */
407
    /* Do we want non-bound stack? */
406
    if (depth > 0) {
408
    if (depth > 0) {
407
        jc->stack_capacity = depth;
409
        jc->stack_capacity = depth;
408
        jc->depth = depth;
410
        jc->depth = depth;
409
        if (depth <= COUNTOF(jc->static_stack)) {
411
        if (depth <= COUNTOF(jc->static_stack)) {
410
            jc->stack = &jc->static_stack[0];
412
            jc->stack = &jc->static_stack[0];
411
        } else {
413
        } else {
412
            jc->stack = (signed char*)malloc(jc->stack_capacity * sizeof(jc->static_stack[0]));
414
            jc->stack = (signed char*)malloc(jc->stack_capacity * sizeof(jc->static_stack[0]));
413
        }
415
        }
414
    } else {
416
    } else {
415
        jc->stack_capacity = COUNTOF(jc->static_stack);
417
        jc->stack_capacity = COUNTOF(jc->static_stack);
416
        jc->depth = -1;
418
        jc->depth = -1;
417
        jc->stack = &jc->static_stack[0];
419
        jc->stack = &jc->static_stack[0];
418
    }
420
    }
419
    
421
    
420
    /* set parser to start */
422
    /* set parser to start */
421
    push(jc, MODE_DONE);
423
    push(jc, MODE_DONE);
422
    
424
    
423
    /* set up the parse buffer */
425
    /* set up the parse buffer */
424
    jc->parse_buffer = &jc->static_parse_buffer[0];
426
    jc->parse_buffer = &jc->static_parse_buffer[0];
425
    jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer);
427
    jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer);
426
    parse_buffer_clear(jc);
428
    parse_buffer_clear(jc);
427
    
429
    
428
    /* set up callback, comment & float handling */
430
    /* set up callback, comment & float handling */
429
    jc->callback = config->callback;
431
    jc->callback = config->callback;
430
    jc->ctx = config->callback_ctx;
432
    jc->ctx = config->callback_ctx;
431
    jc->allow_comments = config->allow_comments != 0;
433
    jc->allow_comments = config->allow_comments != 0;
432
    jc->handle_floats_manually = config->handle_floats_manually != 0;
434
    jc->handle_floats_manually = config->handle_floats_manually != 0;
433
    return jc;
435
    return jc;
434
}
436
}
435
 
437
 
436
static void grow_parse_buffer(JSON_parser jc)
438
static void grow_parse_buffer(JSON_parser jc)
437
{
439
{
438
    size_t bytes_to_allocate;
440
    size_t bytes_to_allocate;
439
    jc->parse_buffer_capacity *= 2;
441
    jc->parse_buffer_capacity *= 2;
440
    bytes_to_allocate = jc->parse_buffer_capacity * sizeof(jc->parse_buffer[0]);
442
    bytes_to_allocate = jc->parse_buffer_capacity * sizeof(jc->parse_buffer[0]);
441
    if (jc->parse_buffer == &jc->static_parse_buffer[0]) {
443
    if (jc->parse_buffer == &jc->static_parse_buffer[0]) {
442
        jc->parse_buffer = (char*)malloc(bytes_to_allocate);
444
        jc->parse_buffer = (char*)malloc(bytes_to_allocate);
443
        memcpy(jc->parse_buffer, jc->static_parse_buffer, jc->parse_buffer_count);
445
        memcpy(jc->parse_buffer, jc->static_parse_buffer, jc->parse_buffer_count);
444
    } else {
446
    } else {
445
        jc->parse_buffer = (char*)realloc(jc->parse_buffer, bytes_to_allocate);
447
        jc->parse_buffer = (char*)realloc(jc->parse_buffer, bytes_to_allocate);
446
    }
448
    }
447
}
449
}
448
 
450
 
449
#define parse_buffer_push_back_char(jc, c)\
451
#define parse_buffer_push_back_char(jc, c)\
450
    do {\
452
    do {\
451
        if (jc->parse_buffer_count + 1 >= jc->parse_buffer_capacity) grow_parse_buffer(jc);\
453
        if (jc->parse_buffer_count + 1 >= jc->parse_buffer_capacity) grow_parse_buffer(jc);\
452
        jc->parse_buffer[jc->parse_buffer_count++] = c;\
454
        jc->parse_buffer[jc->parse_buffer_count++] = c;\
453
        jc->parse_buffer[jc->parse_buffer_count]   = 0;\
455
        jc->parse_buffer[jc->parse_buffer_count]   = 0;\
454
    } while (0)
456
    } while (0)
455
 
457
 
456
 
458
 
457
static int parse_parse_buffer(JSON_parser jc)
459
static int parse_parse_buffer(JSON_parser jc)
458
{
460
{
459
    if (jc->callback) {
461
    if (jc->callback) {
460
        JSON_value value, *arg = NULL;
462
        JSON_value value, *arg = NULL;
461
        
463
        
462
        if (jc->type != JSON_T_NONE) {
464
        if (jc->type != JSON_T_NONE) {
463
            assert(
465
            assert(
464
                jc->type == JSON_T_NULL ||
466
                jc->type == JSON_T_NULL ||
465
                jc->type == JSON_T_FALSE ||
467
                jc->type == JSON_T_FALSE ||
466
                jc->type == JSON_T_TRUE ||
468
                jc->type == JSON_T_TRUE ||
467
                jc->type == JSON_T_FLOAT ||
469
                jc->type == JSON_T_FLOAT ||
468
                jc->type == JSON_T_INTEGER ||
470
                jc->type == JSON_T_INTEGER ||
469
                jc->type == JSON_T_STRING);
471
                jc->type == JSON_T_STRING);
470
        
472
        
471
            switch(jc->type) {
473
            switch(jc->type) {
472
                case JSON_T_FLOAT:
474
                case JSON_T_FLOAT:
473
                    arg = &value;
475
                    arg = &value;
474
                    if (jc->handle_floats_manually) {
476
                    if (jc->handle_floats_manually) {
475
                        value.vu.str.value = jc->parse_buffer;
477
                        value.vu.str.value = jc->parse_buffer;
476
                        value.vu.str.length = jc->parse_buffer_count;
478
                        value.vu.str.length = jc->parse_buffer_count;
477
                    } else { 
479
                    } else { 
478
                        sscanf(jc->parse_buffer, DoubleScanFormat, &value.vu.float_value);
480
                        sscanf(jc->parse_buffer, DoubleScanFormat, &value.vu.float_value);
479
                    }
481
                    }
480
                    break;
482
                    break;
481
                case JSON_T_INTEGER: {
483
                case JSON_T_INTEGER: {
482
                    double tmp;
484
                    double tmp;
483
                    arg = &value;
485
                    arg = &value;
484
		    // value unused
486
		    // value unused
485
                    int ok = sscanf(jc->parse_buffer, "%lf", &tmp);
487
                    int ok = sscanf(jc->parse_buffer, "%lf", &tmp);
486
	            if(tmp > MAX_INT || tmp < - MAX_INT) {
488
	            if(tmp > MAX_INT || tmp < - MAX_INT) {
487
                       jc->type = JSON_T_FLOAT;
489
                       jc->type = JSON_T_FLOAT;
488
	               value.vu.float_value = tmp;
490
	               value.vu.float_value = tmp;
489
	            } else
491
	            } else
490
			value.vu.integer_value = (JSON_int_t)tmp;
492
			value.vu.integer_value = (JSON_int_t)tmp;
491
                    break;
493
                    break;
492
                }
494
                }
493
                case JSON_T_STRING:
495
                case JSON_T_STRING:
494
                    arg = &value;
496
                    arg = &value;
495
                    value.vu.str.value = jc->parse_buffer;
497
                    value.vu.str.value = jc->parse_buffer;
496
                    value.vu.str.length = jc->parse_buffer_count;
498
                    value.vu.str.length = jc->parse_buffer_count;
497
                    break;
499
                    break;
498
            }
500
            }
499
            
501
            
500
            if (!(*jc->callback)(jc->ctx, jc->type, arg)) {
502
            if (!(*jc->callback)(jc->ctx, jc->type, arg)) {
501
                return false;
503
                return false;
502
            }
504
            }
503
        }
505
        }
504
    }
506
    }
505
    
507
    
506
    parse_buffer_clear(jc);
508
    parse_buffer_clear(jc);
507
    
509
    
508
    return true;
510
    return true;
509
}
511
}
510
 
512
 
511
static int decode_unicode_char(JSON_parser jc)
513
static int decode_unicode_char(JSON_parser jc)
512
{
514
{
513
    const unsigned chars = jc->utf16_decode_buffer[0] ? 2 : 1;
515
    const unsigned chars = jc->utf16_decode_buffer[0] ? 2 : 1;
514
    int i;
516
    int i;
515
    UTF16 *uc = chars == 1 ? &jc->utf16_decode_buffer[0] : &jc->utf16_decode_buffer[1];
517
    UTF16 *uc = chars == 1 ? &jc->utf16_decode_buffer[0] : &jc->utf16_decode_buffer[1];
516
    UTF16 x;
518
    UTF16 x;
517
    char* p;
519
    char* p;
518
    
520
    
519
    assert(jc->parse_buffer_count >= 6);
521
    assert(jc->parse_buffer_count >= 6);
520
    
522
    
521
    p = &jc->parse_buffer[jc->parse_buffer_count - 4];
523
    p = &jc->parse_buffer[jc->parse_buffer_count - 4];
522
    
524
    
523
    for (i = 0; i < 4; ++i, ++p) {
525
    for (i = 0; i < 4; ++i, ++p) {
524
        x = *p;
526
        x = *p;
525
        
527
        
526
        if (x >= 'a') {
528
        if (x >= 'a') {
527
            x -= ('a' - 10);
529
            x -= ('a' - 10);
528
        } else if (x >= 'A') {
530
        } else if (x >= 'A') {
529
            x -= ('A' - 10);
531
            x -= ('A' - 10);
530
        } else {
532
        } else {
531
            x &= ~((UTF16) 0x30);
533
            x &= ~((UTF16) 0x30);
532
        }
534
        }
533
        
535
        
534
        assert(x < 16);
536
        assert(x < 16);
535
        
537
        
536
        *uc |= x << ((3u - i) << 2);
538
        *uc |= x << ((3u - i) << 2);
537
    }
539
    }
538
    
540
    
539
    /* clear UTF-16 char form buffer */
541
    /* clear UTF-16 char form buffer */
540
    jc->parse_buffer_count -= 6;
542
    jc->parse_buffer_count -= 6;
541
    jc->parse_buffer[jc->parse_buffer_count] = 0;
543
    jc->parse_buffer[jc->parse_buffer_count] = 0;
542
    
544
    
543
    /* attempt decoding ... */
545
    /* attempt decoding ... */
544
    {
546
    {
545
        UTF8* dec_start = (UTF8*)&jc->parse_buffer[jc->parse_buffer_count];
547
        UTF8* dec_start = (UTF8*)&jc->parse_buffer[jc->parse_buffer_count];
546
        UTF8* dec_start_dup = dec_start;
548
        UTF8* dec_start_dup = dec_start;
547
        UTF8* dec_end = dec_start + 6;
549
        UTF8* dec_end = dec_start + 6;
548
        
550
        
549
        const UTF16* enc_start = &jc->utf16_decode_buffer[0];
551
        const UTF16* enc_start = &jc->utf16_decode_buffer[0];
550
        const UTF16* enc_end = enc_start + chars;
552
        const UTF16* enc_end = enc_start + chars;
551
    
553
    
552
        const ConversionResult result = ConvertUTF16toUTF8(
554
        const ConversionResult result = ConvertUTF16toUTF8(
553
            &enc_start, enc_end, &dec_start, dec_end, strictConversion);
555
            &enc_start, enc_end, &dec_start, dec_end, strictConversion);
554
        
556
        
555
        const size_t new_chars = dec_start - dec_start_dup;
557
        const size_t new_chars = dec_start - dec_start_dup;
556
        
558
        
557
        /* was it a surrogate UTF-16 char? */
559
        /* was it a surrogate UTF-16 char? */
558
        if (chars == 1 && result == sourceExhausted) {
560
        if (chars == 1 && result == sourceExhausted) {
559
            return true;
561
            return true;
560
        }
562
        }
561
        
563
        
562
        if (result != conversionOK) {
564
        if (result != conversionOK) {
563
            return false;
565
            return false;
564
        }
566
        }
565
        
567
        
566
        /* NOTE: clear decode buffer to resume string reading,
568
        /* NOTE: clear decode buffer to resume string reading,
567
           otherwise we continue to read UTF-16 */
569
           otherwise we continue to read UTF-16 */
568
        jc->utf16_decode_buffer[0] = 0;
570
        jc->utf16_decode_buffer[0] = 0;
569
        
571
        
570
        assert(new_chars <= 6);
572
        assert(new_chars <= 6);
571
        
573
        
572
        jc->parse_buffer_count += new_chars;
574
        jc->parse_buffer_count += new_chars;
573
        jc->parse_buffer[jc->parse_buffer_count] = 0;
575
        jc->parse_buffer[jc->parse_buffer_count] = 0;
574
    }
576
    }
575
    
577
    
576
    return true;
578
    return true;
577
}
579
}
578
 
580
 
579
 
581
 
580
int
582
int
581
JSON_parser_char(JSON_parser jc, int next_char)
583
JSON_parser_char(JSON_parser jc, int next_char)
582
{
584
{
583
/*
585
/*
584
    After calling new_JSON_parser, call this function for each character (or
586
    After calling new_JSON_parser, call this function for each character (or
585
    partial character) in your JSON text. It can accept UTF-8, UTF-16, or
587
    partial character) in your JSON text. It can accept UTF-8, UTF-16, or
586
    UTF-32. It returns true if things are looking ok so far. If it rejects the
588
    UTF-32. It returns true if things are looking ok so far. If it rejects the
587
    text, it returns false.
589
    text, it returns false.
588
*/
590
*/
589
    int next_class, next_state;
591
    int next_class, next_state;
590
    
592
    
591
/*
593
/*
592
    Determine the character's class.
594
    Determine the character's class.
593
*/
595
*/
594
    if (next_char < 0) {
596
    if (next_char < 0) {
595
        return false;
597
        return false;
596
    }
598
    }
597
    if (next_char >= 128) {
599
    if (next_char >= 128) {
598
        next_class = C_ETC;
600
        next_class = C_ETC;
599
    } else {
601
    } else {
600
        next_class = ascii_class[next_char];
602
        next_class = ascii_class[next_char];
601
        if (next_class <= __) {
603
        if (next_class <= __) {
602
            return false;
604
            return false;
603
        }
605
        }
604
    }
606
    }
605
    
607
    
606
    if (jc->escaped) {
608
    if (jc->escaped) {
607
        jc->escaped = 0;
609
        jc->escaped = 0;
608
        /* remove the backslash */
610
        /* remove the backslash */
609
        parse_buffer_pop_back_char(jc);
611
        parse_buffer_pop_back_char(jc);
610
        switch(next_char) {
612
        switch(next_char) {
611
        case 'b':
613
        case 'b':
612
            parse_buffer_push_back_char(jc, '\b');
614
            parse_buffer_push_back_char(jc, '\b');
613
            break;
615
            break;
614
        case 'f':
616
        case 'f':
615
            parse_buffer_push_back_char(jc, '\f');
617
            parse_buffer_push_back_char(jc, '\f');
616
            break;
618
            break;
617
        case 'n':
619
        case 'n':
618
            parse_buffer_push_back_char(jc, '\n');
620
            parse_buffer_push_back_char(jc, '\n');
619
            break;
621
            break;
620
        case 'r':
622
        case 'r':
621
            parse_buffer_push_back_char(jc, '\r');
623
            parse_buffer_push_back_char(jc, '\r');
622
            break;
624
            break;
623
        case 't':
625
        case 't':
624
            parse_buffer_push_back_char(jc, '\t');
626
            parse_buffer_push_back_char(jc, '\t');
625
            break;
627
            break;
626
        case '"':
628
        case '"':
627
            parse_buffer_push_back_char(jc, '"');
629
            parse_buffer_push_back_char(jc, '"');
628
            break;
630
            break;
629
        case '\\':
631
        case '\\':
630
            parse_buffer_push_back_char(jc, '\\');
632
            parse_buffer_push_back_char(jc, '\\');
631
            break;
633
            break;
632
        case '/':
634
        case '/':
633
            parse_buffer_push_back_char(jc, '/');
635
            parse_buffer_push_back_char(jc, '/');
634
            break;
636
            break;
635
        case 'u':
637
        case 'u':
636
            parse_buffer_push_back_char(jc, '\\');
638
            parse_buffer_push_back_char(jc, '\\');
637
            parse_buffer_push_back_char(jc, 'u');
639
            parse_buffer_push_back_char(jc, 'u');
638
            break;
640
            break;
639
        default:
641
        default:
640
            return false;
642
            return false;
641
        }
643
        }
642
    } else if (!jc->comment) { 
644
    } else if (!jc->comment) { 
643
        if (jc->type != JSON_T_NONE || !(next_class == C_SPACE || next_class == C_WHITE) /* non-white-space */) {
645
        if (jc->type != JSON_T_NONE || !(next_class == C_SPACE || next_class == C_WHITE) /* non-white-space */) {
644
            parse_buffer_push_back_char(jc, (char)next_char);
646
            parse_buffer_push_back_char(jc, (char)next_char);
645
        }
647
        }
646
    }
648
    }
647
    
649
    
648
    
650
    
649
    
651
    
650
/*
652
/*
651
    Get the next state from the state transition table.
653
    Get the next state from the state transition table.
652
*/
654
*/
653
    next_state = state_transition_table[jc->state][next_class];
655
    next_state = state_transition_table[jc->state][next_class];
654
    if (next_state >= 0) {
656
    if (next_state >= 0) {
655
/*
657
/*
656
    Change the state.
658
    Change the state.
657
*/
659
*/
658
        jc->state = (signed char) next_state;
660
        jc->state = (signed char) next_state;
659
    } else {
661
    } else {
660
/*
662
/*
661
    Or perform one of the actions.
663
    Or perform one of the actions.
662
*/
664
*/
663
        switch (next_state) {
665
        switch (next_state) {
664
/* Unicode character */        
666
/* Unicode character */        
665
        case UC:
667
        case UC:
666
            if(!decode_unicode_char(jc)) {
668
            if(!decode_unicode_char(jc)) {
667
                return false;
669
                return false;
668
            }
670
            }
669
            /* check if we need to read a second UTF-16 char */
671
            /* check if we need to read a second UTF-16 char */
670
            if (jc->utf16_decode_buffer[0]) {
672
            if (jc->utf16_decode_buffer[0]) {
671
                jc->state = D1;
673
                jc->state = D1;
672
            } else {
674
            } else {
673
                jc->state = ST;
675
                jc->state = ST;
674
            }
676
            }
675
            break;
677
            break;
676
/* escaped char */
678
/* escaped char */
677
        case EX:
679
        case EX:
678
            jc->escaped = 1;
680
            jc->escaped = 1;
679
            jc->state = ES;
681
            jc->state = ES;
680
            break;
682
            break;
681
/* integer detected by minus */
683
/* integer detected by minus */
682
        case MX:
684
        case MX:
683
            jc->type = JSON_T_INTEGER;
685
            jc->type = JSON_T_INTEGER;
684
            jc->state = MI;
686
            jc->state = MI;
685
            break;  
687
            break;  
686
/* integer detected by zero */            
688
/* integer detected by zero */            
687
        case ZX:
689
        case ZX:
688
            jc->type = JSON_T_INTEGER;
690
            jc->type = JSON_T_INTEGER;
689
            jc->state = ZE;
691
            jc->state = ZE;
690
            break;  
692
            break;  
691
/* integer detected by 1-9 */            
693
/* integer detected by 1-9 */            
692
        case IX:
694
        case IX:
693
            jc->type = JSON_T_INTEGER;
695
            jc->type = JSON_T_INTEGER;
694
            jc->state = IT;
696
            jc->state = IT;
695
            break;  
697
            break;  
696
            
698
            
697
/* floating point number detected by exponent*/
699
/* floating point number detected by exponent*/
698
        case DE:
700
        case DE:
699
            assert(jc->type != JSON_T_FALSE);
701
            assert(jc->type != JSON_T_FALSE);
700
            assert(jc->type != JSON_T_TRUE);
702
            assert(jc->type != JSON_T_TRUE);
701
            assert(jc->type != JSON_T_NULL);
703
            assert(jc->type != JSON_T_NULL);
702
            assert(jc->type != JSON_T_STRING);
704
            assert(jc->type != JSON_T_STRING);
703
            jc->type = JSON_T_FLOAT;
705
            jc->type = JSON_T_FLOAT;
704
            jc->state = E1;
706
            jc->state = E1;
705
            break;   
707
            break;   
706
        
708
        
707
/* floating point number detected by fraction */
709
/* floating point number detected by fraction */
708
        case DF:
710
        case DF:
709
            assert(jc->type != JSON_T_FALSE);
711
            assert(jc->type != JSON_T_FALSE);
710
            assert(jc->type != JSON_T_TRUE);
712
            assert(jc->type != JSON_T_TRUE);
711
            assert(jc->type != JSON_T_NULL);
713
            assert(jc->type != JSON_T_NULL);
712
            assert(jc->type != JSON_T_STRING);
714
            assert(jc->type != JSON_T_STRING);
713
            jc->type = JSON_T_FLOAT;
715
            jc->type = JSON_T_FLOAT;
714
            jc->state = FX;
716
            jc->state = FX;
715
            break;   
717
            break;   
716
/* string begin " */
718
/* string begin " */
717
        case SB:
719
        case SB:
718
            parse_buffer_clear(jc);
720
            parse_buffer_clear(jc);
719
            assert(jc->type == JSON_T_NONE);
721
            assert(jc->type == JSON_T_NONE);
720
            jc->type = JSON_T_STRING;
722
            jc->type = JSON_T_STRING;
721
            jc->state = ST;
723
            jc->state = ST;
722
            break;        
724
            break;        
723
        
725
        
724
/* n */
726
/* n */
725
        case NU:
727
        case NU:
726
            assert(jc->type == JSON_T_NONE);
728
            assert(jc->type == JSON_T_NONE);
727
            jc->type = JSON_T_NULL;
729
            jc->type = JSON_T_NULL;
728
            jc->state = N1;
730
            jc->state = N1;
729
            break;        
731
            break;        
730
/* f */
732
/* f */
731
        case FA:
733
        case FA:
732
            assert(jc->type == JSON_T_NONE);
734
            assert(jc->type == JSON_T_NONE);
733
            jc->type = JSON_T_FALSE;
735
            jc->type = JSON_T_FALSE;
734
            jc->state = F1;
736
            jc->state = F1;
735
            break;        
737
            break;        
736
/* t */
738
/* t */
737
        case TR:
739
        case TR:
738
            assert(jc->type == JSON_T_NONE);
740
            assert(jc->type == JSON_T_NONE);
739
            jc->type = JSON_T_TRUE;
741
            jc->type = JSON_T_TRUE;
740
            jc->state = T1;
742
            jc->state = T1;
741
            break;        
743
            break;        
742
        
744
        
743
/* closing comment */
745
/* closing comment */
744
        case CE:
746
        case CE:
745
            jc->comment = 0;
747
            jc->comment = 0;
746
            assert(jc->parse_buffer_count == 0);
748
            assert(jc->parse_buffer_count == 0);
747
            assert(jc->type == JSON_T_NONE);
749
            assert(jc->type == JSON_T_NONE);
748
            jc->state = jc->before_comment_state;
750
            jc->state = jc->before_comment_state;
749
            break;        
751
            break;        
750
        
752
        
751
/* opening comment  */
753
/* opening comment  */
752
        case CB:
754
        case CB:
753
            if (!jc->allow_comments) {
755
            if (!jc->allow_comments) {
754
                return false;
756
                return false;
755
            }
757
            }
756
            parse_buffer_pop_back_char(jc);
758
            parse_buffer_pop_back_char(jc);
757
            if (!parse_parse_buffer(jc)) {
759
            if (!parse_parse_buffer(jc)) {
758
                return false;
760
                return false;
759
            }
761
            }
760
            assert(jc->parse_buffer_count == 0);
762
            assert(jc->parse_buffer_count == 0);
761
            assert(jc->type != JSON_T_STRING);
763
            assert(jc->type != JSON_T_STRING);
762
            switch (jc->stack[jc->top]) {
764
            switch (jc->stack[jc->top]) {
763
            case MODE_ARRAY:
765
            case MODE_ARRAY:
764
            case MODE_OBJECT:   
766
            case MODE_OBJECT:   
765
                switch(jc->state) {
767
                switch(jc->state) {
766
                case VA:
768
                case VA:
767
                case AR:
769
                case AR:
768
                    jc->before_comment_state = jc->state;
770
                    jc->before_comment_state = jc->state;
769
                    break;
771
                    break;
770
                default:
772
                default:
771
                    jc->before_comment_state = OK;
773
                    jc->before_comment_state = OK;
772
                    break;
774
                    break;
773
                }
775
                }
774
                break;
776
                break;
775
            default:
777
            default:
776
                jc->before_comment_state = jc->state;
778
                jc->before_comment_state = jc->state;
777
                break;
779
                break;
778
            }
780
            }
779
            jc->type = JSON_T_NONE;
781
            jc->type = JSON_T_NONE;
780
            jc->state = C1;
782
            jc->state = C1;
781
            jc->comment = 1;
783
            jc->comment = 1;
782
            break;
784
            break;
783
/* empty } */
785
/* empty } */
784
        case -9:        
786
        case -9:        
785
            parse_buffer_clear(jc);
787
            parse_buffer_clear(jc);
786
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
788
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
787
                return false;
789
                return false;
788
            }
790
            }
789
            if (!pop(jc, MODE_KEY)) {
791
            if (!pop(jc, MODE_KEY)) {
790
                return false;
792
                return false;
791
            }
793
            }
792
            jc->state = OK;
794
            jc->state = OK;
793
            break;
795
            break;
794
 
796
 
795
/* } */ case -8:
797
/* } */ case -8:
796
            parse_buffer_pop_back_char(jc);
798
            parse_buffer_pop_back_char(jc);
797
            if (!parse_parse_buffer(jc)) {
799
            if (!parse_parse_buffer(jc)) {
798
                return false;
800
                return false;
799
            }
801
            }
800
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
802
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
801
                return false;
803
                return false;
802
            }
804
            }
803
            if (!pop(jc, MODE_OBJECT)) {
805
            if (!pop(jc, MODE_OBJECT)) {
804
                return false;
806
                return false;
805
            }
807
            }
806
            jc->type = JSON_T_NONE;
808
            jc->type = JSON_T_NONE;
807
            jc->state = OK;
809
            jc->state = OK;
808
            break;
810
            break;
809
 
811
 
810
/* ] */ case -7:
812
/* ] */ case -7:
811
            parse_buffer_pop_back_char(jc);
813
            parse_buffer_pop_back_char(jc);
812
            if (!parse_parse_buffer(jc)) {
814
            if (!parse_parse_buffer(jc)) {
813
                return false;
815
                return false;
814
            }
816
            }
815
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_ARRAY_END, NULL)) {
817
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_ARRAY_END, NULL)) {
816
                return false;
818
                return false;
817
            }
819
            }
818
            if (!pop(jc, MODE_ARRAY)) {
820
            if (!pop(jc, MODE_ARRAY)) {
819
                return false;
821
                return false;
820
            }
822
            }
821
            
823
            
822
            jc->type = JSON_T_NONE;
824
            jc->type = JSON_T_NONE;
823
            jc->state = OK;
825
            jc->state = OK;
824
            break;
826
            break;
825
 
827
 
826
/* { */ case -6:
828
/* { */ case -6:
827
            parse_buffer_pop_back_char(jc);
829
            parse_buffer_pop_back_char(jc);
828
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_BEGIN, NULL)) {
830
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_BEGIN, NULL)) {
829
                return false;
831
                return false;
830
            }
832
            }
831
            if (!push(jc, MODE_KEY)) {
833
            if (!push(jc, MODE_KEY)) {
832
                return false;
834
                return false;
833
            }
835
            }
834
            assert(jc->type == JSON_T_NONE);
836
            assert(jc->type == JSON_T_NONE);
835
            jc->state = OB;
837
            jc->state = OB;
836
            break;
838
            break;
837
 
839
 
838
/* [ */ case -5:
840
/* [ */ case -5:
839
            parse_buffer_pop_back_char(jc);
841
            parse_buffer_pop_back_char(jc);
840
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_ARRAY_BEGIN, NULL)) {
842
            if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_ARRAY_BEGIN, NULL)) {
841
                return false;
843
                return false;
842
            }
844
            }
843
            if (!push(jc, MODE_ARRAY)) {
845
            if (!push(jc, MODE_ARRAY)) {
844
                return false;
846
                return false;
845
            }
847
            }
846
            assert(jc->type == JSON_T_NONE);
848
            assert(jc->type == JSON_T_NONE);
847
            jc->state = AR;
849
            jc->state = AR;
848
            break;
850
            break;
849
 
851
 
850
/* string end " */ case -4:
852
/* string end " */ case -4:
851
            parse_buffer_pop_back_char(jc);
853
            parse_buffer_pop_back_char(jc);
852
            switch (jc->stack[jc->top]) {
854
            switch (jc->stack[jc->top]) {
853
            case MODE_KEY:
855
            case MODE_KEY:
854
                assert(jc->type == JSON_T_STRING);
856
                assert(jc->type == JSON_T_STRING);
855
                jc->type = JSON_T_NONE;
857
                jc->type = JSON_T_NONE;
856
                jc->state = CO;
858
                jc->state = CO;
857
                
859
                
858
                if (jc->callback) {
860
                if (jc->callback) {
859
                    JSON_value value;
861
                    JSON_value value;
860
                    value.vu.str.value = jc->parse_buffer;
862
                    value.vu.str.value = jc->parse_buffer;
861
                    value.vu.str.length = jc->parse_buffer_count;
863
                    value.vu.str.length = jc->parse_buffer_count;
862
                    if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
864
                    if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
863
                        return false;
865
                        return false;
864
                    }
866
                    }
865
                }
867
                }
866
                parse_buffer_clear(jc);
868
                parse_buffer_clear(jc);
867
                break;
869
                break;
868
            case MODE_ARRAY:
870
            case MODE_ARRAY:
869
            case MODE_OBJECT:
871
            case MODE_OBJECT:
870
                assert(jc->type == JSON_T_STRING);
872
                assert(jc->type == JSON_T_STRING);
871
                if (!parse_parse_buffer(jc)) {
873
                if (!parse_parse_buffer(jc)) {
872
                    return false;
874
                    return false;
873
                }
875
                }
874
                jc->type = JSON_T_NONE;
876
                jc->type = JSON_T_NONE;
875
                jc->state = OK;
877
                jc->state = OK;
876
                break;
878
                break;
877
            default:
879
            default:
878
                return false;
880
                return false;
879
            }
881
            }
880
            break;
882
            break;
881
 
883
 
882
/* , */ case -3:
884
/* , */ case -3:
883
            parse_buffer_pop_back_char(jc);
885
            parse_buffer_pop_back_char(jc);
884
            if (!parse_parse_buffer(jc)) {
886
            if (!parse_parse_buffer(jc)) {
885
                return false;
887
                return false;
886
            }
888
            }
887
            switch (jc->stack[jc->top]) {
889
            switch (jc->stack[jc->top]) {
888
            case MODE_OBJECT:
890
            case MODE_OBJECT:
889
/*
891
/*
890
    A comma causes a flip from object mode to key mode.
892
    A comma causes a flip from object mode to key mode.
891
*/
893
*/
892
                if (!pop(jc, MODE_OBJECT) || !push(jc, MODE_KEY)) {
894
                if (!pop(jc, MODE_OBJECT) || !push(jc, MODE_KEY)) {
893
                    return false;
895
                    return false;
894
                }
896
                }
895
                assert(jc->type != JSON_T_STRING);
897
                assert(jc->type != JSON_T_STRING);
896
                jc->type = JSON_T_NONE;
898
                jc->type = JSON_T_NONE;
897
                jc->state = KE;
899
                jc->state = KE;
898
                break;
900
                break;
899
            case MODE_ARRAY:
901
            case MODE_ARRAY:
900
                assert(jc->type != JSON_T_STRING);
902
                assert(jc->type != JSON_T_STRING);
901
                jc->type = JSON_T_NONE;
903
                jc->type = JSON_T_NONE;
902
                jc->state = VA;
904
                jc->state = VA;
903
                break;
905
                break;
904
            default:
906
            default:
905
                return false;
907
                return false;
906
            }
908
            }
907
            break;
909
            break;
908
 
910
 
909
/* : */ case -2:
911
/* : */ case -2:
910
/*
912
/*
911
    A colon causes a flip from key mode to object mode.
913
    A colon causes a flip from key mode to object mode.
912
*/
914
*/
913
            parse_buffer_pop_back_char(jc);
915
            parse_buffer_pop_back_char(jc);
914
            if (!pop(jc, MODE_KEY) || !push(jc, MODE_OBJECT)) {
916
            if (!pop(jc, MODE_KEY) || !push(jc, MODE_OBJECT)) {
915
                return false;
917
                return false;
916
            }
918
            }
917
            assert(jc->type == JSON_T_NONE);
919
            assert(jc->type == JSON_T_NONE);
918
            jc->state = VA;
920
            jc->state = VA;
919
            break;
921
            break;
920
/*
922
/*
921
    Bad action.
923
    Bad action.
922
*/
924
*/
923
        default:
925
        default:
924
            return false;
926
            return false;
925
        }
927
        }
926
    }
928
    }
927
    return true;
929
    return true;
928
}
930
}
929
 
931
 
930
 
932
 
931
int
933
int
932
JSON_parser_done(JSON_parser jc)
934
JSON_parser_done(JSON_parser jc)
933
{
935
{
934
    const int result = jc->state == OK && pop(jc, MODE_DONE);
936
    const int result = jc->state == OK && pop(jc, MODE_DONE);
935
 
937
 
936
    return result;
938
    return result;
937
}
939
}
938
 
940
 
939
 
941
 
940
int JSON_parser_is_legal_white_space_string(const char* s)
942
int JSON_parser_is_legal_white_space_string(const char* s)
941
{
943
{
942
    int c, char_class;
944
    int c, char_class;
943
    
945
    
944
    if (s == NULL) {
946
    if (s == NULL) {
945
        return false;
947
        return false;
946
    }
948
    }
947
    
949
    
948
    for (; *s; ++s) {   
950
    for (; *s; ++s) {   
949
        c = *s;
951
        c = *s;
950
        
952
        
951
        if (c < 0 || c >= 128) {
953
        if (c < 0 || c >= 128) {
952
            return false;
954
            return false;
953
        }
955
        }
954
        
956
        
955
        char_class = ascii_class[c];
957
        char_class = ascii_class[c];
956
        
958
        
957
        if (char_class != C_SPACE && char_class != C_WHITE) {
959
        if (char_class != C_SPACE && char_class != C_WHITE) {
958
            return false;
960
            return false;
959
        }
961
        }
960
    }
962
    }
961
    
963
    
962
    return true;
964
    return true;
963
}
965
}
964
 
966
 
965
 
967
 
966
 
968
 
967
void init_JSON_config(JSON_config* config)
969
void init_JSON_config(JSON_config* config)
968
{
970
{
969
    if (config) {
971
    if (config) {
970
        memset(config, 0, sizeof(*config));
972
        memset(config, 0, sizeof(*config));
971
        
973
        
972
        config->depth = JSON_PARSER_STACK_SIZE - 1;
974
        config->depth = JSON_PARSER_STACK_SIZE - 1;
973
    }
975
    }
974
}
976
}