1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
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 * Stream parser for RFC8949 CBOR
25 */
26
27 #include "private-lib-core.h"
28 #include <string.h>
29 #include <stdio.h>
30
31 #if defined(LWS_WITH_CBOR_FLOAT)
32 #include <math.h>
33 #endif
34
35 #define lwsl_lecp lwsl_debug
36
37 static const char * const parser_errs[] = {
38 "",
39 "",
40 "Bad CBOR coding",
41 "Unknown",
42 "Parser callback errored (see earlier error)",
43 "Overflow"
44 };
45
46 enum lecp_states {
47 LECP_OPC,
48 LECP_COLLECT,
49 LECP_SIMPLEX8,
50 LECP_COLLATE,
51 LECP_ONLY_SAME
52 };
53
54 void
lecp_construct(struct lecp_ctx * ctx,lecp_callback cb,void * user,const char * const * paths,unsigned char count_paths)55 lecp_construct(struct lecp_ctx *ctx, lecp_callback cb, void *user,
56 const char * const *paths, unsigned char count_paths)
57 {
58 uint16_t x = 0x1234;
59
60 memset(ctx, 0, sizeof(*ctx) - sizeof(ctx->buf));
61
62 ctx->user = user;
63 ctx->pst[0].cb = cb;
64 ctx->pst[0].paths = paths;
65 ctx->pst[0].count_paths = count_paths;
66 ctx->be = *((uint8_t *)&x) == 0x12;
67
68 ctx->st[0].s = LECP_OPC;
69
70 ctx->pst[0].cb(ctx, LECPCB_CONSTRUCTED);
71 }
72
73 void
lecp_destruct(struct lecp_ctx * ctx)74 lecp_destruct(struct lecp_ctx *ctx)
75 {
76 /* no allocations... just let callback know what it happening */
77 if (ctx->pst[0].cb)
78 ctx->pst[0].cb(ctx, LECPCB_DESTRUCTED);
79 }
80
81 void
lecp_change_callback(struct lecp_ctx * ctx,lecp_callback cb)82 lecp_change_callback(struct lecp_ctx *ctx, lecp_callback cb)
83 {
84 ctx->pst[0].cb(ctx, LECPCB_DESTRUCTED);
85 ctx->pst[0].cb = cb;
86 ctx->pst[0].cb(ctx, LECPCB_CONSTRUCTED);
87 }
88
89
90 const char *
lecp_error_to_string(int e)91 lecp_error_to_string(int e)
92 {
93 if (e > 0)
94 e = 0;
95 else
96 e = -e;
97
98 if (e >= (int)LWS_ARRAY_SIZE(parser_errs))
99 return "Unknown error";
100
101 return parser_errs[e];
102 }
103
104 static void
ex(struct lecp_ctx * ctx,void * _start,size_t len)105 ex(struct lecp_ctx *ctx, void *_start, size_t len)
106 {
107 struct _lecp_stack *st = &ctx->st[ctx->sp];
108 uint8_t *start = (uint8_t *)_start;
109
110 st->s = LECP_COLLECT;
111 st->collect_rem = (uint8_t)len;
112
113 if (ctx->be)
114 ctx->collect_tgt = start;
115 else
116 ctx->collect_tgt = start + len - 1;
117 }
118
119 static void
lecp_check_path_match(struct lecp_ctx * ctx)120 lecp_check_path_match(struct lecp_ctx *ctx)
121 {
122 const char *p, *q;
123 size_t s = sizeof(char *);
124 int n;
125
126 if (ctx->path_stride)
127 s = ctx->path_stride;
128
129 /* we only need to check if a match is not active */
130 for (n = 0; !ctx->path_match &&
131 n < ctx->pst[ctx->pst_sp].count_paths; n++) {
132 ctx->wildcount = 0;
133 p = ctx->path;
134
135 q = *((char **)(((char *)ctx->pst[ctx->pst_sp].paths) +
136 ((unsigned int)n * s)));
137
138 while (*p && *q) {
139 if (*q != '*') {
140 if (*p != *q)
141 break;
142 p++;
143 q++;
144 continue;
145 }
146 ctx->wild[ctx->wildcount++] =
147 (uint16_t)lws_ptr_diff_size_t(p, ctx->path);
148 q++;
149 /*
150 * if * has something after it, match to .
151 * if ends with *, eat everything.
152 * This implies match sequences must be ordered like
153 * x.*.*
154 * x.*
155 * if both options are possible
156 */
157 while (*p && (*p != '.' || !*q))
158 p++;
159 }
160 if (*p || *q)
161 continue;
162
163 ctx->path_match = (uint8_t)(n + 1);
164 ctx->path_match_len = ctx->pst[ctx->pst_sp].ppos;
165 return;
166 }
167
168 if (!ctx->path_match)
169 ctx->wildcount = 0;
170 }
171
172 int
lecp_push(struct lecp_ctx * ctx,char s_start,char s_end,char state)173 lecp_push(struct lecp_ctx *ctx, char s_start, char s_end, char state)
174 {
175 struct _lecp_stack *st = &ctx->st[ctx->sp];
176
177 if (ctx->sp + 1 == LWS_ARRAY_SIZE(ctx->st))
178 return LECP_STACK_OVERFLOW;
179
180 if (s_start && ctx->pst[ctx->pst_sp].cb(ctx, s_start))
181 return LECP_REJECT_CALLBACK;
182
183 lwsl_lecp("%s: pushing from sp %d, parent "
184 "(opc %d, indet %d, collect_rem %d)\n",
185 __func__, ctx->sp, st->opcode >> 5, st->indet,
186 (int)st->collect_rem);
187
188
189 st->pop_iss = s_end; /* issue this when we pop back here */
190 ctx->st[ctx->sp + 1] = *st;
191 ctx->sp++;
192 st++;
193
194 st->s = state;
195 st->collect_rem = 0;
196 st->intermediate = 0;
197 st->indet = 0;
198 st->ordinal = 0;
199 st->send_new_array_item = 0;
200 st->barrier = 0;
201
202 return 0;
203 }
204
205 int
lecp_pop(struct lecp_ctx * ctx)206 lecp_pop(struct lecp_ctx *ctx)
207 {
208 struct _lecp_stack *st;
209
210 assert(ctx->sp);
211 ctx->sp--;
212
213 st = &ctx->st[ctx->sp];
214
215 if (st->pop_iss == LECPCB_ARRAY_END) {
216 assert(ctx->ipos);
217 ctx->ipos--;
218 }
219
220 ctx->pst[ctx->pst_sp].ppos = st->p;
221 ctx->path[st->p] = '\0';
222 lecp_check_path_match(ctx);
223
224 lwsl_lecp("%s: popping to sp %d, parent "
225 "(opc %d, indet %d, collect_rem %d)\n",
226 __func__, ctx->sp, st->opcode >> 5, st->indet,
227 (int)st->collect_rem);
228
229 if (st->pop_iss && ctx->pst[ctx->pst_sp].cb(ctx, st->pop_iss))
230 return LECP_REJECT_CALLBACK;
231
232 return 0;
233 }
234
235 static struct _lecp_stack *
lwcp_st_parent(struct lecp_ctx * ctx)236 lwcp_st_parent(struct lecp_ctx *ctx)
237 {
238 assert(ctx->sp);
239
240 return &ctx->st[ctx->sp - 1];
241 }
242
243 int
lwcp_completed(struct lecp_ctx * ctx,char indet)244 lwcp_completed(struct lecp_ctx *ctx, char indet)
245 {
246 int r, il = ctx->ipos;
247
248 ctx->st[ctx->sp].s = LECP_OPC;
249
250 while (ctx->sp && !ctx->st[ctx->sp].barrier) {
251 struct _lecp_stack *parent = lwcp_st_parent(ctx);
252
253 lwsl_lecp("%s: sp %d, parent "
254 "(opc %d, indet %d, collect_rem %d)\n",
255 __func__, ctx->sp, parent->opcode >> 5, parent->indet,
256 (int)parent->collect_rem);
257
258 parent->ordinal++;
259 if (parent->opcode == LWS_CBOR_MAJTYP_ARRAY) {
260 assert(il);
261 il--;
262 ctx->i[il]++;
263 if (!parent->send_new_array_item) {
264 if (ctx->pst[ctx->pst_sp].cb(ctx,
265 LECPCB_ARRAY_ITEM_END))
266 return LECP_REJECT_CALLBACK;
267 parent->send_new_array_item = 1;
268 }
269 }
270
271 if (!indet && parent->indet) {
272 lwsl_lecp("%s: abandoning walk as parent needs indet\n", __func__);
273 break;
274 }
275
276 if (!parent->indet && parent->collect_rem) {
277 parent->collect_rem--;
278 lwsl_lecp("%s: sp %d, parent (opc %d, indet %d, collect_rem -> %d)\n",
279 __func__, ctx->sp, parent->opcode >> 5, parent->indet, (int)parent->collect_rem);
280
281 if (parent->collect_rem) {
282 /* more items to come */
283 if (parent->opcode == LWS_CBOR_MAJTYP_ARRAY)
284 parent->send_new_array_item = 1;
285 break;
286 }
287 }
288
289 lwsl_lecp("%s: parent (opc %d) collect_rem became zero\n", __func__, parent->opcode >> 5);
290
291 ctx->st[ctx->sp - 1].s = LECP_OPC;
292 r = lecp_pop(ctx);
293 if (r)
294 return r;
295 indet = 0;
296 }
297
298 return 0;
299 }
300
301 static int
lwcp_is_indet_string(struct lecp_ctx * ctx)302 lwcp_is_indet_string(struct lecp_ctx *ctx)
303 {
304 if (ctx->st[ctx->sp].indet)
305 return 1;
306
307 if (!ctx->sp)
308 return 0;
309
310 if (lwcp_st_parent(ctx)->opcode != LWS_CBOR_MAJTYP_BSTR &&
311 lwcp_st_parent(ctx)->opcode != LWS_CBOR_MAJTYP_TSTR)
312 return 0;
313
314 if (ctx->st[ctx->sp - 1].indet)
315 return 1;
316
317 return 0;
318 }
319
320 static int
report_raw_cbor(struct lecp_ctx * ctx)321 report_raw_cbor(struct lecp_ctx *ctx)
322 {
323 struct _lecp_parsing_stack *pst = &ctx->pst[ctx->pst_sp];
324
325 if (!ctx->cbor_pos)
326 return 0;
327
328 if (pst->cb(ctx, LECPCB_LITERAL_CBOR))
329 return 1;
330
331 ctx->cbor_pos = 0;
332
333 return 0;
334 }
335
336 void
lecp_parse_report_raw(struct lecp_ctx * ctx,int on)337 lecp_parse_report_raw(struct lecp_ctx *ctx, int on)
338 {
339 ctx->literal_cbor_report = (uint8_t)on;
340 report_raw_cbor(ctx);
341 }
342
343 int
lecp_parse_map_is_key(struct lecp_ctx * ctx)344 lecp_parse_map_is_key(struct lecp_ctx *ctx)
345 {
346 return lwcp_st_parent(ctx)->opcode == LWS_CBOR_MAJTYP_MAP &&
347 !(lwcp_st_parent(ctx)->ordinal & 1);
348 }
349
350 int
lecp_parse_subtree(struct lecp_ctx * ctx,const uint8_t * in,size_t len)351 lecp_parse_subtree(struct lecp_ctx *ctx, const uint8_t *in, size_t len)
352 {
353 struct _lecp_stack *st = &ctx->st[++ctx->sp];
354 int n;
355
356 st->s = 0;
357 st->collect_rem = 0;
358 st->intermediate = 0;
359 st->indet = 0;
360 st->ordinal = 0;
361 st->send_new_array_item = 0;
362 st->barrier = 1;
363
364 n = lecp_parse(ctx, in, len);
365 ctx->sp--;
366
367 return n;
368 }
369
370 int
lecp_parse(struct lecp_ctx * ctx,const uint8_t * cbor,size_t len)371 lecp_parse(struct lecp_ctx *ctx, const uint8_t *cbor, size_t len)
372 {
373 size_t olen = len;
374 int ret;
375
376 while (len--) {
377 struct _lecp_parsing_stack *pst = &ctx->pst[ctx->pst_sp];
378 struct _lecp_stack *st = &ctx->st[ctx->sp];
379 uint8_t c, sm, o;
380 char to;
381
382 c = *cbor++;
383
384 /*
385 * for, eg, cose_sign, we sometimes need to collect subtrees of
386 * raw CBOR. Report buffers of it via the callback if we filled
387 * the buffer, or we stopped collecting.
388 */
389
390 if (ctx->literal_cbor_report) {
391 ctx->cbor[ctx->cbor_pos++] = c;
392 if (ctx->cbor_pos == sizeof(ctx->cbor) &&
393 report_raw_cbor(ctx))
394 goto reject_callback;
395 }
396
397 switch (st->s) {
398 /*
399 * We're getting the nex opcode
400 */
401 case LECP_OPC:
402 st->opcode = ctx->item.opcode = c & LWS_CBOR_MAJTYP_MASK;
403 sm = c & LWS_CBOR_SUBMASK;
404 to = 0;
405
406 lwsl_lecp("%s: %d: OPC %d|%d\n", __func__, ctx->sp,
407 c >> 5, sm);
408
409 if (c != 0xff && ctx->sp &&
410 ctx->st[ctx->sp - 1].send_new_array_item) {
411 ctx->st[ctx->sp - 1].send_new_array_item = 0;
412 if (ctx->pst[ctx->pst_sp].cb(ctx,
413 LECPCB_ARRAY_ITEM_START))
414 goto reject_callback;
415 }
416
417 switch (st->opcode) {
418 case LWS_CBOR_MAJTYP_UINT:
419 ctx->present = LECPCB_VAL_NUM_UINT;
420 if (sm < LWS_CBOR_1) {
421 ctx->item.u.i64 = (int64_t)sm;
422 goto issue;
423 }
424 goto i2;
425
426 case LWS_CBOR_MAJTYP_INT_NEG:
427 ctx->present = LECPCB_VAL_NUM_INT;
428 if (sm < 24) {
429 ctx->item.u.i64 = (-1ll) - (int64_t)sm;
430 goto issue;
431 }
432 i2:
433 if (sm >= LWS_CBOR_RESERVED)
434 goto bad_coding;
435 ctx->item.u.u64 = 0;
436 o = (uint8_t)(1 << (sm - LWS_CBOR_1));
437 ex(ctx, (uint8_t *)&ctx->item.u.u64, o);
438 break;
439
440 case LWS_CBOR_MAJTYP_BSTR:
441 to = LECPCB_VAL_BLOB_END - LECPCB_VAL_STR_END;
442
443 /* fallthru */
444
445 case LWS_CBOR_MAJTYP_TSTR:
446 /*
447 * The first thing is the string length, it's
448 * going to either be a byte count for the
449 * string or the indefinite length marker
450 * followed by determinite-length chunks of the
451 * same MAJTYP
452 */
453
454 ctx->npos = 0;
455 ctx->buf[0] = '\0';
456
457 if (!sm) {
458 if ((!ctx->sp || (ctx->sp &&
459 !ctx->st[ctx->sp - 1].intermediate)) &&
460 pst->cb(ctx, (char)(LECPCB_VAL_STR_START + to)))
461 goto reject_callback;
462
463 if (pst->cb(ctx, (char)(LECPCB_VAL_STR_END + to)))
464 goto reject_callback;
465 lwcp_completed(ctx, 0);
466 break;
467 }
468
469 if (sm < LWS_CBOR_1) {
470 ctx->item.u.u64 = (uint64_t)sm;
471 if ((!ctx->sp || (ctx->sp &&
472 !ctx->st[ctx->sp - 1].intermediate)) &&
473 pst->cb(ctx, (char)(LECPCB_VAL_STR_START + to)))
474 goto reject_callback;
475
476 st->indet = 0;
477 st->collect_rem = sm;
478 st->s = LECP_COLLATE;
479 break;
480 }
481
482 if (sm < LWS_CBOR_RESERVED)
483 goto i2;
484
485 if (sm != LWS_CBOR_INDETERMINITE)
486 goto bad_coding;
487
488 if ((!ctx->sp || (ctx->sp &&
489 !ctx->st[ctx->sp - 1].intermediate)) &&
490 pst->cb(ctx, (char)(LECPCB_VAL_STR_START + to)))
491 goto reject_callback;
492
493 st->indet = 1;
494
495 st->p = pst->ppos;
496 lecp_push(ctx, 0, (char)(LECPCB_VAL_STR_END + to),
497 LECP_ONLY_SAME);
498 break;
499
500 case LWS_CBOR_MAJTYP_ARRAY:
501 ctx->npos = 0;
502 ctx->buf[0] = '\0';
503
504 if (pst->ppos + 3u >= sizeof(ctx->path))
505 goto reject_overflow;
506
507 st->p = pst->ppos;
508 ctx->path[pst->ppos++] = '[';
509 ctx->path[pst->ppos++] = ']';
510 ctx->path[pst->ppos] = '\0';
511
512 lecp_check_path_match(ctx);
513
514 if (ctx->ipos + 1u >= LWS_ARRAY_SIZE(ctx->i))
515 goto reject_overflow;
516
517 ctx->i[ctx->ipos++] = 0;
518
519 if (pst->cb(ctx, LECPCB_ARRAY_START))
520 goto reject_callback;
521
522 if (!sm) {
523 if (pst->cb(ctx, LECPCB_ARRAY_END))
524 goto reject_callback;
525 pst->ppos = st->p;
526 ctx->path[pst->ppos] = '\0';
527 ctx->ipos--;
528 lecp_check_path_match(ctx);
529 lwcp_completed(ctx, 0);
530 break;
531 }
532
533 ctx->st[ctx->sp].send_new_array_item = 1;
534
535 if (sm < LWS_CBOR_1) {
536 st->indet = 0;
537 st->collect_rem = sm;
538 goto push_a;
539 }
540
541 if (sm < LWS_CBOR_RESERVED)
542 goto i2;
543
544 if (sm != LWS_CBOR_INDETERMINITE)
545 goto bad_coding;
546
547 st->indet = 1;
548 push_a:
549 lecp_push(ctx, 0, LECPCB_ARRAY_END, LECP_OPC);
550 break;
551
552 case LWS_CBOR_MAJTYP_MAP:
553 ctx->npos = 0;
554 ctx->buf[0] = '\0';
555
556 if (pst->ppos + 1u >= sizeof(ctx->path))
557 goto reject_overflow;
558
559 st->p = pst->ppos;
560 ctx->path[pst->ppos++] = '.';
561 ctx->path[pst->ppos] = '\0';
562
563 lecp_check_path_match(ctx);
564
565 if (pst->cb(ctx, LECPCB_OBJECT_START))
566 goto reject_callback;
567
568 if (!sm) {
569 if (pst->cb(ctx, LECPCB_OBJECT_END))
570 goto reject_callback;
571 pst->ppos = st->p;
572 ctx->path[pst->ppos] = '\0';
573 lecp_check_path_match(ctx);
574 lwcp_completed(ctx, 0);
575 break;
576 }
577 if (sm < LWS_CBOR_1) {
578 st->indet = 0;
579 st->collect_rem = (uint64_t)(sm * 2);
580 goto push_m;
581 }
582
583 if (sm < LWS_CBOR_RESERVED)
584 goto i2;
585
586 if (sm != LWS_CBOR_INDETERMINITE)
587 goto bad_coding;
588
589 st->indet = 1;
590 push_m:
591 lecp_push(ctx, 0, LECPCB_OBJECT_END, LECP_OPC);
592 break;
593
594 case LWS_CBOR_MAJTYP_TAG:
595 /* tag has one or another kind of int first */
596 if (sm < LWS_CBOR_1) {
597 /*
598 * We have a literal tag number, push
599 * to decode the tag body
600 */
601 ctx->item.u.u64 = st->tag = (uint64_t)sm;
602 goto start_tag_enclosure;
603 }
604 /*
605 * We have to do more stuff to get the tag
606 * number...
607 */
608 goto i2;
609
610 case LWS_CBOR_MAJTYP_FLOAT:
611 /*
612 * This can also be a bunch of specials as well
613 * as sizes of float...
614 */
615 sm = c & LWS_CBOR_SUBMASK;
616
617 switch (sm) {
618 case LWS_CBOR_SWK_FALSE:
619 ctx->present = LECPCB_VAL_FALSE;
620 goto issue;
621
622 case LWS_CBOR_SWK_TRUE:
623 ctx->present = LECPCB_VAL_TRUE;
624 goto issue;
625
626 case LWS_CBOR_SWK_NULL:
627 ctx->present = LECPCB_VAL_NULL;
628 goto issue;
629
630 case LWS_CBOR_SWK_UNDEFINED:
631 ctx->present = LECPCB_VAL_UNDEFINED;
632 goto issue;
633
634 case LWS_CBOR_M7_SUBTYP_SIMPLE_X8:
635 st->s = LECP_SIMPLEX8;
636 break;
637
638 case LWS_CBOR_M7_SUBTYP_FLOAT16:
639 ctx->present = LECPCB_VAL_FLOAT16;
640 ex(ctx, &ctx->item.u.hf, 2);
641 break;
642
643 case LWS_CBOR_M7_SUBTYP_FLOAT32:
644 ctx->present = LECPCB_VAL_FLOAT32;
645 ex(ctx, &ctx->item.u.f, 4);
646 break;
647
648 case LWS_CBOR_M7_SUBTYP_FLOAT64:
649 ctx->present = LECPCB_VAL_FLOAT64;
650 ex(ctx, &ctx->item.u.d, 8);
651 break;
652
653 case LWS_CBOR_M7_BREAK:
654 if (!ctx->sp ||
655 !ctx->st[ctx->sp - 1].indet)
656 goto bad_coding;
657
658 lwcp_completed(ctx, 1);
659 break;
660
661 default:
662 /* handle as simple */
663 ctx->item.u.u64 = (uint64_t)sm;
664 if (pst->cb(ctx, LECPCB_VAL_SIMPLE))
665 goto reject_callback;
666 break;
667 }
668 break;
669 }
670 break;
671
672 /*
673 * We're collecting int / float pieces
674 */
675 case LECP_COLLECT:
676 if (ctx->be)
677 *ctx->collect_tgt++ = c;
678 else
679 *ctx->collect_tgt-- = c;
680
681 if (--st->collect_rem)
682 break;
683
684 /*
685 * We collected whatever it was...
686 */
687
688 ctx->npos = 0;
689 ctx->buf[0] = '\0';
690
691 switch (st->opcode) {
692 case LWS_CBOR_MAJTYP_BSTR:
693 case LWS_CBOR_MAJTYP_TSTR:
694 st->collect_rem = ctx->item.u.u64;
695 if ((!ctx->sp || (ctx->sp &&
696 !ctx->st[ctx->sp - 1].intermediate)) &&
697 pst->cb(ctx, (char)((st->opcode ==
698 LWS_CBOR_MAJTYP_TSTR) ?
699 LECPCB_VAL_STR_START :
700 LECPCB_VAL_BLOB_START)))
701 goto reject_callback;
702 st->s = LECP_COLLATE;
703 break;
704
705 case LWS_CBOR_MAJTYP_ARRAY:
706 st->collect_rem = ctx->item.u.u64;
707 lecp_push(ctx, 0, LECPCB_ARRAY_END, LECP_OPC);
708 break;
709
710 case LWS_CBOR_MAJTYP_MAP:
711 st->collect_rem = ctx->item.u.u64 * 2;
712 lecp_push(ctx, 0, LECPCB_OBJECT_END, LECP_OPC);
713 break;
714
715 case LWS_CBOR_MAJTYP_TAG:
716 st->tag = ctx->item.u.u64;
717 goto start_tag_enclosure;
718
719 default:
720 /*
721 * ... then issue what we collected as a
722 * literal
723 */
724
725 if (st->opcode == LWS_CBOR_MAJTYP_INT_NEG)
726 ctx->item.u.i64 = (-1ll) - ctx->item.u.i64;
727
728 goto issue;
729 }
730 break;
731
732 case LECP_SIMPLEX8:
733 /*
734 * Extended SIMPLE byte for 7|24 opcode, no uses
735 * for it in RFC8949
736 */
737 if (c <= LWS_CBOR_INDETERMINITE)
738 /*
739 * Duplication of implicit simple values is
740 * denied by RFC8949 3.3
741 */
742 goto bad_coding;
743
744 ctx->item.u.u64 = (uint64_t)c;
745 if (pst->cb(ctx, LECPCB_VAL_SIMPLE))
746 goto reject_callback;
747
748 lwcp_completed(ctx, 0);
749 break;
750
751 case LECP_COLLATE:
752 /*
753 * let's grab b/t string content into the context
754 * buffer, and issue chunks from there
755 */
756
757 ctx->buf[ctx->npos++] = (char)c;
758 if (st->collect_rem)
759 st->collect_rem--;
760
761 /* spill at chunk boundaries, or if we filled the buf */
762 if (ctx->npos != sizeof(ctx->buf) - 1 &&
763 st->collect_rem)
764 break;
765
766 /* spill */
767 ctx->buf[ctx->npos] = '\0';
768
769 /* if it's a map name, deal with the path */
770 if (ctx->sp && lecp_parse_map_is_key(ctx)) {
771 if (lwcp_st_parent(ctx)->ordinal)
772 pst->ppos = st->p;
773 st->p = pst->ppos;
774 if (pst->ppos + ctx->npos > sizeof(ctx->path))
775 goto reject_overflow;
776 memcpy(&ctx->path[pst->ppos], ctx->buf,
777 (size_t)(ctx->npos + 1));
778 pst->ppos = (uint8_t)(pst->ppos + ctx->npos);
779 lecp_check_path_match(ctx);
780 }
781
782 to = 0;
783 if (ctx->item.opcode == LWS_CBOR_MAJTYP_BSTR)
784 to = LECPCB_VAL_BLOB_END - LECPCB_VAL_STR_END;
785
786 o = (uint8_t)(LECPCB_VAL_STR_END + to);
787 c = (st->collect_rem /* more to come at this layer */ ||
788 /* we or direct parent is indeterminite */
789 lwcp_is_indet_string(ctx));
790
791 if (ctx->sp)
792 ctx->st[ctx->sp - 1].intermediate = !!c;
793 if (c)
794 o--;
795
796 if (pst->cb(ctx, (char)o))
797 goto reject_callback;
798 ctx->npos = 0;
799 ctx->buf[0] = '\0';
800
801 if (ctx->sp && lwcp_st_parent(ctx)->indet)
802 st->s = LECP_OPC;
803 if (o == LECPCB_VAL_STR_END + to)
804 lwcp_completed(ctx, 0);
805
806 break;
807
808 case LECP_ONLY_SAME:
809 /*
810 * deterministic sized chunks same MAJTYP as parent
811 * level only (BSTR and TSTR frags inside interderminite
812 * BSTR or TSTR)
813 *
814 * Clean end when we see M7|31
815 */
816 if (!ctx->sp) {
817 /*
818 * We should only come here by pushing on stack
819 */
820 assert(0);
821 return LECP_STACK_OVERFLOW;
822 }
823
824 if (c == (LWS_CBOR_MAJTYP_FLOAT | LWS_CBOR_M7_BREAK)) {
825 /* if's the end of an interdetminite list */
826 if (!ctx->sp || !ctx->st[ctx->sp - 1].indet)
827 /*
828 * Can't have a break without an
829 * indeterminite parent
830 */
831 goto bad_coding;
832
833 if (lwcp_completed(ctx, 1))
834 goto reject_callback;
835 break;
836 }
837
838 if (st->opcode != lwcp_st_parent(ctx)->opcode)
839 /*
840 * Fragments have to be of the same type as the
841 * outer opcode
842 */
843 goto bad_coding;
844
845 sm = c & LWS_CBOR_SUBMASK;
846
847 if (sm == LWS_CBOR_INDETERMINITE)
848 /* indeterminite length frags not allowed */
849 goto bad_coding;
850
851 if (sm < LWS_CBOR_1) {
852 st->indet = 0;
853 st->collect_rem = (uint64_t)sm;
854 st->s = LECP_COLLATE;
855 break;
856 }
857
858 if (sm >= LWS_CBOR_RESERVED)
859 goto bad_coding;
860
861 goto i2;
862
863 default:
864 assert(0);
865 return -1;
866 }
867
868 continue;
869
870 start_tag_enclosure:
871 st->p = pst->ppos;
872 ret = lecp_push(ctx, LECPCB_TAG_START, LECPCB_TAG_END, LECP_OPC);
873 if (ret)
874 return ret;
875
876 continue;
877
878 issue:
879 if (ctx->item.opcode == LWS_CBOR_MAJTYP_TAG) {
880 st->tag = ctx->item.u.u64;
881 goto start_tag_enclosure;
882 }
883
884 /* we are just a number */
885
886 if (pst->cb(ctx, ctx->present))
887 goto reject_callback;
888
889 lwcp_completed(ctx, 0);
890
891 }
892
893 ctx->used_in = olen - len;
894
895 if (!ctx->sp && ctx->st[0].s == LECP_OPC)
896 return 0;
897
898 return LECP_CONTINUE;
899
900 reject_overflow:
901 ret = LECP_STACK_OVERFLOW;
902 goto reject;
903
904 bad_coding:
905 ret = LECP_REJECT_BAD_CODING;
906 goto reject;
907
908 reject_callback:
909 ret = LECP_REJECT_CALLBACK;
910
911 reject:
912 ctx->pst[ctx->pst_sp].cb(ctx, LECPCB_FAILED);
913
914 return ret;
915 }
916
917
918
919 void
lws_lec_init(lws_lec_pctx_t * ctx,uint8_t * buf,size_t len)920 lws_lec_init(lws_lec_pctx_t *ctx, uint8_t *buf, size_t len)
921 {
922 memset(ctx, 0, sizeof(*ctx));
923 ctx->start = ctx->buf = buf;
924 ctx->end = ctx->start + len;
925 ctx->fmt_pos = 0;
926 }
927
928 void
lws_lec_setbuf(lws_lec_pctx_t * ctx,uint8_t * buf,size_t len)929 lws_lec_setbuf(lws_lec_pctx_t *ctx, uint8_t *buf, size_t len)
930 {
931 ctx->start = ctx->buf = buf;
932 ctx->end = ctx->start + len;
933 ctx->used = 0;
934 ctx->vaa_pos = 0;
935 }
936
937 enum lws_lec_pctx_ret
lws_lec_printf(lws_lec_pctx_t * ctx,const char * format,...)938 lws_lec_printf(lws_lec_pctx_t *ctx, const char *format, ...)
939 {
940 enum lws_lec_pctx_ret r;
941 va_list ap;
942
943 va_start(ap, format);
944 r = lws_lec_vsprintf(ctx, format, ap);
945 va_end(ap);
946
947 return r;
948 }
949
950 /*
951 * Report how many next-level elements inbetween fmt[0] and the matching
952 * closure, eg, [] returns 0, [123] would return 1, [123,456] returns 2, and
953 * [123,{'a':[123,456]}] returns 2. Counts for { } maps are in pairs, ie,
954 * {'a':1, 'b': 2} returns 2
955 *
956 * If there is no closure in the string it returns -1
957 *
958 * We use this to figure out if we should use indeterminite lengths or specific
959 * lengths for items in the format string
960 */
961
962 #define bump(_r) count[sp]++
963 //; lwsl_notice("%s: count[%d] -> %d\n", _r, sp, count[sp])
964
965 static int
format_scan(const char * fmt)966 format_scan(const char *fmt)
967 {
968 char stack[12], literal = 0, numeric = 0;
969 int count[12], sp = 0, pc = 0, swallow = 0;
970
971 literal = *fmt == '\'';
972 stack[sp] = *fmt++;
973 count[sp] = 0;
974
975 // lwsl_notice("%s: start %s\n", __func__, fmt - 1);
976
977 while (*fmt) {
978
979 // lwsl_notice("%s: %c %d %d\n", __func__, *fmt, sp, literal);
980
981 if (swallow) {
982 swallow--;
983 fmt++;
984 continue;
985 }
986
987 if (numeric) {
988 if (*fmt >= '0' && *fmt <= '9')
989 fmt++;
990 numeric = 0;
991 if (*fmt != '(')
992 bump("a");
993 }
994
995 if (literal) {
996 if (*fmt == '\\' && fmt[1]) {
997 fmt += 2;
998 continue;
999 }
1000 if (*fmt == '\'') {
1001 literal = 0;
1002 if (!sp && stack[sp] == '\'')
1003 return count[sp];
1004
1005 if (sp)
1006 sp--;
1007 fmt++;
1008 continue;
1009 }
1010
1011 bump("b");
1012 fmt++;
1013 continue;
1014 }
1015
1016 if (*fmt == '\'') {
1017 bump("c");
1018 sp++;
1019 literal = 1;
1020 fmt++;
1021 continue;
1022 }
1023
1024 switch (pc) {
1025 case 1:
1026 if (*fmt == '.') {
1027 pc++;
1028 fmt++;
1029 continue;
1030 }
1031 if (*fmt == 'l') {
1032 pc++;
1033 fmt++;
1034 continue;
1035 }
1036 /* fallthru */
1037 case 2:
1038 if (*fmt == '*') {
1039 pc++;
1040 fmt++;
1041 continue;
1042 }
1043 if (*fmt == 'l') {
1044 pc++;
1045 fmt++;
1046 continue;
1047 }
1048 /* fallthru */
1049 case 3:
1050 bump("pc");
1051 pc = 0;
1052 fmt++;
1053 continue;
1054 }
1055
1056 switch (*fmt) {
1057
1058 case '<':
1059 swallow = 1;
1060 /* fallthru */
1061 case '[':
1062 case '(':
1063 case '{':
1064 if (sp == sizeof(stack))
1065 return -2;
1066
1067 bump("d");
1068 sp++;
1069 stack[sp] = *fmt;
1070 count[sp] = 0;
1071 break;
1072 case ' ':
1073 break;
1074 case ',':
1075 //count[sp]++;
1076 break;
1077 case ':':
1078 if (stack[sp] != '{')
1079 goto mismatch;
1080 //count[sp]++;
1081 break;
1082 case '%':
1083 pc = 1;
1084 break;
1085 case ']':
1086 if (stack[sp] != '[')
1087 goto mismatch;
1088 goto pop;
1089 case ')':
1090 if (stack[sp] != '(')
1091 goto mismatch;
1092 goto pop;
1093 case '}':
1094 if (stack[sp] != '{')
1095 goto mismatch;
1096 goto pop;
1097 case '>':
1098 if (stack[sp] != '<')
1099 goto mismatch;
1100 pop:
1101 if (sp) {
1102 sp--;
1103 break;
1104 }
1105
1106 if (stack[0] == '{') {
1107 /* args have to come in pairs */
1108 if (count[0] & 1) {
1109 lwsl_err("%s: odd map args %d %s\n",
1110 __func__, count[0], fmt);
1111 return -2;
1112 }
1113 // lwsl_notice("%s: return %d pairs\n", __func__, count[0] >> 1);
1114 /* report how many pairs */
1115 return count[0] >> 1;
1116 }
1117
1118 // lwsl_notice("%s: return %d items\n", __func__, count[0]);
1119
1120 return count[0];
1121
1122 case '0':
1123 case '1':
1124 case '2':
1125 case '3':
1126 case '4':
1127 case '5':
1128 case '6':
1129 case '7':
1130 case '8':
1131 case '9':
1132 numeric = 1;
1133
1134 break;
1135
1136 default:
1137 bump("e");
1138 break;
1139 }
1140 fmt++;
1141 }
1142
1143 return -1;
1144
1145 mismatch:
1146 lwsl_err("%s: format mismatch %c %c\n", __func__, stack[sp], *fmt);
1147
1148 return -2;
1149 }
1150
1151 void
lws_lec_signed(lws_lec_pctx_t * ctx,int64_t num)1152 lws_lec_signed(lws_lec_pctx_t *ctx, int64_t num)
1153 {
1154 if (num < 0)
1155 lws_lec_int(ctx, LWS_CBOR_MAJTYP_INT_NEG, 0,
1156 (uint64_t)(-1ll - num));
1157 else
1158 lws_lec_int(ctx, LWS_CBOR_MAJTYP_UINT, 0, (uint64_t)num);
1159 }
1160
1161 void
lws_lec_int(lws_lec_pctx_t * ctx,uint8_t opcode,uint8_t indet,uint64_t num)1162 lws_lec_int(lws_lec_pctx_t *ctx, uint8_t opcode, uint8_t indet, uint64_t num)
1163 {
1164 uint8_t hint = 0;
1165 unsigned int n;
1166
1167 if (indet) {
1168 ctx->scratch[ctx->scratch_len++] = (uint8_t)(opcode |
1169 LWS_CBOR_INDETERMINITE);
1170 return;
1171 }
1172
1173 if ((opcode & LWS_CBOR_MAJTYP_MASK) == LWS_CBOR_MAJTYP_FLOAT) {
1174 hint = opcode & LWS_CBOR_SUBMASK;
1175 switch (hint) {
1176 case LWS_CBOR_M7_SUBTYP_FLOAT16:
1177 num <<= 48;
1178 break;
1179 case LWS_CBOR_M7_SUBTYP_FLOAT32:
1180 num <<= 32;
1181 break;
1182 }
1183 } else {
1184
1185 if (num < LWS_CBOR_1) {
1186 ctx->scratch[ctx->scratch_len++] = (uint8_t)(opcode | num);
1187 return;
1188 }
1189
1190 if (!(num & (uint64_t)(~0xffull))) {
1191 hint = LWS_CBOR_1;
1192 num <<= 56;
1193 } else
1194 if (!(num & (uint64_t)(~0xffffull))) {
1195 hint = LWS_CBOR_2;
1196 num <<= 48;
1197 } else
1198 if (!(num & (uint64_t)(~0xffffffffull))) {
1199 hint = LWS_CBOR_4;
1200 num <<= 32;
1201 }
1202 else
1203 hint = LWS_CBOR_8;
1204 }
1205
1206 ctx->scratch[ctx->scratch_len++] = (uint8_t)(opcode | hint);
1207 n = 1u << (hint - LWS_CBOR_1);
1208 while (n--) {
1209 ctx->scratch[ctx->scratch_len++] = (uint8_t)(num >> 56);
1210 num <<= 8;
1211 }
1212 }
1213
1214 enum {
1215 NATTYPE_INT,
1216 NATTYPE_LONG,
1217 NATTYPE_LONG_LONG,
1218 NATTYPE_PTR,
1219 NATTYPE_DOUBLE,
1220 };
1221
1222 int
lws_lec_scratch(lws_lec_pctx_t * ctx)1223 lws_lec_scratch(lws_lec_pctx_t *ctx)
1224 {
1225 size_t s;
1226
1227 if (!ctx->scratch_len)
1228 return 0;
1229
1230 s = lws_ptr_diff_size_t(ctx->end, ctx->buf);
1231 if (s > (size_t)ctx->scratch_len)
1232 s = (size_t)ctx->scratch_len;
1233
1234 memcpy(ctx->buf, ctx->scratch, s);
1235 ctx->buf += s;
1236 ctx->scratch_len = (uint8_t)(ctx->scratch_len - (uint8_t)s);
1237
1238 return ctx->buf == ctx->end;
1239 }
1240
1241 enum lws_lec_pctx_ret
lws_lec_vsprintf(lws_lec_pctx_t * ctx,const char * fmt,va_list args)1242 lws_lec_vsprintf(lws_lec_pctx_t *ctx, const char *fmt, va_list args)
1243 {
1244 size_t fl = strlen(fmt);
1245 uint64_t u64;
1246 int64_t i64;
1247 #if defined(LWS_WITH_CBOR_FLOAT)
1248 double dbl;
1249 #endif
1250 size_t s;
1251 char c;
1252 int n;
1253
1254 /*
1255 * We might be being called after the first time, since we had to emit
1256 * output buffer(s) before we could move on in the format string. For
1257 * this case, reposition ourselves at the vaarg we got to from the last
1258 * call.
1259 */
1260
1261 for (n = 0; n < ctx->vaa_pos; n++) {
1262
1263 switch (ctx->vaa[n]) {
1264 case NATTYPE_INT:
1265 (void)va_arg(args, int);
1266 break;
1267 case NATTYPE_LONG:
1268 (void)va_arg(args, long);
1269 break;
1270 case NATTYPE_LONG_LONG:
1271 (void)va_arg(args, long long);
1272 break;
1273 case NATTYPE_PTR:
1274 (void)va_arg(args, const char *);
1275 break;
1276 case NATTYPE_DOUBLE:
1277 (void)va_arg(args, double);
1278 break;
1279 }
1280 if (ctx->state == CBPS_STRING_BODY)
1281 /*
1282 * when copying out text or binary strings, we reload
1283 * the %s or %.*s pointer on subsequent calls, in case
1284 * it was on the stack. The length and contents should
1285 * not change between calls, but it's OK if the source
1286 * address does.
1287 */
1288 ctx->ongoing_src = va_arg(args, uint8_t *);
1289 }
1290
1291 while (ctx->buf != ctx->end) {
1292
1293 /*
1294 * We write small things into the context scratch array, then
1295 * copy that into the output buffer fragmenting as needed. Next
1296 * time we will finish emptying the scratch into the output
1297 * buffer preferentially.
1298 *
1299 * Then we don't otherwise have to handle fragmentations in
1300 * order to exactly fill the output buffer, simplifying
1301 * everything else.
1302 */
1303
1304 if (lws_lec_scratch(ctx))
1305 break;
1306
1307 if (ctx->fmt_pos >= fl) {
1308 if (ctx->state == CBPS_IDLE)
1309 break;
1310 c = 0;
1311 } else
1312 c = fmt[ctx->fmt_pos];
1313
1314 // lwsl_notice("%s: %d %d %c\n", __func__, ctx->state, ctx->sp, c);
1315
1316 switch (ctx->state) {
1317 case CBPS_IDLE:
1318 ctx->scratch_len = 0;
1319 switch (c) {
1320 case '[':
1321 n = format_scan(&fmt[ctx->fmt_pos]);
1322 if (n == -2)
1323 return LWS_LECPCTX_RET_FAIL;
1324 lws_lec_int(ctx, LWS_CBOR_MAJTYP_ARRAY, n == -1,
1325 (uint64_t)n);
1326 goto stack_push;
1327 case '{':
1328 n = format_scan(&fmt[ctx->fmt_pos]);
1329 if (n == -2)
1330 return LWS_LECPCTX_RET_FAIL;
1331 lws_lec_int(ctx, LWS_CBOR_MAJTYP_MAP, n == -1,
1332 (uint64_t)n);
1333 goto stack_push;
1334 case '(':
1335 /* must be preceded by a number */
1336 goto fail;
1337
1338 case '<': /* <t or <b */
1339 ctx->state = CBPS_CONTYPE;
1340 break;
1341
1342 case ']':
1343 if (!ctx->sp || ctx->stack[ctx->sp - 1] != '[')
1344 return LWS_LECPCTX_RET_FAIL;
1345 ctx->sp--;
1346 break;
1347 case '}':
1348 if (!ctx->sp || ctx->stack[ctx->sp - 1] != '{')
1349 return LWS_LECPCTX_RET_FAIL;
1350 ctx->sp--;
1351 break;
1352 case ')':
1353 if (!ctx->sp || ctx->stack[ctx->sp - 1] != '(') {
1354 lwsl_notice("bad tag end %d %c\n",
1355 ctx->sp, ctx->stack[ctx->sp - 1]);
1356 goto fail;
1357 }
1358 ctx->sp--;
1359 break;
1360 case '>':
1361 if (!ctx->sp || ctx->stack[ctx->sp - 1] != '<')
1362 return LWS_LECPCTX_RET_FAIL;
1363 ctx->scratch[ctx->scratch_len++] =
1364 (uint8_t)(LWS_CBOR_MAJTYP_FLOAT |
1365 LWS_CBOR_M7_BREAK);
1366 ctx->sp--;
1367 break;
1368 case '\'':
1369 n = format_scan(&fmt[ctx->fmt_pos]);
1370 // lwsl_notice("%s: quote fs %d\n", __func__, n);
1371 if (n < 0)
1372 return LWS_LECPCTX_RET_FAIL;
1373 lws_lec_int(ctx, LWS_CBOR_MAJTYP_TSTR, 0,
1374 (uint64_t)n);
1375 ctx->state = CBPS_STRING_LIT;
1376 break;
1377 case '%':
1378 if (ctx->vaa_pos >= sizeof(ctx->vaa) - 1) {
1379 lwsl_err("%s: too many %%\n", __func__);
1380 goto fail;
1381 }
1382 ctx->_long = 0;
1383 ctx->dotstar = 0;
1384 ctx->state = CBPS_PC1;
1385 break;
1386 case ':':
1387 break;
1388 case ',':
1389 break;
1390 case '-':
1391 ctx->item.opcode = LWS_CBOR_MAJTYP_INT_NEG;
1392 ctx->item.u.i64 = 0;
1393 ctx->state = CBPS_NUM_LIT;
1394 break;
1395 case '0':
1396 case '1':
1397 case '2':
1398 case '3':
1399 case '4':
1400 case '5':
1401 case '6':
1402 case '7':
1403 case '8':
1404 case '9':
1405 ctx->item.opcode = LWS_CBOR_MAJTYP_UINT;
1406 ctx->item.u.u64 = (uint64_t)(c - '0');
1407 ctx->state = CBPS_NUM_LIT;
1408 break;
1409 }
1410 break;
1411 case CBPS_PC1:
1412 if (c == 'l') {
1413 ctx->_long++;
1414 ctx->state = CBPS_PC2;
1415 break;
1416 }
1417 if (c == '.') {
1418 ctx->dotstar++;
1419 ctx->state = CBPS_PC2;
1420 break;
1421 }
1422 /* fallthru */
1423
1424 case CBPS_PC2:
1425 if (c == 'l') {
1426 ctx->_long++;
1427 ctx->state = CBPS_PC3;
1428 break;
1429 }
1430 if (c == '*') {
1431 ctx->dotstar++;
1432 ctx->state = CBPS_PC3;
1433 break;
1434 }
1435 /* fallthru */
1436
1437 case CBPS_PC3:
1438 switch (c) {
1439 case 'd':
1440 switch (ctx->_long) {
1441 case 0:
1442 i64 = (int64_t)va_arg(args, int);
1443 ctx->vaa[ctx->vaa_pos++] = NATTYPE_INT;
1444 break;
1445 case 1:
1446 i64 = (int64_t)va_arg(args, long);
1447 ctx->vaa[ctx->vaa_pos++] = NATTYPE_LONG;
1448 break;
1449 case 2:
1450 i64 = (int64_t)va_arg(args, long long);
1451 ctx->vaa[ctx->vaa_pos++] = NATTYPE_LONG_LONG;
1452 break;
1453 }
1454 if (i64 < 0)
1455 lws_lec_int(ctx,
1456 LWS_CBOR_MAJTYP_INT_NEG, 0,
1457 (uint64_t)(-1ll - i64));
1458 else
1459 lws_lec_int(ctx,
1460 LWS_CBOR_MAJTYP_UINT, 0,
1461 (uint64_t)i64);
1462 break;
1463 case 'u':
1464 switch (ctx->_long) {
1465 case 0:
1466 u64 = (uint64_t)va_arg(args, unsigned int);
1467 ctx->vaa[ctx->vaa_pos++] = NATTYPE_INT;
1468 break;
1469 case 1:
1470 u64 = (uint64_t)va_arg(args, unsigned long);
1471 ctx->vaa[ctx->vaa_pos++] = NATTYPE_LONG;
1472 break;
1473 case 2:
1474 u64 = (uint64_t)va_arg(args, unsigned long long);
1475 ctx->vaa[ctx->vaa_pos++] = NATTYPE_LONG_LONG;
1476 break;
1477 }
1478 lws_lec_int(ctx, LWS_CBOR_MAJTYP_UINT, 0, u64);
1479 break;
1480 case 's': /* text string */
1481 ctx->ongoing_done = 0;
1482 if (ctx->dotstar == 2) {
1483 ctx->ongoing_len = (uint64_t)va_arg(args, int);
1484 ctx->vaa[ctx->vaa_pos++] = NATTYPE_INT;
1485 }
1486 /* vaa for ptr done at end of body copy */
1487 ctx->ongoing_src = va_arg(args, uint8_t *);
1488 if (ctx->dotstar != 2)
1489 ctx->ongoing_len = (uint64_t)strlen(
1490 (const char *)ctx->ongoing_src);
1491 lws_lec_int(ctx, LWS_CBOR_MAJTYP_TSTR, 0, ctx->ongoing_len);
1492 ctx->state = CBPS_STRING_BODY;
1493 ctx->fmt_pos++;
1494 continue;
1495 case 'b': /* binary string (%.*b only) */
1496 if (ctx->dotstar != 2)
1497 goto fail;
1498 ctx->vaa[ctx->vaa_pos++] = NATTYPE_INT;
1499 ctx->ongoing_done = 0;
1500 ctx->ongoing_len = (uint64_t)va_arg(args, int);
1501 /* vaa for ptr done at end of body copy */
1502 ctx->ongoing_src = va_arg(args, uint8_t *);
1503 lws_lec_int(ctx, LWS_CBOR_MAJTYP_BSTR, 0, ctx->ongoing_len);
1504 ctx->state = CBPS_STRING_BODY;
1505 ctx->fmt_pos++;
1506 continue;
1507 case 't': /* dynamic tag */
1508 switch (ctx->_long) {
1509 case 0:
1510 ctx->item.u.u64 = (uint64_t)va_arg(args, int);
1511 ctx->vaa[ctx->vaa_pos++] = NATTYPE_INT;
1512 break;
1513 case 1:
1514 ctx->item.u.u64 = (uint64_t)va_arg(args, long);
1515 ctx->vaa[ctx->vaa_pos++] = NATTYPE_LONG;
1516 break;
1517 case 2:
1518 ctx->item.u.u64 = (uint64_t)va_arg(args, long long);
1519 ctx->vaa[ctx->vaa_pos++] = NATTYPE_LONG_LONG;
1520 break;
1521 }
1522 ctx->item.opcode = LWS_CBOR_MAJTYP_UINT;
1523 ctx->fmt_pos++;
1524 if (ctx->fmt_pos >= fl)
1525 continue;
1526 c = fmt[ctx->fmt_pos];
1527 if (c != '(')
1528 goto fail;
1529 goto tag_body;
1530 #if defined(LWS_WITH_CBOR_FLOAT)
1531 case 'f': /* floating point double */
1532 dbl = va_arg(args, double);
1533
1534 if (dbl == (float)dbl) {
1535 uint16_t hf;
1536 union {
1537 uint32_t ui;
1538 float f;
1539 } u1, u2;
1540
1541 u1.f = (float)dbl;
1542 lws_singles2halfp(&hf, u1.ui);
1543 lws_halfp2singles(&u2.ui, hf);
1544
1545 if ((isinf(u1.f) && isinf(u2.f)) ||
1546 (isnan(u1.f) && isnan(u2.f)) ||
1547 u1.f == u2.f) {
1548 lws_lec_int(ctx,
1549 LWS_CBOR_MAJTYP_FLOAT |
1550 LWS_CBOR_M7_SUBTYP_FLOAT16,
1551 0, hf);
1552 break;
1553 }
1554 /* do it as 32-bit float */
1555 lws_lec_int(ctx,
1556 LWS_CBOR_MAJTYP_FLOAT |
1557 LWS_CBOR_M7_SUBTYP_FLOAT32,
1558 0, u1.ui);
1559 break;
1560 }
1561
1562 /* do it as 64-bit double */
1563
1564 {
1565 union {
1566 uint64_t ui;
1567 double f;
1568 } u3;
1569
1570 u3.f = dbl;
1571 lws_lec_int(ctx,
1572 LWS_CBOR_MAJTYP_FLOAT |
1573 LWS_CBOR_M7_SUBTYP_FLOAT64,
1574 0, u3.ui);
1575 }
1576 break;
1577 #else
1578 case 'f':
1579 lwsl_err("%s: no FP support\n", __func__);
1580 goto fail;
1581 #endif
1582 }
1583 ctx->state = CBPS_IDLE;
1584 break;
1585
1586 case CBPS_STRING_BODY:
1587 s = lws_ptr_diff_size_t(ctx->end, ctx->buf);
1588 if (s > (size_t)(ctx->ongoing_len - ctx->ongoing_done))
1589 s = (size_t)(ctx->ongoing_len - ctx->ongoing_done);
1590 memcpy(ctx->buf, ctx->ongoing_src + ctx->ongoing_done, s);
1591 ctx->buf += s;
1592 ctx->ongoing_done += s;
1593 if (ctx->ongoing_len == ctx->ongoing_done) {
1594 /* vaa for ptr */
1595 ctx->vaa[ctx->vaa_pos++] = NATTYPE_PTR;
1596 ctx->state = CBPS_IDLE;
1597 }
1598 continue;
1599
1600 case CBPS_NUM_LIT:
1601 if (c >= '0' && c <= '9') {
1602 ctx->item.u.u64 = (ctx->item.u.u64 * 10) +
1603 (uint64_t)(c - '0');
1604 break;
1605 }
1606
1607 if (ctx->item.opcode == LWS_CBOR_MAJTYP_INT_NEG)
1608 ctx->item.u.i64--;
1609
1610 if (c == '(') { /* tag qualifier */
1611 tag_body:
1612 n = format_scan(&fmt[ctx->fmt_pos]);
1613 if (n == -2)
1614 goto fail;
1615 /*
1616 * inteterminite length not possible for tag,
1617 * take it to mean that the closure is in a
1618 * later format string
1619 */
1620
1621 lws_lec_int(ctx, LWS_CBOR_MAJTYP_TAG, 0,
1622 ctx->item.u.u64);
1623
1624 stack_push:
1625 if (ctx->sp >= sizeof(ctx->stack))
1626 return LWS_LECPCTX_RET_FAIL;
1627 ctx->stack[ctx->sp] = (uint8_t)c;
1628 ctx->indet[ctx->sp++] = (uint8_t)(n == -1);
1629 // lwsl_notice("%s: pushed %c\n", __func__, c);
1630 ctx->state = CBPS_IDLE;
1631 break;
1632 }
1633
1634 lws_lec_int(ctx, ctx->item.opcode, 0, ctx->item.u.u64);
1635
1636 ctx->state = CBPS_IDLE;
1637 /* deal with the terminating char fresh */
1638 continue;
1639
1640 case CBPS_STRING_LIT:
1641 if (!ctx->escflag && c == '\\') {
1642 ctx->escflag = 1;
1643 break;
1644 }
1645 if (!ctx->escflag && c == '\'') {
1646 ctx->state = CBPS_IDLE;
1647 break;
1648 }
1649
1650 *ctx->buf++ = (uint8_t)c;
1651 ctx->escflag = 0;
1652
1653 break;
1654
1655 case CBPS_CONTYPE:
1656 if (c != 't' && c != 'b')
1657 return LWS_LECPCTX_RET_FAIL;
1658
1659 lws_lec_int(ctx, c == 't' ? LWS_CBOR_MAJTYP_TSTR :
1660 LWS_CBOR_MAJTYP_BSTR, 1, 0);
1661 c = '<';
1662 n = 0;
1663 goto stack_push;
1664 }
1665
1666 ctx->fmt_pos++;
1667 }
1668
1669 ctx->used = lws_ptr_diff_size_t(ctx->buf, ctx->start);
1670 // lwsl_notice("%s: ctx->used %d\n", __func__, (int)ctx->used);
1671
1672 if (ctx->buf == ctx->end || ctx->scratch_len)
1673 return LWS_LECPCTX_RET_AGAIN;
1674
1675 ctx->fmt_pos = 0;
1676 ctx->vaa_pos = 0;
1677
1678 return LWS_LECPCTX_RET_FINISHED;
1679
1680 fail:
1681 lwsl_notice("%s: failed\n", __func__);
1682
1683 ctx->fmt_pos = 0;
1684
1685 return LWS_LECPCTX_RET_FAIL;
1686 }
1687