lws-struct-lejp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 <assert.h>
  27. signed char
  28. lws_struct_schema_only_lejp_cb(struct lejp_ctx *ctx, char reason)
  29. {
  30. lws_struct_args_t *a = (lws_struct_args_t *)ctx->user;
  31. const lws_struct_map_t *map = a->map_st[ctx->pst_sp];
  32. size_t n = a->map_entries_st[ctx->pst_sp], imp = 0;
  33. lejp_callback cb = map->lejp_cb;
  34. if (reason == LEJPCB_PAIR_NAME && strcmp(ctx->path, "schema")) {
  35. /*
  36. * If not "schema", the schema is implicit rather than
  37. * explicitly given, ie, he just goes ahead and starts using
  38. * member names that imply a particular type. For example, he
  39. * may have an implicit type normally, and a different one for
  40. * exceptions that just starts using "error-message" or whatever
  41. * and we can understand that's the exception type now.
  42. *
  43. * Let's look into each of the maps in the top level array
  44. * and match the first one that mentions the name he gave here,
  45. * and bind to the associated type / create a toplevel object
  46. * of that type.
  47. */
  48. while (n--) {
  49. const lws_struct_map_t *child = map->child_map;
  50. int m, child_members = (int)map->child_map_size;
  51. for (m = 0; m < child_members; m++) {
  52. if (!strcmp(ctx->path, child->colname)) {
  53. /*
  54. * We matched on him... map is pointing
  55. * to the right toplevel type, let's
  56. * just pick up from there as if we
  57. * matched the explicit schema name...
  58. */
  59. ctx->path_match = 1;
  60. imp = 1;
  61. goto matched;
  62. }
  63. }
  64. map++;
  65. }
  66. lwsl_notice("%s: can't match implicit schema %s\n",
  67. __func__, ctx->path);
  68. return -1;
  69. }
  70. if (reason != LEJPCB_VAL_STR_END || ctx->path_match != 1)
  71. return 0;
  72. /* If "schema", then look for a matching name in the map array */
  73. while (n--) {
  74. if (strcmp(ctx->buf, map->colname)) {
  75. map++;
  76. continue;
  77. }
  78. matched:
  79. a->dest = lwsac_use_zero(&a->ac, map->aux, a->ac_block_size);
  80. if (!a->dest) {
  81. lwsl_err("%s: OOT\n", __func__);
  82. return 1;
  83. }
  84. a->dest_len = map->aux;
  85. if (!ctx->pst_sp)
  86. a->top_schema_index = (int)(map - a->map_st[ctx->pst_sp]);
  87. if (!cb)
  88. cb = lws_struct_default_lejp_cb;
  89. lejp_parser_push(ctx, a->dest, &map->child_map[0].colname,
  90. (uint8_t)map->child_map_size, cb);
  91. a->map_st[ctx->pst_sp] = map->child_map;
  92. a->map_entries_st[ctx->pst_sp] = map->child_map_size;
  93. // lwsl_notice("%s: child map ofs_clist %d\n", __func__,
  94. // (int)a->map_st[ctx->pst_sp]->ofs_clist);
  95. if (imp)
  96. return cb(ctx, reason);
  97. return 0;
  98. }
  99. lwsl_notice("%s: unknown schema %s\n", __func__, ctx->buf);
  100. return 1;
  101. }
  102. static int
  103. lws_struct_lejp_push(struct lejp_ctx *ctx, lws_struct_args_t *args,
  104. const lws_struct_map_t *map, uint8_t *ch)
  105. {
  106. lejp_callback cb = map->lejp_cb;
  107. if (!cb)
  108. cb = lws_struct_default_lejp_cb;
  109. lejp_parser_push(ctx, ch, (const char * const*)map->child_map,
  110. (uint8_t)map->child_map_size, cb);
  111. args->map_st[ctx->pst_sp] = map->child_map;
  112. args->map_entries_st[ctx->pst_sp] = map->child_map_size;
  113. return 0;
  114. }
  115. signed char
  116. lws_struct_default_lejp_cb(struct lejp_ctx *ctx, char reason)
  117. {
  118. lws_struct_args_t *args = (lws_struct_args_t *)ctx->user;
  119. const lws_struct_map_t *map, *pmap = NULL;
  120. uint8_t *ch;
  121. size_t n;
  122. char *u;
  123. if (reason == LEJPCB_ARRAY_END) {
  124. lejp_parser_pop(ctx);
  125. return 0;
  126. }
  127. if (reason == LEJPCB_ARRAY_START) {
  128. if (!ctx->path_match)
  129. lwsl_err("%s: ARRAY_START with ctx->path_match 0\n", __func__);
  130. map = &args->map_st[ctx->pst_sp][ctx->path_match - 1];
  131. if (map->type == LSMT_LIST)
  132. lws_struct_lejp_push(ctx, args, map, NULL);
  133. return 0;
  134. }
  135. if (ctx->pst_sp)
  136. pmap = &args->map_st[ctx->pst_sp - 1]
  137. [ctx->pst[ctx->pst_sp - 1].path_match - 1];
  138. if (reason == LEJPCB_OBJECT_START) {
  139. if (!ctx->path_match) {
  140. ctx->pst[ctx->pst_sp].user = NULL;
  141. return 0;
  142. }
  143. map = &args->map_st[ctx->pst_sp][ctx->path_match - 1];
  144. n = args->map_entries_st[ctx->pst_sp];
  145. if (map->type != LSMT_CHILD_PTR && map->type != LSMT_LIST) {
  146. ctx->pst[ctx->pst_sp].user = NULL;
  147. return 0;
  148. }
  149. pmap = map;
  150. lws_struct_lejp_push(ctx, args, map, NULL);
  151. }
  152. if (reason == LEJPCB_OBJECT_END && pmap) {
  153. if (pmap->type == LSMT_CHILD_PTR)
  154. lejp_parser_pop(ctx);
  155. if (ctx->pst_sp)
  156. pmap = &args->map_st[ctx->pst_sp - 1]
  157. [ctx->pst[ctx->pst_sp - 1].path_match - 1];
  158. }
  159. if (!ctx->path_match)
  160. return 0;
  161. map = &args->map_st[ctx->pst_sp][ctx->path_match - 1];
  162. n = args->map_entries_st[ctx->pst_sp];
  163. if (map->type == LSMT_SCHEMA) {
  164. while (n--) {
  165. if (strncmp(map->colname, ctx->buf, ctx->npos)) {
  166. map++;
  167. continue;
  168. }
  169. /* instantiate the correct toplevel object */
  170. ch = lwsac_use_zero(&args->ac, map->aux,
  171. args->ac_block_size);
  172. if (!ch) {
  173. lwsl_err("OOM\n");
  174. return 1;
  175. }
  176. lws_struct_lejp_push(ctx, args, map, ch);
  177. return 0;
  178. }
  179. lwsl_notice("%s: unknown schema %.*s, tried %d\n", __func__,
  180. ctx->npos, ctx->buf,
  181. (int)args->map_entries_st[ctx->pst_sp]);
  182. goto cleanup;
  183. }
  184. if (!ctx->pst[ctx->pst_sp].user) {
  185. struct lws_dll2_owner *owner;
  186. struct lws_dll2 *list;
  187. /* create list item object if none already */
  188. if (!ctx->path_match || !pmap)
  189. return 0;
  190. map = &args->map_st[ctx->pst_sp - 1][ctx->path_match - 1];
  191. n = args->map_entries_st[ctx->pst_sp - 1];
  192. if (!ctx->pst_sp)
  193. return 0;
  194. if (pmap->type != LSMT_LIST && pmap->type != LSMT_CHILD_PTR)
  195. return 1;
  196. /* we need to create a child or array item object */
  197. owner = (struct lws_dll2_owner *)
  198. (((char *)ctx->pst[ctx->pst_sp - 1].user) + pmap->ofs);
  199. assert(pmap->aux);
  200. /* instantiate one of the child objects */
  201. ctx->pst[ctx->pst_sp].user = lwsac_use_zero(&args->ac,
  202. pmap->aux, args->ac_block_size);
  203. if (!ctx->pst[ctx->pst_sp].user) {
  204. lwsl_err("OOM\n");
  205. return 1;
  206. }
  207. lwsl_info("%s: created '%s' object size %d\n", __func__,
  208. pmap->colname, (int)pmap->aux);
  209. if (pmap->type == LSMT_LIST) {
  210. list = (struct lws_dll2 *)
  211. ((char *)ctx->pst[ctx->pst_sp].user +
  212. pmap->ofs_clist);
  213. lws_dll2_add_tail(list, owner);
  214. }
  215. }
  216. if (!ctx->path_match)
  217. return 0;
  218. if (reason == LEJPCB_VAL_STR_CHUNK) {
  219. lejp_collation_t *coll;
  220. /* don't cache stuff we are going to ignore */
  221. if (map->type == LSMT_STRING_CHAR_ARRAY &&
  222. args->chunks_length >= map->aux)
  223. return 0;
  224. coll = lwsac_use_zero(&args->ac_chunks, sizeof(*coll),
  225. sizeof(*coll));
  226. if (!coll) {
  227. lwsl_err("%s: OOT\n", __func__);
  228. return 1;
  229. }
  230. coll->chunks.prev = NULL;
  231. coll->chunks.next = NULL;
  232. coll->chunks.owner = NULL;
  233. coll->len = ctx->npos;
  234. lws_dll2_add_tail(&coll->chunks, &args->chunks_owner);
  235. memcpy(coll->buf, ctx->buf, ctx->npos);
  236. args->chunks_length += ctx->npos;
  237. return 0;
  238. }
  239. if (reason != LEJPCB_VAL_STR_END && reason != LEJPCB_VAL_NUM_INT &&
  240. reason != LEJPCB_VAL_TRUE && reason != LEJPCB_VAL_FALSE)
  241. return 0;
  242. /* this is the end of the string */
  243. if (ctx->pst[ctx->pst_sp].user && pmap && pmap->type == LSMT_CHILD_PTR) {
  244. void **pp = (void **)
  245. (((char *)ctx->pst[ctx->pst_sp - 1].user) + pmap->ofs);
  246. *pp = ctx->pst[ctx->pst_sp].user;
  247. }
  248. u = (char *)ctx->pst[ctx->pst_sp].user;
  249. if (!u)
  250. u = (char *)ctx->pst[ctx->pst_sp - 1].user;
  251. {
  252. char **pp, *s;
  253. size_t lim, b;
  254. long long li;
  255. switch (map->type) {
  256. case LSMT_SIGNED:
  257. if (map->aux == sizeof(signed char)) {
  258. signed char *pc;
  259. pc = (signed char *)(u + map->ofs);
  260. *pc = atoi(ctx->buf);
  261. break;
  262. }
  263. if (map->aux == sizeof(int)) {
  264. int *pi;
  265. pi = (int *)(u + map->ofs);
  266. *pi = atoi(ctx->buf);
  267. break;
  268. }
  269. if (map->aux == sizeof(long)) {
  270. long *pl;
  271. pl = (long *)(u + map->ofs);
  272. *pl = atol(ctx->buf);
  273. } else {
  274. long long *pll;
  275. pll = (long long *)(u + map->ofs);
  276. *pll = atoll(ctx->buf);
  277. }
  278. break;
  279. case LSMT_UNSIGNED:
  280. if (map->aux == sizeof(unsigned char)) {
  281. unsigned char *pc;
  282. pc = (unsigned char *)(u + map->ofs);
  283. *pc = atoi(ctx->buf);
  284. break;
  285. }
  286. if (map->aux == sizeof(unsigned int)) {
  287. unsigned int *pi;
  288. pi = (unsigned int *)(u + map->ofs);
  289. *pi = atoi(ctx->buf);
  290. break;
  291. }
  292. if (map->aux == sizeof(unsigned long)) {
  293. unsigned long *pl;
  294. pl = (unsigned long *)(u + map->ofs);
  295. *pl = atol(ctx->buf);
  296. } else {
  297. unsigned long long *pll;
  298. pll = (unsigned long long *)(u + map->ofs);
  299. *pll = atoll(ctx->buf);
  300. }
  301. break;
  302. case LSMT_BOOLEAN:
  303. li = reason == LEJPCB_VAL_TRUE;
  304. if (map->aux == sizeof(char)) {
  305. char *pc;
  306. pc = (char *)(u + map->ofs);
  307. *pc = (char)li;
  308. break;
  309. }
  310. if (map->aux == sizeof(int)) {
  311. int *pi;
  312. pi = (int *)(u + map->ofs);
  313. *pi = (int)li;
  314. } else {
  315. uint64_t *p64;
  316. p64 = (uint64_t *)(u + map->ofs);
  317. *p64 = li;
  318. }
  319. break;
  320. case LSMT_STRING_CHAR_ARRAY:
  321. s = (char *)(u + map->ofs);
  322. lim = map->aux - 1;
  323. goto chunk_copy;
  324. case LSMT_STRING_PTR:
  325. pp = (char **)(u + map->ofs);
  326. lim = args->chunks_length + ctx->npos;
  327. s = lwsac_use(&args->ac, lim + 1, args->ac_block_size);
  328. if (!s)
  329. goto cleanup;
  330. *pp = s;
  331. chunk_copy:
  332. s[lim] = '\0';
  333. /* copy up to lim from the string chunk ac first */
  334. lws_start_foreach_dll_safe(struct lws_dll2 *, p, p1,
  335. args->chunks_owner.head) {
  336. lejp_collation_t *coll = (lejp_collation_t *)p;
  337. if (lim) {
  338. b = coll->len;
  339. if (b > lim)
  340. b = lim;
  341. memcpy(s, coll->buf, b);
  342. s += b;
  343. lim -= b;
  344. }
  345. } lws_end_foreach_dll_safe(p, p1);
  346. lwsac_free(&args->ac_chunks);
  347. args->chunks_owner.count = 0;
  348. args->chunks_owner.head = NULL;
  349. args->chunks_owner.tail = NULL;
  350. if (lim) {
  351. b = ctx->npos;
  352. if (b > lim)
  353. b = lim;
  354. memcpy(s, ctx->buf, b);
  355. s[b] = '\0';
  356. }
  357. break;
  358. default:
  359. break;
  360. }
  361. }
  362. if (args->cb)
  363. args->cb(args->dest, args->cb_arg);
  364. return 0;
  365. cleanup:
  366. lwsl_notice("%s: cleanup\n", __func__);
  367. lwsac_free(&args->ac_chunks);
  368. args->chunks_owner.count = 0;
  369. args->chunks_owner.head = NULL;
  370. args->chunks_owner.tail = NULL;
  371. return 1;
  372. }
  373. static const char * schema[] = { "schema" };
  374. int
  375. lws_struct_json_init_parse(struct lejp_ctx *ctx, lejp_callback cb, void *user)
  376. {
  377. /*
  378. * By default we are looking to match on a toplevel member called
  379. * "schema", against an LSM_SCHEMA
  380. */
  381. if (!cb)
  382. cb = lws_struct_schema_only_lejp_cb;
  383. lejp_construct(ctx, cb, user, schema, 1);
  384. ctx->path_stride = sizeof(lws_struct_map_t);
  385. return 0;
  386. }
  387. lws_struct_serialize_t *
  388. lws_struct_json_serialize_create(const lws_struct_map_t *map,
  389. size_t map_entries, int flags,
  390. const void *ptoplevel)
  391. {
  392. lws_struct_serialize_t *js = lws_zalloc(sizeof(*js), __func__);
  393. lws_struct_serialize_st_t *j;
  394. if (!js)
  395. return NULL;
  396. js->flags = flags;
  397. j = &js->st[0];
  398. j->map = map;
  399. j->map_entries = map_entries;
  400. j->obj = ptoplevel;
  401. j->idt = 0;
  402. return js;
  403. }
  404. void
  405. lws_struct_json_serialize_destroy(lws_struct_serialize_t **pjs)
  406. {
  407. if (!*pjs)
  408. return;
  409. lws_free(*pjs);
  410. *pjs = NULL;
  411. }
  412. static void
  413. lws_struct_pretty(lws_struct_serialize_t *js, uint8_t **pbuf, size_t *plen)
  414. {
  415. if (js->flags & LSSERJ_FLAG_PRETTY) {
  416. int n;
  417. *(*pbuf)++ = '\n';
  418. (*plen)--;
  419. for (n = 0; n < js->st[js->sp].idt; n++) {
  420. *(*pbuf)++ = ' ';
  421. (*plen)--;
  422. }
  423. }
  424. }
  425. lws_struct_json_serialize_result_t
  426. lws_struct_json_serialize(lws_struct_serialize_t *js, uint8_t *buf,
  427. size_t len, size_t *written)
  428. {
  429. lws_struct_serialize_st_t *j;
  430. const lws_struct_map_t *map;
  431. size_t budget = 0, olen = len, m;
  432. struct lws_dll2_owner *o;
  433. unsigned long long uli;
  434. const char *q;
  435. const void *p;
  436. char dbuf[72];
  437. long long li;
  438. int n, used;
  439. *written = 0;
  440. *buf = '\0';
  441. while (len > sizeof(dbuf) + 20) {
  442. j = &js->st[js->sp];
  443. map = &j->map[j->map_entry];
  444. q = j->obj + map->ofs;
  445. /* early check if the entry should be elided */
  446. switch (map->type) {
  447. case LSMT_STRING_CHAR_ARRAY:
  448. if (!q)
  449. goto up;
  450. break;
  451. case LSMT_STRING_PTR:
  452. case LSMT_CHILD_PTR:
  453. q = (char *)*(char **)q;
  454. if (!q)
  455. goto up;
  456. break;
  457. case LSMT_LIST:
  458. o = (struct lws_dll2_owner *)q;
  459. p = j->dllpos = lws_dll2_get_head(o);
  460. if (!p)
  461. goto up;
  462. break;
  463. case LSMT_BLOB_PTR:
  464. goto up;
  465. default:
  466. break;
  467. }
  468. if (j->subsequent) {
  469. *buf++ = ',';
  470. len--;
  471. lws_struct_pretty(js, &buf, &len);
  472. }
  473. j->subsequent = 1;
  474. if (map->type != LSMT_SCHEMA && !js->offset) {
  475. n = lws_snprintf((char *)buf, len, "\"%s\":",
  476. map->colname);
  477. buf += n;
  478. len -= n;
  479. if (js->flags & LSSERJ_FLAG_PRETTY) {
  480. *buf++ = ' ';
  481. len--;
  482. }
  483. }
  484. switch (map->type) {
  485. case LSMT_BOOLEAN:
  486. case LSMT_UNSIGNED:
  487. if (map->aux == sizeof(char)) {
  488. uli = *(unsigned char *)q;
  489. } else {
  490. if (map->aux == sizeof(int)) {
  491. uli = *(unsigned int *)q;
  492. } else {
  493. if (map->aux == sizeof(long))
  494. uli = *(unsigned long *)q;
  495. else
  496. uli = *(unsigned long long *)q;
  497. }
  498. }
  499. q = dbuf;
  500. if (map->type == LSMT_BOOLEAN) {
  501. budget = lws_snprintf(dbuf, sizeof(dbuf),
  502. "%s", uli ? "true" : "false");
  503. } else
  504. budget = lws_snprintf(dbuf, sizeof(dbuf),
  505. "%llu", uli);
  506. break;
  507. case LSMT_SIGNED:
  508. if (map->aux == sizeof(signed char)) {
  509. li = (long long)*(signed char *)q;
  510. } else {
  511. if (map->aux == sizeof(int)) {
  512. li = (long long)*(int *)q;
  513. } else {
  514. if (map->aux == sizeof(long))
  515. li = (long long)*(long *)q;
  516. else
  517. li = *(long long *)q;
  518. }
  519. }
  520. q = dbuf;
  521. budget = lws_snprintf(dbuf, sizeof(dbuf), "%lld", li);
  522. break;
  523. case LSMT_STRING_CHAR_ARRAY:
  524. case LSMT_STRING_PTR:
  525. if (!js->offset) {
  526. *buf++ = '\"';
  527. len--;
  528. }
  529. break;
  530. case LSMT_LIST:
  531. *buf++ = '[';
  532. len--;
  533. if (js->sp + 1 == LEJP_MAX_PARSING_STACK_DEPTH)
  534. return LSJS_RESULT_ERROR;
  535. /* add a stack level to handle parsing array members */
  536. o = (struct lws_dll2_owner *)q;
  537. p = j->dllpos = lws_dll2_get_head(o);
  538. if (!j->dllpos) {
  539. *buf++ = ']';
  540. len--;
  541. goto up;
  542. }
  543. n = j->idt;
  544. j = &js->st[++js->sp];
  545. j->idt = n + 2;
  546. j->map = map->child_map;
  547. j->map_entries = map->child_map_size;
  548. j->size = map->aux;
  549. j->subsequent = 0;
  550. j->map_entry = 0;
  551. lws_struct_pretty(js, &buf, &len);
  552. *buf++ = '{';
  553. len--;
  554. lws_struct_pretty(js, &buf, &len);
  555. if (p)
  556. j->obj = ((char *)p) - j->map->ofs_clist;
  557. else
  558. j->obj = NULL;
  559. continue;
  560. case LSMT_CHILD_PTR:
  561. if (js->sp + 1 == LEJP_MAX_PARSING_STACK_DEPTH)
  562. return LSJS_RESULT_ERROR;
  563. /* add a stack level to handle parsing child members */
  564. n = j->idt;
  565. j = &js->st[++js->sp];
  566. j->idt = n + 2;
  567. j->map = map->child_map;
  568. j->map_entries = map->child_map_size;
  569. j->size = map->aux;
  570. j->subsequent = 0;
  571. j->map_entry = 0;
  572. *buf++ = '{';
  573. len--;
  574. lws_struct_pretty(js, &buf, &len);
  575. j->obj = q;
  576. continue;
  577. case LSMT_SCHEMA:
  578. q = dbuf;
  579. *buf++ = '{';
  580. len--;
  581. j = &js->st[++js->sp];
  582. lws_struct_pretty(js, &buf, &len);
  583. if (!(js->flags & LSSERJ_FLAG_OMIT_SCHEMA)) {
  584. budget = lws_snprintf(dbuf, 15, "\"schema\":");
  585. if (js->flags & LSSERJ_FLAG_PRETTY)
  586. dbuf[budget++] = ' ';
  587. budget += lws_snprintf(dbuf + budget,
  588. sizeof(dbuf) - budget,
  589. "\"%s\"", map->colname);
  590. }
  591. if (js->sp != 1)
  592. return LSJS_RESULT_ERROR;
  593. j->map = map->child_map;
  594. j->map_entries = map->child_map_size;
  595. j->size = map->aux;
  596. j->subsequent = 0;
  597. j->map_entry = 0;
  598. j->obj = js->st[js->sp - 1].obj;
  599. j->dllpos = NULL;
  600. if (!(js->flags & LSSERJ_FLAG_OMIT_SCHEMA))
  601. /* we're actually at the same level */
  602. j->subsequent = 1;
  603. j->idt = 1;
  604. break;
  605. default:
  606. break;
  607. }
  608. switch (map->type) {
  609. case LSMT_STRING_CHAR_ARRAY:
  610. case LSMT_STRING_PTR:
  611. /*
  612. * This is a bit tricky... we have to escape the string
  613. * which may 6x its length depending on what the
  614. * contents are.
  615. *
  616. * We offset the unescaped string starting point first
  617. */
  618. q += js->offset;
  619. budget = strlen(q); /* how much unescaped is left */
  620. /*
  621. * This is going to escape as much as it can fit, and
  622. * let us know the amount of input that was consumed
  623. * in "used".
  624. */
  625. lws_json_purify((char *)buf, q, (int)len, &used);
  626. m = strlen((const char *)buf);
  627. buf += m;
  628. len -= m;
  629. js->remaining = budget - used;
  630. js->offset = used;
  631. if (!js->remaining)
  632. js->offset = 0;
  633. break;
  634. default:
  635. q += js->offset;
  636. budget -= js->remaining;
  637. if (budget > len) {
  638. js->remaining = budget - len;
  639. js->offset = len;
  640. budget = len;
  641. } else {
  642. js->remaining = 0;
  643. js->offset = 0;
  644. }
  645. memcpy(buf, q, budget);
  646. buf += budget;
  647. *buf = '\0';
  648. len -= budget;
  649. break;
  650. }
  651. switch (map->type) {
  652. case LSMT_STRING_CHAR_ARRAY:
  653. case LSMT_STRING_PTR:
  654. *buf++ = '\"';
  655. len--;
  656. break;
  657. case LSMT_SCHEMA:
  658. continue;
  659. default:
  660. break;
  661. }
  662. if (js->remaining)
  663. continue;
  664. up:
  665. if (++j->map_entry < j->map_entries)
  666. continue;
  667. if (!js->sp)
  668. continue;
  669. js->sp--;
  670. if (!js->sp) {
  671. lws_struct_pretty(js, &buf, &len);
  672. *buf++ = '}';
  673. len--;
  674. lws_struct_pretty(js, &buf, &len);
  675. break;
  676. }
  677. js->offset = 0;
  678. j = &js->st[js->sp];
  679. map = &j->map[j->map_entry];
  680. if (map->type == LSMT_CHILD_PTR) {
  681. lws_struct_pretty(js, &buf, &len);
  682. *buf++ = '}';
  683. len--;
  684. /* we have done the singular child pointer */
  685. js->offset = 0;
  686. goto up;
  687. }
  688. if (map->type != LSMT_LIST)
  689. continue;
  690. /*
  691. * we are coming back up to an array map, it means we should
  692. * advance to the next array member if there is one
  693. */
  694. lws_struct_pretty(js, &buf, &len);
  695. *buf++ = '}';
  696. len--;
  697. p = j->dllpos = j->dllpos->next;
  698. if (j->dllpos) {
  699. /*
  700. * there was another item in the array to do... let's
  701. * move on to that and do it
  702. */
  703. *buf++ = ',';
  704. len--;
  705. lws_struct_pretty(js, &buf, &len);
  706. js->offset = 0;
  707. j = &js->st[++js->sp];
  708. j->map_entry = 0;
  709. map = &j->map[j->map_entry];
  710. *buf++ = '{';
  711. len--;
  712. lws_struct_pretty(js, &buf, &len);
  713. j->subsequent = 0;
  714. j->obj = ((char *)p) - j->map->ofs_clist;
  715. continue;
  716. }
  717. /* there are no further items in the array */
  718. js->offset = 0;
  719. lws_struct_pretty(js, &buf, &len);
  720. *buf++ = ']';
  721. len--;
  722. goto up;
  723. }
  724. *written = olen - len;
  725. *buf = '\0'; /* convenience, a NUL after the official end */
  726. return LSJS_RESULT_FINISH;
  727. }