zlib_rs/
inflate.rs

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
#![allow(non_snake_case)] // TODO ultimately remove this
#![allow(clippy::missing_safety_doc)] // obviously needs to be fixed long-term

use core::ffi::{c_char, c_int, c_long, c_ulong};
use core::marker::PhantomData;
use core::mem::MaybeUninit;

mod bitreader;
mod inffixed_tbl;
mod inftrees;
mod window;
mod writer;

use crate::allocate::Allocator;
use crate::c_api::internal_state;
use crate::{
    adler32::adler32,
    c_api::{gz_header, z_checksum, z_size, z_stream, Z_DEFLATED},
    inflate::writer::Writer,
    Code, InflateFlush, ReturnCode, DEF_WBITS, MAX_WBITS, MIN_WBITS,
};

use crate::crc32::{crc32, Crc32Fold};

use self::{
    bitreader::BitReader,
    inftrees::{inflate_table, CodeType, InflateTable},
    window::Window,
};

const INFLATE_STRICT: bool = false;

// SAFETY: This struct must have the same layout as [`z_stream`], so that casts and transmutations
// between the two can work without UB.
#[repr(C)]
pub struct InflateStream<'a> {
    pub(crate) next_in: *mut crate::c_api::Bytef,
    pub(crate) avail_in: crate::c_api::uInt,
    pub(crate) total_in: crate::c_api::z_size,
    pub(crate) next_out: *mut crate::c_api::Bytef,
    pub(crate) avail_out: crate::c_api::uInt,
    pub(crate) total_out: crate::c_api::z_size,
    pub(crate) msg: *mut c_char,
    pub(crate) state: &'a mut State<'a>,
    pub(crate) alloc: Allocator<'a>,
    pub(crate) data_type: c_int,
    pub(crate) adler: crate::c_api::z_checksum,
    pub(crate) reserved: crate::c_api::uLong,
}

#[cfg(feature = "__internal-test")]
#[doc(hidden)]
pub const INFLATE_STATE_SIZE: usize = core::mem::size_of::<crate::inflate::State>();

#[cfg(feature = "__internal-test")]
#[doc(hidden)]
pub unsafe fn set_mode_dict(strm: &mut z_stream) {
    unsafe {
        (*(strm.state as *mut State)).mode = Mode::Dict;
    }
}

impl<'a> InflateStream<'a> {
    // z_stream and DeflateStream must have the same layout. Do our best to check if this is true.
    // (imperfect check, but should catch most mistakes.)
    const _S: () = assert!(core::mem::size_of::<z_stream>() == core::mem::size_of::<Self>());
    const _A: () = assert!(core::mem::align_of::<z_stream>() == core::mem::align_of::<Self>());

    /// # Safety
    ///
    /// Behavior is undefined if any of the following conditions are violated:
    ///
    /// - `strm` satisfies the conditions of [`pointer::as_ref`]
    /// - if not `NULL`, `strm` as initialized using [`init`] or similar
    ///
    /// [`pointer::as_ref`]: https://doc.rust-lang.org/core/primitive.pointer.html#method.as_ref
    #[inline(always)]
    pub unsafe fn from_stream_ref(strm: *const z_stream) -> Option<&'a Self> {
        {
            // Safety: ptr points to a valid value of type z_stream (if non-null)
            let stream = unsafe { strm.as_ref() }?;

            if stream.zalloc.is_none() || stream.zfree.is_none() {
                return None;
            }

            if stream.state.is_null() {
                return None;
            }
        }

        // Safety: InflateStream has an equivalent layout as z_stream
        strm.cast::<InflateStream>().as_ref()
    }

    /// # Safety
    ///
    /// Behavior is undefined if any of the following conditions are violated:
    ///
    /// - `strm` satisfies the conditions of [`pointer::as_mut`]
    /// - if not `NULL`, `strm` as initialized using [`init`] or similar
    ///
    /// [`pointer::as_mut`]: https://doc.rust-lang.org/core/primitive.pointer.html#method.as_mut
    #[inline(always)]
    pub unsafe fn from_stream_mut(strm: *mut z_stream) -> Option<&'a mut Self> {
        {
            // Safety: ptr points to a valid value of type z_stream (if non-null)
            let stream = unsafe { strm.as_ref() }?;

            if stream.zalloc.is_none() || stream.zfree.is_none() {
                return None;
            }

            if stream.state.is_null() {
                return None;
            }
        }

        // Safety: InflateStream has an equivalent layout as z_stream
        strm.cast::<InflateStream>().as_mut()
    }

    fn as_z_stream_mut(&mut self) -> &mut z_stream {
        // safety: a valid &mut InflateStream is also a valid &mut z_stream
        unsafe { &mut *(self as *mut _ as *mut z_stream) }
    }
}

const MAX_BITS: u8 = 15; // maximum number of bits in a code
const MAX_DIST_EXTRA_BITS: u8 = 13; // maximum number of extra distance bits
                                    //
pub fn uncompress_slice<'a>(
    output: &'a mut [u8],
    input: &[u8],
    config: InflateConfig,
) -> (&'a mut [u8], ReturnCode) {
    // SAFETY: [u8] is also a valid [MaybeUninit<u8>]
    let output_uninit = unsafe {
        core::slice::from_raw_parts_mut(output.as_mut_ptr() as *mut MaybeUninit<u8>, output.len())
    };

    uncompress(output_uninit, input, config)
}

/// Inflates `source` into `dest`, and writes the final inflated size into `dest_len`.
pub fn uncompress<'a>(
    output: &'a mut [MaybeUninit<u8>],
    input: &[u8],
    config: InflateConfig,
) -> (&'a mut [u8], ReturnCode) {
    let mut dest_len_ptr = output.len() as z_checksum;

    // for detection of incomplete stream when *destLen == 0
    let mut buf = [0u8];

    let mut left;
    let mut len = input.len() as u64;

    let dest = if output.is_empty() {
        left = 1;

        buf.as_mut_ptr()
    } else {
        left = output.len() as u64;
        dest_len_ptr = 0;

        output.as_mut_ptr() as *mut u8
    };

    let mut stream = z_stream {
        next_in: input.as_ptr() as *mut u8,
        avail_in: 0,

        zalloc: None,
        zfree: None,
        opaque: core::ptr::null_mut(),

        ..z_stream::default()
    };

    let err = init(&mut stream, config);
    if err != ReturnCode::Ok {
        return (&mut [], err);
    }

    stream.next_out = dest;
    stream.avail_out = 0;

    let Some(stream) = (unsafe { InflateStream::from_stream_mut(&mut stream) }) else {
        return (&mut [], ReturnCode::StreamError);
    };

    let err = loop {
        if stream.avail_out == 0 {
            stream.avail_out = Ord::min(left, u32::MAX as u64) as u32;
            left -= stream.avail_out as u64;
        }

        if stream.avail_in == 0 {
            stream.avail_in = Ord::min(len, u32::MAX as u64) as u32;
            len -= stream.avail_in as u64;
        }

        let err = unsafe { inflate(stream, InflateFlush::NoFlush) };

        if err != ReturnCode::Ok {
            break err;
        }
    };

    if !output.is_empty() {
        dest_len_ptr = stream.total_out;
    } else if stream.total_out != 0 && err == ReturnCode::BufError {
        left = 1;
    }

    let avail_out = stream.avail_out;

    end(stream);

    let ret = match err {
        ReturnCode::StreamEnd => ReturnCode::Ok,
        ReturnCode::NeedDict => ReturnCode::DataError,
        ReturnCode::BufError if (left + avail_out as u64) != 0 => ReturnCode::DataError,
        _ => err,
    };

    // SAFETY: we have now initialized these bytes
    let output_slice = unsafe {
        core::slice::from_raw_parts_mut(output.as_mut_ptr() as *mut u8, dest_len_ptr as usize)
    };

    (output_slice, ret)
}

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum Mode {
    Head,
    Flags,
    Time,
    Os,
    ExLen,
    Extra,
    Name,
    Comment,
    HCrc,
    Sync,
    Mem,
    Length,
    Type,
    TypeDo,
    Stored,
    CopyBlock,
    Check,
    Len_,
    Len,
    Lit,
    LenExt,
    Dist,
    DistExt,
    Match,
    Table,
    LenLens,
    CodeLens,
    DictId,
    Dict,
    Done,
    Bad,
}

#[derive(Default, Clone, Copy)]
#[allow(clippy::enum_variant_names)]
enum Codes {
    #[default]
    Fixed,
    Codes,
    Len,
    Dist,
}

#[derive(Default, Clone, Copy)]
struct Table {
    codes: Codes,
    bits: usize,
}

#[derive(Clone, Copy)]
struct Flags(u8);

impl Default for Flags {
    fn default() -> Self {
        Self::SANE
    }
}

impl Flags {
    /// set if currently processing the last block
    const IS_LAST_BLOCK: Self = Self(0b0000_0001);

    /// set if a custom dictionary was provided
    const HAVE_DICT: Self = Self(0b0000_0010);

    /// if false, allow invalid distance too far
    const SANE: Self = Self(0b0000_0100);

    pub(crate) const fn contains(self, other: Self) -> bool {
        debug_assert!(other.0.count_ones() == 1);

        self.0 & other.0 != 0
    }

    #[inline(always)]
    pub(crate) fn update(&mut self, other: Self, value: bool) {
        if value {
            *self = Self(self.0 | other.0);
        } else {
            *self = Self(self.0 & !other.0);
        }
    }
}

#[repr(C, align(64))]
pub(crate) struct State<'a> {
    /// Current inflate mode
    mode: Mode,

    flags: Flags,

    /// log base 2 of requested window size
    wbits: u8,

    /// bitflag
    ///
    /// - bit 0 true if zlib
    /// - bit 1 true if gzip
    /// - bit 2 true to validate check value
    wrap: u8,

    flush: InflateFlush,

    // allocated window if needed (capacity == 0 if unused)
    window: Window<'a>,

    //
    /// number of code length code lengths
    ncode: usize,
    /// number of length code lengths
    nlen: usize,
    /// number of distance code lengths
    ndist: usize,
    /// number of code lengths in lens[]
    have: usize,
    /// next available space in codes[]
    next: usize, // represented as an index, don't want a self-referential structure here

    // IO
    bit_reader: BitReader<'a>,

    writer: Writer<'a>,
    total: usize,

    /// length of a block to copy
    length: usize,
    /// distance back to copy the string from
    offset: usize,

    /// extra bits needed
    extra: usize,

    /// bits back of last unprocessed length/lit
    back: usize,

    /// initial length of match
    was: usize,

    /// size of memory copying chunk
    chunksize: usize,

    in_available: usize,
    out_available: usize,

    gzip_flags: i32,

    checksum: u32,
    crc_fold: Crc32Fold,

    error_message: Option<&'static str>,

    /// place to store gzip header if needed
    head: Option<&'a mut gz_header>,
    dmax: usize,

    /// table for length/literal codes
    len_table: Table,

    /// table for dist codes
    dist_table: Table,

    codes_codes: [Code; crate::ENOUGH_LENS],
    len_codes: [Code; crate::ENOUGH_LENS],
    dist_codes: [Code; crate::ENOUGH_DISTS],

    /// temporary storage space for code lengths
    lens: [u16; 320],
    /// work area for code table building
    work: [u16; 288],
}

impl<'a> State<'a> {
    fn new(reader: &'a [u8], writer: Writer<'a>) -> Self {
        let in_available = reader.len();
        let out_available = writer.capacity();

        Self {
            flush: InflateFlush::NoFlush,

            flags: Flags::default(),
            wrap: 0,
            mode: Mode::Head,
            length: 0,

            len_table: Table::default(),
            dist_table: Table::default(),

            wbits: 0,
            offset: 0,
            extra: 0,
            back: 0,
            was: 0,
            chunksize: 0,
            in_available,
            out_available,

            bit_reader: BitReader::new(reader),

            writer,
            total: 0,

            window: Window::empty(),
            head: None,

            lens: [0u16; 320],
            work: [0u16; 288],

            ncode: 0,
            nlen: 0,
            ndist: 0,
            have: 0,
            next: 0,

            error_message: None,

            checksum: 0,
            crc_fold: Crc32Fold::new(),

            dmax: 0,
            gzip_flags: 0,

            codes_codes: [Code::default(); crate::ENOUGH_LENS],
            len_codes: [Code::default(); crate::ENOUGH_LENS],
            dist_codes: [Code::default(); crate::ENOUGH_DISTS],
        }
    }

    fn len_table_ref(&self) -> &[Code] {
        match self.len_table.codes {
            Codes::Fixed => &self::inffixed_tbl::LENFIX,
            Codes::Codes => &self.codes_codes,
            Codes::Len => &self.len_codes,
            Codes::Dist => &self.dist_codes,
        }
    }

    fn dist_table_ref(&self) -> &[Code] {
        match self.dist_table.codes {
            Codes::Fixed => &self::inffixed_tbl::DISTFIX,
            Codes::Codes => &self.codes_codes,
            Codes::Len => &self.len_codes,
            Codes::Dist => &self.dist_codes,
        }
    }

    fn len_table_get(&self, index: usize) -> Code {
        self.len_table_ref()[index]
    }

    fn dist_table_get(&self, index: usize) -> Code {
        self.dist_table_ref()[index]
    }
}

// swaps endianness
const fn zswap32(q: u32) -> u32 {
    u32::from_be(q.to_le())
}

const INFLATE_FAST_MIN_HAVE: usize = 15;
const INFLATE_FAST_MIN_LEFT: usize = 260;

impl<'a> State<'a> {
    // NOTE: DO NOT RETURN FROM THIS FUNCTION!
    //
    // this function loads fields from `self` to the stack, and it is crucial that these fields
    // are written back into `self` before exiting.
    //
    // Likewise, be careful with `self.foo()` function calls: the reader and writer are invalidated
    // for the duration of this function!
    fn dispatch(&mut self) -> ReturnCode {
        let mut writer;
        let mut bit_reader;

        macro_rules! load {
            ($state:expr) => {
                bit_reader = core::mem::replace(&mut $state.bit_reader, BitReader::new(&[]));
                writer = core::mem::replace(&mut $state.writer, Writer::new(&mut []));
            };
        }

        macro_rules! restore {
            ($state:expr) => {
                $state.bit_reader = bit_reader;
                $state.writer = writer;
            };
        }

        // load variables to the stack
        load!(self);

        let return_code = 'label: loop {
            macro_rules! pull_byte {
                ($self:expr) => {
                    match bit_reader.pull_byte() {
                        Err(return_code) => break 'label return_code,
                        Ok(_) => (),
                    }
                };
            }

            macro_rules! need_bits {
                ($self:expr, $n:expr) => {
                    match bit_reader.need_bits($n) {
                        Err(return_code) => break 'label return_code,
                        Ok(v) => v,
                    }
                };
            }

            match self.mode {
                Mode::Head => {
                    if self.wrap == 0 {
                        self.mode = Mode::TypeDo;

                        continue 'label;
                    }

                    need_bits!(self, 16);

                    // Gzip
                    if (self.wrap & 2) != 0 && bit_reader.hold() == 0x8b1f {
                        if self.wbits == 0 {
                            self.wbits = 15;
                        }

                        let b0 = bit_reader.bits(8) as u8;
                        let b1 = (bit_reader.hold() >> 8) as u8;
                        self.checksum = crc32(crate::CRC32_INITIAL_VALUE, &[b0, b1]);
                        bit_reader.init_bits();

                        self.mode = Mode::Flags;

                        continue 'label;
                    }

                    if let Some(header) = &mut self.head {
                        header.done = -1;
                    }

                    // check if zlib header is allowed
                    if (self.wrap & 1) == 0
                        || ((bit_reader.bits(8) << 8) + (bit_reader.hold() >> 8)) % 31 != 0
                    {
                        self.mode = Mode::Bad;
                        break 'label self.bad("incorrect header check\0");
                    }

                    if bit_reader.bits(4) != Z_DEFLATED as u64 {
                        self.mode = Mode::Bad;
                        break 'label self.bad("unknown compression method\0");
                    }

                    bit_reader.drop_bits(4);
                    let len = bit_reader.bits(4) as u8 + 8;

                    if self.wbits == 0 {
                        self.wbits = len;
                    }

                    if len as i32 > MAX_WBITS || len > self.wbits {
                        self.mode = Mode::Bad;
                        break 'label self.bad("invalid window size\0");
                    }

                    self.dmax = 1 << len;
                    self.gzip_flags = 0; // indicate zlib header
                    self.checksum = crate::ADLER32_INITIAL_VALUE as _;

                    if bit_reader.hold() & 0x200 != 0 {
                        bit_reader.init_bits();

                        self.mode = Mode::DictId;

                        continue 'label;
                    } else {
                        bit_reader.init_bits();

                        self.mode = Mode::Type;

                        continue 'label;
                    }
                }
                Mode::Flags => {
                    need_bits!(self, 16);
                    self.gzip_flags = bit_reader.hold() as i32;

                    // Z_DEFLATED = 8 is the only supported method
                    if self.gzip_flags & 0xff != Z_DEFLATED {
                        self.mode = Mode::Bad;
                        break 'label self.bad("unknown compression method\0");
                    }

                    if self.gzip_flags & 0xe000 != 0 {
                        self.mode = Mode::Bad;
                        break 'label self.bad("unknown header flags set\0");
                    }

                    if let Some(head) = self.head.as_mut() {
                        head.text = ((bit_reader.hold() >> 8) & 1) as i32;
                    }

                    if (self.gzip_flags & 0x0200) != 0 && (self.wrap & 4) != 0 {
                        let b0 = bit_reader.bits(8) as u8;
                        let b1 = (bit_reader.hold() >> 8) as u8;
                        self.checksum = crc32(self.checksum, &[b0, b1]);
                    }

                    bit_reader.init_bits();
                    self.mode = Mode::Time;

                    continue 'label;
                }
                Mode::Time => {
                    need_bits!(self, 32);
                    if let Some(head) = self.head.as_mut() {
                        head.time = bit_reader.hold() as z_size;
                    }

                    if (self.gzip_flags & 0x0200) != 0 && (self.wrap & 4) != 0 {
                        let bytes = (bit_reader.hold() as u32).to_le_bytes();
                        self.checksum = crc32(self.checksum, &bytes);
                    }

                    bit_reader.init_bits();
                    self.mode = Mode::Os;

                    continue 'label;
                }
                Mode::Os => {
                    need_bits!(self, 16);
                    if let Some(head) = self.head.as_mut() {
                        head.xflags = (bit_reader.hold() & 0xff) as i32;
                        head.os = (bit_reader.hold() >> 8) as i32;
                    }

                    if (self.gzip_flags & 0x0200) != 0 && (self.wrap & 4) != 0 {
                        let bytes = (bit_reader.hold() as u16).to_le_bytes();
                        self.checksum = crc32(self.checksum, &bytes);
                    }

                    bit_reader.init_bits();
                    self.mode = Mode::ExLen;

                    continue 'label;
                }
                Mode::ExLen => {
                    if (self.gzip_flags & 0x0400) != 0 {
                        need_bits!(self, 16);

                        // self.length (and head.extra_len) represent the length of the extra field
                        self.length = bit_reader.hold() as usize;
                        if let Some(head) = self.head.as_mut() {
                            head.extra_len = self.length as u32;
                        }

                        if (self.gzip_flags & 0x0200) != 0 && (self.wrap & 4) != 0 {
                            let bytes = (bit_reader.hold() as u16).to_le_bytes();
                            self.checksum = crc32(self.checksum, &bytes);
                        }
                        bit_reader.init_bits();
                    } else if let Some(head) = self.head.as_mut() {
                        head.extra = core::ptr::null_mut();
                    }

                    self.mode = Mode::Extra;

                    continue 'label;
                }
                Mode::Extra => {
                    if (self.gzip_flags & 0x0400) != 0 {
                        // self.length is the number of remaining `extra` bytes. But they may not all be available
                        let extra_available = Ord::min(self.length, bit_reader.bytes_remaining());

                        if extra_available > 0 {
                            if let Some(head) = self.head.as_mut() {
                                if !head.extra.is_null() {
                                    // at `head.extra`, the caller has reserved `head.extra_max` bytes.
                                    // in the deflated byte stream, we've found a gzip header with
                                    // `head.extra_len` bytes of data. We must be careful because
                                    // `head.extra_len` may be larger than `head.extra_max`.

                                    // how many bytes we've already written into `head.extra`
                                    let written_so_far = head.extra_len as usize - self.length;

                                    // min of number of bytes available at dst and at src
                                    let count = Ord::min(
                                        (head.extra_max as usize).saturating_sub(written_so_far),
                                        extra_available,
                                    );

                                    // SAFETY: location where we'll write: this saturates at the
                                    // `head.extra.add(head.extra.max)` to prevent UB
                                    let next_write_offset =
                                        Ord::min(written_so_far, head.extra_max as usize);

                                    unsafe {
                                        // SAFETY: count is effectively bounded by head.extra_max
                                        // and bit_reader.bytes_remaining(), so the count won't
                                        // go out of bounds.
                                        core::ptr::copy_nonoverlapping(
                                            bit_reader.as_mut_ptr(),
                                            head.extra.add(next_write_offset),
                                            count,
                                        );
                                    }
                                }
                            }

                            // Checksum
                            if (self.gzip_flags & 0x0200) != 0 && (self.wrap & 4) != 0 {
                                let extra_slice = &bit_reader.as_slice()[..extra_available];
                                self.checksum = crc32(self.checksum, extra_slice)
                            }

                            self.in_available -= extra_available;
                            bit_reader.advance(extra_available);
                            self.length -= extra_available;
                        }

                        // Checks for errors occur after returning
                        if self.length != 0 {
                            break 'label self.inflate_leave(ReturnCode::Ok);
                        }
                    }

                    self.length = 0;
                    self.mode = Mode::Name;

                    continue 'label;
                }
                Mode::Name => {
                    if (self.gzip_flags & 0x0800) != 0 {
                        if self.in_available == 0 {
                            break 'label self.inflate_leave(ReturnCode::Ok);
                        }

                        // the name string will always be null-terminated, but might be longer than we have
                        // space for in the header struct. Nonetheless, we read the whole thing.
                        let slice = bit_reader.as_slice();
                        let null_terminator_index = slice.iter().position(|c| *c == 0);

                        // we include the null terminator if it exists
                        let name_slice = match null_terminator_index {
                            Some(i) => &slice[..=i],
                            None => slice,
                        };

                        // if the header has space, store as much as possible in there
                        if let Some(head) = self.head.as_mut() {
                            if !head.name.is_null() {
                                let remaining_name_bytes = (head.name_max as usize)
                                    .checked_sub(self.length)
                                    .expect("name out of bounds");
                                let copy = Ord::min(name_slice.len(), remaining_name_bytes);

                                unsafe {
                                    // SAFETY: copy is effectively bound by the name length and
                                    // head.name_max, so this won't go out of bounds.
                                    core::ptr::copy_nonoverlapping(
                                        name_slice.as_ptr(),
                                        head.name.add(self.length),
                                        copy,
                                    )
                                };

                                self.length += copy;
                            }
                        }

                        if (self.gzip_flags & 0x0200) != 0 && (self.wrap & 4) != 0 {
                            self.checksum = crc32(self.checksum, name_slice);
                        }

                        let reached_end = name_slice.last() == Some(&0);
                        bit_reader.advance(name_slice.len());

                        if !reached_end && bit_reader.bytes_remaining() == 0 {
                            break 'label self.inflate_leave(ReturnCode::Ok);
                        }
                    } else if let Some(head) = self.head.as_mut() {
                        head.name = core::ptr::null_mut();
                    }

                    self.length = 0;
                    self.mode = Mode::Comment;

                    continue 'label;
                }
                Mode::Comment => {
                    if (self.gzip_flags & 0x01000) != 0 {
                        if self.in_available == 0 {
                            break 'label self.inflate_leave(ReturnCode::Ok);
                        }

                        // the comment string will always be null-terminated, but might be longer than we have
                        // space for in the header struct. Nonetheless, we read the whole thing.
                        let slice = bit_reader.as_slice();
                        let null_terminator_index = slice.iter().position(|c| *c == 0);

                        // we include the null terminator if it exists
                        let comment_slice = match null_terminator_index {
                            Some(i) => &slice[..=i],
                            None => slice,
                        };

                        // if the header has space, store as much as possible in there
                        if let Some(head) = self.head.as_mut() {
                            if !head.comment.is_null() {
                                let remaining_comm_bytes = (head.comm_max as usize)
                                    .checked_sub(self.length)
                                    .expect("comm out of bounds");
                                let copy = Ord::min(comment_slice.len(), remaining_comm_bytes);

                                unsafe {
                                    // SAFETY: copy is effectively bound by the comment length and
                                    // head.comm_max, so this won't go out of bounds.
                                    core::ptr::copy_nonoverlapping(
                                        comment_slice.as_ptr(),
                                        head.comment.add(self.length),
                                        copy,
                                    )
                                };

                                self.length += copy;
                            }
                        }

                        if (self.gzip_flags & 0x0200) != 0 && (self.wrap & 4) != 0 {
                            self.checksum = crc32(self.checksum, comment_slice);
                        }

                        let reached_end = comment_slice.last() == Some(&0);
                        bit_reader.advance(comment_slice.len());

                        if !reached_end && bit_reader.bytes_remaining() == 0 {
                            break 'label self.inflate_leave(ReturnCode::Ok);
                        }
                    } else if let Some(head) = self.head.as_mut() {
                        head.comment = core::ptr::null_mut();
                    }

                    self.mode = Mode::HCrc;

                    continue 'label;
                }
                Mode::HCrc => {
                    if (self.gzip_flags & 0x0200) != 0 {
                        need_bits!(self, 16);

                        if (self.wrap & 4) != 0
                            && bit_reader.hold() as u32 != (self.checksum & 0xffff)
                        {
                            self.mode = Mode::Bad;
                            break 'label self.bad("header crc mismatch\0");
                        }

                        bit_reader.init_bits();
                    }

                    if let Some(head) = self.head.as_mut() {
                        head.hcrc = (self.gzip_flags >> 9) & 1;
                        head.done = 1;
                    }

                    // compute crc32 checksum if not in raw mode
                    if (self.wrap & 4 != 0) && self.gzip_flags != 0 {
                        self.crc_fold = Crc32Fold::new();
                        self.checksum = crate::CRC32_INITIAL_VALUE;
                    }

                    self.mode = Mode::Type;

                    continue 'label;
                }
                Mode::Type => {
                    use InflateFlush::*;

                    match self.flush {
                        Block | Trees => break 'label ReturnCode::Ok,
                        NoFlush | SyncFlush | Finish => {
                            // NOTE: this is slightly different to what zlib-rs does!
                            self.mode = Mode::TypeDo;
                            continue 'label;
                        }
                    }
                }
                Mode::TypeDo => {
                    if self.flags.contains(Flags::IS_LAST_BLOCK) {
                        bit_reader.next_byte_boundary();
                        self.mode = Mode::Check;

                        continue 'label;
                    }

                    need_bits!(self, 3);
                    // self.last = bit_reader.bits(1) != 0;
                    self.flags
                        .update(Flags::IS_LAST_BLOCK, bit_reader.bits(1) != 0);
                    bit_reader.drop_bits(1);

                    match bit_reader.bits(2) {
                        0b00 => {
                            // eprintln!("inflate:     stored block (last = {last})");

                            bit_reader.drop_bits(2);

                            self.mode = Mode::Stored;

                            continue 'label;
                        }
                        0b01 => {
                            // eprintln!("inflate:     fixed codes block (last = {last})");

                            self.len_table = Table {
                                codes: Codes::Fixed,
                                bits: 9,
                            };

                            self.dist_table = Table {
                                codes: Codes::Fixed,
                                bits: 5,
                            };

                            self.mode = Mode::Len_;

                            bit_reader.drop_bits(2);

                            if let InflateFlush::Trees = self.flush {
                                break 'label self.inflate_leave(ReturnCode::Ok);
                            } else {
                                continue 'label;
                            }
                        }
                        0b10 => {
                            // eprintln!("inflate:     dynamic codes block (last = {last})");

                            bit_reader.drop_bits(2);

                            self.mode = Mode::Table;

                            continue 'label;
                        }
                        0b11 => {
                            // eprintln!("inflate:     invalid block type");

                            bit_reader.drop_bits(2);

                            self.mode = Mode::Bad;
                            break 'label self.bad("invalid block type\0");
                        }
                        _ => {
                            // LLVM will optimize this branch away
                            unreachable!("BitReader::bits(2) only yields a value of two bits, so this match is already exhaustive")
                        }
                    }
                }
                Mode::Stored => {
                    bit_reader.next_byte_boundary();

                    need_bits!(self, 32);

                    let hold = bit_reader.bits(32) as u32;

                    // eprintln!("hold {hold:#x}");

                    if hold as u16 != !((hold >> 16) as u16) {
                        self.mode = Mode::Bad;
                        break 'label self.bad("invalid stored block lengths\0");
                    }

                    self.length = hold as usize & 0xFFFF;
                    // eprintln!("inflate:     stored length {}", state.length);

                    bit_reader.init_bits();

                    if let InflateFlush::Trees = self.flush {
                        break 'label self.inflate_leave(ReturnCode::Ok);
                    } else {
                        self.mode = Mode::CopyBlock;

                        continue 'label;
                    }
                }
                Mode::CopyBlock => {
                    loop {
                        let mut copy = self.length;

                        if copy == 0 {
                            break;
                        }

                        copy = Ord::min(copy, writer.remaining());
                        copy = Ord::min(copy, bit_reader.bytes_remaining());

                        if copy == 0 {
                            break 'label self.inflate_leave(ReturnCode::Ok);
                        }

                        writer.extend(&bit_reader.as_slice()[..copy]);
                        bit_reader.advance(copy);

                        self.length -= copy;
                    }

                    self.mode = Mode::Type;

                    continue 'label;
                }
                Mode::Check => {
                    if !cfg!(feature = "__internal-fuzz-disable-checksum") && self.wrap != 0 {
                        need_bits!(self, 32);

                        self.total += writer.len();

                        if self.wrap & 4 != 0 {
                            if self.gzip_flags != 0 {
                                self.crc_fold.fold(writer.filled(), self.checksum);
                                self.checksum = self.crc_fold.finish();
                            } else {
                                self.checksum = adler32(self.checksum, writer.filled());
                            }
                        }

                        let given_checksum = if self.gzip_flags != 0 {
                            bit_reader.hold() as u32
                        } else {
                            zswap32(bit_reader.hold() as u32)
                        };

                        self.out_available = writer.capacity() - writer.len();

                        if self.wrap & 4 != 0 && given_checksum != self.checksum {
                            self.mode = Mode::Bad;
                            break 'label self.bad("incorrect data check\0");
                        }

                        bit_reader.init_bits();
                    }
                    self.mode = Mode::Length;

                    continue 'label;
                }
                Mode::Len => {
                    let avail_in = bit_reader.bytes_remaining();
                    let avail_out = writer.remaining();

                    // INFLATE_FAST_MIN_LEFT is important. It makes sure there is at least 32 bytes of free
                    // space available. This means for many SIMD operations we don't need to process a
                    // remainder; we just copy blindly, and a later operation will overwrite the extra copied
                    // bytes
                    if avail_in >= INFLATE_FAST_MIN_HAVE && avail_out >= INFLATE_FAST_MIN_LEFT {
                        restore!(self);
                        inflate_fast_help(self, 0);
                        load!(self);
                        continue 'label;
                    }

                    self.back = 0;

                    // get a literal, length, or end-of-block code
                    let mut here;
                    loop {
                        let bits = bit_reader.bits(self.len_table.bits);
                        here = self.len_table_get(bits as usize);

                        if here.bits <= bit_reader.bits_in_buffer() {
                            break;
                        }

                        pull_byte!(self);
                    }

                    if here.op != 0 && here.op & 0xf0 == 0 {
                        let last = here;
                        loop {
                            let bits = bit_reader.bits((last.bits + last.op) as usize) as u16;
                            here = self.len_table_get((last.val + (bits >> last.bits)) as usize);
                            if last.bits + here.bits <= bit_reader.bits_in_buffer() {
                                break;
                            }

                            pull_byte!(self);
                        }

                        bit_reader.drop_bits(last.bits);
                        self.back += last.bits as usize;
                    }

                    bit_reader.drop_bits(here.bits);
                    self.back += here.bits as usize;
                    self.length = here.val as usize;

                    if here.op == 0 {
                        self.mode = Mode::Lit;

                        continue 'label;
                    } else if here.op & 32 != 0 {
                        // end of block

                        // eprintln!("inflate:         end of block");

                        self.back = usize::MAX;
                        self.mode = Mode::Type;

                        continue 'label;
                    } else if here.op & 64 != 0 {
                        self.mode = Mode::Bad;

                        break 'label self.bad("invalid literal/length code\0");
                    } else {
                        // length code
                        self.extra = (here.op & MAX_BITS) as usize;
                        self.mode = Mode::LenExt;

                        continue 'label;
                    }
                }
                Mode::Len_ => {
                    self.mode = Mode::Len;

                    continue 'label;
                }
                Mode::LenExt => {
                    let extra = self.extra;

                    // get extra bits, if any
                    if extra != 0 {
                        need_bits!(self, extra);
                        self.length += bit_reader.bits(extra) as usize;
                        bit_reader.drop_bits(extra as u8);
                        self.back += extra;
                    }

                    // eprintln!("inflate: length {}", state.length);

                    self.was = self.length;
                    self.mode = Mode::Dist;

                    continue 'label;
                }
                Mode::Lit => {
                    if writer.is_full() {
                        #[cfg(all(test, feature = "std"))]
                        eprintln!("Ok: writer is full ({} bytes)", writer.capacity());
                        break 'label self.inflate_leave(ReturnCode::Ok);
                    }

                    writer.push(self.length as u8);

                    self.mode = Mode::Len;

                    continue 'label;
                }
                Mode::Dist => {
                    // get distance code
                    let mut here;
                    loop {
                        let bits = bit_reader.bits(self.dist_table.bits) as usize;
                        here = self.dist_table_get(bits);
                        if here.bits <= bit_reader.bits_in_buffer() {
                            break;
                        }

                        pull_byte!(self);
                    }

                    if here.op & 0xf0 == 0 {
                        let last = here;

                        loop {
                            let bits = bit_reader.bits((last.bits + last.op) as usize);
                            here = self
                                .dist_table_get(last.val as usize + ((bits as usize) >> last.bits));

                            if last.bits + here.bits <= bit_reader.bits_in_buffer() {
                                break;
                            }

                            pull_byte!(self);
                        }

                        bit_reader.drop_bits(last.bits);
                        self.back += last.bits as usize;
                    }

                    bit_reader.drop_bits(here.bits);

                    if here.op & 64 != 0 {
                        self.mode = Mode::Bad;
                        break 'label self.bad("invalid distance code\0");
                    }

                    self.offset = here.val as usize;

                    self.extra = (here.op & MAX_BITS) as usize;
                    self.mode = Mode::DistExt;

                    continue 'label;
                }
                Mode::DistExt => {
                    let extra = self.extra;

                    if extra > 0 {
                        need_bits!(self, extra);
                        self.offset += bit_reader.bits(extra) as usize;
                        bit_reader.drop_bits(extra as u8);
                        self.back += extra;
                    }

                    if INFLATE_STRICT && self.offset > self.dmax {
                        self.mode = Mode::Bad;
                        break 'label self.bad("invalid distance code too far back\0");
                    }

                    // eprintln!("inflate: distance {}", state.offset);

                    self.mode = Mode::Match;

                    continue 'label;
                }
                Mode::Match => {
                    'match_: loop {
                        if writer.is_full() {
                            #[cfg(all(feature = "std", test))]
                            eprintln!("BufError: writer is full ({} bytes)", writer.capacity());
                            break 'label self.inflate_leave(ReturnCode::Ok);
                        }

                        let left = writer.remaining();
                        let copy = writer.len();

                        let copy = if self.offset > copy {
                            // copy from window to output

                            let mut copy = self.offset - copy;

                            if copy > self.window.have() {
                                if self.flags.contains(Flags::SANE) {
                                    self.mode = Mode::Bad;
                                    break 'label self.bad("invalid distance too far back\0");
                                }

                                // TODO INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
                                panic!("INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR")
                            }

                            let wnext = self.window.next();
                            let wsize = self.window.size();

                            let from = if copy > wnext {
                                copy -= wnext;
                                wsize - copy
                            } else {
                                wnext - copy
                            };

                            copy = Ord::min(copy, self.length);
                            copy = Ord::min(copy, left);

                            writer.extend_from_window(&self.window, from..from + copy);

                            copy
                        } else {
                            let copy = Ord::min(self.length, left);
                            writer.copy_match(self.offset, copy);

                            copy
                        };

                        self.length -= copy;

                        if self.length == 0 {
                            self.mode = Mode::Len;

                            continue 'label;
                        } else {
                            // otherwise it seems to recurse?
                            continue 'match_;
                        }
                    }
                }
                Mode::Done => todo!(),
                Mode::Table => {
                    need_bits!(self, 14);
                    self.nlen = bit_reader.bits(5) as usize + 257;
                    bit_reader.drop_bits(5);
                    self.ndist = bit_reader.bits(5) as usize + 1;
                    bit_reader.drop_bits(5);
                    self.ncode = bit_reader.bits(4) as usize + 4;
                    bit_reader.drop_bits(4);

                    // TODO pkzit_bug_workaround
                    if self.nlen > 286 || self.ndist > 30 {
                        self.mode = Mode::Bad;
                        break 'label self.bad("too many length or distance symbols\0");
                    }

                    self.have = 0;
                    self.mode = Mode::LenLens;

                    continue 'label;
                }
                Mode::LenLens => {
                    // permutation of code lengths ;
                    const ORDER: [u16; 19] = [
                        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
                    ];

                    while self.have < self.ncode {
                        need_bits!(self, 3);
                        self.lens[ORDER[self.have] as usize] = bit_reader.bits(3) as u16;
                        self.have += 1;
                        bit_reader.drop_bits(3);
                    }

                    while self.have < 19 {
                        self.lens[ORDER[self.have] as usize] = 0;
                        self.have += 1;
                    }

                    self.len_table.bits = 7;

                    let InflateTable::Success(root) = inflate_table(
                        CodeType::Codes,
                        &self.lens,
                        19,
                        &mut self.codes_codes,
                        self.len_table.bits,
                        &mut self.work,
                    ) else {
                        self.mode = Mode::Bad;
                        break 'label self.bad("invalid code lengths set\0");
                    };

                    self.len_table.codes = Codes::Codes;
                    self.len_table.bits = root;

                    self.have = 0;
                    self.mode = Mode::CodeLens;

                    continue 'label;
                }
                Mode::CodeLens => {
                    while self.have < self.nlen + self.ndist {
                        let here = loop {
                            let bits = bit_reader.bits(self.len_table.bits);
                            let here = self.len_table_get(bits as usize);
                            if here.bits <= bit_reader.bits_in_buffer() {
                                break here;
                            }

                            pull_byte!(self);
                        };

                        let here_bits = here.bits;

                        match here.val {
                            0..=15 => {
                                bit_reader.drop_bits(here_bits);
                                self.lens[self.have] = here.val;
                                self.have += 1;
                            }
                            16 => {
                                need_bits!(self, here_bits as usize + 2);
                                bit_reader.drop_bits(here_bits);
                                if self.have == 0 {
                                    self.mode = Mode::Bad;
                                    break 'label self.bad("invalid bit length repeat\0");
                                }

                                let len = self.lens[self.have - 1];
                                let copy = 3 + bit_reader.bits(2) as usize;
                                bit_reader.drop_bits(2);

                                if self.have + copy > self.nlen + self.ndist {
                                    self.mode = Mode::Bad;
                                    break 'label self.bad("invalid bit length repeat\0");
                                }

                                for _ in 0..copy {
                                    self.lens[self.have] = len;
                                    self.have += 1;
                                }
                            }
                            17 => {
                                need_bits!(self, here_bits as usize + 3);
                                bit_reader.drop_bits(here_bits);
                                let len = 0;
                                let copy = 3 + bit_reader.bits(3) as usize;
                                bit_reader.drop_bits(3);

                                if self.have + copy > self.nlen + self.ndist {
                                    self.mode = Mode::Bad;
                                    break 'label self.bad("invalid bit length repeat\0");
                                }

                                for _ in 0..copy {
                                    self.lens[self.have] = len as u16;
                                    self.have += 1;
                                }
                            }
                            18.. => {
                                need_bits!(self, here_bits as usize + 7);
                                bit_reader.drop_bits(here_bits);
                                let len = 0;
                                let copy = 11 + bit_reader.bits(7) as usize;
                                bit_reader.drop_bits(7);

                                if self.have + copy > self.nlen + self.ndist {
                                    self.mode = Mode::Bad;
                                    break 'label self.bad("invalid bit length repeat\0");
                                }

                                for _ in 0..copy {
                                    self.lens[self.have] = len as u16;
                                    self.have += 1;
                                }
                            }
                        }
                    }

                    // check for end-of-block code (better have one)
                    if self.lens[256] == 0 {
                        self.mode = Mode::Bad;
                        break 'label self.bad("invalid code -- missing end-of-block\0");
                    }

                    // build code tables

                    self.len_table.bits = 10;

                    let InflateTable::Success(root) = inflate_table(
                        CodeType::Lens,
                        &self.lens,
                        self.nlen,
                        &mut self.len_codes,
                        self.len_table.bits,
                        &mut self.work,
                    ) else {
                        self.mode = Mode::Bad;
                        break 'label self.bad("invalid literal/lengths set\0");
                    };

                    self.len_table.codes = Codes::Len;
                    self.len_table.bits = root;

                    self.dist_table.bits = 9;

                    let InflateTable::Success(root) = inflate_table(
                        CodeType::Dists,
                        &self.lens[self.nlen..],
                        self.ndist,
                        &mut self.dist_codes,
                        self.dist_table.bits,
                        &mut self.work,
                    ) else {
                        self.mode = Mode::Bad;
                        break 'label self.bad("invalid distances set\0");
                    };

                    self.dist_table.bits = root;
                    self.dist_table.codes = Codes::Dist;

                    self.mode = Mode::Len_;

                    if matches!(self.flush, InflateFlush::Trees) {
                        break 'label self.inflate_leave(ReturnCode::Ok);
                    }

                    continue 'label;
                }
                Mode::Dict => {
                    if !self.flags.contains(Flags::HAVE_DICT) {
                        break 'label self.inflate_leave(ReturnCode::NeedDict);
                    }

                    self.checksum = crate::ADLER32_INITIAL_VALUE as _;

                    self.mode = Mode::Type;

                    continue 'label;
                }
                Mode::DictId => {
                    need_bits!(self, 32);

                    self.checksum = zswap32(bit_reader.hold() as u32);

                    bit_reader.init_bits();

                    self.mode = Mode::Dict;

                    continue 'label;
                }
                Mode::Bad => {
                    let msg = "repeated call with bad state\0";
                    #[cfg(all(feature = "std", test))]
                    dbg!(msg);
                    self.error_message = Some(msg);

                    break 'label ReturnCode::DataError;
                }
                Mode::Mem => {
                    break 'label ReturnCode::MemError;
                }
                Mode::Sync => {
                    break 'label ReturnCode::StreamError;
                }
                Mode::Length => {
                    // for gzip, last bytes contain LENGTH
                    if self.wrap != 0 && self.gzip_flags != 0 {
                        need_bits!(self, 32);
                        if (self.wrap & 4) != 0 && bit_reader.hold() != self.total as u64 {
                            self.mode = Mode::Bad;
                            break 'label self.bad("incorrect length check\0");
                        }

                        bit_reader.init_bits();
                    }

                    // inflate stream terminated properly
                    break 'label ReturnCode::StreamEnd;
                }
            };
        };

        restore!(self);

        return_code
    }

    fn bad(&mut self, msg: &'static str) -> ReturnCode {
        #[cfg(all(feature = "std", test))]
        dbg!(msg);
        self.error_message = Some(msg);
        self.inflate_leave(ReturnCode::DataError)
    }

    // NOTE: it is crucial for the internal bookkeeping that this is the only route for actually
    // leaving the inflate function call chain
    fn inflate_leave(&mut self, return_code: ReturnCode) -> ReturnCode {
        // actual logic is in `inflate` itself
        return_code
    }

    /// Stored in the `z_stream.data_type` field
    fn decoding_state(&self) -> i32 {
        let bit_reader_bits = self.bit_reader.bits_in_buffer() as i32;
        debug_assert!(bit_reader_bits < 64);

        let last = if self.flags.contains(Flags::IS_LAST_BLOCK) {
            64
        } else {
            0
        };

        let mode = match self.mode {
            Mode::Type => 128,
            Mode::Len_ | Mode::CopyBlock => 256,
            _ => 0,
        };

        bit_reader_bits | last | mode
    }
}

fn inflate_fast_help(state: &mut State, _start: usize) {
    let mut bit_reader = BitReader::new(&[]);
    core::mem::swap(&mut bit_reader, &mut state.bit_reader);

    let mut writer = Writer::new(&mut []);
    core::mem::swap(&mut writer, &mut state.writer);

    let lcode = state.len_table_ref();
    let dcode = state.dist_table_ref();

    // IDEA: use const generics for the bits here?
    let lmask = (1u64 << state.len_table.bits) - 1;
    let dmask = (1u64 << state.dist_table.bits) - 1;

    // TODO verify if this is relevant for us
    let extra_safe = false;

    let window_size = state.window.size();

    let mut bad = None;

    if bit_reader.bits_in_buffer() < 10 {
        bit_reader.refill();
    }

    'outer: loop {
        let mut here = {
            let bits = bit_reader.bits_in_buffer();
            let hold = bit_reader.hold();

            bit_reader.refill();

            // in most cases, the read can be interleaved with the logic
            // based on benchmarks this matters in practice. wild.
            if bits as usize >= state.len_table.bits {
                lcode[(hold & lmask) as usize]
            } else {
                lcode[(bit_reader.hold() & lmask) as usize]
            }
        };

        if here.op == 0 {
            writer.push(here.val as u8);
            bit_reader.drop_bits(here.bits);
            here = lcode[(bit_reader.hold() & lmask) as usize];

            if here.op == 0 {
                writer.push(here.val as u8);
                bit_reader.drop_bits(here.bits);
                here = lcode[(bit_reader.hold() & lmask) as usize];
            }
        }

        'dolen: loop {
            bit_reader.drop_bits(here.bits);
            let op = here.op;

            if op == 0 {
                writer.push(here.val as u8);
            } else if op & 16 != 0 {
                let op = op & MAX_BITS;
                let mut len = here.val + bit_reader.bits(op as usize) as u16;
                bit_reader.drop_bits(op);

                here = dcode[(bit_reader.hold() & dmask) as usize];

                // we have two fast-path loads: 10+10 + 15+5 = 40,
                // but we may need to refill here in the worst case
                if bit_reader.bits_in_buffer() < MAX_BITS + MAX_DIST_EXTRA_BITS {
                    bit_reader.refill();
                }

                'dodist: loop {
                    bit_reader.drop_bits(here.bits);
                    let op = here.op;

                    if op & 16 != 0 {
                        let op = op & MAX_BITS;
                        let dist = here.val + bit_reader.bits(op as usize) as u16;

                        if INFLATE_STRICT && dist as usize > state.dmax {
                            bad = Some("invalid distance too far back\0");
                            state.mode = Mode::Bad;
                            break 'outer;
                        }

                        bit_reader.drop_bits(op);

                        // max distance in output
                        let written = writer.len();

                        if dist as usize > written {
                            // copy fropm the window
                            if (dist as usize - written) > state.window.have() {
                                if state.flags.contains(Flags::SANE) {
                                    bad = Some("invalid distance too far back\0");
                                    state.mode = Mode::Bad;
                                    break 'outer;
                                }

                                panic!("INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR")
                            }

                            let mut op = dist as usize - written;
                            let mut from;

                            let window_next = state.window.next();

                            if window_next == 0 {
                                // This case is hit when the window has just wrapped around
                                // by logic in `Window::extend`. It is special-cased because
                                // apparently this is quite common.
                                //
                                // the match is at the end of the window, even though the next
                                // position has now wrapped around.
                                from = window_size - op;
                            } else if window_next >= op {
                                // the standard case: a contiguous copy from the window, no wrapping
                                from = window_next - op;
                            } else {
                                // This case is hit when the window has recently wrapped around
                                // by logic in `Window::extend`.
                                //
                                // The match is (partially) at the end of the window
                                op -= window_next;
                                from = window_size - op;

                                if op < len as usize {
                                    // This case is hit when part of the match is at the end of the
                                    // window, and part of it has wrapped around to the start. Copy
                                    // the end section here, the start section will be copied below.
                                    len -= op as u16;
                                    writer.extend_from_window(&state.window, from..from + op);
                                    from = 0;
                                    op = window_next;
                                }
                            }

                            let copy = Ord::min(op, len as usize);
                            writer.extend_from_window(&state.window, from..from + copy);

                            if op < len as usize {
                                // here we need some bytes from the output itself
                                writer.copy_match(dist as usize, len as usize - op);
                            }
                        } else if extra_safe {
                            todo!()
                        } else {
                            writer.copy_match(dist as usize, len as usize)
                        }
                    } else if (op & 64) == 0 {
                        // 2nd level distance code
                        here = dcode[(here.val + bit_reader.bits(op as usize) as u16) as usize];
                        continue 'dodist;
                    } else {
                        bad = Some("invalid distance code\0");
                        state.mode = Mode::Bad;
                        break 'outer;
                    }

                    break 'dodist;
                }
            } else if (op & 64) == 0 {
                // 2nd level length code
                here = lcode[(here.val + bit_reader.bits(op as usize) as u16) as usize];
                continue 'dolen;
            } else if op & 32 != 0 {
                // end of block
                state.mode = Mode::Type;
                break 'outer;
            } else {
                bad = Some("invalid literal/length code\0");
                state.mode = Mode::Bad;
                break 'outer;
            }

            break 'dolen;
        }

        let remaining = bit_reader.bytes_remaining();
        if remaining.saturating_sub(INFLATE_FAST_MIN_LEFT - 1) > 0
            && writer.remaining() > INFLATE_FAST_MIN_LEFT
        {
            continue;
        }

        break 'outer;
    }

    // return unused bytes (on entry, bits < 8, so in won't go too far back)
    bit_reader.return_unused_bytes();

    state.bit_reader = bit_reader;
    state.writer = writer;

    if let Some(error_message) = bad {
        debug_assert!(matches!(state.mode, Mode::Bad));
        state.bad(error_message);
    }
}

pub fn prime(stream: &mut InflateStream, bits: i32, value: i32) -> ReturnCode {
    if bits == 0 {
        /* fall through */
    } else if bits < 0 {
        stream.state.bit_reader.init_bits();
    } else if bits > 16 || stream.state.bit_reader.bits_in_buffer() + bits as u8 > 32 {
        return ReturnCode::StreamError;
    } else {
        stream.state.bit_reader.prime(bits as u8, value as u64);
    }

    ReturnCode::Ok
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct InflateConfig {
    pub window_bits: i32,
}

impl Default for InflateConfig {
    fn default() -> Self {
        Self {
            window_bits: DEF_WBITS,
        }
    }
}

/// Initialize the stream in an inflate state
pub fn init(stream: &mut z_stream, config: InflateConfig) -> ReturnCode {
    stream.msg = core::ptr::null_mut();

    // for safety we must really make sure that alloc and free are consistent
    // this is a (slight) deviation from stock zlib. In this crate we pick the rust
    // allocator as the default, but `libz-rs-sys` configures the C allocator
    #[cfg(feature = "rust-allocator")]
    if stream.zalloc.is_none() || stream.zfree.is_none() {
        stream.configure_default_rust_allocator()
    }

    #[cfg(feature = "c-allocator")]
    if stream.zalloc.is_none() || stream.zfree.is_none() {
        stream.configure_default_c_allocator()
    }

    if stream.zalloc.is_none() || stream.zfree.is_none() {
        return ReturnCode::StreamError;
    }

    let mut state = State::new(&[], Writer::new(&mut []));

    // TODO this can change depending on the used/supported SIMD instructions
    state.chunksize = 32;

    let alloc = Allocator {
        zalloc: stream.zalloc.unwrap(),
        zfree: stream.zfree.unwrap(),
        opaque: stream.opaque,
        _marker: PhantomData,
    };

    // allocated here to have the same order as zlib
    let Some(state_allocation) = alloc.allocate_raw::<State>() else {
        return ReturnCode::MemError;
    };

    unsafe { state_allocation.write(state) };
    stream.state = state_allocation as *mut internal_state;

    // SAFETY: we've correctly initialized the stream to be an InflateStream
    let ret = if let Some(stream) = unsafe { InflateStream::from_stream_mut(stream) } {
        reset_with_config(stream, config)
    } else {
        ReturnCode::StreamError
    };

    if ret != ReturnCode::Ok {
        let ptr = stream.state;
        stream.state = core::ptr::null_mut();
        // SAFETY: we assume deallocation does not cause UB
        unsafe { alloc.deallocate(ptr, 1) };
    }

    ret
}

pub fn reset_with_config(stream: &mut InflateStream, config: InflateConfig) -> ReturnCode {
    let mut window_bits = config.window_bits;
    let wrap;

    if window_bits < 0 {
        wrap = 0;

        if window_bits < -MAX_WBITS {
            return ReturnCode::StreamError;
        }

        window_bits = -window_bits;
    } else {
        wrap = (window_bits >> 4) + 5; // TODO wth?

        if window_bits < 48 {
            window_bits &= MAX_WBITS;
        }
    }

    if window_bits != 0 && !(MIN_WBITS..=MAX_WBITS).contains(&window_bits) {
        #[cfg(feature = "std")]
        eprintln!("invalid windowBits");
        return ReturnCode::StreamError;
    }

    if stream.state.window.size() != 0 && stream.state.wbits as i32 != window_bits {
        let mut window = Window::empty();
        core::mem::swap(&mut window, &mut stream.state.window);

        let (ptr, len) = window.into_raw_parts();
        assert_ne!(len, 0);
        // SAFETY: window is discarded after this deallocation.
        unsafe { stream.alloc.deallocate(ptr, len) };
    }

    stream.state.wrap = wrap as u8;
    stream.state.wbits = window_bits as _;

    reset(stream)
}

pub fn reset(stream: &mut InflateStream) -> ReturnCode {
    // reset the state of the window
    stream.state.window.clear();

    stream.state.error_message = None;

    reset_keep(stream)
}

pub fn reset_keep(stream: &mut InflateStream) -> ReturnCode {
    stream.total_in = 0;
    stream.total_out = 0;
    stream.state.total = 0;

    stream.msg = core::ptr::null_mut();

    let state = &mut stream.state;

    if state.wrap != 0 {
        // to support ill-conceived Java test suite
        stream.adler = (state.wrap & 1) as _;
    }

    state.mode = Mode::Head;
    state.checksum = crate::ADLER32_INITIAL_VALUE as u32;

    state.flags.update(Flags::IS_LAST_BLOCK, false);
    state.flags.update(Flags::HAVE_DICT, false);
    state.flags.update(Flags::SANE, true);
    state.gzip_flags = -1;
    state.dmax = 32768;
    state.head = None;
    state.bit_reader = BitReader::new(&[]);

    state.next = 0;
    state.len_table = Table::default();
    state.dist_table = Table::default();

    state.back = usize::MAX;

    ReturnCode::Ok
}

pub unsafe fn inflate(stream: &mut InflateStream, flush: InflateFlush) -> ReturnCode {
    if stream.next_out.is_null() || (stream.next_in.is_null() && stream.avail_in != 0) {
        return ReturnCode::StreamError as _;
    }

    let state = &mut stream.state;

    // skip check
    if let Mode::Type = state.mode {
        state.mode = Mode::TypeDo;
    }

    state.flush = flush;

    unsafe {
        state
            .bit_reader
            .update_slice(stream.next_in, stream.avail_in as usize)
    };
    state.writer = Writer::new_uninit(stream.next_out.cast(), stream.avail_out as usize);

    state.in_available = stream.avail_in as _;
    state.out_available = stream.avail_out as _;

    let mut err = state.dispatch();

    let in_read = state.bit_reader.as_ptr() as usize - stream.next_in as usize;
    let out_written = state.out_available - (state.writer.capacity() - state.writer.len());

    stream.total_in += in_read as z_size;
    state.total += out_written;
    stream.total_out = state.total as _;

    stream.avail_in = state.bit_reader.bytes_remaining() as u32;
    stream.next_in = state.bit_reader.as_ptr() as *mut u8;

    stream.avail_out = (state.writer.capacity() - state.writer.len()) as u32;
    stream.next_out = state.writer.next_out() as *mut u8;

    stream.adler = state.checksum as z_checksum;

    let valid_mode = |mode| !matches!(mode, Mode::Bad | Mode::Mem | Mode::Sync);
    let not_done = |mode| {
        !matches!(
            mode,
            Mode::Check | Mode::Length | Mode::Bad | Mode::Mem | Mode::Sync
        )
    };

    let must_update_window = state.window.size() != 0
        || (out_written != 0
            && valid_mode(state.mode)
            && (not_done(state.mode) || !matches!(state.flush, InflateFlush::Finish)));

    let update_checksum = state.wrap & 4 != 0;

    if must_update_window {
        'blk: {
            // initialize the window if needed
            if state.window.size() == 0 {
                match Window::new_in(&stream.alloc, state.wbits as usize) {
                    Some(window) => state.window = window,
                    None => {
                        state.mode = Mode::Mem;
                        err = ReturnCode::MemError;
                        break 'blk;
                    }
                }
            }

            state.window.extend(
                &state.writer.filled()[..out_written],
                state.gzip_flags,
                update_checksum,
                &mut state.checksum,
                &mut state.crc_fold,
            );
        }
    }

    if let Some(msg) = state.error_message {
        assert!(msg.ends_with('\0'));
        stream.msg = msg.as_ptr() as *mut u8 as *mut core::ffi::c_char;
    }

    stream.data_type = state.decoding_state();

    if ((in_read == 0 && out_written == 0) || flush == InflateFlush::Finish as _)
        && err == (ReturnCode::Ok as _)
    {
        ReturnCode::BufError as _
    } else {
        err as _
    }
}

fn syncsearch(mut got: usize, buf: &[u8]) -> (usize, usize) {
    let len = buf.len();
    let mut next = 0;

    while next < len && got < 4 {
        if buf[next] == if got < 2 { 0 } else { 0xff } {
            got += 1;
        } else if buf[next] != 0 {
            got = 0;
        } else {
            got = 4 - got;
        }
        next += 1;
    }

    (got, next)
}

pub fn sync(stream: &mut InflateStream) -> ReturnCode {
    let state = &mut stream.state;

    if stream.avail_in == 0 && state.bit_reader.bits_in_buffer() < 8 {
        return ReturnCode::BufError;
    }
    /* if first time, start search in bit buffer */
    if !matches!(state.mode, Mode::Sync) {
        state.mode = Mode::Sync;

        let (buf, len) = state.bit_reader.start_sync_search();

        (state.have, _) = syncsearch(0, &buf[..len]);
    }

    // search available input
    // SAFETY: user guarantees that pointer and length are valid.
    let slice = unsafe { core::slice::from_raw_parts(stream.next_in, stream.avail_in as usize) };

    let len;
    (state.have, len) = syncsearch(state.have, slice);
    // SAFETY: syncsearch() returns an index that is in-bounds of the slice.
    stream.next_in = unsafe { stream.next_in.add(len) };
    stream.avail_in -= len as u32;
    stream.total_in += len as z_size;

    /* return no joy or set up to restart inflate() on a new block */
    if state.have != 4 {
        return ReturnCode::DataError;
    }

    if state.gzip_flags == -1 {
        state.wrap = 0; /* if no header yet, treat as raw */
    } else {
        state.wrap &= !4; /* no point in computing a check value now */
    }

    let flags = state.gzip_flags;
    let total_in = stream.total_in;
    let total_out = stream.total_out;

    reset(stream);

    stream.total_in = total_in;
    stream.total_out = total_out;

    stream.state.gzip_flags = flags;
    stream.state.mode = Mode::Type;

    ReturnCode::Ok
}

/*
  Returns true if inflate is currently at the end of a block generated by
  Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  implementation to provide an additional safety check. PPP uses
  Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
  block. When decompressing, PPP checks that at the end of input packet,
  inflate is waiting for these length bytes.
*/
pub fn sync_point(stream: &mut InflateStream) -> bool {
    matches!(stream.state.mode, Mode::Stored) && stream.state.bit_reader.bits_in_buffer() == 0
}

pub unsafe fn copy<'a>(
    dest: &mut MaybeUninit<InflateStream<'a>>,
    source: &InflateStream<'a>,
) -> ReturnCode {
    if source.next_out.is_null() || (source.next_in.is_null() && source.avail_in != 0) {
        return ReturnCode::StreamError;
    }

    // Safety: source and dest are both mutable references, so guaranteed not to overlap.
    // dest being a reference to maybe uninitialized memory makes a copy of 1 DeflateStream valid.
    unsafe {
        core::ptr::copy_nonoverlapping(source, dest.as_mut_ptr(), 1);
    }

    // allocated here to have the same order as zlib
    let Some(state_allocation) = source.alloc.allocate_raw::<State>() else {
        return ReturnCode::MemError;
    };

    let state = &source.state;

    // SAFETY: an initialized Writer is a valid MaybeUninit<Writer>.
    let writer: MaybeUninit<Writer> =
        unsafe { core::ptr::read(&state.writer as *const _ as *const MaybeUninit<Writer>) };

    let mut copy = State {
        mode: state.mode,
        flags: state.flags,
        wrap: state.wrap,
        len_table: state.len_table,
        dist_table: state.dist_table,
        wbits: state.wbits,
        window: Window::empty(),
        head: None,
        ncode: state.ncode,
        nlen: state.nlen,
        ndist: state.ndist,
        have: state.have,
        next: state.next,
        bit_reader: state.bit_reader,
        writer: Writer::new(&mut []),
        total: state.total,
        length: state.length,
        offset: state.offset,
        extra: state.extra,
        back: state.back,
        was: state.was,
        chunksize: state.chunksize,
        in_available: state.in_available,
        out_available: state.out_available,
        lens: state.lens,
        work: state.work,
        error_message: state.error_message,
        flush: state.flush,
        checksum: state.checksum,
        crc_fold: state.crc_fold,
        dmax: state.dmax,
        gzip_flags: state.gzip_flags,
        codes_codes: state.codes_codes,
        len_codes: state.len_codes,
        dist_codes: state.dist_codes,
    };

    if !state.window.is_empty() {
        let Some(window) = state.window.clone_in(&source.alloc) else {
            // SAFETY: state_allocation is not used again.
            source.alloc.deallocate(state_allocation, 1);
            return ReturnCode::MemError;
        };

        copy.window = window;
    }

    // write the cloned state into state_ptr
    unsafe { state_allocation.write(copy) };

    // insert the state_ptr into `dest`
    let field_ptr = unsafe { core::ptr::addr_of_mut!((*dest.as_mut_ptr()).state) };
    unsafe { core::ptr::write(field_ptr as *mut *mut State, state_allocation) };

    // update the writer; it cannot be cloned so we need to use some shennanigans
    let field_ptr = unsafe { core::ptr::addr_of_mut!((*dest.as_mut_ptr()).state.writer) };
    unsafe { core::ptr::copy(writer.as_ptr(), field_ptr, 1) };

    // update the gzhead field (it contains a mutable reference so we need to be careful
    let field_ptr = unsafe { core::ptr::addr_of_mut!((*dest.as_mut_ptr()).state.head) };
    unsafe { core::ptr::copy(&source.state.head, field_ptr, 1) };

    ReturnCode::Ok
}

pub fn undermine(stream: &mut InflateStream, subvert: i32) -> ReturnCode {
    stream.state.flags.update(Flags::SANE, (!subvert) != 0);

    ReturnCode::Ok
}

pub fn mark(stream: &InflateStream) -> c_long {
    if stream.next_out.is_null() || (stream.next_in.is_null() && stream.avail_in != 0) {
        return c_long::MIN;
    }

    let state = &stream.state;

    let length = match state.mode {
        Mode::CopyBlock => state.length,
        Mode::Match => state.was - state.length,
        _ => 0,
    };

    (((state.back as c_long) as c_ulong) << 16) as c_long + length as c_long
}

pub fn set_dictionary(stream: &mut InflateStream, dictionary: &[u8]) -> ReturnCode {
    if stream.state.wrap != 0 && !matches!(stream.state.mode, Mode::Dict) {
        return ReturnCode::StreamError;
    }

    // check for correct dictionary identifier
    if matches!(stream.state.mode, Mode::Dict) {
        let dictid = adler32(1, dictionary);

        if dictid != stream.state.checksum {
            return ReturnCode::DataError;
        }
    }

    let err = 'blk: {
        // initialize the window if needed
        if stream.state.window.size() == 0 {
            match Window::new_in(&stream.alloc, stream.state.wbits as usize) {
                None => break 'blk ReturnCode::MemError,
                Some(window) => stream.state.window = window,
            }
        }

        stream.state.window.extend(
            dictionary,
            stream.state.gzip_flags,
            false,
            &mut stream.state.checksum,
            &mut stream.state.crc_fold,
        );

        ReturnCode::Ok
    };

    if err != ReturnCode::Ok {
        stream.state.mode = Mode::Mem;
        return ReturnCode::MemError;
    }

    stream.state.flags.update(Flags::HAVE_DICT, true);

    ReturnCode::Ok
}

pub fn end<'a>(stream: &'a mut InflateStream<'a>) -> &'a mut z_stream {
    let alloc = stream.alloc;

    let mut window = Window::empty();
    core::mem::swap(&mut window, &mut stream.state.window);

    // safety: window is not used again
    if !window.is_empty() {
        let (ptr, len) = window.into_raw_parts();
        unsafe { alloc.deallocate(ptr, len) };
    }

    let stream = stream.as_z_stream_mut();

    let state_ptr = core::mem::replace(&mut stream.state, core::ptr::null_mut());

    // safety: state_ptr is not used again
    unsafe { alloc.deallocate(state_ptr as *mut State, 1) };

    stream
}

/// # Safety
///
/// The caller must guarantee:
///
/// * If `head` is `Some`:
///     - If `head.extra` is not NULL, it must be writable for at least `head.extra_max` bytes
///     - if `head.name` is not NULL, it must be writable for at least `head.name_max` bytes
///     - if `head.comment` is not NULL, it must be writable for at least `head.comm_max` bytes
pub unsafe fn get_header<'a>(
    stream: &mut InflateStream<'a>,
    head: Option<&'a mut gz_header>,
) -> ReturnCode {
    if (stream.state.wrap & 2) == 0 {
        return ReturnCode::StreamError;
    }

    stream.state.head = head.map(|head| {
        head.done = 0;
        head
    });
    ReturnCode::Ok
}
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn uncompress_buffer_overflow() {
        let mut output = [0; 1 << 13];
        let input = [
            72, 137, 58, 0, 3, 39, 255, 255, 255, 255, 255, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
            14, 14, 184, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 184, 14, 14,
            14, 14, 14, 14, 14, 63, 14, 14, 14, 14, 14, 14, 14, 14, 184, 14, 14, 255, 14, 103, 14,
            14, 14, 14, 14, 14, 61, 14, 255, 255, 63, 14, 14, 14, 14, 14, 14, 14, 14, 184, 14, 14,
            255, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 6, 14, 14, 14, 14, 14, 14, 14, 14, 71,
            4, 137, 106,
        ];

        let config = InflateConfig { window_bits: 15 };

        let (_decompressed, err) = uncompress_slice(&mut output, &input, config);
        assert_eq!(err, ReturnCode::DataError);
    }
}