json.c 30 KB

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