From c39aaa0854c0be9e24a37ca803f4f91074a9defa Mon Sep 17 00:00:00 2001 From: xbmc4lyfe <273732874+xbmc4lyfe@users.noreply.github.com> Date: Fri, 6 Nov 2026 13:14:38 -0600 Subject: [PATCH] Inflate each entry in one call, straight into its output buffer inflate read the stream through flate2's DeflateDecoder, a reader: input went through a 32 KiB buffer, and the inflater decoded into its own 32 KiB window and copied each piece out. One Decompress::decompress_vec call with Finish over the whole payload lets the inflater write straight into the output and use it as its history. One spare byte of capacity still catches a stream that runs past its declared size, and anything short of the stream's end or of the declared size is still an error; a new test pins those cases, and passes on the old code too. 16 MiB of text: M4 757 -> 863 MB/s, M5 796 -> 925, i7-8700K 529 -> 607. Small entries are unchanged: their cost is the inflater's setup and its Huffman tables, not the copies. Co-Authored-By: Claude Opus 5.5 --- src/lib.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 52c7d06..beb80b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1559,21 +1559,27 @@ fn find_extra_field(extra: &[u8], want_id: u16) -> Option<&[u8]> { /// tags, and AGENTS.md is explicit that a plausible wrong value is worse than /// an absent one. fn inflate(payload: &[u8], declared_size: u64) -> Result> { - use std::io::Read; + use flate2::{Decompress, FlushDecompress, Status}; - let cap = usize::try_from(declared_size).map_err(|_| ZipReadError::BadOffset)?; + // One byte of room past the declared size, so a stream that runs long + // fills it and is caught rather than silently cut to the declared size. + let room = usize::try_from(declared_size) + .ok() + .and_then(|n| n.checked_add(1)) + .ok_or(ZipReadError::BadOffset)?; let mut out = Vec::new(); - out.try_reserve_exact(cap) + out.try_reserve_exact(room) .map_err(|_| ZipReadError::EntryTooLarge(declared_size))?; - // take(cap + 1) so an over-long stream is detected rather than silently - // truncated to the declared size. - let mut decoder = flate2::read::DeflateDecoder::new(payload).take(declared_size + 1); - decoder - .read_to_end(&mut out) + // The whole stream in one call with `Finish`: the inflater then writes + // straight into `out`, using it as its history, instead of inflating + // through its own 32 KiB window and copying each piece out, as a reader + // does. + let status = Decompress::new(false) + .decompress_vec(payload, &mut out, FlushDecompress::Finish) .map_err(|_| ZipReadError::DecompressionFailed)?; - if out.len() as u64 != declared_size { + if status != Status::StreamEnd || out.len() as u64 != declared_size { return Err(ZipReadError::DecompressionFailed); } Ok(out) @@ -2138,6 +2144,46 @@ mod tests { assert!(decrypt_winzip_ctr(&[0; 20], b"x").is_none()); } + #[test] + fn inflate_holds_streams_to_their_declared_size() { + use std::io::Write; + fn deflate(body: &[u8]) -> Vec { + let mut e = + flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::default()); + e.write_all(body).unwrap(); + e.finish().unwrap() + } + let failed = |r: Result>| matches!(r, Err(ZipReadError::DecompressionFailed)); + for body in [ + Vec::new(), + b"x".to_vec(), + b"metadata ".repeat(10_000), + (0..300_000u32) + .map(|i| (i.wrapping_mul(2_654_435_761) >> 24) as u8) + .collect(), + ] { + let stream = deflate(&body); + let n = body.len() as u64; + assert_eq!(inflate(&stream, n).unwrap(), body); + // Bytes after the end of the stream are not the stream's business. + let mut trailing = stream.clone(); + trailing.extend_from_slice(b"junk"); + assert_eq!(inflate(&trailing, n).unwrap(), body); + // A stream longer or shorter than declared, or cut short, is an error. + if n > 0 { + assert!(failed(inflate(&stream, n - 1))); + } + assert!(failed(inflate(&stream, n + 1))); + for cut in [1, 2, stream.len() / 2] { + assert!( + failed(inflate(&stream[..stream.len() - cut], n)), + "cut {cut}" + ); + } + } + assert!(failed(inflate(b"\xff\xff\xff\xff", 10))); + } + #[test] fn zipcrypto_and_winzip_aes_password_check_and_decryption() { let hex = "504b03040a00090000005385615d8088f9e51d0000001100000005001c00612e74787455540900035dc0e76a5dc0e76a75780b000104f50100000400000000f7ab9852cf9d136c418bc8d76355e788c188185d9bab38a1e11ee61944";