-
Notifications
You must be signed in to change notification settings - Fork 1
/
SmallJSONParser.c
482 lines (403 loc) · 11.3 KB
/
SmallJSONParser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "SmallJSONParser.h"
#include <string.h>
void InitialiseJSONParser(JSONParser *self)
{
memset(self,0,sizeof(JSONParser));
self->partialtokentype=OutOfDataJSONToken;
}
void ProvideJSONInput(JSONParser *self,const void *bytes,size_t length)
{
self->currentbyte=bytes;
self->end=self->currentbyte+length;
}
#define BaseState 0
#define BareSymbolState 1
#define StringState 2
#define StringEscapeState 3
#define ParseError() do { token.typeandflags=ParseErrorJSONToken; token.end=self->currentbyte; return token; } while(0)
JSONToken NextJSONToken(JSONParser *self)
{
JSONToken token={
.typeandflags=self->partialtokentype,
.start=self->currentbyte,
};
if(self->currentbyte==self->end) token.typeandflags=OutOfDataJSONToken;
self->partialtokentype=OutOfDataJSONToken;
while(self->currentbyte<self->end)
{
uint8_t c=*self->currentbyte++;
switch(self->state)
{
case BaseState:
switch(c)
{
case '\t': case ' ': case '\r': case '\n': // Skip white space.
break;
case '"':
token.typeandflags=StringJSONToken;
token.start=self->currentbyte;
self->state=StringState;
break;
case ':': case ',': // Ignore : and ,
break;
case '[':
token.typeandflags=StartArrayJSONToken;
token.start=self->currentbyte-1;
token.end=self->currentbyte;
return token;
case ']':
token.typeandflags=EndArrayJSONToken;
token.start=self->currentbyte-1;
token.end=self->currentbyte;
return token;
case '{':
token.typeandflags=StartObjectJSONToken;
token.start=self->currentbyte-1;
token.end=self->currentbyte;
return token;
case '}':
token.typeandflags=EndObjectJSONToken;
token.start=self->currentbyte-1;
token.end=self->currentbyte;
return token;
case '-':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
token.typeandflags=NumberJSONToken;
token.start=self->currentbyte-1;
self->state=BareSymbolState;
break;
case 't':
token.typeandflags=TrueJSONToken;
token.start=self->currentbyte-1;
self->state=BareSymbolState;
break;
case 'f':
token.typeandflags=FalseJSONToken;
token.start=self->currentbyte-1;
self->state=BareSymbolState;
break;
case 'n':
token.typeandflags=NullJSONToken;
token.start=self->currentbyte-1;
self->state=BareSymbolState;
break;
default:
ParseError();
}
break;
case BareSymbolState:
switch(c)
{
case '\t': case ' ': case '\r': case '\n': case ',': case ':':
token.end=self->currentbyte-1;
self->state=BaseState;
return token;
case ']': case '}':
token.end=self->currentbyte-1;
self->state=BaseState;
self->currentbyte--;
return token;
default:
if(c<=31 || c>=127) ParseError();
break;
}
break;
case StringState:
switch(c)
{
case '\\':
self->state=StringEscapeState;
break;
case '"':
token.end=self->currentbyte-1;
self->state=BaseState;
return token;
default:
if(c<=31 || (c>=127 && c<=191)) ParseError();
break;
}
break;
case StringEscapeState:
switch(c)
{
case '"': case '\\': case '/': case 'b':
case 'f': case 'n': case 'r': case 't': case 'u':
self->state=StringState;
break;
default: ParseError();
}
break;
}
}
self->partialtokentype=token.typeandflags;
token.typeandflags|=PartialJSONTokenFlag;
token.end=self->currentbyte;
return token;
}
JSONToken NextJSONTokenWithProvider(JSONParser *self,JSONProvider *provider)
{
size_t bufferposition=0;
int flags=0;
JSONToken token;
for(;;)
{
// Get the next possibly partial token.
token=NextJSONToken(self);
// If we get a complete token and have not yet copied any data,
// we can just return the token without copying and exit early.
if(!IsJSONTokenPartial(token) && bufferposition==0) return token;
if(JSONTokenType(token)!=OutOfDataJSONToken)
{
// Otherwise, calculate how much more data we can fit into the
// buffer, and set the truncated flag if we can't fit all of it.
// Use the buffer size -1, so that a terminating zero byte can
// be added later by the unescaping function.
size_t bytestocopy=token.end-token.start;
if(bufferposition+bytestocopy>provider->buffersize-1)
{
bytestocopy=provider->buffersize-1-bufferposition;
flags|=TruncatedJSONTokenFlag;
}
// Copy this fragment into the buffer.
memcpy(&provider->buffer[bufferposition],token.start,bytestocopy);
bufferposition+=bytestocopy;
// If this was the last fragment, exit the loop and return the
// data we have collected.
if(!IsJSONTokenPartial(token)) break;
}
// Otherwise, try to fetch more data through the callback. If
// it signals no more data is available, set the partial flag,
// exit the loop and return as much data as we managed to collect.
if(!provider->callback(self,provider->context))
{
flags|=PartialJSONTokenFlag;
break;
}
}
return (JSONToken){
.typeandflags=JSONTokenType(token)|flags,
.start=&provider->buffer[0],
.end=&provider->buffer[bufferposition],
};
}
static int HexDigit(uint8_t c)
{
if(c>='0' && c<='9') return c-'0';
else if(c>='a' && c<='f') return c-'a'+10;
else if(c>='A' && c<='F') return c-'A'+10;
else return -1;
}
bool UnescapeJSONStringToken(JSONToken token,char *unescapedbuffer,char **end)
{
const uint8_t *src=token.start;
char *dest=unescapedbuffer;
while(src<token.end)
{
uint8_t c1=*src++;
if(c1=='\\')
{
if(src==token.end) return false;
uint8_t c2=*src++;
switch(c2)
{
case '"': case '\\': case '/': *dest++=c2; break;
case 'b': *dest++='\b'; break;
case 'f': *dest++='\f'; break;
case 'n': *dest++='\n'; break;
case 'r': *dest++='\r'; break;
case 't': *dest++='\t'; break;
case 'u':
if(src+4>token.end) return false;
int h0=HexDigit(*src++);
int h1=HexDigit(*src++);
int h2=HexDigit(*src++);
int h3=HexDigit(*src++);
if(h0<0 || h1<0 || h2<0 || h3<0) return false;
int code=(h0<<12)|(h1<<8)|(h2<<4)|h3;
// If we encounter a high surrogate, look for a pair.
if(code>=0xd800 && code<=0xdbff)
{
// Check if there is enough space for another unicode
// escape, and that it starts with \u...
if(src+6<=token.end && src[0]=='\\' && src[1]=='u')
{
int h0=HexDigit(src[2]);
int h1=HexDigit(src[3]);
int h2=HexDigit(src[4]);
int h3=HexDigit(src[5]);
// And is followed by four valid hex digits...
if(h0>=0 && h1>=0 && h2>=0 && h3>=0)
{
// Specifying a low surrogate.
int code2=(h0<<12)|(h1<<8)|(h2<<4)|h3;
if(code2>=0xdc00 && code2<=0xdfff)
{
// If so, adjust the code accordingly.
code=((code&0x3ff)<<10)+(code2&0x3ff)+0x10000;
// And skip the escape code.
src+=6;
}
}
}
}
if(code<128)
{
*dest++=code;
}
else if(code<2048)
{
*dest++=0xc0|(code>>6);
*dest++=0x80|(code&0x3f);
}
else if(code<65536)
{
*dest++=0xe0|(code>>12);
*dest++=0x80|((code>>6)&0x3f);
*dest++=0x80|(code&0x3f);
}
else
{
*dest++=0xf0|(code>>18);
*dest++=0x80|((code>>12)&0x3f);
*dest++=0x80|((code>>6)&0x3f);
*dest++=0x80|(code&0x3f);
}
break;
}
}
else
{
*dest++=c1;
}
}
if(end) *end=dest;
*dest=0;
return true;
}
bool UnescapeJSONStringTokenInPlace(JSONToken *token)
{
return UnescapeJSONStringToken(*token,(char *)token->start,(char **)&token->end);
}
bool FastIsJSONStringEqual(JSONToken token,const char *string)
{
return FastIsJSONStringEqualWithLength(token,string,strlen(string));
}
bool FastIsJSONStringEqualWithLength(JSONToken token,const char *string,size_t length)
{
size_t testlength=token.end-token.start;
if(testlength!=length) return false;
return memcmp(token.start,string,testlength)==0;
}
bool ParseNumberTokenAsInteger(JSONToken token,int *result)
{
int value=0;
bool negative=false;
const uint8_t *ptr=token.start;
if(ptr==token.end) return false;
if(*ptr=='-')
{
negative=true;
ptr++;
if(ptr==token.end) return false;
}
while(ptr<token.end)
{
uint8_t c=*ptr++;
if(c<'0' || c>'9') return false;
value=value*10+c-'0';
}
if(negative) *result=-value;
else *result=value;
return true;
}
bool ParseNumberTokenAsFloat(JSONToken token,float *result)
{
size_t length=token.end-token.start;
char buffer[length+1];
memcpy(buffer,token.start,length);
buffer[length]=0;
char *end;
*result=strtof(buffer,&end);
return end!=(const char *)token.start;
}
bool ParseNumberTokenAsDouble(JSONToken token,double *result)
{
size_t length=token.end-token.start;
char buffer[length+1];
memcpy(buffer,token.start,length);
buffer[length]=0;
char *end;
*result=strtod(buffer,&end);
return end!=(const char *)token.start;
}
static bool ParseBraces(JSONParser *self,JSONProvider *provider,int level);
bool ExpectJSONTokenOfTypeWithProvider(JSONParser *self,JSONProvider *provider,int expectedtype,JSONToken *token)
{
if(provider) *token=NextJSONTokenWithProvider(self,provider);
else *token=NextJSONToken(self);
return JSONTokenType(*token)==expectedtype;
}
bool SkipJSONValueWithProvider(JSONParser *self,JSONProvider *provider)
{
return ParseBraces(self,provider,0);
}
bool ExpectAndSkipJSONValueOfTypeWithProvider(JSONParser *self,JSONProvider *provider,int expectedtype)
{
JSONToken token;
if(provider) token=NextJSONTokenWithProvider(self,provider);
else token=NextJSONToken(self);
int type=JSONTokenType(token);
if(type!=expectedtype) return false;
else if(type==StartObjectJSONToken || type==StartArrayJSONToken) return ParseBraces(self,provider,1);
else return true;
}
bool SkipUntilEndOfJSONObjectWithProvider(JSONParser *self,JSONProvider *provider)
{
return ParseBraces(self,provider,1);
}
bool SkipUntilEndOfJSONArrayWithProvider(JSONParser *self,JSONProvider *provider)
{
return ParseBraces(self,provider,1);
}
bool SkipUntilJSONObjectKeyWithProvider(JSONParser *self,JSONProvider *provider,const char *key)
{
size_t keylength=strlen(key);
for(;;)
{
JSONToken token;
if(!ExpectJSONTokenOfTypeWithProvider(self,provider,StringJSONToken,&token)) return false;
if(FastIsJSONStringEqualWithLength(token,key,keylength)) return true;
if(!SkipJSONValueWithProvider(self,provider)) return false;
}
}
bool ExpectAndSkipUntilJSONObjectKeyWithProvider(JSONParser *self,JSONProvider *provider,const char *key)
{
JSONToken token;
if(!ExpectJSONTokenOfTypeWithProvider(self,provider,StartObjectJSONToken,&token)) return false;
return SkipUntilJSONObjectKeyWithProvider(self,provider,key);
}
static bool ParseBraces(JSONParser *self,JSONProvider *provider,int level)
{
for(;;)
{
JSONToken token;
if(provider) token=NextJSONTokenWithProvider(self,provider);
else token=NextJSONToken(self);
int type=JSONTokenType(token);
if(type==StartObjectJSONToken || type==StartArrayJSONToken)
{
level++;
}
else if(type==EndObjectJSONToken || type==EndArrayJSONToken)
{
level--;
if(level<0) return false;
}
else if(type==OutOfDataJSONToken || type==ParseErrorJSONToken)
{
return false;
}
if(level==0) return true;
}
}