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
|
/* ZstdDec.c -- Zstd Decoder
2024-06-18 : the code was developed by Igor Pavlov, using Zstandard format
specification and original zstd decoder code as reference code.
original zstd decoder code: Copyright (c) Facebook, Inc. All rights reserved.
This source code is licensed under BSD 3-Clause License.
*/
#include "Precomp.h"
#include <string.h>
#include <stdlib.h>
// #include <stdio.h>
#include "Alloc.h"
#include "Xxh64.h"
#include "ZstdDec.h"
#include "CpuArch.h"
#if defined(MY_CPU_ARM64)
#include <arm_neon.h>
#endif
/* original-zstd still doesn't support window larger than 2 GiB.
So we also limit our decoder for 2 GiB window: */
#if defined(MY_CPU_64BIT) && 0 == 1
#define MAX_WINDOW_SIZE_LOG 41
#else
#define MAX_WINDOW_SIZE_LOG 31
#endif
typedef
#if MAX_WINDOW_SIZE_LOG < 32
UInt32
#else
size_t
#endif
CZstdDecOffset;
// for debug: simpler and smaller code but slow:
// #define Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
// #define SHOW_STAT
#ifdef SHOW_STAT
#include <stdio.h>
static unsigned g_Num_Blocks_Compressed = 0;
static unsigned g_Num_Blocks_memcpy = 0;
static unsigned g_Num_Wrap_memmove_Num = 0;
static unsigned g_Num_Wrap_memmove_Bytes = 0;
static unsigned g_NumSeqs_total = 0;
// static unsigned g_NumCopy = 0;
static unsigned g_NumOver = 0;
static unsigned g_NumOver2 = 0;
static unsigned g_Num_Match = 0;
static unsigned g_Num_Lits = 0;
static unsigned g_Num_LitsBig = 0;
static unsigned g_Num_Lit0 = 0;
static unsigned g_Num_Rep0 = 0;
static unsigned g_Num_Rep1 = 0;
static unsigned g_Num_Rep2 = 0;
static unsigned g_Num_Rep3 = 0;
static unsigned g_Num_Threshold_0 = 0;
static unsigned g_Num_Threshold_1 = 0;
static unsigned g_Num_Threshold_0sum = 0;
static unsigned g_Num_Threshold_1sum = 0;
#define STAT_UPDATE(v) v
#else
#define STAT_UPDATE(v)
#endif
#define STAT_INC(v) STAT_UPDATE(v++;)
typedef struct
{
const Byte *ptr;
size_t len;
}
CInBufPair;
#if defined(MY_CPU_ARM_OR_ARM64) || defined(MY_CPU_X86_OR_AMD64)
#if (defined(__clang__) && (__clang_major__ >= 6)) \
|| (defined(__GNUC__) && (__GNUC__ >= 6))
// disable for debug:
#define Z7_ZSTD_DEC_USE_BSR
#elif defined(_MSC_VER) && (_MSC_VER >= 1300)
// #if defined(MY_CPU_ARM_OR_ARM64)
#if (_MSC_VER >= 1600)
#include <intrin.h>
#endif
// disable for debug:
#define Z7_ZSTD_DEC_USE_BSR
#endif
#endif
#ifdef Z7_ZSTD_DEC_USE_BSR
#if defined(__clang__) || defined(__GNUC__)
#define MY_clz(x) ((unsigned)__builtin_clz((UInt32)x))
#else // #if defined(_MSC_VER)
#ifdef MY_CPU_ARM_OR_ARM64
#define MY_clz _CountLeadingZeros
#endif // MY_CPU_X86_OR_AMD64
#endif // _MSC_VER
#elif !defined(Z7_ZSTD_DEC_USE_LOG_TABLE)
#define Z7_ZSTD_DEC_USE_LOG_TABLE
#endif
static
Z7_FORCE_INLINE
unsigned GetHighestSetBit_32_nonzero_big(UInt32 num)
{
// (num != 0)
#ifdef MY_clz
return 31 - MY_clz(num);
#elif defined(Z7_ZSTD_DEC_USE_BSR)
{
unsigned long zz;
_BitScanReverse(&zz, num);
return zz;
}
#else
{
int i = -1;
for (;;)
{
i++;
num >>= 1;
if (num == 0)
return (unsigned)i;
}
}
#endif
}
#ifdef Z7_ZSTD_DEC_USE_LOG_TABLE
#define R1(a) a, a
#define R2(a) R1(a), R1(a)
#define R3(a) R2(a), R2(a)
#define R4(a) R3(a), R3(a)
#define R5(a) R4(a), R4(a)
#define R6(a) R5(a), R5(a)
#define R7(a) R6(a), R6(a)
#define R8(a) R7(a), R7(a)
#define R9(a) R8(a), R8(a)
#define Z7_ZSTD_FSE_MAX_ACCURACY 9
// states[] values in FSE_Generate() can use (Z7_ZSTD_FSE_MAX_ACCURACY + 1) bits.
static const Byte k_zstd_LogTable[2 << Z7_ZSTD_FSE_MAX_ACCURACY] =
{
R1(0), R1(1), R2(2), R3(3), R4(4), R5(5), R6(6), R7(7), R8(8), R9(9)
};
#define GetHighestSetBit_32_nonzero_small(num) (k_zstd_LogTable[num])
#else
#define GetHighestSetBit_32_nonzero_small GetHighestSetBit_32_nonzero_big
#endif
#ifdef MY_clz
#define UPDATE_BIT_OFFSET_FOR_PADDING(b, bitOffset) \
bitOffset -= (CBitCtr)(MY_clz(b) - 23);
#elif defined(Z7_ZSTD_DEC_USE_BSR)
#define UPDATE_BIT_OFFSET_FOR_PADDING(b, bitOffset) \
{ unsigned long zz; _BitScanReverse(&zz, b); bitOffset -= 8; bitOffset += zz; }
#else
#define UPDATE_BIT_OFFSET_FOR_PADDING(b, bitOffset) \
for (;;) { bitOffset--; if (b & 0x80) { break; } b <<= 1; }
#endif
#define SET_bitOffset_TO_PAD(bitOffset, src, srcLen) \
{ \
unsigned lastByte = (src)[(size_t)(srcLen) - 1]; \
if (lastByte == 0) return SZ_ERROR_DATA; \
bitOffset = (CBitCtr)((srcLen) * 8); \
UPDATE_BIT_OFFSET_FOR_PADDING(lastByte, bitOffset) \
}
#ifndef Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
#define SET_bitOffset_TO_PAD_and_SET_BIT_SIZE(bitOffset, src, srcLen_res) \
{ \
unsigned lastByte = (src)[(size_t)(srcLen_res) - 1]; \
if (lastByte == 0) return SZ_ERROR_DATA; \
srcLen_res *= 8; \
bitOffset = (CBitCtr)srcLen_res; \
UPDATE_BIT_OFFSET_FOR_PADDING(lastByte, bitOffset) \
}
#endif
/*
typedef Int32 CBitCtr_signed;
typedef Int32 CBitCtr;
*/
// /*
typedef ptrdiff_t CBitCtr_signed;
typedef ptrdiff_t CBitCtr;
// */
#define MATCH_LEN_MIN 3
#define kBlockSizeMax (1u << 17)
// #define Z7_ZSTD_DEC_PRINT_TABLE
#ifdef Z7_ZSTD_DEC_PRINT_TABLE
#define NUM_OFFSET_SYMBOLS_PREDEF 29
#endif
#define NUM_OFFSET_SYMBOLS_MAX (MAX_WINDOW_SIZE_LOG + 1) // 32
#define NUM_LL_SYMBOLS 36
#define NUM_ML_SYMBOLS 53
#define FSE_NUM_SYMBOLS_MAX 53 // NUM_ML_SYMBOLS
// /*
#if !defined(MY_CPU_X86) || defined(__PIC__) || defined(MY_CPU_64BIT)
#define Z7_ZSTD_DEC_USE_BASES_IN_OBJECT
#endif
// */
// for debug:
// #define Z7_ZSTD_DEC_USE_BASES_LOCAL
// #define Z7_ZSTD_DEC_USE_BASES_IN_OBJECT
#define GLOBAL_TABLE(n) k_ ## n
#if defined(Z7_ZSTD_DEC_USE_BASES_LOCAL)
#define BASES_TABLE(n) a_ ## n
#elif defined(Z7_ZSTD_DEC_USE_BASES_IN_OBJECT)
#define BASES_TABLE(n) p->m_ ## n
#else
#define BASES_TABLE(n) GLOBAL_TABLE(n)
#endif
#define Z7_ZSTD_DEC_USE_ML_PLUS3
#if defined(Z7_ZSTD_DEC_USE_BASES_LOCAL) || \
defined(Z7_ZSTD_DEC_USE_BASES_IN_OBJECT)
#define SEQ_EXTRA_TABLES(n) \
Byte n ## SEQ_LL_EXTRA [NUM_LL_SYMBOLS]; \
Byte n ## SEQ_ML_EXTRA [NUM_ML_SYMBOLS]; \
UInt32 n ## SEQ_LL_BASES [NUM_LL_SYMBOLS]; \
UInt32 n ## SEQ_ML_BASES [NUM_ML_SYMBOLS]; \
#define Z7_ZSTD_DEC_USE_BASES_CALC
#ifdef Z7_ZSTD_DEC_USE_BASES_CALC
#define FILL_LOC_BASES(n, startSum) \
{ unsigned i; UInt32 sum = startSum; \
for (i = 0; i != Z7_ARRAY_SIZE(GLOBAL_TABLE(n ## _EXTRA)); i++) \
{ const unsigned a = GLOBAL_TABLE(n ## _EXTRA)[i]; \
BASES_TABLE(n ## _BASES)[i] = sum; \
/* if (sum != GLOBAL_TABLE(n ## _BASES)[i]) exit(1); */ \
sum += (UInt32)1 << a; \
BASES_TABLE(n ## _EXTRA)[i] = (Byte)a; }}
#define FILL_LOC_BASES_ALL \
FILL_LOC_BASES (SEQ_LL, 0) \
FILL_LOC_BASES (SEQ_ML, MATCH_LEN_MIN) \
#else
#define COPY_GLOBAL_ARR(n) \
memcpy(BASES_TABLE(n), GLOBAL_TABLE(n), sizeof(GLOBAL_TABLE(n)));
#define FILL_LOC_BASES_ALL \
COPY_GLOBAL_ARR (SEQ_LL_EXTRA) \
COPY_GLOBAL_ARR (SEQ_ML_EXTRA) \
COPY_GLOBAL_ARR (SEQ_LL_BASES) \
COPY_GLOBAL_ARR (SEQ_ML_BASES) \
#endif
#endif
/// The sequence decoding baseline and number of additional bits to read/add
#if !defined(Z7_ZSTD_DEC_USE_BASES_CALC)
static const UInt32 GLOBAL_TABLE(SEQ_LL_BASES) [NUM_LL_SYMBOLS] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 18, 20, 22, 24, 28, 32, 40, 48, 64, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000,
0x2000, 0x4000, 0x8000, 0x10000
};
#endif
static const Byte GLOBAL_TABLE(SEQ_LL_EXTRA) [NUM_LL_SYMBOLS] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16
};
#if !defined(Z7_ZSTD_DEC_USE_BASES_CALC)
static const UInt32 GLOBAL_TABLE(SEQ_ML_BASES) [NUM_ML_SYMBOLS] =
{
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, 37, 39, 41, 43, 47, 51, 59, 67, 83, 99, 0x83, 0x103, 0x203, 0x403, 0x803,
0x1003, 0x2003, 0x4003, 0x8003, 0x10003
};
#endif
static const Byte GLOBAL_TABLE(SEQ_ML_EXTRA) [NUM_ML_SYMBOLS] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16
};
#ifdef Z7_ZSTD_DEC_PRINT_TABLE
static const Int16 SEQ_LL_PREDEF_DIST [NUM_LL_SYMBOLS] =
{
4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
-1,-1,-1,-1
};
static const Int16 SEQ_OFFSET_PREDEF_DIST [NUM_OFFSET_SYMBOLS_PREDEF] =
{
1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1
};
static const Int16 SEQ_ML_PREDEF_DIST [NUM_ML_SYMBOLS] =
{
1, 4, 3, 2, 2, 2, 2, 2, 2, 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
};
#endif
// typedef int FastInt;
// typedef Int32 FastInt32;
typedef unsigned FastInt;
typedef UInt32 FastInt32;
typedef FastInt32 CFseRecord;
#define FSE_REC_LEN_OFFSET 8
#define FSE_REC_STATE_OFFSET 16
#define GET_FSE_REC_SYM(st) ((Byte)(st))
#define GET_FSE_REC_LEN(st) ((Byte)((st) >> FSE_REC_LEN_OFFSET))
#define GET_FSE_REC_STATE(st) ((st) >> FSE_REC_STATE_OFFSET)
// #define FSE_REC_SYM_MASK (0xff)
// #define GET_FSE_REC_SYM(st) (st & FSE_REC_SYM_MASK)
#define W_BASE(state, len, sym) \
(((UInt32)state << (4 + FSE_REC_STATE_OFFSET)) + \
(len << FSE_REC_LEN_OFFSET) + (sym))
#define W(state, len, sym) W_BASE(state, len, sym)
static const CFseRecord k_PredefRecords_LL[1 << 6] = {
W(0,4, 0),W(1,4, 0),W(2,5, 1),W(0,5, 3),W(0,5, 4),W(0,5, 6),W(0,5, 7),W(0,5, 9),
W(0,5,10),W(0,5,12),W(0,6,14),W(0,5,16),W(0,5,18),W(0,5,19),W(0,5,21),W(0,5,22),
W(0,5,24),W(2,5,25),W(0,5,26),W(0,6,27),W(0,6,29),W(0,6,31),W(2,4, 0),W(0,4, 1),
W(0,5, 2),W(2,5, 4),W(0,5, 5),W(2,5, 7),W(0,5, 8),W(2,5,10),W(0,5,11),W(0,6,13),
W(2,5,16),W(0,5,17),W(2,5,19),W(0,5,20),W(2,5,22),W(0,5,23),W(0,4,25),W(1,4,25),
W(2,5,26),W(0,6,28),W(0,6,30),W(3,4, 0),W(1,4, 1),W(2,5, 2),W(2,5, 3),W(2,5, 5),
W(2,5, 6),W(2,5, 8),W(2,5, 9),W(2,5,11),W(2,5,12),W(0,6,15),W(2,5,17),W(2,5,18),
W(2,5,20),W(2,5,21),W(2,5,23),W(2,5,24),W(0,6,35),W(0,6,34),W(0,6,33),W(0,6,32)
};
static const CFseRecord k_PredefRecords_OF[1 << 5] = {
W(0,5, 0),W(0,4, 6),W(0,5, 9),W(0,5,15),W(0,5,21),W(0,5, 3),W(0,4, 7),W(0,5,12),
W(0,5,18),W(0,5,23),W(0,5, 5),W(0,4, 8),W(0,5,14),W(0,5,20),W(0,5, 2),W(1,4, 7),
W(0,5,11),W(0,5,17),W(0,5,22),W(0,5, 4),W(1,4, 8),W(0,5,13),W(0,5,19),W(0,5, 1),
W(1,4, 6),W(0,5,10),W(0,5,16),W(0,5,28),W(0,5,27),W(0,5,26),W(0,5,25),W(0,5,24)
};
#if defined(Z7_ZSTD_DEC_USE_ML_PLUS3)
#undef W
#define W(state, len, sym) W_BASE(state, len, (sym + MATCH_LEN_MIN))
#endif
static const CFseRecord k_PredefRecords_ML[1 << 6] = {
W(0,6, 0),W(0,4, 1),W(2,5, 2),W(0,5, 3),W(0,5, 5),W(0,5, 6),W(0,5, 8),W(0,6,10),
W(0,6,13),W(0,6,16),W(0,6,19),W(0,6,22),W(0,6,25),W(0,6,28),W(0,6,31),W(0,6,33),
W(0,6,35),W(0,6,37),W(0,6,39),W(0,6,41),W(0,6,43),W(0,6,45),W(1,4, 1),W(0,4, 2),
W(2,5, 3),W(0,5, 4),W(2,5, 6),W(0,5, 7),W(0,6, 9),W(0,6,12),W(0,6,15),W(0,6,18),
W(0,6,21),W(0,6,24),W(0,6,27),W(0,6,30),W(0,6,32),W(0,6,34),W(0,6,36),W(0,6,38),
W(0,6,40),W(0,6,42),W(0,6,44),W(2,4, 1),W(3,4, 1),W(1,4, 2),W(2,5, 4),W(2,5, 5),
W(2,5, 7),W(2,5, 8),W(0,6,11),W(0,6,14),W(0,6,17),W(0,6,20),W(0,6,23),W(0,6,26),
W(0,6,29),W(0,6,52),W(0,6,51),W(0,6,50),W(0,6,49),W(0,6,48),W(0,6,47),W(0,6,46)
};
// sum of freqs[] must be correct
// (numSyms != 0)
// (accuracy >= 5)
static
Z7_NO_INLINE
// Z7_FORCE_INLINE
void FSE_Generate(CFseRecord *table,
const Int16 *const freqs, const size_t numSyms,
const unsigned accuracy, UInt32 delta)
{
size_t size = (size_t)1 << accuracy;
// max value in states[x] is ((1 << accuracy) * 2)
UInt16 states[FSE_NUM_SYMBOLS_MAX];
{
/* Symbols with "less than 1" probability get a single cell,
starting from the end of the table.
These symbols define a full state reset, reading (accuracy) bits. */
size_t threshold = size;
{
size_t s = 0;
do
if (freqs[s] == -1)
{
table[--threshold] = (CFseRecord)s;
states[s] = 1;
}
while (++s != numSyms);
}
#ifdef SHOW_STAT
if (threshold == size)
{
STAT_INC(g_Num_Threshold_0)
STAT_UPDATE(g_Num_Threshold_0sum += (unsigned)size;)
}
else
{
STAT_INC(g_Num_Threshold_1)
STAT_UPDATE(g_Num_Threshold_1sum += (unsigned)size;)
}
#endif
// { unsigned uuu; for (uuu = 0; uuu < 400; uuu++)
{
// Each (symbol) gets freqs[symbol] cells.
// Cell allocation is spread, not linear.
const size_t step = (size >> 1) + (size >> 3) + 3;
size_t pos = 0;
// const unsigned mask = size - 1;
/*
if (threshold == size)
{
size_t s = 0;
size--;
do
{
int freq = freqs[s];
if (freq <= 0)
continue;
states[s] = (UInt16)freq;
do
{
table[pos] (CFseRecord)s;
pos = (pos + step) & size; // & mask;
}
while (--freq);
}
while (++s != numSyms);
}
else
*/
{
size_t s = 0;
size--;
do
{
int freq = freqs[s];
if (freq <= 0)
continue;
states[s] = (UInt16)freq;
do
{
table[pos] = (CFseRecord)s;
// we skip position, if it's already occupied by a "less than 1" probability symbol.
// (step) is coprime to table size, so the cycle will visit each position exactly once
do
pos = (pos + step) & size; // & mask;
while (pos >= threshold);
}
while (--freq);
}
while (++s != numSyms);
}
size++;
// (pos != 0) is unexpected case that means that freqs[] are not correct.
// so it's some failure in code (for example, incorrect predefined freq[] table)
// if (pos != 0) return SZ_ERROR_FAIL;
}
// }
}
{
const CFseRecord * const limit = table + size;
delta = ((UInt32)size << FSE_REC_STATE_OFFSET) - delta;
/* State increases by symbol over time, decreasing number of bits.
Baseline increases until the bit threshold is passed, at which point it resets to 0 */
do
{
#define TABLE_ITER(a) \
{ \
const FastInt sym = (FastInt)table[a]; \
const unsigned nextState = states[sym]; \
unsigned nb; \
states[sym] = (UInt16)(nextState + 1); \
nb = accuracy - GetHighestSetBit_32_nonzero_small(nextState); \
table[a] = (CFseRecord)(sym - delta \
+ ((UInt32)nb << FSE_REC_LEN_OFFSET) \
+ ((UInt32)nextState << FSE_REC_STATE_OFFSET << nb)); \
}
TABLE_ITER(0)
TABLE_ITER(1)
table += 2;
}
while (table != limit);
}
}
#ifdef Z7_ZSTD_DEC_PRINT_TABLE
static void Print_Predef(unsigned predefAccuracy,
const unsigned numSymsPredef,
const Int16 * const predefFreqs,
const CFseRecord *checkTable)
{
CFseRecord table[1 << 6];
unsigned i;
FSE_Generate(table, predefFreqs, numSymsPredef, predefAccuracy,
#if defined(Z7_ZSTD_DEC_USE_ML_PLUS3)
numSymsPredef == NUM_ML_SYMBOLS ? MATCH_LEN_MIN :
#endif
0
);
if (memcmp(table, checkTable, sizeof(UInt32) << predefAccuracy) != 0)
exit(1);
for (i = 0; i < (1u << predefAccuracy); i++)
{
const UInt32 v = table[i];
const unsigned state = (unsigned)(GET_FSE_REC_STATE(v));
if (state & 0xf)
exit(1);
if (i != 0)
{
printf(",");
if (i % 8 == 0)
printf("\n");
}
printf("W(%d,%d,%2d)",
(unsigned)(state >> 4),
(unsigned)((v >> FSE_REC_LEN_OFFSET) & 0xff),
(unsigned)GET_FSE_REC_SYM(v));
}
printf("\n\n");
}
#endif
#define GET16(dest, p) { const Byte *ptr = p; dest = GetUi16(ptr); }
#define GET32(dest, p) { const Byte *ptr = p; dest = GetUi32(ptr); }
// (1 <= numBits <= 9)
#define FORWARD_READ_BITS(destVal, numBits, mask) \
{ const CBitCtr_signed bos3 = (bitOffset) >> 3; \
if (bos3 >= 0) return SZ_ERROR_DATA; \
GET16(destVal, src + bos3) \
destVal >>= (bitOffset) & 7; \
bitOffset += (CBitCtr_signed)(numBits); \
mask = (1u << (numBits)) - 1; \
destVal &= mask; \
}
#define FORWARD_READ_1BIT(destVal) \
{ const CBitCtr_signed bos3 = (bitOffset) >> 3; \
if (bos3 >= 0) return SZ_ERROR_DATA; \
destVal = *(src + bos3); \
destVal >>= (bitOffset) & 7; \
(bitOffset)++; \
destVal &= 1; \
}
// in: (accuracyMax <= 9)
// at least 2 bytes will be processed from (in) stream.
// at return: (in->len > 0)
static
Z7_NO_INLINE
SRes FSE_DecodeHeader(CFseRecord *const table,
CInBufPair *const in,
const unsigned accuracyMax,
Byte *const accuracyRes,
unsigned numSymbolsMax)
{
unsigned accuracy;
unsigned remain1;
unsigned syms;
Int16 freqs[FSE_NUM_SYMBOLS_MAX + 3]; // +3 for overwrite (repeat)
const Byte *src = in->ptr;
CBitCtr_signed bitOffset = (CBitCtr_signed)in->len - 1;
if (bitOffset <= 0)
return SZ_ERROR_DATA;
accuracy = *src & 0xf;
accuracy += 5;
if (accuracy > accuracyMax)
return SZ_ERROR_DATA;
*accuracyRes = (Byte)accuracy;
remain1 = (1u << accuracy) + 1; // (it's remain_freqs_sum + 1)
syms = 0;
src += bitOffset; // src points to last byte
bitOffset = 4 - (bitOffset << 3);
for (;;)
{
// (2 <= remain1)
const unsigned bits = GetHighestSetBit_32_nonzero_small((unsigned)remain1);
// (1 <= bits <= accuracy)
unsigned val; // it must be unsigned or int
unsigned mask;
FORWARD_READ_BITS(val, bits, mask)
{
const unsigned val2 = remain1 + val - mask;
if (val2 > mask)
{
unsigned bit;
FORWARD_READ_1BIT(bit)
if (bit)
val = val2;
}
}
{
// (remain1 >= 2)
// (0 <= (int)val <= remain1)
val = (unsigned)((int)val - 1);
// val now is "probability" of symbol
// (probability == -1) means "less than 1" frequency.
// (-1 <= (int)val <= remain1 - 1)
freqs[syms++] = (Int16)(int)val;
if (val != 0)
{
remain1 -= (int)val < 0 ? 1u : (unsigned)val;
// remain1 -= val;
// val >>= (sizeof(val) * 8 - 2);
// remain1 -= val & 2;
// freqs[syms++] = (Int16)(int)val;
// syms++;
if (remain1 == 1)
break;
if (syms >= FSE_NUM_SYMBOLS_MAX)
return SZ_ERROR_DATA;
}
else // if (val == 0)
{
// freqs[syms++] = 0;
// syms++;
for (;;)
{
unsigned repeat;
FORWARD_READ_BITS(repeat, 2, mask)
freqs[syms ] = 0;
freqs[syms + 1] = 0;
freqs[syms + 2] = 0;
syms += repeat;
if (syms >= FSE_NUM_SYMBOLS_MAX)
return SZ_ERROR_DATA;
if (repeat != 3)
break;
}
}
}
}
if (syms > numSymbolsMax)
return SZ_ERROR_DATA;
bitOffset += 7;
bitOffset >>= 3;
if (bitOffset > 0)
return SZ_ERROR_DATA;
in->ptr = src + bitOffset;
in->len = (size_t)(1 - bitOffset);
{
// unsigned uuu; for (uuu = 0; uuu < 50; uuu++)
FSE_Generate(table, freqs, syms, accuracy,
#if defined(Z7_ZSTD_DEC_USE_ML_PLUS3)
numSymbolsMax == NUM_ML_SYMBOLS ? MATCH_LEN_MIN :
#endif
0
);
}
return SZ_OK;
}
// ---------- HUFFMAN ----------
#define HUF_MAX_BITS 12
#define HUF_MAX_SYMBS 256
#define HUF_DUMMY_SIZE (128 + 8 * 2) // it must multiple of 8
// #define HUF_DUMMY_SIZE 0
#define HUF_TABLE_SIZE ((2 << HUF_MAX_BITS) + HUF_DUMMY_SIZE)
#define HUF_GET_SYMBOLS(table) ((table) + (1 << HUF_MAX_BITS) + HUF_DUMMY_SIZE)
// #define HUF_GET_LENS(table) (table)
typedef struct
{
// Byte table[HUF_TABLE_SIZE];
UInt64 table64[HUF_TABLE_SIZE / sizeof(UInt64)];
}
CZstdDecHufTable;
/*
Input:
numSyms != 0
(bits) array size must be aligned for 2
if (numSyms & 1), then bits[numSyms] == 0,
Huffman tree must be correct before Huf_Build() call:
(sum (1/2^bits[i]) == 1).
&& (bits[i] <= HUF_MAX_BITS)
*/
static
Z7_FORCE_INLINE
void Huf_Build(Byte * const table,
const Byte *bits, const unsigned numSyms)
{
unsigned counts0[HUF_MAX_BITS + 1];
unsigned counts1[HUF_MAX_BITS + 1];
const Byte * const bitsEnd = bits + numSyms;
// /*
{
unsigned t;
for (t = 0; t < Z7_ARRAY_SIZE(counts0); t++) counts0[t] = 0;
for (t = 0; t < Z7_ARRAY_SIZE(counts1); t++) counts1[t] = 0;
}
// */
// memset(counts0, 0, sizeof(counts0));
// memset(counts1, 0, sizeof(counts1));
{
const Byte *bits2 = bits;
// we access additional bits[symbol] if (numSyms & 1)
do
{
counts0[bits2[0]]++;
counts1[bits2[1]]++;
}
while ((bits2 += 2) < bitsEnd);
}
{
unsigned r = 0;
unsigned i = HUF_MAX_BITS;
// Byte *lens = HUF_GET_LENS(symbols);
do
{
const unsigned num = (counts0[i] + counts1[i]) << (HUF_MAX_BITS - i);
counts0[i] = r;
if (num)
{
Byte *lens = &table[r];
r += num;
memset(lens, (int)i, num);
}
}
while (--i);
counts0[0] = 0; // for speculated loads
// no need for check:
// if (r != (UInt32)1 << HUF_MAX_BITS) exit(0);
}
{
#ifdef MY_CPU_64BIT
UInt64
#else
UInt32
#endif
v = 0;
Byte *symbols = HUF_GET_SYMBOLS(table);
do
{
const unsigned nb = *bits++;
if (nb)
{
const unsigned code = counts0[nb];
const unsigned num = (1u << HUF_MAX_BITS) >> nb;
counts0[nb] = code + num;
// memset(&symbols[code], i, num);
// /*
{
Byte *s2 = &symbols[code];
if (num <= 2)
{
s2[0] = (Byte)v;
s2[(size_t)num - 1] = (Byte)v;
}
else if (num <= 8)
{
*(UInt32 *)(void *)s2 = (UInt32)v;
*(UInt32 *)(void *)(s2 + (size_t)num - 4) = (UInt32)v;
}
else
{
#ifdef MY_CPU_64BIT
UInt64 *s = (UInt64 *)(void *)s2;
const UInt64 *lim = (UInt64 *)(void *)(s2 + num);
do
{
s[0] = v; s[1] = v; s += 2;
}
while (s != lim);
#else
UInt32 *s = (UInt32 *)(void *)s2;
const UInt32 *lim = (const UInt32 *)(const void *)(s2 + num);
do
{
s[0] = v; s[1] = v; s += 2;
s[0] = v; s[1] = v; s += 2;
}
while (s != lim);
#endif
}
}
// */
}
v +=
#ifdef MY_CPU_64BIT
0x0101010101010101;
#else
0x01010101;
#endif
}
while (bits != bitsEnd);
}
}
// how many bytes (src) was moved back from original value.
// we need (HUF_SRC_OFFSET == 3) for optimized 32-bit memory access
#define HUF_SRC_OFFSET 3
// v <<= 8 - (bitOffset & 7) + numBits;
// v >>= 32 - HUF_MAX_BITS;
#define HUF_GET_STATE(v, bitOffset, numBits) \
GET32(v, src + (HUF_SRC_OFFSET - 3) + ((CBitCtr_signed)bitOffset >> 3)) \
v >>= 32 - HUF_MAX_BITS - 8 + ((unsigned)bitOffset & 7) - numBits; \
v &= (1u << HUF_MAX_BITS) - 1; \
#ifndef Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
#if defined(MY_CPU_AMD64) && defined(_MSC_VER) && _MSC_VER == 1400 \
|| !defined(MY_CPU_X86_OR_AMD64) \
// || 1 == 1 /* for debug : to force STREAM4_PRELOAD mode */
// we need big number (>=16) of registers for PRELOAD4
#define Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD4
// #define Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD2 // for debug
#endif
#endif
// for debug: simpler and smaller code but slow:
// #define Z7_ZSTD_DEC_USE_HUF_STREAM1_SIMPLE
#if defined(Z7_ZSTD_DEC_USE_HUF_STREAM1_SIMPLE) || \
!defined(Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS)
#define HUF_DECODE(bitOffset, dest) \
{ \
UInt32 v; \
HUF_GET_STATE(v, bitOffset, 0) \
bitOffset -= table[v]; \
*(dest) = symbols[v]; \
if ((CBitCtr_signed)bitOffset < 0) return SZ_ERROR_DATA; \
}
#endif
#if !defined(Z7_ZSTD_DEC_USE_HUF_STREAM1_SIMPLE) || \
defined(Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD4) || \
defined(Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD2) \
#define HUF_DECODE_2_INIT(v, bitOffset) \
HUF_GET_STATE(v, bitOffset, 0)
#define HUF_DECODE_2(v, bitOffset, dest) \
{ \
unsigned numBits; \
numBits = table[v]; \
*(dest) = symbols[v]; \
HUF_GET_STATE(v, bitOffset, numBits) \
bitOffset -= (CBitCtr)numBits; \
if ((CBitCtr_signed)bitOffset < 0) return SZ_ERROR_DATA; \
}
#endif
// src == ptr - HUF_SRC_OFFSET
// we are allowed to access 3 bytes before start of input buffer
static
Z7_NO_INLINE
SRes Huf_Decompress_1stream(const Byte * const table,
const Byte *src, const size_t srcLen,
Byte *dest, const size_t destLen)
{
CBitCtr bitOffset;
if (srcLen == 0)
return SZ_ERROR_DATA;
SET_bitOffset_TO_PAD (bitOffset, src + HUF_SRC_OFFSET, srcLen)
if (destLen)
{
const Byte *symbols = HUF_GET_SYMBOLS(table);
const Byte *destLim = dest + destLen;
#ifdef Z7_ZSTD_DEC_USE_HUF_STREAM1_SIMPLE
{
do
{
HUF_DECODE (bitOffset, dest)
}
while (++dest != destLim);
}
#else
{
UInt32 v;
HUF_DECODE_2_INIT (v, bitOffset)
do
{
HUF_DECODE_2 (v, bitOffset, dest)
}
while (++dest != destLim);
}
#endif
}
return bitOffset == 0 ? SZ_OK : SZ_ERROR_DATA;
}
// for debug : it reduces register pressure : by array copy can be slow :
// #define Z7_ZSTD_DEC_USE_HUF_LOCAL
// src == ptr + (6 - HUF_SRC_OFFSET)
// srcLen >= 10
// we are allowed to access 3 bytes before start of input buffer
static
Z7_NO_INLINE
SRes Huf_Decompress_4stream(const Byte * const
#ifdef Z7_ZSTD_DEC_USE_HUF_LOCAL
table2,
#else
table,
#endif
const Byte *src, size_t srcLen,
Byte *dest, size_t destLen)
{
#ifdef Z7_ZSTD_DEC_USE_HUF_LOCAL
Byte table[HUF_TABLE_SIZE];
#endif
UInt32 sizes[3];
const size_t delta = (destLen + 3) / 4;
if ((sizes[0] = GetUi16(src + (0 + HUF_SRC_OFFSET - 6))) == 0) return SZ_ERROR_DATA;
if ((sizes[1] = GetUi16(src + (2 + HUF_SRC_OFFSET - 6))) == 0) return SZ_ERROR_DATA;
sizes[1] += sizes[0];
if ((sizes[2] = GetUi16(src + (4 + HUF_SRC_OFFSET - 6))) == 0) return SZ_ERROR_DATA;
sizes[2] += sizes[1];
srcLen -= 6;
if (srcLen <= sizes[2])
return SZ_ERROR_DATA;
#ifdef Z7_ZSTD_DEC_USE_HUF_LOCAL
{
// unsigned i = 0; for(; i < 1000; i++)
memcpy(table, table2, HUF_TABLE_SIZE);
}
#endif
#ifndef Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
{
CBitCtr bitOffset_0,
bitOffset_1,
bitOffset_2,
bitOffset_3;
{
SET_bitOffset_TO_PAD_and_SET_BIT_SIZE (bitOffset_0, src + HUF_SRC_OFFSET, sizes[0])
SET_bitOffset_TO_PAD_and_SET_BIT_SIZE (bitOffset_1, src + HUF_SRC_OFFSET, sizes[1])
SET_bitOffset_TO_PAD_and_SET_BIT_SIZE (bitOffset_2, src + HUF_SRC_OFFSET, sizes[2])
SET_bitOffset_TO_PAD (bitOffset_3, src + HUF_SRC_OFFSET, srcLen)
}
{
const Byte * const symbols = HUF_GET_SYMBOLS(table);
Byte *destLim = dest + destLen - delta * 3;
if (dest != destLim)
#ifdef Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD4
{
UInt32 v_0, v_1, v_2, v_3;
HUF_DECODE_2_INIT (v_0, bitOffset_0)
HUF_DECODE_2_INIT (v_1, bitOffset_1)
HUF_DECODE_2_INIT (v_2, bitOffset_2)
HUF_DECODE_2_INIT (v_3, bitOffset_3)
// #define HUF_DELTA (1 << 17) / 4
do
{
HUF_DECODE_2 (v_3, bitOffset_3, dest + delta * 3)
HUF_DECODE_2 (v_2, bitOffset_2, dest + delta * 2)
HUF_DECODE_2 (v_1, bitOffset_1, dest + delta)
HUF_DECODE_2 (v_0, bitOffset_0, dest)
}
while (++dest != destLim);
/*
{// unsigned y = 0; for (;y < 1; y++)
{
const size_t num = destLen - delta * 3;
Byte *orig = dest - num;
memmove (orig + delta , orig + HUF_DELTA, num);
memmove (orig + delta * 2, orig + HUF_DELTA * 2, num);
memmove (orig + delta * 3, orig + HUF_DELTA * 3, num);
}}
*/
}
#elif defined(Z7_ZSTD_DEC_USE_HUF_STREAM4_PRELOAD2)
{
UInt32 v_0, v_1, v_2, v_3;
HUF_DECODE_2_INIT (v_0, bitOffset_0)
HUF_DECODE_2_INIT (v_1, bitOffset_1)
do
{
HUF_DECODE_2 (v_0, bitOffset_0, dest)
HUF_DECODE_2 (v_1, bitOffset_1, dest + delta)
}
while (++dest != destLim);
dest = destLim - (destLen - delta * 3);
dest += delta * 2;
destLim += delta * 2;
HUF_DECODE_2_INIT (v_2, bitOffset_2)
HUF_DECODE_2_INIT (v_3, bitOffset_3)
do
{
HUF_DECODE_2 (v_2, bitOffset_2, dest)
HUF_DECODE_2 (v_3, bitOffset_3, dest + delta)
}
while (++dest != destLim);
dest -= delta * 2;
destLim -= delta * 2;
}
#else
{
do
{
HUF_DECODE (bitOffset_3, dest + delta * 3)
HUF_DECODE (bitOffset_2, dest + delta * 2)
HUF_DECODE (bitOffset_1, dest + delta)
HUF_DECODE (bitOffset_0, dest)
}
while (++dest != destLim);
}
#endif
if (bitOffset_3 != (CBitCtr)sizes[2])
return SZ_ERROR_DATA;
if (destLen &= 3)
{
destLim = dest + 4 - destLen;
do
{
HUF_DECODE (bitOffset_2, dest + delta * 2)
HUF_DECODE (bitOffset_1, dest + delta)
HUF_DECODE (bitOffset_0, dest)
}
while (++dest != destLim);
}
if ( bitOffset_0 != 0
|| bitOffset_1 != (CBitCtr)sizes[0]
|| bitOffset_2 != (CBitCtr)sizes[1])
return SZ_ERROR_DATA;
}
}
#else // Z7_ZSTD_DEC_USE_HUF_STREAM1_ALWAYS
{
unsigned i;
for (i = 0; i < 4; i++)
{
size_t d = destLen;
size_t size = srcLen;
if (i != 3)
{
d = delta;
size = sizes[i];
}
if (i != 0)
size -= sizes[i - 1];
destLen -= d;
RINOK(Huf_Decompress_1stream(table, src, size, dest, d))
dest += d;
src += size;
}
}
#endif
return SZ_OK;
}
// (in->len != 0)
// we are allowed to access in->ptr[-3]
// at least 2 bytes in (in->ptr) will be processed
static SRes Huf_DecodeTable(CZstdDecHufTable *const p, CInBufPair *const in)
{
Byte weights[HUF_MAX_SYMBS + 1]; // +1 for extra write for loop unroll
unsigned numSyms;
const unsigned header = *(in->ptr)++;
in->len--;
// memset(weights, 0, sizeof(weights));
if (header >= 128)
{
// direct representation: 4 bits field (0-15) per weight
numSyms = header - 127;
// numSyms != 0
{
const size_t numBytes = (numSyms + 1) / 2;
const Byte *const ws = in->ptr;
size_t i = 0;
if (in->len < numBytes)
return SZ_ERROR_DATA;
in->ptr += numBytes;
in->len -= numBytes;
do
{
const unsigned b = ws[i];
weights[i * 2 ] = (Byte)(b >> 4);
weights[i * 2 + 1] = (Byte)(b & 0xf);
}
while (++i != numBytes);
/* 7ZIP: we can restore correct zero value for weights[numSyms],
if we want to use zero values starting from numSyms in code below. */
// weights[numSyms] = 0;
}
}
else
{
#define MAX_ACCURACY_LOG_FOR_WEIGHTS 6
CFseRecord table[1 << MAX_ACCURACY_LOG_FOR_WEIGHTS];
Byte accuracy;
const Byte *src;
size_t srcLen;
if (in->len < header)
return SZ_ERROR_DATA;
{
CInBufPair fse_stream;
fse_stream.len = header;
fse_stream.ptr = in->ptr;
in->ptr += header;
in->len -= header;
RINOK(FSE_DecodeHeader(table, &fse_stream,
MAX_ACCURACY_LOG_FOR_WEIGHTS,
&accuracy,
16 // num weight symbols max (max-symbol is 15)
))
// at least 2 bytes were processed in fse_stream.
// (srcLen > 0) after FSE_DecodeHeader()
// if (srcLen == 0) return SZ_ERROR_DATA;
src = fse_stream.ptr;
srcLen = fse_stream.len;
}
// we are allowed to access src[-5]
{
// unsigned yyy = 200; do {
CBitCtr bitOffset;
FastInt32 state1, state2;
SET_bitOffset_TO_PAD (bitOffset, src, srcLen)
state1 = accuracy;
src -= state1 >> 2; // src -= 1; // for GET16() optimization
state1 <<= FSE_REC_LEN_OFFSET;
state2 = state1;
numSyms = 0;
for (;;)
{
#define FSE_WEIGHT_DECODE(st) \
{ \
const unsigned bits = GET_FSE_REC_LEN(st); \
FastInt r; \
GET16(r, src + (bitOffset >> 3)) \
r >>= (unsigned)bitOffset & 7; \
if ((CBitCtr_signed)(bitOffset -= (CBitCtr)bits) < 0) \
{ if (bitOffset + (CBitCtr)bits != 0) \
return SZ_ERROR_DATA; \
break; } \
r &= 0xff; \
r >>= 8 - bits; \
st = table[GET_FSE_REC_STATE(st) + r]; \
weights[numSyms++] = (Byte)GET_FSE_REC_SYM(st); \
}
FSE_WEIGHT_DECODE (state1)
FSE_WEIGHT_DECODE (state2)
if (numSyms == HUF_MAX_SYMBS)
return SZ_ERROR_DATA;
}
// src += (unsigned)accuracy >> 2; } while (--yyy);
}
}
// Build using weights:
{
UInt32 sum = 0;
{
// numSyms >= 1
unsigned i = 0;
weights[numSyms] = 0;
do
{
sum += ((UInt32)1 << weights[i ]) & ~(UInt32)1;
sum += ((UInt32)1 << weights[i + 1]) & ~(UInt32)1;
i += 2;
}
while (i < numSyms);
if (sum == 0)
return SZ_ERROR_DATA;
}
{
const unsigned maxBits = GetHighestSetBit_32_nonzero_big(sum) + 1;
{
const UInt32 left = ((UInt32)1 << maxBits) - sum;
// (left != 0)
// (left) must be power of 2 in correct stream
if (left & (left - 1))
return SZ_ERROR_DATA;
weights[numSyms++] = (Byte)GetHighestSetBit_32_nonzero_big(left);
}
// if (numSyms & 1)
weights[numSyms] = 0; // for loop unroll
// numSyms >= 2
{
unsigned i = 0;
do
{
/*
#define WEIGHT_ITER(a) \
{ unsigned w = weights[i + (a)]; \
const unsigned t = maxBits - w; \
w = w ? t: w; \
if (w > HUF_MAX_BITS) return SZ_ERROR_DATA; \
weights[i + (a)] = (Byte)w; }
*/
// /*
#define WEIGHT_ITER(a) \
{ unsigned w = weights[i + (a)]; \
if (w) { \
w = maxBits - w; \
if (w > HUF_MAX_BITS) return SZ_ERROR_DATA; \
weights[i + (a)] = (Byte)w; }}
// */
WEIGHT_ITER(0)
// WEIGHT_ITER(1)
// i += 2;
}
while (++i != numSyms);
}
}
}
{
// unsigned yyy; for (yyy = 0; yyy < 100; yyy++)
Huf_Build((Byte *)(void *)p->table64, weights, numSyms);
}
return SZ_OK;
}
typedef enum
{
k_SeqMode_Predef = 0,
k_SeqMode_RLE = 1,
k_SeqMode_FSE = 2,
k_SeqMode_Repeat = 3
}
z7_zstd_enum_SeqMode;
// predefAccuracy == 5 for OFFSET symbols
// predefAccuracy == 6 for MATCH/LIT LEN symbols
static
SRes
Z7_NO_INLINE
// Z7_FORCE_INLINE
FSE_Decode_SeqTable(CFseRecord * const table,
CInBufPair * const in,
unsigned predefAccuracy,
Byte * const accuracyRes,
unsigned numSymbolsMax,
const CFseRecord * const predefs,
const unsigned seqMode)
{
// UNUSED_VAR(numSymsPredef)
// UNUSED_VAR(predefFreqs)
if (seqMode == k_SeqMode_FSE)
{
// unsigned y = 50; CInBufPair in2 = *in; do { *in = in2; RINOK(
return
FSE_DecodeHeader(table, in,
predefAccuracy + 3, // accuracyMax
accuracyRes,
numSymbolsMax)
;
// )} while (--y); return SZ_OK;
}
// numSymsMax = numSymsPredef + ((predefAccuracy & 1) * (32 - 29))); // numSymsMax
// numSymsMax == 32 for offsets
if (seqMode == k_SeqMode_Predef)
{
*accuracyRes = (Byte)predefAccuracy;
memcpy(table, predefs, sizeof(UInt32) << predefAccuracy);
return SZ_OK;
}
// (seqMode == k_SeqMode_RLE)
if (in->len == 0)
return SZ_ERROR_DATA;
in->len--;
{
const Byte *ptr = in->ptr;
const unsigned sym = ptr[0];
in->ptr = ptr + 1;
if (sym >= numSymbolsMax)
return SZ_ERROR_DATA;
table[0] = (FastInt32)sym
#if defined(Z7_ZSTD_DEC_USE_ML_PLUS3)
+ (numSymbolsMax == NUM_ML_SYMBOLS ? MATCH_LEN_MIN : 0)
#endif
;
*accuracyRes = 0;
}
return SZ_OK;
}
typedef struct
{
CFseRecord of[1 << 8];
CFseRecord ll[1 << 9];
CFseRecord ml[1 << 9];
}
CZstdDecFseTables;
typedef struct
{
Byte *win;
SizeT cycSize;
/*
if (outBuf_fromCaller) : cycSize = outBufSize_fromCaller
else {
if ( isCyclicMode) : cycSize = cyclic_buffer_size = (winSize + extra_space)
if (!isCyclicMode) : cycSize = ContentSize,
(isCyclicMode == true) if (ContetSize >= winSize) or ContetSize is unknown
}
*/
SizeT winPos;
CZstdDecOffset reps[3];
Byte ll_accuracy;
Byte of_accuracy;
Byte ml_accuracy;
// Byte seqTables_wereSet;
Byte litHuf_wasSet;
Byte *literalsBase;
size_t winSize; // from header
size_t totalOutCheck; // totalOutCheck <= winSize
#ifdef Z7_ZSTD_DEC_USE_BASES_IN_OBJECT
SEQ_EXTRA_TABLES(m_)
#endif
// UInt64 _pad_Alignment; // is not required now
CZstdDecFseTables fse;
CZstdDecHufTable huf;
}
CZstdDec1;
#define ZstdDec1_GET_BLOCK_SIZE_LIMIT(p) \
((p)->winSize < kBlockSizeMax ? (UInt32)(p)->winSize : kBlockSizeMax)
#define SEQ_TABLES_WERE_NOT_SET_ml_accuracy 1 // accuracy=1 is not used by zstd
#define IS_SEQ_TABLES_WERE_SET(p) (((p)->ml_accuracy != SEQ_TABLES_WERE_NOT_SET_ml_accuracy))
// #define IS_SEQ_TABLES_WERE_SET(p) ((p)->seqTables_wereSet)
static void ZstdDec1_Construct(CZstdDec1 *p)
{
#ifdef Z7_ZSTD_DEC_PRINT_TABLE
Print_Predef(6, NUM_LL_SYMBOLS, SEQ_LL_PREDEF_DIST, k_PredefRecords_LL);
Print_Predef(5, NUM_OFFSET_SYMBOLS_PREDEF, SEQ_OFFSET_PREDEF_DIST, k_PredefRecords_OF);
Print_Predef(6, NUM_ML_SYMBOLS, SEQ_ML_PREDEF_DIST, k_PredefRecords_ML);
#endif
p->win = NULL;
p->cycSize = 0;
p->literalsBase = NULL;
#ifdef Z7_ZSTD_DEC_USE_BASES_IN_OBJECT
FILL_LOC_BASES_ALL
#endif
}
static void ZstdDec1_Init(CZstdDec1 *p)
{
p->reps[0] = 1;
p->reps[1] = 4;
p->reps[2] = 8;
// p->seqTables_wereSet = False;
p->ml_accuracy = SEQ_TABLES_WERE_NOT_SET_ml_accuracy;
p->litHuf_wasSet = False;
p->totalOutCheck = 0;
}
#ifdef MY_CPU_LE_UNALIGN
#define Z7_ZSTD_DEC_USE_UNALIGNED_COPY
#endif
#ifdef Z7_ZSTD_DEC_USE_UNALIGNED_COPY
#define COPY_CHUNK_SIZE 16
#define COPY_CHUNK_4_2(dest, src) \
{ \
((UInt32 *)(void *)dest)[0] = ((const UInt32 *)(const void *)src)[0]; \
((UInt32 *)(void *)dest)[1] = ((const UInt32 *)(const void *)src)[1]; \
src += 4 * 2; \
dest += 4 * 2; \
}
/* sse2 doesn't help here in GCC and CLANG.
so we disabled sse2 here */
/*
#if defined(MY_CPU_AMD64)
#define Z7_ZSTD_DEC_USE_SSE2
#elif defined(MY_CPU_X86)
#if defined(_MSC_VER) && _MSC_VER >= 1300 && defined(_M_IX86_FP) && (_M_IX86_FP >= 2) \
|| defined(__SSE2__) \
// || 1 == 1 // for debug only
#define Z7_ZSTD_DEC_USE_SSE2
#endif
#endif
*/
#if defined(MY_CPU_ARM64)
#define COPY_OFFSET_MIN 16
#define COPY_CHUNK1(dest, src) \
{ \
vst1q_u8((uint8_t *)(void *)dest, \
vld1q_u8((const uint8_t *)(const void *)src)); \
src += 16; \
dest += 16; \
}
#define COPY_CHUNK(dest, src) \
{ \
COPY_CHUNK1(dest, src) \
if ((len -= COPY_CHUNK_SIZE) == 0) break; \
COPY_CHUNK1(dest, src) \
}
#elif defined(Z7_ZSTD_DEC_USE_SSE2)
#include <emmintrin.h> // sse2
#define COPY_OFFSET_MIN 16
#define COPY_CHUNK1(dest, src) \
{ \
_mm_storeu_si128((__m128i *)(void *)dest, \
_mm_loadu_si128((const __m128i *)(const void *)src)); \
src += 16; \
dest += 16; \
}
#define COPY_CHUNK(dest, src) \
{ \
COPY_CHUNK1(dest, src) \
if ((len -= COPY_CHUNK_SIZE) == 0) break; \
COPY_CHUNK1(dest, src) \
}
#elif defined(MY_CPU_64BIT)
#define COPY_OFFSET_MIN 8
#define COPY_CHUNK(dest, src) \
{ \
((UInt64 *)(void *)dest)[0] = ((const UInt64 *)(const void *)src)[0]; \
((UInt64 *)(void *)dest)[1] = ((const UInt64 *)(const void *)src)[1]; \
src += 8 * 2; \
dest += 8 * 2; \
}
#else
#define COPY_OFFSET_MIN 4
#define COPY_CHUNK(dest, src) \
{ \
COPY_CHUNK_4_2(dest, src); \
COPY_CHUNK_4_2(dest, src); \
}
#endif
#endif
#ifndef COPY_CHUNK_SIZE
#define COPY_OFFSET_MIN 4
#define COPY_CHUNK_SIZE 8
#define COPY_CHUNK_2(dest, src) \
{ \
const Byte a0 = src[0]; \
const Byte a1 = src[1]; \
dest[0] = a0; \
dest[1] = a1; \
src += 2; \
dest += 2; \
}
#define COPY_CHUNK(dest, src) \
{ \
COPY_CHUNK_2(dest, src) \
COPY_CHUNK_2(dest, src) \
COPY_CHUNK_2(dest, src) \
COPY_CHUNK_2(dest, src) \
}
#endif
#define COPY_PREPARE \
len += (COPY_CHUNK_SIZE - 1); \
len &= ~(size_t)(COPY_CHUNK_SIZE - 1); \
{ if (len > rem) \
{ len = rem; \
rem &= (COPY_CHUNK_SIZE - 1); \
if (rem) { \
len -= rem; \
Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE \
do *dest++ = *src++; while (--rem); \
if (len == 0) return; }}}
#define COPY_CHUNKS \
{ \
Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE \
do { COPY_CHUNK(dest, src) } \
while (len -= COPY_CHUNK_SIZE); \
}
// (len != 0)
// (len <= rem)
static
Z7_FORCE_INLINE
// Z7_ATTRIB_NO_VECTOR
void CopyLiterals(Byte *dest, Byte const *src, size_t len, size_t rem)
{
COPY_PREPARE
COPY_CHUNKS
}
/* we can define Z7_STD_DEC_USE_AFTER_CYC_BUF, if we want to use additional
space after cycSize that can be used to reduce the code in CopyMatch(): */
// for debug:
// #define Z7_STD_DEC_USE_AFTER_CYC_BUF
/*
CopyMatch()
if wrap (offset > winPos)
{
then we have at least (COPY_CHUNK_SIZE) avail in (dest) before we will overwrite (src):
(cycSize >= offset + COPY_CHUNK_SIZE)
if defined(Z7_STD_DEC_USE_AFTER_CYC_BUF)
we are allowed to read win[cycSize + COPY_CHUNK_SIZE - 1],
}
(len != 0)
*/
static
Z7_FORCE_INLINE
// Z7_ATTRIB_NO_VECTOR
void CopyMatch(size_t offset, size_t len,
Byte *win, size_t winPos, size_t rem, const size_t cycSize)
{
Byte *dest = win + winPos;
const Byte *src;
// STAT_INC(g_NumCopy)
if (offset > winPos)
{
size_t back = offset - winPos;
// src = win + cycSize - back;
// cycSize -= offset;
STAT_INC(g_NumOver)
src = dest + (cycSize - offset);
// (src >= dest) here
#ifdef Z7_STD_DEC_USE_AFTER_CYC_BUF
if (back < len)
{
#else
if (back < len + (COPY_CHUNK_SIZE - 1))
{
if (back >= len)
{
Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
do
*dest++ = *src++;
while (--len);
return;
}
#endif
// back < len
STAT_INC(g_NumOver2)
len -= back;
rem -= back;
Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
do
*dest++ = *src++;
while (--back);
src = dest - offset;
// src = win;
// we go to MAIN-COPY
}
}
else
src = dest - offset;
// len != 0
// do *dest++ = *src++; while (--len); return;
// --- MAIN COPY ---
// if (src >= dest), then ((size_t)(src - dest) >= COPY_CHUNK_SIZE)
// so we have at least COPY_CHUNK_SIZE space before overlap for writing.
COPY_PREPARE
/* now (len == COPY_CHUNK_SIZE * x)
so we can unroll for aligned copy */
{
// const unsigned b0 = src[0];
// (COPY_OFFSET_MIN >= 4)
if (offset >= COPY_OFFSET_MIN)
{
COPY_CHUNKS
// return;
}
else
#if (COPY_OFFSET_MIN > 4)
#if COPY_CHUNK_SIZE < 8
#error Stop_Compiling_Bad_COPY_CHUNK_SIZE
#endif
if (offset >= 4)
{
Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
do
{
COPY_CHUNK_4_2(dest, src)
#if COPY_CHUNK_SIZE != 16
if (len == 8) break;
#endif
COPY_CHUNK_4_2(dest, src)
}
while (len -= 16);
// return;
}
else
#endif
{
// (offset < 4)
const unsigned b0 = src[0];
if (offset < 2)
{
#if defined(Z7_ZSTD_DEC_USE_UNALIGNED_COPY) && (COPY_CHUNK_SIZE == 16)
#if defined(MY_CPU_64BIT)
{
const UInt64 v64 = (UInt64)b0 * 0x0101010101010101;
Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
do
{
((UInt64 *)(void *)dest)[0] = v64;
((UInt64 *)(void *)dest)[1] = v64;
dest += 16;
}
while (len -= 16);
}
#else
{
UInt32 v = b0;
v |= v << 8;
v |= v << 16;
do
{
((UInt32 *)(void *)dest)[0] = v;
((UInt32 *)(void *)dest)[1] = v;
dest += 8;
((UInt32 *)(void *)dest)[0] = v;
((UInt32 *)(void *)dest)[1] = v;
dest += 8;
}
while (len -= 16);
}
#endif
#else
do
{
dest[0] = (Byte)b0;
dest[1] = (Byte)b0;
dest += 2;
dest[0] = (Byte)b0;
dest[1] = (Byte)b0;
dest += 2;
}
while (len -= 4);
#endif
}
else if (offset == 2)
{
const Byte b1 = src[1];
{
do
{
dest[0] = (Byte)b0;
dest[1] = b1;
dest += 2;
}
while (len -= 2);
}
}
else // (offset == 3)
{
const Byte *lim = dest + len - 2;
const Byte b1 = src[1];
const Byte b2 = src[2];
do
{
dest[0] = (Byte)b0;
dest[1] = b1;
dest[2] = b2;
dest += 3;
}
while (dest < lim);
lim++; // points to last byte that must be written
if (dest <= lim)
{
*dest = (Byte)b0;
if (dest != lim)
dest[1] = b1;
}
}
}
}
}
#define UPDATE_TOTAL_OUT(p, size) \
{ \
size_t _toc = (p)->totalOutCheck + (size); \
const size_t _ws = (p)->winSize; \
if (_toc >= _ws) _toc = _ws; \
(p)->totalOutCheck = _toc; \
}
#if defined(MY_CPU_64BIT) && defined(MY_CPU_LE_UNALIGN)
// we can disable it for debug:
#define Z7_ZSTD_DEC_USE_64BIT_LOADS
#endif
// #define Z7_ZSTD_DEC_USE_64BIT_LOADS // for debug : slow in 32-bit
// SEQ_SRC_OFFSET: how many bytes (src) (seqSrc) was moved back from original value.
// we need (SEQ_SRC_OFFSET != 0) for optimized memory access
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
#define SEQ_SRC_OFFSET 7
#else
#define SEQ_SRC_OFFSET 3
#endif
#define SRC_PLUS_FOR_4BYTES(bitOffset) (SEQ_SRC_OFFSET - 3) + ((CBitCtr_signed)(bitOffset) >> 3)
#define BIT_OFFSET_7BITS(bitOffset) ((unsigned)(bitOffset) & 7)
/*
if (BIT_OFFSET_DELTA_BITS == 0) : bitOffset == number_of_unprocessed_bits
if (BIT_OFFSET_DELTA_BITS == 1) : bitOffset == number_of_unprocessed_bits - 1
and we can read 1 bit more in that mode : (8 * n + 1).
*/
// #define BIT_OFFSET_DELTA_BITS 0
#define BIT_OFFSET_DELTA_BITS 1
#if BIT_OFFSET_DELTA_BITS == 1
#define GET_SHIFT_FROM_BOFFS7(boff7) (7 ^ (boff7))
#else
#define GET_SHIFT_FROM_BOFFS7(boff7) (8 - BIT_OFFSET_DELTA_BITS - (boff7))
#endif
#define UPDATE_BIT_OFFSET(bitOffset, numBits) \
(bitOffset) -= (CBitCtr)(numBits);
#define GET_SHIFT(bitOffset) GET_SHIFT_FROM_BOFFS7(BIT_OFFSET_7BITS(bitOffset))
#if defined(Z7_ZSTD_DEC_USE_64BIT_LOADS)
#if (NUM_OFFSET_SYMBOLS_MAX - BIT_OFFSET_DELTA_BITS < 32)
/* if (NUM_OFFSET_SYMBOLS_MAX == 32 && BIT_OFFSET_DELTA_BITS == 1),
we have depth 31 + 9 + 9 + 8 = 57 bits that can b read with single read. */
#define Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF
#endif
#ifndef Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF
#if (BIT_OFFSET_DELTA_BITS == 1)
/* if (winLimit - winPos <= (kBlockSizeMax = (1 << 17)))
{
the case (16 bits literal extra + 16 match extra) is not possible
in correct stream. So error will be detected for (16 + 16) case.
And longest correct sequence after offset reading is (31 + 9 + 9 + 8 = 57 bits).
So we can use just one 64-bit load here in that case.
}
*/
#define Z7_ZSTD_DEC_USE_64BIT_PRELOAD_ML
#endif
#endif
#endif
#if !defined(Z7_ZSTD_DEC_USE_64BIT_LOADS) || \
(!defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF) && \
!defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_ML))
// in : (0 < bits <= (24 or 25)):
#define STREAM_READ_BITS(dest, bits) \
{ \
GET32(dest, src + SRC_PLUS_FOR_4BYTES(bitOffset)) \
dest <<= GET_SHIFT(bitOffset); \
UPDATE_BIT_OFFSET(bitOffset, bits) \
dest >>= 32 - bits; \
}
#endif
#define FSE_Peek_1(table, state) table[state]
#define STATE_VAR(name) state_ ## name
// in : (0 <= accuracy <= (24 or 25))
#define FSE_INIT_STATE(name, cond) \
{ \
UInt32 r; \
const unsigned bits = p->name ## _accuracy; \
GET32(r, src + SRC_PLUS_FOR_4BYTES(bitOffset)) \
r <<= GET_SHIFT(bitOffset); \
r >>= 1; \
r >>= 31 ^ bits; \
UPDATE_BIT_OFFSET(bitOffset, bits) \
cond \
STATE_VAR(name) = FSE_Peek_1(FSE_TABLE(name), r); \
/* STATE_VAR(name) = dest << 16; */ \
}
#define FSE_Peek_Plus(name, r) \
STATE_VAR(name) = FSE_Peek_1(FSE_TABLE(name), \
GET_FSE_REC_STATE(STATE_VAR(name)) + r);
#define LZ_LOOP_ERROR_EXIT { return SZ_ERROR_DATA; }
#define BO_OVERFLOW_CHECK \
{ if ((CBitCtr_signed)bitOffset < 0) LZ_LOOP_ERROR_EXIT }
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
#define GET64(dest, p) { const Byte *ptr = p; dest = GetUi64(ptr); }
#define FSE_PRELOAD \
{ \
GET64(v, src - 4 + SRC_PLUS_FOR_4BYTES(bitOffset)) \
v <<= GET_SHIFT(bitOffset); \
}
#define FSE_UPDATE_STATE_2(name, cond) \
{ \
const unsigned bits = GET_FSE_REC_LEN(STATE_VAR(name)); \
UInt64 r = v; \
v <<= bits; \
r >>= 1; \
UPDATE_BIT_OFFSET(bitOffset, bits) \
cond \
r >>= 63 ^ bits; \
FSE_Peek_Plus(name, r); \
}
#define FSE_UPDATE_STATES \
FSE_UPDATE_STATE_2 (ll, {} ) \
FSE_UPDATE_STATE_2 (ml, {} ) \
FSE_UPDATE_STATE_2 (of, BO_OVERFLOW_CHECK) \
#else // Z7_ZSTD_DEC_USE_64BIT_LOADS
// it supports 8 bits accuracy for any code
// it supports 9 bits accuracy, if (BIT_OFFSET_DELTA_BITS == 1)
#define FSE_UPDATE_STATE_0(name, cond) \
{ \
UInt32 r; \
const unsigned bits = GET_FSE_REC_LEN(STATE_VAR(name)); \
GET16(r, src + 2 + SRC_PLUS_FOR_4BYTES(bitOffset)) \
r >>= (bitOffset & 7); \
r &= (1 << (8 + BIT_OFFSET_DELTA_BITS)) - 1; \
UPDATE_BIT_OFFSET(bitOffset, bits) \
cond \
r >>= (8 + BIT_OFFSET_DELTA_BITS) - bits; \
FSE_Peek_Plus(name, r); \
}
// for debug (slow):
// #define Z7_ZSTD_DEC_USE_FSE_FUSION_FORCE
#if BIT_OFFSET_DELTA_BITS == 0 || defined(Z7_ZSTD_DEC_USE_FSE_FUSION_FORCE)
#define Z7_ZSTD_DEC_USE_FSE_FUSION
#endif
#ifdef Z7_ZSTD_DEC_USE_FSE_FUSION
#define FSE_UPDATE_STATE_1(name) \
{ UInt32 rest2; \
{ \
UInt32 r; \
unsigned bits; \
GET32(r, src + SRC_PLUS_FOR_4BYTES(bitOffset)) \
bits = GET_FSE_REC_LEN(STATE_VAR(name)); \
r <<= GET_SHIFT(bitOffset); \
rest2 = r << bits; \
r >>= 1; \
UPDATE_BIT_OFFSET(bitOffset, bits) \
r >>= 31 ^ bits; \
FSE_Peek_Plus(name, r); \
}
#define FSE_UPDATE_STATE_3(name) \
{ \
const unsigned bits = GET_FSE_REC_LEN(STATE_VAR(name)); \
rest2 >>= 1; \
UPDATE_BIT_OFFSET(bitOffset, bits) \
rest2 >>= 31 ^ bits; \
FSE_Peek_Plus(name, rest2); \
}}
#define FSE_UPDATE_STATES \
FSE_UPDATE_STATE_1 (ll) \
FSE_UPDATE_STATE_3 (ml) \
FSE_UPDATE_STATE_0 (of, BO_OVERFLOW_CHECK) \
#else // Z7_ZSTD_DEC_USE_64BIT_LOADS
#define FSE_UPDATE_STATES \
FSE_UPDATE_STATE_0 (ll, {} ) \
FSE_UPDATE_STATE_0 (ml, {} ) \
FSE_UPDATE_STATE_0 (of, BO_OVERFLOW_CHECK) \
#endif // Z7_ZSTD_DEC_USE_FSE_FUSION
#endif // Z7_ZSTD_DEC_USE_64BIT_LOADS
typedef struct
{
UInt32 numSeqs;
UInt32 literalsLen;
const Byte *literals;
}
CZstdDec1_Vars;
// if (BIT_OFFSET_DELTA_BITS != 0), we need (BIT_OFFSET_DELTA_BYTES > 0)
#define BIT_OFFSET_DELTA_BYTES BIT_OFFSET_DELTA_BITS
/* if (NUM_OFFSET_SYMBOLS_MAX == 32)
max_seq_bit_length = (31) + 16 + 16 + 9 + 8 + 9 = 89 bits
if defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF) we have longest backward
lookahead offset, and we read UInt64 after literal_len reading.
if (BIT_OFFSET_DELTA_BITS == 1 && NUM_OFFSET_SYMBOLS_MAX == 32)
MAX_BACKWARD_DEPTH = 16 bytes
*/
#define MAX_BACKWARD_DEPTH \
((NUM_OFFSET_SYMBOLS_MAX - 1 + 16 + 16 + 7) / 8 + 7 + BIT_OFFSET_DELTA_BYTES)
/* srcLen != 0
src == real_data_ptr - SEQ_SRC_OFFSET - BIT_OFFSET_DELTA_BYTES
if defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_ML) then
(winLimit - p->winPos <= (1 << 17)) is required
*/
static
Z7_NO_INLINE
// Z7_ATTRIB_NO_VECTOR
SRes Decompress_Sequences(CZstdDec1 * const p,
const Byte *src, const size_t srcLen,
const size_t winLimit,
const CZstdDec1_Vars * const vars)
{
#ifdef Z7_ZSTD_DEC_USE_BASES_LOCAL
SEQ_EXTRA_TABLES(a_)
#endif
// for debug:
// #define Z7_ZSTD_DEC_USE_LOCAL_FSE_TABLES
#ifdef Z7_ZSTD_DEC_USE_LOCAL_FSE_TABLES
#define FSE_TABLE(n) fse. n
const CZstdDecFseTables fse = p->fse;
/*
CZstdDecFseTables fse;
#define COPY_FSE_TABLE(n) \
memcpy(fse. n, p->fse. n, (size_t)4 << p-> n ## _accuracy);
COPY_FSE_TABLE(of)
COPY_FSE_TABLE(ll)
COPY_FSE_TABLE(ml)
*/
#else
#define FSE_TABLE(n) (p->fse. n)
#endif
#ifdef Z7_ZSTD_DEC_USE_BASES_LOCAL
FILL_LOC_BASES_ALL
#endif
{
unsigned numSeqs = vars->numSeqs;
const Byte *literals = vars->literals;
ptrdiff_t literalsLen = (ptrdiff_t)vars->literalsLen;
Byte * const win = p->win;
size_t winPos = p->winPos;
const size_t cycSize = p->cycSize;
size_t totalOutCheck = p->totalOutCheck;
const size_t winSize = p->winSize;
size_t reps_0 = p->reps[0];
size_t reps_1 = p->reps[1];
size_t reps_2 = p->reps[2];
UInt32 STATE_VAR(ll), STATE_VAR(of), STATE_VAR(ml);
CBitCtr bitOffset;
SET_bitOffset_TO_PAD (bitOffset, src + SEQ_SRC_OFFSET, srcLen + BIT_OFFSET_DELTA_BYTES)
bitOffset -= BIT_OFFSET_DELTA_BITS;
FSE_INIT_STATE(ll, {} )
FSE_INIT_STATE(of, {} )
FSE_INIT_STATE(ml, BO_OVERFLOW_CHECK)
for (;;)
{
size_t matchLen;
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
UInt64 v;
#endif
#ifdef Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF
FSE_PRELOAD
#endif
// if (of_code == 0)
if ((Byte)STATE_VAR(of) == 0)
{
if (GET_FSE_REC_SYM(STATE_VAR(ll)) == 0)
{
const size_t offset = reps_1;
reps_1 = reps_0;
reps_0 = offset;
STAT_INC(g_Num_Rep1)
}
STAT_UPDATE(else g_Num_Rep0++;)
}
else
{
const unsigned of_code = (Byte)STATE_VAR(of);
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
#if !defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF)
FSE_PRELOAD
#endif
#else
UInt32 v;
{
const Byte *src4 = src + SRC_PLUS_FOR_4BYTES(bitOffset);
const unsigned skip = GET_SHIFT(bitOffset);
GET32(v, src4)
v <<= skip;
v |= (UInt32)src4[-1] >> (8 - skip);
}
#endif
UPDATE_BIT_OFFSET(bitOffset, of_code)
if (of_code == 1)
{
// read 1 bit
#if defined(Z7_MSC_VER_ORIGINAL) || defined(MY_CPU_X86_OR_AMD64)
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
#define CHECK_HIGH_BIT_64(a) ((Int64)(UInt64)(a) < 0)
#else
#define CHECK_HIGH_BIT_32(a) ((Int32)(UInt32)(a) < 0)
#endif
#else
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
#define CHECK_HIGH_BIT_64(a) ((UInt64)(a) & ((UInt64)1 << 63))
#else
#define CHECK_HIGH_BIT_32(a) ((UInt32)(a) & ((UInt32)1 << 31))
#endif
#endif
if
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
CHECK_HIGH_BIT_64 (((UInt64)GET_FSE_REC_SYM(STATE_VAR(ll)) - 1) ^ v)
#else
CHECK_HIGH_BIT_32 (((UInt32)GET_FSE_REC_SYM(STATE_VAR(ll)) - 1) ^ v)
#endif
{
v <<= 1;
{
const size_t offset = reps_2;
reps_2 = reps_1;
reps_1 = reps_0;
reps_0 = offset;
STAT_INC(g_Num_Rep2)
}
}
else
{
if (GET_FSE_REC_SYM(STATE_VAR(ll)) == 0)
{
// litLen == 0 && bit == 1
STAT_INC(g_Num_Rep3)
v <<= 1;
reps_2 = reps_1;
reps_1 = reps_0;
if (--reps_0 == 0)
{
// LZ_LOOP_ERROR_EXIT
// original-zstd decoder : input is corrupted; force offset to 1
// reps_0 = 1;
reps_0++;
}
}
else
{
// litLen != 0 && bit == 0
v <<= 1;
{
const size_t offset = reps_1;
reps_1 = reps_0;
reps_0 = offset;
STAT_INC(g_Num_Rep1)
}
}
}
}
else
{
// (2 <= of_code)
// if (of_code >= 32) LZ_LOOP_ERROR_EXIT // optional check
// we don't allow (of_code >= 32) cases in another code
reps_2 = reps_1;
reps_1 = reps_0;
reps_0 = ((size_t)1 << of_code) - 3 + (size_t)
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
(v >> (64 - of_code));
v <<= of_code;
#else
(v >> (32 - of_code));
#endif
}
}
#ifdef Z7_ZSTD_DEC_USE_64BIT_PRELOAD_ML
FSE_PRELOAD
#endif
matchLen = (size_t)GET_FSE_REC_SYM(STATE_VAR(ml))
#ifndef Z7_ZSTD_DEC_USE_ML_PLUS3
+ MATCH_LEN_MIN
#endif
;
{
{
if (matchLen >= 32 + MATCH_LEN_MIN) // if (state_ml & 0x20)
{
const unsigned extra = BASES_TABLE(SEQ_ML_EXTRA) [(size_t)matchLen - MATCH_LEN_MIN];
matchLen = BASES_TABLE(SEQ_ML_BASES) [(size_t)matchLen - MATCH_LEN_MIN];
#if defined(Z7_ZSTD_DEC_USE_64BIT_LOADS) && \
(defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_ML) || \
defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF))
{
UPDATE_BIT_OFFSET(bitOffset, extra)
matchLen += (size_t)(v >> (64 - extra));
#if defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF)
FSE_PRELOAD
#else
v <<= extra;
#endif
}
#else
{
UInt32 v32;
STREAM_READ_BITS(v32, extra)
matchLen += v32;
}
#endif
STAT_INC(g_Num_Match)
}
}
}
#if defined(Z7_ZSTD_DEC_USE_64BIT_LOADS) && \
!defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF) && \
!defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_ML)
FSE_PRELOAD
#endif
{
size_t litLen = GET_FSE_REC_SYM(STATE_VAR(ll));
if (litLen)
{
// if (STATE_VAR(ll) & 0x70)
if (litLen >= 16)
{
const unsigned extra = BASES_TABLE(SEQ_LL_EXTRA) [litLen];
litLen = BASES_TABLE(SEQ_LL_BASES) [litLen];
#ifdef Z7_ZSTD_DEC_USE_64BIT_LOADS
{
UPDATE_BIT_OFFSET(bitOffset, extra)
litLen += (size_t)(v >> (64 - extra));
#if defined(Z7_ZSTD_DEC_USE_64BIT_PRELOAD_OF)
FSE_PRELOAD
#else
v <<= extra;
#endif
}
#else
{
UInt32 v32;
STREAM_READ_BITS(v32, extra)
litLen += v32;
}
#endif
STAT_INC(g_Num_LitsBig)
}
if ((literalsLen -= (ptrdiff_t)litLen) < 0)
LZ_LOOP_ERROR_EXIT
totalOutCheck += litLen;
{
const size_t rem = winLimit - winPos;
if (litLen > rem)
LZ_LOOP_ERROR_EXIT
{
const Byte *literals_temp = literals;
Byte *d = win + winPos;
literals += litLen;
winPos += litLen;
CopyLiterals(d, literals_temp, litLen, rem);
}
}
}
STAT_UPDATE(else g_Num_Lit0++;)
}
#define COPY_MATCH \
{ if (reps_0 > winSize || reps_0 > totalOutCheck) LZ_LOOP_ERROR_EXIT \
totalOutCheck += matchLen; \
{ const size_t rem = winLimit - winPos; \
if (matchLen > rem) LZ_LOOP_ERROR_EXIT \
{ const size_t winPos_temp = winPos; \
winPos += matchLen; \
CopyMatch(reps_0, matchLen, win, winPos_temp, rem, cycSize); }}}
if (--numSeqs == 0)
{
COPY_MATCH
break;
}
FSE_UPDATE_STATES
COPY_MATCH
} // for
if ((CBitCtr_signed)bitOffset != BIT_OFFSET_DELTA_BYTES * 8 - BIT_OFFSET_DELTA_BITS)
return SZ_ERROR_DATA;
if (literalsLen)
{
const size_t rem = winLimit - winPos;
if ((size_t)literalsLen > rem)
return SZ_ERROR_DATA;
{
Byte *d = win + winPos;
winPos += (size_t)literalsLen;
totalOutCheck += (size_t)literalsLen;
CopyLiterals
// memcpy
(d, literals, (size_t)literalsLen, rem);
}
}
if (totalOutCheck >= winSize)
totalOutCheck = winSize;
p->totalOutCheck = totalOutCheck;
p->winPos = winPos;
p->reps[0] = (CZstdDecOffset)reps_0;
p->reps[1] = (CZstdDecOffset)reps_1;
p->reps[2] = (CZstdDecOffset)reps_2;
}
return SZ_OK;
}
// for debug: define to check that ZstdDec1_NeedTempBufferForInput() works correctly:
// #define Z7_ZSTD_DEC_USE_CHECK_OF_NEED_TEMP // define it for debug only
#ifdef Z7_ZSTD_DEC_USE_CHECK_OF_NEED_TEMP
static unsigned g_numSeqs;
#endif
#define k_LitBlockType_Flag_RLE_or_Treeless 1
#define k_LitBlockType_Flag_Compressed 2
// outLimit : is strong limit
// outLimit <= ZstdDec1_GET_BLOCK_SIZE_LIMIT(p)
// inSize != 0
static
Z7_NO_INLINE
SRes ZstdDec1_DecodeBlock(CZstdDec1 *p,
const Byte *src, SizeT inSize, SizeT afterAvail,
const size_t outLimit)
{
CZstdDec1_Vars vars;
vars.literals = p->literalsBase;
{
const unsigned b0 = *src++;
UInt32 numLits, compressedSize;
const Byte *litStream;
Byte *literalsDest;
inSize--;
if ((b0 & k_LitBlockType_Flag_Compressed) == 0)
{
// we need at least one additional byte for (numSeqs).
// so we check for that additional byte in conditions.
numLits = b0 >> 3;
if (b0 & 4)
{
UInt32 v;
if (inSize < 1 + 1) // we need at least 1 byte here and 1 byte for (numSeqs).
return SZ_ERROR_DATA;
numLits >>= 1;
v = GetUi16(src);
src += 2;
inSize -= 2;
if ((b0 & 8) == 0)
{
src--;
inSize++;
v = (Byte)v;
}
numLits += v << 4;
}
compressedSize = 1;
if ((b0 & k_LitBlockType_Flag_RLE_or_Treeless) == 0)
compressedSize = numLits;
}
else if (inSize < 4)
return SZ_ERROR_DATA;
else
{
const unsigned mode4Streams = b0 & 0xc;
const unsigned numBytes = (3 * mode4Streams + 32) >> 4;
const unsigned numBits = 4 * numBytes - 2;
const UInt32 mask = ((UInt32)16 << numBits) - 1;
compressedSize = GetUi32(src);
numLits = ((
#ifdef MY_CPU_LE_UNALIGN
GetUi32(src - 1)
#else
((compressedSize << 8) + b0)
#endif
) >> 4) & mask;
src += numBytes;
inSize -= numBytes;
compressedSize >>= numBits;
compressedSize &= mask;
/*
if (numLits != 0) printf("inSize = %7u num_lits=%7u compressed=%7u ratio = %u ratio2 = %u\n",
i1, numLits, (unsigned)compressedSize * 1, (unsigned)compressedSize * 100 / numLits,
(unsigned)numLits * 100 / (unsigned)inSize);
}
*/
if (compressedSize == 0)
return SZ_ERROR_DATA; // (compressedSize == 0) is not allowed
}
STAT_UPDATE(g_Num_Lits += numLits;)
vars.literalsLen = numLits;
if (compressedSize >= inSize)
return SZ_ERROR_DATA;
litStream = src;
src += compressedSize;
inSize -= compressedSize;
// inSize != 0
{
UInt32 numSeqs = *src++;
inSize--;
if (numSeqs > 127)
{
UInt32 b1;
if (inSize == 0)
return SZ_ERROR_DATA;
numSeqs -= 128;
b1 = *src++;
inSize--;
if (numSeqs == 127)
{
if (inSize == 0)
return SZ_ERROR_DATA;
numSeqs = (UInt32)(*src++) + 127;
inSize--;
}
numSeqs = (numSeqs << 8) + b1;
}
if (numSeqs * MATCH_LEN_MIN + numLits > outLimit)
return SZ_ERROR_DATA;
vars.numSeqs = numSeqs;
STAT_UPDATE(g_NumSeqs_total += numSeqs;)
/*
#ifdef SHOW_STAT
printf("\n %5u : %8u, %8u : %5u", (int)g_Num_Blocks_Compressed, (int)numSeqs, (int)g_NumSeqs_total,
(int)g_NumSeqs_total / g_Num_Blocks_Compressed);
#endif
// printf("\nnumSeqs2 = %d", numSeqs);
*/
#ifdef Z7_ZSTD_DEC_USE_CHECK_OF_NEED_TEMP
if (numSeqs != g_numSeqs) return SZ_ERROR_DATA; // for debug
#endif
if (numSeqs == 0)
{
if (inSize != 0)
return SZ_ERROR_DATA;
literalsDest = p->win + p->winPos;
}
else
literalsDest = p->literalsBase;
}
if ((b0 & k_LitBlockType_Flag_Compressed) == 0)
{
if (b0 & k_LitBlockType_Flag_RLE_or_Treeless)
{
memset(literalsDest, litStream[0], numLits);
if (vars.numSeqs)
{
// literalsDest == p->literalsBase == vars.literals
#if COPY_CHUNK_SIZE > 1
memset(p->literalsBase + numLits, 0, COPY_CHUNK_SIZE);
#endif
}
}
else
{
// unsigned y;
// for (y = 0; y < 10000; y++)
memcpy(literalsDest, litStream, numLits);
if (vars.numSeqs)
{
/* we need up to (15 == COPY_CHUNK_SIZE - 1) space for optimized CopyLiterals().
If we have additional space in input stream after literals stream,
we use direct copy of rar literals in input stream */
if ((size_t)(src + inSize - litStream) - numLits + afterAvail >= (COPY_CHUNK_SIZE - 1))
vars.literals = litStream;
else
{
// literalsDest == p->literalsBase == vars.literals
#if COPY_CHUNK_SIZE > 1
/* CopyLiterals():
1) we don't want reading non-initialized data
2) we will copy only zero byte after literals buffer */
memset(p->literalsBase + numLits, 0, COPY_CHUNK_SIZE);
#endif
}
}
}
}
else
{
CInBufPair hufStream;
hufStream.ptr = litStream;
hufStream.len = compressedSize;
if ((b0 & k_LitBlockType_Flag_RLE_or_Treeless) == 0)
{
// unsigned y = 100; CInBufPair hs2 = hufStream; do { hufStream = hs2;
RINOK(Huf_DecodeTable(&p->huf, &hufStream))
p->litHuf_wasSet = True;
// } while (--y);
}
else if (!p->litHuf_wasSet)
return SZ_ERROR_DATA;
{
// int yyy; for (yyy = 0; yyy < 34; yyy++) {
SRes sres;
if ((b0 & 0xc) == 0) // mode4Streams
sres = Huf_Decompress_1stream((const Byte *)(const void *)p->huf.table64,
hufStream.ptr - HUF_SRC_OFFSET, hufStream.len, literalsDest, numLits);
else
{
// 6 bytes for the jump table + 4x1 bytes of end-padding Bytes)
if (hufStream.len < 6 + 4)
return SZ_ERROR_DATA;
// the condition from original-zstd decoder:
#define Z7_ZSTD_MIN_LITERALS_FOR_4_STREAMS 6
if (numLits < Z7_ZSTD_MIN_LITERALS_FOR_4_STREAMS)
return SZ_ERROR_DATA;
sres = Huf_Decompress_4stream((const Byte *)(const void *)p->huf.table64,
hufStream.ptr + (6 - HUF_SRC_OFFSET), hufStream.len, literalsDest, numLits);
}
RINOK(sres)
// }
}
}
if (vars.numSeqs == 0)
{
p->winPos += numLits;
UPDATE_TOTAL_OUT(p, numLits)
return SZ_OK;
}
}
{
CInBufPair in;
unsigned mode;
unsigned seqMode;
in.ptr = src;
in.len = inSize;
if (in.len == 0)
return SZ_ERROR_DATA;
in.len--;
mode = *in.ptr++;
if (mode & 3) // Reserved bits
return SZ_ERROR_DATA;
seqMode = (mode >> 6);
if (seqMode == k_SeqMode_Repeat)
{ if (!IS_SEQ_TABLES_WERE_SET(p)) return SZ_ERROR_DATA; }
else RINOK(FSE_Decode_SeqTable(
p->fse.ll,
&in,
6, // predefAccuracy
&p->ll_accuracy,
NUM_LL_SYMBOLS,
k_PredefRecords_LL,
seqMode))
seqMode = (mode >> 4) & 3;
if (seqMode == k_SeqMode_Repeat)
{ if (!IS_SEQ_TABLES_WERE_SET(p)) return SZ_ERROR_DATA; }
else RINOK(FSE_Decode_SeqTable(
p->fse.of,
&in,
5, // predefAccuracy
&p->of_accuracy,
NUM_OFFSET_SYMBOLS_MAX,
k_PredefRecords_OF,
seqMode))
seqMode = (mode >> 2) & 3;
if (seqMode == k_SeqMode_Repeat)
{ if (!IS_SEQ_TABLES_WERE_SET(p)) return SZ_ERROR_DATA; }
else
{
RINOK(FSE_Decode_SeqTable(
p->fse.ml,
&in,
6, // predefAccuracy
&p->ml_accuracy,
NUM_ML_SYMBOLS,
k_PredefRecords_ML,
seqMode))
/*
#if defined(Z7_ZSTD_DEC_USE_ML_PLUS3)
// { unsigned y = 1 << 10; do
{
const unsigned accuracy = p->ml_accuracy;
if (accuracy == 0)
p->fse.ml[0] += 3;
else
#ifdef MY_CPU_64BIT
{
// alignemt (UInt64 _pad_Alignment) in fse.ml is required for that code
UInt64 *table = (UInt64 *)(void *)p->fse.ml;
const UInt64 *end = (const UInt64 *)(const void *)
((const Byte *)(const void *)table + ((size_t)sizeof(CFseRecord) << accuracy));
do
{
table[0] += ((UInt64)MATCH_LEN_MIN << 32) + MATCH_LEN_MIN;
table[1] += ((UInt64)MATCH_LEN_MIN << 32) + MATCH_LEN_MIN;
table += 2;
}
while (table != end);
}
#else
{
UInt32 *table = p->fse.ml;
const UInt32 *end = (const UInt32 *)(const void *)
((const Byte *)(const void *)table + ((size_t)sizeof(CFseRecord) << accuracy));
do
{
table[0] += MATCH_LEN_MIN;
table[1] += MATCH_LEN_MIN;
table += 2;
table[0] += MATCH_LEN_MIN;
table[1] += MATCH_LEN_MIN;
table += 2;
}
while (table != end);
}
#endif
}
// while (--y); }
#endif
*/
}
// p->seqTables_wereSet = True;
if (in.len == 0)
return SZ_ERROR_DATA;
return Decompress_Sequences(p,
in.ptr - SEQ_SRC_OFFSET - BIT_OFFSET_DELTA_BYTES, in.len,
p->winPos + outLimit, &vars);
}
}
// inSize != 0
// it must do similar to ZstdDec1_DecodeBlock()
static size_t ZstdDec1_NeedTempBufferForInput(
const SizeT beforeSize, const Byte * const src, const SizeT inSize)
{
unsigned b0;
UInt32 pos;
#ifdef Z7_ZSTD_DEC_USE_CHECK_OF_NEED_TEMP
g_numSeqs = 1 << 24;
#else
// we have at least 3 bytes before seq data: litBlockType, numSeqs, seqMode
#define MIN_BLOCK_LZ_HEADERS_SIZE 3
if (beforeSize >= MAX_BACKWARD_DEPTH - MIN_BLOCK_LZ_HEADERS_SIZE)
return 0;
#endif
b0 = src[0];
if ((b0 & k_LitBlockType_Flag_Compressed) == 0)
{
UInt32 numLits = b0 >> 3;
pos = 1;
if (b0 & 4)
{
UInt32 v;
if (inSize < 3)
return 0;
numLits >>= 1;
v = GetUi16(src + 1);
pos = 3;
if ((b0 & 8) == 0)
{
pos = 2;
v = (Byte)v;
}
numLits += v << 4;
}
if (b0 & k_LitBlockType_Flag_RLE_or_Treeless)
numLits = 1;
pos += numLits;
}
else if (inSize < 5)
return 0;
else
{
const unsigned mode4Streams = b0 & 0xc;
const unsigned numBytes = (3 * mode4Streams + 48) >> 4;
const unsigned numBits = 4 * numBytes - 6;
UInt32 cs = GetUi32(src + 1);
cs >>= numBits;
cs &= ((UInt32)16 << numBits) - 1;
if (cs == 0)
return 0;
pos = numBytes + cs;
}
if (pos >= inSize)
return 0;
{
UInt32 numSeqs = src[pos++];
if (numSeqs > 127)
{
UInt32 b1;
if (pos >= inSize)
return 0;
numSeqs -= 128;
b1 = src[pos++];
if (numSeqs == 127)
{
if (pos >= inSize)
return 0;
numSeqs = (UInt32)(src[pos++]) + 127;
}
numSeqs = (numSeqs << 8) + b1;
}
#ifdef Z7_ZSTD_DEC_USE_CHECK_OF_NEED_TEMP
g_numSeqs = numSeqs; // for debug
#endif
if (numSeqs == 0)
return 0;
}
/*
if (pos >= inSize)
return 0;
pos++;
*/
// we will have one additional byte for seqMode:
if (beforeSize + pos >= MAX_BACKWARD_DEPTH - 1)
return 0;
return 1;
}
// ---------- ZSTD FRAME ----------
#define kBlockType_Raw 0
#define kBlockType_RLE 1
#define kBlockType_Compressed 2
#define kBlockType_Reserved 3
typedef enum
{
// begin: states that require 4 bytes:
ZSTD2_STATE_SIGNATURE,
ZSTD2_STATE_HASH,
ZSTD2_STATE_SKIP_HEADER,
// end of states that require 4 bytes
ZSTD2_STATE_SKIP_DATA,
ZSTD2_STATE_FRAME_HEADER,
ZSTD2_STATE_AFTER_HEADER,
ZSTD2_STATE_BLOCK,
ZSTD2_STATE_DATA,
ZSTD2_STATE_FINISHED
} EZstd2State;
struct CZstdDec
{
EZstd2State frameState;
unsigned tempSize;
Byte temp[14]; // 14 is required
Byte descriptor;
Byte windowDescriptor;
Byte isLastBlock;
Byte blockType;
Byte isErrorState;
Byte hashError;
Byte disableHash;
Byte isCyclicMode;
UInt32 blockSize;
UInt32 dictionaryId;
UInt32 curBlockUnpackRem; // for compressed blocks only
UInt32 inTempPos;
UInt64 contentSize;
UInt64 contentProcessed;
CXxh64State xxh64;
Byte *inTemp;
SizeT winBufSize_Allocated;
Byte *win_Base;
ISzAllocPtr alloc_Small;
ISzAllocPtr alloc_Big;
CZstdDec1 decoder;
};
#define ZstdDec_GET_UNPROCESSED_XXH64_SIZE(p) \
((unsigned)(p)->contentProcessed & (Z7_XXH64_BLOCK_SIZE - 1))
#define ZSTD_DEC_IS_LAST_BLOCK(p) ((p)->isLastBlock)
static void ZstdDec_FreeWindow(CZstdDec * const p)
{
if (p->win_Base)
{
ISzAlloc_Free(p->alloc_Big, p->win_Base);
p->win_Base = NULL;
// p->decoder.win = NULL;
p->winBufSize_Allocated = 0;
}
}
CZstdDecHandle ZstdDec_Create(ISzAllocPtr alloc_Small, ISzAllocPtr alloc_Big)
{
CZstdDec *p = (CZstdDec *)ISzAlloc_Alloc(alloc_Small, sizeof(CZstdDec));
if (!p)
return NULL;
p->alloc_Small = alloc_Small;
p->alloc_Big = alloc_Big;
// ZstdDec_CONSTRUCT(p)
p->inTemp = NULL;
p->win_Base = NULL;
p->winBufSize_Allocated = 0;
p->disableHash = False;
ZstdDec1_Construct(&p->decoder);
return p;
}
void ZstdDec_Destroy(CZstdDecHandle p)
{
#ifdef SHOW_STAT
#define PRINT_STAT1(name, v) \
printf("\n%25s = %9u", name, v);
PRINT_STAT1("g_Num_Blocks_Compressed", g_Num_Blocks_Compressed)
PRINT_STAT1("g_Num_Blocks_memcpy", g_Num_Blocks_memcpy)
PRINT_STAT1("g_Num_Wrap_memmove_Num", g_Num_Wrap_memmove_Num)
PRINT_STAT1("g_Num_Wrap_memmove_Bytes", g_Num_Wrap_memmove_Bytes)
if (g_Num_Blocks_Compressed)
{
#define PRINT_STAT(name, v) \
printf("\n%17s = %9u, per_block = %8u", name, v, v / g_Num_Blocks_Compressed);
PRINT_STAT("g_NumSeqs", g_NumSeqs_total)
// PRINT_STAT("g_NumCopy", g_NumCopy)
PRINT_STAT("g_NumOver", g_NumOver)
PRINT_STAT("g_NumOver2", g_NumOver2)
PRINT_STAT("g_Num_Match", g_Num_Match)
PRINT_STAT("g_Num_Lits", g_Num_Lits)
PRINT_STAT("g_Num_LitsBig", g_Num_LitsBig)
PRINT_STAT("g_Num_Lit0", g_Num_Lit0)
PRINT_STAT("g_Num_Rep_0", g_Num_Rep0)
PRINT_STAT("g_Num_Rep_1", g_Num_Rep1)
PRINT_STAT("g_Num_Rep_2", g_Num_Rep2)
PRINT_STAT("g_Num_Rep_3", g_Num_Rep3)
PRINT_STAT("g_Num_Threshold_0", g_Num_Threshold_0)
PRINT_STAT("g_Num_Threshold_1", g_Num_Threshold_1)
PRINT_STAT("g_Num_Threshold_0sum", g_Num_Threshold_0sum)
PRINT_STAT("g_Num_Threshold_1sum", g_Num_Threshold_1sum)
}
printf("\n");
#endif
ISzAlloc_Free(p->alloc_Small, p->decoder.literalsBase);
// p->->decoder.literalsBase = NULL;
ISzAlloc_Free(p->alloc_Small, p->inTemp);
// p->inTemp = NULL;
ZstdDec_FreeWindow(p);
ISzAlloc_Free(p->alloc_Small, p);
}
#define kTempBuffer_PreSize (1u << 6)
#if kTempBuffer_PreSize < MAX_BACKWARD_DEPTH
#error Stop_Compiling_Bad_kTempBuffer_PreSize
#endif
static SRes ZstdDec_AllocateMisc(CZstdDec *p)
{
#define k_Lit_AfterAvail (1u << 6)
#if k_Lit_AfterAvail < (COPY_CHUNK_SIZE - 1)
#error Stop_Compiling_Bad_k_Lit_AfterAvail
#endif
// return ZstdDec1_Allocate(&p->decoder, p->alloc_Small);
if (!p->decoder.literalsBase)
{
p->decoder.literalsBase = (Byte *)ISzAlloc_Alloc(p->alloc_Small,
kBlockSizeMax + k_Lit_AfterAvail);
if (!p->decoder.literalsBase)
return SZ_ERROR_MEM;
}
if (!p->inTemp)
{
// we need k_Lit_AfterAvail here for owerread from raw literals stream
p->inTemp = (Byte *)ISzAlloc_Alloc(p->alloc_Small,
kBlockSizeMax + kTempBuffer_PreSize + k_Lit_AfterAvail);
if (!p->inTemp)
return SZ_ERROR_MEM;
}
return SZ_OK;
}
static void ZstdDec_Init_ForNewFrame(CZstdDec *p)
{
p->frameState = ZSTD2_STATE_SIGNATURE;
p->tempSize = 0;
p->isErrorState = False;
p->hashError = False;
p->isCyclicMode = False;
p->contentProcessed = 0;
Xxh64State_Init(&p->xxh64);
ZstdDec1_Init(&p->decoder);
}
void ZstdDec_Init(CZstdDec *p)
{
ZstdDec_Init_ForNewFrame(p);
p->decoder.winPos = 0;
memset(p->temp, 0, sizeof(p->temp));
}
#define DESCRIPTOR_Get_DictionaryId_Flag(d) ((d) & 3)
#define DESCRIPTOR_FLAG_CHECKSUM (1 << 2)
#define DESCRIPTOR_FLAG_RESERVED (1 << 3)
// #define DESCRIPTOR_FLAG_UNUSED (1 << 4)
#define DESCRIPTOR_FLAG_SINGLE (1 << 5)
#define DESCRIPTOR_Get_ContentSize_Flag3(d) ((d) >> 5)
#define DESCRIPTOR_Is_ContentSize_Defined(d) (((d) & 0xe0) != 0)
static EZstd2State ZstdDec_UpdateState(CZstdDec * const p, const Byte b, CZstdDecInfo * const info)
{
unsigned tempSize = p->tempSize;
p->temp[tempSize++] = b;
p->tempSize = tempSize;
if (p->frameState == ZSTD2_STATE_BLOCK)
{
if (tempSize < 3)
return ZSTD2_STATE_BLOCK;
{
UInt32 b0 = GetUi32(p->temp);
const unsigned type = ((unsigned)b0 >> 1) & 3;
if (type == kBlockType_RLE && tempSize == 3)
return ZSTD2_STATE_BLOCK;
// info->num_Blocks_forType[type]++;
info->num_Blocks++;
if (type == kBlockType_Reserved)
{
p->isErrorState = True; // SZ_ERROR_UNSUPPORTED
return ZSTD2_STATE_BLOCK;
}
p->blockType = (Byte)type;
p->isLastBlock = (Byte)(b0 & 1);
p->inTempPos = 0;
p->tempSize = 0;
b0 >>= 3;
b0 &= 0x1fffff;
// info->num_BlockBytes_forType[type] += b0;
if (b0 == 0)
{
// empty RAW/RLE blocks are allowed in original-zstd decoder
if (type == kBlockType_Compressed)
{
p->isErrorState = True;
return ZSTD2_STATE_BLOCK;
}
if (!ZSTD_DEC_IS_LAST_BLOCK(p))
return ZSTD2_STATE_BLOCK;
if (p->descriptor & DESCRIPTOR_FLAG_CHECKSUM)
return ZSTD2_STATE_HASH;
return ZSTD2_STATE_FINISHED;
}
p->blockSize = b0;
{
UInt32 blockLim = ZstdDec1_GET_BLOCK_SIZE_LIMIT(&p->decoder);
// compressed and uncompressed block sizes cannot be larger than min(kBlockSizeMax, window_size)
if (b0 > blockLim)
{
p->isErrorState = True; // SZ_ERROR_UNSUPPORTED;
return ZSTD2_STATE_BLOCK;
}
if (DESCRIPTOR_Is_ContentSize_Defined(p->descriptor))
{
const UInt64 rem = p->contentSize - p->contentProcessed;
if (blockLim > rem)
blockLim = (UInt32)rem;
}
p->curBlockUnpackRem = blockLim;
// uncompressed block size cannot be larger than remain data size:
if (type != kBlockType_Compressed)
{
if (b0 > blockLim)
{
p->isErrorState = True; // SZ_ERROR_UNSUPPORTED;
return ZSTD2_STATE_BLOCK;
}
}
}
}
return ZSTD2_STATE_DATA;
}
if ((unsigned)p->frameState < ZSTD2_STATE_SKIP_DATA)
{
UInt32 v;
if (tempSize != 4)
return p->frameState;
v = GetUi32(p->temp);
if ((unsigned)p->frameState < ZSTD2_STATE_HASH) // == ZSTD2_STATE_SIGNATURE
{
if (v == 0xfd2fb528)
{
p->tempSize = 0;
info->num_DataFrames++;
return ZSTD2_STATE_FRAME_HEADER;
}
if ((v & 0xfffffff0) == 0x184d2a50)
{
p->tempSize = 0;
info->num_SkipFrames++;
return ZSTD2_STATE_SKIP_HEADER;
}
p->isErrorState = True;
return ZSTD2_STATE_SIGNATURE;
// return ZSTD2_STATE_ERROR; // is not ZSTD stream
}
if (p->frameState == ZSTD2_STATE_HASH)
{
info->checksum_Defined = True;
info->checksum = v;
// #ifndef DISABLE_XXH_CHECK
if (!p->disableHash)
{
if (p->decoder.winPos < ZstdDec_GET_UNPROCESSED_XXH64_SIZE(p))
{
// unexpected code failure
p->isErrorState = True;
// SZ_ERROR_FAIL;
}
else
if ((UInt32)Xxh64State_Digest(&p->xxh64,
p->decoder.win + (p->decoder.winPos - ZstdDec_GET_UNPROCESSED_XXH64_SIZE(p)),
p->contentProcessed) != v)
{
p->hashError = True;
// return ZSTD2_STATE_ERROR; // hash error
}
}
// #endif
return ZSTD2_STATE_FINISHED;
}
// (p->frameState == ZSTD2_STATE_SKIP_HEADER)
{
p->blockSize = v;
info->skipFrames_Size += v;
p->tempSize = 0;
/* we want the caller could know that there was finished frame
finished frame. So we allow the case where
we have ZSTD2_STATE_SKIP_DATA state with (blockSize == 0).
*/
// if (v == 0) return ZSTD2_STATE_SIGNATURE;
return ZSTD2_STATE_SKIP_DATA;
}
}
// if (p->frameState == ZSTD2_STATE_FRAME_HEADER)
{
unsigned descriptor;
const Byte *h;
descriptor = p->temp[0];
p->descriptor = (Byte)descriptor;
if (descriptor & DESCRIPTOR_FLAG_RESERVED) // reserved bit
{
p->isErrorState = True;
return ZSTD2_STATE_FRAME_HEADER;
// return ZSTD2_STATE_ERROR;
}
{
const unsigned n = DESCRIPTOR_Get_ContentSize_Flag3(descriptor);
// tempSize -= 1 + ((1u << (n >> 1)) | ((n + 1) & 1));
tempSize -= (0x9a563422u >> (n * 4)) & 0xf;
}
if (tempSize != (4u >> (3 - DESCRIPTOR_Get_DictionaryId_Flag(descriptor))))
return ZSTD2_STATE_FRAME_HEADER;
info->descriptor_OR = (Byte)(info->descriptor_OR | descriptor);
info->descriptor_NOT_OR = (Byte)(info->descriptor_NOT_OR | ~descriptor);
h = &p->temp[1];
{
Byte w = 0;
if ((descriptor & DESCRIPTOR_FLAG_SINGLE) == 0)
{
w = *h++;
if (info->windowDescriptor_MAX < w)
info->windowDescriptor_MAX = w;
// info->are_WindowDescriptors = True;
// info->num_WindowDescriptors++;
}
else
{
// info->are_SingleSegments = True;
// info->num_SingleSegments++;
}
p->windowDescriptor = w;
}
{
unsigned n = DESCRIPTOR_Get_DictionaryId_Flag(descriptor);
UInt32 d = 0;
if (n)
{
n = 1u << (n - 1);
d = GetUi32(h) & ((UInt32)(Int32)-1 >> (32 - 8u * n));
h += n;
}
p->dictionaryId = d;
// info->dictionaryId_Cur = d;
if (d != 0)
{
if (info->dictionaryId == 0)
info->dictionaryId = d;
else if (info->dictionaryId != d)
info->are_DictionaryId_Different = True;
}
}
{
unsigned n = DESCRIPTOR_Get_ContentSize_Flag3(descriptor);
UInt64 v = 0;
if (n)
{
n >>= 1;
if (n == 1)
v = 256;
v += GetUi64(h) & ((UInt64)(Int64)-1 >> (64 - (8u << n)));
// info->are_ContentSize_Known = True;
// info->num_Frames_with_ContentSize++;
if (info->contentSize_MAX < v)
info->contentSize_MAX = v;
info->contentSize_Total += v;
}
else
{
info->are_ContentSize_Unknown = True;
// info->num_Frames_without_ContentSize++;
}
p->contentSize = v;
}
// if ((size_t)(h - p->temp) != headerSize) return ZSTD2_STATE_ERROR; // it's unexpected internal code failure
p->tempSize = 0;
info->checksum_Defined = False;
/*
if (descriptor & DESCRIPTOR_FLAG_CHECKSUM)
info->are_Checksums = True;
else
info->are_Non_Checksums = True;
*/
return ZSTD2_STATE_AFTER_HEADER; // ZSTD2_STATE_BLOCK;
}
}
static void ZstdDec_Update_XXH(CZstdDec * const p, size_t xxh64_winPos)
{
/*
#ifdef DISABLE_XXH_CHECK
UNUSED_VAR(data)
#else
*/
if (!p->disableHash && (p->descriptor & DESCRIPTOR_FLAG_CHECKSUM))
{
// const size_t pos = p->xxh64_winPos;
const size_t size = (p->decoder.winPos - xxh64_winPos) & ~(size_t)31;
if (size)
{
// p->xxh64_winPos = pos + size;
Xxh64State_UpdateBlocks(&p->xxh64,
p->decoder.win + xxh64_winPos,
p->decoder.win + xxh64_winPos + size);
}
}
}
/*
in:
(winLimit) : is relaxed limit, where this function is allowed to stop writing of decoded data (if possible).
- this function uses (winLimit) for RAW/RLE blocks only,
because this function can decode single RAW/RLE block in several different calls.
- this function DOESN'T use (winLimit) for Compressed blocks,
because this function decodes full compressed block in single call.
(CZstdDec1::winPos <= winLimit)
(winLimit <= CZstdDec1::cycSize).
Note: if (ds->outBuf_fromCaller) mode is used, then
{
(strong_limit) is stored in CZstdDec1::cycSize.
So (winLimit) is more strong than (strong_limit).
}
exit:
Note: (CZstdDecState::winPos) will be set by caller after exit of this function.
This function can exit for any of these conditions:
- (frameState == ZSTD2_STATE_AFTER_HEADER)
- (frameState == ZSTD2_STATE_FINISHED) : frame was finished : (status == ZSTD_STATUS_FINISHED_FRAME) is set
- finished non-empty non-last block. So (CZstdDec1::winPos_atExit != winPos_atFuncStart).
- ZSTD_STATUS_NEEDS_MORE_INPUT in src
- (CZstdDec1::winPos) have reached (winLimit) in non-finished RAW/RLE block
This function decodes no more than one non-empty block.
So it fulfills the condition at exit:
(CZstdDec1::winPos_atExit - winPos_atFuncStart <= block_size_max)
Note: (winPos_atExit > winLimit) is possible in some cases after compressed block decoding.
if (ds->outBuf_fromCaller) mode (useAdditionalWinLimit medo)
{
then this function uses additional strong limit from (CZstdDec1::cycSize).
So this function will not write any data after (CZstdDec1::cycSize)
And it fulfills the condition at exit:
(CZstdDec1::winPos_atExit <= CZstdDec1::cycSize)
}
*/
static SRes ZstdDec_DecodeBlock(CZstdDec * const p, CZstdDecState * const ds,
SizeT winLimitAdd)
{
const Byte *src = ds->inBuf;
SizeT * const srcLen = &ds->inPos;
const SizeT inSize = ds->inLim;
// const int useAdditionalWinLimit = ds->outBuf_fromCaller ? 1 : 0;
enum_ZstdStatus * const status = &ds->status;
CZstdDecInfo * const info = &ds->info;
SizeT winLimit;
const SizeT winPos_atFuncStart = p->decoder.winPos;
src += *srcLen;
*status = ZSTD_STATUS_NOT_SPECIFIED;
// finishMode = ZSTD_FINISH_ANY;
if (ds->outSize_Defined)
{
if (ds->outSize < ds->outProcessed)
{
// p->isAfterSizeMode = 2; // we have extra bytes already
*status = ZSTD_STATUS_OUT_REACHED;
return SZ_OK;
// size = 0;
}
else
{
// p->outSize >= p->outProcessed
const UInt64 rem = ds->outSize - ds->outProcessed;
/*
if (rem == 0)
p->isAfterSizeMode = 1; // we have reached exact required size
*/
if (winLimitAdd >= rem)
{
winLimitAdd = (SizeT)rem;
// if (p->finishMode) finishMode = ZSTD_FINISH_END;
}
}
}
winLimit = p->decoder.winPos + winLimitAdd;
// (p->decoder.winPos <= winLimit)
// while (p->frameState != ZSTD2_STATE_ERROR)
while (!p->isErrorState)
{
SizeT inCur = inSize - *srcLen;
if (p->frameState == ZSTD2_STATE_DATA)
{
/* (p->decoder.winPos == winPos_atFuncStart) is expected,
because this function doesn't start new block.
if it have finished some non-empty block in this call. */
if (p->decoder.winPos != winPos_atFuncStart)
return SZ_ERROR_FAIL; // it's unexpected
/*
if (p->decoder.winPos > winLimit)
{
// we can be here, if in this function call
// - we have extracted non-empty compressed block, and (winPos > winLimit) after that.
// - we have started new block decoding after that.
// It's unexpected case, because we exit after non-empty non-last block.
*status = (inSize == *srcLen) ?
ZSTD_STATUS_NEEDS_MORE_INPUT :
ZSTD_STATUS_NOT_FINISHED;
return SZ_OK;
}
*/
// p->decoder.winPos <= winLimit
if (p->blockType != kBlockType_Compressed)
{
// it's RLE or RAW block.
// p->BlockSize != 0_
// winLimit <= p->decoder.cycSize
/* So here we use more strong (winLimit), even for
(ds->outBuf_fromCaller) mode. */
SizeT outCur = winLimit - p->decoder.winPos;
{
const UInt32 rem = p->blockSize;
if (outCur > rem)
outCur = rem;
}
if (p->blockType == kBlockType_Raw)
{
if (outCur > inCur)
outCur = inCur;
/* output buffer is better aligned for XXH code.
So we use hash for output buffer data */
// ZstdDec_Update_XXH(p, src, outCur); // for debug:
memcpy(p->decoder.win + p->decoder.winPos, src, outCur);
src += outCur;
*srcLen += outCur;
}
else // kBlockType_RLE
{
#define RLE_BYTE_INDEX_IN_temp 3
memset(p->decoder.win + p->decoder.winPos,
p->temp[RLE_BYTE_INDEX_IN_temp], outCur);
}
{
const SizeT xxh64_winPos = p->decoder.winPos - ZstdDec_GET_UNPROCESSED_XXH64_SIZE(p);
p->decoder.winPos += outCur;
UPDATE_TOTAL_OUT(&p->decoder, outCur)
p->contentProcessed += outCur;
ZstdDec_Update_XXH(p, xxh64_winPos);
}
// ds->winPos = p->decoder.winPos; // the caller does it instead. for debug:
ds->outProcessed += outCur;
if (p->blockSize -= (UInt32)outCur)
{
/*
if (ds->outSize_Defined)
{
if (ds->outSize <= ds->outProcessed) ds->isAfterSizeMode = (enum_ZstdStatus)
(ds->outSize == ds->outProcessed ? 1u: 2u);
}
*/
*status = (enum_ZstdStatus)
(ds->outSize_Defined && ds->outSize <= ds->outProcessed ?
ZSTD_STATUS_OUT_REACHED : (p->blockType == kBlockType_Raw && inSize == *srcLen) ?
ZSTD_STATUS_NEEDS_MORE_INPUT :
ZSTD_STATUS_NOT_FINISHED);
return SZ_OK;
}
}
else // kBlockType_Compressed
{
// p->blockSize != 0
// (uncompressed_size_of_block == 0) is allowed
// (p->curBlockUnpackRem == 0) is allowed
/*
if (p->decoder.winPos >= winLimit)
{
if (p->decoder.winPos != winPos_atFuncStart)
{
// it's unexpected case
// We already have some data in finished blocks in this function call.
// So we don't decompress new block after (>=winLimit),
// even if it's empty block.
*status = (inSize == *srcLen) ?
ZSTD_STATUS_NEEDS_MORE_INPUT :
ZSTD_STATUS_NOT_FINISHED;
return SZ_OK;
}
// (p->decoder.winPos == winLimit == winPos_atFuncStart)
// we will decode current block, because that current
// block can be empty block and we want to make some visible
// change of (src) stream after function start.
}
*/
/*
if (ds->outSize_Defined && ds->outSize < ds->outProcessed)
{
// we don't want to start new block, if we have more extra decoded bytes already
*status = ZSTD_STATUS_OUT_REACHED;
return SZ_OK;
}
*/
{
const Byte *comprStream;
size_t afterAvail;
UInt32 inTempPos = p->inTempPos;
const UInt32 rem = p->blockSize - inTempPos;
// rem != 0
if (inTempPos != 0 // (inTemp) buffer already contains some input data
|| inCur < rem // available input data size is smaller than compressed block size
|| ZstdDec1_NeedTempBufferForInput(*srcLen, src, rem))
{
if (inCur > rem)
inCur = rem;
if (inCur)
{
STAT_INC(g_Num_Blocks_memcpy)
// we clear data for backward lookahead reading
if (inTempPos == 0)
memset(p->inTemp + kTempBuffer_PreSize - MAX_BACKWARD_DEPTH, 0, MAX_BACKWARD_DEPTH);
// { unsigned y = 0; for(;y < 1000; y++)
memcpy(p->inTemp + inTempPos + kTempBuffer_PreSize, src, inCur);
// }
src += inCur;
*srcLen += inCur;
inTempPos += (UInt32)inCur;
p->inTempPos = inTempPos;
}
if (inTempPos != p->blockSize)
{
*status = ZSTD_STATUS_NEEDS_MORE_INPUT;
return SZ_OK;
}
#if COPY_CHUNK_SIZE > 1
memset(p->inTemp + kTempBuffer_PreSize + inTempPos, 0, COPY_CHUNK_SIZE);
#endif
comprStream = p->inTemp + kTempBuffer_PreSize;
afterAvail = k_Lit_AfterAvail;
// we don't want to read non-initialized data or junk in CopyMatch():
}
else
{
// inCur >= rem
// we use direct decoding from (src) buffer:
afterAvail = inCur - rem;
comprStream = src;
src += rem;
*srcLen += rem;
}
#ifdef Z7_ZSTD_DEC_USE_CHECK_OF_NEED_TEMP
ZstdDec1_NeedTempBufferForInput(*srcLen, comprStream, p->blockSize);
#endif
// printf("\nblockSize=%u", p->blockSize);
// printf("%x\n", (unsigned)p->contentProcessed);
STAT_INC(g_Num_Blocks_Compressed)
{
SRes sres;
const size_t winPos = p->decoder.winPos;
/*
if ( useAdditionalWinLimit), we use strong unpack limit: smallest from
- limit from stream : (curBlockUnpackRem)
- limit from caller : (cycSize - winPos)
if (!useAdditionalWinLimit), we use only relaxed limit:
- limit from stream : (curBlockUnpackRem)
*/
SizeT outLimit = p->curBlockUnpackRem;
if (ds->outBuf_fromCaller)
// if (useAdditionalWinLimit)
{
const size_t limit = p->decoder.cycSize - winPos;
if (outLimit > limit)
outLimit = limit;
}
sres = ZstdDec1_DecodeBlock(&p->decoder,
comprStream, p->blockSize, afterAvail, outLimit);
// ds->winPos = p->decoder.winPos; // the caller does it instead. for debug:
if (sres)
{
p->isErrorState = True;
return sres;
}
{
const SizeT xxh64_winPos = winPos - ZstdDec_GET_UNPROCESSED_XXH64_SIZE(p);
const size_t num = p->decoder.winPos - winPos;
ds->outProcessed += num;
p->contentProcessed += num;
ZstdDec_Update_XXH(p, xxh64_winPos);
}
}
// printf("\nwinPos=%x", (int)(unsigned)p->decoder.winPos);
}
}
/*
if (ds->outSize_Defined)
{
if (ds->outSize <= ds->outProcessed) ds->isAfterSizeMode = (enum_ZstdStatus)
(ds->outSize == ds->outProcessed ? 1u: 2u);
}
*/
if (!ZSTD_DEC_IS_LAST_BLOCK(p))
{
p->frameState = ZSTD2_STATE_BLOCK;
if (ds->outSize_Defined && ds->outSize < ds->outProcessed)
{
*status = ZSTD_STATUS_OUT_REACHED;
return SZ_OK;
}
// we exit only if (winPos) was changed in this function call:
if (p->decoder.winPos != winPos_atFuncStart)
{
// decoded block was not empty. So we exit:
*status = (enum_ZstdStatus)(
(inSize == *srcLen) ?
ZSTD_STATUS_NEEDS_MORE_INPUT :
ZSTD_STATUS_NOT_FINISHED);
return SZ_OK;
}
// (p->decoder.winPos == winPos_atFuncStart)
// so current decoded block was empty.
// we will try to decode more blocks in this function.
continue;
}
// decoded block was last in frame
if (p->descriptor & DESCRIPTOR_FLAG_CHECKSUM)
{
p->frameState = ZSTD2_STATE_HASH;
if (ds->outSize_Defined && ds->outSize < ds->outProcessed)
{
*status = ZSTD_STATUS_OUT_REACHED;
return SZ_OK; // disable if want to
/* We want to get same return codes for any input buffer sizes.
We want to get faster ZSTD_STATUS_OUT_REACHED status.
So we exit with ZSTD_STATUS_OUT_REACHED here,
instead of ZSTD2_STATE_HASH and ZSTD2_STATE_FINISHED processing.
that depends from input buffer size and that can set
ZSTD_STATUS_NEEDS_MORE_INPUT or return SZ_ERROR_DATA or SZ_ERROR_CRC.
*/
}
}
else
{
/* ZSTD2_STATE_FINISHED proccesing doesn't depend from input buffer */
p->frameState = ZSTD2_STATE_FINISHED;
}
/*
p->frameState = (p->descriptor & DESCRIPTOR_FLAG_CHECKSUM) ?
ZSTD2_STATE_HASH :
ZSTD2_STATE_FINISHED;
*/
/* it's required to process ZSTD2_STATE_FINISHED state in this function call,
because we must check contentSize and hashError in ZSTD2_STATE_FINISHED code,
while the caller can reinit full state for ZSTD2_STATE_FINISHED
So we can't exit from function here. */
continue;
}
if (p->frameState == ZSTD2_STATE_FINISHED)
{
*status = ZSTD_STATUS_FINISHED_FRAME;
if (DESCRIPTOR_Is_ContentSize_Defined(p->descriptor)
&& p->contentSize != p->contentProcessed)
return SZ_ERROR_DATA;
if (p->hashError) // for debug
return SZ_ERROR_CRC;
return SZ_OK;
// p->frameState = ZSTD2_STATE_SIGNATURE;
// continue;
}
if (p->frameState == ZSTD2_STATE_AFTER_HEADER)
return SZ_OK; // we need memory allocation for that state
if (p->frameState == ZSTD2_STATE_SKIP_DATA)
{
UInt32 blockSize = p->blockSize;
// (blockSize == 0) is possible
if (inCur > blockSize)
inCur = blockSize;
src += inCur;
*srcLen += inCur;
blockSize -= (UInt32)inCur;
p->blockSize = blockSize;
if (blockSize == 0)
{
p->frameState = ZSTD2_STATE_SIGNATURE;
// continue; // for debug: we can continue without return to caller.
// we notify the caller that skip frame was finished:
*status = ZSTD_STATUS_FINISHED_FRAME;
return SZ_OK;
}
// blockSize != 0
// (inCur) was smaller than previous value of p->blockSize.
// (inSize == *srcLen) now
*status = ZSTD_STATUS_NEEDS_MORE_INPUT;
return SZ_OK;
}
if (inCur == 0)
{
*status = ZSTD_STATUS_NEEDS_MORE_INPUT;
return SZ_OK;
}
{
(*srcLen)++;
p->frameState = ZstdDec_UpdateState(p, *src++, info);
}
}
*status = ZSTD_STATUS_NOT_SPECIFIED;
p->isErrorState = True;
// p->frameState = ZSTD2_STATE_ERROR;
// if (p->frameState = ZSTD2_STATE_SIGNATURE) return SZ_ERROR_NO_ARCHIVE
return SZ_ERROR_DATA;
}
SRes ZstdDec_Decode(CZstdDecHandle dec, CZstdDecState *p)
{
p->needWrite_Size = 0;
p->status = ZSTD_STATUS_NOT_SPECIFIED;
dec->disableHash = p->disableHash;
if (p->outBuf_fromCaller)
{
dec->decoder.win = p->outBuf_fromCaller;
dec->decoder.cycSize = p->outBufSize_fromCaller;
}
// p->winPos = dec->decoder.winPos;
for (;;)
{
SizeT winPos, size;
// SizeT outProcessed;
SRes res;
if (p->wrPos > dec->decoder.winPos)
return SZ_ERROR_FAIL;
if (dec->frameState == ZSTD2_STATE_FINISHED)
{
if (!p->outBuf_fromCaller)
{
// we need to set positions to zero for new frame.
if (p->wrPos != dec->decoder.winPos)
{
/* We have already asked the caller to flush all data
with (p->needWrite_Size) and (ZSTD_STATUS_FINISHED_FRAME) status.
So it's unexpected case */
// p->winPos = dec->decoder.winPos;
// p->needWrite_Size = dec->decoder.winPos - p->wrPos; // flush size asking
// return SZ_OK; // ask to flush again
return SZ_ERROR_FAIL;
}
// (p->wrPos == dec->decoder.winPos), and we wrap to zero:
dec->decoder.winPos = 0;
p->winPos = 0;
p->wrPos = 0;
}
ZstdDec_Init_ForNewFrame(dec);
// continue;
}
winPos = dec->decoder.winPos;
{
SizeT next = dec->decoder.cycSize;
/* cycSize == 0, if no buffer was allocated still,
or, if (outBuf_fromCaller) mode and (outBufSize_fromCaller == 0) */
if (!p->outBuf_fromCaller
&& next
&& next <= winPos
&& dec->isCyclicMode)
{
// (0 < decoder.cycSize <= winPos) in isCyclicMode.
// so we need to wrap (winPos) and (wrPos) over (cycSize).
const size_t delta = next;
// (delta) is how many bytes we remove from buffer.
/*
// we don't need data older than last (cycSize) bytes.
size_t delta = winPos - next; // num bytes after (cycSize)
if (delta <= next) // it's expected case
delta = next;
// delta == Max(cycSize, winPos - cycSize)
*/
if (p->wrPos < delta)
{
// (wrPos < decoder.cycSize)
// We have asked already the caller to flush required data
// p->status = ZSTD_STATUS_NOT_SPECIFIED;
// p->winPos = winPos;
// p->needWrite_Size = delta - p->wrPos; // flush size asking
// return SZ_OK; // ask to flush again
return SZ_ERROR_FAIL;
}
// p->wrPos >= decoder.cycSize
// we move extra data after (decoder.cycSize) to start of cyclic buffer:
winPos -= delta;
if (winPos)
{
if (winPos >= delta)
return SZ_ERROR_FAIL;
memmove(dec->decoder.win, dec->decoder.win + delta, winPos);
// printf("\nmemmove processed=%8x winPos=%8x\n", (unsigned)p->outProcessed, (unsigned)dec->decoder.winPos);
STAT_INC(g_Num_Wrap_memmove_Num)
STAT_UPDATE(g_Num_Wrap_memmove_Bytes += (unsigned)winPos;)
}
dec->decoder.winPos = winPos;
p->winPos = winPos;
p->wrPos -= delta;
// dec->xxh64_winPos -= delta;
// (winPos < delta)
#ifdef Z7_STD_DEC_USE_AFTER_CYC_BUF
/* we set the data after cycSize, because
we don't want to read non-initialized data or junk in CopyMatch(). */
memset(dec->decoder.win + next, 0, COPY_CHUNK_SIZE);
#endif
/*
if (winPos == next)
{
if (winPos != p->wrPos)
{
// we already requested before to flush full data for that case.
// but we give the caller a second chance to flush data:
p->needWrite_Size = winPos - p->wrPos;
return SZ_OK;
}
// (decoder.cycSize == winPos == p->wrPos)
// so we do second wrapping to zero:
winPos = 0;
dec->decoder.winPos = 0;
p->winPos = 0;
p->wrPos = 0;
}
*/
// (winPos < next)
}
if (winPos > next)
return SZ_ERROR_FAIL; // it's unexpected case
/*
if (!outBuf_fromCaller && isCyclicMode && cycSize != 0)
then (winPos < cycSize)
else (winPos <= cycSize)
*/
if (!p->outBuf_fromCaller)
{
// that code is optional. We try to optimize write chunk sizes.
/* (next2) is expected next write position in the caller,
if the caller writes by kBlockSizeMax chunks.
*/
/*
const size_t next2 = (winPos + kBlockSizeMax) & (kBlockSizeMax - 1);
if (winPos < next2 && next2 < next)
next = next2;
*/
}
size = next - winPos;
}
// note: ZstdDec_DecodeBlock() uses (winLimit = winPos + size) only for RLE and RAW blocks
res = ZstdDec_DecodeBlock(dec, p, size);
/*
after one block decoding:
if (!outBuf_fromCaller && isCyclicMode && cycSize != 0)
then (winPos < cycSize + max_block_size)
else (winPos <= cycSize)
*/
if (!p->outBuf_fromCaller)
p->win = dec->decoder.win;
p->winPos = dec->decoder.winPos;
// outProcessed = dec->decoder.winPos - winPos;
// p->outProcessed += outProcessed;
if (res != SZ_OK)
return res;
if (dec->frameState != ZSTD2_STATE_AFTER_HEADER)
{
if (p->outBuf_fromCaller)
return SZ_OK;
{
// !p->outBuf_fromCaller
/*
if (ZSTD_STATUS_FINISHED_FRAME), we request full flushing here because
1) it's simpler to work with allocation and extracting of next frame,
2) it's better to start writing to next new frame with aligned memory
for faster xxh 64-bit reads.
*/
size_t end = dec->decoder.winPos; // end pos for all data flushing
if (p->status != ZSTD_STATUS_FINISHED_FRAME)
{
// we will request flush here only for cases when wrap in cyclic buffer can be required in next call.
if (!dec->isCyclicMode)
return SZ_OK;
// isCyclicMode
{
const size_t delta = dec->decoder.cycSize;
if (end < delta)
return SZ_OK; // (winPos < cycSize). no need for flush
// cycSize <= winPos
// So we ask the caller to flush of (cycSize - wrPos) bytes,
// and then we will wrap cylicBuffer in next call
end = delta;
}
}
p->needWrite_Size = end - p->wrPos;
}
return SZ_OK;
}
// ZSTD2_STATE_AFTER_HEADER
{
BoolInt useCyclic = False;
size_t cycSize;
// p->status = ZSTD_STATUS_NOT_FINISHED;
if (dec->dictionaryId != 0)
{
/* actually we can try to decode some data,
because it's possible that some data doesn't use dictionary */
// p->status = ZSTD_STATUS_NOT_SPECIFIED;
return SZ_ERROR_UNSUPPORTED;
}
{
UInt64 winSize = dec->contentSize;
UInt64 winSize_Allocate = winSize;
const unsigned descriptor = dec->descriptor;
if ((descriptor & DESCRIPTOR_FLAG_SINGLE) == 0)
{
const Byte wd = dec->windowDescriptor;
winSize = (UInt64)(8 + (wd & 7)) << ((wd >> 3) + 10 - 3);
if (!DESCRIPTOR_Is_ContentSize_Defined(descriptor)
|| winSize_Allocate > winSize)
{
winSize_Allocate = winSize;
useCyclic = True;
}
}
/*
else
{
if (p->info.singleSegment_ContentSize_MAX < winSize)
p->info.singleSegment_ContentSize_MAX = winSize;
// p->info.num_SingleSegments++;
}
*/
if (p->info.windowSize_MAX < winSize)
p->info.windowSize_MAX = winSize;
if (p->info.windowSize_Allocate_MAX < winSize_Allocate)
p->info.windowSize_Allocate_MAX = winSize_Allocate;
/*
winSize_Allocate is MIN(content_size, window_size_from_descriptor).
Wven if (content_size < (window_size_from_descriptor))
original-zstd still uses (window_size_from_descriptor) to check that decoding is allowed.
We try to follow original-zstd, and here we check (winSize) instead of (winSize_Allocate))
*/
if (
// winSize_Allocate // it's relaxed check
winSize // it's more strict check to be compatible with original-zstd
> ((UInt64)1 << MAX_WINDOW_SIZE_LOG))
return SZ_ERROR_UNSUPPORTED; // SZ_ERROR_MEM
cycSize = (size_t)winSize_Allocate;
if (cycSize != winSize_Allocate)
return SZ_ERROR_MEM;
// cycSize <= winSize
/* later we will use (CZstdDec1::winSize) to check match offsets and check block sizes.
if (there is window descriptor)
{
We will check block size with (window_size_from_descriptor) instead of (winSize_Allocate).
Does original-zstd do it that way also?
}
Here we must reduce full real 64-bit (winSize) to size_t for (CZstdDec1::winSize).
Also we don't want too big values for (CZstdDec1::winSize).
our (CZstdDec1::winSize) will meet the condition:
(CZstdDec1::winSize < kBlockSizeMax || CZstdDec1::winSize <= cycSize).
*/
dec->decoder.winSize = (winSize < kBlockSizeMax) ? (size_t)winSize: cycSize;
// note: (CZstdDec1::winSize > cycSize) is possible, if (!useCyclic)
}
RINOK(ZstdDec_AllocateMisc(dec))
if (p->outBuf_fromCaller)
dec->isCyclicMode = False;
else
{
size_t d = cycSize;
if (dec->decoder.winPos != p->wrPos)
return SZ_ERROR_FAIL;
dec->decoder.winPos = 0;
p->wrPos = 0;
p->winPos = dec->decoder.winPos;
/*
const size_t needWrite = dec->decoder.winPos - p->wrPos;
if (!needWrite)
{
dec->decoder.winPos = 0;
p->wrPos = 0;
p->winPos = dec->decoder.winPos;
}
*/
/* if (!useCyclic) we allocate only cycSize = ContentSize.
But if we want to support the case where new frame starts with winPos != 0,
then we will wrap over zero, and we still need
to set (useCyclic) and allocate additional buffer spaces.
Now we don't allow new frame starting with (winPos != 0).
so (dec->decoder->winPos == 0)
can use (!useCyclic) with reduced buffer sizes.
*/
/*
if (dec->decoder->winPos != 0)
useCyclic = True;
*/
if (useCyclic)
{
/* cyclyc buffer size must be at least (COPY_CHUNK_SIZE - 1) bytes
larger than window size, because CopyMatch() can write additional
(COPY_CHUNK_SIZE - 1) bytes and overwrite oldests data in cyclyc buffer.
But for performance reasons we align (cycSize) for (kBlockSizeMax).
also we must provide (cycSize >= max_decoded_data_after_cycSize),
because after data move wrapping over zero we must provide (winPos < cycSize).
*/
const size_t alignSize = kBlockSizeMax;
/* here we add (1 << 7) instead of (COPY_CHUNK_SIZE - 1), because
we want to get same (cycSize) for different COPY_CHUNK_SIZE values. */
// cycSize += (COPY_CHUNK_SIZE - 1) + (alignSize - 1); // for debug : we can get smallest (cycSize)
cycSize += (1 << 7) + alignSize;
cycSize &= ~(size_t)(alignSize - 1);
// cycSize must be aligned for 32, because xxh requires 32-bytes blocks.
// cycSize += 12345; // for debug
// cycSize += 1 << 10; // for debug
// cycSize += 32; // for debug
// cycSize += kBlockSizeMax; // for debug
if (cycSize < d)
return SZ_ERROR_MEM;
/*
in cyclic buffer mode we allow to decode one additional block
that exceeds (cycSize).
So we must allocate additional (kBlockSizeMax) bytes after (cycSize).
if defined(Z7_STD_DEC_USE_AFTER_CYC_BUF)
{
we can read (COPY_CHUNK_SIZE - 1) bytes after (cycSize)
but we aready allocate additional kBlockSizeMax that
is larger than COPY_CHUNK_SIZE.
So we don't need additional space of COPY_CHUNK_SIZE after (cycSize).
}
*/
/*
#ifdef Z7_STD_DEC_USE_AFTER_CYC_BUF
d = cycSize + (1 << 7); // we must add at least (COPY_CHUNK_SIZE - 1)
#endif
*/
d = cycSize + kBlockSizeMax;
if (d < cycSize)
return SZ_ERROR_MEM;
}
{
const size_t kMinWinAllocSize = 1 << 12;
if (d < kMinWinAllocSize)
d = kMinWinAllocSize;
}
if (d > dec->winBufSize_Allocated)
{
/*
if (needWrite)
{
p->needWrite_Size = needWrite;
return SZ_OK;
// return SZ_ERROR_FAIL;
}
*/
if (dec->winBufSize_Allocated != 0)
{
const size_t k_extra = (useCyclic || d >= (1u << 20)) ?
2 * kBlockSizeMax : 0;
unsigned i = useCyclic ? 17 : 12;
for (; i < sizeof(size_t) * 8; i++)
{
const size_t d2 = ((size_t)1 << i) + k_extra;
if (d2 >= d)
{
d = d2;
break;
}
}
}
// RINOK(ZstdDec_AllocateWindow(dec, d))
ZstdDec_FreeWindow(dec);
dec->win_Base = (Byte *)ISzAlloc_Alloc(dec->alloc_Big, d);
if (!dec->win_Base)
return SZ_ERROR_MEM;
dec->decoder.win = dec->win_Base;
dec->winBufSize_Allocated = d;
}
/*
else
{
// for non-cyclycMode we want flush data, and set winPos = 0
if (needWrite)
{
if (!useCyclic || dec->decoder.winPos >= cycSize)
{
p->needWrite_Size = needWrite;
return SZ_OK;
// return SZ_ERROR_FAIL;
}
}
}
*/
dec->decoder.cycSize = cycSize;
p->win = dec->decoder.win;
// p->cycSize = dec->decoder.cycSize;
dec->isCyclicMode = (Byte)useCyclic;
} // (!p->outBuf_fromCaller) end
// p->winPos = dec->decoder.winPos;
dec->frameState = ZSTD2_STATE_BLOCK;
// continue;
} // ZSTD2_STATE_AFTER_HEADER end
}
}
void ZstdDec_GetResInfo(const CZstdDec *dec,
const CZstdDecState *p,
SRes res,
CZstdDecResInfo *stat)
{
// ZstdDecInfo_CLEAR(stat);
stat->extraSize = 0;
stat->is_NonFinishedFrame = False;
if (dec->frameState != ZSTD2_STATE_FINISHED)
{
if (dec->frameState == ZSTD2_STATE_SIGNATURE)
{
stat->extraSize = (Byte)dec->tempSize;
if (ZstdDecInfo_GET_NUM_FRAMES(&p->info) == 0)
res = SZ_ERROR_NO_ARCHIVE;
}
else
{
stat->is_NonFinishedFrame = True;
if (res == SZ_OK && p->status == ZSTD_STATUS_NEEDS_MORE_INPUT)
res = SZ_ERROR_INPUT_EOF;
}
}
stat->decode_SRes = res;
}
size_t ZstdDec_ReadUnusedFromInBuf(
CZstdDecHandle dec,
size_t afterDecoding_tempPos,
void *data, size_t size)
{
size_t processed = 0;
if (dec->frameState == ZSTD2_STATE_SIGNATURE)
{
Byte *dest = (Byte *)data;
const size_t tempSize = dec->tempSize;
while (afterDecoding_tempPos < tempSize)
{
if (size == 0)
break;
size--;
*dest++ = dec->temp[afterDecoding_tempPos++];
processed++;
}
}
return processed;
}
void ZstdDecState_Clear(CZstdDecState *p)
{
memset(p, 0 , sizeof(*p));
}
|