zstd-safe 7.4.1

Safe low-level bindings for the zstd compression library.
Documentation
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
#![no_std]
//! Minimal safe wrapper around zstd-sys.
//!
//! This crates provides a minimal translation of the [zstd-sys] methods.
//! For a more comfortable high-level library, see the [zstd] crate.
//!
//! [zstd-sys]: https://crates.io/crates/zstd-sys
//! [zstd]: https://crates.io/crates/zstd
//!
//! Most of the functions here map 1-for-1 to a function from
//! [the C zstd library][zstd-c] mentioned in their descriptions.
//! Check the [source documentation][doc] for more information on their
//! behaviour.
//!
//! [doc]: https://facebook.github.io/zstd/zstd_manual.html
//! [zstd-c]: https://facebook.github.io/zstd/
//!
//! Features denoted as experimental in the C library are hidden behind an
//! `experimental` feature.
#![cfg_attr(feature = "doc-cfg", feature(doc_cfg))]

// TODO: Use alloc feature instead to implement stuff for Vec
// TODO: What about Cursor?
#[cfg(feature = "std")]
extern crate std;

#[cfg(test)]
mod tests;

#[cfg(feature = "seekable")]
pub mod seekable;

// Re-export zstd-sys
pub use zstd_sys;

/// How to compress data.
pub use zstd_sys::ZSTD_strategy as Strategy;

/// Frame progression state.
#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
pub use zstd_sys::ZSTD_frameProgression as FrameProgression;

/// Reset directive.
// pub use zstd_sys::ZSTD_ResetDirective as ResetDirective;
use core::ffi::{c_char, c_int, c_ulonglong, c_void};

use core::marker::PhantomData;
use core::num::{NonZeroU32, NonZeroU64};
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
use core::str;

include!("constants.rs");

#[cfg(feature = "experimental")]
include!("constants_experimental.rs");

#[cfg(feature = "seekable")]
include!("constants_seekable.rs");

/// Represents the compression level used by zstd.
pub type CompressionLevel = i32;

/// Represents a possible error from the zstd library.
pub type ErrorCode = usize;

/// Wrapper result around most zstd functions.
///
/// Either a success code (usually number of bytes written), or an error code.
pub type SafeResult = Result<usize, ErrorCode>;

/// The error code zstd itself returns when an allocation fails
/// (`-ZSTD_error_memory_allocation` as a `size_t`), for the places where a
/// context is allocated on the Rust side of a call.
#[cfg(feature = "experimental")]
const MEMORY_ALLOCATION_ERROR: ErrorCode =
    (zstd_sys::ZSTD_ErrorCode::ZSTD_error_memory_allocation as usize)
        .wrapping_neg();

/// The error code zstd itself returns for a parameter it does not support
/// (`-ZSTD_error_parameter_unsupported` as a `size_t`), for the parameters
/// that are refused on the Rust side of a call.
#[cfg(feature = "experimental")]
const PARAMETER_UNSUPPORTED_ERROR: ErrorCode =
    (zstd_sys::ZSTD_ErrorCode::ZSTD_error_parameter_unsupported as usize)
        .wrapping_neg();

/// The error code zstd itself returns for an operation the context is in no
/// state to perform (`-ZSTD_error_stage_wrong` as a `size_t`), for the places
/// where that has to be decided on the Rust side of a call.
const STAGE_WRONG_ERROR: ErrorCode =
    (zstd_sys::ZSTD_ErrorCode::ZSTD_error_stage_wrong as usize).wrapping_neg();

/// The error code `ZSTD_compress2()` returns when the destination is too
/// small, leaving its frame in progress (`-ZSTD_error_dstSize_tooSmall`).
const DST_SIZE_TOO_SMALL_ERROR: ErrorCode =
    (zstd_sys::ZSTD_ErrorCode::ZSTD_error_dstSize_tooSmall as usize)
        .wrapping_neg();

/// The largest frame zstd compresses on the calling thread when its size is
/// known (`ZSTDMT_JOBSIZE_MIN` in zstd 1.5; older versions used larger
/// values, which only makes this cautious).
const ZSTDMT_JOBSIZE_MIN: usize = 512 << 10;

/// Indicates an error happened when parsing the frame content size.
///
/// The stream may be corrupted, or the given frame prefix was too small.
#[derive(Debug)]
pub struct ContentSizeError;

impl core::fmt::Display for ContentSizeError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("Could not get content size")
    }
}

/// Returns true if code represents error.
fn is_error(code: usize) -> bool {
    // Safety: Just FFI
    unsafe { zstd_sys::ZSTD_isError(code) != 0 }
}

/// Parse the result code
///
/// Returns the number of bytes written if the code represents success,
/// or the error message code otherwise.
fn parse_code(code: usize) -> SafeResult {
    if !is_error(code) {
        Ok(code)
    } else {
        Err(code)
    }
}

/// Parse a content size value.
///
/// zstd uses 2 special content size values to indicate either unknown size or parsing error.
fn parse_content_size(
    content_size: u64,
) -> Result<Option<u64>, ContentSizeError> {
    match content_size {
        CONTENTSIZE_ERROR => Err(ContentSizeError),
        CONTENTSIZE_UNKNOWN => Ok(None),
        other => Ok(Some(other)),
    }
}

fn ptr_void(src: &[u8]) -> *const c_void {
    src.as_ptr() as *const c_void
}

fn ptr_mut_void(dst: &mut (impl WriteBuf + ?Sized)) -> *mut c_void {
    dst.as_mut_ptr() as *mut c_void
}

/// Returns the ZSTD version.
///
/// Returns `major * 10_000 + minor * 100 + patch`.
/// So 1.5.3 would be returned as `10_503`.
pub fn version_number() -> u32 {
    // Safety: Just FFI
    unsafe { zstd_sys::ZSTD_versionNumber() as u32 }
}

/// Returns a string representation of the ZSTD version.
///
/// For example "1.5.3".
pub fn version_string() -> &'static str {
    // Safety: Assumes `ZSTD_versionString` returns a valid utf8 string.
    unsafe { c_char_to_str(zstd_sys::ZSTD_versionString()) }
}

/// Returns the minimum (fastest) compression level supported.
///
/// This is likely going to be a _very_ large negative number.
pub fn min_c_level() -> CompressionLevel {
    // Safety: Just FFI
    unsafe { zstd_sys::ZSTD_minCLevel() as CompressionLevel }
}

/// Returns the maximum (slowest) compression level supported.
pub fn max_c_level() -> CompressionLevel {
    // Safety: Just FFI
    unsafe { zstd_sys::ZSTD_maxCLevel() as CompressionLevel }
}

/// Wraps the `ZSTD_compress` function.
///
/// This will try to compress `src` entirely and write the result to `dst`, returning the number of
/// bytes written. If `dst` is too small to hold the compressed content, an error will be returned.
///
/// For streaming operations that don't require to store the entire input/output in memory, see
/// `compress_stream`.
pub fn compress<C: WriteBuf + ?Sized>(
    dst: &mut C,
    src: &[u8],
    compression_level: CompressionLevel,
) -> SafeResult {
    // Safety: ZSTD_compress indeed returns how many bytes have been written.
    unsafe {
        dst.write_from(|buffer, capacity| {
            parse_code(zstd_sys::ZSTD_compress(
                buffer,
                capacity,
                ptr_void(src),
                src.len(),
                compression_level,
            ))
        })
    }
}

/// Wraps the `ZSTD_decompress` function.
///
/// This is a one-step decompression (not streaming).
///
/// You will need to make sure `dst` is large enough to store all the decompressed content, or an
/// error will be returned.
///
/// If decompression was a success, the number of bytes written will be returned.
pub fn decompress<C: WriteBuf + ?Sized>(
    dst: &mut C,
    src: &[u8],
) -> SafeResult {
    // Safety: ZSTD_decompress indeed returns how many bytes have been written.
    unsafe {
        dst.write_from(|buffer, capacity| {
            parse_code(zstd_sys::ZSTD_decompress(
                buffer,
                capacity,
                ptr_void(src),
                src.len(),
            ))
        })
    }
}

/// Wraps the `ZSTD_getDecompressedSize` function.
///
/// Returns `None` if the size could not be found, or if the content is actually empty.
#[deprecated(note = "Use ZSTD_getFrameContentSize instead")]
pub fn get_decompressed_size(src: &[u8]) -> Option<NonZeroU64> {
    // Safety: Just FFI
    NonZeroU64::new(unsafe {
        zstd_sys::ZSTD_getDecompressedSize(ptr_void(src), src.len()) as u64
    })
}

/// Maximum compressed size in worst case single-pass scenario
pub fn compress_bound(src_size: usize) -> usize {
    // Safety: Just FFI
    unsafe { zstd_sys::ZSTD_compressBound(src_size) }
}

/// Remembers an error that may have left a context in a state zstd considers
/// undefined.
///
/// zstd.h says of both `ZSTD_compressStream2()` and `ZSTD_decompressStream()`
/// that "if an operation ends with an error, it may leave [the context] in an
/// undefined state", and that calling them again on such a state is undefined
/// behaviour - the context has to be reset first. So once a streaming
/// operation fails, refuse to run another one until something resets the
/// context, and hand the original error back instead.
#[derive(Clone, Debug, Default)]
struct Poison(Option<ErrorCode>);

impl Poison {
    /// Fails with the error that poisoned the context, if there was one.
    fn guard(&self) -> Result<(), ErrorCode> {
        match self.0 {
            Some(code) => Err(code),
            None => Ok(()),
        }
    }

    /// Remembers `res` if it failed, and passes it through.
    fn record(&mut self, res: SafeResult) -> SafeResult {
        if let Err(code) = res {
            self.0 = Some(code);
        }
        res
    }

    /// The context was reset, so it is usable again.
    fn clear(&mut self) {
        self.0 = None;
    }
}

/// What a `CCtx` knows about its worker threads (`NbWorkers`).
#[derive(Clone, Copy, Debug, Default)]
struct Workers {
    /// `NbWorkers` was ever set to a non-zero value, see `guard_own_tables`.
    ever: bool,
    /// `NbWorkers` is currently set to a non-zero value.
    requested: bool,
    /// A frame was started while `NbWorkers` was set, and is not finished.
    ///
    /// Its jobs may be running on the worker threads, reading the context's
    /// dictionary and job table. zstd up to 1.5.7 does not wait for them when
    /// the frame is abandoned: not on a session reset, not before a short
    /// frame (which it compresses on the calling thread), and not before
    /// freeing the dictionary when the context is freed. So the frame is
    /// drained first, see `CCtx::drain()`.
    pending: bool,
    /// `ZSTD_e_end` was given for the `pending` frame. zstd then answers
    /// `ZSTD_e_continue` by resetting the session without waiting for the
    /// jobs, so that is refused here first.
    ended: bool,
    /// A `ZSTD_e_end` call took all of its input: zstd made the frame's last
    /// job. It would hand any new input to a worker as a job after the last
    /// one, which waits forever, so that is refused here first.
    last: bool,
    /// The size pledged for the next frame is short (see `short`).
    pledged_short: bool,
    /// The `pending` frame is known to be compressed on the calling thread:
    /// its size is known (pledged, or given whole with `ZSTD_e_end`), and is
    /// short (zstd hands out no jobs for a frame of at most
    /// `ZSTDMT_JOBSIZE_MIN`).
    short: bool,
    /// The `pending` frame may have handed jobs to the worker threads: it
    /// took some input, and is not `short`.
    jobs: bool,
    /// A job failed. zstd up to 1.5.7 can then keep a job it had prepared but
    /// not handed to a worker yet (`jobReady`), after clearing it: the next
    /// multithreaded frame hands that empty job to a worker, which crashes.
    /// That stays in the context's multithreading state, which only freeing
    /// the context frees, so no other multithreaded frame is started on it.
    broken: bool,
    /// `StableOutBuffer` is enabled. Draining needs an output buffer of its
    /// own, which zstd then refuses, so it is not allowed with workers.
    stable_out: bool,
}

/// Compression context
///
/// It is recommended to allocate a single context per thread and re-use it
/// for many compression operations.
///
/// # Worker threads and leaks
///
/// With `CParameter::NbWorkers`, the jobs of a frame run on worker threads,
/// which read the dictionary. Anything that abandons a frame in progress
/// waits for them, dropping the context included (see
/// [`CCtx::wait_for_workers()`]). A context that is leaked instead
/// (`mem::forget`, `Box::leak`, a reference cycle) never waits: with a
/// dictionary or prefix it only *borrows* ([`CCtx::ref_cdict()`],
/// [`CCtx::ref_prefix()`]), the workers may then read it after it is
/// dropped. Do not leak a context that is compressing with worker threads
/// from borrowed data. A dictionary loaded with
/// [`CCtx::load_dictionary()`] is copied, and leaks with the context.
pub struct CCtx<'a>(
    NonNull<zstd_sys::ZSTD_CCtx>,
    PhantomData<&'a ()>,
    Poison,
    Workers,
);

impl Default for CCtx<'_> {
    fn default() -> Self {
        CCtx::create()
    }
}

impl<'a> CCtx<'a> {
    /// Tries to create a new context.
    ///
    /// Returns `None` if zstd returns a NULL pointer - may happen if allocation fails.
    pub fn try_create() -> Option<Self> {
        // Safety: Just FFI
        Some(CCtx(
            NonNull::new(unsafe { zstd_sys::ZSTD_createCCtx() })?,
            PhantomData,
            Poison::default(),
            Workers::default(),
        ))
    }

    /// Wrap `ZSTD_createCCtx`
    ///
    /// # Panics
    ///
    /// If zstd returns a NULL pointer.
    pub fn create() -> Self {
        Self::try_create()
            .expect("zstd returned null pointer when creating new context")
    }

    /// Ends the session of a one-shot compression, and passes `res` through.
    ///
    /// zstd.h has these calls imply a reset, but most of them leave the stream
    /// stage alone (and their source size behind as the pledged one), while
    /// `ZSTD_compress2()` stays mid-frame when it fails. A later stream call
    /// would then carry on from a state that still points at buffers and
    /// dictionaries borrowed for that one call. So reset explicitly: the next
    /// stream starts from scratch.
    fn finish_one_shot(&mut self, res: SafeResult) -> SafeResult {
        self.reset(ResetDirective::SessionOnly).and(res)
    }

    /// Wraps the `ZSTD_compressCCtx()` function
    pub fn compress<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
        compression_level: CompressionLevel,
    ) -> SafeResult {
        // The one-shot calls run on the calling thread, but end by resetting
        // the session (see `finish_one_shot`), and overwrite the parameters a
        // multithreaded frame left open was started with.
        self.drain();
        // Safety: ZSTD_compressCCtx returns how many bytes were written.
        let res = unsafe {
            dst.write_from(|buffer, capacity| {
                parse_code(zstd_sys::ZSTD_compressCCtx(
                    self.0.as_ptr(),
                    buffer,
                    capacity,
                    ptr_void(src),
                    src.len(),
                    compression_level,
                ))
            })
        };
        self.finish_one_shot(res)
    }

    /// Wraps the `ZSTD_compress2()` function.
    pub fn compress2<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
    ) -> SafeResult {
        if !self.3.requested {
            // Safety: ZSTD_compress2 returns how many bytes were written.
            let res = unsafe {
                dst.write_from(|buffer, capacity| {
                    parse_code(zstd_sys::ZSTD_compress2(
                        self.0.as_ptr(),
                        buffer,
                        capacity,
                        ptr_void(src),
                        src.len(),
                    ))
                })
            };
            // Unlike the others, this starts by resetting the session, and
            // ends it when it succeeds. When it fails, it stays mid-frame:
            // make streaming calls wait for an explicit reset.
            self.2.clear();
            return self.2.record(res);
        }

        // With worker threads, a frame this leaves in progress must be one
        // that can be waited for later, see `drain()`. `ZSTD_compress2()`
        // runs its frame with stable buffers, and zstd would then refuse any
        // other buffer: do what it does, but with ordinary ones. That costs
        // nothing to a multithreaded frame, which copies its input and output
        // anyway. A short frame, compressed on the calling thread, goes
        // straight to the destination when it can hold the whole frame, and
        // through a copy otherwise.
        // Waiting for the frame in progress may find that one of its jobs
        // failed: check `broken` after.
        self.reset(ResetDirective::SessionOnly)?;
        self.guard_broken()?;
        let mut raw = Ok(0);
        let mut taken = 0;
        // Safety: ZSTD_compressStream2 reports how many bytes it wrote in
        // `output.pos`.
        let res = unsafe {
            dst.write_from(|buffer, capacity| {
                let mut output = zstd_sys::ZSTD_outBuffer {
                    dst: buffer,
                    size: capacity,
                    pos: 0,
                };
                let mut input = zstd_sys::ZSTD_inBuffer {
                    src: ptr_void(src),
                    size: src.len(),
                    pos: 0,
                };
                raw = parse_code(zstd_sys::ZSTD_compressStream2(
                    self.0.as_ptr(),
                    &mut output,
                    &mut input,
                    zstd_sys::ZSTD_EndDirective::ZSTD_e_end,
                ));
                taken = input.pos;
                match raw {
                    Ok(0) => Ok(output.pos),
                    // As `ZSTD_compress2()`: the frame stays in progress.
                    Ok(_) => Err(DST_SIZE_TOO_SMALL_ERROR),
                    Err(code) => Err(code),
                }
            })
        };
        self.track_frame(
            raw,
            zstd_sys::ZSTD_EndDirective::ZSTD_e_end,
            src.len(),
            taken,
        );
        self.2.record(res)
    }

    /// Wraps the `ZSTD_compress_usingDict()` function.
    pub fn compress_using_dict<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
        dict: &[u8],
        compression_level: CompressionLevel,
    ) -> SafeResult {
        // See `compress()`.
        self.drain();
        // Safety: ZSTD_compress_usingDict returns how many bytes were written.
        let res = unsafe {
            dst.write_from(|buffer, capacity| {
                parse_code(zstd_sys::ZSTD_compress_usingDict(
                    self.0.as_ptr(),
                    buffer,
                    capacity,
                    ptr_void(src),
                    src.len(),
                    ptr_void(dict),
                    dict.len(),
                    compression_level,
                ))
            })
        };
        self.finish_one_shot(res)
    }

    /// Wraps the `ZSTD_compress_usingCDict()` function.
    pub fn compress_using_cdict<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
        cdict: &CDict<'_>,
    ) -> SafeResult {
        // See `compress()`.
        self.drain();
        // Safety: ZSTD_compress_usingCDict returns how many bytes were written.
        let res = unsafe {
            dst.write_from(|buffer, capacity| {
                parse_code(zstd_sys::ZSTD_compress_usingCDict(
                    self.0.as_ptr(),
                    buffer,
                    capacity,
                    ptr_void(src),
                    src.len(),
                    cdict.0.as_ptr(),
                ))
            })
        };
        self.finish_one_shot(res)
    }

    /// Initializes the context with the given compression level.
    ///
    /// This is equivalent to running:
    /// * `reset()`
    /// * `set_parameter(CompressionLevel, compression_level)`
    ///
    /// Abandons the current frame, if any. With worker threads, this first
    /// waits for its jobs, see [`CCtx::wait_for_workers()`].
    pub fn init(&mut self, compression_level: CompressionLevel) -> SafeResult {
        // This also resets the session and unloads the dictionary.
        self.drain();
        self.2.clear();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_initCStream(self.0.as_ptr(), compression_level)
        };
        self.2.record(parse_code(code))
    }

    /// Wraps the `ZSTD_initCStream_srcSize()` function.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    #[deprecated]
    pub fn init_src_size(
        &mut self,
        compression_level: CompressionLevel,
        pledged_src_size: u64,
    ) -> SafeResult {
        self.drain();
        self.2.clear();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_initCStream_srcSize(
                self.0.as_ptr(),
                compression_level as c_int,
                pledged_src_size as c_ulonglong,
            )
        };
        parse_code(code)
    }

    /// Wraps the `ZSTD_initCStream_usingDict()` function.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    #[deprecated]
    pub fn init_using_dict(
        &mut self,
        dict: &[u8],
        compression_level: CompressionLevel,
    ) -> SafeResult {
        self.drain();
        self.2.clear();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_initCStream_usingDict(
                self.0.as_ptr(),
                ptr_void(dict),
                dict.len(),
                compression_level,
            )
        };
        parse_code(code)
    }

    /// Wraps the `ZSTD_initCStream_usingCDict()` function.
    ///
    /// Dictionary must outlive the context.
    ///
    /// ```compile_fail,E0515
    /// # #![allow(deprecated)]
    /// fn compressor() -> zstd_safe::CCtx<'static> {
    ///     let cdict = zstd_safe::CDict::create(b"dictionary", 3);
    ///     let mut cctx = zstd_safe::CCtx::create();
    ///     cctx.init_using_cdict(&cdict).unwrap();
    ///     cctx // `cdict` would be dropped while `cctx` still points at it.
    /// }
    /// ```
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    #[deprecated]
    pub fn init_using_cdict<'b>(&mut self, cdict: &'a CDict<'b>) -> SafeResult
    where
        'b: 'a, // Dictionary outlives the stream.
    {
        self.drain();
        self.2.clear();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_initCStream_usingCDict(
                self.0.as_ptr(),
                cdict.0.as_ptr(),
            )
        };
        self.2.record(parse_code(code))
    }

    /// Tries to load a dictionary.
    ///
    /// The dictionary content will be copied internally and does not need to be kept alive after
    /// calling this function.
    ///
    /// If you need to use the same dictionary for multiple contexts, it may be more efficient to
    /// create a `CDict` first, then loads that.
    ///
    /// The dictionary will apply to all compressed frames, until a new dictionary is set.
    ///
    /// Like zstd, this fails with a `stage_wrong` error while a frame is in
    /// progress: reset the session first (see [`CCtx::wait_for_workers()`]).
    pub fn load_dictionary(&mut self, dict: &[u8]) -> SafeResult {
        self.guard_workers()?;
        // Safety: Just FFI
        parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_loadDictionary(
                self.0.as_ptr(),
                ptr_void(dict),
                dict.len(),
            )
        })
    }

    /// Wraps the `ZSTD_CCtx_refCDict()` function.
    ///
    /// Dictionary must outlive the context. With worker threads, do not leak
    /// the context, see [its documentation](CCtx#worker-threads-and-leaks).
    ///
    /// Like zstd, this fails with a `stage_wrong` error while a frame is in
    /// progress: reset the session first (see [`CCtx::wait_for_workers()`]).
    pub fn ref_cdict<'b>(&mut self, cdict: &CDict<'b>) -> SafeResult
    where
        'b: 'a,
    {
        self.guard_workers()?;
        // Safety: Just FFI
        parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_refCDict(self.0.as_ptr(), cdict.0.as_ptr())
        })
    }

    /// Return to "no-dictionary" mode.
    ///
    /// This will disable any dictionary/prefix previously registered for future frames.
    ///
    /// Like zstd, this fails with a `stage_wrong` error while a frame is in
    /// progress: reset the session first (see [`CCtx::wait_for_workers()`]).
    pub fn disable_dictionary(&mut self) -> SafeResult {
        self.guard_workers()?;
        // Safety: Just FFI
        parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_loadDictionary(
                self.0.as_ptr(),
                core::ptr::null(),
                0,
            )
        })
    }

    /// Use some prefix as single-use dictionary for the next compressed frame.
    ///
    /// Just like a dictionary, decompression will need to be given the same prefix.
    ///
    /// This is best used if the "prefix" looks like the data to be compressed.
    /// With worker threads, do not leak the context, see
    /// [its documentation](CCtx#worker-threads-and-leaks).
    ///
    /// Like zstd, this fails with a `stage_wrong` error while a frame is in
    /// progress: reset the session first (see [`CCtx::wait_for_workers()`]).
    pub fn ref_prefix<'b>(&mut self, prefix: &'b [u8]) -> SafeResult
    where
        'b: 'a,
    {
        self.guard_workers()?;
        // Safety: Just FFI
        parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_refPrefix(
                self.0.as_ptr(),
                ptr_void(prefix),
                prefix.len(),
            )
        })
    }

    /// Performs a step of a streaming compression operation.
    ///
    /// This will read some data from `input` and/or write some data to `output`.
    ///
    /// # Returns
    ///
    /// A hint for the "ideal" amount of input data to provide in the next call.
    ///
    /// This hint is only for performance purposes.
    ///
    /// Wraps the `ZSTD_compressStream()` function.
    pub fn compress_stream<C: WriteBuf + ?Sized>(
        &mut self,
        output: &mut OutBuffer<'_, C>,
        input: &mut InBuffer<'_>,
    ) -> SafeResult {
        use zstd_sys::ZSTD_EndDirective::ZSTD_e_continue;
        self.2.guard()?;
        if let Some(res) =
            self.before_stream(input.pos == input.src.len(), ZSTD_e_continue)
        {
            // Any input is welcome.
            return res.map(|_| Self::in_size());
        }
        let remaining = input.src.len().saturating_sub(input.pos);
        let mut output = output.wrap();
        let mut input = input.wrap();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_compressStream(
                self.0.as_ptr(),
                ptr_mut(&mut output),
                ptr_mut(&mut input),
            )
        };
        let taken = input.buf.pos.saturating_sub(input.parent.pos);
        let res = output.check(parse_code(code));
        let res = self.2.record(input.check(res));
        self.track_frame(res, ZSTD_e_continue, remaining, taken);
        res
    }

    /// Performs a step of a streaming compression operation.
    ///
    /// This will read some data from `input` and/or write some data to `output`.
    ///
    /// The `end_op` directive can be used to specify what to do after: nothing special, flush
    /// internal buffers, or end the frame.
    ///
    /// # Returns
    ///
    /// An lower bound for the amount of data that still needs to be flushed out.
    ///
    /// This is useful when flushing or ending the frame: you need to keep calling this function
    /// until it returns 0.
    ///
    /// Wraps the `ZSTD_compressStream2()` function.
    pub fn compress_stream2<C: WriteBuf + ?Sized>(
        &mut self,
        output: &mut OutBuffer<'_, C>,
        input: &mut InBuffer<'_>,
        end_op: zstd_sys::ZSTD_EndDirective,
    ) -> SafeResult {
        self.2.guard()?;
        if let Some(res) =
            self.before_stream(input.pos == input.src.len(), end_op)
        {
            return res;
        }
        let remaining = input.src.len().saturating_sub(input.pos);
        let mut output = output.wrap();
        let mut input = input.wrap();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_compressStream2(
                self.0.as_ptr(),
                ptr_mut(&mut output),
                ptr_mut(&mut input),
                end_op,
            )
        };
        let taken = input.buf.pos.saturating_sub(input.parent.pos);
        let res = output.check(parse_code(code));
        let res = self.2.record(input.check(res));
        self.track_frame(res, end_op, remaining, taken);
        res
    }

    /// Flush any intermediate buffer.
    ///
    /// To fully flush, you should keep calling this function until it returns `Ok(0)`.
    ///
    /// Wraps the `ZSTD_flushStream()` function.
    pub fn flush_stream<C: WriteBuf + ?Sized>(
        &mut self,
        output: &mut OutBuffer<'_, C>,
    ) -> SafeResult {
        use zstd_sys::ZSTD_EndDirective::ZSTD_e_flush;
        self.2.guard()?;
        if let Some(res) = self.before_stream(true, ZSTD_e_flush) {
            return res;
        }
        let mut output = output.wrap();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_flushStream(self.0.as_ptr(), ptr_mut(&mut output))
        };
        let res = self.2.record(output.check(parse_code(code)));
        self.track_frame(res, ZSTD_e_flush, 0, 0);
        res
    }

    /// Ends the stream.
    ///
    /// You should keep calling this function until it returns `Ok(0)`.
    ///
    /// Wraps the `ZSTD_endStream()` function.
    pub fn end_stream<C: WriteBuf + ?Sized>(
        &mut self,
        output: &mut OutBuffer<'_, C>,
    ) -> SafeResult {
        use zstd_sys::ZSTD_EndDirective::ZSTD_e_end;
        self.2.guard()?;
        if let Some(res) = self.before_stream(true, ZSTD_e_end) {
            return res;
        }
        let mut output = output.wrap();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_endStream(self.0.as_ptr(), ptr_mut(&mut output))
        };
        let res = self.2.record(output.check(parse_code(code)));
        self.track_frame(res, ZSTD_e_end, 0, 0);
        res
    }

    /// Decides whether a streaming call may reach zstd, see `Workers`.
    ///
    /// Returns what the call should return instead when it may not.
    fn before_stream(
        &self,
        no_input: bool,
        end_op: zstd_sys::ZSTD_EndDirective,
    ) -> Option<SafeResult> {
        use zstd_sys::ZSTD_EndDirective::{ZSTD_e_continue, ZSTD_e_end};
        if !self.3.requested {
            return None;
        }
        if self.3.broken {
            return Some(Err(STAGE_WRONG_ERROR));
        }
        if self.3.pending {
            if (self.3.ended && end_op == ZSTD_e_continue)
                || (self.3.last && !no_input)
            {
                return Some(Err(STAGE_WRONG_ERROR));
            }
        } else if no_input && end_op != ZSTD_e_end {
            // Starting a frame with nothing in it. If it was then ended,
            // zstd up to 1.5.7 would hand an empty job to a worker, which its
            // own wait on errors (`ZSTDMT_waitForAllJobsCompleted`) takes for
            // done before the worker even starts. `drain()` does wait for it,
            // so this is only a safeguard: wait for some input, nothing would
            // have been written yet anyway. (A frame that starts with
            // `ZSTD_e_end` takes the size of its input, so an empty one is
            // compressed on the calling thread.)
            return Some(Ok(0));
        }
        None
    }

    /// Records what a streaming call left open, see `Workers`.
    ///
    /// `given` is how much input the call was given, and `taken` how much of
    /// it zstd took.
    fn track_frame(
        &mut self,
        res: SafeResult,
        end_op: zstd_sys::ZSTD_EndDirective,
        given: usize,
        taken: usize,
    ) {
        use zstd_sys::ZSTD_EndDirective::ZSTD_e_end;
        // A pledged size is for the frame this call started or continued.
        let pledged_short = core::mem::take(&mut self.3.pledged_short);
        if !self.3.requested {
            return;
        }
        if !self.3.pending {
            // This call started the frame. Given whole, its size is the
            // input's, whatever was pledged.
            self.3.short = if end_op == ZSTD_e_end {
                given <= ZSTDMT_JOBSIZE_MIN
            } else {
                pledged_short
            };
        }
        self.3.jobs |= taken > 0 && !self.3.short;
        match res {
            Ok(0) if end_op == ZSTD_e_end => self.end_frame(),
            Ok(_) => {
                self.3.pending = true;
                if end_op == ZSTD_e_end {
                    self.3.ended = true;
                    self.3.last |= taken == given;
                }
            }
            // When a job failed, zstd waited for the others and reset the
            // session (refused directives and buffers never get that far).
            // Otherwise, there are no jobs. Either way, nothing is left to
            // wait for.
            Err(_) => {
                self.3.broken |= self.3.jobs;
                self.end_frame();
            }
        }
    }

    /// Records that no frame is in progress anymore.
    fn end_frame(&mut self) {
        self.3.pending = false;
        self.3.ended = false;
        self.3.last = false;
        self.3.short = false;
        self.3.jobs = false;
    }

    /// Waits for the jobs of the frame in progress, if any, so that it can
    /// be abandoned.
    ///
    /// Asking zstd to flush everything waits for every job to be done and
    /// its output flushed (here to a scratch buffer, and thrown away). This
    /// also hands a worker the job zstd prepared but could not hand out yet.
    /// That compresses what was left in zstd's input buffer as one more
    /// job: wasted work, but bounded by one job. Starting a new frame would
    /// avoid it, but zstd up to 1.5.7 can then hand that prepared job out
    /// after clearing it, and a worker crashes.
    fn drain(&mut self) {
        if !self.3.pending {
            return;
        }
        let mut scratch = [0u8; 4096];
        loop {
            let mut output = zstd_sys::ZSTD_outBuffer {
                dst: ptr_mut_void(&mut scratch[..]),
                size: scratch.len(),
                pos: 0,
            };
            let mut input = zstd_sys::ZSTD_inBuffer {
                src: ptr_void(&[]),
                size: 0,
                pos: 0,
            };
            // Safety: Just FFI, with buffers that live through the call.
            let code = unsafe {
                zstd_sys::ZSTD_compressStream2(
                    self.0.as_ptr(),
                    &mut output,
                    &mut input,
                    zstd_sys::ZSTD_EndDirective::ZSTD_e_flush,
                )
            };
            match parse_code(code) {
                Ok(0) => break,
                Ok(_) => continue,
                // A job failed: see `track_frame`.
                Err(_) => {
                    self.3.broken |= self.3.jobs;
                    break;
                }
            }
        }
        self.end_frame();
    }

    /// Fails while a frame is in progress (zstd refuses these calls then).
    ///
    /// zstd up to 1.5.7 would also take them after the session was reset
    /// while worker threads were still running jobs, freeing what they read:
    /// every reset here waits for them first, and this keeps the error the
    /// same whatever the library does.
    fn guard_workers(&self) -> Result<(), ErrorCode> {
        if self.3.pending {
            return Err(STAGE_WRONG_ERROR);
        }
        Ok(())
    }

    /// Fails if a multithreaded call failed on this context, see
    /// `Workers::broken`.
    fn guard_broken(&self) -> Result<(), ErrorCode> {
        if self.3.requested && self.3.broken {
            return Err(STAGE_WRONG_ERROR);
        }
        Ok(())
    }

    /// Abandons the current frame, if any, and waits for the worker threads
    /// to be done with it.
    ///
    /// With `CParameter::NbWorkers`, the jobs of a frame run on worker
    /// threads, reading the dictionary. Anything that abandons a frame in
    /// progress waits for them first: a session reset (and `init()`, which
    /// implies one), a one-shot compression, and dropping the context. This
    /// is the same as `reset(SessionOnly)`. Waiting first compresses what was
    /// left of the frame's input as one more job, and throws the result away.
    ///
    /// After a job failed on a worker thread (for instance because the frame
    /// was given more than its pledged size), the context refuses any
    /// streaming call or `compress2()` while `NbWorkers` is set, and setting
    /// it again (with a `stage_wrong` error): zstd up to 1.5.7, the version
    /// bundled here, can leave a cleared job behind that the next
    /// multithreaded frame would hand to a worker, which crashes. Create a new
    /// context then, or set `NbWorkers` to 0.
    ///
    /// Once a `ZSTD_e_end` call took all of its input, a frame takes no more:
    /// that is refused with a `stage_wrong` error too, instead of the worker
    /// threads waiting forever for a job after the last one.
    pub fn wait_for_workers(&mut self) -> SafeResult {
        self.reset(ResetDirective::SessionOnly)
    }

    /// Returns the size currently used by this context.
    ///
    /// This may change over time.
    pub fn sizeof(&self) -> usize {
        // Safety: Just FFI
        unsafe { zstd_sys::ZSTD_sizeof_CCtx(self.0.as_ptr()) }
    }

    /// Resets the state of the context.
    ///
    /// Depending on the reset mode, it can reset the session, the parameters, or both.
    ///
    /// Resetting the session abandons the frame in progress, if any: with
    /// worker threads, this first waits for its jobs, see
    /// [`CCtx::wait_for_workers()`]. Like zstd, resetting only the parameters
    /// fails with a `stage_wrong` error while a frame is in progress.
    ///
    /// Wraps the `ZSTD_CCtx_reset()` function.
    pub fn reset(&mut self, reset: ResetDirective) -> SafeResult {
        let resets_parameters = !matches!(reset, ResetDirective::SessionOnly);
        if reset.resets_session() {
            self.drain();
        } else {
            self.guard_workers()?;
        }
        let resets_session = reset.resets_session();
        // Safety: Just FFI
        let res = parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_reset(self.0.as_ptr(), reset.as_sys())
        });
        if res.is_ok() && resets_session {
            self.2.clear();
            self.3.pledged_short = false;
        }
        if res.is_ok() && resets_parameters {
            self.3.requested = false;
            self.3.stable_out = false;
        }
        res
    }

    /// Sets a compression parameter.
    ///
    /// Some of these parameters need to be set during de-compression as well.
    ///
    /// Like zstd, changing `NbWorkers` fails with a `stage_wrong` error while
    /// a frame is in progress, and so does setting it to a non-zero value
    /// after a multithreaded call failed (see [`CCtx::wait_for_workers()`]).
    /// `NbWorkers` and `StableOutBuffer` cannot be used together.
    pub fn set_parameter(&mut self, param: CParameter) -> SafeResult {
        match param {
            CParameter::NbWorkers(workers) => {
                // zstd would resize the job table the old jobs still use.
                self.guard_workers()?;
                if workers != 0 {
                    if self.3.broken {
                        return Err(STAGE_WRONG_ERROR);
                    }
                    #[cfg(feature = "experimental")]
                    if self.3.stable_out {
                        return Err(PARAMETER_UNSUPPORTED_ERROR);
                    }
                }
            }
            // Waiting for the workers needs an output buffer of its own.
            #[cfg(feature = "experimental")]
            CParameter::StableOutBuffer(true) if self.3.requested => {
                return Err(PARAMETER_UNSUPPORTED_ERROR);
            }
            _ => (),
        }
        // TODO: Until bindgen properly generates a binding for this, we'll need to do it here.

        #[cfg(feature = "experimental")]
        use zstd_sys::ZSTD_cParameter::{
            ZSTD_c_experimentalParam1 as ZSTD_c_rsyncable,
            ZSTD_c_experimentalParam10 as ZSTD_c_stableOutBuffer,
            ZSTD_c_experimentalParam11 as ZSTD_c_blockDelimiters,
            ZSTD_c_experimentalParam12 as ZSTD_c_validateSequences,
            ZSTD_c_experimentalParam13 as ZSTD_c_useBlockSplitter,
            ZSTD_c_experimentalParam14 as ZSTD_c_useRowMatchFinder,
            ZSTD_c_experimentalParam15 as ZSTD_c_deterministicRefPrefix,
            ZSTD_c_experimentalParam16 as ZSTD_c_prefetchCDictTables,
            ZSTD_c_experimentalParam17 as ZSTD_c_enableSeqProducerFallback,
            ZSTD_c_experimentalParam18 as ZSTD_c_maxBlockSize,
            ZSTD_c_experimentalParam19 as ZSTD_c_searchForExternalRepcodes,
            ZSTD_c_experimentalParam2 as ZSTD_c_format,
            ZSTD_c_experimentalParam3 as ZSTD_c_forceMaxWindow,
            ZSTD_c_experimentalParam4 as ZSTD_c_forceAttachDict,
            ZSTD_c_experimentalParam5 as ZSTD_c_literalCompressionMode,
            ZSTD_c_experimentalParam7 as ZSTD_c_srcSizeHint,
            ZSTD_c_experimentalParam8 as ZSTD_c_enableDedicatedDictSearch,
            ZSTD_c_experimentalParam9 as ZSTD_c_stableInBuffer,
        };

        use zstd_sys::ZSTD_cParameter::*;
        use CParameter::*;

        let (param, value) = match param {
            #[cfg(feature = "experimental")]
            RSyncable(rsyncable) => (ZSTD_c_rsyncable, rsyncable as c_int),
            #[cfg(feature = "experimental")]
            Format(format) => (ZSTD_c_format, format as c_int),
            #[cfg(feature = "experimental")]
            ForceMaxWindow(force) => (ZSTD_c_forceMaxWindow, force as c_int),
            #[cfg(feature = "experimental")]
            ForceAttachDict(force) => (ZSTD_c_forceAttachDict, force as c_int),
            #[cfg(feature = "experimental")]
            LiteralCompressionMode(mode) => {
                (ZSTD_c_literalCompressionMode, mode as c_int)
            }
            #[cfg(feature = "experimental")]
            SrcSizeHint(value) => (ZSTD_c_srcSizeHint, value as c_int),
            #[cfg(feature = "experimental")]
            EnableDedicatedDictSearch(enable) => {
                (ZSTD_c_enableDedicatedDictSearch, enable as c_int)
            }
            // In stable mode zstd keeps reading from the `InBuffer` of one
            // call during `flush_stream`/`end_stream`, but the slice is only
            // borrowed for that one call: the safe API cannot uphold this.
            #[cfg(feature = "experimental")]
            StableInBuffer(true) => return Err(PARAMETER_UNSUPPORTED_ERROR),
            #[cfg(feature = "experimental")]
            StableInBuffer(false) => (ZSTD_c_stableInBuffer, 0),
            #[cfg(feature = "experimental")]
            StableOutBuffer(stable) => {
                (ZSTD_c_stableOutBuffer, stable as c_int)
            }
            #[cfg(feature = "experimental")]
            BlockDelimiters(value) => (ZSTD_c_blockDelimiters, value as c_int),
            #[cfg(feature = "experimental")]
            ValidateSequences(validate) => {
                (ZSTD_c_validateSequences, validate as c_int)
            }
            #[cfg(feature = "experimental")]
            UseBlockSplitter(split) => {
                (ZSTD_c_useBlockSplitter, split as c_int)
            }
            #[cfg(feature = "experimental")]
            UseRowMatchFinder(mode) => {
                (ZSTD_c_useRowMatchFinder, mode as c_int)
            }
            #[cfg(feature = "experimental")]
            DeterministicRefPrefix(deterministic) => {
                (ZSTD_c_deterministicRefPrefix, deterministic as c_int)
            }
            #[cfg(feature = "experimental")]
            PrefetchCDictTables(prefetch) => {
                (ZSTD_c_prefetchCDictTables, prefetch as c_int)
            }
            #[cfg(feature = "experimental")]
            EnableSeqProducerFallback(enable) => {
                (ZSTD_c_enableSeqProducerFallback, enable as c_int)
            }
            #[cfg(feature = "experimental")]
            MaxBlockSize(value) => (ZSTD_c_maxBlockSize, value as c_int),
            #[cfg(feature = "experimental")]
            SearchForExternalRepcodes(value) => {
                (ZSTD_c_searchForExternalRepcodes, value as c_int)
            }
            TargetCBlockSize(value) => {
                (ZSTD_c_targetCBlockSize, value as c_int)
            }
            CompressionLevel(level) => (ZSTD_c_compressionLevel, level),
            WindowLog(value) => (ZSTD_c_windowLog, value as c_int),
            HashLog(value) => (ZSTD_c_hashLog, value as c_int),
            ChainLog(value) => (ZSTD_c_chainLog, value as c_int),
            SearchLog(value) => (ZSTD_c_searchLog, value as c_int),
            MinMatch(value) => (ZSTD_c_minMatch, value as c_int),
            TargetLength(value) => (ZSTD_c_targetLength, value as c_int),
            Strategy(strategy) => (ZSTD_c_strategy, strategy as c_int),
            EnableLongDistanceMatching(flag) => {
                (ZSTD_c_enableLongDistanceMatching, flag as c_int)
            }
            LdmHashLog(value) => (ZSTD_c_ldmHashLog, value as c_int),
            LdmMinMatch(value) => (ZSTD_c_ldmMinMatch, value as c_int),
            LdmBucketSizeLog(value) => {
                (ZSTD_c_ldmBucketSizeLog, value as c_int)
            }
            LdmHashRateLog(value) => (ZSTD_c_ldmHashRateLog, value as c_int),
            ContentSizeFlag(flag) => (ZSTD_c_contentSizeFlag, flag as c_int),
            ChecksumFlag(flag) => (ZSTD_c_checksumFlag, flag as c_int),
            DictIdFlag(flag) => (ZSTD_c_dictIDFlag, flag as c_int),

            NbWorkers(value) => (ZSTD_c_nbWorkers, value as c_int),

            JobSize(value) => (ZSTD_c_jobSize, value as c_int),

            OverlapSizeLog(value) => (ZSTD_c_overlapLog, value as c_int),
        };

        // Safety: Just FFI
        let res = parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_setParameter(self.0.as_ptr(), param, value)
        });
        if res.is_ok() && matches!(param, ZSTD_c_nbWorkers) {
            self.3.requested = value != 0;
            self.3.ever |= value != 0;
        }
        #[cfg(feature = "experimental")]
        if res.is_ok() && matches!(param, ZSTD_c_stableOutBuffer) {
            self.3.stable_out = value != 0;
        }
        res
    }

    /// Guarantee that the input size will be this value.
    ///
    /// If given `None`, assumes the size is unknown.
    ///
    /// Unless explicitly disabled, this will cause the size to be written in the compressed frame
    /// header.
    ///
    /// If the actual data given to compress has a different size, an error will be returned.
    pub fn set_pledged_src_size(
        &mut self,
        pledged_src_size: Option<u64>,
    ) -> SafeResult {
        // Safety: Just FFI
        let res = parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_setPledgedSrcSize(
                self.0.as_ptr(),
                pledged_src_size.unwrap_or(CONTENTSIZE_UNKNOWN) as c_ulonglong,
            )
        });
        if res.is_ok() {
            // See `Workers::short`.
            self.3.pledged_short = matches!(
                pledged_src_size,
                Some(size) if size <= ZSTDMT_JOBSIZE_MIN as u64
            );
        }
        res
    }

    /// Fails if the tables of this context may not be the ones its applied
    /// parameters describe.
    ///
    /// Starting a multi-threaded frame overwrites the applied parameters with
    /// those of the workers, but leaves the context's own stage and workspace
    /// as the last single-threaded frame left them. If that frame was
    /// abandoned before its first block, zstd still accepts the calls that
    /// work on this context directly, and overruns a workspace that was sized
    /// for other parameters. The applied parameters cannot be queried, so
    /// refuse these calls as soon as a multi-threaded frame could have been
    /// started.
    #[cfg(feature = "experimental")]
    fn guard_own_tables(&self) -> Result<(), ErrorCode> {
        if self.3.ever {
            return Err(STAGE_WRONG_ERROR);
        }
        Ok(())
    }

    /// Creates a copy of this context.
    ///
    /// This only works before any data has been compressed. An error will be
    /// returned otherwise.
    /// A poisoned context cannot be cloned until its session is reset.
    /// A context on which `NbWorkers` was ever set to a non-zero value cannot
    /// be cloned anymore.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub fn try_clone(
        &self,
        pledged_src_size: Option<u64>,
    ) -> Result<Self, ErrorCode> {
        // An error may leave the C context undefined; do not copy it.
        self.2.guard()?;
        self.guard_own_tables()?;
        let context = Self::try_create().ok_or(MEMORY_ALLOCATION_ERROR)?;

        // Safety: Just FFI
        parse_code(unsafe {
            zstd_sys::ZSTD_copyCCtx(
                context.0.as_ptr(),
                self.0.as_ptr(),
                pledged_src_size.unwrap_or(CONTENTSIZE_UNKNOWN),
            )
        })?;

        Ok(context)
    }

    /// Wraps the `ZSTD_getBlockSize()` function.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub fn get_block_size(&self) -> usize {
        // Safety: Just FFI
        unsafe { zstd_sys::ZSTD_getBlockSize(self.0.as_ptr()) }
    }

    /// Wraps the `ZSTD_compressBlock()` function.
    ///
    /// Returns an error once `NbWorkers` was set to a non-zero value on this
    /// context.
    ///
    /// # Safety
    ///
    /// `src` becomes this context's history window, so it must stay allocated and unmodified until
    /// the next call to `compress_block` on this context (or until this context is dropped), as the
    /// following block is compressed against it.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub unsafe fn compress_block<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
    ) -> SafeResult {
        self.guard_own_tables()?;
        // Safety: ZSTD_compressBlock returns the number of bytes written.
        unsafe {
            dst.write_from(|buffer, capacity| {
                parse_code(zstd_sys::ZSTD_compressBlock(
                    self.0.as_ptr(),
                    buffer,
                    capacity,
                    ptr_void(src),
                    src.len(),
                ))
            })
        }
    }

    /// Returns the recommended input buffer size.
    ///
    /// Using this size may result in minor performance boost.
    pub fn in_size() -> usize {
        // Safety: Just FFI
        unsafe { zstd_sys::ZSTD_CStreamInSize() }
    }

    /// Returns the recommended output buffer size.
    ///
    /// Using this may result in minor performance boost.
    pub fn out_size() -> usize {
        // Safety: Just FFI
        unsafe { zstd_sys::ZSTD_CStreamOutSize() }
    }

    /// Use a shared thread pool for this context.
    ///
    /// Thread pool must outlive the context.
    #[cfg(all(feature = "experimental", feature = "zstdmt"))]
    #[cfg_attr(
        feature = "doc-cfg",
        doc(cfg(all(feature = "experimental", feature = "zstdmt")))
    )]
    pub fn ref_thread_pool<'b>(&mut self, pool: &'b ThreadPool) -> SafeResult
    where
        'b: 'a,
    {
        parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_refThreadPool(self.0.as_ptr(), pool.0.as_ptr())
        })
    }

    /// Return to using a private thread pool for this context.
    #[cfg(all(feature = "experimental", feature = "zstdmt"))]
    #[cfg_attr(
        feature = "doc-cfg",
        doc(cfg(all(feature = "experimental", feature = "zstdmt")))
    )]
    pub fn disable_thread_pool(&mut self) -> SafeResult {
        parse_code(unsafe {
            zstd_sys::ZSTD_CCtx_refThreadPool(
                self.0.as_ptr(),
                core::ptr::null_mut(),
            )
        })
    }

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub fn get_frame_progression(&self) -> FrameProgression {
        // Safety: Just FFI
        unsafe { zstd_sys::ZSTD_getFrameProgression(self.0.as_ptr()) }
    }
}

impl<'a> Drop for CCtx<'a> {
    fn drop(&mut self) {
        // zstd up to 1.5.7 frees the dictionary before it stops the worker
        // threads, and does not stop a shared pool's at all: wait for the
        // jobs of the frame in progress first.
        self.drain();
        // Safety: Just FFI
        unsafe {
            zstd_sys::ZSTD_freeCCtx(self.0.as_ptr());
        }
    }
}

unsafe impl Send for CCtx<'_> {}
// Non thread-safe methods already take `&mut self`, so it's fine to implement Sync here.
unsafe impl Sync for CCtx<'_> {}

unsafe fn c_char_to_str(text: *const c_char) -> &'static str {
    core::ffi::CStr::from_ptr(text)
        .to_str()
        .expect("bad error message from zstd")
}

/// Returns the error string associated with an error code.
pub fn get_error_name(code: usize) -> &'static str {
    unsafe {
        // Safety: assumes ZSTD returns a well-formed utf8 string.
        let name = zstd_sys::ZSTD_getErrorName(code);
        c_char_to_str(name)
    }
}

/// A Decompression Context.
///
/// The lifetime references the potential dictionary used for this context.
///
/// If no dictionary was used, it will most likely be `'static`.
///
/// Same as `DStream`.
pub struct DCtx<'a>(
    NonNull<zstd_sys::ZSTD_DCtx>,
    PhantomData<&'a ()>,
    Poison,
    DState,
);

/// What a `DCtx` remembers about its own use.
#[derive(Clone, Copy, Debug, Default)]
struct DState {
    /// `RefMultipleDDicts` was ever enabled, see `try_clone`.
    #[cfg(feature = "experimental")]
    multiple_ddicts: bool,
    /// A streaming (or block) call ran since the session was last reset, so
    /// a stream may be open, see `finish_one_shot`.
    streamed: bool,
}

impl Default for DCtx<'_> {
    fn default() -> Self {
        DCtx::create()
    }
}

impl<'a> DCtx<'a> {
    /// Try to create a new decompression context.
    ///
    /// Returns `None` if the operation failed (for example, not enough memory).
    pub fn try_create() -> Option<Self> {
        Some(DCtx(
            NonNull::new(unsafe { zstd_sys::ZSTD_createDCtx() })?,
            PhantomData,
            Poison::default(),
            DState::default(),
        ))
    }

    /// Creates a new decoding context.
    ///
    /// # Panics
    ///
    /// If the context creation fails.
    pub fn create() -> Self {
        Self::try_create()
            .expect("zstd returned null pointer when creating new context")
    }

    /// Ends the session of a one-shot decompression, and passes `res` through.
    ///
    /// zstd.h has these calls imply a reset, but they leave the stream stage
    /// alone. A stream they interrupted would then carry on from a state that
    /// still points at buffers and dictionaries borrowed for that one call. So
    /// reset explicitly: the next stream starts from scratch.
    ///
    /// Only an open stream can carry on, though: a new one starts by setting
    /// everything up again from the context's own dictionary. So when no
    /// streaming call ran since the last reset, there is nothing to end, and
    /// skipping the reset saves a call per frame - which shows on tiny ones.
    ///
    /// Only the check is inlined: it runs once per frame.
    #[inline]
    fn finish_one_shot(&mut self, res: SafeResult) -> SafeResult {
        if self.3.streamed || self.2.guard().is_err() {
            self.reset_after_one_shot(res)
        } else {
            res
        }
    }

    #[cold]
    #[inline(never)]
    fn reset_after_one_shot(&mut self, res: SafeResult) -> SafeResult {
        self.reset(ResetDirective::SessionOnly).and(res)
    }

    /// Fully decompress the given frame.
    ///
    /// This decompress an entire frame in-memory. If you can have enough memory to store both the
    /// input and output buffer, then it may be faster that streaming decompression.
    ///
    /// Wraps the `ZSTD_decompressDCtx()` function.
    pub fn decompress<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
    ) -> SafeResult {
        let res = unsafe {
            dst.write_from(|buffer, capacity| {
                parse_code(zstd_sys::ZSTD_decompressDCtx(
                    self.0.as_ptr(),
                    buffer,
                    capacity,
                    ptr_void(src),
                    src.len(),
                ))
            })
        };
        self.finish_one_shot(res)
    }

    /// Fully decompress the given frame using a dictionary.
    ///
    /// Dictionary must be identical to the one used during compression.
    ///
    /// If you plan on using the same dictionary multiple times, it is faster to create a `DDict`
    /// first and use `decompress_using_ddict`.
    ///
    /// Wraps `ZSTD_decompress_usingDict`
    pub fn decompress_using_dict<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
        dict: &[u8],
    ) -> SafeResult {
        let res = unsafe {
            dst.write_from(|buffer, capacity| {
                parse_code(zstd_sys::ZSTD_decompress_usingDict(
                    self.0.as_ptr(),
                    buffer,
                    capacity,
                    ptr_void(src),
                    src.len(),
                    ptr_void(dict),
                    dict.len(),
                ))
            })
        };
        self.finish_one_shot(res)
    }

    /// Fully decompress the given frame using a dictionary.
    ///
    /// Dictionary must be identical to the one used during compression.
    ///
    /// Wraps the `ZSTD_decompress_usingDDict()` function.
    pub fn decompress_using_ddict<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
        ddict: &DDict<'_>,
    ) -> SafeResult {
        let res = unsafe {
            dst.write_from(|buffer, capacity| {
                parse_code(zstd_sys::ZSTD_decompress_usingDDict(
                    self.0.as_ptr(),
                    buffer,
                    capacity,
                    ptr_void(src),
                    src.len(),
                    ddict.0.as_ptr(),
                ))
            })
        };
        self.finish_one_shot(res)
    }

    /// Initializes an existing `DStream` for decompression.
    ///
    /// This is equivalent to calling:
    /// * `reset(SessionOnly)`
    /// * `disable_dictionary()`
    ///
    /// Wraps the `ZSTD_initCStream()` function.
    pub fn init(&mut self) -> SafeResult {
        self.2.clear();
        let code = unsafe { zstd_sys::ZSTD_initDStream(self.0.as_ptr()) };
        self.2.record(parse_code(code))
    }

    /// Wraps the `ZSTD_initDStream_usingDict()` function.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    #[deprecated]
    pub fn init_using_dict(&mut self, dict: &[u8]) -> SafeResult {
        self.2.clear();
        let code = unsafe {
            zstd_sys::ZSTD_initDStream_usingDict(
                self.0.as_ptr(),
                ptr_void(dict),
                dict.len(),
            )
        };
        parse_code(code)
    }

    /// Wraps the `ZSTD_initDStream_usingDDict()` function.
    ///
    /// Dictionary must outlive the context.
    ///
    /// ```compile_fail,E0515
    /// # #![allow(deprecated)]
    /// fn decompressor() -> zstd_safe::DCtx<'static> {
    ///     let ddict = zstd_safe::DDict::create(b"dictionary");
    ///     let mut dctx = zstd_safe::DCtx::create();
    ///     dctx.init_using_ddict(&ddict).unwrap();
    ///     dctx // `ddict` would be dropped while `dctx` still points at it.
    /// }
    /// ```
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    #[deprecated]
    pub fn init_using_ddict<'b>(&mut self, ddict: &'a DDict<'b>) -> SafeResult
    where
        'b: 'a,
    {
        self.2.clear();
        // Safety: Just FFI
        let code = unsafe {
            zstd_sys::ZSTD_initDStream_usingDDict(
                self.0.as_ptr(),
                ddict.0.as_ptr(),
            )
        };
        self.2.record(parse_code(code))
    }

    /// Resets the state of the context.
    ///
    /// Depending on the reset mode, it can reset the session, the parameters, or both.
    ///
    /// Wraps the `ZSTD_DCtx_reset()` function.
    pub fn reset(&mut self, reset: ResetDirective) -> SafeResult {
        let resets_session = reset.resets_session();
        let res = parse_code(unsafe {
            zstd_sys::ZSTD_DCtx_reset(self.0.as_ptr(), reset.as_sys())
        });
        if res.is_ok() && resets_session {
            self.2.clear();
            self.3.streamed = false;
        }
        res
    }

    /// Loads a dictionary.
    ///
    /// This will let this context decompress frames that were compressed using this dictionary.
    ///
    /// The dictionary content will be copied internally and does not need to be kept alive after
    /// calling this function.
    ///
    /// If you need to use the same dictionary for multiple contexts, it may be more efficient to
    /// create a `DDict` first, then loads that.
    ///
    /// The dictionary will apply to all future frames, until a new dictionary is set.
    pub fn load_dictionary(&mut self, dict: &[u8]) -> SafeResult {
        parse_code(unsafe {
            zstd_sys::ZSTD_DCtx_loadDictionary(
                self.0.as_ptr(),
                ptr_void(dict),
                dict.len(),
            )
        })
    }

    /// Return to "no-dictionary" mode.
    ///
    /// This will disable any dictionary/prefix previously registered for future frames.
    pub fn disable_dictionary(&mut self) -> SafeResult {
        parse_code(unsafe {
            zstd_sys::ZSTD_DCtx_loadDictionary(
                self.0.as_ptr(),
                core::ptr::null(),
                0,
            )
        })
    }

    /// References a dictionary.
    ///
    /// This will let this context decompress frames compressed with the same dictionary.
    ///
    /// It will apply to all frames decompressed by this context (until a new dictionary is set).
    ///
    /// Wraps the `ZSTD_DCtx_refDDict()` function.
    pub fn ref_ddict<'b>(&mut self, ddict: &DDict<'b>) -> SafeResult
    where
        'b: 'a,
    {
        parse_code(unsafe {
            zstd_sys::ZSTD_DCtx_refDDict(self.0.as_ptr(), ddict.0.as_ptr())
        })
    }

    /// Use some prefix as single-use dictionary for the next frame.
    ///
    /// Just like a dictionary, this only works if compression was done with the same prefix.
    ///
    /// But unlike a dictionary, this only applies to the next frame.
    ///
    /// Wraps the `ZSTD_DCtx_refPrefix()` function.
    pub fn ref_prefix<'b>(&mut self, prefix: &'b [u8]) -> SafeResult
    where
        'b: 'a,
    {
        parse_code(unsafe {
            zstd_sys::ZSTD_DCtx_refPrefix(
                self.0.as_ptr(),
                ptr_void(prefix),
                prefix.len(),
            )
        })
    }

    /// Sets a decompression parameter.
    pub fn set_parameter(&mut self, param: DParameter) -> SafeResult {
        #[cfg(feature = "experimental")]
        use zstd_sys::ZSTD_dParameter::{
            ZSTD_d_experimentalParam1 as ZSTD_d_format,
            ZSTD_d_experimentalParam2 as ZSTD_d_stableOutBuffer,
            ZSTD_d_experimentalParam3 as ZSTD_d_forceIgnoreChecksum,
            ZSTD_d_experimentalParam4 as ZSTD_d_refMultipleDDicts,
        };

        use zstd_sys::ZSTD_dParameter::*;
        use DParameter::*;

        let (param, value) = match param {
            #[cfg(feature = "experimental")]
            Format(format) => (ZSTD_d_format, format as c_int),
            #[cfg(feature = "experimental")]
            StableOutBuffer(stable) => {
                (ZSTD_d_stableOutBuffer, stable as c_int)
            }
            #[cfg(feature = "experimental")]
            ForceIgnoreChecksum(force) => {
                (ZSTD_d_forceIgnoreChecksum, force as c_int)
            }
            #[cfg(feature = "experimental")]
            RefMultipleDDicts(value) => {
                // From now on `ref_ddict` may allocate the table of
                // dictionaries, which zstd keeps until the context is freed.
                self.3.multiple_ddicts |= value;
                (ZSTD_d_refMultipleDDicts, value as c_int)
            }

            WindowLogMax(value) => (ZSTD_d_windowLogMax, value as c_int),
        };

        parse_code(unsafe {
            zstd_sys::ZSTD_DCtx_setParameter(self.0.as_ptr(), param, value)
        })
    }

    /// Performs a step of a streaming decompression operation.
    ///
    /// This will read some data from `input` and/or write some data to `output`.
    ///
    /// # Returns
    ///
    /// * `Ok(0)` if the current frame just finished decompressing successfully.
    /// * `Ok(hint)` with a hint for the "ideal" amount of input data to provide in the next call.
    ///     Can be safely ignored.
    ///
    /// Wraps the `ZSTD_decompressStream()` function.
    pub fn decompress_stream<C: WriteBuf + ?Sized>(
        &mut self,
        output: &mut OutBuffer<'_, C>,
        input: &mut InBuffer<'_>,
    ) -> SafeResult {
        self.2.guard()?;
        self.3.streamed = true;
        let mut output = output.wrap();
        let mut input = input.wrap();
        let code = unsafe {
            zstd_sys::ZSTD_decompressStream(
                self.0.as_ptr(),
                ptr_mut(&mut output),
                ptr_mut(&mut input),
            )
        };
        let res = output.check(parse_code(code));
        self.2.record(input.check(res))
    }

    /// Wraps the `ZSTD_DStreamInSize()` function.
    ///
    /// Returns a hint for the recommended size of the input buffer for decompression.
    pub fn in_size() -> usize {
        unsafe { zstd_sys::ZSTD_DStreamInSize() }
    }

    /// Wraps the `ZSTD_DStreamOutSize()` function.
    ///
    /// Returns a hint for the recommended size of the output buffer for decompression.
    pub fn out_size() -> usize {
        unsafe { zstd_sys::ZSTD_DStreamOutSize() }
    }

    /// Wraps the `ZSTD_sizeof_DCtx()` function.
    pub fn sizeof(&self) -> usize {
        unsafe { zstd_sys::ZSTD_sizeof_DCtx(self.0.as_ptr()) }
    }

    /// Wraps the `ZSTD_decompressBlock()` function.
    ///
    /// # Safety
    ///
    /// The bytes written to `dst` become this context's history window, so `dst` must stay
    /// allocated and unmodified until the next call to `decompress_block` or `insert_block` on this
    /// context (or until this context is dropped), as the following block is decoded against it.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub unsafe fn decompress_block<C: WriteBuf + ?Sized>(
        &mut self,
        dst: &mut C,
        src: &[u8],
    ) -> SafeResult {
        self.3.streamed = true;
        unsafe {
            dst.write_from(|buffer, capacity| {
                parse_code(zstd_sys::ZSTD_decompressBlock(
                    self.0.as_ptr(),
                    buffer,
                    capacity,
                    ptr_void(src),
                    src.len(),
                ))
            })
        }
    }

    /// Wraps the `ZSTD_insertBlock()` function.
    ///
    /// # Safety
    ///
    /// `block` becomes this context's history window, so it must stay allocated and unmodified
    /// until the next call to `decompress_block` or `insert_block` on this context (or until this
    /// context is dropped), as the following block is decoded against it.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub unsafe fn insert_block(&mut self, block: &[u8]) -> usize {
        self.3.streamed = true;
        unsafe {
            zstd_sys::ZSTD_insertBlock(
                self.0.as_ptr(),
                ptr_void(block),
                block.len(),
            )
        }
    }

    /// Creates a copy of this context.
    ///
    /// This only works before any data has been decompressed. An error will be
    /// returned otherwise.
    /// A poisoned context cannot be cloned until its session is reset.
    /// The copy always starts at the beginning of a frame.
    ///
    /// A dictionary this context merely references (see [`DCtx::ref_ddict`])
    /// is referenced by the copy too. One it owns cannot be shared, so an
    /// error is also returned while a dictionary from
    /// [`DCtx::load_dictionary`] or a prefix from [`DCtx::ref_prefix`] is set,
    /// and once [`DParameter::RefMultipleDDicts`] has been enabled.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub fn try_clone(&self) -> Result<Self, ErrorCode> {
        use zstd_sys::ZSTD_dParameter::{
            ZSTD_d_experimentalParam2 as ZSTD_d_stableOutBuffer,
            ZSTD_d_windowLogMax,
        };

        // An error may leave the C context undefined; do not copy it.
        self.2.guard()?;
        let mut context = Self::try_create().ok_or(MEMORY_ALLOCATION_ERROR)?;

        // `ZSTD_copyDCtx()` is a plain `memcpy`, so a dictionary `self` owns
        // would end up owned, and freed, by both contexts. A loaded dictionary
        // or prefix shows up as memory a new context does not use - as do the
        // streaming buffers, once data has been decompressed. The table behind
        // `RefMultipleDDicts` is not counted there, and outlives the
        // parameter, hence the flag.
        if self.3.multiple_ddicts || self.sizeof() != context.sizeof() {
            return Err(STAGE_WRONG_ERROR);
        }

        // `ZSTD_copyDCtx` stops where the streaming buffers start and leaves
        // the parameters stored past that point (the window limit and the
        // output buffer mode) at their defaults. Carry them over explicitly,
        // on the fresh context so that it is still in its initial stage; the
        // copy below does not touch these fields.
        // Only for values that differ: the window limit reads back as a log,
        // which rounds zstd's default limit down.
        let get = |dctx: &Self, param| {
            let mut value: c_int = 0;
            // Safety: Just FFI
            parse_code(unsafe {
                zstd_sys::ZSTD_DCtx_getParameter(
                    dctx.0.as_ptr(),
                    param,
                    &mut value,
                )
            })
            .map(|_| value)
        };
        for param in [ZSTD_d_windowLogMax, ZSTD_d_stableOutBuffer] {
            let value = get(self, param)?;
            if value == get(&context, param)? {
                continue;
            }
            // Safety: Just FFI
            parse_code(unsafe {
                zstd_sys::ZSTD_DCtx_setParameter(
                    context.0.as_ptr(),
                    param,
                    value,
                )
            })?;
        }

        // Safety: Just FFI
        unsafe {
            zstd_sys::ZSTD_copyDCtx(context.0.as_ptr(), self.0.as_ptr())
        };

        // The position within a frame is copied, but not the state that goes
        // with it (a partial header, a legacy context), so start from a frame
        // boundary rather than run on what the new context never initialized.
        context.reset(ResetDirective::SessionOnly)?;

        Ok(context)
    }
}

impl Drop for DCtx<'_> {
    fn drop(&mut self) {
        unsafe {
            zstd_sys::ZSTD_freeDCtx(self.0.as_ptr());
        }
    }
}

unsafe impl Send for DCtx<'_> {}
// Non thread-safe methods already take `&mut self`, so it's fine to implement Sync here.
unsafe impl Sync for DCtx<'_> {}

/// Compression dictionary.
pub struct CDict<'a>(NonNull<zstd_sys::ZSTD_CDict>, PhantomData<&'a ()>);

impl CDict<'static> {
    /// Prepare a dictionary to compress data.
    ///
    /// This will make it easier for compression contexts to load this dictionary.
    ///
    /// The dictionary content will be copied internally, and does not need to be kept around.
    ///
    /// # Panics
    ///
    /// If loading this dictionary failed.
    pub fn create(
        dict_buffer: &[u8],
        compression_level: CompressionLevel,
    ) -> Self {
        Self::try_create(dict_buffer, compression_level)
            .expect("zstd returned null pointer when creating dict")
    }

    /// Prepare a dictionary to compress data.
    ///
    /// This will make it easier for compression contexts to load this dictionary.
    ///
    /// The dictionary content will be copied internally, and does not need to be kept around.
    pub fn try_create(
        dict_buffer: &[u8],
        compression_level: CompressionLevel,
    ) -> Option<Self> {
        Some(CDict(
            NonNull::new(unsafe {
                zstd_sys::ZSTD_createCDict(
                    ptr_void(dict_buffer),
                    dict_buffer.len(),
                    compression_level,
                )
            })?,
            PhantomData,
        ))
    }
}

impl<'a> CDict<'a> {
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub fn create_by_reference(
        dict_buffer: &'a [u8],
        compression_level: CompressionLevel,
    ) -> Self {
        CDict(
            NonNull::new(unsafe {
                zstd_sys::ZSTD_createCDict_byReference(
                    ptr_void(dict_buffer),
                    dict_buffer.len(),
                    compression_level,
                )
            })
            .expect("zstd returned null pointer"),
            PhantomData,
        )
    }

    /// Returns the _current_ memory usage of this dictionary.
    ///
    /// Note that this may change over time.
    pub fn sizeof(&self) -> usize {
        unsafe { zstd_sys::ZSTD_sizeof_CDict(self.0.as_ptr()) }
    }

    /// Returns the dictionary ID for this dict.
    ///
    /// Returns `None` if this dictionary is empty or invalid.
    pub fn get_dict_id(&self) -> Option<NonZeroU32> {
        NonZeroU32::new(unsafe {
            zstd_sys::ZSTD_getDictID_fromCDict(self.0.as_ptr()) as u32
        })
    }
}

/// Wraps the `ZSTD_createCDict()` function.
pub fn create_cdict(
    dict_buffer: &[u8],
    compression_level: CompressionLevel,
) -> CDict<'static> {
    CDict::create(dict_buffer, compression_level)
}

impl<'a> Drop for CDict<'a> {
    fn drop(&mut self) {
        unsafe {
            zstd_sys::ZSTD_freeCDict(self.0.as_ptr());
        }
    }
}

unsafe impl<'a> Send for CDict<'a> {}
unsafe impl<'a> Sync for CDict<'a> {}

/// Wraps the `ZSTD_compress_usingCDict()` function.
pub fn compress_using_cdict(
    cctx: &mut CCtx<'_>,
    dst: &mut [u8],
    src: &[u8],
    cdict: &CDict<'_>,
) -> SafeResult {
    cctx.compress_using_cdict(dst, src, cdict)
}

/// A digested decompression dictionary.
pub struct DDict<'a>(NonNull<zstd_sys::ZSTD_DDict>, PhantomData<&'a ()>);

impl DDict<'static> {
    pub fn create(dict_buffer: &[u8]) -> Self {
        Self::try_create(dict_buffer)
            .expect("zstd returned null pointer when creating dict")
    }

    pub fn try_create(dict_buffer: &[u8]) -> Option<Self> {
        Some(DDict(
            NonNull::new(unsafe {
                zstd_sys::ZSTD_createDDict(
                    ptr_void(dict_buffer),
                    dict_buffer.len(),
                )
            })?,
            PhantomData,
        ))
    }
}

impl<'a> DDict<'a> {
    pub fn sizeof(&self) -> usize {
        unsafe { zstd_sys::ZSTD_sizeof_DDict(self.0.as_ptr()) }
    }

    /// Wraps the `ZSTD_createDDict_byReference()` function.
    ///
    /// The dictionary will keep referencing `dict_buffer`.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    pub fn create_by_reference(dict_buffer: &'a [u8]) -> Self {
        DDict(
            NonNull::new(unsafe {
                zstd_sys::ZSTD_createDDict_byReference(
                    ptr_void(dict_buffer),
                    dict_buffer.len(),
                )
            })
            .expect("zstd returned null pointer"),
            PhantomData,
        )
    }

    /// Returns the dictionary ID for this dict.
    ///
    /// Returns `None` if this dictionary is empty or invalid.
    pub fn get_dict_id(&self) -> Option<NonZeroU32> {
        NonZeroU32::new(unsafe {
            zstd_sys::ZSTD_getDictID_fromDDict(self.0.as_ptr()) as u32
        })
    }
}

/// Wraps the `ZSTD_createDDict()` function.
///
/// It copies the dictionary internally, so the resulting `DDict` is `'static`.
pub fn create_ddict(dict_buffer: &[u8]) -> DDict<'static> {
    DDict::create(dict_buffer)
}

impl<'a> Drop for DDict<'a> {
    fn drop(&mut self) {
        unsafe {
            zstd_sys::ZSTD_freeDDict(self.0.as_ptr());
        }
    }
}

unsafe impl<'a> Send for DDict<'a> {}
unsafe impl<'a> Sync for DDict<'a> {}

/// A shared thread pool for one or more compression contexts
#[cfg(all(feature = "experimental", feature = "zstdmt"))]
#[cfg_attr(
    feature = "doc-cfg",
    doc(cfg(all(feature = "experimental", feature = "zstdmt")))
)]
pub struct ThreadPool(NonNull<zstd_sys::ZSTD_threadPool>);

#[cfg(all(feature = "experimental", feature = "zstdmt"))]
#[cfg_attr(
    feature = "doc-cfg",
    doc(cfg(all(feature = "experimental", feature = "zstdmt")))
)]
impl ThreadPool {
    /// Create a thread pool with the specified number of threads.
    ///
    /// # Panics
    ///
    /// If creating the thread pool failed.
    pub fn new(num_threads: usize) -> Self {
        Self::try_new(num_threads)
            .expect("zstd returned null pointer when creating thread pool")
    }

    /// Create a thread pool with the specified number of threads.
    pub fn try_new(num_threads: usize) -> Option<Self> {
        Some(Self(NonNull::new(unsafe {
            zstd_sys::ZSTD_createThreadPool(num_threads)
        })?))
    }
}

#[cfg(all(feature = "experimental", feature = "zstdmt"))]
#[cfg_attr(
    feature = "doc-cfg",
    doc(cfg(all(feature = "experimental", feature = "zstdmt")))
)]
impl Drop for ThreadPool {
    fn drop(&mut self) {
        unsafe {
            zstd_sys::ZSTD_freeThreadPool(self.0.as_ptr());
        }
    }
}

#[cfg(all(feature = "experimental", feature = "zstdmt"))]
#[cfg_attr(
    feature = "doc-cfg",
    doc(cfg(all(feature = "experimental", feature = "zstdmt")))
)]
unsafe impl Send for ThreadPool {}
#[cfg(all(feature = "experimental", feature = "zstdmt"))]
#[cfg_attr(
    feature = "doc-cfg",
    doc(cfg(all(feature = "experimental", feature = "zstdmt")))
)]
unsafe impl Sync for ThreadPool {}

/// Wraps the `ZSTD_decompress_usingDDict()` function.
pub fn decompress_using_ddict(
    dctx: &mut DCtx<'_>,
    dst: &mut [u8],
    src: &[u8],
    ddict: &DDict<'_>,
) -> SafeResult {
    dctx.decompress_using_ddict(dst, src, ddict)
}

/// Compression stream.
///
/// Same as `CCtx`.
pub type CStream<'a> = CCtx<'a>;

// CStream can't be shared across threads, so it does not implement Sync.

/// Allocates a new `CStream`.
pub fn create_cstream<'a>() -> CStream<'a> {
    CCtx::create()
}

/// Prepares an existing `CStream` for compression at the given level.
pub fn init_cstream(
    zcs: &mut CStream<'_>,
    compression_level: CompressionLevel,
) -> SafeResult {
    zcs.init(compression_level)
}

#[derive(Debug)]
/// Wrapper around an input buffer.
///
/// Bytes will be read starting at `src[pos]`.
///
/// `pos` will be updated after reading.
///
/// # Invariant
///
/// `pos <= src.len()`
///
/// Both fields are public, so this is checked (and panics if it does not hold)
/// whenever the buffer is given to zstd.
pub struct InBuffer<'a> {
    pub src: &'a [u8],
    pub pos: usize,
}

/// Describe a bytes container, like `Vec<u8>`.
///
/// Represents a contiguous segment of allocated memory, a prefix of which is initialized.
///
/// It allows starting from an uninitializes chunk of memory and writing to it, progressively
/// initializing it. No re-allocation typically occur after the initial creation.
///
/// The main implementors are:
/// * `Vec<u8>` and similar structures. These hold both a length (initialized data) and a capacity
///   (allocated memory).
///
///   Use `Vec::with_capacity` to create an empty `Vec` with non-zero capacity, and the length
///   field will be updated to cover the data written to it (as long as it fits in the given
///   capacity).
/// * `[u8]` and `[u8; N]`. These must start already-initialized, and will not be resized. It will
///   be up to the caller to only use the part that was written (as returned by the various writing
///   operations).
/// * `std::io::Cursor<T: WriteBuf>`. This will ignore data before the cursor's position, and
///   append data after that.
pub unsafe trait WriteBuf {
    /// Returns the valid data part of this container. Should only cover initialized data.
    fn as_slice(&self) -> &[u8];

    /// Returns the full capacity of this container. May include uninitialized data.
    fn capacity(&self) -> usize;

    /// Returns a pointer to the start of the data.
    fn as_mut_ptr(&mut self) -> *mut u8;

    /// Indicates that the first `n` bytes of the container have been written.
    ///
    /// Safety: this should only be called if the `n` first bytes of this buffer have actually been
    /// initialized.
    unsafe fn filled_until(&mut self, n: usize);

    /// Call the given closure using the pointer and capacity from `self`.
    ///
    /// Assumes the given function returns a parseable code, which if valid, represents how many
    /// bytes were written to `self`.
    ///
    /// The given closure must treat its first argument as pointing to potentially uninitialized
    /// memory, and should not read from it.
    ///
    /// In addition, it must have written at least `n` bytes contiguously from this pointer, where
    /// `n` is the returned value.
    unsafe fn write_from<F>(&mut self, f: F) -> SafeResult
    where
        F: FnOnce(*mut c_void, usize) -> SafeResult,
    {
        let capacity = self.capacity();
        let res = f(ptr_mut_void(self), capacity);
        if let Ok(n) = res {
            // Nothing can have been written past the end of the buffer; a
            // larger count would mark memory we do not own as initialized.
            assert!(n <= capacity, "zstd reported writing past the buffer");
            self.filled_until(n);
        }
        res
    }
}

/// The position of a `Cursor`, as an index into the buffer it wraps.
///
/// `Cursor` stores the position as a `u64` and lets it be set anywhere, so on a
/// target where `usize` is narrower it can name an offset no buffer can hold.
/// Saturating leaves such a position out of range, where the bounds checks
/// below reject it; casting would wrap it around into a valid-looking offset
/// and quietly read or write the wrong part of the buffer.
#[cfg(feature = "std")]
fn cursor_position<T>(cursor: &std::io::Cursor<T>) -> usize {
    use core::convert::TryFrom;

    usize::try_from(cursor.position()).unwrap_or(usize::MAX)
}

#[cfg(feature = "std")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "std")))]
unsafe impl<T> WriteBuf for std::io::Cursor<T>
where
    T: WriteBuf,
{
    fn as_slice(&self) -> &[u8] {
        // A cursor past the inner buffer's initialized part has nothing
        // initialized after it yet (see `filled_until` below).
        self.get_ref()
            .as_slice()
            .get(cursor_position(self)..)
            .unwrap_or(&[])
    }

    fn capacity(&self) -> usize {
        self.get_ref()
            .capacity()
            .saturating_sub(cursor_position(self))
    }

    fn as_mut_ptr(&mut self) -> *mut u8 {
        let start = cursor_position(self);
        assert!(start <= self.get_ref().capacity());
        // Safety: start is still in the same memory allocation
        unsafe { self.get_mut().as_mut_ptr().add(start) }
    }

    unsafe fn filled_until(&mut self, n: usize) {
        // Here we assume data _before_ self.position() was already initialized.
        // Egh it's not actually guaranteed by Cursor? So let's guarantee it ourselves.
        // Since the cursor wraps another `WriteBuf`, we know how much data is initialized there.
        let position = cursor_position(self);
        let capacity = self.get_ref().capacity();
        if n == 0 && position > capacity {
            // Nothing was written, and nothing could have been: there is
            // no room at all after the cursor.
            return;
        }
        // Otherwise the caller wrote `n` bytes starting at `position`, so
        // `position` is inside the buffer. Checking it before the zero-fill
        // below keeps a position that could not be converted from running off
        // the end.
        //
        // Note that `n == 0` still truncates the data after the cursor, like
        // any other `n` does: an empty result must not leave a previous one
        // in place.
        assert!(position <= capacity);
        let initialized = self.get_ref().as_slice().len();
        if let Some(uninitialized) = position.checked_sub(initialized) {
            // Here, the cursor is further than the known-initialized part.
            // Cursor's solution is to pad with zeroes, so let's do the same.
            // We'll zero bytes from the end of valid data (as_slice().len()) to the cursor position.

            // Safety:
            // * We checked `position <= self.get_ref().capacity()`
            // * This means that `position` is within (or one past) the nested
            //   pointer's allocation.
            // * Finally, `initialized + uninitialized = position`, so the entire byte
            //   range here is within the allocation
            unsafe {
                self.get_mut()
                    .as_mut_ptr()
                    .add(initialized)
                    .write_bytes(0u8, uninitialized)
            };
        }

        let start = position;
        assert!(start + n <= self.get_ref().capacity());
        self.get_mut().filled_until(start + n);
    }
}

#[cfg(feature = "std")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "std")))]
unsafe impl<'a> WriteBuf for &'a mut std::vec::Vec<u8> {
    fn as_slice(&self) -> &[u8] {
        std::vec::Vec::as_slice(self)
    }

    fn capacity(&self) -> usize {
        std::vec::Vec::capacity(self)
    }

    fn as_mut_ptr(&mut self) -> *mut u8 {
        std::vec::Vec::as_mut_ptr(self)
    }

    unsafe fn filled_until(&mut self, n: usize) {
        std::vec::Vec::set_len(self, n)
    }
}

#[cfg(feature = "std")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "std")))]
unsafe impl WriteBuf for std::vec::Vec<u8> {
    fn as_slice(&self) -> &[u8] {
        &self[..]
    }
    fn capacity(&self) -> usize {
        self.capacity()
    }
    fn as_mut_ptr(&mut self) -> *mut u8 {
        self.as_mut_ptr()
    }
    unsafe fn filled_until(&mut self, n: usize) {
        self.set_len(n);
    }
}

#[cfg(feature = "arrays")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "arrays")))]
unsafe impl<const N: usize> WriteBuf for [u8; N] {
    fn as_slice(&self) -> &[u8] {
        self
    }
    fn capacity(&self) -> usize {
        self.len()
    }

    fn as_mut_ptr(&mut self) -> *mut u8 {
        (&mut self[..]).as_mut_ptr()
    }

    unsafe fn filled_until(&mut self, _n: usize) {
        // Assume the slice is already initialized
    }
}

unsafe impl WriteBuf for [u8] {
    fn as_slice(&self) -> &[u8] {
        self
    }
    fn capacity(&self) -> usize {
        self.len()
    }

    fn as_mut_ptr(&mut self) -> *mut u8 {
        self.as_mut_ptr()
    }

    unsafe fn filled_until(&mut self, _n: usize) {
        // Assume the slice is already initialized
    }
}

/*
// This is possible, but... why?
unsafe impl<'a> WriteBuf for OutBuffer<'a, [u8]> {
    fn as_slice(&self) -> &[u8] {
        self.dst
    }
    fn capacity(&self) -> usize {
        self.dst.len()
    }
    fn as_mut_ptr(&mut self) -> *mut u8 {
        self.dst.as_mut_ptr()
    }
    unsafe fn filled_until(&mut self, n: usize) {
        self.pos = n;
    }
}
*/

#[derive(Debug)]
/// Wrapper around an output buffer.
///
/// `C` is usually either `[u8]` or `Vec<u8>`.
///
/// Bytes will be written starting at `dst[pos]`.
///
/// `pos` will be updated after writing.
///
/// # Invariant
///
/// `pos <= dst.capacity()`
pub struct OutBuffer<'a, C: WriteBuf + ?Sized> {
    dst: &'a mut C,
    pos: usize,
}

/// Convenience method to get a mut pointer from a mut ref.
fn ptr_mut<B>(ptr_void: &mut B) -> *mut B {
    ptr_void as *mut B
}

/// Interface between a C-level ZSTD_outBuffer and a rust-level `OutBuffer`.
///
/// Will update the parent buffer from the C buffer on drop.
struct OutBufferWrapper<'a, 'b, C: WriteBuf + ?Sized> {
    buf: zstd_sys::ZSTD_outBuffer,
    parent: &'a mut OutBuffer<'b, C>,
}

impl<'a, 'b: 'a, C: WriteBuf + ?Sized> Deref for OutBufferWrapper<'a, 'b, C> {
    type Target = zstd_sys::ZSTD_outBuffer;

    fn deref(&self) -> &Self::Target {
        &self.buf
    }
}

impl<'a, 'b: 'a, C: WriteBuf + ?Sized> DerefMut
    for OutBufferWrapper<'a, 'b, C>
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.buf
    }
}

impl<'a, C: WriteBuf + ?Sized> OutBuffer<'a, C> {
    /// Returns a new `OutBuffer` around the given slice.
    ///
    /// Starts with `pos = 0`.
    pub fn around(dst: &'a mut C) -> Self {
        OutBuffer { dst, pos: 0 }
    }

    /// Returns a new `OutBuffer` around the given slice, starting at the given position.
    ///
    /// If `pos` is past the initialized part of `dst` (for example a `Vec`
    /// whose `len()` is smaller than `pos`), the gap is zero-filled, as a
    /// `Cursor` would do.
    ///
    /// # Panics
    ///
    /// If `pos > dst.capacity()`.
    pub fn around_pos(dst: &'a mut C, pos: usize) -> Self {
        if pos > dst.capacity() {
            panic!("Given position outside of the buffer bounds.");
        }

        let initialized = dst.as_slice().len();
        if pos > initialized {
            // Writing to this buffer marks everything before `pos` as
            // initialized (see `set_pos`), so it has to be before we start.
            // Safety: `initialized..pos` lies within `capacity()`, which
            // `WriteBuf` promises is valid to write to.
            unsafe {
                dst.as_mut_ptr()
                    .add(initialized)
                    .write_bytes(0u8, pos - initialized);
                dst.filled_until(pos);
            }
        }

        OutBuffer { dst, pos }
    }

    /// Returns the current cursor position.
    ///
    /// Guaranteed to be <= self.capacity()
    pub fn pos(&self) -> usize {
        assert!(self.pos <= self.dst.capacity());
        self.pos
    }

    /// Returns the capacity of the underlying buffer.
    pub fn capacity(&self) -> usize {
        self.dst.capacity()
    }

    /// Sets the new cursor position.
    ///
    /// # Panics
    ///
    /// If `pos > self.dst.capacity()`.
    ///
    /// # Safety
    ///
    /// Data up to `pos` must have actually been written to.
    pub unsafe fn set_pos(&mut self, pos: usize) {
        if pos > self.dst.capacity() {
            panic!("Given position outside of the buffer bounds.");
        }

        self.dst.filled_until(pos);

        self.pos = pos;
    }

    fn wrap<'b>(&'b mut self) -> OutBufferWrapper<'b, 'a, C> {
        OutBufferWrapper {
            buf: zstd_sys::ZSTD_outBuffer {
                dst: ptr_mut_void(self.dst),
                size: self.dst.capacity(),
                pos: self.pos,
            },
            parent: self,
        }
    }

    /// Returns the part of this buffer that was written to.
    pub fn as_slice<'b>(&'b self) -> &'a [u8]
    where
        'b: 'a,
    {
        let pos = self.pos;
        &self.dst.as_slice()[..pos]
    }

    /// Returns a pointer to the start of this buffer.
    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        self.dst.as_mut_ptr()
    }
}

impl<'a, 'b, C: WriteBuf + ?Sized> OutBufferWrapper<'a, 'b, C> {
    /// Passes `res` through, and forgets the position reported by zstd if it
    /// is an error, so the parent keeps the one from before the call.
    ///
    /// zstd.h makes no promise about `pos` after a failed call, and the legacy
    /// decoders report the whole output buffer as written when they fail; only
    /// a successful call vouches for the bytes up to `pos` being initialized.
    fn check(&mut self, res: SafeResult) -> SafeResult {
        if res.is_err() {
            self.buf.pos = self.parent.pos;
        }
        res
    }
}

impl<'a, 'b, C: WriteBuf + ?Sized> Drop for OutBufferWrapper<'a, 'b, C> {
    fn drop(&mut self) {
        // Safe because `self.buf.pos` either comes from a successful call,
        // which guarantees that data until there has been written, or was put
        // back to the parent's position by `check()`.
        unsafe { self.parent.set_pos(self.buf.pos) };
    }
}

struct InBufferWrapper<'a, 'b> {
    buf: zstd_sys::ZSTD_inBuffer,
    parent: &'a mut InBuffer<'b>,
}

impl<'a, 'b: 'a> Deref for InBufferWrapper<'a, 'b> {
    type Target = zstd_sys::ZSTD_inBuffer;

    fn deref(&self) -> &Self::Target {
        &self.buf
    }
}

impl<'a, 'b: 'a> DerefMut for InBufferWrapper<'a, 'b> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.buf
    }
}

impl<'a> InBuffer<'a> {
    /// Returns a new `InBuffer` around the given slice.
    ///
    /// Starts with `pos = 0`.
    pub fn around(src: &'a [u8]) -> Self {
        InBuffer { src, pos: 0 }
    }

    /// Returns the current cursor position.
    pub fn pos(&self) -> usize {
        self.pos
    }

    /// Sets the new cursor position.
    ///
    /// # Panics
    ///
    /// If `pos > self.src.len()`.
    pub fn set_pos(&mut self, pos: usize) {
        if pos > self.src.len() {
            panic!("Given position outside of the buffer bounds.");
        }
        self.pos = pos;
    }

    fn wrap<'b>(&'b mut self) -> InBufferWrapper<'b, 'a> {
        // The fields are public, so `pos` may not have gone through `set_pos`.
        // Not all zstd functions check it before reading from `src + pos`.
        if self.pos > self.src.len() {
            panic!("Given position outside of the buffer bounds.");
        }

        InBufferWrapper {
            buf: zstd_sys::ZSTD_inBuffer {
                src: ptr_void(self.src),
                size: self.src.len(),
                pos: self.pos,
            },
            parent: self,
        }
    }
}

impl<'a, 'b> InBufferWrapper<'a, 'b> {
    /// Passes `res` through, and forgets the position reported by zstd if it
    /// is an error, so the parent keeps the one from before the call.
    ///
    /// See `OutBufferWrapper::check()`: a failed call consumed nothing usable,
    /// and the legacy decoders report the whole input as read.
    fn check(&mut self, res: SafeResult) -> SafeResult {
        if res.is_err() {
            self.buf.pos = self.parent.pos;
        }
        res
    }
}

impl<'a, 'b> Drop for InBufferWrapper<'a, 'b> {
    fn drop(&mut self) {
        self.parent.set_pos(self.buf.pos);
    }
}

/// A Decompression stream.
///
/// Same as `DCtx`.
pub type DStream<'a> = DCtx<'a>;

// Some functions work on a "frame prefix".
// TODO: Define `struct FramePrefix(&[u8]);` and move these functions to it?
//
// Some other functions work on a dictionary (not CDict or DDict).
// Same thing?

/// Wraps the `ZSTD_findFrameCompressedSize()` function.
///
/// `src` should contain at least an entire frame.
pub fn find_frame_compressed_size(src: &[u8]) -> SafeResult {
    let code = unsafe {
        zstd_sys::ZSTD_findFrameCompressedSize(ptr_void(src), src.len())
    };
    parse_code(code)
}

/// Wraps the `ZSTD_getFrameContentSize()` function.
///
/// Args:
/// * `src`: A prefix of the compressed frame. It should at least include the frame header.
///
/// Returns:
/// * `Err(ContentSizeError)` if `src` is too small of a prefix, or if it appears corrupted.
/// * `Ok(None)` if the frame does not include a content size.
/// * `Ok(Some(content_size_in_bytes))` otherwise.
pub fn get_frame_content_size(
    src: &[u8],
) -> Result<Option<u64>, ContentSizeError> {
    parse_content_size(unsafe {
        zstd_sys::ZSTD_getFrameContentSize(ptr_void(src), src.len())
    })
}

/// Wraps the `ZSTD_findDecompressedSize()` function.
///
/// `src` should be exactly a sequence of ZSTD frames.
#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
pub fn find_decompressed_size(
    src: &[u8],
) -> Result<Option<u64>, ContentSizeError> {
    parse_content_size(unsafe {
        zstd_sys::ZSTD_findDecompressedSize(ptr_void(src), src.len())
    })
}

/// Wraps the `ZSTD_isFrame()` function.
#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
pub fn is_frame(buffer: &[u8]) -> bool {
    unsafe { zstd_sys::ZSTD_isFrame(ptr_void(buffer), buffer.len()) > 0 }
}

/// Wraps the `ZSTD_getDictID_fromDict()` function.
///
/// Returns `None` if the dictionary is not a valid zstd dictionary.
pub fn get_dict_id_from_dict(dict: &[u8]) -> Option<NonZeroU32> {
    NonZeroU32::new(unsafe {
        zstd_sys::ZSTD_getDictID_fromDict(ptr_void(dict), dict.len()) as u32
    })
}

/// Wraps the `ZSTD_getDictID_fromFrame()` function.
///
/// Returns `None` if the dictionary ID could not be decoded. This may happen if:
/// * The frame was not encoded with a dictionary.
/// * The frame intentionally did not include dictionary ID.
/// * The dictionary was non-conformant.
/// * `src` is too small and does not include the frame header.
/// * `src` is not a valid zstd frame prefix.
pub fn get_dict_id_from_frame(src: &[u8]) -> Option<NonZeroU32> {
    NonZeroU32::new(unsafe {
        zstd_sys::ZSTD_getDictID_fromFrame(ptr_void(src), src.len()) as u32
    })
}

/// What kind of context reset should be applied.
pub enum ResetDirective {
    /// Only the session will be reset.
    ///
    /// All parameters will be preserved (including the dictionary).
    /// But any frame being processed will be dropped.
    ///
    /// It can be useful to start re-using a context after an error or when an
    /// ongoing compression is no longer needed.
    SessionOnly,

    /// Only reset parameters (including dictionary or referenced prefix).
    ///
    /// All parameters will be reset to default values.
    ///
    /// This can only be done between sessions - no compression or decompression must be ongoing.
    Parameters,

    /// Reset both the session and parameters.
    ///
    /// The result is similar to a newly created context.
    SessionAndParameters,
}

impl ResetDirective {
    /// Does this drop a session in progress?
    ///
    /// Only a session reset brings a context back from the undefined state an
    /// error can leave it in - `Parameters` alone is refused while a session is
    /// open.
    fn resets_session(&self) -> bool {
        matches!(
            self,
            ResetDirective::SessionOnly | ResetDirective::SessionAndParameters
        )
    }

    fn as_sys(self) -> zstd_sys::ZSTD_ResetDirective {
        match self {
            ResetDirective::SessionOnly => zstd_sys::ZSTD_ResetDirective::ZSTD_reset_session_only,
            ResetDirective::Parameters => zstd_sys::ZSTD_ResetDirective::ZSTD_reset_parameters,
            ResetDirective::SessionAndParameters => zstd_sys::ZSTD_ResetDirective::ZSTD_reset_session_and_parameters,
        }
    }
}

#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum FrameFormat {
    /// Regular zstd format.
    One = zstd_sys::ZSTD_format_e::ZSTD_f_zstd1 as u32,

    /// Skip the 4 bytes identifying the content as zstd-compressed data.
    Magicless = zstd_sys::ZSTD_format_e::ZSTD_f_zstd1_magicless as u32,
}

#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum DictAttachPref {
    DefaultAttach =
        zstd_sys::ZSTD_dictAttachPref_e::ZSTD_dictDefaultAttach as u32,
    ForceAttach = zstd_sys::ZSTD_dictAttachPref_e::ZSTD_dictForceAttach as u32,
    ForceCopy = zstd_sys::ZSTD_dictAttachPref_e::ZSTD_dictForceCopy as u32,
    ForceLoad = zstd_sys::ZSTD_dictAttachPref_e::ZSTD_dictForceLoad as u32,
}

#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum ParamSwitch {
    Auto = zstd_sys::ZSTD_ParamSwitch_e::ZSTD_ps_auto as u32,
    Enable = zstd_sys::ZSTD_ParamSwitch_e::ZSTD_ps_enable as u32,
    Disable = zstd_sys::ZSTD_ParamSwitch_e::ZSTD_ps_disable as u32,
}

/// A compression parameter.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CParameter {
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    RSyncable(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    Format(FrameFormat),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    ForceMaxWindow(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    ForceAttachDict(DictAttachPref),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    LiteralCompressionMode(ParamSwitch),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    SrcSizeHint(u32),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    EnableDedicatedDictSearch(bool),

    /// Whether zstd may keep referencing the input buffer instead of copying
    /// it.
    ///
    /// This requires the input to stay allocated and unmodified until the
    /// frame is complete, which a borrowed `InBuffer` cannot guarantee:
    /// `CCtx::set_parameter` refuses to enable it and returns an error.
    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    StableInBuffer(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    StableOutBuffer(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    BlockDelimiters(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    ValidateSequences(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    UseBlockSplitter(ParamSwitch),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    UseRowMatchFinder(ParamSwitch),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    DeterministicRefPrefix(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    PrefetchCDictTables(ParamSwitch),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    EnableSeqProducerFallback(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    MaxBlockSize(u32),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    SearchForExternalRepcodes(ParamSwitch),

    /// Target CBlock size.
    ///
    /// Tries to make compressed blocks fit in this size (not a guarantee, just a target).
    /// Useful to reduce end-to-end latency in low-bandwidth environments.
    ///
    /// No target when the value is 0.
    TargetCBlockSize(u32),

    /// Compression level to use.
    ///
    /// Compression levels are global presets for the other compression parameters.
    CompressionLevel(CompressionLevel),

    /// Maximum allowed back-reference distance.
    ///
    /// The actual distance is 2 power "this value".
    WindowLog(u32),

    HashLog(u32),

    ChainLog(u32),

    SearchLog(u32),

    MinMatch(u32),

    TargetLength(u32),

    Strategy(Strategy),

    EnableLongDistanceMatching(bool),

    LdmHashLog(u32),

    LdmMinMatch(u32),

    LdmBucketSizeLog(u32),

    LdmHashRateLog(u32),

    ContentSizeFlag(bool),

    ChecksumFlag(bool),

    DictIdFlag(bool),

    /// How many threads will be spawned.
    ///
    /// With a default value of `0`, `compress_stream*` functions block until they complete.
    ///
    /// With any other value (including 1, a single compressing thread), these methods directly
    /// return, and the actual compression is done in the background (until a flush is requested).
    /// A context compressing that way from a borrowed dictionary or prefix
    /// must not be leaked, see [`CCtx`](CCtx#worker-threads-and-leaks).
    ///
    /// Note: this will only work if the `zstdmt` feature is activated.
    NbWorkers(u32),

    /// Size in bytes of a compression job.
    ///
    /// Does not have any effect when `NbWorkers` is set to 0.
    ///
    /// The default value of 0 finds the best job size based on the compression parameters.
    ///
    /// Note: this will only work if the `zstdmt` feature is activated.
    JobSize(u32),

    /// Specifies how much overlap must be given to each worker.
    ///
    /// Possible values:
    ///
    /// * `0` (default value): automatic overlap based on compression strategy.
    /// * `1`: No overlap
    /// * `1 < n < 9`: Overlap a fraction of the window size, defined as `1/(2 ^ 9-n)`.
    /// * `9`: Full overlap (as long as the window)
    /// * `9 < m`: Will return an error.
    ///
    /// Note: this will only work if the `zstdmt` feature is activated.
    OverlapSizeLog(u32),
}

/// A decompression parameter.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DParameter {
    WindowLogMax(u32),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    /// See `FrameFormat`.
    Format(FrameFormat),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    StableOutBuffer(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    ForceIgnoreChecksum(bool),

    #[cfg(feature = "experimental")]
    #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
    RefMultipleDDicts(bool),
}

/// Wraps the `ZDICT_trainFromBuffer()` function.
#[cfg(feature = "zdict_builder")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "zdict_builder")))]
pub fn train_from_buffer<C: WriteBuf + ?Sized>(
    dict_buffer: &mut C,
    samples_buffer: &[u8],
    samples_sizes: &[usize],
) -> SafeResult {
    // A plain `sum()` wraps around when overflow checks are disabled, and
    // would accept sizes reaching far outside of `samples_buffer`.
    let total_size = samples_sizes
        .iter()
        .try_fold(0usize, |total, &size| total.checked_add(size));
    assert_eq!(
        Some(samples_buffer.len()),
        total_size,
        "sample sizes don't add up to the samples buffer length"
    );

    unsafe {
        dict_buffer.write_from(|buffer, capacity| {
            parse_code(zstd_sys::ZDICT_trainFromBuffer(
                buffer,
                capacity,
                ptr_void(samples_buffer),
                samples_sizes.as_ptr(),
                samples_sizes.len() as u32,
            ))
        })
    }
}

/// Wraps the `ZDICT_getDictID()` function.
#[cfg(feature = "zdict_builder")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "zdict_builder")))]
pub fn get_dict_id(dict_buffer: &[u8]) -> Option<NonZeroU32> {
    NonZeroU32::new(unsafe {
        zstd_sys::ZDICT_getDictID(ptr_void(dict_buffer), dict_buffer.len())
    })
}

/// Wraps the `ZSTD_getBlockSize()` function.
#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
pub fn get_block_size(cctx: &CCtx) -> usize {
    unsafe { zstd_sys::ZSTD_getBlockSize(cctx.0.as_ptr()) }
}

/// Wraps the `ZSTD_decompressBound` function
#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
pub fn decompress_bound(data: &[u8]) -> Result<u64, ErrorCode> {
    let bound =
        unsafe { zstd_sys::ZSTD_decompressBound(ptr_void(data), data.len()) };
    if is_error(bound as usize) {
        Err(bound as usize)
    } else {
        Ok(bound)
    }
}

/// Given a buffer of size `src_size`, returns the maximum number of sequences that can ge
/// generated.
#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
pub fn sequence_bound(src_size: usize) -> usize {
    // Safety: Just FFI.
    unsafe { zstd_sys::ZSTD_sequenceBound(src_size) }
}

/// Returns the minimum extra space when output and input buffer overlap.
///
/// When using in-place decompression, the output buffer must be at least this much bigger (in
/// bytes) than the input buffer. The extra space must be at the front of the output buffer (the
/// input buffer must be at the end of the output buffer).
#[cfg(feature = "experimental")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
pub fn decompression_margin(
    compressed_data: &[u8],
) -> Result<usize, ErrorCode> {
    parse_code(unsafe {
        zstd_sys::ZSTD_decompressionMargin(
            ptr_void(compressed_data),
            compressed_data.len(),
        )
    })
}