json.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /* vim: set et ts=3 sw=3 sts=3 ft=c:
  2. *
  3. * Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
  4. * https://github.com/udp/json-parser
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "json.h"
  30. #ifdef _MSC_VER
  31. #ifndef _CRT_SECURE_NO_WARNINGS
  32. #define _CRT_SECURE_NO_WARNINGS
  33. #endif
  34. #endif
  35. const struct _json_value json_value_none;
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <ctype.h>
  39. #include <math.h>
  40. typedef unsigned int json_uchar;
  41. static unsigned char hex_value (json_char c)
  42. {
  43. if (isdigit(c))
  44. return c - '0';
  45. switch (c) {
  46. case 'a': case 'A': return 0x0A;
  47. case 'b': case 'B': return 0x0B;
  48. case 'c': case 'C': return 0x0C;
  49. case 'd': case 'D': return 0x0D;
  50. case 'e': case 'E': return 0x0E;
  51. case 'f': case 'F': return 0x0F;
  52. default: return 0xFF;
  53. }
  54. }
  55. typedef struct
  56. {
  57. unsigned long used_memory;
  58. unsigned int uint_max;
  59. unsigned long ulong_max;
  60. json_settings settings;
  61. int first_pass;
  62. const json_char * ptr;
  63. unsigned int cur_line, cur_col;
  64. } json_state;
  65. static void * default_alloc (size_t size, int zero, void * user_data)
  66. {
  67. return zero ? calloc (1, size) : malloc (size);
  68. }
  69. static void default_free (void * ptr, void * user_data)
  70. {
  71. free (ptr);
  72. }
  73. static void * json_alloc (json_state * state, unsigned long size, int zero)
  74. {
  75. if ((state->ulong_max - state->used_memory) < size)
  76. return 0;
  77. if (state->settings.max_memory
  78. && (state->used_memory += size) > state->settings.max_memory)
  79. {
  80. return 0;
  81. }
  82. return state->settings.mem_alloc (size, zero, state->settings.user_data);
  83. }
  84. static int new_value (json_state * state,
  85. json_value ** top, json_value ** root, json_value ** alloc,
  86. json_type type)
  87. {
  88. json_value * value;
  89. int values_size;
  90. if (!state->first_pass)
  91. {
  92. value = *top = *alloc;
  93. *alloc = (*alloc)->_reserved.next_alloc;
  94. if (!*root)
  95. *root = value;
  96. switch (value->type)
  97. {
  98. case json_array:
  99. if (value->u.array.length == 0)
  100. break;
  101. if (! (value->u.array.values = (json_value **) json_alloc
  102. (state, value->u.array.length * sizeof (json_value *), 0)) )
  103. {
  104. return 0;
  105. }
  106. value->u.array.length = 0;
  107. break;
  108. case json_object:
  109. if (value->u.object.length == 0)
  110. break;
  111. values_size = sizeof (*value->u.object.values) * value->u.object.length;
  112. if (! (value->u.object.values = (json_object_entry *) json_alloc
  113. (state, values_size + ((unsigned long) value->u.object.values), 0)) )
  114. {
  115. return 0;
  116. }
  117. value->_reserved.object_mem = (*(char **) &value->u.object.values) + values_size;
  118. value->u.object.length = 0;
  119. break;
  120. case json_string:
  121. if (! (value->u.string.ptr = (json_char *) json_alloc
  122. (state, (value->u.string.length + 1) * sizeof (json_char), 0)) )
  123. {
  124. return 0;
  125. }
  126. value->u.string.length = 0;
  127. break;
  128. default:
  129. break;
  130. };
  131. return 1;
  132. }
  133. if (! (value = (json_value *) json_alloc
  134. (state, sizeof (json_value) + state->settings.value_extra, 1)))
  135. {
  136. return 0;
  137. }
  138. if (!*root)
  139. *root = value;
  140. value->type = type;
  141. value->parent = *top;
  142. #ifdef JSON_TRACK_SOURCE
  143. value->line = state->cur_line;
  144. value->col = state->cur_col;
  145. #endif
  146. if (*alloc)
  147. (*alloc)->_reserved.next_alloc = value;
  148. *alloc = *top = value;
  149. return 1;
  150. }
  151. #define whitespace \
  152. case '\n': ++ state.cur_line; state.cur_col = 0; \
  153. case ' ': case '\t': case '\r'
  154. #define string_add(b) \
  155. do { if (!state.first_pass) string [string_length] = b; ++ string_length; } while (0);
  156. #define line_and_col \
  157. state.cur_line, state.cur_col
  158. static const long
  159. flag_next = 1 << 0,
  160. flag_reproc = 1 << 1,
  161. flag_need_comma = 1 << 2,
  162. flag_seek_value = 1 << 3,
  163. flag_escaped = 1 << 4,
  164. flag_string = 1 << 5,
  165. flag_need_colon = 1 << 6,
  166. flag_done = 1 << 7,
  167. flag_num_negative = 1 << 8,
  168. flag_num_zero = 1 << 9,
  169. flag_num_e = 1 << 10,
  170. flag_num_e_got_sign = 1 << 11,
  171. flag_num_e_negative = 1 << 12,
  172. flag_line_comment = 1 << 13,
  173. flag_block_comment = 1 << 14;
  174. json_value * json_parse_ex (json_settings * settings,
  175. const json_char * json,
  176. size_t length,
  177. char * error_buf)
  178. {
  179. json_char error [json_error_max];
  180. const json_char * end;
  181. json_value * top, * root, * alloc = 0;
  182. json_state state = { 0 };
  183. long flags;
  184. long num_digits = 0, num_e = 0;
  185. json_int_t num_fraction = 0;
  186. /* Skip UTF-8 BOM
  187. */
  188. if (length >= 3 && ((unsigned char) json [0]) == 0xEF
  189. && ((unsigned char) json [1]) == 0xBB
  190. && ((unsigned char) json [2]) == 0xBF)
  191. {
  192. json += 3;
  193. length -= 3;
  194. }
  195. error[0] = '\0';
  196. end = (json + length);
  197. memcpy (&state.settings, settings, sizeof (json_settings));
  198. if (!state.settings.mem_alloc)
  199. state.settings.mem_alloc = default_alloc;
  200. if (!state.settings.mem_free)
  201. state.settings.mem_free = default_free;
  202. memset (&state.uint_max, 0xFF, sizeof (state.uint_max));
  203. memset (&state.ulong_max, 0xFF, sizeof (state.ulong_max));
  204. state.uint_max -= 8; /* limit of how much can be added before next check */
  205. state.ulong_max -= 8;
  206. for (state.first_pass = 1; state.first_pass >= 0; -- state.first_pass)
  207. {
  208. json_uchar uchar;
  209. unsigned char uc_b1, uc_b2, uc_b3, uc_b4;
  210. json_char * string = 0;
  211. unsigned int string_length = 0;
  212. top = root = 0;
  213. flags = flag_seek_value;
  214. state.cur_line = 1;
  215. for (state.ptr = json ;; ++ state.ptr)
  216. {
  217. json_char b = (state.ptr == end ? 0 : *state.ptr);
  218. if (flags & flag_string)
  219. {
  220. if (!b)
  221. { sprintf (error, "Unexpected EOF in string (at %d:%d)", line_and_col);
  222. goto e_failed;
  223. }
  224. if (string_length > state.uint_max)
  225. goto e_overflow;
  226. if (flags & flag_escaped)
  227. {
  228. flags &= ~ flag_escaped;
  229. switch (b)
  230. {
  231. case 'b': string_add ('\b'); break;
  232. case 'f': string_add ('\f'); break;
  233. case 'n': string_add ('\n'); break;
  234. case 'r': string_add ('\r'); break;
  235. case 't': string_add ('\t'); break;
  236. case 'u':
  237. if (end - state.ptr < 4 ||
  238. (uc_b1 = hex_value (*++ state.ptr)) == 0xFF ||
  239. (uc_b2 = hex_value (*++ state.ptr)) == 0xFF ||
  240. (uc_b3 = hex_value (*++ state.ptr)) == 0xFF ||
  241. (uc_b4 = hex_value (*++ state.ptr)) == 0xFF)
  242. {
  243. sprintf (error, "Invalid character value `%c` (at %d:%d)", b, line_and_col);
  244. goto e_failed;
  245. }
  246. uc_b1 = (uc_b1 << 4) | uc_b2;
  247. uc_b2 = (uc_b3 << 4) | uc_b4;
  248. uchar = (uc_b1 << 8) | uc_b2;
  249. if ((uchar & 0xF800) == 0xD800) {
  250. json_uchar uchar2;
  251. if (end - state.ptr < 6 || (*++ state.ptr) != '\\' || (*++ state.ptr) != 'u' ||
  252. (uc_b1 = hex_value (*++ state.ptr)) == 0xFF ||
  253. (uc_b2 = hex_value (*++ state.ptr)) == 0xFF ||
  254. (uc_b3 = hex_value (*++ state.ptr)) == 0xFF ||
  255. (uc_b4 = hex_value (*++ state.ptr)) == 0xFF)
  256. {
  257. sprintf (error, "Invalid character value `%c` (at %d:%d)", b, line_and_col);
  258. goto e_failed;
  259. }
  260. uc_b1 = (uc_b1 << 4) | uc_b2;
  261. uc_b2 = (uc_b3 << 4) | uc_b4;
  262. uchar2 = (uc_b1 << 8) | uc_b2;
  263. uchar = 0x010000 | ((uchar & 0x3FF) << 10) | (uchar2 & 0x3FF);
  264. }
  265. if (sizeof (json_char) >= sizeof (json_uchar) || (uchar <= 0x7F))
  266. {
  267. string_add ((json_char) uchar);
  268. break;
  269. }
  270. if (uchar <= 0x7FF)
  271. {
  272. if (state.first_pass)
  273. string_length += 2;
  274. else
  275. { string [string_length ++] = 0xC0 | (uchar >> 6);
  276. string [string_length ++] = 0x80 | (uchar & 0x3F);
  277. }
  278. break;
  279. }
  280. if (uchar <= 0xFFFF) {
  281. if (state.first_pass)
  282. string_length += 3;
  283. else
  284. { string [string_length ++] = 0xE0 | (uchar >> 12);
  285. string [string_length ++] = 0x80 | ((uchar >> 6) & 0x3F);
  286. string [string_length ++] = 0x80 | (uchar & 0x3F);
  287. }
  288. break;
  289. }
  290. if (state.first_pass)
  291. string_length += 4;
  292. else
  293. { string [string_length ++] = 0xF0 | (uchar >> 18);
  294. string [string_length ++] = 0x80 | ((uchar >> 12) & 0x3F);
  295. string [string_length ++] = 0x80 | ((uchar >> 6) & 0x3F);
  296. string [string_length ++] = 0x80 | (uchar & 0x3F);
  297. }
  298. break;
  299. default:
  300. string_add (b);
  301. };
  302. continue;
  303. }
  304. if (b == '\\')
  305. {
  306. flags |= flag_escaped;
  307. continue;
  308. }
  309. if (b == '"')
  310. {
  311. if (!state.first_pass)
  312. string [string_length] = 0;
  313. flags &= ~ flag_string;
  314. string = 0;
  315. switch (top->type)
  316. {
  317. case json_string:
  318. top->u.string.length = string_length;
  319. flags |= flag_next;
  320. break;
  321. case json_object:
  322. if (state.first_pass)
  323. (*(json_char **) &top->u.object.values) += string_length + 1;
  324. else
  325. {
  326. top->u.object.values [top->u.object.length].name
  327. = (json_char *) top->_reserved.object_mem;
  328. top->u.object.values [top->u.object.length].name_length
  329. = string_length;
  330. (*(json_char **) &top->_reserved.object_mem) += string_length + 1;
  331. }
  332. flags |= flag_seek_value | flag_need_colon;
  333. continue;
  334. default:
  335. break;
  336. };
  337. }
  338. else
  339. {
  340. string_add (b);
  341. continue;
  342. }
  343. }
  344. if (state.settings.settings & json_enable_comments)
  345. {
  346. if (flags & (flag_line_comment | flag_block_comment))
  347. {
  348. if (flags & flag_line_comment)
  349. {
  350. if (b == '\r' || b == '\n' || !b)
  351. {
  352. flags &= ~ flag_line_comment;
  353. -- state.ptr; /* so null can be reproc'd */
  354. }
  355. continue;
  356. }
  357. if (flags & flag_block_comment)
  358. {
  359. if (!b)
  360. { sprintf (error, "%d:%d: Unexpected EOF in block comment", line_and_col);
  361. goto e_failed;
  362. }
  363. if (b == '*' && state.ptr < (end - 1) && state.ptr [1] == '/')
  364. {
  365. flags &= ~ flag_block_comment;
  366. ++ state.ptr; /* skip closing sequence */
  367. }
  368. continue;
  369. }
  370. }
  371. else if (b == '/')
  372. {
  373. if (! (flags & (flag_seek_value | flag_done)) && top->type != json_object)
  374. { sprintf (error, "%d:%d: Comment not allowed here", line_and_col);
  375. goto e_failed;
  376. }
  377. if (++ state.ptr == end)
  378. { sprintf (error, "%d:%d: EOF unexpected", line_and_col);
  379. goto e_failed;
  380. }
  381. switch (b = *state.ptr)
  382. {
  383. case '/':
  384. flags |= flag_line_comment;
  385. continue;
  386. case '*':
  387. flags |= flag_block_comment;
  388. continue;
  389. default:
  390. sprintf (error, "%d:%d: Unexpected `%c` in comment opening sequence", line_and_col, b);
  391. goto e_failed;
  392. };
  393. }
  394. }
  395. if (flags & flag_done)
  396. {
  397. if (!b)
  398. break;
  399. switch (b)
  400. {
  401. whitespace:
  402. continue;
  403. default:
  404. sprintf (error, "%d:%d: Trailing garbage: `%c`",
  405. state.cur_line, state.cur_col, b);
  406. goto e_failed;
  407. };
  408. }
  409. if (flags & flag_seek_value)
  410. {
  411. switch (b)
  412. {
  413. whitespace:
  414. continue;
  415. case ']':
  416. if (top && top->type == json_array)
  417. flags = (flags & ~ (flag_need_comma | flag_seek_value)) | flag_next;
  418. else
  419. { sprintf (error, "%d:%d: Unexpected ]", line_and_col);
  420. goto e_failed;
  421. }
  422. break;
  423. default:
  424. if (flags & flag_need_comma)
  425. {
  426. if (b == ',')
  427. { flags &= ~ flag_need_comma;
  428. continue;
  429. }
  430. else
  431. {
  432. sprintf (error, "%d:%d: Expected , before %c",
  433. state.cur_line, state.cur_col, b);
  434. goto e_failed;
  435. }
  436. }
  437. if (flags & flag_need_colon)
  438. {
  439. if (b == ':')
  440. { flags &= ~ flag_need_colon;
  441. continue;
  442. }
  443. else
  444. {
  445. sprintf (error, "%d:%d: Expected : before %c",
  446. state.cur_line, state.cur_col, b);
  447. goto e_failed;
  448. }
  449. }
  450. flags &= ~ flag_seek_value;
  451. switch (b)
  452. {
  453. case '{':
  454. if (!new_value (&state, &top, &root, &alloc, json_object))
  455. goto e_alloc_failure;
  456. continue;
  457. case '[':
  458. if (!new_value (&state, &top, &root, &alloc, json_array))
  459. goto e_alloc_failure;
  460. flags |= flag_seek_value;
  461. continue;
  462. case '"':
  463. if (!new_value (&state, &top, &root, &alloc, json_string))
  464. goto e_alloc_failure;
  465. flags |= flag_string;
  466. string = top->u.string.ptr;
  467. string_length = 0;
  468. continue;
  469. case 't':
  470. if ((end - state.ptr) < 3 || *(++ state.ptr) != 'r' ||
  471. *(++ state.ptr) != 'u' || *(++ state.ptr) != 'e')
  472. {
  473. goto e_unknown_value;
  474. }
  475. if (!new_value (&state, &top, &root, &alloc, json_boolean))
  476. goto e_alloc_failure;
  477. top->u.boolean = 1;
  478. flags |= flag_next;
  479. break;
  480. case 'f':
  481. if ((end - state.ptr) < 4 || *(++ state.ptr) != 'a' ||
  482. *(++ state.ptr) != 'l' || *(++ state.ptr) != 's' ||
  483. *(++ state.ptr) != 'e')
  484. {
  485. goto e_unknown_value;
  486. }
  487. if (!new_value (&state, &top, &root, &alloc, json_boolean))
  488. goto e_alloc_failure;
  489. flags |= flag_next;
  490. break;
  491. case 'n':
  492. if ((end - state.ptr) < 3 || *(++ state.ptr) != 'u' ||
  493. *(++ state.ptr) != 'l' || *(++ state.ptr) != 'l')
  494. {
  495. goto e_unknown_value;
  496. }
  497. if (!new_value (&state, &top, &root, &alloc, json_null))
  498. goto e_alloc_failure;
  499. flags |= flag_next;
  500. break;
  501. default:
  502. if (isdigit (b) || b == '-')
  503. {
  504. if (!new_value (&state, &top, &root, &alloc, json_integer))
  505. goto e_alloc_failure;
  506. if (!state.first_pass)
  507. {
  508. while (isdigit (b) || b == '+' || b == '-'
  509. || b == 'e' || b == 'E' || b == '.')
  510. {
  511. if ( (++ state.ptr) == end)
  512. {
  513. b = 0;
  514. break;
  515. }
  516. b = *state.ptr;
  517. }
  518. flags |= flag_next | flag_reproc;
  519. break;
  520. }
  521. flags &= ~ (flag_num_negative | flag_num_e |
  522. flag_num_e_got_sign | flag_num_e_negative |
  523. flag_num_zero);
  524. num_digits = 0;
  525. num_fraction = 0;
  526. num_e = 0;
  527. if (b != '-')
  528. {
  529. flags |= flag_reproc;
  530. break;
  531. }
  532. flags |= flag_num_negative;
  533. continue;
  534. }
  535. else
  536. { sprintf (error, "%d:%d: Unexpected %c when seeking value", line_and_col, b);
  537. goto e_failed;
  538. }
  539. };
  540. };
  541. }
  542. else
  543. {
  544. switch (top->type)
  545. {
  546. case json_object:
  547. switch (b)
  548. {
  549. whitespace:
  550. continue;
  551. case '"':
  552. if (flags & flag_need_comma)
  553. { sprintf (error, "%d:%d: Expected , before \"", line_and_col);
  554. goto e_failed;
  555. }
  556. flags |= flag_string;
  557. string = (json_char *) top->_reserved.object_mem;
  558. string_length = 0;
  559. break;
  560. case '}':
  561. flags = (flags & ~ flag_need_comma) | flag_next;
  562. break;
  563. case ',':
  564. if (flags & flag_need_comma)
  565. {
  566. flags &= ~ flag_need_comma;
  567. break;
  568. }
  569. default:
  570. sprintf (error, "%d:%d: Unexpected `%c` in object", line_and_col, b);
  571. goto e_failed;
  572. };
  573. break;
  574. case json_integer:
  575. case json_double:
  576. if (isdigit (b))
  577. {
  578. ++ num_digits;
  579. if (top->type == json_integer || flags & flag_num_e)
  580. {
  581. if (! (flags & flag_num_e))
  582. {
  583. if (flags & flag_num_zero)
  584. { sprintf (error, "%d:%d: Unexpected `0` before `%c`", line_and_col, b);
  585. goto e_failed;
  586. }
  587. if (num_digits == 1 && b == '0')
  588. flags |= flag_num_zero;
  589. }
  590. else
  591. {
  592. flags |= flag_num_e_got_sign;
  593. num_e = (num_e * 10) + (b - '0');
  594. continue;
  595. }
  596. top->u.integer = (top->u.integer * 10) + (b - '0');
  597. continue;
  598. }
  599. num_fraction = (num_fraction * 10) + (b - '0');
  600. continue;
  601. }
  602. if (b == '+' || b == '-')
  603. {
  604. if ( (flags & flag_num_e) && !(flags & flag_num_e_got_sign))
  605. {
  606. flags |= flag_num_e_got_sign;
  607. if (b == '-')
  608. flags |= flag_num_e_negative;
  609. continue;
  610. }
  611. }
  612. else if (b == '.' && top->type == json_integer)
  613. {
  614. if (!num_digits)
  615. { sprintf (error, "%d:%d: Expected digit before `.`", line_and_col);
  616. goto e_failed;
  617. }
  618. top->type = json_double;
  619. top->u.dbl = (double) top->u.integer;
  620. num_digits = 0;
  621. continue;
  622. }
  623. if (! (flags & flag_num_e))
  624. {
  625. if (top->type == json_double)
  626. {
  627. if (!num_digits)
  628. { sprintf (error, "%d:%d: Expected digit after `.`", line_and_col);
  629. goto e_failed;
  630. }
  631. top->u.dbl += ((double) num_fraction) / (pow (10.0, (double) num_digits));
  632. }
  633. if (b == 'e' || b == 'E')
  634. {
  635. flags |= flag_num_e;
  636. if (top->type == json_integer)
  637. {
  638. top->type = json_double;
  639. top->u.dbl = (double) top->u.integer;
  640. }
  641. num_digits = 0;
  642. flags &= ~ flag_num_zero;
  643. continue;
  644. }
  645. }
  646. else
  647. {
  648. if (!num_digits)
  649. { sprintf (error, "%d:%d: Expected digit after `e`", line_and_col);
  650. goto e_failed;
  651. }
  652. top->u.dbl *= pow (10.0, (double)
  653. (flags & flag_num_e_negative ? - num_e : num_e));
  654. }
  655. if (flags & flag_num_negative)
  656. {
  657. if (top->type == json_integer)
  658. top->u.integer = - top->u.integer;
  659. else
  660. top->u.dbl = - top->u.dbl;
  661. }
  662. flags |= flag_next | flag_reproc;
  663. break;
  664. default:
  665. break;
  666. };
  667. }
  668. if (flags & flag_reproc)
  669. {
  670. flags &= ~ flag_reproc;
  671. -- state.ptr;
  672. }
  673. if (flags & flag_next)
  674. {
  675. flags = (flags & ~ flag_next) | flag_need_comma;
  676. if (!top->parent)
  677. {
  678. /* root value done */
  679. flags |= flag_done;
  680. continue;
  681. }
  682. if (top->parent->type == json_array)
  683. flags |= flag_seek_value;
  684. if (!state.first_pass)
  685. {
  686. json_value * parent = top->parent;
  687. switch (parent->type)
  688. {
  689. case json_object:
  690. parent->u.object.values
  691. [parent->u.object.length].value = top;
  692. break;
  693. case json_array:
  694. parent->u.array.values
  695. [parent->u.array.length] = top;
  696. break;
  697. default:
  698. break;
  699. };
  700. }
  701. if ( (++ top->parent->u.array.length) > state.uint_max)
  702. goto e_overflow;
  703. top = top->parent;
  704. continue;
  705. }
  706. }
  707. alloc = root;
  708. }
  709. return root;
  710. e_unknown_value:
  711. sprintf (error, "%d:%d: Unknown value", line_and_col);
  712. goto e_failed;
  713. e_alloc_failure:
  714. strcpy (error, "Memory allocation failure");
  715. goto e_failed;
  716. e_overflow:
  717. sprintf (error, "%d:%d: Too long (caught overflow)", line_and_col);
  718. goto e_failed;
  719. e_failed:
  720. if (error_buf)
  721. {
  722. if (*error)
  723. strcpy (error_buf, error);
  724. else
  725. strcpy (error_buf, "Unknown error");
  726. }
  727. if (state.first_pass)
  728. alloc = root;
  729. while (alloc)
  730. {
  731. top = alloc->_reserved.next_alloc;
  732. state.settings.mem_free (alloc, state.settings.user_data);
  733. alloc = top;
  734. }
  735. if (!state.first_pass)
  736. json_value_free_ex (&state.settings, root);
  737. return 0;
  738. }
  739. json_value * json_parse (const json_char * json, size_t length)
  740. {
  741. json_settings settings = { 0 };
  742. return json_parse_ex (&settings, json, length, 0);
  743. }
  744. void json_value_free_ex (json_settings * settings, json_value * value)
  745. {
  746. json_value * cur_value;
  747. if (!value)
  748. return;
  749. value->parent = 0;
  750. while (value)
  751. {
  752. switch (value->type)
  753. {
  754. case json_array:
  755. if (!value->u.array.length)
  756. {
  757. settings->mem_free (value->u.array.values, settings->user_data);
  758. break;
  759. }
  760. value = value->u.array.values [-- value->u.array.length];
  761. continue;
  762. case json_object:
  763. if (!value->u.object.length)
  764. {
  765. settings->mem_free (value->u.object.values, settings->user_data);
  766. break;
  767. }
  768. value = value->u.object.values [-- value->u.object.length].value;
  769. continue;
  770. case json_string:
  771. settings->mem_free (value->u.string.ptr, settings->user_data);
  772. break;
  773. default:
  774. break;
  775. };
  776. cur_value = value;
  777. value = value->parent;
  778. settings->mem_free (cur_value, settings->user_data);
  779. }
  780. }
  781. void json_value_free (json_value * value)
  782. {
  783. json_settings settings = { 0 };
  784. settings.mem_free = default_free;
  785. json_value_free_ex (&settings, value);
  786. }