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

json_tokener.c 32 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /*
  2. * $Id: json_tokener.c,v 1.20 2006/07/25 03:24:50 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. *
  11. * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
  12. * The copyrights to the contents of this file are licensed under the MIT License
  13. * (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. #include "config.h"
  16. #include "math_compat.h"
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <limits.h>
  20. #include <math.h>
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "debug.h"
  26. #include "json_inttypes.h"
  27. #include "json_object.h"
  28. #include "json_object_private.h"
  29. #include "json_tokener.h"
  30. #include "json_util.h"
  31. #include "printbuf.h"
  32. #include "strdup_compat.h"
  33. #ifdef HAVE_LOCALE_H
  34. #include <locale.h>
  35. #endif /* HAVE_LOCALE_H */
  36. #ifdef HAVE_XLOCALE_H
  37. #include <xlocale.h>
  38. #endif
  39. #ifdef HAVE_STRINGS_H
  40. #include <strings.h>
  41. #endif /* HAVE_STRINGS_H */
  42. #define jt_hexdigit(x) (((x) <= '9') ? (x) - '0' : ((x)&7) + 9)
  43. #if !HAVE_STRNCASECMP && defined(_MSC_VER)
  44. /* MSC has the version as _strnicmp */
  45. #define strncasecmp _strnicmp
  46. #elif !HAVE_STRNCASECMP
  47. #error You do not have strncasecmp on your system.
  48. #endif /* HAVE_STRNCASECMP */
  49. /* Use C99 NAN by default; if not available, nan("") should work too. */
  50. #ifndef NAN
  51. #define NAN nan("")
  52. #endif /* !NAN */
  53. static const char json_null_str[] = "null";
  54. static const int json_null_str_len = sizeof(json_null_str) - 1;
  55. static const char json_inf_str[] = "Infinity";
  56. static const char json_inf_str_lower[] = "infinity";
  57. static const unsigned int json_inf_str_len = sizeof(json_inf_str) - 1;
  58. static const char json_nan_str[] = "NaN";
  59. static const int json_nan_str_len = sizeof(json_nan_str) - 1;
  60. static const char json_true_str[] = "true";
  61. static const int json_true_str_len = sizeof(json_true_str) - 1;
  62. static const char json_false_str[] = "false";
  63. static const int json_false_str_len = sizeof(json_false_str) - 1;
  64. /* clang-format off */
  65. static const char *json_tokener_errors[] = {
  66. "success",
  67. "continue",
  68. "nesting too deep",
  69. "unexpected end of data",
  70. "unexpected character",
  71. "null expected",
  72. "boolean expected",
  73. "number expected",
  74. "array value separator ',' expected",
  75. "quoted object property name expected",
  76. "object property name separator ':' expected",
  77. "object value separator ',' expected",
  78. "invalid string sequence",
  79. "expected comment",
  80. "invalid utf-8 string",
  81. "buffer size overflow"
  82. };
  83. /* clang-format on */
  84. /**
  85. * validete the utf-8 string in strict model.
  86. * if not utf-8 format, return err.
  87. */
  88. static json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes);
  89. const char *json_tokener_error_desc(enum json_tokener_error jerr)
  90. {
  91. int jerr_int = (int)jerr;
  92. if (jerr_int < 0 ||
  93. jerr_int >= (int)(sizeof(json_tokener_errors) / sizeof(json_tokener_errors[0])))
  94. return "Unknown error, "
  95. "invalid json_tokener_error value passed to json_tokener_error_desc()";
  96. return json_tokener_errors[jerr];
  97. }
  98. enum json_tokener_error json_tokener_get_error(struct json_tokener *tok)
  99. {
  100. return tok->err;
  101. }
  102. /* Stuff for decoding unicode sequences */
  103. #define IS_HIGH_SURROGATE(uc) (((uc)&0xFC00) == 0xD800)
  104. #define IS_LOW_SURROGATE(uc) (((uc)&0xFC00) == 0xDC00)
  105. #define DECODE_SURROGATE_PAIR(hi, lo) ((((hi)&0x3FF) << 10) + ((lo)&0x3FF) + 0x10000)
  106. static unsigned char utf8_replacement_char[3] = {0xEF, 0xBF, 0xBD};
  107. struct json_tokener *json_tokener_new_ex(int depth)
  108. {
  109. struct json_tokener *tok;
  110. tok = (struct json_tokener *)calloc(1, sizeof(struct json_tokener));
  111. if (!tok)
  112. return NULL;
  113. tok->stack = (struct json_tokener_srec *)calloc(depth, sizeof(struct json_tokener_srec));
  114. if (!tok->stack)
  115. {
  116. free(tok);
  117. return NULL;
  118. }
  119. tok->pb = printbuf_new();
  120. tok->max_depth = depth;
  121. json_tokener_reset(tok);
  122. return tok;
  123. }
  124. struct json_tokener *json_tokener_new(void)
  125. {
  126. return json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH);
  127. }
  128. void json_tokener_free(struct json_tokener *tok)
  129. {
  130. json_tokener_reset(tok);
  131. if (tok->pb)
  132. printbuf_free(tok->pb);
  133. free(tok->stack);
  134. free(tok);
  135. }
  136. static void json_tokener_reset_level(struct json_tokener *tok, int depth)
  137. {
  138. tok->stack[depth].state = json_tokener_state_eatws;
  139. tok->stack[depth].saved_state = json_tokener_state_start;
  140. json_object_put(tok->stack[depth].current);
  141. tok->stack[depth].current = NULL;
  142. free(tok->stack[depth].obj_field_name);
  143. tok->stack[depth].obj_field_name = NULL;
  144. }
  145. void json_tokener_reset(struct json_tokener *tok)
  146. {
  147. int i;
  148. if (!tok)
  149. return;
  150. for (i = tok->depth; i >= 0; i--)
  151. json_tokener_reset_level(tok, i);
  152. tok->depth = 0;
  153. tok->err = json_tokener_success;
  154. }
  155. struct json_object *json_tokener_parse(const char *str)
  156. {
  157. enum json_tokener_error jerr_ignored;
  158. struct json_object *obj;
  159. obj = json_tokener_parse_verbose(str, &jerr_ignored);
  160. return obj;
  161. }
  162. struct json_object *json_tokener_parse_verbose(const char *str, enum json_tokener_error *error)
  163. {
  164. struct json_tokener *tok;
  165. struct json_object *obj;
  166. tok = json_tokener_new();
  167. if (!tok)
  168. return NULL;
  169. obj = json_tokener_parse_ex(tok, str, -1);
  170. *error = tok->err;
  171. if (tok->err != json_tokener_success)
  172. {
  173. if (obj != NULL)
  174. json_object_put(obj);
  175. obj = NULL;
  176. }
  177. json_tokener_free(tok);
  178. return obj;
  179. }
  180. #define state tok->stack[tok->depth].state
  181. #define saved_state tok->stack[tok->depth].saved_state
  182. #define current tok->stack[tok->depth].current
  183. #define obj_field_name tok->stack[tok->depth].obj_field_name
  184. /* Optimization:
  185. * json_tokener_parse_ex() consumed a lot of CPU in its main loop,
  186. * iterating character-by character. A large performance boost is
  187. * achieved by using tighter loops to locally handle units such as
  188. * comments and strings. Loops that handle an entire token within
  189. * their scope also gather entire strings and pass them to
  190. * printbuf_memappend() in a single call, rather than calling
  191. * printbuf_memappend() one char at a time.
  192. *
  193. * PEEK_CHAR() and ADVANCE_CHAR() macros are used for code that is
  194. * common to both the main loop and the tighter loops.
  195. */
  196. /* PEEK_CHAR(dest, tok) macro:
  197. * Peeks at the current char and stores it in dest.
  198. * Returns 1 on success, sets tok->err and returns 0 if no more chars.
  199. * Implicit inputs: str, len vars
  200. */
  201. #define PEEK_CHAR(dest, tok) \
  202. (((tok)->char_offset == len) \
  203. ? (((tok)->depth == 0 && state == json_tokener_state_eatws && \
  204. saved_state == json_tokener_state_finish) \
  205. ? (((tok)->err = json_tokener_success), 0) \
  206. : (((tok)->err = json_tokener_continue), 0)) \
  207. : (((tok->flags & JSON_TOKENER_VALIDATE_UTF8) && \
  208. (!json_tokener_validate_utf8(*str, nBytesp))) \
  209. ? ((tok->err = json_tokener_error_parse_utf8_string), 0) \
  210. : (((dest) = *str), 1)))
  211. /* ADVANCE_CHAR() macro:
  212. * Increments str & tok->char_offset.
  213. * For convenience of existing conditionals, returns the old value of c (0 on eof)
  214. * Implicit inputs: c var
  215. */
  216. #define ADVANCE_CHAR(str, tok) (++(str), ((tok)->char_offset)++, c)
  217. /* End optimization macro defs */
  218. struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char *str, int len)
  219. {
  220. struct json_object *obj = NULL;
  221. char c = '\1';
  222. unsigned int nBytes = 0;
  223. unsigned int *nBytesp = &nBytes;
  224. #ifdef HAVE_USELOCALE
  225. locale_t oldlocale = uselocale(NULL);
  226. locale_t newloc;
  227. #elif defined(HAVE_SETLOCALE)
  228. char *oldlocale = NULL;
  229. #endif
  230. tok->char_offset = 0;
  231. tok->err = json_tokener_success;
  232. /* this interface is presently not 64-bit clean due to the int len argument
  233. * and the internal printbuf interface that takes 32-bit int len arguments
  234. * so the function limits the maximum string size to INT32_MAX (2GB).
  235. * If the function is called with len == -1 then strlen is called to check
  236. * the string length is less than INT32_MAX (2GB)
  237. */
  238. if ((len < -1) || (len == -1 && strlen(str) > INT32_MAX))
  239. {
  240. tok->err = json_tokener_error_size;
  241. return NULL;
  242. }
  243. #ifdef HAVE_USELOCALE
  244. {
  245. locale_t duploc = duplocale(oldlocale);
  246. newloc = newlocale(LC_NUMERIC_MASK, "C", duploc);
  247. if (newloc == NULL)
  248. {
  249. freelocale(duploc);
  250. return NULL;
  251. }
  252. uselocale(newloc);
  253. }
  254. #elif defined(HAVE_SETLOCALE)
  255. {
  256. char *tmplocale;
  257. tmplocale = setlocale(LC_NUMERIC, NULL);
  258. if (tmplocale)
  259. oldlocale = strdup(tmplocale);
  260. setlocale(LC_NUMERIC, "C");
  261. }
  262. #endif
  263. while (PEEK_CHAR(c, tok))
  264. {
  265. redo_char:
  266. switch (state)
  267. {
  268. case json_tokener_state_eatws:
  269. /* Advance until we change state */
  270. while (isspace((unsigned char)c))
  271. {
  272. if ((!ADVANCE_CHAR(str, tok)) || (!PEEK_CHAR(c, tok)))
  273. goto out;
  274. }
  275. if (c == '/' && !(tok->flags & JSON_TOKENER_STRICT))
  276. {
  277. printbuf_reset(tok->pb);
  278. printbuf_memappend_fast(tok->pb, &c, 1);
  279. state = json_tokener_state_comment_start;
  280. }
  281. else
  282. {
  283. state = saved_state;
  284. goto redo_char;
  285. }
  286. break;
  287. case json_tokener_state_start:
  288. switch (c)
  289. {
  290. case '{':
  291. state = json_tokener_state_eatws;
  292. saved_state = json_tokener_state_object_field_start;
  293. current = json_object_new_object();
  294. if (current == NULL)
  295. goto out;
  296. break;
  297. case '[':
  298. state = json_tokener_state_eatws;
  299. saved_state = json_tokener_state_array;
  300. current = json_object_new_array();
  301. if (current == NULL)
  302. goto out;
  303. break;
  304. case 'I':
  305. case 'i':
  306. state = json_tokener_state_inf;
  307. printbuf_reset(tok->pb);
  308. tok->st_pos = 0;
  309. goto redo_char;
  310. case 'N':
  311. case 'n':
  312. state = json_tokener_state_null; // or NaN
  313. printbuf_reset(tok->pb);
  314. tok->st_pos = 0;
  315. goto redo_char;
  316. case '\'':
  317. if (tok->flags & JSON_TOKENER_STRICT)
  318. {
  319. /* in STRICT mode only double-quote are allowed */
  320. tok->err = json_tokener_error_parse_unexpected;
  321. goto out;
  322. }
  323. /* FALLTHRU */
  324. case '"':
  325. state = json_tokener_state_string;
  326. printbuf_reset(tok->pb);
  327. tok->quote_char = c;
  328. break;
  329. case 'T':
  330. case 't':
  331. case 'F':
  332. case 'f':
  333. state = json_tokener_state_boolean;
  334. printbuf_reset(tok->pb);
  335. tok->st_pos = 0;
  336. goto redo_char;
  337. case '0':
  338. case '1':
  339. case '2':
  340. case '3':
  341. case '4':
  342. case '5':
  343. case '6':
  344. case '7':
  345. case '8':
  346. case '9':
  347. case '-':
  348. state = json_tokener_state_number;
  349. printbuf_reset(tok->pb);
  350. tok->is_double = 0;
  351. goto redo_char;
  352. default: tok->err = json_tokener_error_parse_unexpected; goto out;
  353. }
  354. break;
  355. case json_tokener_state_finish:
  356. if (tok->depth == 0)
  357. goto out;
  358. obj = json_object_get(current);
  359. json_tokener_reset_level(tok, tok->depth);
  360. tok->depth--;
  361. goto redo_char;
  362. case json_tokener_state_inf: /* aka starts with 'i' (or 'I', or "-i", or "-I") */
  363. {
  364. /* If we were guaranteed to have len set, then we could (usually) handle
  365. * the entire "Infinity" check in a single strncmp (strncasecmp), but
  366. * since len might be -1 (i.e. "read until \0"), we need to check it
  367. * a character at a time.
  368. * Trying to handle it both ways would make this code considerably more
  369. * complicated with likely little performance benefit.
  370. */
  371. int is_negative = 0;
  372. const char *_json_inf_str = json_inf_str;
  373. if (!(tok->flags & JSON_TOKENER_STRICT))
  374. _json_inf_str = json_inf_str_lower;
  375. /* Note: tok->st_pos must be 0 when state is set to json_tokener_state_inf */
  376. while (tok->st_pos < (int)json_inf_str_len)
  377. {
  378. char inf_char = *str;
  379. if (!(tok->flags & JSON_TOKENER_STRICT))
  380. inf_char = tolower((int)*str);
  381. if (inf_char != _json_inf_str[tok->st_pos])
  382. {
  383. tok->err = json_tokener_error_parse_unexpected;
  384. goto out;
  385. }
  386. tok->st_pos++;
  387. (void)ADVANCE_CHAR(str, tok);
  388. if (!PEEK_CHAR(c, tok))
  389. {
  390. /* out of input chars, for now at least */
  391. goto out;
  392. }
  393. }
  394. /* We checked the full length of "Infinity", so create the object.
  395. * When handling -Infinity, the number parsing code will have dropped
  396. * the "-" into tok->pb for us, so check it now.
  397. */
  398. if (printbuf_length(tok->pb) > 0 && *(tok->pb->buf) == '-')
  399. {
  400. is_negative = 1;
  401. }
  402. current = json_object_new_double(is_negative ? -INFINITY : INFINITY);
  403. if (current == NULL)
  404. goto out;
  405. saved_state = json_tokener_state_finish;
  406. state = json_tokener_state_eatws;
  407. goto redo_char;
  408. }
  409. break;
  410. case json_tokener_state_null: /* aka starts with 'n' */
  411. {
  412. int size;
  413. int size_nan;
  414. printbuf_memappend_fast(tok->pb, &c, 1);
  415. size = json_min(tok->st_pos + 1, json_null_str_len);
  416. size_nan = json_min(tok->st_pos + 1, json_nan_str_len);
  417. if ((!(tok->flags & JSON_TOKENER_STRICT) &&
  418. strncasecmp(json_null_str, tok->pb->buf, size) == 0) ||
  419. (strncmp(json_null_str, tok->pb->buf, size) == 0))
  420. {
  421. if (tok->st_pos == json_null_str_len)
  422. {
  423. current = NULL;
  424. saved_state = json_tokener_state_finish;
  425. state = json_tokener_state_eatws;
  426. goto redo_char;
  427. }
  428. }
  429. else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
  430. strncasecmp(json_nan_str, tok->pb->buf, size_nan) == 0) ||
  431. (strncmp(json_nan_str, tok->pb->buf, size_nan) == 0))
  432. {
  433. if (tok->st_pos == json_nan_str_len)
  434. {
  435. current = json_object_new_double(NAN);
  436. if (current == NULL)
  437. goto out;
  438. saved_state = json_tokener_state_finish;
  439. state = json_tokener_state_eatws;
  440. goto redo_char;
  441. }
  442. }
  443. else
  444. {
  445. tok->err = json_tokener_error_parse_null;
  446. goto out;
  447. }
  448. tok->st_pos++;
  449. }
  450. break;
  451. case json_tokener_state_comment_start:
  452. if (c == '*')
  453. {
  454. state = json_tokener_state_comment;
  455. }
  456. else if (c == '/')
  457. {
  458. state = json_tokener_state_comment_eol;
  459. }
  460. else
  461. {
  462. tok->err = json_tokener_error_parse_comment;
  463. goto out;
  464. }
  465. printbuf_memappend_fast(tok->pb, &c, 1);
  466. break;
  467. case json_tokener_state_comment:
  468. {
  469. /* Advance until we change state */
  470. const char *case_start = str;
  471. while (c != '*')
  472. {
  473. if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
  474. {
  475. printbuf_memappend_fast(tok->pb, case_start,
  476. str - case_start);
  477. goto out;
  478. }
  479. }
  480. printbuf_memappend_fast(tok->pb, case_start, 1 + str - case_start);
  481. state = json_tokener_state_comment_end;
  482. }
  483. break;
  484. case json_tokener_state_comment_eol:
  485. {
  486. /* Advance until we change state */
  487. const char *case_start = str;
  488. while (c != '\n')
  489. {
  490. if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
  491. {
  492. printbuf_memappend_fast(tok->pb, case_start,
  493. str - case_start);
  494. goto out;
  495. }
  496. }
  497. printbuf_memappend_fast(tok->pb, case_start, str - case_start);
  498. MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
  499. state = json_tokener_state_eatws;
  500. }
  501. break;
  502. case json_tokener_state_comment_end:
  503. printbuf_memappend_fast(tok->pb, &c, 1);
  504. if (c == '/')
  505. {
  506. MC_DEBUG("json_tokener_comment: %s\n", tok->pb->buf);
  507. state = json_tokener_state_eatws;
  508. }
  509. else
  510. {
  511. state = json_tokener_state_comment;
  512. }
  513. break;
  514. case json_tokener_state_string:
  515. {
  516. /* Advance until we change state */
  517. const char *case_start = str;
  518. while (1)
  519. {
  520. if (c == tok->quote_char)
  521. {
  522. printbuf_memappend_fast(tok->pb, case_start,
  523. str - case_start);
  524. current =
  525. json_object_new_string_len(tok->pb->buf, tok->pb->bpos);
  526. if (current == NULL)
  527. goto out;
  528. saved_state = json_tokener_state_finish;
  529. state = json_tokener_state_eatws;
  530. break;
  531. }
  532. else if (c == '\\')
  533. {
  534. printbuf_memappend_fast(tok->pb, case_start,
  535. str - case_start);
  536. saved_state = json_tokener_state_string;
  537. state = json_tokener_state_string_escape;
  538. break;
  539. }
  540. if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
  541. {
  542. printbuf_memappend_fast(tok->pb, case_start,
  543. str - case_start);
  544. goto out;
  545. }
  546. }
  547. }
  548. break;
  549. case json_tokener_state_string_escape:
  550. switch (c)
  551. {
  552. case '"':
  553. case '\\':
  554. case '/':
  555. printbuf_memappend_fast(tok->pb, &c, 1);
  556. state = saved_state;
  557. break;
  558. case 'b':
  559. case 'n':
  560. case 'r':
  561. case 't':
  562. case 'f':
  563. if (c == 'b')
  564. printbuf_memappend_fast(tok->pb, "\b", 1);
  565. else if (c == 'n')
  566. printbuf_memappend_fast(tok->pb, "\n", 1);
  567. else if (c == 'r')
  568. printbuf_memappend_fast(tok->pb, "\r", 1);
  569. else if (c == 't')
  570. printbuf_memappend_fast(tok->pb, "\t", 1);
  571. else if (c == 'f')
  572. printbuf_memappend_fast(tok->pb, "\f", 1);
  573. state = saved_state;
  574. break;
  575. case 'u':
  576. tok->ucs_char = 0;
  577. tok->st_pos = 0;
  578. state = json_tokener_state_escape_unicode;
  579. break;
  580. default: tok->err = json_tokener_error_parse_string; goto out;
  581. }
  582. break;
  583. case json_tokener_state_escape_unicode:
  584. {
  585. unsigned int got_hi_surrogate = 0;
  586. /* Handle a 4-byte sequence, or two sequences if a surrogate pair */
  587. while (1)
  588. {
  589. if (c && strchr(json_hex_chars, c))
  590. {
  591. tok->ucs_char += ((unsigned int)jt_hexdigit(c)
  592. << ((3 - tok->st_pos++) * 4));
  593. if (tok->st_pos == 4)
  594. {
  595. unsigned char unescaped_utf[4];
  596. if (got_hi_surrogate)
  597. {
  598. if (IS_LOW_SURROGATE(tok->ucs_char))
  599. {
  600. /* Recalculate the ucs_char, then fall thru to process normally */
  601. tok->ucs_char =
  602. DECODE_SURROGATE_PAIR(
  603. got_hi_surrogate,
  604. tok->ucs_char);
  605. }
  606. else
  607. {
  608. /* Hi surrogate was not followed by a low surrogate */
  609. /* Replace the hi and process the rest normally */
  610. printbuf_memappend_fast(
  611. tok->pb,
  612. (char *)utf8_replacement_char,
  613. 3);
  614. }
  615. got_hi_surrogate = 0;
  616. }
  617. if (tok->ucs_char < 0x80)
  618. {
  619. unescaped_utf[0] = tok->ucs_char;
  620. printbuf_memappend_fast(
  621. tok->pb, (char *)unescaped_utf, 1);
  622. }
  623. else if (tok->ucs_char < 0x800)
  624. {
  625. unescaped_utf[0] =
  626. 0xc0 | (tok->ucs_char >> 6);
  627. unescaped_utf[1] =
  628. 0x80 | (tok->ucs_char & 0x3f);
  629. printbuf_memappend_fast(
  630. tok->pb, (char *)unescaped_utf, 2);
  631. }
  632. else if (IS_HIGH_SURROGATE(tok->ucs_char))
  633. {
  634. /* Got a high surrogate. Remember it and look for
  635. * the beginning of another sequence, which
  636. * should be the low surrogate.
  637. */
  638. got_hi_surrogate = tok->ucs_char;
  639. /* Not at end, and the next two chars should be "\u" */
  640. if ((len == -1 ||
  641. len > (tok->char_offset + 2)) &&
  642. // str[0] != '0' && // implied by json_hex_chars, above.
  643. (str[1] == '\\') && (str[2] == 'u'))
  644. {
  645. /* Advance through the 16 bit surrogate, and move
  646. * on to the next sequence. The next step is to
  647. * process the following characters.
  648. */
  649. if (!ADVANCE_CHAR(str, tok) ||
  650. !ADVANCE_CHAR(str, tok))
  651. {
  652. printbuf_memappend_fast(
  653. tok->pb,
  654. (char *)
  655. utf8_replacement_char,
  656. 3);
  657. }
  658. /* Advance to the first char of the next sequence and
  659. * continue processing with the next sequence.
  660. */
  661. if (!ADVANCE_CHAR(str, tok) ||
  662. !PEEK_CHAR(c, tok))
  663. {
  664. printbuf_memappend_fast(
  665. tok->pb,
  666. (char *)
  667. utf8_replacement_char,
  668. 3);
  669. goto out;
  670. }
  671. tok->ucs_char = 0;
  672. tok->st_pos = 0;
  673. /* other json_tokener_state_escape_unicode */
  674. continue;
  675. }
  676. else
  677. {
  678. /* Got a high surrogate without another sequence following
  679. * it. Put a replacement char in for the hi surrogate
  680. * and pretend we finished.
  681. */
  682. printbuf_memappend_fast(
  683. tok->pb,
  684. (char *)utf8_replacement_char,
  685. 3);
  686. }
  687. }
  688. else if (IS_LOW_SURROGATE(tok->ucs_char))
  689. {
  690. /* Got a low surrogate not preceded by a high */
  691. printbuf_memappend_fast(
  692. tok->pb, (char *)utf8_replacement_char,
  693. 3);
  694. }
  695. else if (tok->ucs_char < 0x10000)
  696. {
  697. unescaped_utf[0] =
  698. 0xe0 | (tok->ucs_char >> 12);
  699. unescaped_utf[1] =
  700. 0x80 | ((tok->ucs_char >> 6) & 0x3f);
  701. unescaped_utf[2] =
  702. 0x80 | (tok->ucs_char & 0x3f);
  703. printbuf_memappend_fast(
  704. tok->pb, (char *)unescaped_utf, 3);
  705. }
  706. else if (tok->ucs_char < 0x110000)
  707. {
  708. unescaped_utf[0] =
  709. 0xf0 | ((tok->ucs_char >> 18) & 0x07);
  710. unescaped_utf[1] =
  711. 0x80 | ((tok->ucs_char >> 12) & 0x3f);
  712. unescaped_utf[2] =
  713. 0x80 | ((tok->ucs_char >> 6) & 0x3f);
  714. unescaped_utf[3] =
  715. 0x80 | (tok->ucs_char & 0x3f);
  716. printbuf_memappend_fast(
  717. tok->pb, (char *)unescaped_utf, 4);
  718. }
  719. else
  720. {
  721. /* Don't know what we got--insert the replacement char */
  722. printbuf_memappend_fast(
  723. tok->pb, (char *)utf8_replacement_char,
  724. 3);
  725. }
  726. state = saved_state;
  727. break;
  728. }
  729. }
  730. else
  731. {
  732. tok->err = json_tokener_error_parse_string;
  733. goto out;
  734. }
  735. if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
  736. {
  737. /* Clean up any pending chars */
  738. if (got_hi_surrogate)
  739. printbuf_memappend_fast(
  740. tok->pb, (char *)utf8_replacement_char, 3);
  741. goto out;
  742. }
  743. }
  744. }
  745. break;
  746. case json_tokener_state_boolean:
  747. {
  748. int size1, size2;
  749. printbuf_memappend_fast(tok->pb, &c, 1);
  750. size1 = json_min(tok->st_pos + 1, json_true_str_len);
  751. size2 = json_min(tok->st_pos + 1, json_false_str_len);
  752. if ((!(tok->flags & JSON_TOKENER_STRICT) &&
  753. strncasecmp(json_true_str, tok->pb->buf, size1) == 0) ||
  754. (strncmp(json_true_str, tok->pb->buf, size1) == 0))
  755. {
  756. if (tok->st_pos == json_true_str_len)
  757. {
  758. current = json_object_new_boolean(1);
  759. if (current == NULL)
  760. goto out;
  761. saved_state = json_tokener_state_finish;
  762. state = json_tokener_state_eatws;
  763. goto redo_char;
  764. }
  765. }
  766. else if ((!(tok->flags & JSON_TOKENER_STRICT) &&
  767. strncasecmp(json_false_str, tok->pb->buf, size2) == 0) ||
  768. (strncmp(json_false_str, tok->pb->buf, size2) == 0))
  769. {
  770. if (tok->st_pos == json_false_str_len)
  771. {
  772. current = json_object_new_boolean(0);
  773. if (current == NULL)
  774. goto out;
  775. saved_state = json_tokener_state_finish;
  776. state = json_tokener_state_eatws;
  777. goto redo_char;
  778. }
  779. }
  780. else
  781. {
  782. tok->err = json_tokener_error_parse_boolean;
  783. goto out;
  784. }
  785. tok->st_pos++;
  786. }
  787. break;
  788. case json_tokener_state_number:
  789. {
  790. /* Advance until we change state */
  791. const char *case_start = str;
  792. int case_len = 0;
  793. int is_exponent = 0;
  794. int negativesign_next_possible_location = 1;
  795. while (c && strchr(json_number_chars, c))
  796. {
  797. ++case_len;
  798. /* non-digit characters checks */
  799. /* note: since the main loop condition to get here was
  800. * an input starting with 0-9 or '-', we are
  801. * protected from input starting with '.' or
  802. * e/E.
  803. */
  804. if (c == '.')
  805. {
  806. if (tok->is_double != 0)
  807. {
  808. /* '.' can only be found once, and out of the exponent part.
  809. * Thus, if the input is already flagged as double, it
  810. * is invalid.
  811. */
  812. tok->err = json_tokener_error_parse_number;
  813. goto out;
  814. }
  815. tok->is_double = 1;
  816. }
  817. if (c == 'e' || c == 'E')
  818. {
  819. if (is_exponent != 0)
  820. {
  821. /* only one exponent possible */
  822. tok->err = json_tokener_error_parse_number;
  823. goto out;
  824. }
  825. is_exponent = 1;
  826. tok->is_double = 1;
  827. /* the exponent part can begin with a negative sign */
  828. negativesign_next_possible_location = case_len + 1;
  829. }
  830. if (c == '-' && case_len != negativesign_next_possible_location)
  831. {
  832. /* If the negative sign is not where expected (ie
  833. * start of input or start of exponent part), the
  834. * input is invalid.
  835. */
  836. tok->err = json_tokener_error_parse_number;
  837. goto out;
  838. }
  839. if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
  840. {
  841. printbuf_memappend_fast(tok->pb, case_start, case_len);
  842. goto out;
  843. }
  844. }
  845. if (case_len > 0)
  846. printbuf_memappend_fast(tok->pb, case_start, case_len);
  847. // Check for -Infinity
  848. if (tok->pb->buf[0] == '-' && case_len <= 1 && (c == 'i' || c == 'I'))
  849. {
  850. state = json_tokener_state_inf;
  851. tok->st_pos = 0;
  852. goto redo_char;
  853. }
  854. }
  855. {
  856. int64_t num64;
  857. uint64_t numuint64;
  858. double numd;
  859. if (!tok->is_double && tok->pb->buf[0] == '-' &&
  860. json_parse_int64(tok->pb->buf, &num64) == 0)
  861. {
  862. current = json_object_new_int64(num64);
  863. if (current == NULL)
  864. goto out;
  865. }
  866. else if (!tok->is_double && tok->pb->buf[0] != '-' &&
  867. json_parse_uint64(tok->pb->buf, &numuint64) == 0)
  868. {
  869. if (numuint64 && tok->pb->buf[0] == '0' &&
  870. (tok->flags & JSON_TOKENER_STRICT))
  871. {
  872. tok->err = json_tokener_error_parse_number;
  873. goto out;
  874. }
  875. if (numuint64 <= INT64_MAX)
  876. {
  877. num64 = (uint64_t)numuint64;
  878. current = json_object_new_int64(num64);
  879. if (current == NULL)
  880. goto out;
  881. }
  882. else
  883. {
  884. current = json_object_new_uint64(numuint64);
  885. if (current == NULL)
  886. goto out;
  887. }
  888. }
  889. else if (tok->is_double &&
  890. json_parse_double(tok->pb->buf, &numd) == 0)
  891. {
  892. current = json_object_new_double_s(numd, tok->pb->buf);
  893. if (current == NULL)
  894. goto out;
  895. }
  896. else
  897. {
  898. tok->err = json_tokener_error_parse_number;
  899. goto out;
  900. }
  901. saved_state = json_tokener_state_finish;
  902. state = json_tokener_state_eatws;
  903. goto redo_char;
  904. }
  905. break;
  906. case json_tokener_state_array_after_sep:
  907. case json_tokener_state_array:
  908. if (c == ']')
  909. {
  910. if (state == json_tokener_state_array_after_sep &&
  911. (tok->flags & JSON_TOKENER_STRICT))
  912. {
  913. tok->err = json_tokener_error_parse_unexpected;
  914. goto out;
  915. }
  916. saved_state = json_tokener_state_finish;
  917. state = json_tokener_state_eatws;
  918. }
  919. else
  920. {
  921. if (tok->depth >= tok->max_depth - 1)
  922. {
  923. tok->err = json_tokener_error_depth;
  924. goto out;
  925. }
  926. state = json_tokener_state_array_add;
  927. tok->depth++;
  928. json_tokener_reset_level(tok, tok->depth);
  929. goto redo_char;
  930. }
  931. break;
  932. case json_tokener_state_array_add:
  933. if (json_object_array_add(current, obj) != 0)
  934. goto out;
  935. saved_state = json_tokener_state_array_sep;
  936. state = json_tokener_state_eatws;
  937. goto redo_char;
  938. case json_tokener_state_array_sep:
  939. if (c == ']')
  940. {
  941. saved_state = json_tokener_state_finish;
  942. state = json_tokener_state_eatws;
  943. }
  944. else if (c == ',')
  945. {
  946. saved_state = json_tokener_state_array_after_sep;
  947. state = json_tokener_state_eatws;
  948. }
  949. else
  950. {
  951. tok->err = json_tokener_error_parse_array;
  952. goto out;
  953. }
  954. break;
  955. case json_tokener_state_object_field_start:
  956. case json_tokener_state_object_field_start_after_sep:
  957. if (c == '}')
  958. {
  959. if (state == json_tokener_state_object_field_start_after_sep &&
  960. (tok->flags & JSON_TOKENER_STRICT))
  961. {
  962. tok->err = json_tokener_error_parse_unexpected;
  963. goto out;
  964. }
  965. saved_state = json_tokener_state_finish;
  966. state = json_tokener_state_eatws;
  967. }
  968. else if (c == '"' || c == '\'')
  969. {
  970. tok->quote_char = c;
  971. printbuf_reset(tok->pb);
  972. state = json_tokener_state_object_field;
  973. }
  974. else
  975. {
  976. tok->err = json_tokener_error_parse_object_key_name;
  977. goto out;
  978. }
  979. break;
  980. case json_tokener_state_object_field:
  981. {
  982. /* Advance until we change state */
  983. const char *case_start = str;
  984. while (1)
  985. {
  986. if (c == tok->quote_char)
  987. {
  988. printbuf_memappend_fast(tok->pb, case_start,
  989. str - case_start);
  990. obj_field_name = strdup(tok->pb->buf);
  991. saved_state = json_tokener_state_object_field_end;
  992. state = json_tokener_state_eatws;
  993. break;
  994. }
  995. else if (c == '\\')
  996. {
  997. printbuf_memappend_fast(tok->pb, case_start,
  998. str - case_start);
  999. saved_state = json_tokener_state_object_field;
  1000. state = json_tokener_state_string_escape;
  1001. break;
  1002. }
  1003. if (!ADVANCE_CHAR(str, tok) || !PEEK_CHAR(c, tok))
  1004. {
  1005. printbuf_memappend_fast(tok->pb, case_start,
  1006. str - case_start);
  1007. goto out;
  1008. }
  1009. }
  1010. }
  1011. break;
  1012. case json_tokener_state_object_field_end:
  1013. if (c == ':')
  1014. {
  1015. saved_state = json_tokener_state_object_value;
  1016. state = json_tokener_state_eatws;
  1017. }
  1018. else
  1019. {
  1020. tok->err = json_tokener_error_parse_object_key_sep;
  1021. goto out;
  1022. }
  1023. break;
  1024. case json_tokener_state_object_value:
  1025. if (tok->depth >= tok->max_depth - 1)
  1026. {
  1027. tok->err = json_tokener_error_depth;
  1028. goto out;
  1029. }
  1030. state = json_tokener_state_object_value_add;
  1031. tok->depth++;
  1032. json_tokener_reset_level(tok, tok->depth);
  1033. goto redo_char;
  1034. case json_tokener_state_object_value_add:
  1035. json_object_object_add(current, obj_field_name, obj);
  1036. free(obj_field_name);
  1037. obj_field_name = NULL;
  1038. saved_state = json_tokener_state_object_sep;
  1039. state = json_tokener_state_eatws;
  1040. goto redo_char;
  1041. case json_tokener_state_object_sep:
  1042. /* { */
  1043. if (c == '}')
  1044. {
  1045. saved_state = json_tokener_state_finish;
  1046. state = json_tokener_state_eatws;
  1047. }
  1048. else if (c == ',')
  1049. {
  1050. saved_state = json_tokener_state_object_field_start_after_sep;
  1051. state = json_tokener_state_eatws;
  1052. }
  1053. else
  1054. {
  1055. tok->err = json_tokener_error_parse_object_value_sep;
  1056. goto out;
  1057. }
  1058. break;
  1059. }
  1060. if (!ADVANCE_CHAR(str, tok))
  1061. goto out;
  1062. } /* while(PEEK_CHAR) */
  1063. out:
  1064. if ((tok->flags & JSON_TOKENER_VALIDATE_UTF8) && (nBytes != 0))
  1065. {
  1066. tok->err = json_tokener_error_parse_utf8_string;
  1067. }
  1068. if (c && (state == json_tokener_state_finish) && (tok->depth == 0) &&
  1069. (tok->flags & (JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS)) == JSON_TOKENER_STRICT)
  1070. {
  1071. /* unexpected char after JSON data */
  1072. tok->err = json_tokener_error_parse_unexpected;
  1073. }
  1074. if (!c)
  1075. {
  1076. /* We hit an eof char (0) */
  1077. if (state != json_tokener_state_finish && saved_state != json_tokener_state_finish)
  1078. tok->err = json_tokener_error_parse_eof;
  1079. }
  1080. #ifdef HAVE_USELOCALE
  1081. uselocale(oldlocale);
  1082. freelocale(newloc);
  1083. #elif defined(HAVE_SETLOCALE)
  1084. setlocale(LC_NUMERIC, oldlocale);
  1085. free(oldlocale);
  1086. #endif
  1087. if (tok->err == json_tokener_success)
  1088. {
  1089. json_object *ret = json_object_get(current);
  1090. int ii;
  1091. /* Partially reset, so we parse additional objects on subsequent calls. */
  1092. for (ii = tok->depth; ii >= 0; ii--)
  1093. json_tokener_reset_level(tok, ii);
  1094. return ret;
  1095. }
  1096. MC_DEBUG("json_tokener_parse_ex: error %s at offset %d\n", json_tokener_errors[tok->err],
  1097. tok->char_offset);
  1098. return NULL;
  1099. }
  1100. static json_bool json_tokener_validate_utf8(const char c, unsigned int *nBytes)
  1101. {
  1102. unsigned char chr = c;
  1103. if (*nBytes == 0)
  1104. {
  1105. if (chr >= 0x80)
  1106. {
  1107. if ((chr & 0xe0) == 0xc0)
  1108. *nBytes = 1;
  1109. else if ((chr & 0xf0) == 0xe0)
  1110. *nBytes = 2;
  1111. else if ((chr & 0xf8) == 0xf0)
  1112. *nBytes = 3;
  1113. else
  1114. return 0;
  1115. }
  1116. }
  1117. else
  1118. {
  1119. if ((chr & 0xC0) != 0x80)
  1120. return 0;
  1121. (*nBytes)--;
  1122. }
  1123. return 1;
  1124. }
  1125. void json_tokener_set_flags(struct json_tokener *tok, int flags)
  1126. {
  1127. tok->flags = flags;
  1128. }
  1129. size_t json_tokener_get_parse_end(struct json_tokener *tok)
  1130. {
  1131. assert(tok->char_offset >= 0); /* Drop this line when char_offset becomes a size_t */
  1132. return (size_t)tok->char_offset;
  1133. }