json_tokener.c 29 KB

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