You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

838 lines
23 KiB

  1. /* vim: set et ts=3 sw=3 ft=c:
  2. *
  3. * Copyright (C) 2012 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. #ifdef __cplusplus
  36. const struct _json_value json_value_none; /* zero-d by ctor */
  37. #else
  38. const struct _json_value json_value_none = { 0 };
  39. #endif
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <math.h>
  45. typedef unsigned short json_uchar;
  46. static unsigned char hex_value (json_char c)
  47. {
  48. if (c >= 'A' && c <= 'F')
  49. return (c - 'A') + 10;
  50. if (c >= 'a' && c <= 'f')
  51. return (c - 'a') + 10;
  52. if (c >= '0' && c <= '9')
  53. return c - '0';
  54. return 0xFF;
  55. }
  56. typedef struct
  57. {
  58. json_settings settings;
  59. int first_pass;
  60. unsigned long used_memory;
  61. unsigned int uint_max;
  62. unsigned long ulong_max;
  63. } json_state;
  64. static void * json_alloc (json_state * state, unsigned long size, int zero)
  65. {
  66. void * mem;
  67. if ((state->ulong_max - state->used_memory) < size)
  68. return 0;
  69. if (state->settings.max_memory
  70. && (state->used_memory += size) > state->settings.max_memory)
  71. {
  72. return 0;
  73. }
  74. if (! (mem = zero ? calloc (size, 1) : malloc (size)))
  75. return 0;
  76. return mem;
  77. }
  78. static int new_value
  79. (json_state * state, json_value ** top, json_value ** root, json_value ** alloc, json_type type)
  80. {
  81. json_value * value;
  82. int values_size;
  83. if (!state->first_pass)
  84. {
  85. value = *top = *alloc;
  86. *alloc = (*alloc)->_reserved.next_alloc;
  87. if (!*root)
  88. *root = value;
  89. switch (value->type)
  90. {
  91. case json_array:
  92. if (! (value->u.array.values = (json_value **) json_alloc
  93. (state, value->u.array.length * sizeof (json_value *), 0)) )
  94. {
  95. return 0;
  96. }
  97. value->u.array.length = 0;
  98. break;
  99. case json_object:
  100. values_size = sizeof (*value->u.object.values) * value->u.object.length;
  101. if (! ((*(void **) &value->u.object.values) = json_alloc
  102. (state, values_size + ((unsigned long) value->u.object.values), 0)) )
  103. {
  104. return 0;
  105. }
  106. value->_reserved.object_mem = (*(char **) &value->u.object.values) + values_size;
  107. value->u.object.length = 0;
  108. break;
  109. case json_string:
  110. if (! (value->u.string.ptr = (json_char *) json_alloc
  111. (state, (value->u.string.length + 1) * sizeof (json_char), 0)) )
  112. {
  113. return 0;
  114. }
  115. value->u.string.length = 0;
  116. break;
  117. default:
  118. break;
  119. };
  120. return 1;
  121. }
  122. value = (json_value *) json_alloc (state, sizeof (json_value), 1);
  123. if (!value)
  124. return 0;
  125. if (!*root)
  126. *root = value;
  127. value->type = type;
  128. value->parent = *top;
  129. if (*alloc)
  130. (*alloc)->_reserved.next_alloc = value;
  131. *alloc = *top = value;
  132. return 1;
  133. }
  134. #define e_off \
  135. ((int) (i - cur_line_begin))
  136. #define whitespace \
  137. case '\n': ++ cur_line; cur_line_begin = i; \
  138. case ' ': case '\t': case '\r'
  139. #define string_add(b) \
  140. do { if (!state.first_pass) string [string_length] = b; ++ string_length; } while (0);
  141. const static long
  142. flag_next = 1, flag_reproc = 2, flag_need_comma = 4, flag_seek_value = 8,
  143. flag_escaped = 16, flag_string = 32, flag_need_colon = 64, flag_done = 128,
  144. flag_num_negative = 256, flag_num_zero = 512, flag_num_e = 1024,
  145. flag_num_e_got_sign = 2048, flag_num_e_negative = 4096;
  146. json_value * json_parse_ex (json_settings * settings, const json_char * json, char * error_buf)
  147. {
  148. json_char error [128];
  149. unsigned int cur_line;
  150. const json_char * cur_line_begin, * i;
  151. json_value * top, * root, * alloc = 0;
  152. json_state state;
  153. long flags;
  154. long num_digits, num_fraction, num_e;
  155. error[0] = '\0';
  156. memset (&state, 0, sizeof (json_state));
  157. memcpy (&state.settings, settings, sizeof (json_settings));
  158. memset (&state.uint_max, 0xFF, sizeof (state.uint_max));
  159. memset (&state.ulong_max, 0xFF, sizeof (state.ulong_max));
  160. state.uint_max -= 8; /* limit of how much can be added before next check */
  161. state.ulong_max -= 8;
  162. for (state.first_pass = 1; state.first_pass >= 0; -- state.first_pass)
  163. {
  164. json_uchar uchar;
  165. unsigned char uc_b1, uc_b2, uc_b3, uc_b4;
  166. json_char * string;
  167. unsigned int string_length;
  168. top = root = 0;
  169. flags = flag_seek_value;
  170. cur_line = 1;
  171. cur_line_begin = json;
  172. for (i = json ;; ++ i)
  173. {
  174. json_char b = *i;
  175. if (flags & flag_done)
  176. {
  177. if (!b)
  178. break;
  179. switch (b)
  180. {
  181. whitespace:
  182. continue;
  183. default:
  184. sprintf (error, "%d:%d: Trailing garbage: `%c`", cur_line, e_off, b);
  185. goto e_failed;
  186. };
  187. }
  188. if (flags & flag_string)
  189. {
  190. if (!b)
  191. { sprintf (error, "Unexpected EOF in string (at %d:%d)", cur_line, e_off);
  192. goto e_failed;
  193. }
  194. if (string_length > state.uint_max)
  195. goto e_overflow;
  196. if (flags & flag_escaped)
  197. {
  198. flags &= ~ flag_escaped;
  199. switch (b)
  200. {
  201. case 'b': string_add ('\b'); break;
  202. case 'f': string_add ('\f'); break;
  203. case 'n': string_add ('\n'); break;
  204. case 'r': string_add ('\r'); break;
  205. case 't': string_add ('\t'); break;
  206. case 'u':
  207. if ((uc_b1 = hex_value (*++ i)) == 0xFF || (uc_b2 = hex_value (*++ i)) == 0xFF
  208. || (uc_b3 = hex_value (*++ i)) == 0xFF || (uc_b4 = hex_value (*++ i)) == 0xFF)
  209. {
  210. sprintf (error, "Invalid character value `%c` (at %d:%d)", b, cur_line, e_off);
  211. goto e_failed;
  212. }
  213. uc_b1 = uc_b1 * 16 + uc_b2;
  214. uc_b2 = uc_b3 * 16 + uc_b4;
  215. uchar = ((json_char) uc_b1) * 256 + uc_b2;
  216. if (sizeof (json_char) >= sizeof (json_uchar) || (uc_b1 == 0 && uc_b2 <= 0x7F))
  217. {
  218. string_add ((json_char) uchar);
  219. break;
  220. }
  221. if (uchar <= 0x7FF)
  222. {
  223. if (state.first_pass)
  224. string_length += 2;
  225. else
  226. { string [string_length ++] = 0xC0 | ((uc_b2 & 0xC0) >> 6) | ((uc_b1 & 0x7) << 2);
  227. string [string_length ++] = 0x80 | (uc_b2 & 0x3F);
  228. }
  229. break;
  230. }
  231. if (state.first_pass)
  232. string_length += 3;
  233. else
  234. { string [string_length ++] = 0xE0 | ((uc_b1 & 0xF0) >> 4);
  235. string [string_length ++] = 0x80 | ((uc_b1 & 0xF) << 2) | ((uc_b2 & 0xC0) >> 6);
  236. string [string_length ++] = 0x80 | (uc_b2 & 0x3F);
  237. }
  238. break;
  239. default:
  240. string_add (b);
  241. };
  242. continue;
  243. }
  244. if (b == '\\')
  245. {
  246. flags |= flag_escaped;
  247. continue;
  248. }
  249. if (b == '"')
  250. {
  251. if (!state.first_pass)
  252. string [string_length] = 0;
  253. flags &= ~ flag_string;
  254. string = 0;
  255. switch (top->type)
  256. {
  257. case json_string:
  258. top->u.string.length = string_length;
  259. flags |= flag_next;
  260. break;
  261. case json_object:
  262. if (state.first_pass)
  263. (*(json_char **) &top->u.object.values) += string_length + 1;
  264. else
  265. {
  266. top->u.object.values [top->u.object.length].name
  267. = (json_char *) top->_reserved.object_mem;
  268. (*(json_char **) &top->_reserved.object_mem) += string_length + 1;
  269. }
  270. flags |= flag_seek_value | flag_need_colon;
  271. continue;
  272. default:
  273. break;
  274. };
  275. }
  276. else
  277. {
  278. string_add (b);
  279. continue;
  280. }
  281. }
  282. if (flags & flag_seek_value)
  283. {
  284. switch (b)
  285. {
  286. whitespace:
  287. continue;
  288. case ']':
  289. if (top->type == json_array)
  290. flags = (flags & ~ (flag_need_comma | flag_seek_value)) | flag_next;
  291. else if (!state.settings.settings & json_relaxed_commas)
  292. { sprintf (error, "%d:%d: Unexpected ]", cur_line, e_off);
  293. goto e_failed;
  294. }
  295. break;
  296. default:
  297. if (flags & flag_need_comma)
  298. {
  299. if (b == ',')
  300. { flags &= ~ flag_need_comma;
  301. continue;
  302. }
  303. else
  304. { sprintf (error, "%d:%d: Expected , before %c", cur_line, e_off, b);
  305. goto e_failed;
  306. }
  307. }
  308. if (flags & flag_need_colon)
  309. {
  310. if (b == ':')
  311. { flags &= ~ flag_need_colon;
  312. continue;
  313. }
  314. else
  315. { sprintf (error, "%d:%d: Expected : before %c", cur_line, e_off, b);
  316. goto e_failed;
  317. }
  318. }
  319. flags &= ~ flag_seek_value;
  320. switch (b)
  321. {
  322. case '{':
  323. if (!new_value (&state, &top, &root, &alloc, json_object))
  324. goto e_alloc_failure;
  325. continue;
  326. case '[':
  327. if (!new_value (&state, &top, &root, &alloc, json_array))
  328. goto e_alloc_failure;
  329. flags |= flag_seek_value;
  330. continue;
  331. case '"':
  332. if (!new_value (&state, &top, &root, &alloc, json_string))
  333. goto e_alloc_failure;
  334. flags |= flag_string;
  335. string = top->u.string.ptr;
  336. string_length = 0;
  337. continue;
  338. case 't':
  339. if (*(++ i) != 'r' || *(++ i) != 'u' || *(++ i) != 'e')
  340. goto e_unknown_value;
  341. if (!new_value (&state, &top, &root, &alloc, json_boolean))
  342. goto e_alloc_failure;
  343. top->u.boolean = 1;
  344. flags |= flag_next;
  345. break;
  346. case 'f':
  347. if (*(++ i) != 'a' || *(++ i) != 'l' || *(++ i) != 's' || *(++ i) != 'e')
  348. goto e_unknown_value;
  349. if (!new_value (&state, &top, &root, &alloc, json_boolean))
  350. goto e_alloc_failure;
  351. flags |= flag_next;
  352. break;
  353. case 'n':
  354. if (*(++ i) != 'u' || *(++ i) != 'l' || *(++ i) != 'l')
  355. goto e_unknown_value;
  356. if (!new_value (&state, &top, &root, &alloc, json_null))
  357. goto e_alloc_failure;
  358. flags |= flag_next;
  359. break;
  360. default:
  361. if (isdigit (b) || b == '-')
  362. {
  363. if (!new_value (&state, &top, &root, &alloc, json_integer))
  364. goto e_alloc_failure;
  365. if (!state.first_pass)
  366. {
  367. while (isdigit (b) || b == '+' || b == '-'
  368. || b == 'e' || b == 'E' || b == '.')
  369. {
  370. b = *++ i;
  371. }
  372. flags |= flag_next | flag_reproc;
  373. break;
  374. }
  375. flags &= ~ (flag_num_negative | flag_num_e |
  376. flag_num_e_got_sign | flag_num_e_negative |
  377. flag_num_zero);
  378. num_digits = 0;
  379. num_fraction = 0;
  380. num_e = 0;
  381. if (b != '-')
  382. {
  383. flags |= flag_reproc;
  384. break;
  385. }
  386. flags |= flag_num_negative;
  387. continue;
  388. }
  389. else
  390. { sprintf (error, "%d:%d: Unexpected %c when seeking value", cur_line, e_off, b);
  391. goto e_failed;
  392. }
  393. };
  394. };
  395. }
  396. else
  397. {
  398. switch (top->type)
  399. {
  400. case json_object:
  401. switch (b)
  402. {
  403. whitespace:
  404. continue;
  405. case '"':
  406. if (flags & flag_need_comma && (!state.settings.settings & json_relaxed_commas))
  407. {
  408. sprintf (error, "%d:%d: Expected , before \"", cur_line, e_off);
  409. goto e_failed;
  410. }
  411. flags |= flag_string;
  412. string = (json_char *) top->_reserved.object_mem;
  413. string_length = 0;
  414. break;
  415. case '}':
  416. flags = (flags & ~ flag_need_comma) | flag_next;
  417. break;
  418. case ',':
  419. if (flags & flag_need_comma)
  420. {
  421. flags &= ~ flag_need_comma;
  422. break;
  423. }
  424. default:
  425. sprintf (error, "%d:%d: Unexpected `%c` in object", cur_line, e_off, b);
  426. goto e_failed;
  427. };
  428. break;
  429. case json_integer:
  430. case json_double:
  431. if (isdigit (b))
  432. {
  433. ++ num_digits;
  434. if (top->type == json_integer || flags & flag_num_e)
  435. {
  436. if (! (flags & flag_num_e))
  437. {
  438. if (flags & flag_num_zero)
  439. { sprintf (error, "%d:%d: Unexpected `0` before `%c`", cur_line, e_off, b);
  440. goto e_failed;
  441. }
  442. if (num_digits == 1 && b == '0')
  443. flags |= flag_num_zero;
  444. }
  445. else
  446. {
  447. flags |= flag_num_e_got_sign;
  448. num_e = (num_e * 10) + (b - '0');
  449. continue;
  450. }
  451. top->u.integer = (top->u.integer * 10) + (b - '0');
  452. continue;
  453. }
  454. num_fraction = (num_fraction * 10) + (b - '0');
  455. continue;
  456. }
  457. if (b == '+' || b == '-')
  458. {
  459. if ( (flags & flag_num_e) && !(flags & flag_num_e_got_sign))
  460. {
  461. flags |= flag_num_e_got_sign;
  462. if (b == '-')
  463. flags |= flag_num_e_negative;
  464. continue;
  465. }
  466. }
  467. else if (b == '.' && top->type == json_integer)
  468. {
  469. if (!num_digits)
  470. { sprintf (error, "%d:%d: Expected digit before `.`", cur_line, e_off);
  471. goto e_failed;
  472. }
  473. top->type = json_double;
  474. top->u.dbl = top->u.integer;
  475. num_digits = 0;
  476. continue;
  477. }
  478. if (! (flags & flag_num_e))
  479. {
  480. if (top->type == json_double)
  481. {
  482. if (!num_digits)
  483. { sprintf (error, "%d:%d: Expected digit after `.`", cur_line, e_off);
  484. goto e_failed;
  485. }
  486. top->u.dbl += ((double) num_fraction) / (pow (10, (double) num_digits));
  487. }
  488. if (b == 'e' || b == 'E')
  489. {
  490. flags |= flag_num_e;
  491. if (top->type == json_integer)
  492. {
  493. top->type = json_double;
  494. top->u.dbl = top->u.integer;
  495. }
  496. num_digits = 0;
  497. flags &= ~ flag_num_zero;
  498. continue;
  499. }
  500. }
  501. else
  502. {
  503. if (!num_digits)
  504. { sprintf (error, "%d:%d: Expected digit after `e`", cur_line, e_off);
  505. goto e_failed;
  506. }
  507. top->u.dbl *= pow (10, (double) (flags & flag_num_e_negative ? - num_e : num_e));
  508. }
  509. if (flags & flag_num_negative)
  510. {
  511. if (top->type == json_integer)
  512. top->u.integer = - top->u.integer;
  513. else
  514. top->u.dbl = - top->u.dbl;
  515. }
  516. flags |= flag_next | flag_reproc;
  517. break;
  518. default:
  519. break;
  520. };
  521. }
  522. if (flags & flag_reproc)
  523. {
  524. flags &= ~ flag_reproc;
  525. -- i;
  526. }
  527. if (flags & flag_next)
  528. {
  529. flags = (flags & ~ flag_next) | flag_need_comma;
  530. if (!top->parent)
  531. {
  532. /* root value done */
  533. flags |= flag_done;
  534. continue;
  535. }
  536. if (top->parent->type == json_array)
  537. flags |= flag_seek_value;
  538. if (!state.first_pass)
  539. {
  540. json_value * parent = top->parent;
  541. switch (parent->type)
  542. {
  543. case json_object:
  544. parent->u.object.values
  545. [parent->u.object.length].value = top;
  546. break;
  547. case json_array:
  548. parent->u.array.values
  549. [parent->u.array.length] = top;
  550. break;
  551. default:
  552. break;
  553. };
  554. }
  555. if ( (++ top->parent->u.array.length) > state.uint_max)
  556. goto e_overflow;
  557. top = top->parent;
  558. continue;
  559. }
  560. }
  561. alloc = root;
  562. }
  563. return root;
  564. e_unknown_value:
  565. sprintf (error, "%d:%d: Unknown value", cur_line, e_off);
  566. goto e_failed;
  567. e_alloc_failure:
  568. strcpy (error, "Memory allocation failure");
  569. goto e_failed;
  570. e_overflow:
  571. sprintf (error, "%d:%d: Too long (caught overflow)", cur_line, e_off);
  572. goto e_failed;
  573. e_failed:
  574. if (error_buf)
  575. {
  576. if (*error)
  577. strcpy (error_buf, error);
  578. else
  579. strcpy (error_buf, "Unknown error");
  580. }
  581. if (state.first_pass)
  582. alloc = root;
  583. while (alloc)
  584. {
  585. top = alloc->_reserved.next_alloc;
  586. free (alloc);
  587. alloc = top;
  588. }
  589. if (!state.first_pass)
  590. json_value_free (root);
  591. return 0;
  592. }
  593. json_value * json_parse (const json_char * json)
  594. {
  595. json_settings settings;
  596. memset (&settings, 0, sizeof (json_settings));
  597. return json_parse_ex (&settings, json, 0);
  598. }
  599. void json_value_free (json_value * value)
  600. {
  601. json_value * cur_value;
  602. if (!value)
  603. return;
  604. value->parent = 0;
  605. while (value)
  606. {
  607. switch (value->type)
  608. {
  609. case json_array:
  610. if (!value->u.array.length)
  611. {
  612. free (value->u.array.values);
  613. break;
  614. }
  615. value = value->u.array.values [-- value->u.array.length];
  616. continue;
  617. case json_object:
  618. if (!value->u.object.length)
  619. {
  620. free (value->u.object.values);
  621. break;
  622. }
  623. value = value->u.object.values [-- value->u.object.length].value;
  624. continue;
  625. case json_string:
  626. free (value->u.string.ptr);
  627. break;
  628. default:
  629. break;
  630. };
  631. cur_value = value;
  632. value = value->parent;
  633. free (cur_value);
  634. }
  635. }