ultrajsondec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. Copyright (c) 2011, Jonas Tarnstrom and ESN Social Software AB
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. All advertising materials mentioning features or use of this software
  12. must display the following acknowledgement:
  13. This product includes software developed by ESN Social Software AB (www.esn.me).
  14. 4. Neither the name of the ESN Social Software AB nor the
  15. names of its contributors may be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY ESN SOCIAL SOFTWARE AB ''AS IS'' AND ANY
  18. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL ESN SOCIAL SOFTWARE AB BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. Portions of code from:
  28. MODP_ASCII - Ascii transformations (upper/lower, etc)
  29. http://code.google.com/p/stringencoders/
  30. Copyright (c) 2007 Nick Galbreath -- nickg [at] modp [dot] com. All rights reserved.
  31. */
  32. #include "ultrajson.h"
  33. #include <math.h>
  34. #include <assert.h>
  35. #include <string.h>
  36. #include <limits.h>
  37. #include <wchar.h>
  38. struct DecoderState
  39. {
  40. char *start;
  41. char *end;
  42. wchar_t *escStart;
  43. wchar_t *escEnd;
  44. int escHeap;
  45. int lastType;
  46. JSONObjectDecoder *dec;
  47. };
  48. JSOBJ FASTCALL_MSVC decode_any( struct DecoderState *ds) FASTCALL_ATTR;
  49. typedef JSOBJ (*PFN_DECODER)( struct DecoderState *ds);
  50. #define RETURN_JSOBJ_NULLCHECK(_expr) return(_expr);
  51. double createDouble(double intNeg, double intValue, double frcValue, int frcDecimalCount)
  52. {
  53. static const double g_pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
  54. return (intValue + (frcValue / g_pow10[frcDecimalCount])) * intNeg;
  55. }
  56. static JSOBJ SetError( struct DecoderState *ds, int offset, const char *message)
  57. {
  58. ds->dec->errorOffset = ds->start + offset;
  59. ds->dec->errorStr = (char *) message;
  60. return NULL;
  61. }
  62. FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_numeric ( struct DecoderState *ds)
  63. {
  64. #ifdef JSON_DECODE_NUMERIC_AS_DOUBLE
  65. double intNeg = 1;
  66. double intValue;
  67. #else
  68. int intNeg = 1;
  69. JSLONG intValue;
  70. #endif
  71. double expNeg;
  72. int chr;
  73. int decimalCount = 0;
  74. double frcValue = 0.0;
  75. double expValue;
  76. if (*(ds->start) == '-')
  77. {
  78. ds->start ++;
  79. intNeg = -1;
  80. }
  81. // Scan integer part
  82. intValue = 0;
  83. while (1)
  84. {
  85. chr = (int) (unsigned char) *(ds->start);
  86. switch (chr)
  87. {
  88. case '0':
  89. case '1':
  90. case '2':
  91. case '3':
  92. case '4':
  93. case '5':
  94. case '6':
  95. case '7':
  96. case '8':
  97. case '9':
  98. //FIXME: Check for arithemtic overflow here
  99. //PERF: Don't do 64-bit arithmetic here unless we know we have to
  100. #ifdef JSON_DECODE_NUMERIC_AS_DOUBLE
  101. intValue = intValue * 10.0 + (double) (chr - 48);
  102. #else
  103. intValue = intValue * 10LL + (JSLONG) (chr - 48);
  104. #endif
  105. ds->start ++;
  106. break;
  107. case '.':
  108. ds->start ++;
  109. goto DECODE_FRACTION;
  110. break;
  111. case 'e':
  112. case 'E':
  113. ds->start ++;
  114. goto DECODE_EXPONENT;
  115. break;
  116. default:
  117. goto BREAK_INT_LOOP;
  118. break;
  119. }
  120. }
  121. BREAK_INT_LOOP:
  122. ds->lastType = JT_INT;
  123. //If input string is LONGLONG_MIN here the value is already negative so we should not flip it
  124. #ifdef JSON_DECODE_NUMERIC_AS_DOUBLE
  125. #else
  126. if (intValue < 0)
  127. {
  128. intNeg = 1;
  129. }
  130. #endif
  131. //dbg1 = (intValue * intNeg);
  132. //dbg2 = (JSLONG) dbg1;
  133. #ifdef JSON_DECODE_NUMERIC_AS_DOUBLE
  134. if (intValue > (double) INT_MAX || intValue < (double) INT_MIN)
  135. #else
  136. if ( (intValue >> 32))
  137. #endif
  138. {
  139. RETURN_JSOBJ_NULLCHECK(ds->dec->newLong( (JSINT64) (intValue * (JSINT64) intNeg)));
  140. }
  141. else
  142. {
  143. RETURN_JSOBJ_NULLCHECK(ds->dec->newInt( (JSINT32) (intValue * intNeg)));
  144. }
  145. DECODE_FRACTION:
  146. // Scan fraction part
  147. frcValue = 0.0;
  148. while (1)
  149. {
  150. chr = (int) (unsigned char) *(ds->start);
  151. switch (chr)
  152. {
  153. case '0':
  154. case '1':
  155. case '2':
  156. case '3':
  157. case '4':
  158. case '5':
  159. case '6':
  160. case '7':
  161. case '8':
  162. case '9':
  163. if (decimalCount < JSON_DOUBLE_MAX_DECIMALS)
  164. {
  165. frcValue = frcValue * 10.0 + (double) (chr - 48);
  166. decimalCount ++;
  167. }
  168. ds->start ++;
  169. break;
  170. case 'e':
  171. case 'E':
  172. ds->start ++;
  173. goto DECODE_EXPONENT;
  174. break;
  175. default:
  176. goto BREAK_FRC_LOOP;
  177. }
  178. }
  179. BREAK_FRC_LOOP:
  180. if (intValue < 0)
  181. {
  182. intNeg = 1;
  183. }
  184. //FIXME: Check for arithemtic overflow here
  185. ds->lastType = JT_DOUBLE;
  186. RETURN_JSOBJ_NULLCHECK(ds->dec->newDouble (createDouble( (double) intNeg, (double) intValue, frcValue, decimalCount)));
  187. DECODE_EXPONENT:
  188. expNeg = 1.0;
  189. if (*(ds->start) == '-')
  190. {
  191. expNeg = -1.0;
  192. ds->start ++;
  193. }
  194. else
  195. if (*(ds->start) == '+')
  196. {
  197. expNeg = +1.0;
  198. ds->start ++;
  199. }
  200. expValue = 0.0;
  201. while (1)
  202. {
  203. chr = (int) (unsigned char) *(ds->start);
  204. switch (chr)
  205. {
  206. case '0':
  207. case '1':
  208. case '2':
  209. case '3':
  210. case '4':
  211. case '5':
  212. case '6':
  213. case '7':
  214. case '8':
  215. case '9':
  216. expValue = expValue * 10.0 + (double) (chr - 48);
  217. ds->start ++;
  218. break;
  219. default:
  220. goto BREAK_EXP_LOOP;
  221. }
  222. }
  223. BREAK_EXP_LOOP:
  224. #ifdef JSON_DECODE_NUMERIC_AS_DOUBLE
  225. #else
  226. if (intValue < 0)
  227. {
  228. intNeg = 1;
  229. }
  230. #endif
  231. //FIXME: Check for arithemtic overflow here
  232. ds->lastType = JT_DOUBLE;
  233. RETURN_JSOBJ_NULLCHECK(ds->dec->newDouble (createDouble( (double) intNeg, (double) intValue , frcValue, decimalCount) * pow(10.0, expValue * expNeg)));
  234. }
  235. FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_true ( struct DecoderState *ds)
  236. {
  237. ds->start ++;
  238. if (*(ds->start++) != 'r')
  239. goto SETERROR;
  240. if (*(ds->start++) != 'u')
  241. goto SETERROR;
  242. if (*(ds->start++) != 'e')
  243. goto SETERROR;
  244. ds->lastType = JT_TRUE;
  245. RETURN_JSOBJ_NULLCHECK(ds->dec->newTrue());
  246. SETERROR:
  247. return SetError(ds, -1, "Unexpected character found when decoding 'true'");
  248. }
  249. FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_false ( struct DecoderState *ds)
  250. {
  251. ds->start ++;
  252. if (*(ds->start++) != 'a')
  253. goto SETERROR;
  254. if (*(ds->start++) != 'l')
  255. goto SETERROR;
  256. if (*(ds->start++) != 's')
  257. goto SETERROR;
  258. if (*(ds->start++) != 'e')
  259. goto SETERROR;
  260. ds->lastType = JT_FALSE;
  261. RETURN_JSOBJ_NULLCHECK(ds->dec->newFalse());
  262. SETERROR:
  263. return SetError(ds, -1, "Unexpected character found when decoding 'false'");
  264. }
  265. FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_null ( struct DecoderState *ds)
  266. {
  267. ds->start ++;
  268. if (*(ds->start++) != 'u')
  269. goto SETERROR;
  270. if (*(ds->start++) != 'l')
  271. goto SETERROR;
  272. if (*(ds->start++) != 'l')
  273. goto SETERROR;
  274. ds->lastType = JT_NULL;
  275. RETURN_JSOBJ_NULLCHECK(ds->dec->newNull());
  276. SETERROR:
  277. return SetError(ds, -1, "Unexpected character found when decoding 'null'");
  278. }
  279. FASTCALL_ATTR void FASTCALL_MSVC SkipWhitespace(struct DecoderState *ds)
  280. {
  281. while (1)
  282. {
  283. switch (*ds->start)
  284. {
  285. case ' ':
  286. case '\t':
  287. case '\r':
  288. case '\n':
  289. ds->start ++;
  290. break;
  291. default:
  292. return;
  293. }
  294. }
  295. }
  296. enum DECODESTRINGSTATE
  297. {
  298. DS_ISNULL = 0x32,
  299. DS_ISQUOTE,
  300. DS_ISESCAPE,
  301. DS_UTFLENERROR,
  302. };
  303. static const JSUINT8 g_decoderLookup[256] =
  304. {
  305. /* 0x00 */ DS_ISNULL, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  306. /* 0x10 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  307. /* 0x20 */ 1, 1, DS_ISQUOTE, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  308. /* 0x30 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  309. /* 0x40 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  310. /* 0x50 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, DS_ISESCAPE, 1, 1, 1,
  311. /* 0x60 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  312. /* 0x70 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  313. /* 0x80 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  314. /* 0x90 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  315. /* 0xa0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  316. /* 0xb0 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  317. /* 0xc0 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  318. /* 0xd0 */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  319. /* 0xe0 */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  320. /* 0xf0 */ 4, 4, 4, 4, 4, 4, 4, 4, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR,
  321. };
  322. FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_string ( struct DecoderState *ds)
  323. {
  324. JSUTF16 sur[2] = { 0 };
  325. int iSur = 0;
  326. int index;
  327. wchar_t *escOffset;
  328. size_t escLen = (ds->escEnd - ds->escStart);
  329. JSUINT8 *inputOffset;
  330. JSUINT8 oct;
  331. JSUTF32 ucs;
  332. ds->lastType = JT_INVALID;
  333. ds->start ++;
  334. if ( (ds->end - ds->start) > escLen)
  335. {
  336. size_t newSize = (ds->end - ds->start);
  337. if (ds->escHeap)
  338. {
  339. ds->escStart = (wchar_t *) ds->dec->realloc (ds->escStart, newSize * sizeof(wchar_t));
  340. }
  341. else
  342. {
  343. wchar_t *oldStart = ds->escStart;
  344. ds->escHeap = 1;
  345. ds->escStart = (wchar_t *) ds->dec->malloc (newSize * sizeof(wchar_t));
  346. memcpy (ds->escStart, oldStart, escLen * sizeof(wchar_t));
  347. }
  348. ds->escEnd = ds->escStart + newSize;
  349. }
  350. escOffset = ds->escStart;
  351. //inputOffset = ds->start;
  352. inputOffset = (JSUINT8*)ds->start; // miloyip
  353. while(1)
  354. {
  355. switch (g_decoderLookup[(JSUINT8)(*inputOffset)])
  356. {
  357. case DS_ISNULL:
  358. return SetError(ds, -1, "Unmatched ''\"' when when decoding 'string'");
  359. case DS_ISQUOTE:
  360. ds->lastType = JT_UTF8;
  361. inputOffset ++;
  362. ds->start += ( (char *) inputOffset - (ds->start));
  363. RETURN_JSOBJ_NULLCHECK(ds->dec->newString(ds->escStart, escOffset));
  364. case DS_UTFLENERROR:
  365. return SetError (ds, -1, "Invalid UTF-8 sequence length when decoding 'string'");
  366. case DS_ISESCAPE:
  367. inputOffset ++;
  368. switch (*inputOffset)
  369. {
  370. case '\\': *(escOffset++) = L'\\'; inputOffset++; continue;
  371. case '\"': *(escOffset++) = L'\"'; inputOffset++; continue;
  372. case '/': *(escOffset++) = L'/'; inputOffset++; continue;
  373. case 'b': *(escOffset++) = L'\b'; inputOffset++; continue;
  374. case 'f': *(escOffset++) = L'\f'; inputOffset++; continue;
  375. case 'n': *(escOffset++) = L'\n'; inputOffset++; continue;
  376. case 'r': *(escOffset++) = L'\r'; inputOffset++; continue;
  377. case 't': *(escOffset++) = L'\t'; inputOffset++; continue;
  378. case 'u':
  379. {
  380. int index;
  381. inputOffset ++;
  382. for (index = 0; index < 4; index ++)
  383. {
  384. switch (*inputOffset)
  385. {
  386. case '\0': return SetError (ds, -1, "Unterminated unicode escape sequence when decoding 'string'");
  387. default: return SetError (ds, -1, "Unexpected character in unicode escape sequence when decoding 'string'");
  388. case '0':
  389. case '1':
  390. case '2':
  391. case '3':
  392. case '4':
  393. case '5':
  394. case '6':
  395. case '7':
  396. case '8':
  397. case '9':
  398. sur[iSur] = (sur[iSur] << 4) + (JSUTF16) (*inputOffset - '0');
  399. break;
  400. case 'a':
  401. case 'b':
  402. case 'c':
  403. case 'd':
  404. case 'e':
  405. case 'f':
  406. sur[iSur] = (sur[iSur] << 4) + 10 + (JSUTF16) (*inputOffset - 'a');
  407. break;
  408. case 'A':
  409. case 'B':
  410. case 'C':
  411. case 'D':
  412. case 'E':
  413. case 'F':
  414. sur[iSur] = (sur[iSur] << 4) + 10 + (JSUTF16) (*inputOffset - 'A');
  415. break;
  416. }
  417. inputOffset ++;
  418. }
  419. if (iSur == 0)
  420. {
  421. if((sur[iSur] & 0xfc00) == 0xd800)
  422. {
  423. // First of a surrogate pair, continue parsing
  424. iSur ++;
  425. break;
  426. }
  427. (*escOffset++) = (wchar_t) sur[iSur];
  428. iSur = 0;
  429. }
  430. else
  431. {
  432. // Decode pair
  433. if ((sur[1] & 0xfc00) != 0xdc00)
  434. {
  435. return SetError (ds, -1, "Unpaired high surrogate when decoding 'string'");
  436. }
  437. #if WCHAR_MAX == 0xffff
  438. (*escOffset++) = (wchar_t) sur[0];
  439. (*escOffset++) = (wchar_t) sur[1];
  440. #else
  441. (*escOffset++) = (wchar_t) 0x10000 + (((sur[0] - 0xd800) << 10) | (sur[1] - 0xdc00));
  442. #endif
  443. iSur = 0;
  444. }
  445. break;
  446. }
  447. case '\0': return SetError(ds, -1, "Unterminated escape sequence when decoding 'string'");
  448. default: return SetError(ds, -1, "Unrecognized escape sequence when decoding 'string'");
  449. }
  450. break;
  451. case 1:
  452. *(escOffset++) = (wchar_t) (*inputOffset++);
  453. break;
  454. case 2:
  455. {
  456. ucs = (*inputOffset++) & 0x1f;
  457. ucs <<= 6;
  458. if (((*inputOffset) & 0x80) != 0x80)
  459. {
  460. return SetError(ds, -1, "Invalid octet in UTF-8 sequence when decoding 'string'");
  461. }
  462. ucs |= (*inputOffset++) & 0x3f;
  463. if (ucs < 0x80) return SetError (ds, -1, "Overlong 2 byte UTF-8 sequence detected when decoding 'string'");
  464. *(escOffset++) = (wchar_t) ucs;
  465. break;
  466. }
  467. case 3:
  468. {
  469. JSUTF32 ucs = 0;
  470. ucs |= (*inputOffset++) & 0x0f;
  471. for (index = 0; index < 2; index ++)
  472. {
  473. ucs <<= 6;
  474. oct = (*inputOffset++);
  475. if ((oct & 0x80) != 0x80)
  476. {
  477. return SetError(ds, -1, "Invalid octet in UTF-8 sequence when decoding 'string'");
  478. }
  479. ucs |= oct & 0x3f;
  480. }
  481. if (ucs < 0x800) return SetError (ds, -1, "Overlong 3 byte UTF-8 sequence detected when encoding string");
  482. *(escOffset++) = (wchar_t) ucs;
  483. break;
  484. }
  485. case 4:
  486. {
  487. JSUTF32 ucs = 0;
  488. ucs |= (*inputOffset++) & 0x07;
  489. for (index = 0; index < 3; index ++)
  490. {
  491. ucs <<= 6;
  492. oct = (*inputOffset++);
  493. if ((oct & 0x80) != 0x80)
  494. {
  495. return SetError(ds, -1, "Invalid octet in UTF-8 sequence when decoding 'string'");
  496. }
  497. ucs |= oct & 0x3f;
  498. }
  499. if (ucs < 0x10000) return SetError (ds, -1, "Overlong 4 byte UTF-8 sequence detected when decoding 'string'");
  500. #if WCHAR_MAX == 0xffff
  501. if (ucs >= 0x10000)
  502. {
  503. ucs -= 0x10000;
  504. *(escOffset++) = (ucs >> 10) + 0xd800;
  505. *(escOffset++) = (ucs & 0x3ff) + 0xdc00;
  506. }
  507. else
  508. {
  509. *(escOffset++) = (wchar_t) ucs;
  510. }
  511. #else
  512. *(escOffset++) = (wchar_t) ucs;
  513. #endif
  514. break;
  515. }
  516. }
  517. }
  518. }
  519. FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_array( struct DecoderState *ds)
  520. {
  521. JSOBJ itemValue;
  522. JSOBJ newObj = ds->dec->newArray();
  523. ds->lastType = JT_INVALID;
  524. ds->start ++;
  525. while (1)//(*ds->start) != '\0')
  526. {
  527. SkipWhitespace(ds);
  528. if ((*ds->start) == ']')
  529. {
  530. *ds->start ++;
  531. return newObj;
  532. }
  533. itemValue = decode_any(ds);
  534. if (itemValue == NULL)
  535. {
  536. ds->dec->releaseObject(newObj);
  537. return NULL;
  538. }
  539. ds->dec->arrayAddItem (newObj, itemValue);
  540. SkipWhitespace(ds);
  541. switch (*(ds->start++))
  542. {
  543. case ']':
  544. return newObj;
  545. case ',':
  546. break;
  547. default:
  548. ds->dec->releaseObject(newObj);
  549. return SetError(ds, -1, "Unexpected character in found when decoding array value");
  550. }
  551. }
  552. ds->dec->releaseObject(newObj);
  553. return SetError(ds, -1, "Unmatched ']' when decoding 'array'");
  554. }
  555. FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_object( struct DecoderState *ds)
  556. {
  557. JSOBJ itemName;
  558. JSOBJ itemValue;
  559. JSOBJ newObj = ds->dec->newObject();
  560. ds->start ++;
  561. while (1)
  562. {
  563. SkipWhitespace(ds);
  564. if ((*ds->start) == '}')
  565. {
  566. ds->start ++;
  567. return newObj;
  568. }
  569. ds->lastType = JT_INVALID;
  570. itemName = decode_any(ds);
  571. if (itemName == NULL)
  572. {
  573. ds->dec->releaseObject(newObj);
  574. return NULL;
  575. }
  576. if (ds->lastType != JT_UTF8)
  577. {
  578. ds->dec->releaseObject(newObj);
  579. ds->dec->releaseObject(itemName);
  580. return SetError(ds, -1, "Key name of object must be 'string' when decoding 'object'");
  581. }
  582. SkipWhitespace(ds);
  583. if (*(ds->start++) != ':')
  584. {
  585. ds->dec->releaseObject(newObj);
  586. ds->dec->releaseObject(itemName);
  587. return SetError(ds, -1, "No ':' found when decoding object value");
  588. }
  589. SkipWhitespace(ds);
  590. itemValue = decode_any(ds);
  591. if (itemValue == NULL)
  592. {
  593. ds->dec->releaseObject(newObj);
  594. ds->dec->releaseObject(itemName);
  595. return NULL;
  596. }
  597. ds->dec->objectAddKey (newObj, itemName, itemValue);
  598. SkipWhitespace(ds);
  599. switch (*(ds->start++))
  600. {
  601. case '}':
  602. return newObj;
  603. case ',':
  604. break;
  605. default:
  606. ds->dec->releaseObject(newObj);
  607. return SetError(ds, -1, "Unexpected character in found when decoding object value");
  608. }
  609. }
  610. ds->dec->releaseObject(newObj);
  611. return SetError(ds, -1, "Unmatched '}' when decoding object");
  612. }
  613. FASTCALL_ATTR JSOBJ FASTCALL_MSVC decode_any(struct DecoderState *ds)
  614. {
  615. while (1)
  616. {
  617. switch (*ds->start)
  618. {
  619. case '\"':
  620. return decode_string (ds);
  621. case '0':
  622. case '1':
  623. case '2':
  624. case '3':
  625. case '4':
  626. case '5':
  627. case '6':
  628. case '7':
  629. case '8':
  630. case '9':
  631. case '-':
  632. return decode_numeric (ds);
  633. case '[': return decode_array (ds);
  634. case '{': return decode_object (ds);
  635. case 't': return decode_true (ds);
  636. case 'f': return decode_false (ds);
  637. case 'n': return decode_null (ds);
  638. case ' ':
  639. case '\t':
  640. case '\r':
  641. case '\n':
  642. // White space
  643. ds->start ++;
  644. break;
  645. default:
  646. return SetError(ds, -1, "Expected object or value");
  647. }
  648. }
  649. }
  650. JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuffer)
  651. {
  652. /*
  653. FIXME: Base the size of escBuffer of that of cbBuffer so that the unicode escaping doesn't run into the wall each time */
  654. struct DecoderState ds;
  655. wchar_t escBuffer[(JSON_MAX_STACK_BUFFER_SIZE / sizeof(wchar_t))];
  656. JSOBJ ret;
  657. ds.start = (char *) buffer;
  658. ds.end = ds.start + cbBuffer;
  659. ds.escStart = escBuffer;
  660. ds.escEnd = ds.escStart + (JSON_MAX_STACK_BUFFER_SIZE / sizeof(wchar_t));
  661. ds.escHeap = 0;
  662. ds.dec = dec;
  663. ds.dec->errorStr = NULL;
  664. ds.dec->errorOffset = NULL;
  665. ds.dec = dec;
  666. ret = decode_any (&ds);
  667. if (ds.escHeap)
  668. {
  669. dec->free(ds.escStart);
  670. }
  671. return ret;
  672. }