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.

1002 lines
30 KiB

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