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 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 15321 15322 15323 15324 15325 15326 15327 15328 15329 15330 15331 15332 15333 15334 15335 15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 15347 15348 15349 15350 15351 15352 15353 15354 15355 15356 15357 15358 15359 15360 15361 15362 15363 15364 15365 15366 15367 15368 15369 15370 15371 15372 15373 15374 15375 15376 15377 15378 15379 15380 15381 15382 15383 15384 15385 15386 15387 15388 15389 15390 15391 15392 15393 15394 15395 15396 15397 15398 15399 15400 15401 15402 15403 15404 15405 15406 15407 15408 15409 15410 15411 15412 15413 15414 15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 15463 15464 15465 15466 15467 15468 15469 15470 15471 15472 15473 15474 15475 15476 15477 15478 15479 15480 15481 15482 15483 15484 15485 15486 15487 15488 15489 15490 15491 15492 15493 15494 15495 15496 15497 15498 15499 15500 15501 15502 15503 15504 15505 15506 15507 15508 15509 15510 15511 15512 15513 15514 15515 15516 15517 15518 15519 15520 15521 15522 15523 15524 15525 15526 15527 15528 15529 15530 15531 15532 15533 15534 15535 15536 15537 15538 15539 15540 15541 15542 15543 15544 15545 15546 15547 15548 15549 15550 15551 15552 15553 15554 15555 15556 15557 15558 15559 15560 15561 15562 15563 15564 15565 15566 15567 15568 15569 15570 15571 15572 15573 15574 15575 15576 15577 15578 15579 15580 15581 15582 15583 15584 15585 15586 15587 15588 15589 15590 15591 15592 15593 15594 15595 15596 15597 15598 15599 15600 15601 15602 15603 15604 15605 15606 15607 15608 15609 15610 15611 15612 15613 15614 15615 15616 15617 15618 15619 15620 15621 15622 15623 15624 15625 15626 15627 15628 15629 15630 15631 15632 15633 15634 15635 15636 15637 15638 15639 15640 15641 15642 15643 15644 15645 15646 15647 15648 15649 15650 15651 15652 15653 15654 15655 15656 15657 15658 15659 15660 15661 15662 15663 15664 15665 15666 15667 15668 15669 15670 15671 15672 15673 15674 15675 15676 15677 15678 15679 15680 15681 15682 15683 15684 15685 15686 15687 15688 15689 15690 15691 15692 15693 15694 15695 15696 15697 15698 15699 15700 15701 15702 15703 15704 15705 15706 15707 15708 15709 15710 15711 15712 15713 15714 15715 15716 15717 15718 15719 15720 15721 15722 15723 15724 15725 15726 15727 15728 15729 15730 15731 15732 15733 15734 15735 15736 15737 15738 15739 15740 15741 15742 15743 15744 15745 15746 15747 15748 15749 15750 15751 15752 15753 15754 15755 15756 15757 15758 15759 15760 15761 15762 15763 15764 15765 15766 15767 15768 15769 15770 15771 15772 15773 15774 15775 15776 15777 15778 15779 15780 15781 15782 15783 15784 15785 15786 15787 15788 15789 15790 15791 15792 15793 15794 15795 15796 15797 15798 15799 15800 15801 15802 15803 15804 15805 15806 15807 15808 15809 15810 15811 15812 15813 15814 15815 15816 15817 15818 15819 15820 15821 15822 15823 15824 15825 15826 15827 15828 15829 15830 15831 15832 15833 15834 15835 15836 15837 15838 15839 15840 15841 15842 15843 15844 15845 15846 15847 15848 15849 15850 15851 15852 15853 15854 15855 15856 15857 15858 15859 15860 15861 15862 15863 15864 15865 15866 15867 15868 15869 15870 15871 15872 15873 15874 15875 15876 15877 15878 15879 15880 15881 15882 15883 15884 15885 15886 15887 15888 15889 15890 15891 15892 15893 15894 15895 15896 15897 15898 15899 15900 15901 15902 15903 15904 15905 15906 15907 15908 15909 15910 15911 15912 15913 15914 15915 15916 15917 15918 15919 15920 15921 15922 15923 15924 15925 15926 15927 15928 15929 15930 15931 15932 15933 15934 15935 15936 15937 15938 15939 15940 15941 15942 15943 15944 15945 15946 15947 15948 15949 15950 15951 15952 15953 15954 15955 15956 15957 15958 15959 15960 15961 15962 15963 15964 15965 15966 15967 15968 15969 15970 15971 15972 15973 15974 15975 15976 15977 15978 15979 15980 15981 15982 15983 15984 15985 15986 15987 15988 15989 15990 15991 15992 15993 15994 15995 15996 15997 15998 15999 16000 16001 16002 16003 16004 16005 16006 16007 16008 16009 16010 16011 16012 16013 16014 16015 16016 16017 16018 16019 16020 16021 16022 16023 16024 16025 16026 16027 16028 16029 16030 16031 16032 16033 16034 16035 16036 16037 16038 16039 16040 16041 16042 16043 16044 16045 16046 16047 16048 16049 16050 16051 16052 16053 16054 16055 16056 16057 16058 16059 16060 16061 16062 16063 16064 16065 16066 16067 16068 16069 16070 16071 16072 16073 16074 16075 16076 16077 16078 16079 16080 16081 16082 16083 16084 16085 16086 16087 16088 16089 16090 16091 16092 16093 16094 16095 16096 16097 16098 16099 16100 16101 16102 16103 16104 16105 16106 16107 16108 16109 16110 16111 16112 16113 16114 16115 16116 16117 16118 16119 16120 16121 16122 16123 16124 16125 16126 16127 16128 16129 16130 16131 16132 16133 16134 16135 16136 16137 16138 16139 16140 16141 16142 16143 16144 16145 16146 16147 16148 16149 16150 16151 16152 16153 16154 16155 16156 16157 16158 16159 16160 16161 16162 16163 16164 16165 16166 16167 16168 16169 16170 16171 16172 16173 16174 16175 16176 16177 16178 16179 16180 16181 16182 16183 16184 16185 16186 16187 16188 16189 16190 16191 16192 16193 16194 16195 16196 16197 16198 16199 16200 16201 16202 16203 16204 16205 16206 16207 16208 16209 16210 16211 16212 16213 16214 16215 16216 16217 16218 16219 16220 16221 16222 16223 16224 16225 16226 16227 16228 16229 16230 16231 16232 16233 16234 16235 16236 16237 16238 16239 16240 16241 16242 16243 16244 16245 16246 16247 16248 16249 16250 16251 16252 16253 16254 16255 16256 16257 16258 16259 16260 16261 16262 16263 16264 16265 16266 16267 16268 16269 16270 16271 16272 16273 16274 16275 16276 16277 16278 16279 16280 16281 16282 16283 16284 16285 16286 16287 16288 16289 16290 16291 16292 16293 16294 16295 16296 16297 16298 16299 16300 16301 16302 16303 16304 16305 16306 16307 16308 16309 16310 16311 16312 16313 16314 16315 16316 16317 16318 16319 16320 16321 16322 16323 16324 16325 16326 16327 16328 16329 16330 16331 16332 16333 16334 16335 16336 16337 16338 16339 16340 16341 16342 16343 16344 16345 16346 16347 16348 16349 16350 16351 16352 16353 16354 16355 16356 16357 16358 16359 16360 16361 16362 16363 16364 16365 16366 16367 16368 16369 16370 16371 16372 16373 16374 16375 16376 16377 16378 16379 16380 16381 16382 16383 16384 16385 16386 16387 16388 16389 16390 16391 16392 16393 16394 16395 16396 16397 16398 16399 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16410 16411 16412 16413 16414 16415 16416 16417 16418 16419 16420 16421 16422 16423 16424 16425 16426 16427 16428 16429 16430 16431 16432 16433 16434 16435 16436 16437 16438 16439 16440 16441 16442 16443 16444 16445 16446 16447 16448 16449 16450 16451 16452 16453 16454 16455 16456 16457 16458 16459 16460 16461 16462 16463 16464 16465 16466 16467 16468 16469 16470 16471 16472 16473 16474 16475 16476 16477 16478 16479 16480 16481 16482 16483 16484 16485 16486 16487 16488 16489 16490 16491 16492 16493 16494 16495 16496 16497 16498 16499 16500 16501 16502 16503 16504 16505 16506 16507 16508 16509 16510 16511 16512 16513 16514 16515 16516 16517 16518 16519 16520 16521 16522 16523 16524 16525 16526 16527 16528 16529 16530 16531 16532 16533 16534 16535 16536 16537 16538 16539 16540 16541 16542 16543 16544 16545 16546 16547 16548 16549 16550 16551 16552 16553 16554 16555 16556 16557 16558 16559 16560 16561 16562 16563 16564 16565 16566 16567 16568 16569 16570 16571 16572 16573 16574 16575 16576 16577 16578 16579 16580 16581 16582 16583 16584 16585 16586 16587 16588 16589 16590 16591 16592 16593 16594 16595 16596 16597 16598 16599 16600 16601 16602 16603 16604 16605 16606 16607 16608 16609 16610 16611 16612 16613 16614 16615 16616 16617 16618 16619 16620 16621 16622 16623 16624 16625 16626 16627 16628 16629 16630 16631 16632 16633 16634 16635 16636 16637 16638 16639 16640 16641 16642 16643 16644 16645 16646 16647 16648 16649 16650 16651 16652 16653 16654 16655 16656 16657 16658 16659 16660 16661 16662 16663 16664 16665 16666 16667 16668 16669 16670 16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 16719 16720 16721 16722 16723 16724 16725 16726 16727 16728 16729 16730 16731 16732 16733 16734 16735 16736 16737 16738 16739 16740 16741 16742 16743 16744 16745 16746 16747 16748 16749 16750 16751 16752 16753 16754 16755 16756 16757 16758 16759 16760 16761 16762 16763 16764 16765 16766 16767 16768 16769 16770 16771 16772 16773 16774 16775 16776 16777 16778 16779 16780 16781 16782 16783 16784 16785 16786 16787 16788 16789 16790 16791 16792 16793 16794 16795 16796 16797 16798 16799 16800 16801 16802 16803 16804 16805 16806 16807 16808 16809 16810 16811 16812 16813 16814 16815 16816 16817 16818 16819 16820 16821 16822 16823 16824 16825 16826 16827 16828 16829 16830 16831 16832 16833 16834 16835 16836 16837 16838 16839 16840 16841 16842 16843 16844 16845 16846 16847 16848 16849 16850 16851 16852 16853 16854 16855 16856 16857 16858 16859 16860 16861 16862 16863 16864 16865 16866 16867 16868 16869 16870 16871 16872 16873 16874 16875 16876 16877 16878 16879 16880 16881 16882 16883 16884 16885 16886 16887 16888 16889 16890 16891 16892 16893 16894 16895 16896 16897 16898 16899 16900 16901 16902 16903 16904 16905 16906 16907 16908 16909 16910 16911 16912 16913 16914 16915 16916 16917 16918 16919 16920 16921 16922 16923 16924 16925 16926 16927 16928 16929 16930 16931 16932 16933 16934 16935 16936 16937 16938 16939 16940 16941 16942 16943 16944 16945 16946 16947 16948 16949 16950 16951 16952 16953 16954 16955 16956 16957 16958 16959 16960 16961 16962 16963 16964 16965 16966 16967 16968 16969 16970 16971 16972 16973 16974 16975 16976 16977 16978 16979 16980 16981 16982 16983 16984 16985 16986 16987 16988 16989 16990 16991 16992 16993 16994 16995 16996 16997 16998 16999 17000 17001 17002 17003 17004 17005 17006 17007 17008 17009 17010 17011 17012 17013 17014 17015 17016 17017 17018 17019 17020 17021 17022 17023 17024 17025 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 17036 17037 17038 17039 17040 17041 17042 17043 17044 17045 17046 17047 17048 17049 17050 17051 17052 17053 17054 17055 17056 17057 17058 17059 17060 17061 17062 17063 17064 17065 17066 17067 17068 17069 17070 17071 17072 17073 17074 17075 17076 17077 17078 17079 17080 17081 17082 17083 17084 17085 17086 17087 17088 17089 17090 17091 17092 17093 17094 17095 17096 17097 17098 17099 17100 17101 17102 17103 17104 17105 17106 17107 17108 17109 17110 17111 17112 17113 17114 17115 17116 17117 17118 17119 17120 17121 17122 17123 17124 17125 17126 17127 17128 17129 17130 17131 17132 17133 17134 17135 17136 17137 17138 17139 17140 17141 17142 17143 17144 17145 17146 17147 17148 17149 17150 17151 17152 17153 17154 17155 17156 17157 17158 17159 17160 17161 17162 17163 17164 17165 17166 17167 17168 17169 17170 17171 17172 17173 17174 17175 17176 17177 17178 17179 17180 17181 17182 17183 17184 17185 17186 17187 17188 17189 17190 17191 17192 17193 17194 17195 17196 17197 17198 17199 17200 17201 17202 17203 17204 17205 17206 17207 17208 17209 17210 17211 17212 17213 17214 17215 17216 17217 17218 17219 17220 17221 17222 17223 17224 17225 17226 17227 17228 17229 17230 17231 17232 17233 17234 17235 17236 17237 17238 17239 17240 17241 17242 17243 17244 17245 17246 17247 17248 17249 17250 17251 17252 17253 17254 17255 17256 17257 17258 17259 17260 17261 17262 17263 17264 17265 17266 17267 17268 17269 17270 17271 17272 17273 17274 17275 17276 17277 17278 17279 17280 17281 17282 17283 17284 17285 17286 17287 17288 17289 17290 17291 17292 17293 17294 17295 17296 17297 17298 17299 17300 17301 17302 17303 17304 17305 17306 17307 17308 17309 17310 17311 17312 17313 17314 17315 17316 17317 17318 17319 17320 17321 17322 17323 17324 17325 17326 17327 17328 17329 17330 17331 17332 17333 17334 17335 17336 17337 17338 17339 17340 17341 17342 17343 17344 17345 17346 17347 17348 17349 17350 17351 17352 17353 17354 17355 17356 17357 17358 17359 17360 17361 17362 17363 17364 17365 17366 17367 17368 17369 17370 17371 17372 17373 17374 17375 17376 17377 17378 17379 17380 17381 17382 17383 17384 17385 17386 17387 17388 17389 17390 17391 17392 17393 17394 17395 17396 17397 17398 17399 17400 17401 17402 17403 17404 17405 17406 17407 17408 17409 17410 17411 17412 17413 17414 17415 17416 17417 17418 17419 17420 17421 17422 17423 17424 17425 17426 17427 17428 17429 17430 17431 17432 17433 17434 17435 17436 17437 17438 17439 17440 17441 17442 17443 17444 17445 17446 17447 17448 17449 17450 17451 17452 17453 17454 17455 17456 17457 17458 17459 17460 17461 17462 17463 17464 17465 17466 17467 17468 17469 17470 17471 17472 17473 17474 17475 17476 17477 17478 17479 17480 17481 17482 17483 17484 17485 17486 17487 17488 17489 17490 17491 17492 17493 17494 17495 17496 17497 17498 17499 17500 17501 17502 17503 17504 17505 17506 17507 17508 17509 17510 17511 17512 17513 17514 17515 17516 17517 17518 17519 17520 17521 17522 17523 17524 17525 17526 17527 17528 17529 17530 17531 17532 17533 17534 17535 17536 17537 17538 17539 17540 17541 17542 17543 17544 17545 17546 17547 17548 17549 17550 17551 17552 17553 17554 17555 17556 17557 17558 17559 17560 17561 17562 17563 17564 17565 17566 17567 17568 17569 17570 17571 17572 17573 17574 17575 17576 17577 17578 17579 17580 17581 17582 17583 17584 17585 17586 17587 17588 17589 17590 17591 17592 17593 17594 17595 17596 17597 17598 17599 17600 17601 17602 17603 17604 17605 17606 17607 17608 17609 17610 17611 17612 17613 17614 17615 17616 17617 17618 17619 17620 17621 17622 17623 17624 17625 17626 17627 17628 17629 17630 17631 17632 17633 17634 17635 17636 17637 17638 17639 17640 17641 17642 17643 17644 17645 17646 17647 17648 17649 17650 17651 17652 17653 17654 17655 17656 17657 17658 17659 17660 17661 17662 17663 17664 17665 17666 17667 17668 17669 17670 17671 17672 17673 17674 17675 17676 17677 17678 17679 17680 17681 17682 17683 17684 17685 17686 17687 17688 17689 17690 17691 17692 17693 17694 17695 17696 17697 17698 17699 17700 17701 17702 17703 17704 17705 17706 17707 17708 17709 17710 17711 17712 17713 17714 17715 17716 17717 17718 17719 17720 17721 17722 17723 17724 17725 17726 17727 17728 17729 17730 17731 17732 17733 17734 17735 17736 17737 17738 17739 17740 17741 17742 17743 17744 17745 17746 17747 17748 17749 17750 17751 17752 17753 17754 17755 17756 17757 17758 17759 17760 17761 17762 17763 17764 17765 17766 17767 17768 17769 17770 17771 17772 17773 17774 17775 17776 17777 17778 17779 17780 17781 17782 17783 17784 17785 17786 17787 17788 17789 17790 17791 17792 17793 17794 17795 17796 17797 17798 17799 17800 17801 17802 17803 17804 17805 17806 17807 17808 17809 17810 17811 17812 17813 17814 17815 17816 17817 17818 17819 17820 17821 17822 17823 17824 17825 17826 17827 17828 17829 17830 17831 17832 17833 17834 17835 17836 17837 17838 17839 17840 17841 17842 17843 17844 17845 17846 17847 17848 17849 17850 17851 17852 17853 17854 17855 17856 17857 17858 17859 17860 17861 17862 17863 17864 17865 17866 17867 17868 17869 17870 17871 17872 17873 17874 17875 17876 17877 17878 17879 17880 17881 17882 17883 17884 17885 17886 17887 17888 17889 17890 17891 17892 17893 17894 17895 17896 17897 17898 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918 17919 17920 17921 17922 17923 17924 17925 17926 17927 17928 17929 17930 17931 17932 17933 17934 17935 17936 17937 17938 17939 17940 17941 17942 17943 17944 17945 17946 17947 17948 17949 17950 17951 17952 17953 17954 17955 17956 17957 17958 17959 17960 17961 17962 17963 17964 17965 17966 17967 17968 17969 17970 17971 17972 17973 17974 17975 17976 17977 17978 17979 17980 17981 17982 17983 17984 17985 17986 17987 17988 17989 17990 17991 17992 17993 17994 17995 17996 17997 17998 17999 18000 18001 18002 18003 18004 18005 18006 18007 18008 18009 18010 18011 18012 18013 18014 18015 18016 18017 18018 18019 18020 18021 18022 18023 18024 18025 18026 18027 18028 18029 18030 18031 18032 18033 18034 18035 18036 18037 18038 18039 18040 18041 18042 18043 18044 18045 18046 18047 18048 18049 18050 18051 18052 18053 18054 18055 18056 18057 18058 18059 18060 18061 18062 18063 18064 18065 18066 18067 18068 18069 18070 18071 18072 18073 18074 18075 18076 18077 18078 18079 18080 18081 18082 18083 18084 18085 18086 18087 18088 18089 18090 18091 18092 18093 18094 18095 18096 18097 18098 18099 18100 18101 18102 18103 18104 18105 18106 18107 18108 18109 18110 18111 18112 18113 18114 18115 18116 18117 18118 18119 18120 18121 18122 18123 18124 18125 18126 18127 18128 18129 18130 18131 18132 18133 18134 18135 18136 18137 18138 18139 18140 18141 18142 18143 18144 18145 18146 18147 18148 18149 18150 18151 18152 18153 18154 18155 18156 18157 18158 18159 18160 18161 18162 18163 18164 18165 18166 18167 18168 18169 18170 18171 18172 18173 18174 18175 18176 18177 18178 18179 18180 18181 18182 18183 18184 18185 18186 18187 18188 18189 18190 18191 18192 18193 18194 18195 18196 18197 18198 18199 18200 18201 18202 18203 18204 18205 18206 18207 18208 18209 18210 18211 18212 18213 18214 18215 18216 18217 18218 18219 18220 18221 18222 18223 18224 18225 18226 18227 18228 18229 18230 18231 18232 18233 18234 18235 18236 18237 18238 18239 18240 18241 18242 18243 18244 18245 18246 18247 18248 18249 18250 18251 18252 18253 18254 18255 18256 18257 18258 18259 18260 18261 18262 18263 18264 18265 18266 18267 18268 18269 18270 18271 18272 18273 18274 18275 18276 18277 18278 18279 18280 18281 18282 18283 18284 18285 18286 18287 18288 18289 18290 18291 18292 18293 18294 18295 18296 18297 18298 18299 18300 18301 18302 18303 18304 18305
|
/* Amalgamated source file */
#include "ruby-upb.h"
/*
* This is where we define internal portability macros used across upb.
*
* All of these macros are undef'd in undef.inc to avoid leaking them to users.
*
* The correct usage is:
*
* #include "upb/foobar.h"
* #include "upb/baz.h"
*
* // MUST be last included header.
* #include "upb/port/def.inc"
*
* // Code for this file.
* // <...>
*
* // Can be omitted for .c files, required for .h.
* #include "upb/port/undef.inc"
*
* This file is private and must not be included by users!
*/
#if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
(defined(__cplusplus) && __cplusplus >= 201703L) || \
(defined(_MSC_VER) && _MSC_VER >= 1900))
#error upb requires C99 or C++17 or MSVC >= 2015.
#endif
// Portable check for GCC minimum version:
// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
#define UPB_GNUC_MIN(x, y) \
(__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y))
#else
#define UPB_GNUC_MIN(x, y) 0
#endif
#include <assert.h>
#include <setjmp.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef UINTPTR_MAX
Error, UINTPTR_MAX is undefined
#endif
#if UINTPTR_MAX == 0xffffffff
#define UPB_SIZE(size32, size64) size32
#else
#define UPB_SIZE(size32, size64) size64
#endif
/* If we always read/write as a consistent type to each address, this shouldn't
* violate aliasing.
*/
#define UPB_PTR_AT(msg, ofs, type) ((type *)((char *)(msg) + (ofs)))
// A flexible array member may have lower alignment requirements than the struct
// overall - in that case, it can overlap with the trailing padding of the rest
// of the struct, and a naive sizeof(base) + sizeof(flex) * count calculation
// will not take into account that overlap, and allocate more than is required.
#define UPB_SIZEOF_FLEX(type, member, count) \
UPB_MAX(sizeof(type), offsetof(type, member[count]))
#define UPB_SIZEOF_FLEX_WOULD_OVERFLOW(type, member, count) \
(((SIZE_MAX - offsetof(type, member[0])) / \
(offsetof(type, member[1]) - offsetof(type, member[0]))) < (size_t)count)
#define UPB_MAPTYPE_STRING 0
// UPB_EXPORT: always generate a public symbol.
#if defined(__GNUC__) || defined(__clang__)
#define UPB_EXPORT __attribute__((visibility("default"))) __attribute__((used))
#else
#define UPB_EXPORT
#endif
// UPB_INLINE: inline if possible, emit standalone code if required.
#ifdef __cplusplus
#define UPB_INLINE inline
#elif defined(__GNUC__) || defined(__clang__)
#define UPB_INLINE static __inline__
#else
#define UPB_INLINE static
#endif
// UPB_INLINE_IF_NOT_GCC: because gcc can be very noisy at times.
#if defined(__GNUC__) && !defined(__clang__)
#define UPB_INLINE_IF_NOT_GCC static
#else
#define UPB_INLINE_IF_NOT_GCC UPB_INLINE
#endif
#ifdef UPB_BUILD_API
#define UPB_API UPB_EXPORT
#define UPB_API_INLINE UPB_EXPORT
#else
#define UPB_API
#define UPB_API_INLINE UPB_INLINE
#endif
#ifdef EXPORT_UPBC
#define UPBC_API UPB_EXPORT
#else
#define UPBC_API
#endif
#define UPB_MALLOC_ALIGN 8
#define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
#define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
#define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, UPB_MALLOC_ALIGN)
#ifdef __clang__
#define UPB_ALIGN_OF(type) _Alignof(type)
#else
#define UPB_ALIGN_OF(type) \
offsetof( \
struct { \
char c; \
type member; \
}, \
member)
#endif
#ifdef _MSC_VER
// Some versions of our Windows compiler don't support the C11 syntax.
#define UPB_ALIGN_AS(x) __declspec(align(x))
#elif defined(__GNUC__)
#define UPB_ALIGN_AS(x) __attribute__((aligned(x)))
#else
#define UPB_ALIGN_AS(x) _Alignas(x)
#endif
// Hints to the compiler about likely/unlikely branches.
#if defined(__GNUC__) || defined(__clang__)
#define UPB_LIKELY(x) __builtin_expect((bool)(x), 1)
#define UPB_UNLIKELY(x) __builtin_expect((bool)(x), 0)
#else
#define UPB_LIKELY(x) (x)
#define UPB_UNLIKELY(x) (x)
#endif
#ifdef __has_builtin
#if __has_builtin(__builtin_expect_with_probability)
#define UPB_UNPREDICTABLE(x) \
__builtin_expect_with_probability((bool)(x), 1, 0.5)
#else
#define UPB_UNPREDICTABLE(x) (x)
#endif
#else
#define UPB_UNPREDICTABLE(x) (x)
#endif
// Macros for function attributes on compilers that support them.
#if defined(__GNUC__) || defined(__clang__)
#define UPB_FORCEINLINE __inline__ __attribute__((always_inline)) static
#define UPB_NOINLINE __attribute__((noinline))
#define UPB_NORETURN __attribute__((__noreturn__))
#define UPB_PRINTF(str, first_vararg) \
__attribute__((format(printf, str, first_vararg)))
#elif defined(_MSC_VER)
#define UPB_NOINLINE
#define UPB_FORCEINLINE static
#define UPB_NORETURN __declspec(noreturn)
#define UPB_PRINTF(str, first_vararg)
#else /* !defined(__GNUC__) */
#define UPB_FORCEINLINE static
#define UPB_NOINLINE
#define UPB_NORETURN
#define UPB_PRINTF(str, first_vararg)
#endif
#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
#define UPB_UNUSED(var) (void)var
// UPB_ASSUME(): in release mode, we tell the compiler to assume this is true.
#ifdef NDEBUG
#ifdef __GNUC__
#define UPB_ASSUME(expr) \
if (!(expr)) __builtin_unreachable()
#elif defined _MSC_VER
#define UPB_ASSUME(expr) \
if (!(expr)) __assume(0)
#else
#define UPB_ASSUME(expr) \
do { \
} while (false && (expr))
#endif
#else
#define UPB_ASSUME(expr) assert(expr)
#endif
/* UPB_ASSERT(): in release mode, we use the expression without letting it be
* evaluated. This prevents "unused variable" warnings. */
#ifdef NDEBUG
#define UPB_ASSERT(expr) \
do { \
} while (false && (expr))
#else
#define UPB_ASSERT(expr) assert(expr)
#endif
#if defined(__GNUC__) || defined(__clang__)
#define UPB_UNREACHABLE() \
do { \
assert(0); \
__builtin_unreachable(); \
} while (0)
#elif defined(_MSC_VER)
#define UPB_UNREACHABLE() \
do { \
assert(0); \
__assume(0); \
} while (0)
#else
#define UPB_UNREACHABLE() \
do { \
assert(0); \
} while (0)
#endif
#ifdef __ANDROID__
#define UPB_DEFAULT_MAX_BLOCK_SIZE 8192
#else
#define UPB_DEFAULT_MAX_BLOCK_SIZE 32768
#endif
/* UPB_SETJMP() / UPB_LONGJMP() */
// Android uses a custom libc that does not implement all of posix, but it has
// had sigsetjmp/siglongjmp forever on arm and since API 12 on x86. Apple has
// sigsetjmp, but does not define the posix feature test macro.
#if defined(__APPLE__) || defined(_POSIX_C_SOURCE) || defined(__ANDROID__)
// avoid setting/restoring signal mask, which involves costly syscalls
#define UPB_SETJMP(buf) sigsetjmp(buf, 0)
#define UPB_LONGJMP(buf, val) siglongjmp(buf, val)
#elif defined(WASM_WAMR)
#define UPB_SETJMP(buf) 0
#define UPB_LONGJMP(buf, val) abort()
#else
#define UPB_SETJMP(buf) setjmp(buf)
#define UPB_LONGJMP(buf, val) longjmp(buf, val)
#endif
#if ((__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__))
#define UPB_USE_C11_ATOMICS
#elif defined(__has_extension)
#if __has_extension(c_atomic)
#define UPB_USE_C11_ATOMICS
#endif
#elif defined(__GNUC__)
// GCC supported atomics as an extension before it supported __has_extension
#define UPB_USE_C11_ATOMICS
#elif defined(_MSC_VER)
#define UPB_USE_MSC_ATOMICS
#endif
#if defined(UPB_USE_C11_ATOMICS)
#define UPB_ATOMIC(T) _Atomic(T)
#elif defined(UPB_USE_MSC_ATOMICS)
#define UPB_ATOMIC(T) volatile T
#else
#define UPB_ATOMIC(T) T
#endif
/* UPB_PTRADD(ptr, ofs): add pointer while avoiding "NULL + 0" UB */
#define UPB_PTRADD(ptr, ofs) ((ofs) ? (ptr) + (ofs) : (ptr))
#define UPB_PRIVATE(x) x##_dont_copy_me__upb_internal_use_only
#ifdef UPB_ALLOW_PRIVATE_ACCESS__FOR_BITS_ONLY
#define UPB_ONLYBITS(x) x
#else
#define UPB_ONLYBITS(x) UPB_PRIVATE(x)
#endif
/* Configure whether fasttable is switched on or not. *************************/
#ifdef __has_attribute
#define UPB_HAS_ATTRIBUTE(x) __has_attribute(x)
#else
#define UPB_HAS_ATTRIBUTE(x) 0
#endif
#if UPB_HAS_ATTRIBUTE(musttail)
#define UPB_MUSTTAIL __attribute__((musttail))
#else
#define UPB_MUSTTAIL
#endif
#undef UPB_HAS_ATTRIBUTE
/* This check is not fully robust: it does not require that we have "musttail"
* support available. We need tail calls to avoid consuming arbitrary amounts
* of stack space.
*
* GCC/Clang can mostly be trusted to generate tail calls as long as
* optimization is enabled, but, debug builds will not generate tail calls
* unless "musttail" is available.
*
* We should probably either:
* 1. require that the compiler supports musttail.
* 2. add some fallback code for when musttail isn't available (ie. return
* instead of tail calling). This is safe and portable, but this comes at
* a CPU cost.
*/
#if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__)
#define UPB_FASTTABLE_SUPPORTED 1
#else
#define UPB_FASTTABLE_SUPPORTED 0
#endif
/* define UPB_ENABLE_FASTTABLE to force fast table support.
* This is useful when we want to ensure we are really getting fasttable,
* for example for testing or benchmarking. */
#if defined(UPB_ENABLE_FASTTABLE)
#if !UPB_FASTTABLE_SUPPORTED
#error fasttable is x86-64/ARM64 only and requires GCC or Clang.
#endif
#define UPB_FASTTABLE 1
/* Define UPB_TRY_ENABLE_FASTTABLE to use fasttable if possible.
* This is useful for releasing code that might be used on multiple platforms,
* for example the PHP or Ruby C extensions. */
#elif defined(UPB_TRY_ENABLE_FASTTABLE)
#define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED
#else
#define UPB_FASTTABLE 0
#endif
/* UPB_FASTTABLE_INIT() allows protos compiled for fasttable to gracefully
* degrade to non-fasttable if the runtime or platform do not support it. */
#if !UPB_FASTTABLE
#define UPB_FASTTABLE_INIT(...)
#define UPB_FASTTABLE_MASK(mask) -1
#else
#define UPB_FASTTABLE_INIT(...) __VA_ARGS__
#define UPB_FASTTABLE_MASK(mask) mask
#endif
#undef UPB_FASTTABLE_SUPPORTED
/* ASAN poisoning (for arena).
* If using UPB from an interpreted language like Ruby, a build of the
* interpreter compiled with ASAN enabled must be used in order to get sane and
* expected behavior.
*/
/* Due to preprocessor limitations, the conditional logic for setting
* UPB_CLANG_ASAN below cannot be consolidated into a portable one-liner.
* See https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005fattribute.html.
*/
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define UPB_CLANG_ASAN 1
#else
#define UPB_CLANG_ASAN 0
#endif
#if __has_feature(thread_sanitizer)
#define UPB_CLANG_TSAN 1
#else
#define UPB_CLANG_TSAN 0
#endif
#else
#define UPB_CLANG_ASAN 0
#define UPB_CLANG_TSAN 0
#endif
#if defined(__SANITIZE_ADDRESS__) || UPB_CLANG_ASAN
#define UPB_ASAN 1
#define UPB_ASAN_GUARD_SIZE 32
#ifdef __cplusplus
extern "C" {
#endif
void __asan_poison_memory_region(void const volatile *addr, size_t size);
void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#ifdef __cplusplus
} /* extern "C" */
#endif
#define UPB_POISON_MEMORY_REGION(addr, size) \
__asan_poison_memory_region((addr), (size))
#define UPB_UNPOISON_MEMORY_REGION(addr, size) \
__asan_unpoison_memory_region((addr), (size))
#else
#define UPB_ASAN 0
#define UPB_ASAN_GUARD_SIZE 0
#define UPB_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#define UPB_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
#endif
#if defined(__SANITIZE_THREAD__) || UPB_CLANG_TSAN
#define UPB_TSAN_PUBLISHED_MEMBER uintptr_t upb_tsan_safely_published;
#define UPB_TSAN_INIT_PUBLISHED(ptr) (ptr)->upb_tsan_safely_published = 0x5AFE
#define UPB_TSAN_CHECK_PUBLISHED(ptr) \
UPB_ASSERT((ptr)->upb_tsan_safely_published == 0x5AFE)
#define UPB_TSAN_PUBLISH 1
#define UPB_TSAN_CHECK_READ(member) \
__asm__ volatile("" ::"r"(*(char *)&(member)))
#define UPB_TSAN_CHECK_WRITE(member) \
do { \
char *write_upb_tsan_detect_race_ptr = (char *)&(member); \
char write_upb_tsan_detect_race = *write_upb_tsan_detect_race_ptr; \
__asm__ volatile("" : "+r"(write_upb_tsan_detect_race)); \
*write_upb_tsan_detect_race_ptr = write_upb_tsan_detect_race; \
} while (false)
#else
#define UPB_TSAN_PUBLISHED_MEMBER
#define UPB_TSAN_INIT_PUBLISHED(ptr)
#define UPB_TSAN_CHECK_PUBLISHED(ptr) \
do { \
} while (false && (ptr))
#define UPB_TSAN_PUBLISH 0
#define UPB_TSAN_CHECK_READ(member) \
do { \
} while (false && (member))
#define UPB_TSAN_CHECK_WRITE(member) \
do { \
} while (false && (member))
#endif
/* Disable proto2 arena behavior (TEMPORARY) **********************************/
#ifdef UPB_DISABLE_CLOSED_ENUM_CHECKING
#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 1
#else
#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 0
#endif
#if defined(__cplusplus)
#if defined(__clang__) || UPB_GNUC_MIN(6, 0)
// https://gcc.gnu.org/gcc-6/changes.html
#if __cplusplus >= 201402L
#define UPB_DEPRECATED [[deprecated]]
#else
#define UPB_DEPRECATED __attribute__((deprecated))
#endif
#else
#define UPB_DEPRECATED
#endif
#else
#define UPB_DEPRECATED
#endif
#if defined(UPB_IS_GOOGLE3) && \
(!defined(UPB_BOOTSTRAP_STAGE) || UPB_BOOTSTRAP_STAGE != 0)
#define UPB_DESC(sym) proto2_##sym
#define UPB_DESC_MINITABLE(sym) &proto2__##sym##_msg_init
#elif defined(UPB_IS_GOOGLE3) && defined(UPB_BOOTSTRAP_STAGE) && \
UPB_BOOTSTRAP_STAGE == 0
#define UPB_DESC(sym) proto2_##sym
#define UPB_DESC_MINITABLE(sym) proto2__##sym##_msg_init()
#elif defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0
#define UPB_DESC(sym) google_protobuf_##sym
#define UPB_DESC_MINITABLE(sym) google__protobuf__##sym##_msg_init()
#else
#define UPB_DESC(sym) google_protobuf_##sym
#define UPB_DESC_MINITABLE(sym) &google__protobuf__##sym##_msg_init
#endif
#undef UPB_IS_GOOGLE3
#ifdef __clang__
#define UPB_NO_SANITIZE_ADDRESS __attribute__((no_sanitize("address")))
#else
#define UPB_NO_SANITIZE_ADDRESS
#endif
// Linker arrays combine elements from multiple translation units into a single
// array that can be iterated over at runtime.
//
// It is an alternative to pre-main "registration" functions.
//
// Usage:
//
// // In N translation units.
// UPB_LINKARR_APPEND(foo_array) static int elems[3] = {1, 2, 3};
//
// // At runtime:
// UPB_LINKARR_DECLARE(foo_array, int);
//
// void f() {
// const int* start = UPB_LINKARR_START(foo_array);
// const int* stop = UPB_LINKARR_STOP(foo_array);
// for (const int* p = start; p < stop; p++) {
// // Windows can introduce zero padding, so we have to skip zeroes.
// if (*p != 0) {
// vec.push_back(*p);
// }
// }
// }
#if defined(__ELF__) || defined(__wasm__)
#define UPB_LINKARR_APPEND(name) \
__attribute__((retain, used, \
section("linkarr_" #name))) UPB_NO_SANITIZE_ADDRESS
#define UPB_LINKARR_DECLARE(name, type) \
extern type __start_linkarr_##name; \
extern type __stop_linkarr_##name; \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1]
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#elif defined(__MACH__)
/* As described in: https://stackoverflow.com/a/22366882 */
#define UPB_LINKARR_APPEND(name) \
__attribute__((retain, used, \
section("__DATA,__la_" #name))) UPB_NO_SANITIZE_ADDRESS
#define UPB_LINKARR_DECLARE(name, type) \
extern type __start_linkarr_##name __asm( \
"section$start$__DATA$__la_" #name); \
extern type __stop_linkarr_##name __asm( \
"section$end$__DATA$" \
"__la_" #name); \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1]
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#elif defined(_MSC_VER) && defined(__clang__)
/* See:
* https://devblogs.microsoft.com/oldnewthing/20181107-00/?p=100155
* https://devblogs.microsoft.com/oldnewthing/20181108-00/?p=100165
* https://devblogs.microsoft.com/oldnewthing/20181109-00/?p=100175 */
// Usage of __attribute__ here probably means this is Clang-specific, and would
// not work on MSVC.
#define UPB_LINKARR_APPEND(name) \
__declspec(allocate("la_" #name "$j")) \
__attribute__((retain, used)) UPB_NO_SANITIZE_ADDRESS
#define UPB_LINKARR_DECLARE(name, type) \
__declspec(allocate("la_" #name "$a")) type __start_linkarr_##name; \
__declspec(allocate("la_" #name "$z")) type __stop_linkarr_##name; \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1] = {0}
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#else
// Linker arrays are not supported on this platform. Make appends a no-op but
// don't define the other macros.
#define UPB_LINKARR_APPEND(name)
#endif
// Future versions of upb will include breaking changes to some APIs.
// This macro can be set to enable these API changes ahead of time, so that
// user code can be updated before upgrading versions of protobuf.
#ifdef UPB_FUTURE_BREAKING_CHANGES
#endif
#include <errno.h>
#include <float.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
// Must be last.
void upb_Status_Clear(upb_Status* status) {
if (!status) return;
status->ok = true;
status->msg[0] = '\0';
}
bool upb_Status_IsOk(const upb_Status* status) { return status->ok; }
const char* upb_Status_ErrorMessage(const upb_Status* status) {
return status->msg;
}
void upb_Status_SetErrorMessage(upb_Status* status, const char* msg) {
if (!status) return;
status->ok = false;
strncpy(status->msg, msg, _kUpb_Status_MaxMessage - 1);
status->msg[_kUpb_Status_MaxMessage - 1] = '\0';
}
void upb_Status_SetErrorFormat(upb_Status* status, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
upb_Status_VSetErrorFormat(status, fmt, args);
va_end(args);
}
void upb_Status_VSetErrorFormat(upb_Status* status, const char* fmt,
va_list args) {
if (!status) return;
status->ok = false;
vsnprintf(status->msg, sizeof(status->msg), fmt, args);
status->msg[_kUpb_Status_MaxMessage - 1] = '\0';
}
void upb_Status_VAppendErrorFormat(upb_Status* status, const char* fmt,
va_list args) {
size_t len;
if (!status) return;
status->ok = false;
len = strlen(status->msg);
vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args);
status->msg[_kUpb_Status_MaxMessage - 1] = '\0';
}
static const char* _upb_EpsCopyInputStream_NoOpCallback(
upb_EpsCopyInputStream* e, const char* old_end, const char* new_start) {
return new_start;
}
const char* _upb_EpsCopyInputStream_IsDoneFallbackNoCallback(
upb_EpsCopyInputStream* e, const char* ptr, int overrun) {
return _upb_EpsCopyInputStream_IsDoneFallbackInline(
e, ptr, overrun, _upb_EpsCopyInputStream_NoOpCallback);
}
#include <errno.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Must be last.
typedef struct {
const char *ptr, *end;
upb_Arena* arena; /* TODO: should we have a tmp arena for tmp data? */
const upb_DefPool* symtab;
int depth;
int result;
upb_Status* status;
jmp_buf err;
int line;
const char* line_begin;
bool is_first;
int options;
const upb_FieldDef* debug_field;
} jsondec;
typedef struct {
upb_MessageValue value;
bool ignore;
} upb_JsonMessageValue;
enum { JD_OBJECT, JD_ARRAY, JD_STRING, JD_NUMBER, JD_TRUE, JD_FALSE, JD_NULL };
/* Forward declarations of mutually-recursive functions. */
static void jsondec_wellknown(jsondec* d, upb_Message* msg,
const upb_MessageDef* m);
static upb_JsonMessageValue jsondec_value(jsondec* d, const upb_FieldDef* f);
static void jsondec_wellknownvalue(jsondec* d, upb_Message* msg,
const upb_MessageDef* m);
static void jsondec_object(jsondec* d, upb_Message* msg,
const upb_MessageDef* m);
static bool jsondec_streql(upb_StringView str, const char* lit) {
return str.size == strlen(lit) && memcmp(str.data, lit, str.size) == 0;
}
static bool jsondec_isnullvalue(const upb_FieldDef* f) {
return upb_FieldDef_CType(f) == kUpb_CType_Enum &&
strcmp(upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(f)),
"google.protobuf.NullValue") == 0;
}
static bool jsondec_isvalue(const upb_FieldDef* f) {
return (upb_FieldDef_CType(f) == kUpb_CType_Message &&
upb_MessageDef_WellKnownType(upb_FieldDef_MessageSubDef(f)) ==
kUpb_WellKnown_Value) ||
jsondec_isnullvalue(f);
}
static void jsondec_seterrmsg(jsondec* d, const char* msg) {
upb_Status_SetErrorFormat(d->status, "Error parsing JSON @%d:%d: %s", d->line,
(int)(d->ptr - d->line_begin), msg);
}
UPB_NORETURN static void jsondec_err(jsondec* d, const char* msg) {
jsondec_seterrmsg(d, msg);
UPB_LONGJMP(d->err, 1);
}
UPB_PRINTF(2, 3)
UPB_NORETURN static void jsondec_errf(jsondec* d, const char* fmt, ...) {
va_list argp;
upb_Status_SetErrorFormat(d->status, "Error parsing JSON @%d:%d: ", d->line,
(int)(d->ptr - d->line_begin));
va_start(argp, fmt);
upb_Status_VAppendErrorFormat(d->status, fmt, argp);
va_end(argp);
UPB_LONGJMP(d->err, 1);
}
// Advances d->ptr until the next non-whitespace character or to the end of
// the buffer.
static void jsondec_consumews(jsondec* d) {
while (d->ptr != d->end) {
switch (*d->ptr) {
case '\n':
d->line++;
d->line_begin = d->ptr;
/* Fallthrough. */
case '\r':
case '\t':
case ' ':
d->ptr++;
break;
default:
return;
}
}
}
// Advances d->ptr until the next non-whitespace character. Postcondition that
// d->ptr is pointing at a valid non-whitespace character (will err if end of
// buffer is reached).
static void jsondec_skipws(jsondec* d) {
jsondec_consumews(d);
if (d->ptr == d->end) {
jsondec_err(d, "Unexpected EOF");
}
}
static bool jsondec_tryparsech(jsondec* d, char ch) {
if (d->ptr == d->end || *d->ptr != ch) return false;
d->ptr++;
return true;
}
static void jsondec_parselit(jsondec* d, const char* lit) {
size_t avail = d->end - d->ptr;
size_t len = strlen(lit);
if (avail < len || memcmp(d->ptr, lit, len) != 0) {
jsondec_errf(d, "Expected: '%s'", lit);
}
d->ptr += len;
}
static void jsondec_wsch(jsondec* d, char ch) {
jsondec_skipws(d);
if (!jsondec_tryparsech(d, ch)) {
jsondec_errf(d, "Expected: '%c'", ch);
}
}
static void jsondec_true(jsondec* d) { jsondec_parselit(d, "true"); }
static void jsondec_false(jsondec* d) { jsondec_parselit(d, "false"); }
static void jsondec_null(jsondec* d) { jsondec_parselit(d, "null"); }
static void jsondec_entrysep(jsondec* d) {
jsondec_skipws(d);
jsondec_parselit(d, ":");
}
static int jsondec_rawpeek(jsondec* d) {
if (d->ptr == d->end) {
jsondec_err(d, "Unexpected EOF");
}
switch (*d->ptr) {
case '{':
return JD_OBJECT;
case '[':
return JD_ARRAY;
case '"':
return JD_STRING;
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return JD_NUMBER;
case 't':
return JD_TRUE;
case 'f':
return JD_FALSE;
case 'n':
return JD_NULL;
default:
jsondec_errf(d, "Unexpected character: '%c'", *d->ptr);
}
}
/* JSON object/array **********************************************************/
/* These are used like so:
*
* jsondec_objstart(d);
* while (jsondec_objnext(d)) {
* ...
* }
* jsondec_objend(d) */
static int jsondec_peek(jsondec* d) {
jsondec_skipws(d);
return jsondec_rawpeek(d);
}
static void jsondec_push(jsondec* d) {
if (--d->depth < 0) {
jsondec_err(d, "Recursion limit exceeded");
}
d->is_first = true;
}
static bool jsondec_seqnext(jsondec* d, char end_ch) {
bool is_first = d->is_first;
d->is_first = false;
jsondec_skipws(d);
if (*d->ptr == end_ch) return false;
if (!is_first) jsondec_parselit(d, ",");
return true;
}
static void jsondec_arrstart(jsondec* d) {
jsondec_push(d);
jsondec_wsch(d, '[');
}
static void jsondec_arrend(jsondec* d) {
d->depth++;
jsondec_wsch(d, ']');
}
static bool jsondec_arrnext(jsondec* d) { return jsondec_seqnext(d, ']'); }
static void jsondec_objstart(jsondec* d) {
jsondec_push(d);
jsondec_wsch(d, '{');
}
static void jsondec_objend(jsondec* d) {
d->depth++;
jsondec_wsch(d, '}');
}
static bool jsondec_objnext(jsondec* d) {
if (!jsondec_seqnext(d, '}')) return false;
if (jsondec_peek(d) != JD_STRING) {
jsondec_err(d, "Object must start with string");
}
return true;
}
/* JSON number ****************************************************************/
static bool jsondec_tryskipdigits(jsondec* d) {
const char* start = d->ptr;
while (d->ptr < d->end) {
if (*d->ptr < '0' || *d->ptr > '9') {
break;
}
d->ptr++;
}
return d->ptr != start;
}
static void jsondec_skipdigits(jsondec* d) {
if (!jsondec_tryskipdigits(d)) {
jsondec_err(d, "Expected one or more digits");
}
}
static double jsondec_number(jsondec* d) {
const char* start = d->ptr;
UPB_ASSERT(jsondec_rawpeek(d) == JD_NUMBER);
/* Skip over the syntax of a number, as specified by JSON. */
if (*d->ptr == '-') d->ptr++;
if (jsondec_tryparsech(d, '0')) {
if (jsondec_tryskipdigits(d)) {
jsondec_err(d, "number cannot have leading zero");
}
} else {
jsondec_skipdigits(d);
}
if (d->ptr == d->end) goto parse;
if (jsondec_tryparsech(d, '.')) {
jsondec_skipdigits(d);
}
if (d->ptr == d->end) goto parse;
if (*d->ptr == 'e' || *d->ptr == 'E') {
d->ptr++;
if (d->ptr == d->end) {
jsondec_err(d, "Unexpected EOF in number");
}
if (*d->ptr == '+' || *d->ptr == '-') {
d->ptr++;
}
jsondec_skipdigits(d);
}
parse:
/* Having verified the syntax of a JSON number, use strtod() to parse
* (strtod() accepts a superset of JSON syntax). */
errno = 0;
{
// Copy the number into a null-terminated scratch buffer since strtod
// expects a null-terminated string.
char nullz[64];
ptrdiff_t len = d->ptr - start;
if (len > (ptrdiff_t)(sizeof(nullz) - 1)) {
jsondec_err(d, "excessively long number");
}
memcpy(nullz, start, len);
nullz[len] = '\0';
char* end;
double val = strtod(nullz, &end);
UPB_ASSERT(end - nullz == len);
/* Currently the min/max-val conformance tests fail if we check this. Does
* this mean the conformance tests are wrong or strtod() is wrong, or
* something else? Investigate further. */
/*
if (errno == ERANGE) {
jsondec_err(d, "Number out of range");
}
*/
if (val > DBL_MAX || val < -DBL_MAX) {
jsondec_err(d, "Number out of range");
}
return val;
}
}
/* JSON string ****************************************************************/
static char jsondec_escape(jsondec* d) {
switch (*d->ptr++) {
case '"':
return '\"';
case '\\':
return '\\';
case '/':
return '/';
case 'b':
return '\b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
default:
jsondec_err(d, "Invalid escape char");
}
}
static uint32_t jsondec_codepoint(jsondec* d) {
uint32_t cp = 0;
const char* end;
if (d->end - d->ptr < 4) {
jsondec_err(d, "EOF inside string");
}
end = d->ptr + 4;
while (d->ptr < end) {
char ch = *d->ptr++;
if (ch >= '0' && ch <= '9') {
ch -= '0';
} else if (ch >= 'a' && ch <= 'f') {
ch = ch - 'a' + 10;
} else if (ch >= 'A' && ch <= 'F') {
ch = ch - 'A' + 10;
} else {
jsondec_err(d, "Invalid hex digit");
}
cp = (cp << 4) | ch;
}
return cp;
}
/* Parses a \uXXXX unicode escape (possibly a surrogate pair). */
static size_t jsondec_unicode(jsondec* d, char* out) {
uint32_t cp = jsondec_codepoint(d);
if (upb_Unicode_IsHigh(cp)) {
/* Surrogate pair: two 16-bit codepoints become a 32-bit codepoint. */
jsondec_parselit(d, "\\u");
uint32_t low = jsondec_codepoint(d);
if (!upb_Unicode_IsLow(low)) jsondec_err(d, "Invalid low surrogate");
cp = upb_Unicode_FromPair(cp, low);
} else if (upb_Unicode_IsLow(cp)) {
jsondec_err(d, "Unpaired low surrogate");
}
/* Write to UTF-8 */
int bytes = upb_Unicode_ToUTF8(cp, out);
if (bytes == 0) jsondec_err(d, "Invalid codepoint");
return bytes;
}
static void jsondec_resize(jsondec* d, char** buf, char** end, char** buf_end) {
size_t oldsize = *buf_end - *buf;
size_t len = *end - *buf;
size_t size = UPB_MAX(8, 2 * oldsize);
*buf = upb_Arena_Realloc(d->arena, *buf, len, size);
if (!*buf) jsondec_err(d, "Out of memory");
*end = *buf + len;
*buf_end = *buf + size;
}
static upb_StringView jsondec_string(jsondec* d) {
char* buf = NULL;
char* end = NULL;
char* buf_end = NULL;
jsondec_skipws(d);
if (*d->ptr++ != '"') {
jsondec_err(d, "Expected string");
}
while (d->ptr < d->end) {
char ch = *d->ptr++;
if (end == buf_end) {
jsondec_resize(d, &buf, &end, &buf_end);
}
switch (ch) {
case '"': {
upb_StringView ret;
ret.data = buf;
ret.size = end - buf;
*end = '\0'; /* Needed for possible strtod(). */
return ret;
}
case '\\':
if (d->ptr == d->end) goto eof;
if (*d->ptr == 'u') {
d->ptr++;
if (buf_end - end < 4) {
/* Allow space for maximum-sized codepoint (4 bytes). */
jsondec_resize(d, &buf, &end, &buf_end);
}
end += jsondec_unicode(d, end);
} else {
*end++ = jsondec_escape(d);
}
break;
default:
if ((unsigned char)ch < 0x20) {
jsondec_err(d, "Invalid char in JSON string");
}
*end++ = ch;
break;
}
}
eof:
jsondec_err(d, "EOF inside string");
}
static void jsondec_skipval(jsondec* d) {
switch (jsondec_peek(d)) {
case JD_OBJECT:
jsondec_objstart(d);
while (jsondec_objnext(d)) {
jsondec_string(d);
jsondec_entrysep(d);
jsondec_skipval(d);
}
jsondec_objend(d);
break;
case JD_ARRAY:
jsondec_arrstart(d);
while (jsondec_arrnext(d)) {
jsondec_skipval(d);
}
jsondec_arrend(d);
break;
case JD_TRUE:
jsondec_true(d);
break;
case JD_FALSE:
jsondec_false(d);
break;
case JD_NULL:
jsondec_null(d);
break;
case JD_STRING:
jsondec_string(d);
break;
case JD_NUMBER:
jsondec_number(d);
break;
}
}
/* Base64 decoding for bytes fields. ******************************************/
static unsigned int jsondec_base64_tablelookup(const char ch) {
/* Table includes the normal base64 chars plus the URL-safe variant. */
const signed char table[256] = {
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, 62 /*+*/, -1, 62 /*-*/, -1, 63 /*/ */, 52 /*0*/,
53 /*1*/, 54 /*2*/, 55 /*3*/, 56 /*4*/, 57 /*5*/, 58 /*6*/, 59 /*7*/,
60 /*8*/, 61 /*9*/, -1, -1, -1, -1, -1,
-1, -1, 0 /*A*/, 1 /*B*/, 2 /*C*/, 3 /*D*/, 4 /*E*/,
5 /*F*/, 6 /*G*/, 07 /*H*/, 8 /*I*/, 9 /*J*/, 10 /*K*/, 11 /*L*/,
12 /*M*/, 13 /*N*/, 14 /*O*/, 15 /*P*/, 16 /*Q*/, 17 /*R*/, 18 /*S*/,
19 /*T*/, 20 /*U*/, 21 /*V*/, 22 /*W*/, 23 /*X*/, 24 /*Y*/, 25 /*Z*/,
-1, -1, -1, -1, 63 /*_*/, -1, 26 /*a*/,
27 /*b*/, 28 /*c*/, 29 /*d*/, 30 /*e*/, 31 /*f*/, 32 /*g*/, 33 /*h*/,
34 /*i*/, 35 /*j*/, 36 /*k*/, 37 /*l*/, 38 /*m*/, 39 /*n*/, 40 /*o*/,
41 /*p*/, 42 /*q*/, 43 /*r*/, 44 /*s*/, 45 /*t*/, 46 /*u*/, 47 /*v*/,
48 /*w*/, 49 /*x*/, 50 /*y*/, 51 /*z*/, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1};
/* Sign-extend return value so high bit will be set on any unexpected char. */
return table[(unsigned)ch];
}
static char* jsondec_partialbase64(jsondec* d, const char* ptr, const char* end,
char* out) {
int32_t val = -1;
switch (end - ptr) {
case 2:
val = jsondec_base64_tablelookup(ptr[0]) << 18 |
jsondec_base64_tablelookup(ptr[1]) << 12;
out[0] = val >> 16;
out += 1;
break;
case 3:
val = jsondec_base64_tablelookup(ptr[0]) << 18 |
jsondec_base64_tablelookup(ptr[1]) << 12 |
jsondec_base64_tablelookup(ptr[2]) << 6;
out[0] = val >> 16;
out[1] = (val >> 8) & 0xff;
out += 2;
break;
}
if (val < 0) {
jsondec_err(d, "Corrupt base64");
}
return out;
}
static size_t jsondec_base64(jsondec* d, upb_StringView str) {
/* We decode in place. This is safe because this is a new buffer (not
* aliasing the input) and because base64 decoding shrinks 4 bytes into 3. */
char* out = (char*)str.data;
const char* ptr = str.data;
const char* end = ptr + str.size;
const char* end4 = ptr + (str.size & -4); /* Round down to multiple of 4. */
for (; ptr < end4; ptr += 4, out += 3) {
int val = jsondec_base64_tablelookup(ptr[0]) << 18 |
jsondec_base64_tablelookup(ptr[1]) << 12 |
jsondec_base64_tablelookup(ptr[2]) << 6 |
jsondec_base64_tablelookup(ptr[3]) << 0;
if (val < 0) {
/* Junk chars or padding. Remove trailing padding, if any. */
if (end - ptr == 4 && ptr[3] == '=') {
if (ptr[2] == '=') {
end -= 2;
} else {
end -= 1;
}
}
break;
}
out[0] = val >> 16;
out[1] = (val >> 8) & 0xff;
out[2] = val & 0xff;
}
if (ptr < end) {
/* Process remaining chars. We do not require padding. */
out = jsondec_partialbase64(d, ptr, end, out);
}
return out - str.data;
}
/* Low-level integer parsing **************************************************/
static const char* jsondec_buftouint64(jsondec* d, const char* ptr,
const char* end, uint64_t* val) {
const char* out = upb_BufToUint64(ptr, end, val);
if (!out) jsondec_err(d, "Integer overflow");
return out;
}
static const char* jsondec_buftoint64(jsondec* d, const char* ptr,
const char* end, int64_t* val,
bool* is_neg) {
const char* out = upb_BufToInt64(ptr, end, val, is_neg);
if (!out) jsondec_err(d, "Integer overflow");
return out;
}
static uint64_t jsondec_strtouint64(jsondec* d, upb_StringView str) {
const char* end = str.data + str.size;
uint64_t ret;
if (jsondec_buftouint64(d, str.data, end, &ret) != end) {
jsondec_err(d, "Non-number characters in quoted integer");
}
return ret;
}
static int64_t jsondec_strtoint64(jsondec* d, upb_StringView str) {
const char* end = str.data + str.size;
int64_t ret;
if (jsondec_buftoint64(d, str.data, end, &ret, NULL) != end) {
jsondec_err(d, "Non-number characters in quoted integer");
}
return ret;
}
static void jsondec_checkempty(jsondec* d, upb_StringView str,
const upb_FieldDef* f) {
if (str.size != 0) return;
d->result = kUpb_JsonDecodeResult_Error;
upb_Status_SetErrorFormat(d->status,
"Empty string is not a valid number (field: %s). "
"This will be an error in a future version.",
upb_FieldDef_FullName(f));
}
/* Primitive value types ******************************************************/
/* Parse INT32 or INT64 value. */
static upb_MessageValue jsondec_int(jsondec* d, const upb_FieldDef* f) {
upb_MessageValue val;
switch (jsondec_peek(d)) {
case JD_NUMBER: {
double dbl = jsondec_number(d);
if (dbl > 9223372036854774784.0 || dbl < -9223372036854775808.0) {
jsondec_err(d, "JSON number is out of range.");
}
val.int64_val = dbl; /* must be guarded, overflow here is UB */
if (val.int64_val != dbl) {
jsondec_errf(d, "JSON number was not integral (%f != %" PRId64 ")", dbl,
val.int64_val);
}
break;
}
case JD_STRING: {
upb_StringView str = jsondec_string(d);
jsondec_checkempty(d, str, f);
val.int64_val = jsondec_strtoint64(d, str);
break;
}
default:
jsondec_err(d, "Expected number or string");
}
if (upb_FieldDef_CType(f) == kUpb_CType_Int32 ||
upb_FieldDef_CType(f) == kUpb_CType_Enum) {
if (val.int64_val > INT32_MAX || val.int64_val < INT32_MIN) {
jsondec_err(d, "Integer out of range.");
}
val.int32_val = (int32_t)val.int64_val;
}
return val;
}
/* Parse UINT32 or UINT64 value. */
static upb_MessageValue jsondec_uint(jsondec* d, const upb_FieldDef* f) {
upb_MessageValue val;
switch (jsondec_peek(d)) {
case JD_NUMBER: {
double dbl = jsondec_number(d);
if (dbl > 18446744073709549568.0 || dbl < 0) {
jsondec_err(d, "JSON number is out of range.");
}
val.uint64_val = dbl; /* must be guarded, overflow here is UB */
if (val.uint64_val != dbl) {
jsondec_errf(d, "JSON number was not integral (%f != %" PRIu64 ")", dbl,
val.uint64_val);
}
break;
}
case JD_STRING: {
upb_StringView str = jsondec_string(d);
jsondec_checkempty(d, str, f);
val.uint64_val = jsondec_strtouint64(d, str);
break;
}
default:
jsondec_err(d, "Expected number or string");
}
if (upb_FieldDef_CType(f) == kUpb_CType_UInt32) {
if (val.uint64_val > UINT32_MAX) {
jsondec_err(d, "Integer out of range.");
}
val.uint32_val = (uint32_t)val.uint64_val;
}
return val;
}
/* Parse DOUBLE or FLOAT value. */
static upb_MessageValue jsondec_double(jsondec* d, const upb_FieldDef* f) {
upb_StringView str;
upb_MessageValue val;
switch (jsondec_peek(d)) {
case JD_NUMBER:
val.double_val = jsondec_number(d);
break;
case JD_STRING:
str = jsondec_string(d);
if (str.size == 0) {
jsondec_checkempty(d, str, f);
val.double_val = 0.0;
} else if (jsondec_streql(str, "NaN")) {
val.double_val = NAN;
} else if (jsondec_streql(str, "Infinity")) {
val.double_val = INFINITY;
} else if (jsondec_streql(str, "-Infinity")) {
val.double_val = -INFINITY;
} else {
char* end;
val.double_val = strtod(str.data, &end);
if (end != str.data + str.size) {
d->result = kUpb_JsonDecodeResult_Error;
upb_Status_SetErrorFormat(
d->status,
"Non-number characters in quoted number (field: %s). "
"This will be an error in a future version.",
upb_FieldDef_FullName(f));
}
}
break;
default:
jsondec_err(d, "Expected number or string");
}
if (upb_FieldDef_CType(f) == kUpb_CType_Float) {
float f = val.double_val;
if (val.double_val != INFINITY && val.double_val != -INFINITY) {
if (f == INFINITY || f == -INFINITY) jsondec_err(d, "Float out of range");
}
val.float_val = f;
}
return val;
}
/* Parse STRING or BYTES value. */
static upb_MessageValue jsondec_strfield(jsondec* d, const upb_FieldDef* f) {
upb_MessageValue val;
val.str_val = jsondec_string(d);
if (upb_FieldDef_CType(f) == kUpb_CType_Bytes) {
val.str_val.size = jsondec_base64(d, val.str_val);
}
return val;
}
static upb_JsonMessageValue jsondec_enum(jsondec* d, const upb_FieldDef* f) {
switch (jsondec_peek(d)) {
case JD_STRING: {
upb_StringView str = jsondec_string(d);
const upb_EnumDef* e = upb_FieldDef_EnumSubDef(f);
const upb_EnumValueDef* ev =
upb_EnumDef_FindValueByNameWithSize(e, str.data, str.size);
upb_JsonMessageValue val = {.ignore = false};
if (ev) {
val.value.int32_val = upb_EnumValueDef_Number(ev);
} else {
if (d->options & upb_JsonDecode_IgnoreUnknown) {
val.ignore = true;
} else {
jsondec_errf(d, "Unknown enumerator: '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(str));
}
}
return val;
}
case JD_NULL: {
if (jsondec_isnullvalue(f)) {
upb_JsonMessageValue val = {.ignore = false};
jsondec_null(d);
val.value.int32_val = 0;
return val;
}
}
/* Fallthrough. */
default:
return (upb_JsonMessageValue){.value = jsondec_int(d, f),
.ignore = false};
}
}
static upb_MessageValue jsondec_bool(jsondec* d, const upb_FieldDef* f) {
bool is_map_key = upb_FieldDef_Number(f) == 1 &&
upb_MessageDef_IsMapEntry(upb_FieldDef_ContainingType(f));
upb_MessageValue val;
if (is_map_key) {
upb_StringView str = jsondec_string(d);
if (jsondec_streql(str, "true")) {
val.bool_val = true;
} else if (jsondec_streql(str, "false")) {
val.bool_val = false;
} else {
jsondec_err(d, "Invalid boolean map key");
}
} else {
switch (jsondec_peek(d)) {
case JD_TRUE:
val.bool_val = true;
jsondec_true(d);
break;
case JD_FALSE:
val.bool_val = false;
jsondec_false(d);
break;
default:
jsondec_err(d, "Expected true or false");
}
}
return val;
}
/* Composite types (array/message/map) ****************************************/
static void jsondec_array(jsondec* d, upb_Message* msg, const upb_FieldDef* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Array* arr = upb_Message_Mutable(msg, f, d->arena).array;
jsondec_arrstart(d);
while (jsondec_arrnext(d)) {
upb_JsonMessageValue elem = jsondec_value(d, f);
if (!elem.ignore) {
upb_Array_Append(arr, elem.value, d->arena);
}
}
jsondec_arrend(d);
}
static void jsondec_map(jsondec* d, upb_Message* msg, const upb_FieldDef* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Map* map = upb_Message_Mutable(msg, f, d->arena).map;
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry, 1);
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry, 2);
jsondec_objstart(d);
while (jsondec_objnext(d)) {
upb_JsonMessageValue key, val;
key = jsondec_value(d, key_f);
UPB_ASSUME(!key.ignore); // Map key cannot be enum.
jsondec_entrysep(d);
val = jsondec_value(d, val_f);
if (!val.ignore) {
upb_Map_Set(map, key.value, val.value, d->arena);
}
}
jsondec_objend(d);
}
static void jsondec_tomsg(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (upb_MessageDef_WellKnownType(m) == kUpb_WellKnown_Unspecified) {
jsondec_object(d, msg, m);
} else {
jsondec_wellknown(d, msg, m);
}
}
static upb_MessageValue jsondec_msg(jsondec* d, const upb_FieldDef* f) {
const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
const upb_MiniTable* layout = upb_MessageDef_MiniTable(m);
upb_Message* msg = upb_Message_New(layout, d->arena);
upb_MessageValue val;
jsondec_tomsg(d, msg, m);
val.msg_val = msg;
return val;
}
static void jsondec_field(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_StringView name;
const upb_FieldDef* f;
const upb_FieldDef* preserved;
name = jsondec_string(d);
jsondec_entrysep(d);
if (name.size >= 2 && name.data[0] == '[' &&
name.data[name.size - 1] == ']') {
f = upb_DefPool_FindExtensionByNameWithSize(d->symtab, name.data + 1,
name.size - 2);
if (f && upb_FieldDef_ContainingType(f) != m) {
jsondec_errf(
d, "Extension %s extends message %s, but was seen in message %s",
upb_FieldDef_FullName(f),
upb_MessageDef_FullName(upb_FieldDef_ContainingType(f)),
upb_MessageDef_FullName(m));
}
} else {
f = upb_MessageDef_FindByJsonNameWithSize(m, name.data, name.size);
}
if (!f) {
if ((d->options & upb_JsonDecode_IgnoreUnknown) == 0) {
jsondec_errf(d, "No such field: " UPB_STRINGVIEW_FORMAT,
UPB_STRINGVIEW_ARGS(name));
}
jsondec_skipval(d);
return;
}
if (jsondec_peek(d) == JD_NULL && !jsondec_isvalue(f)) {
/* JSON "null" indicates a default value, so no need to set anything. */
jsondec_null(d);
return;
}
if (upb_FieldDef_RealContainingOneof(f) &&
upb_Message_WhichOneofByDef(msg, upb_FieldDef_ContainingOneof(f))) {
jsondec_err(d, "More than one field for this oneof.");
}
preserved = d->debug_field;
d->debug_field = f;
if (upb_FieldDef_IsMap(f)) {
jsondec_map(d, msg, f);
} else if (upb_FieldDef_IsRepeated(f)) {
jsondec_array(d, msg, f);
} else if (upb_FieldDef_IsSubMessage(f)) {
upb_Message* submsg = upb_Message_Mutable(msg, f, d->arena).msg;
const upb_MessageDef* subm = upb_FieldDef_MessageSubDef(f);
jsondec_tomsg(d, submsg, subm);
} else {
upb_JsonMessageValue val = jsondec_value(d, f);
if (!val.ignore) {
upb_Message_SetFieldByDef(msg, f, val.value, d->arena);
}
}
d->debug_field = preserved;
}
static void jsondec_object(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
jsondec_objstart(d);
while (jsondec_objnext(d)) {
jsondec_field(d, msg, m);
}
jsondec_objend(d);
}
static upb_MessageValue jsondec_nonenum(jsondec* d, const upb_FieldDef* f) {
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Bool:
return jsondec_bool(d, f);
case kUpb_CType_Float:
case kUpb_CType_Double:
return jsondec_double(d, f);
case kUpb_CType_UInt32:
case kUpb_CType_UInt64:
return jsondec_uint(d, f);
case kUpb_CType_Int32:
case kUpb_CType_Int64:
return jsondec_int(d, f);
case kUpb_CType_String:
case kUpb_CType_Bytes:
return jsondec_strfield(d, f);
case kUpb_CType_Message:
return jsondec_msg(d, f);
case kUpb_CType_Enum:
default:
UPB_UNREACHABLE();
}
}
static upb_JsonMessageValue jsondec_value(jsondec* d, const upb_FieldDef* f) {
if (upb_FieldDef_CType(f) == kUpb_CType_Enum) {
return jsondec_enum(d, f);
} else {
return (upb_JsonMessageValue){.value = jsondec_nonenum(d, f),
.ignore = false};
}
}
/* Well-known types ***********************************************************/
static int jsondec_tsdigits(jsondec* d, const char** ptr, size_t digits,
const char* after) {
uint64_t val;
const char* p = *ptr;
const char* end = p + digits;
size_t after_len = after ? strlen(after) : 0;
UPB_ASSERT(digits <= 9); /* int can't overflow. */
if (jsondec_buftouint64(d, p, end, &val) != end ||
(after_len && memcmp(end, after, after_len) != 0)) {
jsondec_err(d, "Malformed timestamp");
}
UPB_ASSERT(val < INT_MAX);
*ptr = end + after_len;
return (int)val;
}
static int jsondec_nanos(jsondec* d, const char** ptr, const char* end) {
uint64_t nanos = 0;
const char* p = *ptr;
if (p != end && *p == '.') {
const char* nano_end = jsondec_buftouint64(d, p + 1, end, &nanos);
int digits = (int)(nano_end - p - 1);
int exp_lg10 = 9 - digits;
if (digits > 9) {
jsondec_err(d, "Too many digits for partial seconds");
}
while (exp_lg10--) nanos *= 10;
*ptr = nano_end;
}
UPB_ASSERT(nanos < INT_MAX);
return (int)nanos;
}
/* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */
int jsondec_epochdays(int y, int m, int d) {
const uint32_t year_base = 4800; /* Before min year, multiple of 400. */
const uint32_t m_adj = m - 3; /* March-based month. */
const uint32_t carry = m_adj > (uint32_t)m ? 1 : 0;
const uint32_t adjust = carry ? 12 : 0;
const uint32_t y_adj = y + year_base - carry;
const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
}
static int64_t jsondec_unixtime(int y, int m, int d, int h, int min, int s) {
return (int64_t)jsondec_epochdays(y, m, d) * 86400 + h * 3600 + min * 60 + s;
}
static void jsondec_timestamp(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_MessageValue seconds;
upb_MessageValue nanos;
upb_StringView str = jsondec_string(d);
const char* ptr = str.data;
const char* end = ptr + str.size;
if (str.size < 20) goto malformed;
{
/* 1972-01-01T01:00:00 */
int year = jsondec_tsdigits(d, &ptr, 4, "-");
int mon = jsondec_tsdigits(d, &ptr, 2, "-");
int day = jsondec_tsdigits(d, &ptr, 2, "T");
int hour = jsondec_tsdigits(d, &ptr, 2, ":");
int min = jsondec_tsdigits(d, &ptr, 2, ":");
int sec = jsondec_tsdigits(d, &ptr, 2, NULL);
seconds.int64_val = jsondec_unixtime(year, mon, day, hour, min, sec);
}
nanos.int32_val = jsondec_nanos(d, &ptr, end);
{
/* [+-]08:00 or Z */
int ofs_hour = 0;
int ofs_min = 0;
bool neg = false;
if (ptr == end) goto malformed;
switch (*ptr++) {
case '-':
neg = true;
/* fallthrough */
case '+':
if ((end - ptr) != 5) goto malformed;
ofs_hour = jsondec_tsdigits(d, &ptr, 2, ":");
ofs_min = jsondec_tsdigits(d, &ptr, 2, NULL);
ofs_min = ((ofs_hour * 60) + ofs_min) * 60;
seconds.int64_val += (neg ? ofs_min : -ofs_min);
break;
case 'Z':
if (ptr != end) goto malformed;
break;
default:
goto malformed;
}
}
if (seconds.int64_val < -62135596800) {
jsondec_err(d, "Timestamp out of range");
}
upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 1),
seconds, d->arena);
upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 2), nanos,
d->arena);
return;
malformed:
jsondec_err(d, "Malformed timestamp");
}
static void jsondec_duration(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_MessageValue seconds;
upb_MessageValue nanos;
upb_StringView str = jsondec_string(d);
const char* ptr = str.data;
const char* end = ptr + str.size;
const int64_t max = (uint64_t)3652500 * 86400;
bool neg = false;
/* "3.000000001s", "3s", etc. */
ptr = jsondec_buftoint64(d, ptr, end, &seconds.int64_val, &neg);
nanos.int32_val = jsondec_nanos(d, &ptr, end);
if (end - ptr != 1 || *ptr != 's') {
jsondec_err(d, "Malformed duration");
}
if (seconds.int64_val < -max || seconds.int64_val > max) {
jsondec_err(d, "Duration out of range");
}
if (neg) {
nanos.int32_val = -nanos.int32_val;
}
upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 1),
seconds, d->arena);
upb_Message_SetFieldByDef(msg, upb_MessageDef_FindFieldByNumber(m, 2), nanos,
d->arena);
}
static void jsondec_listvalue(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_FieldDef* values_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_MessageDef* value_m = upb_FieldDef_MessageSubDef(values_f);
const upb_MiniTable* value_layout = upb_MessageDef_MiniTable(value_m);
upb_Array* values = upb_Message_Mutable(msg, values_f, d->arena).array;
jsondec_arrstart(d);
while (jsondec_arrnext(d)) {
upb_Message* value_msg = upb_Message_New(value_layout, d->arena);
upb_MessageValue value;
value.msg_val = value_msg;
upb_Array_Append(values, value, d->arena);
jsondec_wellknownvalue(d, value_msg, value_m);
}
jsondec_arrend(d);
}
static void jsondec_struct(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_FieldDef* fields_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(fields_f);
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
const upb_MessageDef* value_m = upb_FieldDef_MessageSubDef(value_f);
const upb_MiniTable* value_layout = upb_MessageDef_MiniTable(value_m);
upb_Map* fields = upb_Message_Mutable(msg, fields_f, d->arena).map;
jsondec_objstart(d);
while (jsondec_objnext(d)) {
upb_MessageValue key, value;
upb_Message* value_msg = upb_Message_New(value_layout, d->arena);
key.str_val = jsondec_string(d);
value.msg_val = value_msg;
upb_Map_Set(fields, key, value, d->arena);
jsondec_entrysep(d);
jsondec_wellknownvalue(d, value_msg, value_m);
}
jsondec_objend(d);
}
static void jsondec_wellknownvalue(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_MessageValue val;
const upb_FieldDef* f;
upb_Message* submsg;
switch (jsondec_peek(d)) {
case JD_NUMBER:
/* double number_value = 2; */
f = upb_MessageDef_FindFieldByNumber(m, 2);
val.double_val = jsondec_number(d);
break;
case JD_STRING:
/* string string_value = 3; */
f = upb_MessageDef_FindFieldByNumber(m, 3);
val.str_val = jsondec_string(d);
break;
case JD_FALSE:
/* bool bool_value = 4; */
f = upb_MessageDef_FindFieldByNumber(m, 4);
val.bool_val = false;
jsondec_false(d);
break;
case JD_TRUE:
/* bool bool_value = 4; */
f = upb_MessageDef_FindFieldByNumber(m, 4);
val.bool_val = true;
jsondec_true(d);
break;
case JD_NULL:
/* NullValue null_value = 1; */
f = upb_MessageDef_FindFieldByNumber(m, 1);
val.int32_val = 0;
jsondec_null(d);
break;
/* Note: these cases return, because upb_Message_Mutable() is enough. */
case JD_OBJECT:
/* Struct struct_value = 5; */
f = upb_MessageDef_FindFieldByNumber(m, 5);
submsg = upb_Message_Mutable(msg, f, d->arena).msg;
jsondec_struct(d, submsg, upb_FieldDef_MessageSubDef(f));
return;
case JD_ARRAY:
/* ListValue list_value = 6; */
f = upb_MessageDef_FindFieldByNumber(m, 6);
submsg = upb_Message_Mutable(msg, f, d->arena).msg;
jsondec_listvalue(d, submsg, upb_FieldDef_MessageSubDef(f));
return;
default:
UPB_UNREACHABLE();
}
upb_Message_SetFieldByDef(msg, f, val, d->arena);
}
static upb_StringView jsondec_mask(jsondec* d, const char* buf,
const char* end) {
/* FieldMask fields grow due to inserted '_' characters, so we can't do the
* transform in place. */
const char* ptr = buf;
upb_StringView ret;
char* out;
ret.size = end - ptr;
while (ptr < end) {
ret.size += (*ptr >= 'A' && *ptr <= 'Z');
ptr++;
}
out = upb_Arena_Malloc(d->arena, ret.size);
ptr = buf;
ret.data = out;
while (ptr < end) {
char ch = *ptr++;
if (ch >= 'A' && ch <= 'Z') {
*out++ = '_';
*out++ = ch + 32;
} else if (ch == '_') {
jsondec_err(d, "field mask may not contain '_'");
} else {
*out++ = ch;
}
}
return ret;
}
static void jsondec_fieldmask(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
/* repeated string paths = 1; */
const upb_FieldDef* paths_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_Array* arr = upb_Message_Mutable(msg, paths_f, d->arena).array;
upb_StringView str = jsondec_string(d);
const char* ptr = str.data;
const char* end = ptr + str.size;
upb_MessageValue val;
while (ptr < end) {
const char* elem_end = memchr(ptr, ',', end - ptr);
if (elem_end) {
val.str_val = jsondec_mask(d, ptr, elem_end);
ptr = elem_end + 1;
} else {
val.str_val = jsondec_mask(d, ptr, end);
ptr = end;
}
upb_Array_Append(arr, val, d->arena);
}
}
static void jsondec_anyfield(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (upb_MessageDef_WellKnownType(m) == kUpb_WellKnown_Unspecified) {
/* For regular types: {"@type": "[user type]", "f1": <V1>, "f2": <V2>}
* where f1, f2, etc. are the normal fields of this type. */
jsondec_field(d, msg, m);
} else {
/* For well-known types: {"@type": "[well-known type]", "value": <X>}
* where <X> is whatever encoding the WKT normally uses. */
upb_StringView str = jsondec_string(d);
jsondec_entrysep(d);
if (!jsondec_streql(str, "value")) {
jsondec_err(d, "Key for well-known type must be 'value'");
}
jsondec_wellknown(d, msg, m);
}
}
static const upb_MessageDef* jsondec_typeurl(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_FieldDef* type_url_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_MessageDef* type_m;
upb_StringView type_url = jsondec_string(d);
const char* end = type_url.data + type_url.size;
const char* ptr = end;
upb_MessageValue val;
val.str_val = type_url;
upb_Message_SetFieldByDef(msg, type_url_f, val, d->arena);
/* Find message name after the last '/' */
while (ptr > type_url.data && *--ptr != '/') {
}
if (ptr == type_url.data || ptr == end) {
jsondec_err(d, "Type url must have at least one '/' and non-empty host");
}
ptr++;
type_m = upb_DefPool_FindMessageByNameWithSize(d->symtab, ptr, end - ptr);
if (!type_m) {
jsondec_err(d, "Type was not found");
}
return type_m;
}
static void jsondec_any(jsondec* d, upb_Message* msg, const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
/* string type_url = 1;
* bytes value = 2; */
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(m, 2);
upb_Message* any_msg;
const upb_MessageDef* any_m = NULL;
const char* pre_type_data = NULL;
const char* pre_type_end = NULL;
upb_MessageValue encoded;
jsondec_objstart(d);
/* Scan looking for "@type", which is not necessarily first. */
while (!any_m && jsondec_objnext(d)) {
const char* start = d->ptr;
upb_StringView name = jsondec_string(d);
jsondec_entrysep(d);
if (jsondec_streql(name, "@type")) {
any_m = jsondec_typeurl(d, msg, m);
if (pre_type_data) {
pre_type_end = start;
while (*pre_type_end != ',') pre_type_end--;
}
} else {
if (!pre_type_data) pre_type_data = start;
jsondec_skipval(d);
}
}
if (!any_m) {
jsondec_err(d, "Any object didn't contain a '@type' field");
}
const upb_MiniTable* any_layout = upb_MessageDef_MiniTable(any_m);
any_msg = upb_Message_New(any_layout, d->arena);
if (pre_type_data) {
size_t len = pre_type_end - pre_type_data + 1;
char* tmp = upb_Arena_Malloc(d->arena, len);
const char* saved_ptr = d->ptr;
const char* saved_end = d->end;
memcpy(tmp, pre_type_data, len - 1);
tmp[len - 1] = '}';
d->ptr = tmp;
d->end = tmp + len;
d->is_first = true;
while (jsondec_objnext(d)) {
jsondec_anyfield(d, any_msg, any_m);
}
d->ptr = saved_ptr;
d->end = saved_end;
}
while (jsondec_objnext(d)) {
jsondec_anyfield(d, any_msg, any_m);
}
jsondec_objend(d);
upb_EncodeStatus status =
upb_Encode(any_msg, upb_MessageDef_MiniTable(any_m), 0, d->arena,
(char**)&encoded.str_val.data, &encoded.str_val.size);
// TODO: We should fail gracefully here on a bad return status.
UPB_ASSERT(status == kUpb_EncodeStatus_Ok);
upb_Message_SetFieldByDef(msg, value_f, encoded, d->arena);
}
static void jsondec_wrapper(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_JsonMessageValue val = jsondec_value(d, value_f);
UPB_ASSUME(val.ignore == false); // Wrapper cannot be an enum.
upb_Message_SetFieldByDef(msg, value_f, val.value, d->arena);
}
static void jsondec_wellknown(jsondec* d, upb_Message* msg,
const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
switch (upb_MessageDef_WellKnownType(m)) {
case kUpb_WellKnown_Any:
jsondec_any(d, msg, m);
break;
case kUpb_WellKnown_FieldMask:
jsondec_fieldmask(d, msg, m);
break;
case kUpb_WellKnown_Duration:
jsondec_duration(d, msg, m);
break;
case kUpb_WellKnown_Timestamp:
jsondec_timestamp(d, msg, m);
break;
case kUpb_WellKnown_Value:
jsondec_wellknownvalue(d, msg, m);
break;
case kUpb_WellKnown_ListValue:
jsondec_listvalue(d, msg, m);
break;
case kUpb_WellKnown_Struct:
jsondec_struct(d, msg, m);
break;
case kUpb_WellKnown_DoubleValue:
case kUpb_WellKnown_FloatValue:
case kUpb_WellKnown_Int64Value:
case kUpb_WellKnown_UInt64Value:
case kUpb_WellKnown_Int32Value:
case kUpb_WellKnown_UInt32Value:
case kUpb_WellKnown_StringValue:
case kUpb_WellKnown_BytesValue:
case kUpb_WellKnown_BoolValue:
jsondec_wrapper(d, msg, m);
break;
default:
UPB_UNREACHABLE();
}
}
static int upb_JsonDecoder_Decode(jsondec* const d, upb_Message* const msg,
const upb_MessageDef* const m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (UPB_SETJMP(d->err)) return kUpb_JsonDecodeResult_Error;
jsondec_tomsg(d, msg, m);
// Consume any trailing whitespace before checking if we read the entire
// input.
jsondec_consumews(d);
if (d->ptr == d->end) {
return d->result;
} else {
jsondec_seterrmsg(d, "unexpected trailing characters");
return kUpb_JsonDecodeResult_Error;
}
}
int upb_JsonDecodeDetectingNonconformance(const char* buf, size_t size,
upb_Message* msg,
const upb_MessageDef* m,
const upb_DefPool* symtab,
int options, upb_Arena* arena,
upb_Status* status) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
jsondec d;
if (size == 0) return true;
d.ptr = buf;
d.end = buf + size;
d.arena = arena;
d.symtab = symtab;
d.status = status;
d.options = options;
d.depth = 64;
d.result = kUpb_JsonDecodeResult_Ok;
d.line = 1;
d.line_begin = d.ptr;
d.debug_field = NULL;
d.is_first = false;
return upb_JsonDecoder_Decode(&d, msg, m);
}
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
// Must be last.
typedef struct {
char *buf, *ptr, *end;
size_t overflow;
int indent_depth;
int options;
const upb_DefPool* ext_pool;
jmp_buf err;
upb_Status* status;
upb_Arena* arena;
} jsonenc;
static void jsonenc_msg(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m);
static void jsonenc_scalar(jsonenc* e, upb_MessageValue val,
const upb_FieldDef* f);
static void jsonenc_msgfield(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m);
static void jsonenc_msgfields(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m, bool first);
static void jsonenc_value(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m);
UPB_NORETURN static void jsonenc_err(jsonenc* e, const char* msg) {
upb_Status_SetErrorMessage(e->status, msg);
UPB_LONGJMP(e->err, 1);
}
UPB_PRINTF(2, 3)
UPB_NORETURN static void jsonenc_errf(jsonenc* e, const char* fmt, ...) {
va_list argp;
va_start(argp, fmt);
upb_Status_VSetErrorFormat(e->status, fmt, argp);
va_end(argp);
UPB_LONGJMP(e->err, 1);
}
static upb_Arena* jsonenc_arena(jsonenc* e) {
/* Create lazily, since it's only needed for Any */
if (!e->arena) {
e->arena = upb_Arena_New();
}
return e->arena;
}
static void jsonenc_putbytes(jsonenc* e, const void* data, size_t len) {
size_t have = e->end - e->ptr;
if (UPB_LIKELY(have >= len)) {
memcpy(e->ptr, data, len);
e->ptr += len;
} else {
if (have) {
memcpy(e->ptr, data, have);
e->ptr += have;
}
e->overflow += (len - have);
}
}
static void jsonenc_putstr(jsonenc* e, const char* str) {
jsonenc_putbytes(e, str, strlen(str));
}
UPB_PRINTF(2, 3)
static void jsonenc_printf(jsonenc* e, const char* fmt, ...) {
size_t n;
size_t have = e->end - e->ptr;
va_list args;
va_start(args, fmt);
n = _upb_vsnprintf(e->ptr, have, fmt, args);
va_end(args);
if (UPB_LIKELY(have > n)) {
e->ptr += n;
} else {
e->ptr = UPB_PTRADD(e->ptr, have);
e->overflow += (n - have);
}
}
static void jsonenc_nanos(jsonenc* e, int32_t nanos) {
int digits = 9;
if (nanos == 0) return;
if (nanos < 0 || nanos >= 1000000000) {
jsonenc_err(e, "error formatting timestamp as JSON: invalid nanos");
}
while (nanos % 1000 == 0) {
nanos /= 1000;
digits -= 3;
}
jsonenc_printf(e, ".%.*" PRId32, digits, nanos);
}
static void jsonenc_timestamp(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* seconds_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_FieldDef* nanos_f = upb_MessageDef_FindFieldByNumber(m, 2);
int64_t seconds = upb_Message_GetFieldByDef(msg, seconds_f).int64_val;
int32_t nanos = upb_Message_GetFieldByDef(msg, nanos_f).int32_val;
int L, N, I, J, K, hour, min, sec;
if (seconds < -62135596800) {
jsonenc_err(e,
"error formatting timestamp as JSON: minimum acceptable value "
"is 0001-01-01T00:00:00Z");
} else if (seconds > 253402300799) {
jsonenc_err(e,
"error formatting timestamp as JSON: maximum acceptable value "
"is 9999-12-31T23:59:59Z");
}
/* Julian Day -> Y/M/D, Algorithm from:
* Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for
* Processing Calendar Dates," Communications of the Association of
* Computing Machines, vol. 11 (1968), p. 657. */
seconds += 62135596800; // Ensure seconds is positive.
L = (int)(seconds / 86400) - 719162 + 68569 + 2440588;
N = 4 * L / 146097;
L = L - (146097 * N + 3) / 4;
I = 4000 * (L + 1) / 1461001;
L = L - 1461 * I / 4 + 31;
J = 80 * L / 2447;
K = L - 2447 * J / 80;
L = J / 11;
J = J + 2 - 12 * L;
I = 100 * (N - 49) + I + L;
sec = seconds % 60;
min = (seconds / 60) % 60;
hour = (seconds / 3600) % 24;
jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec);
jsonenc_nanos(e, nanos);
jsonenc_putstr(e, "Z\"");
}
static void jsonenc_duration(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* seconds_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_FieldDef* nanos_f = upb_MessageDef_FindFieldByNumber(m, 2);
int64_t seconds = upb_Message_GetFieldByDef(msg, seconds_f).int64_val;
int32_t nanos = upb_Message_GetFieldByDef(msg, nanos_f).int32_val;
bool negative = false;
if (seconds > 315576000000 || seconds < -315576000000 ||
(seconds != 0 && nanos != 0 && (seconds < 0) != (nanos < 0))) {
jsonenc_err(e, "bad duration");
}
if (seconds < 0) {
negative = true;
seconds = -seconds;
}
if (nanos < 0) {
negative = true;
nanos = -nanos;
}
jsonenc_putstr(e, "\"");
if (negative) {
jsonenc_putstr(e, "-");
}
jsonenc_printf(e, "%" PRId64, seconds);
jsonenc_nanos(e, nanos);
jsonenc_putstr(e, "s\"");
}
static void jsonenc_enum(int32_t val, const upb_FieldDef* f, jsonenc* e) {
const upb_EnumDef* e_def = upb_FieldDef_EnumSubDef(f);
if (strcmp(upb_EnumDef_FullName(e_def), "google.protobuf.NullValue") == 0) {
jsonenc_putstr(e, "null");
} else {
const upb_EnumValueDef* ev =
(e->options & upb_JsonEncode_FormatEnumsAsIntegers)
? NULL
: upb_EnumDef_FindValueByNumber(e_def, val);
if (ev) {
jsonenc_printf(e, "\"%s\"", upb_EnumValueDef_Name(ev));
} else {
jsonenc_printf(e, "%" PRId32, val);
}
}
}
static void jsonenc_bytes(jsonenc* e, upb_StringView str) {
/* This is the regular base64, not the "web-safe" version. */
static const char base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const unsigned char* ptr = (unsigned char*)str.data;
const unsigned char* end = UPB_PTRADD(ptr, str.size);
char buf[4];
jsonenc_putstr(e, "\"");
while (end - ptr >= 3) {
buf[0] = base64[ptr[0] >> 2];
buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
buf[2] = base64[((ptr[1] & 0xf) << 2) | (ptr[2] >> 6)];
buf[3] = base64[ptr[2] & 0x3f];
jsonenc_putbytes(e, buf, 4);
ptr += 3;
}
switch (end - ptr) {
case 2:
buf[0] = base64[ptr[0] >> 2];
buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
buf[2] = base64[(ptr[1] & 0xf) << 2];
buf[3] = '=';
jsonenc_putbytes(e, buf, 4);
break;
case 1:
buf[0] = base64[ptr[0] >> 2];
buf[1] = base64[((ptr[0] & 0x3) << 4)];
buf[2] = '=';
buf[3] = '=';
jsonenc_putbytes(e, buf, 4);
break;
}
jsonenc_putstr(e, "\"");
}
static void jsonenc_stringbody(jsonenc* e, upb_StringView str) {
const char* ptr = str.data;
const char* end = UPB_PTRADD(ptr, str.size);
while (ptr < end) {
switch (*ptr) {
case '\n':
jsonenc_putstr(e, "\\n");
break;
case '\r':
jsonenc_putstr(e, "\\r");
break;
case '\t':
jsonenc_putstr(e, "\\t");
break;
case '\"':
jsonenc_putstr(e, "\\\"");
break;
case '\f':
jsonenc_putstr(e, "\\f");
break;
case '\b':
jsonenc_putstr(e, "\\b");
break;
case '\\':
jsonenc_putstr(e, "\\\\");
break;
default:
if ((uint8_t)*ptr < 0x20) {
jsonenc_printf(e, "\\u%04x", (int)(uint8_t)*ptr);
} else {
/* This could be a non-ASCII byte. We rely on the string being valid
* UTF-8. */
jsonenc_putbytes(e, ptr, 1);
}
break;
}
ptr++;
}
}
static void jsonenc_string(jsonenc* e, upb_StringView str) {
jsonenc_putstr(e, "\"");
jsonenc_stringbody(e, str);
jsonenc_putstr(e, "\"");
}
static bool upb_JsonEncode_HandleSpecialDoubles(jsonenc* e, double val) {
if (val == INFINITY) {
jsonenc_putstr(e, "\"Infinity\"");
} else if (val == -INFINITY) {
jsonenc_putstr(e, "\"-Infinity\"");
} else if (val != val) {
jsonenc_putstr(e, "\"NaN\"");
} else {
return false;
}
return true;
}
static void upb_JsonEncode_Double(jsonenc* e, double val) {
if (upb_JsonEncode_HandleSpecialDoubles(e, val)) return;
char buf[32];
_upb_EncodeRoundTripDouble(val, buf, sizeof(buf));
jsonenc_putstr(e, buf);
}
static void upb_JsonEncode_Float(jsonenc* e, float val) {
if (upb_JsonEncode_HandleSpecialDoubles(e, val)) return;
char buf[32];
_upb_EncodeRoundTripFloat(val, buf, sizeof(buf));
jsonenc_putstr(e, buf);
}
static void jsonenc_wrapper(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(m, 1);
upb_MessageValue val = upb_Message_GetFieldByDef(msg, val_f);
jsonenc_scalar(e, val, val_f);
}
static const upb_MessageDef* jsonenc_getanymsg(jsonenc* e,
upb_StringView type_url) {
/* Find last '/', if any. */
const char* end = type_url.data + type_url.size;
const char* ptr = end;
const upb_MessageDef* ret;
if (!e->ext_pool) {
jsonenc_err(e, "Tried to encode Any, but no symtab was provided");
}
if (type_url.size == 0) goto badurl;
while (true) {
if (--ptr == type_url.data) {
/* Type URL must contain at least one '/', with host before. */
goto badurl;
}
if (*ptr == '/') {
ptr++;
break;
}
}
ret = upb_DefPool_FindMessageByNameWithSize(e->ext_pool, ptr, end - ptr);
if (!ret) {
jsonenc_errf(e, "Couldn't find Any type: %.*s", (int)(end - ptr), ptr);
}
return ret;
badurl:
jsonenc_errf(e, "Bad type URL: " UPB_STRINGVIEW_FORMAT,
UPB_STRINGVIEW_ARGS(type_url));
}
static void jsonenc_any(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* type_url_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(m, 2);
upb_StringView type_url = upb_Message_GetFieldByDef(msg, type_url_f).str_val;
upb_StringView value = upb_Message_GetFieldByDef(msg, value_f).str_val;
const upb_MessageDef* any_m = jsonenc_getanymsg(e, type_url);
const upb_MiniTable* any_layout = upb_MessageDef_MiniTable(any_m);
upb_Arena* arena = jsonenc_arena(e);
upb_Message* any = upb_Message_New(any_layout, arena);
if (upb_Decode(value.data, value.size, any, any_layout, NULL, 0, arena) !=
kUpb_DecodeStatus_Ok) {
jsonenc_err(e, "Error decoding message in Any");
}
jsonenc_putstr(e, "{\"@type\":");
jsonenc_string(e, type_url);
if (upb_MessageDef_WellKnownType(any_m) == kUpb_WellKnown_Unspecified) {
/* Regular messages: {"@type": "...","foo": 1, "bar": 2} */
jsonenc_msgfields(e, any, any_m, false);
} else {
/* Well-known type: {"@type": "...","value": <well-known encoding>} */
jsonenc_putstr(e, ",\"value\":");
jsonenc_msgfield(e, any, any_m);
}
jsonenc_putstr(e, "}");
}
static void jsonenc_putsep(jsonenc* e, const char* str, bool* first) {
if (*first) {
*first = false;
} else {
jsonenc_putstr(e, str);
}
}
static void jsonenc_fieldpath(jsonenc* e, upb_StringView path) {
const char* ptr = path.data;
const char* end = ptr + path.size;
while (ptr < end) {
char ch = *ptr;
if (ch >= 'A' && ch <= 'Z') {
jsonenc_err(e, "Field mask element may not have upper-case letter.");
} else if (ch == '_') {
if (ptr == end - 1 || *(ptr + 1) < 'a' || *(ptr + 1) > 'z') {
jsonenc_err(e, "Underscore must be followed by a lowercase letter.");
}
ch = *++ptr - 32;
}
jsonenc_putbytes(e, &ch, 1);
ptr++;
}
}
static void jsonenc_fieldmask(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* paths_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_Array* paths = upb_Message_GetFieldByDef(msg, paths_f).array_val;
bool first = true;
size_t i, n = 0;
if (paths) n = upb_Array_Size(paths);
jsonenc_putstr(e, "\"");
for (i = 0; i < n; i++) {
jsonenc_putsep(e, ",", &first);
jsonenc_fieldpath(e, upb_Array_Get(paths, i).str_val);
}
jsonenc_putstr(e, "\"");
}
static void jsonenc_struct(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
jsonenc_putstr(e, "{");
const upb_FieldDef* fields_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_Map* fields = upb_Message_GetFieldByDef(msg, fields_f).map_val;
if (fields) {
const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(fields_f);
const upb_FieldDef* value_f = upb_MessageDef_FindFieldByNumber(entry_m, 2);
size_t iter = kUpb_Map_Begin;
bool first = true;
upb_MessageValue key, val;
while (upb_Map_Next(fields, &key, &val, &iter)) {
jsonenc_putsep(e, ",", &first);
jsonenc_string(e, key.str_val);
jsonenc_putstr(e, ":");
jsonenc_value(e, val.msg_val, upb_FieldDef_MessageSubDef(value_f));
}
}
jsonenc_putstr(e, "}");
}
static void jsonenc_listvalue(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
const upb_FieldDef* values_f = upb_MessageDef_FindFieldByNumber(m, 1);
const upb_MessageDef* values_m = upb_FieldDef_MessageSubDef(values_f);
const upb_Array* values = upb_Message_GetFieldByDef(msg, values_f).array_val;
size_t i;
bool first = true;
jsonenc_putstr(e, "[");
if (values) {
const size_t size = upb_Array_Size(values);
for (i = 0; i < size; i++) {
upb_MessageValue elem = upb_Array_Get(values, i);
jsonenc_putsep(e, ",", &first);
jsonenc_value(e, elem.msg_val, values_m);
}
}
jsonenc_putstr(e, "]");
}
static void jsonenc_value(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
/* TODO: do we want a reflection method to get oneof case? */
size_t iter = kUpb_Message_Begin;
const upb_FieldDef* f;
upb_MessageValue val;
if (!upb_Message_Next(msg, m, NULL, &f, &val, &iter)) {
jsonenc_err(e, "No value set in Value proto");
}
switch (upb_FieldDef_Number(f)) {
case 1:
jsonenc_putstr(e, "null");
break;
case 2:
if (upb_JsonEncode_HandleSpecialDoubles(e, val.double_val)) {
jsonenc_err(
e,
"google.protobuf.Value cannot encode double values for "
"infinity or nan, because they would be parsed as a string");
}
upb_JsonEncode_Double(e, val.double_val);
break;
case 3:
jsonenc_string(e, val.str_val);
break;
case 4:
jsonenc_putstr(e, val.bool_val ? "true" : "false");
break;
case 5:
jsonenc_struct(e, val.msg_val, upb_FieldDef_MessageSubDef(f));
break;
case 6:
jsonenc_listvalue(e, val.msg_val, upb_FieldDef_MessageSubDef(f));
break;
}
}
static void jsonenc_msgfield(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
switch (upb_MessageDef_WellKnownType(m)) {
case kUpb_WellKnown_Unspecified:
jsonenc_msg(e, msg, m);
break;
case kUpb_WellKnown_Any:
jsonenc_any(e, msg, m);
break;
case kUpb_WellKnown_FieldMask:
jsonenc_fieldmask(e, msg, m);
break;
case kUpb_WellKnown_Duration:
jsonenc_duration(e, msg, m);
break;
case kUpb_WellKnown_Timestamp:
jsonenc_timestamp(e, msg, m);
break;
case kUpb_WellKnown_DoubleValue:
case kUpb_WellKnown_FloatValue:
case kUpb_WellKnown_Int64Value:
case kUpb_WellKnown_UInt64Value:
case kUpb_WellKnown_Int32Value:
case kUpb_WellKnown_UInt32Value:
case kUpb_WellKnown_StringValue:
case kUpb_WellKnown_BytesValue:
case kUpb_WellKnown_BoolValue:
jsonenc_wrapper(e, msg, m);
break;
case kUpb_WellKnown_Value:
jsonenc_value(e, msg, m);
break;
case kUpb_WellKnown_ListValue:
jsonenc_listvalue(e, msg, m);
break;
case kUpb_WellKnown_Struct:
jsonenc_struct(e, msg, m);
break;
}
}
static void jsonenc_scalar(jsonenc* e, upb_MessageValue val,
const upb_FieldDef* f) {
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Bool:
jsonenc_putstr(e, val.bool_val ? "true" : "false");
break;
case kUpb_CType_Float:
upb_JsonEncode_Float(e, val.float_val);
break;
case kUpb_CType_Double:
upb_JsonEncode_Double(e, val.double_val);
break;
case kUpb_CType_Int32:
jsonenc_printf(e, "%" PRId32, val.int32_val);
break;
case kUpb_CType_UInt32:
jsonenc_printf(e, "%" PRIu32, val.uint32_val);
break;
case kUpb_CType_Int64:
jsonenc_printf(e, "\"%" PRId64 "\"", val.int64_val);
break;
case kUpb_CType_UInt64:
jsonenc_printf(e, "\"%" PRIu64 "\"", val.uint64_val);
break;
case kUpb_CType_String:
jsonenc_string(e, val.str_val);
break;
case kUpb_CType_Bytes:
jsonenc_bytes(e, val.str_val);
break;
case kUpb_CType_Enum:
jsonenc_enum(val.int32_val, f, e);
break;
case kUpb_CType_Message:
jsonenc_msgfield(e, val.msg_val, upb_FieldDef_MessageSubDef(f));
break;
}
}
static void jsonenc_mapkey(jsonenc* e, upb_MessageValue val,
const upb_FieldDef* f) {
jsonenc_putstr(e, "\"");
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Bool:
jsonenc_putstr(e, val.bool_val ? "true" : "false");
break;
case kUpb_CType_Int32:
jsonenc_printf(e, "%" PRId32, val.int32_val);
break;
case kUpb_CType_UInt32:
jsonenc_printf(e, "%" PRIu32, val.uint32_val);
break;
case kUpb_CType_Int64:
jsonenc_printf(e, "%" PRId64, val.int64_val);
break;
case kUpb_CType_UInt64:
jsonenc_printf(e, "%" PRIu64, val.uint64_val);
break;
case kUpb_CType_String:
jsonenc_stringbody(e, val.str_val);
break;
default:
UPB_UNREACHABLE();
}
jsonenc_putstr(e, "\":");
}
static void jsonenc_array(jsonenc* e, const upb_Array* arr,
const upb_FieldDef* f) {
size_t i;
size_t size = arr ? upb_Array_Size(arr) : 0;
bool first = true;
jsonenc_putstr(e, "[");
for (i = 0; i < size; i++) {
jsonenc_putsep(e, ",", &first);
jsonenc_scalar(e, upb_Array_Get(arr, i), f);
}
jsonenc_putstr(e, "]");
}
static void jsonenc_map(jsonenc* e, const upb_Map* map, const upb_FieldDef* f) {
jsonenc_putstr(e, "{");
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef* key_f = upb_MessageDef_FindFieldByNumber(entry, 1);
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(entry, 2);
if (map) {
size_t iter = kUpb_Map_Begin;
bool first = true;
upb_MessageValue key, val;
while (upb_Map_Next(map, &key, &val, &iter)) {
jsonenc_putsep(e, ",", &first);
jsonenc_mapkey(e, key, key_f);
jsonenc_scalar(e, val, val_f);
}
}
jsonenc_putstr(e, "}");
}
static void jsonenc_fieldval(jsonenc* e, const upb_FieldDef* f,
upb_MessageValue val, bool* first) {
const char* name;
jsonenc_putsep(e, ",", first);
if (upb_FieldDef_IsExtension(f)) {
// TODO: For MessageSet, I would have expected this to print the message
// name here, but Python doesn't appear to do this. We should do more
// research here about what various implementations do.
jsonenc_printf(e, "\"[%s]\":", upb_FieldDef_FullName(f));
} else {
if (e->options & upb_JsonEncode_UseProtoNames) {
name = upb_FieldDef_Name(f);
} else {
name = upb_FieldDef_JsonName(f);
}
jsonenc_printf(e, "\"%s\":", name);
}
if (upb_FieldDef_IsMap(f)) {
jsonenc_map(e, val.map_val, f);
} else if (upb_FieldDef_IsRepeated(f)) {
jsonenc_array(e, val.array_val, f);
} else {
jsonenc_scalar(e, val, f);
}
}
static void jsonenc_msgfields(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m, bool first) {
upb_MessageValue val;
const upb_FieldDef* f;
if (e->options & upb_JsonEncode_EmitDefaults) {
/* Iterate over all fields. */
int i = 0;
int n = upb_MessageDef_FieldCount(m);
for (i = 0; i < n; i++) {
f = upb_MessageDef_Field(m, i);
if (!upb_FieldDef_HasPresence(f) || upb_Message_HasFieldByDef(msg, f)) {
jsonenc_fieldval(e, f, upb_Message_GetFieldByDef(msg, f), &first);
}
}
} else {
/* Iterate over non-empty fields. */
size_t iter = kUpb_Message_Begin;
while (upb_Message_Next(msg, m, e->ext_pool, &f, &val, &iter)) {
jsonenc_fieldval(e, f, val, &first);
}
}
}
static void jsonenc_msg(jsonenc* e, const upb_Message* msg,
const upb_MessageDef* m) {
jsonenc_putstr(e, "{");
jsonenc_msgfields(e, msg, m, true);
jsonenc_putstr(e, "}");
}
static size_t jsonenc_nullz(jsonenc* e, size_t size) {
size_t ret = e->ptr - e->buf + e->overflow;
if (size > 0) {
if (e->ptr == e->end) e->ptr--;
*e->ptr = '\0';
}
return ret;
}
static size_t upb_JsonEncoder_Encode(jsonenc* const e,
const upb_Message* const msg,
const upb_MessageDef* const m,
const size_t size) {
if (UPB_SETJMP(e->err) != 0) return -1;
jsonenc_msgfield(e, msg, m);
if (e->arena) upb_Arena_Free(e->arena);
return jsonenc_nullz(e, size);
}
size_t upb_JsonEncode(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, int options, char* buf,
size_t size, upb_Status* status) {
jsonenc e;
e.buf = buf;
e.ptr = buf;
e.end = UPB_PTRADD(buf, size);
e.overflow = 0;
e.options = options;
e.ext_pool = ext_pool;
e.status = status;
e.arena = NULL;
return upb_JsonEncoder_Encode(&e, msg, m, size);
}
#include <stdlib.h>
// Must be last.
static void* upb_global_allocfunc(upb_alloc* alloc, void* ptr, size_t oldsize,
size_t size) {
UPB_UNUSED(alloc);
UPB_UNUSED(oldsize);
if (size == 0) {
free(ptr);
return NULL;
} else {
return realloc(ptr, size);
}
}
upb_alloc upb_alloc_global = {&upb_global_allocfunc};
#ifdef UPB_TRACING_ENABLED
#include <stdatomic.h>
#endif
#include <stddef.h>
#include <stdint.h>
// Must be last.
static UPB_ATOMIC(size_t) g_max_block_size = UPB_DEFAULT_MAX_BLOCK_SIZE;
void upb_Arena_SetMaxBlockSize(size_t max) {
upb_Atomic_Store(&g_max_block_size, max, memory_order_relaxed);
}
typedef struct upb_MemBlock {
struct upb_MemBlock* next;
// If this block is the head of the list, tracks a growing hint of what the
// *next* block should be; otherwise tracks the size of the actual allocation.
size_t size_or_hint;
// Data follows.
} upb_MemBlock;
typedef struct upb_ArenaInternal {
// upb_alloc* together with a low bit which signals if there is an initial
// block.
uintptr_t block_alloc;
// The cleanup for the allocator. This is called after all the blocks are
// freed in an arena.
upb_AllocCleanupFunc* upb_alloc_cleanup;
// When multiple arenas are fused together, each arena points to a parent
// arena (root points to itself). The root tracks how many live arenas
// reference it.
// The low bit is tagged:
// 0: pointer to parent
// 1: count, left shifted by one
UPB_ATOMIC(uintptr_t) parent_or_count;
// All nodes that are fused together are in a singly-linked list.
// == NULL at end of list.
UPB_ATOMIC(struct upb_ArenaInternal*) next;
// - If the low bit is set, is a pointer to the tail of the list (populated
// for roots, set to self for roots with no fused arenas). This is best
// effort, and it may not always reflect the true tail, but it will always
// be a valid node in the list. This is useful for finding the list tail
// without having to walk the entire list.
// - If the low bit is not set, is a pointer to the previous node in the list,
// such that a->previous_or_tail->next == a.
UPB_ATOMIC(uintptr_t) previous_or_tail;
// Linked list of blocks to free/cleanup.
upb_MemBlock* blocks;
// Total space allocated in blocks, atomic only for SpaceAllocated
UPB_ATOMIC(uintptr_t) space_allocated;
UPB_TSAN_PUBLISHED_MEMBER
} upb_ArenaInternal;
// All public + private state for an arena.
typedef struct {
upb_Arena head;
upb_ArenaInternal body;
} upb_ArenaState;
typedef struct {
upb_ArenaInternal* root;
uintptr_t tagged_count;
} upb_ArenaRoot;
static const size_t kUpb_MemblockReserve =
UPB_ALIGN_MALLOC(sizeof(upb_MemBlock));
// Extracts the (upb_ArenaInternal*) from a (upb_Arena*)
static upb_ArenaInternal* upb_Arena_Internal(const upb_Arena* a) {
return &((upb_ArenaState*)a)->body;
}
// Extracts the (upb_Arena*) from a (upb_ArenaInternal*)
static upb_Arena* upb_Arena_FromInternal(const upb_ArenaInternal* ai) {
ptrdiff_t offset = -offsetof(upb_ArenaState, body);
return UPB_PTR_AT(ai, offset, upb_Arena);
}
static bool _upb_Arena_IsTaggedRefcount(uintptr_t parent_or_count) {
return (parent_or_count & 1) == 1;
}
static bool _upb_Arena_IsTaggedPointer(uintptr_t parent_or_count) {
return (parent_or_count & 1) == 0;
}
static uintptr_t _upb_Arena_RefCountFromTagged(uintptr_t parent_or_count) {
UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
return parent_or_count >> 1;
}
static uintptr_t _upb_Arena_TaggedFromRefcount(uintptr_t refcount) {
uintptr_t parent_or_count = (refcount << 1) | 1;
UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
return parent_or_count;
}
static upb_ArenaInternal* _upb_Arena_PointerFromTagged(
uintptr_t parent_or_count) {
UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
return (upb_ArenaInternal*)parent_or_count;
}
static uintptr_t _upb_Arena_TaggedFromPointer(upb_ArenaInternal* ai) {
uintptr_t parent_or_count = (uintptr_t)ai;
UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
return parent_or_count;
}
static bool _upb_Arena_IsTaggedTail(uintptr_t previous_or_tail) {
return (previous_or_tail & 1) == 1;
}
static bool _upb_Arena_IsTaggedPrevious(uintptr_t previous_or_tail) {
return (previous_or_tail & 1) == 0;
}
static upb_ArenaInternal* _upb_Arena_TailFromTagged(
uintptr_t previous_or_tail) {
UPB_ASSERT(_upb_Arena_IsTaggedTail(previous_or_tail));
return (upb_ArenaInternal*)(previous_or_tail ^ 1);
}
static uintptr_t _upb_Arena_TaggedFromTail(upb_ArenaInternal* tail) {
uintptr_t previous_or_tail = (uintptr_t)tail | 1;
UPB_ASSERT(_upb_Arena_IsTaggedTail(previous_or_tail));
return previous_or_tail;
}
static upb_ArenaInternal* _upb_Arena_PreviousFromTagged(
uintptr_t previous_or_tail) {
UPB_ASSERT(_upb_Arena_IsTaggedPrevious(previous_or_tail));
return (upb_ArenaInternal*)previous_or_tail;
}
static uintptr_t _upb_Arena_TaggedFromPrevious(upb_ArenaInternal* ai) {
uintptr_t previous = (uintptr_t)ai;
UPB_ASSERT(_upb_Arena_IsTaggedPrevious(previous));
return previous;
}
static upb_alloc* _upb_ArenaInternal_BlockAlloc(upb_ArenaInternal* ai) {
return (upb_alloc*)(ai->block_alloc & ~0x1);
}
static uintptr_t _upb_Arena_MakeBlockAlloc(upb_alloc* alloc, bool has_initial) {
uintptr_t alloc_uint = (uintptr_t)alloc;
UPB_ASSERT((alloc_uint & 1) == 0);
return alloc_uint | (has_initial ? 1 : 0);
}
static bool _upb_ArenaInternal_HasInitialBlock(upb_ArenaInternal* ai) {
return ai->block_alloc & 0x1;
}
#ifdef UPB_TRACING_ENABLED
static void (*_init_arena_trace_handler)(const upb_Arena*, size_t size) = NULL;
static void (*_fuse_arena_trace_handler)(const upb_Arena*,
const upb_Arena*) = NULL;
static void (*_free_arena_trace_handler)(const upb_Arena*) = NULL;
void upb_Arena_SetTraceHandler(
void (*initArenaTraceHandler)(const upb_Arena*, size_t size),
void (*fuseArenaTraceHandler)(const upb_Arena*, const upb_Arena*),
void (*freeArenaTraceHandler)(const upb_Arena*)) {
_init_arena_trace_handler = initArenaTraceHandler;
_fuse_arena_trace_handler = fuseArenaTraceHandler;
_free_arena_trace_handler = freeArenaTraceHandler;
}
void upb_Arena_LogInit(const upb_Arena* arena, size_t size) {
if (_init_arena_trace_handler) {
_init_arena_trace_handler(arena, size);
}
}
void upb_Arena_LogFuse(const upb_Arena* arena1, const upb_Arena* arena2) {
if (_fuse_arena_trace_handler) {
_fuse_arena_trace_handler(arena1, arena2);
}
}
void upb_Arena_LogFree(const upb_Arena* arena) {
if (_free_arena_trace_handler) {
_free_arena_trace_handler(arena);
}
}
#endif // UPB_TRACING_ENABLED
// If the param a is already the root, provides no memory order of refcount.
// If it has a parent, then acquire memory order is provided for both the root
// and the refcount. Thread safe.
static upb_ArenaRoot _upb_Arena_FindRoot(upb_ArenaInternal* ai) {
uintptr_t poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_relaxed);
if (_upb_Arena_IsTaggedRefcount(poc)) {
// Fast, relaxed path - arenas that have never been fused to a parent only
// need relaxed memory order, since they're returning themselves and the
// refcount.
return (upb_ArenaRoot){.root = ai, .tagged_count = poc};
}
// Slow path needs acquire order; reloading is cheaper than a fence on ARM
// (LDA vs DMB ISH). Even though this is a reread, we know it must be a tagged
// pointer because if this Arena isn't a root, it can't ever become one.
poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire);
do {
upb_ArenaInternal* next = _upb_Arena_PointerFromTagged(poc);
UPB_TSAN_CHECK_PUBLISHED(next);
UPB_ASSERT(ai != next);
poc = upb_Atomic_Load(&next->parent_or_count, memory_order_acquire);
if (_upb_Arena_IsTaggedPointer(poc)) {
// To keep complexity down, we lazily collapse levels of the tree. This
// keeps it flat in the final case, but doesn't cost much incrementally.
//
// Path splitting keeps time complexity down, see:
// https://en.wikipedia.org/wiki/Disjoint-set_data_structure
UPB_ASSERT(ai != _upb_Arena_PointerFromTagged(poc));
upb_Atomic_Store(&ai->parent_or_count, poc, memory_order_release);
}
ai = next;
} while (_upb_Arena_IsTaggedPointer(poc));
return (upb_ArenaRoot){.root = ai, .tagged_count = poc};
}
uintptr_t upb_Arena_SpaceAllocated(const upb_Arena* arena,
size_t* fused_count) {
upb_ArenaInternal* ai = upb_Arena_Internal(arena);
uintptr_t memsize = 0;
size_t local_fused_count = 0;
// Our root would get updated by any racing fuses before our target arena
// became reachable from the root via the linked list; in order to preserve
// monotonic output (any arena counted by a previous invocation is counted by
// this one), we instead iterate forwards and backwards so that we only see
// the results of completed fuses.
uintptr_t previous_or_tail =
upb_Atomic_Load(&ai->previous_or_tail, memory_order_acquire);
while (_upb_Arena_IsTaggedPrevious(previous_or_tail)) {
upb_ArenaInternal* previous =
_upb_Arena_PreviousFromTagged(previous_or_tail);
UPB_ASSERT(previous != ai);
UPB_TSAN_CHECK_PUBLISHED(previous);
// Unfortunate macro behavior; prior to C11 when using nonstandard atomics
// this returns a void* and can't be used with += without an intermediate
// conversion to an integer.
// Relaxed is safe - no subsequent reads depend this one
uintptr_t allocated =
upb_Atomic_Load(&previous->space_allocated, memory_order_relaxed);
memsize += allocated;
previous_or_tail =
upb_Atomic_Load(&previous->previous_or_tail, memory_order_acquire);
local_fused_count++;
}
while (ai != NULL) {
UPB_TSAN_CHECK_PUBLISHED(ai);
// Unfortunate macro behavior; prior to C11 when using nonstandard atomics
// this returns a void* and can't be used with += without an intermediate
// conversion to an integer.
// Relaxed is safe - no subsequent reads depend this one
uintptr_t allocated =
upb_Atomic_Load(&ai->space_allocated, memory_order_relaxed);
memsize += allocated;
ai = upb_Atomic_Load(&ai->next, memory_order_acquire);
local_fused_count++;
}
if (fused_count) *fused_count = local_fused_count;
return memsize;
}
uint32_t upb_Arena_DebugRefCount(const upb_Arena* a) {
uintptr_t tagged = _upb_Arena_FindRoot(upb_Arena_Internal(a)).tagged_count;
return (uint32_t)_upb_Arena_RefCountFromTagged(tagged);
}
// Adds an allocated block to the head of the list.
static void _upb_Arena_AddBlock(upb_Arena* a, void* ptr, size_t offset,
size_t block_size) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
upb_MemBlock* block = ptr;
block->size_or_hint = block_size;
UPB_ASSERT(offset >= kUpb_MemblockReserve);
char* start = UPB_PTR_AT(block, offset, char);
upb_MemBlock* head = ai->blocks;
if (head && head->next) {
// Fix up size to match actual allocation size
head->size_or_hint = a->UPB_PRIVATE(end) - (char*)head;
}
block->next = head;
ai->blocks = block;
a->UPB_PRIVATE(ptr) = start;
a->UPB_PRIVATE(end) = UPB_PTR_AT(block, block_size, char);
UPB_POISON_MEMORY_REGION(start, a->UPB_PRIVATE(end) - start);
UPB_ASSERT(UPB_PRIVATE(_upb_ArenaHas)(a) >= block_size - offset);
}
// Fulfills the allocation request by allocating a new block. Returns NULL on
// allocation failure.
void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(upb_Arena* a, size_t size) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
if (!ai->block_alloc) return NULL;
size_t last_size = 128;
size_t current_free = 0;
upb_MemBlock* last_block = ai->blocks;
if (last_block) {
last_size = a->UPB_PRIVATE(end) - (char*)last_block;
current_free = a->UPB_PRIVATE(end) - a->UPB_PRIVATE(ptr);
}
// Relaxed order is safe here as we don't need any ordering with the setter.
size_t max_block_size =
upb_Atomic_Load(&g_max_block_size, memory_order_relaxed);
// Don't naturally grow beyond the max block size.
size_t target_size = UPB_MIN(last_size * 2, max_block_size);
size_t future_free = UPB_MAX(size, target_size - kUpb_MemblockReserve) - size;
// We want to preserve exponential growth in block size without wasting too
// much unused space at the end of blocks. Once the head of our blocks list is
// large enough to always trigger a max-sized block for all subsequent
// allocations, allocate blocks that would net reduce free space behind it.
if (last_block && current_free > future_free &&
target_size < max_block_size) {
last_size = last_block->size_or_hint;
// Recalculate sizes with possibly larger last_size
target_size = UPB_MIN(last_size * 2, max_block_size);
future_free = UPB_MAX(size, target_size - kUpb_MemblockReserve) - size;
}
bool insert_after_head = false;
// Only insert after head if an allocated block is present; we don't want to
// continue allocating out of the initial block because we'll have no way of
// restoring the size of our allocated block if we add another.
if (last_block && current_free >= future_free) {
// If we're still going to net reduce free space with this new block, then
// only allocate the precise size requested and keep the current last block
// as the active block for future allocations.
insert_after_head = true;
target_size = size + kUpb_MemblockReserve;
// Add something to our previous size each time, so that eventually we
// will reach the max block size. Allocations larger than the max block size
// will always get their own backing allocation, so don't include them.
if (target_size <= max_block_size) {
last_block->size_or_hint =
UPB_MIN(last_block->size_or_hint + (size >> 1), max_block_size >> 1);
}
}
// We may need to exceed the max block size if the user requested a large
// allocation.
size_t block_size = UPB_MAX(kUpb_MemblockReserve + size, target_size);
upb_MemBlock* block =
upb_malloc(_upb_ArenaInternal_BlockAlloc(ai), block_size);
if (!block) return NULL;
// Atomic add not required here, as threads won't race allocating blocks, plus
// atomic fetch-add is slower than load/add/store on arm devices compiled
// targetting pre-v8.1. Relaxed order is safe as nothing depends on order of
// size allocated.
uintptr_t old_space_allocated =
upb_Atomic_Load(&ai->space_allocated, memory_order_relaxed);
upb_Atomic_Store(&ai->space_allocated, old_space_allocated + block_size,
memory_order_relaxed);
if (UPB_UNLIKELY(insert_after_head)) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
block->size_or_hint = block_size;
upb_MemBlock* head = ai->blocks;
block->next = head->next;
head->next = block;
char* allocated = UPB_PTR_AT(block, kUpb_MemblockReserve, char);
UPB_POISON_MEMORY_REGION(allocated + size, UPB_ASAN_GUARD_SIZE);
return allocated;
} else {
_upb_Arena_AddBlock(a, block, kUpb_MemblockReserve, block_size);
UPB_ASSERT(UPB_PRIVATE(_upb_ArenaHas)(a) >= size);
return upb_Arena_Malloc(a, size - UPB_ASAN_GUARD_SIZE);
}
}
static upb_Arena* _upb_Arena_InitSlow(upb_alloc* alloc, size_t first_size) {
const size_t first_block_overhead =
UPB_ALIGN_MALLOC(kUpb_MemblockReserve + sizeof(upb_ArenaState));
upb_ArenaState* a;
// We need to malloc the initial block.
char* mem;
size_t block_size =
first_block_overhead +
UPB_MAX(256, UPB_ALIGN_MALLOC(first_size) + UPB_ASAN_GUARD_SIZE);
if (!alloc || !(mem = upb_malloc(alloc, block_size))) {
return NULL;
}
a = UPB_PTR_AT(mem, kUpb_MemblockReserve, upb_ArenaState);
a->body.block_alloc = _upb_Arena_MakeBlockAlloc(alloc, 0);
upb_Atomic_Init(&a->body.parent_or_count, _upb_Arena_TaggedFromRefcount(1));
upb_Atomic_Init(&a->body.next, NULL);
upb_Atomic_Init(&a->body.previous_or_tail,
_upb_Arena_TaggedFromTail(&a->body));
upb_Atomic_Init(&a->body.space_allocated, block_size);
a->body.blocks = NULL;
a->body.upb_alloc_cleanup = NULL;
UPB_TSAN_INIT_PUBLISHED(&a->body);
_upb_Arena_AddBlock(&a->head, mem, first_block_overhead, block_size);
return &a->head;
}
upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc) {
UPB_ASSERT(sizeof(void*) * UPB_ARENA_SIZE_HACK >= sizeof(upb_ArenaState));
upb_ArenaState* a;
if (mem) {
/* Align initial pointer up so that we return properly-aligned pointers. */
void* aligned = (void*)UPB_ALIGN_MALLOC((uintptr_t)mem);
size_t delta = (uintptr_t)aligned - (uintptr_t)mem;
n = delta <= n ? n - delta : 0;
mem = aligned;
}
if (UPB_UNLIKELY(n < sizeof(upb_ArenaState) || !mem)) {
upb_Arena* ret = _upb_Arena_InitSlow(alloc, mem ? 0 : n);
#ifdef UPB_TRACING_ENABLED
upb_Arena_LogInit(ret, n);
#endif
return ret;
}
a = mem;
upb_Atomic_Init(&a->body.parent_or_count, _upb_Arena_TaggedFromRefcount(1));
upb_Atomic_Init(&a->body.next, NULL);
upb_Atomic_Init(&a->body.previous_or_tail,
_upb_Arena_TaggedFromTail(&a->body));
upb_Atomic_Init(&a->body.space_allocated, 0);
a->body.blocks = NULL;
a->body.upb_alloc_cleanup = NULL;
a->body.block_alloc = _upb_Arena_MakeBlockAlloc(alloc, 1);
a->head.UPB_PRIVATE(ptr) = (void*)UPB_ALIGN_MALLOC((uintptr_t)(a + 1));
a->head.UPB_PRIVATE(end) = UPB_PTR_AT(mem, n, char);
UPB_TSAN_INIT_PUBLISHED(&a->body);
#ifdef UPB_TRACING_ENABLED
upb_Arena_LogInit(&a->head, n);
#endif
return &a->head;
}
static void _upb_Arena_DoFree(upb_ArenaInternal* ai) {
UPB_ASSERT(_upb_Arena_RefCountFromTagged(ai->parent_or_count) == 1);
while (ai != NULL) {
UPB_TSAN_CHECK_PUBLISHED(ai);
// Load first since arena itself is likely from one of its blocks.
upb_ArenaInternal* next_arena =
(upb_ArenaInternal*)upb_Atomic_Load(&ai->next, memory_order_acquire);
// Freeing may have memory barriers that confuse tsan, so assert immediately
// after load here
if (next_arena) {
UPB_TSAN_CHECK_PUBLISHED(next_arena);
}
upb_alloc* block_alloc = _upb_ArenaInternal_BlockAlloc(ai);
upb_MemBlock* block = ai->blocks;
if (block && block->next) {
block->size_or_hint =
upb_Arena_FromInternal(ai)->UPB_PRIVATE(end) - (char*)block;
}
upb_AllocCleanupFunc* alloc_cleanup = *ai->upb_alloc_cleanup;
while (block != NULL) {
// Load first since we are deleting block.
upb_MemBlock* next_block = block->next;
upb_free_sized(block_alloc, block, block->size_or_hint);
block = next_block;
}
if (alloc_cleanup != NULL) {
alloc_cleanup(block_alloc);
}
ai = next_arena;
}
}
void upb_Arena_Free(upb_Arena* a) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
// Cannot be replaced with _upb_Arena_FindRoot, as that provides only a
// relaxed read of the refcount if ai is already the root.
uintptr_t poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire);
retry:
while (_upb_Arena_IsTaggedPointer(poc)) {
ai = _upb_Arena_PointerFromTagged(poc);
UPB_TSAN_CHECK_PUBLISHED(ai);
poc = upb_Atomic_Load(&ai->parent_or_count, memory_order_acquire);
}
// compare_exchange or fetch_sub are RMW operations, which are more
// expensive then direct loads. As an optimization, we only do RMW ops
// when we need to update things for other threads to see.
if (poc == _upb_Arena_TaggedFromRefcount(1)) {
#ifdef UPB_TRACING_ENABLED
upb_Arena_LogFree(a);
#endif
_upb_Arena_DoFree(ai);
return;
}
if (upb_Atomic_CompareExchangeWeak(
&ai->parent_or_count, &poc,
_upb_Arena_TaggedFromRefcount(_upb_Arena_RefCountFromTagged(poc) - 1),
memory_order_release, memory_order_acquire)) {
// We were >1 and we decremented it successfully, so we are done.
return;
}
// We failed our update, so someone has done something, retry the whole
// process, but the failed exchange reloaded `poc` for us.
goto retry;
}
// Logically performs the following operation, in a way that is safe against
// racing fuses:
// ret = TAIL(parent)
// ret->next = child
// return ret
//
// The caller is therefore guaranteed that ret->next == child.
static upb_ArenaInternal* _upb_Arena_LinkForward(
upb_ArenaInternal* const parent, upb_ArenaInternal* child) {
UPB_TSAN_CHECK_PUBLISHED(parent);
uintptr_t parent_previous_or_tail =
upb_Atomic_Load(&parent->previous_or_tail, memory_order_acquire);
// Optimization: use parent->previous_or_tail to skip to TAIL(parent) in O(1)
// time when possible. This is the common case because we just fused into
// parent, suggesting that it should be a root with a cached tail.
//
// However, if there was a racing fuse, parent may no longer be a root, in
// which case we need to walk the entire list to find the tail. The tail
// pointer is also not guaranteed to be the true tail, so even when the
// optimization is taken, we still need to walk list nodes to find the true
// tail.
upb_ArenaInternal* parent_tail =
_upb_Arena_IsTaggedTail(parent_previous_or_tail)
? _upb_Arena_TailFromTagged(parent_previous_or_tail)
: parent;
UPB_TSAN_CHECK_PUBLISHED(parent_tail);
upb_ArenaInternal* parent_tail_next =
upb_Atomic_Load(&parent_tail->next, memory_order_acquire);
do {
// Walk the list to find the true tail (a node with next == NULL).
while (parent_tail_next != NULL) {
parent_tail = parent_tail_next;
UPB_TSAN_CHECK_PUBLISHED(parent_tail);
parent_tail_next =
upb_Atomic_Load(&parent_tail->next, memory_order_acquire);
}
} while (!upb_Atomic_CompareExchangeWeak( // Replace a NULL next with child.
&parent_tail->next, &parent_tail_next, child, memory_order_release,
memory_order_acquire));
return parent_tail;
}
// Updates parent->previous_or_tail = child->previous_or_tail in hopes that the
// latter represents the true tail of the newly-combined list.
//
// This is a best-effort operation that may set the tail to a stale value, and
// may fail to update the tail at all.
void _upb_Arena_UpdateParentTail(upb_ArenaInternal* parent,
upb_ArenaInternal* child) {
// We are guaranteed that child->previous_or_tail is tagged, because we have
// just transitioned child from root -> non-root, which is an exclusive
// operation that can only happen once. So we are the exclusive updater of
// child->previous_or_tail that can transition it from tagged to untagged.
//
// However, we are not guaranteed that child->previous_or_tail is the true
// tail. A racing fuse may have appended to child's list but not yet updated
// child->previous_or_tail.
uintptr_t child_previous_or_tail =
upb_Atomic_Load(&child->previous_or_tail, memory_order_acquire);
upb_ArenaInternal* new_parent_tail =
_upb_Arena_TailFromTagged(child_previous_or_tail);
UPB_TSAN_CHECK_PUBLISHED(new_parent_tail);
// If another thread fused with parent, such that it is no longer a root,
// don't overwrite their previous pointer with our tail. Relaxed order is fine
// here as we only inspect the tag bit.
uintptr_t parent_previous_or_tail =
upb_Atomic_Load(&parent->previous_or_tail, memory_order_relaxed);
if (_upb_Arena_IsTaggedTail(parent_previous_or_tail)) {
upb_Atomic_CompareExchangeStrong(
&parent->previous_or_tail, &parent_previous_or_tail,
_upb_Arena_TaggedFromTail(new_parent_tail), memory_order_release,
memory_order_relaxed);
}
}
static void _upb_Arena_LinkBackward(upb_ArenaInternal* child,
upb_ArenaInternal* old_parent_tail) {
// Link child to parent going backwards, for SpaceAllocated. This transitions
// child->previous_or_tail from tail (tagged) to previous (untagged), after
// which its value is immutable.
//
// - We are guaranteed that no other threads are also attempting to perform
// this transition (tail -> previous), because we just updated
// old_parent_tail->next from NULL to non-NULL, an exclusive operation that
// can only happen once.
//
// - _upb_Arena_UpdateParentTail() uses CAS to ensure that it
// does not perform the reverse transition (previous -> tail).
//
// - We are guaranteed that old_parent_tail is the correct "previous" pointer,
// even in the presence of racing fuses that are adding more nodes to the
// list, because _upb_Arena_LinkForward() guarantees that:
// old_parent_tail->next == child.
upb_Atomic_Store(&child->previous_or_tail,
_upb_Arena_TaggedFromPrevious(old_parent_tail),
memory_order_release);
}
static void _upb_Arena_DoFuseArenaLists(upb_ArenaInternal* const parent,
upb_ArenaInternal* child) {
upb_ArenaInternal* old_parent_tail = _upb_Arena_LinkForward(parent, child);
_upb_Arena_UpdateParentTail(parent, child);
_upb_Arena_LinkBackward(child, old_parent_tail);
}
void upb_Arena_SetAllocCleanup(upb_Arena* a, upb_AllocCleanupFunc* func) {
UPB_TSAN_CHECK_READ(a->UPB_ONLYBITS(ptr));
upb_ArenaInternal* ai = upb_Arena_Internal(a);
UPB_ASSERT(ai->upb_alloc_cleanup == NULL);
ai->upb_alloc_cleanup = func;
}
// Thread safe.
static upb_ArenaInternal* _upb_Arena_DoFuse(upb_ArenaInternal** ai1,
upb_ArenaInternal** ai2,
uintptr_t* ref_delta) {
// `parent_or_count` has two distinct modes
// - parent pointer mode
// - refcount mode
//
// In parent pointer mode, it may change what pointer it refers to in the
// tree, but it will always approach a root. Any operation that walks the
// tree to the root may collapse levels of the tree concurrently.
upb_ArenaRoot r1 = _upb_Arena_FindRoot(*ai1);
upb_ArenaRoot r2 = _upb_Arena_FindRoot(*ai2);
if (r1.root == r2.root) return r1.root; // Already fused.
*ai1 = r1.root;
*ai2 = r2.root;
// Avoid cycles by always fusing into the root with the lower address.
if ((uintptr_t)r1.root > (uintptr_t)r2.root) {
upb_ArenaRoot tmp = r1;
r1 = r2;
r2 = tmp;
}
// The moment we install `r1` as the parent for `r2` all racing frees may
// immediately begin decrementing `r1`'s refcount (including pending
// increments to that refcount and their frees!). We need to add `r2`'s refs
// now, so that `r1` can withstand any unrefs that come from r2.
//
// Note that while it is possible for `r2`'s refcount to increase
// asynchronously, we will not actually do the reparenting operation below
// unless `r2`'s refcount is unchanged from when we read it.
//
// Note that we may have done this previously, either to this node or a
// different node, during a previous and failed DoFuse() attempt. But we will
// not lose track of these refs because we always add them to our overall
// delta.
uintptr_t r2_untagged_count = r2.tagged_count & ~1;
uintptr_t with_r2_refs = r1.tagged_count + r2_untagged_count;
if (!upb_Atomic_CompareExchangeStrong(
&r1.root->parent_or_count, &r1.tagged_count, with_r2_refs,
memory_order_release, memory_order_acquire)) {
return NULL;
}
// Perform the actual fuse by removing the refs from `r2` and swapping in the
// parent pointer.
if (!upb_Atomic_CompareExchangeStrong(
&r2.root->parent_or_count, &r2.tagged_count,
_upb_Arena_TaggedFromPointer(r1.root), memory_order_release,
memory_order_acquire)) {
// We'll need to remove the excess refs we added to r1 previously.
*ref_delta += r2_untagged_count;
return NULL;
}
// Now that the fuse has been performed (and can no longer fail) we need to
// append `r2` to `r1`'s linked list.
_upb_Arena_DoFuseArenaLists(r1.root, r2.root);
return r1.root;
}
// Thread safe.
static bool _upb_Arena_FixupRefs(upb_ArenaInternal* new_root,
uintptr_t ref_delta) {
if (ref_delta == 0) return true; // No fixup required.
// Relaxed order is safe here as if the value is a pointer, we don't deref it
// or publish it anywhere else. The refcount does provide memory order
// between allocations on arenas and the eventual free and thus normally
// requires acquire/release; but in this case any edges provided by the refs
// we are cleaning up were already provided by the fuse operation itself. It's
// not valid for a decrement that could cause the overall fused arena to reach
// a zero refcount to race with this function, as that could result in a
// use-after-free anyway.
uintptr_t poc =
upb_Atomic_Load(&new_root->parent_or_count, memory_order_relaxed);
if (_upb_Arena_IsTaggedPointer(poc)) return false;
uintptr_t with_refs = poc - ref_delta;
UPB_ASSERT(!_upb_Arena_IsTaggedPointer(with_refs));
// Relaxed order on success is safe here, for the same reasons as the relaxed
// read above. Relaxed order is safe on failure because the updated value is
// stored in a local variable which goes immediately out of scope; the retry
// loop will reread what it needs with proper memory order.
return upb_Atomic_CompareExchangeStrong(&new_root->parent_or_count, &poc,
with_refs, memory_order_relaxed,
memory_order_relaxed);
}
bool upb_Arena_Fuse(const upb_Arena* a1, const upb_Arena* a2) {
if (a1 == a2) return true; // trivial fuse
#ifdef UPB_TRACING_ENABLED
upb_Arena_LogFuse(a1, a2);
#endif
upb_ArenaInternal* ai1 = upb_Arena_Internal(a1);
upb_ArenaInternal* ai2 = upb_Arena_Internal(a2);
// Do not fuse initial blocks since we cannot lifetime extend them.
// Any other fuse scenario is allowed.
if (_upb_ArenaInternal_HasInitialBlock(ai1) ||
_upb_ArenaInternal_HasInitialBlock(ai2)) {
return false;
}
// The number of refs we ultimately need to transfer to the new root.
uintptr_t ref_delta = 0;
while (true) {
upb_ArenaInternal* new_root = _upb_Arena_DoFuse(&ai1, &ai2, &ref_delta);
if (new_root != NULL && _upb_Arena_FixupRefs(new_root, ref_delta)) {
return true;
}
}
}
bool upb_Arena_IsFused(const upb_Arena* a, const upb_Arena* b) {
if (a == b) return true; // trivial fuse
upb_ArenaInternal* ra = _upb_Arena_FindRoot(upb_Arena_Internal(a)).root;
upb_ArenaInternal* rb = upb_Arena_Internal(b);
while (true) {
rb = _upb_Arena_FindRoot(rb).root;
if (ra == rb) return true;
upb_ArenaInternal* tmp = _upb_Arena_FindRoot(ra).root;
if (ra == tmp) return false;
// a's root changed since we last checked. Retry.
ra = tmp;
}
}
bool upb_Arena_IncRefFor(const upb_Arena* a, const void* owner) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
if (_upb_ArenaInternal_HasInitialBlock(ai)) return false;
upb_ArenaRoot r;
r.root = ai;
retry:
r = _upb_Arena_FindRoot(r.root);
if (upb_Atomic_CompareExchangeWeak(
&r.root->parent_or_count, &r.tagged_count,
_upb_Arena_TaggedFromRefcount(
_upb_Arena_RefCountFromTagged(r.tagged_count) + 1),
// Relaxed order is safe on success, incrementing the refcount
// need not perform any synchronization with the eventual free of the
// arena - that's provided by decrements.
memory_order_relaxed,
// Relaxed order is safe on failure as r.tagged_count is immediately
// overwritten by retrying the find root operation.
memory_order_relaxed)) {
// We incremented it successfully, so we are done.
return true;
}
// We failed update due to parent switching on the arena.
goto retry;
}
void upb_Arena_DecRefFor(const upb_Arena* a, const void* owner) {
upb_Arena_Free((upb_Arena*)a);
}
upb_alloc* upb_Arena_GetUpbAlloc(upb_Arena* a) {
UPB_TSAN_CHECK_READ(a->UPB_ONLYBITS(ptr));
upb_ArenaInternal* ai = upb_Arena_Internal(a);
return _upb_ArenaInternal_BlockAlloc(ai);
}
void UPB_PRIVATE(_upb_Arena_SwapIn)(upb_Arena* des, const upb_Arena* src) {
upb_ArenaInternal* desi = upb_Arena_Internal(des);
upb_ArenaInternal* srci = upb_Arena_Internal(src);
*des = *src;
desi->block_alloc = srci->block_alloc;
desi->blocks = srci->blocks;
}
void UPB_PRIVATE(_upb_Arena_SwapOut)(upb_Arena* des, const upb_Arena* src) {
upb_ArenaInternal* desi = upb_Arena_Internal(des);
upb_ArenaInternal* srci = upb_Arena_Internal(src);
*des = *src;
desi->blocks = srci->blocks;
}
bool _upb_Arena_WasLastAlloc(struct upb_Arena* a, void* ptr, size_t oldsize) {
upb_ArenaInternal* ai = upb_Arena_Internal(a);
upb_MemBlock* block = ai->blocks;
if (block == NULL) return false;
block = block->next;
if (block == NULL) return false;
char* start = UPB_PTR_AT(block, kUpb_MemblockReserve, char);
return ptr == start && oldsize == block->size_or_hint - kUpb_MemblockReserve;
}
// Must be last.
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* m,
const upb_MiniTableField* f,
upb_Message* map_entry_message, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(map_entry_message));
const upb_MiniTable* map_entry_mini_table =
upb_MiniTable_MapEntrySubMessage(m, f);
UPB_ASSERT(map_entry_mini_table);
const upb_MiniTableField* map_entry_key_field =
upb_MiniTable_MapKey(map_entry_mini_table);
const upb_MiniTableField* map_entry_value_field =
upb_MiniTable_MapValue(map_entry_mini_table);
// Map key/value cannot have explicit defaults,
// hence assuming a zero default is valid.
upb_MessageValue default_val = upb_MessageValue_Zero();
upb_MessageValue map_entry_key =
upb_Message_GetField(map_entry_message, map_entry_key_field, default_val);
upb_MessageValue map_entry_value = upb_Message_GetField(
map_entry_message, map_entry_value_field, default_val);
return upb_Map_Set(map, map_entry_key, map_entry_value, arena);
}
#include <stdint.h>
#include <string.h>
// Must be last.
upb_Array* upb_Array_New(upb_Arena* a, upb_CType type) {
const int lg2 = UPB_PRIVATE(_upb_CType_SizeLg2)(type);
return UPB_PRIVATE(_upb_Array_New)(a, 4, lg2);
}
upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i) {
UPB_ASSERT(i < upb_Array_Size(arr));
upb_MessageValue ret;
const char* data = upb_Array_DataPtr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
memcpy(&ret, data + (i << lg2), 1 << lg2);
return ret;
}
upb_Message* upb_Array_GetMutable(upb_Array* arr, size_t i) {
UPB_ASSERT(i < upb_Array_Size(arr));
size_t elem_size = sizeof(upb_Message*);
UPB_ASSERT(elem_size == (1 << UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr)));
char* data = upb_Array_MutableDataPtr(arr);
upb_Message* ret;
memcpy(&ret, data + (i * elem_size), elem_size);
UPB_ASSERT(!upb_Message_IsFrozen(ret));
return ret;
}
void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
UPB_ASSERT(i < upb_Array_Size(arr));
char* data = upb_Array_MutableDataPtr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
memcpy(data + (i << lg2), &val, 1 << lg2);
}
bool upb_Array_Append(upb_Array* arr, upb_MessageValue val, upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
UPB_ASSERT(arena);
if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(
arr, arr->UPB_PRIVATE(size) + 1, arena)) {
return false;
}
upb_Array_Set(arr, arr->UPB_PRIVATE(size) - 1, val);
return true;
}
void upb_Array_Move(upb_Array* arr, size_t dst_idx, size_t src_idx,
size_t count) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
char* data = upb_Array_MutableDataPtr(arr);
memmove(&data[dst_idx << lg2], &data[src_idx << lg2], count << lg2);
}
bool upb_Array_Insert(upb_Array* arr, size_t i, size_t count,
upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
UPB_ASSERT(arena);
UPB_ASSERT(i <= arr->UPB_PRIVATE(size));
UPB_ASSERT(count + arr->UPB_PRIVATE(size) >= count);
const size_t oldsize = arr->UPB_PRIVATE(size);
if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(
arr, arr->UPB_PRIVATE(size) + count, arena)) {
return false;
}
upb_Array_Move(arr, i + count, i, oldsize - i);
return true;
}
/*
* i end arr->size
* |------------|XXXXXXXX|--------|
*/
void upb_Array_Delete(upb_Array* arr, size_t i, size_t count) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
const size_t end = i + count;
UPB_ASSERT(i <= end);
UPB_ASSERT(end <= arr->UPB_PRIVATE(size));
upb_Array_Move(arr, i, end, arr->UPB_PRIVATE(size) - end);
arr->UPB_PRIVATE(size) -= count;
}
bool upb_Array_Resize(upb_Array* arr, size_t size, upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
const size_t oldsize = arr->UPB_PRIVATE(size);
if (UPB_UNLIKELY(
!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena))) {
return false;
}
const size_t newsize = arr->UPB_PRIVATE(size);
if (newsize > oldsize) {
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
char* data = upb_Array_MutableDataPtr(arr);
memset(data + (oldsize << lg2), 0, (newsize - oldsize) << lg2);
}
return true;
}
bool UPB_PRIVATE(_upb_Array_Realloc)(upb_Array* array, size_t min_capacity,
upb_Arena* arena) {
size_t new_capacity = UPB_MAX(array->UPB_PRIVATE(capacity), 4);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array);
size_t old_bytes = array->UPB_PRIVATE(capacity) << lg2;
void* ptr = upb_Array_MutableDataPtr(array);
// Log2 ceiling of size.
while (new_capacity < min_capacity) new_capacity *= 2;
const size_t new_bytes = new_capacity << lg2;
ptr = upb_Arena_Realloc(arena, ptr, old_bytes, new_bytes);
if (!ptr) return false;
UPB_PRIVATE(_upb_Array_SetTaggedPtr)(array, ptr, lg2);
array->UPB_PRIVATE(capacity) = new_capacity;
return true;
}
void upb_Array_Freeze(upb_Array* arr, const upb_MiniTable* m) {
if (upb_Array_IsFrozen(arr)) return;
UPB_PRIVATE(_upb_Array_ShallowFreeze)(arr);
if (m) {
const size_t size = upb_Array_Size(arr);
for (size_t i = 0; i < size; i++) {
upb_MessageValue val = upb_Array_Get(arr, i);
upb_Message_Freeze((upb_Message*)val.msg_val, m);
}
}
}
#include <stddef.h>
#include <stdint.h>
// Must be last.
bool upb_Message_NextExtensionReverse(const upb_Message* msg,
const upb_MiniTableExtension** result,
uintptr_t* iter) {
upb_MessageValue val;
return UPB_PRIVATE(_upb_Message_NextExtensionReverse)(msg, result, &val,
iter);
}
const upb_MiniTableExtension* upb_Message_FindExtensionByNumber(
const upb_Message* msg, uint32_t field_number) {
uintptr_t iter = kUpb_Message_ExtensionBegin;
const upb_MiniTableExtension* result;
while (upb_Message_NextExtensionReverse(msg, &result, &iter)) {
if (upb_MiniTableExtension_Number(result) == field_number) return result;
}
return NULL;
}
#include <stdint.h>
#include <string.h>
// Must be last.
// Strings/bytes are special-cased in maps.
char _upb_Map_CTypeSizeTable[12] = {
[kUpb_CType_Bool] = 1,
[kUpb_CType_Float] = 4,
[kUpb_CType_Int32] = 4,
[kUpb_CType_UInt32] = 4,
[kUpb_CType_Enum] = 4,
[kUpb_CType_Message] = sizeof(void*),
[kUpb_CType_Double] = 8,
[kUpb_CType_Int64] = 8,
[kUpb_CType_UInt64] = 8,
[kUpb_CType_String] = UPB_MAPTYPE_STRING,
[kUpb_CType_Bytes] = UPB_MAPTYPE_STRING,
};
upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, upb_CType value_type) {
return _upb_Map_New(a, _upb_Map_CTypeSize(key_type),
_upb_Map_CTypeSize(value_type));
}
size_t upb_Map_Size(const upb_Map* map) { return _upb_Map_Size(map); }
bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
upb_MessageValue* val) {
return _upb_Map_Get(map, &key, map->key_size, val, map->val_size);
}
struct upb_Message* upb_Map_GetMutable(upb_Map* map, upb_MessageValue key) {
UPB_ASSERT(map->val_size == sizeof(upb_Message*));
upb_Message* val = NULL;
if (_upb_Map_Get(map, &key, map->key_size, &val, sizeof(upb_Message*))) {
return val;
} else {
return NULL;
}
}
void upb_Map_Clear(upb_Map* map) { _upb_Map_Clear(map); }
upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key,
upb_MessageValue val, upb_Arena* arena) {
UPB_ASSERT(arena);
return (upb_MapInsertStatus)_upb_Map_Insert(map, &key, map->key_size, &val,
map->val_size, arena);
}
bool upb_Map_Delete(upb_Map* map, upb_MessageValue key, upb_MessageValue* val) {
upb_value v;
const bool removed = _upb_Map_Delete(map, &key, map->key_size, &v);
if (val) _upb_map_fromvalue(v, val, map->val_size);
return removed;
}
bool upb_Map_Next(const upb_Map* map, upb_MessageValue* key,
upb_MessageValue* val, size_t* iter) {
upb_value v;
bool ret;
if (map->UPB_PRIVATE(is_strtable)) {
upb_StringView strkey;
ret = upb_strtable_next2(&map->t.strtable, &strkey, &v, (intptr_t*)iter);
if (ret) {
_upb_map_fromkey(strkey, key, map->key_size);
}
} else {
uintptr_t intkey;
ret = upb_inttable_next(&map->t.inttable, &intkey, &v, (intptr_t*)iter);
if (ret) {
memcpy(key, &intkey, map->key_size);
}
}
if (ret) {
_upb_map_fromvalue(v, val, map->val_size);
}
return ret;
}
UPB_API void upb_Map_SetEntryValue(upb_Map* map, size_t iter,
upb_MessageValue val) {
upb_value v;
_upb_map_tovalue(&val, map->val_size, &v, NULL);
if (map->UPB_PRIVATE(is_strtable)) {
upb_strtable_setentryvalue(&map->t.strtable, iter, v);
} else {
upb_inttable_setentryvalue(&map->t.inttable, iter, v);
}
}
bool upb_MapIterator_Next(const upb_Map* map, size_t* iter) {
return _upb_map_next(map, iter);
}
bool upb_MapIterator_Done(const upb_Map* map, size_t iter) {
UPB_ASSERT(iter != kUpb_Map_Begin);
if (map->UPB_PRIVATE(is_strtable)) {
upb_strtable_iter i;
i.t = &map->t.strtable;
i.index = iter;
return upb_strtable_done(&i);
} else {
return upb_inttable_done(&map->t.inttable, iter);
}
}
// Returns the key and value for this entry of the map.
upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter) {
upb_MessageValue ret;
if (map->UPB_PRIVATE(is_strtable)) {
upb_strtable_iter i;
i.t = &map->t.strtable;
i.index = iter;
_upb_map_fromkey(upb_strtable_iter_key(&i), &ret, map->key_size);
} else {
uintptr_t intkey = upb_inttable_iter_key(&map->t.inttable, iter);
memcpy(&ret, &intkey, map->key_size);
}
return ret;
}
upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter) {
upb_value v;
if (map->UPB_PRIVATE(is_strtable)) {
upb_strtable_iter i;
i.t = &map->t.strtable;
i.index = iter;
v = upb_strtable_iter_value(&i);
} else {
v = upb_inttable_iter_value(&map->t.inttable, iter);
}
upb_MessageValue ret;
_upb_map_fromvalue(v, &ret, map->val_size);
return ret;
}
void upb_Map_Freeze(upb_Map* map, const upb_MiniTable* m) {
if (upb_Map_IsFrozen(map)) return;
UPB_PRIVATE(_upb_Map_ShallowFreeze)(map);
if (m) {
size_t iter = kUpb_Map_Begin;
upb_MessageValue key, val;
while (upb_Map_Next(map, &key, &val, &iter)) {
upb_Message_Freeze((upb_Message*)val.msg_val, m);
}
}
}
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size) {
upb_Map* map = upb_Arena_Malloc(a, sizeof(upb_Map));
if (!map) return NULL;
if (key_size <= sizeof(uintptr_t) && key_size != UPB_MAPTYPE_STRING) {
if (!upb_inttable_init(&map->t.inttable, a)) return NULL;
map->UPB_PRIVATE(is_strtable) = false;
} else {
if (!upb_strtable_init(&map->t.strtable, 4, a)) return NULL;
map->UPB_PRIVATE(is_strtable) = true;
}
map->key_size = key_size;
map->val_size = value_size;
map->UPB_PRIVATE(is_frozen) = false;
return map;
}
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Must be last.
static int _upb_mapsorter_intkeys(const void* _a, const void* _b) {
const upb_tabent* const* a = _a;
const upb_tabent* const* b = _b;
uintptr_t a_key = (*a)->key.num;
uintptr_t b_key = (*b)->key.num;
return a_key < b_key ? -1 : a_key > b_key;
}
static void _upb_mapsorter_getkeys(const void* _a, const void* _b, void* a_key,
void* b_key, size_t size) {
const upb_tabent* const* a = _a;
const upb_tabent* const* b = _b;
upb_StringView a_tabkey = upb_key_strview((*a)->key);
upb_StringView b_tabkey = upb_key_strview((*b)->key);
_upb_map_fromkey(a_tabkey, a_key, size);
_upb_map_fromkey(b_tabkey, b_key, size);
}
static int _upb_mapsorter_cmpi64(const void* _a, const void* _b) {
int64_t a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 8);
return a < b ? -1 : a > b;
}
static int _upb_mapsorter_cmpu64(const void* _a, const void* _b) {
uint64_t a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 8);
return a < b ? -1 : a > b;
}
static int _upb_mapsorter_cmpi32(const void* _a, const void* _b) {
int32_t a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 4);
return a < b ? -1 : a > b;
}
static int _upb_mapsorter_cmpu32(const void* _a, const void* _b) {
uint32_t a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 4);
return a < b ? -1 : a > b;
}
static int _upb_mapsorter_cmpbool(const void* _a, const void* _b) {
bool a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, 1);
return a < b ? -1 : a > b;
}
static int _upb_mapsorter_cmpstr(const void* _a, const void* _b) {
upb_StringView a, b;
_upb_mapsorter_getkeys(_a, _b, &a, &b, UPB_MAPTYPE_STRING);
size_t common_size = UPB_MIN(a.size, b.size);
int cmp = memcmp(a.data, b.data, common_size);
if (cmp) return -cmp;
return a.size < b.size ? -1 : a.size > b.size;
}
static int (*const compar[kUpb_FieldType_SizeOf])(const void*, const void*) = {
[kUpb_FieldType_Int64] = _upb_mapsorter_cmpi64,
[kUpb_FieldType_SFixed64] = _upb_mapsorter_cmpi64,
[kUpb_FieldType_SInt64] = _upb_mapsorter_cmpi64,
[kUpb_FieldType_UInt64] = _upb_mapsorter_cmpu64,
[kUpb_FieldType_Fixed64] = _upb_mapsorter_cmpu64,
[kUpb_FieldType_Int32] = _upb_mapsorter_cmpi32,
[kUpb_FieldType_SInt32] = _upb_mapsorter_cmpi32,
[kUpb_FieldType_SFixed32] = _upb_mapsorter_cmpi32,
[kUpb_FieldType_Enum] = _upb_mapsorter_cmpi32,
[kUpb_FieldType_UInt32] = _upb_mapsorter_cmpu32,
[kUpb_FieldType_Fixed32] = _upb_mapsorter_cmpu32,
[kUpb_FieldType_Bool] = _upb_mapsorter_cmpbool,
[kUpb_FieldType_String] = _upb_mapsorter_cmpstr,
[kUpb_FieldType_Bytes] = _upb_mapsorter_cmpstr,
};
static bool _upb_mapsorter_resize(_upb_mapsorter* s, _upb_sortedmap* sorted,
int size) {
sorted->start = s->size;
sorted->pos = sorted->start;
sorted->end = sorted->start + size;
if (sorted->end > s->cap) {
const int oldsize = s->cap * sizeof(*s->entries);
s->cap = upb_RoundUpToPowerOfTwo(sorted->end);
const int newsize = s->cap * sizeof(*s->entries);
s->entries = upb_grealloc(s->entries, oldsize, newsize);
if (!s->entries) return false;
}
s->size = sorted->end;
return true;
}
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
const upb_Map* map, _upb_sortedmap* sorted) {
int map_size;
if (map->UPB_PRIVATE(is_strtable)) {
map_size = _upb_Map_Size(map);
} else {
// For inttable, only sort the table entries, since the array part is
// already in a sorted order.
map_size = map->t.inttable.t.count;
}
if (!_upb_mapsorter_resize(s, sorted, map_size)) return false;
// Copy non-empty entries from the table to s->entries.
const void** dst = &s->entries[sorted->start];
const upb_tabent* src;
const upb_tabent* end;
if (map->UPB_PRIVATE(is_strtable)) {
src = map->t.strtable.t.entries;
end = src + upb_table_size(&map->t.strtable.t);
} else {
// For inttable, only sort the table entries, since the array part is
// already in a sorted order.
src = map->t.inttable.t.entries;
end = src + upb_table_size(&map->t.inttable.t);
}
for (; src < end; src++) {
if (!upb_tabent_isempty(src)) {
*dst = src;
dst++;
}
}
UPB_ASSERT(dst == &s->entries[sorted->end]);
// Sort entries according to the key type.
qsort(&s->entries[sorted->start], map_size, sizeof(*s->entries),
map->UPB_PRIVATE(is_strtable) ? compar[key_type]
: _upb_mapsorter_intkeys);
return true;
}
static int _upb_mapsorter_cmpext(const void* _a, const void* _b) {
const upb_Extension* const* a = _a;
const upb_Extension* const* b = _b;
uint32_t a_num = upb_MiniTableExtension_Number((*a)->ext);
uint32_t b_num = upb_MiniTableExtension_Number((*b)->ext);
UPB_ASSERT(a_num != b_num);
return a_num < b_num ? -1 : 1;
}
bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Message_Internal* in,
_upb_sortedmap* sorted) {
size_t count = 0;
for (size_t i = 0; i < in->size; i++) {
count += upb_TaggedAuxPtr_IsExtension(in->aux_data[i]);
}
if (!_upb_mapsorter_resize(s, sorted, count)) return false;
if (count == 0) return true;
const upb_Extension** entry =
(const upb_Extension**)&s->entries[sorted->start];
for (size_t i = 0; i < in->size; i++) {
upb_TaggedAuxPtr tagged_ptr = in->aux_data[i];
if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) {
*entry++ = upb_TaggedAuxPtr_Extension(tagged_ptr);
}
}
qsort(&s->entries[sorted->start], count, sizeof(*s->entries),
_upb_mapsorter_cmpext);
return true;
}
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* a) {
return _upb_Message_New(m, a);
}
UPB_NOINLINE bool UPB_PRIVATE(_upb_Message_AddUnknownSlowPath)(upb_Message* msg,
const char* data,
size_t len,
upb_Arena* arena,
bool alias) {
{
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
// Alias fast path was already checked in the inline function that calls
// this one
if (!alias && in && in->size) {
upb_TaggedAuxPtr ptr = in->aux_data[in->size - 1];
if (upb_TaggedAuxPtr_IsUnknown(ptr)) {
upb_StringView* existing = upb_TaggedAuxPtr_UnknownData(ptr);
if (!upb_TaggedAuxPtr_IsUnknownAliased(ptr)) {
// If part of the existing field was deleted at the beginning, we can
// reconstruct it by comparing the address of the end with the address
// of the entry itself; having the non-aliased tag means that the
// string_view and the data it points to are part of the same original
// upb_Arena_Malloc allocation, and the end of the string view
// represents the end of that allocation.
size_t prev_alloc_size =
(existing->data + existing->size) - (char*)existing;
if (SIZE_MAX - prev_alloc_size >= len) {
size_t new_alloc_size = prev_alloc_size + len;
if (upb_Arena_TryExtend(arena, existing, prev_alloc_size,
new_alloc_size)) {
memcpy(UPB_PTR_AT(existing, prev_alloc_size, void), data, len);
existing->size += len;
return true;
}
}
}
}
}
}
// TODO: b/376969853 - Add debug check that the unknown field is an overall
// valid proto field
if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, arena)) {
return false;
}
upb_StringView* view;
if (alias) {
view = upb_Arena_Malloc(arena, sizeof(upb_StringView));
if (!view) return false;
view->data = data;
} else {
view = upb_Arena_Malloc(arena, sizeof(upb_StringView) + len);
if (!view) return false;
char* copy = UPB_PTR_AT(view, sizeof(upb_StringView), char);
memcpy(copy, data, len);
view->data = copy;
}
view->size = len;
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
in->aux_data[in->size++] = alias
? upb_TaggedAuxPtr_MakeUnknownDataAliased(view)
: upb_TaggedAuxPtr_MakeUnknownData(view);
return true;
}
bool UPB_PRIVATE(_upb_Message_AddUnknownV)(struct upb_Message* msg,
upb_Arena* arena,
upb_StringView data[],
size_t count) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSERT(count > 0);
size_t total_len = 0;
for (size_t i = 0; i < count; i++) {
if (SIZE_MAX - total_len < data[i].size) {
return false;
}
total_len += data[i].size;
}
{
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (in && in->size) {
upb_TaggedAuxPtr ptr = in->aux_data[in->size - 1];
if (upb_TaggedAuxPtr_IsUnknown(ptr)) {
upb_StringView* existing = upb_TaggedAuxPtr_UnknownData(ptr);
if (!upb_TaggedAuxPtr_IsUnknownAliased(ptr)) {
size_t prev_alloc_size =
(existing->data + existing->size) - (char*)existing;
if (SIZE_MAX - prev_alloc_size >= total_len) {
size_t new_alloc_size = prev_alloc_size + total_len;
if (upb_Arena_TryExtend(arena, existing, prev_alloc_size,
new_alloc_size)) {
char* copy = UPB_PTR_AT(existing, prev_alloc_size, char);
for (size_t i = 0; i < count; i++) {
memcpy(copy, data[i].data, data[i].size);
copy += data[i].size;
}
existing->size += total_len;
return true;
}
}
}
}
}
}
if (SIZE_MAX - sizeof(upb_StringView) < total_len) return false;
if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, arena)) return false;
upb_StringView* view =
upb_Arena_Malloc(arena, sizeof(upb_StringView) + total_len);
if (!view) return false;
char* copy = UPB_PTR_AT(view, sizeof(upb_StringView), char);
view->data = copy;
view->size = total_len;
for (size_t i = 0; i < count; i++) {
memcpy(copy, data[i].data, data[i].size);
copy += data[i].size;
}
// TODO: b/376969853 - Add debug check that the unknown field is an overall
// valid proto field
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
in->aux_data[in->size++] = upb_TaggedAuxPtr_MakeUnknownData(view);
return true;
}
void _upb_Message_DiscardUnknown_shallow(upb_Message* msg) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) return;
uint32_t size = 0;
for (uint32_t i = 0; i < in->size; i++) {
upb_TaggedAuxPtr tagged_ptr = in->aux_data[i];
if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) {
in->aux_data[size++] = tagged_ptr;
}
}
in->size = size;
}
upb_Message_DeleteUnknownStatus upb_Message_DeleteUnknown(upb_Message* msg,
upb_StringView* data,
uintptr_t* iter,
upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSERT(*iter != kUpb_Message_UnknownBegin);
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
UPB_ASSERT(in);
UPB_ASSERT(*iter <= in->size);
upb_TaggedAuxPtr unknown_ptr = in->aux_data[*iter - 1];
UPB_ASSERT(upb_TaggedAuxPtr_IsUnknown(unknown_ptr));
upb_StringView* unknown = upb_TaggedAuxPtr_UnknownData(unknown_ptr);
if (unknown->data == data->data && unknown->size == data->size) {
// Remove whole field
in->aux_data[*iter - 1] = upb_TaggedAuxPtr_Null();
} else if (unknown->data == data->data) {
// Strip prefix
unknown->data += data->size;
unknown->size -= data->size;
*data = *unknown;
return kUpb_DeleteUnknown_IterUpdated;
} else if (unknown->data + unknown->size == data->data + data->size) {
// Truncate existing field
unknown->size -= data->size;
if (!upb_TaggedAuxPtr_IsUnknownAliased(unknown_ptr)) {
in->aux_data[*iter - 1] =
upb_TaggedAuxPtr_MakeUnknownDataAliased(unknown);
}
} else {
UPB_ASSERT(unknown->data < data->data &&
unknown->data + unknown->size > data->data + data->size);
// Split in the middle
upb_StringView* prefix = unknown;
upb_StringView* suffix = upb_Arena_Malloc(arena, sizeof(upb_StringView));
if (!suffix) {
return kUpb_DeleteUnknown_AllocFail;
}
if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, arena)) {
return kUpb_DeleteUnknown_AllocFail;
}
in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (*iter != in->size) {
// Shift later entries down so that unknown field ordering is preserved
memmove(&in->aux_data[*iter + 1], &in->aux_data[*iter],
sizeof(upb_TaggedAuxPtr) * (in->size - *iter));
}
in->aux_data[*iter] = upb_TaggedAuxPtr_MakeUnknownDataAliased(suffix);
if (!upb_TaggedAuxPtr_IsUnknownAliased(unknown_ptr)) {
in->aux_data[*iter - 1] = upb_TaggedAuxPtr_MakeUnknownDataAliased(prefix);
}
in->size++;
suffix->data = data->data + data->size;
suffix->size = (prefix->data + prefix->size) - suffix->data;
prefix->size = data->data - prefix->data;
}
return upb_Message_NextUnknown(msg, data, iter)
? kUpb_DeleteUnknown_IterUpdated
: kUpb_DeleteUnknown_DeletedLast;
}
size_t upb_Message_ExtensionCount(const upb_Message* msg) {
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) return 0;
const upb_MiniTableExtension* ext;
upb_MessageValue val;
uintptr_t iter = kUpb_Message_ExtensionBegin;
size_t count = 0;
while (upb_Message_NextExtension(msg, &ext, &val, &iter)) {
count++;
}
return count;
}
void upb_Message_Freeze(upb_Message* msg, const upb_MiniTable* m) {
if (upb_Message_IsFrozen(msg)) return;
UPB_PRIVATE(_upb_Message_ShallowFreeze)(msg);
// Base Fields.
const size_t field_count = upb_MiniTable_FieldCount(m);
for (size_t i = 0; i < field_count; i++) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
const upb_MiniTable* m2 = upb_MiniTable_SubMessage(m, f);
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) {
case kUpb_FieldMode_Array: {
upb_Array* arr = upb_Message_GetMutableArray(msg, f);
if (arr) upb_Array_Freeze(arr, m2);
break;
}
case kUpb_FieldMode_Map: {
upb_Map* map = upb_Message_GetMutableMap(msg, f);
if (map) {
const upb_MiniTableField* f2 = upb_MiniTable_MapValue(m2);
const upb_MiniTable* m3 = upb_MiniTable_SubMessage(m2, f2);
upb_Map_Freeze(map, m3);
}
break;
}
case kUpb_FieldMode_Scalar: {
if (m2) {
upb_Message* msg2 = upb_Message_GetMutableMessage(msg, f);
if (msg2) upb_Message_Freeze(msg2, m2);
}
break;
}
}
}
// Extensions.
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
// TODO: b/376969853 - use iterator API
uint32_t size = in ? in->size : 0;
for (size_t i = 0; i < size; i++) {
upb_TaggedAuxPtr tagged_ptr = in->aux_data[i];
if (!upb_TaggedAuxPtr_IsExtension(tagged_ptr)) {
continue;
}
const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr);
const upb_MiniTableExtension* e = ext->ext;
const upb_MiniTableField* f = &e->UPB_PRIVATE(field);
const upb_MiniTable* m2 = upb_MiniTableExtension_GetSubMessage(e);
upb_MessageValue val;
memcpy(&val, &(ext->data), sizeof(upb_MessageValue));
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) {
case kUpb_FieldMode_Array: {
upb_Array* arr = (upb_Array*)val.array_val;
if (arr) upb_Array_Freeze(arr, m2);
break;
}
case kUpb_FieldMode_Map:
UPB_UNREACHABLE(); // Maps cannot be extensions.
break;
case kUpb_FieldMode_Scalar:
if (upb_MiniTableField_IsSubMessage(f)) {
upb_Message* msg2 = (upb_Message*)val.msg_val;
if (msg2) upb_Message_Freeze(msg2, m2);
}
break;
}
}
}
#include <stddef.h>
#include <stdint.h>
// Must be last.
#ifdef __cplusplus
extern "C" {
#endif
bool upb_Message_IsEmpty(const upb_Message* msg, const upb_MiniTable* m) {
if (upb_Message_ExtensionCount(msg)) return false;
const upb_MiniTableField* f;
upb_MessageValue v;
size_t iter = kUpb_BaseField_Begin;
return !UPB_PRIVATE(_upb_Message_NextBaseField)(msg, m, &f, &v, &iter);
}
static bool _upb_Array_IsEqual(const upb_Array* arr1, const upb_Array* arr2,
upb_CType ctype, const upb_MiniTable* m,
int options) {
// Check for trivial equality.
if (arr1 == arr2) return true;
// Must have identical element counts.
const size_t size1 = arr1 ? upb_Array_Size(arr1) : 0;
const size_t size2 = arr2 ? upb_Array_Size(arr2) : 0;
if (size1 != size2) return false;
for (size_t i = 0; i < size1; i++) {
const upb_MessageValue val1 = upb_Array_Get(arr1, i);
const upb_MessageValue val2 = upb_Array_Get(arr2, i);
if (!upb_MessageValue_IsEqual(val1, val2, ctype, m, options)) return false;
}
return true;
}
static bool _upb_Map_IsEqual(const upb_Map* map1, const upb_Map* map2,
const upb_MiniTable* m, int options) {
// Check for trivial equality.
if (map1 == map2) return true;
// Must have identical element counts.
size_t size1 = map1 ? upb_Map_Size(map1) : 0;
size_t size2 = map2 ? upb_Map_Size(map2) : 0;
if (size1 != size2) return false;
const upb_MiniTableField* f = upb_MiniTable_MapValue(m);
const upb_MiniTable* m2_value = upb_MiniTable_SubMessage(m, f);
const upb_CType ctype = upb_MiniTableField_CType(f);
upb_MessageValue key, val1, val2;
size_t iter = kUpb_Map_Begin;
while (upb_Map_Next(map1, &key, &val1, &iter)) {
if (!upb_Map_Get(map2, key, &val2)) return false;
if (!upb_MessageValue_IsEqual(val1, val2, ctype, m2_value, options))
return false;
}
return true;
}
static bool _upb_Message_BaseFieldsAreEqual(const upb_Message* msg1,
const upb_Message* msg2,
const upb_MiniTable* m,
int options) {
// Iterate over all base fields for each message.
// The order will always match if the messages are equal.
size_t iter1 = kUpb_BaseField_Begin;
size_t iter2 = kUpb_BaseField_Begin;
for (;;) {
const upb_MiniTableField *f1, *f2;
upb_MessageValue val1, val2;
const bool got1 =
UPB_PRIVATE(_upb_Message_NextBaseField)(msg1, m, &f1, &val1, &iter1);
const bool got2 =
UPB_PRIVATE(_upb_Message_NextBaseField)(msg2, m, &f2, &val2, &iter2);
if (got1 != got2) return false; // Must have identical field counts.
if (!got1) return true; // Loop termination condition.
if (f1 != f2) return false; // Must have identical fields set.
const upb_MiniTable* subm = upb_MiniTable_SubMessage(m, f1);
const upb_CType ctype = upb_MiniTableField_CType(f1);
bool eq;
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f1)) {
case kUpb_FieldMode_Array:
eq = _upb_Array_IsEqual(val1.array_val, val2.array_val, ctype, subm,
options);
break;
case kUpb_FieldMode_Map:
eq = _upb_Map_IsEqual(val1.map_val, val2.map_val, subm, options);
break;
case kUpb_FieldMode_Scalar:
eq = upb_MessageValue_IsEqual(val1, val2, ctype, subm, options);
break;
}
if (!eq) return false;
}
}
static bool _upb_Message_ExtensionsAreEqual(const upb_Message* msg1,
const upb_Message* msg2,
const upb_MiniTable* m,
int options) {
const upb_MiniTableExtension* e;
upb_MessageValue val1;
// Iterate over all extensions for msg1, and search msg2 for each extension.
size_t count1 = 0;
size_t iter1 = kUpb_Message_ExtensionBegin;
while (upb_Message_NextExtension(msg1, &e, &val1, &iter1)) {
const upb_Extension* ext2 = UPB_PRIVATE(_upb_Message_Getext)(msg2, e);
if (!ext2) return false;
count1++;
const upb_MessageValue val2 = ext2->data;
const upb_MiniTableField* f = &e->UPB_PRIVATE(field);
const upb_MiniTable* subm = upb_MiniTableField_IsSubMessage(f)
? upb_MiniTableExtension_GetSubMessage(e)
: NULL;
const upb_CType ctype = upb_MiniTableField_CType(f);
bool eq;
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(f)) {
case kUpb_FieldMode_Array:
eq = _upb_Array_IsEqual(val1.array_val, val2.array_val, ctype, subm,
options);
break;
case kUpb_FieldMode_Map:
UPB_UNREACHABLE(); // Maps cannot be extensions.
break;
case kUpb_FieldMode_Scalar: {
eq = upb_MessageValue_IsEqual(val1, val2, ctype, subm, options);
break;
}
}
if (!eq) return false;
}
// Must have identical extension counts (this catches the case where msg2
// has extensions that msg1 doesn't).
if (count1 != upb_Message_ExtensionCount(msg2)) return false;
return true;
}
bool upb_Message_IsEqual(const upb_Message* msg1, const upb_Message* msg2,
const upb_MiniTable* m, int options) {
if (UPB_UNLIKELY(msg1 == msg2)) return true;
if (!_upb_Message_BaseFieldsAreEqual(msg1, msg2, m, options)) return false;
if (!_upb_Message_ExtensionsAreEqual(msg1, msg2, m, options)) return false;
if (!(options & kUpb_CompareOption_IncludeUnknownFields)) return true;
// The wire encoder enforces a maximum depth of 100 so we match that here.
return UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)(msg1, msg2, 100) ==
kUpb_UnknownCompareResult_Equal;
}
#include <stdint.h>
#include <stdlib.h>
// Must be last.
typedef struct upb_UnknownFields upb_UnknownFields;
typedef struct {
uint32_t tag;
union {
uint64_t varint;
uint64_t uint64;
uint32_t uint32;
upb_StringView delimited;
upb_UnknownFields* group;
} data;
} upb_UnknownField;
struct upb_UnknownFields {
size_t size;
size_t capacity;
upb_UnknownField* fields;
};
typedef struct {
upb_EpsCopyInputStream stream;
upb_Arena* arena;
upb_UnknownField* tmp;
size_t tmp_size;
int depth;
upb_UnknownCompareResult status;
jmp_buf err;
} upb_UnknownField_Context;
typedef struct {
upb_UnknownField* arr_base;
upb_UnknownField* arr_ptr;
upb_UnknownField* arr_end;
uint32_t last_tag;
bool sorted;
} upb_UnknownFields_Builder;
UPB_NORETURN static void upb_UnknownFields_OutOfMemory(
upb_UnknownField_Context* ctx) {
ctx->status = kUpb_UnknownCompareResult_OutOfMemory;
UPB_LONGJMP(ctx->err, 1);
}
static void upb_UnknownFields_Grow(upb_UnknownField_Context* ctx,
upb_UnknownField** base,
upb_UnknownField** ptr,
upb_UnknownField** end) {
size_t old = (*ptr - *base);
size_t new = UPB_MAX(4, old * 2);
*base = upb_Arena_Realloc(ctx->arena, *base, old * sizeof(**base),
new * sizeof(**base));
if (!*base) upb_UnknownFields_OutOfMemory(ctx);
*ptr = *base + old;
*end = *base + new;
}
// We have to implement our own sort here, since qsort() is not an in-order
// sort. Here we use merge sort, the simplest in-order sort.
static void upb_UnknownFields_Merge(upb_UnknownField* arr, size_t start,
size_t mid, size_t end,
upb_UnknownField* tmp) {
memcpy(tmp, &arr[start], (end - start) * sizeof(*tmp));
upb_UnknownField* ptr1 = tmp;
upb_UnknownField* end1 = &tmp[mid - start];
upb_UnknownField* ptr2 = &tmp[mid - start];
upb_UnknownField* end2 = &tmp[end - start];
upb_UnknownField* out = &arr[start];
while (ptr1 < end1 && ptr2 < end2) {
if (ptr1->tag <= ptr2->tag) {
*out++ = *ptr1++;
} else {
*out++ = *ptr2++;
}
}
if (ptr1 < end1) {
memcpy(out, ptr1, (end1 - ptr1) * sizeof(*out));
} else if (ptr2 < end2) {
memcpy(out, ptr1, (end2 - ptr2) * sizeof(*out));
}
}
static void upb_UnknownFields_SortRecursive(upb_UnknownField* arr, size_t start,
size_t end, upb_UnknownField* tmp) {
if (end - start > 1) {
size_t mid = start + ((end - start) / 2);
upb_UnknownFields_SortRecursive(arr, start, mid, tmp);
upb_UnknownFields_SortRecursive(arr, mid, end, tmp);
upb_UnknownFields_Merge(arr, start, mid, end, tmp);
}
}
static void upb_UnknownFields_Sort(upb_UnknownField_Context* ctx,
upb_UnknownFields* fields) {
if (ctx->tmp_size < fields->size) {
const int oldsize = ctx->tmp_size * sizeof(*ctx->tmp);
ctx->tmp_size = UPB_MAX(8, ctx->tmp_size);
while (ctx->tmp_size < fields->size) ctx->tmp_size *= 2;
const int newsize = ctx->tmp_size * sizeof(*ctx->tmp);
ctx->tmp = upb_grealloc(ctx->tmp, oldsize, newsize);
}
upb_UnknownFields_SortRecursive(fields->fields, 0, fields->size, ctx->tmp);
}
static upb_UnknownFields* upb_UnknownFields_BuildFromBuffer(
upb_UnknownField_Context* ctx, const char** buf);
// Combines two unknown fields into one.
static void upb_CombineUnknownFields(upb_UnknownField_Context* ctx,
upb_UnknownFields_Builder* builder,
const char** buf) {
upb_UnknownField* arr_base = builder->arr_base;
upb_UnknownField* arr_ptr = builder->arr_ptr;
upb_UnknownField* arr_end = builder->arr_end;
const char* ptr = *buf;
uint32_t last_tag = builder->last_tag;
bool sorted = builder->sorted;
while (!upb_EpsCopyInputStream_IsDone(&ctx->stream, &ptr)) {
uint32_t tag;
ptr = upb_WireReader_ReadTag(ptr, &tag);
UPB_ASSERT(tag <= UINT32_MAX);
int wire_type = upb_WireReader_GetWireType(tag);
if (wire_type == kUpb_WireType_EndGroup) break;
if (tag < last_tag) sorted = false;
last_tag = tag;
if (arr_ptr == arr_end) {
upb_UnknownFields_Grow(ctx, &arr_base, &arr_ptr, &arr_end);
}
upb_UnknownField* field = arr_ptr;
field->tag = tag;
arr_ptr++;
switch (wire_type) {
case kUpb_WireType_Varint:
ptr = upb_WireReader_ReadVarint(ptr, &field->data.varint);
break;
case kUpb_WireType_64Bit:
ptr = upb_WireReader_ReadFixed64(ptr, &field->data.uint64);
break;
case kUpb_WireType_32Bit:
ptr = upb_WireReader_ReadFixed32(ptr, &field->data.uint32);
break;
case kUpb_WireType_Delimited: {
int size;
ptr = upb_WireReader_ReadSize(ptr, &size);
const char* s_ptr = ptr;
ptr = upb_EpsCopyInputStream_ReadStringAliased(&ctx->stream, &s_ptr,
size);
field->data.delimited.data = s_ptr;
field->data.delimited.size = size;
break;
}
case kUpb_WireType_StartGroup:
if (--ctx->depth == 0) {
ctx->status = kUpb_UnknownCompareResult_MaxDepthExceeded;
UPB_LONGJMP(ctx->err, 1);
}
field->data.group = upb_UnknownFields_BuildFromBuffer(ctx, &ptr);
ctx->depth++;
break;
default:
UPB_UNREACHABLE();
}
}
*buf = ptr;
builder->arr_base = arr_base;
builder->arr_ptr = arr_ptr;
builder->arr_end = arr_end;
builder->sorted = sorted;
builder->last_tag = last_tag;
}
static upb_UnknownFields* upb_UnknownFields_DoBuild(
upb_UnknownField_Context* ctx, upb_UnknownFields_Builder* builder) {
upb_UnknownFields* ret = upb_Arena_Malloc(ctx->arena, sizeof(*ret));
if (!ret) upb_UnknownFields_OutOfMemory(ctx);
ret->fields = builder->arr_base;
ret->size = builder->arr_ptr - builder->arr_base;
ret->capacity = builder->arr_end - builder->arr_base;
if (!builder->sorted) {
upb_UnknownFields_Sort(ctx, ret);
}
return ret;
}
// Builds a upb_UnknownFields data structure from the binary data in buf.
static upb_UnknownFields* upb_UnknownFields_BuildFromBuffer(
upb_UnknownField_Context* ctx, const char** buf) {
upb_UnknownFields_Builder builder = {
.arr_base = NULL,
.arr_ptr = NULL,
.arr_end = NULL,
.sorted = true,
.last_tag = 0,
};
const char* ptr = *buf;
upb_CombineUnknownFields(ctx, &builder, &ptr);
upb_UnknownFields* fields = upb_UnknownFields_DoBuild(ctx, &builder);
*buf = ptr;
return fields;
}
// Builds a upb_UnknownFields data structure from the unknown fields of a
// upb_Message.
static upb_UnknownFields* upb_UnknownFields_Build(upb_UnknownField_Context* ctx,
const upb_Message* msg) {
upb_UnknownFields_Builder builder = {
.arr_base = NULL,
.arr_ptr = NULL,
.arr_end = NULL,
.sorted = true,
.last_tag = 0,
};
uintptr_t iter = kUpb_Message_UnknownBegin;
upb_StringView view;
while (upb_Message_NextUnknown(msg, &view, &iter)) {
upb_EpsCopyInputStream_Init(&ctx->stream, &view.data, view.size, true);
upb_CombineUnknownFields(ctx, &builder, &view.data);
UPB_ASSERT(upb_EpsCopyInputStream_IsDone(&ctx->stream, &view.data) &&
!upb_EpsCopyInputStream_IsError(&ctx->stream));
}
upb_UnknownFields* fields = upb_UnknownFields_DoBuild(ctx, &builder);
return fields;
}
// Compares two sorted upb_UnknownFields structures for equality.
static bool upb_UnknownFields_IsEqual(const upb_UnknownFields* uf1,
const upb_UnknownFields* uf2) {
if (uf1->size != uf2->size) return false;
for (size_t i = 0, n = uf1->size; i < n; i++) {
upb_UnknownField* f1 = &uf1->fields[i];
upb_UnknownField* f2 = &uf2->fields[i];
if (f1->tag != f2->tag) return false;
int wire_type = f1->tag & 7;
switch (wire_type) {
case kUpb_WireType_Varint:
if (f1->data.varint != f2->data.varint) return false;
break;
case kUpb_WireType_64Bit:
if (f1->data.uint64 != f2->data.uint64) return false;
break;
case kUpb_WireType_32Bit:
if (f1->data.uint32 != f2->data.uint32) return false;
break;
case kUpb_WireType_Delimited:
if (!upb_StringView_IsEqual(f1->data.delimited, f2->data.delimited)) {
return false;
}
break;
case kUpb_WireType_StartGroup:
if (!upb_UnknownFields_IsEqual(f1->data.group, f2->data.group)) {
return false;
}
break;
default:
UPB_UNREACHABLE();
}
}
return true;
}
static upb_UnknownCompareResult upb_UnknownField_DoCompare(
upb_UnknownField_Context* ctx, const upb_Message* msg1,
const upb_Message* msg2) {
upb_UnknownCompareResult ret;
// First build both unknown fields into a sorted data structure (similar
// to the UnknownFieldSet in C++).
upb_UnknownFields* uf1 = upb_UnknownFields_Build(ctx, msg1);
upb_UnknownFields* uf2 = upb_UnknownFields_Build(ctx, msg2);
// Now perform the equality check on the sorted structures.
if (upb_UnknownFields_IsEqual(uf1, uf2)) {
ret = kUpb_UnknownCompareResult_Equal;
} else {
ret = kUpb_UnknownCompareResult_NotEqual;
}
return ret;
}
static upb_UnknownCompareResult upb_UnknownField_Compare(
upb_UnknownField_Context* const ctx, const upb_Message* msg1,
const upb_Message* msg2) {
upb_UnknownCompareResult ret;
if (UPB_SETJMP(ctx->err) == 0) {
ret = upb_UnknownField_DoCompare(ctx, msg1, msg2);
} else {
ret = ctx->status;
UPB_ASSERT(ret != kUpb_UnknownCompareResult_Equal);
}
upb_Arena_Free(ctx->arena);
upb_gfree(ctx->tmp);
return ret;
}
upb_UnknownCompareResult UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)(
const upb_Message* msg1, const upb_Message* msg2, int max_depth) {
bool msg1_empty = !upb_Message_HasUnknown(msg1);
bool msg2_empty = !upb_Message_HasUnknown(msg2);
if (msg1_empty && msg2_empty) return kUpb_UnknownCompareResult_Equal;
if (msg1_empty || msg2_empty) return kUpb_UnknownCompareResult_NotEqual;
upb_UnknownField_Context ctx = {
.arena = upb_Arena_New(),
.depth = max_depth,
.tmp = NULL,
.tmp_size = 0,
.status = kUpb_UnknownCompareResult_Equal,
};
if (!ctx.arena) return kUpb_UnknownCompareResult_OutOfMemory;
return upb_UnknownField_Compare(&ctx, msg1, msg2);
}
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
// Must be last.
static upb_StringView upb_Clone_StringView(upb_StringView str,
upb_Arena* arena) {
if (str.size == 0) {
return upb_StringView_FromDataAndSize(NULL, 0);
}
void* cloned_data = upb_Arena_Malloc(arena, str.size);
upb_StringView cloned_str =
upb_StringView_FromDataAndSize(cloned_data, str.size);
memcpy(cloned_data, str.data, str.size);
return cloned_str;
}
static bool upb_Clone_MessageValue(void* value, upb_CType value_type,
const upb_MiniTable* sub, upb_Arena* arena) {
switch (value_type) {
case kUpb_CType_Bool:
case kUpb_CType_Float:
case kUpb_CType_Int32:
case kUpb_CType_UInt32:
case kUpb_CType_Enum:
case kUpb_CType_Double:
case kUpb_CType_Int64:
case kUpb_CType_UInt64:
return true;
case kUpb_CType_String:
case kUpb_CType_Bytes: {
upb_StringView source = *(upb_StringView*)value;
int size = source.size;
void* cloned_data = upb_Arena_Malloc(arena, size);
if (cloned_data == NULL) {
return false;
}
*(upb_StringView*)value =
upb_StringView_FromDataAndSize(cloned_data, size);
memcpy(cloned_data, source.data, size);
return true;
} break;
case kUpb_CType_Message: {
const upb_TaggedMessagePtr source = *(upb_TaggedMessagePtr*)value;
bool is_empty = upb_TaggedMessagePtr_IsEmpty(source);
if (is_empty) sub = UPB_PRIVATE(_upb_MiniTable_Empty)();
UPB_ASSERT(source);
upb_Message* clone = upb_Message_DeepClone(
UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(source), sub, arena);
*(upb_TaggedMessagePtr*)value =
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(clone, is_empty);
return clone != NULL;
} break;
}
UPB_UNREACHABLE();
}
upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type,
upb_CType value_type,
const upb_MiniTable* map_entry_table,
upb_Arena* arena) {
upb_Map* cloned_map = _upb_Map_New(arena, map->key_size, map->val_size);
if (cloned_map == NULL) {
return NULL;
}
upb_MessageValue key, val;
size_t iter = kUpb_Map_Begin;
while (upb_Map_Next(map, &key, &val, &iter)) {
const upb_MiniTableField* value_field =
upb_MiniTable_MapValue(map_entry_table);
const upb_MiniTable* value_sub =
upb_MiniTableField_CType(value_field) == kUpb_CType_Message
? upb_MiniTable_GetSubMessageTable(map_entry_table, value_field)
: NULL;
upb_CType value_field_type = upb_MiniTableField_CType(value_field);
if (!upb_Clone_MessageValue(&val, value_field_type, value_sub, arena)) {
return NULL;
}
if (!upb_Map_Set(cloned_map, key, val, arena)) {
return NULL;
}
}
return cloned_map;
}
static upb_Map* upb_Message_Map_DeepClone(const upb_Map* map,
const upb_MiniTable* mini_table,
const upb_MiniTableField* f,
upb_Message* clone,
upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(clone));
const upb_MiniTable* map_entry_table =
upb_MiniTable_MapEntrySubMessage(mini_table, f);
UPB_ASSERT(map_entry_table);
const upb_MiniTableField* key_field = upb_MiniTable_MapKey(map_entry_table);
const upb_MiniTableField* value_field =
upb_MiniTable_MapValue(map_entry_table);
upb_Map* cloned_map = upb_Map_DeepClone(
map, upb_MiniTableField_CType(key_field),
upb_MiniTableField_CType(value_field), map_entry_table, arena);
if (!cloned_map) {
return NULL;
}
upb_Message_SetBaseField(clone, f, &cloned_map);
return cloned_map;
}
upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type,
const upb_MiniTable* sub, upb_Arena* arena) {
const size_t size = upb_Array_Size(array);
const int lg2 = UPB_PRIVATE(_upb_CType_SizeLg2)(value_type);
upb_Array* cloned_array = UPB_PRIVATE(_upb_Array_New)(arena, size, lg2);
if (!cloned_array) {
return NULL;
}
if (!UPB_PRIVATE(_upb_Array_ResizeUninitialized)(cloned_array, size, arena)) {
return NULL;
}
for (size_t i = 0; i < size; ++i) {
upb_MessageValue val = upb_Array_Get(array, i);
if (!upb_Clone_MessageValue(&val, value_type, sub, arena)) {
return NULL;
}
upb_Array_Set(cloned_array, i, val);
}
return cloned_array;
}
static bool upb_Message_Array_DeepClone(const upb_Array* array,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* clone, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(clone));
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(field);
upb_Array* cloned_array = upb_Array_DeepClone(
array, upb_MiniTableField_CType(field),
upb_MiniTableField_CType(field) == kUpb_CType_Message
? upb_MiniTable_GetSubMessageTable(mini_table, field)
: NULL,
arena);
// Clear out upb_Array* due to parent memcpy.
upb_Message_SetBaseField(clone, field, &cloned_array);
return true;
}
static bool upb_Clone_ExtensionValue(
const upb_MiniTableExtension* mini_table_ext, const upb_Extension* source,
upb_Extension* dest, upb_Arena* arena) {
dest->data = source->data;
return upb_Clone_MessageValue(
&dest->data, upb_MiniTableExtension_CType(mini_table_ext),
upb_MiniTableExtension_GetSubMessage(mini_table_ext), arena);
}
upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* mini_table,
upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(dst));
upb_StringView empty_string = upb_StringView_FromDataAndSize(NULL, 0);
// Only copy message area skipping upb_Message_Internal.
memcpy(dst + 1, src + 1, mini_table->UPB_PRIVATE(size) - sizeof(upb_Message));
for (int i = 0; i < upb_MiniTable_FieldCount(mini_table); ++i) {
const upb_MiniTableField* field =
upb_MiniTable_GetFieldByIndex(mini_table, i);
if (upb_MiniTableField_IsScalar(field)) {
switch (upb_MiniTableField_CType(field)) {
case kUpb_CType_Message: {
upb_TaggedMessagePtr tagged =
upb_Message_GetTaggedMessagePtr(src, field, NULL);
const upb_Message* sub_message =
UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(tagged);
if (sub_message != NULL) {
// If the message is currently in an unlinked, "empty" state we keep
// it that way, because we don't want to deal with decode options,
// decode status, or possible parse failure here.
bool is_empty = upb_TaggedMessagePtr_IsEmpty(tagged);
const upb_MiniTable* sub_message_table =
is_empty ? UPB_PRIVATE(_upb_MiniTable_Empty)()
: upb_MiniTable_GetSubMessageTable(mini_table, field);
upb_Message* dst_sub_message =
upb_Message_DeepClone(sub_message, sub_message_table, arena);
if (dst_sub_message == NULL) {
return NULL;
}
UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)
(dst, field,
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(dst_sub_message,
is_empty));
}
} break;
case kUpb_CType_String:
case kUpb_CType_Bytes: {
upb_StringView str = upb_Message_GetString(src, field, empty_string);
if (str.size != 0) {
if (!upb_Message_SetString(
dst, field, upb_Clone_StringView(str, arena), arena)) {
return NULL;
}
}
} break;
default:
// Scalar, already copied.
break;
}
} else {
if (upb_MiniTableField_IsMap(field)) {
const upb_Map* map = upb_Message_GetMap(src, field);
if (map != NULL) {
if (!upb_Message_Map_DeepClone(map, mini_table, field, dst, arena)) {
return NULL;
}
}
} else {
const upb_Array* array = upb_Message_GetArray(src, field);
if (array != NULL) {
if (!upb_Message_Array_DeepClone(array, mini_table, field, dst,
arena)) {
return NULL;
}
}
}
}
}
// Clone extensions.
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(src);
if (!in) return dst;
for (size_t i = 0; i < in->size; i++) {
upb_TaggedAuxPtr tagged_ptr = in->aux_data[i];
if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) {
// Clone extension
const upb_Extension* msg_ext = upb_TaggedAuxPtr_Extension(tagged_ptr);
const upb_MiniTableField* field = &msg_ext->ext->UPB_PRIVATE(field);
upb_Extension* dst_ext = UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
dst, msg_ext->ext, arena);
if (!dst_ext) return NULL;
if (upb_MiniTableField_IsScalar(field)) {
if (!upb_Clone_ExtensionValue(msg_ext->ext, msg_ext, dst_ext, arena)) {
return NULL;
}
} else {
upb_Array* msg_array = (upb_Array*)msg_ext->data.array_val;
UPB_ASSERT(msg_array);
upb_Array* cloned_array = upb_Array_DeepClone(
msg_array, upb_MiniTableField_CType(field),
upb_MiniTableExtension_GetSubMessage(msg_ext->ext), arena);
if (!cloned_array) {
return NULL;
}
dst_ext->data.array_val = cloned_array;
}
} else if (upb_TaggedAuxPtr_IsUnknown(tagged_ptr)) {
// Clone unknown
upb_StringView* unknown = upb_TaggedAuxPtr_UnknownData(tagged_ptr);
// Make a copy into destination arena.
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(dst, unknown->data,
unknown->size, arena, false)) {
return NULL;
}
}
}
return dst;
}
bool upb_Message_DeepCopy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* mini_table, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(dst));
upb_Message_Clear(dst, mini_table);
return _upb_Message_Copy(dst, src, mini_table, arena) != NULL;
}
// Deep clones a message using the provided target arena.
//
// Returns NULL on failure.
upb_Message* upb_Message_DeepClone(const upb_Message* msg,
const upb_MiniTable* m, upb_Arena* arena) {
upb_Message* clone = upb_Message_New(m, arena);
return _upb_Message_Copy(clone, msg, m, arena);
}
// Performs a shallow copy. TODO: Extend to handle unknown fields.
void upb_Message_ShallowCopy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* m) {
UPB_ASSERT(!upb_Message_IsFrozen(dst));
memcpy(dst, src, m->UPB_PRIVATE(size));
}
// Performs a shallow clone. Ignores unknown fields.
upb_Message* upb_Message_ShallowClone(const upb_Message* msg,
const upb_MiniTable* m,
upb_Arena* arena) {
upb_Message* clone = upb_Message_New(m, arena);
upb_Message_ShallowCopy(clone, msg, m);
return clone;
}
#include "stddef.h"
// Must be last.
bool upb_Message_MergeFrom(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg,
upb_Arena* arena) {
char* buf = NULL;
size_t size = 0;
// This tmp arena is used to hold the bytes for `src` serialized. This bends
// the typical "no hidden allocations" design of upb, but under a properly
// optimized implementation this extra allocation would not be necessary and
// so we don't want to unnecessarily have the bad API or bloat the passed-in
// arena with this very-short-term allocation.
upb_Arena* encode_arena = upb_Arena_New();
upb_EncodeStatus e_status = upb_Encode(src, mt, 0, encode_arena, &buf, &size);
if (e_status != kUpb_EncodeStatus_Ok) {
upb_Arena_Free(encode_arena);
return false;
}
upb_DecodeStatus d_status = upb_Decode(buf, size, dst, mt, extreg, 0, arena);
if (d_status != kUpb_DecodeStatus_Ok) {
upb_Arena_Free(encode_arena);
return false;
}
upb_Arena_Free(encode_arena);
return true;
}
#include <stddef.h>
#include <stdint.h>
// Must be last.
typedef struct {
upb_MdDecoder base;
upb_Arena* arena;
upb_MiniTableEnum* enum_table;
uint32_t enum_value_count;
uint32_t enum_data_count;
uint32_t enum_data_capacity;
} upb_MdEnumDecoder;
static size_t upb_MiniTableEnum_Size(uint32_t count) {
return UPB_SIZEOF_FLEX(upb_MiniTableEnum, UPB_PRIVATE(data), count);
}
static upb_MiniTableEnum* _upb_MiniTable_AddEnumDataMember(upb_MdEnumDecoder* d,
uint32_t val) {
if (d->enum_data_count == d->enum_data_capacity) {
size_t old_sz = upb_MiniTableEnum_Size(d->enum_data_capacity);
if (d->enum_data_capacity > UINT32_MAX / 2) {
upb_MdDecoder_ErrorJmp(&d->base, "Out of memory");
}
uint32_t new_capacity = UPB_MAX(2, d->enum_data_capacity * 2);
if (UPB_SIZEOF_FLEX_WOULD_OVERFLOW(upb_MiniTableEnum, UPB_PRIVATE(data),
new_capacity)) {
upb_MdDecoder_ErrorJmp(&d->base, "Out of memory");
}
size_t new_sz = upb_MiniTableEnum_Size(new_capacity);
d->enum_table = upb_Arena_Realloc(d->arena, d->enum_table, old_sz, new_sz);
upb_MdDecoder_CheckOutOfMemory(&d->base, d->enum_table);
d->enum_data_capacity = new_capacity;
}
d->enum_table->UPB_PRIVATE(data)[d->enum_data_count++] = val;
return d->enum_table;
}
static void upb_MiniTableEnum_BuildValue(upb_MdEnumDecoder* d, uint32_t val) {
upb_MiniTableEnum* table = d->enum_table;
d->enum_value_count++;
if (table->UPB_PRIVATE(value_count) ||
(val > 512 && d->enum_value_count < val / 32)) {
if (table->UPB_PRIVATE(value_count) == 0) {
UPB_ASSERT(d->enum_data_count == table->UPB_PRIVATE(mask_limit) / 32);
}
table = _upb_MiniTable_AddEnumDataMember(d, val);
table->UPB_PRIVATE(value_count)++;
} else {
uint32_t new_mask_limit = ((val / 32) + 1) * 32;
while (table->UPB_PRIVATE(mask_limit) < new_mask_limit) {
table = _upb_MiniTable_AddEnumDataMember(d, 0);
table->UPB_PRIVATE(mask_limit) += 32;
}
table->UPB_PRIVATE(data)[val / 32] |= 1ULL << (val % 32);
}
}
static upb_MiniTableEnum* upb_MtDecoder_DoBuildMiniTableEnum(
upb_MdEnumDecoder* d, const char* data, size_t len) {
// If the string is non-empty then it must begin with a version tag.
if (len) {
if (*data != kUpb_EncodedVersion_EnumV1) {
upb_MdDecoder_ErrorJmp(&d->base, "Invalid enum version: %c", *data);
}
data++;
len--;
}
upb_MdDecoder_CheckOutOfMemory(&d->base, d->enum_table);
// Guarantee at least 64 bits of mask without checking mask size.
d->enum_table->UPB_PRIVATE(mask_limit) = 64;
d->enum_table = _upb_MiniTable_AddEnumDataMember(d, 0);
d->enum_table = _upb_MiniTable_AddEnumDataMember(d, 0);
d->enum_table->UPB_PRIVATE(value_count) = 0;
const char* ptr = data;
uint32_t base = 0;
while (ptr < d->base.end) {
char ch = *ptr++;
if (ch <= kUpb_EncodedValue_MaxEnumMask) {
uint32_t mask = _upb_FromBase92(ch);
for (int i = 0; i < 5; i++, base++, mask >>= 1) {
if (mask & 1) upb_MiniTableEnum_BuildValue(d, base);
}
} else if (kUpb_EncodedValue_MinSkip <= ch &&
ch <= kUpb_EncodedValue_MaxSkip) {
uint32_t skip;
ptr = upb_MdDecoder_DecodeBase92Varint(&d->base, ptr, ch,
kUpb_EncodedValue_MinSkip,
kUpb_EncodedValue_MaxSkip, &skip);
base += skip;
} else {
upb_MdDecoder_ErrorJmp(&d->base, "Unexpected character: %c", ch);
}
}
return d->enum_table;
}
static upb_MiniTableEnum* upb_MtDecoder_BuildMiniTableEnum(
upb_MdEnumDecoder* const decoder, const char* const data,
size_t const len) {
if (UPB_SETJMP(decoder->base.err) != 0) return NULL;
return upb_MtDecoder_DoBuildMiniTableEnum(decoder, data, len);
}
upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data, size_t len,
upb_Arena* arena,
upb_Status* status) {
uint32_t initial_capacity = 2;
upb_MdEnumDecoder decoder = {
.base =
{
.end = UPB_PTRADD(data, len),
.status = status,
},
.arena = arena,
.enum_table =
upb_Arena_Malloc(arena, upb_MiniTableEnum_Size(initial_capacity)),
.enum_value_count = 0,
.enum_data_count = 0,
.enum_data_capacity = initial_capacity,
};
return upb_MtDecoder_BuildMiniTableEnum(&decoder, data, len);
}
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
// Must be last.
// We reserve unused hasbits to make room for upb_Message fields.
#define kUpb_Reserved_Hasbytes sizeof(struct upb_Message)
// 64 is the first hasbit that we currently use.
#define kUpb_Reserved_Hasbits (kUpb_Reserved_Hasbytes * 8)
#define kUpb_OneOfLayoutItem_IndexSentinel ((uint16_t)-1)
// Stores the field number of the present value of the oneof
#define kUpb_OneOf_CaseFieldRep (kUpb_FieldRep_4Byte)
typedef struct {
// Index of the corresponding field. The field's offset will be the index of
// the next field in a linked list.
uint16_t field_index;
// This enum is stored in bytes to avoid trailing padding while preserving
// two-byte alignment.
uint8_t /* upb_FieldRep*/ rep;
} upb_OneOfLayoutItem;
typedef struct {
upb_OneOfLayoutItem* data;
size_t size;
size_t buf_capacity_bytes;
} upb_OneOfLayoutItemVector;
typedef struct {
upb_MdDecoder base;
upb_MiniTable* table;
upb_MiniTableField* fields;
upb_MiniTablePlatform platform;
upb_OneOfLayoutItemVector oneofs;
upb_Arena* arena;
// Initially tracks the count of each field rep type; then, during assignment,
// tracks the base offset for the next processed field of the given rep.
uint16_t rep_counts_offsets[kUpb_FieldRep_Max + 1];
} upb_MtDecoder;
// In each field's offset, we temporarily store a presence classifier:
enum PresenceClass {
kNoPresence = 0,
kHasbitPresence = 1,
kRequiredPresence = 2,
kOneofBase = 3,
// Negative values refer to a specific oneof with that number. Positive
// values >= kOneofBase indicate that this field is in a oneof, and specify
// the next field in this oneof's linked list.
};
static bool upb_MtDecoder_FieldIsPackable(upb_MiniTableField* field) {
return (field->UPB_PRIVATE(mode) & kUpb_FieldMode_Array) &&
upb_FieldType_IsPackable(field->UPB_PRIVATE(descriptortype));
}
typedef struct {
uint16_t submsg_count;
uint16_t subenum_count;
} upb_SubCounts;
static void upb_MiniTable_SetTypeAndSub(upb_MiniTableField* field,
upb_FieldType type,
upb_SubCounts* sub_counts,
uint64_t msg_modifiers,
bool is_proto3_enum) {
if (is_proto3_enum) {
UPB_ASSERT(type == kUpb_FieldType_Enum);
type = kUpb_FieldType_Int32;
field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsAlternate;
} else if (type == kUpb_FieldType_String &&
!(msg_modifiers & kUpb_MessageModifier_ValidateUtf8)) {
type = kUpb_FieldType_Bytes;
field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsAlternate;
}
field->UPB_PRIVATE(descriptortype) = type;
if (upb_MtDecoder_FieldIsPackable(field) &&
(msg_modifiers & kUpb_MessageModifier_DefaultIsPacked)) {
field->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsPacked;
}
if (type == kUpb_FieldType_Message || type == kUpb_FieldType_Group) {
field->UPB_PRIVATE(submsg_index) = sub_counts->submsg_count++;
} else if (type == kUpb_FieldType_Enum) {
// We will need to update this later once we know the total number of
// submsg fields.
field->UPB_PRIVATE(submsg_index) = sub_counts->subenum_count++;
} else {
field->UPB_PRIVATE(submsg_index) = kUpb_NoSub;
}
}
static const char kUpb_EncodedToType[] = {
[kUpb_EncodedType_Double] = kUpb_FieldType_Double,
[kUpb_EncodedType_Float] = kUpb_FieldType_Float,
[kUpb_EncodedType_Int64] = kUpb_FieldType_Int64,
[kUpb_EncodedType_UInt64] = kUpb_FieldType_UInt64,
[kUpb_EncodedType_Int32] = kUpb_FieldType_Int32,
[kUpb_EncodedType_Fixed64] = kUpb_FieldType_Fixed64,
[kUpb_EncodedType_Fixed32] = kUpb_FieldType_Fixed32,
[kUpb_EncodedType_Bool] = kUpb_FieldType_Bool,
[kUpb_EncodedType_String] = kUpb_FieldType_String,
[kUpb_EncodedType_Group] = kUpb_FieldType_Group,
[kUpb_EncodedType_Message] = kUpb_FieldType_Message,
[kUpb_EncodedType_Bytes] = kUpb_FieldType_Bytes,
[kUpb_EncodedType_UInt32] = kUpb_FieldType_UInt32,
[kUpb_EncodedType_OpenEnum] = kUpb_FieldType_Enum,
[kUpb_EncodedType_SFixed32] = kUpb_FieldType_SFixed32,
[kUpb_EncodedType_SFixed64] = kUpb_FieldType_SFixed64,
[kUpb_EncodedType_SInt32] = kUpb_FieldType_SInt32,
[kUpb_EncodedType_SInt64] = kUpb_FieldType_SInt64,
[kUpb_EncodedType_ClosedEnum] = kUpb_FieldType_Enum,
};
static void upb_MiniTable_SetField(upb_MtDecoder* d, uint8_t ch,
upb_MiniTableField* field,
uint64_t msg_modifiers,
upb_SubCounts* sub_counts) {
static const char kUpb_EncodedToFieldRep[] = {
[kUpb_EncodedType_Double] = kUpb_FieldRep_8Byte,
[kUpb_EncodedType_Float] = kUpb_FieldRep_4Byte,
[kUpb_EncodedType_Int64] = kUpb_FieldRep_8Byte,
[kUpb_EncodedType_UInt64] = kUpb_FieldRep_8Byte,
[kUpb_EncodedType_Int32] = kUpb_FieldRep_4Byte,
[kUpb_EncodedType_Fixed64] = kUpb_FieldRep_8Byte,
[kUpb_EncodedType_Fixed32] = kUpb_FieldRep_4Byte,
[kUpb_EncodedType_Bool] = kUpb_FieldRep_1Byte,
[kUpb_EncodedType_String] = kUpb_FieldRep_StringView,
[kUpb_EncodedType_Bytes] = kUpb_FieldRep_StringView,
[kUpb_EncodedType_UInt32] = kUpb_FieldRep_4Byte,
[kUpb_EncodedType_OpenEnum] = kUpb_FieldRep_4Byte,
[kUpb_EncodedType_SFixed32] = kUpb_FieldRep_4Byte,
[kUpb_EncodedType_SFixed64] = kUpb_FieldRep_8Byte,
[kUpb_EncodedType_SInt32] = kUpb_FieldRep_4Byte,
[kUpb_EncodedType_SInt64] = kUpb_FieldRep_8Byte,
[kUpb_EncodedType_ClosedEnum] = kUpb_FieldRep_4Byte,
};
char pointer_rep = d->platform == kUpb_MiniTablePlatform_32Bit
? kUpb_FieldRep_4Byte
: kUpb_FieldRep_8Byte;
int8_t type = _upb_FromBase92(ch);
if (ch >= _upb_ToBase92(kUpb_EncodedType_RepeatedBase)) {
type -= kUpb_EncodedType_RepeatedBase;
field->UPB_PRIVATE(mode) = kUpb_FieldMode_Array;
field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift;
field->UPB_PRIVATE(offset) = kNoPresence;
} else {
field->UPB_PRIVATE(mode) = kUpb_FieldMode_Scalar;
field->UPB_PRIVATE(offset) = kHasbitPresence;
if (type == kUpb_EncodedType_Group || type == kUpb_EncodedType_Message) {
field->UPB_PRIVATE(mode) |= pointer_rep << kUpb_FieldRep_Shift;
} else if ((unsigned long)type >= sizeof(kUpb_EncodedToFieldRep)) {
upb_MdDecoder_ErrorJmp(&d->base, "Invalid field type: %d", (int)type);
} else {
field->UPB_PRIVATE(mode) |= kUpb_EncodedToFieldRep[type]
<< kUpb_FieldRep_Shift;
}
}
if ((unsigned long)type >= sizeof(kUpb_EncodedToType)) {
upb_MdDecoder_ErrorJmp(&d->base, "Invalid field type: %d", (int)type);
}
upb_MiniTable_SetTypeAndSub(field, kUpb_EncodedToType[type], sub_counts,
msg_modifiers, type == kUpb_EncodedType_OpenEnum);
}
static void upb_MtDecoder_ModifyField(upb_MtDecoder* d,
uint32_t message_modifiers,
uint32_t field_modifiers,
upb_MiniTableField* field) {
if (field_modifiers & kUpb_EncodedFieldModifier_FlipPacked) {
if (!upb_MtDecoder_FieldIsPackable(field)) {
upb_MdDecoder_ErrorJmp(&d->base,
"Cannot flip packed on unpackable field %" PRIu32,
upb_MiniTableField_Number(field));
}
field->UPB_PRIVATE(mode) ^= kUpb_LabelFlags_IsPacked;
}
if (field_modifiers & kUpb_EncodedFieldModifier_FlipValidateUtf8) {
if (field->UPB_PRIVATE(descriptortype) != kUpb_FieldType_Bytes ||
!(field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsAlternate)) {
upb_MdDecoder_ErrorJmp(&d->base,
"Cannot flip ValidateUtf8 on field %" PRIu32
", type=%d, mode=%d",
upb_MiniTableField_Number(field),
(int)field->UPB_PRIVATE(descriptortype),
(int)field->UPB_PRIVATE(mode));
}
field->UPB_PRIVATE(descriptortype) = kUpb_FieldType_String;
field->UPB_PRIVATE(mode) &= ~kUpb_LabelFlags_IsAlternate;
}
bool singular = field_modifiers & kUpb_EncodedFieldModifier_IsProto3Singular;
bool required = field_modifiers & kUpb_EncodedFieldModifier_IsRequired;
// Validate.
if ((singular || required) && field->UPB_PRIVATE(offset) != kHasbitPresence) {
upb_MdDecoder_ErrorJmp(&d->base,
"Invalid modifier(s) for repeated field %" PRIu32,
upb_MiniTableField_Number(field));
}
if (singular && required) {
upb_MdDecoder_ErrorJmp(
&d->base, "Field %" PRIu32 " cannot be both singular and required",
upb_MiniTableField_Number(field));
}
if (singular && upb_MiniTableField_IsSubMessage(field)) {
upb_MdDecoder_ErrorJmp(&d->base,
"Field %" PRIu32 " cannot be a singular submessage",
upb_MiniTableField_Number(field));
}
if (singular) field->UPB_PRIVATE(offset) = kNoPresence;
if (required) {
field->UPB_PRIVATE(offset) = kRequiredPresence;
}
}
static void upb_MtDecoder_PushOneof(upb_MtDecoder* d,
upb_OneOfLayoutItem item) {
if (item.field_index == kUpb_OneOfLayoutItem_IndexSentinel) {
upb_MdDecoder_ErrorJmp(&d->base, "Empty oneof");
}
if ((d->oneofs.size + 1) * sizeof(*d->oneofs.data) >
d->oneofs.buf_capacity_bytes) {
size_t new_cap = UPB_MAX(8, d->oneofs.size * 2) * sizeof(*d->oneofs.data);
d->oneofs.data =
upb_grealloc(d->oneofs.data, d->oneofs.buf_capacity_bytes, new_cap);
upb_MdDecoder_CheckOutOfMemory(&d->base, d->oneofs.data);
d->oneofs.buf_capacity_bytes = new_cap;
}
item.field_index -= kOneofBase;
d->rep_counts_offsets[kUpb_OneOf_CaseFieldRep]++;
d->rep_counts_offsets[item.rep]++;
d->oneofs.data[d->oneofs.size++] = item;
}
static size_t upb_MtDecoder_SizeOfRep(upb_FieldRep rep,
upb_MiniTablePlatform platform) {
static const uint8_t kRepToSize32[] = {
[kUpb_FieldRep_1Byte] = 1,
[kUpb_FieldRep_4Byte] = 4,
[kUpb_FieldRep_StringView] = 8,
[kUpb_FieldRep_8Byte] = 8,
};
static const uint8_t kRepToSize64[] = {
[kUpb_FieldRep_1Byte] = 1,
[kUpb_FieldRep_4Byte] = 4,
[kUpb_FieldRep_StringView] = 16,
[kUpb_FieldRep_8Byte] = 8,
};
UPB_ASSERT(sizeof(upb_StringView) ==
UPB_SIZE(kRepToSize32, kRepToSize64)[kUpb_FieldRep_StringView]);
return platform == kUpb_MiniTablePlatform_32Bit ? kRepToSize32[rep]
: kRepToSize64[rep];
}
static size_t upb_MtDecoder_AlignOfRep(upb_FieldRep rep,
upb_MiniTablePlatform platform) {
static const uint8_t kRepToAlign32[] = {
[kUpb_FieldRep_1Byte] = 1,
[kUpb_FieldRep_4Byte] = 4,
[kUpb_FieldRep_StringView] = 4,
[kUpb_FieldRep_8Byte] = 8,
};
static const uint8_t kRepToAlign64[] = {
[kUpb_FieldRep_1Byte] = 1,
[kUpb_FieldRep_4Byte] = 4,
[kUpb_FieldRep_StringView] = 8,
[kUpb_FieldRep_8Byte] = 8,
};
UPB_ASSERT(UPB_ALIGN_OF(upb_StringView) ==
UPB_SIZE(kRepToAlign32, kRepToAlign64)[kUpb_FieldRep_StringView]);
return platform == kUpb_MiniTablePlatform_32Bit ? kRepToAlign32[rep]
: kRepToAlign64[rep];
}
static const char* upb_MtDecoder_DecodeOneofField(upb_MtDecoder* d,
const char* ptr,
char first_ch,
upb_OneOfLayoutItem* item) {
uint32_t field_num;
ptr = upb_MdDecoder_DecodeBase92Varint(
&d->base, ptr, first_ch, kUpb_EncodedValue_MinOneofField,
kUpb_EncodedValue_MaxOneofField, &field_num);
upb_MiniTableField* f =
(void*)upb_MiniTable_FindFieldByNumber(d->table, field_num);
if (!f) {
upb_MdDecoder_ErrorJmp(&d->base,
"Couldn't add field number %" PRIu32
" to oneof, no such field number.",
field_num);
}
if (f->UPB_PRIVATE(offset) != kHasbitPresence) {
upb_MdDecoder_ErrorJmp(
&d->base,
"Cannot add repeated, required, or singular field %" PRIu32
" to oneof.",
field_num);
}
// Oneof storage must be large enough to accommodate the largest member.
int rep = f->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift;
size_t new_size = upb_MtDecoder_SizeOfRep(rep, d->platform);
size_t new_align = upb_MtDecoder_AlignOfRep(rep, d->platform);
size_t current_size = upb_MtDecoder_SizeOfRep(item->rep, d->platform);
size_t current_align = upb_MtDecoder_AlignOfRep(item->rep, d->platform);
if (new_size > current_size ||
(new_size == current_size && new_align > current_align)) {
UPB_ASSERT(new_align >= current_align);
item->rep = rep;
} else {
UPB_ASSERT(current_align >= new_align);
}
// Prepend this field to the linked list.
f->UPB_PRIVATE(offset) = item->field_index;
item->field_index = (f - d->fields) + kOneofBase;
return ptr;
}
static const char* upb_MtDecoder_DecodeOneofs(upb_MtDecoder* d,
const char* ptr) {
upb_OneOfLayoutItem item = {
.rep = 0, .field_index = kUpb_OneOfLayoutItem_IndexSentinel};
while (ptr < d->base.end) {
char ch = *ptr++;
if (ch == kUpb_EncodedValue_FieldSeparator) {
// Field separator, no action needed.
} else if (ch == kUpb_EncodedValue_OneofSeparator) {
// End of oneof.
upb_MtDecoder_PushOneof(d, item);
item.field_index =
kUpb_OneOfLayoutItem_IndexSentinel; // Move to next oneof.
} else {
ptr = upb_MtDecoder_DecodeOneofField(d, ptr, ch, &item);
}
}
// Push final oneof.
upb_MtDecoder_PushOneof(d, item);
return ptr;
}
static const char* upb_MtDecoder_ParseModifier(upb_MtDecoder* d,
const char* ptr, char first_ch,
upb_MiniTableField* last_field,
uint64_t* msg_modifiers) {
uint32_t mod;
ptr = upb_MdDecoder_DecodeBase92Varint(&d->base, ptr, first_ch,
kUpb_EncodedValue_MinModifier,
kUpb_EncodedValue_MaxModifier, &mod);
if (last_field) {
upb_MtDecoder_ModifyField(d, *msg_modifiers, mod, last_field);
} else {
if (!d->table) {
upb_MdDecoder_ErrorJmp(&d->base,
"Extensions cannot have message modifiers");
}
*msg_modifiers = mod;
}
return ptr;
}
static void upb_MtDecoder_AllocateSubs(upb_MtDecoder* d,
upb_SubCounts sub_counts) {
uint32_t total_count = sub_counts.submsg_count + sub_counts.subenum_count;
size_t subs_bytes = sizeof(*d->table->UPB_PRIVATE(subs)) * total_count;
size_t ptrs_bytes = sizeof(upb_MiniTable*) * sub_counts.submsg_count;
upb_MiniTableSubInternal* subs = upb_Arena_Malloc(d->arena, subs_bytes);
const upb_MiniTable** subs_ptrs = upb_Arena_Malloc(d->arena, ptrs_bytes);
upb_MdDecoder_CheckOutOfMemory(&d->base, subs);
upb_MdDecoder_CheckOutOfMemory(&d->base, subs_ptrs);
uint32_t i = 0;
for (; i < sub_counts.submsg_count; i++) {
subs_ptrs[i] = UPB_PRIVATE(_upb_MiniTable_Empty)();
subs[i].UPB_PRIVATE(submsg) = &subs_ptrs[i];
}
if (sub_counts.subenum_count) {
upb_MiniTableField* f = d->fields;
upb_MiniTableField* end_f = f + d->table->UPB_PRIVATE(field_count);
for (; f < end_f; f++) {
if (f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum) {
f->UPB_PRIVATE(submsg_index) += sub_counts.submsg_count;
}
}
for (; i < sub_counts.submsg_count + sub_counts.subenum_count; i++) {
subs[i].UPB_PRIVATE(subenum) = NULL;
}
}
d->table->UPB_PRIVATE(subs) = subs;
}
static const char* upb_MtDecoder_Parse(upb_MtDecoder* d, const char* ptr,
size_t len, void* fields,
size_t field_size, uint16_t* field_count,
upb_SubCounts* sub_counts) {
uint64_t msg_modifiers = 0;
uint32_t last_field_number = 0;
upb_MiniTableField* last_field = NULL;
bool need_dense_below = d->table != NULL;
d->base.end = UPB_PTRADD(ptr, len);
while (ptr < d->base.end) {
char ch = *ptr++;
if (ch <= kUpb_EncodedValue_MaxField) {
if (!d->table && last_field) {
// For extensions, consume only a single field and then return.
return --ptr;
}
upb_MiniTableField* field = fields;
*field_count += 1;
fields = (char*)fields + field_size;
field->UPB_PRIVATE(number) = ++last_field_number;
last_field = field;
upb_MiniTable_SetField(d, ch, field, msg_modifiers, sub_counts);
} else if (kUpb_EncodedValue_MinModifier <= ch &&
ch <= kUpb_EncodedValue_MaxModifier) {
ptr = upb_MtDecoder_ParseModifier(d, ptr, ch, last_field, &msg_modifiers);
if (msg_modifiers & kUpb_MessageModifier_IsExtendable) {
d->table->UPB_PRIVATE(ext) |= kUpb_ExtMode_Extendable;
}
} else if (ch == kUpb_EncodedValue_End) {
if (!d->table) {
upb_MdDecoder_ErrorJmp(&d->base, "Extensions cannot have oneofs.");
}
ptr = upb_MtDecoder_DecodeOneofs(d, ptr);
} else if (kUpb_EncodedValue_MinSkip <= ch &&
ch <= kUpb_EncodedValue_MaxSkip) {
if (need_dense_below) {
d->table->UPB_PRIVATE(dense_below) = d->table->UPB_PRIVATE(field_count);
need_dense_below = false;
}
uint32_t skip;
ptr = upb_MdDecoder_DecodeBase92Varint(&d->base, ptr, ch,
kUpb_EncodedValue_MinSkip,
kUpb_EncodedValue_MaxSkip, &skip);
last_field_number += skip;
last_field_number--; // Next field seen will increment.
} else {
upb_MdDecoder_ErrorJmp(&d->base, "Invalid char: %c", ch);
}
}
if (need_dense_below) {
d->table->UPB_PRIVATE(dense_below) = d->table->UPB_PRIVATE(field_count);
}
return ptr;
}
static void upb_MtDecoder_ParseMessage(upb_MtDecoder* d, const char* data,
size_t len) {
// Buffer length is an upper bound on the number of fields. We will return
// what we don't use.
if (SIZE_MAX / sizeof(*d->fields) < len) {
upb_MdDecoder_ErrorJmp(&d->base, "Out of memory");
}
d->fields = upb_Arena_Malloc(d->arena, sizeof(*d->fields) * len);
upb_MdDecoder_CheckOutOfMemory(&d->base, d->fields);
upb_SubCounts sub_counts = {0, 0};
d->table->UPB_PRIVATE(field_count) = 0;
d->table->UPB_PRIVATE(fields) = d->fields;
upb_MtDecoder_Parse(d, data, len, d->fields, sizeof(*d->fields),
&d->table->UPB_PRIVATE(field_count), &sub_counts);
upb_Arena_ShrinkLast(d->arena, d->fields, sizeof(*d->fields) * len,
sizeof(*d->fields) * d->table->UPB_PRIVATE(field_count));
d->table->UPB_PRIVATE(fields) = d->fields;
upb_MtDecoder_AllocateSubs(d, sub_counts);
}
static void upb_MtDecoder_CalculateAlignments(upb_MtDecoder* d) {
// Add alignment counts for non-oneof fields (oneofs were added already)
int n = d->table->UPB_PRIVATE(field_count);
for (int i = 0; i < n; i++) {
upb_MiniTableField* f = &d->fields[i];
if (f->UPB_PRIVATE(offset) >= kOneofBase) continue;
d->rep_counts_offsets[f->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift]++;
}
// Reserve properly aligned space for each type of field representation
// present in this message. When we iterate over the fields, they will obtain
// their offset from within the region matching their alignment requirements.
size_t base = d->table->UPB_PRIVATE(size);
// Start with the lowest alignment requirement, going up, because:
// 1. If there are presence bits, we won't be aligned to start, but adding
// some lower-alignment fields may get us closer without wasting space to
// padding.
// 2. The allocator enforces 8 byte alignment, so moving intermediate padding
// to trailing padding doesn't save us anything.
for (upb_FieldRep rep = kUpb_FieldRep_1Byte; rep <= kUpb_FieldRep_Max;
rep++) {
uint16_t count = d->rep_counts_offsets[rep];
if (count) {
base = UPB_ALIGN_UP(base, upb_MtDecoder_AlignOfRep(rep, d->platform));
// This entry now tracks the base offset for this field representation
// type, instead of the count
d->rep_counts_offsets[rep] = base;
base += upb_MtDecoder_SizeOfRep(rep, d->platform) * count;
}
}
static const size_t max = UINT16_MAX;
if (base > max) {
upb_MdDecoder_ErrorJmp(
&d->base, "Message size exceeded maximum size of %zu bytes", max);
}
d->table->UPB_PRIVATE(size) = (uint16_t)base;
}
static size_t upb_MiniTable_DivideRoundUp(size_t n, size_t d) {
return (n + d - 1) / d;
}
static void upb_MtDecoder_AssignHasbits(upb_MtDecoder* d) {
upb_MiniTable* ret = d->table;
int n = ret->UPB_PRIVATE(field_count);
size_t last_hasbit = kUpb_Reserved_Hasbits - 1;
// First assign required fields, which must have the lowest hasbits.
for (int i = 0; i < n; i++) {
upb_MiniTableField* field =
(upb_MiniTableField*)&ret->UPB_PRIVATE(fields)[i];
if (field->UPB_PRIVATE(offset) == kRequiredPresence) {
field->presence = ++last_hasbit;
} else if (field->UPB_PRIVATE(offset) == kNoPresence) {
field->presence = 0;
}
}
if (last_hasbit > kUpb_Reserved_Hasbits + 63) {
upb_MdDecoder_ErrorJmp(&d->base, "Too many required fields");
}
ret->UPB_PRIVATE(required_count) = last_hasbit - (kUpb_Reserved_Hasbits - 1);
// Next assign non-required hasbit fields.
for (int i = 0; i < n; i++) {
upb_MiniTableField* field =
(upb_MiniTableField*)&ret->UPB_PRIVATE(fields)[i];
if (field->UPB_PRIVATE(offset) == kHasbitPresence) {
field->presence = ++last_hasbit;
}
}
ret->UPB_PRIVATE(size) =
last_hasbit ? upb_MiniTable_DivideRoundUp(last_hasbit + 1, 8) : 0;
}
static size_t upb_MtDecoder_Place(upb_MtDecoder* d, upb_FieldRep rep) {
size_t size = upb_MtDecoder_SizeOfRep(rep, d->platform);
size_t offset = d->rep_counts_offsets[rep];
d->rep_counts_offsets[rep] += size;
return offset;
}
static void upb_MtDecoder_AssignOffsets(upb_MtDecoder* d) {
upb_MiniTableField* field_end =
UPB_PTRADD(d->fields, d->table->UPB_PRIVATE(field_count));
for (upb_MiniTableField* field = d->fields; field < field_end; field++) {
if (field->UPB_PRIVATE(offset) >= kOneofBase) continue;
field->UPB_PRIVATE(offset) =
upb_MtDecoder_Place(d, field->UPB_PRIVATE(mode) >> kUpb_FieldRep_Shift);
}
upb_OneOfLayoutItem* oneof_end = UPB_PTRADD(d->oneofs.data, d->oneofs.size);
for (upb_OneOfLayoutItem* item = d->oneofs.data; item < oneof_end; item++) {
upb_MiniTableField* f = &d->fields[item->field_index];
uint16_t case_offset = upb_MtDecoder_Place(d, kUpb_OneOf_CaseFieldRep);
uint16_t data_offset = upb_MtDecoder_Place(d, item->rep);
while (true) {
f->presence = ~case_offset;
uint16_t next_offset = f->UPB_PRIVATE(offset);
f->UPB_PRIVATE(offset) = data_offset;
if (next_offset == kUpb_OneOfLayoutItem_IndexSentinel) break;
UPB_ASSERT(next_offset - kOneofBase < d->table->UPB_PRIVATE(field_count));
f = &d->fields[next_offset - kOneofBase];
}
}
// The fasttable parser (supported on 64-bit only) depends on this being a
// multiple of 8 in order to satisfy UPB_MALLOC_ALIGN, which is also 8.
//
// On 32-bit we could potentially make this smaller, but there is no
// compelling reason to optimize this right now.
d->table->UPB_PRIVATE(size) = UPB_ALIGN_UP(d->table->UPB_PRIVATE(size), 8);
}
static void upb_MtDecoder_ValidateEntryField(upb_MtDecoder* d,
const upb_MiniTableField* f,
uint32_t expected_num) {
const char* name = expected_num == 1 ? "key" : "val";
const uint32_t f_number = upb_MiniTableField_Number(f);
if (f_number != expected_num) {
upb_MdDecoder_ErrorJmp(&d->base,
"map %s did not have expected number (%d vs %d)",
name, expected_num, f_number);
}
if (!upb_MiniTableField_IsScalar(f)) {
upb_MdDecoder_ErrorJmp(
&d->base, "map %s cannot be repeated or map, or be in oneof", name);
}
uint32_t not_ok_types;
if (expected_num == 1) {
not_ok_types = (1 << kUpb_FieldType_Float) | (1 << kUpb_FieldType_Double) |
(1 << kUpb_FieldType_Message) | (1 << kUpb_FieldType_Group) |
(1 << kUpb_FieldType_Bytes) | (1 << kUpb_FieldType_Enum);
} else {
not_ok_types = 1 << kUpb_FieldType_Group;
}
if ((1 << upb_MiniTableField_Type(f)) & not_ok_types) {
upb_MdDecoder_ErrorJmp(&d->base, "map %s cannot have type %d", name,
(int)f->UPB_PRIVATE(descriptortype));
}
}
static void upb_MtDecoder_ParseMap(upb_MtDecoder* d, const char* data,
size_t len) {
upb_MtDecoder_ParseMessage(d, data, len);
upb_MtDecoder_AssignHasbits(d);
if (UPB_UNLIKELY(d->table->UPB_PRIVATE(field_count) != 2)) {
upb_MdDecoder_ErrorJmp(&d->base, "%hu fields in map",
d->table->UPB_PRIVATE(field_count));
UPB_UNREACHABLE();
}
if (d->oneofs.size != 0) {
upb_MdDecoder_ErrorJmp(&d->base, "Map entry cannot have oneof");
}
upb_MtDecoder_ValidateEntryField(d, &d->table->UPB_PRIVATE(fields)[0], 1);
upb_MtDecoder_ValidateEntryField(d, &d->table->UPB_PRIVATE(fields)[1], 2);
d->fields[0].UPB_PRIVATE(offset) = offsetof(upb_MapEntry, k);
d->fields[1].UPB_PRIVATE(offset) = offsetof(upb_MapEntry, v);
d->table->UPB_PRIVATE(size) = sizeof(upb_MapEntry);
// Map entries have a special bit set to signal it's a map entry, used in
// upb_MiniTable_SetSubMessage() below.
d->table->UPB_PRIVATE(ext) |= kUpb_ExtMode_IsMapEntry;
}
static void upb_MtDecoder_ParseMessageSet(upb_MtDecoder* d, const char* data,
size_t len) {
if (len > 0) {
upb_MdDecoder_ErrorJmp(&d->base, "Invalid message set encode length: %zu",
len);
}
upb_MiniTable* ret = d->table;
ret->UPB_PRIVATE(size) = kUpb_Reserved_Hasbytes;
ret->UPB_PRIVATE(field_count) = 0;
ret->UPB_PRIVATE(ext) = kUpb_ExtMode_IsMessageSet;
ret->UPB_PRIVATE(dense_below) = 0;
ret->UPB_PRIVATE(table_mask) = -1;
ret->UPB_PRIVATE(required_count) = 0;
}
static upb_MiniTable* upb_MtDecoder_DoBuildMiniTableWithBuf(
upb_MtDecoder* decoder, const char* data, size_t len, void** buf,
size_t* buf_size) {
upb_MdDecoder_CheckOutOfMemory(&decoder->base, decoder->table);
decoder->table->UPB_PRIVATE(size) = kUpb_Reserved_Hasbytes;
decoder->table->UPB_PRIVATE(field_count) = 0;
decoder->table->UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable;
decoder->table->UPB_PRIVATE(dense_below) = 0;
decoder->table->UPB_PRIVATE(table_mask) = -1;
decoder->table->UPB_PRIVATE(required_count) = 0;
#ifdef UPB_TRACING_ENABLED
// MiniTables built from MiniDescriptors will not be able to vend the message
// name unless it is explicitly set with upb_MiniTable_SetFullName().
decoder->table->UPB_PRIVATE(full_name) = 0;
#endif
// Strip off and verify the version tag.
if (!len--) goto done;
const char vers = *data++;
switch (vers) {
case kUpb_EncodedVersion_MapV1:
upb_MtDecoder_ParseMap(decoder, data, len);
break;
case kUpb_EncodedVersion_MessageV1:
upb_MtDecoder_ParseMessage(decoder, data, len);
upb_MtDecoder_AssignHasbits(decoder);
upb_MtDecoder_CalculateAlignments(decoder);
upb_MtDecoder_AssignOffsets(decoder);
break;
case kUpb_EncodedVersion_MessageSetV1:
upb_MtDecoder_ParseMessageSet(decoder, data, len);
break;
default:
upb_MdDecoder_ErrorJmp(&decoder->base, "Invalid message version: %c",
vers);
}
done:
*buf = decoder->oneofs.data;
*buf_size = decoder->oneofs.buf_capacity_bytes;
return decoder->table;
}
static upb_MiniTable* upb_MtDecoder_BuildMiniTableWithBuf(
upb_MtDecoder* const decoder, const char* const data, const size_t len,
void** const buf, size_t* const buf_size) {
if (UPB_SETJMP(decoder->base.err) != 0) {
*buf = decoder->oneofs.data;
*buf_size = decoder->oneofs.buf_capacity_bytes;
return NULL;
}
return upb_MtDecoder_DoBuildMiniTableWithBuf(decoder, data, len, buf,
buf_size);
}
upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len,
upb_MiniTablePlatform platform,
upb_Arena* arena, void** buf,
size_t* buf_size,
upb_Status* status) {
upb_MtDecoder decoder = {
.base = {.status = status},
.platform = platform,
.oneofs =
{
.data = *buf,
.buf_capacity_bytes = *buf_size,
.size = 0,
},
.arena = arena,
.table = upb_Arena_Malloc(arena, sizeof(*decoder.table)),
};
return upb_MtDecoder_BuildMiniTableWithBuf(&decoder, data, len, buf,
buf_size);
}
static const char* upb_MtDecoder_DoBuildMiniTableExtension(
upb_MtDecoder* decoder, const char* data, size_t len,
upb_MiniTableExtension* ext, const upb_MiniTable* extendee,
upb_MiniTableSub sub) {
if (!(extendee->UPB_PRIVATE(ext) &
(kUpb_ExtMode_Extendable | kUpb_ExtMode_IsMessageSet))) {
upb_MdDecoder_ErrorJmp(&decoder->base, "Extendee is not extendable");
}
// If the string is non-empty then it must begin with a version tag.
if (len) {
if (*data != kUpb_EncodedVersion_ExtensionV1) {
upb_MdDecoder_ErrorJmp(&decoder->base, "Invalid ext version: %c", *data);
}
data++;
len--;
}
uint16_t count = 0;
upb_SubCounts sub_counts = {0, 0};
const char* ret = upb_MtDecoder_Parse(decoder, data, len, ext, sizeof(*ext),
&count, &sub_counts);
if (!ret || count != 1) return NULL;
upb_MiniTableField* f = &ext->UPB_PRIVATE(field);
f->UPB_PRIVATE(mode) |= kUpb_LabelFlags_IsExtension;
f->UPB_PRIVATE(offset) = 0;
f->presence = 0;
if (extendee->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMessageSet) {
// Extensions of MessageSet must be messages.
if (!upb_MiniTableField_IsSubMessage(f)) return NULL;
// Extensions of MessageSet must be non-repeating.
if (upb_MiniTableField_IsArray(f)) return NULL;
}
ext->UPB_PRIVATE(extendee) = extendee;
ext->UPB_PRIVATE(sub) = sub;
return ret;
}
static const char* upb_MtDecoder_BuildMiniTableExtension(
upb_MtDecoder* const decoder, const char* const data, const size_t len,
upb_MiniTableExtension* const ext, const upb_MiniTable* const extendee,
const upb_MiniTableSub sub) {
if (UPB_SETJMP(decoder->base.err) != 0) return NULL;
return upb_MtDecoder_DoBuildMiniTableExtension(decoder, data, len, ext,
extendee, sub);
}
const char* _upb_MiniTableExtension_Init(const char* data, size_t len,
upb_MiniTableExtension* ext,
const upb_MiniTable* extendee,
upb_MiniTableSub sub,
upb_MiniTablePlatform platform,
upb_Status* status) {
upb_MtDecoder decoder = {
.base = {.status = status},
.arena = NULL,
.table = NULL,
.platform = platform,
};
return upb_MtDecoder_BuildMiniTableExtension(&decoder, data, len, ext,
extendee, sub);
}
upb_MiniTableExtension* _upb_MiniTableExtension_Build(
const char* data, size_t len, const upb_MiniTable* extendee,
upb_MiniTableSub sub, upb_MiniTablePlatform platform, upb_Arena* arena,
upb_Status* status) {
upb_MiniTableExtension* ext =
upb_Arena_Malloc(arena, sizeof(upb_MiniTableExtension));
if (UPB_UNLIKELY(!ext)) return NULL;
const char* ptr = _upb_MiniTableExtension_Init(data, len, ext, extendee, sub,
platform, status);
if (UPB_UNLIKELY(!ptr)) return NULL;
return ext;
}
upb_MiniTable* _upb_MiniTable_Build(const char* data, size_t len,
upb_MiniTablePlatform platform,
upb_Arena* arena, upb_Status* status) {
void* buf = NULL;
size_t size = 0;
upb_MiniTable* ret = upb_MiniTable_BuildWithBuf(data, len, platform, arena,
&buf, &size, status);
upb_gfree(buf);
return ret;
}
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
bool upb_MiniTable_SetSubMessage(upb_MiniTable* table,
upb_MiniTableField* field,
const upb_MiniTable* sub) {
UPB_ASSERT((uintptr_t)table->UPB_PRIVATE(fields) <= (uintptr_t)field &&
(uintptr_t)field < (uintptr_t)(table->UPB_PRIVATE(fields) +
table->UPB_PRIVATE(field_count)));
UPB_ASSERT(sub);
const bool sub_is_map = sub->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry;
switch (field->UPB_PRIVATE(descriptortype)) {
case kUpb_FieldType_Message:
if (sub_is_map) {
const bool table_is_map =
table->UPB_PRIVATE(ext) & kUpb_ExtMode_IsMapEntry;
if (UPB_UNLIKELY(table_is_map)) return false;
field->UPB_PRIVATE(mode) =
(field->UPB_PRIVATE(mode) & ~kUpb_FieldMode_Mask) |
kUpb_FieldMode_Map;
}
break;
case kUpb_FieldType_Group:
if (UPB_UNLIKELY(sub_is_map)) return false;
break;
default:
return false;
}
int idx = field->UPB_PRIVATE(submsg_index);
upb_MiniTableSubInternal* table_subs = (void*)table->UPB_PRIVATE(subs);
// TODO: Add this assert back once YouTube is updated to not call
// this function repeatedly.
// UPB_ASSERT(UPB_PRIVATE(_upb_MiniTable_IsEmpty)(table_sub->submsg));
memcpy((void*)table_subs[idx].UPB_PRIVATE(submsg), &sub, sizeof(void*));
return true;
}
bool upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTableField* field,
const upb_MiniTableEnum* sub) {
UPB_ASSERT((uintptr_t)table->UPB_PRIVATE(fields) <= (uintptr_t)field &&
(uintptr_t)field < (uintptr_t)(table->UPB_PRIVATE(fields) +
table->UPB_PRIVATE(field_count)));
UPB_ASSERT(sub);
if (field->UPB_PRIVATE(descriptortype) != kUpb_FieldType_Enum) {
return false;
}
upb_MiniTableSub* table_sub =
(void*)&table->UPB_PRIVATE(subs)[field->UPB_PRIVATE(submsg_index)];
*table_sub = upb_MiniTableSub_FromEnum(sub);
return true;
}
uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* m,
const upb_MiniTableField** subs) {
uint32_t msg_count = 0;
uint32_t enum_count = 0;
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_CType(f) == kUpb_CType_Message) {
*subs = f;
++subs;
msg_count++;
}
}
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_IsClosedEnum(f)) {
*subs = f;
++subs;
enum_count++;
}
}
return (msg_count << 16) | enum_count;
}
// The list of sub_tables and sub_enums must exactly match the number and order
// of sub-message fields and sub-enum fields given by upb_MiniTable_GetSubList()
// above.
bool upb_MiniTable_Link(upb_MiniTable* m, const upb_MiniTable** sub_tables,
size_t sub_table_count,
const upb_MiniTableEnum** sub_enums,
size_t sub_enum_count) {
uint32_t msg_count = 0;
uint32_t enum_count = 0;
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
upb_MiniTableField* f =
(upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_CType(f) == kUpb_CType_Message) {
const upb_MiniTable* sub = sub_tables[msg_count++];
if (msg_count > sub_table_count) return false;
if (sub && !upb_MiniTable_SetSubMessage(m, f, sub)) return false;
}
}
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
upb_MiniTableField* f =
(upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_IsClosedEnum(f)) {
const upb_MiniTableEnum* sub = sub_enums[enum_count++];
if (enum_count > sub_enum_count) return false;
if (sub && !upb_MiniTable_SetSubEnum(m, f, sub)) return false;
}
}
return (msg_count == sub_table_count) && (enum_count == sub_enum_count);
}
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
#define EXTREG_KEY_SIZE (sizeof(upb_MiniTable*) + sizeof(uint32_t))
struct upb_ExtensionRegistry {
upb_Arena* arena;
upb_strtable exts; // Key is upb_MiniTable* concatenated with fieldnum.
};
static void extreg_key(char* buf, const upb_MiniTable* l, uint32_t fieldnum) {
memcpy(buf, &l, sizeof(l));
memcpy(buf + sizeof(l), &fieldnum, sizeof(fieldnum));
}
upb_ExtensionRegistry* upb_ExtensionRegistry_New(upb_Arena* arena) {
upb_ExtensionRegistry* r = upb_Arena_Malloc(arena, sizeof(*r));
if (!r) return NULL;
r->arena = arena;
if (!upb_strtable_init(&r->exts, 8, arena)) return NULL;
return r;
}
UPB_API upb_ExtensionRegistryStatus upb_ExtensionRegistry_Add(
upb_ExtensionRegistry* r, const upb_MiniTableExtension* e) {
char buf[EXTREG_KEY_SIZE];
extreg_key(buf, e->UPB_PRIVATE(extendee), upb_MiniTableExtension_Number(e));
if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, NULL)) {
return kUpb_ExtensionRegistryStatus_DuplicateEntry;
}
if (!upb_strtable_insert(&r->exts, buf, EXTREG_KEY_SIZE,
upb_value_constptr(e), r->arena)) {
return kUpb_ExtensionRegistryStatus_OutOfMemory;
}
return kUpb_ExtensionRegistryStatus_Ok;
}
upb_ExtensionRegistryStatus upb_ExtensionRegistry_AddArray(
upb_ExtensionRegistry* r, const upb_MiniTableExtension** e, size_t count) {
const upb_MiniTableExtension** start = e;
const upb_MiniTableExtension** end = UPB_PTRADD(e, count);
upb_ExtensionRegistryStatus status = kUpb_ExtensionRegistryStatus_Ok;
for (; e < end; e++) {
status = upb_ExtensionRegistry_Add(r, *e);
if (status != kUpb_ExtensionRegistryStatus_Ok) goto failure;
}
return kUpb_ExtensionRegistryStatus_Ok;
failure:
// Back out the entries previously added.
for (end = e, e = start; e < end; e++) {
const upb_MiniTableExtension* ext = *e;
char buf[EXTREG_KEY_SIZE];
extreg_key(buf, ext->UPB_PRIVATE(extendee),
upb_MiniTableExtension_Number(ext));
upb_strtable_remove2(&r->exts, buf, EXTREG_KEY_SIZE, NULL);
}
UPB_ASSERT(status != kUpb_ExtensionRegistryStatus_Ok);
return status;
}
#ifdef UPB_LINKARR_DECLARE
UPB_LINKARR_DECLARE(upb_AllExts, const upb_MiniTableExtension);
bool upb_ExtensionRegistry_AddAllLinkedExtensions(upb_ExtensionRegistry* r) {
const upb_MiniTableExtension* start = UPB_LINKARR_START(upb_AllExts);
const upb_MiniTableExtension* stop = UPB_LINKARR_STOP(upb_AllExts);
for (const upb_MiniTableExtension* p = start; p < stop; p++) {
// Windows can introduce zero padding, so we have to skip zeroes.
if (upb_MiniTableExtension_Number(p) != 0) {
if (upb_ExtensionRegistry_Add(r, p) != kUpb_ExtensionRegistryStatus_Ok) {
return false;
}
}
}
return true;
}
#endif // UPB_LINKARR_DECLARE
const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(
const upb_ExtensionRegistry* r, const upb_MiniTable* t, uint32_t num) {
char buf[EXTREG_KEY_SIZE];
upb_value v;
extreg_key(buf, t, num);
if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, &v)) {
return upb_value_getconstptr(v);
} else {
return NULL;
}
}
#include <stddef.h>
#include <stdint.h>
// Must be last.
const upb_MiniTableField* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* m, uint32_t number) {
const size_t i = ((size_t)number) - 1; // 0 wraps to SIZE_MAX
// Ideal case: index into dense fields
if (i < m->UPB_PRIVATE(dense_below)) {
UPB_ASSERT(m->UPB_PRIVATE(fields)[i].UPB_PRIVATE(number) == number);
return &m->UPB_PRIVATE(fields)[i];
}
// Slow case: binary search
uint32_t lo = m->UPB_PRIVATE(dense_below);
int32_t hi = m->UPB_PRIVATE(field_count) - 1;
const upb_MiniTableField* base = m->UPB_PRIVATE(fields);
while (hi >= (int32_t)lo) {
uint32_t mid = (hi + lo) / 2;
uint32_t num = base[mid].UPB_ONLYBITS(number);
// These comparison operations allow, on ARM machines, to fuse all these
// branches into one comparison followed by two CSELs to set the lo/hi
// values, followed by a BNE to continue or terminate the loop. Since binary
// search branches are generally unpredictable (50/50 in each direction),
// this is a good deal. We use signed for the high, as this decrement may
// underflow if mid is 0.
int32_t hi_mid = mid - 1;
uint32_t lo_mid = mid + 1;
if (num == number) {
return &base[mid];
}
if (UPB_UNPREDICTABLE(num < number)) {
lo = lo_mid;
} else {
hi = hi_mid;
}
}
return NULL;
}
const upb_MiniTableField* upb_MiniTable_GetOneof(const upb_MiniTable* m,
const upb_MiniTableField* f) {
if (UPB_UNLIKELY(!upb_MiniTableField_IsInOneof(f))) {
return NULL;
}
const upb_MiniTableField* ptr = &m->UPB_PRIVATE(fields)[0];
const upb_MiniTableField* end =
&m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)];
for (; ptr < end; ptr++) {
if (ptr->presence == (*f).presence) {
return ptr;
}
}
return NULL;
}
bool upb_MiniTable_NextOneofField(const upb_MiniTable* m,
const upb_MiniTableField** f) {
const upb_MiniTableField* ptr = *f;
const upb_MiniTableField* end =
&m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)];
while (++ptr < end) {
if (ptr->presence == (*f)->presence) {
*f = ptr;
return true;
}
}
return false;
}
#include <stddef.h>
#include <stdint.h>
// Must be last.
// Checks if source and target mini table fields are identical.
//
// If the field is a sub message and sub messages are identical we record
// the association in table.
//
// Hashing the source sub message mini table and it's equivalent in the table
// stops recursing when a cycle is detected and instead just checks if the
// destination table is equal.
static upb_MiniTableEquals_Status upb_deep_check(const upb_MiniTable* src,
const upb_MiniTable* dst,
upb_inttable* table,
upb_Arena** arena) {
if (src->UPB_PRIVATE(field_count) != dst->UPB_PRIVATE(field_count))
return kUpb_MiniTableEquals_NotEqual;
bool marked_src = false;
for (int i = 0; i < upb_MiniTable_FieldCount(src); i++) {
const upb_MiniTableField* src_field = upb_MiniTable_GetFieldByIndex(src, i);
const upb_MiniTableField* dst_field = upb_MiniTable_FindFieldByNumber(
dst, upb_MiniTableField_Number(src_field));
if (upb_MiniTableField_CType(src_field) !=
upb_MiniTableField_CType(dst_field))
return false;
if (src_field->UPB_PRIVATE(mode) != dst_field->UPB_PRIVATE(mode))
return false;
if (src_field->UPB_PRIVATE(offset) != dst_field->UPB_PRIVATE(offset))
return false;
if (src_field->presence != dst_field->presence) return false;
if (src_field->UPB_PRIVATE(submsg_index) !=
dst_field->UPB_PRIVATE(submsg_index))
return kUpb_MiniTableEquals_NotEqual;
// Go no further if we are only checking for compatibility.
if (!table) continue;
if (upb_MiniTableField_CType(src_field) == kUpb_CType_Message) {
if (!*arena) {
*arena = upb_Arena_New();
if (!upb_inttable_init(table, *arena)) {
return kUpb_MiniTableEquals_OutOfMemory;
}
}
if (!marked_src) {
marked_src = true;
upb_value val;
val.val = (uint64_t)dst;
if (!upb_inttable_insert(table, (uintptr_t)src, val, *arena)) {
return kUpb_MiniTableEquals_OutOfMemory;
}
}
const upb_MiniTable* sub_src =
upb_MiniTable_GetSubMessageTable(src, src_field);
const upb_MiniTable* sub_dst =
upb_MiniTable_GetSubMessageTable(dst, dst_field);
if (sub_src != NULL) {
upb_value cmp;
if (upb_inttable_lookup(table, (uintptr_t)sub_src, &cmp)) {
// We already compared this src before. Check if same dst.
if (cmp.val != (uint64_t)sub_dst) {
return kUpb_MiniTableEquals_NotEqual;
}
} else {
// Recurse if not already visited.
upb_MiniTableEquals_Status s =
upb_deep_check(sub_src, sub_dst, table, arena);
if (s != kUpb_MiniTableEquals_Equal) {
return s;
}
}
}
}
}
return kUpb_MiniTableEquals_Equal;
}
bool upb_MiniTable_Compatible(const upb_MiniTable* src,
const upb_MiniTable* dst) {
return upb_deep_check(src, dst, NULL, NULL);
}
upb_MiniTableEquals_Status upb_MiniTable_Equals(const upb_MiniTable* src,
const upb_MiniTable* dst) {
// Arena allocated on demand for hash table.
upb_Arena* arena = NULL;
// Table to keep track of visited mini tables to guard against cycles.
upb_inttable table;
upb_MiniTableEquals_Status status = upb_deep_check(src, dst, &table, &arena);
if (arena) {
upb_Arena_Free(arena);
}
return status;
}
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
// A few fake field types for our tables.
enum {
kUpb_FakeFieldType_FieldNotFound = 0,
kUpb_FakeFieldType_MessageSetItem = 19,
};
// DecodeOp: an action to be performed for a wire-type/field-type combination.
enum {
// Special ops: we don't write data to regular fields for these.
kUpb_DecodeOp_UnknownField = -1,
kUpb_DecodeOp_MessageSetItem = -2,
// Scalar-only ops.
kUpb_DecodeOp_Scalar1Byte = 0,
kUpb_DecodeOp_Scalar4Byte = 2,
kUpb_DecodeOp_Scalar8Byte = 3,
kUpb_DecodeOp_Enum = 1,
// Scalar/repeated ops.
kUpb_DecodeOp_String = 4,
kUpb_DecodeOp_Bytes = 5,
kUpb_DecodeOp_SubMessage = 6,
// Repeated-only ops (also see macros below).
kUpb_DecodeOp_PackedEnum = 13,
};
// For packed fields it is helpful to be able to recover the lg2 of the data
// size from the op.
#define OP_FIXPCK_LG2(n) (n + 5) /* n in [2, 3] => op in [7, 8] */
#define OP_VARPCK_LG2(n) (n + 9) /* n in [0, 2, 3] => op in [9, 11, 12] */
typedef union {
bool bool_val;
uint32_t uint32_val;
uint64_t uint64_val;
uint32_t size;
} wireval;
// Ideally these two functions should take the owning MiniTable pointer as a
// first argument, then we could just put them in mini_table/message.h as nice
// clean getters. But we don't have that so instead we gotta write these
// Frankenfunctions that take an array of subtables.
// TODO: Move these to mini_table/ anyway since there are other places
// that could use them.
// Returns the MiniTable corresponding to a given MiniTableField
// from an array of MiniTableSubs.
static const upb_MiniTable* _upb_MiniTableSubs_MessageByField(
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field) {
return *subs[field->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(submsg);
}
// Returns the MiniTableEnum corresponding to a given MiniTableField
// from an array of MiniTableSub.
static const upb_MiniTableEnum* _upb_MiniTableSubs_EnumByField(
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field) {
return subs[field->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(subenum);
}
static const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr,
upb_Message* msg,
const upb_MiniTable* layout);
UPB_NORETURN static void* _upb_Decoder_ErrorJmp(upb_Decoder* d,
upb_DecodeStatus status) {
UPB_ASSERT(status != kUpb_DecodeStatus_Ok);
d->status = status;
UPB_LONGJMP(d->err, 1);
}
const char* _upb_FastDecoder_ErrorJmp(upb_Decoder* d, int status) {
UPB_ASSERT(status != kUpb_DecodeStatus_Ok);
d->status = status;
UPB_LONGJMP(d->err, 1);
return NULL;
}
static void _upb_Decoder_VerifyUtf8(upb_Decoder* d, const char* buf, int len) {
if (!_upb_Decoder_VerifyUtf8Inline(buf, len)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_BadUtf8);
}
}
static bool _upb_Decoder_Reserve(upb_Decoder* d, upb_Array* arr, size_t elem) {
bool need_realloc =
arr->UPB_PRIVATE(capacity) - arr->UPB_PRIVATE(size) < elem;
if (need_realloc && !UPB_PRIVATE(_upb_Array_Realloc)(
arr, arr->UPB_PRIVATE(size) + elem, &d->arena)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
return need_realloc;
}
typedef struct {
const char* ptr;
uint64_t val;
} _upb_DecodeLongVarintReturn;
UPB_NOINLINE
static _upb_DecodeLongVarintReturn _upb_Decoder_DecodeLongVarint(
const char* ptr, uint64_t val) {
_upb_DecodeLongVarintReturn ret = {NULL, 0};
uint64_t byte;
for (int i = 1; i < 10; i++) {
byte = (uint8_t)ptr[i];
val += (byte - 1) << (i * 7);
if (!(byte & 0x80)) {
ret.ptr = ptr + i + 1;
ret.val = val;
return ret;
}
}
return ret;
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeVarint(upb_Decoder* d, const char* ptr,
uint64_t* val) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = byte;
return ptr + 1;
} else {
_upb_DecodeLongVarintReturn res = _upb_Decoder_DecodeLongVarint(ptr, byte);
if (!res.ptr) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
*val = res.val;
return res.ptr;
}
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeTag(upb_Decoder* d, const char* ptr,
uint32_t* val) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = byte;
return ptr + 1;
} else {
const char* start = ptr;
_upb_DecodeLongVarintReturn res = _upb_Decoder_DecodeLongVarint(ptr, byte);
if (!res.ptr || res.ptr - start > 5 || res.val > UINT32_MAX) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
*val = res.val;
return res.ptr;
}
}
UPB_FORCEINLINE
const char* upb_Decoder_DecodeSize(upb_Decoder* d, const char* ptr,
uint32_t* size) {
uint64_t size64;
ptr = _upb_Decoder_DecodeVarint(d, ptr, &size64);
if (size64 >= INT32_MAX ||
!upb_EpsCopyInputStream_CheckSize(&d->input, ptr, (int)size64)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
*size = size64;
return ptr;
}
static void _upb_Decoder_MungeInt32(wireval* val) {
if (!upb_IsLittleEndian()) {
/* The next stage will memcpy(dst, &val, 4) */
val->uint32_val = val->uint64_val;
}
}
static void _upb_Decoder_Munge(int type, wireval* val) {
switch (type) {
case kUpb_FieldType_Bool:
val->bool_val = val->uint64_val != 0;
break;
case kUpb_FieldType_SInt32: {
uint32_t n = val->uint64_val;
val->uint32_val = (n >> 1) ^ -(int32_t)(n & 1);
break;
}
case kUpb_FieldType_SInt64: {
uint64_t n = val->uint64_val;
val->uint64_val = (n >> 1) ^ -(int64_t)(n & 1);
break;
}
case kUpb_FieldType_Int32:
case kUpb_FieldType_UInt32:
case kUpb_FieldType_Enum:
_upb_Decoder_MungeInt32(val);
break;
}
}
static upb_Message* _upb_Decoder_NewSubMessage2(upb_Decoder* d,
const upb_MiniTable* subl,
const upb_MiniTableField* field,
upb_TaggedMessagePtr* target) {
UPB_ASSERT(subl);
upb_Message* msg = _upb_Message_New(subl, &d->arena);
if (!msg) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
// Extensions should not be unlinked. A message extension should not be
// registered until its sub-message type is available to be linked.
bool is_empty = UPB_PRIVATE(_upb_MiniTable_IsEmpty)(subl);
bool is_extension = field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension;
UPB_ASSERT(!(is_empty && is_extension));
if (is_empty && !(d->options & kUpb_DecodeOption_ExperimentalAllowUnlinked)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_UnlinkedSubMessage);
}
upb_TaggedMessagePtr tagged =
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(msg, is_empty);
memcpy(target, &tagged, sizeof(tagged));
return msg;
}
static upb_Message* _upb_Decoder_NewSubMessage(
upb_Decoder* d, const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field, upb_TaggedMessagePtr* target) {
const upb_MiniTable* subl = _upb_MiniTableSubs_MessageByField(subs, field);
return _upb_Decoder_NewSubMessage2(d, subl, field, target);
}
static upb_Message* _upb_Decoder_ReuseSubMessage(
upb_Decoder* d, const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field, upb_TaggedMessagePtr* target) {
upb_TaggedMessagePtr tagged = *target;
const upb_MiniTable* subl = _upb_MiniTableSubs_MessageByField(subs, field);
UPB_ASSERT(subl);
if (!upb_TaggedMessagePtr_IsEmpty(tagged) ||
UPB_PRIVATE(_upb_MiniTable_IsEmpty)(subl)) {
return UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(tagged);
}
// We found an empty message from a previous parse that was performed before
// this field was linked. But it is linked now, so we want to allocate a new
// message of the correct type and promote data into it before continuing.
upb_Message* existing =
UPB_PRIVATE(_upb_TaggedMessagePtr_GetEmptyMessage)(tagged);
upb_Message* promoted = _upb_Decoder_NewSubMessage(d, subs, field, target);
uintptr_t iter = kUpb_Message_UnknownBegin;
upb_StringView unknown;
while (upb_Message_NextUnknown(existing, &unknown, &iter)) {
upb_DecodeStatus status =
upb_Decode(unknown.data, unknown.size, promoted, subl, d->extreg,
d->options, &d->arena);
if (status != kUpb_DecodeStatus_Ok) _upb_Decoder_ErrorJmp(d, status);
}
return promoted;
}
static const char* _upb_Decoder_ReadString(upb_Decoder* d, const char* ptr,
int size, upb_StringView* str) {
const char* str_ptr = ptr;
ptr = upb_EpsCopyInputStream_ReadString(&d->input, &str_ptr, size, &d->arena);
if (!ptr) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
str->data = str_ptr;
str->size = size;
return ptr;
}
UPB_FORCEINLINE
const char* _upb_Decoder_RecurseSubMessage(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTable* subl,
uint32_t expected_end_group) {
if (--d->depth < 0) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_MaxDepthExceeded);
}
ptr = _upb_Decoder_DecodeMessage(d, ptr, submsg, subl);
d->depth++;
if (d->end_group != expected_end_group) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
return ptr;
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeSubMessage(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field,
int size) {
int saved_delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, size);
const upb_MiniTable* subl = _upb_MiniTableSubs_MessageByField(subs, field);
UPB_ASSERT(subl);
ptr = _upb_Decoder_RecurseSubMessage(d, ptr, submsg, subl, DECODE_NOGROUP);
upb_EpsCopyInputStream_PopLimit(&d->input, ptr, saved_delta);
return ptr;
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeGroup(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTable* subl,
uint32_t number) {
if (_upb_Decoder_IsDone(d, &ptr)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
ptr = _upb_Decoder_RecurseSubMessage(d, ptr, submsg, subl, number);
d->end_group = DECODE_NOGROUP;
return ptr;
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeUnknownGroup(upb_Decoder* d, const char* ptr,
uint32_t number) {
return _upb_Decoder_DecodeGroup(d, ptr, NULL, NULL, number);
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeKnownGroup(upb_Decoder* d, const char* ptr,
upb_Message* submsg,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field) {
const upb_MiniTable* subl = _upb_MiniTableSubs_MessageByField(subs, field);
UPB_ASSERT(subl);
return _upb_Decoder_DecodeGroup(d, ptr, submsg, subl,
field->UPB_PRIVATE(number));
}
#define kUpb_Decoder_EncodeVarint32MaxSize 5
static char* upb_Decoder_EncodeVarint32(uint32_t val, char* ptr) {
do {
uint8_t byte = val & 0x7fU;
val >>= 7;
if (val) byte |= 0x80U;
*(ptr++) = byte;
} while (val);
return ptr;
}
UPB_FORCEINLINE
bool _upb_Decoder_CheckEnum(upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableEnum* e,
const upb_MiniTableField* field, wireval* val) {
const uint32_t v = val->uint32_val;
if (UPB_LIKELY(upb_MiniTableEnum_CheckValue(e, v))) return true;
// Unrecognized enum goes into unknown fields.
// For packed fields the tag could be arbitrarily far in the past,
// so we just re-encode the tag and value here.
const uint32_t tag =
((uint32_t)field->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint;
upb_Message* unknown_msg =
field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension ? d->original_msg
: msg;
char buf[2 * kUpb_Decoder_EncodeVarint32MaxSize];
char* end = buf;
end = upb_Decoder_EncodeVarint32(tag, end);
end = upb_Decoder_EncodeVarint32(v, end);
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(unknown_msg, buf, end - buf,
&d->arena, false)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
return false;
}
UPB_NOINLINE
static const char* _upb_Decoder_DecodeEnumArray(
upb_Decoder* d, const char* ptr, upb_Message* msg, upb_Array* arr,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val) {
const upb_MiniTableEnum* e = _upb_MiniTableSubs_EnumByField(subs, field);
if (!_upb_Decoder_CheckEnum(d, ptr, msg, e, field, val)) return ptr;
void* mem = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) * 4, void);
arr->UPB_PRIVATE(size)++;
memcpy(mem, val, 4);
return ptr;
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeFixedPacked(upb_Decoder* d, const char* ptr,
upb_Array* arr, wireval* val,
const upb_MiniTableField* field,
int lg2) {
int mask = (1 << lg2) - 1;
size_t count = val->size >> lg2;
if ((val->size & mask) != 0) {
// Length isn't a round multiple of elem size.
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
_upb_Decoder_Reserve(d, arr, count);
void* mem = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) << lg2, void);
arr->UPB_PRIVATE(size) += count;
// Note: if/when the decoder supports multi-buffer input, we will need to
// handle buffer seams here.
if (upb_IsLittleEndian()) {
ptr = upb_EpsCopyInputStream_Copy(&d->input, ptr, mem, val->size);
} else {
int delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
char* dst = mem;
while (!_upb_Decoder_IsDone(d, &ptr)) {
if (lg2 == 2) {
ptr = upb_WireReader_ReadFixed32(ptr, dst);
dst += 4;
} else {
UPB_ASSERT(lg2 == 3);
ptr = upb_WireReader_ReadFixed64(ptr, dst);
dst += 8;
}
}
upb_EpsCopyInputStream_PopLimit(&d->input, ptr, delta);
}
return ptr;
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeVarintPacked(upb_Decoder* d, const char* ptr,
upb_Array* arr, wireval* val,
const upb_MiniTableField* field,
int lg2) {
int scale = 1 << lg2;
int saved_limit = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
char* out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) << lg2, void);
while (!_upb_Decoder_IsDone(d, &ptr)) {
wireval elem;
ptr = _upb_Decoder_DecodeVarint(d, ptr, &elem.uint64_val);
_upb_Decoder_Munge(field->UPB_PRIVATE(descriptortype), &elem);
if (_upb_Decoder_Reserve(d, arr, 1)) {
out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) << lg2, void);
}
arr->UPB_PRIVATE(size)++;
memcpy(out, &elem, scale);
out += scale;
}
upb_EpsCopyInputStream_PopLimit(&d->input, ptr, saved_limit);
return ptr;
}
UPB_NOINLINE
static const char* _upb_Decoder_DecodeEnumPacked(
upb_Decoder* d, const char* ptr, upb_Message* msg, upb_Array* arr,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val) {
const upb_MiniTableEnum* e = _upb_MiniTableSubs_EnumByField(subs, field);
int saved_limit = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, val->size);
char* out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) * 4, void);
while (!_upb_Decoder_IsDone(d, &ptr)) {
wireval elem;
ptr = _upb_Decoder_DecodeVarint(d, ptr, &elem.uint64_val);
_upb_Decoder_MungeInt32(&elem);
if (!_upb_Decoder_CheckEnum(d, ptr, msg, e, field, &elem)) {
continue;
}
if (_upb_Decoder_Reserve(d, arr, 1)) {
out = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) * 4, void);
}
arr->UPB_PRIVATE(size)++;
memcpy(out, &elem, 4);
out += 4;
}
upb_EpsCopyInputStream_PopLimit(&d->input, ptr, saved_limit);
return ptr;
}
static upb_Array* _upb_Decoder_CreateArray(upb_Decoder* d,
const upb_MiniTableField* field) {
const upb_FieldType field_type = field->UPB_PRIVATE(descriptortype);
const size_t lg2 = UPB_PRIVATE(_upb_FieldType_SizeLg2)(field_type);
upb_Array* ret = UPB_PRIVATE(_upb_Array_New)(&d->arena, 4, lg2);
if (!ret) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
return ret;
}
static const char* _upb_Decoder_DecodeToArray(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val, int op) {
upb_Array** arrp = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void);
upb_Array* arr = *arrp;
void* mem;
if (arr) {
_upb_Decoder_Reserve(d, arr, 1);
} else {
arr = _upb_Decoder_CreateArray(d, field);
*arrp = arr;
}
switch (op) {
case kUpb_DecodeOp_Scalar1Byte:
case kUpb_DecodeOp_Scalar4Byte:
case kUpb_DecodeOp_Scalar8Byte:
/* Append scalar value. */
mem = UPB_PTR_AT(upb_Array_MutableDataPtr(arr),
arr->UPB_PRIVATE(size) << op, void);
arr->UPB_PRIVATE(size)++;
memcpy(mem, val, 1 << op);
return ptr;
case kUpb_DecodeOp_String:
_upb_Decoder_VerifyUtf8(d, ptr, val->size);
/* Fallthrough. */
case kUpb_DecodeOp_Bytes: {
/* Append bytes. */
upb_StringView* str = (upb_StringView*)upb_Array_MutableDataPtr(arr) +
arr->UPB_PRIVATE(size);
arr->UPB_PRIVATE(size)++;
return _upb_Decoder_ReadString(d, ptr, val->size, str);
}
case kUpb_DecodeOp_SubMessage: {
/* Append submessage / group. */
upb_TaggedMessagePtr* target = UPB_PTR_AT(
upb_Array_MutableDataPtr(arr), arr->UPB_PRIVATE(size) * sizeof(void*),
upb_TaggedMessagePtr);
upb_Message* submsg = _upb_Decoder_NewSubMessage(d, subs, field, target);
arr->UPB_PRIVATE(size)++;
if (UPB_UNLIKELY(field->UPB_PRIVATE(descriptortype) ==
kUpb_FieldType_Group)) {
return _upb_Decoder_DecodeKnownGroup(d, ptr, submsg, subs, field);
} else {
return _upb_Decoder_DecodeSubMessage(d, ptr, submsg, subs, field,
val->size);
}
}
case OP_FIXPCK_LG2(2):
case OP_FIXPCK_LG2(3):
return _upb_Decoder_DecodeFixedPacked(d, ptr, arr, val, field,
op - OP_FIXPCK_LG2(0));
case OP_VARPCK_LG2(0):
case OP_VARPCK_LG2(2):
case OP_VARPCK_LG2(3):
return _upb_Decoder_DecodeVarintPacked(d, ptr, arr, val, field,
op - OP_VARPCK_LG2(0));
case kUpb_DecodeOp_Enum:
return _upb_Decoder_DecodeEnumArray(d, ptr, msg, arr, subs, field, val);
case kUpb_DecodeOp_PackedEnum:
return _upb_Decoder_DecodeEnumPacked(d, ptr, msg, arr, subs, field, val);
default:
UPB_UNREACHABLE();
}
}
static upb_Map* _upb_Decoder_CreateMap(upb_Decoder* d,
const upb_MiniTable* entry) {
// Maps descriptor type -> upb map size
static const uint8_t kSizeInMap[] = {
[0] = -1, // invalid descriptor type
[kUpb_FieldType_Double] = 8,
[kUpb_FieldType_Float] = 4,
[kUpb_FieldType_Int64] = 8,
[kUpb_FieldType_UInt64] = 8,
[kUpb_FieldType_Int32] = 4,
[kUpb_FieldType_Fixed64] = 8,
[kUpb_FieldType_Fixed32] = 4,
[kUpb_FieldType_Bool] = 1,
[kUpb_FieldType_String] = UPB_MAPTYPE_STRING,
[kUpb_FieldType_Group] = sizeof(void*),
[kUpb_FieldType_Message] = sizeof(void*),
[kUpb_FieldType_Bytes] = UPB_MAPTYPE_STRING,
[kUpb_FieldType_UInt32] = 4,
[kUpb_FieldType_Enum] = 4,
[kUpb_FieldType_SFixed32] = 4,
[kUpb_FieldType_SFixed64] = 8,
[kUpb_FieldType_SInt32] = 4,
[kUpb_FieldType_SInt64] = 8,
};
const upb_MiniTableField* key_field = &entry->UPB_PRIVATE(fields)[0];
const upb_MiniTableField* val_field = &entry->UPB_PRIVATE(fields)[1];
char key_size = kSizeInMap[key_field->UPB_PRIVATE(descriptortype)];
char val_size = kSizeInMap[val_field->UPB_PRIVATE(descriptortype)];
UPB_ASSERT(key_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntry, k));
UPB_ASSERT(val_field->UPB_PRIVATE(offset) == offsetof(upb_MapEntry, v));
upb_Map* ret = _upb_Map_New(&d->arena, key_size, val_size);
if (!ret) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
return ret;
}
UPB_NOINLINE static void _upb_Decoder_AddMapEntryUnknown(
upb_Decoder* d, upb_Message* msg, const upb_MiniTableField* field,
upb_Message* ent_msg, const upb_MiniTable* entry) {
char* buf;
size_t size;
upb_EncodeStatus status =
upb_Encode(ent_msg, entry, 0, &d->arena, &buf, &size);
if (status != kUpb_EncodeStatus_Ok) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
char delim_buf[2 * kUpb_Decoder_EncodeVarint32MaxSize];
char* delim_end = delim_buf;
uint32_t tag =
((uint32_t)field->UPB_PRIVATE(number) << 3) | kUpb_WireType_Delimited;
delim_end = upb_Decoder_EncodeVarint32(tag, delim_end);
delim_end = upb_Decoder_EncodeVarint32(size, delim_end);
upb_StringView unknown[] = {
{delim_buf, delim_end - delim_buf},
{buf, size},
};
if (!UPB_PRIVATE(_upb_Message_AddUnknownV)(msg, &d->arena, unknown, 2)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}
static const char* _upb_Decoder_DecodeToMap(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val) {
upb_Map** map_p = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), upb_Map*);
upb_Map* map = *map_p;
upb_MapEntry ent;
UPB_ASSERT(upb_MiniTableField_Type(field) == kUpb_FieldType_Message);
const upb_MiniTable* entry = _upb_MiniTableSubs_MessageByField(subs, field);
UPB_ASSERT(entry);
UPB_ASSERT(entry->UPB_PRIVATE(field_count) == 2);
UPB_ASSERT(upb_MiniTableField_IsScalar(&entry->UPB_PRIVATE(fields)[0]));
UPB_ASSERT(upb_MiniTableField_IsScalar(&entry->UPB_PRIVATE(fields)[1]));
if (!map) {
map = _upb_Decoder_CreateMap(d, entry);
*map_p = map;
}
// Parse map entry.
memset(&ent, 0, sizeof(ent));
if (entry->UPB_PRIVATE(fields)[1].UPB_PRIVATE(descriptortype) ==
kUpb_FieldType_Message ||
entry->UPB_PRIVATE(fields)[1].UPB_PRIVATE(descriptortype) ==
kUpb_FieldType_Group) {
// Create proactively to handle the case where it doesn't appear.
upb_TaggedMessagePtr msg;
_upb_Decoder_NewSubMessage(d, entry->UPB_PRIVATE(subs),
&entry->UPB_PRIVATE(fields)[1], &msg);
ent.v.val = upb_value_uintptr(msg);
}
ptr = _upb_Decoder_DecodeSubMessage(d, ptr, &ent.message, subs, field,
val->size);
if (upb_Message_HasUnknown(&ent.message)) {
_upb_Decoder_AddMapEntryUnknown(d, msg, field, &ent.message, entry);
} else {
if (_upb_Map_Insert(map, &ent.k, map->key_size, &ent.v, map->val_size,
&d->arena) == kUpb_MapInsertStatus_OutOfMemory) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}
return ptr;
}
static const char* _upb_Decoder_DecodeToSubMessage(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field,
wireval* val, int op) {
void* mem = UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void);
int type = field->UPB_PRIVATE(descriptortype);
if (UPB_UNLIKELY(op == kUpb_DecodeOp_Enum) &&
!_upb_Decoder_CheckEnum(d, ptr, msg,
_upb_MiniTableSubs_EnumByField(subs, field),
field, val)) {
return ptr;
}
// Set presence if necessary.
if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(field)) {
UPB_PRIVATE(_upb_Message_SetHasbit)(msg, field);
} else if (upb_MiniTableField_IsInOneof(field)) {
// Oneof case
uint32_t* oneof_case = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, field);
if (op == kUpb_DecodeOp_SubMessage &&
*oneof_case != field->UPB_PRIVATE(number)) {
memset(mem, 0, sizeof(void*));
}
*oneof_case = field->UPB_PRIVATE(number);
}
// Store into message.
switch (op) {
case kUpb_DecodeOp_SubMessage: {
upb_TaggedMessagePtr* submsgp = mem;
upb_Message* submsg;
if (*submsgp) {
submsg = _upb_Decoder_ReuseSubMessage(d, subs, field, submsgp);
} else {
submsg = _upb_Decoder_NewSubMessage(d, subs, field, submsgp);
}
if (UPB_UNLIKELY(type == kUpb_FieldType_Group)) {
ptr = _upb_Decoder_DecodeKnownGroup(d, ptr, submsg, subs, field);
} else {
ptr = _upb_Decoder_DecodeSubMessage(d, ptr, submsg, subs, field,
val->size);
}
break;
}
case kUpb_DecodeOp_String:
_upb_Decoder_VerifyUtf8(d, ptr, val->size);
/* Fallthrough. */
case kUpb_DecodeOp_Bytes:
return _upb_Decoder_ReadString(d, ptr, val->size, mem);
case kUpb_DecodeOp_Scalar8Byte:
memcpy(mem, val, 8);
break;
case kUpb_DecodeOp_Enum:
case kUpb_DecodeOp_Scalar4Byte:
memcpy(mem, val, 4);
break;
case kUpb_DecodeOp_Scalar1Byte:
memcpy(mem, val, 1);
break;
default:
UPB_UNREACHABLE();
}
return ptr;
}
UPB_NOINLINE
const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
const upb_Message* msg,
const upb_MiniTable* m) {
UPB_ASSERT(m->UPB_PRIVATE(required_count));
if (UPB_UNLIKELY(d->options & kUpb_DecodeOption_CheckRequired)) {
d->missing_required =
!UPB_PRIVATE(_upb_Message_IsInitializedShallow)(msg, m);
}
return ptr;
}
UPB_FORCEINLINE
bool _upb_Decoder_TryFastDispatch(upb_Decoder* d, const char** ptr,
upb_Message* msg, const upb_MiniTable* m) {
#if UPB_FASTTABLE
if (m && m->UPB_PRIVATE(table_mask) != (unsigned char)-1) {
uint16_t tag = _upb_FastDecoder_LoadTag(*ptr);
intptr_t table = decode_totable(m);
*ptr = _upb_FastDecoder_TagDispatch(d, *ptr, msg, table, 0, tag);
return true;
}
#endif
return false;
}
static const char* upb_Decoder_SkipField(upb_Decoder* d, const char* ptr,
uint32_t tag) {
int field_number = tag >> 3;
int wire_type = tag & 7;
switch (wire_type) {
case kUpb_WireType_Varint: {
uint64_t val;
return _upb_Decoder_DecodeVarint(d, ptr, &val);
}
case kUpb_WireType_64Bit:
return ptr + 8;
case kUpb_WireType_32Bit:
return ptr + 4;
case kUpb_WireType_Delimited: {
uint32_t size;
ptr = upb_Decoder_DecodeSize(d, ptr, &size);
return ptr + size;
}
case kUpb_WireType_StartGroup:
return _upb_Decoder_DecodeUnknownGroup(d, ptr, field_number);
default:
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
}
enum {
kStartItemTag = ((kUpb_MsgSet_Item << 3) | kUpb_WireType_StartGroup),
kEndItemTag = ((kUpb_MsgSet_Item << 3) | kUpb_WireType_EndGroup),
kTypeIdTag = ((kUpb_MsgSet_TypeId << 3) | kUpb_WireType_Varint),
kMessageTag = ((kUpb_MsgSet_Message << 3) | kUpb_WireType_Delimited),
};
static void upb_Decoder_AddKnownMessageSetItem(
upb_Decoder* d, upb_Message* msg, const upb_MiniTableExtension* item_mt,
const char* data, uint32_t size) {
upb_Extension* ext =
UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, item_mt, &d->arena);
if (UPB_UNLIKELY(!ext)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
upb_Message* submsg = _upb_Decoder_NewSubMessage2(
d, ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg),
&ext->ext->UPB_PRIVATE(field), &ext->data.tagged_msg_val);
upb_DecodeStatus status = upb_Decode(
data, size, submsg, upb_MiniTableExtension_GetSubMessage(item_mt),
d->extreg, d->options, &d->arena);
if (status != kUpb_DecodeStatus_Ok) _upb_Decoder_ErrorJmp(d, status);
}
static void upb_Decoder_AddUnknownMessageSetItem(upb_Decoder* d,
upb_Message* msg,
uint32_t type_id,
const char* message_data,
uint32_t message_size) {
char buf[6 * kUpb_Decoder_EncodeVarint32MaxSize];
char* ptr = buf;
ptr = upb_Decoder_EncodeVarint32(kStartItemTag, ptr);
ptr = upb_Decoder_EncodeVarint32(kTypeIdTag, ptr);
ptr = upb_Decoder_EncodeVarint32(type_id, ptr);
ptr = upb_Decoder_EncodeVarint32(kMessageTag, ptr);
ptr = upb_Decoder_EncodeVarint32(message_size, ptr);
char* split = ptr;
ptr = upb_Decoder_EncodeVarint32(kEndItemTag, ptr);
char* end = ptr;
upb_StringView unknown[] = {
{buf, split - buf},
{message_data, message_size},
{split, end - split},
};
if (!UPB_PRIVATE(_upb_Message_AddUnknownV)(msg, &d->arena, unknown, 3)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
}
static void upb_Decoder_AddMessageSetItem(upb_Decoder* d, upb_Message* msg,
const upb_MiniTable* t,
uint32_t type_id, const char* data,
uint32_t size) {
const upb_MiniTableExtension* item_mt =
upb_ExtensionRegistry_Lookup(d->extreg, t, type_id);
if (item_mt) {
upb_Decoder_AddKnownMessageSetItem(d, msg, item_mt, data, size);
} else {
upb_Decoder_AddUnknownMessageSetItem(d, msg, type_id, data, size);
}
}
static const char* upb_Decoder_DecodeMessageSetItem(
upb_Decoder* d, const char* ptr, upb_Message* msg,
const upb_MiniTable* layout) {
uint32_t type_id = 0;
upb_StringView preserved = {NULL, 0};
typedef enum {
kUpb_HaveId = 1 << 0,
kUpb_HavePayload = 1 << 1,
} StateMask;
StateMask state_mask = 0;
while (!_upb_Decoder_IsDone(d, &ptr)) {
uint32_t tag;
ptr = _upb_Decoder_DecodeTag(d, ptr, &tag);
switch (tag) {
case kEndItemTag:
return ptr;
case kTypeIdTag: {
uint64_t tmp;
ptr = _upb_Decoder_DecodeVarint(d, ptr, &tmp);
if (state_mask & kUpb_HaveId) break; // Ignore dup.
state_mask |= kUpb_HaveId;
type_id = tmp;
if (state_mask & kUpb_HavePayload) {
upb_Decoder_AddMessageSetItem(d, msg, layout, type_id, preserved.data,
preserved.size);
}
break;
}
case kMessageTag: {
uint32_t size;
ptr = upb_Decoder_DecodeSize(d, ptr, &size);
const char* data = ptr;
ptr += size;
if (state_mask & kUpb_HavePayload) break; // Ignore dup.
state_mask |= kUpb_HavePayload;
if (state_mask & kUpb_HaveId) {
upb_Decoder_AddMessageSetItem(d, msg, layout, type_id, data, size);
} else {
// Out of order, we must preserve the payload.
preserved.data = data;
preserved.size = size;
}
break;
}
default:
// We do not preserve unexpected fields inside a message set item.
ptr = upb_Decoder_SkipField(d, ptr, tag);
break;
}
}
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
static upb_MiniTableField upb_Decoder_FieldNotFoundField = {
0, 0, 0, 0, kUpb_FakeFieldType_FieldNotFound, 0};
UPB_NOINLINE const upb_MiniTableField* _upb_Decoder_FindExtensionField(
upb_Decoder* d, const upb_MiniTable* t, uint32_t field_number, int ext_mode,
int wire_type) {
// Treat a message set as an extendable message if it is a delimited field.
// This provides compatibility with encoders that are unaware of message
// sets and serialize them as normal extensions.
if (ext_mode == kUpb_ExtMode_Extendable ||
(ext_mode == kUpb_ExtMode_IsMessageSet &&
wire_type == kUpb_WireType_Delimited)) {
const upb_MiniTableExtension* ext =
upb_ExtensionRegistry_Lookup(d->extreg, t, field_number);
if (ext) return &ext->UPB_PRIVATE(field);
} else if (ext_mode == kUpb_ExtMode_IsMessageSet) {
if (field_number == kUpb_MsgSet_Item) {
static upb_MiniTableField item = {
0, 0, 0, 0, kUpb_FakeFieldType_MessageSetItem, 0};
return &item;
}
}
return &upb_Decoder_FieldNotFoundField;
}
static const upb_MiniTableField* _upb_Decoder_FindField(
upb_Decoder* d, const upb_MiniTable* t, uint32_t field_number,
uint32_t* last_field_index, int wire_type) {
if (t == NULL) return &upb_Decoder_FieldNotFoundField;
uint32_t idx = ((uint32_t)field_number) - 1; // 0 wraps to UINT32_MAX
if (idx < t->UPB_PRIVATE(dense_below)) {
// Fastest case: index into dense fields, and don't update last_field_index.
return &t->UPB_PRIVATE(fields)[idx];
}
uint32_t field_count = t->UPB_PRIVATE(field_count);
if (t->UPB_PRIVATE(dense_below) < field_count) {
// Linear search non-dense fields. Resume scanning from last_field_index
// since fields are usually in order.
idx = *last_field_index;
uint32_t candidate;
do {
candidate = t->UPB_PRIVATE(fields)[idx].UPB_PRIVATE(number);
if (candidate == field_number) {
goto found;
}
} while (++idx < field_count);
if (UPB_LIKELY(field_number > candidate)) {
// The field number we encountered is larger than any of our known fields,
// so it's likely that subsequent ones will be too.
*last_field_index = idx - 1;
} else {
// Fields not in tag order - scan from beginning
for (idx = t->UPB_PRIVATE(dense_below); idx < *last_field_index; idx++) {
if (t->UPB_PRIVATE(fields)[idx].UPB_PRIVATE(number) == field_number) {
goto found;
}
}
}
}
if (d->extreg && t->UPB_PRIVATE(ext)) {
return _upb_Decoder_FindExtensionField(d, t, field_number,
t->UPB_PRIVATE(ext), wire_type);
}
return &upb_Decoder_FieldNotFoundField; // Unknown field.
found:
UPB_ASSERT(t->UPB_PRIVATE(fields)[idx].UPB_PRIVATE(number) == field_number);
*last_field_index = idx;
return &t->UPB_PRIVATE(fields)[idx];
}
static int _upb_Decoder_GetVarintOp(const upb_MiniTableField* field) {
static const int8_t kVarintOps[] = {
[kUpb_FakeFieldType_FieldNotFound] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Double] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Float] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Int64] = kUpb_DecodeOp_Scalar8Byte,
[kUpb_FieldType_UInt64] = kUpb_DecodeOp_Scalar8Byte,
[kUpb_FieldType_Int32] = kUpb_DecodeOp_Scalar4Byte,
[kUpb_FieldType_Fixed64] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Fixed32] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Bool] = kUpb_DecodeOp_Scalar1Byte,
[kUpb_FieldType_String] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Group] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Message] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Bytes] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_UInt32] = kUpb_DecodeOp_Scalar4Byte,
[kUpb_FieldType_Enum] = kUpb_DecodeOp_Enum,
[kUpb_FieldType_SFixed32] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_SFixed64] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_SInt32] = kUpb_DecodeOp_Scalar4Byte,
[kUpb_FieldType_SInt64] = kUpb_DecodeOp_Scalar8Byte,
[kUpb_FakeFieldType_MessageSetItem] = kUpb_DecodeOp_UnknownField,
};
return kVarintOps[field->UPB_PRIVATE(descriptortype)];
}
UPB_FORCEINLINE
void _upb_Decoder_CheckUnlinked(upb_Decoder* d, const upb_MiniTable* mt,
const upb_MiniTableField* field, int* op) {
// If sub-message is not linked, treat as unknown.
if (field->UPB_PRIVATE(mode) & kUpb_LabelFlags_IsExtension) return;
const upb_MiniTable* mt_sub =
_upb_MiniTableSubs_MessageByField(mt->UPB_PRIVATE(subs), field);
if ((d->options & kUpb_DecodeOption_ExperimentalAllowUnlinked) ||
!UPB_PRIVATE(_upb_MiniTable_IsEmpty)(mt_sub)) {
return;
}
#ifndef NDEBUG
const upb_MiniTableField* oneof = upb_MiniTable_GetOneof(mt, field);
if (oneof) {
// All other members of the oneof must be message fields that are also
// unlinked.
do {
UPB_ASSERT(upb_MiniTableField_CType(oneof) == kUpb_CType_Message);
const upb_MiniTable* oneof_sub =
*mt->UPB_PRIVATE(subs)[oneof->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(
submsg);
UPB_ASSERT(!oneof_sub);
} while (upb_MiniTable_NextOneofField(mt, &oneof));
}
#endif // NDEBUG
*op = kUpb_DecodeOp_UnknownField;
}
UPB_FORCEINLINE
void _upb_Decoder_MaybeVerifyUtf8(upb_Decoder* d,
const upb_MiniTableField* field, int* op) {
if ((field->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsAlternate) &&
UPB_UNLIKELY(d->options & kUpb_DecodeOption_AlwaysValidateUtf8))
*op = kUpb_DecodeOp_String;
}
static int _upb_Decoder_GetDelimitedOp(upb_Decoder* d, const upb_MiniTable* mt,
const upb_MiniTableField* field) {
enum { kRepeatedBase = 19 };
static const int8_t kDelimitedOps[] = {
// For non-repeated field type.
[kUpb_FakeFieldType_FieldNotFound] =
kUpb_DecodeOp_UnknownField, // Field not found.
[kUpb_FieldType_Double] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Float] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Int64] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_UInt64] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Int32] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Fixed64] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Fixed32] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Bool] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_String] = kUpb_DecodeOp_String,
[kUpb_FieldType_Group] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Message] = kUpb_DecodeOp_SubMessage,
[kUpb_FieldType_Bytes] = kUpb_DecodeOp_Bytes,
[kUpb_FieldType_UInt32] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_Enum] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_SFixed32] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_SFixed64] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_SInt32] = kUpb_DecodeOp_UnknownField,
[kUpb_FieldType_SInt64] = kUpb_DecodeOp_UnknownField,
[kUpb_FakeFieldType_MessageSetItem] = kUpb_DecodeOp_SubMessage,
// For repeated field type.
[kRepeatedBase + kUpb_FieldType_Double] = OP_FIXPCK_LG2(3),
[kRepeatedBase + kUpb_FieldType_Float] = OP_FIXPCK_LG2(2),
[kRepeatedBase + kUpb_FieldType_Int64] = OP_VARPCK_LG2(3),
[kRepeatedBase + kUpb_FieldType_UInt64] = OP_VARPCK_LG2(3),
[kRepeatedBase + kUpb_FieldType_Int32] = OP_VARPCK_LG2(2),
[kRepeatedBase + kUpb_FieldType_Fixed64] = OP_FIXPCK_LG2(3),
[kRepeatedBase + kUpb_FieldType_Fixed32] = OP_FIXPCK_LG2(2),
[kRepeatedBase + kUpb_FieldType_Bool] = OP_VARPCK_LG2(0),
[kRepeatedBase + kUpb_FieldType_String] = kUpb_DecodeOp_String,
[kRepeatedBase + kUpb_FieldType_Group] = kUpb_DecodeOp_SubMessage,
[kRepeatedBase + kUpb_FieldType_Message] = kUpb_DecodeOp_SubMessage,
[kRepeatedBase + kUpb_FieldType_Bytes] = kUpb_DecodeOp_Bytes,
[kRepeatedBase + kUpb_FieldType_UInt32] = OP_VARPCK_LG2(2),
[kRepeatedBase + kUpb_FieldType_Enum] = kUpb_DecodeOp_PackedEnum,
[kRepeatedBase + kUpb_FieldType_SFixed32] = OP_FIXPCK_LG2(2),
[kRepeatedBase + kUpb_FieldType_SFixed64] = OP_FIXPCK_LG2(3),
[kRepeatedBase + kUpb_FieldType_SInt32] = OP_VARPCK_LG2(2),
[kRepeatedBase + kUpb_FieldType_SInt64] = OP_VARPCK_LG2(3),
// Omitting kUpb_FakeFieldType_MessageSetItem, because we never emit a
// repeated msgset type
};
int ndx = field->UPB_PRIVATE(descriptortype);
if (upb_MiniTableField_IsArray(field)) ndx += kRepeatedBase;
int op = kDelimitedOps[ndx];
if (op == kUpb_DecodeOp_SubMessage) {
_upb_Decoder_CheckUnlinked(d, mt, field, &op);
} else if (op == kUpb_DecodeOp_Bytes) {
_upb_Decoder_MaybeVerifyUtf8(d, field, &op);
}
return op;
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeWireValue(upb_Decoder* d, const char* ptr,
const upb_MiniTable* mt,
const upb_MiniTableField* field,
int wire_type, wireval* val, int* op) {
static const unsigned kFixed32OkMask = (1 << kUpb_FieldType_Float) |
(1 << kUpb_FieldType_Fixed32) |
(1 << kUpb_FieldType_SFixed32);
static const unsigned kFixed64OkMask = (1 << kUpb_FieldType_Double) |
(1 << kUpb_FieldType_Fixed64) |
(1 << kUpb_FieldType_SFixed64);
switch (wire_type) {
case kUpb_WireType_Varint:
ptr = _upb_Decoder_DecodeVarint(d, ptr, &val->uint64_val);
*op = _upb_Decoder_GetVarintOp(field);
_upb_Decoder_Munge(field->UPB_PRIVATE(descriptortype), val);
return ptr;
case kUpb_WireType_32Bit:
*op = kUpb_DecodeOp_Scalar4Byte;
if (((1 << field->UPB_PRIVATE(descriptortype)) & kFixed32OkMask) == 0) {
*op = kUpb_DecodeOp_UnknownField;
}
return upb_WireReader_ReadFixed32(ptr, &val->uint32_val);
case kUpb_WireType_64Bit:
*op = kUpb_DecodeOp_Scalar8Byte;
if (((1 << field->UPB_PRIVATE(descriptortype)) & kFixed64OkMask) == 0) {
*op = kUpb_DecodeOp_UnknownField;
}
return upb_WireReader_ReadFixed64(ptr, &val->uint64_val);
case kUpb_WireType_Delimited:
ptr = upb_Decoder_DecodeSize(d, ptr, &val->size);
*op = _upb_Decoder_GetDelimitedOp(d, mt, field);
return ptr;
case kUpb_WireType_StartGroup:
val->uint32_val = field->UPB_PRIVATE(number);
if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group) {
*op = kUpb_DecodeOp_SubMessage;
_upb_Decoder_CheckUnlinked(d, mt, field, op);
} else if (field->UPB_PRIVATE(descriptortype) ==
kUpb_FakeFieldType_MessageSetItem) {
*op = kUpb_DecodeOp_MessageSetItem;
} else {
*op = kUpb_DecodeOp_UnknownField;
}
return ptr;
default:
break;
}
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
}
UPB_FORCEINLINE
const char* _upb_Decoder_DecodeKnownField(upb_Decoder* d, const char* ptr,
upb_Message* msg,
const upb_MiniTable* layout,
const upb_MiniTableField* field,
int op, wireval* val) {
const upb_MiniTableSubInternal* subs = layout->UPB_PRIVATE(subs);
uint8_t mode = field->UPB_PRIVATE(mode);
upb_MiniTableSubInternal ext_sub;
if (UPB_UNLIKELY(mode & kUpb_LabelFlags_IsExtension)) {
const upb_MiniTableExtension* ext_layout =
(const upb_MiniTableExtension*)field;
upb_Extension* ext = UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
msg, ext_layout, &d->arena);
if (UPB_UNLIKELY(!ext)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
d->original_msg = msg;
msg = &ext->data.UPB_PRIVATE(ext_msg_val);
if (upb_MiniTableField_IsSubMessage(&ext->ext->UPB_PRIVATE(field))) {
ext_sub.UPB_PRIVATE(submsg) =
&ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg);
} else {
ext_sub.UPB_PRIVATE(subenum) =
ext->ext->UPB_PRIVATE(sub).UPB_PRIVATE(subenum);
}
subs = &ext_sub;
}
switch (mode & kUpb_FieldMode_Mask) {
case kUpb_FieldMode_Array:
return _upb_Decoder_DecodeToArray(d, ptr, msg, subs, field, val, op);
case kUpb_FieldMode_Map:
return _upb_Decoder_DecodeToMap(d, ptr, msg, subs, field, val);
case kUpb_FieldMode_Scalar:
return _upb_Decoder_DecodeToSubMessage(d, ptr, msg, subs, field, val, op);
default:
UPB_UNREACHABLE();
}
}
static const char* _upb_Decoder_DecodeUnknownField(upb_Decoder* d,
const char* ptr,
upb_Message* msg,
int field_number,
int wire_type, wireval val) {
if (field_number == 0) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
// Since unknown fields are the uncommon case, we do a little extra work here
// to walk backwards through the buffer to find the field start. This frees
// up a register in the fast paths (when the field is known), which leads to
// significant speedups in benchmarks. Note that ptr may point into the slop
// space, beyond the normal end of the input buffer.
const char* start = ptr;
if (wire_type == kUpb_WireType_Delimited) ptr += val.size;
if (msg) {
switch (wire_type) {
case kUpb_WireType_Varint:
case kUpb_WireType_Delimited:
// Skip the last byte
start--;
// Skip bytes until we encounter the final byte of the tag varint.
while (start[-1] & 0x80) start--;
break;
case kUpb_WireType_32Bit:
start -= 4;
break;
case kUpb_WireType_64Bit:
start -= 8;
break;
default:
break;
}
assert(start == d->debug_valstart);
{
// The varint parser does not enforce that integers are encoded with their
// minimum size; for example the value 1 could be encoded with three
// bytes: 0x81, 0x80, 0x00. These unnecessary trailing zeroes mean that we
// cannot skip backwards by the minimum encoded size of the tag; and
// unlike the loop for delimited or varint fields, we can't stop at a
// sentinel value because anything can precede a tag. Instead, parse back
// one byte at a time until we read the same tag value that was parsed
// earlier.
uint32_t tag = ((uint32_t)field_number << 3) | wire_type;
uint32_t seen = 0;
do {
start--;
seen <<= 7;
seen |= *start & 0x7f;
} while (seen != tag);
}
assert(start == d->debug_tagstart);
const char* input_start =
upb_EpsCopyInputStream_GetInputPtr(&d->input, start);
if (wire_type == kUpb_WireType_StartGroup) {
ptr = _upb_Decoder_DecodeUnknownGroup(d, ptr, field_number);
}
// Normally, bounds checks for fixed or varint fields are performed after
// the field is parsed; it's OK for the field to overrun the end of the
// buffer, because it'll just read into slop space. However, because this
// path reads bytes from the input buffer rather than the patch buffer,
// bounds checks are needed before adding the unknown field.
_upb_Decoder_IsDone(d, &ptr);
const char* input_ptr = upb_EpsCopyInputStream_GetInputPtr(&d->input, ptr);
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(msg, input_start,
input_ptr - input_start,
&d->arena, d->input.aliasing)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
} else if (wire_type == kUpb_WireType_StartGroup) {
ptr = _upb_Decoder_DecodeUnknownGroup(d, ptr, field_number);
}
return ptr;
}
UPB_NOINLINE
static const char* _upb_Decoder_DecodeMessage(upb_Decoder* d, const char* ptr,
upb_Message* msg,
const upb_MiniTable* layout) {
uint32_t last_field_index = 0;
#if UPB_FASTTABLE
// The first time we want to skip fast dispatch, because we may have just been
// invoked by the fast parser to handle a case that it bailed on.
if (!_upb_Decoder_IsDone(d, &ptr)) goto nofast;
#endif
while (!_upb_Decoder_IsDone(d, &ptr)) {
uint32_t tag;
const upb_MiniTableField* field;
int field_number;
int wire_type;
wireval val;
int op;
if (_upb_Decoder_TryFastDispatch(d, &ptr, msg, layout)) break;
#if UPB_FASTTABLE
nofast:
#endif
#ifndef NDEBUG
d->debug_tagstart = ptr;
#endif
UPB_ASSERT(ptr < d->input.limit_ptr);
ptr = _upb_Decoder_DecodeTag(d, ptr, &tag);
field_number = tag >> 3;
wire_type = tag & 7;
#ifndef NDEBUG
d->debug_valstart = ptr;
#endif
if (wire_type == kUpb_WireType_EndGroup) {
d->end_group = field_number;
return ptr;
}
field = _upb_Decoder_FindField(d, layout, field_number, &last_field_index,
wire_type);
ptr = _upb_Decoder_DecodeWireValue(d, ptr, layout, field, wire_type, &val,
&op);
if (op >= 0) {
ptr = _upb_Decoder_DecodeKnownField(d, ptr, msg, layout, field, op, &val);
} else {
switch (op) {
case kUpb_DecodeOp_UnknownField:
ptr = _upb_Decoder_DecodeUnknownField(d, ptr, msg, field_number,
wire_type, val);
break;
case kUpb_DecodeOp_MessageSetItem:
ptr = upb_Decoder_DecodeMessageSetItem(d, ptr, msg, layout);
break;
}
}
}
return UPB_UNLIKELY(layout && layout->UPB_PRIVATE(required_count))
? _upb_Decoder_CheckRequired(d, ptr, msg, layout)
: ptr;
}
const char* _upb_FastDecoder_DecodeGeneric(struct upb_Decoder* d,
const char* ptr, upb_Message* msg,
intptr_t table, uint64_t hasbits,
uint64_t data) {
(void)data;
*(uint32_t*)msg |= hasbits;
return _upb_Decoder_DecodeMessage(d, ptr, msg, decode_totablep(table));
}
static upb_DecodeStatus _upb_Decoder_DecodeTop(struct upb_Decoder* d,
const char* buf,
upb_Message* msg,
const upb_MiniTable* m) {
if (!_upb_Decoder_TryFastDispatch(d, &buf, msg, m)) {
_upb_Decoder_DecodeMessage(d, buf, msg, m);
}
if (d->end_group != DECODE_NOGROUP) return kUpb_DecodeStatus_Malformed;
if (d->missing_required) return kUpb_DecodeStatus_MissingRequired;
return kUpb_DecodeStatus_Ok;
}
UPB_NOINLINE
const char* _upb_Decoder_IsDoneFallback(upb_EpsCopyInputStream* e,
const char* ptr, int overrun) {
return _upb_EpsCopyInputStream_IsDoneFallbackInline(
e, ptr, overrun, _upb_Decoder_BufferFlipCallback);
}
static upb_DecodeStatus upb_Decoder_Decode(upb_Decoder* const decoder,
const char* const buf,
upb_Message* const msg,
const upb_MiniTable* const m,
upb_Arena* const arena) {
if (UPB_SETJMP(decoder->err) == 0) {
decoder->status = _upb_Decoder_DecodeTop(decoder, buf, msg, m);
} else {
UPB_ASSERT(decoder->status != kUpb_DecodeStatus_Ok);
}
UPB_PRIVATE(_upb_Arena_SwapOut)(arena, &decoder->arena);
return decoder->status;
}
uint16_t upb_DecodeOptions_GetEffectiveMaxDepth(uint32_t options) {
uint16_t max_depth = upb_DecodeOptions_GetMaxDepth(options);
return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit;
}
upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg,
const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg, int options,
upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Decoder decoder;
upb_EpsCopyInputStream_Init(&decoder.input, &buf, size,
options & kUpb_DecodeOption_AliasString);
decoder.extreg = extreg;
decoder.depth = upb_DecodeOptions_GetEffectiveMaxDepth(options);
decoder.end_group = DECODE_NOGROUP;
decoder.options = (uint16_t)options;
decoder.missing_required = false;
decoder.status = kUpb_DecodeStatus_Ok;
// Violating the encapsulation of the arena for performance reasons.
// This is a temporary arena that we swap into and swap out of when we are
// done. The temporary arena only needs to be able to handle allocation,
// not fuse or free, so it does not need many of the members to be initialized
// (particularly parent_or_count).
UPB_PRIVATE(_upb_Arena_SwapIn)(&decoder.arena, arena);
return upb_Decoder_Decode(&decoder, buf, msg, mt, arena);
}
upb_DecodeStatus upb_DecodeLengthPrefixed(const char* buf, size_t size,
upb_Message* msg,
size_t* num_bytes_read,
const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg,
int options, upb_Arena* arena) {
// To avoid needing to make a Decoder just to decode the initial length,
// hand-decode the leading varint for the message length here.
uint64_t msg_len = 0;
for (size_t i = 0;; ++i) {
if (i >= size || i > 9) {
return kUpb_DecodeStatus_Malformed;
}
uint64_t b = *buf;
buf++;
msg_len += (b & 0x7f) << (i * 7);
if ((b & 0x80) == 0) {
*num_bytes_read = i + 1 + msg_len;
break;
}
}
// If the total number of bytes we would read (= the bytes from the varint
// plus however many bytes that varint says we should read) is larger then the
// input buffer then error as malformed.
if (*num_bytes_read > size) {
return kUpb_DecodeStatus_Malformed;
}
if (msg_len > INT32_MAX) {
return kUpb_DecodeStatus_Malformed;
}
return upb_Decode(buf, msg_len, msg, mt, extreg, options, arena);
}
const char* upb_DecodeStatus_String(upb_DecodeStatus status) {
switch (status) {
case kUpb_DecodeStatus_Ok:
return "Ok";
case kUpb_DecodeStatus_Malformed:
return "Wire format was corrupt";
case kUpb_DecodeStatus_OutOfMemory:
return "Arena alloc failed";
case kUpb_DecodeStatus_BadUtf8:
return "String field had bad UTF-8";
case kUpb_DecodeStatus_MaxDepthExceeded:
return "Exceeded upb_DecodeOptions_MaxDepth";
case kUpb_DecodeStatus_MissingRequired:
return "Missing required field";
case kUpb_DecodeStatus_UnlinkedSubMessage:
return "Unlinked sub-message field was present";
default:
return "Unknown decode status";
}
}
#undef OP_FIXPCK_LG2
#undef OP_VARPCK_LG2
// We encode backwards, to avoid pre-computing lengths (one-pass encode).
#include <setjmp.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Must be last.
// Returns the MiniTable corresponding to a given MiniTableField
// from an array of MiniTableSubs.
static const upb_MiniTable* _upb_Encoder_GetSubMiniTable(
const upb_MiniTableSubInternal* subs, const upb_MiniTableField* field) {
return *subs[field->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(submsg);
}
#define UPB_PB_VARINT_MAX_LEN 10
UPB_NOINLINE
static size_t encode_varint64(uint64_t val, char* buf) {
size_t i = 0;
do {
uint8_t byte = val & 0x7fU;
val >>= 7;
if (val) byte |= 0x80U;
buf[i++] = byte;
} while (val);
return i;
}
static uint32_t encode_zz32(int32_t n) {
return ((uint32_t)n << 1) ^ (n >> 31);
}
static uint64_t encode_zz64(int64_t n) {
return ((uint64_t)n << 1) ^ (n >> 63);
}
typedef struct {
upb_EncodeStatus status;
jmp_buf err;
upb_Arena* arena;
char *buf, *ptr, *limit;
int options;
int depth;
_upb_mapsorter sorter;
} upb_encstate;
static size_t upb_roundup_pow2(size_t bytes) {
size_t ret = 128;
while (ret < bytes) {
ret *= 2;
}
return ret;
}
UPB_NORETURN static void encode_err(upb_encstate* e, upb_EncodeStatus s) {
UPB_ASSERT(s != kUpb_EncodeStatus_Ok);
e->status = s;
UPB_LONGJMP(e->err, 1);
}
UPB_NOINLINE
static void encode_growbuffer(upb_encstate* e, size_t bytes) {
size_t old_size = e->limit - e->buf;
size_t needed_size = bytes + (e->limit - e->ptr);
size_t new_size = upb_roundup_pow2(needed_size);
char* new_buf = upb_Arena_Realloc(e->arena, e->buf, old_size, new_size);
if (!new_buf) encode_err(e, kUpb_EncodeStatus_OutOfMemory);
// We want previous data at the end, realloc() put it at the beginning.
// TODO: This is somewhat inefficient since we are copying twice.
// Maybe create a realloc() that copies to the end of the new buffer?
if (old_size > 0) {
memmove(new_buf + new_size - old_size, new_buf, old_size);
}
e->buf = new_buf;
e->limit = new_buf + new_size;
e->ptr = new_buf + new_size - needed_size;
}
/* Call to ensure that at least "bytes" bytes are available for writing at
* e->ptr. Returns false if the bytes could not be allocated. */
UPB_FORCEINLINE
void encode_reserve(upb_encstate* e, size_t bytes) {
if ((size_t)(e->ptr - e->buf) < bytes) {
encode_growbuffer(e, bytes);
return;
}
e->ptr -= bytes;
}
/* Writes the given bytes to the buffer, handling reserve/advance. */
static void encode_bytes(upb_encstate* e, const void* data, size_t len) {
if (len == 0) return; /* memcpy() with zero size is UB */
encode_reserve(e, len);
memcpy(e->ptr, data, len);
}
static void encode_fixed64(upb_encstate* e, uint64_t val) {
val = upb_BigEndian64(val);
encode_bytes(e, &val, sizeof(uint64_t));
}
static void encode_fixed32(upb_encstate* e, uint32_t val) {
val = upb_BigEndian32(val);
encode_bytes(e, &val, sizeof(uint32_t));
}
UPB_NOINLINE
static void encode_longvarint(upb_encstate* e, uint64_t val) {
size_t len;
char* start;
encode_reserve(e, UPB_PB_VARINT_MAX_LEN);
len = encode_varint64(val, e->ptr);
start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
memmove(start, e->ptr, len);
e->ptr = start;
}
UPB_FORCEINLINE
void encode_varint(upb_encstate* e, uint64_t val) {
if (val < 128 && e->ptr != e->buf) {
--e->ptr;
*e->ptr = val;
} else {
encode_longvarint(e, val);
}
}
static void encode_double(upb_encstate* e, double d) {
uint64_t u64;
UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
memcpy(&u64, &d, sizeof(uint64_t));
encode_fixed64(e, u64);
}
static void encode_float(upb_encstate* e, float d) {
uint32_t u32;
UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
memcpy(&u32, &d, sizeof(uint32_t));
encode_fixed32(e, u32);
}
static void encode_tag(upb_encstate* e, uint32_t field_number,
uint8_t wire_type) {
encode_varint(e, (field_number << 3) | wire_type);
}
static void encode_fixedarray(upb_encstate* e, const upb_Array* arr,
size_t elem_size, uint32_t tag) {
size_t bytes = upb_Array_Size(arr) * elem_size;
const char* data = upb_Array_DataPtr(arr);
const char* ptr = data + bytes - elem_size;
if (tag || !upb_IsLittleEndian()) {
while (true) {
if (elem_size == 4) {
uint32_t val;
memcpy(&val, ptr, sizeof(val));
val = upb_BigEndian32(val);
encode_bytes(e, &val, elem_size);
} else {
UPB_ASSERT(elem_size == 8);
uint64_t val;
memcpy(&val, ptr, sizeof(val));
val = upb_BigEndian64(val);
encode_bytes(e, &val, elem_size);
}
if (tag) encode_varint(e, tag);
if (ptr == data) break;
ptr -= elem_size;
}
} else {
encode_bytes(e, data, bytes);
}
}
static void encode_message(upb_encstate* e, const upb_Message* msg,
const upb_MiniTable* m, size_t* size);
static void encode_TaggedMessagePtr(upb_encstate* e,
upb_TaggedMessagePtr tagged,
const upb_MiniTable* m, size_t* size) {
if (upb_TaggedMessagePtr_IsEmpty(tagged)) {
m = UPB_PRIVATE(_upb_MiniTable_Empty)();
}
encode_message(e, UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(tagged), m,
size);
}
static void encode_scalar(upb_encstate* e, const void* _field_mem,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* f) {
const char* field_mem = _field_mem;
int wire_type;
#define CASE(ctype, type, wtype, encodeval) \
{ \
ctype val = *(ctype*)field_mem; \
encode_##type(e, encodeval); \
wire_type = wtype; \
break; \
}
switch (f->UPB_PRIVATE(descriptortype)) {
case kUpb_FieldType_Double:
CASE(double, double, kUpb_WireType_64Bit, val);
case kUpb_FieldType_Float:
CASE(float, float, kUpb_WireType_32Bit, val);
case kUpb_FieldType_Int64:
case kUpb_FieldType_UInt64:
CASE(uint64_t, varint, kUpb_WireType_Varint, val);
case kUpb_FieldType_UInt32:
CASE(uint32_t, varint, kUpb_WireType_Varint, val);
case kUpb_FieldType_Int32:
case kUpb_FieldType_Enum:
CASE(int32_t, varint, kUpb_WireType_Varint, (int64_t)val);
case kUpb_FieldType_SFixed64:
case kUpb_FieldType_Fixed64:
CASE(uint64_t, fixed64, kUpb_WireType_64Bit, val);
case kUpb_FieldType_Fixed32:
case kUpb_FieldType_SFixed32:
CASE(uint32_t, fixed32, kUpb_WireType_32Bit, val);
case kUpb_FieldType_Bool:
CASE(bool, varint, kUpb_WireType_Varint, val);
case kUpb_FieldType_SInt32:
CASE(int32_t, varint, kUpb_WireType_Varint, encode_zz32(val));
case kUpb_FieldType_SInt64:
CASE(int64_t, varint, kUpb_WireType_Varint, encode_zz64(val));
case kUpb_FieldType_String:
case kUpb_FieldType_Bytes: {
upb_StringView view = *(upb_StringView*)field_mem;
encode_bytes(e, view.data, view.size);
encode_varint(e, view.size);
wire_type = kUpb_WireType_Delimited;
break;
}
case kUpb_FieldType_Group: {
size_t size;
upb_TaggedMessagePtr submsg = *(upb_TaggedMessagePtr*)field_mem;
const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
if (submsg == 0) {
return;
}
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_EndGroup);
encode_TaggedMessagePtr(e, submsg, subm, &size);
wire_type = kUpb_WireType_StartGroup;
e->depth++;
break;
}
case kUpb_FieldType_Message: {
size_t size;
upb_TaggedMessagePtr submsg = *(upb_TaggedMessagePtr*)field_mem;
const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
if (submsg == 0) {
return;
}
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
encode_TaggedMessagePtr(e, submsg, subm, &size);
encode_varint(e, size);
wire_type = kUpb_WireType_Delimited;
e->depth++;
break;
}
default:
UPB_UNREACHABLE();
}
#undef CASE
encode_tag(e, upb_MiniTableField_Number(f), wire_type);
}
static void encode_array(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* f) {
const upb_Array* arr = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), upb_Array*);
bool packed = upb_MiniTableField_IsPacked(f);
size_t pre_len = e->limit - e->ptr;
if (arr == NULL || upb_Array_Size(arr) == 0) {
return;
}
#define VARINT_CASE(ctype, encode) \
{ \
const ctype* start = upb_Array_DataPtr(arr); \
const ctype* ptr = start + upb_Array_Size(arr); \
uint32_t tag = \
packed ? 0 : (f->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint; \
do { \
ptr--; \
encode_varint(e, encode); \
if (tag) encode_varint(e, tag); \
} while (ptr != start); \
} \
break;
#define TAG(wire_type) (packed ? 0 : (f->UPB_PRIVATE(number) << 3 | wire_type))
switch (f->UPB_PRIVATE(descriptortype)) {
case kUpb_FieldType_Double:
encode_fixedarray(e, arr, sizeof(double), TAG(kUpb_WireType_64Bit));
break;
case kUpb_FieldType_Float:
encode_fixedarray(e, arr, sizeof(float), TAG(kUpb_WireType_32Bit));
break;
case kUpb_FieldType_SFixed64:
case kUpb_FieldType_Fixed64:
encode_fixedarray(e, arr, sizeof(uint64_t), TAG(kUpb_WireType_64Bit));
break;
case kUpb_FieldType_Fixed32:
case kUpb_FieldType_SFixed32:
encode_fixedarray(e, arr, sizeof(uint32_t), TAG(kUpb_WireType_32Bit));
break;
case kUpb_FieldType_Int64:
case kUpb_FieldType_UInt64:
VARINT_CASE(uint64_t, *ptr);
case kUpb_FieldType_UInt32:
VARINT_CASE(uint32_t, *ptr);
case kUpb_FieldType_Int32:
case kUpb_FieldType_Enum:
VARINT_CASE(int32_t, (int64_t)*ptr);
case kUpb_FieldType_Bool:
VARINT_CASE(bool, *ptr);
case kUpb_FieldType_SInt32:
VARINT_CASE(int32_t, encode_zz32(*ptr));
case kUpb_FieldType_SInt64:
VARINT_CASE(int64_t, encode_zz64(*ptr));
case kUpb_FieldType_String:
case kUpb_FieldType_Bytes: {
const upb_StringView* start = upb_Array_DataPtr(arr);
const upb_StringView* ptr = start + upb_Array_Size(arr);
do {
ptr--;
encode_bytes(e, ptr->data, ptr->size);
encode_varint(e, ptr->size);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
} while (ptr != start);
return;
}
case kUpb_FieldType_Group: {
const upb_TaggedMessagePtr* start = upb_Array_DataPtr(arr);
const upb_TaggedMessagePtr* ptr = start + upb_Array_Size(arr);
const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
do {
size_t size;
ptr--;
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_EndGroup);
encode_TaggedMessagePtr(e, *ptr, subm, &size);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_StartGroup);
} while (ptr != start);
e->depth++;
return;
}
case kUpb_FieldType_Message: {
const upb_TaggedMessagePtr* start = upb_Array_DataPtr(arr);
const upb_TaggedMessagePtr* ptr = start + upb_Array_Size(arr);
const upb_MiniTable* subm = _upb_Encoder_GetSubMiniTable(subs, f);
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
do {
size_t size;
ptr--;
encode_TaggedMessagePtr(e, *ptr, subm, &size);
encode_varint(e, size);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
} while (ptr != start);
e->depth++;
return;
}
}
#undef VARINT_CASE
if (packed) {
encode_varint(e, e->limit - e->ptr - pre_len);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
}
}
static void encode_mapentry(upb_encstate* e, uint32_t number,
const upb_MiniTable* layout,
const upb_MapEntry* ent) {
const upb_MiniTableField* key_field = upb_MiniTable_MapKey(layout);
const upb_MiniTableField* val_field = upb_MiniTable_MapValue(layout);
size_t pre_len = e->limit - e->ptr;
size_t size;
encode_scalar(e, &ent->v, layout->UPB_PRIVATE(subs), val_field);
encode_scalar(e, &ent->k, layout->UPB_PRIVATE(subs), key_field);
size = (e->limit - e->ptr) - pre_len;
encode_varint(e, size);
encode_tag(e, number, kUpb_WireType_Delimited);
}
static void encode_map(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* f) {
const upb_Map* map = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), const upb_Map*);
const upb_MiniTable* layout = _upb_Encoder_GetSubMiniTable(subs, f);
UPB_ASSERT(upb_MiniTable_FieldCount(layout) == 2);
if (!map || !upb_Map_Size(map)) return;
if (e->options & kUpb_EncodeOption_Deterministic) {
if (!map->UPB_PRIVATE(is_strtable)) {
// For inttable, first encode the array part, then sort the table entries.
intptr_t iter = UPB_INTTABLE_BEGIN;
while ((size_t)++iter < map->t.inttable.array_size) {
upb_value value = map->t.inttable.array[iter];
if (upb_inttable_arrhas(&map->t.inttable, iter)) {
upb_MapEntry ent;
memcpy(&ent.k, &iter, sizeof(iter));
_upb_map_fromvalue(value, &ent.v, map->val_size);
encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
}
}
}
_upb_sortedmap sorted;
_upb_mapsorter_pushmap(
&e->sorter, layout->UPB_PRIVATE(fields)[0].UPB_PRIVATE(descriptortype),
map, &sorted);
upb_MapEntry ent;
while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
} else {
upb_value val;
if (map->UPB_PRIVATE(is_strtable)) {
intptr_t iter = UPB_STRTABLE_BEGIN;
upb_StringView strkey;
while (upb_strtable_next2(&map->t.strtable, &strkey, &val, &iter)) {
upb_MapEntry ent;
_upb_map_fromkey(strkey, &ent.k, map->key_size);
_upb_map_fromvalue(val, &ent.v, map->val_size);
encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
}
} else {
intptr_t iter = UPB_INTTABLE_BEGIN;
uintptr_t intkey = 0;
while (upb_inttable_next(&map->t.inttable, &intkey, &val, &iter)) {
upb_MapEntry ent;
memcpy(&ent.k, &intkey, map->key_size);
_upb_map_fromvalue(val, &ent.v, map->val_size);
encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
}
}
}
}
static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableField* f) {
if (f->presence == 0) {
// Proto3 presence or map/array.
const void* mem = UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), void);
switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
case kUpb_FieldRep_1Byte: {
char ch;
memcpy(&ch, mem, 1);
return ch != 0;
}
case kUpb_FieldRep_4Byte: {
uint32_t u32;
memcpy(&u32, mem, 4);
return u32 != 0;
}
case kUpb_FieldRep_8Byte: {
uint64_t u64;
memcpy(&u64, mem, 8);
return u64 != 0;
}
case kUpb_FieldRep_StringView: {
const upb_StringView* str = (const upb_StringView*)mem;
return str->size != 0;
}
default:
UPB_UNREACHABLE();
}
} else if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
// Proto2 presence: hasbit.
return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, f);
} else {
// Field is in a oneof.
return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, f) ==
upb_MiniTableField_Number(f);
}
}
static void encode_field(upb_encstate* e, const upb_Message* msg,
const upb_MiniTableSubInternal* subs,
const upb_MiniTableField* field) {
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(field)) {
case kUpb_FieldMode_Array:
encode_array(e, msg, subs, field);
break;
case kUpb_FieldMode_Map:
encode_map(e, msg, subs, field);
break;
case kUpb_FieldMode_Scalar:
encode_scalar(e, UPB_PTR_AT(msg, field->UPB_PRIVATE(offset), void), subs,
field);
break;
default:
UPB_UNREACHABLE();
}
}
static void encode_msgset_item(upb_encstate* e,
const upb_MiniTableExtension* ext,
const upb_MessageValue ext_val) {
size_t size;
encode_tag(e, kUpb_MsgSet_Item, kUpb_WireType_EndGroup);
encode_message(e, ext_val.msg_val, upb_MiniTableExtension_GetSubMessage(ext),
&size);
encode_varint(e, size);
encode_tag(e, kUpb_MsgSet_Message, kUpb_WireType_Delimited);
encode_varint(e, upb_MiniTableExtension_Number(ext));
encode_tag(e, kUpb_MsgSet_TypeId, kUpb_WireType_Varint);
encode_tag(e, kUpb_MsgSet_Item, kUpb_WireType_StartGroup);
}
static void encode_ext(upb_encstate* e, const upb_MiniTableExtension* ext,
upb_MessageValue ext_val, bool is_message_set) {
if (UPB_UNLIKELY(is_message_set)) {
encode_msgset_item(e, ext, ext_val);
} else {
upb_MiniTableSubInternal sub;
if (upb_MiniTableField_IsSubMessage(&ext->UPB_PRIVATE(field))) {
sub.UPB_PRIVATE(submsg) = &ext->UPB_PRIVATE(sub).UPB_PRIVATE(submsg);
} else {
sub.UPB_PRIVATE(subenum) = ext->UPB_PRIVATE(sub).UPB_PRIVATE(subenum);
}
encode_field(e, &ext_val.UPB_PRIVATE(ext_msg_val), &sub,
&ext->UPB_PRIVATE(field));
}
}
static void encode_exts(upb_encstate* e, const upb_MiniTable* m,
const upb_Message* msg) {
if (m->UPB_PRIVATE(ext) == kUpb_ExtMode_NonExtendable) return;
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) return;
/* Encode all extensions together. Unlike C++, we do not attempt to keep
* these in field number order relative to normal fields or even to each
* other. */
uintptr_t iter = kUpb_Message_ExtensionBegin;
const upb_MiniTableExtension* ext;
upb_MessageValue ext_val;
if (!UPB_PRIVATE(_upb_Message_NextExtensionReverse)(msg, &ext, &ext_val,
&iter)) {
// Message has no extensions.
return;
}
if (e->options & kUpb_EncodeOption_Deterministic) {
_upb_sortedmap sorted;
if (!_upb_mapsorter_pushexts(&e->sorter, in, &sorted)) {
// TODO: b/378744096 - handle alloc failure
}
const upb_Extension* ext;
while (_upb_sortedmap_nextext(&e->sorter, &sorted, &ext)) {
encode_ext(e, ext->ext, ext->data,
m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet);
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
} else {
do {
encode_ext(e, ext, ext_val,
m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet);
} while (UPB_PRIVATE(_upb_Message_NextExtensionReverse)(msg, &ext, &ext_val,
&iter));
}
}
static void encode_message(upb_encstate* e, const upb_Message* msg,
const upb_MiniTable* m, size_t* size) {
size_t pre_len = e->limit - e->ptr;
if (e->options & kUpb_EncodeOption_CheckRequired) {
if (m->UPB_PRIVATE(required_count)) {
if (!UPB_PRIVATE(_upb_Message_IsInitializedShallow)(msg, m)) {
encode_err(e, kUpb_EncodeStatus_MissingRequired);
}
}
}
if ((e->options & kUpb_EncodeOption_SkipUnknown) == 0) {
size_t unknown_size = 0;
uintptr_t iter = kUpb_Message_UnknownBegin;
upb_StringView unknown;
// Need to write in reverse order, but iteration is in-order; scan to
// reserve capacity up front, then write in-order
while (upb_Message_NextUnknown(msg, &unknown, &iter)) {
unknown_size += unknown.size;
}
if (unknown_size != 0) {
encode_reserve(e, unknown_size);
char* ptr = e->ptr;
iter = kUpb_Message_UnknownBegin;
while (upb_Message_NextUnknown(msg, &unknown, &iter)) {
memcpy(ptr, unknown.data, unknown.size);
ptr += unknown.size;
}
}
}
encode_exts(e, m, msg);
if (upb_MiniTable_FieldCount(m)) {
const upb_MiniTableField* f =
&m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)];
const upb_MiniTableField* first = &m->UPB_PRIVATE(fields)[0];
while (f != first) {
f--;
if (encode_shouldencode(e, msg, f)) {
encode_field(e, msg, m->UPB_PRIVATE(subs), f);
}
}
}
*size = (e->limit - e->ptr) - pre_len;
}
static upb_EncodeStatus upb_Encoder_Encode(upb_encstate* const encoder,
const upb_Message* const msg,
const upb_MiniTable* const l,
char** const buf, size_t* const size,
bool prepend_len) {
// Unfortunately we must continue to perform hackery here because there are
// code paths which blindly copy the returned pointer without bothering to
// check for errors until much later (b/235839510). So we still set *buf to
// NULL on error and we still set it to non-NULL on a successful empty result.
if (UPB_SETJMP(encoder->err) == 0) {
size_t encoded_msg_size;
encode_message(encoder, msg, l, &encoded_msg_size);
if (prepend_len) {
encode_varint(encoder, encoded_msg_size);
}
*size = encoder->limit - encoder->ptr;
if (*size == 0) {
static char ch;
*buf = &ch;
} else {
UPB_ASSERT(encoder->ptr);
*buf = encoder->ptr;
}
} else {
UPB_ASSERT(encoder->status != kUpb_EncodeStatus_Ok);
*buf = NULL;
*size = 0;
}
_upb_mapsorter_destroy(&encoder->sorter);
return encoder->status;
}
uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options) {
uint16_t max_depth = upb_EncodeOptions_GetMaxDepth(options);
return max_depth ? max_depth : kUpb_WireFormat_DefaultDepthLimit;
}
static upb_EncodeStatus _upb_Encode(const upb_Message* msg,
const upb_MiniTable* l, int options,
upb_Arena* arena, char** buf, size_t* size,
bool prepend_len) {
upb_encstate e;
e.status = kUpb_EncodeStatus_Ok;
e.arena = arena;
e.buf = NULL;
e.limit = NULL;
e.ptr = NULL;
e.depth = upb_EncodeOptions_GetEffectiveMaxDepth(options);
e.options = options;
_upb_mapsorter_init(&e.sorter);
return upb_Encoder_Encode(&e, msg, l, buf, size, prepend_len);
}
upb_EncodeStatus upb_Encode(const upb_Message* msg, const upb_MiniTable* l,
int options, upb_Arena* arena, char** buf,
size_t* size) {
return _upb_Encode(msg, l, options, arena, buf, size, false);
}
upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg,
const upb_MiniTable* l, int options,
upb_Arena* arena, char** buf,
size_t* size) {
return _upb_Encode(msg, l, options, arena, buf, size, true);
}
const char* upb_EncodeStatus_String(upb_EncodeStatus status) {
switch (status) {
case kUpb_EncodeStatus_Ok:
return "Ok";
case kUpb_EncodeStatus_MissingRequired:
return "Missing required field";
case kUpb_EncodeStatus_MaxDepthExceeded:
return "Max depth exceeded";
case kUpb_EncodeStatus_OutOfMemory:
return "Arena alloc failed";
default:
return "Unknown encode status";
}
}
// Fast decoder: ~3x the speed of decode.c, but requires x86-64/ARM64.
// Also the table size grows by 2x.
//
// Could potentially be ported to other 64-bit archs that pass at least six
// arguments in registers and have 8 unused high bits in pointers.
//
// The overall design is to create specialized functions for every possible
// field type (eg. oneof boolean field with a 1 byte tag) and then dispatch
// to the specialized function as quickly as possible.
// Must be last.
#if UPB_FASTTABLE
// The standard set of arguments passed to each parsing function.
// Thanks to x86-64 calling conventions, these will stay in registers.
#define UPB_PARSE_PARAMS \
upb_Decoder *d, const char *ptr, upb_Message *msg, intptr_t table, \
uint64_t hasbits, uint64_t data
#define UPB_PARSE_ARGS d, ptr, msg, table, hasbits, data
#define RETURN_GENERIC(m) \
/* Uncomment either of these for debugging purposes. */ \
/* fprintf(stderr, m); */ \
/*__builtin_trap(); */ \
return _upb_FastDecoder_DecodeGeneric(d, ptr, msg, table, hasbits, 0);
typedef enum {
CARD_s = 0, /* Singular (optional, non-repeated) */
CARD_o = 1, /* Oneof */
CARD_r = 2, /* Repeated */
CARD_p = 3 /* Packed Repeated */
} upb_card;
UPB_NOINLINE
static const char* fastdecode_isdonefallback(UPB_PARSE_PARAMS) {
int overrun = data;
ptr = _upb_EpsCopyInputStream_IsDoneFallbackInline(
&d->input, ptr, overrun, _upb_Decoder_BufferFlipCallback);
data = _upb_FastDecoder_LoadTag(ptr);
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS);
}
UPB_FORCEINLINE
const char* fastdecode_dispatch(UPB_PARSE_PARAMS) {
int overrun;
switch (upb_EpsCopyInputStream_IsDoneStatus(&d->input, ptr, &overrun)) {
case kUpb_IsDoneStatus_Done:
((uint32_t*)msg)[2] |= hasbits; // Sync hasbits.
const upb_MiniTable* m = decode_totablep(table);
return UPB_UNLIKELY(m->UPB_PRIVATE(required_count))
? _upb_Decoder_CheckRequired(d, ptr, msg, m)
: ptr;
case kUpb_IsDoneStatus_NotDone:
break;
case kUpb_IsDoneStatus_NeedFallback:
data = overrun;
UPB_MUSTTAIL return fastdecode_isdonefallback(UPB_PARSE_ARGS);
}
// Read two bytes of tag data (for a one-byte tag, the high byte is junk).
data = _upb_FastDecoder_LoadTag(ptr);
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS);
}
UPB_FORCEINLINE
bool fastdecode_checktag(uint16_t data, int tagbytes) {
if (tagbytes == 1) {
return (data & 0xff) == 0;
} else {
return data == 0;
}
}
UPB_FORCEINLINE
const char* fastdecode_longsize(const char* ptr, int* size) {
int i;
UPB_ASSERT(*size & 0x80);
*size &= 0xff;
for (i = 0; i < 3; i++) {
ptr++;
size_t byte = (uint8_t)ptr[-1];
*size += (byte - 1) << (7 + 7 * i);
if (UPB_LIKELY((byte & 0x80) == 0)) return ptr;
}
ptr++;
size_t byte = (uint8_t)ptr[-1];
// len is limited by 2gb not 4gb, hence 8 and not 16 as normally expected
// for a 32 bit varint.
if (UPB_UNLIKELY(byte >= 8)) return NULL;
*size += (byte - 1) << 28;
return ptr;
}
UPB_FORCEINLINE
const char* fastdecode_delimited(
upb_Decoder* d, const char* ptr,
upb_EpsCopyInputStream_ParseDelimitedFunc* func, void* ctx) {
ptr++;
// Sign-extend so varint greater than one byte becomes negative, causing
// fast delimited parse to fail.
int len = (int8_t)ptr[-1];
if (!upb_EpsCopyInputStream_TryParseDelimitedFast(&d->input, &ptr, len, func,
ctx)) {
// Slow case: Sub-message is >=128 bytes and/or exceeds the current buffer.
// If it exceeds the buffer limit, limit/limit_ptr will change during
// sub-message parsing, so we need to preserve delta, not limit.
if (UPB_UNLIKELY(len & 0x80)) {
// Size varint >1 byte (length >= 128).
ptr = fastdecode_longsize(ptr, &len);
if (!ptr) {
// Corrupt wire format: size exceeded INT_MAX.
return NULL;
}
}
if (!upb_EpsCopyInputStream_CheckSize(&d->input, ptr, len)) {
// Corrupt wire format: invalid limit.
return NULL;
}
int delta = upb_EpsCopyInputStream_PushLimit(&d->input, ptr, len);
ptr = func(&d->input, ptr, ctx);
upb_EpsCopyInputStream_PopLimit(&d->input, ptr, delta);
}
return ptr;
}
/* singular, oneof, repeated field handling ***********************************/
typedef struct {
upb_Array* arr;
void* end;
} fastdecode_arr;
typedef enum {
FD_NEXT_ATLIMIT,
FD_NEXT_SAMEFIELD,
FD_NEXT_OTHERFIELD
} fastdecode_next;
typedef struct {
void* dst;
fastdecode_next next;
uint32_t tag;
} fastdecode_nextret;
UPB_FORCEINLINE
void* fastdecode_resizearr(upb_Decoder* d, void* dst, fastdecode_arr* farr,
int valbytes) {
if (UPB_UNLIKELY(dst == farr->end)) {
size_t old_capacity = farr->arr->UPB_PRIVATE(capacity);
size_t old_bytes = old_capacity * valbytes;
size_t new_capacity = old_capacity * 2;
size_t new_bytes = new_capacity * valbytes;
char* old_ptr = upb_Array_MutableDataPtr(farr->arr);
char* new_ptr = upb_Arena_Realloc(&d->arena, old_ptr, old_bytes, new_bytes);
uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
UPB_PRIVATE(_upb_Array_SetTaggedPtr)(farr->arr, new_ptr, elem_size_lg2);
farr->arr->UPB_PRIVATE(capacity) = new_capacity;
dst = (void*)(new_ptr + (old_capacity * valbytes));
farr->end = (void*)(new_ptr + (new_capacity * valbytes));
}
return dst;
}
UPB_FORCEINLINE
bool fastdecode_tagmatch(uint32_t tag, uint64_t data, int tagbytes) {
if (tagbytes == 1) {
return (uint8_t)tag == (uint8_t)data;
} else {
return (uint16_t)tag == (uint16_t)data;
}
}
UPB_FORCEINLINE
void fastdecode_commitarr(void* dst, fastdecode_arr* farr, int valbytes) {
farr->arr->UPB_PRIVATE(size) =
(size_t)((char*)dst - (char*)upb_Array_MutableDataPtr(farr->arr)) /
valbytes;
}
UPB_FORCEINLINE
fastdecode_nextret fastdecode_nextrepeated(upb_Decoder* d, void* dst,
const char** ptr,
fastdecode_arr* farr, uint64_t data,
int tagbytes, int valbytes) {
fastdecode_nextret ret;
dst = (char*)dst + valbytes;
if (UPB_LIKELY(!_upb_Decoder_IsDone(d, ptr))) {
ret.tag = _upb_FastDecoder_LoadTag(*ptr);
if (fastdecode_tagmatch(ret.tag, data, tagbytes)) {
ret.next = FD_NEXT_SAMEFIELD;
} else {
fastdecode_commitarr(dst, farr, valbytes);
ret.next = FD_NEXT_OTHERFIELD;
}
} else {
fastdecode_commitarr(dst, farr, valbytes);
ret.next = FD_NEXT_ATLIMIT;
}
ret.dst = dst;
return ret;
}
UPB_FORCEINLINE
void* fastdecode_fieldmem(upb_Message* msg, uint64_t data) {
size_t ofs = data >> 48;
return (char*)msg + ofs;
}
UPB_FORCEINLINE
void* fastdecode_getfield(upb_Decoder* d, const char* ptr, upb_Message* msg,
uint64_t* data, uint64_t* hasbits,
fastdecode_arr* farr, int valbytes, upb_card card) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
switch (card) {
case CARD_s: {
uint8_t hasbit_index = *data >> 24;
// Set hasbit and return pointer to scalar field.
*hasbits |= 1ull << hasbit_index;
return fastdecode_fieldmem(msg, *data);
}
case CARD_o: {
uint16_t case_ofs = *data >> 32;
uint32_t* oneof_case = UPB_PTR_AT(msg, case_ofs, uint32_t);
uint8_t field_number = *data >> 24;
*oneof_case = field_number;
return fastdecode_fieldmem(msg, *data);
}
case CARD_r: {
// Get pointer to upb_Array and allocate/expand if necessary.
uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
upb_Array** arr_p = fastdecode_fieldmem(msg, *data);
char* begin;
((uint32_t*)msg)[2] |= *hasbits;
*hasbits = 0;
if (UPB_LIKELY(!*arr_p)) {
farr->arr = UPB_PRIVATE(_upb_Array_New)(&d->arena, 8, elem_size_lg2);
*arr_p = farr->arr;
} else {
farr->arr = *arr_p;
}
begin = upb_Array_MutableDataPtr(farr->arr);
farr->end = begin + (farr->arr->UPB_PRIVATE(capacity) * valbytes);
*data = _upb_FastDecoder_LoadTag(ptr);
return begin + (farr->arr->UPB_PRIVATE(size) * valbytes);
}
default:
UPB_UNREACHABLE();
}
}
UPB_FORCEINLINE
bool fastdecode_flippacked(uint64_t* data, int tagbytes) {
*data ^= (0x2 ^ 0x0); // Patch data to match packed wiretype.
return fastdecode_checktag(*data, tagbytes);
}
#define FASTDECODE_CHECKPACKED(tagbytes, card, func) \
if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
if (card == CARD_r && fastdecode_flippacked(&data, tagbytes)) { \
UPB_MUSTTAIL return func(UPB_PARSE_ARGS); \
} \
RETURN_GENERIC("packed check tag mismatch\n"); \
}
/* varint fields **************************************************************/
UPB_FORCEINLINE
uint64_t fastdecode_munge(uint64_t val, int valbytes, bool zigzag) {
if (valbytes == 1) {
return val != 0;
} else if (zigzag) {
if (valbytes == 4) {
uint32_t n = val;
return (n >> 1) ^ -(int32_t)(n & 1);
} else if (valbytes == 8) {
return (val >> 1) ^ -(int64_t)(val & 1);
}
UPB_UNREACHABLE();
}
return val;
}
UPB_FORCEINLINE
const char* fastdecode_varint64(const char* ptr, uint64_t* val) {
ptr++;
*val = (uint8_t)ptr[-1];
if (UPB_UNLIKELY(*val & 0x80)) {
int i;
for (i = 0; i < 8; i++) {
ptr++;
uint64_t byte = (uint8_t)ptr[-1];
*val += (byte - 1) << (7 + 7 * i);
if (UPB_LIKELY((byte & 0x80) == 0)) goto done;
}
ptr++;
uint64_t byte = (uint8_t)ptr[-1];
if (byte > 1) {
return NULL;
}
*val += (byte - 1) << 63;
}
done:
UPB_ASSUME(ptr != NULL);
return ptr;
}
#define FASTDECODE_UNPACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, card, zigzag, packed) \
uint64_t val; \
void* dst; \
fastdecode_arr farr; \
\
FASTDECODE_CHECKPACKED(tagbytes, card, packed); \
\
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, valbytes, \
card); \
if (card == CARD_r) { \
if (UPB_UNLIKELY(!dst)) { \
RETURN_GENERIC("need array resize\n"); \
} \
} \
\
again: \
if (card == CARD_r) { \
dst = fastdecode_resizearr(d, dst, &farr, valbytes); \
} \
\
ptr += tagbytes; \
ptr = fastdecode_varint64(ptr, &val); \
if (ptr == NULL) _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
val = fastdecode_munge(val, valbytes, zigzag); \
memcpy(dst, &val, valbytes); \
\
if (card == CARD_r) { \
fastdecode_nextret ret = fastdecode_nextrepeated( \
d, dst, &ptr, &farr, data, tagbytes, valbytes); \
switch (ret.next) { \
case FD_NEXT_SAMEFIELD: \
dst = ret.dst; \
goto again; \
case FD_NEXT_OTHERFIELD: \
data = ret.tag; \
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
case FD_NEXT_ATLIMIT: \
return ptr; \
} \
} \
\
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
typedef struct {
uint8_t valbytes;
bool zigzag;
void* dst;
fastdecode_arr farr;
} fastdecode_varintdata;
UPB_FORCEINLINE
const char* fastdecode_topackedvarint(upb_EpsCopyInputStream* e,
const char* ptr, void* ctx) {
upb_Decoder* d = (upb_Decoder*)e;
fastdecode_varintdata* data = ctx;
void* dst = data->dst;
uint64_t val;
while (!_upb_Decoder_IsDone(d, &ptr)) {
dst = fastdecode_resizearr(d, dst, &data->farr, data->valbytes);
ptr = fastdecode_varint64(ptr, &val);
if (ptr == NULL) return NULL;
val = fastdecode_munge(val, data->valbytes, data->zigzag);
memcpy(dst, &val, data->valbytes);
dst = (char*)dst + data->valbytes;
}
fastdecode_commitarr(dst, &data->farr, data->valbytes);
return ptr;
}
#define FASTDECODE_PACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, zigzag, unpacked) \
fastdecode_varintdata ctx = {valbytes, zigzag}; \
\
FASTDECODE_CHECKPACKED(tagbytes, CARD_r, unpacked); \
\
ctx.dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &ctx.farr, \
valbytes, CARD_r); \
if (UPB_UNLIKELY(!ctx.dst)) { \
RETURN_GENERIC("need array resize\n"); \
} \
\
ptr += tagbytes; \
ptr = fastdecode_delimited(d, ptr, &fastdecode_topackedvarint, &ctx); \
\
if (UPB_UNLIKELY(ptr == NULL)) { \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
} \
\
UPB_MUSTTAIL return fastdecode_dispatch(d, ptr, msg, table, hasbits, 0);
#define FASTDECODE_VARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, card, zigzag, unpacked, packed) \
if (card == CARD_p) { \
FASTDECODE_PACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, zigzag, unpacked); \
} else { \
FASTDECODE_UNPACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, card, zigzag, packed); \
}
#define z_ZZ true
#define b_ZZ false
#define v_ZZ false
/* Generate all combinations:
* {s,o,r,p} x {b1,v4,z4,v8,z8} x {1bt,2bt} */
#define F(card, type, valbytes, tagbytes) \
UPB_NOINLINE \
const char* upb_p##card##type##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
FASTDECODE_VARINT(d, ptr, msg, table, hasbits, data, tagbytes, valbytes, \
CARD_##card, type##_ZZ, \
upb_pr##type##valbytes##_##tagbytes##bt, \
upb_pp##type##valbytes##_##tagbytes##bt); \
}
#define TYPES(card, tagbytes) \
F(card, b, 1, tagbytes) \
F(card, v, 4, tagbytes) \
F(card, v, 8, tagbytes) \
F(card, z, 4, tagbytes) \
F(card, z, 8, tagbytes)
#define TAGBYTES(card) \
TYPES(card, 1) \
TYPES(card, 2)
TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
TAGBYTES(p)
#undef z_ZZ
#undef b_ZZ
#undef v_ZZ
#undef o_ONEOF
#undef s_ONEOF
#undef r_ONEOF
#undef F
#undef TYPES
#undef TAGBYTES
#undef FASTDECODE_UNPACKEDVARINT
#undef FASTDECODE_PACKEDVARINT
#undef FASTDECODE_VARINT
/* fixed fields ***************************************************************/
#define FASTDECODE_UNPACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, card, packed) \
void* dst; \
fastdecode_arr farr; \
\
FASTDECODE_CHECKPACKED(tagbytes, card, packed) \
\
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, valbytes, \
card); \
if (card == CARD_r) { \
if (UPB_UNLIKELY(!dst)) { \
RETURN_GENERIC("couldn't allocate array in arena\n"); \
} \
} \
\
again: \
if (card == CARD_r) { \
dst = fastdecode_resizearr(d, dst, &farr, valbytes); \
} \
\
ptr += tagbytes; \
memcpy(dst, ptr, valbytes); \
ptr += valbytes; \
\
if (card == CARD_r) { \
fastdecode_nextret ret = fastdecode_nextrepeated( \
d, dst, &ptr, &farr, data, tagbytes, valbytes); \
switch (ret.next) { \
case FD_NEXT_SAMEFIELD: \
dst = ret.dst; \
goto again; \
case FD_NEXT_OTHERFIELD: \
data = ret.tag; \
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
case FD_NEXT_ATLIMIT: \
return ptr; \
} \
} \
\
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
#define FASTDECODE_PACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, unpacked) \
FASTDECODE_CHECKPACKED(tagbytes, CARD_r, unpacked) \
\
ptr += tagbytes; \
int size = (uint8_t)ptr[0]; \
ptr++; \
if (size & 0x80) { \
ptr = fastdecode_longsize(ptr, &size); \
} \
\
if (UPB_UNLIKELY(!upb_EpsCopyInputStream_CheckDataSizeAvailable( \
&d->input, ptr, size) || \
(size % valbytes) != 0)) { \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
} \
\
upb_Array** arr_p = fastdecode_fieldmem(msg, data); \
upb_Array* arr = *arr_p; \
uint8_t elem_size_lg2 = __builtin_ctz(valbytes); \
int elems = size / valbytes; \
\
if (UPB_LIKELY(!arr)) { \
*arr_p = arr = \
UPB_PRIVATE(_upb_Array_New)(&d->arena, elems, elem_size_lg2); \
if (!arr) { \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
} \
} else { \
UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, elems, &d->arena); \
} \
\
char* dst = upb_Array_MutableDataPtr(arr); \
memcpy(dst, ptr, size); \
arr->UPB_PRIVATE(size) = elems; \
\
ptr += size; \
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
#define FASTDECODE_FIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, card, unpacked, packed) \
if (card == CARD_p) { \
FASTDECODE_PACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, unpacked); \
} else { \
FASTDECODE_UNPACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
valbytes, card, packed); \
}
/* Generate all combinations:
* {s,o,r,p} x {f4,f8} x {1bt,2bt} */
#define F(card, valbytes, tagbytes) \
UPB_NOINLINE \
const char* upb_p##card##f##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
FASTDECODE_FIXED(d, ptr, msg, table, hasbits, data, tagbytes, valbytes, \
CARD_##card, upb_ppf##valbytes##_##tagbytes##bt, \
upb_prf##valbytes##_##tagbytes##bt); \
}
#define TYPES(card, tagbytes) \
F(card, 4, tagbytes) \
F(card, 8, tagbytes)
#define TAGBYTES(card) \
TYPES(card, 1) \
TYPES(card, 2)
TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
TAGBYTES(p)
#undef F
#undef TYPES
#undef TAGBYTES
#undef FASTDECODE_UNPACKEDFIXED
#undef FASTDECODE_PACKEDFIXED
/* string fields **************************************************************/
typedef const char* fastdecode_copystr_func(struct upb_Decoder* d,
const char* ptr, upb_Message* msg,
const upb_MiniTable* table,
uint64_t hasbits,
upb_StringView* dst);
UPB_NOINLINE
static const char* fastdecode_verifyutf8(upb_Decoder* d, const char* ptr,
upb_Message* msg, intptr_t table,
uint64_t hasbits, uint64_t data) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_StringView* dst = (upb_StringView*)data;
if (!_upb_Decoder_VerifyUtf8Inline(dst->data, dst->size)) {
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_BadUtf8);
}
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
}
#define FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, validate_utf8) \
int size = (uint8_t)ptr[0]; /* Could plumb through hasbits. */ \
ptr++; \
if (size & 0x80) { \
ptr = fastdecode_longsize(ptr, &size); \
} \
\
if (UPB_UNLIKELY(!upb_EpsCopyInputStream_CheckSize(&d->input, ptr, size))) { \
dst->size = 0; \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
} \
\
const char* s_ptr = ptr; \
ptr = upb_EpsCopyInputStream_ReadString(&d->input, &s_ptr, size, &d->arena); \
if (!ptr) _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory); \
dst->data = s_ptr; \
dst->size = size; \
\
if (validate_utf8) { \
data = (uint64_t)dst; \
UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
} else { \
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS); \
}
UPB_NOINLINE
static const char* fastdecode_longstring_utf8(struct upb_Decoder* d,
const char* ptr, upb_Message* msg,
intptr_t table, uint64_t hasbits,
uint64_t data) {
upb_StringView* dst = (upb_StringView*)data;
FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, true);
}
UPB_NOINLINE
static const char* fastdecode_longstring_noutf8(
struct upb_Decoder* d, const char* ptr, upb_Message* msg, intptr_t table,
uint64_t hasbits, uint64_t data) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_StringView* dst = (upb_StringView*)data;
FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, false);
}
UPB_FORCEINLINE
void fastdecode_docopy(upb_Decoder* d, const char* ptr, uint32_t size, int copy,
char* data, size_t data_offset, upb_StringView* dst) {
d->arena.UPB_PRIVATE(ptr) += copy;
dst->data = data + data_offset;
UPB_UNPOISON_MEMORY_REGION(data, copy);
memcpy(data, ptr, copy);
UPB_POISON_MEMORY_REGION(data + data_offset + size,
copy - data_offset - size);
}
#define FASTDECODE_COPYSTRING(d, ptr, msg, table, hasbits, data, tagbytes, \
card, validate_utf8) \
upb_StringView* dst; \
fastdecode_arr farr; \
int64_t size; \
size_t arena_has; \
size_t common_has; \
char* buf; \
\
UPB_ASSERT(!upb_EpsCopyInputStream_AliasingAvailable(&d->input, ptr, 0)); \
UPB_ASSERT(fastdecode_checktag(data, tagbytes)); \
\
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
sizeof(upb_StringView), card); \
\
again: \
if (card == CARD_r) { \
dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_StringView)); \
} \
\
size = (uint8_t)ptr[tagbytes]; \
ptr += tagbytes + 1; \
dst->size = size; \
\
buf = d->arena.UPB_PRIVATE(ptr); \
arena_has = UPB_PRIVATE(_upb_ArenaHas)(&d->arena); \
common_has = UPB_MIN(arena_has, \
upb_EpsCopyInputStream_BytesAvailable(&d->input, ptr)); \
\
if (UPB_LIKELY(size <= 15 - tagbytes)) { \
if (arena_has < 16) goto longstr; \
fastdecode_docopy(d, ptr - tagbytes - 1, size, 16, buf, tagbytes + 1, \
dst); \
} else if (UPB_LIKELY(size <= 32)) { \
if (UPB_UNLIKELY(common_has < 32)) goto longstr; \
fastdecode_docopy(d, ptr, size, 32, buf, 0, dst); \
} else if (UPB_LIKELY(size <= 64)) { \
if (UPB_UNLIKELY(common_has < 64)) goto longstr; \
fastdecode_docopy(d, ptr, size, 64, buf, 0, dst); \
} else if (UPB_LIKELY(size < 128)) { \
if (UPB_UNLIKELY(common_has < 128)) goto longstr; \
fastdecode_docopy(d, ptr, size, 128, buf, 0, dst); \
} else { \
goto longstr; \
} \
\
ptr += size; \
\
if (card == CARD_r) { \
if (validate_utf8 && \
!_upb_Decoder_VerifyUtf8Inline(dst->data, dst->size)) { \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_BadUtf8); \
} \
fastdecode_nextret ret = fastdecode_nextrepeated( \
d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_StringView)); \
switch (ret.next) { \
case FD_NEXT_SAMEFIELD: \
dst = ret.dst; \
goto again; \
case FD_NEXT_OTHERFIELD: \
data = ret.tag; \
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
case FD_NEXT_ATLIMIT: \
return ptr; \
} \
} \
\
if (card != CARD_r && validate_utf8) { \
data = (uint64_t)dst; \
UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
} \
\
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS); \
\
longstr: \
if (card == CARD_r) { \
fastdecode_commitarr(dst + 1, &farr, sizeof(upb_StringView)); \
} \
ptr--; \
if (validate_utf8) { \
UPB_MUSTTAIL return fastdecode_longstring_utf8(d, ptr, msg, table, \
hasbits, (uint64_t)dst); \
} else { \
UPB_MUSTTAIL return fastdecode_longstring_noutf8(d, ptr, msg, table, \
hasbits, (uint64_t)dst); \
}
#define FASTDECODE_STRING(d, ptr, msg, table, hasbits, data, tagbytes, card, \
copyfunc, validate_utf8) \
upb_StringView* dst; \
fastdecode_arr farr; \
int64_t size; \
\
if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
RETURN_GENERIC("string field tag mismatch\n"); \
} \
\
if (UPB_UNLIKELY( \
!upb_EpsCopyInputStream_AliasingAvailable(&d->input, ptr, 0))) { \
UPB_MUSTTAIL return copyfunc(UPB_PARSE_ARGS); \
} \
\
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
sizeof(upb_StringView), card); \
\
again: \
if (card == CARD_r) { \
dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_StringView)); \
} \
\
size = (int8_t)ptr[tagbytes]; \
ptr += tagbytes + 1; \
\
if (UPB_UNLIKELY( \
!upb_EpsCopyInputStream_AliasingAvailable(&d->input, ptr, size))) { \
ptr--; \
if (validate_utf8) { \
return fastdecode_longstring_utf8(d, ptr, msg, table, hasbits, \
(uint64_t)dst); \
} else { \
return fastdecode_longstring_noutf8(d, ptr, msg, table, hasbits, \
(uint64_t)dst); \
} \
} \
\
dst->data = ptr; \
dst->size = size; \
ptr = upb_EpsCopyInputStream_ReadStringAliased(&d->input, &dst->data, \
dst->size); \
\
if (card == CARD_r) { \
if (validate_utf8 && \
!_upb_Decoder_VerifyUtf8Inline(dst->data, dst->size)) { \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_BadUtf8); \
} \
fastdecode_nextret ret = fastdecode_nextrepeated( \
d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_StringView)); \
switch (ret.next) { \
case FD_NEXT_SAMEFIELD: \
dst = ret.dst; \
goto again; \
case FD_NEXT_OTHERFIELD: \
data = ret.tag; \
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
case FD_NEXT_ATLIMIT: \
return ptr; \
} \
} \
\
if (card != CARD_r && validate_utf8) { \
data = (uint64_t)dst; \
UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
} \
\
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
/* Generate all combinations:
* {p,c} x {s,o,r} x {s, b} x {1bt,2bt} */
#define s_VALIDATE true
#define b_VALIDATE false
#define F(card, tagbytes, type) \
UPB_NOINLINE \
const char* upb_c##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
FASTDECODE_COPYSTRING(d, ptr, msg, table, hasbits, data, tagbytes, \
CARD_##card, type##_VALIDATE); \
} \
const char* upb_p##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
FASTDECODE_STRING(d, ptr, msg, table, hasbits, data, tagbytes, \
CARD_##card, upb_c##card##type##_##tagbytes##bt, \
type##_VALIDATE); \
}
#define UTF8(card, tagbytes) \
F(card, tagbytes, s) \
F(card, tagbytes, b)
#define TAGBYTES(card) \
UTF8(card, 1) \
UTF8(card, 2)
TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
#undef s_VALIDATE
#undef b_VALIDATE
#undef F
#undef TAGBYTES
#undef FASTDECODE_LONGSTRING
#undef FASTDECODE_COPYSTRING
#undef FASTDECODE_STRING
/* message fields *************************************************************/
UPB_INLINE
upb_Message* decode_newmsg_ceil(upb_Decoder* d, const upb_MiniTable* m,
int msg_ceil_bytes) {
size_t size = m->UPB_PRIVATE(size);
char* msg_data;
if (UPB_LIKELY(msg_ceil_bytes > 0 &&
UPB_PRIVATE(_upb_ArenaHas)(&d->arena) >= msg_ceil_bytes)) {
UPB_ASSERT(size <= (size_t)msg_ceil_bytes);
msg_data = d->arena.UPB_PRIVATE(ptr);
d->arena.UPB_PRIVATE(ptr) += size;
UPB_UNPOISON_MEMORY_REGION(msg_data, msg_ceil_bytes);
memset(msg_data, 0, msg_ceil_bytes);
UPB_POISON_MEMORY_REGION(msg_data + size, msg_ceil_bytes - size);
} else {
msg_data = (char*)upb_Arena_Malloc(&d->arena, size);
memset(msg_data, 0, size);
}
return (upb_Message*)msg_data;
}
typedef struct {
intptr_t table;
upb_Message* msg;
} fastdecode_submsgdata;
UPB_FORCEINLINE
const char* fastdecode_tosubmsg(upb_EpsCopyInputStream* e, const char* ptr,
void* ctx) {
upb_Decoder* d = (upb_Decoder*)e;
fastdecode_submsgdata* submsg = ctx;
ptr = fastdecode_dispatch(d, ptr, submsg->msg, submsg->table, 0, 0);
UPB_ASSUME(ptr != NULL);
return ptr;
}
#define FASTDECODE_SUBMSG(d, ptr, msg, table, hasbits, data, tagbytes, \
msg_ceil_bytes, card) \
\
if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
RETURN_GENERIC("submessage field tag mismatch\n"); \
} \
\
if (--d->depth == 0) { \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_MaxDepthExceeded); \
} \
\
upb_Message** dst; \
uint32_t submsg_idx = (data >> 16) & 0xff; \
const upb_MiniTable* tablep = decode_totablep(table); \
const upb_MiniTable* subtablep = \
UPB_PRIVATE(_upb_MiniTable_GetSubTableByIndex)(tablep, submsg_idx); \
fastdecode_submsgdata submsg = {decode_totable(subtablep)}; \
fastdecode_arr farr; \
\
if (subtablep->UPB_PRIVATE(table_mask) == (uint8_t)-1) { \
d->depth++; \
RETURN_GENERIC("submessage doesn't have fast tables."); \
} \
\
dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
sizeof(upb_Message*), card); \
\
if (card == CARD_s) { \
((uint32_t*)msg)[2] |= hasbits; \
hasbits = 0; \
} \
\
again: \
if (card == CARD_r) { \
dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_Message*)); \
} \
\
submsg.msg = *dst; \
\
if (card == CARD_r || UPB_LIKELY(!submsg.msg)) { \
*dst = submsg.msg = decode_newmsg_ceil(d, subtablep, msg_ceil_bytes); \
} \
\
ptr += tagbytes; \
ptr = fastdecode_delimited(d, ptr, fastdecode_tosubmsg, &submsg); \
\
if (UPB_UNLIKELY(ptr == NULL || d->end_group != DECODE_NOGROUP)) { \
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed); \
} \
\
if (card == CARD_r) { \
fastdecode_nextret ret = fastdecode_nextrepeated( \
d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_Message*)); \
switch (ret.next) { \
case FD_NEXT_SAMEFIELD: \
dst = ret.dst; \
goto again; \
case FD_NEXT_OTHERFIELD: \
d->depth++; \
data = ret.tag; \
UPB_MUSTTAIL return _upb_FastDecoder_TagDispatch(UPB_PARSE_ARGS); \
case FD_NEXT_ATLIMIT: \
d->depth++; \
return ptr; \
} \
} \
\
d->depth++; \
UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
#define F(card, tagbytes, size_ceil, ceil_arg) \
const char* upb_p##card##m_##tagbytes##bt_max##size_ceil##b( \
UPB_PARSE_PARAMS) { \
FASTDECODE_SUBMSG(d, ptr, msg, table, hasbits, data, tagbytes, ceil_arg, \
CARD_##card); \
}
#define SIZES(card, tagbytes) \
F(card, tagbytes, 64, 64) \
F(card, tagbytes, 128, 128) \
F(card, tagbytes, 192, 192) \
F(card, tagbytes, 256, 256) \
F(card, tagbytes, max, -1)
#define TAGBYTES(card) \
SIZES(card, 1) \
SIZES(card, 2)
TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
#undef TAGBYTES
#undef SIZES
#undef F
#undef FASTDECODE_SUBMSG
#endif /* UPB_FASTTABLE */
#include <stddef.h>
#include <stdint.h>
// Must be last.
UPB_NOINLINE UPB_PRIVATE(_upb_WireReader_LongVarint)
UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(const char* ptr, uint64_t val) {
UPB_PRIVATE(_upb_WireReader_LongVarint) ret = {NULL, 0};
uint64_t byte;
for (int i = 1; i < 10; i++) {
byte = (uint8_t)ptr[i];
val += (byte - 1) << (i * 7);
if (!(byte & 0x80)) {
ret.ptr = ptr + i + 1;
ret.val = val;
return ret;
}
}
return ret;
}
const char* UPB_PRIVATE(_upb_WireReader_SkipGroup)(
const char* ptr, uint32_t tag, int depth_limit,
upb_EpsCopyInputStream* stream) {
if (--depth_limit == 0) return NULL;
uint32_t end_group_tag = (tag & ~7ULL) | kUpb_WireType_EndGroup;
while (!upb_EpsCopyInputStream_IsDone(stream, &ptr)) {
uint32_t tag;
ptr = upb_WireReader_ReadTag(ptr, &tag);
if (!ptr) return NULL;
if (tag == end_group_tag) return ptr;
ptr = _upb_WireReader_SkipValue(ptr, tag, depth_limit, stream);
if (!ptr) return NULL;
}
return ptr;
}
/* This file was generated by upb_generator from the input file:
*
* google/protobuf/descriptor.proto
*
* Do not edit -- your changes will be discarded when the file is
* regenerated.
* NO CHECKED-IN PROTOBUF GENCODE */
#include <stddef.h>
// Must be last.
extern const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_StaticallyTreeShaken);
static const upb_MiniTableSubInternal google_protobuf_FileDescriptorSet__submsgs[1] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FileDescriptorProto_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_FileDescriptorSet__fields[1] = {
{1, 8, 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FileDescriptorSet_msg_init = {
&google_protobuf_FileDescriptorSet__submsgs[0],
&google_protobuf_FileDescriptorSet__fields[0],
16, 1, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(8), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FileDescriptorSet",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x000800003f00000a, &upb_prm_1bt_max192b},
})
};
const upb_MiniTable* google__protobuf__FileDescriptorSet_msg_init_ptr = &google__protobuf__FileDescriptorSet_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FileDescriptorProto__submsgs[7] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__DescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__EnumDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__ServiceDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__FieldDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__FileOptions_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__SourceCodeInfo_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init},
};
static const upb_MiniTableField google_protobuf_FileDescriptorProto__fields[14] = {
{1, UPB_SIZE(56, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(64, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(12, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{4, UPB_SIZE(16, 72), 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{5, UPB_SIZE(20, 80), 0, 1, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{6, UPB_SIZE(24, 88), 0, 2, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{7, UPB_SIZE(28, 96), 0, 3, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{8, UPB_SIZE(32, 104), 66, 4, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{9, UPB_SIZE(36, 112), 67, 5, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{10, UPB_SIZE(40, 120), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{11, UPB_SIZE(44, 128), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{12, UPB_SIZE(72, 48), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{14, UPB_SIZE(48, 12), 69, 6, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{15, UPB_SIZE(52, 136), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FileDescriptorProto_msg_init = {
&google_protobuf_FileDescriptorProto__submsgs[0],
&google_protobuf_FileDescriptorProto__fields[0],
UPB_SIZE(80, 144), 14, kUpb_ExtMode_NonExtendable, 12, UPB_FASTTABLE_MASK(120), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FileDescriptorProto",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x004000003f00001a, &upb_prs_1bt},
{0x004800003f000022, &upb_prm_1bt_max128b},
{0x005000003f01002a, &upb_prm_1bt_max128b},
{0x005800003f020032, &upb_prm_1bt_max64b},
{0x006000003f03003a, &upb_prm_1bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x007800003f000050, &upb_prv4_1bt},
{0x008000003f000058, &upb_prv4_1bt},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x008800003f00007a, &upb_prs_1bt},
})
};
const upb_MiniTable* google__protobuf__FileDescriptorProto_msg_init_ptr = &google__protobuf__FileDescriptorProto_msg_init;
static const upb_MiniTableSubInternal google_protobuf_DescriptorProto__submsgs[9] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FieldDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__DescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__EnumDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__DescriptorProto__ExtensionRange_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__FieldDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__MessageOptions_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__OneofDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__DescriptorProto__ReservedRange_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__SymbolVisibility_enum_init},
};
static const upb_MiniTableField google_protobuf_DescriptorProto__fields[11] = {
{1, UPB_SIZE(52, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(12, 32), 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(16, 40), 0, 1, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{4, UPB_SIZE(20, 48), 0, 2, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{5, UPB_SIZE(24, 56), 0, 3, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{6, UPB_SIZE(28, 64), 0, 4, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{7, UPB_SIZE(32, 72), 65, 5, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{8, UPB_SIZE(36, 80), 0, 6, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{9, UPB_SIZE(40, 88), 0, 7, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{10, UPB_SIZE(44, 96), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{11, UPB_SIZE(48, 12), 66, 8, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__DescriptorProto_msg_init = {
&google_protobuf_DescriptorProto__submsgs[0],
&google_protobuf_DescriptorProto__fields[0],
UPB_SIZE(64, 104), 11, kUpb_ExtMode_NonExtendable, 11, UPB_FASTTABLE_MASK(120), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.DescriptorProto",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x002000003f000012, &upb_prm_1bt_max128b},
{0x002800003f01001a, &upb_prm_1bt_max128b},
{0x003000003f020022, &upb_prm_1bt_max128b},
{0x003800003f03002a, &upb_prm_1bt_max64b},
{0x004000003f040032, &upb_prm_1bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x005000003f060042, &upb_prm_1bt_max64b},
{0x005800003f07004a, &upb_prm_1bt_max64b},
{0x006000003f000052, &upb_prs_1bt},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__DescriptorProto_msg_init_ptr = &google__protobuf__DescriptorProto_msg_init;
static const upb_MiniTableSubInternal google_protobuf_DescriptorProto_ExtensionRange__submsgs[1] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__ExtensionRangeOptions_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_DescriptorProto_ExtensionRange__fields[3] = {
{1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(20, 24), 66, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__DescriptorProto__ExtensionRange_msg_init = {
&google_protobuf_DescriptorProto_ExtensionRange__submsgs[0],
&google_protobuf_DescriptorProto_ExtensionRange__fields[0],
UPB_SIZE(24, 32), 3, kUpb_ExtMode_NonExtendable, 3, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.DescriptorProto.ExtensionRange",
#endif
};
const upb_MiniTable* google__protobuf__DescriptorProto__ExtensionRange_msg_init_ptr = &google__protobuf__DescriptorProto__ExtensionRange_msg_init;
static const upb_MiniTableField google_protobuf_DescriptorProto_ReservedRange__fields[2] = {
{1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__DescriptorProto__ReservedRange_msg_init = {
NULL,
&google_protobuf_DescriptorProto_ReservedRange__fields[0],
24, 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.DescriptorProto.ReservedRange",
#endif
};
const upb_MiniTable* google__protobuf__DescriptorProto__ReservedRange_msg_init_ptr = &google__protobuf__DescriptorProto__ReservedRange_msg_init;
static const upb_MiniTableSubInternal google_protobuf_ExtensionRangeOptions__submsgs[4] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__ExtensionRangeOptions__Declaration_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__ExtensionRangeOptions__VerificationState_enum_init},
};
static const upb_MiniTableField google_protobuf_ExtensionRangeOptions__fields[4] = {
{2, UPB_SIZE(12, 16), 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(16, 12), 64, 3, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{50, UPB_SIZE(20, 24), 65, 1, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(24, 32), 0, 2, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__ExtensionRangeOptions_msg_init = {
&google_protobuf_ExtensionRangeOptions__submsgs[0],
&google_protobuf_ExtensionRangeOptions__fields[0],
UPB_SIZE(32, 40), 4, kUpb_ExtMode_Extendable, 0, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.ExtensionRangeOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x001000003f000012, &upb_prm_1bt_max64b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x002000003f023eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__ExtensionRangeOptions_msg_init_ptr = &google__protobuf__ExtensionRangeOptions_msg_init;
static const upb_MiniTableField google_protobuf_ExtensionRangeOptions_Declaration__fields[5] = {
{1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{2, 16, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(24, 32), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{5, 9, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{6, 10, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__ExtensionRangeOptions__Declaration_msg_init = {
NULL,
&google_protobuf_ExtensionRangeOptions_Declaration__fields[0],
UPB_SIZE(32, 48), 5, kUpb_ExtMode_NonExtendable, 3, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.ExtensionRangeOptions.Declaration",
#endif
};
const upb_MiniTable* google__protobuf__ExtensionRangeOptions__Declaration_msg_init_ptr = &google__protobuf__ExtensionRangeOptions__Declaration_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FieldDescriptorProto__submsgs[3] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FieldOptions_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__FieldDescriptorProto__Label_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FieldDescriptorProto__Type_enum_init},
};
static const upb_MiniTableField google_protobuf_FieldDescriptorProto__fields[11] = {
{1, 32, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(40, 48), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{3, 12, 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{4, 16, 67, 1, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{5, 20, 68, 2, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{6, UPB_SIZE(48, 64), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{7, UPB_SIZE(56, 80), 70, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{8, UPB_SIZE(24, 112), 71, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{9, UPB_SIZE(28, 24), 72, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{10, UPB_SIZE(64, 96), 73, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{17, 10, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FieldDescriptorProto_msg_init = {
&google_protobuf_FieldDescriptorProto__submsgs[0],
&google_protobuf_FieldDescriptorProto__fields[0],
UPB_SIZE(72, 120), 11, kUpb_ExtMode_NonExtendable, 10, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FieldDescriptorProto",
#endif
};
const upb_MiniTable* google__protobuf__FieldDescriptorProto_msg_init_ptr = &google__protobuf__FieldDescriptorProto_msg_init;
static const upb_MiniTableSubInternal google_protobuf_OneofDescriptorProto__submsgs[1] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__OneofOptions_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_OneofDescriptorProto__fields[2] = {
{1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(12, 32), 65, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__OneofDescriptorProto_msg_init = {
&google_protobuf_OneofDescriptorProto__submsgs[0],
&google_protobuf_OneofDescriptorProto__fields[0],
UPB_SIZE(24, 40), 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.OneofDescriptorProto",
#endif
};
const upb_MiniTable* google__protobuf__OneofDescriptorProto_msg_init_ptr = &google__protobuf__OneofDescriptorProto_msg_init;
static const upb_MiniTableSubInternal google_protobuf_EnumDescriptorProto__submsgs[4] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__EnumValueDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__EnumOptions_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__SymbolVisibility_enum_init},
};
static const upb_MiniTableField google_protobuf_EnumDescriptorProto__fields[6] = {
{1, UPB_SIZE(32, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(12, 32), 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(16, 40), 65, 1, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{4, UPB_SIZE(20, 48), 0, 2, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{5, UPB_SIZE(24, 56), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{6, UPB_SIZE(28, 12), 66, 3, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__EnumDescriptorProto_msg_init = {
&google_protobuf_EnumDescriptorProto__submsgs[0],
&google_protobuf_EnumDescriptorProto__fields[0],
UPB_SIZE(40, 64), 6, kUpb_ExtMode_NonExtendable, 6, UPB_FASTTABLE_MASK(56), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.EnumDescriptorProto",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x002000003f000012, &upb_prm_1bt_max64b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x003000003f020022, &upb_prm_1bt_max64b},
{0x003800003f00002a, &upb_prs_1bt},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__EnumDescriptorProto_msg_init_ptr = &google__protobuf__EnumDescriptorProto_msg_init;
static const upb_MiniTableField google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = {
{1, 12, 64, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{2, 16, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init = {
NULL,
&google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0],
24, 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.EnumDescriptorProto.EnumReservedRange",
#endif
};
const upb_MiniTable* google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init_ptr = &google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init;
static const upb_MiniTableSubInternal google_protobuf_EnumValueDescriptorProto__submsgs[1] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__EnumValueOptions_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_EnumValueDescriptorProto__fields[3] = {
{1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, 12, 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(16, 32), 66, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__EnumValueDescriptorProto_msg_init = {
&google_protobuf_EnumValueDescriptorProto__submsgs[0],
&google_protobuf_EnumValueDescriptorProto__fields[0],
UPB_SIZE(32, 40), 3, kUpb_ExtMode_NonExtendable, 3, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.EnumValueDescriptorProto",
#endif
};
const upb_MiniTable* google__protobuf__EnumValueDescriptorProto_msg_init_ptr = &google__protobuf__EnumValueDescriptorProto_msg_init;
static const upb_MiniTableSubInternal google_protobuf_ServiceDescriptorProto__submsgs[2] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__MethodDescriptorProto_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__ServiceOptions_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_ServiceDescriptorProto__fields[3] = {
{1, UPB_SIZE(20, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(12, 32), 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(16, 40), 65, 1, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__ServiceDescriptorProto_msg_init = {
&google_protobuf_ServiceDescriptorProto__submsgs[0],
&google_protobuf_ServiceDescriptorProto__fields[0],
UPB_SIZE(32, 48), 3, kUpb_ExtMode_NonExtendable, 3, UPB_FASTTABLE_MASK(24), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.ServiceDescriptorProto",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x002000003f000012, &upb_prm_1bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__ServiceDescriptorProto_msg_init_ptr = &google__protobuf__ServiceDescriptorProto_msg_init;
static const upb_MiniTableSubInternal google_protobuf_MethodDescriptorProto__submsgs[1] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__MethodOptions_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_MethodDescriptorProto__fields[6] = {
{1, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(24, 32), 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(32, 48), 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{4, UPB_SIZE(12, 64), 67, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{5, 9, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{6, 10, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__MethodDescriptorProto_msg_init = {
&google_protobuf_MethodDescriptorProto__submsgs[0],
&google_protobuf_MethodDescriptorProto__fields[0],
UPB_SIZE(40, 72), 6, kUpb_ExtMode_NonExtendable, 6, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.MethodDescriptorProto",
#endif
};
const upb_MiniTable* google__protobuf__MethodDescriptorProto_msg_init_ptr = &google__protobuf__MethodDescriptorProto_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FileOptions__submsgs[3] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__FileOptions__OptimizeMode_enum_init},
};
static const upb_MiniTableField google_protobuf_FileOptions__fields[21] = {
{1, UPB_SIZE(32, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{8, 40, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{9, 20, 66, 2, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{10, 11, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{11, UPB_SIZE(48, 56), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{16, 12, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{17, 13, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{18, 14, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{20, 15, 72, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{23, 16, 73, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{27, 17, 74, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{31, 18, 75, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{36, UPB_SIZE(56, 72), 76, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{37, UPB_SIZE(64, 88), 77, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{39, UPB_SIZE(72, 104), 78, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{40, UPB_SIZE(80, 120), 79, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{41, UPB_SIZE(88, 136), 80, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{44, UPB_SIZE(96, 152), 81, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{45, UPB_SIZE(104, 168), 82, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{50, UPB_SIZE(24, 184), 83, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(28, 192), 0, 1, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FileOptions_msg_init = {
&google_protobuf_FileOptions__submsgs[0],
&google_protobuf_FileOptions__fields[0],
UPB_SIZE(112, 200), 21, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FileOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x00c000003f013eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__FileOptions_msg_init_ptr = &google__protobuf__FileOptions_msg_init;
static const upb_MiniTableSubInternal google_protobuf_MessageOptions__submsgs[2] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_MessageOptions__fields[7] = {
{1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{7, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{11, 13, 68, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{12, 16, 69, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(20, 24), 0, 1, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__MessageOptions_msg_init = {
&google_protobuf_MessageOptions__submsgs[0],
&google_protobuf_MessageOptions__fields[0],
UPB_SIZE(24, 32), 7, kUpb_ExtMode_Extendable, 3, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.MessageOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x001800003f013eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__MessageOptions_msg_init_ptr = &google__protobuf__MessageOptions_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FieldOptions__submsgs[8] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FieldOptions__EditionDefault_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__FieldOptions__FeatureSupport_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__FieldOptions__CType_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FieldOptions__JSType_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FieldOptions__OptionRetention_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FieldOptions__OptionTargetType_enum_init},
};
static const upb_MiniTableField google_protobuf_FieldOptions__fields[14] = {
{1, 16, 64, 4, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{2, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{3, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{5, 12, 67, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{6, 20, 68, 5, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{10, 13, 69, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{15, 14, 70, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{16, 15, 71, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{17, 24, 72, 6, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{19, UPB_SIZE(28, 32), 0, 7, 14, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{20, UPB_SIZE(32, 40), 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{21, UPB_SIZE(36, 48), 73, 1, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{22, UPB_SIZE(40, 56), 74, 2, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(44, 64), 0, 3, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FieldOptions_msg_init = {
&google_protobuf_FieldOptions__submsgs[0],
&google_protobuf_FieldOptions__fields[0],
UPB_SIZE(48, 72), 14, kUpb_ExtMode_Extendable, 3, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FieldOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x002800003f0001a2, &upb_prm_2bt_max64b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x004000003f033eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__FieldOptions_msg_init_ptr = &google__protobuf__FieldOptions_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FieldOptions_EditionDefault__submsgs[1] = {
{.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init},
};
static const upb_MiniTableField google_protobuf_FieldOptions_EditionDefault__fields[2] = {
{2, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{3, 12, 65, 0, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FieldOptions__EditionDefault_msg_init = {
&google_protobuf_FieldOptions_EditionDefault__submsgs[0],
&google_protobuf_FieldOptions_EditionDefault__fields[0],
UPB_SIZE(24, 32), 2, kUpb_ExtMode_NonExtendable, 0, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FieldOptions.EditionDefault",
#endif
};
const upb_MiniTable* google__protobuf__FieldOptions__EditionDefault_msg_init_ptr = &google__protobuf__FieldOptions__EditionDefault_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FieldOptions_FeatureSupport__submsgs[3] = {
{.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init},
};
static const upb_MiniTableField google_protobuf_FieldOptions_FeatureSupport__fields[4] = {
{1, 12, 64, 0, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{2, 16, 65, 1, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{3, 24, 66, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{4, 20, 67, 2, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FieldOptions__FeatureSupport_msg_init = {
&google_protobuf_FieldOptions_FeatureSupport__submsgs[0],
&google_protobuf_FieldOptions_FeatureSupport__fields[0],
UPB_SIZE(32, 40), 4, kUpb_ExtMode_NonExtendable, 4, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FieldOptions.FeatureSupport",
#endif
};
const upb_MiniTable* google__protobuf__FieldOptions__FeatureSupport_msg_init_ptr = &google__protobuf__FieldOptions__FeatureSupport_msg_init;
static const upb_MiniTableSubInternal google_protobuf_OneofOptions__submsgs[2] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_OneofOptions__fields[2] = {
{1, UPB_SIZE(12, 16), 64, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(16, 24), 0, 1, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__OneofOptions_msg_init = {
&google_protobuf_OneofOptions__submsgs[0],
&google_protobuf_OneofOptions__fields[0],
UPB_SIZE(24, 32), 2, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.OneofOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x001800003f013eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__OneofOptions_msg_init_ptr = &google__protobuf__OneofOptions_msg_init;
static const upb_MiniTableSubInternal google_protobuf_EnumOptions__submsgs[2] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_EnumOptions__fields[5] = {
{2, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{3, 10, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{6, 11, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{7, UPB_SIZE(12, 16), 67, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(16, 24), 0, 1, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__EnumOptions_msg_init = {
&google_protobuf_EnumOptions__submsgs[0],
&google_protobuf_EnumOptions__fields[0],
UPB_SIZE(24, 32), 5, kUpb_ExtMode_Extendable, 0, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.EnumOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x001800003f013eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__EnumOptions_msg_init_ptr = &google__protobuf__EnumOptions_msg_init;
static const upb_MiniTableSubInternal google_protobuf_EnumValueOptions__submsgs[3] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__FieldOptions__FeatureSupport_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_EnumValueOptions__fields[5] = {
{1, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(12, 16), 65, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{3, 10, 66, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{4, UPB_SIZE(16, 24), 67, 1, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(20, 32), 0, 2, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__EnumValueOptions_msg_init = {
&google_protobuf_EnumValueOptions__submsgs[0],
&google_protobuf_EnumValueOptions__fields[0],
UPB_SIZE(24, 40), 5, kUpb_ExtMode_Extendable, 4, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.EnumValueOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x002000003f023eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__EnumValueOptions_msg_init_ptr = &google__protobuf__EnumValueOptions_msg_init;
static const upb_MiniTableSubInternal google_protobuf_ServiceOptions__submsgs[2] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_ServiceOptions__fields[3] = {
{33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{34, UPB_SIZE(12, 16), 65, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(16, 24), 0, 1, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__ServiceOptions_msg_init = {
&google_protobuf_ServiceOptions__submsgs[0],
&google_protobuf_ServiceOptions__fields[0],
UPB_SIZE(24, 32), 3, kUpb_ExtMode_Extendable, 0, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.ServiceOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x001800003f013eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__ServiceOptions_msg_init_ptr = &google__protobuf__ServiceOptions_msg_init;
static const upb_MiniTableSubInternal google_protobuf_MethodOptions__submsgs[3] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__MethodOptions__IdempotencyLevel_enum_init},
};
static const upb_MiniTableField google_protobuf_MethodOptions__fields[4] = {
{33, 9, 64, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{34, 12, 65, 2, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{35, 16, 66, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{999, UPB_SIZE(20, 24), 0, 1, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__MethodOptions_msg_init = {
&google_protobuf_MethodOptions__submsgs[0],
&google_protobuf_MethodOptions__fields[0],
UPB_SIZE(24, 32), 4, kUpb_ExtMode_Extendable, 0, UPB_FASTTABLE_MASK(248), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.MethodOptions",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x001800003f013eba, &upb_prm_2bt_max128b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__MethodOptions_msg_init_ptr = &google__protobuf__MethodOptions_msg_init;
static const upb_MiniTableSubInternal google_protobuf_UninterpretedOption__submsgs[1] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__UninterpretedOption__NamePart_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_UninterpretedOption__fields[7] = {
{2, UPB_SIZE(12, 64), 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{3, 16, 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{4, UPB_SIZE(40, 72), 65, kUpb_NoSub, 4, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)},
{5, UPB_SIZE(48, 80), 66, kUpb_NoSub, 3, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)},
{6, UPB_SIZE(56, 88), 67, kUpb_NoSub, 1, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_8Byte << kUpb_FieldRep_Shift)},
{7, UPB_SIZE(24, 32), 68, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{8, UPB_SIZE(32, 48), 69, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__UninterpretedOption_msg_init = {
&google_protobuf_UninterpretedOption__submsgs[0],
&google_protobuf_UninterpretedOption__fields[0],
UPB_SIZE(64, 96), 7, kUpb_ExtMode_NonExtendable, 0, UPB_FASTTABLE_MASK(24), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.UninterpretedOption",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x004000003f000012, &upb_prm_1bt_max64b},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__UninterpretedOption_msg_init_ptr = &google__protobuf__UninterpretedOption_msg_init;
static const upb_MiniTableField google_protobuf_UninterpretedOption_NamePart__fields[2] = {
{1, UPB_SIZE(12, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{2, 9, 65, kUpb_NoSub, 8, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__UninterpretedOption__NamePart_msg_init = {
NULL,
&google_protobuf_UninterpretedOption_NamePart__fields[0],
UPB_SIZE(24, 32), 2, kUpb_ExtMode_NonExtendable, 2, UPB_FASTTABLE_MASK(255), 2,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.UninterpretedOption.NamePart",
#endif
};
const upb_MiniTable* google__protobuf__UninterpretedOption__NamePart_msg_init_ptr = &google__protobuf__UninterpretedOption__NamePart_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FeatureSet__submsgs[8] = {
{.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__FieldPresence_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__EnumType_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__Utf8Validation_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__MessageEncoding_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__JsonFormat_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__EnforceNamingStyle_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__FeatureSet__VisibilityFeature__DefaultSymbolVisibility_enum_init},
};
static const upb_MiniTableField google_protobuf_FeatureSet__fields[8] = {
{1, 12, 64, 0, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{2, 16, 65, 1, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{3, 20, 66, 2, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{4, 24, 67, 3, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{5, 28, 68, 4, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{6, 32, 69, 5, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{7, 36, 70, 6, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{8, 40, 71, 7, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FeatureSet_msg_init = {
&google_protobuf_FeatureSet__submsgs[0],
&google_protobuf_FeatureSet__fields[0],
48, 8, kUpb_ExtMode_Extendable, 8, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FeatureSet",
#endif
};
const upb_MiniTable* google__protobuf__FeatureSet_msg_init_ptr = &google__protobuf__FeatureSet_msg_init;
const upb_MiniTable google__protobuf__FeatureSet__VisibilityFeature_msg_init = {
NULL,
NULL,
8, 0, kUpb_ExtMode_NonExtendable, 0, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FeatureSet.VisibilityFeature",
#endif
};
const upb_MiniTable* google__protobuf__FeatureSet__VisibilityFeature_msg_init_ptr = &google__protobuf__FeatureSet__VisibilityFeature_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FeatureSetDefaults__submsgs[3] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init},
{.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init},
};
static const upb_MiniTableField google_protobuf_FeatureSetDefaults__fields[3] = {
{1, UPB_SIZE(12, 24), 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{4, UPB_SIZE(16, 12), 64, 1, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{5, UPB_SIZE(20, 16), 65, 2, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FeatureSetDefaults_msg_init = {
&google_protobuf_FeatureSetDefaults__submsgs[0],
&google_protobuf_FeatureSetDefaults__fields[0],
UPB_SIZE(24, 32), 3, kUpb_ExtMode_NonExtendable, 1, UPB_FASTTABLE_MASK(8), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FeatureSetDefaults",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x001800003f00000a, &upb_prm_1bt_max64b},
})
};
const upb_MiniTable* google__protobuf__FeatureSetDefaults_msg_init_ptr = &google__protobuf__FeatureSetDefaults_msg_init;
static const upb_MiniTableSubInternal google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault__submsgs[3] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(submsg) = &google__protobuf__FeatureSet_msg_init_ptr},
{.UPB_PRIVATE(subenum) = &google__protobuf__Edition_enum_init},
};
static const upb_MiniTableField google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault__fields[3] = {
{3, 12, 64, 2, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{4, 16, 65, 0, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{5, UPB_SIZE(20, 24), 66, 1, 11, (int)kUpb_FieldMode_Scalar | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init = {
&google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault__submsgs[0],
&google_protobuf_FeatureSetDefaults_FeatureSetEditionDefault__fields[0],
UPB_SIZE(24, 32), 3, kUpb_ExtMode_NonExtendable, 0, UPB_FASTTABLE_MASK(255), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.FeatureSetDefaults.FeatureSetEditionDefault",
#endif
};
const upb_MiniTable* google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init_ptr = &google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init;
static const upb_MiniTableSubInternal google_protobuf_SourceCodeInfo__submsgs[1] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__SourceCodeInfo__Location_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_SourceCodeInfo__fields[1] = {
{1, 8, 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__SourceCodeInfo_msg_init = {
&google_protobuf_SourceCodeInfo__submsgs[0],
&google_protobuf_SourceCodeInfo__fields[0],
16, 1, kUpb_ExtMode_Extendable, 1, UPB_FASTTABLE_MASK(8), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.SourceCodeInfo",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x000800003f00000a, &upb_prm_1bt_max128b},
})
};
const upb_MiniTable* google__protobuf__SourceCodeInfo_msg_init_ptr = &google__protobuf__SourceCodeInfo_msg_init;
static const upb_MiniTableField google_protobuf_SourceCodeInfo_Location__fields[5] = {
{1, UPB_SIZE(12, 48), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(16, 56), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(24, 16), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{4, 32, 65, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{6, UPB_SIZE(20, 64), 0, kUpb_NoSub, 12, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsAlternate | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__SourceCodeInfo__Location_msg_init = {
NULL,
&google_protobuf_SourceCodeInfo_Location__fields[0],
UPB_SIZE(40, 72), 5, kUpb_ExtMode_NonExtendable, 4, UPB_FASTTABLE_MASK(56), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.SourceCodeInfo.Location",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x003000003f00000a, &upb_ppv4_1bt},
{0x003800003f000012, &upb_ppv4_1bt},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x004000003f000032, &upb_prs_1bt},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
};
const upb_MiniTable* google__protobuf__SourceCodeInfo__Location_msg_init_ptr = &google__protobuf__SourceCodeInfo__Location_msg_init;
static const upb_MiniTableSubInternal google_protobuf_GeneratedCodeInfo__submsgs[1] = {
{.UPB_PRIVATE(submsg) = &google__protobuf__GeneratedCodeInfo__Annotation_msg_init_ptr},
};
static const upb_MiniTableField google_protobuf_GeneratedCodeInfo__fields[1] = {
{1, 8, 0, 0, 11, (int)kUpb_FieldMode_Array | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__GeneratedCodeInfo_msg_init = {
&google_protobuf_GeneratedCodeInfo__submsgs[0],
&google_protobuf_GeneratedCodeInfo__fields[0],
16, 1, kUpb_ExtMode_NonExtendable, 1, UPB_FASTTABLE_MASK(8), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.GeneratedCodeInfo",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x000800003f00000a, &upb_prm_1bt_max64b},
})
};
const upb_MiniTable* google__protobuf__GeneratedCodeInfo_msg_init_ptr = &google__protobuf__GeneratedCodeInfo_msg_init;
static const upb_MiniTableSubInternal google_protobuf_GeneratedCodeInfo_Annotation__submsgs[1] = {
{.UPB_PRIVATE(subenum) = &google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init},
};
static const upb_MiniTableField google_protobuf_GeneratedCodeInfo_Annotation__fields[5] = {
{1, UPB_SIZE(12, 40), 0, kUpb_NoSub, 5, (int)kUpb_FieldMode_Array | (int)kUpb_LabelFlags_IsPacked | ((int)UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte) << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(28, 24), 64, kUpb_NoSub, 12, (int)kUpb_FieldMode_Scalar | (int)kUpb_LabelFlags_IsAlternate | ((int)kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(16, 12), 65, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{4, UPB_SIZE(20, 16), 66, kUpb_NoSub, 5, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{5, UPB_SIZE(24, 20), 67, 0, 14, (int)kUpb_FieldMode_Scalar | ((int)kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
};
const upb_MiniTable google__protobuf__GeneratedCodeInfo__Annotation_msg_init = {
&google_protobuf_GeneratedCodeInfo_Annotation__submsgs[0],
&google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
UPB_SIZE(40, 48), 5, kUpb_ExtMode_NonExtendable, 5, UPB_FASTTABLE_MASK(8), 0,
#ifdef UPB_TRACING_ENABLED
"google.protobuf.GeneratedCodeInfo.Annotation",
#endif
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x002800003f00000a, &upb_ppv4_1bt},
})
};
const upb_MiniTable* google__protobuf__GeneratedCodeInfo__Annotation_msg_init_ptr = &google__protobuf__GeneratedCodeInfo__Annotation_msg_init;
const upb_MiniTableEnum google__protobuf__Edition_enum_init = {
64,
9,
{
0x7,
0x0,
0x384,
0x3e6,
0x3e7,
0x3e8,
0x3e9,
0x1869d,
0x1869e,
0x1869f,
0x7fffffff,
},
};
const upb_MiniTableEnum google__protobuf__ExtensionRangeOptions__VerificationState_enum_init = {
64,
0,
{
0x3,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FeatureSet__EnforceNamingStyle_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FeatureSet__EnumType_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FeatureSet__FieldPresence_enum_init = {
64,
0,
{
0xf,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FeatureSet__JsonFormat_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FeatureSet__MessageEncoding_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FeatureSet__Utf8Validation_enum_init = {
64,
0,
{
0xd,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FeatureSet__VisibilityFeature__DefaultSymbolVisibility_enum_init = {
64,
0,
{
0x1f,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FieldDescriptorProto__Label_enum_init = {
64,
0,
{
0xe,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FieldDescriptorProto__Type_enum_init = {
64,
0,
{
0x7fffe,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FieldOptions__CType_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FieldOptions__JSType_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FieldOptions__OptionRetention_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FieldOptions__OptionTargetType_enum_init = {
64,
0,
{
0x3ff,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__FileOptions__OptimizeMode_enum_init = {
64,
0,
{
0xe,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__MethodOptions__IdempotencyLevel_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
const upb_MiniTableEnum google__protobuf__SymbolVisibility_enum_init = {
64,
0,
{
0x7,
0x0,
},
};
static const upb_MiniTable *messages_layout[34] = {
&google__protobuf__FileDescriptorSet_msg_init,
&google__protobuf__FileDescriptorProto_msg_init,
&google__protobuf__DescriptorProto_msg_init,
&google__protobuf__DescriptorProto__ExtensionRange_msg_init,
&google__protobuf__DescriptorProto__ReservedRange_msg_init,
&google__protobuf__ExtensionRangeOptions_msg_init,
&google__protobuf__ExtensionRangeOptions__Declaration_msg_init,
&google__protobuf__FieldDescriptorProto_msg_init,
&google__protobuf__OneofDescriptorProto_msg_init,
&google__protobuf__EnumDescriptorProto_msg_init,
&google__protobuf__EnumDescriptorProto__EnumReservedRange_msg_init,
&google__protobuf__EnumValueDescriptorProto_msg_init,
&google__protobuf__ServiceDescriptorProto_msg_init,
&google__protobuf__MethodDescriptorProto_msg_init,
&google__protobuf__FileOptions_msg_init,
&google__protobuf__MessageOptions_msg_init,
&google__protobuf__FieldOptions_msg_init,
&google__protobuf__FieldOptions__EditionDefault_msg_init,
&google__protobuf__FieldOptions__FeatureSupport_msg_init,
&google__protobuf__OneofOptions_msg_init,
&google__protobuf__EnumOptions_msg_init,
&google__protobuf__EnumValueOptions_msg_init,
&google__protobuf__ServiceOptions_msg_init,
&google__protobuf__MethodOptions_msg_init,
&google__protobuf__UninterpretedOption_msg_init,
&google__protobuf__UninterpretedOption__NamePart_msg_init,
&google__protobuf__FeatureSet_msg_init,
&google__protobuf__FeatureSet__VisibilityFeature_msg_init,
&google__protobuf__FeatureSetDefaults_msg_init,
&google__protobuf__FeatureSetDefaults__FeatureSetEditionDefault_msg_init,
&google__protobuf__SourceCodeInfo_msg_init,
&google__protobuf__SourceCodeInfo__Location_msg_init,
&google__protobuf__GeneratedCodeInfo_msg_init,
&google__protobuf__GeneratedCodeInfo__Annotation_msg_init,
};
static const upb_MiniTableEnum *enums_layout[20] = {
&google__protobuf__Edition_enum_init,
&google__protobuf__ExtensionRangeOptions__VerificationState_enum_init,
&google__protobuf__FeatureSet__EnforceNamingStyle_enum_init,
&google__protobuf__FeatureSet__EnumType_enum_init,
&google__protobuf__FeatureSet__FieldPresence_enum_init,
&google__protobuf__FeatureSet__JsonFormat_enum_init,
&google__protobuf__FeatureSet__MessageEncoding_enum_init,
&google__protobuf__FeatureSet__RepeatedFieldEncoding_enum_init,
&google__protobuf__FeatureSet__Utf8Validation_enum_init,
&google__protobuf__FeatureSet__VisibilityFeature__DefaultSymbolVisibility_enum_init,
&google__protobuf__FieldDescriptorProto__Label_enum_init,
&google__protobuf__FieldDescriptorProto__Type_enum_init,
&google__protobuf__FieldOptions__CType_enum_init,
&google__protobuf__FieldOptions__JSType_enum_init,
&google__protobuf__FieldOptions__OptionRetention_enum_init,
&google__protobuf__FieldOptions__OptionTargetType_enum_init,
&google__protobuf__FileOptions__OptimizeMode_enum_init,
&google__protobuf__GeneratedCodeInfo__Annotation__Semantic_enum_init,
&google__protobuf__MethodOptions__IdempotencyLevel_enum_init,
&google__protobuf__SymbolVisibility_enum_init,
};
const upb_MiniTableFile google_protobuf_descriptor_proto_upb_file_layout = {
messages_layout,
enums_layout,
NULL,
34,
20,
0,
};
/*
* upb_table Implementation
*
* Implementation is heavily inspired by Lua's ltable.c.
*/
#include <stdint.h>
#include <string.h>
// Must be last.
#define UPB_MAXARRSIZE 16 // 2**16 = 64k.
// From Chromium.
#define ARRAY_SIZE(x) \
((sizeof(x) / sizeof(0 [x])) / ((size_t)(!(sizeof(x) % sizeof(0 [x])))))
/* The minimum utilization of the array part of a mixed hash/array table. This
* is a speed/memory-usage tradeoff (though it's not straightforward because of
* cache effects). The lower this is, the more memory we'll use. */
static const double MIN_DENSITY = 0.1;
#if defined(__has_builtin)
#if __has_builtin(__builtin_popcount)
#define UPB_FAST_POPCOUNT32(i) __builtin_popcount(i)
#endif
#elif defined(__GNUC__)
#define UPB_FAST_POPCOUNT32(i) __builtin_popcount(i)
#elif defined(_MSC_VER)
#define UPB_FAST_POPCOUNT32(i) __popcnt(i)
#endif
UPB_INLINE int _upb_popcnt32(uint32_t i) {
#ifdef UPB_FAST_POPCOUNT32
return UPB_FAST_POPCOUNT32(i);
#else
int count = 0;
while (i != 0) {
count += i & 1;
i >>= 1;
}
return count;
#endif
}
#undef UPB_FAST_POPCOUNT32
UPB_INLINE uint8_t _upb_log2_table_size(upb_table* t) {
return _upb_popcnt32(t->mask);
}
static bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
static int log2ceil(uint64_t v) {
int ret = 0;
bool pow2 = is_pow2(v);
while (v >>= 1) ret++;
ret = pow2 ? ret : ret + 1; // Ceiling.
return UPB_MIN(UPB_MAXARRSIZE, ret);
}
/* A type to represent the lookup key of either a strtable or an inttable. */
typedef union {
uintptr_t num;
upb_StringView str;
} lookupkey_t;
static lookupkey_t strkey2(const char* str, size_t len) {
return (lookupkey_t){.str = upb_StringView_FromDataAndSize(str, len)};
}
static lookupkey_t intkey(uintptr_t key) { return (lookupkey_t){.num = key}; }
typedef uint32_t hashfunc_t(upb_key key);
typedef bool eqlfunc_t(upb_key k1, lookupkey_t k2);
/* Base table (shared code) ***************************************************/
static uint32_t upb_inthash(uintptr_t key) {
if (sizeof(uintptr_t) == 8) {
return (uint32_t)key ^ (uint32_t)(key >> 32);
} else {
UPB_ASSERT(sizeof(uintptr_t) == 4);
return (uint32_t)key;
}
}
static const upb_tabent* upb_getentry(const upb_table* t, uint32_t hash) {
return t->entries + (hash & t->mask);
}
static bool isfull(upb_table* t) {
uint32_t size = upb_table_size(t);
// 0.875 load factor
return t->count == (size - (size >> 3));
}
static bool init(upb_table* t, uint8_t size_lg2, upb_Arena* a) {
if (size_lg2 >= 32) {
return false;
}
t->count = 0;
uint32_t size = 1 << size_lg2;
t->mask = size - 1; // 0 mask if size_lg2 is 0
if (upb_table_size(t) > (SIZE_MAX / sizeof(upb_tabent))) {
return false;
}
size_t bytes = upb_table_size(t) * sizeof(upb_tabent);
if (bytes > 0) {
t->entries = upb_Arena_Malloc(a, bytes);
if (!t->entries) return false;
memset(t->entries, 0, bytes);
} else {
t->entries = NULL;
}
return true;
}
static upb_tabent* emptyent(upb_table* t, upb_tabent* e) {
upb_tabent* begin = t->entries;
upb_tabent* end = begin + upb_table_size(t);
for (e = e + 1; e < end; e++) {
if (upb_tabent_isempty(e)) return e;
}
for (e = begin; e < end; e++) {
if (upb_tabent_isempty(e)) return e;
}
UPB_ASSERT(false);
return NULL;
}
static upb_tabent* getentry_mutable(upb_table* t, uint32_t hash) {
return (upb_tabent*)upb_getentry(t, hash);
}
static const upb_tabent* findentry(const upb_table* t, lookupkey_t key,
uint32_t hash, eqlfunc_t* eql) {
const upb_tabent* e;
if (t->count == 0) return NULL;
e = upb_getentry(t, hash);
if (upb_tabent_isempty(e)) return NULL;
while (1) {
if (eql(e->key, key)) return e;
if ((e = e->next) == NULL) return NULL;
}
}
static upb_tabent* findentry_mutable(upb_table* t, lookupkey_t key,
uint32_t hash, eqlfunc_t* eql) {
return (upb_tabent*)findentry(t, key, hash, eql);
}
static bool lookup(const upb_table* t, lookupkey_t key, upb_value* v,
uint32_t hash, eqlfunc_t* eql) {
const upb_tabent* e = findentry(t, key, hash, eql);
if (e) {
if (v) *v = e->val;
return true;
} else {
return false;
}
}
/* The given key must not already exist in the table. */
static void insert(upb_table* t, lookupkey_t key, upb_key tabkey, upb_value val,
uint32_t hash, hashfunc_t* hashfunc, eqlfunc_t* eql) {
upb_tabent* mainpos_e;
upb_tabent* our_e;
UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
t->count++;
mainpos_e = getentry_mutable(t, hash);
our_e = mainpos_e;
if (upb_tabent_isempty(mainpos_e)) {
/* Our main position is empty; use it. */
our_e->next = NULL;
} else {
/* Collision. */
upb_tabent* new_e = emptyent(t, mainpos_e);
/* Head of collider's chain. */
upb_tabent* chain = getentry_mutable(t, hashfunc(mainpos_e->key));
if (chain == mainpos_e) {
/* Existing ent is in its main position (it has the same hash as us, and
* is the head of our chain). Insert to new ent and append to this chain.
*/
new_e->next = mainpos_e->next;
mainpos_e->next = new_e;
our_e = new_e;
} else {
/* Existing ent is not in its main position (it is a node in some other
* chain). This implies that no existing ent in the table has our hash.
* Evict it (updating its chain) and use its ent for head of our chain. */
*new_e = *mainpos_e; /* copies next. */
while (chain->next != mainpos_e) {
chain = (upb_tabent*)chain->next;
UPB_ASSERT(chain);
}
chain->next = new_e;
our_e = mainpos_e;
our_e->next = NULL;
}
}
our_e->key = tabkey;
our_e->val = val;
UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
}
static bool rm(upb_table* t, lookupkey_t key, upb_value* val, uint32_t hash,
eqlfunc_t* eql) {
upb_tabent* chain = getentry_mutable(t, hash);
if (upb_tabent_isempty(chain)) return false;
if (eql(chain->key, key)) {
/* Element to remove is at the head of its chain. */
t->count--;
if (val) *val = chain->val;
if (chain->next) {
upb_tabent* move = (upb_tabent*)chain->next;
*chain = *move;
move->key = upb_key_empty();
} else {
chain->key = upb_key_empty();
}
return true;
} else {
/* Element to remove is either in a non-head position or not in the
* table. */
while (chain->next && !eql(chain->next->key, key)) {
chain = (upb_tabent*)chain->next;
}
if (chain->next) {
/* Found element to remove. */
upb_tabent* rm = (upb_tabent*)chain->next;
t->count--;
if (val) *val = chain->next->val;
rm->key = upb_key_empty();
chain->next = rm->next;
return true;
} else {
/* Element to remove is not in the table. */
return false;
}
}
}
static size_t next(const upb_table* t, size_t i) {
do {
if (++i >= upb_table_size(t)) return SIZE_MAX - 1; /* Distinct from -1. */
} while (upb_tabent_isempty(&t->entries[i]));
return i;
}
static size_t begin(const upb_table* t) { return next(t, -1); }
/* upb_strtable ***************************************************************/
// A simple "subclass" of upb_table that only adds a hash function for strings.
static upb_SizePrefixString* upb_SizePrefixString_Copy(upb_StringView s,
upb_Arena* a) {
// A 2GB string will fail at serialization time, but we accept up to 4GB in
// memory here.
if (s.size > UINT32_MAX) return NULL;
upb_SizePrefixString* str =
upb_Arena_Malloc(a, sizeof(uint32_t) + s.size + 1);
if (str == NULL) return NULL;
str->size = s.size;
char* data = (char*)str->data;
if (s.size) memcpy(data, s.data, s.size);
data[s.size] = '\0';
return str;
}
/* Adapted from ABSL's wyhash. */
static uint64_t UnalignedLoad64(const void* p) {
uint64_t val;
memcpy(&val, p, 8);
return val;
}
static uint32_t UnalignedLoad32(const void* p) {
uint32_t val;
memcpy(&val, p, 4);
return val;
}
#if defined(_MSC_VER) && defined(_M_X64)
#include <intrin.h>
#endif
/* Computes a * b, returning the low 64 bits of the result and storing the high
* 64 bits in |*high|. */
static uint64_t upb_umul128(uint64_t v0, uint64_t v1, uint64_t* out_high) {
#ifdef __SIZEOF_INT128__
__uint128_t p = v0;
p *= v1;
*out_high = (uint64_t)(p >> 64);
return (uint64_t)p;
#elif defined(_MSC_VER) && defined(_M_X64)
return _umul128(v0, v1, out_high);
#else
uint64_t a32 = v0 >> 32;
uint64_t a00 = v0 & 0xffffffff;
uint64_t b32 = v1 >> 32;
uint64_t b00 = v1 & 0xffffffff;
uint64_t high = a32 * b32;
uint64_t low = a00 * b00;
uint64_t mid1 = a32 * b00;
uint64_t mid2 = a00 * b32;
low += (mid1 << 32) + (mid2 << 32);
// Omit carry bit, for mixing we do not care about exact numerical precision.
high += (mid1 >> 32) + (mid2 >> 32);
*out_high = high;
return low;
#endif
}
static uint64_t WyhashMix(uint64_t v0, uint64_t v1) {
uint64_t high;
uint64_t low = upb_umul128(v0, v1, &high);
return low ^ high;
}
static uint64_t Wyhash(const void* data, size_t len, uint64_t seed,
const uint64_t salt[]) {
const uint8_t* ptr = (const uint8_t*)data;
uint64_t starting_length = (uint64_t)len;
uint64_t current_state = seed ^ salt[0];
if (len > 64) {
// If we have more than 64 bytes, we're going to handle chunks of 64
// bytes at a time. We're going to build up two separate hash states
// which we will then hash together.
uint64_t duplicated_state = current_state;
do {
uint64_t a = UnalignedLoad64(ptr);
uint64_t b = UnalignedLoad64(ptr + 8);
uint64_t c = UnalignedLoad64(ptr + 16);
uint64_t d = UnalignedLoad64(ptr + 24);
uint64_t e = UnalignedLoad64(ptr + 32);
uint64_t f = UnalignedLoad64(ptr + 40);
uint64_t g = UnalignedLoad64(ptr + 48);
uint64_t h = UnalignedLoad64(ptr + 56);
uint64_t cs0 = WyhashMix(a ^ salt[1], b ^ current_state);
uint64_t cs1 = WyhashMix(c ^ salt[2], d ^ current_state);
current_state = (cs0 ^ cs1);
uint64_t ds0 = WyhashMix(e ^ salt[3], f ^ duplicated_state);
uint64_t ds1 = WyhashMix(g ^ salt[4], h ^ duplicated_state);
duplicated_state = (ds0 ^ ds1);
ptr += 64;
len -= 64;
} while (len > 64);
current_state = current_state ^ duplicated_state;
}
// We now have a data `ptr` with at most 64 bytes and the current state
// of the hashing state machine stored in current_state.
while (len > 16) {
uint64_t a = UnalignedLoad64(ptr);
uint64_t b = UnalignedLoad64(ptr + 8);
current_state = WyhashMix(a ^ salt[1], b ^ current_state);
ptr += 16;
len -= 16;
}
// We now have a data `ptr` with at most 16 bytes.
uint64_t a = 0;
uint64_t b = 0;
if (len > 8) {
// When we have at least 9 and at most 16 bytes, set A to the first 64
// bits of the input and B to the last 64 bits of the input. Yes, they will
// overlap in the middle if we are working with less than the full 16
// bytes.
a = UnalignedLoad64(ptr);
b = UnalignedLoad64(ptr + len - 8);
} else if (len > 3) {
// If we have at least 4 and at most 8 bytes, set A to the first 32
// bits and B to the last 32 bits.
a = UnalignedLoad32(ptr);
b = UnalignedLoad32(ptr + len - 4);
} else if (len > 0) {
// If we have at least 1 and at most 3 bytes, read all of the provided
// bits into A, with some adjustments.
a = ((ptr[0] << 16) | (ptr[len >> 1] << 8) | ptr[len - 1]);
b = 0;
} else {
a = 0;
b = 0;
}
uint64_t w = WyhashMix(a ^ salt[1], b ^ current_state);
uint64_t z = salt[1] ^ starting_length;
return WyhashMix(w, z);
}
const uint64_t kWyhashSalt[5] = {
0x243F6A8885A308D3ULL, 0x13198A2E03707344ULL, 0xA4093822299F31D0ULL,
0x082EFA98EC4E6C89ULL, 0x452821E638D01377ULL,
};
uint32_t _upb_Hash(const void* p, size_t n, uint64_t seed) {
return Wyhash(p, n, seed, kWyhashSalt);
}
static const void* const _upb_seed;
// Returns a random seed for upb's hash function. This does not provide
// high-quality randomness, but it should be enough to prevent unit tests from
// relying on a deterministic map ordering. By returning the address of a
// variable, we are able to get some randomness for free provided that ASLR is
// enabled.
static uint64_t _upb_Seed(void) { return (uint64_t)&_upb_seed; }
static uint32_t _upb_Hash_NoSeed(const char* p, size_t n) {
return _upb_Hash(p, n, _upb_Seed());
}
static uint32_t strhash(upb_key key) {
return _upb_Hash_NoSeed(key.str->data, key.str->size);
}
static bool streql(upb_key k1, lookupkey_t k2) {
const upb_SizePrefixString* k1s = k1.str;
const upb_StringView k2s = k2.str;
return k1s->size == k2s.size &&
(k1s->size == 0 || memcmp(k1s->data, k2s.data, k1s->size) == 0);
}
/** Calculates the number of entries required to hold an expected number of
* values, within the table's load factor. */
static size_t _upb_entries_needed_for(size_t expected_size) {
size_t need_entries = expected_size + 1 + expected_size / 7;
UPB_ASSERT(need_entries - (need_entries >> 3) >= expected_size);
return need_entries;
}
bool upb_strtable_init(upb_strtable* t, size_t expected_size, upb_Arena* a) {
int size_lg2 = upb_Log2Ceiling(_upb_entries_needed_for(expected_size));
return init(&t->t, size_lg2, a);
}
void upb_strtable_clear(upb_strtable* t) {
size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent);
t->t.count = 0;
memset((char*)t->t.entries, 0, bytes);
}
bool upb_strtable_resize(upb_strtable* t, size_t size_lg2, upb_Arena* a) {
upb_strtable new_table;
if (!init(&new_table.t, size_lg2, a)) return false;
intptr_t iter = UPB_STRTABLE_BEGIN;
upb_StringView sv;
upb_value val;
while (upb_strtable_next2(t, &sv, &val, &iter)) {
// Unlike normal insert, does not copy string data or possibly reallocate
// the table
// The data pointer used in the table is guaranteed to point at a
// upb_SizePrefixString, we just need to back up by the size of the uint32_t
// length prefix.
const upb_SizePrefixString* keystr =
(const upb_SizePrefixString*)(sv.data - sizeof(uint32_t));
UPB_ASSERT(keystr->data == sv.data);
UPB_ASSERT(keystr->size == sv.size);
lookupkey_t lookupkey = {.str = sv};
upb_key tabkey = {.str = keystr};
uint32_t hash = _upb_Hash_NoSeed(sv.data, sv.size);
insert(&new_table.t, lookupkey, tabkey, val, hash, &strhash, &streql);
}
*t = new_table;
return true;
}
bool upb_strtable_insert(upb_strtable* t, const char* k, size_t len,
upb_value v, upb_Arena* a) {
if (isfull(&t->t)) {
/* Need to resize. New table of double the size, add old elements to it. */
if (!upb_strtable_resize(t, _upb_log2_table_size(&t->t) + 1, a)) {
return false;
}
}
upb_StringView sv = upb_StringView_FromDataAndSize(k, len);
upb_SizePrefixString* size_prefix_string = upb_SizePrefixString_Copy(sv, a);
if (!size_prefix_string) return false;
lookupkey_t lookupkey = {.str = sv};
upb_key key = {.str = size_prefix_string};
uint32_t hash = _upb_Hash_NoSeed(k, len);
insert(&t->t, lookupkey, key, v, hash, &strhash, &streql);
return true;
}
bool upb_strtable_lookup2(const upb_strtable* t, const char* key, size_t len,
upb_value* v) {
uint32_t hash = _upb_Hash_NoSeed(key, len);
return lookup(&t->t, strkey2(key, len), v, hash, &streql);
}
bool upb_strtable_remove2(upb_strtable* t, const char* key, size_t len,
upb_value* val) {
uint32_t hash = _upb_Hash_NoSeed(key, len);
return rm(&t->t, strkey2(key, len), val, hash, &streql);
}
/* Iteration */
void upb_strtable_begin(upb_strtable_iter* i, const upb_strtable* t) {
i->t = t;
i->index = begin(&t->t);
}
void upb_strtable_next(upb_strtable_iter* i) {
i->index = next(&i->t->t, i->index);
}
bool upb_strtable_done(const upb_strtable_iter* i) {
if (!i->t) return true;
return i->index >= upb_table_size(&i->t->t) ||
upb_tabent_isempty(str_tabent(i));
}
upb_StringView upb_strtable_iter_key(const upb_strtable_iter* i) {
UPB_ASSERT(!upb_strtable_done(i));
return upb_key_strview(str_tabent(i)->key);
}
upb_value upb_strtable_iter_value(const upb_strtable_iter* i) {
UPB_ASSERT(!upb_strtable_done(i));
return str_tabent(i)->val;
}
void upb_strtable_iter_setdone(upb_strtable_iter* i) {
i->t = NULL;
i->index = SIZE_MAX;
}
bool upb_strtable_iter_isequal(const upb_strtable_iter* i1,
const upb_strtable_iter* i2) {
if (upb_strtable_done(i1) && upb_strtable_done(i2)) return true;
return i1->t == i2->t && i1->index == i2->index;
}
bool upb_strtable_next2(const upb_strtable* t, upb_StringView* key,
upb_value* val, intptr_t* iter) {
size_t tab_idx = next(&t->t, *iter);
if (tab_idx < upb_table_size(&t->t)) {
upb_tabent* ent = &t->t.entries[tab_idx];
*key = upb_key_strview(ent->key);
*val = ent->val;
*iter = tab_idx;
return true;
}
return false;
}
void upb_strtable_removeiter(upb_strtable* t, intptr_t* iter) {
intptr_t i = *iter;
upb_tabent* ent = &t->t.entries[i];
upb_tabent* prev = NULL;
// Linear search, not great.
upb_tabent* end = &t->t.entries[upb_table_size(&t->t)];
for (upb_tabent* e = t->t.entries; e != end; e++) {
if (e->next == ent) {
prev = e;
break;
}
}
if (prev) {
prev->next = ent->next;
}
t->t.count--;
ent->key = upb_key_empty();
ent->next = NULL;
}
void upb_strtable_setentryvalue(upb_strtable* t, intptr_t iter, upb_value v) {
t->t.entries[iter].val = v;
}
/* upb_inttable ***************************************************************/
/* For inttables we use a hybrid structure where small keys are kept in an
* array and large keys are put in the hash table. */
// The sentinel value used in the dense array part. Note that callers must
// ensure that inttable is never used with a value of this sentinel type
// (pointers and u32 values will never be; i32 needs to be handled carefully
// to avoid sign-extending into this value).
static const upb_value kInttableSentinel = {.val = UINT64_MAX};
static uint32_t presence_mask_arr_size(uint32_t array_size) {
return (array_size + 7) / 8; // sizeof(uint8_t) is always 1.
}
static uint32_t inthash(upb_key key) { return upb_inthash(key.num); }
static bool inteql(upb_key k1, lookupkey_t k2) { return k1.num == k2.num; }
static upb_value* mutable_array(upb_inttable* t) {
return (upb_value*)t->array;
}
static const upb_value* inttable_array_get(const upb_inttable* t,
uintptr_t key) {
UPB_ASSERT(key < t->array_size);
const upb_value* val = &t->array[key];
return upb_inttable_arrhas(t, key) ? val : NULL;
}
static upb_value* inttable_val(upb_inttable* t, uintptr_t key) {
if (key < t->array_size) {
return (upb_value*)inttable_array_get(t, key);
} else {
upb_tabent* e =
findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
return e ? &e->val : NULL;
}
}
static const upb_value* inttable_val_const(const upb_inttable* t,
uintptr_t key) {
return inttable_val((upb_inttable*)t, key);
}
size_t upb_inttable_count(const upb_inttable* t) {
return t->t.count + t->array_count;
}
static void check(upb_inttable* t) {
UPB_UNUSED(t);
#if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
{
// This check is very expensive (makes inserts/deletes O(N)).
size_t count = 0;
intptr_t iter = UPB_INTTABLE_BEGIN;
uintptr_t key;
upb_value val;
while (upb_inttable_next(t, &key, &val, &iter)) {
UPB_ASSERT(upb_inttable_lookup(t, key, NULL));
}
UPB_ASSERT(count == upb_inttable_count(t));
}
#endif
}
bool upb_inttable_sizedinit(upb_inttable* t, uint32_t asize, int hsize_lg2,
upb_Arena* a) {
if (!init(&t->t, hsize_lg2, a)) return false;
/* Always make the array part at least 1 long, so that we know key 0
* won't be in the hash part, which simplifies things. */
t->array_size = UPB_MAX(1, asize);
t->array_count = 0;
#if UINT32_MAX >= SIZE_MAX
if (UPB_UNLIKELY(SIZE_MAX / sizeof(upb_value) < t->array_size)) {
return false;
}
#endif
// Allocate the array part and the presence mask array in one allocation.
size_t array_bytes = t->array_size * sizeof(upb_value);
uint32_t presence_bytes = presence_mask_arr_size(t->array_size);
uintptr_t total_bytes = array_bytes + presence_bytes;
if (UPB_UNLIKELY(total_bytes > SIZE_MAX)) {
return false;
}
void* alloc = upb_Arena_Malloc(a, total_bytes);
if (!alloc) {
return false;
}
t->array = alloc;
memset(mutable_array(t), 0xff, array_bytes);
t->presence_mask = (uint8_t*)alloc + array_bytes;
memset((uint8_t*)t->presence_mask, 0, presence_bytes);
check(t);
return true;
}
bool upb_inttable_init(upb_inttable* t, upb_Arena* a) {
// The init size of the table part to match that of strtable.
return upb_inttable_sizedinit(t, 0, 3, a);
}
bool upb_inttable_insert(upb_inttable* t, uintptr_t key, upb_value val,
upb_Arena* a) {
if (key < t->array_size) {
UPB_ASSERT(!upb_inttable_arrhas(t, key));
t->array_count++;
mutable_array(t)[key] = val;
((uint8_t*)t->presence_mask)[key / 8] |= (1 << (key % 8));
} else {
if (isfull(&t->t)) {
/* Need to resize the hash part, but we re-use the array part. */
size_t i;
upb_table new_table;
if (!init(&new_table, _upb_log2_table_size(&t->t) + 1, a)) {
return false;
}
for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
const upb_tabent* e = &t->t.entries[i];
insert(&new_table, intkey(e->key.num), e->key, e->val, inthash(e->key),
&inthash, &inteql);
}
UPB_ASSERT(t->t.count == new_table.count);
t->t = new_table;
}
upb_key tabkey = {.num = key};
insert(&t->t, intkey(key), tabkey, val, upb_inthash(key), &inthash,
&inteql);
}
check(t);
return true;
}
bool upb_inttable_lookup(const upb_inttable* t, uintptr_t key, upb_value* v) {
const upb_value* table_v = inttable_val_const(t, key);
if (!table_v) return false;
if (v) *v = *table_v;
return true;
}
bool upb_inttable_replace(upb_inttable* t, uintptr_t key, upb_value val) {
upb_value* table_v = inttable_val(t, key);
if (!table_v) return false;
*table_v = val;
return true;
}
bool upb_inttable_remove(upb_inttable* t, uintptr_t key, upb_value* val) {
bool success;
if (key < t->array_size) {
if (upb_inttable_arrhas(t, key)) {
t->array_count--;
if (val) {
*val = t->array[key];
}
mutable_array(t)[key] = kInttableSentinel;
((uint8_t*)t->presence_mask)[key / 8] &= ~(1 << (key % 8));
success = true;
} else {
success = false;
}
} else {
success = rm(&t->t, intkey(key), val, upb_inthash(key), &inteql);
}
check(t);
return success;
}
bool upb_inttable_compact(upb_inttable* t, upb_Arena* a) {
/* A power-of-two histogram of the table keys. */
uint32_t counts[UPB_MAXARRSIZE + 1] = {0};
/* The max key in each bucket. */
uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
{
intptr_t iter = UPB_INTTABLE_BEGIN;
uintptr_t key;
upb_value val;
while (upb_inttable_next(t, &key, &val, &iter)) {
int bucket = log2ceil(key);
max[bucket] = UPB_MAX(max[bucket], key);
counts[bucket]++;
}
}
/* Find the largest power of two that satisfies the MIN_DENSITY
* definition (while actually having some keys). */
uint32_t arr_count = upb_inttable_count(t);
// Scan all buckets except capped bucket
int size_lg2 = ARRAY_SIZE(counts) - 1;
for (; size_lg2 > 0; size_lg2--) {
if (counts[size_lg2] == 0) {
/* We can halve again without losing any entries. */
continue;
} else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
break;
}
arr_count -= counts[size_lg2];
}
UPB_ASSERT(arr_count <= upb_inttable_count(t));
upb_inttable new_t;
{
/* Insert all elements into new, perfectly-sized table. */
uintptr_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
uint32_t hash_count = upb_inttable_count(t) - arr_count;
size_t hash_size = hash_count ? _upb_entries_needed_for(hash_count) : 0;
int hashsize_lg2 = log2ceil(hash_size);
if (!upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a)) {
return false;
}
{
intptr_t iter = UPB_INTTABLE_BEGIN;
uintptr_t key;
upb_value val;
while (upb_inttable_next(t, &key, &val, &iter)) {
upb_inttable_insert(&new_t, key, val, a);
}
}
UPB_ASSERT(new_t.array_size == arr_size);
}
*t = new_t;
return true;
}
void upb_inttable_clear(upb_inttable* t) {
// Clear the array part.
size_t array_bytes = t->array_size * sizeof(upb_value);
t->array_count = 0;
// Clear the array by setting all bits to 1, as UINT64_MAX is the sentinel
// value for an empty array.
memset(mutable_array(t), 0xff, array_bytes);
// Clear the presence mask array.
memset((uint8_t*)t->presence_mask, 0, presence_mask_arr_size(t->array_size));
// Clear the table part.
size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent);
t->t.count = 0;
memset((char*)t->t.entries, 0, bytes);
}
// Iteration.
bool upb_inttable_next(const upb_inttable* t, uintptr_t* key, upb_value* val,
intptr_t* iter) {
intptr_t i = *iter;
if ((size_t)(i + 1) <= t->array_size) {
while ((size_t)++i < t->array_size) {
const upb_value* ent = inttable_array_get(t, i);
if (ent) {
*key = i;
*val = *ent;
*iter = i;
return true;
}
}
i--; // Back up to exactly one position before the start of the table.
}
size_t tab_idx = next(&t->t, i - t->array_size);
if (tab_idx < upb_table_size(&t->t)) {
upb_tabent* ent = &t->t.entries[tab_idx];
*key = ent->key.num;
*val = ent->val;
*iter = tab_idx + t->array_size;
return true;
} else {
// We should set the iterator any way. When we are done, the iterator value
// is invalidated. `upb_inttable_done` will check on the iterator value to
// determine if the iteration is done.
*iter = INTPTR_MAX - 1; // To disambiguate from UPB_INTTABLE_BEGIN, to
// match the behavior of `upb_strtable_iter`.
return false;
}
}
void upb_inttable_removeiter(upb_inttable* t, intptr_t* iter) {
intptr_t i = *iter;
if ((size_t)i < t->array_size) {
t->array_count--;
mutable_array(t)[i].val = -1;
} else {
upb_tabent* ent = &t->t.entries[i - t->array_size];
upb_tabent* prev = NULL;
// Linear search, not great.
upb_tabent* end = &t->t.entries[upb_table_size(&t->t)];
for (upb_tabent* e = t->t.entries; e != end; e++) {
if (e->next == ent) {
prev = e;
break;
}
}
if (prev) {
prev->next = ent->next;
}
t->t.count--;
ent->key = upb_key_empty();
ent->next = NULL;
}
}
void upb_inttable_setentryvalue(upb_inttable* t, intptr_t iter, upb_value v) {
if ((size_t)iter < t->array_size) {
mutable_array(t)[iter] = v;
} else {
upb_tabent* ent = &t->t.entries[iter - t->array_size];
ent->val = v;
}
}
bool upb_inttable_done(const upb_inttable* t, intptr_t iter) {
if ((uintptr_t)iter >= t->array_size + upb_table_size(&t->t)) {
return true;
} else if ((size_t)iter < t->array_size) {
return !upb_inttable_arrhas(t, iter);
} else {
return upb_tabent_isempty(&t->t.entries[iter - t->array_size]);
}
}
uintptr_t upb_inttable_iter_key(const upb_inttable* t, intptr_t iter) {
UPB_ASSERT(!upb_inttable_done(t, iter));
return (size_t)iter < t->array_size
? iter
: t->t.entries[iter - t->array_size].key.num;
}
upb_value upb_inttable_iter_value(const upb_inttable* t, intptr_t iter) {
UPB_ASSERT(!upb_inttable_done(t, iter));
if ((size_t)iter < t->array_size) {
return t->array[iter];
} else {
return t->t.entries[iter - t->array_size].val;
}
}
// Must be last.
const char* upb_BufToUint64(const char* ptr, const char* end, uint64_t* val) {
uint64_t u64 = 0;
while (ptr < end) {
unsigned ch = *ptr - '0';
if (ch >= 10) break;
if (u64 > UINT64_MAX / 10 || u64 * 10 > UINT64_MAX - ch) {
return NULL; // integer overflow
}
u64 *= 10;
u64 += ch;
ptr++;
}
*val = u64;
return ptr;
}
const char* upb_BufToInt64(const char* ptr, const char* end, int64_t* val,
bool* is_neg) {
bool neg = false;
uint64_t u64;
if (ptr != end && *ptr == '-') {
ptr++;
neg = true;
}
ptr = upb_BufToUint64(ptr, end, &u64);
if (!ptr || u64 > (uint64_t)INT64_MAX + neg) {
return NULL; // integer overflow
}
*val = neg ? -u64 : u64;
if (is_neg) *is_neg = neg;
return ptr;
}
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// Must be last.
/* Miscellaneous utilities ****************************************************/
static void upb_FixLocale(char* p) {
/* printf() is dependent on locales; sadly there is no easy and portable way
* to avoid this. This little post-processing step will translate 1,2 -> 1.2
* since JSON needs the latter. Arguably a hack, but it is simple and the
* alternatives are far more complicated, platform-dependent, and/or larger
* in code size. */
for (; *p; p++) {
if (*p == ',') *p = '.';
}
}
void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size) {
assert(size >= kUpb_RoundTripBufferSize);
if (isnan(val)) {
snprintf(buf, size, "%s", "nan");
return;
}
snprintf(buf, size, "%.*g", DBL_DIG, val);
if (strtod(buf, NULL) != val) {
snprintf(buf, size, "%.*g", DBL_DIG + 2, val);
assert(strtod(buf, NULL) == val);
}
upb_FixLocale(buf);
}
void _upb_EncodeRoundTripFloat(float val, char* buf, size_t size) {
assert(size >= kUpb_RoundTripBufferSize);
if (isnan(val)) {
snprintf(buf, size, "%s", "nan");
return;
}
snprintf(buf, size, "%.*g", FLT_DIG, val);
if (strtof(buf, NULL) != val) {
snprintf(buf, size, "%.*g", FLT_DIG + 3, val);
assert(strtof(buf, NULL) == val);
}
upb_FixLocale(buf);
}
#include <stdlib.h>
#include <string.h>
// Must be last.
// Determine the locale-specific radix character by calling sprintf() to print
// the number 1.5, then stripping off the digits. As far as I can tell, this
// is the only portable, thread-safe way to get the C library to divulge the
// locale's radix character. No, localeconv() is NOT thread-safe.
static int GetLocaleRadix(char *data, size_t capacity) {
char temp[16];
const int size = snprintf(temp, sizeof(temp), "%.1f", 1.5);
UPB_ASSERT(temp[0] == '1');
UPB_ASSERT(temp[size - 1] == '5');
UPB_ASSERT(size < capacity);
temp[size - 1] = '\0';
strcpy(data, temp + 1);
return size - 2;
}
// Populates a string identical to *input except that the character pointed to
// by pos (which should be '.') is replaced with the locale-specific radix.
static void LocalizeRadix(const char *input, const char *pos, char *output) {
const int len1 = pos - input;
char radix[8];
const int len2 = GetLocaleRadix(radix, sizeof(radix));
memcpy(output, input, len1);
memcpy(output + len1, radix, len2);
strcpy(output + len1 + len2, input + len1 + 1);
}
double _upb_NoLocaleStrtod(const char *str, char **endptr) {
// We cannot simply set the locale to "C" temporarily with setlocale()
// as this is not thread-safe. Instead, we try to parse in the current
// locale first. If parsing stops at a '.' character, then this is a
// pretty good hint that we're actually in some other locale in which
// '.' is not the radix character.
char *temp_endptr;
double result = strtod(str, &temp_endptr);
if (endptr != NULL) *endptr = temp_endptr;
if (*temp_endptr != '.') return result;
// Parsing halted on a '.'. Perhaps we're in a different locale? Let's
// try to replace the '.' with a locale-specific radix character and
// try again.
char localized[80];
LocalizeRadix(str, temp_endptr, localized);
char *localized_endptr;
result = strtod(localized, &localized_endptr);
if ((localized_endptr - &localized[0]) > (temp_endptr - str)) {
// This attempt got further, so replacing the decimal must have helped.
// Update endptr to point at the right location.
if (endptr != NULL) {
// size_diff is non-zero if the localized radix has multiple bytes.
int size_diff = strlen(localized) - strlen(str);
*endptr = (char *)str + (localized_endptr - &localized[0] - size_diff);
}
}
return result;
}
// Must be last.
int upb_Unicode_ToUTF8(uint32_t cp, char* out) {
if (cp <= 0x7f) {
out[0] = cp;
return 1;
}
if (cp <= 0x07ff) {
out[0] = (cp >> 6) | 0xc0;
out[1] = (cp & 0x3f) | 0x80;
return 2;
}
if (cp <= 0xffff) {
out[0] = (cp >> 12) | 0xe0;
out[1] = ((cp >> 6) & 0x3f) | 0x80;
out[2] = (cp & 0x3f) | 0x80;
return 3;
}
if (cp <= 0x10ffff) {
out[0] = (cp >> 18) | 0xf0;
out[1] = ((cp >> 12) & 0x3f) | 0x80;
out[2] = ((cp >> 6) & 0x3f) | 0x80;
out[3] = (cp & 0x3f) | 0x80;
return 4;
}
return 0;
}
#include <stdint.h>
#include <string.h>
// Must be last.
const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
const struct upb_Message* msg, const upb_MiniTableExtension* e) {
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) return NULL;
for (size_t i = 0; i < in->size; i++) {
upb_TaggedAuxPtr tagged_ptr = in->aux_data[i];
if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) {
const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr);
if (ext->ext == e) {
return ext;
}
}
}
return NULL;
}
upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
struct upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Extension* ext = (upb_Extension*)UPB_PRIVATE(_upb_Message_Getext)(msg, e);
if (ext) return ext;
if (!UPB_PRIVATE(_upb_Message_ReserveSlot)(msg, a)) return NULL;
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
ext = upb_Arena_Malloc(a, sizeof(upb_Extension));
if (!ext) return NULL;
memset(ext, 0, sizeof(upb_Extension));
ext->ext = e;
in->aux_data[in->size++] = upb_TaggedAuxPtr_MakeExtension(ext);
return ext;
}
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
// The latest win32 SDKs have an invalid definition of NAN.
// https://developercommunity.visualstudio.com/t/NAN-is-no-longer-compile-time-constant-i/10688907
//
// Unfortunately, the `0.0 / 0.0` workaround doesn't work in Clang under C23, so
// try __builtin_nan first, if that exists.
#ifdef _WIN32
#ifdef __has_builtin
#if __has_builtin(__builtin_nan)
#define UPB_NAN __builtin_nan("0")
#endif
#if __has_builtin(__builtin_inf)
#define UPB_INFINITY __builtin_inf()
#endif
#endif
#ifndef UPB_NAN
#define UPB_NAN 0.0 / 0.0
#endif
#ifndef UPB_INFINITY
#define UPB_INFINITY 1.0 / 0.0
#endif
#else
// For !_WIN32, assume math.h works.
#define UPB_NAN NAN
#define UPB_INFINITY INFINITY
#endif
const float kUpb_FltInfinity = UPB_INFINITY;
const double kUpb_Infinity = UPB_INFINITY;
const double kUpb_NaN = UPB_NAN;
static size_t _upb_Message_SizeOfInternal(uint32_t count) {
return UPB_SIZEOF_FLEX(upb_Message_Internal, aux_data, count);
}
bool UPB_PRIVATE(_upb_Message_ReserveSlot)(struct upb_Message* msg,
upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) {
// No internal data, allocate from scratch.
uint32_t capacity = 4;
in = upb_Arena_Malloc(a, _upb_Message_SizeOfInternal(capacity));
if (!in) return false;
in->size = 0;
in->capacity = capacity;
UPB_PRIVATE(_upb_Message_SetInternal)(msg, in);
} else if (in->capacity == in->size) {
// Internal data is too small, reallocate.
uint32_t new_capacity = upb_RoundUpToPowerOfTwo(in->size + 1);
in = upb_Arena_Realloc(a, in, _upb_Message_SizeOfInternal(in->capacity),
_upb_Message_SizeOfInternal(new_capacity));
if (!in) return false;
in->capacity = new_capacity;
UPB_PRIVATE(_upb_Message_SetInternal)(msg, in);
}
UPB_ASSERT(in->capacity - in->size >= 1);
return true;
}
#ifdef UPB_TRACING_ENABLED
static void (*_message_trace_handler)(const upb_MiniTable*, const upb_Arena*);
void upb_Message_LogNewMessage(const upb_MiniTable* m, const upb_Arena* arena) {
if (_message_trace_handler) {
_message_trace_handler(m, arena);
}
}
void upb_Message_SetNewMessageTraceHandler(void (*handler)(const upb_MiniTable*,
const upb_Arena*)) {
_message_trace_handler = handler;
}
#endif // UPB_TRACING_ENABLED
#include <stddef.h>
// Must be last.
bool UPB_PRIVATE(_upb_Message_NextBaseField)(const upb_Message* msg,
const upb_MiniTable* m,
const upb_MiniTableField** out_f,
upb_MessageValue* out_v,
size_t* iter) {
const size_t count = upb_MiniTable_FieldCount(m);
size_t i = *iter;
while (++i < count) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
const void* src = UPB_PRIVATE(_upb_Message_DataPtr)(msg, f);
upb_MessageValue val;
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, &val, src);
// Skip field if unset or empty.
if (upb_MiniTableField_HasPresence(f)) {
if (!upb_Message_HasBaseField(msg, f)) continue;
} else {
if (UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(f, src)) continue;
if (upb_MiniTableField_IsArray(f)) {
if (upb_Array_Size(val.array_val) == 0) continue;
} else if (upb_MiniTableField_IsMap(f)) {
if (upb_Map_Size(val.map_val) == 0) continue;
}
}
*out_f = f;
*out_v = val;
*iter = i;
return true;
}
return false;
}
const char _kUpb_ToBase92[] = {
' ', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=',
'>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', '[', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '{', '|', '}', '~',
};
const int8_t _kUpb_FromBase92[] = {
0, 1, -1, 2, 3, 4, 5, -1, 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, -1, 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,
};
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
// Must be last.
typedef struct {
uint64_t present_values_mask;
uint32_t last_written_value;
} upb_MtDataEncoderInternal_EnumState;
typedef struct {
uint64_t msg_modifiers;
uint32_t last_field_num;
enum {
kUpb_OneofState_NotStarted,
kUpb_OneofState_StartedOneof,
kUpb_OneofState_EmittedOneofField,
} oneof_state;
} upb_MtDataEncoderInternal_MsgState;
typedef struct {
char* buf_start; // Only for checking kUpb_MtDataEncoder_MinSize.
union {
upb_MtDataEncoderInternal_EnumState enum_state;
upb_MtDataEncoderInternal_MsgState msg_state;
} state;
} upb_MtDataEncoderInternal;
static upb_MtDataEncoderInternal* upb_MtDataEncoder_GetInternal(
upb_MtDataEncoder* e, char* buf_start) {
UPB_ASSERT(sizeof(upb_MtDataEncoderInternal) <= sizeof(e->internal));
upb_MtDataEncoderInternal* ret = (upb_MtDataEncoderInternal*)e->internal;
ret->buf_start = buf_start;
return ret;
}
static char* upb_MtDataEncoder_PutRaw(upb_MtDataEncoder* e, char* ptr,
char ch) {
upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal;
UPB_ASSERT(ptr - in->buf_start < kUpb_MtDataEncoder_MinSize);
if (ptr == e->end) return NULL;
*ptr++ = ch;
return ptr;
}
static char* upb_MtDataEncoder_Put(upb_MtDataEncoder* e, char* ptr, char ch) {
return upb_MtDataEncoder_PutRaw(e, ptr, _upb_ToBase92(ch));
}
static char* upb_MtDataEncoder_PutBase92Varint(upb_MtDataEncoder* e, char* ptr,
uint32_t val, int min, int max) {
int shift = upb_Log2Ceiling(_upb_FromBase92(max) - _upb_FromBase92(min) + 1);
UPB_ASSERT(shift <= 6);
uint32_t mask = (1 << shift) - 1;
do {
uint32_t bits = val & mask;
ptr = upb_MtDataEncoder_Put(e, ptr, bits + _upb_FromBase92(min));
if (!ptr) return NULL;
val >>= shift;
} while (val);
return ptr;
}
char* upb_MtDataEncoder_PutModifier(upb_MtDataEncoder* e, char* ptr,
uint64_t mod) {
if (mod) {
ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, mod,
kUpb_EncodedValue_MinModifier,
kUpb_EncodedValue_MaxModifier);
}
return ptr;
}
char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = 0;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_ExtensionV1);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutField(e, ptr, type, field_num, field_mod);
}
char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr,
upb_FieldType key_type,
upb_FieldType value_type, uint64_t key_mod,
uint64_t value_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = 0;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MapV1);
if (!ptr) return NULL;
ptr = upb_MtDataEncoder_PutField(e, ptr, key_type, 1, key_mod);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutField(e, ptr, value_type, 2, value_mod);
}
char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr) {
(void)upb_MtDataEncoder_GetInternal(e, ptr);
return upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MessageSetV1);
}
char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
uint64_t msg_mod) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.msg_state.msg_modifiers = msg_mod;
in->state.msg_state.last_field_num = 0;
in->state.msg_state.oneof_state = kUpb_OneofState_NotStarted;
ptr = upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_MessageV1);
if (!ptr) return NULL;
return upb_MtDataEncoder_PutModifier(e, ptr, msg_mod);
}
static char* _upb_MtDataEncoder_MaybePutFieldSkip(upb_MtDataEncoder* e,
char* ptr,
uint32_t field_num) {
upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal;
if (field_num <= in->state.msg_state.last_field_num) return NULL;
if (in->state.msg_state.last_field_num + 1 != field_num) {
// Put skip.
UPB_ASSERT(field_num > in->state.msg_state.last_field_num);
uint32_t skip = field_num - in->state.msg_state.last_field_num;
ptr = upb_MtDataEncoder_PutBase92Varint(
e, ptr, skip, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip);
if (!ptr) return NULL;
}
in->state.msg_state.last_field_num = field_num;
return ptr;
}
static char* _upb_MtDataEncoder_PutFieldType(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type,
uint64_t field_mod) {
static const char kUpb_TypeToEncoded[] = {
[kUpb_FieldType_Double] = kUpb_EncodedType_Double,
[kUpb_FieldType_Float] = kUpb_EncodedType_Float,
[kUpb_FieldType_Int64] = kUpb_EncodedType_Int64,
[kUpb_FieldType_UInt64] = kUpb_EncodedType_UInt64,
[kUpb_FieldType_Int32] = kUpb_EncodedType_Int32,
[kUpb_FieldType_Fixed64] = kUpb_EncodedType_Fixed64,
[kUpb_FieldType_Fixed32] = kUpb_EncodedType_Fixed32,
[kUpb_FieldType_Bool] = kUpb_EncodedType_Bool,
[kUpb_FieldType_String] = kUpb_EncodedType_String,
[kUpb_FieldType_Group] = kUpb_EncodedType_Group,
[kUpb_FieldType_Message] = kUpb_EncodedType_Message,
[kUpb_FieldType_Bytes] = kUpb_EncodedType_Bytes,
[kUpb_FieldType_UInt32] = kUpb_EncodedType_UInt32,
[kUpb_FieldType_Enum] = kUpb_EncodedType_OpenEnum,
[kUpb_FieldType_SFixed32] = kUpb_EncodedType_SFixed32,
[kUpb_FieldType_SFixed64] = kUpb_EncodedType_SFixed64,
[kUpb_FieldType_SInt32] = kUpb_EncodedType_SInt32,
[kUpb_FieldType_SInt64] = kUpb_EncodedType_SInt64,
};
int encoded_type = kUpb_TypeToEncoded[type];
if (field_mod & kUpb_FieldModifier_IsClosedEnum) {
UPB_ASSERT(type == kUpb_FieldType_Enum);
encoded_type = kUpb_EncodedType_ClosedEnum;
}
if (field_mod & kUpb_FieldModifier_IsRepeated) {
// Repeated fields shift the type number up (unlike other modifiers which
// are bit flags).
encoded_type += kUpb_EncodedType_RepeatedBase;
}
return upb_MtDataEncoder_Put(e, ptr, encoded_type);
}
static char* _upb_MtDataEncoder_MaybePutModifiers(upb_MtDataEncoder* e,
char* ptr, upb_FieldType type,
uint64_t field_mod) {
upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal;
uint32_t encoded_modifiers = 0;
if ((field_mod & kUpb_FieldModifier_IsRepeated) &&
upb_FieldType_IsPackable(type)) {
bool field_is_packed = field_mod & kUpb_FieldModifier_IsPacked;
bool default_is_packed = in->state.msg_state.msg_modifiers &
kUpb_MessageModifier_DefaultIsPacked;
if (field_is_packed != default_is_packed) {
encoded_modifiers |= kUpb_EncodedFieldModifier_FlipPacked;
}
}
if (type == kUpb_FieldType_String) {
bool field_validates_utf8 = field_mod & kUpb_FieldModifier_ValidateUtf8;
bool message_validates_utf8 =
in->state.msg_state.msg_modifiers & kUpb_MessageModifier_ValidateUtf8;
if (field_validates_utf8 != message_validates_utf8) {
// Old binaries do not recognize the field modifier. We need the failure
// mode to be too lax rather than too strict. Our caller should have
// handled this (see _upb_MessageDef_ValidateUtf8()).
assert(!message_validates_utf8);
encoded_modifiers |= kUpb_EncodedFieldModifier_FlipValidateUtf8;
}
}
if (field_mod & kUpb_FieldModifier_IsProto3Singular) {
encoded_modifiers |= kUpb_EncodedFieldModifier_IsProto3Singular;
}
if (field_mod & kUpb_FieldModifier_IsRequired) {
encoded_modifiers |= kUpb_EncodedFieldModifier_IsRequired;
}
return upb_MtDataEncoder_PutModifier(e, ptr, encoded_modifiers);
}
char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
upb_FieldType type, uint32_t field_num,
uint64_t field_mod) {
upb_MtDataEncoder_GetInternal(e, ptr);
ptr = _upb_MtDataEncoder_MaybePutFieldSkip(e, ptr, field_num);
if (!ptr) return NULL;
ptr = _upb_MtDataEncoder_PutFieldType(e, ptr, type, field_mod);
if (!ptr) return NULL;
return _upb_MtDataEncoder_MaybePutModifiers(e, ptr, type, field_mod);
}
char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (in->state.msg_state.oneof_state == kUpb_OneofState_NotStarted) {
ptr = upb_MtDataEncoder_Put(e, ptr, _upb_FromBase92(kUpb_EncodedValue_End));
} else {
ptr = upb_MtDataEncoder_Put(
e, ptr, _upb_FromBase92(kUpb_EncodedValue_OneofSeparator));
}
in->state.msg_state.oneof_state = kUpb_OneofState_StartedOneof;
return ptr;
}
char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
uint32_t field_num) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (in->state.msg_state.oneof_state == kUpb_OneofState_EmittedOneofField) {
ptr = upb_MtDataEncoder_Put(
e, ptr, _upb_FromBase92(kUpb_EncodedValue_FieldSeparator));
if (!ptr) return NULL;
}
ptr = upb_MtDataEncoder_PutBase92Varint(e, ptr, field_num, _upb_ToBase92(0),
_upb_ToBase92(63));
in->state.msg_state.oneof_state = kUpb_OneofState_EmittedOneofField;
return ptr;
}
char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
in->state.enum_state.present_values_mask = 0;
in->state.enum_state.last_written_value = 0;
return upb_MtDataEncoder_PutRaw(e, ptr, kUpb_EncodedVersion_EnumV1);
}
static char* upb_MtDataEncoder_FlushDenseEnumMask(upb_MtDataEncoder* e,
char* ptr) {
upb_MtDataEncoderInternal* in = (upb_MtDataEncoderInternal*)e->internal;
ptr = upb_MtDataEncoder_Put(e, ptr, in->state.enum_state.present_values_mask);
in->state.enum_state.present_values_mask = 0;
in->state.enum_state.last_written_value += 5;
return ptr;
}
char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
uint32_t val) {
// TODO: optimize this encoding.
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
UPB_ASSERT(val >= in->state.enum_state.last_written_value);
uint32_t delta = val - in->state.enum_state.last_written_value;
if (delta >= 5 && in->state.enum_state.present_values_mask) {
ptr = upb_MtDataEncoder_FlushDenseEnumMask(e, ptr);
if (!ptr) {
return NULL;
}
delta -= 5;
}
if (delta >= 5) {
ptr = upb_MtDataEncoder_PutBase92Varint(
e, ptr, delta, kUpb_EncodedValue_MinSkip, kUpb_EncodedValue_MaxSkip);
in->state.enum_state.last_written_value += delta;
delta = 0;
}
UPB_ASSERT((in->state.enum_state.present_values_mask >> delta) == 0);
in->state.enum_state.present_values_mask |= 1ULL << delta;
return ptr;
}
char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr) {
upb_MtDataEncoderInternal* in = upb_MtDataEncoder_GetInternal(e, ptr);
if (!in->state.enum_state.present_values_mask) return ptr;
return upb_MtDataEncoder_FlushDenseEnumMask(e, ptr);
}
#include <stddef.h>
// Must be last.
// A MiniTable for an empty message, used for unlinked sub-messages that are
// built via MiniDescriptors. Messages that use this MiniTable may possibly
// be linked later, in which case this MiniTable will be replaced with a real
// one. This pattern is known as "dynamic tree shaking", and it introduces
// complication because sub-messages may either be the "empty" type or the
// "real" type. A tagged bit indicates the difference.
const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_Empty) = {
.UPB_PRIVATE(subs) = NULL,
.UPB_PRIVATE(fields) = NULL,
.UPB_PRIVATE(size) = sizeof(struct upb_Message),
.UPB_PRIVATE(field_count) = 0,
.UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable,
.UPB_PRIVATE(dense_below) = 0,
.UPB_PRIVATE(table_mask) = -1,
.UPB_PRIVATE(required_count) = 0,
};
// A MiniTable for a statically tree shaken message. Messages that use this
// MiniTable are guaranteed to remain unlinked; unlike the empty message, this
// MiniTable is never replaced, which greatly simplifies everything, because the
// type of a sub-message is always known, without consulting a tagged bit.
const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_StaticallyTreeShaken) = {
.UPB_PRIVATE(subs) = NULL,
.UPB_PRIVATE(fields) = NULL,
.UPB_PRIVATE(size) = sizeof(struct upb_Message),
.UPB_PRIVATE(field_count) = 0,
.UPB_PRIVATE(ext) = kUpb_ExtMode_NonExtendable,
.UPB_PRIVATE(dense_below) = 0,
.UPB_PRIVATE(table_mask) = -1,
.UPB_PRIVATE(required_count) = 0,
};
// Must be last.
struct upb_DefPool {
upb_Arena* arena;
upb_strtable syms; // full_name -> packed def ptr
upb_strtable files; // file_name -> (upb_FileDef*)
upb_inttable exts; // (upb_MiniTableExtension*) -> (upb_FieldDef*)
upb_ExtensionRegistry* extreg;
const UPB_DESC(FeatureSetDefaults) * feature_set_defaults;
upb_MiniTablePlatform platform;
void* scratch_data;
size_t scratch_size;
size_t bytes_loaded;
};
void upb_DefPool_Free(upb_DefPool* s) {
upb_Arena_Free(s->arena);
upb_gfree(s->scratch_data);
upb_gfree(s);
}
static const char serialized_defaults[] = UPB_INTERNAL_UPB_EDITION_DEFAULTS;
upb_DefPool* upb_DefPool_New(void) {
upb_DefPool* s = upb_gmalloc(sizeof(*s));
if (!s) return NULL;
s->arena = upb_Arena_New();
s->bytes_loaded = 0;
s->scratch_size = 240;
s->scratch_data = upb_gmalloc(s->scratch_size);
if (!s->scratch_data) goto err;
if (!upb_strtable_init(&s->syms, 32, s->arena)) goto err;
if (!upb_strtable_init(&s->files, 4, s->arena)) goto err;
if (!upb_inttable_init(&s->exts, s->arena)) goto err;
s->extreg = upb_ExtensionRegistry_New(s->arena);
if (!s->extreg) goto err;
s->platform = kUpb_MiniTablePlatform_Native;
upb_Status status;
if (!upb_DefPool_SetFeatureSetDefaults(
s, serialized_defaults, sizeof(serialized_defaults) - 1, &status)) {
goto err;
}
if (!s->feature_set_defaults) goto err;
return s;
err:
upb_DefPool_Free(s);
return NULL;
}
const UPB_DESC(FeatureSetDefaults) *
upb_DefPool_FeatureSetDefaults(const upb_DefPool* s) {
return s->feature_set_defaults;
}
bool upb_DefPool_SetFeatureSetDefaults(upb_DefPool* s,
const char* serialized_defaults,
size_t serialized_len,
upb_Status* status) {
const UPB_DESC(FeatureSetDefaults)* defaults = UPB_DESC(
FeatureSetDefaults_parse)(serialized_defaults, serialized_len, s->arena);
if (!defaults) {
upb_Status_SetErrorFormat(status, "Failed to parse defaults");
return false;
}
if (upb_strtable_count(&s->files) > 0) {
upb_Status_SetErrorFormat(status,
"Feature set defaults can't be changed once the "
"pool has started building");
return false;
}
int min_edition = UPB_DESC(FeatureSetDefaults_minimum_edition(defaults));
int max_edition = UPB_DESC(FeatureSetDefaults_maximum_edition(defaults));
if (min_edition > max_edition) {
upb_Status_SetErrorFormat(status, "Invalid edition range %s to %s",
upb_FileDef_EditionName(min_edition),
upb_FileDef_EditionName(max_edition));
return false;
}
size_t size;
const UPB_DESC(
FeatureSetDefaults_FeatureSetEditionDefault)* const* default_list =
UPB_DESC(FeatureSetDefaults_defaults(defaults, &size));
int prev_edition = UPB_DESC(EDITION_UNKNOWN);
for (size_t i = 0; i < size; ++i) {
int edition = UPB_DESC(
FeatureSetDefaults_FeatureSetEditionDefault_edition(default_list[i]));
if (edition == UPB_DESC(EDITION_UNKNOWN)) {
upb_Status_SetErrorFormat(status, "Invalid edition UNKNOWN specified");
return false;
}
if (edition <= prev_edition) {
upb_Status_SetErrorFormat(status,
"Feature set defaults are not strictly "
"increasing, %s is greater than or equal to %s",
upb_FileDef_EditionName(prev_edition),
upb_FileDef_EditionName(edition));
return false;
}
prev_edition = edition;
}
// Copy the defaults into the pool.
s->feature_set_defaults = defaults;
return true;
}
bool _upb_DefPool_InsertExt(upb_DefPool* s, const upb_MiniTableExtension* ext,
const upb_FieldDef* f) {
return upb_inttable_insert(&s->exts, (uintptr_t)ext, upb_value_constptr(f),
s->arena);
}
bool _upb_DefPool_InsertSym(upb_DefPool* s, upb_StringView sym, upb_value v,
upb_Status* status) {
// TODO: table should support an operation "tryinsert" to avoid the double
// lookup.
if (upb_strtable_lookup2(&s->syms, sym.data, sym.size, NULL)) {
upb_Status_SetErrorFormat(status, "duplicate symbol '%s'", sym.data);
return false;
}
if (!upb_strtable_insert(&s->syms, sym.data, sym.size, v, s->arena)) {
upb_Status_SetErrorMessage(status, "out of memory");
return false;
}
return true;
}
static const void* _upb_DefPool_Unpack(const upb_DefPool* s, const char* sym,
size_t size, upb_deftype_t type) {
upb_value v;
return upb_strtable_lookup2(&s->syms, sym, size, &v)
? _upb_DefType_Unpack(v, type)
: NULL;
}
bool _upb_DefPool_LookupSym(const upb_DefPool* s, const char* sym, size_t size,
upb_value* v) {
return upb_strtable_lookup2(&s->syms, sym, size, v);
}
upb_ExtensionRegistry* _upb_DefPool_ExtReg(const upb_DefPool* s) {
return s->extreg;
}
void** _upb_DefPool_ScratchData(const upb_DefPool* s) {
return (void**)&s->scratch_data;
}
size_t* _upb_DefPool_ScratchSize(const upb_DefPool* s) {
return (size_t*)&s->scratch_size;
}
void _upb_DefPool_SetPlatform(upb_DefPool* s, upb_MiniTablePlatform platform) {
assert(upb_strtable_count(&s->files) == 0);
s->platform = platform;
}
const upb_MessageDef* upb_DefPool_FindMessageByName(const upb_DefPool* s,
const char* sym) {
return _upb_DefPool_Unpack(s, sym, strlen(sym), UPB_DEFTYPE_MSG);
}
const upb_MessageDef* upb_DefPool_FindMessageByNameWithSize(
const upb_DefPool* s, const char* sym, size_t len) {
return _upb_DefPool_Unpack(s, sym, len, UPB_DEFTYPE_MSG);
}
const upb_EnumDef* upb_DefPool_FindEnumByName(const upb_DefPool* s,
const char* sym) {
return _upb_DefPool_Unpack(s, sym, strlen(sym), UPB_DEFTYPE_ENUM);
}
const upb_EnumValueDef* upb_DefPool_FindEnumByNameval(const upb_DefPool* s,
const char* sym) {
return _upb_DefPool_Unpack(s, sym, strlen(sym), UPB_DEFTYPE_ENUMVAL);
}
const upb_FileDef* upb_DefPool_FindFileByName(const upb_DefPool* s,
const char* name) {
upb_value v;
return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v)
: NULL;
}
const upb_FileDef* upb_DefPool_FindFileByNameWithSize(const upb_DefPool* s,
const char* name,
size_t len) {
upb_value v;
return upb_strtable_lookup2(&s->files, name, len, &v)
? upb_value_getconstptr(v)
: NULL;
}
const upb_FieldDef* upb_DefPool_FindExtensionByNameWithSize(
const upb_DefPool* s, const char* name, size_t size) {
upb_value v;
if (!upb_strtable_lookup2(&s->syms, name, size, &v)) return NULL;
switch (_upb_DefType_Type(v)) {
case UPB_DEFTYPE_FIELD:
return _upb_DefType_Unpack(v, UPB_DEFTYPE_FIELD);
case UPB_DEFTYPE_MSG: {
const upb_MessageDef* m = _upb_DefType_Unpack(v, UPB_DEFTYPE_MSG);
if (_upb_MessageDef_InMessageSet(m)) {
for (int i = 0; i < upb_MessageDef_NestedExtensionCount(m); i++) {
const upb_FieldDef* ext = upb_MessageDef_NestedExtension(m, i);
if (upb_FieldDef_MessageSubDef(ext) == m) {
return ext;
}
}
}
return NULL;
}
default:
break;
}
return NULL;
}
const upb_FieldDef* upb_DefPool_FindExtensionByName(const upb_DefPool* s,
const char* sym) {
return upb_DefPool_FindExtensionByNameWithSize(s, sym, strlen(sym));
}
const upb_ServiceDef* upb_DefPool_FindServiceByName(const upb_DefPool* s,
const char* name) {
return _upb_DefPool_Unpack(s, name, strlen(name), UPB_DEFTYPE_SERVICE);
}
const upb_ServiceDef* upb_DefPool_FindServiceByNameWithSize(
const upb_DefPool* s, const char* name, size_t size) {
return _upb_DefPool_Unpack(s, name, size, UPB_DEFTYPE_SERVICE);
}
const upb_FileDef* upb_DefPool_FindFileContainingSymbol(const upb_DefPool* s,
const char* name) {
upb_value v;
// TODO: non-extension fields and oneofs.
if (upb_strtable_lookup(&s->syms, name, &v)) {
switch (_upb_DefType_Type(v)) {
case UPB_DEFTYPE_EXT: {
const upb_FieldDef* f = _upb_DefType_Unpack(v, UPB_DEFTYPE_EXT);
return upb_FieldDef_File(f);
}
case UPB_DEFTYPE_MSG: {
const upb_MessageDef* m = _upb_DefType_Unpack(v, UPB_DEFTYPE_MSG);
return upb_MessageDef_File(m);
}
case UPB_DEFTYPE_ENUM: {
const upb_EnumDef* e = _upb_DefType_Unpack(v, UPB_DEFTYPE_ENUM);
return upb_EnumDef_File(e);
}
case UPB_DEFTYPE_ENUMVAL: {
const upb_EnumValueDef* ev =
_upb_DefType_Unpack(v, UPB_DEFTYPE_ENUMVAL);
return upb_EnumDef_File(upb_EnumValueDef_Enum(ev));
}
case UPB_DEFTYPE_SERVICE: {
const upb_ServiceDef* service =
_upb_DefType_Unpack(v, UPB_DEFTYPE_SERVICE);
return upb_ServiceDef_File(service);
}
default:
UPB_UNREACHABLE();
}
}
const char* last_dot = strrchr(name, '.');
if (last_dot) {
const upb_MessageDef* parent =
upb_DefPool_FindMessageByNameWithSize(s, name, last_dot - name);
if (parent) {
const char* shortname = last_dot + 1;
if (upb_MessageDef_FindByNameWithSize(parent, shortname,
strlen(shortname), NULL, NULL)) {
return upb_MessageDef_File(parent);
}
}
}
return NULL;
}
static void remove_filedef(upb_DefPool* s, upb_FileDef* file) {
intptr_t iter = UPB_INTTABLE_BEGIN;
upb_StringView key;
upb_value val;
while (upb_strtable_next2(&s->syms, &key, &val, &iter)) {
const upb_FileDef* f;
switch (_upb_DefType_Type(val)) {
case UPB_DEFTYPE_EXT:
f = upb_FieldDef_File(_upb_DefType_Unpack(val, UPB_DEFTYPE_EXT));
break;
case UPB_DEFTYPE_MSG:
f = upb_MessageDef_File(_upb_DefType_Unpack(val, UPB_DEFTYPE_MSG));
break;
case UPB_DEFTYPE_ENUM:
f = upb_EnumDef_File(_upb_DefType_Unpack(val, UPB_DEFTYPE_ENUM));
break;
case UPB_DEFTYPE_ENUMVAL:
f = upb_EnumDef_File(upb_EnumValueDef_Enum(
_upb_DefType_Unpack(val, UPB_DEFTYPE_ENUMVAL)));
break;
case UPB_DEFTYPE_SERVICE:
f = upb_ServiceDef_File(_upb_DefType_Unpack(val, UPB_DEFTYPE_SERVICE));
break;
default:
UPB_UNREACHABLE();
}
if (f == file) upb_strtable_removeiter(&s->syms, &iter);
}
}
static const upb_FileDef* upb_DefBuilder_AddFileToPool(
upb_DefBuilder* const builder, upb_DefPool* const s,
const UPB_DESC(FileDescriptorProto) * const file_proto,
const upb_StringView name, upb_Status* const status) {
if (UPB_SETJMP(builder->err) != 0) {
UPB_ASSERT(!upb_Status_IsOk(status));
if (builder->file) {
remove_filedef(s, builder->file);
builder->file = NULL;
}
} else if (!builder->arena || !builder->tmp_arena ||
!upb_strtable_init(&builder->feature_cache, 16,
builder->tmp_arena) ||
!(builder->legacy_features =
UPB_DESC(FeatureSet_new)(builder->tmp_arena))) {
_upb_DefBuilder_OomErr(builder);
} else {
_upb_FileDef_Create(builder, file_proto);
upb_strtable_insert(&s->files, name.data, name.size,
upb_value_constptr(builder->file), builder->arena);
UPB_ASSERT(upb_Status_IsOk(status));
upb_Arena_Fuse(s->arena, builder->arena);
}
if (builder->arena) upb_Arena_Free(builder->arena);
if (builder->tmp_arena) upb_Arena_Free(builder->tmp_arena);
return builder->file;
}
static const upb_FileDef* _upb_DefPool_AddFile(
upb_DefPool* s, const UPB_DESC(FileDescriptorProto) * file_proto,
const upb_MiniTableFile* layout, upb_Status* status) {
const upb_StringView name = UPB_DESC(FileDescriptorProto_name)(file_proto);
// Determine whether we already know about this file.
{
upb_value v;
if (upb_strtable_lookup2(&s->files, name.data, name.size, &v)) {
upb_Status_SetErrorFormat(status,
"duplicate file name " UPB_STRINGVIEW_FORMAT,
UPB_STRINGVIEW_ARGS(name));
return NULL;
}
}
upb_DefBuilder ctx = {
.symtab = s,
.tmp_buf = NULL,
.tmp_buf_size = 0,
.layout = layout,
.platform = s->platform,
.msg_count = 0,
.enum_count = 0,
.ext_count = 0,
.status = status,
.file = NULL,
.arena = upb_Arena_New(),
.tmp_arena = upb_Arena_New(),
};
return upb_DefBuilder_AddFileToPool(&ctx, s, file_proto, name, status);
}
const upb_FileDef* upb_DefPool_AddFile(upb_DefPool* s,
const UPB_DESC(FileDescriptorProto) *
file_proto,
upb_Status* status) {
return _upb_DefPool_AddFile(s, file_proto, NULL, status);
}
bool _upb_DefPool_LoadDefInitEx(upb_DefPool* s, const _upb_DefPool_Init* init,
bool rebuild_minitable) {
/* Since this function should never fail (it would indicate a bug in upb) we
* print errors to stderr instead of returning error status to the user. */
_upb_DefPool_Init** deps = init->deps;
UPB_DESC(FileDescriptorProto) * file;
upb_Arena* arena;
upb_Status status;
upb_Status_Clear(&status);
if (upb_DefPool_FindFileByName(s, init->filename)) {
return true;
}
arena = upb_Arena_New();
for (; *deps; deps++) {
if (!_upb_DefPool_LoadDefInitEx(s, *deps, rebuild_minitable)) goto err;
}
file = UPB_DESC(FileDescriptorProto_parse_ex)(
init->descriptor.data, init->descriptor.size, NULL,
kUpb_DecodeOption_AliasString, arena);
s->bytes_loaded += init->descriptor.size;
if (!file) {
upb_Status_SetErrorFormat(
&status,
"Failed to parse compiled-in descriptor for file '%s'. This should "
"never happen.",
init->filename);
goto err;
}
const upb_MiniTableFile* mt = rebuild_minitable ? NULL : init->layout;
if (!_upb_DefPool_AddFile(s, file, mt, &status)) {
goto err;
}
upb_Arena_Free(arena);
return true;
err:
fprintf(stderr,
"Error loading compiled-in descriptor for file '%s' (this should "
"never happen): %s\n",
init->filename, upb_Status_ErrorMessage(&status));
upb_Arena_Free(arena);
return false;
}
size_t _upb_DefPool_BytesLoaded(const upb_DefPool* s) {
return s->bytes_loaded;
}
upb_Arena* _upb_DefPool_Arena(const upb_DefPool* s) { return s->arena; }
const upb_FieldDef* upb_DefPool_FindExtensionByMiniTable(
const upb_DefPool* s, const upb_MiniTableExtension* ext) {
upb_value v;
bool ok = upb_inttable_lookup(&s->exts, (uintptr_t)ext, &v);
UPB_ASSERT(ok);
return upb_value_getconstptr(v);
}
const upb_FieldDef* upb_DefPool_FindExtensionByNumber(const upb_DefPool* s,
const upb_MessageDef* m,
int32_t fieldnum) {
const upb_MiniTable* t = upb_MessageDef_MiniTable(m);
const upb_MiniTableExtension* ext =
upb_ExtensionRegistry_Lookup(s->extreg, t, fieldnum);
return ext ? upb_DefPool_FindExtensionByMiniTable(s, ext) : NULL;
}
const upb_ExtensionRegistry* upb_DefPool_ExtensionRegistry(
const upb_DefPool* s) {
return s->extreg;
}
const upb_FieldDef** upb_DefPool_GetAllExtensions(const upb_DefPool* s,
const upb_MessageDef* m,
size_t* count) {
size_t n = 0;
intptr_t iter = UPB_INTTABLE_BEGIN;
uintptr_t key;
upb_value val;
// This is O(all exts) instead of O(exts for m). If we need this to be
// efficient we may need to make extreg into a two-level table, or have a
// second per-message index.
while (upb_inttable_next(&s->exts, &key, &val, &iter)) {
const upb_FieldDef* f = upb_value_getconstptr(val);
if (upb_FieldDef_ContainingType(f) == m) n++;
}
const upb_FieldDef** exts = upb_gmalloc(n * sizeof(*exts));
iter = UPB_INTTABLE_BEGIN;
size_t i = 0;
while (upb_inttable_next(&s->exts, &key, &val, &iter)) {
const upb_FieldDef* f = upb_value_getconstptr(val);
if (upb_FieldDef_ContainingType(f) == m) exts[i++] = f;
}
*count = n;
return exts;
}
bool _upb_DefPool_LoadDefInit(upb_DefPool* s, const _upb_DefPool_Init* init) {
return _upb_DefPool_LoadDefInitEx(s, init, false);
}
// Must be last.
upb_deftype_t _upb_DefType_Type(upb_value v) {
const uintptr_t num = (uintptr_t)upb_value_getconstptr(v);
return num & UPB_DEFTYPE_MASK;
}
upb_value _upb_DefType_Pack(const void* ptr, upb_deftype_t type) {
uintptr_t num = (uintptr_t)ptr;
UPB_ASSERT((num & UPB_DEFTYPE_MASK) == 0);
num |= type;
return upb_value_constptr((const void*)num);
}
const void* _upb_DefType_Unpack(upb_value v, upb_deftype_t type) {
uintptr_t num = (uintptr_t)upb_value_getconstptr(v);
return (num & UPB_DEFTYPE_MASK) == type
? (const void*)(num & ~UPB_DEFTYPE_MASK)
: NULL;
}
// Must be last.
bool _upb_DescState_Grow(upb_DescState* d, upb_Arena* a) {
const size_t oldbufsize = d->bufsize;
const int used = d->ptr - d->buf;
if (!d->buf) {
d->buf = upb_Arena_Malloc(a, d->bufsize);
if (!d->buf) return false;
d->ptr = d->buf;
d->e.end = d->buf + d->bufsize;
}
if (oldbufsize - used < kUpb_MtDataEncoder_MinSize) {
d->bufsize *= 2;
d->buf = upb_Arena_Realloc(a, d->buf, oldbufsize, d->bufsize);
if (!d->buf) return false;
d->ptr = d->buf + used;
d->e.end = d->buf + d->bufsize;
}
return true;
}
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
struct upb_EnumDef {
UPB_ALIGN_AS(8) const UPB_DESC(EnumOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_MiniTableEnum* layout; // Only for proto2.
const upb_FileDef* file;
const upb_MessageDef* containing_type; // Could be merged with "file".
const char* full_name;
upb_strtable ntoi;
upb_inttable iton;
const upb_EnumValueDef* values;
const upb_EnumReservedRange* res_ranges;
const upb_StringView* res_names;
int value_count;
int res_range_count;
int res_name_count;
int32_t defaultval;
bool is_sorted; // Whether all of the values are defined in ascending order.
#if UINTPTR_MAX == 0xffffffff
uint32_t padding; // Increase size to a multiple of 8.
#endif
};
upb_EnumDef* _upb_EnumDef_At(const upb_EnumDef* e, int i) {
return (upb_EnumDef*)&e[i];
}
const upb_MiniTableEnum* _upb_EnumDef_MiniTable(const upb_EnumDef* e) {
return e->layout;
}
bool _upb_EnumDef_Insert(upb_EnumDef* e, upb_EnumValueDef* v, upb_Arena* a) {
const char* name = upb_EnumValueDef_Name(v);
const upb_value val = upb_value_constptr(v);
bool ok = upb_strtable_insert(&e->ntoi, name, strlen(name), val, a);
if (!ok) return false;
// Multiple enumerators can have the same number, first one wins.
const int number = upb_EnumValueDef_Number(v);
if (!upb_inttable_lookup(&e->iton, number, NULL)) {
return upb_inttable_insert(&e->iton, number, val, a);
}
return true;
}
const UPB_DESC(EnumOptions) * upb_EnumDef_Options(const upb_EnumDef* e) {
return e->opts;
}
bool upb_EnumDef_HasOptions(const upb_EnumDef* e) {
return e->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_EnumDef_ResolvedFeatures(const upb_EnumDef* e) {
return e->resolved_features;
}
const char* upb_EnumDef_FullName(const upb_EnumDef* e) { return e->full_name; }
const char* upb_EnumDef_Name(const upb_EnumDef* e) {
return _upb_DefBuilder_FullToShort(e->full_name);
}
const upb_FileDef* upb_EnumDef_File(const upb_EnumDef* e) { return e->file; }
const upb_MessageDef* upb_EnumDef_ContainingType(const upb_EnumDef* e) {
return e->containing_type;
}
int32_t upb_EnumDef_Default(const upb_EnumDef* e) {
UPB_ASSERT(upb_EnumDef_FindValueByNumber(e, e->defaultval));
return e->defaultval;
}
int upb_EnumDef_ReservedRangeCount(const upb_EnumDef* e) {
return e->res_range_count;
}
const upb_EnumReservedRange* upb_EnumDef_ReservedRange(const upb_EnumDef* e,
int i) {
UPB_ASSERT(0 <= i && i < e->res_range_count);
return _upb_EnumReservedRange_At(e->res_ranges, i);
}
int upb_EnumDef_ReservedNameCount(const upb_EnumDef* e) {
return e->res_name_count;
}
upb_StringView upb_EnumDef_ReservedName(const upb_EnumDef* e, int i) {
UPB_ASSERT(0 <= i && i < e->res_name_count);
return e->res_names[i];
}
int upb_EnumDef_ValueCount(const upb_EnumDef* e) { return e->value_count; }
const upb_EnumValueDef* upb_EnumDef_FindValueByName(const upb_EnumDef* e,
const char* name) {
return upb_EnumDef_FindValueByNameWithSize(e, name, strlen(name));
}
const upb_EnumValueDef* upb_EnumDef_FindValueByNameWithSize(
const upb_EnumDef* e, const char* name, size_t size) {
upb_value v;
return upb_strtable_lookup2(&e->ntoi, name, size, &v)
? upb_value_getconstptr(v)
: NULL;
}
const upb_EnumValueDef* upb_EnumDef_FindValueByNumber(const upb_EnumDef* e,
int32_t num) {
upb_value v;
return upb_inttable_lookup(&e->iton, num, &v) ? upb_value_getconstptr(v)
: NULL;
}
bool upb_EnumDef_CheckNumber(const upb_EnumDef* e, int32_t num) {
// We could use upb_EnumDef_FindValueByNumber(e, num) != NULL, but we expect
// this to be faster (especially for small numbers).
return upb_MiniTableEnum_CheckValue(e->layout, num);
}
const upb_EnumValueDef* upb_EnumDef_Value(const upb_EnumDef* e, int i) {
UPB_ASSERT(0 <= i && i < e->value_count);
return _upb_EnumValueDef_At(e->values, i);
}
bool upb_EnumDef_IsClosed(const upb_EnumDef* e) {
if (UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN) return false;
return upb_EnumDef_IsSpecifiedAsClosed(e);
}
bool upb_EnumDef_IsSpecifiedAsClosed(const upb_EnumDef* e) {
return UPB_DESC(FeatureSet_enum_type)(e->resolved_features) ==
UPB_DESC(FeatureSet_CLOSED);
}
bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
upb_StringView* out) {
upb_DescState s;
_upb_DescState_Init(&s);
const upb_EnumValueDef** sorted = NULL;
if (!e->is_sorted) {
sorted = _upb_EnumValueDefs_Sorted(e->values, e->value_count, a);
if (!sorted) return false;
}
if (!_upb_DescState_Grow(&s, a)) return false;
s.ptr = upb_MtDataEncoder_StartEnum(&s.e, s.ptr);
// Duplicate values are allowed but we only encode each value once.
uint32_t previous = 0;
for (int i = 0; i < e->value_count; i++) {
const uint32_t current =
upb_EnumValueDef_Number(sorted ? sorted[i] : upb_EnumDef_Value(e, i));
if (i != 0 && previous == current) continue;
if (!_upb_DescState_Grow(&s, a)) return false;
s.ptr = upb_MtDataEncoder_PutEnumValue(&s.e, s.ptr, current);
previous = current;
}
if (!_upb_DescState_Grow(&s, a)) return false;
s.ptr = upb_MtDataEncoder_EndEnum(&s.e, s.ptr);
// There will always be room for this '\0' in the encoder buffer because
// kUpb_MtDataEncoder_MinSize is overkill for upb_MtDataEncoder_EndEnum().
UPB_ASSERT(s.ptr < s.buf + s.bufsize);
*s.ptr = '\0';
out->data = s.buf;
out->size = s.ptr - s.buf;
return true;
}
static upb_MiniTableEnum* create_enumlayout(upb_DefBuilder* ctx,
const upb_EnumDef* e) {
upb_StringView sv;
bool ok = upb_EnumDef_MiniDescriptorEncode(e, ctx->tmp_arena, &sv);
if (!ok) _upb_DefBuilder_Errf(ctx, "OOM while building enum MiniDescriptor");
upb_Status status;
upb_MiniTableEnum* layout =
upb_MiniTableEnum_Build(sv.data, sv.size, ctx->arena, &status);
if (!layout)
_upb_DefBuilder_Errf(ctx, "Error building enum MiniTable: %s", status.msg);
return layout;
}
static upb_StringView* _upb_EnumReservedNames_New(
upb_DefBuilder* ctx, int n, const upb_StringView* protos) {
upb_StringView* sv = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_StringView, n);
for (int i = 0; i < n; i++) {
sv[i].data =
upb_strdup2(protos[i].data, protos[i].size, _upb_DefBuilder_Arena(ctx));
sv[i].size = protos[i].size;
}
return sv;
}
static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(EnumDescriptorProto) * enum_proto,
const UPB_DESC(FeatureSet*) parent_features,
upb_EnumDef* e) {
const UPB_DESC(EnumValueDescriptorProto)* const* values;
const UPB_DESC(EnumDescriptorProto_EnumReservedRange)* const* res_ranges;
const upb_StringView* res_names;
upb_StringView name;
size_t n_value, n_res_range, n_res_name;
UPB_DEF_SET_OPTIONS(e->opts, EnumDescriptorProto, EnumOptions, enum_proto);
e->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(EnumOptions_features)(e->opts));
// Must happen before _upb_DefBuilder_Add()
e->file = _upb_DefBuilder_File(ctx);
name = UPB_DESC(EnumDescriptorProto_name)(enum_proto);
e->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
_upb_DefBuilder_Add(ctx, e->full_name,
_upb_DefType_Pack(e, UPB_DEFTYPE_ENUM));
values = UPB_DESC(EnumDescriptorProto_value)(enum_proto, &n_value);
bool ok = upb_strtable_init(&e->ntoi, n_value, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
ok = upb_inttable_init(&e->iton, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
e->defaultval = 0;
e->value_count = n_value;
e->values = _upb_EnumValueDefs_New(ctx, prefix, n_value, values,
e->resolved_features, e, &e->is_sorted);
if (n_value == 0) {
_upb_DefBuilder_Errf(ctx, "enums must contain at least one value (%s)",
e->full_name);
}
res_ranges =
UPB_DESC(EnumDescriptorProto_reserved_range)(enum_proto, &n_res_range);
e->res_range_count = n_res_range;
e->res_ranges = _upb_EnumReservedRanges_New(ctx, n_res_range, res_ranges, e);
res_names =
UPB_DESC(EnumDescriptorProto_reserved_name)(enum_proto, &n_res_name);
e->res_name_count = n_res_name;
e->res_names = _upb_EnumReservedNames_New(ctx, n_res_name, res_names);
if (!upb_inttable_compact(&e->iton, ctx->arena)) _upb_DefBuilder_OomErr(ctx);
if (upb_EnumDef_IsClosed(e)) {
if (ctx->layout) {
e->layout = upb_MiniTableFile_Enum(ctx->layout, ctx->enum_count++);
} else {
e->layout = create_enumlayout(ctx, e);
}
} else {
e->layout = NULL;
}
}
upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const upb_MessageDef* containing_type) {
_upb_DefType_CheckPadding(sizeof(upb_EnumDef));
// If a containing type is defined then get the full name from that.
// Otherwise use the package name from the file def.
const char* name = containing_type ? upb_MessageDef_FullName(containing_type)
: _upb_FileDef_RawPackage(ctx->file);
upb_EnumDef* e = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_EnumDef, n);
for (int i = 0; i < n; i++) {
create_enumdef(ctx, name, protos[i], parent_features, &e[i]);
e[i].containing_type = containing_type;
}
return e;
}
// Must be last.
struct upb_EnumReservedRange {
int32_t start;
int32_t end;
};
upb_EnumReservedRange* _upb_EnumReservedRange_At(const upb_EnumReservedRange* r,
int i) {
return (upb_EnumReservedRange*)&r[i];
}
int32_t upb_EnumReservedRange_Start(const upb_EnumReservedRange* r) {
return r->start;
}
int32_t upb_EnumReservedRange_End(const upb_EnumReservedRange* r) {
return r->end;
}
upb_EnumReservedRange* _upb_EnumReservedRanges_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto_EnumReservedRange) * const* protos,
const upb_EnumDef* e) {
upb_EnumReservedRange* r =
UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_EnumReservedRange, n);
for (int i = 0; i < n; i++) {
const int32_t start =
UPB_DESC(EnumDescriptorProto_EnumReservedRange_start)(protos[i]);
const int32_t end =
UPB_DESC(EnumDescriptorProto_EnumReservedRange_end)(protos[i]);
// A full validation would also check that each range is disjoint, and that
// none of the fields overlap with the extension ranges, but we are just
// sanity checking here.
// Note: Not a typo! Unlike extension ranges and message reserved ranges,
// the end value of an enum reserved range is *inclusive*!
if (end < start) {
_upb_DefBuilder_Errf(ctx, "Reserved range (%d, %d) is invalid, enum=%s\n",
(int)start, (int)end, upb_EnumDef_FullName(e));
}
r[i].start = start;
r[i].end = end;
}
return r;
}
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
// Must be last.
struct upb_EnumValueDef {
UPB_ALIGN_AS(8) const UPB_DESC(EnumValueOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_EnumDef* parent;
const char* full_name;
int32_t number;
};
upb_EnumValueDef* _upb_EnumValueDef_At(const upb_EnumValueDef* v, int i) {
return (upb_EnumValueDef*)&v[i];
}
static int _upb_EnumValueDef_Compare(const void* p1, const void* p2) {
const uint32_t v1 = (*(const upb_EnumValueDef**)p1)->number;
const uint32_t v2 = (*(const upb_EnumValueDef**)p2)->number;
return (v1 < v2) ? -1 : (v1 > v2);
}
const upb_EnumValueDef** _upb_EnumValueDefs_Sorted(const upb_EnumValueDef* v,
size_t n, upb_Arena* a) {
if (SIZE_MAX / sizeof(void*) < n) return NULL;
// TODO: Try to replace this arena alloc with a persistent scratch buffer.
upb_EnumValueDef** out =
(upb_EnumValueDef**)upb_Arena_Malloc(a, n * sizeof(void*));
if (!out) return NULL;
for (int i = 0; i < n; i++) {
out[i] = (upb_EnumValueDef*)&v[i];
}
qsort(out, n, sizeof(void*), _upb_EnumValueDef_Compare);
return (const upb_EnumValueDef**)out;
}
const UPB_DESC(EnumValueOptions) *
upb_EnumValueDef_Options(const upb_EnumValueDef* v) {
return v->opts;
}
bool upb_EnumValueDef_HasOptions(const upb_EnumValueDef* v) {
return v->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_EnumValueDef_ResolvedFeatures(const upb_EnumValueDef* e) {
return e->resolved_features;
}
const upb_EnumDef* upb_EnumValueDef_Enum(const upb_EnumValueDef* v) {
return v->parent;
}
const char* upb_EnumValueDef_FullName(const upb_EnumValueDef* v) {
return v->full_name;
}
const char* upb_EnumValueDef_Name(const upb_EnumValueDef* v) {
return _upb_DefBuilder_FullToShort(v->full_name);
}
int32_t upb_EnumValueDef_Number(const upb_EnumValueDef* v) { return v->number; }
uint32_t upb_EnumValueDef_Index(const upb_EnumValueDef* v) {
// Compute index in our parent's array.
return v - upb_EnumDef_Value(v->parent, 0);
}
static void create_enumvaldef(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(EnumValueDescriptorProto*)
val_proto,
const UPB_DESC(FeatureSet*) parent_features,
upb_EnumDef* e, upb_EnumValueDef* v) {
UPB_DEF_SET_OPTIONS(v->opts, EnumValueDescriptorProto, EnumValueOptions,
val_proto);
v->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(EnumValueOptions_features)(v->opts));
upb_StringView name = UPB_DESC(EnumValueDescriptorProto_name)(val_proto);
v->parent = e; // Must happen prior to _upb_DefBuilder_Add()
v->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
v->number = UPB_DESC(EnumValueDescriptorProto_number)(val_proto);
_upb_DefBuilder_Add(ctx, v->full_name,
_upb_DefType_Pack(v, UPB_DEFTYPE_ENUMVAL));
bool ok = _upb_EnumDef_Insert(e, v, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
}
static void _upb_EnumValueDef_CheckZeroValue(upb_DefBuilder* ctx,
const upb_EnumDef* e,
const upb_EnumValueDef* v, int n) {
// When the special UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN is enabled, we have to
// exempt closed enums from this check, even when we are treating them as
// open.
if (upb_EnumDef_IsSpecifiedAsClosed(e) || n == 0 || v[0].number == 0) return;
_upb_DefBuilder_Errf(ctx, "for open enums, the first value must be zero (%s)",
upb_EnumDef_FullName(e));
}
// Allocate and initialize an array of |n| enum value defs owned by |e|.
upb_EnumValueDef* _upb_EnumValueDefs_New(
upb_DefBuilder* ctx, const char* prefix, int n,
const UPB_DESC(EnumValueDescriptorProto*) const* protos,
const UPB_DESC(FeatureSet*) parent_features, upb_EnumDef* e,
bool* is_sorted) {
_upb_DefType_CheckPadding(sizeof(upb_EnumValueDef));
upb_EnumValueDef* v = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_EnumValueDef, n);
*is_sorted = true;
uint32_t previous = 0;
for (int i = 0; i < n; i++) {
create_enumvaldef(ctx, prefix, protos[i], parent_features, e, &v[i]);
const uint32_t current = v[i].number;
if (previous > current) *is_sorted = false;
previous = current;
}
_upb_EnumValueDef_CheckZeroValue(ctx, e, v, n);
return v;
}
#include <stdint.h>
// Must be last.
struct upb_ExtensionRange {
const UPB_DESC(ExtensionRangeOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
int32_t start;
int32_t end;
};
upb_ExtensionRange* _upb_ExtensionRange_At(const upb_ExtensionRange* r, int i) {
return (upb_ExtensionRange*)&r[i];
}
const UPB_DESC(ExtensionRangeOptions) *
upb_ExtensionRange_Options(const upb_ExtensionRange* r) {
return r->opts;
}
bool upb_ExtensionRange_HasOptions(const upb_ExtensionRange* r) {
return r->opts != (void*)kUpbDefOptDefault;
}
int32_t upb_ExtensionRange_Start(const upb_ExtensionRange* r) {
return r->start;
}
int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r) { return r->end; }
upb_ExtensionRange* _upb_ExtensionRanges_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto_ExtensionRange*) const* protos,
const UPB_DESC(FeatureSet*) parent_features, const upb_MessageDef* m) {
upb_ExtensionRange* r = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_ExtensionRange, n);
for (int i = 0; i < n; i++) {
UPB_DEF_SET_OPTIONS(r[i].opts, DescriptorProto_ExtensionRange,
ExtensionRangeOptions, protos[i]);
r[i].resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features,
UPB_DESC(ExtensionRangeOptions_features)(r[i].opts));
const int32_t start =
UPB_DESC(DescriptorProto_ExtensionRange_start)(protos[i]);
const int32_t end = UPB_DESC(DescriptorProto_ExtensionRange_end)(protos[i]);
const int32_t max = UPB_DESC(MessageOptions_message_set_wire_format)(
upb_MessageDef_Options(m))
? INT32_MAX
: kUpb_MaxFieldNumber + 1;
// A full validation would also check that each range is disjoint, and that
// none of the fields overlap with the extension ranges, but we are just
// sanity checking here.
if (start < 1 || end <= start || end > max) {
_upb_DefBuilder_Errf(ctx,
"Extension range (%d, %d) is invalid, message=%s\n",
(int)start, (int)end, upb_MessageDef_FullName(m));
}
r[i].start = start;
r[i].end = end;
}
return r;
}
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// Must be last.
#define UPB_FIELD_TYPE_UNSPECIFIED 0
typedef struct {
size_t len;
char str[1]; // Null-terminated string data follows.
} str_t;
struct upb_FieldDef {
UPB_ALIGN_AS(8) const UPB_DESC(FieldOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_FileDef* file;
const upb_MessageDef* msgdef;
const char* full_name;
const char* json_name;
union {
int64_t sint;
uint64_t uint;
double dbl;
float flt;
bool boolean;
str_t* str;
void* msg; // Always NULL.
} defaultval;
union {
const upb_OneofDef* oneof;
const upb_MessageDef* extension_scope;
} scope;
union {
const upb_MessageDef* msgdef;
const upb_EnumDef* enumdef;
const UPB_DESC(FieldDescriptorProto) * unresolved;
} sub;
uint32_t number_;
uint16_t index_;
uint16_t layout_index; // Index into msgdef->layout->fields or file->exts
bool has_default;
bool has_json_name;
bool has_presence;
bool is_extension;
bool is_proto3_optional;
upb_FieldType type_;
upb_Label label_;
};
upb_FieldDef* _upb_FieldDef_At(const upb_FieldDef* f, int i) {
return (upb_FieldDef*)&f[i];
}
const UPB_DESC(FieldOptions) * upb_FieldDef_Options(const upb_FieldDef* f) {
return f->opts;
}
bool upb_FieldDef_HasOptions(const upb_FieldDef* f) {
return f->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_FieldDef_ResolvedFeatures(const upb_FieldDef* f) {
return f->resolved_features;
}
const char* upb_FieldDef_FullName(const upb_FieldDef* f) {
return f->full_name;
}
upb_CType upb_FieldDef_CType(const upb_FieldDef* f) {
return upb_FieldType_CType(f->type_);
}
upb_FieldType upb_FieldDef_Type(const upb_FieldDef* f) { return f->type_; }
uint32_t upb_FieldDef_Index(const upb_FieldDef* f) { return f->index_; }
uint32_t upb_FieldDef_LayoutIndex(const upb_FieldDef* f) {
return f->layout_index;
}
upb_Label upb_FieldDef_Label(const upb_FieldDef* f) { return f->label_; }
uint32_t upb_FieldDef_Number(const upb_FieldDef* f) { return f->number_; }
bool upb_FieldDef_IsExtension(const upb_FieldDef* f) { return f->is_extension; }
bool _upb_FieldDef_IsPackable(const upb_FieldDef* f) {
return upb_FieldDef_IsRepeated(f) && upb_FieldDef_IsPrimitive(f);
}
bool upb_FieldDef_IsPacked(const upb_FieldDef* f) {
return _upb_FieldDef_IsPackable(f) &&
UPB_DESC(FeatureSet_repeated_field_encoding(f->resolved_features)) ==
UPB_DESC(FeatureSet_PACKED);
}
const char* upb_FieldDef_Name(const upb_FieldDef* f) {
return _upb_DefBuilder_FullToShort(f->full_name);
}
const char* upb_FieldDef_JsonName(const upb_FieldDef* f) {
return f->json_name;
}
bool upb_FieldDef_HasJsonName(const upb_FieldDef* f) {
return f->has_json_name;
}
const upb_FileDef* upb_FieldDef_File(const upb_FieldDef* f) { return f->file; }
const upb_MessageDef* upb_FieldDef_ContainingType(const upb_FieldDef* f) {
return f->msgdef;
}
const upb_MessageDef* upb_FieldDef_ExtensionScope(const upb_FieldDef* f) {
return f->is_extension ? f->scope.extension_scope : NULL;
}
const upb_OneofDef* upb_FieldDef_ContainingOneof(const upb_FieldDef* f) {
return f->is_extension ? NULL : f->scope.oneof;
}
const upb_OneofDef* upb_FieldDef_RealContainingOneof(const upb_FieldDef* f) {
const upb_OneofDef* oneof = upb_FieldDef_ContainingOneof(f);
if (!oneof || upb_OneofDef_IsSynthetic(oneof)) return NULL;
return oneof;
}
upb_MessageValue upb_FieldDef_Default(const upb_FieldDef* f) {
upb_MessageValue ret;
if (upb_FieldDef_IsRepeated(f) || upb_FieldDef_IsSubMessage(f)) {
return (upb_MessageValue){.msg_val = NULL};
}
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Bool:
return (upb_MessageValue){.bool_val = f->defaultval.boolean};
case kUpb_CType_Int64:
return (upb_MessageValue){.int64_val = f->defaultval.sint};
case kUpb_CType_UInt64:
return (upb_MessageValue){.uint64_val = f->defaultval.uint};
case kUpb_CType_Enum:
case kUpb_CType_Int32:
return (upb_MessageValue){.int32_val = (int32_t)f->defaultval.sint};
case kUpb_CType_UInt32:
return (upb_MessageValue){.uint32_val = (uint32_t)f->defaultval.uint};
case kUpb_CType_Float:
return (upb_MessageValue){.float_val = f->defaultval.flt};
case kUpb_CType_Double:
return (upb_MessageValue){.double_val = f->defaultval.dbl};
case kUpb_CType_String:
case kUpb_CType_Bytes: {
str_t* str = f->defaultval.str;
if (str) {
return (upb_MessageValue){
.str_val = (upb_StringView){.data = str->str, .size = str->len}};
} else {
return (upb_MessageValue){
.str_val = (upb_StringView){.data = NULL, .size = 0}};
}
}
default:
UPB_UNREACHABLE();
}
return ret;
}
const upb_MessageDef* upb_FieldDef_MessageSubDef(const upb_FieldDef* f) {
return upb_FieldDef_IsSubMessage(f) ? f->sub.msgdef : NULL;
}
const upb_EnumDef* upb_FieldDef_EnumSubDef(const upb_FieldDef* f) {
return upb_FieldDef_IsEnum(f) ? f->sub.enumdef : NULL;
}
const upb_MiniTableField* upb_FieldDef_MiniTable(const upb_FieldDef* f) {
if (upb_FieldDef_IsExtension(f)) {
const upb_FileDef* file = upb_FieldDef_File(f);
return (upb_MiniTableField*)_upb_FileDef_ExtensionMiniTable(
file, f->layout_index);
} else {
const upb_MiniTable* layout = upb_MessageDef_MiniTable(f->msgdef);
return &layout->UPB_PRIVATE(fields)[f->layout_index];
}
}
const upb_MiniTableExtension* upb_FieldDef_MiniTableExtension(
const upb_FieldDef* f) {
UPB_ASSERT(upb_FieldDef_IsExtension(f));
const upb_FileDef* file = upb_FieldDef_File(f);
return _upb_FileDef_ExtensionMiniTable(file, f->layout_index);
}
bool _upb_FieldDef_IsClosedEnum(const upb_FieldDef* f) {
if (f->type_ != kUpb_FieldType_Enum) return false;
return upb_EnumDef_IsClosed(f->sub.enumdef);
}
bool _upb_FieldDef_IsProto3Optional(const upb_FieldDef* f) {
return f->is_proto3_optional;
}
int _upb_FieldDef_LayoutIndex(const upb_FieldDef* f) { return f->layout_index; }
bool _upb_FieldDef_ValidateUtf8(const upb_FieldDef* f) {
if (upb_FieldDef_Type(f) != kUpb_FieldType_String) return false;
return UPB_DESC(FeatureSet_utf8_validation(f->resolved_features)) ==
UPB_DESC(FeatureSet_VERIFY);
}
bool _upb_FieldDef_IsGroupLike(const upb_FieldDef* f) {
// Groups are always tag-delimited.
if (f->type_ != kUpb_FieldType_Group) {
return false;
}
const upb_MessageDef* msg = upb_FieldDef_MessageSubDef(f);
// Group fields always are always the lowercase type name.
const char* mname = upb_MessageDef_Name(msg);
const char* fname = upb_FieldDef_Name(f);
size_t name_size = strlen(fname);
if (name_size != strlen(mname)) return false;
for (size_t i = 0; i < name_size; ++i) {
if ((mname[i] | 0x20) != fname[i]) {
// Case-insensitive ascii comparison.
return false;
}
}
if (upb_MessageDef_File(msg) != upb_FieldDef_File(f)) {
return false;
}
// Group messages are always defined in the same scope as the field. File
// level extensions will compare NULL == NULL here, which is why the file
// comparison above is necessary to ensure both come from the same file.
return upb_FieldDef_IsExtension(f) ? upb_FieldDef_ExtensionScope(f) ==
upb_MessageDef_ContainingType(msg)
: upb_FieldDef_ContainingType(f) ==
upb_MessageDef_ContainingType(msg);
}
uint64_t _upb_FieldDef_Modifiers(const upb_FieldDef* f) {
uint64_t out = upb_FieldDef_IsPacked(f) ? kUpb_FieldModifier_IsPacked : 0;
if (upb_FieldDef_IsRepeated(f)) {
out |= kUpb_FieldModifier_IsRepeated;
} else if (upb_FieldDef_IsRequired(f)) {
out |= kUpb_FieldModifier_IsRequired;
} else if (!upb_FieldDef_HasPresence(f)) {
out |= kUpb_FieldModifier_IsProto3Singular;
}
if (_upb_FieldDef_IsClosedEnum(f)) {
out |= kUpb_FieldModifier_IsClosedEnum;
}
if (_upb_FieldDef_ValidateUtf8(f)) {
out |= kUpb_FieldModifier_ValidateUtf8;
}
return out;
}
bool upb_FieldDef_HasDefault(const upb_FieldDef* f) { return f->has_default; }
bool upb_FieldDef_HasPresence(const upb_FieldDef* f) { return f->has_presence; }
bool upb_FieldDef_HasSubDef(const upb_FieldDef* f) {
return upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsEnum(f);
}
bool upb_FieldDef_IsEnum(const upb_FieldDef* f) {
return upb_FieldDef_CType(f) == kUpb_CType_Enum;
}
bool upb_FieldDef_IsMap(const upb_FieldDef* f) {
return upb_FieldDef_IsRepeated(f) && upb_FieldDef_IsSubMessage(f) &&
upb_MessageDef_IsMapEntry(upb_FieldDef_MessageSubDef(f));
}
bool upb_FieldDef_IsOptional(const upb_FieldDef* f) {
return upb_FieldDef_Label(f) == kUpb_Label_Optional;
}
bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f) {
return !upb_FieldDef_IsString(f) && !upb_FieldDef_IsSubMessage(f);
}
bool upb_FieldDef_IsRepeated(const upb_FieldDef* f) {
return upb_FieldDef_Label(f) == kUpb_Label_Repeated;
}
bool upb_FieldDef_IsRequired(const upb_FieldDef* f) {
return UPB_DESC(FeatureSet_field_presence)(f->resolved_features) ==
UPB_DESC(FeatureSet_LEGACY_REQUIRED);
}
bool upb_FieldDef_IsString(const upb_FieldDef* f) {
return upb_FieldDef_CType(f) == kUpb_CType_String ||
upb_FieldDef_CType(f) == kUpb_CType_Bytes;
}
bool upb_FieldDef_IsSubMessage(const upb_FieldDef* f) {
return upb_FieldDef_CType(f) == kUpb_CType_Message;
}
static bool between(int32_t x, int32_t low, int32_t high) {
return x >= low && x <= high;
}
bool upb_FieldDef_checklabel(int32_t label) { return between(label, 1, 3); }
bool upb_FieldDef_checktype(int32_t type) { return between(type, 1, 11); }
bool upb_FieldDef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
bool upb_FieldDef_checkdescriptortype(int32_t type) {
return between(type, 1, 18);
}
static bool streql2(const char* a, size_t n, const char* b) {
return n == strlen(b) && memcmp(a, b, n) == 0;
}
// Implement the transformation as described in the spec:
// 1. upper case all letters after an underscore.
// 2. remove all underscores.
static char* make_json_name(const char* name, size_t size, upb_Arena* a) {
char* out = upb_Arena_Malloc(a, size + 1); // +1 is to add a trailing '\0'
if (out == NULL) return NULL;
bool ucase_next = false;
char* des = out;
for (size_t i = 0; i < size; i++) {
if (name[i] == '_') {
ucase_next = true;
} else {
*des++ = ucase_next ? toupper(name[i]) : name[i];
ucase_next = false;
}
}
*des++ = '\0';
return out;
}
static str_t* newstr(upb_DefBuilder* ctx, const char* data, size_t len) {
str_t* ret = _upb_DefBuilder_Alloc(ctx, sizeof(*ret) + len);
if (!ret) _upb_DefBuilder_OomErr(ctx);
ret->len = len;
if (len) memcpy(ret->str, data, len);
ret->str[len] = '\0';
return ret;
}
static str_t* unescape(upb_DefBuilder* ctx, const upb_FieldDef* f,
const char* data, size_t len) {
// Size here is an upper bound; escape sequences could ultimately shrink it.
str_t* ret = _upb_DefBuilder_Alloc(ctx, sizeof(*ret) + len);
char* dst = &ret->str[0];
const char* src = data;
const char* end = data + len;
while (src < end) {
if (*src == '\\') {
src++;
*dst++ = _upb_DefBuilder_ParseEscape(ctx, f, &src, end);
} else {
*dst++ = *src++;
}
}
ret->len = dst - &ret->str[0];
return ret;
}
static void parse_default(upb_DefBuilder* ctx, const char* str, size_t len,
upb_FieldDef* f) {
char* end;
char nullz[64];
errno = 0;
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Int32:
case kUpb_CType_Int64:
case kUpb_CType_UInt32:
case kUpb_CType_UInt64:
case kUpb_CType_Double:
case kUpb_CType_Float:
// Standard C number parsing functions expect null-terminated strings.
if (len >= sizeof(nullz) - 1) {
_upb_DefBuilder_Errf(ctx, "Default too long: %.*s", (int)len, str);
}
memcpy(nullz, str, len);
nullz[len] = '\0';
str = nullz;
break;
default:
break;
}
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Int32: {
long val = strtol(str, &end, 0);
if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || *end) {
goto invalid;
}
f->defaultval.sint = val;
break;
}
case kUpb_CType_Enum: {
const upb_EnumDef* e = f->sub.enumdef;
const upb_EnumValueDef* ev =
upb_EnumDef_FindValueByNameWithSize(e, str, len);
if (!ev) {
goto invalid;
}
f->defaultval.sint = upb_EnumValueDef_Number(ev);
break;
}
case kUpb_CType_Int64: {
long long val = strtoll(str, &end, 0);
if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end) {
goto invalid;
}
f->defaultval.sint = val;
break;
}
case kUpb_CType_UInt32: {
unsigned long val = strtoul(str, &end, 0);
if (val > UINT32_MAX || errno == ERANGE || *end) {
goto invalid;
}
f->defaultval.uint = val;
break;
}
case kUpb_CType_UInt64: {
unsigned long long val = strtoull(str, &end, 0);
if (val > UINT64_MAX || errno == ERANGE || *end) {
goto invalid;
}
f->defaultval.uint = val;
break;
}
case kUpb_CType_Double: {
double val = strtod(str, &end);
if (errno == ERANGE || *end) {
goto invalid;
}
f->defaultval.dbl = val;
break;
}
case kUpb_CType_Float: {
float val = strtof(str, &end);
if (errno == ERANGE || *end) {
goto invalid;
}
f->defaultval.flt = val;
break;
}
case kUpb_CType_Bool: {
if (streql2(str, len, "false")) {
f->defaultval.boolean = false;
} else if (streql2(str, len, "true")) {
f->defaultval.boolean = true;
} else {
goto invalid;
}
break;
}
case kUpb_CType_String:
f->defaultval.str = newstr(ctx, str, len);
break;
case kUpb_CType_Bytes:
f->defaultval.str = unescape(ctx, f, str, len);
break;
case kUpb_CType_Message:
/* Should not have a default value. */
_upb_DefBuilder_Errf(ctx, "Message should not have a default (%s)",
upb_FieldDef_FullName(f));
}
return;
invalid:
_upb_DefBuilder_Errf(ctx, "Invalid default '%.*s' for field %s of type %d",
(int)len, str, upb_FieldDef_FullName(f),
(int)upb_FieldDef_Type(f));
}
static void set_default_default(upb_DefBuilder* ctx, upb_FieldDef* f) {
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Int32:
case kUpb_CType_Int64:
f->defaultval.sint = 0;
break;
case kUpb_CType_UInt64:
case kUpb_CType_UInt32:
f->defaultval.uint = 0;
break;
case kUpb_CType_Double:
case kUpb_CType_Float:
f->defaultval.dbl = 0;
break;
case kUpb_CType_String:
case kUpb_CType_Bytes:
f->defaultval.str = newstr(ctx, NULL, 0);
break;
case kUpb_CType_Bool:
f->defaultval.boolean = false;
break;
case kUpb_CType_Enum: {
const upb_EnumValueDef* v = upb_EnumDef_Value(f->sub.enumdef, 0);
f->defaultval.sint = upb_EnumValueDef_Number(v);
break;
}
case kUpb_CType_Message:
break;
}
}
static bool _upb_FieldDef_InferLegacyFeatures(
upb_DefBuilder* ctx, upb_FieldDef* f,
const UPB_DESC(FieldDescriptorProto*) proto,
const UPB_DESC(FieldOptions*) options, upb_Syntax syntax,
UPB_DESC(FeatureSet*) features) {
bool ret = false;
if (UPB_DESC(FieldDescriptorProto_label)(proto) == kUpb_Label_Required) {
if (syntax == kUpb_Syntax_Proto3) {
_upb_DefBuilder_Errf(ctx, "proto3 fields cannot be required (%s)",
f->full_name);
}
int val = UPB_DESC(FeatureSet_LEGACY_REQUIRED);
UPB_DESC(FeatureSet_set_field_presence(features, val));
ret = true;
}
if (UPB_DESC(FieldDescriptorProto_type)(proto) == kUpb_FieldType_Group) {
int val = UPB_DESC(FeatureSet_DELIMITED);
UPB_DESC(FeatureSet_set_message_encoding(features, val));
ret = true;
}
if (UPB_DESC(FieldOptions_has_packed)(options)) {
int val = UPB_DESC(FieldOptions_packed)(options)
? UPB_DESC(FeatureSet_PACKED)
: UPB_DESC(FeatureSet_EXPANDED);
UPB_DESC(FeatureSet_set_repeated_field_encoding(features, val));
ret = true;
}
return ret;
}
static void _upb_FieldDef_Create(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(FeatureSet*) parent_features,
const UPB_DESC(FieldDescriptorProto*)
field_proto,
upb_MessageDef* m, upb_FieldDef* f) {
// Must happen before _upb_DefBuilder_Add()
f->file = _upb_DefBuilder_File(ctx);
const upb_StringView name = UPB_DESC(FieldDescriptorProto_name)(field_proto);
f->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
f->number_ = UPB_DESC(FieldDescriptorProto_number)(field_proto);
f->is_proto3_optional =
UPB_DESC(FieldDescriptorProto_proto3_optional)(field_proto);
f->msgdef = m;
f->scope.oneof = NULL;
UPB_DEF_SET_OPTIONS(f->opts, FieldDescriptorProto, FieldOptions, field_proto);
upb_Syntax syntax = upb_FileDef_Syntax(f->file);
const UPB_DESC(FeatureSet*) unresolved_features =
UPB_DESC(FieldOptions_features)(f->opts);
bool implicit = false;
if (syntax != kUpb_Syntax_Editions) {
upb_Message_Clear(UPB_UPCAST(ctx->legacy_features),
UPB_DESC_MINITABLE(FeatureSet));
if (_upb_FieldDef_InferLegacyFeatures(ctx, f, field_proto, f->opts, syntax,
ctx->legacy_features)) {
implicit = true;
unresolved_features = ctx->legacy_features;
}
}
if (UPB_DESC(FieldDescriptorProto_has_oneof_index)(field_proto)) {
int oneof_index = UPB_DESC(FieldDescriptorProto_oneof_index)(field_proto);
if (!m) {
_upb_DefBuilder_Errf(ctx, "oneof field (%s) has no containing msg",
f->full_name);
}
if (oneof_index < 0 || oneof_index >= upb_MessageDef_OneofCount(m)) {
_upb_DefBuilder_Errf(ctx, "oneof_index out of range (%s)", f->full_name);
}
upb_OneofDef* oneof = (upb_OneofDef*)upb_MessageDef_Oneof(m, oneof_index);
f->scope.oneof = oneof;
parent_features = upb_OneofDef_ResolvedFeatures(oneof);
_upb_OneofDef_Insert(ctx, oneof, f, name.data, name.size);
}
f->resolved_features = _upb_DefBuilder_DoResolveFeatures(
ctx, parent_features, unresolved_features, implicit);
f->label_ = (int)UPB_DESC(FieldDescriptorProto_label)(field_proto);
if (f->label_ == kUpb_Label_Optional &&
// TODO: remove once we can deprecate kUpb_Label_Required.
UPB_DESC(FeatureSet_field_presence)(f->resolved_features) ==
UPB_DESC(FeatureSet_LEGACY_REQUIRED)) {
f->label_ = kUpb_Label_Required;
}
if (!UPB_DESC(FieldDescriptorProto_has_name)(field_proto)) {
_upb_DefBuilder_Errf(ctx, "field has no name");
}
f->has_json_name = UPB_DESC(FieldDescriptorProto_has_json_name)(field_proto);
if (f->has_json_name) {
const upb_StringView sv =
UPB_DESC(FieldDescriptorProto_json_name)(field_proto);
f->json_name = upb_strdup2(sv.data, sv.size, ctx->arena);
} else {
f->json_name = make_json_name(name.data, name.size, ctx->arena);
}
if (!f->json_name) _upb_DefBuilder_OomErr(ctx);
const bool has_type = UPB_DESC(FieldDescriptorProto_has_type)(field_proto);
const bool has_type_name =
UPB_DESC(FieldDescriptorProto_has_type_name)(field_proto);
f->type_ = (int)UPB_DESC(FieldDescriptorProto_type)(field_proto);
if (has_type) {
switch (f->type_) {
case kUpb_FieldType_Message:
case kUpb_FieldType_Group:
case kUpb_FieldType_Enum:
if (!has_type_name) {
_upb_DefBuilder_Errf(ctx, "field of type %d requires type name (%s)",
(int)f->type_, f->full_name);
}
break;
default:
if (has_type_name) {
_upb_DefBuilder_Errf(
ctx, "invalid type for field with type_name set (%s, %d)",
f->full_name, (int)f->type_);
}
}
}
if ((!has_type && has_type_name) || f->type_ == kUpb_FieldType_Message) {
f->type_ =
UPB_FIELD_TYPE_UNSPECIFIED; // We'll assign this in resolve_subdef()
} else {
if (f->type_ < kUpb_FieldType_Double || f->type_ > kUpb_FieldType_SInt64) {
_upb_DefBuilder_Errf(ctx, "invalid type for field %s (%d)", f->full_name,
f->type_);
}
}
if (f->label_ < kUpb_Label_Optional || f->label_ > kUpb_Label_Repeated) {
_upb_DefBuilder_Errf(ctx, "invalid label for field %s (%d)", f->full_name,
f->label_);
}
/* We can't resolve the subdef or (in the case of extensions) the containing
* message yet, because it may not have been defined yet. We stash a pointer
* to the field_proto until later when we can properly resolve it. */
f->sub.unresolved = field_proto;
if (UPB_DESC(FieldDescriptorProto_has_oneof_index)(field_proto)) {
if (upb_FieldDef_Label(f) != kUpb_Label_Optional) {
_upb_DefBuilder_Errf(ctx, "fields in oneof must have OPTIONAL label (%s)",
f->full_name);
}
}
f->has_presence =
(!upb_FieldDef_IsRepeated(f)) &&
(f->is_extension ||
(f->type_ == kUpb_FieldType_Message ||
f->type_ == kUpb_FieldType_Group || upb_FieldDef_ContainingOneof(f) ||
UPB_DESC(FeatureSet_field_presence)(f->resolved_features) !=
UPB_DESC(FeatureSet_IMPLICIT)));
}
static void _upb_FieldDef_CreateExt(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(FeatureSet*) parent_features,
const UPB_DESC(FieldDescriptorProto*)
field_proto,
upb_MessageDef* m, upb_FieldDef* f) {
f->is_extension = true;
_upb_FieldDef_Create(ctx, prefix, parent_features, field_proto, m, f);
if (UPB_DESC(FieldDescriptorProto_has_oneof_index)(field_proto)) {
_upb_DefBuilder_Errf(ctx, "oneof_index provided for extension field (%s)",
f->full_name);
}
f->scope.extension_scope = m;
_upb_DefBuilder_Add(ctx, f->full_name, _upb_DefType_Pack(f, UPB_DEFTYPE_EXT));
f->layout_index = ctx->ext_count++;
if (ctx->layout) {
UPB_ASSERT(upb_MiniTableExtension_Number(
upb_FieldDef_MiniTableExtension(f)) == f->number_);
}
}
static void _upb_FieldDef_CreateNotExt(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(FeatureSet*)
parent_features,
const UPB_DESC(FieldDescriptorProto*)
field_proto,
upb_MessageDef* m, upb_FieldDef* f) {
f->is_extension = false;
_upb_FieldDef_Create(ctx, prefix, parent_features, field_proto, m, f);
if (!UPB_DESC(FieldDescriptorProto_has_oneof_index)(field_proto)) {
if (f->is_proto3_optional) {
_upb_DefBuilder_Errf(
ctx,
"non-extension field (%s) with proto3_optional was not in a oneof",
f->full_name);
}
}
_upb_MessageDef_InsertField(ctx, m, f);
}
upb_FieldDef* _upb_Extensions_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const char* prefix, upb_MessageDef* m) {
_upb_DefType_CheckPadding(sizeof(upb_FieldDef));
upb_FieldDef* defs = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_FieldDef, n);
for (int i = 0; i < n; i++) {
upb_FieldDef* f = &defs[i];
_upb_FieldDef_CreateExt(ctx, prefix, parent_features, protos[i], m, f);
f->index_ = i;
}
return defs;
}
upb_FieldDef* _upb_FieldDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const char* prefix, upb_MessageDef* m,
bool* is_sorted) {
_upb_DefType_CheckPadding(sizeof(upb_FieldDef));
upb_FieldDef* defs = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_FieldDef, n);
uint32_t previous = 0;
for (int i = 0; i < n; i++) {
upb_FieldDef* f = &defs[i];
_upb_FieldDef_CreateNotExt(ctx, prefix, parent_features, protos[i], m, f);
f->index_ = i;
if (!ctx->layout) {
// Speculate that the def fields are sorted. We will always sort the
// MiniTable fields, so if defs are sorted then indices will match.
//
// If this is incorrect, we will overwrite later.
f->layout_index = i;
}
const uint32_t current = f->number_;
if (previous > current) *is_sorted = false;
previous = current;
}
return defs;
}
static void resolve_subdef(upb_DefBuilder* ctx, const char* prefix,
upb_FieldDef* f) {
const UPB_DESC(FieldDescriptorProto)* field_proto = f->sub.unresolved;
upb_StringView name = UPB_DESC(FieldDescriptorProto_type_name)(field_proto);
bool has_name = UPB_DESC(FieldDescriptorProto_has_type_name)(field_proto);
switch ((int)f->type_) {
case UPB_FIELD_TYPE_UNSPECIFIED: {
// Type was not specified and must be inferred.
UPB_ASSERT(has_name);
upb_deftype_t type;
const void* def =
_upb_DefBuilder_ResolveAny(ctx, f->full_name, prefix, name, &type);
switch (type) {
case UPB_DEFTYPE_ENUM:
f->sub.enumdef = def;
f->type_ = kUpb_FieldType_Enum;
break;
case UPB_DEFTYPE_MSG:
f->sub.msgdef = def;
f->type_ = kUpb_FieldType_Message;
// TODO: remove once we can deprecate
// kUpb_FieldType_Group.
if (UPB_DESC(FeatureSet_message_encoding)(f->resolved_features) ==
UPB_DESC(FeatureSet_DELIMITED) &&
!upb_MessageDef_IsMapEntry(def) &&
!(f->msgdef && upb_MessageDef_IsMapEntry(f->msgdef))) {
f->type_ = kUpb_FieldType_Group;
}
f->has_presence = !upb_FieldDef_IsRepeated(f);
break;
default:
_upb_DefBuilder_Errf(ctx, "Couldn't resolve type name for field %s",
f->full_name);
}
break;
}
case kUpb_FieldType_Message:
case kUpb_FieldType_Group:
UPB_ASSERT(has_name);
f->sub.msgdef = _upb_DefBuilder_Resolve(ctx, f->full_name, prefix, name,
UPB_DEFTYPE_MSG);
break;
case kUpb_FieldType_Enum:
UPB_ASSERT(has_name);
f->sub.enumdef = _upb_DefBuilder_Resolve(ctx, f->full_name, prefix, name,
UPB_DEFTYPE_ENUM);
break;
default:
// No resolution necessary.
break;
}
}
static int _upb_FieldDef_Compare(const void* p1, const void* p2) {
const uint32_t v1 = (*(upb_FieldDef**)p1)->number_;
const uint32_t v2 = (*(upb_FieldDef**)p2)->number_;
return (v1 < v2) ? -1 : (v1 > v2);
}
// _upb_FieldDefs_Sorted() is mostly a pure function of its inputs, but has one
// critical side effect that we depend on: it sets layout_index appropriately
// for non-sorted lists of fields.
const upb_FieldDef** _upb_FieldDefs_Sorted(const upb_FieldDef* f, int n,
upb_Arena* a) {
// TODO: Replace this arena alloc with a persistent scratch buffer.
upb_FieldDef** out = (upb_FieldDef**)upb_Arena_Malloc(a, n * sizeof(void*));
if (!out) return NULL;
for (int i = 0; i < n; i++) {
out[i] = (upb_FieldDef*)&f[i];
}
qsort(out, n, sizeof(void*), _upb_FieldDef_Compare);
for (int i = 0; i < n; i++) {
out[i]->layout_index = i;
}
return (const upb_FieldDef**)out;
}
bool upb_FieldDef_MiniDescriptorEncode(const upb_FieldDef* f, upb_Arena* a,
upb_StringView* out) {
UPB_ASSERT(f->is_extension);
upb_DescState s;
_upb_DescState_Init(&s);
const int number = upb_FieldDef_Number(f);
const uint64_t modifiers = _upb_FieldDef_Modifiers(f);
if (!_upb_DescState_Grow(&s, a)) return false;
s.ptr = upb_MtDataEncoder_EncodeExtension(&s.e, s.ptr, f->type_, number,
modifiers);
*s.ptr = '\0';
out->data = s.buf;
out->size = s.ptr - s.buf;
return true;
}
static void resolve_extension(upb_DefBuilder* ctx, const char* prefix,
upb_FieldDef* f,
const UPB_DESC(FieldDescriptorProto) *
field_proto) {
if (!UPB_DESC(FieldDescriptorProto_has_extendee)(field_proto)) {
_upb_DefBuilder_Errf(ctx, "extension for field '%s' had no extendee",
f->full_name);
}
upb_StringView name = UPB_DESC(FieldDescriptorProto_extendee)(field_proto);
const upb_MessageDef* m =
_upb_DefBuilder_Resolve(ctx, f->full_name, prefix, name, UPB_DEFTYPE_MSG);
f->msgdef = m;
if (!_upb_MessageDef_IsValidExtensionNumber(m, f->number_)) {
_upb_DefBuilder_Errf(
ctx,
"field number %u in extension %s has no extension range in message %s",
(unsigned)f->number_, f->full_name, upb_MessageDef_FullName(m));
}
}
void _upb_FieldDef_BuildMiniTableExtension(upb_DefBuilder* ctx,
const upb_FieldDef* f) {
const upb_MiniTableExtension* ext = upb_FieldDef_MiniTableExtension(f);
if (ctx->layout) {
UPB_ASSERT(upb_FieldDef_Number(f) == upb_MiniTableExtension_Number(ext));
} else {
upb_StringView desc;
if (!upb_FieldDef_MiniDescriptorEncode(f, ctx->tmp_arena, &desc)) {
_upb_DefBuilder_OomErr(ctx);
}
upb_MiniTableExtension* mut_ext = (upb_MiniTableExtension*)ext;
upb_MiniTableSub sub = {NULL};
if (upb_FieldDef_IsSubMessage(f)) {
const upb_MiniTable* submsg = upb_MessageDef_MiniTable(f->sub.msgdef);
sub = upb_MiniTableSub_FromMessage(submsg);
} else if (_upb_FieldDef_IsClosedEnum(f)) {
const upb_MiniTableEnum* subenum = _upb_EnumDef_MiniTable(f->sub.enumdef);
sub = upb_MiniTableSub_FromEnum(subenum);
}
bool ok2 = _upb_MiniTableExtension_Init(desc.data, desc.size, mut_ext,
upb_MessageDef_MiniTable(f->msgdef),
sub, ctx->platform, ctx->status);
if (!ok2) _upb_DefBuilder_Errf(ctx, "Could not build extension mini table");
}
bool ok = _upb_DefPool_InsertExt(ctx->symtab, ext, f);
if (!ok) _upb_DefBuilder_OomErr(ctx);
}
static void resolve_default(upb_DefBuilder* ctx, upb_FieldDef* f,
const UPB_DESC(FieldDescriptorProto) *
field_proto) {
// Have to delay resolving of the default value until now because of the enum
// case, since enum defaults are specified with a label.
if (UPB_DESC(FieldDescriptorProto_has_default_value)(field_proto)) {
upb_StringView defaultval =
UPB_DESC(FieldDescriptorProto_default_value)(field_proto);
if (upb_FileDef_Syntax(f->file) == kUpb_Syntax_Proto3) {
_upb_DefBuilder_Errf(ctx,
"proto3 fields cannot have explicit defaults (%s)",
f->full_name);
}
if (upb_FieldDef_IsSubMessage(f)) {
_upb_DefBuilder_Errf(ctx,
"message fields cannot have explicit defaults (%s)",
f->full_name);
}
parse_default(ctx, defaultval.data, defaultval.size, f);
f->has_default = true;
} else {
set_default_default(ctx, f);
f->has_default = false;
}
}
void _upb_FieldDef_Resolve(upb_DefBuilder* ctx, const char* prefix,
upb_FieldDef* f) {
// We have to stash this away since resolve_subdef() may overwrite it.
const UPB_DESC(FieldDescriptorProto)* field_proto = f->sub.unresolved;
resolve_subdef(ctx, prefix, f);
resolve_default(ctx, f, field_proto);
if (f->is_extension) {
resolve_extension(ctx, prefix, f, field_proto);
}
}
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
struct upb_FileDef {
const UPB_DESC(FileOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const char* name;
const char* package;
UPB_DESC(Edition) edition;
const upb_FileDef** deps;
const int32_t* public_deps;
const int32_t* weak_deps;
const upb_MessageDef* top_lvl_msgs;
const upb_EnumDef* top_lvl_enums;
const upb_FieldDef* top_lvl_exts;
const upb_ServiceDef* services;
const upb_MiniTableExtension** ext_layouts;
const upb_DefPool* symtab;
int dep_count;
int public_dep_count;
int weak_dep_count;
int top_lvl_msg_count;
int top_lvl_enum_count;
int top_lvl_ext_count;
int service_count;
int ext_count; // All exts in the file.
upb_Syntax syntax;
};
UPB_API const char* upb_FileDef_EditionName(int edition) {
// TODO Synchronize this with descriptor.proto better.
switch (edition) {
case UPB_DESC(EDITION_PROTO2):
return "PROTO2";
case UPB_DESC(EDITION_PROTO3):
return "PROTO3";
case UPB_DESC(EDITION_2023):
return "2023";
default:
return "UNKNOWN";
}
}
const UPB_DESC(FileOptions) * upb_FileDef_Options(const upb_FileDef* f) {
return f->opts;
}
const UPB_DESC(FeatureSet) *
upb_FileDef_ResolvedFeatures(const upb_FileDef* f) {
return f->resolved_features;
}
bool upb_FileDef_HasOptions(const upb_FileDef* f) {
return f->opts != (void*)kUpbDefOptDefault;
}
const char* upb_FileDef_Name(const upb_FileDef* f) { return f->name; }
const char* upb_FileDef_Package(const upb_FileDef* f) {
return f->package ? f->package : "";
}
UPB_DESC(Edition) upb_FileDef_Edition(const upb_FileDef* f) {
return f->edition;
}
const char* _upb_FileDef_RawPackage(const upb_FileDef* f) { return f->package; }
upb_Syntax upb_FileDef_Syntax(const upb_FileDef* f) { return f->syntax; }
int upb_FileDef_TopLevelMessageCount(const upb_FileDef* f) {
return f->top_lvl_msg_count;
}
int upb_FileDef_DependencyCount(const upb_FileDef* f) { return f->dep_count; }
int upb_FileDef_PublicDependencyCount(const upb_FileDef* f) {
return f->public_dep_count;
}
int upb_FileDef_WeakDependencyCount(const upb_FileDef* f) {
return f->weak_dep_count;
}
const int32_t* _upb_FileDef_PublicDependencyIndexes(const upb_FileDef* f) {
return f->public_deps;
}
const int32_t* _upb_FileDef_WeakDependencyIndexes(const upb_FileDef* f) {
return f->weak_deps;
}
int upb_FileDef_TopLevelEnumCount(const upb_FileDef* f) {
return f->top_lvl_enum_count;
}
int upb_FileDef_TopLevelExtensionCount(const upb_FileDef* f) {
return f->top_lvl_ext_count;
}
int upb_FileDef_ServiceCount(const upb_FileDef* f) { return f->service_count; }
const upb_FileDef* upb_FileDef_Dependency(const upb_FileDef* f, int i) {
UPB_ASSERT(0 <= i && i < f->dep_count);
return f->deps[i];
}
const upb_FileDef* upb_FileDef_PublicDependency(const upb_FileDef* f, int i) {
UPB_ASSERT(0 <= i && i < f->public_dep_count);
return f->deps[f->public_deps[i]];
}
const upb_FileDef* upb_FileDef_WeakDependency(const upb_FileDef* f, int i) {
UPB_ASSERT(0 <= i && i < f->public_dep_count);
return f->deps[f->weak_deps[i]];
}
const upb_MessageDef* upb_FileDef_TopLevelMessage(const upb_FileDef* f, int i) {
UPB_ASSERT(0 <= i && i < f->top_lvl_msg_count);
return _upb_MessageDef_At(f->top_lvl_msgs, i);
}
const upb_EnumDef* upb_FileDef_TopLevelEnum(const upb_FileDef* f, int i) {
UPB_ASSERT(0 <= i && i < f->top_lvl_enum_count);
return _upb_EnumDef_At(f->top_lvl_enums, i);
}
const upb_FieldDef* upb_FileDef_TopLevelExtension(const upb_FileDef* f, int i) {
UPB_ASSERT(0 <= i && i < f->top_lvl_ext_count);
return _upb_FieldDef_At(f->top_lvl_exts, i);
}
const upb_ServiceDef* upb_FileDef_Service(const upb_FileDef* f, int i) {
UPB_ASSERT(0 <= i && i < f->service_count);
return _upb_ServiceDef_At(f->services, i);
}
const upb_DefPool* upb_FileDef_Pool(const upb_FileDef* f) { return f->symtab; }
const upb_MiniTableExtension* _upb_FileDef_ExtensionMiniTable(
const upb_FileDef* f, int i) {
return f->ext_layouts[i];
}
// Note: Import cycles are not allowed so this will terminate.
bool upb_FileDef_Resolves(const upb_FileDef* f, const char* path) {
if (!strcmp(f->name, path)) return true;
for (int i = 0; i < upb_FileDef_PublicDependencyCount(f); i++) {
const upb_FileDef* dep = upb_FileDef_PublicDependency(f, i);
if (upb_FileDef_Resolves(dep, path)) return true;
}
return false;
}
static char* _strviewdup(upb_DefBuilder* ctx, upb_StringView view) {
char* ret = upb_strdup2(view.data, view.size, _upb_DefBuilder_Arena(ctx));
if (!ret) _upb_DefBuilder_OomErr(ctx);
return ret;
}
static bool streql_view(upb_StringView view, const char* b) {
return view.size == strlen(b) && memcmp(view.data, b, view.size) == 0;
}
static int count_exts_in_msg(const UPB_DESC(DescriptorProto) * msg_proto) {
size_t n;
UPB_DESC(DescriptorProto_extension)(msg_proto, &n);
int ext_count = n;
const UPB_DESC(DescriptorProto)* const* nested_msgs =
UPB_DESC(DescriptorProto_nested_type)(msg_proto, &n);
for (size_t i = 0; i < n; i++) {
ext_count += count_exts_in_msg(nested_msgs[i]);
}
return ext_count;
}
const UPB_DESC(FeatureSet*)
_upb_FileDef_FindEdition(upb_DefBuilder* ctx, int edition) {
const UPB_DESC(FeatureSetDefaults)* defaults =
upb_DefPool_FeatureSetDefaults(ctx->symtab);
int min = UPB_DESC(FeatureSetDefaults_minimum_edition)(defaults);
int max = UPB_DESC(FeatureSetDefaults_maximum_edition)(defaults);
if (edition < min) {
_upb_DefBuilder_Errf(ctx,
"Edition %s is earlier than the minimum edition %s "
"given in the defaults",
upb_FileDef_EditionName(edition),
upb_FileDef_EditionName(min));
return NULL;
}
if (edition > max) {
_upb_DefBuilder_Errf(ctx,
"Edition %s is later than the maximum edition %s "
"given in the defaults",
upb_FileDef_EditionName(edition),
upb_FileDef_EditionName(max));
return NULL;
}
size_t n;
const UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault)* const* d =
UPB_DESC(FeatureSetDefaults_defaults)(defaults, &n);
const UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault)* result = NULL;
for (size_t i = 0; i < n; i++) {
if (UPB_DESC(FeatureSetDefaults_FeatureSetEditionDefault_edition)(d[i]) >
edition) {
break;
}
result = d[i];
}
if (result == NULL) {
_upb_DefBuilder_Errf(ctx, "No valid default found for edition %s",
upb_FileDef_EditionName(edition));
return NULL;
}
// Merge the fixed and overridable features to get the edition's default
// feature set.
const UPB_DESC(FeatureSet)* fixed = UPB_DESC(
FeatureSetDefaults_FeatureSetEditionDefault_fixed_features)(result);
const UPB_DESC(FeatureSet)* overridable = UPB_DESC(
FeatureSetDefaults_FeatureSetEditionDefault_overridable_features)(result);
if (!fixed && !overridable) {
_upb_DefBuilder_Errf(ctx, "No valid default found for edition %s",
upb_FileDef_EditionName(edition));
return NULL;
} else if (!fixed) {
return overridable;
}
return _upb_DefBuilder_DoResolveFeatures(ctx, fixed, overridable,
/*is_implicit=*/true);
}
// Allocate and initialize one file def, and add it to the context object.
void _upb_FileDef_Create(upb_DefBuilder* ctx,
const UPB_DESC(FileDescriptorProto) * file_proto) {
upb_FileDef* file = _upb_DefBuilder_Alloc(ctx, sizeof(upb_FileDef));
ctx->file = file;
const UPB_DESC(DescriptorProto)* const* msgs;
const UPB_DESC(EnumDescriptorProto)* const* enums;
const UPB_DESC(FieldDescriptorProto)* const* exts;
const UPB_DESC(ServiceDescriptorProto)* const* services;
const upb_StringView* strs;
const int32_t* public_deps;
const int32_t* weak_deps;
size_t n;
file->symtab = ctx->symtab;
// Count all extensions in the file, to build a flat array of layouts.
UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
int ext_count = n;
msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
for (size_t i = 0; i < n; i++) {
ext_count += count_exts_in_msg(msgs[i]);
}
file->ext_count = ext_count;
if (ctx->layout) {
// We are using the ext layouts that were passed in.
file->ext_layouts = ctx->layout->UPB_PRIVATE(exts);
const int mt_ext_count = upb_MiniTableFile_ExtensionCount(ctx->layout);
if (mt_ext_count != file->ext_count) {
_upb_DefBuilder_Errf(ctx,
"Extension count did not match layout (%d vs %d)",
mt_ext_count, file->ext_count);
}
} else {
// We are building ext layouts from scratch.
file->ext_layouts = _upb_DefBuilder_Alloc(
ctx, sizeof(*file->ext_layouts) * file->ext_count);
upb_MiniTableExtension* ext =
UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_MiniTableExtension, file->ext_count);
for (int i = 0; i < file->ext_count; i++) {
file->ext_layouts[i] = &ext[i];
}
}
upb_StringView name = UPB_DESC(FileDescriptorProto_name)(file_proto);
file->name = _strviewdup(ctx, name);
if (strlen(file->name) != name.size) {
_upb_DefBuilder_Errf(ctx, "File name contained embedded NULL");
}
upb_StringView package = UPB_DESC(FileDescriptorProto_package)(file_proto);
if (package.size) {
_upb_DefBuilder_CheckIdentFull(ctx, package);
file->package = _strviewdup(ctx, package);
} else {
file->package = NULL;
}
// TODO: How should we validate this?
file->edition = UPB_DESC(FileDescriptorProto_edition)(file_proto);
if (UPB_DESC(FileDescriptorProto_has_syntax)(file_proto)) {
upb_StringView syntax = UPB_DESC(FileDescriptorProto_syntax)(file_proto);
if (streql_view(syntax, "proto2")) {
file->syntax = kUpb_Syntax_Proto2;
file->edition = UPB_DESC(EDITION_PROTO2);
} else if (streql_view(syntax, "proto3")) {
file->syntax = kUpb_Syntax_Proto3;
file->edition = UPB_DESC(EDITION_PROTO3);
} else if (streql_view(syntax, "editions")) {
file->syntax = kUpb_Syntax_Editions;
file->edition = UPB_DESC(FileDescriptorProto_edition)(file_proto);
} else {
_upb_DefBuilder_Errf(ctx, "Invalid syntax '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(syntax));
}
} else {
file->syntax = kUpb_Syntax_Proto2;
file->edition = UPB_DESC(EDITION_PROTO2);
}
// Read options.
UPB_DEF_SET_OPTIONS(file->opts, FileDescriptorProto, FileOptions, file_proto);
// Resolve features.
const UPB_DESC(FeatureSet*) edition_defaults =
_upb_FileDef_FindEdition(ctx, file->edition);
const UPB_DESC(FeatureSet*) unresolved =
UPB_DESC(FileOptions_features)(file->opts);
file->resolved_features =
_upb_DefBuilder_ResolveFeatures(ctx, edition_defaults, unresolved);
// Verify dependencies.
strs = UPB_DESC(FileDescriptorProto_dependency)(file_proto, &n);
file->dep_count = n;
file->deps = UPB_DEFBUILDER_ALLOCARRAY(ctx, const upb_FileDef*, n);
for (size_t i = 0; i < n; i++) {
upb_StringView str = strs[i];
file->deps[i] =
upb_DefPool_FindFileByNameWithSize(ctx->symtab, str.data, str.size);
if (!file->deps[i]) {
_upb_DefBuilder_Errf(ctx,
"Depends on file '" UPB_STRINGVIEW_FORMAT
"', but it has not been loaded",
UPB_STRINGVIEW_ARGS(str));
}
}
public_deps = UPB_DESC(FileDescriptorProto_public_dependency)(file_proto, &n);
file->public_dep_count = n;
file->public_deps = UPB_DEFBUILDER_ALLOCARRAY(ctx, int32_t, n);
int32_t* mutable_public_deps = (int32_t*)file->public_deps;
for (size_t i = 0; i < n; i++) {
if (public_deps[i] >= file->dep_count) {
_upb_DefBuilder_Errf(ctx, "public_dep %d is out of range",
(int)public_deps[i]);
}
mutable_public_deps[i] = public_deps[i];
}
weak_deps = UPB_DESC(FileDescriptorProto_weak_dependency)(file_proto, &n);
file->weak_dep_count = n;
file->weak_deps = UPB_DEFBUILDER_ALLOCARRAY(ctx, const int32_t, n);
int32_t* mutable_weak_deps = (int32_t*)file->weak_deps;
for (size_t i = 0; i < n; i++) {
if (weak_deps[i] >= file->dep_count) {
_upb_DefBuilder_Errf(ctx, "weak_dep %d is out of range",
(int)weak_deps[i]);
}
mutable_weak_deps[i] = weak_deps[i];
}
// Create enums.
enums = UPB_DESC(FileDescriptorProto_enum_type)(file_proto, &n);
file->top_lvl_enum_count = n;
file->top_lvl_enums =
_upb_EnumDefs_New(ctx, n, enums, file->resolved_features, NULL);
// Create extensions.
exts = UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
file->top_lvl_ext_count = n;
file->top_lvl_exts = _upb_Extensions_New(
ctx, n, exts, file->resolved_features, file->package, NULL);
// Create messages.
msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);
file->top_lvl_msg_count = n;
file->top_lvl_msgs =
_upb_MessageDefs_New(ctx, n, msgs, file->resolved_features, NULL);
// Create services.
services = UPB_DESC(FileDescriptorProto_service)(file_proto, &n);
file->service_count = n;
file->services =
_upb_ServiceDefs_New(ctx, n, services, file->resolved_features);
// Now that all names are in the table, build layouts and resolve refs.
for (int i = 0; i < file->top_lvl_msg_count; i++) {
upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
_upb_MessageDef_Resolve(ctx, m);
}
for (int i = 0; i < file->top_lvl_ext_count; i++) {
upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i);
_upb_FieldDef_Resolve(ctx, file->package, f);
}
for (int i = 0; i < file->top_lvl_msg_count; i++) {
upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
_upb_MessageDef_CreateMiniTable(ctx, (upb_MessageDef*)m);
}
for (int i = 0; i < file->top_lvl_ext_count; i++) {
upb_FieldDef* f = (upb_FieldDef*)upb_FileDef_TopLevelExtension(file, i);
_upb_FieldDef_BuildMiniTableExtension(ctx, f);
}
for (int i = 0; i < file->top_lvl_msg_count; i++) {
upb_MessageDef* m = (upb_MessageDef*)upb_FileDef_TopLevelMessage(file, i);
_upb_MessageDef_LinkMiniTable(ctx, m);
}
if (file->ext_count) {
upb_ExtensionRegistryStatus status = upb_ExtensionRegistry_AddArray(
_upb_DefPool_ExtReg(ctx->symtab), file->ext_layouts, file->ext_count);
if (status != kUpb_ExtensionRegistryStatus_Ok) {
if (status == kUpb_ExtensionRegistryStatus_OutOfMemory) {
_upb_DefBuilder_OomErr(ctx);
}
UPB_ASSERT(status == kUpb_ExtensionRegistryStatus_DuplicateEntry);
_upb_DefBuilder_Errf(ctx, "duplicate extension entry");
}
}
}
#include <string.h>
// Must be last.
/* The upb core does not generally have a concept of default instances. However
* for descriptor options we make an exception since the max size is known and
* modest (<200 bytes). All types can share a default instance since it is
* initialized to zeroes.
*
* We have to allocate an extra pointer for upb's internal metadata. */
static UPB_ALIGN_AS(8) const
char opt_default_buf[_UPB_MAXOPT_SIZE + sizeof(void*)] = {0};
const char* kUpbDefOptDefault = &opt_default_buf[sizeof(void*)];
const char* _upb_DefBuilder_FullToShort(const char* fullname) {
const char* p;
if (fullname == NULL) {
return NULL;
} else if ((p = strrchr(fullname, '.')) == NULL) {
/* No '.' in the name, return the full string. */
return fullname;
} else {
/* Return one past the last '.'. */
return p + 1;
}
}
void _upb_DefBuilder_FailJmp(upb_DefBuilder* ctx) { UPB_LONGJMP(ctx->err, 1); }
void _upb_DefBuilder_Errf(upb_DefBuilder* ctx, const char* fmt, ...) {
va_list argp;
va_start(argp, fmt);
upb_Status_VSetErrorFormat(ctx->status, fmt, argp);
va_end(argp);
_upb_DefBuilder_FailJmp(ctx);
}
void _upb_DefBuilder_OomErr(upb_DefBuilder* ctx) {
upb_Status_SetErrorMessage(ctx->status, "out of memory");
_upb_DefBuilder_FailJmp(ctx);
}
// Verify a relative identifier string. The loop is branchless for speed.
static void _upb_DefBuilder_CheckIdentNotFull(upb_DefBuilder* ctx,
upb_StringView name) {
bool good = name.size > 0;
for (size_t i = 0; i < name.size; i++) {
const char c = name.data[i];
const char d = c | 0x20; // force lowercase
const bool is_alpha = (('a' <= d) & (d <= 'z')) | (c == '_');
const bool is_numer = ('0' <= c) & (c <= '9') & (i != 0);
good &= is_alpha | is_numer;
}
if (!good) _upb_DefBuilder_CheckIdentSlow(ctx, name, false);
}
const char* _upb_DefBuilder_MakeFullName(upb_DefBuilder* ctx,
const char* prefix,
upb_StringView name) {
_upb_DefBuilder_CheckIdentNotFull(ctx, name);
if (prefix) {
// ret = prefix + '.' + name;
size_t n = strlen(prefix);
char* ret = _upb_DefBuilder_Alloc(ctx, n + name.size + 2);
strcpy(ret, prefix);
ret[n] = '.';
memcpy(&ret[n + 1], name.data, name.size);
ret[n + 1 + name.size] = '\0';
return ret;
} else {
char* ret = upb_strdup2(name.data, name.size, ctx->arena);
if (!ret) _upb_DefBuilder_OomErr(ctx);
return ret;
}
}
static bool remove_component(char* base, size_t* len) {
if (*len == 0) return false;
for (size_t i = *len - 1; i > 0; i--) {
if (base[i] == '.') {
*len = i;
return true;
}
}
*len = 0;
return true;
}
const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx,
const char* from_name_dbg,
const char* base, upb_StringView sym,
upb_deftype_t* type) {
if (sym.size == 0) goto notfound;
upb_value v;
if (sym.data[0] == '.') {
// Symbols starting with '.' are absolute, so we do a single lookup.
// Slice to omit the leading '.'
if (!_upb_DefPool_LookupSym(ctx->symtab, sym.data + 1, sym.size - 1, &v)) {
goto notfound;
}
} else {
// Remove components from base until we find an entry or run out.
size_t baselen = base ? strlen(base) : 0;
char* tmp = upb_gmalloc(sym.size + baselen + 1);
while (1) {
char* p = tmp;
if (baselen) {
memcpy(p, base, baselen);
p[baselen] = '.';
p += baselen + 1;
}
memcpy(p, sym.data, sym.size);
p += sym.size;
if (_upb_DefPool_LookupSym(ctx->symtab, tmp, p - tmp, &v)) {
break;
}
if (!remove_component(tmp, &baselen)) {
upb_gfree(tmp);
goto notfound;
}
}
upb_gfree(tmp);
}
*type = _upb_DefType_Type(v);
return _upb_DefType_Unpack(v, *type);
notfound:
_upb_DefBuilder_Errf(ctx, "couldn't resolve name '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(sym));
}
const void* _upb_DefBuilder_Resolve(upb_DefBuilder* ctx,
const char* from_name_dbg, const char* base,
upb_StringView sym, upb_deftype_t type) {
upb_deftype_t found_type;
const void* ret =
_upb_DefBuilder_ResolveAny(ctx, from_name_dbg, base, sym, &found_type);
if (ret && found_type != type) {
_upb_DefBuilder_Errf(ctx,
"type mismatch when resolving %s: couldn't find "
"name " UPB_STRINGVIEW_FORMAT " with type=%d",
from_name_dbg, UPB_STRINGVIEW_ARGS(sym), (int)type);
}
return ret;
}
// Per ASCII this will lower-case a letter. If the result is a letter, the
// input was definitely a letter. If the output is not a letter, this may
// have transformed the character unpredictably.
static char upb_ascii_lower(char ch) { return ch | 0x20; }
// isalpha() etc. from <ctype.h> are locale-dependent, which we don't want.
static bool upb_isbetween(uint8_t c, uint8_t low, uint8_t high) {
return low <= c && c <= high;
}
static bool upb_isletter(char c) {
char lower = upb_ascii_lower(c);
return upb_isbetween(lower, 'a', 'z') || c == '_';
}
static bool upb_isalphanum(char c) {
return upb_isletter(c) || upb_isbetween(c, '0', '9');
}
static bool TryGetChar(const char** src, const char* end, char* ch) {
if (*src == end) return false;
*ch = **src;
*src += 1;
return true;
}
static int TryGetHexDigit(const char** src, const char* end) {
char ch;
if (!TryGetChar(src, end, &ch)) return -1;
if ('0' <= ch && ch <= '9') {
return ch - '0';
}
ch = upb_ascii_lower(ch);
if ('a' <= ch && ch <= 'f') {
return ch - 'a' + 0xa;
}
*src -= 1; // Char wasn't actually a hex digit.
return -1;
}
static char upb_DefBuilder_ParseHexEscape(upb_DefBuilder* ctx,
const upb_FieldDef* f,
const char** src, const char* end) {
int hex_digit = TryGetHexDigit(src, end);
if (hex_digit < 0) {
_upb_DefBuilder_Errf(
ctx, "\\x must be followed by at least one hex digit (field='%s')",
upb_FieldDef_FullName(f));
return 0;
}
unsigned int ret = hex_digit;
while ((hex_digit = TryGetHexDigit(src, end)) >= 0) {
ret = (ret << 4) | hex_digit;
}
if (ret > 0xff) {
_upb_DefBuilder_Errf(ctx, "Value of hex escape in field %s exceeds 8 bits",
upb_FieldDef_FullName(f));
return 0;
}
return ret;
}
static char TryGetOctalDigit(const char** src, const char* end) {
char ch;
if (!TryGetChar(src, end, &ch)) return -1;
if ('0' <= ch && ch <= '7') {
return ch - '0';
}
*src -= 1; // Char wasn't actually an octal digit.
return -1;
}
static char upb_DefBuilder_ParseOctalEscape(upb_DefBuilder* ctx,
const upb_FieldDef* f,
const char** src, const char* end) {
char ch = 0;
for (int i = 0; i < 3; i++) {
char digit;
if ((digit = TryGetOctalDigit(src, end)) >= 0) {
ch = (ch << 3) | digit;
}
}
return ch;
}
char _upb_DefBuilder_ParseEscape(upb_DefBuilder* ctx, const upb_FieldDef* f,
const char** src, const char* end) {
char ch;
if (!TryGetChar(src, end, &ch)) {
_upb_DefBuilder_Errf(ctx, "unterminated escape sequence in field %s",
upb_FieldDef_FullName(f));
return 0;
}
switch (ch) {
case 'a':
return '\a';
case 'b':
return '\b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case 'v':
return '\v';
case '\\':
return '\\';
case '\'':
return '\'';
case '\"':
return '\"';
case '?':
return '\?';
case 'x':
case 'X':
return upb_DefBuilder_ParseHexEscape(ctx, f, src, end);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
*src -= 1;
return upb_DefBuilder_ParseOctalEscape(ctx, f, src, end);
}
_upb_DefBuilder_Errf(ctx, "Unknown escape sequence: \\%c", ch);
}
void _upb_DefBuilder_CheckIdentSlow(upb_DefBuilder* ctx, upb_StringView name,
bool full) {
const char* str = name.data;
const size_t len = name.size;
bool start = true;
for (size_t i = 0; i < len; i++) {
const char c = str[i];
if (c == '.') {
if (start || !full) {
_upb_DefBuilder_Errf(
ctx, "invalid name: unexpected '.' (" UPB_STRINGVIEW_FORMAT ")",
UPB_STRINGVIEW_ARGS(name));
}
start = true;
} else if (start) {
if (!upb_isletter(c)) {
_upb_DefBuilder_Errf(ctx,
"invalid name: path components must start with a "
"letter (" UPB_STRINGVIEW_FORMAT ")",
UPB_STRINGVIEW_ARGS(name));
}
start = false;
} else if (!upb_isalphanum(c)) {
_upb_DefBuilder_Errf(
ctx,
"invalid name: non-alphanumeric character (" UPB_STRINGVIEW_FORMAT
")",
UPB_STRINGVIEW_ARGS(name));
}
}
if (start) {
_upb_DefBuilder_Errf(ctx,
"invalid name: empty part (" UPB_STRINGVIEW_FORMAT ")",
UPB_STRINGVIEW_ARGS(name));
}
// We should never reach this point.
UPB_ASSERT(false);
}
upb_StringView _upb_DefBuilder_MakeKey(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
upb_StringView key) {
size_t need = key.size + sizeof(void*);
if (ctx->tmp_buf_size < need) {
ctx->tmp_buf_size = UPB_MAX(64, upb_RoundUpToPowerOfTwo(need));
ctx->tmp_buf = upb_Arena_Malloc(ctx->tmp_arena, ctx->tmp_buf_size);
if (!ctx->tmp_buf) _upb_DefBuilder_OomErr(ctx);
}
memcpy(ctx->tmp_buf, &parent, sizeof(void*));
memcpy(ctx->tmp_buf + sizeof(void*), key.data, key.size);
return upb_StringView_FromDataAndSize(ctx->tmp_buf, need);
}
bool _upb_DefBuilder_GetOrCreateFeatureSet(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
upb_StringView key,
UPB_DESC(FeatureSet**) set) {
upb_StringView k = _upb_DefBuilder_MakeKey(ctx, parent, key);
upb_value v;
if (upb_strtable_lookup2(&ctx->feature_cache, k.data, k.size, &v)) {
*set = upb_value_getptr(v);
return false;
}
*set = (UPB_DESC(FeatureSet*))upb_Message_DeepClone(
UPB_UPCAST(parent), UPB_DESC_MINITABLE(FeatureSet), ctx->arena);
if (!*set) _upb_DefBuilder_OomErr(ctx);
v = upb_value_ptr(*set);
if (!upb_strtable_insert(&ctx->feature_cache, k.data, k.size, v,
ctx->tmp_arena)) {
_upb_DefBuilder_OomErr(ctx);
}
return true;
}
const UPB_DESC(FeatureSet*)
_upb_DefBuilder_DoResolveFeatures(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
const UPB_DESC(FeatureSet*) child,
bool is_implicit) {
assert(parent);
if (!child) return parent;
if (child && !is_implicit &&
upb_FileDef_Syntax(ctx->file) != kUpb_Syntax_Editions) {
_upb_DefBuilder_Errf(ctx, "Features can only be specified for editions");
}
UPB_DESC(FeatureSet*) resolved;
size_t child_size;
const char* child_bytes =
UPB_DESC(FeatureSet_serialize)(child, ctx->tmp_arena, &child_size);
if (!child_bytes) _upb_DefBuilder_OomErr(ctx);
upb_StringView key = upb_StringView_FromDataAndSize(child_bytes, child_size);
if (!_upb_DefBuilder_GetOrCreateFeatureSet(ctx, parent, key, &resolved)) {
return resolved;
}
upb_DecodeStatus dec_status =
upb_Decode(child_bytes, child_size, UPB_UPCAST(resolved),
UPB_DESC_MINITABLE(FeatureSet), NULL, 0, ctx->arena);
if (dec_status != kUpb_DecodeStatus_Ok) _upb_DefBuilder_OomErr(ctx);
return resolved;
}
#include <string.h>
// Must be last.
char* upb_strdup2(const char* s, size_t len, upb_Arena* a) {
size_t n;
char* p;
// Prevent overflow errors.
if (len == SIZE_MAX) return NULL;
// Always null-terminate, even if binary data; but don't rely on the input to
// have a null-terminating byte since it may be a raw binary buffer.
n = len + 1;
p = upb_Arena_Malloc(a, n);
if (p) {
if (len != 0) memcpy(p, s, len);
p[len] = 0;
}
return p;
}
#include <stdint.h>
#include <string.h>
// Must be last.
bool upb_Message_HasFieldByDef(const upb_Message* msg, const upb_FieldDef* f) {
const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f);
UPB_ASSERT(upb_FieldDef_HasPresence(f));
if (upb_MiniTableField_IsExtension(m_f)) {
return upb_Message_HasExtension(msg, (const upb_MiniTableExtension*)m_f);
} else {
return upb_Message_HasBaseField(msg, m_f);
}
}
const upb_FieldDef* upb_Message_WhichOneofByDef(const upb_Message* msg,
const upb_OneofDef* o) {
const upb_FieldDef* f = upb_OneofDef_Field(o, 0);
if (upb_OneofDef_IsSynthetic(o)) {
UPB_ASSERT(upb_OneofDef_FieldCount(o) == 1);
return upb_Message_HasFieldByDef(msg, f) ? f : NULL;
} else {
const upb_MiniTableField* field = upb_FieldDef_MiniTable(f);
uint32_t oneof_case = upb_Message_WhichOneofFieldNumber(msg, field);
f = oneof_case ? upb_OneofDef_LookupNumber(o, oneof_case) : NULL;
UPB_ASSERT((f != NULL) == (oneof_case != 0));
return f;
}
}
upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg,
const upb_FieldDef* f) {
upb_MessageValue default_val = upb_FieldDef_Default(f);
return upb_Message_GetField(msg, upb_FieldDef_MiniTable(f), default_val);
}
upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
const upb_FieldDef* f,
upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSERT(upb_FieldDef_IsSubMessage(f) || upb_FieldDef_IsRepeated(f));
if (upb_FieldDef_HasPresence(f) && !upb_Message_HasFieldByDef(msg, f)) {
// We need to skip the upb_Message_GetFieldByDef() call in this case.
goto make;
}
upb_MessageValue val = upb_Message_GetFieldByDef(msg, f);
if (val.array_val) {
return (upb_MutableMessageValue){.array = (upb_Array*)val.array_val};
}
upb_MutableMessageValue ret;
make:
if (!a) return (upb_MutableMessageValue){.array = NULL};
if (upb_FieldDef_IsMap(f)) {
const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
const upb_FieldDef* key =
upb_MessageDef_FindFieldByNumber(entry, kUpb_MapEntry_KeyFieldNumber);
const upb_FieldDef* value =
upb_MessageDef_FindFieldByNumber(entry, kUpb_MapEntry_ValueFieldNumber);
ret.map =
upb_Map_New(a, upb_FieldDef_CType(key), upb_FieldDef_CType(value));
} else if (upb_FieldDef_IsRepeated(f)) {
ret.array = upb_Array_New(a, upb_FieldDef_CType(f));
} else {
UPB_ASSERT(upb_FieldDef_IsSubMessage(f));
const upb_MessageDef* m = upb_FieldDef_MessageSubDef(f);
ret.msg = upb_Message_New(upb_MessageDef_MiniTable(m), a);
}
val.array_val = ret.array;
upb_Message_SetFieldByDef(msg, f, val, a);
return ret;
}
bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f,
upb_MessageValue val, upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f);
if (upb_MiniTableField_IsExtension(m_f)) {
return upb_Message_SetExtension(msg, (const upb_MiniTableExtension*)m_f,
&val, a);
} else {
upb_Message_SetBaseField(msg, m_f, &val);
return true;
}
}
void upb_Message_ClearFieldByDef(upb_Message* msg, const upb_FieldDef* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
const upb_MiniTableField* m_f = upb_FieldDef_MiniTable(f);
if (upb_MiniTableField_IsExtension(m_f)) {
upb_Message_ClearExtension(msg, (const upb_MiniTableExtension*)m_f);
} else {
upb_Message_ClearBaseField(msg, m_f);
}
}
void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Clear(msg, upb_MessageDef_MiniTable(m));
}
bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, const upb_FieldDef** out_f,
upb_MessageValue* out_val, size_t* iter) {
const upb_MiniTable* mt = upb_MessageDef_MiniTable(m);
size_t i = *iter;
size_t n = upb_MiniTable_FieldCount(mt);
upb_MessageValue zero = upb_MessageValue_Zero();
UPB_UNUSED(ext_pool);
// Iterate over normal fields, returning the first one that is set.
while (++i < n) {
const upb_MiniTableField* field = upb_MiniTable_GetFieldByIndex(mt, i);
upb_MessageValue val = upb_Message_GetField(msg, field, zero);
// Skip field if unset or empty.
if (upb_MiniTableField_HasPresence(field)) {
if (!upb_Message_HasBaseField(msg, field)) continue;
} else {
switch (UPB_PRIVATE(_upb_MiniTableField_Mode)(field)) {
case kUpb_FieldMode_Map:
if (!val.map_val || upb_Map_Size(val.map_val) == 0) continue;
break;
case kUpb_FieldMode_Array:
if (!val.array_val || upb_Array_Size(val.array_val) == 0) continue;
break;
case kUpb_FieldMode_Scalar:
if (UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(field, &val))
continue;
break;
}
}
*out_val = val;
*out_f =
upb_MessageDef_FindFieldByNumber(m, upb_MiniTableField_Number(field));
*iter = i;
return true;
}
if (ext_pool) {
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) return false;
for (; (i - n) < in->size; i++) {
upb_TaggedAuxPtr tagged_ptr = in->aux_data[i - n];
if (upb_TaggedAuxPtr_IsExtension(tagged_ptr)) {
const upb_Extension* ext = upb_TaggedAuxPtr_Extension(tagged_ptr);
memcpy(out_val, &ext->data, sizeof(*out_val));
*out_f = upb_DefPool_FindExtensionByMiniTable(ext_pool, ext->ext);
*iter = i;
return true;
}
}
}
*iter = i;
return false;
}
bool _upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, int depth) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
size_t iter = kUpb_Message_Begin;
const upb_FieldDef* f;
upb_MessageValue val;
bool ret = true;
if (--depth == 0) return false;
_upb_Message_DiscardUnknown_shallow(msg);
while (upb_Message_Next(msg, m, ext_pool, &f, &val, &iter)) {
const upb_MessageDef* subm = upb_FieldDef_MessageSubDef(f);
if (!subm) continue;
if (upb_FieldDef_IsMap(f)) {
const upb_FieldDef* val_f = upb_MessageDef_FindFieldByNumber(subm, 2);
const upb_MessageDef* val_m = upb_FieldDef_MessageSubDef(val_f);
upb_Map* map = (upb_Map*)val.map_val;
size_t iter = kUpb_Map_Begin;
if (!val_m) continue;
upb_MessageValue map_key, map_val;
while (upb_Map_Next(map, &map_key, &map_val, &iter)) {
if (!_upb_Message_DiscardUnknown((upb_Message*)map_val.msg_val, val_m,
ext_pool, depth)) {
ret = false;
}
}
} else if (upb_FieldDef_IsRepeated(f)) {
const upb_Array* arr = val.array_val;
size_t i, n = upb_Array_Size(arr);
for (i = 0; i < n; i++) {
upb_MessageValue elem = upb_Array_Get(arr, i);
if (!_upb_Message_DiscardUnknown((upb_Message*)elem.msg_val, subm,
ext_pool, depth)) {
ret = false;
}
}
} else {
if (!_upb_Message_DiscardUnknown((upb_Message*)val.msg_val, subm,
ext_pool, depth)) {
ret = false;
}
}
}
return ret;
}
bool upb_Message_DiscardUnknown(upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, int maxdepth) {
return _upb_Message_DiscardUnknown(msg, m, ext_pool, maxdepth);
}
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
struct upb_MessageDef {
UPB_ALIGN_AS(8) const UPB_DESC(MessageOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_MiniTable* layout;
const upb_FileDef* file;
const upb_MessageDef* containing_type;
const char* full_name;
// Tables for looking up fields by number and name.
upb_inttable itof;
upb_strtable ntof;
// Looking up fields by json name.
upb_strtable jtof;
/* All nested defs.
* MEM: We could save some space here by putting nested defs in a contiguous
* region and calculating counts from offsets or vice-versa. */
const upb_FieldDef* fields;
const upb_OneofDef* oneofs;
const upb_ExtensionRange* ext_ranges;
const upb_StringView* res_names;
const upb_MessageDef* nested_msgs;
const upb_MessageReservedRange* res_ranges;
const upb_EnumDef* nested_enums;
const upb_FieldDef* nested_exts;
// TODO: These counters don't need anywhere near 32 bits.
int field_count;
int real_oneof_count;
int oneof_count;
int ext_range_count;
int res_range_count;
int res_name_count;
int nested_msg_count;
int nested_enum_count;
int nested_ext_count;
bool in_message_set;
bool is_sorted;
upb_WellKnown well_known_type;
};
static void assign_msg_wellknowntype(upb_MessageDef* m) {
const char* name = m->full_name;
if (name == NULL) {
m->well_known_type = kUpb_WellKnown_Unspecified;
return;
}
if (!strcmp(name, "google.protobuf.Any")) {
m->well_known_type = kUpb_WellKnown_Any;
} else if (!strcmp(name, "google.protobuf.FieldMask")) {
m->well_known_type = kUpb_WellKnown_FieldMask;
} else if (!strcmp(name, "google.protobuf.Duration")) {
m->well_known_type = kUpb_WellKnown_Duration;
} else if (!strcmp(name, "google.protobuf.Timestamp")) {
m->well_known_type = kUpb_WellKnown_Timestamp;
} else if (!strcmp(name, "google.protobuf.DoubleValue")) {
m->well_known_type = kUpb_WellKnown_DoubleValue;
} else if (!strcmp(name, "google.protobuf.FloatValue")) {
m->well_known_type = kUpb_WellKnown_FloatValue;
} else if (!strcmp(name, "google.protobuf.Int64Value")) {
m->well_known_type = kUpb_WellKnown_Int64Value;
} else if (!strcmp(name, "google.protobuf.UInt64Value")) {
m->well_known_type = kUpb_WellKnown_UInt64Value;
} else if (!strcmp(name, "google.protobuf.Int32Value")) {
m->well_known_type = kUpb_WellKnown_Int32Value;
} else if (!strcmp(name, "google.protobuf.UInt32Value")) {
m->well_known_type = kUpb_WellKnown_UInt32Value;
} else if (!strcmp(name, "google.protobuf.BoolValue")) {
m->well_known_type = kUpb_WellKnown_BoolValue;
} else if (!strcmp(name, "google.protobuf.StringValue")) {
m->well_known_type = kUpb_WellKnown_StringValue;
} else if (!strcmp(name, "google.protobuf.BytesValue")) {
m->well_known_type = kUpb_WellKnown_BytesValue;
} else if (!strcmp(name, "google.protobuf.Value")) {
m->well_known_type = kUpb_WellKnown_Value;
} else if (!strcmp(name, "google.protobuf.ListValue")) {
m->well_known_type = kUpb_WellKnown_ListValue;
} else if (!strcmp(name, "google.protobuf.Struct")) {
m->well_known_type = kUpb_WellKnown_Struct;
} else {
m->well_known_type = kUpb_WellKnown_Unspecified;
}
}
upb_MessageDef* _upb_MessageDef_At(const upb_MessageDef* m, int i) {
return (upb_MessageDef*)&m[i];
}
bool _upb_MessageDef_IsValidExtensionNumber(const upb_MessageDef* m, int n) {
for (int i = 0; i < m->ext_range_count; i++) {
const upb_ExtensionRange* r = upb_MessageDef_ExtensionRange(m, i);
if (upb_ExtensionRange_Start(r) <= n && n < upb_ExtensionRange_End(r)) {
return true;
}
}
return false;
}
const UPB_DESC(MessageOptions) *
upb_MessageDef_Options(const upb_MessageDef* m) {
return m->opts;
}
bool upb_MessageDef_HasOptions(const upb_MessageDef* m) {
return m->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_MessageDef_ResolvedFeatures(const upb_MessageDef* m) {
return m->resolved_features;
}
const char* upb_MessageDef_FullName(const upb_MessageDef* m) {
return m->full_name;
}
const upb_FileDef* upb_MessageDef_File(const upb_MessageDef* m) {
return m->file;
}
const upb_MessageDef* upb_MessageDef_ContainingType(const upb_MessageDef* m) {
return m->containing_type;
}
const char* upb_MessageDef_Name(const upb_MessageDef* m) {
return _upb_DefBuilder_FullToShort(m->full_name);
}
upb_Syntax upb_MessageDef_Syntax(const upb_MessageDef* m) {
return upb_FileDef_Syntax(m->file);
}
const upb_FieldDef* upb_MessageDef_FindFieldByNumber(const upb_MessageDef* m,
uint32_t i) {
upb_value val;
return upb_inttable_lookup(&m->itof, i, &val) ? upb_value_getconstptr(val)
: NULL;
}
const upb_FieldDef* upb_MessageDef_FindFieldByNameWithSize(
const upb_MessageDef* m, const char* name, size_t size) {
upb_value val;
if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) {
return NULL;
}
return _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD);
}
const upb_OneofDef* upb_MessageDef_FindOneofByNameWithSize(
const upb_MessageDef* m, const char* name, size_t size) {
upb_value val;
if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) {
return NULL;
}
return _upb_DefType_Unpack(val, UPB_DEFTYPE_ONEOF);
}
bool _upb_MessageDef_Insert(upb_MessageDef* m, const char* name, size_t len,
upb_value v, upb_Arena* a) {
return upb_strtable_insert(&m->ntof, name, len, v, a);
}
bool upb_MessageDef_FindByNameWithSize(const upb_MessageDef* m,
const char* name, size_t len,
const upb_FieldDef** out_f,
const upb_OneofDef** out_o) {
upb_value val;
if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
return false;
}
const upb_FieldDef* f = _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD);
const upb_OneofDef* o = _upb_DefType_Unpack(val, UPB_DEFTYPE_ONEOF);
if (out_f) *out_f = f;
if (out_o) *out_o = o;
return f || o; /* False if this was a JSON name. */
}
const upb_FieldDef* upb_MessageDef_FindByJsonNameWithSize(
const upb_MessageDef* m, const char* name, size_t size) {
upb_value val;
if (upb_strtable_lookup2(&m->jtof, name, size, &val)) {
return upb_value_getconstptr(val);
}
if (!upb_strtable_lookup2(&m->ntof, name, size, &val)) {
return NULL;
}
return _upb_DefType_Unpack(val, UPB_DEFTYPE_FIELD);
}
int upb_MessageDef_ExtensionRangeCount(const upb_MessageDef* m) {
return m->ext_range_count;
}
int upb_MessageDef_ReservedRangeCount(const upb_MessageDef* m) {
return m->res_range_count;
}
int upb_MessageDef_ReservedNameCount(const upb_MessageDef* m) {
return m->res_name_count;
}
int upb_MessageDef_FieldCount(const upb_MessageDef* m) {
return m->field_count;
}
int upb_MessageDef_OneofCount(const upb_MessageDef* m) {
return m->oneof_count;
}
int upb_MessageDef_RealOneofCount(const upb_MessageDef* m) {
return m->real_oneof_count;
}
int upb_MessageDef_NestedMessageCount(const upb_MessageDef* m) {
return m->nested_msg_count;
}
int upb_MessageDef_NestedEnumCount(const upb_MessageDef* m) {
return m->nested_enum_count;
}
int upb_MessageDef_NestedExtensionCount(const upb_MessageDef* m) {
return m->nested_ext_count;
}
const upb_MiniTable* upb_MessageDef_MiniTable(const upb_MessageDef* m) {
return m->layout;
}
const upb_ExtensionRange* upb_MessageDef_ExtensionRange(const upb_MessageDef* m,
int i) {
UPB_ASSERT(0 <= i && i < m->ext_range_count);
return _upb_ExtensionRange_At(m->ext_ranges, i);
}
const upb_MessageReservedRange* upb_MessageDef_ReservedRange(
const upb_MessageDef* m, int i) {
UPB_ASSERT(0 <= i && i < m->res_range_count);
return _upb_MessageReservedRange_At(m->res_ranges, i);
}
upb_StringView upb_MessageDef_ReservedName(const upb_MessageDef* m, int i) {
UPB_ASSERT(0 <= i && i < m->res_name_count);
return m->res_names[i];
}
const upb_FieldDef* upb_MessageDef_Field(const upb_MessageDef* m, int i) {
UPB_ASSERT(0 <= i && i < m->field_count);
return _upb_FieldDef_At(m->fields, i);
}
const upb_OneofDef* upb_MessageDef_Oneof(const upb_MessageDef* m, int i) {
UPB_ASSERT(0 <= i && i < m->oneof_count);
return _upb_OneofDef_At(m->oneofs, i);
}
const upb_MessageDef* upb_MessageDef_NestedMessage(const upb_MessageDef* m,
int i) {
UPB_ASSERT(0 <= i && i < m->nested_msg_count);
return &m->nested_msgs[i];
}
const upb_EnumDef* upb_MessageDef_NestedEnum(const upb_MessageDef* m, int i) {
UPB_ASSERT(0 <= i && i < m->nested_enum_count);
return _upb_EnumDef_At(m->nested_enums, i);
}
const upb_FieldDef* upb_MessageDef_NestedExtension(const upb_MessageDef* m,
int i) {
UPB_ASSERT(0 <= i && i < m->nested_ext_count);
return _upb_FieldDef_At(m->nested_exts, i);
}
upb_WellKnown upb_MessageDef_WellKnownType(const upb_MessageDef* m) {
return m->well_known_type;
}
bool _upb_MessageDef_InMessageSet(const upb_MessageDef* m) {
return m->in_message_set;
}
const upb_FieldDef* upb_MessageDef_FindFieldByName(const upb_MessageDef* m,
const char* name) {
return upb_MessageDef_FindFieldByNameWithSize(m, name, strlen(name));
}
const upb_OneofDef* upb_MessageDef_FindOneofByName(const upb_MessageDef* m,
const char* name) {
return upb_MessageDef_FindOneofByNameWithSize(m, name, strlen(name));
}
bool upb_MessageDef_IsMapEntry(const upb_MessageDef* m) {
return UPB_DESC(MessageOptions_map_entry)(m->opts);
}
bool upb_MessageDef_IsMessageSet(const upb_MessageDef* m) {
return UPB_DESC(MessageOptions_message_set_wire_format)(m->opts);
}
static upb_MiniTable* _upb_MessageDef_MakeMiniTable(upb_DefBuilder* ctx,
const upb_MessageDef* m) {
upb_StringView desc;
// Note: this will assign layout_index for fields, so upb_FieldDef_MiniTable()
// is safe to call only after this call.
bool ok = upb_MessageDef_MiniDescriptorEncode(m, ctx->tmp_arena, &desc);
if (!ok) _upb_DefBuilder_OomErr(ctx);
void** scratch_data = _upb_DefPool_ScratchData(ctx->symtab);
size_t* scratch_size = _upb_DefPool_ScratchSize(ctx->symtab);
upb_MiniTable* ret = upb_MiniTable_BuildWithBuf(
desc.data, desc.size, ctx->platform, ctx->arena, scratch_data,
scratch_size, ctx->status);
if (!ret) _upb_DefBuilder_FailJmp(ctx);
return ret;
}
void _upb_MessageDef_Resolve(upb_DefBuilder* ctx, upb_MessageDef* m) {
for (int i = 0; i < m->field_count; i++) {
upb_FieldDef* f = (upb_FieldDef*)upb_MessageDef_Field(m, i);
_upb_FieldDef_Resolve(ctx, m->full_name, f);
}
m->in_message_set = false;
for (int i = 0; i < upb_MessageDef_NestedExtensionCount(m); i++) {
upb_FieldDef* ext = (upb_FieldDef*)upb_MessageDef_NestedExtension(m, i);
_upb_FieldDef_Resolve(ctx, m->full_name, ext);
if (upb_FieldDef_Type(ext) == kUpb_FieldType_Message &&
upb_FieldDef_Label(ext) == kUpb_Label_Optional &&
upb_FieldDef_MessageSubDef(ext) == m &&
UPB_DESC(MessageOptions_message_set_wire_format)(
upb_MessageDef_Options(upb_FieldDef_ContainingType(ext)))) {
m->in_message_set = true;
}
}
for (int i = 0; i < upb_MessageDef_NestedMessageCount(m); i++) {
upb_MessageDef* n = (upb_MessageDef*)upb_MessageDef_NestedMessage(m, i);
_upb_MessageDef_Resolve(ctx, n);
}
}
void _upb_MessageDef_InsertField(upb_DefBuilder* ctx, upb_MessageDef* m,
const upb_FieldDef* f) {
const int32_t field_number = upb_FieldDef_Number(f);
if (field_number <= 0 || field_number > kUpb_MaxFieldNumber) {
_upb_DefBuilder_Errf(ctx, "invalid field number (%u)", field_number);
}
const char* json_name = upb_FieldDef_JsonName(f);
const char* shortname = upb_FieldDef_Name(f);
const size_t shortnamelen = strlen(shortname);
upb_value v = upb_value_constptr(f);
upb_value existing_v;
if (upb_strtable_lookup(&m->ntof, shortname, &existing_v)) {
_upb_DefBuilder_Errf(ctx, "duplicate field name (%s)", shortname);
}
const upb_value field_v = _upb_DefType_Pack(f, UPB_DEFTYPE_FIELD);
bool ok =
_upb_MessageDef_Insert(m, shortname, shortnamelen, field_v, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
bool skip_json_conflicts =
UPB_DESC(MessageOptions_deprecated_legacy_json_field_conflicts)(
upb_MessageDef_Options(m));
if (!skip_json_conflicts && strcmp(shortname, json_name) != 0 &&
UPB_DESC(FeatureSet_json_format)(m->resolved_features) ==
UPB_DESC(FeatureSet_ALLOW) &&
upb_strtable_lookup(&m->ntof, json_name, &v)) {
_upb_DefBuilder_Errf(
ctx, "duplicate json_name for (%s) with original field name (%s)",
shortname, json_name);
}
if (upb_strtable_lookup(&m->jtof, json_name, &v)) {
if (!skip_json_conflicts) {
_upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name);
}
} else {
const size_t json_size = strlen(json_name);
ok = upb_strtable_insert(&m->jtof, json_name, json_size,
upb_value_constptr(f), ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
}
if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
_upb_DefBuilder_Errf(ctx, "duplicate field number (%u)", field_number);
}
ok = upb_inttable_insert(&m->itof, field_number, v, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
}
void _upb_MessageDef_CreateMiniTable(upb_DefBuilder* ctx, upb_MessageDef* m) {
if (ctx->layout == NULL) {
m->layout = _upb_MessageDef_MakeMiniTable(ctx, m);
} else {
m->layout = upb_MiniTableFile_Message(ctx->layout, ctx->msg_count++);
UPB_ASSERT(m->field_count == upb_MiniTable_FieldCount(m->layout));
// We don't need the result of this call, but it will assign layout_index
// for all the fields in O(n lg n) time.
_upb_FieldDefs_Sorted(m->fields, m->field_count, ctx->tmp_arena);
}
for (int i = 0; i < m->nested_msg_count; i++) {
upb_MessageDef* nested =
(upb_MessageDef*)upb_MessageDef_NestedMessage(m, i);
_upb_MessageDef_CreateMiniTable(ctx, nested);
}
}
void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
const upb_MessageDef* m) {
for (int i = 0; i < upb_MessageDef_NestedExtensionCount(m); i++) {
const upb_FieldDef* ext = upb_MessageDef_NestedExtension(m, i);
_upb_FieldDef_BuildMiniTableExtension(ctx, ext);
}
for (int i = 0; i < m->nested_msg_count; i++) {
_upb_MessageDef_LinkMiniTable(ctx, upb_MessageDef_NestedMessage(m, i));
}
if (ctx->layout) return;
for (int i = 0; i < m->field_count; i++) {
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
const upb_MessageDef* sub_m = upb_FieldDef_MessageSubDef(f);
const upb_EnumDef* sub_e = upb_FieldDef_EnumSubDef(f);
const int layout_index = _upb_FieldDef_LayoutIndex(f);
upb_MiniTable* mt = (upb_MiniTable*)upb_MessageDef_MiniTable(m);
UPB_ASSERT(layout_index < m->field_count);
upb_MiniTableField* mt_f =
(upb_MiniTableField*)&m->layout->UPB_PRIVATE(fields)[layout_index];
if (sub_m) {
if (!mt->UPB_PRIVATE(subs)) {
_upb_DefBuilder_Errf(ctx, "unexpected submsg for (%s)", m->full_name);
}
UPB_ASSERT(mt_f);
UPB_ASSERT(sub_m->layout);
if (UPB_UNLIKELY(!upb_MiniTable_SetSubMessage(mt, mt_f, sub_m->layout))) {
_upb_DefBuilder_Errf(ctx, "invalid submsg for (%s)", m->full_name);
}
} else if (_upb_FieldDef_IsClosedEnum(f)) {
const upb_MiniTableEnum* mt_e = _upb_EnumDef_MiniTable(sub_e);
if (UPB_UNLIKELY(!upb_MiniTable_SetSubEnum(mt, mt_f, mt_e))) {
_upb_DefBuilder_Errf(ctx, "invalid subenum for (%s)", m->full_name);
}
}
}
#ifndef NDEBUG
for (int i = 0; i < m->field_count; i++) {
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
const int layout_index = _upb_FieldDef_LayoutIndex(f);
UPB_ASSERT(layout_index < upb_MiniTable_FieldCount(m->layout));
const upb_MiniTableField* mt_f =
&m->layout->UPB_PRIVATE(fields)[layout_index];
UPB_ASSERT(upb_FieldDef_Type(f) == upb_MiniTableField_Type(mt_f));
UPB_ASSERT(upb_FieldDef_CType(f) == upb_MiniTableField_CType(mt_f));
UPB_ASSERT(upb_FieldDef_HasPresence(f) ==
upb_MiniTableField_HasPresence(mt_f));
}
#endif
}
static bool _upb_MessageDef_ValidateUtf8(const upb_MessageDef* m) {
bool has_string = false;
for (int i = 0; i < m->field_count; i++) {
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
// Old binaries do not recognize the field-level "FlipValidateUtf8" wire
// modifier, so we do not actually have field-level control for old
// binaries. Given this, we judge that the better failure mode is to be
// more lax than intended, rather than more strict. To achieve this, we
// only mark the message with the ValidateUtf8 modifier if *all* fields
// validate UTF-8.
if (!_upb_FieldDef_ValidateUtf8(f)) return false;
if (upb_FieldDef_Type(f) == kUpb_FieldType_String) has_string = true;
}
return has_string;
}
static uint64_t _upb_MessageDef_Modifiers(const upb_MessageDef* m) {
uint64_t out = 0;
if (UPB_DESC(FeatureSet_repeated_field_encoding(m->resolved_features)) ==
UPB_DESC(FeatureSet_PACKED)) {
out |= kUpb_MessageModifier_DefaultIsPacked;
}
if (_upb_MessageDef_ValidateUtf8(m)) {
out |= kUpb_MessageModifier_ValidateUtf8;
}
if (m->ext_range_count) {
out |= kUpb_MessageModifier_IsExtendable;
}
return out;
}
static bool _upb_MessageDef_EncodeMap(upb_DescState* s, const upb_MessageDef* m,
upb_Arena* a) {
if (m->field_count != 2) return false;
const upb_FieldDef* key_field = upb_MessageDef_Field(m, 0);
const upb_FieldDef* val_field = upb_MessageDef_Field(m, 1);
if (key_field == NULL || val_field == NULL) return false;
UPB_ASSERT(_upb_FieldDef_LayoutIndex(key_field) == 0);
UPB_ASSERT(_upb_FieldDef_LayoutIndex(val_field) == 1);
s->ptr = upb_MtDataEncoder_EncodeMap(
&s->e, s->ptr, upb_FieldDef_Type(key_field), upb_FieldDef_Type(val_field),
_upb_FieldDef_Modifiers(key_field), _upb_FieldDef_Modifiers(val_field));
return true;
}
static bool _upb_MessageDef_EncodeMessage(upb_DescState* s,
const upb_MessageDef* m,
upb_Arena* a) {
const upb_FieldDef** sorted = NULL;
if (!m->is_sorted) {
sorted = _upb_FieldDefs_Sorted(m->fields, m->field_count, a);
if (!sorted) return false;
}
s->ptr = upb_MtDataEncoder_StartMessage(&s->e, s->ptr,
_upb_MessageDef_Modifiers(m));
for (int i = 0; i < m->field_count; i++) {
const upb_FieldDef* f = sorted ? sorted[i] : upb_MessageDef_Field(m, i);
const upb_FieldType type = upb_FieldDef_Type(f);
const int number = upb_FieldDef_Number(f);
const uint64_t modifiers = _upb_FieldDef_Modifiers(f);
if (!_upb_DescState_Grow(s, a)) return false;
s->ptr = upb_MtDataEncoder_PutField(&s->e, s->ptr, type, number, modifiers);
}
for (int i = 0; i < m->real_oneof_count; i++) {
if (!_upb_DescState_Grow(s, a)) return false;
s->ptr = upb_MtDataEncoder_StartOneof(&s->e, s->ptr);
const upb_OneofDef* o = upb_MessageDef_Oneof(m, i);
const int field_count = upb_OneofDef_FieldCount(o);
for (int j = 0; j < field_count; j++) {
const int number = upb_FieldDef_Number(upb_OneofDef_Field(o, j));
if (!_upb_DescState_Grow(s, a)) return false;
s->ptr = upb_MtDataEncoder_PutOneofField(&s->e, s->ptr, number);
}
}
return true;
}
static bool _upb_MessageDef_EncodeMessageSet(upb_DescState* s,
const upb_MessageDef* m,
upb_Arena* a) {
s->ptr = upb_MtDataEncoder_EncodeMessageSet(&s->e, s->ptr);
return true;
}
bool upb_MessageDef_MiniDescriptorEncode(const upb_MessageDef* m, upb_Arena* a,
upb_StringView* out) {
upb_DescState s;
_upb_DescState_Init(&s);
if (!_upb_DescState_Grow(&s, a)) return false;
if (upb_MessageDef_IsMapEntry(m)) {
if (!_upb_MessageDef_EncodeMap(&s, m, a)) return false;
} else if (UPB_DESC(MessageOptions_message_set_wire_format)(m->opts)) {
if (!_upb_MessageDef_EncodeMessageSet(&s, m, a)) return false;
} else {
if (!_upb_MessageDef_EncodeMessage(&s, m, a)) return false;
}
if (!_upb_DescState_Grow(&s, a)) return false;
*s.ptr = '\0';
out->data = s.buf;
out->size = s.ptr - s.buf;
return true;
}
static upb_StringView* _upb_ReservedNames_New(upb_DefBuilder* ctx, int n,
const upb_StringView* protos) {
upb_StringView* sv = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_StringView, n);
for (int i = 0; i < n; i++) {
sv[i].data =
upb_strdup2(protos[i].data, protos[i].size, _upb_DefBuilder_Arena(ctx));
sv[i].size = protos[i].size;
}
return sv;
}
static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(DescriptorProto*) msg_proto,
const UPB_DESC(FeatureSet*) parent_features,
const upb_MessageDef* containing_type,
upb_MessageDef* m) {
const UPB_DESC(OneofDescriptorProto)* const* oneofs;
const UPB_DESC(FieldDescriptorProto)* const* fields;
const UPB_DESC(DescriptorProto_ExtensionRange)* const* ext_ranges;
const UPB_DESC(DescriptorProto_ReservedRange)* const* res_ranges;
const upb_StringView* res_names;
size_t n_oneof, n_field, n_enum, n_ext, n_msg;
size_t n_ext_range, n_res_range, n_res_name;
upb_StringView name;
UPB_DEF_SET_OPTIONS(m->opts, DescriptorProto, MessageOptions, msg_proto);
m->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(MessageOptions_features)(m->opts));
// Must happen before _upb_DefBuilder_Add()
m->file = _upb_DefBuilder_File(ctx);
m->containing_type = containing_type;
m->is_sorted = true;
name = UPB_DESC(DescriptorProto_name)(msg_proto);
m->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
_upb_DefBuilder_Add(ctx, m->full_name, _upb_DefType_Pack(m, UPB_DEFTYPE_MSG));
oneofs = UPB_DESC(DescriptorProto_oneof_decl)(msg_proto, &n_oneof);
fields = UPB_DESC(DescriptorProto_field)(msg_proto, &n_field);
ext_ranges =
UPB_DESC(DescriptorProto_extension_range)(msg_proto, &n_ext_range);
res_ranges =
UPB_DESC(DescriptorProto_reserved_range)(msg_proto, &n_res_range);
res_names = UPB_DESC(DescriptorProto_reserved_name)(msg_proto, &n_res_name);
bool ok = upb_inttable_init(&m->itof, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
ok = upb_strtable_init(&m->ntof, n_oneof + n_field, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
ok = upb_strtable_init(&m->jtof, n_field, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
m->oneof_count = n_oneof;
m->oneofs = _upb_OneofDefs_New(ctx, n_oneof, oneofs, m->resolved_features, m);
m->field_count = n_field;
m->fields = _upb_FieldDefs_New(ctx, n_field, fields, m->resolved_features,
m->full_name, m, &m->is_sorted);
// Message Sets may not contain fields.
if (UPB_UNLIKELY(UPB_DESC(MessageOptions_message_set_wire_format)(m->opts))) {
if (UPB_UNLIKELY(n_field > 0)) {
_upb_DefBuilder_Errf(ctx, "invalid message set (%s)", m->full_name);
}
}
m->ext_range_count = n_ext_range;
m->ext_ranges = _upb_ExtensionRanges_New(ctx, n_ext_range, ext_ranges,
m->resolved_features, m);
m->res_range_count = n_res_range;
m->res_ranges =
_upb_MessageReservedRanges_New(ctx, n_res_range, res_ranges, m);
m->res_name_count = n_res_name;
m->res_names = _upb_ReservedNames_New(ctx, n_res_name, res_names);
const size_t synthetic_count = _upb_OneofDefs_Finalize(ctx, m);
m->real_oneof_count = m->oneof_count - synthetic_count;
assign_msg_wellknowntype(m);
if (!upb_inttable_compact(&m->itof, ctx->arena)) _upb_DefBuilder_OomErr(ctx);
const UPB_DESC(EnumDescriptorProto)* const* enums =
UPB_DESC(DescriptorProto_enum_type)(msg_proto, &n_enum);
m->nested_enum_count = n_enum;
m->nested_enums =
_upb_EnumDefs_New(ctx, n_enum, enums, m->resolved_features, m);
const UPB_DESC(FieldDescriptorProto)* const* exts =
UPB_DESC(DescriptorProto_extension)(msg_proto, &n_ext);
m->nested_ext_count = n_ext;
m->nested_exts = _upb_Extensions_New(ctx, n_ext, exts, m->resolved_features,
m->full_name, m);
const UPB_DESC(DescriptorProto)* const* msgs =
UPB_DESC(DescriptorProto_nested_type)(msg_proto, &n_msg);
m->nested_msg_count = n_msg;
m->nested_msgs =
_upb_MessageDefs_New(ctx, n_msg, msgs, m->resolved_features, m);
}
// Allocate and initialize an array of |n| message defs.
upb_MessageDef* _upb_MessageDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*)
parent_features,
const upb_MessageDef* containing_type) {
_upb_DefType_CheckPadding(sizeof(upb_MessageDef));
const char* name = containing_type ? containing_type->full_name
: _upb_FileDef_RawPackage(ctx->file);
upb_MessageDef* m = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_MessageDef, n);
for (int i = 0; i < n; i++) {
create_msgdef(ctx, name, protos[i], parent_features, containing_type,
&m[i]);
}
return m;
}
// Must be last.
struct upb_MessageReservedRange {
int32_t start;
int32_t end;
};
upb_MessageReservedRange* _upb_MessageReservedRange_At(
const upb_MessageReservedRange* r, int i) {
return (upb_MessageReservedRange*)&r[i];
}
int32_t upb_MessageReservedRange_Start(const upb_MessageReservedRange* r) {
return r->start;
}
int32_t upb_MessageReservedRange_End(const upb_MessageReservedRange* r) {
return r->end;
}
upb_MessageReservedRange* _upb_MessageReservedRanges_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto_ReservedRange) * const* protos,
const upb_MessageDef* m) {
upb_MessageReservedRange* r =
UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_MessageReservedRange, n);
for (int i = 0; i < n; i++) {
const int32_t start =
UPB_DESC(DescriptorProto_ReservedRange_start)(protos[i]);
const int32_t end = UPB_DESC(DescriptorProto_ReservedRange_end)(protos[i]);
const int32_t max = kUpb_MaxFieldNumber + 1;
// A full validation would also check that each range is disjoint, and that
// none of the fields overlap with the extension ranges, but we are just
// sanity checking here.
if (start < 1 || end <= start || end > max) {
_upb_DefBuilder_Errf(ctx,
"Reserved range (%d, %d) is invalid, message=%s\n",
(int)start, (int)end, upb_MessageDef_FullName(m));
}
r[i].start = start;
r[i].end = end;
}
return r;
}
// Must be last.
struct upb_MethodDef {
const UPB_DESC(MethodOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
upb_ServiceDef* service;
const char* full_name;
const upb_MessageDef* input_type;
const upb_MessageDef* output_type;
int index;
bool client_streaming;
bool server_streaming;
};
upb_MethodDef* _upb_MethodDef_At(const upb_MethodDef* m, int i) {
return (upb_MethodDef*)&m[i];
}
const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m) {
return m->service;
}
const UPB_DESC(MethodOptions) * upb_MethodDef_Options(const upb_MethodDef* m) {
return m->opts;
}
bool upb_MethodDef_HasOptions(const upb_MethodDef* m) {
return m->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_MethodDef_ResolvedFeatures(const upb_MethodDef* m) {
return m->resolved_features;
}
const char* upb_MethodDef_FullName(const upb_MethodDef* m) {
return m->full_name;
}
const char* upb_MethodDef_Name(const upb_MethodDef* m) {
return _upb_DefBuilder_FullToShort(m->full_name);
}
int upb_MethodDef_Index(const upb_MethodDef* m) { return m->index; }
const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m) {
return m->input_type;
}
const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m) {
return m->output_type;
}
bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m) {
return m->client_streaming;
}
bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m) {
return m->server_streaming;
}
static void create_method(upb_DefBuilder* ctx,
const UPB_DESC(MethodDescriptorProto*) method_proto,
const UPB_DESC(FeatureSet*) parent_features,
upb_ServiceDef* s, upb_MethodDef* m) {
UPB_DEF_SET_OPTIONS(m->opts, MethodDescriptorProto, MethodOptions,
method_proto);
m->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(MethodOptions_features)(m->opts));
upb_StringView name = UPB_DESC(MethodDescriptorProto_name)(method_proto);
m->service = s;
m->full_name =
_upb_DefBuilder_MakeFullName(ctx, upb_ServiceDef_FullName(s), name);
m->client_streaming =
UPB_DESC(MethodDescriptorProto_client_streaming)(method_proto);
m->server_streaming =
UPB_DESC(MethodDescriptorProto_server_streaming)(method_proto);
m->input_type = _upb_DefBuilder_Resolve(
ctx, m->full_name, m->full_name,
UPB_DESC(MethodDescriptorProto_input_type)(method_proto),
UPB_DEFTYPE_MSG);
m->output_type = _upb_DefBuilder_Resolve(
ctx, m->full_name, m->full_name,
UPB_DESC(MethodDescriptorProto_output_type)(method_proto),
UPB_DEFTYPE_MSG);
}
// Allocate and initialize an array of |n| method defs belonging to |s|.
upb_MethodDef* _upb_MethodDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(MethodDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
upb_ServiceDef* s) {
upb_MethodDef* m = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_MethodDef, n);
for (int i = 0; i < n; i++) {
create_method(ctx, protos[i], parent_features, s, &m[i]);
m[i].index = i;
}
return m;
}
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
// Must be last.
struct upb_OneofDef {
UPB_ALIGN_AS(8) const UPB_DESC(OneofOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_MessageDef* parent;
const char* full_name;
int field_count;
bool synthetic;
const upb_FieldDef** fields;
upb_strtable ntof; // lookup a field by name
upb_inttable itof; // lookup a field by number (index)
};
upb_OneofDef* _upb_OneofDef_At(const upb_OneofDef* o, int i) {
return (upb_OneofDef*)&o[i];
}
const UPB_DESC(OneofOptions) * upb_OneofDef_Options(const upb_OneofDef* o) {
return o->opts;
}
bool upb_OneofDef_HasOptions(const upb_OneofDef* o) {
return o->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_OneofDef_ResolvedFeatures(const upb_OneofDef* o) {
return o->resolved_features;
}
const char* upb_OneofDef_FullName(const upb_OneofDef* o) {
return o->full_name;
}
const char* upb_OneofDef_Name(const upb_OneofDef* o) {
return _upb_DefBuilder_FullToShort(o->full_name);
}
const upb_MessageDef* upb_OneofDef_ContainingType(const upb_OneofDef* o) {
return o->parent;
}
int upb_OneofDef_FieldCount(const upb_OneofDef* o) { return o->field_count; }
const upb_FieldDef* upb_OneofDef_Field(const upb_OneofDef* o, int i) {
UPB_ASSERT(i < o->field_count);
return o->fields[i];
}
int upb_OneofDef_numfields(const upb_OneofDef* o) { return o->field_count; }
uint32_t upb_OneofDef_Index(const upb_OneofDef* o) {
// Compute index in our parent's array.
return o - upb_MessageDef_Oneof(o->parent, 0);
}
bool upb_OneofDef_IsSynthetic(const upb_OneofDef* o) { return o->synthetic; }
const upb_FieldDef* upb_OneofDef_LookupNameWithSize(const upb_OneofDef* o,
const char* name,
size_t size) {
upb_value val;
return upb_strtable_lookup2(&o->ntof, name, size, &val)
? upb_value_getptr(val)
: NULL;
}
const upb_FieldDef* upb_OneofDef_LookupName(const upb_OneofDef* o,
const char* name) {
return upb_OneofDef_LookupNameWithSize(o, name, strlen(name));
}
const upb_FieldDef* upb_OneofDef_LookupNumber(const upb_OneofDef* o,
uint32_t num) {
upb_value val;
return upb_inttable_lookup(&o->itof, num, &val) ? upb_value_getptr(val)
: NULL;
}
void _upb_OneofDef_Insert(upb_DefBuilder* ctx, upb_OneofDef* o,
const upb_FieldDef* f, const char* name,
size_t size) {
o->field_count++;
if (_upb_FieldDef_IsProto3Optional(f)) o->synthetic = true;
const int number = upb_FieldDef_Number(f);
const upb_value v = upb_value_constptr(f);
// TODO: This lookup is unfortunate because we also perform it when
// inserting into the message's table. Unfortunately that step occurs after
// this one and moving things around could be tricky so let's leave it for
// a future refactoring.
const bool number_exists = upb_inttable_lookup(&o->itof, number, NULL);
if (UPB_UNLIKELY(number_exists)) {
_upb_DefBuilder_Errf(ctx, "oneof fields have the same number (%d)", number);
}
// TODO: More redundant work happening here.
const bool name_exists = upb_strtable_lookup2(&o->ntof, name, size, NULL);
if (UPB_UNLIKELY(name_exists)) {
_upb_DefBuilder_Errf(ctx, "oneof fields have the same name (%.*s)",
(int)size, name);
}
const bool ok = upb_inttable_insert(&o->itof, number, v, ctx->arena) &&
upb_strtable_insert(&o->ntof, name, size, v, ctx->arena);
if (UPB_UNLIKELY(!ok)) {
_upb_DefBuilder_OomErr(ctx);
}
}
// Returns the synthetic count.
size_t _upb_OneofDefs_Finalize(upb_DefBuilder* ctx, upb_MessageDef* m) {
int synthetic_count = 0;
for (int i = 0; i < upb_MessageDef_OneofCount(m); i++) {
upb_OneofDef* o = (upb_OneofDef*)upb_MessageDef_Oneof(m, i);
if (o->synthetic && o->field_count != 1) {
_upb_DefBuilder_Errf(ctx,
"Synthetic oneofs must have one field, not %d: %s",
o->field_count, upb_OneofDef_Name(o));
}
if (o->synthetic) {
synthetic_count++;
} else if (synthetic_count != 0) {
_upb_DefBuilder_Errf(
ctx, "Synthetic oneofs must be after all other oneofs: %s",
upb_OneofDef_Name(o));
}
o->fields =
UPB_DEFBUILDER_ALLOCARRAY(ctx, const upb_FieldDef*, o->field_count);
o->field_count = 0;
}
for (int i = 0; i < upb_MessageDef_FieldCount(m); i++) {
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
upb_OneofDef* o = (upb_OneofDef*)upb_FieldDef_ContainingOneof(f);
if (o) {
o->fields[o->field_count++] = f;
}
}
return synthetic_count;
}
static void create_oneofdef(upb_DefBuilder* ctx, upb_MessageDef* m,
const UPB_DESC(OneofDescriptorProto*) oneof_proto,
const UPB_DESC(FeatureSet*) parent_features,
const upb_OneofDef* _o) {
upb_OneofDef* o = (upb_OneofDef*)_o;
UPB_DEF_SET_OPTIONS(o->opts, OneofDescriptorProto, OneofOptions, oneof_proto);
o->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(OneofOptions_features)(o->opts));
upb_StringView name = UPB_DESC(OneofDescriptorProto_name)(oneof_proto);
o->parent = m;
o->full_name =
_upb_DefBuilder_MakeFullName(ctx, upb_MessageDef_FullName(m), name);
o->field_count = 0;
o->synthetic = false;
if (upb_MessageDef_FindByNameWithSize(m, name.data, name.size, NULL, NULL)) {
_upb_DefBuilder_Errf(ctx, "duplicate oneof name (%s)", o->full_name);
}
upb_value v = _upb_DefType_Pack(o, UPB_DEFTYPE_ONEOF);
bool ok = _upb_MessageDef_Insert(m, name.data, name.size, v, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
ok = upb_inttable_init(&o->itof, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
ok = upb_strtable_init(&o->ntof, 4, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
}
// Allocate and initialize an array of |n| oneof defs.
upb_OneofDef* _upb_OneofDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(OneofDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
upb_MessageDef* m) {
_upb_DefType_CheckPadding(sizeof(upb_OneofDef));
upb_OneofDef* o = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_OneofDef, n);
for (int i = 0; i < n; i++) {
create_oneofdef(ctx, m, protos[i], parent_features, &o[i]);
}
return o;
}
// Must be last.
struct upb_ServiceDef {
UPB_ALIGN_AS(8) const UPB_DESC(ServiceOptions*) opts;
const UPB_DESC(FeatureSet*) resolved_features;
const upb_FileDef* file;
const char* full_name;
upb_MethodDef* methods;
int method_count;
int index;
};
upb_ServiceDef* _upb_ServiceDef_At(const upb_ServiceDef* s, int index) {
return (upb_ServiceDef*)&s[index];
}
const UPB_DESC(ServiceOptions) *
upb_ServiceDef_Options(const upb_ServiceDef* s) {
return s->opts;
}
bool upb_ServiceDef_HasOptions(const upb_ServiceDef* s) {
return s->opts != (void*)kUpbDefOptDefault;
}
const UPB_DESC(FeatureSet) *
upb_ServiceDef_ResolvedFeatures(const upb_ServiceDef* s) {
return s->resolved_features;
}
const char* upb_ServiceDef_FullName(const upb_ServiceDef* s) {
return s->full_name;
}
const char* upb_ServiceDef_Name(const upb_ServiceDef* s) {
return _upb_DefBuilder_FullToShort(s->full_name);
}
int upb_ServiceDef_Index(const upb_ServiceDef* s) { return s->index; }
const upb_FileDef* upb_ServiceDef_File(const upb_ServiceDef* s) {
return s->file;
}
int upb_ServiceDef_MethodCount(const upb_ServiceDef* s) {
return s->method_count;
}
const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s, int i) {
return (i < 0 || i >= s->method_count) ? NULL
: _upb_MethodDef_At(s->methods, i);
}
const upb_MethodDef* upb_ServiceDef_FindMethodByName(const upb_ServiceDef* s,
const char* name) {
for (int i = 0; i < s->method_count; i++) {
const upb_MethodDef* m = _upb_MethodDef_At(s->methods, i);
if (strcmp(name, upb_MethodDef_Name(m)) == 0) {
return m;
}
}
return NULL;
}
static void create_service(upb_DefBuilder* ctx,
const UPB_DESC(ServiceDescriptorProto*) svc_proto,
const UPB_DESC(FeatureSet*) parent_features,
upb_ServiceDef* s) {
UPB_DEF_SET_OPTIONS(s->opts, ServiceDescriptorProto, ServiceOptions,
svc_proto);
s->resolved_features = _upb_DefBuilder_ResolveFeatures(
ctx, parent_features, UPB_DESC(ServiceOptions_features)(s->opts));
// Must happen before _upb_DefBuilder_Add()
s->file = _upb_DefBuilder_File(ctx);
upb_StringView name = UPB_DESC(ServiceDescriptorProto_name)(svc_proto);
const char* package = _upb_FileDef_RawPackage(s->file);
s->full_name = _upb_DefBuilder_MakeFullName(ctx, package, name);
_upb_DefBuilder_Add(ctx, s->full_name,
_upb_DefType_Pack(s, UPB_DEFTYPE_SERVICE));
size_t n;
const UPB_DESC(MethodDescriptorProto)* const* methods =
UPB_DESC(ServiceDescriptorProto_method)(svc_proto, &n);
s->method_count = n;
s->methods = _upb_MethodDefs_New(ctx, n, methods, s->resolved_features, s);
}
upb_ServiceDef* _upb_ServiceDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(ServiceDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*)
parent_features) {
_upb_DefType_CheckPadding(sizeof(upb_ServiceDef));
upb_ServiceDef* s = UPB_DEFBUILDER_ALLOCARRAY(ctx, upb_ServiceDef, n);
for (int i = 0; i < n; i++) {
create_service(ctx, protos[i], parent_features, &s[i]);
s[i].index = i;
}
return s;
}
#include <inttypes.h>
#include <math.h>
// Must be last.
typedef struct {
upb_Arena* arena;
jmp_buf err;
} upb_ToProto_Context;
#define CHK_OOM(val) \
if (!(val)) UPB_LONGJMP(ctx->err, 1);
// We want to copy the options verbatim into the destination options proto.
// We use serialize+parse as our deep copy.
#define SET_OPTIONS(proto, desc_type, options_type, src) \
{ \
size_t size; \
/* MEM: could use a temporary arena here instead. */ \
char* pb = google_protobuf_##options_type##_serialize(src, ctx->arena, &size); \
CHK_OOM(pb); \
google_protobuf_##options_type* dst = \
google_protobuf_##options_type##_parse(pb, size, ctx->arena); \
CHK_OOM(dst); \
google_protobuf_##desc_type##_set_options(proto, dst); \
}
static upb_StringView strviewdup2(upb_ToProto_Context* ctx,
upb_StringView str) {
char* p = upb_Arena_Malloc(ctx->arena, str.size);
CHK_OOM(p);
memcpy(p, str.data, str.size);
return (upb_StringView){.data = p, .size = str.size};
}
static upb_StringView strviewdup(upb_ToProto_Context* ctx, const char* s) {
return strviewdup2(ctx, (upb_StringView){.data = s, .size = strlen(s)});
}
static upb_StringView qual_dup(upb_ToProto_Context* ctx, const char* s) {
size_t n = strlen(s);
char* p = upb_Arena_Malloc(ctx->arena, n + 1);
CHK_OOM(p);
p[0] = '.';
memcpy(p + 1, s, n);
return (upb_StringView){.data = p, .size = n + 1};
}
UPB_PRINTF(2, 3)
static upb_StringView printf_dup(upb_ToProto_Context* ctx, const char* fmt,
...) {
const size_t max = 32;
char* p = upb_Arena_Malloc(ctx->arena, max);
CHK_OOM(p);
va_list args;
va_start(args, fmt);
size_t n = _upb_vsnprintf(p, max, fmt, args);
va_end(args);
UPB_ASSERT(n < max);
return (upb_StringView){.data = p, .size = n};
}
static bool upb_isprint(char ch) { return ch >= 0x20 && ch <= 0x7f; }
static int special_escape(char ch) {
switch (ch) {
// This is the same set of special escapes recognized by
// absl::CEscape().
case '\n':
return 'n';
case '\r':
return 'r';
case '\t':
return 't';
case '\\':
return '\\';
case '\'':
return '\'';
case '"':
return '"';
default:
return -1;
}
}
static upb_StringView default_bytes(upb_ToProto_Context* ctx,
upb_StringView val) {
size_t n = 0;
for (size_t i = 0; i < val.size; i++) {
char ch = val.data[i];
if (special_escape(ch) >= 0)
n += 2; // '\C'
else if (upb_isprint(ch))
n += 1;
else
n += 4; // '\123'
}
char* p = upb_Arena_Malloc(ctx->arena, n);
CHK_OOM(p);
char* dst = p;
const char* src = val.data;
const char* end = src + val.size;
while (src < end) {
unsigned char ch = *src++;
if (special_escape(ch) >= 0) {
*dst++ = '\\';
*dst++ = (char)special_escape(ch);
} else if (upb_isprint(ch)) {
*dst++ = ch;
} else {
*dst++ = '\\';
*dst++ = '0' + (ch >> 6);
*dst++ = '0' + ((ch >> 3) & 0x7);
*dst++ = '0' + (ch & 0x7);
}
}
return (upb_StringView){.data = p, .size = n};
}
static upb_StringView default_string(upb_ToProto_Context* ctx,
const upb_FieldDef* f) {
upb_MessageValue d = upb_FieldDef_Default(f);
upb_CType type = upb_FieldDef_CType(f);
if (type == kUpb_CType_Float || type == kUpb_CType_Double) {
double val = type == kUpb_CType_Float ? d.float_val : d.double_val;
if (val == INFINITY) {
return strviewdup(ctx, "inf");
} else if (val == -INFINITY) {
return strviewdup(ctx, "-inf");
} else if (val != val) {
return strviewdup(ctx, "nan");
}
}
switch (upb_FieldDef_CType(f)) {
case kUpb_CType_Bool:
return strviewdup(ctx, d.bool_val ? "true" : "false");
case kUpb_CType_Enum: {
const upb_EnumDef* e = upb_FieldDef_EnumSubDef(f);
const upb_EnumValueDef* ev =
upb_EnumDef_FindValueByNumber(e, d.int32_val);
return strviewdup(ctx, upb_EnumValueDef_Name(ev));
}
case kUpb_CType_Int64:
return printf_dup(ctx, "%" PRId64, d.int64_val);
case kUpb_CType_UInt64:
return printf_dup(ctx, "%" PRIu64, d.uint64_val);
case kUpb_CType_Int32:
return printf_dup(ctx, "%" PRId32, d.int32_val);
case kUpb_CType_UInt32:
return printf_dup(ctx, "%" PRIu32, d.uint32_val);
case kUpb_CType_Float:
return printf_dup(ctx, "%.9g", d.float_val);
case kUpb_CType_Double:
return printf_dup(ctx, "%.17g", d.double_val);
case kUpb_CType_String:
return strviewdup2(ctx, d.str_val);
case kUpb_CType_Bytes:
return default_bytes(ctx, d.str_val);
default:
UPB_UNREACHABLE();
}
}
static google_protobuf_DescriptorProto_ReservedRange* resrange_toproto(
upb_ToProto_Context* ctx, const upb_MessageReservedRange* r) {
google_protobuf_DescriptorProto_ReservedRange* proto =
google_protobuf_DescriptorProto_ReservedRange_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_DescriptorProto_ReservedRange_set_start(
proto, upb_MessageReservedRange_Start(r));
google_protobuf_DescriptorProto_ReservedRange_set_end(proto,
upb_MessageReservedRange_End(r));
return proto;
}
static google_protobuf_EnumDescriptorProto_EnumReservedRange* enumresrange_toproto(
upb_ToProto_Context* ctx, const upb_EnumReservedRange* r) {
google_protobuf_EnumDescriptorProto_EnumReservedRange* proto =
google_protobuf_EnumDescriptorProto_EnumReservedRange_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(
proto, upb_EnumReservedRange_Start(r));
google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(
proto, upb_EnumReservedRange_End(r));
return proto;
}
static google_protobuf_FieldDescriptorProto* fielddef_toproto(upb_ToProto_Context* ctx,
const upb_FieldDef* f) {
google_protobuf_FieldDescriptorProto* proto =
google_protobuf_FieldDescriptorProto_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_FieldDescriptorProto_set_name(proto,
strviewdup(ctx, upb_FieldDef_Name(f)));
google_protobuf_FieldDescriptorProto_set_number(proto, upb_FieldDef_Number(f));
if (upb_FieldDef_IsRequired(f) &&
upb_FileDef_Edition(upb_FieldDef_File(f)) >= UPB_DESC(EDITION_2023)) {
google_protobuf_FieldDescriptorProto_set_label(
proto, UPB_DESC(FieldDescriptorProto_LABEL_OPTIONAL));
} else {
google_protobuf_FieldDescriptorProto_set_label(proto, upb_FieldDef_Label(f));
}
if (upb_FieldDef_Type(f) == kUpb_FieldType_Group &&
upb_FileDef_Edition(upb_FieldDef_File(f)) >= UPB_DESC(EDITION_2023)) {
google_protobuf_FieldDescriptorProto_set_type(proto, kUpb_FieldType_Message);
} else {
google_protobuf_FieldDescriptorProto_set_type(proto, upb_FieldDef_Type(f));
}
if (upb_FieldDef_HasJsonName(f)) {
google_protobuf_FieldDescriptorProto_set_json_name(
proto, strviewdup(ctx, upb_FieldDef_JsonName(f)));
}
if (upb_FieldDef_IsSubMessage(f)) {
google_protobuf_FieldDescriptorProto_set_type_name(
proto,
qual_dup(ctx, upb_MessageDef_FullName(upb_FieldDef_MessageSubDef(f))));
} else if (upb_FieldDef_CType(f) == kUpb_CType_Enum) {
google_protobuf_FieldDescriptorProto_set_type_name(
proto, qual_dup(ctx, upb_EnumDef_FullName(upb_FieldDef_EnumSubDef(f))));
}
if (upb_FieldDef_IsExtension(f)) {
google_protobuf_FieldDescriptorProto_set_extendee(
proto,
qual_dup(ctx, upb_MessageDef_FullName(upb_FieldDef_ContainingType(f))));
}
if (upb_FieldDef_HasDefault(f)) {
google_protobuf_FieldDescriptorProto_set_default_value(proto,
default_string(ctx, f));
}
const upb_OneofDef* o = upb_FieldDef_ContainingOneof(f);
if (o) {
google_protobuf_FieldDescriptorProto_set_oneof_index(proto, upb_OneofDef_Index(o));
}
if (_upb_FieldDef_IsProto3Optional(f)) {
google_protobuf_FieldDescriptorProto_set_proto3_optional(proto, true);
}
if (upb_FieldDef_HasOptions(f)) {
SET_OPTIONS(proto, FieldDescriptorProto, FieldOptions,
upb_FieldDef_Options(f));
}
return proto;
}
static google_protobuf_OneofDescriptorProto* oneofdef_toproto(upb_ToProto_Context* ctx,
const upb_OneofDef* o) {
google_protobuf_OneofDescriptorProto* proto =
google_protobuf_OneofDescriptorProto_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_OneofDescriptorProto_set_name(proto,
strviewdup(ctx, upb_OneofDef_Name(o)));
if (upb_OneofDef_HasOptions(o)) {
SET_OPTIONS(proto, OneofDescriptorProto, OneofOptions,
upb_OneofDef_Options(o));
}
return proto;
}
static google_protobuf_EnumValueDescriptorProto* enumvaldef_toproto(
upb_ToProto_Context* ctx, const upb_EnumValueDef* e) {
google_protobuf_EnumValueDescriptorProto* proto =
google_protobuf_EnumValueDescriptorProto_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_EnumValueDescriptorProto_set_name(
proto, strviewdup(ctx, upb_EnumValueDef_Name(e)));
google_protobuf_EnumValueDescriptorProto_set_number(proto, upb_EnumValueDef_Number(e));
if (upb_EnumValueDef_HasOptions(e)) {
SET_OPTIONS(proto, EnumValueDescriptorProto, EnumValueOptions,
upb_EnumValueDef_Options(e));
}
return proto;
}
static google_protobuf_EnumDescriptorProto* enumdef_toproto(upb_ToProto_Context* ctx,
const upb_EnumDef* e) {
google_protobuf_EnumDescriptorProto* proto =
google_protobuf_EnumDescriptorProto_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_EnumDescriptorProto_set_name(proto,
strviewdup(ctx, upb_EnumDef_Name(e)));
int n = upb_EnumDef_ValueCount(e);
google_protobuf_EnumValueDescriptorProto** vals =
google_protobuf_EnumDescriptorProto_resize_value(proto, n, ctx->arena);
CHK_OOM(vals);
for (int i = 0; i < n; i++) {
vals[i] = enumvaldef_toproto(ctx, upb_EnumDef_Value(e, i));
}
n = upb_EnumDef_ReservedRangeCount(e);
google_protobuf_EnumDescriptorProto_EnumReservedRange** res_ranges =
google_protobuf_EnumDescriptorProto_resize_reserved_range(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
res_ranges[i] = enumresrange_toproto(ctx, upb_EnumDef_ReservedRange(e, i));
}
n = upb_EnumDef_ReservedNameCount(e);
upb_StringView* res_names =
google_protobuf_EnumDescriptorProto_resize_reserved_name(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
res_names[i] = upb_EnumDef_ReservedName(e, i);
}
if (upb_EnumDef_HasOptions(e)) {
SET_OPTIONS(proto, EnumDescriptorProto, EnumOptions,
upb_EnumDef_Options(e));
}
return proto;
}
static google_protobuf_DescriptorProto_ExtensionRange* extrange_toproto(
upb_ToProto_Context* ctx, const upb_ExtensionRange* e) {
google_protobuf_DescriptorProto_ExtensionRange* proto =
google_protobuf_DescriptorProto_ExtensionRange_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_DescriptorProto_ExtensionRange_set_start(proto,
upb_ExtensionRange_Start(e));
google_protobuf_DescriptorProto_ExtensionRange_set_end(proto,
upb_ExtensionRange_End(e));
if (upb_ExtensionRange_HasOptions(e)) {
SET_OPTIONS(proto, DescriptorProto_ExtensionRange, ExtensionRangeOptions,
upb_ExtensionRange_Options(e));
}
return proto;
}
static google_protobuf_DescriptorProto* msgdef_toproto(upb_ToProto_Context* ctx,
const upb_MessageDef* m) {
google_protobuf_DescriptorProto* proto = google_protobuf_DescriptorProto_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_DescriptorProto_set_name(proto,
strviewdup(ctx, upb_MessageDef_Name(m)));
int n;
n = upb_MessageDef_FieldCount(m);
google_protobuf_FieldDescriptorProto** fields =
google_protobuf_DescriptorProto_resize_field(proto, n, ctx->arena);
CHK_OOM(fields);
for (int i = 0; i < n; i++) {
fields[i] = fielddef_toproto(ctx, upb_MessageDef_Field(m, i));
}
n = upb_MessageDef_OneofCount(m);
google_protobuf_OneofDescriptorProto** oneofs =
google_protobuf_DescriptorProto_resize_oneof_decl(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
oneofs[i] = oneofdef_toproto(ctx, upb_MessageDef_Oneof(m, i));
}
n = upb_MessageDef_NestedMessageCount(m);
google_protobuf_DescriptorProto** nested_msgs =
google_protobuf_DescriptorProto_resize_nested_type(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
nested_msgs[i] = msgdef_toproto(ctx, upb_MessageDef_NestedMessage(m, i));
}
n = upb_MessageDef_NestedEnumCount(m);
google_protobuf_EnumDescriptorProto** nested_enums =
google_protobuf_DescriptorProto_resize_enum_type(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
nested_enums[i] = enumdef_toproto(ctx, upb_MessageDef_NestedEnum(m, i));
}
n = upb_MessageDef_NestedExtensionCount(m);
google_protobuf_FieldDescriptorProto** nested_exts =
google_protobuf_DescriptorProto_resize_extension(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
nested_exts[i] =
fielddef_toproto(ctx, upb_MessageDef_NestedExtension(m, i));
}
n = upb_MessageDef_ExtensionRangeCount(m);
google_protobuf_DescriptorProto_ExtensionRange** ext_ranges =
google_protobuf_DescriptorProto_resize_extension_range(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
ext_ranges[i] = extrange_toproto(ctx, upb_MessageDef_ExtensionRange(m, i));
}
n = upb_MessageDef_ReservedRangeCount(m);
google_protobuf_DescriptorProto_ReservedRange** res_ranges =
google_protobuf_DescriptorProto_resize_reserved_range(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
res_ranges[i] = resrange_toproto(ctx, upb_MessageDef_ReservedRange(m, i));
}
n = upb_MessageDef_ReservedNameCount(m);
upb_StringView* res_names =
google_protobuf_DescriptorProto_resize_reserved_name(proto, n, ctx->arena);
for (int i = 0; i < n; i++) {
res_names[i] = upb_MessageDef_ReservedName(m, i);
}
if (upb_MessageDef_HasOptions(m)) {
SET_OPTIONS(proto, DescriptorProto, MessageOptions,
upb_MessageDef_Options(m));
}
return proto;
}
static google_protobuf_MethodDescriptorProto* methoddef_toproto(upb_ToProto_Context* ctx,
const upb_MethodDef* m) {
google_protobuf_MethodDescriptorProto* proto =
google_protobuf_MethodDescriptorProto_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_MethodDescriptorProto_set_name(proto,
strviewdup(ctx, upb_MethodDef_Name(m)));
google_protobuf_MethodDescriptorProto_set_input_type(
proto,
qual_dup(ctx, upb_MessageDef_FullName(upb_MethodDef_InputType(m))));
google_protobuf_MethodDescriptorProto_set_output_type(
proto,
qual_dup(ctx, upb_MessageDef_FullName(upb_MethodDef_OutputType(m))));
if (upb_MethodDef_ClientStreaming(m)) {
google_protobuf_MethodDescriptorProto_set_client_streaming(proto, true);
}
if (upb_MethodDef_ServerStreaming(m)) {
google_protobuf_MethodDescriptorProto_set_server_streaming(proto, true);
}
if (upb_MethodDef_HasOptions(m)) {
SET_OPTIONS(proto, MethodDescriptorProto, MethodOptions,
upb_MethodDef_Options(m));
}
return proto;
}
static google_protobuf_ServiceDescriptorProto* servicedef_toproto(
upb_ToProto_Context* ctx, const upb_ServiceDef* s) {
google_protobuf_ServiceDescriptorProto* proto =
google_protobuf_ServiceDescriptorProto_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_ServiceDescriptorProto_set_name(
proto, strviewdup(ctx, upb_ServiceDef_Name(s)));
size_t n = upb_ServiceDef_MethodCount(s);
google_protobuf_MethodDescriptorProto** methods =
google_protobuf_ServiceDescriptorProto_resize_method(proto, n, ctx->arena);
for (size_t i = 0; i < n; i++) {
methods[i] = methoddef_toproto(ctx, upb_ServiceDef_Method(s, i));
}
if (upb_ServiceDef_HasOptions(s)) {
SET_OPTIONS(proto, ServiceDescriptorProto, ServiceOptions,
upb_ServiceDef_Options(s));
}
return proto;
}
static google_protobuf_FileDescriptorProto* filedef_toproto(upb_ToProto_Context* ctx,
const upb_FileDef* f) {
google_protobuf_FileDescriptorProto* proto =
google_protobuf_FileDescriptorProto_new(ctx->arena);
CHK_OOM(proto);
google_protobuf_FileDescriptorProto_set_name(proto,
strviewdup(ctx, upb_FileDef_Name(f)));
const char* package = upb_FileDef_Package(f);
if (package) {
size_t n = strlen(package);
if (n) {
google_protobuf_FileDescriptorProto_set_package(proto, strviewdup(ctx, package));
}
}
if (upb_FileDef_Syntax(f) == kUpb_Syntax_Editions) {
google_protobuf_FileDescriptorProto_set_edition(proto, upb_FileDef_Edition(f));
}
if (upb_FileDef_Syntax(f) == kUpb_Syntax_Proto3) {
google_protobuf_FileDescriptorProto_set_syntax(proto, strviewdup(ctx, "proto3"));
} else if (upb_FileDef_Syntax(f) == kUpb_Syntax_Editions) {
google_protobuf_FileDescriptorProto_set_syntax(proto, strviewdup(ctx, "editions"));
}
size_t n;
n = upb_FileDef_DependencyCount(f);
upb_StringView* deps =
google_protobuf_FileDescriptorProto_resize_dependency(proto, n, ctx->arena);
for (size_t i = 0; i < n; i++) {
deps[i] = strviewdup(ctx, upb_FileDef_Name(upb_FileDef_Dependency(f, i)));
}
n = upb_FileDef_PublicDependencyCount(f);
int32_t* public_deps =
google_protobuf_FileDescriptorProto_resize_public_dependency(proto, n, ctx->arena);
const int32_t* public_dep_nums = _upb_FileDef_PublicDependencyIndexes(f);
if (n) memcpy(public_deps, public_dep_nums, n * sizeof(int32_t));
n = upb_FileDef_WeakDependencyCount(f);
int32_t* weak_deps =
google_protobuf_FileDescriptorProto_resize_weak_dependency(proto, n, ctx->arena);
const int32_t* weak_dep_nums = _upb_FileDef_WeakDependencyIndexes(f);
if (n) memcpy(weak_deps, weak_dep_nums, n * sizeof(int32_t));
n = upb_FileDef_TopLevelMessageCount(f);
google_protobuf_DescriptorProto** msgs =
google_protobuf_FileDescriptorProto_resize_message_type(proto, n, ctx->arena);
for (size_t i = 0; i < n; i++) {
msgs[i] = msgdef_toproto(ctx, upb_FileDef_TopLevelMessage(f, i));
}
n = upb_FileDef_TopLevelEnumCount(f);
google_protobuf_EnumDescriptorProto** enums =
google_protobuf_FileDescriptorProto_resize_enum_type(proto, n, ctx->arena);
for (size_t i = 0; i < n; i++) {
enums[i] = enumdef_toproto(ctx, upb_FileDef_TopLevelEnum(f, i));
}
n = upb_FileDef_ServiceCount(f);
google_protobuf_ServiceDescriptorProto** services =
google_protobuf_FileDescriptorProto_resize_service(proto, n, ctx->arena);
for (size_t i = 0; i < n; i++) {
services[i] = servicedef_toproto(ctx, upb_FileDef_Service(f, i));
}
n = upb_FileDef_TopLevelExtensionCount(f);
google_protobuf_FieldDescriptorProto** exts =
google_protobuf_FileDescriptorProto_resize_extension(proto, n, ctx->arena);
for (size_t i = 0; i < n; i++) {
exts[i] = fielddef_toproto(ctx, upb_FileDef_TopLevelExtension(f, i));
}
if (upb_FileDef_HasOptions(f)) {
SET_OPTIONS(proto, FileDescriptorProto, FileOptions,
upb_FileDef_Options(f));
}
return proto;
}
static google_protobuf_DescriptorProto* upb_ToProto_ConvertMessageDef(
upb_ToProto_Context* const ctx, const upb_MessageDef* const m) {
if (UPB_SETJMP(ctx->err)) return NULL;
return msgdef_toproto(ctx, m);
}
google_protobuf_DescriptorProto* upb_MessageDef_ToProto(const upb_MessageDef* m,
upb_Arena* a) {
upb_ToProto_Context ctx = {a};
return upb_ToProto_ConvertMessageDef(&ctx, m);
}
google_protobuf_EnumDescriptorProto* upb_ToProto_ConvertEnumDef(
upb_ToProto_Context* const ctx, const upb_EnumDef* const e) {
if (UPB_SETJMP(ctx->err)) return NULL;
return enumdef_toproto(ctx, e);
}
google_protobuf_EnumDescriptorProto* upb_EnumDef_ToProto(const upb_EnumDef* e,
upb_Arena* a) {
upb_ToProto_Context ctx = {a};
return upb_ToProto_ConvertEnumDef(&ctx, e);
}
google_protobuf_EnumValueDescriptorProto* upb_ToProto_ConvertEnumValueDef(
upb_ToProto_Context* const ctx, const upb_EnumValueDef* e) {
if (UPB_SETJMP(ctx->err)) return NULL;
return enumvaldef_toproto(ctx, e);
}
google_protobuf_EnumValueDescriptorProto* upb_EnumValueDef_ToProto(
const upb_EnumValueDef* e, upb_Arena* a) {
upb_ToProto_Context ctx = {a};
return upb_ToProto_ConvertEnumValueDef(&ctx, e);
}
google_protobuf_FieldDescriptorProto* upb_ToProto_ConvertFieldDef(
upb_ToProto_Context* const ctx, const upb_FieldDef* f) {
if (UPB_SETJMP(ctx->err)) return NULL;
return fielddef_toproto(ctx, f);
}
google_protobuf_FieldDescriptorProto* upb_FieldDef_ToProto(const upb_FieldDef* f,
upb_Arena* a) {
upb_ToProto_Context ctx = {a};
return upb_ToProto_ConvertFieldDef(&ctx, f);
}
google_protobuf_OneofDescriptorProto* upb_ToProto_ConvertOneofDef(
upb_ToProto_Context* const ctx, const upb_OneofDef* o) {
if (UPB_SETJMP(ctx->err)) return NULL;
return oneofdef_toproto(ctx, o);
}
google_protobuf_OneofDescriptorProto* upb_OneofDef_ToProto(const upb_OneofDef* o,
upb_Arena* a) {
upb_ToProto_Context ctx = {a};
return upb_ToProto_ConvertOneofDef(&ctx, o);
}
google_protobuf_FileDescriptorProto* upb_ToProto_ConvertFileDef(
upb_ToProto_Context* const ctx, const upb_FileDef* const f) {
if (UPB_SETJMP(ctx->err)) return NULL;
return filedef_toproto(ctx, f);
}
google_protobuf_FileDescriptorProto* upb_FileDef_ToProto(const upb_FileDef* f,
upb_Arena* a) {
upb_ToProto_Context ctx = {a};
return upb_ToProto_ConvertFileDef(&ctx, f);
}
google_protobuf_MethodDescriptorProto* upb_ToProto_ConvertMethodDef(
upb_ToProto_Context* const ctx, const upb_MethodDef* m) {
if (UPB_SETJMP(ctx->err)) return NULL;
return methoddef_toproto(ctx, m);
}
google_protobuf_MethodDescriptorProto* upb_MethodDef_ToProto(
const upb_MethodDef* const m, upb_Arena* a) {
upb_ToProto_Context ctx = {a};
return upb_ToProto_ConvertMethodDef(&ctx, m);
}
google_protobuf_ServiceDescriptorProto* upb_ToProto_ConvertServiceDef(
upb_ToProto_Context* const ctx, const upb_ServiceDef* const s) {
if (UPB_SETJMP(ctx->err)) return NULL;
return servicedef_toproto(ctx, s);
}
google_protobuf_ServiceDescriptorProto* upb_ServiceDef_ToProto(const upb_ServiceDef* s,
upb_Arena* a) {
upb_ToProto_Context ctx = {a};
return upb_ToProto_ConvertServiceDef(&ctx, s);
}
// This should #undef all macros #defined in def.inc
#undef UPB_SIZE
#undef UPB_PTR_AT
#undef UPB_SIZEOF_FLEX
#undef UPB_SIZEOF_FLEX_WOULD_OVERFLOW
#undef UPB_MAPTYPE_STRING
#undef UPB_EXPORT
#undef UPB_INLINE
#undef UPB_API
#undef UPBC_API
#undef UPB_API_INLINE
#undef UPB_API_INLINE_IF_NOT_GCC
#undef UPB_ALIGN_UP
#undef UPB_ALIGN_DOWN
#undef UPB_ALIGN_MALLOC
#undef UPB_ALIGN_OF
#undef UPB_ALIGN_AS
#undef UPB_MALLOC_ALIGN
#undef UPB_LIKELY
#undef UPB_UNLIKELY
#undef UPB_UNPREDICTABLE
#undef UPB_FORCEINLINE
#undef UPB_NOINLINE
#undef UPB_NORETURN
#undef UPB_PRINTF
#undef UPB_MAX
#undef UPB_MIN
#undef UPB_UNUSED
#undef UPB_ASSUME
#undef UPB_ASSERT
#undef UPB_UNREACHABLE
#undef UPB_DEFAULT_MAX_BLOCK_SIZE
#undef UPB_SETJMP
#undef UPB_LONGJMP
#undef UPB_PTRADD
#undef UPB_MUSTTAIL
#undef UPB_FASTTABLE_SUPPORTED
#undef UPB_FASTTABLE_MASK
#undef UPB_FASTTABLE
#undef UPB_FASTTABLE_INIT
#undef UPB_POISON_MEMORY_REGION
#undef UPB_UNPOISON_MEMORY_REGION
#undef UPB_ASAN
#undef UPB_ASAN_GUARD_SIZE
#undef UPB_CLANG_ASAN
#undef UPB_TSAN_PUBLISHED_MEMBER
#undef UPB_TSAN_INIT_PUBLISHED
#undef UPB_TSAN_CHECK_PUBLISHED
#undef UPB_TSAN_PUBLISH
#undef UPB_TSAN_CHECK_READ
#undef UPB_TSAN_CHECK_WRITE
#undef UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN
#undef UPB_DEPRECATED
#undef UPB_GNUC_MIN
#undef UPB_DESCRIPTOR_UPB_H_FILENAME
#undef UPB_DESC
#undef UPB_DESC_MINITABLE
#undef UPB_IS_GOOGLE3
#undef UPB_ATOMIC
#undef UPB_USE_C11_ATOMICS
#undef UPB_USE_MSC_ATOMICS
#undef UPB_PRIVATE
#undef UPB_ONLYBITS
#undef UPB_LINKARR_DECLARE
#undef UPB_LINKARR_APPEND
#undef UPB_LINKARR_START
#undef UPB_LINKARR_STOP
#undef UPB_FUTURE_BREAKING_CHANGES
|