Debian Codegraph @

debian/rust-vendor/zlib-rs/src/inflate/infback.rs

raw
1 use core::ffi::c_void;
2 use core::marker::PhantomData;
3
4 use crate::allocate::Allocator;
5 use crate::c_api::{in_func, internal_state, out_func};
6 use crate::inflate::bitreader::BitReader;
7 use crate::inflate::inftrees::{inflate_table, CodeType, InflateTable};
8 use crate::inflate::{
9 Codes, Flags, InflateAllocOffsets, InflateConfig, InflateStream, Mode, State, Table, Window,
10 INFLATE_FAST_MIN_HAVE, INFLATE_FAST_MIN_LEFT, INFLATE_STRICT, MAX_BITS, MAX_DIST_EXTRA_BITS,
11 };
12 use crate::traceln;
13 use crate::{c_api::z_stream, inflate::writer::Writer, ReturnCode};
14 use crate::{MAX_WBITS, MIN_WBITS};
15
16 /// Initialize the stream in an inflate state
17 pub fn back_init(stream: &mut z_stream, config: InflateConfig, window: Window) -> ReturnCode {
18 assert_eq!(1 << config.window_bits, window.buffer_size());
19
20 if !(MIN_WBITS..=MAX_WBITS).contains(&config.window_bits) {
21 return ReturnCode::StreamError as _;
22 }
23
24 stream.msg = core::ptr::null_mut();
25
26 // for safety we must really make sure that alloc and free are consistent
27 // this is a (slight) deviation from stock zlib. In this crate we pick the rust
28 // allocator as the default, but `libz-rs-sys` configures the C allocator
29 #[cfg(feature = "rust-allocator")]
30 if stream.zalloc.is_none() || stream.zfree.is_none() {
31 stream.configure_default_rust_allocator()
32 }
33
34 #[cfg(feature = "c-allocator")]
35 if stream.zalloc.is_none() || stream.zfree.is_none() {
36 stream.configure_default_c_allocator()
37 }
38
39 if stream.zalloc.is_none() || stream.zfree.is_none() {
40 return ReturnCode::StreamError;
41 }
42
43 let mut state = State::new(&[], Writer::new(&mut []));
44
45 // TODO this can change depending on the used/supported SIMD instructions
46 state.chunksize = 32;
47
48 let alloc = Allocator {
49 zalloc: stream.zalloc.unwrap(),
50 zfree: stream.zfree.unwrap(),
51 opaque: stream.opaque,
52 _marker: PhantomData,
53 };
54 let allocs = InflateAllocOffsets::new();
55
56 let Some(allocation_start) = alloc.allocate_slice_raw::<u8>(allocs.total_size) else {
57 return ReturnCode::MemError;
58 };
59
60 let address = allocation_start.as_ptr() as usize;
61 let align_offset = address.next_multiple_of(64) - address;
62 let buf = unsafe { allocation_start.as_ptr().add(align_offset) };
63
64 // NOTE: the window part of the allocation is ignored in this case.
65 state.window = window;
66
67 let state_allocation = unsafe { buf.add(allocs.state_pos).cast::<State>() };
68 unsafe { state_allocation.write(state) };
69 stream.state = state_allocation.cast::<internal_state>();
70
71 // SAFETY: we've correctly initialized the stream to be an InflateStream
72 let Some(stream) = (unsafe { InflateStream::from_stream_mut(stream) }) else {
73 return ReturnCode::StreamError;
74 };
75
76 stream.state.allocation_start = allocation_start.as_ptr();
77 stream.state.total_allocation_size = allocs.total_size;
78
79 stream.state.wbits = config.window_bits as u8;
80 stream.state.flags.update(Flags::SANE, true);
81
82 ReturnCode::Ok
83 }
84
85 pub unsafe fn back(
86 strm: &mut InflateStream,
87 in_: in_func,
88 in_desc: *mut c_void,
89 out: out_func,
90 out_desc: *mut c_void,
91 ) -> ReturnCode {
92 let mut ret;
93
94 /* Reset the state */
95 strm.msg = core::ptr::null_mut();
96 strm.state.mode = Mode::Type;
97 strm.state.flags.update(Flags::IS_LAST_BLOCK, false);
98 strm.state.window.clear();
99
100 let mut next = strm.next_in.cast_const();
101 let mut have = if !next.is_null() { strm.avail_in } else { 0 };
102 let mut hold = 0;
103 let mut bits = 0u8;
104 let mut put = strm.state.window.as_mut_ptr();
105 let mut left = strm.state.window.buffer_size();
106
107 let state = &mut strm.state;
108
109 'inf_leave: loop {
110 macro_rules! initbits {
111 () => {
112 hold = 0;
113 bits = 0;
114 };
115 }
116
117 macro_rules! bytebits {
118 () => {
119 hold >>= bits & 7;
120 bits -= bits & 7;
121 };
122 }
123
124 macro_rules! dropbits {
125 ($n:expr) => {
126 hold >>= $n;
127 bits -= $n;
128 };
129 }
130
131 macro_rules! bits {
132 ($n:expr) => {
133 hold & ((1 << $n) - 1)
134 };
135 }
136
137 macro_rules! needbits {
138 ($n:expr) => {
139 while usize::from(bits) < $n {
140 pullbyte!();
141 }
142 };
143 }
144
145 macro_rules! pull {
146 () => {
147 if have == 0 {
148 have = unsafe { in_(in_desc, &mut next) };
149 if have == 0 {
150 #[allow(unused_assignments)]
151 {
152 next = core::ptr::null();
153 }
154 ret = ReturnCode::BufError;
155 break 'inf_leave;
156 }
157 }
158 };
159 }
160
161 macro_rules! pullbyte {
162 () => {
163 pull!();
164 have -= 1;
165 hold += (unsafe { *next as u64 }) << bits;
166 next = unsafe { next.add(1) };
167 bits += 8;
168 };
169 }
170
171 macro_rules! room {
172 () => {
173 if left == 0 {
174 left = state.window.buffer_size();
175 put = state.window.as_mut_ptr();
176
177 unsafe { state.window.set_have(left) };
178
179 if unsafe { out(out_desc, put, left as u32) } != 0 {
180 ret = ReturnCode::BufError;
181 break 'inf_leave;
182 }
183 }
184 };
185 }
186
187 match state.mode {
188 Mode::Type => {
189 if state.flags.contains(Flags::IS_LAST_BLOCK) {
190 bytebits!();
191 state.mode = Mode::Done;
192 continue;
193 }
194
195 needbits!(3);
196
197 let last = bits!(1) != 0;
198 state.flags.update(Flags::IS_LAST_BLOCK, last);
199 dropbits!(1);
200
201 match bits!(2) {
202 0b00 => {
203 traceln!("inflate: stored block (last = {last})");
204
205 dropbits!(2);
206 state.mode = Mode::Stored;
207 continue;
208 }
209 0b01 => {
210 traceln!("inflate: fixed codes block (last = {last})");
211
212 state.len_table = Table {
213 codes: Codes::Fixed,
214 bits: 9,
215 };
216
217 state.dist_table = Table {
218 codes: Codes::Fixed,
219 bits: 5,
220 };
221
222 dropbits!(2);
223 state.mode = Mode::Len;
224 continue;
225 }
226 0b10 => {
227 traceln!("inflate: dynamic codes block (last = {last})");
228
229 dropbits!(2);
230 state.mode = Mode::Table;
231 continue;
232 }
233 0b11 => {
234 traceln!("inflate: invalid block type");
235
236 dropbits!(2);
237 state.mode = Mode::Bad;
238 state.bad("invalid block type\0");
239 continue;
240 }
241 _ => {
242 // LLVM will optimize this branch away
243 unreachable!("BitReader::bits(2) only yields a value of two bits, so this match is already exhaustive")
244 }
245 }
246 }
247 Mode::Stored => {
248 bytebits!();
249 needbits!(32);
250
251 if hold as u16 != !((hold >> 16) as u16) {
252 state.mode = Mode::Bad;
253 state.bad("invalid stored block lengths\0");
254 continue;
255 }
256
257 state.length = hold as usize & 0xFFFF;
258 traceln!("inflate: stored length {}", state.length);
259
260 initbits!();
261
262 /* copy stored block from input to output */
263 while state.length != 0 {
264 let mut copy = state.length;
265
266 pull!();
267 room!();
268
269 copy = Ord::min(copy, have as usize);
270 copy = Ord::min(copy, left);
271
272 unsafe { core::ptr::copy(next, put, copy) };
273
274 have -= copy as u32;
275 next = unsafe { next.add(copy) };
276
277 left -= copy;
278 put = unsafe { put.add(copy) };
279
280 state.length -= copy;
281 }
282
283 state.mode = Mode::Type;
284 continue;
285 }
286 Mode::Table => {
287 needbits!(14);
288 state.nlen = bits!(5) as usize + 257;
289 dropbits!(5);
290 state.ndist = bits!(5) as usize + 1;
291 dropbits!(5);
292 state.ncode = bits!(4) as usize + 4;
293 dropbits!(4);
294
295 // TODO pkzit_bug_workaround
296 if state.nlen > 286 || state.ndist > 30 {
297 state.mode = Mode::Bad;
298 state.bad("too many length or distance symbols\0");
299 continue;
300 }
301
302 traceln!("inflate: table sizes ok");
303 state.have = 0;
304
305 // permutation of code lengths ;
306 const ORDER: [u8; 19] = [
307 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
308 ];
309
310 while state.have < state.ncode {
311 needbits!(3);
312 state.lens[usize::from(ORDER[state.have])] = bits!(3) as u16;
313 state.have += 1;
314 dropbits!(3);
315 }
316
317 while state.have < 19 {
318 state.lens[usize::from(ORDER[state.have])] = 0;
319 state.have += 1;
320 }
321
322 let InflateTable::Success { root, used } = inflate_table(
323 CodeType::Codes,
324 &state.lens[..19],
325 &mut state.codes_codes,
326 7,
327 &mut state.work,
328 ) else {
329 state.mode = Mode::Bad;
330 state.bad("invalid code lengths set\0");
331 continue;
332 };
333
334 state.next = used;
335 state.len_table.codes = Codes::Codes;
336 state.len_table.bits = root;
337
338 traceln!("inflate: table sizes ok");
339 state.have = 0;
340
341 while state.have < state.nlen + state.ndist {
342 let here = loop {
343 let here = state.len_table_get(bits!(state.len_table.bits) as usize);
344 if here.bits <= bits {
345 break here;
346 }
347
348 pullbyte!();
349 };
350
351 let here_bits = here.bits;
352
353 match here.val {
354 0..=15 => {
355 dropbits!(here_bits);
356 state.lens[state.have] = here.val;
357 state.have += 1;
358 }
359 16 => {
360 needbits!(usize::from(here_bits) + 2);
361 dropbits!(here_bits);
362 if state.have == 0 {
363 state.mode = Mode::Bad;
364 state.bad("invalid bit length repeat\0");
365 continue 'inf_leave;
366 }
367
368 let len = state.lens[state.have - 1];
369 let copy = 3 + bits!(2) as usize;
370 dropbits!(2);
371
372 if state.have + copy > state.nlen + state.ndist {
373 state.mode = Mode::Bad;
374 state.bad("invalid bit length repeat\0");
375 continue 'inf_leave;
376 }
377
378 state.lens[state.have..][..copy].fill(len);
379 state.have += copy;
380 }
381 17 => {
382 needbits!(usize::from(here_bits) + 3);
383 dropbits!(here_bits);
384 let copy = 3 + bits!(3) as usize;
385 dropbits!(3);
386
387 if state.have + copy > state.nlen + state.ndist {
388 state.mode = Mode::Bad;
389 state.bad("invalid bit length repeat\0");
390 continue 'inf_leave;
391 }
392
393 state.lens[state.have..][..copy].fill(0);
394 state.have += copy;
395 }
396 18.. => {
397 needbits!(usize::from(here_bits) + 7);
398 dropbits!(here_bits);
399 let copy = 11 + bits!(7) as usize;
400 dropbits!(7);
401
402 if state.have + copy > state.nlen + state.ndist {
403 state.mode = Mode::Bad;
404 state.bad("invalid bit length repeat\0");
405 continue 'inf_leave;
406 }
407
408 state.lens[state.have..][..copy].fill(0);
409 state.have += copy;
410 }
411 }
412 }
413
414 // check for end-of-block code (better have one)
415 if state.lens[256] == 0 {
416 state.mode = Mode::Bad;
417 state.bad("invalid code -- missing end-of-block\0");
418 continue 'inf_leave;
419 }
420
421 // build code tables
422
423 let InflateTable::Success { root, used } = inflate_table(
424 CodeType::Lens,
425 &state.lens[..state.nlen],
426 &mut state.len_codes,
427 10,
428 &mut state.work,
429 ) else {
430 state.mode = Mode::Bad;
431 state.bad("invalid literal/lengths set\0");
432 continue 'inf_leave;
433 };
434
435 state.len_table.codes = Codes::Len;
436 state.len_table.bits = root;
437 state.next = used;
438
439 let InflateTable::Success { root, used } = inflate_table(
440 CodeType::Dists,
441 &state.lens[state.nlen..][..state.ndist],
442 &mut state.dist_codes,
443 9,
444 &mut state.work,
445 ) else {
446 state.mode = Mode::Bad;
447 state.bad("invalid distances set\0");
448 continue 'inf_leave;
449 };
450
451 state.dist_table.bits = root;
452 state.dist_table.codes = Codes::Dist;
453 state.next += used;
454
455 state.mode = Mode::Len;
456 }
457 Mode::Len => {
458 if (have as usize) >= INFLATE_FAST_MIN_HAVE && left >= INFLATE_FAST_MIN_LEFT {
459 let mut bit_reader = BitReader::new(&[]);
460 unsafe { bit_reader.update_slice(next, have as usize) };
461 bit_reader.prime(bits, hold);
462
463 state.bit_reader = bit_reader;
464 state.writer = unsafe {
465 Writer::new_uninit_raw(
466 put.wrapping_sub(state.window.buffer_size() - left),
467 state.window.buffer_size() - left,
468 state.window.buffer_size(),
469 )
470 };
471
472 if state.window.have() < state.window.buffer_size() {
473 unsafe { state.window.set_have(state.window.buffer_size() - left) };
474 }
475
476 unsafe { inflate_fast_back(state) };
477
478 hold = state.bit_reader.hold();
479 bits = state.bit_reader.bits_in_buffer();
480
481 next = state.bit_reader.as_ptr();
482 have = state.bit_reader.bytes_remaining() as u32;
483
484 put = state.writer.next_out().cast();
485 left = state.writer.remaining();
486
487 continue 'inf_leave;
488 }
489
490 let len_table = match state.len_table.codes {
491 Codes::Fixed => &crate::inflate::inffixed_tbl::LENFIX[..],
492 Codes::Codes => &state.codes_codes,
493 Codes::Len => &state.len_codes,
494 Codes::Dist => &state.dist_codes,
495 };
496
497 // get a literal, length, or end-of-block code
498 let mut here;
499 loop {
500 here = len_table[bits!(state.len_table.bits) as usize];
501
502 if here.bits <= bits {
503 break;
504 }
505
506 pullbyte!();
507 }
508
509 if here.op != 0 && here.op & 0xf0 == 0 {
510 let last = here;
511 loop {
512 let tmp = bits!((last.bits + last.op) as usize) as u16;
513 here = len_table[(last.val + (tmp >> last.bits)) as usize];
514 if last.bits + here.bits <= bits {
515 break;
516 }
517
518 pullbyte!();
519 }
520
521 dropbits!(last.bits);
522 }
523
524 dropbits!(here.bits);
525 state.length = here.val as usize;
526
527 if here.op == 0 {
528 if here.val >= 0x20 && here.val < 0x7f {
529 traceln!("inflate: literal '{}'", here.val as u8 as char);
530 } else {
531 traceln!("inflate: literal {:#04x}", here.val);
532 }
533 room!();
534
535 unsafe {
536 *put = state.length as u8;
537 put = put.add(1)
538 }
539 left -= 1;
540
541 state.mode = Mode::Len;
542 continue;
543 } else if here.op & 32 != 0 {
544 // end of block
545
546 traceln!("inflate: end of block");
547
548 state.mode = Mode::Type;
549 continue;
550 } else if here.op & 64 != 0 {
551 state.mode = Mode::Bad;
552 state.bad("invalid literal/length code\0");
553 continue;
554 } else {
555 // length code
556 state.extra = (here.op & MAX_BITS) as usize;
557 }
558
559 // get extra bits, if any
560 if state.extra != 0 {
561 needbits!(state.extra);
562 state.length += bits!(state.extra) as usize;
563 dropbits!(state.extra as u8);
564 }
565 traceln!("inflate: length {}", state.length);
566
567 // get distance code
568 let mut here;
569 loop {
570 here = state.dist_table_get(bits!(state.dist_table.bits) as usize);
571 if here.bits <= bits {
572 break;
573 }
574
575 pullbyte!();
576 }
577
578 if here.op & 0xf0 == 0 {
579 let last = here;
580
581 loop {
582 here = state.dist_table_get(
583 last.val as usize
584 + ((bits!((last.bits + last.op) as usize) as usize) >> last.bits),
585 );
586
587 if last.bits + here.bits <= bits {
588 break;
589 }
590
591 pullbyte!();
592 }
593
594 dropbits!(last.bits);
595 }
596
597 dropbits!(here.bits);
598
599 if here.op & 64 != 0 {
600 state.mode = Mode::Bad;
601 state.bad("invalid distance code\0");
602 continue 'inf_leave;
603 }
604
605 state.offset = here.val as usize;
606
607 state.extra = (here.op & MAX_BITS) as usize;
608
609 let extra = state.extra;
610
611 if extra > 0 {
612 needbits!(extra);
613 state.offset += bits!(extra) as usize;
614 dropbits!(extra as u8);
615 }
616
617 if INFLATE_STRICT
618 && state.offset
619 > state.window.buffer_size()
620 - (if state.window.have() < state.window.buffer_size() {
621 left
622 } else {
623 0
624 })
625 {
626 state.mode = Mode::Bad;
627 state.bad("invalid distance too far back\0");
628 continue 'inf_leave;
629 }
630
631 traceln!("inflate: distance {}", state.offset);
632
633 loop {
634 room!();
635 let mut copy = state.window.buffer_size() - state.offset;
636 let mut from;
637
638 if copy < left {
639 from = put.wrapping_add(copy);
640 copy = left - copy;
641 } else {
642 from = put.wrapping_sub(state.offset);
643 copy = left;
644 }
645
646 copy = Ord::min(copy, state.length);
647 state.length -= copy;
648 left -= copy;
649
650 for _ in 0..copy {
651 unsafe {
652 *put = *from;
653 put = put.add(1);
654 from = from.add(1);
655 }
656 }
657
658 if state.length == 0 {
659 break;
660 }
661 }
662
663 continue 'inf_leave;
664 }
665 Mode::Done => {
666 ret = ReturnCode::StreamEnd;
667 break 'inf_leave;
668 }
669 Mode::Bad => {
670 ret = ReturnCode::DataError;
671 break 'inf_leave;
672 }
673
674 Mode::Head
675 | Mode::Flags
676 | Mode::Time
677 | Mode::Os
678 | Mode::ExLen
679 | Mode::Extra
680 | Mode::Name
681 | Mode::Comment
682 | Mode::HCrc
683 | Mode::Sync
684 | Mode::Mem
685 | Mode::Length
686 | Mode::TypeDo
687 | Mode::CopyBlock
688 | Mode::Check
689 | Mode::Len_
690 | Mode::Lit
691 | Mode::LenExt
692 | Mode::Dist
693 | Mode::DistExt
694 | Mode::Match
695 | Mode::LenLens
696 | Mode::CodeLens
697 | Mode::DictId
698 | Mode::Dict => {
699 // All other states should be unreachable, and return StreamError.
700 ret = ReturnCode::StreamError;
701 break 'inf_leave;
702 }
703 }
704 }
705
706 if left < state.window.buffer_size()
707 && unsafe {
708 out(
709 out_desc,
710 state.window.as_mut_ptr(),
711 state.window.buffer_size() as u32 - left as u32,
712 )
713 } != 0
714 && ret == ReturnCode::StreamEnd
715 {
716 ret = ReturnCode::BufError;
717 }
718
719 strm.next_in = next.cast_mut();
720 strm.avail_in = have;
721
722 ret
723 }
724
725 #[inline(always)]
726 unsafe fn inflate_fast_back(state: &mut State) {
727 let mut bit_reader = BitReader::new(&[]);
728 core::mem::swap(&mut bit_reader, &mut state.bit_reader);
729 debug_assert!(bit_reader.bytes_remaining() >= 15);
730
731 let mut writer = Writer::new(&mut []);
732 core::mem::swap(&mut writer, &mut state.writer);
733
734 let lcode = state.len_table_ref();
735 let dcode = state.dist_table_ref();
736
737 // IDEA: use const generics for the bits here?
738 let lmask = (1u64 << state.len_table.bits) - 1;
739 let dmask = (1u64 << state.dist_table.bits) - 1;
740
741 // TODO verify if this is relevant for us
742 let extra_safe = false;
743
744 let window_size = state.window.buffer_size();
745
746 let mut bad = None;
747
748 if bit_reader.bits_in_buffer() < 10 {
749 debug_assert!(bit_reader.bytes_remaining() >= 15);
750 // Safety: Caller ensured that bit_reader has >= 15 bytes available; refill only needs 8.
751 unsafe { bit_reader.refill() };
752 }
753 // We had at least 15 bytes in the slice, plus whatever was in the buffer. After filling the
754 // buffer from the slice, we now have at least 8 bytes remaining in the slice, plus a full buffer.
755 debug_assert!(
756 bit_reader.bytes_remaining() >= 8 && bit_reader.bytes_remaining_including_buffer() >= 15
757 );
758
759 'outer: loop {
760 // This condition is ensured above for the first iteration of the `outer` loop. For
761 // subsequent iterations, the loop continuation condition is
762 // `bit_reader.bytes_remaining_including_buffer() > 15`. And because the buffer
763 // contributes at most 7 bytes to the result of bit_reader.bytes_remaining_including_buffer(),
764 // that means that the slice contains at least 8 bytes.
765 debug_assert!(
766 bit_reader.bytes_remaining() >= 8
767 && bit_reader.bytes_remaining_including_buffer() >= 15
768 );
769
770 let mut here = {
771 let bits = bit_reader.bits_in_buffer();
772 let hold = bit_reader.hold();
773
774 // Safety: As described in the comments for the debug_assert at the start of
775 // the `outer` loop, it is guaranteed that `bit_reader.bytes_remaining() >= 8` here,
776 // which satisfies the safety precondition for `refill`. And, because the total
777 // number of bytes in `bit_reader`'s buffer plus its slice is at least 15, and
778 // `refill` moves at most 7 bytes from the slice to the buffer, the slice will still
779 // contain at least 8 bytes after this `refill` call.
780 unsafe { bit_reader.refill() };
781 // After the refill, there will be at least 8 bytes left in the bit_reader's slice.
782 debug_assert!(bit_reader.bytes_remaining() >= 8);
783
784 // in most cases, the read can be interleaved with the logic
785 // based on benchmarks this matters in practice. wild.
786 if bits as usize >= state.len_table.bits {
787 lcode[(hold & lmask) as usize]
788 } else {
789 lcode[(bit_reader.hold() & lmask) as usize]
790 }
791 };
792
793 if here.op == 0 {
794 writer.push(here.val as u8);
795 bit_reader.drop_bits(here.bits);
796 here = lcode[(bit_reader.hold() & lmask) as usize];
797
798 if here.op == 0 {
799 writer.push(here.val as u8);
800 bit_reader.drop_bits(here.bits);
801 here = lcode[(bit_reader.hold() & lmask) as usize];
802 }
803 }
804
805 'dolen: loop {
806 bit_reader.drop_bits(here.bits);
807 let op = here.op;
808
809 if op == 0 {
810 writer.push(here.val as u8);
811 } else if op & 16 != 0 {
812 let op = op & MAX_BITS;
813 let mut len = here.val + bit_reader.bits(op as usize) as u16;
814 bit_reader.drop_bits(op);
815
816 here = dcode[(bit_reader.hold() & dmask) as usize];
817
818 // we have two fast-path loads: 10+10 + 15+5 = 40,
819 // but we may need to refill here in the worst case
820 if bit_reader.bits_in_buffer() < MAX_BITS + MAX_DIST_EXTRA_BITS {
821 debug_assert!(bit_reader.bytes_remaining() >= 8);
822 // Safety: On the first iteration of the `dolen` loop, we can rely on the
823 // invariant documented for the previous `refill` call above: after that
824 // operation, `bit_reader.bytes_remining >= 8`, which satisfies the safety
825 // precondition for this call. For subsequent iterations, this invariant
826 // remains true because nothing else within the `dolen` loop consumes data
827 // from the slice.
828 unsafe { bit_reader.refill() };
829 }
830
831 'dodist: loop {
832 bit_reader.drop_bits(here.bits);
833 let op = here.op;
834
835 if op & 16 != 0 {
836 let op = op & MAX_BITS;
837 let dist = here.val + bit_reader.bits(op as usize) as u16;
838
839 if INFLATE_STRICT && dist as usize > state.dmax {
840 bad = Some("invalid distance too far back\0");
841 state.mode = Mode::Bad;
842 break 'outer;
843 }
844
845 bit_reader.drop_bits(op);
846
847 // max distance in output
848 let written = writer.len();
849
850 if dist as usize > written {
851 // copy fropm the window
852 if (dist as usize - written) > state.window.have() {
853 if state.flags.contains(Flags::SANE) {
854 bad = Some("invalid distance too far back\0");
855 state.mode = Mode::Bad;
856 break 'outer;
857 }
858
859 panic!("INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR")
860 }
861
862 let mut op = dist as usize - written;
863 let mut from;
864
865 let window_next = state.window.next();
866
867 if window_next == 0 {
868 // This case is hit when the window has just wrapped around
869 // by logic in `Window::extend`. It is special-cased because
870 // apparently this is quite common.
871 //
872 // the match is at the end of the window, even though the next
873 // position has now wrapped around.
874 from = window_size - op;
875 } else if window_next >= op {
876 // the standard case: a contiguous copy from the window, no wrapping
877 from = window_next - op;
878 } else {
879 // This case is hit when the window has recently wrapped around
880 // by logic in `Window::extend`.
881 //
882 // The match is (partially) at the end of the window
883 op -= window_next;
884 from = window_size - op;
885
886 if op < len as usize {
887 // This case is hit when part of the match is at the end of the
888 // window, and part of it has wrapped around to the start. Copy
889 // the end section here, the start section will be copied below.
890 len -= op as u16;
891 writer.extend_from_window_back(&state.window, from..from + op);
892 from = 0;
893 op = window_next;
894 }
895 }
896
897 let copy = Ord::min(op, len as usize);
898 writer.extend_from_window_back(&state.window, from..from + copy);
899
900 if op < len as usize {
901 // here we need some bytes from the output itself
902 writer.copy_match_back(dist as usize, len as usize - op);
903 }
904 } else if extra_safe {
905 todo!()
906 } else {
907 writer.copy_match_back(dist as usize, len as usize)
908 }
909 } else if (op & 64) == 0 {
910 // 2nd level distance code
911 here = dcode[(here.val + bit_reader.bits(op as usize) as u16) as usize];
912 continue 'dodist;
913 } else {
914 bad = Some("invalid distance code\0");
915 state.mode = Mode::Bad;
916 break 'outer;
917 }
918
919 break 'dodist;
920 }
921 } else if (op & 64) == 0 {
922 // 2nd level length code
923 here = lcode[(here.val + bit_reader.bits(op as usize) as u16) as usize];
924 continue 'dolen;
925 } else if op & 32 != 0 {
926 // end of block
927 state.mode = Mode::Type;
928 break 'outer;
929 } else {
930 bad = Some("invalid literal/length code\0");
931 state.mode = Mode::Bad;
932 break 'outer;
933 }
934
935 break 'dolen;
936 }
937
938 // For normal `inflate`, include the bits in the bit_reader buffer in the count of available bytes.
939 let remaining = bit_reader.bytes_remaining();
940 if remaining >= INFLATE_FAST_MIN_HAVE && writer.remaining() >= INFLATE_FAST_MIN_LEFT {
941 continue;
942 }
943
944 break 'outer;
945 }
946
947 // return unused bytes (on entry, bits < 8, so in won't go too far back)
948 bit_reader.return_unused_bytes();
949
950 state.bit_reader = bit_reader;
951 state.writer = writer;
952
953 if let Some(error_message) = bad {
954 debug_assert!(matches!(state.mode, Mode::Bad));
955 state.bad(error_message);
956 }
957 }
958
959 pub fn back_end<'a>(strm: &'a mut InflateStream<'a>) {
960 // With infback the window is user-supplied, so we mustn't try to free it.
961 let _ = core::mem::replace(&mut strm.state.window, Window::empty());
962 crate::inflate::end(strm);
963 }