lejp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <libwebsockets.h>
  25. #include "private-lib-core.h"
  26. #include <string.h>
  27. #include <stdio.h>
  28. static const char * const parser_errs[] = {
  29. "",
  30. "",
  31. "No opening '{'",
  32. "Expected closing '}'",
  33. "Expected '\"'",
  34. "String underrun",
  35. "Illegal unescaped control char",
  36. "Illegal escape format",
  37. "Illegal hex number",
  38. "Expected ':'",
  39. "Illegal value start",
  40. "Digit required after decimal point",
  41. "Bad number format",
  42. "Bad exponent format",
  43. "Unknown token",
  44. "Too many ']'",
  45. "Mismatched ']'",
  46. "Expected ']'",
  47. "JSON nesting limit exceeded",
  48. "Nesting tracking used up",
  49. "Number too long",
  50. "Comma or block end expected",
  51. "Unknown",
  52. "Parser callback errored (see earlier error)",
  53. };
  54. /**
  55. * lejp_construct - prepare a struct lejp_ctx for use
  56. *
  57. * \param ctx: pointer to your struct lejp_ctx
  58. * \param callback: your user callback which will received parsed tokens
  59. * \param user: optional user data pointer untouched by lejp
  60. * \param paths: your array of name elements you are interested in
  61. * \param count_paths: LWS_ARRAY_SIZE() of @paths
  62. *
  63. * Prepares your context struct for use with lejp
  64. */
  65. void
  66. lejp_construct(struct lejp_ctx *ctx,
  67. signed char (*callback)(struct lejp_ctx *ctx, char reason), void *user,
  68. const char * const *paths, unsigned char count_paths)
  69. {
  70. ctx->st[0].s = 0;
  71. ctx->st[0].p = 0;
  72. ctx->st[0].i = 0;
  73. ctx->st[0].b = 0;
  74. ctx->sp = 0;
  75. ctx->ipos = 0;
  76. ctx->outer_array = 0;
  77. ctx->path_match = 0;
  78. ctx->path_stride = 0;
  79. ctx->path[0] = '\0';
  80. ctx->user = user;
  81. ctx->line = 1;
  82. ctx->pst_sp = 0;
  83. ctx->pst[0].callback = callback;
  84. ctx->pst[0].paths = paths;
  85. ctx->pst[0].count_paths = count_paths;
  86. ctx->pst[0].user = NULL;
  87. ctx->pst[0].ppos = 0;
  88. ctx->pst[0].callback(ctx, LEJPCB_CONSTRUCTED);
  89. }
  90. /**
  91. * lejp_destruct - retire a previously constructed struct lejp_ctx
  92. *
  93. * \param ctx: pointer to your struct lejp_ctx
  94. *
  95. * lejp does not perform any allocations, but since your user code might, this
  96. * provides a one-time LEJPCB_DESTRUCTED callback at destruction time where
  97. * you can clean up in your callback.
  98. */
  99. void
  100. lejp_destruct(struct lejp_ctx *ctx)
  101. {
  102. /* no allocations... just let callback know what it happening */
  103. ctx->pst[0].callback(ctx, LEJPCB_DESTRUCTED);
  104. }
  105. /**
  106. * lejp_change_callback - switch to a different callback from now on
  107. *
  108. * \param ctx: pointer to your struct lejp_ctx
  109. * \param callback: your user callback which will received parsed tokens
  110. *
  111. * This tells the old callback it was destroyed, in case you want to take any
  112. * action because that callback "lost focus", then changes to the new
  113. * callback and tells it first that it was constructed, and then started.
  114. *
  115. * Changing callback is a cheap and powerful trick to split out handlers
  116. * according to information earlier in the parse. For example you may have
  117. * a JSON pair "schema" whose value defines what can be expected for the rest
  118. * of the JSON. Rather than having one huge callback for all cases, you can
  119. * have an initial one looking for "schema" which then calls
  120. * lejp_change_callback() to a handler specific for the schema.
  121. *
  122. * Notice that afterwards, you need to construct the context again anyway to
  123. * parse another JSON object, and the callback is reset then to the main,
  124. * schema-interpreting one. The construction action is very lightweight.
  125. */
  126. void
  127. lejp_change_callback(struct lejp_ctx *ctx,
  128. signed char (*callback)(struct lejp_ctx *ctx, char reason))
  129. {
  130. ctx->pst[0].callback(ctx, LEJPCB_DESTRUCTED);
  131. ctx->pst[0].callback = callback;
  132. ctx->pst[0].callback(ctx, LEJPCB_CONSTRUCTED);
  133. ctx->pst[0].callback(ctx, LEJPCB_START);
  134. }
  135. void
  136. lejp_check_path_match(struct lejp_ctx *ctx)
  137. {
  138. const char *p, *q;
  139. int n;
  140. size_t s = sizeof(char *);
  141. if (ctx->path_stride)
  142. s = ctx->path_stride;
  143. /* we only need to check if a match is not active */
  144. for (n = 0; !ctx->path_match &&
  145. n < ctx->pst[ctx->pst_sp].count_paths; n++) {
  146. ctx->wildcount = 0;
  147. p = ctx->path;
  148. q = *((char **)(((char *)ctx->pst[ctx->pst_sp].paths) + (n * s)));
  149. while (*p && *q) {
  150. if (*q != '*') {
  151. if (*p != *q)
  152. break;
  153. p++;
  154. q++;
  155. continue;
  156. }
  157. ctx->wild[ctx->wildcount++] = lws_ptr_diff(p, ctx->path);
  158. q++;
  159. /*
  160. * if * has something after it, match to .
  161. * if ends with *, eat everything.
  162. * This implies match sequences must be ordered like
  163. * x.*.*
  164. * x.*
  165. * if both options are possible
  166. */
  167. while (*p && (*p != '.' || !*q))
  168. p++;
  169. }
  170. if (*p || *q)
  171. continue;
  172. ctx->path_match = n + 1;
  173. ctx->path_match_len = ctx->pst[ctx->pst_sp].ppos;
  174. return;
  175. }
  176. if (!ctx->path_match)
  177. ctx->wildcount = 0;
  178. }
  179. int
  180. lejp_get_wildcard(struct lejp_ctx *ctx, int wildcard, char *dest, int len)
  181. {
  182. int n;
  183. if (wildcard >= ctx->wildcount || !len)
  184. return 0;
  185. n = ctx->wild[wildcard];
  186. while (--len && n < ctx->pst[ctx->pst_sp].ppos &&
  187. (n == ctx->wild[wildcard] || ctx->path[n] != '.'))
  188. *dest++ = ctx->path[n++];
  189. *dest = '\0';
  190. n++;
  191. return n - ctx->wild[wildcard];
  192. }
  193. /**
  194. * lejp_parse - interpret some more incoming data incrementally
  195. *
  196. * \param ctx: previously constructed parsing context
  197. * \param json: char buffer with the new data to interpret
  198. * \param len: amount of data in the buffer
  199. *
  200. * Because lejp is a stream parser, it incrementally parses as new data
  201. * becomes available, maintaining all state in the context struct. So an
  202. * incomplete JSON is a normal situation, getting you a LEJP_CONTINUE
  203. * return, signalling there's no error but to call again with more data when
  204. * it comes to complete the parsing. Successful parsing completes with a
  205. * 0 or positive integer indicating how much of the last input buffer was
  206. * unused.
  207. */
  208. static const char esc_char[] = "\"\\/bfnrt";
  209. static const char esc_tran[] = "\"\\/\b\f\n\r\t";
  210. static const char tokens[] = "rue alse ull ";
  211. int
  212. lejp_parse(struct lejp_ctx *ctx, const unsigned char *json, int len)
  213. {
  214. unsigned char c, n, s;
  215. int ret = LEJP_REJECT_UNKNOWN;
  216. if (!ctx->sp && !ctx->pst[ctx->pst_sp].ppos)
  217. ctx->pst[ctx->pst_sp].callback(ctx, LEJPCB_START);
  218. while (len--) {
  219. c = *json++;
  220. s = ctx->st[ctx->sp].s;
  221. /* skip whitespace unless we should care */
  222. if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '#') {
  223. if (c == '\n') {
  224. ctx->line++;
  225. ctx->st[ctx->sp].s &= ~LEJP_FLAG_WS_COMMENTLINE;
  226. }
  227. if (!(s & LEJP_FLAG_WS_KEEP)) {
  228. if (c == '#')
  229. ctx->st[ctx->sp].s |=
  230. LEJP_FLAG_WS_COMMENTLINE;
  231. continue;
  232. }
  233. }
  234. if (ctx->st[ctx->sp].s & LEJP_FLAG_WS_COMMENTLINE)
  235. continue;
  236. switch (s) {
  237. case LEJP_IDLE:
  238. if (!ctx->sp && c == '[') {
  239. /* push */
  240. ctx->outer_array = 1;
  241. ctx->st[ctx->sp].s = LEJP_MP_ARRAY_END;
  242. c = LEJP_MP_VALUE;
  243. ctx->path[ctx->pst[ctx->pst_sp].ppos++] = '[';
  244. ctx->path[ctx->pst[ctx->pst_sp].ppos++] = ']';
  245. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  246. if (ctx->pst[ctx->pst_sp].callback(ctx, LEJPCB_ARRAY_START))
  247. goto reject_callback;
  248. ctx->i[ctx->ipos++] = 0;
  249. if (ctx->ipos > LWS_ARRAY_SIZE(ctx->i)) {
  250. ret = LEJP_REJECT_MP_DELIM_ISTACK;
  251. goto reject;
  252. }
  253. goto add_stack_level;
  254. }
  255. if (c != '{') {
  256. ret = LEJP_REJECT_IDLE_NO_BRACE;
  257. goto reject;
  258. }
  259. if (ctx->pst[ctx->pst_sp].callback(ctx,
  260. LEJPCB_OBJECT_START))
  261. goto reject_callback;
  262. ctx->st[ctx->sp].s = LEJP_MEMBERS;
  263. break;
  264. case LEJP_MEMBERS:
  265. if (c == '}') {
  266. if (ctx->sp >= 1)
  267. goto pop_level;
  268. ctx->st[ctx->sp].s = LEJP_IDLE;
  269. ret = LEJP_REJECT_MEMBERS_NO_CLOSE;
  270. goto reject;
  271. }
  272. ctx->st[ctx->sp].s = LEJP_M_P;
  273. goto redo_character;
  274. case LEJP_M_P:
  275. if (c != '\"') {
  276. ret = LEJP_REJECT_MP_NO_OPEN_QUOTE;
  277. goto reject;
  278. }
  279. /* push */
  280. ctx->st[ctx->sp].s = LEJP_MP_DELIM;
  281. c = LEJP_MP_STRING;
  282. goto add_stack_level;
  283. case LEJP_MP_STRING:
  284. if (c == '\"') {
  285. if (!ctx->sp) { /* JSON can't end on quote */
  286. ret = LEJP_REJECT_MP_STRING_UNDERRUN;
  287. goto reject;
  288. }
  289. if (ctx->st[ctx->sp - 1].s != LEJP_MP_DELIM) {
  290. ctx->buf[ctx->npos] = '\0';
  291. if (ctx->pst[ctx->pst_sp].callback(ctx,
  292. LEJPCB_VAL_STR_END) < 0)
  293. goto reject_callback;
  294. }
  295. /* pop */
  296. ctx->sp--;
  297. break;
  298. }
  299. if (c == '\\') {
  300. ctx->st[ctx->sp].s = LEJP_MP_STRING_ESC;
  301. break;
  302. }
  303. if (c < ' ') {/* "control characters" not allowed */
  304. ret = LEJP_REJECT_MP_ILLEGAL_CTRL;
  305. goto reject;
  306. }
  307. goto emit_string_char;
  308. case LEJP_MP_STRING_ESC:
  309. if (c == 'u') {
  310. ctx->st[ctx->sp].s = LEJP_MP_STRING_ESC_U1;
  311. ctx->uni = 0;
  312. break;
  313. }
  314. for (n = 0; n < sizeof(esc_char); n++) {
  315. if (c != esc_char[n])
  316. continue;
  317. /* found it */
  318. c = esc_tran[n];
  319. ctx->st[ctx->sp].s = LEJP_MP_STRING;
  320. goto emit_string_char;
  321. }
  322. ret = LEJP_REJECT_MP_STRING_ESC_ILLEGAL_ESC;
  323. /* illegal escape char */
  324. goto reject;
  325. case LEJP_MP_STRING_ESC_U1:
  326. case LEJP_MP_STRING_ESC_U2:
  327. case LEJP_MP_STRING_ESC_U3:
  328. case LEJP_MP_STRING_ESC_U4:
  329. ctx->uni <<= 4;
  330. if (c >= '0' && c <= '9')
  331. ctx->uni |= c - '0';
  332. else
  333. if (c >= 'a' && c <= 'f')
  334. ctx->uni |= c - 'a' + 10;
  335. else
  336. if (c >= 'A' && c <= 'F')
  337. ctx->uni |= c - 'A' + 10;
  338. else {
  339. ret = LEJP_REJECT_ILLEGAL_HEX;
  340. goto reject;
  341. }
  342. ctx->st[ctx->sp].s++;
  343. switch (s) {
  344. case LEJP_MP_STRING_ESC_U2:
  345. if (ctx->uni < 0x08)
  346. break;
  347. /*
  348. * 0x08-0xff (0x0800 - 0xffff)
  349. * emit 3-byte UTF-8
  350. */
  351. c = 0xe0 | ((ctx->uni >> 4) & 0xf);
  352. goto emit_string_char;
  353. case LEJP_MP_STRING_ESC_U3:
  354. if (ctx->uni >= 0x080) {
  355. /*
  356. * 0x080 - 0xfff (0x0800 - 0xffff)
  357. * middle 3-byte seq
  358. * send ....XXXXXX..
  359. */
  360. c = 0x80 | ((ctx->uni >> 2) & 0x3f);
  361. goto emit_string_char;
  362. }
  363. if (ctx->uni < 0x008)
  364. break;
  365. /*
  366. * 0x008 - 0x7f (0x0080 - 0x07ff)
  367. * start 2-byte seq
  368. */
  369. c = 0xc0 | (ctx->uni >> 2);
  370. goto emit_string_char;
  371. case LEJP_MP_STRING_ESC_U4:
  372. if (ctx->uni >= 0x0080)
  373. /* end of 2 or 3-byte seq */
  374. c = 0x80 | (ctx->uni & 0x3f);
  375. else
  376. /* literal */
  377. c = (unsigned char)ctx->uni;
  378. ctx->st[ctx->sp].s = LEJP_MP_STRING;
  379. goto emit_string_char;
  380. default:
  381. break;
  382. }
  383. break;
  384. case LEJP_MP_DELIM:
  385. if (c != ':') {
  386. ret = LEJP_REJECT_MP_DELIM_MISSING_COLON;
  387. goto reject;
  388. }
  389. ctx->st[ctx->sp].s = LEJP_MP_VALUE;
  390. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  391. lejp_check_path_match(ctx);
  392. if (ctx->pst[ctx->pst_sp].callback(ctx, LEJPCB_PAIR_NAME))
  393. goto reject_callback;
  394. break;
  395. case LEJP_MP_VALUE:
  396. if (c == '-' || (c >= '0' && c <= '9')) {
  397. ctx->npos = 0;
  398. ctx->dcount = 0;
  399. ctx->f = 0;
  400. ctx->st[ctx->sp].s = LEJP_MP_VALUE_NUM_INT;
  401. goto redo_character;
  402. }
  403. switch (c) {
  404. case'\"':
  405. /* push */
  406. ctx->st[ctx->sp].s = LEJP_MP_COMMA_OR_END;
  407. c = LEJP_MP_STRING;
  408. ctx->npos = 0;
  409. ctx->buf[0] = '\0';
  410. if (ctx->pst[ctx->pst_sp].callback(ctx,
  411. LEJPCB_VAL_STR_START))
  412. goto reject_callback;
  413. goto add_stack_level;
  414. case '{':
  415. /* push */
  416. ctx->st[ctx->sp].s = LEJP_MP_COMMA_OR_END;
  417. c = LEJP_MEMBERS;
  418. lejp_check_path_match(ctx);
  419. if (ctx->pst[ctx->pst_sp].callback(ctx,
  420. LEJPCB_OBJECT_START))
  421. goto reject_callback;
  422. ctx->path_match = 0;
  423. goto add_stack_level;
  424. case '[':
  425. /* push */
  426. ctx->st[ctx->sp].s = LEJP_MP_ARRAY_END;
  427. c = LEJP_MP_VALUE;
  428. ctx->path[ctx->pst[ctx->pst_sp].ppos++] = '[';
  429. ctx->path[ctx->pst[ctx->pst_sp].ppos++] = ']';
  430. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  431. if (ctx->pst[ctx->pst_sp].callback(ctx, LEJPCB_ARRAY_START))
  432. goto reject_callback;
  433. ctx->i[ctx->ipos++] = 0;
  434. if (ctx->ipos > LWS_ARRAY_SIZE(ctx->i)) {
  435. ret = LEJP_REJECT_MP_DELIM_ISTACK;
  436. goto reject;
  437. }
  438. goto add_stack_level;
  439. case ']':
  440. /* pop */
  441. if (!ctx->sp) { /* JSON can't end on ] */
  442. ret = LEJP_REJECT_MP_C_OR_E_UNDERF;
  443. goto reject;
  444. }
  445. ctx->sp--;
  446. if (ctx->st[ctx->sp].s != LEJP_MP_ARRAY_END) {
  447. ret = LEJP_REJECT_MP_C_OR_E_NOTARRAY;
  448. goto reject;
  449. }
  450. /* drop the path [n] bit */
  451. if (ctx->sp) {
  452. ctx->pst[ctx->pst_sp].ppos =
  453. ctx->st[ctx->sp - 1].p;
  454. ctx->ipos = ctx->st[ctx->sp - 1].i;
  455. }
  456. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  457. if (ctx->path_match &&
  458. ctx->pst[ctx->pst_sp].ppos <= ctx->path_match_len)
  459. /*
  460. * we shrank the path to be
  461. * smaller than the matching point
  462. */
  463. ctx->path_match = 0;
  464. if (ctx->outer_array && !ctx->sp) { /* ended on ] */
  465. n = LEJPCB_ARRAY_END;
  466. goto completed;
  467. }
  468. goto array_end;
  469. case 't': /* true */
  470. ctx->uni = 0;
  471. ctx->st[ctx->sp].s = LEJP_MP_VALUE_TOK;
  472. break;
  473. case 'f':
  474. ctx->uni = 4;
  475. ctx->st[ctx->sp].s = LEJP_MP_VALUE_TOK;
  476. break;
  477. case 'n':
  478. ctx->uni = 4 + 5;
  479. ctx->st[ctx->sp].s = LEJP_MP_VALUE_TOK;
  480. break;
  481. default:
  482. ret = LEJP_REJECT_MP_DELIM_BAD_VALUE_START;
  483. goto reject;
  484. }
  485. break;
  486. case LEJP_MP_VALUE_NUM_INT:
  487. if (!ctx->npos && c == '-') {
  488. ctx->f |= LEJP_SEEN_MINUS;
  489. goto append_npos;
  490. }
  491. if (ctx->dcount < 20 && c >= '0' && c <= '9') {
  492. if (ctx->f & LEJP_SEEN_POINT)
  493. ctx->f |= LEJP_SEEN_POST_POINT;
  494. ctx->dcount++;
  495. goto append_npos;
  496. }
  497. if (c == '.') {
  498. if (!ctx->dcount || (ctx->f & LEJP_SEEN_POINT)) {
  499. ret = LEJP_REJECT_MP_VAL_NUM_FORMAT;
  500. goto reject;
  501. }
  502. ctx->f |= LEJP_SEEN_POINT;
  503. goto append_npos;
  504. }
  505. /*
  506. * before exponent, if we had . we must have had at
  507. * least one more digit
  508. */
  509. if ((ctx->f &
  510. (LEJP_SEEN_POINT | LEJP_SEEN_POST_POINT)) ==
  511. LEJP_SEEN_POINT) {
  512. ret = LEJP_REJECT_MP_VAL_NUM_INT_NO_FRAC;
  513. goto reject;
  514. }
  515. if (c == 'e' || c == 'E') {
  516. if (ctx->f & LEJP_SEEN_EXP) {
  517. ret = LEJP_REJECT_MP_VAL_NUM_FORMAT;
  518. goto reject;
  519. }
  520. ctx->f |= LEJP_SEEN_EXP;
  521. ctx->st[ctx->sp].s = LEJP_MP_VALUE_NUM_EXP;
  522. goto append_npos;
  523. }
  524. /* if none of the above, did we even have a number? */
  525. if (!ctx->dcount) {
  526. ret = LEJP_REJECT_MP_VAL_NUM_FORMAT;
  527. goto reject;
  528. }
  529. ctx->buf[ctx->npos] = '\0';
  530. if (ctx->f & LEJP_SEEN_POINT) {
  531. if (ctx->pst[ctx->pst_sp].callback(ctx,
  532. LEJPCB_VAL_NUM_FLOAT))
  533. goto reject_callback;
  534. } else {
  535. if (ctx->pst[ctx->pst_sp].callback(ctx,
  536. LEJPCB_VAL_NUM_INT))
  537. goto reject_callback;
  538. }
  539. /* then this is the post-number character, loop */
  540. ctx->st[ctx->sp].s = LEJP_MP_COMMA_OR_END;
  541. goto redo_character;
  542. case LEJP_MP_VALUE_NUM_EXP:
  543. ctx->st[ctx->sp].s = LEJP_MP_VALUE_NUM_INT;
  544. if (c >= '0' && c <= '9')
  545. goto redo_character;
  546. if (c == '+' || c == '-')
  547. goto append_npos;
  548. ret = LEJP_REJECT_MP_VAL_NUM_EXP_BAD_EXP;
  549. goto reject;
  550. case LEJP_MP_VALUE_TOK: /* true, false, null */
  551. if (c != tokens[ctx->uni]) {
  552. ret = LEJP_REJECT_MP_VAL_TOK_UNKNOWN;
  553. goto reject;
  554. }
  555. ctx->uni++;
  556. if (tokens[ctx->uni] != ' ')
  557. break;
  558. switch (ctx->uni) {
  559. case 3:
  560. ctx->buf[0] = '1';
  561. ctx->buf[1] = '\0';
  562. if (ctx->pst[ctx->pst_sp].callback(ctx,
  563. LEJPCB_VAL_TRUE))
  564. goto reject_callback;
  565. break;
  566. case 8:
  567. ctx->buf[0] = '0';
  568. ctx->buf[1] = '\0';
  569. if (ctx->pst[ctx->pst_sp].callback(ctx,
  570. LEJPCB_VAL_FALSE))
  571. goto reject_callback;
  572. break;
  573. case 12:
  574. ctx->buf[0] = '\0';
  575. if (ctx->pst[ctx->pst_sp].callback(ctx,
  576. LEJPCB_VAL_NULL))
  577. goto reject_callback;
  578. break;
  579. }
  580. ctx->st[ctx->sp].s = LEJP_MP_COMMA_OR_END;
  581. break;
  582. case LEJP_MP_COMMA_OR_END:
  583. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  584. if (c == ',') {
  585. /* increment this stack level's index */
  586. ctx->st[ctx->sp].s = LEJP_M_P;
  587. if (!ctx->sp) {
  588. ctx->pst[ctx->pst_sp].ppos = 0;
  589. /*
  590. * since we came back to root level,
  591. * no path can still match
  592. */
  593. ctx->path_match = 0;
  594. break;
  595. }
  596. ctx->pst[ctx->pst_sp].ppos = ctx->st[ctx->sp - 1].p;
  597. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  598. if (ctx->path_match &&
  599. ctx->pst[ctx->pst_sp].ppos <= ctx->path_match_len)
  600. /*
  601. * we shrank the path to be
  602. * smaller than the matching point
  603. */
  604. ctx->path_match = 0;
  605. if (ctx->st[ctx->sp - 1].s != LEJP_MP_ARRAY_END)
  606. break;
  607. /* top level is definitely an array... */
  608. if (ctx->ipos)
  609. ctx->i[ctx->ipos - 1]++;
  610. ctx->st[ctx->sp].s = LEJP_MP_VALUE;
  611. break;
  612. }
  613. if (c == ']') {
  614. if (!ctx->sp) {
  615. ret = LEJP_REJECT_MP_C_OR_E_UNDERF;
  616. goto reject;
  617. }
  618. /* pop */
  619. ctx->sp--;
  620. if (ctx->st[ctx->sp].s != LEJP_MP_ARRAY_END) {
  621. ret = LEJP_REJECT_MP_C_OR_E_NOTARRAY;
  622. goto reject;
  623. }
  624. /* drop the path [n] bit */
  625. if (ctx->sp) {
  626. ctx->pst[ctx->pst_sp].ppos =
  627. ctx->st[ctx->sp - 1].p;
  628. ctx->ipos = ctx->st[ctx->sp - 1].i;
  629. }
  630. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  631. if (ctx->path_match &&
  632. ctx->pst[ctx->pst_sp].ppos <= ctx->path_match_len)
  633. /*
  634. * we shrank the path to be
  635. * smaller than the matching point
  636. */
  637. ctx->path_match = 0;
  638. if (ctx->outer_array && !ctx->sp) { /* ended on ] */
  639. n = LEJPCB_ARRAY_END;
  640. goto completed;
  641. }
  642. /* do LEJP_MP_ARRAY_END processing */
  643. goto redo_character;
  644. }
  645. if (c != '}') {
  646. ret = LEJP_REJECT_MP_C_OR_E_NEITHER;
  647. goto reject;
  648. }
  649. if (!ctx->sp) {
  650. n = LEJPCB_OBJECT_END;
  651. completed:
  652. lejp_check_path_match(ctx);
  653. if (ctx->pst[ctx->pst_sp].callback(ctx, n) ||
  654. ctx->pst[ctx->pst_sp].callback(ctx,
  655. LEJPCB_COMPLETE))
  656. goto reject_callback;
  657. /* done, return unused amount */
  658. return len;
  659. }
  660. /* pop */
  661. pop_level:
  662. ctx->sp--;
  663. if (ctx->sp) {
  664. ctx->pst[ctx->pst_sp].ppos = ctx->st[ctx->sp].p;
  665. ctx->ipos = ctx->st[ctx->sp].i;
  666. }
  667. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  668. if (ctx->path_match &&
  669. ctx->pst[ctx->pst_sp].ppos <= ctx->path_match_len)
  670. /*
  671. * we shrank the path to be
  672. * smaller than the matching point
  673. */
  674. ctx->path_match = 0;
  675. lejp_check_path_match(ctx);
  676. if (ctx->pst[ctx->pst_sp].callback(ctx,
  677. LEJPCB_OBJECT_END))
  678. goto reject_callback;
  679. break;
  680. case LEJP_MP_ARRAY_END:
  681. array_end:
  682. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  683. if (c == ',') {
  684. /* increment this stack level's index */
  685. if (ctx->ipos)
  686. ctx->i[ctx->ipos - 1]++;
  687. ctx->st[ctx->sp].s = LEJP_MP_VALUE;
  688. if (ctx->sp)
  689. ctx->pst[ctx->pst_sp].ppos =
  690. ctx->st[ctx->sp - 1].p;
  691. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  692. break;
  693. }
  694. if (c != ']') {
  695. ret = LEJP_REJECT_MP_ARRAY_END_MISSING;
  696. goto reject;
  697. }
  698. ctx->st[ctx->sp].s = LEJP_MP_COMMA_OR_END;
  699. ctx->pst[ctx->pst_sp].callback(ctx, LEJPCB_ARRAY_END);
  700. break;
  701. }
  702. continue;
  703. emit_string_char:
  704. if (!ctx->sp || ctx->st[ctx->sp - 1].s != LEJP_MP_DELIM) {
  705. /* assemble the string value into chunks */
  706. ctx->buf[ctx->npos++] = c;
  707. if (ctx->npos == sizeof(ctx->buf) - 1) {
  708. if (ctx->pst[ctx->pst_sp].callback(ctx,
  709. LEJPCB_VAL_STR_CHUNK))
  710. goto reject_callback;
  711. ctx->npos = 0;
  712. }
  713. continue;
  714. }
  715. /* name part of name:value pair */
  716. ctx->path[ctx->pst[ctx->pst_sp].ppos++] = c;
  717. continue;
  718. add_stack_level:
  719. /* push on to the object stack */
  720. if (ctx->pst[ctx->pst_sp].ppos &&
  721. ctx->st[ctx->sp].s != LEJP_MP_COMMA_OR_END &&
  722. ctx->st[ctx->sp].s != LEJP_MP_ARRAY_END)
  723. ctx->path[ctx->pst[ctx->pst_sp].ppos++] = '.';
  724. ctx->st[ctx->sp].p = ctx->pst[ctx->pst_sp].ppos;
  725. ctx->st[ctx->sp].i = ctx->ipos;
  726. if (++ctx->sp == LWS_ARRAY_SIZE(ctx->st)) {
  727. ret = LEJP_REJECT_STACK_OVERFLOW;
  728. goto reject;
  729. }
  730. ctx->path[ctx->pst[ctx->pst_sp].ppos] = '\0';
  731. ctx->st[ctx->sp].s = c;
  732. ctx->st[ctx->sp].b = 0;
  733. continue;
  734. append_npos:
  735. if (ctx->npos >= sizeof(ctx->buf)) {
  736. ret = LEJP_REJECT_NUM_TOO_LONG;
  737. goto reject;
  738. }
  739. ctx->buf[ctx->npos++] = c;
  740. continue;
  741. redo_character:
  742. json--;
  743. len++;
  744. }
  745. return LEJP_CONTINUE;
  746. reject_callback:
  747. ret = LEJP_REJECT_CALLBACK;
  748. reject:
  749. ctx->pst[ctx->pst_sp].callback(ctx, LEJPCB_FAILED);
  750. return ret;
  751. }
  752. int
  753. lejp_parser_push(struct lejp_ctx *ctx, void *user, const char * const *paths,
  754. unsigned char paths_count, lejp_callback lejp_cb)
  755. {
  756. struct _lejp_parsing_stack *p;
  757. if (ctx->pst_sp + 1 == LEJP_MAX_PARSING_STACK_DEPTH)
  758. return -1;
  759. lejp_check_path_match(ctx);
  760. ctx->pst[ctx->pst_sp].path_match = ctx->path_match;
  761. ctx->pst_sp++;
  762. p = &ctx->pst[ctx->pst_sp];
  763. p->user = user;
  764. p->callback = lejp_cb;
  765. p->paths = paths;
  766. p->count_paths = paths_count;
  767. p->ppos = 0;
  768. ctx->path_match = 0;
  769. lejp_check_path_match(ctx);
  770. lwsl_debug("%s: pushed parser stack to %d (path %s)\n", __func__,
  771. ctx->pst_sp, ctx->path);
  772. return 0;
  773. }
  774. int
  775. lejp_parser_pop(struct lejp_ctx *ctx)
  776. {
  777. if (!ctx->pst_sp)
  778. return -1;
  779. ctx->pst_sp--;
  780. lwsl_debug("%s: popped parser stack to %d\n", __func__, ctx->pst_sp);
  781. ctx->path_match = 0; /* force it to check */
  782. lejp_check_path_match(ctx);
  783. return 0;
  784. }
  785. const char *
  786. lejp_error_to_string(int e)
  787. {
  788. if (e > 0)
  789. e = 0;
  790. else
  791. e = -e;
  792. if (e >= (int)LWS_ARRAY_SIZE(parser_errs))
  793. return "Unknown error";
  794. return parser_errs[e];
  795. }