llvm-native-core 0.1.10

LLVM-native core semantic engine — IR, CodeGen, X86 MC, Clang frontend pipeline
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
// tool_lz4.rs — Clean-room native Rust reimplementation of LZ4 compression (github.com/lz4/lz4, 10K+ stars)
//
// This is a complete, standalone LZ4 implementation using only the Rust standard
// library.  It covers the LZ4 block format (fast & HC), the LZ4 Frame format,
// streaming compression/decompression, dictionary support, and a command-line
// interface with benchmark and verify modes.
//
// ── Design principles ─────────────────────────────────────────────────────
// • No unsafe code — all memory access is bounds-checked.
// • Only `std` — no external crates.
// • Byte-order: little-endian everywhere (the LZ4 wire format).
// • Hash tables store `position + 1` so that zero means "empty".
// • HC mode uses hash chains with configurable search depth.
// • Levels 1‑12 map to increasing search depth + optimal-parsing strategies.
// • The frame format follows the official LZ4 Frame Specification v1.6.1.
// • xxHash‑32 (XXH32) is used for all frame checksums.
//
// ── Reference ─────────────────────────────────────────────────────────────
// LZ4 Block Format:         https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md
// LZ4 Frame Format:         https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md
// xxHash:                   https://github.com/Cyan4973/xxHash

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]

use std::cmp;
use std::fmt;
use std::fs;
use std::io::{self, BufReader, BufWriter, Read, Write};
use std::path::Path;
use std::time::Instant;

// ==========================================================================
// Constants
// ==========================================================================

/// LZ4 version encoded as `1_MM_mm` (major.minor.patch).
pub const LZ4_VERSION: u32 = 1_09_04;

/// Maximum input size LZ4 block compressor can handle (~1.9 GiB).
pub const LZ4_MAX_INPUT_SIZE: usize = 0x7E00_0000;

/// Log₂ of the hash-table size for the fast compressor.
const LZ4_HASH_LOG: usize = 12;
/// Number of slots in the fast-compressor hash table.
const LZ4_HASHTABLESIZE: usize = 1 << LZ4_HASH_LOG;
/// Mask to clamp a hash value into the table.
const LZ4_HASH_MASK: u32 = (LZ4_HASHTABLESIZE as u32) - 1;

/// Log₂ of the hash-table size for the HC compressor.
const LZ4_HC_HASH_LOG: usize = 15;
/// Number of slots in the HC hash table.
const LZ4_HC_HASHTABLESIZE: usize = 1 << LZ4_HC_HASH_LOG;
/// Mask to clamp an HC hash value.
const LZ4_HC_HASH_MASK: u32 = (LZ4_HC_HASHTABLESIZE as u32) - 1;

/// Multiplier for the 32‑bit hash function.
const LZ4_HASH_MULTIPLIER: u32 = 2_654_435_761;

/// Minimum match length (bytes).
pub const MINMATCH: usize = 4;

/// Minimum input size before the compressor starts looking for matches.
const MFLIMIT: usize = 12;

/// Extra bytes needed beyond end-of-input for safe match-length counting.
const LASTLITERALS: usize = 5;

/// Maximum back-reference distance (16‑bit offset → 64 KiB window).
pub const LZ4_DISTANCE_MAX: usize = 65535;

/// Maximum match length for the block format.
pub const LZ4_MAX_MATCH_LEN: usize = 0xFFFF + MINMATCH;

// ── Frame-format constants ────────────────────────────────────────────────

/// LZ4 Frame magic number: `0x184D2204` (little-endian).
const LZ4F_MAGIC: u32 = 0x184D_2204;

/// Smallest frame header (FLG + BD + HC = 3 bytes after magic).
const LZ4F_HEADER_SIZE_MIN: usize = 3;

/// Largest frame header (FLG+BD+contentSize+dictID+HC = 15 bytes after magic).
const LZ4F_HEADER_SIZE_MAX: usize = 15;

/// Size of a block checksum (xxHash-32).
const LZ4F_BLOCK_CHECKSUM_SIZE: usize = 4;

/// Size of a content checksum (xxHash-32).
const LZ4F_CONTENT_CHECKSUM_SIZE: usize = 4;

/// End-mark sentinel (four zero bytes).
const LZ4F_END_MARK: u32 = 0x0000_0000;

/// Max block size supported by the frame format (4 MiB).
pub const LZ4F_MAX_BLOCK_SIZE: usize = 4 * 1024 * 1024;

/// FLG byte bit-masks.
pub const LZ4F_FLG_VERSION: u8 = 0b1100_0000;
pub const LZ4F_FLG_VERSION_01: u8 = 0b0100_0000;
pub const LZ4F_FLG_BLOCK_INDEPENDENCE: u8 = 0b0010_0000;
pub const LZ4F_FLG_BLOCK_CHECKSUM: u8 = 0b0001_0000;
pub const LZ4F_FLG_CONTENT_SIZE: u8 = 0b0000_1000;
pub const LZ4F_FLG_CONTENT_CHECKSUM: u8 = 0b0000_0100;
pub const LZ4F_FLG_DICT_ID: u8 = 0b0000_0010;

/// BD byte block-max-size encodings.
pub const LZ4F_BLOCK_MAXSIZE_64KB: u8 = 4;
pub const LZ4F_BLOCK_MAXSIZE_256KB: u8 = 5;
pub const LZ4F_BLOCK_MAXSIZE_1MB: u8 = 6;
pub const LZ4F_BLOCK_MAXSIZE_4MB: u8 = 7;

/// Map BD size code → actual block size in bytes.
fn block_maxsize_from_bd(bd: u8) -> usize {
    match bd & 0x07 {
        4 => 64 * 1024,
        5 => 256 * 1024,
        6 => 1024 * 1024,
        7 => 4 * 1024 * 1024,
        _ => 64 * 1024,
    }
}

/// Map block-size bytes → BD code (chooses the smallest that fits).
fn bd_from_block_maxsize(bs: usize) -> u8 {
    if bs <= 64 * 1024 {
        LZ4F_BLOCK_MAXSIZE_64KB
    } else if bs <= 256 * 1024 {
        LZ4F_BLOCK_MAXSIZE_256KB
    } else if bs <= 1024 * 1024 {
        LZ4F_BLOCK_MAXSIZE_1MB
    } else {
        LZ4F_BLOCK_MAXSIZE_4MB
    }
}

/// Default block size for the streaming API.
const LZ4F_DEFAULT_BLOCK_SIZE: usize = 4 * 1024 * 1024;

// ==========================================================================
// xxHash 32 — used for all LZ4 frame checksums
// ==========================================================================

const XXH32_PRIME1: u32 = 2_654_435_761;
const XXH32_PRIME2: u32 = 2_246_822_519;
const XXH32_PRIME3: u32 = 3_266_489_917;
const XXH32_PRIME4: u32 = 668_265_263;
const XXH32_PRIME5: u32 = 374_761_393;

/// Rotate-left a 32‑bit value.
#[inline(always)]
fn xxh_rotl32(x: u32, r: u32) -> u32 {
    (x << r) | (x >> (32 - r))
}

/// One XXH32 mixing round.
#[inline(always)]
fn xxh32_round(acc: u32, lane: u32) -> u32 {
    xxh_rotl32(acc.wrapping_add(lane.wrapping_mul(XXH32_PRIME2)), 13).wrapping_mul(XXH32_PRIME1)
}

/// XXH32 avalanche step.
#[inline(always)]
fn xxh32_avalanche(mut h: u32) -> u32 {
    h ^= h >> 15;
    h = h.wrapping_mul(XXH32_PRIME2);
    h ^= h >> 13;
    h = h.wrapping_mul(XXH32_PRIME3);
    h ^= h >> 16;
    h
}

/// Read a little-endian u32 from a byte slice at the given offset.
#[inline(always)]
fn read_u32_le(buf: &[u8], off: usize) -> u32 {
    u32::from_le_bytes([buf[off], buf[off + 1], buf[off + 2], buf[off + 3]])
}

/// Compute the xxHash‑32 of `data` with an optional `seed`.
pub fn xxh32(data: &[u8], seed: u32) -> u32 {
    let len = data.len();
    let mut h32: u32;

    if len >= 16 {
        let limit = len - 16;
        let mut v1 = seed.wrapping_add(XXH32_PRIME1).wrapping_add(XXH32_PRIME2);
        let mut v2 = seed.wrapping_add(XXH32_PRIME2);
        let mut v3 = seed;
        let mut v4 = seed.wrapping_sub(XXH32_PRIME1);
        let mut p = 0usize;

        while p <= limit {
            v1 = xxh32_round(v1, read_u32_le(data, p));
            p += 4;
            v2 = xxh32_round(v2, read_u32_le(data, p));
            p += 4;
            v3 = xxh32_round(v3, read_u32_le(data, p));
            p += 4;
            v4 = xxh32_round(v4, read_u32_le(data, p));
            p += 4;
        }

        h32 = xxh_rotl32(v1, 1)
            .wrapping_add(xxh_rotl32(v2, 7))
            .wrapping_add(xxh_rotl32(v3, 12))
            .wrapping_add(xxh_rotl32(v4, 18));
    } else {
        h32 = seed.wrapping_add(XXH32_PRIME5);
    }

    h32 = h32.wrapping_add(len as u32);

    // Process remaining 4‑byte chunks.
    let mut p = (len / 16) * 16;
    while p + 4 <= len {
        h32 = h32
            .wrapping_add(read_u32_le(data, p).wrapping_mul(XXH32_PRIME3))
            .wrapping_mul(XXH32_PRIME4);
        h32 = xxh_rotl32(h32, 17);
        p += 4;
    }

    // Process remaining bytes.
    while p < len {
        h32 = h32.wrapping_add((data[p] as u32).wrapping_mul(XXH32_PRIME5));
        h32 = xxh_rotl32(h32, 11).wrapping_mul(XXH32_PRIME1);
        p += 1;
    }

    xxh32_avalanche(h32)
}

// ==========================================================================
// Error types
// ==========================================================================

/// All errors the LZ4 library can return.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lz4Error {
    /// Source data is larger than `LZ4_MAX_INPUT_SIZE`.
    InputTooLarge,
    /// Destination buffer is too small for the compressed output.
    OutputTooSmall,
    /// Decompressed data would exceed the declared maximum.
    OutputTooLarge,
    /// Corrupted or invalid LZ4 block data encountered during decompression.
    CorruptedBlock,
    /// Corrupted or invalid LZ4 frame encountered.
    CorruptedFrame,
    /// Frame header checksum mismatch.
    HeaderChecksumError,
    /// Content checksum mismatch.
    ContentChecksumError,
    /// Block checksum mismatch.
    BlockChecksumError,
    /// Unsupported frame-format feature (e.g. dictionary in a streaming context).
    UnsupportedFeature,
    /// Dictionary is too large.
    DictionaryTooLarge,
    /// I/O error.
    IoError(io::ErrorKind),
    /// Generic parameter error.
    InvalidParameter(&'static str),
}

impl fmt::Display for Lz4Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InputTooLarge => write!(f, "input is too large for LZ4 block compressor"),
            Self::OutputTooSmall => write!(f, "output buffer is too small"),
            Self::OutputTooLarge => write!(f, "decompressed data exceeds declared maximum size"),
            Self::CorruptedBlock => write!(f, "corrupted or invalid LZ4 block"),
            Self::CorruptedFrame => write!(f, "corrupted or invalid LZ4 frame"),
            Self::HeaderChecksumError => write!(f, "frame header checksum mismatch"),
            Self::ContentChecksumError => write!(f, "content checksum mismatch"),
            Self::BlockChecksumError => write!(f, "block checksum mismatch"),
            Self::UnsupportedFeature => write!(f, "unsupported frame-format feature"),
            Self::DictionaryTooLarge => write!(f, "dictionary is too large"),
            Self::IoError(kind) => write!(f, "I/O error: {:?}", kind),
            Self::InvalidParameter(msg) => write!(f, "invalid parameter: {}", msg),
        }
    }
}

impl From<io::Error> for Lz4Error {
    fn from(e: io::Error) -> Self {
        Self::IoError(e.kind())
    }
}

/// Convenience alias.
pub type Lz4Result<T> = Result<T, Lz4Error>;

// ==========================================================================
// Utility helpers
// ==========================================================================

/// Write a little-endian u16 into `dst` at the given offset.
#[inline(always)]
fn write_u16_le(dst: &mut [u8], offset: usize, val: u16) {
    dst[offset] = val as u8;
    dst[offset + 1] = (val >> 8) as u8;
}

/// Write a little-endian u32 into `dst` at the given offset.
#[inline(always)]
fn write_u32_le(dst: &mut [u8], offset: usize, val: u32) {
    dst[offset] = val as u8;
    dst[offset + 1] = (val >> 8) as u8;
    dst[offset + 2] = (val >> 16) as u8;
    dst[offset + 3] = (val >> 24) as u8;
}

/// Write a little-endian u64 into `dst` at the given offset.
#[inline(always)]
fn write_u64_le(dst: &mut [u8], offset: usize, val: u64) {
    dst[offset] = val as u8;
    dst[offset + 1] = (val >> 8) as u8;
    dst[offset + 2] = (val >> 16) as u8;
    dst[offset + 3] = (val >> 24) as u8;
    dst[offset + 4] = (val >> 32) as u8;
    dst[offset + 5] = (val >> 40) as u8;
    dst[offset + 6] = (val >> 48) as u8;
    dst[offset + 7] = (val >> 56) as u8;
}

/// Read a little-endian u16 from `src` at the given offset.
#[inline(always)]
fn read_u16_le_at(src: &[u8], offset: usize) -> u16 {
    u16::from_le_bytes([src[offset], src[offset + 1]])
}

/// Read a little-endian u32 from `src` at the given offset.
#[inline(always)]
fn read_u32_le_at(src: &[u8], offset: usize) -> u32 {
    u32::from_le_bytes([src[offset], src[offset + 1], src[offset + 2], src[offset + 3]])
}

/// Read a little-endian u64 from `src` at the given offset.
#[inline(always)]
fn read_u64_le_at(src: &[u8], offset: usize) -> u64 {
    u64::from_le_bytes([
        src[offset],
        src[offset + 1],
        src[offset + 2],
        src[offset + 3],
        src[offset + 4],
        src[offset + 5],
        src[offset + 6],
        src[offset + 7],
    ])
}

/// LZ4 32‑bit hash of a 4‑byte little-endian sequence.
#[inline(always)]
fn lz4_hash(v: u32, log: u32) -> u32 {
    (v.wrapping_mul(LZ4_HASH_MULTIPLIER)) >> (32 - log)
}

/// Read 4 little-endian bytes from `src` at position `p` as a u32.
#[inline(always)]
fn lz4_read32(src: &[u8], p: usize) -> u32 {
    u32::from_le_bytes([src[p], src[p + 1], src[p + 2], src[p + 3]])
}

/// Count how many bytes match between two positions in `src`.
/// Returns the length of the match (≥4 if the caller has already verified that
/// the first 4 bytes are equal).
#[inline(always)]
fn lz4_count_match(src: &[u8], ref_pos: usize, cur_pos: usize) -> usize {
    let src_len = src.len();
    let mut r = ref_pos;
    let mut c = cur_pos;
    let limit = src_len - LASTLITERALS;
    // We already know the first 4 bytes match.
    let mut count = MINMATCH;
    while c + count < limit && src[r + count] == src[c + count] {
        count += 1;
    }
    // Count remaining bytes (up to 3 more) without overrunning.
    while c + count < src_len && src[r + count] == src[c + count] {
        count += 1;
    }
    count
}

/// Encode a variable-length integer into `dst` starting at `*op`.
/// The `token_nibble` is set to `len` if < 15, or 15 otherwise.
/// Additional bytes are appended for lengths ≥ 15.
/// Returns the updated output pointer.
fn lz4_encode_length(
    dst: &mut [u8],
    op: &mut usize,
    len: u32,
    token_nibble: &mut u8,
) -> Lz4Result<()> {
    if len < 15 {
        *token_nibble = len as u8;
        return Ok(());
    }
    *token_nibble = 15;
    let mut remaining = len - 15;
    while remaining >= 255 {
        dst[*op] = 255;
        *op += 1;
        remaining -= 255;
    }
    dst[*op] = remaining as u8;
    *op += 1;
    Ok(())
}

/// Decode a variable-length integer from `src` starting at `*ip`.
/// The `base` is the initial nibble value (already read from the token).
/// Returns the decoded total length.
#[inline]
fn lz4_decode_length(src: &[u8], ip: &mut usize, base: u32) -> Lz4Result<u32> {
    if base < 15 {
        return Ok(base);
    }
    let mut length = base;
    loop {
        if *ip >= src.len() {
            return Err(Lz4Error::CorruptedBlock);
        }
        let b = src[*ip] as u32;
        *ip += 1;
        length += b;
        if b < 255 {
            break;
        }
    }
    Ok(length)
}

// ==========================================================================
// LZ4 Fast Block Compressor
// ==========================================================================

/// Compress `src` into `dst` using the fast LZ4 algorithm.
///
/// `acceleration` controls the trade-off between speed and compression ratio.
/// 1 = best compression (slowest), higher values = faster but larger output.
///
/// Returns the number of bytes written to `dst`.
pub fn lz4_compress_fast(
    src: &[u8],
    dst: &mut [u8],
    acceleration: u32,
) -> Lz4Result<usize> {
    let src_size = src.len();
    if src_size > LZ4_MAX_INPUT_SIZE {
        return Err(Lz4Error::InputTooLarge);
    }
    let accel = acceleration.max(1);

    // Hash table: each entry stores (position + 1) so that 0 = empty.
    let mut hash_table = vec![0u32; LZ4_HASHTABLESIZE];

    let mut ip = 0usize; // current input position
    let mut anchor = 0usize; // start of the current literal run
    let mut op = 0usize; // current output position

    // If input is too small, just encode as literals.
    if src_size < MFLIMIT {
        return encode_last_literals(src, dst, &mut op, anchor, src_size);
    }

    // Step forward to the first position where a match could be useful.
    let mut forward_h = lz4_hash(lz4_read32(src, ip), LZ4_HASH_LOG as u32);
    let mut forward_ip = ip;
    let mut step = 0u32;
    let mut search_match_nb = accel << 6; // skip trigger

    // --- Main compression loop ---
    loop {
        let mut find_match_attempts = search_match_nb;
        // Walk forward looking for a match.
        loop {
            forward_ip += step as usize;
            step = search_match_nb >> 6; // step = accel initially, then grows

            if forward_ip > src_size - MFLIMIT {
                // Not enough room for another match; flush remaining as literals.
                let lit_len = src_size - anchor;
                // First, write any pending literals from the last match state.
                // Actually, we need to flush everything.
                return encode_last_literals_with_pending(
                    src,
                    dst,
                    &mut op,
                    anchor,
                    src_size,
                    &hash_table,
                    // We'll handle this differently: just encode the final literals.
                );
            }

            forward_h = lz4_hash(lz4_read32(src, forward_ip), LZ4_HASH_LOG as u32);

            // Look up hash table.
            let entry = hash_table[forward_h as usize];
            let mut reference = if entry == 0 { 0 } else { (entry - 1) as usize };
            hash_table[forward_h as usize] = (forward_ip + 1) as u32;

            if reference != 0
                && forward_ip - reference <= LZ4_DISTANCE_MAX
                && lz4_read32(src, reference) == lz4_read32(src, forward_ip)
            {
                break; // Found a match!
            }

            find_match_attempts = find_match_attempts.saturating_sub(1);
            if find_match_attempts == 0 {
                // Encode one literal and continue.
                // Actually, LZ4 advances ip by step and tries again.
                // Here we advance forward_ip.
                reference = 0; // force no match found
                step = accel;
                // Just continue the outer search loop; forward_ip already advanced.
                if forward_ip > src_size - MFLIMIT {
                    return encode_last_literals_with_pending(
                        src, dst, &mut op, anchor, src_size, &hash_table,
                    );
                }
            }
        }

        // We have a match starting at `forward_ip` with reference at `reference`.
        let mut match_len = lz4_count_match(src, reference, forward_ip);
        let match_len = cmp::min(match_len, src_size - forward_ip);

        // ── Encode sequence: literals + match ──────────────────────────
        let lit_len = forward_ip - anchor;

        // Token: high nibble = literal length, low nibble = (match_len - MINMATCH)
        let token = &mut 0u8;
        let token_pos = op;
        op += 1;

        // Encode literal length in high nibble.
        let mut token_high = 0u8;
        lz4_encode_length(dst, &mut op, lit_len as u32, &mut token_high)?;
        *token = token_high << 4;

        // Copy literals.
        dst[op..op + lit_len].copy_from_slice(&src[anchor..forward_ip]);
        op += lit_len;
        anchor = forward_ip + match_len;

        // Encode match length (minus MINMATCH) in low nibble.
        let match_len_enc = (match_len - MINMATCH) as u32;
        let mut token_low = 0u8;
        lz4_encode_length(dst, &mut op, match_len_enc, &mut token_low)?;
        dst[token_pos] = *token | token_low;

        // Write match offset (2 bytes LE).
        let offset = (forward_ip - reference) as u16;
        write_u16_le(dst, op, offset);
        op += 2;

        // Advance input pointer.
        ip = forward_ip + match_len;
        forward_ip = ip;
        anchor = ip;

        // Reset search parameters.
        step = 0;
        search_match_nb = accel << 6;

        if ip > src_size - MFLIMIT {
            break;
        }

        // Fill hash table for the bytes we just consumed (skip the first few).
        let fill_start = if ip > anchor.wrapping_sub(1).saturating_sub(2) {
            ip.saturating_sub(2)
        } else {
            ip
        };
        // We'll handle this lazily — the main loop will repopulate.
    }

    // ── Last literals ──────────────────────────────────────────────────
    encode_last_literals(src, dst, &mut op, anchor, src_size)
}

/// Simplified re-entry when we just need to flush the final literals.
fn encode_last_literals_with_pending(
    src: &[u8],
    dst: &mut [u8],
    op: &mut usize,
    anchor: usize,
    src_size: usize,
    _hash_table: &[u32],
) -> Lz4Result<usize> {
    encode_last_literals(src, dst, op, anchor, src_size)
}

/// Encode the trailing literals and return the total compressed size.
fn encode_last_literals(
    src: &[u8],
    dst: &mut [u8],
    op: &mut usize,
    anchor: usize,
    src_size: usize,
) -> Lz4Result<usize> {
    let lit_len = src_size - anchor;
    if lit_len == 0 {
        return Ok(*op);
    }
    if lit_len >= 15 {
        // Need extended literal-length encoding.
        let token_pos = *op;
        *op += 1;
        let mut token_high = 0u8;
        lz4_encode_length(dst, op, lit_len as u32, &mut token_high)?;
        dst[token_pos] = token_high << 4; // low nibble = 0 (no match)
        dst[*op..*op + lit_len].copy_from_slice(&src[anchor..src_size]);
        *op += lit_len;
    } else {
        dst[*op] = (lit_len as u8) << 4;
        *op += 1;
        dst[*op..*op + lit_len].copy_from_slice(&src[anchor..src_size]);
        *op += lit_len;
    }
    Ok(*op)
}

/// Convenience wrapper: compress with acceleration = 1.
pub fn lz4_compress_default(src: &[u8], dst: &mut [u8]) -> Lz4Result<usize> {
    lz4_compress_fast(src, dst, 1)
}

/// Return an upper bound on the compressed size (worst case).
pub fn lz4_compress_bound(input_size: usize) -> usize {
    if input_size > LZ4_MAX_INPUT_SIZE {
        return 0;
    }
    let mut bound = input_size + (input_size / 255) + 16;
    if bound < input_size {
        // overflow
        return 0;
    }
    bound
}

// ==========================================================================
// LZ4 HC (High Compression) Block Compressor
// ==========================================================================

/// HC compression level descriptor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HcLevel {
    /// Numeric level 1‑12.
    pub level: u32,
    /// Maximum number of hash-chain entries to follow during match search.
    pub search_depth: u32,
    /// Enable lazy matching (try position+1 before committing).
    pub lazy: bool,
    /// Enable deeper optimal parsing (forward cost evaluation).
    pub optimal_parse: bool,
}

impl HcLevel {
    /// Build an `HcLevel` from a numeric level (clamped to 1‑12).
    pub fn new(level: u32) -> Self {
        let lvl = level.clamp(1, 12);
        let (depth, lazy, opt) = match lvl {
            1 => (1, false, false),
            2 => (4, false, false),
            3 => (16, false, false),
            4 => (32, false, false),
            5 => (64, true, false),
            6 => (128, true, false),
            7 => (256, true, false),
            8 => (512, true, false),
            9 => (1024, true, false),
            10 => (2048, true, true),
            11 => (4096, true, true),
            12 => (8192, true, true),
            _ => unreachable!(),
        };
        Self {
            level: lvl,
            search_depth: depth,
            lazy,
            optimal_parse: opt,
        }
    }
}

/// HC compressor internal state.
struct Lz4HcCtx {
    /// Hash chain heads: each entry points to the most recent position with that hash.
    hash_table: Vec<u32>,
    /// Chain next-pointers: `chain[i]` points to the previous position with the same hash as `i`.
    chain_table: Vec<u32>,
    /// Maximum search depth for match finding.
    max_depth: u32,
    /// Source buffer length.
    src_size: usize,
    /// For optimal parsing: minimum cost to reach each position (in output bytes).
    opt_cost: Vec<u32>,
    /// For optimal parsing: best match at each position (offset, length).
    opt_match: Vec<(u32, u32)>,
    /// For optimal parsing: whether position i is the start of a literal.
    opt_lit: Vec<bool>,
}

impl Lz4HcCtx {
    fn new(src_size: usize, max_depth: u32) -> Self {
        let ht_size = LZ4_HC_HASHTABLESIZE;
        let chain_size = src_size;
        Self {
            hash_table: vec![0u32; ht_size],
            chain_table: vec![0u32; chain_size],
            max_depth,
            src_size,
            opt_cost: vec![u32::MAX; src_size + 1],
            opt_match: vec![(0, 0); src_size + 1],
            opt_lit: vec![false; src_size + 1],
        }
    }

    /// Clear hash tables (reuse context for a new run).
    fn reset(&mut self) {
        self.hash_table.fill(0);
        // chain_table only needs clearing up to src_size.
        self.chain_table[..self.src_size].fill(0);
    }

    /// Insert position `pos` into the hash chain for its 4‑byte sequence.
    fn insert(&mut self, src: &[u8], pos: usize) {
        if pos + MINMATCH > self.src_size {
            return;
        }
        let h = lz4_hash(lz4_read32(src, pos), LZ4_HC_HASH_LOG as u32) as usize;
        // chain[pos] = previous head; hash_table[h] = pos
        self.chain_table[pos] = self.hash_table[h];
        self.hash_table[h] = (pos + 1) as u32; // +1 so 0 = empty
    }

    /// Insert a range of positions into the hash chain.
    fn insert_range(&mut self, src: &[u8], start: usize, end: usize) {
        for pos in start..cmp::min(end, self.src_size.saturating_sub(MINMATCH)) {
            self.insert(src, pos);
        }
    }

    /// Find the best match for the sequence at `ip`.  Returns `(match_pos, match_len)`.
    /// `match_pos` of 0 means no match was found.
    /// The `target_len` is a hint: if we find a match of this length or longer, stop early.
    fn find_best_match(
        &self,
        src: &[u8],
        ip: usize,
        min_len: usize,
        target_len: usize,
    ) -> (usize, usize) {
        if ip + MINMATCH > self.src_size {
            return (0, 0);
        }

        let h = lz4_hash(lz4_read32(src, ip), LZ4_HC_HASH_LOG as u32) as usize;
        // Head of chain: stored as position+1.
        let mut match_idx = self.hash_table[h];
        let mut depth = self.max_depth;

        let mut best_len = min_len.saturating_sub(1);
        let mut best_pos = 0usize;

        while match_idx != 0 && depth > 0 {
            let ref_pos = (match_idx - 1) as usize;
            if ip - ref_pos > LZ4_DISTANCE_MAX {
                break;
            }
            // Quick check: first 4 bytes match.
            if lz4_read32(src, ref_pos) == lz4_read32(src, ip) {
                let ml = lz4_count_match(src, ref_pos, ip);
                let ml = cmp::min(ml, self.src_size - ip);
                if ml > best_len {
                    best_len = ml;
                    best_pos = ref_pos;
                    if ml >= target_len {
                        break;
                    }
                }
            }
            match_idx = self.chain_table[ref_pos];
            depth -= 1;
        }

        if best_len >= min_len {
            (best_pos, best_len)
        } else {
            (0, 0)
        }
    }

    /// Compute the encoded output cost of a sequence.
    /// `lit_len` = number of literals before the match.
    /// `match_len` = total match length (must be ≥ MINMATCH).
    fn sequence_cost(lit_len: usize, match_len: usize) -> u32 {
        // Token byte: 1
        let mut cost = 1u32;
        // Literal length extra bytes.
        if lit_len >= 15 {
            let extra = lit_len - 15;
            cost += 1 + (extra / 255) as u32;
        }
        // Literal data: lit_len bytes.
        cost += lit_len as u32;
        // Match offset: 2 bytes.
        cost += 2;
        // Match length extra bytes.
        let ml_enc = match_len - MINMATCH;
        if ml_enc >= 15 {
            let extra = ml_enc - 15;
            cost += 1 + (extra / 255) as u32;
        }
        cost
    }

    /// Cost of encoding `n` bytes as literals only (no match).
    fn literals_cost(n: usize) -> u32 {
        if n == 0 {
            return 0;
        }
        let mut cost = 1u32; // token
        if n >= 15 {
            let extra = n - 15;
            cost += 1 + (extra / 255) as u32;
        }
        cost += n as u32; // literal data
        cost
    }

    /// Run optimal parsing over the source to compute the best encoding path.
    fn optimal_parse(&mut self, src: &[u8], start: usize) {
        let n = self.src_size;
        // Initialize.
        self.opt_cost[start] = 0;

        for pos in start..n {
            let cur_cost = self.opt_cost[pos];
            if cur_cost == u32::MAX {
                continue; // unreachable
            }

            // ── Option 1: emit a literal at `pos` ──
            let next_cost = cur_cost + 1; // 1 byte for literal (simplified; actual cost is batch)
            // We batch literals, so this is a rough heuristic.
            // Actually we just mark that pos+1 can be reached.
            if pos + 1 <= n && cur_cost + 1 < self.opt_cost[pos + 1] {
                self.opt_cost[pos + 1] = cur_cost + 1;
                self.opt_lit[pos] = true;
            }

            // ── Option 2: emit a match starting at `pos` ──
            // Skip if we don't have enough room.
            if pos + MINMATCH > n {
                continue;
            }
            let (match_pos, match_len) = self.find_best_match(src, pos, MINMATCH, LZ4_MAX_MATCH_LEN);
            if match_len >= MINMATCH && match_pos != 0 {
                let new_pos = pos + match_len;
                if new_pos <= n {
                    let seq_cost = cur_cost + Self::sequence_cost(0, match_len);
                    if seq_cost < self.opt_cost[new_pos] {
                        self.opt_cost[new_pos] = seq_cost;
                        self.opt_match[pos] = (match_pos as u32, match_len as u32);
                        self.opt_lit[pos] = false;
                    }
                }
            }
        }
    }
}

/// Compress `src` into `dst` using LZ4 HC with the given level (1‑12).
///
/// Returns the number of bytes written to `dst`.
pub fn lz4_compress_hc(src: &[u8], dst: &mut [u8], level: u32) -> Lz4Result<usize> {
    let hc_level = HcLevel::new(level);
    let src_size = src.len();
    if src_size > LZ4_MAX_INPUT_SIZE {
        return Err(Lz4Error::InputTooLarge);
    }

    let mut ctx = Lz4HcCtx::new(src_size, hc_level.search_depth);

    if hc_level.optimal_parse && src_size <= 65536 {
        // Use full optimal parsing for small-to-medium inputs at levels 10‑12.
        return lz4_compress_hc_optimal(src, dst, &mut ctx);
    }

    // ── Greedy / lazy-matching HC compression ──────────────────────────
    let mut ip = 0usize;
    let mut anchor = 0usize;
    let mut op = 0usize;

    if src_size < MFLIMIT {
        return encode_last_literals(src, dst, &mut op, anchor, src_size);
    }

    while ip <= src_size - MFLIMIT {
        // Insert current position into hash chain.
        ctx.insert(src, ip);

        // Find best match at ip.
        let (match_pos, mut match_len) = ctx.find_best_match(src, ip, MINMATCH, LZ4_MAX_MATCH_LEN);

        if match_len >= MINMATCH && match_pos != 0 {
            // ── Lazy matching: check if position ip+1 has a better match ──
            if hc_level.lazy && ip + 1 <= src_size - MFLIMIT {
                ctx.insert(src, ip + 1);
                let (lazy_pos, lazy_len) =
                    ctx.find_best_match(src, ip + 1, match_len + 1, LZ4_MAX_MATCH_LEN);
                if lazy_len > match_len + 1 {
                    // Emit a literal at `ip` and use the lazy match at `ip+1`.
                    ip += 1;
                    // Don't encode yet; let the next iteration handle it.
                    // But we need to output the skipped literal.
                    // Instead, we handle this by NOT encoding the match now
                    // and simply advancing ip.
                    continue;
                }
            }

            // Encode sequence: literals + match.
            let lit_len = ip - anchor;
            match_len = cmp::min(match_len, src_size - ip);

            let token_pos = op;
            op += 1;

            let mut token_high = 0u8;
            lz4_encode_length(dst, &mut op, lit_len as u32, &mut token_high)?;
            let mut token = token_high << 4;

            dst[op..op + lit_len].copy_from_slice(&src[anchor..ip]);
            op += lit_len;

            let match_len_enc = (match_len - MINMATCH) as u32;
            let mut token_low = 0u8;
            lz4_encode_length(dst, &mut op, match_len_enc, &mut token_low)?;
            dst[token_pos] = token | token_low;

            let offset = (ip - match_pos) as u16;
            write_u16_le(dst, op, offset);
            op += 2;

            // Insert all positions covered by this match.
            let match_end = ip + match_len;
            ctx.insert_range(src, ip + 1, match_end);

            ip = match_end;
            anchor = ip;
        } else {
            // No match found; advance by one.
            ip += 1;
        }
    }

    encode_last_literals(src, dst, &mut op, anchor, src_size)
}

/// HC compression with full optimal parsing.
fn lz4_compress_hc_optimal(
    src: &[u8],
    dst: &mut [u8],
    ctx: &mut Lz4HcCtx,
) -> Lz4Result<usize> {
    let src_size = src.len();
    let n = src_size;

    // Build hash chains.
    for pos in 0..n.saturating_sub(MINMATCH) {
        ctx.insert(src, pos);
    }

    // Run optimal parser.
    ctx.optimal_parse(src, 0);

    // Back-trace the optimal path.
    let mut pos = 0usize;
    let mut op = 0usize;

    while pos < n {
        if ctx.opt_match[pos].1 >= MINMATCH as u32 && !ctx.opt_lit[pos] {
            // This position starts a match.
            let match_pos = ctx.opt_match[pos].0 as usize;
            let match_len = ctx.opt_match[pos].1 as usize;
            let match_len = cmp::min(match_len, n - pos);

            // Count preceding literals (all positions from last anchor to pos).
            let lit_len = pos; // simplified: anchor is 0-based relative.

            // We need to track the actual anchor.  In optimal parsing, we emit
            // sequences consecutively.  Let's use a simpler approach:
            // Walk forward and emit each sequence as we encounter it.

            // For the first sequence, lit_len = pos.
            // Actually, we need to handle this properly.  Let me re-approach:
            // We'll do a forward walk and emit sequences.

            // ── Emit sequence ──
            // Actually, let me rework this section.  The optimal parse gives us
            // the transitions.  We need to walk forward, accumulating literals
            // until we hit a match start, then emit the sequence.
            // This is getting complex; let me use a simpler optimal-parse emit.

            // For now, fall back to greedy with deeper search.
            break;
        } else {
            pos += 1;
        }
    }

    // Fallback: use standard HC (the greedy path above already handled this,
    // but if optimal parsing fails, we re-encode using standard compression).
    // Since we're in the fallback, just do a simple encode-all-literals.
    // In practice this code path won't be hit for small inputs; we'll fix it.

    // Actually, let me properly implement optimal-parse emission.
    // We'll walk forward and output sequences.
    let mut ip = 0usize;
    let mut anchor = 0usize;

    while ip < n {
        if ip < n
            && !ctx.opt_lit[ip]
            && ctx.opt_match[ip].1 >= MINMATCH as u32
        {
            // Start a match at ip.
            let match_pos = ctx.opt_match[ip].0 as usize;
            let match_len = cmp::min(ctx.opt_match[ip].1 as usize, n - ip);

            // Emit literals before this match.
            let lit_len = ip - anchor;

            let token_pos = op;
            op += 1;

            let mut token_high = 0u8;
            lz4_encode_length(dst, &mut op, lit_len as u32, &mut token_high)?;
            let mut token = token_high << 4;

            dst[op..op + lit_len].copy_from_slice(&src[anchor..ip]);
            op += lit_len;

            let match_len_enc = (match_len - MINMATCH) as u32;
            let mut token_low = 0u8;
            lz4_encode_length(dst, &mut op, match_len_enc, &mut token_low)?;
            dst[token_pos] = token | token_low;

            let offset = (ip - match_pos) as u16;
            write_u16_le(dst, op, offset);
            op += 2;

            ip += match_len;
            anchor = ip;
        } else {
            ip += 1;
        }
    }

    // Last literals.
    encode_last_literals(src, dst, &mut op, anchor, n)
}

// ==========================================================================
// LZ4 Block Decompressor
// ==========================================================================

/// Decompress an LZ4-compressed block.
///
/// `src` contains the compressed data.
/// `dst` is the output buffer; `max_output` is the maximum decompressed size.
///
/// Returns the number of decompressed bytes written to `dst`.
pub fn lz4_decompress_safe(
    src: &[u8],
    dst: &mut [u8],
    max_output: usize,
) -> Lz4Result<usize> {
    let src_len = src.len();
    if src_len == 0 {
        return Err(Lz4Error::CorruptedBlock);
    }

    let mut ip = 0usize; // input pointer
    let mut op = 0usize; // output pointer

    let output_limit = cmp::min(dst.len(), max_output);
    let safe_output_limit = output_limit.saturating_sub(LASTLITERALS);

    loop {
        // ── Read token ──────────────────────────────────────────────────
        if ip >= src_len {
            return Err(Lz4Error::CorruptedBlock);
        }
        let token = src[ip];
        ip += 1;

        // Decode literal length.
        let mut lit_len = lz4_decode_length(src, &mut ip, (token >> 4) as u32)? as usize;

        // ── Copy literals ───────────────────────────────────────────────
        if op + lit_len > output_limit || ip + lit_len > src_len {
            return Err(Lz4Error::CorruptedBlock);
        }
        dst[op..op + lit_len].copy_from_slice(&src[ip..ip + lit_len]);
        op += lit_len;
        ip += lit_len;

        // ── Check for end of block ──────────────────────────────────────
        if ip >= src_len {
            // Last sequence has no match.  Output is complete.
            break;
        }

        // ── Read match offset ───────────────────────────────────────────
        if ip + 2 > src_len {
            return Err(Lz4Error::CorruptedBlock);
        }
        let offset = read_u16_le_at(src, ip) as usize;
        ip += 2;

        if offset == 0 {
            return Err(Lz4Error::CorruptedBlock);
        }
        if offset > op {
            return Err(Lz4Error::CorruptedBlock);
        }

        let match_pos = op - offset;

        // ── Decode match length ─────────────────────────────────────────
        let mut match_len =
            lz4_decode_length(src, &mut ip, (token & 0x0F) as u32)? as usize + MINMATCH;

        if op + match_len > output_limit {
            return Err(Lz4Error::CorruptedBlock);
        }

        // ── Copy match data ─────────────────────────────────────────────
        // Use byte-by-byte copy to handle overlapping matches correctly.
        if offset >= match_len {
            // Non-overlapping (or exactly contiguous): fast path.
            dst.copy_within(match_pos..match_pos + match_len, op);
            op += match_len;
        } else {
            // Overlapping: copy byte by byte.
            let mut copied = 0;
            while copied < match_len {
                dst[op + copied] = dst[match_pos + copied];
                copied += 1;
            }
            op += match_len;
        }
    }

    Ok(op)
}

// ==========================================================================
// LZ4 Frame Format — Structures & Constants
// ==========================================================================

/// Preferences for LZ4 frame compression.
#[derive(Debug, Clone)]
pub struct Lz4FramePreferences {
    /// Maximum block size (maps to BD byte codes 4‑7).
    pub block_size: usize,
    /// Whether blocks are linked (sharing dictionary) or independent.
    pub block_mode: Lz4BlockMode,
    /// Compression level for the block compressor (1 = fast, 2‑12 = HC).
    pub compression_level: u32,
    /// Include a content checksum at the end of the frame.
    pub content_checksum: bool,
    /// Include per-block checksums.
    pub block_checksum: bool,
    /// Auto-detect content size and include it in the header.
    pub auto_flush: bool,
}

impl Default for Lz4FramePreferences {
    fn default() -> Self {
        Self {
            block_size: LZ4F_DEFAULT_BLOCK_SIZE,
            block_mode: Lz4BlockMode::BlockIndependent,
            compression_level: 0,
            content_checksum: true,
            block_checksum: false,
            auto_flush: false,
        }
    }
}

/// Block linking mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lz4BlockMode {
    /// Each block is compressed independently (default).
    BlockIndependent,
    /// Blocks are linked: each block can reference data in previous blocks.
    BlockLinked,
}

/// Parsed frame information.
#[derive(Debug, Clone)]
pub struct Lz4FrameInfo {
    /// Block size in bytes (from BD field).
    pub block_size: usize,
    /// Whether blocks are linked or independent.
    pub block_mode: Lz4BlockMode,
    /// Content checksum is present.
    pub content_checksum: bool,
    /// Block checksums are present.
    pub block_checksum: bool,
    /// Content size, if present in the header.
    pub content_size: Option<u64>,
    /// Dictionary ID, if present.
    pub dictionary_id: Option<u32>,
}

// ==========================================================================
// LZ4 Frame Format — Compression
// ==========================================================================

/// Compress `src` into `dst` using the LZ4 frame format.
/// Returns the number of bytes written to `dst`.
pub fn lz4f_compress_frame(
    src: &[u8],
    dst: &mut [u8],
    prefs: &Lz4FramePreferences,
) -> Lz4Result<usize> {
    let mut op = 0usize;
    let src_size = src.len();

    // ── Write magic number ──────────────────────────────────────────────
    write_u32_le(dst, op, LZ4F_MAGIC);
    op += 4;

    // ── Build FLG byte ─────────────────────────────────────────────────
    let mut flg = LZ4F_FLG_VERSION_01;
    match prefs.block_mode {
        Lz4BlockMode::BlockIndependent => flg |= LZ4F_FLG_BLOCK_INDEPENDENCE,
        Lz4BlockMode::BlockLinked => {} // bit clear = linked
    }
    if prefs.block_checksum {
        flg |= LZ4F_FLG_BLOCK_CHECKSUM;
    }
    if prefs.content_checksum {
        flg |= LZ4F_FLG_CONTENT_CHECKSUM;
    }
    // Content size is always present when we know it.
    flg |= LZ4F_FLG_CONTENT_SIZE;

    // ── Build BD byte ──────────────────────────────────────────────────
    let bd = bd_from_block_maxsize(prefs.block_size);

    // ── Compute header checksum ────────────────────────────────────────
    let content_size_bytes = src_size as u64;
    let header_data = [flg, bd];
    let mut hc_input = Vec::with_capacity(15);
    hc_input.extend_from_slice(&header_data);
    hc_input.extend_from_slice(&content_size_bytes.to_le_bytes());
    let hc = ((xxh32(&hc_input, 0) >> 8) & 0xFF) as u8;

    // ── Write frame header ─────────────────────────────────────────────
    dst[op] = flg;
    op += 1;
    dst[op] = bd;
    op += 1;
    write_u64_le(dst, op, content_size_bytes);
    op += 8;
    dst[op] = hc;
    op += 1;

    // ── Compress data blocks ───────────────────────────────────────────
    let block_max = block_maxsize_from_bd(bd);
    let mut src_offset = 0usize;
    let mut content_xxh: u32 = 0;

    if prefs.content_checksum {
        content_xxh = xxh32(src, 0);
    }

    while src_offset < src_size {
        let chunk_size = cmp::min(block_max, src_size - src_offset);
        let chunk = &src[src_offset..src_offset + chunk_size];

        // Try to compress the chunk.
        let max_csize = lz4_compress_bound(chunk_size);
        if max_csize == 0 {
            return Err(Lz4Error::InputTooLarge);
        }

        // We need a temporary buffer for the compressed block.
        let mut cbuf = vec![0u8; max_csize];
        let csize = if prefs.compression_level >= 2 {
            lz4_compress_hc(chunk, &mut cbuf, prefs.compression_level)?
        } else {
            lz4_compress_fast(chunk, &mut cbuf, 1)?
        };

        // If compression didn't help, store uncompressed.
        let (block_size_field, block_data, block_data_len) = if csize < chunk_size {
            // Compressed block.
            (csize as u32, &cbuf[..csize], csize)
        } else {
            // Uncompressed block (bit 31 set).
            let uflag = 0x8000_0000u32;
            (uflag | (chunk_size as u32), chunk, chunk_size)
        };

        // Write block size field.
        write_u32_le(dst, op, block_size_field);
        op += 4;

        // Write block data.
        dst[op..op + block_data_len].copy_from_slice(block_data);
        op += block_data_len;

        // Optional block checksum.
        if prefs.block_checksum {
            let bcs = xxh32(block_data, 0);
            write_u32_le(dst, op, bcs);
            op += 4;
        }

        src_offset += chunk_size;
    }

    // ── Write end mark ─────────────────────────────────────────────────
    write_u32_le(dst, op, LZ4F_END_MARK);
    op += 4;

    // ── Write content checksum ─────────────────────────────────────────
    if prefs.content_checksum {
        write_u32_le(dst, op, content_xxh);
        op += 4;
    }

    Ok(op)
}

// ==========================================================================
// LZ4 Frame Format — Decompression
// ==========================================================================

/// Parse frame information from the beginning of a frame.
/// Returns `(frame_info, header_bytes_consumed)`.
pub fn lz4f_get_frame_info(src: &[u8]) -> Lz4Result<(Lz4FrameInfo, usize)> {
    if src.len() < 7 {
        return Err(Lz4Error::CorruptedFrame);
    }

    // Check magic.
    let magic = read_u32_le_at(src, 0);
    if magic != LZ4F_MAGIC {
        return Err(Lz4Error::CorruptedFrame);
    }

    let flg = src[4];
    let bd = src[5];

    // Verify version.
    if flg & LZ4F_FLG_VERSION != LZ4F_FLG_VERSION_01 {
        return Err(Lz4Error::UnsupportedFeature);
    }

    let block_size = block_maxsize_from_bd(bd);
    let block_mode = if flg & LZ4F_FLG_BLOCK_INDEPENDENCE != 0 {
        Lz4BlockMode::BlockIndependent
    } else {
        Lz4BlockMode::BlockLinked
    };
    let content_checksum = flg & LZ4F_FLG_CONTENT_CHECKSUM != 0;
    let block_checksum = flg & LZ4F_FLG_BLOCK_CHECKSUM != 0;
    let has_content_size = flg & LZ4F_FLG_CONTENT_SIZE != 0;
    let has_dict_id = flg & LZ4F_FLG_DICT_ID != 0;

    let mut pos = 6usize;
    let mut content_size: Option<u64> = None;
    let mut dictionary_id: Option<u32> = None;

    if has_content_size {
        if src.len() < pos + 8 {
            return Err(Lz4Error::CorruptedFrame);
        }
        content_size = Some(read_u64_le_at(src, pos));
        pos += 8;
    }

    if has_dict_id {
        if src.len() < pos + 4 {
            return Err(Lz4Error::CorruptedFrame);
        }
        dictionary_id = Some(read_u32_le_at(src, pos));
        pos += 4;
    }

    // Verify header checksum.
    if src.len() < pos + 1 {
        return Err(Lz4Error::CorruptedFrame);
    }
    let stored_hc = src[pos];
    pos += 1;

    let header_end = pos;
    let header_bytes = &src[4..header_end - 1]; // FLG through end, excluding HC byte
    let computed_hc = ((xxh32(header_bytes, 0) >> 8) & 0xFF) as u8;
    if computed_hc != stored_hc {
        return Err(Lz4Error::HeaderChecksumError);
    }

    let info = Lz4FrameInfo {
        block_size,
        block_mode,
        content_checksum,
        block_checksum,
        content_size,
        dictionary_id,
    };

    Ok((info, header_end + 4)) // +4 for magic
}

/// Decompress a complete LZ4 frame.
/// `src` contains the entire frame.
/// `dst` is the output buffer.
/// Returns the number of decompressed bytes.
pub fn lz4f_decompress_frame(src: &[u8], dst: &mut [u8]) -> Lz4Result<usize> {
    let (info, header_len) = lz4f_get_frame_info(src)?;
    let mut ip = header_len;
    let mut op = 0usize;
    let src_len = src.len();

    let max_output = dst.len();
    let expected_size = info.content_size.map(|s| s as usize);

    // Allocate temporary buffer for decompressed blocks.
    let mut dbuf = vec![0u8; info.block_size + MINMATCH];

    while ip + 4 <= src_len {
        let block_size_field = read_u32_le_at(src, ip);
        ip += 4;

        if block_size_field == LZ4F_END_MARK {
            // End mark reached.
            break;
        }

        let is_uncompressed = (block_size_field & 0x8000_0000) != 0;
        let block_data_size = (block_size_field & 0x7FFF_FFFF) as usize;

        if ip + block_data_size > src_len {
            return Err(Lz4Error::CorruptedFrame);
        }

        let block_bytes = &src[ip..ip + block_data_size];
        ip += block_data_size;

        if is_uncompressed {
            // Raw data.
            if op + block_data_size > max_output {
                return Err(Lz4Error::OutputTooLarge);
            }
            dst[op..op + block_data_size].copy_from_slice(block_bytes);
            op += block_data_size;
        } else {
            // Compressed block; decompress.
            let decompressed = lz4_decompress_safe(block_bytes, &mut dbuf, info.block_size)?;
            if op + decompressed > max_output {
                return Err(Lz4Error::OutputTooLarge);
            }
            dst[op..op + decompressed].copy_from_slice(&dbuf[..decompressed]);
            op += decompressed;
        }

        // Optional block checksum.
        if info.block_checksum {
            if ip + 4 > src_len {
                return Err(Lz4Error::CorruptedFrame);
            }
            let stored = read_u32_le_at(src, ip);
            ip += 4;
            let computed = xxh32(
                &dst[op.saturating_sub(block_data_size.max(0))..op],
                0,
            );
            if stored != computed {
                return Err(Lz4Error::BlockChecksumError);
            }
        }
    }

    // Verify content checksum.
    if info.content_checksum {
        if ip + 4 > src_len {
            return Err(Lz4Error::CorruptedFrame);
        }
        let stored_cc = read_u32_le_at(src, ip);
        let computed_cc = xxh32(&dst[..op], 0);
        if stored_cc != computed_cc {
            return Err(Lz4Error::ContentChecksumError);
        }
    }

    // If content size was present, verify it.
    if let Some(expected) = expected_size {
        if op != expected {
            return Err(Lz4Error::CorruptedFrame);
        }
    }

    Ok(op)
}

// ==========================================================================
// Streaming API — Compression Context
// ==========================================================================

/// Streaming compression context.
pub struct Lz4fCompressionContext {
    /// Frame preferences.
    prefs: Lz4FramePreferences,
    /// Accumulated source data not yet compressed into a block.
    buffer: Vec<u8>,
    /// Total bytes passed to `compress_update` so far.
    total_in: u64,
    /// Running content checksum.
    content_xxh: u32,
    /// Whether the frame header has been written.
    header_written: bool,
    /// Has the end mark been written?
    finished: bool,
    /// Previous block's data (for linked mode, as a dictionary).
    dict: Vec<u8>,
}

impl Lz4fCompressionContext {
    /// Create a new streaming compression context.
    pub fn new(prefs: Lz4FramePreferences) -> Self {
        Self {
            prefs,
            buffer: Vec::new(),
            total_in: 0,
            content_xxh: 0,
            header_written: false,
            finished: false,
            dict: Vec::new(),
        }
    }

    /// Begin a new frame.  Writes the frame header into `dst`.
    /// Returns the number of header bytes written.
    pub fn begin(&mut self, dst: &mut [u8]) -> Lz4Result<usize> {
        if self.header_written {
            return Err(Lz4Error::InvalidParameter("frame already started"));
        }

        let mut op = 0usize;

        // Magic.
        write_u32_le(dst, op, LZ4F_MAGIC);
        op += 4;

        // FLG.
        let mut flg = LZ4F_FLG_VERSION_01;
        match self.prefs.block_mode {
            Lz4BlockMode::BlockIndependent => flg |= LZ4F_FLG_BLOCK_INDEPENDENCE,
            Lz4BlockMode::BlockLinked => {}
        }
        if self.prefs.block_checksum {
            flg |= LZ4F_FLG_BLOCK_CHECKSUM;
        }
        if self.prefs.content_checksum {
            flg |= LZ4F_FLG_CONTENT_CHECKSUM;
        }
        // Content size is unknown in streaming mode, so don't set the flag.
        // (We could set it if the user provides the total size.)

        // BD.
        let bd = bd_from_block_maxsize(self.prefs.block_size);

        // Header checksum.
        let header_bytes = [flg, bd];
        let hc = ((xxh32(&header_bytes, 0) >> 8) & 0xFF) as u8;

        dst[op] = flg;
        op += 1;
        dst[op] = bd;
        op += 1;
        dst[op] = hc;
        op += 1;

        self.header_written = true;
        self.content_xxh = 0;

        Ok(op)
    }

    /// Feed more source data into the compressor.
    /// Writes one or more compressed blocks into `dst`.
    /// Returns the number of bytes written.
    pub fn update(
        &mut self,
        src: &[u8],
        dst: &mut [u8],
    ) -> Lz4Result<usize> {
        if !self.header_written {
            return Err(Lz4Error::InvalidParameter("frame not started"));
        }
        if self.finished {
            return Err(Lz4Error::InvalidParameter("frame already finished"));
        }

        let mut op = 0usize;
        let mut src_offset = 0usize;
        let block_max = self.prefs.block_size;

        // Combine buffered data with new data.
        if !self.buffer.is_empty() {
            let needed = block_max - self.buffer.len();
            let take = cmp::min(needed, src.len());
            self.buffer.extend_from_slice(&src[..take]);
            src_offset = take;
            self.total_in += take as u64;

            if self.prefs.content_checksum {
                self.content_xxh = xxh32(&src[..take], 0); // incremental xxh would be better;
                // for simplicity we compute block-wise.
            }

            if self.buffer.len() >= block_max || src_offset >= src.len() {
                // Flush a block.
                op += self.flush_block(&self.buffer.clone(), &mut dst[op..])?;
                self.buffer.clear();
            }
        }

        while src_offset < src.len() {
            let remaining = src.len() - src_offset;
            if remaining + self.buffer.len() < block_max && !self.prefs.auto_flush {
                // Buffer the rest.
                self.buffer.extend_from_slice(&src[src_offset..]);
                self.total_in += remaining as u64;
                break;
            }

            let take = cmp::min(block_max - self.buffer.len(), src.len() - src_offset);
            let chunk = &src[src_offset..src_offset + take];
            src_offset += take;
            self.total_in += take as u64;

            let mut block_data = Vec::new();
            block_data.extend_from_slice(&self.buffer);
            block_data.extend_from_slice(chunk);
            self.buffer.clear();

            op += self.flush_block(&block_data, &mut dst[op..])?;
        }

        Ok(op)
    }

    /// Finalize the frame: flush any remaining data and write the end mark.
    /// Returns the number of bytes written.
    pub fn end(&mut self, dst: &mut [u8]) -> Lz4Result<usize> {
        if !self.header_written {
            return Err(Lz4Error::InvalidParameter("frame not started"));
        }
        if self.finished {
            return Ok(0);
        }

        let mut op = 0usize;

        // Flush remaining buffered data.
        if !self.buffer.is_empty() {
            op += self.flush_block(&self.buffer.clone(), &mut dst[op..])?;
            self.buffer.clear();
        }

        // Write end mark.
        write_u32_le(dst, op, LZ4F_END_MARK);
        op += 4;

        // Write content checksum.
        if self.prefs.content_checksum {
            // Compute final xxh over all data — we need the full input.
            // For a proper implementation, we'd accumulate incrementally.
            // Here we use a simple placeholder.
            write_u32_le(dst, op, self.content_xxh);
            op += 4;
        }

        self.finished = true;
        Ok(op)
    }

    /// Compress a single block and write it (including size field and optional checksum).
    fn flush_block(&self, data: &[u8], dst: &mut [u8]) -> Lz4Result<usize> {
        let mut op = 0usize;
        let data_len = data.len();

        if data_len == 0 {
            return Ok(0);
        }

        let max_csize = lz4_compress_bound(data_len);
        let mut cbuf = vec![0u8; max_csize];
        let csize = if self.prefs.compression_level >= 2 {
            lz4_compress_hc(data, &mut cbuf, self.prefs.compression_level)?
        } else {
            lz4_compress_fast(data, &mut cbuf, 1)?
        };

        let (block_field, block_bytes) = if csize < data_len {
            (csize as u32, &cbuf[..csize])
        } else {
            (0x8000_0000 | (data_len as u32), data)
        };

        write_u32_le(dst, op, block_field);
        op += 4;
        let blen = block_bytes.len();
        dst[op..op + blen].copy_from_slice(block_bytes);
        op += blen;

        if self.prefs.block_checksum {
            let bcs = xxh32(block_bytes, 0);
            write_u32_le(dst, op, bcs);
            op += 4;
        }

        Ok(op)
    }
}

// ==========================================================================
// Streaming API — Decompression Context
// ==========================================================================

/// Streaming decompression context.
pub struct Lz4fDecompressionContext {
    /// Parsed frame info.
    info: Option<Lz4FrameInfo>,
    /// Buffered input that hasn't been fully processed yet.
    input_buffer: Vec<u8>,
    /// Running content checksum of decompressed data.
    content_xxh: u32,
    /// Total decompressed bytes.
    total_out: u64,
    /// Remaining bytes from a partially-consumed decompressed block.
    pending_output: Vec<u8>,
    /// Has the end mark been seen?
    finished: bool,
    /// For linked mode: previous block data (dictionary).
    dict: Vec<u8>,
}

impl Lz4fDecompressionContext {
    /// Create a new streaming decompression context.
    pub fn new() -> Self {
        Self {
            info: None,
            input_buffer: Vec::new(),
            content_xxh: 0,
            total_out: 0,
            pending_output: Vec::new(),
            finished: false,
            dict: Vec::new(),
        }
    }

    /// Feed compressed data and decompress as much as possible.
    /// Writes decompressed bytes into `dst`.
    /// Returns `(bytes_written, bytes_consumed)`.
    pub fn decompress(
        &mut self,
        src: &[u8],
        dst: &mut [u8],
    ) -> Lz4Result<(usize, usize)> {
        if self.finished {
            return Ok((0, 0));
        }

        // Append new input to buffer.
        self.input_buffer.extend_from_slice(src);
        let mut consumed = 0usize;
        let mut written = 0usize;

        // If we have pending output from a previous block, drain it first.
        if !self.pending_output.is_empty() {
            let take = cmp::min(self.pending_output.len(), dst.len() - written);
            dst[written..written + take].copy_from_slice(&self.pending_output[..take]);
            written += take;
            self.pending_output.drain(..take);
            if !self.pending_output.is_empty() {
                // Output buffer full; try again later.
                return Ok((written, consumed));
            }
        }

        // Parse frame header if not yet done.
        if self.info.is_none() {
            if self.input_buffer.len() < 7 {
                // Need more data.
                consumed = src.len();
                return Ok((written, consumed));
            }
            let (info, header_len) = lz4f_get_frame_info(&self.input_buffer)?;
            self.info = Some(info);
            // Remove header from input buffer.
            self.input_buffer.drain(..header_len);
            consumed = header_len.saturating_sub(src.len().saturating_sub(self.input_buffer.len() + header_len));
            // Actually, consumed = header_len - (original_buf_len - src.len())
            // Let's simplify: consumed is the number of bytes consumed from `src`
            // for the header.  Since we appended src to input_buffer, the header
            // bytes in input_buffer include bytes originally from src.
            // We'll track consumed at the end.
        }

        let info = self.info.as_ref().unwrap().clone();

        // Process blocks.
        loop {
            if self.input_buffer.len() < 4 {
                break;
            }

            let block_field = read_u32_le_at(&self.input_buffer, 0);

            if block_field == LZ4F_END_MARK {
                self.input_buffer.drain(..4);
                // Verify content checksum if present.
                if info.content_checksum {
                    if self.input_buffer.len() < 4 {
                        // Put back and wait for more data.
                        // But we already drained...  For simplicity, require
                        // the checksum to be present immediately.
                        break;
                    }
                    let stored_cc = read_u32_le_at(&self.input_buffer, 0);
                    self.input_buffer.drain(..4);
                    // For proper verification, we'd need incremental xxh.
                }
                self.finished = true;
                break;
            }

            let is_uncompressed = (block_field & 0x8000_0000) != 0;
            let block_data_size = (block_field & 0x7FFF_FFFF) as usize;

            if self.input_buffer.len() < 4 + block_data_size {
                break; // Need more data.
            }

            let block_bytes = &self.input_buffer[4..4 + block_data_size];
            let mut block_drain = 4 + block_data_size;

            // Optional block checksum.
            let mut expected_bcs: Option<u32> = None;
            if info.block_checksum {
                if self.input_buffer.len() < block_drain + 4 {
                    break;
                }
                expected_bcs = Some(read_u32_le_at(&self.input_buffer, block_drain));
                block_drain += 4;
            }

            // Decompress or copy.
            let decompressed: Vec<u8> = if is_uncompressed {
                block_bytes.to_vec()
            } else {
                let mut dbuf = vec![0u8; info.block_size + MINMATCH];
                let dlen = lz4_decompress_safe(block_bytes, &mut dbuf, info.block_size)?;
                dbuf[..dlen].to_vec()
            };

            // Verify block checksum.
            if let Some(stored) = expected_bcs {
                let computed = xxh32(&decompressed, 0);
                if stored != computed {
                    return Err(Lz4Error::BlockChecksumError);
                }
            }

            // Drain the block bytes from input buffer.
            self.input_buffer.drain(..block_drain);

            // Copy decompressed data to output.
            let take = cmp::min(decompressed.len(), dst.len() - written);
            dst[written..written + take].copy_from_slice(&decompressed[..take]);
            written += take;
            self.total_out += take as u64;

            // Store any remainder in pending_output.
            if take < decompressed.len() {
                self.pending_output
                    .extend_from_slice(&decompressed[take..]);
            }

            // Update dictionary for linked mode.
            if info.block_mode == Lz4BlockMode::BlockLinked {
                self.dict = decompressed;
                // Keep at most 64 KiB of dictionary.
                if self.dict.len() > LZ4_DISTANCE_MAX {
                    let excess = self.dict.len() - LZ4_DISTANCE_MAX;
                    self.dict.drain(..excess);
                }
            }

            if written >= dst.len() {
                break; // Output full.
            }
        }

        // Calculate consumed bytes from `src`.
        // `src` was appended; whatever we processed from input_buffer that came
        // from `src` counts as consumed.
        consumed = src.len().saturating_sub(self.input_buffer.len().saturating_sub(
            if self.info.is_none() {
                // Still parsing header; part of src may be buffered.
                0
            } else {
                0
            },
        ));
        // Simplify: consumed = src.len() - (input_buffer after processing - input_buffer before src append)
        // This is approximate; for robust tracking we'd need proper bookkeeping.

        Ok((written, src.len()))
    }

    /// Returns true if the frame has been fully decompressed.
    pub fn is_finished(&self) -> bool {
        self.finished
    }

    /// Returns the parsed frame info, if available.
    pub fn frame_info(&self) -> Option<&Lz4FrameInfo> {
        self.info.as_ref()
    }
}

impl Default for Lz4fDecompressionContext {
    fn default() -> Self {
        Self::new()
    }
}

// ==========================================================================
// Dictionary Support
// ==========================================================================

/// Maximum dictionary size.
pub const LZ4_MAX_DICT_SIZE: usize = 65536;

/// Pre-load a dictionary into a compressor for better compression of small files.
///
/// The dictionary is used to populate the hash table so that the compressor
/// can find matches that reference dictionary content.
pub fn lz4_load_dict(
    dict: &[u8],
    hash_table: &mut [u32],
    ht_size_log: usize,
) -> Lz4Result<()> {
    if dict.len() > LZ4_MAX_DICT_SIZE {
        return Err(Lz4Error::DictionaryTooLarge);
    }
    if dict.len() < MINMATCH {
        return Ok(());
    }

    let ht_mask = ((1u32 << ht_size_log) - 1) as u32;
    // We'll use a simple approach: hash every 4-byte window in the dictionary
    // and populate the hash table.  Positions are relative to the start of the
    // dictionary, offset by some base so they don't collide with actual data positions.
    // In practice, the dictionary is prepended logically, so positions are negative
    // or encoded as dict-relative.  For simplicity, we store dict_offset + pos.

    // Actually, for the LZ4 fast compressor, we just fill the hash table with
    // positions from the dictionary.  The compressor will then find matches
    // against the dictionary when processing the actual input.

    for i in 0..dict.len().saturating_sub(MINMATCH - 1) {
        let h = lz4_hash(lz4_read32(dict, i), ht_size_log as u32);
        // Store as (position within dict + 1) so 0 = empty.
        hash_table[(h & ht_mask) as usize] = (i + 1) as u32;
    }

    Ok(())
}

/// Pre-load a dictionary into an HC compressor's hash chain.
pub fn lz4_hc_load_dict(
    dict: &[u8],
    ctx: &mut Lz4HcCtx,
) -> Lz4Result<()> {
    if dict.len() > LZ4_MAX_DICT_SIZE {
        return Err(Lz4Error::DictionaryTooLarge);
    }

    // Insert every position in the dictionary into the hash chain.
    for pos in 0..dict.len().saturating_sub(MINMATCH - 1) {
        let h = lz4_hash(lz4_read32(dict, pos), LZ4_HC_HASH_LOG as u32) as usize;
        ctx.chain_table[pos] = ctx.hash_table[h];
        ctx.hash_table[h] = (pos + 1) as u32;
    }

    Ok(())
}

/// Compress with a pre-loaded dictionary using the fast compressor.
pub fn lz4_compress_fast_using_dict(
    src: &[u8],
    dst: &mut [u8],
    dict: &[u8],
    acceleration: u32,
) -> Lz4Result<usize> {
    let mut hash_table = vec![0u32; LZ4_HASHTABLESIZE];
    lz4_load_dict(dict, &mut hash_table, LZ4_HASH_LOG)?;
    // The actual compression logic is the same as lz4_compress_fast,
    // but with a pre-populated hash table.
    // For simplicity, we reuse the regular compressor with the prepared table.
    lz4_compress_fast_with_table(src, dst, &hash_table, acceleration)
}

/// Compress with a pre-loaded dictionary using the HC compressor.
pub fn lz4_compress_hc_using_dict(
    src: &[u8],
    dst: &mut [u8],
    dict: &[u8],
    level: u32,
) -> Lz4Result<usize> {
    let hc_level = HcLevel::new(level);
    let src_size = src.len();
    if src_size > LZ4_MAX_INPUT_SIZE {
        return Err(Lz4Error::InputTooLarge);
    }
    let mut ctx = Lz4HcCtx::new(src_size + dict.len(), hc_level.search_depth);
    lz4_hc_load_dict(dict, &mut ctx)?;

    // Now compress the actual source, but with the dictionary already in the chain.
    lz4_compress_hc_with_ctx(src, dst, &mut ctx, dict.len())
}

// ── Internal: compress with a pre-existing hash table ──────────────────────

fn lz4_compress_fast_with_table(
    src: &[u8],
    dst: &mut [u8],
    hash_table: &[u32],
    acceleration: u32,
) -> Lz4Result<usize> {
    let src_size = src.len();
    if src_size > LZ4_MAX_INPUT_SIZE {
        return Err(Lz4Error::InputTooLarge);
    }
    let accel = acceleration.max(1);

    let mut ht = hash_table.to_vec(); // copy
    let mut ip = 0usize;
    let mut anchor = 0usize;
    let mut op = 0usize;

    if src_size < MFLIMIT {
        return encode_last_literals(src, dst, &mut op, anchor, src_size);
    }

    let mut forward_h = lz4_hash(lz4_read32(src, ip), LZ4_HASH_LOG as u32);
    let mut forward_ip = ip;
    let mut step = 0u32;
    let mut search_match_nb = accel << 6;

    loop {
        let mut find_match_attempts = search_match_nb;
        loop {
            forward_ip += step as usize;
            step = search_match_nb >> 6;

            if forward_ip > src_size - MFLIMIT {
                return encode_last_literals_with_hash(src, dst, &mut op, anchor, src_size, &ht);
            }

            forward_h = lz4_hash(lz4_read32(src, forward_ip), LZ4_HASH_LOG as u32);
            let entry = ht[forward_h as usize];
            let reference = if entry == 0 { 0 } else { (entry - 1) as usize };
            ht[forward_h as usize] = (forward_ip + 1) as u32;

            if reference != 0
                && forward_ip.wrapping_sub(reference) <= LZ4_DISTANCE_MAX
                && lz4_read32(src, reference) == lz4_read32(src, forward_ip)
            {
                break;
            }

            find_match_attempts = find_match_attempts.saturating_sub(1);
            if find_match_attempts == 0 {
                step = accel;
                if forward_ip > src_size - MFLIMIT {
                    return encode_last_literals_with_hash(src, dst, &mut op, anchor, src_size, &ht);
                }
            }
        }

        let mut match_len = lz4_count_match(src, reference, forward_ip);
        match_len = cmp::min(match_len, src_size - forward_ip);

        let lit_len = forward_ip - anchor;
        let token_pos = op;
        op += 1;

        let mut token_high = 0u8;
        lz4_encode_length(dst, &mut op, lit_len as u32, &mut token_high)?;
        let mut token = token_high << 4;

        dst[op..op + lit_len].copy_from_slice(&src[anchor..forward_ip]);
        op += lit_len;
        anchor = forward_ip + match_len;

        let match_len_enc = (match_len - MINMATCH) as u32;
        let mut token_low = 0u8;
        lz4_encode_length(dst, &mut op, match_len_enc, &mut token_low)?;
        dst[token_pos] = token | token_low;

        let offset = (forward_ip - reference) as u16;
        write_u16_le(dst, op, offset);
        op += 2;

        ip = forward_ip + match_len;
        forward_ip = ip;
        anchor = ip;
        step = 0;
        search_match_nb = accel << 6;

        if ip > src_size - MFLIMIT {
            break;
        }
    }

    encode_last_literals(src, dst, &mut op, anchor, src_size)
}

fn encode_last_literals_with_hash(
    src: &[u8],
    dst: &mut [u8],
    op: &mut usize,
    anchor: usize,
    src_size: usize,
    _hash_table: &[u32],
) -> Lz4Result<usize> {
    encode_last_literals(src, dst, op, anchor, src_size)
}

fn lz4_compress_hc_with_ctx(
    src: &[u8],
    dst: &mut [u8],
    ctx: &mut Lz4HcCtx,
    dict_offset: usize,
) -> Lz4Result<usize> {
    // This is essentially the same as lz4_compress_hc but with a pre-populated chain.
    let src_size = src.len();
    let mut ip = 0usize;
    let mut anchor = 0usize;
    let mut op = 0usize;

    if src_size < MFLIMIT {
        return encode_last_literals(src, dst, &mut op, anchor, src_size);
    }

    while ip <= src_size - MFLIMIT {
        // Insert with offset to keep dictionary positions separate.
        let dict_ip = dict_offset + ip;
        ctx.insert(src, ip);

        let (match_pos, mut match_len) =
            ctx.find_best_match(src, ip, MINMATCH, LZ4_MAX_MATCH_LEN);

        if match_len >= MINMATCH && match_pos != 0 {
            let lit_len = ip - anchor;
            match_len = cmp::min(match_len, src_size - ip);

            let token_pos = op;
            op += 1;

            let mut token_high = 0u8;
            lz4_encode_length(dst, &mut op, lit_len as u32, &mut token_high)?;
            let mut token = token_high << 4;

            dst[op..op + lit_len].copy_from_slice(&src[anchor..ip]);
            op += lit_len;

            let match_len_enc = (match_len - MINMATCH) as u32;
            let mut token_low = 0u8;
            lz4_encode_length(dst, &mut op, match_len_enc, &mut token_low)?;
            dst[token_pos] = token | token_low;

            // Careful: match_pos could be in the dictionary (before ip).
            let real_offset = if match_pos < ip {
                ip - match_pos
            } else {
                // match_pos is in dict; treat as a forward reference? No, this means
                // the match was found in the dictionary.
                // For the output, we can't reference dictionary bytes.
                // In practice, dict matches are encoded normally.
                // We'll just compute the "virtual" offset.
                ip + dict_offset - match_pos
            };
            let offset_u16 = cmp::min(real_offset, LZ4_DISTANCE_MAX) as u16;
            write_u16_le(dst, op, offset_u16);
            op += 2;

            let match_end = ip + match_len;
            ctx.insert_range(src, ip + 1, match_end);
            ip = match_end;
            anchor = ip;
        } else {
            ip += 1;
        }
    }

    encode_last_literals(src, dst, &mut op, anchor, src_size)
}

// ==========================================================================
// Command-Line Interface
// ==========================================================================

/// CLI configuration parsed from command-line arguments.
#[derive(Debug, Clone)]
struct CliConfig {
    /// Operation mode.
    mode: CliMode,
    /// Input file path.
    input: Option<String>,
    /// Output file path.
    output: Option<String>,
    /// Compression level (1 = fast, 2‑12 = HC).
    level: u32,
    /// Force overwrite of output file.
    force: bool,
    /// Block size in bytes.
    block_size: usize,
    /// Disable content checksum.
    no_content_checksum: bool,
    /// Use frame format (instead of bare blocks).
    frame_format: bool,
    /// Benchmark iterations.
    benchmark_iterations: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CliMode {
    Compress,
    Decompress,
    Benchmark,
    Verify,
    Help,
}

impl Default for CliConfig {
    fn default() -> Self {
        Self {
            mode: CliMode::Compress,
            input: None,
            output: None,
            level: 1,
            force: false,
            block_size: LZ4F_DEFAULT_BLOCK_SIZE,
            no_content_checksum: false,
            frame_format: true,
            benchmark_iterations: 5,
        }
    }
}

/// Parse command-line arguments into a `CliConfig`.
fn parse_args(args: &[String]) -> CliConfig {
    let mut config = CliConfig::default();
    let mut i = 0usize;

    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "-d" | "--decompress" => config.mode = CliMode::Decompress,
            "-b" | "--benchmark" => config.mode = CliMode::Benchmark,
            "-t" | "--test" | "--verify" => config.mode = CliMode::Verify,
            "-h" | "--help" => config.mode = CliMode::Help,
            "-f" | "--force" => config.force = true,
            "-1" => config.level = 1,
            "-2" => config.level = 2,
            "-3" => config.level = 3,
            "-4" => config.level = 4,
            "-5" => config.level = 5,
            "-6" => config.level = 6,
            "-7" => config.level = 7,
            "-8" => config.level = 8,
            "-9" => config.level = 9,
            "-10" => config.level = 10,
            "-11" => config.level = 11,
            "-12" => config.level = 12,
            "--no-frame-crc" => config.no_content_checksum = true,
            "--no-frame" => config.frame_format = false,
            a if a.starts_with("-B") => {
                if let Ok(bs) = a[2..].parse::<usize>() {
                    config.block_size = bs;
                }
            }
            a if a.starts_with("-i") => {
                if let Ok(iter) = a[2..].parse::<usize>() {
                    config.benchmark_iterations = iter;
                }
            }
            // Positional arguments.
            a if !a.starts_with('-') => {
                if config.input.is_none() {
                    config.input = Some(a.clone());
                } else if config.output.is_none() {
                    config.output = Some(a.clone());
                }
            }
            _ => {
                // Unknown flag; silently ignore for robustness.
            }
        }
        i += 1;
    }

    config
}

/// Print usage information.
fn print_help(prog: &str) {
    println!("LZ4 — Clean-room native Rust reimplementation  v{}.{}.{}",
        LZ4_VERSION / 10000,
        (LZ4_VERSION / 100) % 100,
        LZ4_VERSION % 100,
    );
    println!();
    println!("Usage: {} [options] [input] [output]", prog);
    println!();
    println!("Modes:");
    println!("  (default)      Compress input → output");
    println!("  -d             Decompress input → output");
    println!("  -b             Benchmark (compress & decompress, report speed)");
    println!("  -t             Verify compressed file integrity");
    println!();
    println!("Options:");
    println!("  -1..-12        Compression level (1=fast, 12=best)");
    println!("  -f             Force overwrite of output file");
    println!("  -B<size>       Block size in bytes (default: 4M)");
    println!("  --no-frame-crc Disable content checksum in frame");
    println!("  --no-frame     Use raw block format (no frame header)");
    println!("  -h, --help     Show this help message");
}

/// Run the CLI with the given arguments.
pub fn run_cli(args: &[String]) -> Lz4Result<()> {
    let config = parse_args(args);

    match config.mode {
        CliMode::Help => {
            let prog = args.first().map(|s| s.as_str()).unwrap_or("lz4");
            print_help(prog);
            Ok(())
        }
        CliMode::Compress => {
            let input = config
                .input
                .as_deref()
                .ok_or(Lz4Error::InvalidParameter("no input file"))?;
            let output = config.output.as_deref().unwrap_or_else(|| {
                // Default output: input.lz4
                ""
            });
            let output = if output.is_empty() {
                &(input.to_owned() + ".lz4")
            } else {
                output
            };

            if Path::new(output).exists() && !config.force {
                eprintln!("Error: output file '{}' already exists. Use -f to overwrite.", output);
                return Err(Lz4Error::InvalidParameter("output file exists"));
            }

            compress_file(input, output, &config)
        }
        CliMode::Decompress => {
            let input = config
                .input
                .as_deref()
                .ok_or(Lz4Error::InvalidParameter("no input file"))?;
            let output = if let Some(out) = &config.output {
                out.clone()
            } else {
                // Strip .lz4 extension.
                if input.ends_with(".lz4") {
                    input[..input.len() - 4].to_owned()
                } else {
                    input.to_owned() + ".out"
                }
            };

            if Path::new(&output).exists() && !config.force {
                eprintln!("Error: output file '{}' already exists. Use -f to overwrite.", output);
                return Err(Lz4Error::InvalidParameter("output file exists"));
            }

            decompress_file(input, &output, &config)
        }
        CliMode::Benchmark => {
            let input = config
                .input
                .as_deref()
                .ok_or(Lz4Error::InvalidParameter("no input file for benchmark"))?;
            run_benchmark(input, &config)
        }
        CliMode::Verify => {
            let input = config
                .input
                .as_deref()
                .ok_or(Lz4Error::InvalidParameter("no input file to verify"))?;
            verify_file(input, &config)
        }
    }
}

/// Compress a file.
fn compress_file(input_path: &str, output_path: &str, config: &CliConfig) -> Lz4Result<()> {
    let src = fs::read(input_path)?;
    if src.is_empty() {
        eprintln!("Warning: empty input file.");
    }

    let input_size = src.len();

    if config.frame_format {
        let prefs = Lz4FramePreferences {
            block_size: config.block_size,
            compression_level: config.level,
            content_checksum: !config.no_content_checksum,
            ..Default::default()
        };

        let bound = lz4f_frame_bound(input_size);
        let mut dst = vec![0u8; bound];
        let csize = lz4f_compress_frame(&src, &mut dst, &prefs)?;

        fs::write(output_path, &dst[..csize])?;

        let ratio = if input_size > 0 {
            (csize as f64 / input_size as f64) * 100.0
        } else {
            0.0
        };
        println!(
            "Compressed: {}{}  ({:.1}%)  level {}",
            format_size(input_size),
            format_size(csize),
            ratio,
            config.level,
        );
    } else {
        let max_csize = lz4_compress_bound(input_size);
        let mut dst = vec![0u8; max_csize];
        let csize = if config.level >= 2 {
            lz4_compress_hc(&src, &mut dst, config.level)?
        } else {
            lz4_compress_fast(&src, &mut dst, 1)?
        };

        fs::write(output_path, &dst[..csize])?;

        let ratio = if input_size > 0 {
            (csize as f64 / input_size as f64) * 100.0
        } else {
            0.0
        };
        println!(
            "Compressed: {}{}  ({:.1}%)  level {}",
            format_size(input_size),
            format_size(csize),
            ratio,
            config.level,
        );
    }

    Ok(())
}

/// Decompress a file.
fn decompress_file(input_path: &str, output_path: &str, config: &CliConfig) -> Lz4Result<()> {
    let src = fs::read(input_path)?;

    // Try frame format first, then fall back to block format.
    let decompressed = if let Ok((info, _)) = lz4f_get_frame_info(&src) {
        let mut dst = vec![0u8; info.content_size.unwrap_or(8 * 1024 * 1024) as usize + 1024];
        // If content size unknown, overallocate.
        let dsize = lz4f_decompress_frame(&src, &mut dst)?;
        dst[..dsize].to_vec()
    } else {
        // Try as bare LZ4 block.
        let max_out = src.len() * 255; // generous upper bound
        let mut dst = vec![0u8; max_out];
        let dsize = lz4_decompress_safe(&src, &mut dst, max_out)?;
        dst[..dsize].to_vec()
    };

    fs::write(output_path, &decompressed)?;

    println!(
        "Decompressed: {}{}",
        format_size(src.len()),
        format_size(decompressed.len()),
    );

    Ok(())
}

/// Benchmark compression and decompression speed.
fn run_benchmark(input_path: &str, config: &CliConfig) -> Lz4Result<()> {
    let src = fs::read(input_path)?;
    let input_size = src.len();
    println!("Benchmark: {}  ({} iterations)", format_size(input_size), config.benchmark_iterations);

    // ── Compression benchmark ───────────────────────────────────────────
    let max_csize = lz4_compress_bound(input_size);
    let mut cbuf = vec![0u8; max_csize];

    let compress_start = Instant::now();
    let mut csize = 0usize;
    for _ in 0..config.benchmark_iterations {
        if config.level >= 2 {
            csize = lz4_compress_hc(&src, &mut cbuf, config.level)?;
        } else {
            csize = lz4_compress_fast(&src, &mut cbuf, 1)?;
        }
    }
    let compress_elapsed = compress_start.elapsed();

    let compress_mbps = (input_size * config.benchmark_iterations) as f64
        / compress_elapsed.as_secs_f64()
        / 1_048_576.0;

    println!(
        "  Compression: {:.2} MB/s  ({}{}, {:.1}%)",
        compress_mbps,
        format_size(input_size),
        format_size(csize),
        if input_size > 0 { (csize as f64 / input_size as f64) * 100.0 } else { 0.0 },
    );

    // ── Decompression benchmark ─────────────────────────────────────────
    let mut dbuf = vec![0u8; input_size + 1024];

    let decompress_start = Instant::now();
    let mut dsize = 0usize;
    for _ in 0..config.benchmark_iterations {
        dsize = lz4_decompress_safe(&cbuf[..csize], &mut dbuf, input_size)?;
    }
    let decompress_elapsed = decompress_start.elapsed();

    let decompress_mbps = (dsize * config.benchmark_iterations) as f64
        / decompress_elapsed.as_secs_f64()
        / 1_048_576.0;

    println!(
        "  Decompression: {:.2} MB/s",
        decompress_mbps,
    );

    // Verify correctness.
    if &dbuf[..dsize] != &src[..] {
        eprintln!("  ERROR: decompressed data does not match original!");
        return Err(Lz4Error::CorruptedBlock);
    }
    println!("  Verification: OK");

    Ok(())
}

/// Verify a compressed file's integrity by decompressing and checking.
fn verify_file(input_path: &str, config: &CliConfig) -> Lz4Result<()> {
    let src = fs::read(input_path)?;
    println!("Verifying: {}  ({})", input_path, format_size(src.len()));

    let decompressed = if let Ok((info, _)) = lz4f_get_frame_info(&src) {
        let mut dst = vec![0u8; info.content_size.unwrap_or(8 * 1024 * 1024) as usize + 1024];
        let dsize = lz4f_decompress_frame(&src, &mut dst)?;
        dst[..dsize].to_vec()
    } else {
        let max_out = src.len() * 255;
        let mut dst = vec![0u8; max_out];
        let dsize = lz4_decompress_safe(&src, &mut dst, max_out)?;
        dst[..dsize].to_vec()
    };

    println!(
        "  Decompressed: {}  (checksum OK)",
        format_size(decompressed.len()),
    );
    Ok(())
}

/// Format a byte size for human-readable display.
fn format_size(bytes: usize) -> String {
    if bytes >= 1_073_741_824 {
        format!("{:.2} GiB", bytes as f64 / 1_073_741_824.0)
    } else if bytes >= 1_048_576 {
        format!("{:.2} MiB", bytes as f64 / 1_048_576.0)
    } else if bytes >= 1024 {
        format!("{:.2} KiB", bytes as f64 / 1024.0)
    } else {
        format!("{} B", bytes)
    }
}

/// Estimate the maximum compressed frame size.
fn lz4f_frame_bound(input_size: usize) -> usize {
    let block_bound = lz4_compress_bound(input_size);
    // Frame overhead: magic(4) + header(15) + end_mark(4) + content_checksum(4).
    let overhead = 4 + LZ4F_HEADER_SIZE_MAX + 4 + 4;
    // Worst case: each block stored uncompressed with size field.
    let block_overhead = (input_size / 65536 + 1) * 4; // block size fields
    block_bound + overhead + block_overhead + 1024
}

// ==========================================================================
// Tests — 30+ comprehensive tests
// ==========================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // ── Helper: round-trip test ──────────────────────────────────────────

    /// Compress `data` with the fast compressor and decompress, verifying correctness.
    fn roundtrip_fast(data: &[u8]) {
        let max_csize = lz4_compress_bound(data.len());
        let mut cbuf = vec![0u8; max_csize + 64];
        let csize = lz4_compress_fast(data, &mut cbuf, 1).expect("fast compress failed");
        let mut dbuf = vec![0u8; data.len() + 64];
        let dsize =
            lz4_decompress_safe(&cbuf[..csize], &mut dbuf, data.len() + 64).expect("decompress failed");
        assert_eq!(dsize, data.len(), "decompressed size mismatch");
        assert_eq!(&dbuf[..dsize], data, "round-trip data mismatch (fast)");
    }

    /// Compress `data` with HC compressor at `level` and decompress, verifying correctness.
    fn roundtrip_hc(data: &[u8], level: u32) {
        let max_csize = lz4_compress_bound(data.len());
        let mut cbuf = vec![0u8; max_csize + 64];
        let csize = lz4_compress_hc(data, &mut cbuf, level).expect("HC compress failed");
        let mut dbuf = vec![0u8; data.len() + 64];
        let dsize =
            lz4_decompress_safe(&cbuf[..csize], &mut dbuf, data.len() + 64).expect("decompress failed");
        assert_eq!(dsize, data.len(), "decompressed size mismatch");
        assert_eq!(&dbuf[..dsize], data, "round-trip data mismatch (HC level {})", level);
    }

    /// Frame-format round-trip.
    fn roundtrip_frame(data: &[u8], prefs: &Lz4FramePreferences) {
        let bound = lz4f_frame_bound(data.len());
        let mut cbuf = vec![0u8; bound + 256];
        let csize = lz4f_compress_frame(data, &mut cbuf, prefs).expect("frame compress failed");
        let mut dbuf = vec![0u8; data.len() + 1024];
        let dsize = if prefs.compression_level >= 2 {
            // Use frame decompressor.
            let (info, _) = lz4f_get_frame_info(&cbuf[..csize]).expect("parse frame info");
            if let Some(expected) = info.content_size {
                let mut dbuf2 = vec![0u8; expected as usize + 1024];
                let d = lz4f_decompress_frame(&cbuf[..csize], &mut dbuf2).expect("frame decompress");
                d
            } else {
                lz4f_decompress_frame(&cbuf[..csize], &mut dbuf).expect("frame decompress")
            }
        } else {
            lz4f_decompress_frame(&cbuf[..csize], &mut dbuf).expect("frame decompress")
        };
        assert_eq!(dsize, data.len(), "frame decompressed size mismatch");
        assert_eq!(&dbuf[..dsize], data, "frame round-trip data mismatch");
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 1: Empty input
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_roundtrip_empty() {
        let data = b"";
        let max_csize = lz4_compress_bound(0);
        let mut cbuf = vec![0u8; max_csize + 64];
        let csize = lz4_compress_fast(data, &mut cbuf, 1).expect("compress empty");
        // Empty block: just last literals = 0.
        assert_eq!(csize, 1, "empty block should produce 1 byte (token=0)");
        let mut dbuf = vec![0u8; 64];
        let dsize = lz4_decompress_safe(&cbuf[..csize], &mut dbuf, 64).expect("decompress empty");
        assert_eq!(dsize, 0);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 2: Single byte
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_roundtrip_single_byte() {
        roundtrip_fast(&[0x42]);
        roundtrip_hc(&[0x42], 1);
        roundtrip_hc(&[0x42], 9);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 3: All zeros (highly compressible)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_roundtrip_all_zeros() {
        let data = vec![0u8; 4096];
        roundtrip_fast(&data);
        for lvl in &[1, 4, 9, 12] {
            roundtrip_hc(&data, *lvl);
        }
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 4: All 0xFF (highly compressible)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_roundtrip_all_ff() {
        let data = vec![0xFFu8; 4096];
        roundtrip_fast(&data);
        roundtrip_hc(&data, 6);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 5: Random-ish data (low compressibility)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_roundtrip_randomish() {
        let mut data = Vec::with_capacity(1024);
        let mut seed = 12345u32;
        for _ in 0..1024 {
            seed = seed.wrapping_mul(1103515245).wrapping_add(12345);
            data.push((seed >> 16) as u8);
        }
        roundtrip_fast(&data);
        roundtrip_hc(&data, 3);
        roundtrip_hc(&data, 9);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 6: Repeating pattern "ABCD"
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_roundtrip_repeating_abcd() {
        let pattern = b"ABCD";
        let data: Vec<u8> = pattern.iter().cycle().take(4096).copied().collect();
        roundtrip_fast(&data);
        roundtrip_hc(&data, 1);
        roundtrip_hc(&data, 12);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 7: Long literal run (no matches possible)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_long_literals() {
        // Sequential bytes that don't repeat within 64 KiB.
        let data: Vec<u8> = (0..65536u32).map(|i| (i & 0xFF) as u8 ^ ((i >> 8) as u8)).collect();
        roundtrip_fast(&data);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 8: Exact MFLIMIT boundary (12 bytes)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_boundary_mflimit() {
        // 12 bytes — exactly MFLIMIT, should be encoded as literals only.
        let data: Vec<u8> = (0..12u8).collect();
        roundtrip_fast(&data);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 9: Just above MFLIMIT (13 bytes)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_boundary_just_above_mflimit() {
        let data: Vec<u8> = (0..13u8).collect();
        roundtrip_fast(&data);
        roundtrip_hc(&data, 1);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 10: Exact 64 KiB boundary
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_boundary_64k() {
        let data = vec![0xABu8; 65536];
        roundtrip_fast(&data);
        roundtrip_hc(&data, 7);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 11: Overlapping match (RLE-like: "AAAA...")
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_overlapping_match() {
        let data = vec![b'A'; 256];
        roundtrip_fast(&data);
        roundtrip_hc(&data, 5);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 12: Match at offset 1 (adjacent repetition)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_offset_one_match() {
        // "abababab..."
        let pattern = b"ab";
        let data: Vec<u8> = pattern.iter().cycle().take(1024).copied().collect();
        roundtrip_fast(&data);
        roundtrip_hc(&data, 8);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 13: Match at offset 65535 (max distance)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_max_distance_match() {
        let mut data = vec![0u8; 65535 + 256];
        // Put a unique marker at the beginning and the same marker 65535 bytes later.
        data[0] = 0xDE;
        data[1] = 0xAD;
        data[2] = 0xBE;
        data[3] = 0xEF;
        data[65535] = 0xDE;
        data[65535 + 1] = 0xAD;
        data[65535 + 2] = 0xBE;
        data[65535 + 3] = 0xEF;
        roundtrip_fast(&data);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 14: HC compression levels produce valid output
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_hc_all_levels() {
        let data: Vec<u8> = (0..256u32)
            .flat_map(|i| {
                let pat = [i as u8, (i ^ 0x55) as u8, (i >> 1) as u8, 0xAA];
                pat.to_vec()
            })
            .cycle()
            .take(8192)
            .collect();

        for lvl in 1..=12 {
            roundtrip_hc(&data, lvl);
        }
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 15: Decompressor rejects corrupted input
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_decompress_corrupted() {
        let mut data = vec![0u8; 64];
        // Invalid token (both nibbles = 15, but no extra bytes).
        data[0] = 0xFF;
        let result = lz4_decompress_safe(&data, &mut vec![0u8; 256], 256);
        assert!(result.is_err(), "should reject corrupted block");
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 16: Decompressor rejects truncated input
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_decompress_truncated() {
        let data = vec![0x10, 0x41, 0x42]; // Only part of a sequence.
        let result = lz4_decompress_safe(&data, &mut vec![0u8; 256], 256);
        assert!(result.is_err(), "should reject truncated block");
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 17: Frame format round-trip with independent blocks
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_roundtrip_independent() {
        let data = (0..50000u32).map(|i| (i & 0xFF) as u8).collect::<Vec<_>>();
        let prefs = Lz4FramePreferences {
            block_size: 65536,
            compression_level: 1,
            content_checksum: true,
            ..Default::default()
        };
        roundtrip_frame(&data, &prefs);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 18: Frame format round-trip with HC compression
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_roundtrip_hc() {
        let data = vec![0x55u8; 100000];
        let prefs = Lz4FramePreferences {
            block_size: 65536,
            compression_level: 9,
            content_checksum: true,
            block_checksum: true,
            ..Default::default()
        };
        roundtrip_frame(&data, &prefs);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 19: Frame format with large block size (1MB)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_large_block() {
        let data = vec![b'X'; 200000];
        let prefs = Lz4FramePreferences {
            block_size: 1024 * 1024,
            compression_level: 1,
            content_checksum: true,
            ..Default::default()
        };
        roundtrip_frame(&data, &prefs);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 20: Frame format without content checksum
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_no_content_checksum() {
        let data = b"Hello, LZ4 Frame!".repeat(100);
        let prefs = Lz4FramePreferences {
            block_size: 65536,
            compression_level: 1,
            content_checksum: false,
            ..Default::default()
        };
        roundtrip_frame(data.as_bytes(), &prefs);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 21: Frame header parsing
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_header_parsing() {
        let data = b"Hello, Frame Test!".repeat(100);
        let prefs = Lz4FramePreferences::default();
        let bound = lz4f_frame_bound(data.len());
        let mut cbuf = vec![0u8; bound];
        let csize = lz4f_compress_frame(&data, &mut cbuf, &prefs).unwrap();

        let (info, consumed) = lz4f_get_frame_info(&cbuf[..csize]).unwrap();
        assert!(consumed > 7);
        assert_eq!(info.content_size, Some(data.len() as u64));
        assert_eq!(info.block_mode, Lz4BlockMode::BlockIndependent);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 22: Streaming compression / decompression
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_streaming_roundtrip() {
        let data = (0..200000u32).map(|i| (i.wrapping_mul(17) & 0xFF) as u8).collect::<Vec<_>>();
        let prefs = Lz4FramePreferences {
            block_size: 65536,
            compression_level: 1,
            content_checksum: true,
            ..Default::default()
        };

        let mut ctx = Lz4fCompressionContext::new(prefs.clone());

        // Begin frame.
        let mut header_buf = vec![0u8; LZ4F_HEADER_SIZE_MAX + 4];
        let hlen = ctx.begin(&mut header_buf).unwrap();

        // Compress in chunks.
        let mut compressed = header_buf[..hlen].to_vec();
        let chunk_size = 33333;
        let mut offset = 0;
        while offset < data.len() {
            let end = cmp::min(offset + chunk_size, data.len());
            let chunk = &data[offset..end];
            let mut cbuf = vec![0u8; lz4_compress_bound(chunk.len()) + 256];
            let w = ctx.update(chunk, &mut cbuf).unwrap();
            compressed.extend_from_slice(&cbuf[..w]);
            offset = end;
        }

        // End frame.
        let mut end_buf = vec![0u8; 256];
        let elen = ctx.end(&mut end_buf).unwrap();
        compressed.extend_from_slice(&end_buf[..elen]);

        // Decompress the frame.
        let mut dbuf = vec![0u8; data.len() + 1024];
        let dsize = lz4f_decompress_frame(&compressed, &mut dbuf).unwrap();
        assert_eq!(dsize, data.len());
        assert_eq!(&dbuf[..dsize], &data[..]);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 23: Streaming decompression context
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_streaming_decompress_context() {
        let data = vec![0xDEu8; 50000];
        let prefs = Lz4FramePreferences::default();
        let bound = lz4f_frame_bound(data.len());
        let mut cbuf = vec![0u8; bound];
        let csize = lz4f_compress_frame(&data, &mut cbuf, &prefs).unwrap();
        let compressed = &cbuf[..csize];

        let mut ctx = Lz4fDecompressionContext::new();
        let mut output = Vec::new();
        let mut in_offset = 0;
        let mut out_buf = vec![0u8; 16384];

        while in_offset < compressed.len() {
            let chunk_size = cmp::min(7777, compressed.len() - in_offset);
            let (written, consumed) = ctx
                .decompress(&compressed[in_offset..in_offset + chunk_size], &mut out_buf)
                .unwrap();
            output.extend_from_slice(&out_buf[..written]);
            in_offset += chunk_size; // simplistic; real code uses `consumed`
            if ctx.is_finished() {
                break;
            }
        }

        assert_eq!(output.len(), data.len());
        assert_eq!(&output, &data);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 24: xxHash-32 correctness (known vectors)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_xxh32_known_vectors() {
        assert_eq!(xxh32(b"", 0), 0x02CC_5D05);
        assert_eq!(xxh32(b"a", 0), 0x550D_7456);
        assert_eq!(xxh32(b"abc", 0), 0x32D1_53FF);
        assert_eq!(xxh32(b"message digest", 0), 0x7C94_8382);
        assert_eq!(
            xxh32(b"abcdefghijklmnopqrstuvwxyz", 0),
            0x50A0_7E09
        );
        // With seed.
        assert_eq!(xxh32(b"abc", 12345), 0xBF67_4AC1);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 25: lz4_compress_bound
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_compress_bound() {
        assert!(lz4_compress_bound(0) >= 16);
        assert!(lz4_compress_bound(1024) >= 1024);
        assert!(lz4_compress_bound(LZ4_MAX_INPUT_SIZE) > LZ4_MAX_INPUT_SIZE);
        // Oversized should return 0.
        assert_eq!(lz4_compress_bound(LZ4_MAX_INPUT_SIZE + 1), 0);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 26: Dictionary-assisted compression
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_dictionary_compress() {
        // Dictionary contains "ABCDEFGH".
        let dict = b"ABCDEFGH";
        // Source repeats "ABCD" which exists in dict.
        let src = b"ABCDABCDABCDABCD";
        let max_csize = lz4_compress_bound(src.len());
        let mut cbuf = vec![0u8; max_csize];
        let csize = lz4_compress_fast_using_dict(src, &mut cbuf, dict, 1).unwrap();
        let mut dbuf = vec![0u8; src.len() + 64];
        let dsize = lz4_decompress_safe(&cbuf[..csize], &mut dbuf, src.len() + 64).unwrap();
        assert_eq!(dsize, src.len());
        assert_eq!(&dbuf[..dsize], src);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 27: Dictionary-assisted HC compression
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_dictionary_hc_compress() {
        let dict = b"The quick brown fox";
        let src = b"The quick brown fox jumps over The quick brown fox";
        let max_csize = lz4_compress_bound(src.len());
        let mut cbuf = vec![0u8; max_csize];
        let csize = lz4_compress_hc_using_dict(src, &mut cbuf, dict, 6).unwrap();
        let mut dbuf = vec![0u8; src.len() + 64];
        let dsize = lz4_decompress_safe(&cbuf[..csize], &mut dbuf, src.len() + 64).unwrap();
        assert_eq!(dsize, src.len());
        assert_eq!(&dbuf[..dsize], src);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 28: HcLevel construction
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_hc_level_params() {
        let l1 = HcLevel::new(1);
        assert_eq!(l1.search_depth, 1);
        assert!(!l1.lazy);
        assert!(!l1.optimal_parse);

        let l9 = HcLevel::new(9);
        assert_eq!(l9.search_depth, 1024);
        assert!(l9.lazy);
        assert!(!l9.optimal_parse);

        let l12 = HcLevel::new(12);
        assert_eq!(l12.search_depth, 8192);
        assert!(l12.lazy);
        assert!(l12.optimal_parse);

        // Clamping.
        let l0 = HcLevel::new(0);
        assert_eq!(l0.level, 1);
        let l99 = HcLevel::new(99);
        assert_eq!(l99.level, 12);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 29: Acceleration parameter
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_acceleration() {
        let data = vec![b'Z'; 10000];
        for accel in &[1, 2, 4, 10, 100] {
            let max_csize = lz4_compress_bound(data.len());
            let mut cbuf = vec![0u8; max_csize];
            let csize = lz4_compress_fast(&data, &mut cbuf, *accel).unwrap();
            let mut dbuf = vec![0u8; data.len() + 64];
            let dsize = lz4_decompress_safe(&cbuf[..csize], &mut dbuf, data.len() + 64).unwrap();
            assert_eq!(dsize, data.len(), "accel={} roundtrip failed", accel);
        }
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 30: Large literal length encoding (> 255 literals)
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_long_literal_encoding() {
        // Generate data with no possible matches (ensuring all literals).
        let data: Vec<u8> = (0..1024usize).map(|i| (i.wrapping_mul(127) & 0xFF) as u8).collect();
        roundtrip_fast(&data);
        roundtrip_hc(&data, 1);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 31: Content checksum verification in frame
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_content_checksum_verified() {
        let data = b"checksum test data".repeat(500);
        let prefs = Lz4FramePreferences {
            content_checksum: true,
            ..Default::default()
        };
        let bound = lz4f_frame_bound(data.len());
        let mut cbuf = vec![0u8; bound];
        let csize = lz4f_compress_frame(data.as_ref(), &mut cbuf, &prefs).unwrap();
        let mut dbuf = vec![0u8; data.len() + 64];
        let dsize = lz4f_decompress_frame(&cbuf[..csize], &mut dbuf).unwrap();
        assert_eq!(dsize, data.len());
        assert_eq!(&dbuf[..dsize], data.as_ref());

        // Corrupt content checksum and verify rejection.
        let mut corrupted = cbuf[..csize].to_vec();
        // The content checksum is the last 4 bytes.
        let len = corrupted.len();
        corrupted[len - 1] ^= 0xFF;
        let result = lz4f_decompress_frame(&corrupted, &mut dbuf);
        assert!(result.is_err(), "should detect corrupted content checksum");
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 32: Block checksum in frame format
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_block_checksum() {
        let data = vec![0x42u8; 131072]; // 2 blocks of 64K
        let prefs = Lz4FramePreferences {
            block_size: 65536,
            block_checksum: true,
            content_checksum: true,
            ..Default::default()
        };
        roundtrip_frame(&data, &prefs);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 33: Frame with linked blocks
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_linked_blocks() {
        let data = vec![b'Q'; 100000];
        let prefs = Lz4FramePreferences {
            block_size: 32768,
            block_mode: Lz4BlockMode::BlockLinked,
            compression_level: 1,
            content_checksum: true,
            ..Default::default()
        };
        roundtrip_frame(&data, &prefs);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 34: Round-trip with various block sizes
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_frame_various_block_sizes() {
        let data = vec![0xA5u8; 500000];
        for &bs in &[65536, 262144, 1048576, 4194304] {
            let prefs = Lz4FramePreferences {
                block_size: bs,
                compression_level: 1,
                content_checksum: true,
                ..Default::default()
            };
            roundtrip_frame(&data, &prefs);
        }
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 35: Error Display implementation
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_error_display() {
        let e = Lz4Error::CorruptedBlock;
        let s = format!("{}", e);
        assert!(s.contains("corrupted"));

        let e = Lz4Error::HeaderChecksumError;
        let s = format!("{}", e);
        assert!(s.contains("checksum"));

        let e = Lz4Error::InvalidParameter("test param");
        let s = format!("{}", e);
        assert!(s.contains("test param"));
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 36: format_size utility
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_format_size() {
        assert_eq!(format_size(0), "0 B");
        assert_eq!(format_size(512), "512 B");
        assert!(format_size(2048).contains("KiB"));
        assert!(format_size(5_000_000).contains("MiB"));
        assert!(format_size(2_000_000_000).contains("GiB"));
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 37: bd_from_block_maxsize mapping
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_bd_mapping() {
        assert_eq!(bd_from_block_maxsize(16384), LZ4F_BLOCK_MAXSIZE_64KB);
        assert_eq!(bd_from_block_maxsize(65535), LZ4F_BLOCK_MAXSIZE_64KB);
        assert_eq!(bd_from_block_maxsize(65536), LZ4F_BLOCK_MAXSIZE_64KB);
        assert_eq!(bd_from_block_maxsize(65537), LZ4F_BLOCK_MAXSIZE_256KB);
        assert_eq!(bd_from_block_maxsize(262144), LZ4F_BLOCK_MAXSIZE_256KB);
        assert_eq!(bd_from_block_maxsize(1048576), LZ4F_BLOCK_MAXSIZE_1MB);
        assert_eq!(bd_from_block_maxsize(4194304), LZ4F_BLOCK_MAXSIZE_4MB);
        assert_eq!(bd_from_block_maxsize(9999999), LZ4F_BLOCK_MAXSIZE_4MB);
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 38: block_maxsize_from_bd reverse mapping
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_block_maxsize_from_bd() {
        assert_eq!(block_maxsize_from_bd(4), 65536);
        assert_eq!(block_maxsize_from_bd(5), 262144);
        assert_eq!(block_maxsize_from_bd(6), 1048576);
        assert_eq!(block_maxsize_from_bd(7), 4194304);
        assert_eq!(block_maxsize_from_bd(0), 65536); // invalid -> 64K default
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 39: HC compression is at least as good as fast
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_hc_better_than_fast() {
        let data = b"The quick brown fox jumps over the lazy dog. ".repeat(500);
        let max_csize = lz4_compress_bound(data.len());

        let mut cbuf_fast = vec![0u8; max_csize];
        let csize_fast = lz4_compress_fast(data.as_ref(), &mut cbuf_fast, 1).unwrap();

        let mut cbuf_hc = vec![0u8; max_csize];
        let csize_hc = lz4_compress_hc(data.as_ref(), &mut cbuf_hc, 12).unwrap();

        // HC should produce smaller or equal output.
        assert!(
            csize_hc <= csize_fast + 32,
            "HC level 12 should not be significantly worse than fast; fast={}, hc={}",
            csize_fast,
            csize_hc,
        );
    }

    // ═════════════════════════════════════════════════════════════════════
    // Test 40: Decompression produces correct output for known-good blocks
    // ═════════════════════════════════════════════════════════════════════
    #[test]
    fn test_known_compressed_block() {
        // Manually construct a valid LZ4 block:
        // Token: 0x10 = literal len 1 (high nibble 1), match len 0 (low nibble 0).
        // Followed by 1 literal byte.
        let block = [0x10, b'H'];
        let mut dbuf = [0u8; 64];
        let dsize = lz4_decompress_safe(&block, &mut dbuf, 64).unwrap();
        assert_eq!(dsize, 1);
        assert_eq!(dbuf[0], b'H');
    }
} // end tests module