json.c 29 KB

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