From 76bd03756c7c45b691e3381029855157e4d46884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 13:39:56 +0300 Subject: [PATCH] U20: ec-inflate raw-only entry point removed (callers use inflate_into with Format::Raw; no external user); slice-to-array expects replaced by first_chunk --- .../fuzz/fuzz_targets/inflate_diff.rs | 5 ++-- .../fuzz/fuzz_targets/inflate_raw.rs | 4 +-- crates/ec-inflate/src/bits.rs | 4 +-- crates/ec-inflate/src/deflate.rs | 7 +++++- crates/ec-inflate/src/lib.rs | 25 ++++++------------- crates/ec-inflate/tests/inflate.rs | 7 +++++- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/crates/ec-inflate/fuzz/fuzz_targets/inflate_diff.rs b/crates/ec-inflate/fuzz/fuzz_targets/inflate_diff.rs index ab682dab..fb1b5164 100644 --- a/crates/ec-inflate/fuzz/fuzz_targets/inflate_diff.rs +++ b/crates/ec-inflate/fuzz/fuzz_targets/inflate_diff.rs @@ -1,6 +1,6 @@ #![no_main] -use ec_inflate::{inflate, inflate_zlib}; +use ec_inflate::{Format, inflate_into, inflate_zlib}; use libfuzzer_sys::fuzz_target; const LIMIT: usize = 16 << 20; @@ -12,8 +12,9 @@ fuzz_target!(|data: &[u8]| { return; }; let (ours, theirs) = if sel & 1 == 0 { + let mut out = Vec::new(); ( - inflate(body, LIMIT), + inflate_into(body, &mut out, LIMIT, Format::Raw).map(|()| out), miniz_oxide::inflate::decompress_to_vec_with_limit(body, LIMIT), ) } else { diff --git a/crates/ec-inflate/fuzz/fuzz_targets/inflate_raw.rs b/crates/ec-inflate/fuzz/fuzz_targets/inflate_raw.rs index eecc15d5..c973832a 100644 --- a/crates/ec-inflate/fuzz/fuzz_targets/inflate_raw.rs +++ b/crates/ec-inflate/fuzz/fuzz_targets/inflate_raw.rs @@ -1,9 +1,9 @@ #![no_main] -use ec_inflate::inflate; +use ec_inflate::{Format, inflate_into}; use libfuzzer_sys::fuzz_target; // Whole input is one raw DEFLATE stream, capped at 16 MiB of output. fuzz_target!(|data: &[u8]| { - let _ = inflate(data, 16 << 20); + let _ = inflate_into(data, &mut Vec::new(), 16 << 20, Format::Raw); }); diff --git a/crates/ec-inflate/src/bits.rs b/crates/ec-inflate/src/bits.rs index 13e238f4..4fa0aa54 100644 --- a/crates/ec-inflate/src/bits.rs +++ b/crates/ec-inflate/src/bits.rs @@ -39,8 +39,8 @@ impl<'a> Bits<'a> { /// most symbols. #[inline] pub(crate) fn refill(&mut self) { - if let Some(next) = self.src.get(self.pos..self.pos + 8) { - let bytes = u64::from_le_bytes(next.try_into().expect("an eight byte slice")); + if let Some(next) = self.src.get(self.pos..).and_then(<[u8]>::first_chunk::<8>) { + let bytes = u64::from_le_bytes(*next); self.buf |= bytes << self.count; self.pos += ((63 - self.count) >> 3) as usize; self.count |= 56; diff --git a/crates/ec-inflate/src/deflate.rs b/crates/ec-inflate/src/deflate.rs index 75b3066d..98edb40a 100644 --- a/crates/ec-inflate/src/deflate.rs +++ b/crates/ec-inflate/src/deflate.rs @@ -205,7 +205,7 @@ fn stored_blocks(input: &[u8]) -> Vec { #[cfg(test)] mod tests { use super::*; - use crate::{inflate, inflate_zlib}; + use crate::inflate_zlib; fn pseudo_random(n: usize, seed: u32) -> Vec { let mut s = seed; @@ -217,6 +217,11 @@ mod tests { .collect() } + fn inflate(raw: &[u8], limit: usize) -> Result, crate::Error> { + let mut out = Vec::new(); + crate::inflate_into(raw, &mut out, limit, crate::Format::Raw).map(|()| out) + } + #[test] fn empty_and_tiny_inputs_round_trip() { for input in [&b""[..], b"a", b"ab", b"abc", b"abcabcabc"] { diff --git a/crates/ec-inflate/src/lib.rs b/crates/ec-inflate/src/lib.rs index 57ba2107..32942b85 100644 --- a/crates/ec-inflate/src/lib.rs +++ b/crates/ec-inflate/src/lib.rs @@ -1,9 +1,9 @@ //! DEFLATE (RFC 1951) and zlib (RFC 1950) decompression. //! -//! Two entry points, both one-shot over a complete buffer: [`inflate`] for a -//! raw DEFLATE stream (what PNG's `IDAT` chain and Matroska header stripping -//! hand over) and [`inflate_zlib`] for the same thing inside a zlib wrapper, -//! header checked and Adler-32 verified. +//! Two entry points, both one-shot over a complete buffer: [`inflate_zlib`] for a +//! zlib stream, header checked and Adler-32 verified, and [`inflate_into`] for +//! either wrapper, with [`Format::Raw`] for a bare DEFLATE stream (what +//! Matroska header stripping hands over). //! //! Contracts worth knowing before implementing against this crate: //! @@ -117,13 +117,6 @@ pub enum Format { Zlib, } -/// Decompress a raw DEFLATE stream, producing at most `limit` bytes. -pub fn inflate(input: &[u8], limit: usize) -> Result, Error> { - let mut out = Vec::new(); - inflate_into(input, &mut out, limit, Format::Raw)?; - Ok(out) -} - /// Decompress a zlib stream, producing at most `limit` bytes. /// /// The two header bytes are checked and the Adler-32 trailer is verified @@ -153,13 +146,11 @@ pub fn inflate_into( let mut bits = Bits::new(body); let end = inflate::blocks(&mut bits, out, limit)?; if format == Format::Zlib { - let trailer: [u8; 4] = body + let trailer = body .get(end..) - .and_then(|rest| rest.get(..4)) - .ok_or(Error::Truncated)? - .try_into() - .expect("a four byte slice"); - let expected = u32::from_be_bytes(trailer); + .and_then(<[u8]>::first_chunk::<4>) + .ok_or(Error::Truncated)?; + let expected = u32::from_be_bytes(*trailer); let actual = adler32(&out[start..]); if expected != actual { return Err(Error::Adler32Mismatch { expected, actual }); diff --git a/crates/ec-inflate/tests/inflate.rs b/crates/ec-inflate/tests/inflate.rs index 379380b2..af8b5381 100644 --- a/crates/ec-inflate/tests/inflate.rs +++ b/crates/ec-inflate/tests/inflate.rs @@ -7,12 +7,17 @@ use std::io::Write; -use ec_inflate::{Error, Format, inflate, inflate_into, inflate_zlib}; +use ec_inflate::{Error, Format, inflate_into, inflate_zlib}; use flate2::Compression; use flate2::write::{DeflateEncoder, ZlibEncoder}; const NO_LIMIT: usize = usize::MAX; +fn inflate(input: &[u8], limit: usize) -> Result, Error> { + let mut out = Vec::new(); + inflate_into(input, &mut out, limit, Format::Raw).map(|()| out) +} + /// xorshift64*, so the "random" inputs are the same on every run. fn pseudo_random(seed: u64, len: usize) -> Vec { let mut state = seed | 1;