-
Notifications
You must be signed in to change notification settings - Fork 2
/
fastjson.c
393 lines (393 loc) · 10.1 KB
/
fastjson.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
/*
* Symisc unQLite: An Embeddable NoSQL (Post Modern) Database Engine.
* Copyright (C) 2012-2013, Symisc Systems http://unqlite.org/
* Version 1.1.6
* For information on licensing, redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES
* please contact Symisc Systems via:
* or visit:
* http://unqlite.org/licensing.html
*/
/* $SymiscID: fastjson.c v1.1 FreeBSD 2012-12-05 22:52 stable <[email protected]> $ */
#ifndef UNQLITE_AMALGAMATION
#include "unqliteInt.h"
#endif
/* JSON binary encoding, decoding and stuff like that */
#ifndef UNQLITE_FAST_JSON_NEST_LIMIT
#if defined(__WINNT__) || defined(__UNIXES__)
#define UNQLITE_FAST_JSON_NEST_LIMIT 64 /* Nesting limit */
#else
#define UNQLITE_FAST_JSON_NEST_LIMIT 32 /* Nesting limit */
#endif
#endif /* UNQLITE_FAST_JSON_NEST_LIMIT */
/*
* JSON to Binary using the FastJSON implementation (BigEndian).
*/
/*
* FastJSON implemented binary token.
*/
#define FJSON_DOC_START 1 /* { */
#define FJSON_DOC_END 2 /* } */
#define FJSON_ARRAY_START 3 /* [ */
#define FJSON_ARRAY_END 4 /* ] */
#define FJSON_COLON 5 /* : */
#define FJSON_COMMA 6 /* , */
#define FJSON_ID 7 /* ID + 4 Bytes length */
#define FJSON_STRING 8 /* String + 4 bytes length */
#define FJSON_BYTE 9 /* Byte */
#define FJSON_INT64 10 /* Integer 64 + 8 bytes */
#define FJSON_REAL 18 /* Floating point value + 2 bytes */
#define FJSON_NULL 23 /* NULL */
#define FJSON_TRUE 24 /* TRUE */
#define FJSON_FALSE 25 /* FALSE */
/*
* Encode a Jx9 value to binary JSON.
*/
UNQLITE_PRIVATE sxi32 FastJsonEncode(
jx9_value *pValue, /* Value to encode */
SyBlob *pOut, /* Store encoded value here */
int iNest /* Nesting limit */
)
{
sxi32 iType = pValue ? pValue->iFlags : MEMOBJ_NULL;
sxi32 rc = SXRET_OK;
int c;
if( iNest >= UNQLITE_FAST_JSON_NEST_LIMIT ){
/* Nesting limit reached */
return SXERR_LIMIT;
}
if( iType & (MEMOBJ_NULL|MEMOBJ_RES) ){
/*
* Resources are encoded as null also.
*/
c = FJSON_NULL;
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
}else if( iType & MEMOBJ_BOOL ){
c = pValue->x.iVal ? FJSON_TRUE : FJSON_FALSE;
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
}else if( iType & MEMOBJ_STRING ){
unsigned char zBuf[sizeof(sxu32)]; /* String length */
c = FJSON_STRING;
SyBigEndianPack32(zBuf,SyBlobLength(&pValue->sBlob));
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
if( rc == SXRET_OK ){
rc = SyBlobAppend(pOut,(const void *)zBuf,sizeof(zBuf));
if( rc == SXRET_OK ){
rc = SyBlobAppend(pOut,SyBlobData(&pValue->sBlob),SyBlobLength(&pValue->sBlob));
}
}
}else if( iType & MEMOBJ_INT ){
unsigned char zBuf[8];
/* 64bit big endian integer */
c = FJSON_INT64;
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
if( rc == SXRET_OK ){
SyBigEndianPack64(zBuf,(sxu64)pValue->x.iVal);
rc = SyBlobAppend(pOut,(const void *)zBuf,sizeof(zBuf));
}
}else if( iType & MEMOBJ_REAL ){
/* Real number */
c = FJSON_REAL;
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
if( rc == SXRET_OK ){
sxu32 iOfft = SyBlobLength(pOut);
rc = SyBlobAppendBig16(pOut,0);
if( rc == SXRET_OK ){
unsigned char *zBlob;
SyBlobFormat(pOut,"%.15g",pValue->x.rVal);
zBlob = (unsigned char *)SyBlobDataAt(pOut,iOfft);
SyBigEndianPack16(zBlob,(sxu16)(SyBlobLength(pOut) - ( 2 + iOfft)));
}
}
}else if( iType & MEMOBJ_HASHMAP ){
/* A JSON object or array */
jx9_hashmap *pMap = (jx9_hashmap *)pValue->x.pOther;
jx9_hashmap_node *pNode;
jx9_value *pEntry;
/* Reset the hashmap loop cursor */
jx9HashmapResetLoopCursor(pMap);
if( pMap->iFlags & HASHMAP_JSON_OBJECT ){
jx9_value sKey;
/* A JSON object */
c = FJSON_DOC_START; /* { */
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
if( rc == SXRET_OK ){
jx9MemObjInit(pMap->pVm,&sKey);
/* Encode object entries */
while((pNode = jx9HashmapGetNextEntry(pMap)) != 0 ){
/* Extract the key */
jx9HashmapExtractNodeKey(pNode,&sKey);
/* Encode it */
rc = FastJsonEncode(&sKey,pOut,iNest+1);
if( rc != SXRET_OK ){
break;
}
c = FJSON_COLON;
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
if( rc != SXRET_OK ){
break;
}
/* Extract the value */
pEntry = jx9HashmapGetNodeValue(pNode);
/* Encode it */
rc = FastJsonEncode(pEntry,pOut,iNest+1);
if( rc != SXRET_OK ){
break;
}
/* Delimit the entry */
c = FJSON_COMMA;
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
if( rc != SXRET_OK ){
break;
}
}
jx9MemObjRelease(&sKey);
if( rc == SXRET_OK ){
c = FJSON_DOC_END; /* } */
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
}
}
}else{
/* A JSON array */
c = FJSON_ARRAY_START; /* [ */
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
if( rc == SXRET_OK ){
/* Encode array entries */
while( (pNode = jx9HashmapGetNextEntry(pMap)) != 0 ){
/* Extract the value */
pEntry = jx9HashmapGetNodeValue(pNode);
/* Encode it */
rc = FastJsonEncode(pEntry,pOut,iNest+1);
if( rc != SXRET_OK ){
break;
}
/* Delimit the entry */
c = FJSON_COMMA;
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
if( rc != SXRET_OK ){
break;
}
}
if( rc == SXRET_OK ){
c = FJSON_ARRAY_END; /* ] */
rc = SyBlobAppend(pOut,(const void *)&c,sizeof(char));
}
}
}
}
return rc;
}
/*
* Decode a FastJSON binary blob.
*/
UNQLITE_PRIVATE sxi32 FastJsonDecode(
const void *pIn, /* Binary JSON */
sxu32 nByte, /* Chunk delimiter */
jx9_value *pOut, /* Decoded value */
const unsigned char **pzPtr,
int iNest /* Nesting limit */
)
{
const unsigned char *zIn = (const unsigned char *)pIn;
const unsigned char *zEnd = &zIn[nByte];
sxi32 rc = SXRET_OK;
int c;
if( iNest >= UNQLITE_FAST_JSON_NEST_LIMIT ){
/* Nesting limit reached */
return SXERR_LIMIT;
}
c = zIn[0];
/* Advance the stream cursor */
zIn++;
/* Process the binary token */
switch(c){
case FJSON_NULL:
/* null */
jx9_value_null(pOut);
break;
case FJSON_FALSE:
/* Boolean FALSE */
jx9_value_bool(pOut,0);
break;
case FJSON_TRUE:
/* Boolean TRUE */
jx9_value_bool(pOut,1);
break;
case FJSON_INT64: {
/* 64Bit integer */
sxu64 iVal;
/* Sanity check */
if( &zIn[8] >= zEnd ){
/* Corrupt chunk */
rc = SXERR_CORRUPT;
break;
}
SyBigEndianUnpack64(zIn,&iVal);
/* Advance the pointer */
zIn += 8;
jx9_value_int64(pOut,(jx9_int64)iVal);
break;
}
case FJSON_REAL: {
/* Real number */
double iVal = 0; /* cc warning */
sxu16 iLen;
/* Sanity check */
if( &zIn[2] >= zEnd ){
/* Corrupt chunk */
rc = SXERR_CORRUPT;
break;
}
SyBigEndianUnpack16(zIn,&iLen);
if( &zIn[iLen] >= zEnd ){
/* Corrupt chunk */
rc = SXERR_CORRUPT;
break;
}
zIn += 2;
SyStrToReal((const char *)zIn,(sxu32)iLen,&iVal,0);
/* Advance the pointer */
zIn += iLen;
jx9_value_double(pOut,iVal);
break;
}
case FJSON_STRING: {
/* UTF-8/Binary chunk */
sxu32 iLength;
/* Sanity check */
if( &zIn[4] >= zEnd ){
/* Corrupt chunk */
rc = SXERR_CORRUPT;
break;
}
SyBigEndianUnpack32(zIn,&iLength);
if( &zIn[iLength] >= zEnd ){
/* Corrupt chunk */
rc = SXERR_CORRUPT;
break;
}
zIn += 4;
/* Invalidate any prior representation */
if( pOut->iFlags & MEMOBJ_STRING ){
/* Reset the string cursor */
SyBlobReset(&pOut->sBlob);
}
rc = jx9MemObjStringAppend(pOut,(const char *)zIn,iLength);
/* Update pointer */
zIn += iLength;
break;
}
case FJSON_ARRAY_START: {
/* Binary JSON array */
jx9_hashmap *pMap;
jx9_value sVal;
/* Allocate a new hashmap */
pMap = (jx9_hashmap *)jx9NewHashmap(pOut->pVm,0,0);
if( pMap == 0 ){
rc = SXERR_MEM;
break;
}
jx9MemObjInit(pOut->pVm,&sVal);
jx9MemObjRelease(pOut);
MemObjSetType(pOut,MEMOBJ_HASHMAP);
pOut->x.pOther = pMap;
rc = SXRET_OK;
for(;;){
/* Jump leading binary commas */
while (zIn < zEnd && zIn[0] == FJSON_COMMA ){
zIn++;
}
if( zIn >= zEnd || zIn[0] == FJSON_ARRAY_END ){
if( zIn < zEnd ){
zIn++; /* Jump the trailing binary ] */
}
break;
}
/* Decode the value */
rc = FastJsonDecode((const void *)zIn,(sxu32)(zEnd-zIn),&sVal,&zIn,iNest+1);
if( rc != SXRET_OK ){
break;
}
/* Insert the decoded value */
rc = jx9HashmapInsert(pMap,0,&sVal);
if( rc != UNQLITE_OK ){
break;
}
}
if( rc != SXRET_OK ){
jx9MemObjRelease(pOut);
}
jx9MemObjRelease(&sVal);
break;
}
case FJSON_DOC_START: {
/* Binary JSON object */
jx9_value sVal,sKey;
jx9_hashmap *pMap;
/* Allocate a new hashmap */
pMap = (jx9_hashmap *)jx9NewHashmap(pOut->pVm,0,0);
if( pMap == 0 ){
rc = SXERR_MEM;
break;
}
jx9MemObjInit(pOut->pVm,&sVal);
jx9MemObjInit(pOut->pVm,&sKey);
jx9MemObjRelease(pOut);
MemObjSetType(pOut,MEMOBJ_HASHMAP);
pOut->x.pOther = pMap;
rc = SXRET_OK;
for(;;){
/* Jump leading binary commas */
while (zIn < zEnd && zIn[0] == FJSON_COMMA ){
zIn++;
}
if( zIn >= zEnd || zIn[0] == FJSON_DOC_END ){
if( zIn < zEnd ){
zIn++; /* Jump the trailing binary } */
}
break;
}
/* Extract the key */
rc = FastJsonDecode((const void *)zIn,(sxu32)(zEnd-zIn),&sKey,&zIn,iNest+1);
if( rc != UNQLITE_OK ){
break;
}
if( zIn >= zEnd || zIn[0] != FJSON_COLON ){
rc = UNQLITE_CORRUPT;
break;
}
zIn++; /* Jump the binary colon ':' */
if( zIn >= zEnd ){
rc = UNQLITE_CORRUPT;
break;
}
/* Decode the value */
rc = FastJsonDecode((const void *)zIn,(sxu32)(zEnd-zIn),&sVal,&zIn,iNest+1);
if( rc != SXRET_OK ){
break;
}
/* Insert the key and its associated value */
rc = jx9HashmapInsert(pMap,&sKey,&sVal);
if( rc != UNQLITE_OK ){
break;
}
}
if( rc != SXRET_OK ){
jx9MemObjRelease(pOut);
}
jx9MemObjRelease(&sVal);
jx9MemObjRelease(&sKey);
break;
}
default:
/* Corrupt data */
rc = SXERR_CORRUPT;
break;
}
if( pzPtr ){
*pzPtr = zIn;
}
return rc;
}