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