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

1165 lines
36 KiB

  1. /*
  2. Copyright (c) 2005 JSON.org
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. The Software shall be used for Good, not Evil.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. /*
  21. Callbacks, comments, Unicode handling by Jean Gressmann (jean@0x42.de), 2007-2010.
  22. Changelog:
  23. 2010-11-25
  24. Support for custom memory allocation (sgbeal@googlemail.com).
  25. 2010-05-07
  26. Added error handling for memory allocation failure (sgbeal@googlemail.com).
  27. Added diagnosis errors for invalid JSON.
  28. 2010-03-25
  29. Fixed buffer overrun in grow_parse_buffer & cleaned up code.
  30. 2009-10-19
  31. Replaced long double in JSON_value_struct with double after reports
  32. of strtold being broken on some platforms (charles@transmissionbt.com).
  33. 2009-05-17
  34. Incorporated benrudiak@googlemail.com fix for UTF16 decoding.
  35. 2009-05-14
  36. Fixed float parsing bug related to a locale being set that didn't
  37. use '.' as decimal point character (charles@transmissionbt.com).
  38. 2008-10-14
  39. Renamed states.IN to states.IT to avoid name clash which IN macro
  40. defined in windef.h (alexey.pelykh@gmail.com)
  41. 2008-07-19
  42. Removed some duplicate code & debugging variable (charles@transmissionbt.com)
  43. 2008-05-28
  44. Made JSON_value structure ansi C compliant. This bug was report by
  45. trisk@acm.jhu.edu
  46. 2008-05-20
  47. Fixed bug reported by charles@transmissionbt.com where the switching
  48. from static to dynamic parse buffer did not copy the static parse
  49. buffer's content.
  50. */
  51. #include <assert.h>
  52. #include <ctype.h>
  53. #include <float.h>
  54. #include <stddef.h>
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <locale.h>
  59. #include "JSON_parser.h"
  60. #ifdef _MSC_VER
  61. # if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
  62. # pragma warning(disable:4996) /* unsecure sscanf */
  63. # pragma warning(disable:4127) /* conditional expression is constant */
  64. # endif
  65. #endif
  66. #define true 1
  67. #define false 0
  68. #define __ -1 /* the universal error code */
  69. /* values chosen so that the object size is approx equal to one page (4K) */
  70. #ifndef JSON_PARSER_STACK_SIZE
  71. # define JSON_PARSER_STACK_SIZE 128
  72. #endif
  73. #ifndef JSON_PARSER_PARSE_BUFFER_SIZE
  74. # define JSON_PARSER_PARSE_BUFFER_SIZE 3500
  75. #endif
  76. typedef void* (*JSON_debug_malloc_t)(size_t bytes, const char* reason);
  77. #ifdef JSON_PARSER_DEBUG_MALLOC
  78. # define JSON_parser_malloc(func, bytes, reason) ((JSON_debug_malloc_t)func)(bytes, reason)
  79. #else
  80. # undef JSON_parser_malloc
  81. # define JSON_parser_malloc(func, bytes, reason) func(bytes)
  82. #endif
  83. typedef unsigned short UTF16;
  84. struct JSON_parser_struct {
  85. JSON_parser_callback callback;
  86. void* ctx;
  87. signed char state, before_comment_state, type, escaped, comment, allow_comments, handle_floats_manually, error;
  88. char decimal_point;
  89. UTF16 utf16_high_surrogate;
  90. int current_char;
  91. int depth;
  92. int top;
  93. int stack_capacity;
  94. signed char* stack;
  95. char* parse_buffer;
  96. size_t parse_buffer_capacity;
  97. size_t parse_buffer_count;
  98. signed char static_stack[JSON_PARSER_STACK_SIZE];
  99. char static_parse_buffer[JSON_PARSER_PARSE_BUFFER_SIZE];
  100. JSON_malloc_t malloc;
  101. JSON_free_t free;
  102. };
  103. #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
  104. /*
  105. Characters are mapped into these character classes. This allows for
  106. a significant reduction in the size of the state transition table.
  107. */
  108. enum classes {
  109. C_SPACE, /* space */
  110. C_WHITE, /* other whitespace */
  111. C_LCURB, /* { */
  112. C_RCURB, /* } */
  113. C_LSQRB, /* [ */
  114. C_RSQRB, /* ] */
  115. C_COLON, /* : */
  116. C_COMMA, /* , */
  117. C_QUOTE, /* " */
  118. C_BACKS, /* \ */
  119. C_SLASH, /* / */
  120. C_PLUS, /* + */
  121. C_MINUS, /* - */
  122. C_POINT, /* . */
  123. C_ZERO , /* 0 */
  124. C_DIGIT, /* 123456789 */
  125. C_LOW_A, /* a */
  126. C_LOW_B, /* b */
  127. C_LOW_C, /* c */
  128. C_LOW_D, /* d */
  129. C_LOW_E, /* e */
  130. C_LOW_F, /* f */
  131. C_LOW_L, /* l */
  132. C_LOW_N, /* n */
  133. C_LOW_R, /* r */
  134. C_LOW_S, /* s */
  135. C_LOW_T, /* t */
  136. C_LOW_U, /* u */
  137. C_ABCDF, /* ABCDF */
  138. C_E, /* E */
  139. C_ETC, /* everything else */
  140. C_STAR, /* * */
  141. NR_CLASSES
  142. };
  143. static const signed char ascii_class[128] = {
  144. /*
  145. This array maps the 128 ASCII characters into character classes.
  146. The remaining Unicode characters should be mapped to C_ETC.
  147. Non-whitespace control characters are errors.
  148. */
  149. __, __, __, __, __, __, __, __,
  150. __, C_WHITE, C_WHITE, __, __, C_WHITE, __, __,
  151. __, __, __, __, __, __, __, __,
  152. __, __, __, __, __, __, __, __,
  153. C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
  154. C_ETC, C_ETC, C_STAR, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,
  155. C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
  156. C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
  157. C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC,
  158. C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
  159. C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC,
  160. C_ETC, C_ETC, C_ETC, C_LSQRB, C_BACKS, C_RSQRB, C_ETC, C_ETC,
  161. C_ETC, C_LOW_A, C_LOW_B, C_LOW_C, C_LOW_D, C_LOW_E, C_LOW_F, C_ETC,
  162. C_ETC, C_ETC, C_ETC, C_ETC, C_LOW_L, C_ETC, C_LOW_N, C_ETC,
  163. C_ETC, C_ETC, C_LOW_R, C_LOW_S, C_LOW_T, C_LOW_U, C_ETC, C_ETC,
  164. C_ETC, C_ETC, C_ETC, C_LCURB, C_ETC, C_RCURB, C_ETC, C_ETC
  165. };
  166. /*
  167. The state codes.
  168. */
  169. enum states {
  170. GO, /* start */
  171. OK, /* ok */
  172. OB, /* object */
  173. KE, /* key */
  174. CO, /* colon */
  175. VA, /* value */
  176. AR, /* array */
  177. ST, /* string */
  178. ES, /* escape */
  179. U1, /* u1 */
  180. U2, /* u2 */
  181. U3, /* u3 */
  182. U4, /* u4 */
  183. MI, /* minus */
  184. ZE, /* zero */
  185. IT, /* integer */
  186. FR, /* fraction */
  187. E1, /* e */
  188. E2, /* ex */
  189. E3, /* exp */
  190. T1, /* tr */
  191. T2, /* tru */
  192. T3, /* true */
  193. F1, /* fa */
  194. F2, /* fal */
  195. F3, /* fals */
  196. F4, /* false */
  197. N1, /* nu */
  198. N2, /* nul */
  199. N3, /* null */
  200. C1, /* / */
  201. C2, /* / * */
  202. C3, /* * */
  203. FX, /* *.* *eE* */
  204. D1, /* second UTF-16 character decoding started by \ */
  205. D2, /* second UTF-16 character proceeded by u */
  206. NR_STATES
  207. };
  208. enum actions
  209. {
  210. CB = -10, /* comment begin */
  211. CE = -11, /* comment end */
  212. FA = -12, /* false */
  213. TR = -13, /* false */
  214. NU = -14, /* null */
  215. DE = -15, /* double detected by exponent e E */
  216. DF = -16, /* double detected by fraction . */
  217. SB = -17, /* string begin */
  218. MX = -18, /* integer detected by minus */
  219. ZX = -19, /* integer detected by zero */
  220. IX = -20, /* integer detected by 1-9 */
  221. EX = -21, /* next char is escaped */
  222. UC = -22 /* Unicode character read */
  223. };
  224. static const signed char state_transition_table[NR_STATES][NR_CLASSES] = {
  225. /*
  226. The state transition table takes the current state and the current symbol,
  227. and returns either a new state or an action. An action is represented as a
  228. negative number. A JSON text is accepted if at the end of the text the
  229. state is OK and if the mode is MODE_DONE.
  230. white 1-9 ABCDF etc
  231. space | { } [ ] : , " \ / + - . 0 | a b c d e f l n r s t u | E | * */
  232. /*start GO*/ {GO,GO,-6,__,-5,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  233. /*ok OK*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  234. /*object OB*/ {OB,OB,__,-9,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  235. /*key KE*/ {KE,KE,__,__,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  236. /*colon CO*/ {CO,CO,__,__,__,__,-2,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  237. /*value VA*/ {VA,VA,-6,__,-5,__,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
  238. /*array AR*/ {AR,AR,-6,__,-5,-7,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
  239. /*string ST*/ {ST,__,ST,ST,ST,ST,ST,ST,-4,EX,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST},
  240. /*escape ES*/ {__,__,__,__,__,__,__,__,ST,ST,ST,__,__,__,__,__,__,ST,__,__,__,ST,__,ST,ST,__,ST,U1,__,__,__,__},
  241. /*u1 U1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U2,U2,U2,U2,U2,U2,U2,U2,__,__,__,__,__,__,U2,U2,__,__},
  242. /*u2 U2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U3,U3,U3,U3,U3,U3,U3,U3,__,__,__,__,__,__,U3,U3,__,__},
  243. /*u3 U3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U4,U4,U4,U4,U4,U4,U4,U4,__,__,__,__,__,__,U4,U4,__,__},
  244. /*u4 U4*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,UC,UC,UC,UC,UC,UC,UC,UC,__,__,__,__,__,__,UC,UC,__,__},
  245. /*minus MI*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,ZE,IT,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  246. /*zero ZE*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  247. /*int IT*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,IT,IT,__,__,__,__,DE,__,__,__,__,__,__,__,__,DE,__,__},
  248. /*frac FR*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
  249. /*e E1*/ {__,__,__,__,__,__,__,__,__,__,__,E2,E2,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  250. /*ex E2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  251. /*exp E3*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  252. /*tr T1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T2,__,__,__,__,__,__,__},
  253. /*tru T2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T3,__,__,__,__},
  254. /*true T3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
  255. /*fa F1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  256. /*fal F2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F3,__,__,__,__,__,__,__,__,__},
  257. /*fals F3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F4,__,__,__,__,__,__},
  258. /*false F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
  259. /*nu N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__},
  260. /*nul N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__},
  261. /*null N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__},
  262. /*/ C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2},
  263. /*/* C2*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3},
  264. /** C3*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,CE,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3},
  265. /*_. FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
  266. /*\ D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
  267. /*\ D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__},
  268. };
  269. /*
  270. These modes can be pushed on the stack.
  271. */
  272. enum modes {
  273. MODE_ARRAY = 1,
  274. MODE_DONE = 2,
  275. MODE_KEY = 3,
  276. MODE_OBJECT = 4
  277. };
  278. static void set_error(JSON_parser jc)
  279. {
  280. switch (jc->state) {
  281. case GO:
  282. switch (jc->current_char) {
  283. case '{': case '}': case '[': case ']':
  284. jc->error = JSON_E_UNBALANCED_COLLECTION;
  285. break;
  286. default:
  287. jc->error = JSON_E_INVALID_CHAR;
  288. break;
  289. }
  290. break;
  291. case OB:
  292. jc->error = JSON_E_EXPECTED_KEY;
  293. break;
  294. case AR:
  295. jc->error = JSON_E_UNBALANCED_COLLECTION;
  296. break;
  297. case CO:
  298. jc->error = JSON_E_EXPECTED_COLON;
  299. break;
  300. case KE:
  301. jc->error = JSON_E_EXPECTED_KEY;
  302. break;
  303. /* \uXXXX\uYYYY */
  304. case U1: case U2: case U3: case U4: case D1: case D2:
  305. jc->error = JSON_E_INVALID_UNICODE_SEQUENCE;
  306. break;
  307. /* true, false, null */
  308. case T1: case T2: case T3: case F1: case F2: case F3: case F4: case N1: case N2: case N3:
  309. jc->error = JSON_E_INVALID_KEYWORD;
  310. break;
  311. /* minus, integer, fraction, exponent */
  312. case MI: case ZE: case IT: case FR: case E1: case E2: case E3:
  313. jc->error = JSON_E_INVALID_NUMBER;
  314. break;
  315. default:
  316. jc->error = JSON_E_INVALID_CHAR;
  317. break;
  318. }
  319. }
  320. static int
  321. push(JSON_parser jc, int mode)
  322. {
  323. /*
  324. Push a mode onto the stack. Return false if there is overflow.
  325. */
  326. assert(jc->top <= jc->stack_capacity);
  327. if (jc->depth < 0) {
  328. if (jc->top == jc->stack_capacity) {
  329. const size_t bytes_to_copy = jc->stack_capacity * sizeof(jc->stack[0]);
  330. const size_t new_capacity = jc->stack_capacity * 2;
  331. const size_t bytes_to_allocate = new_capacity * sizeof(jc->stack[0]);
  332. void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "stack");
  333. if (!mem) {
  334. jc->error = JSON_E_OUT_OF_MEMORY;
  335. return false;
  336. }
  337. jc->stack_capacity = (int)new_capacity;
  338. memcpy(mem, jc->stack, bytes_to_copy);
  339. if (jc->stack != &jc->static_stack[0]) {
  340. jc->free(jc->stack);
  341. }
  342. jc->stack = (signed char*)mem;
  343. }
  344. } else {
  345. if (jc->top == jc->depth) {
  346. jc->error = JSON_E_NESTING_DEPTH_REACHED;
  347. return false;
  348. }
  349. }
  350. jc->stack[++jc->top] = (signed char)mode;
  351. return true;
  352. }
  353. static int
  354. pop(JSON_parser jc, int mode)
  355. {
  356. /*
  357. Pop the stack, assuring that the current mode matches the expectation.
  358. Return false if there is underflow or if the modes mismatch.
  359. */
  360. if (jc->top < 0 || jc->stack[jc->top] != mode) {
  361. return false;
  362. }
  363. jc->top -= 1;
  364. return true;
  365. }
  366. #define parse_buffer_clear(jc) \
  367. do {\
  368. jc->parse_buffer_count = 0;\
  369. jc->parse_buffer[0] = 0;\
  370. } while (0)
  371. #define parse_buffer_pop_back_char(jc)\
  372. do {\
  373. assert(jc->parse_buffer_count >= 1);\
  374. --jc->parse_buffer_count;\
  375. jc->parse_buffer[jc->parse_buffer_count] = 0;\
  376. } while (0)
  377. void delete_JSON_parser(JSON_parser jc)
  378. {
  379. if (jc) {
  380. if (jc->stack != &jc->static_stack[0]) {
  381. jc->free((void*)jc->stack);
  382. }
  383. if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
  384. jc->free((void*)jc->parse_buffer);
  385. }
  386. jc->free((void*)jc);
  387. }
  388. }
  389. int JSON_parser_reset(JSON_parser jc)
  390. {
  391. if (NULL == jc) {
  392. return false;
  393. }
  394. jc->state = GO;
  395. jc->top = -1;
  396. /* parser has been used previously? */
  397. if (NULL == jc->parse_buffer) {
  398. /* Do we want non-bound stack? */
  399. if (jc->depth > 0) {
  400. jc->stack_capacity = jc->depth;
  401. if (jc->depth <= (int)COUNTOF(jc->static_stack)) {
  402. jc->stack = &jc->static_stack[0];
  403. } else {
  404. const size_t bytes_to_alloc = jc->stack_capacity * sizeof(jc->stack[0]);
  405. jc->stack = (signed char*)JSON_parser_malloc(jc->malloc, bytes_to_alloc, "stack");
  406. if (jc->stack == NULL) {
  407. return false;
  408. }
  409. }
  410. } else {
  411. jc->stack_capacity = (int)COUNTOF(jc->static_stack);
  412. jc->depth = -1;
  413. jc->stack = &jc->static_stack[0];
  414. }
  415. /* set up the parse buffer */
  416. jc->parse_buffer = &jc->static_parse_buffer[0];
  417. jc->parse_buffer_capacity = COUNTOF(jc->static_parse_buffer);
  418. }
  419. /* set parser to start */
  420. push(jc, MODE_DONE);
  421. parse_buffer_clear(jc);
  422. return true;
  423. }
  424. JSON_parser
  425. new_JSON_parser(JSON_config const * config)
  426. {
  427. /*
  428. new_JSON_parser starts the checking process by constructing a JSON_parser
  429. object. It takes a depth parameter that restricts the level of maximum
  430. nesting.
  431. To continue the process, call JSON_parser_char for each character in the
  432. JSON text, and then call JSON_parser_done to obtain the final result.
  433. These functions are fully reentrant.
  434. */
  435. int use_std_malloc = false;
  436. JSON_config default_config;
  437. JSON_parser jc;
  438. JSON_malloc_t alloc;
  439. /* set to default configuration if none was provided */
  440. if (NULL == config) {
  441. /* initialize configuration */
  442. init_JSON_config(&default_config);
  443. config = &default_config;
  444. }
  445. /* use std malloc if either the allocator or deallocator function isn't set */
  446. use_std_malloc = NULL == config->malloc || NULL == config->free;
  447. alloc = use_std_malloc ? malloc : config->malloc;
  448. jc = (JSON_parser)JSON_parser_malloc(alloc, sizeof(*jc), "parser");
  449. if (NULL == jc) {
  450. return NULL;
  451. }
  452. /* configure the parser */
  453. memset(jc, 0, sizeof(*jc));
  454. jc->malloc = alloc;
  455. jc->free = use_std_malloc ? free : config->free;
  456. jc->callback = config->callback;
  457. jc->ctx = config->callback_ctx;
  458. jc->allow_comments = (signed char)(config->allow_comments != 0);
  459. jc->handle_floats_manually = (signed char)(config->handle_floats_manually != 0);
  460. jc->decimal_point = *localeconv()->decimal_point;
  461. /* We need to be able to push at least one object */
  462. jc->depth = config->depth == 0 ? 1 : config->depth;
  463. /* reset the parser */
  464. if (!JSON_parser_reset(jc)) {
  465. jc->free(jc);
  466. return NULL;
  467. }
  468. return jc;
  469. }
  470. static int parse_buffer_grow(JSON_parser jc)
  471. {
  472. const size_t bytes_to_copy = jc->parse_buffer_count * sizeof(jc->parse_buffer[0]);
  473. const size_t new_capacity = jc->parse_buffer_capacity * 2;
  474. const size_t bytes_to_allocate = new_capacity * sizeof(jc->parse_buffer[0]);
  475. void* mem = JSON_parser_malloc(jc->malloc, bytes_to_allocate, "parse buffer");
  476. if (mem == NULL) {
  477. jc->error = JSON_E_OUT_OF_MEMORY;
  478. return false;
  479. }
  480. assert(new_capacity > 0);
  481. memcpy(mem, jc->parse_buffer, bytes_to_copy);
  482. if (jc->parse_buffer != &jc->static_parse_buffer[0]) {
  483. jc->free(jc->parse_buffer);
  484. }
  485. jc->parse_buffer = (char*)mem;
  486. jc->parse_buffer_capacity = new_capacity;
  487. return true;
  488. }
  489. static int parse_buffer_reserve_for(JSON_parser jc, unsigned chars)
  490. {
  491. while (jc->parse_buffer_count + chars + 1 > jc->parse_buffer_capacity) {
  492. if (!parse_buffer_grow(jc)) {
  493. assert(jc->error == JSON_E_OUT_OF_MEMORY);
  494. return false;
  495. }
  496. }
  497. return true;
  498. }
  499. #define parse_buffer_has_space_for(jc, count) \
  500. (jc->parse_buffer_count + (count) + 1 <= jc->parse_buffer_capacity)
  501. #define parse_buffer_push_back_char(jc, c)\
  502. do {\
  503. assert(parse_buffer_has_space_for(jc, 1)); \
  504. jc->parse_buffer[jc->parse_buffer_count++] = c;\
  505. jc->parse_buffer[jc->parse_buffer_count] = 0;\
  506. } while (0)
  507. #define assert_is_non_container_type(jc) \
  508. assert( \
  509. jc->type == JSON_T_NULL || \
  510. jc->type == JSON_T_FALSE || \
  511. jc->type == JSON_T_TRUE || \
  512. jc->type == JSON_T_FLOAT || \
  513. jc->type == JSON_T_INTEGER || \
  514. jc->type == JSON_T_STRING)
  515. static int parse_parse_buffer(JSON_parser jc)
  516. {
  517. if (jc->callback) {
  518. JSON_value value, *arg = NULL;
  519. if (jc->type != JSON_T_NONE) {
  520. assert_is_non_container_type(jc);
  521. switch(jc->type) {
  522. case JSON_T_FLOAT:
  523. arg = &value;
  524. if (jc->handle_floats_manually) {
  525. value.vu.str.value = jc->parse_buffer;
  526. value.vu.str.length = jc->parse_buffer_count;
  527. } else {
  528. /* not checking with end pointer b/c there may be trailing ws */
  529. value.vu.float_value = strtod(jc->parse_buffer, NULL);
  530. }
  531. break;
  532. case JSON_T_INTEGER:
  533. arg = &value;
  534. sscanf(jc->parse_buffer, JSON_PARSER_INTEGER_SSCANF_TOKEN, &value.vu.integer_value);
  535. break;
  536. case JSON_T_STRING:
  537. arg = &value;
  538. value.vu.str.value = jc->parse_buffer;
  539. value.vu.str.length = jc->parse_buffer_count;
  540. break;
  541. }
  542. if (!(*jc->callback)(jc->ctx, jc->type, arg)) {
  543. return false;
  544. }
  545. }
  546. }
  547. parse_buffer_clear(jc);
  548. return true;
  549. }
  550. #define IS_HIGH_SURROGATE(uc) (((uc) & 0xFC00) == 0xD800)
  551. #define IS_LOW_SURROGATE(uc) (((uc) & 0xFC00) == 0xDC00)
  552. #define DECODE_SURROGATE_PAIR(hi,lo) ((((hi) & 0x3FF) << 10) + ((lo) & 0x3FF) + 0x10000)
  553. static const unsigned char utf8_lead_bits[4] = { 0x00, 0xC0, 0xE0, 0xF0 };
  554. static int decode_unicode_char(JSON_parser jc)
  555. {
  556. int i;
  557. unsigned uc = 0;
  558. char* p;
  559. int trail_bytes;
  560. assert(jc->parse_buffer_count >= 6);
  561. p = &jc->parse_buffer[jc->parse_buffer_count - 4];
  562. for (i = 12; i >= 0; i -= 4, ++p) {
  563. unsigned x = *p;
  564. if (x >= 'a') {
  565. x -= ('a' - 10);
  566. } else if (x >= 'A') {
  567. x -= ('A' - 10);
  568. } else {
  569. x &= ~0x30u;
  570. }
  571. assert(x < 16);
  572. uc |= x << i;
  573. }
  574. /* clear UTF-16 char from buffer */
  575. jc->parse_buffer_count -= 6;
  576. jc->parse_buffer[jc->parse_buffer_count] = 0;
  577. /* attempt decoding ... */
  578. if (jc->utf16_high_surrogate) {
  579. if (IS_LOW_SURROGATE(uc)) {
  580. uc = DECODE_SURROGATE_PAIR(jc->utf16_high_surrogate, uc);
  581. trail_bytes = 3;
  582. jc->utf16_high_surrogate = 0;
  583. } else {
  584. /* high surrogate without a following low surrogate */
  585. return false;
  586. }
  587. } else {
  588. if (uc < 0x80) {
  589. trail_bytes = 0;
  590. } else if (uc < 0x800) {
  591. trail_bytes = 1;
  592. } else if (IS_HIGH_SURROGATE(uc)) {
  593. /* save the high surrogate and wait for the low surrogate */
  594. jc->utf16_high_surrogate = (UTF16)uc;
  595. return true;
  596. } else if (IS_LOW_SURROGATE(uc)) {
  597. /* low surrogate without a preceding high surrogate */
  598. return false;
  599. } else {
  600. trail_bytes = 2;
  601. }
  602. }
  603. jc->parse_buffer[jc->parse_buffer_count++] = (char) ((uc >> (trail_bytes * 6)) | utf8_lead_bits[trail_bytes]);
  604. for (i = trail_bytes * 6 - 6; i >= 0; i -= 6) {
  605. jc->parse_buffer[jc->parse_buffer_count++] = (char) (((uc >> i) & 0x3F) | 0x80);
  606. }
  607. jc->parse_buffer[jc->parse_buffer_count] = 0;
  608. return true;
  609. }
  610. static int add_escaped_char_to_parse_buffer(JSON_parser jc, int next_char)
  611. {
  612. assert(parse_buffer_has_space_for(jc, 1));
  613. jc->escaped = 0;
  614. /* remove the backslash */
  615. parse_buffer_pop_back_char(jc);
  616. switch(next_char) {
  617. case 'b':
  618. parse_buffer_push_back_char(jc, '\b');
  619. break;
  620. case 'f':
  621. parse_buffer_push_back_char(jc, '\f');
  622. break;
  623. case 'n':
  624. parse_buffer_push_back_char(jc, '\n');
  625. break;
  626. case 'r':
  627. parse_buffer_push_back_char(jc, '\r');
  628. break;
  629. case 't':
  630. parse_buffer_push_back_char(jc, '\t');
  631. break;
  632. case '"':
  633. parse_buffer_push_back_char(jc, '"');
  634. break;
  635. case '\\':
  636. parse_buffer_push_back_char(jc, '\\');
  637. break;
  638. case '/':
  639. parse_buffer_push_back_char(jc, '/');
  640. break;
  641. case 'u':
  642. parse_buffer_push_back_char(jc, '\\');
  643. parse_buffer_push_back_char(jc, 'u');
  644. break;
  645. default:
  646. return false;
  647. }
  648. return true;
  649. }
  650. static int add_char_to_parse_buffer(JSON_parser jc, int next_char, int next_class)
  651. {
  652. if (!parse_buffer_reserve_for(jc, 1)) {
  653. assert(JSON_E_OUT_OF_MEMORY == jc->error);
  654. return false;
  655. }
  656. if (jc->escaped) {
  657. if (!add_escaped_char_to_parse_buffer(jc, next_char)) {
  658. jc->error = JSON_E_INVALID_ESCAPE_SEQUENCE;
  659. return false;
  660. }
  661. } else if (!jc->comment) {
  662. if ((jc->type != JSON_T_NONE) | !((next_class == C_SPACE) | (next_class == C_WHITE)) /* non-white-space */) {
  663. parse_buffer_push_back_char(jc, (char)next_char);
  664. }
  665. }
  666. return true;
  667. }
  668. #define assert_type_isnt_string_null_or_bool(jc) \
  669. assert(jc->type != JSON_T_FALSE); \
  670. assert(jc->type != JSON_T_TRUE); \
  671. assert(jc->type != JSON_T_NULL); \
  672. assert(jc->type != JSON_T_STRING)
  673. int
  674. JSON_parser_char(JSON_parser jc, int next_char)
  675. {
  676. /*
  677. After calling new_JSON_parser, call this function for each character (or
  678. partial character) in your JSON text. It can accept UTF-8, UTF-16, or
  679. UTF-32. It returns true if things are looking ok so far. If it rejects the
  680. text, it returns false.
  681. */
  682. int next_class, next_state;
  683. /*
  684. Store the current char for error handling
  685. */
  686. jc->current_char = next_char;
  687. /*
  688. Determine the character's class.
  689. */
  690. if (next_char < 0) {
  691. jc->error = JSON_E_INVALID_CHAR;
  692. return false;
  693. }
  694. if (next_char >= 128) {
  695. next_class = C_ETC;
  696. } else {
  697. next_class = ascii_class[next_char];
  698. if (next_class <= __) {
  699. set_error(jc);
  700. return false;
  701. }
  702. }
  703. if (!add_char_to_parse_buffer(jc, next_char, next_class)) {
  704. return false;
  705. }
  706. /*
  707. Get the next state from the state transition table.
  708. */
  709. next_state = state_transition_table[jc->state][next_class];
  710. if (next_state >= 0) {
  711. /*
  712. Change the state.
  713. */
  714. jc->state = (signed char)next_state;
  715. } else {
  716. /*
  717. Or perform one of the actions.
  718. */
  719. switch (next_state) {
  720. /* Unicode character */
  721. case UC:
  722. if(!decode_unicode_char(jc)) {
  723. jc->error = JSON_E_INVALID_UNICODE_SEQUENCE;
  724. return false;
  725. }
  726. /* check if we need to read a second UTF-16 char */
  727. if (jc->utf16_high_surrogate) {
  728. jc->state = D1;
  729. } else {
  730. jc->state = ST;
  731. }
  732. break;
  733. /* escaped char */
  734. case EX:
  735. jc->escaped = 1;
  736. jc->state = ES;
  737. break;
  738. /* integer detected by minus */
  739. case MX:
  740. jc->type = JSON_T_INTEGER;
  741. jc->state = MI;
  742. break;
  743. /* integer detected by zero */
  744. case ZX:
  745. jc->type = JSON_T_INTEGER;
  746. jc->state = ZE;
  747. break;
  748. /* integer detected by 1-9 */
  749. case IX:
  750. jc->type = JSON_T_INTEGER;
  751. jc->state = IT;
  752. break;
  753. /* floating point number detected by exponent*/
  754. case DE:
  755. assert_type_isnt_string_null_or_bool(jc);
  756. jc->type = JSON_T_FLOAT;
  757. jc->state = E1;
  758. break;
  759. /* floating point number detected by fraction */
  760. case DF:
  761. assert_type_isnt_string_null_or_bool(jc);
  762. if (!jc->handle_floats_manually) {
  763. /*
  764. Some versions of strtod (which underlies sscanf) don't support converting
  765. C-locale formated floating point values.
  766. */
  767. assert(jc->parse_buffer[jc->parse_buffer_count-1] == '.');
  768. jc->parse_buffer[jc->parse_buffer_count-1] = jc->decimal_point;
  769. }
  770. jc->type = JSON_T_FLOAT;
  771. jc->state = FX;
  772. break;
  773. /* string begin " */
  774. case SB:
  775. parse_buffer_clear(jc);
  776. assert(jc->type == JSON_T_NONE);
  777. jc->type = JSON_T_STRING;
  778. jc->state = ST;
  779. break;
  780. /* n */
  781. case NU:
  782. assert(jc->type == JSON_T_NONE);
  783. jc->type = JSON_T_NULL;
  784. jc->state = N1;
  785. break;
  786. /* f */
  787. case FA:
  788. assert(jc->type == JSON_T_NONE);
  789. jc->type = JSON_T_FALSE;
  790. jc->state = F1;
  791. break;
  792. /* t */
  793. case TR:
  794. assert(jc->type == JSON_T_NONE);
  795. jc->type = JSON_T_TRUE;
  796. jc->state = T1;
  797. break;
  798. /* closing comment */
  799. case CE:
  800. jc->comment = 0;
  801. assert(jc->parse_buffer_count == 0);
  802. assert(jc->type == JSON_T_NONE);
  803. jc->state = jc->before_comment_state;
  804. break;
  805. /* opening comment */
  806. case CB:
  807. if (!jc->allow_comments) {
  808. return false;
  809. }
  810. parse_buffer_pop_back_char(jc);
  811. if (!parse_parse_buffer(jc)) {
  812. return false;
  813. }
  814. assert(jc->parse_buffer_count == 0);
  815. assert(jc->type != JSON_T_STRING);
  816. switch (jc->stack[jc->top]) {
  817. case MODE_ARRAY:
  818. case MODE_OBJECT:
  819. switch(jc->state) {
  820. case VA:
  821. case AR:
  822. jc->before_comment_state = jc->state;
  823. break;
  824. default:
  825. jc->before_comment_state = OK;
  826. break;
  827. }
  828. break;
  829. default:
  830. jc->before_comment_state = jc->state;
  831. break;
  832. }
  833. jc->type = JSON_T_NONE;
  834. jc->state = C1;
  835. jc->comment = 1;
  836. break;
  837. /* empty } */
  838. case -9:
  839. parse_buffer_clear(jc);
  840. if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
  841. return false;
  842. }
  843. if (!pop(jc, MODE_KEY)) {
  844. return false;
  845. }
  846. jc->state = OK;
  847. break;
  848. /* } */ case -8:
  849. parse_buffer_pop_back_char(jc);
  850. if (!parse_parse_buffer(jc)) {
  851. return false;
  852. }
  853. if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_END, NULL)) {
  854. return false;
  855. }
  856. if (!pop(jc, MODE_OBJECT)) {
  857. jc->error = JSON_E_UNBALANCED_COLLECTION;
  858. return false;
  859. }
  860. jc->type = JSON_T_NONE;
  861. jc->state = OK;
  862. break;
  863. /* ] */ case -7:
  864. parse_buffer_pop_back_char(jc);
  865. if (!parse_parse_buffer(jc)) {
  866. return false;
  867. }
  868. if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_ARRAY_END, NULL)) {
  869. return false;
  870. }
  871. if (!pop(jc, MODE_ARRAY)) {
  872. jc->error = JSON_E_UNBALANCED_COLLECTION;
  873. return false;
  874. }
  875. jc->type = JSON_T_NONE;
  876. jc->state = OK;
  877. break;
  878. /* { */ case -6:
  879. parse_buffer_pop_back_char(jc);
  880. if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_OBJECT_BEGIN, NULL)) {
  881. return false;
  882. }
  883. if (!push(jc, MODE_KEY)) {
  884. return false;
  885. }
  886. assert(jc->type == JSON_T_NONE);
  887. jc->state = OB;
  888. break;
  889. /* [ */ case -5:
  890. parse_buffer_pop_back_char(jc);
  891. if (jc->callback && !(*jc->callback)(jc->ctx, JSON_T_ARRAY_BEGIN, NULL)) {
  892. return false;
  893. }
  894. if (!push(jc, MODE_ARRAY)) {
  895. return false;
  896. }
  897. assert(jc->type == JSON_T_NONE);
  898. jc->state = AR;
  899. break;
  900. /* string end " */ case -4:
  901. parse_buffer_pop_back_char(jc);
  902. switch (jc->stack[jc->top]) {
  903. case MODE_KEY:
  904. assert(jc->type == JSON_T_STRING);
  905. jc->type = JSON_T_NONE;
  906. jc->state = CO;
  907. if (jc->callback) {
  908. JSON_value value;
  909. value.vu.str.value = jc->parse_buffer;
  910. value.vu.str.length = jc->parse_buffer_count;
  911. if (!(*jc->callback)(jc->ctx, JSON_T_KEY, &value)) {
  912. return false;
  913. }
  914. }
  915. parse_buffer_clear(jc);
  916. break;
  917. case MODE_ARRAY:
  918. case MODE_OBJECT:
  919. assert(jc->type == JSON_T_STRING);
  920. if (!parse_parse_buffer(jc)) {
  921. return false;
  922. }
  923. jc->type = JSON_T_NONE;
  924. jc->state = OK;
  925. break;
  926. default:
  927. return false;
  928. }
  929. break;
  930. /* , */ case -3:
  931. parse_buffer_pop_back_char(jc);
  932. if (!parse_parse_buffer(jc)) {
  933. return false;
  934. }
  935. switch (jc->stack[jc->top]) {
  936. case MODE_OBJECT:
  937. /*
  938. A comma causes a flip from object mode to key mode.
  939. */
  940. if (!pop(jc, MODE_OBJECT) || !push(jc, MODE_KEY)) {
  941. return false;
  942. }
  943. assert(jc->type != JSON_T_STRING);
  944. jc->type = JSON_T_NONE;
  945. jc->state = KE;
  946. break;
  947. case MODE_ARRAY:
  948. assert(jc->type != JSON_T_STRING);
  949. jc->type = JSON_T_NONE;
  950. jc->state = VA;
  951. break;
  952. default:
  953. return false;
  954. }
  955. break;
  956. /* : */ case -2:
  957. /*
  958. A colon causes a flip from key mode to object mode.
  959. */
  960. parse_buffer_pop_back_char(jc);
  961. if (!pop(jc, MODE_KEY) || !push(jc, MODE_OBJECT)) {
  962. return false;
  963. }
  964. assert(jc->type == JSON_T_NONE);
  965. jc->state = VA;
  966. break;
  967. /*
  968. Bad action.
  969. */
  970. default:
  971. set_error(jc);
  972. return false;
  973. }
  974. }
  975. return true;
  976. }
  977. int
  978. JSON_parser_done(JSON_parser jc)
  979. {
  980. if ((jc->state == OK || jc->state == GO) && pop(jc, MODE_DONE))
  981. {
  982. return true;
  983. }
  984. jc->error = JSON_E_UNBALANCED_COLLECTION;
  985. return false;
  986. }
  987. int JSON_parser_is_legal_white_space_string(const char* s)
  988. {
  989. int c, char_class;
  990. if (s == NULL) {
  991. return false;
  992. }
  993. for (; *s; ++s) {
  994. c = *s;
  995. if (c < 0 || c >= 128) {
  996. return false;
  997. }
  998. char_class = ascii_class[c];
  999. if (char_class != C_SPACE && char_class != C_WHITE) {
  1000. return false;
  1001. }
  1002. }
  1003. return true;
  1004. }
  1005. int JSON_parser_get_last_error(JSON_parser jc)
  1006. {
  1007. return jc->error;
  1008. }
  1009. void init_JSON_config(JSON_config* config)
  1010. {
  1011. if (config) {
  1012. memset(config, 0, sizeof(*config));
  1013. config->depth = JSON_PARSER_STACK_SIZE - 1;
  1014. config->malloc = malloc;
  1015. config->free = free;
  1016. }
  1017. }