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 29 kB

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