From 8d1467d91c6aa102682a0e84e286c66386074ae3 Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 6 Jan 2025 18:07:08 +0000 Subject: [PATCH 01/32] Fix cfgs --- src/ffi/c.rs | 56 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/src/ffi/c.rs b/src/ffi/c.rs index 226fe0e3..eeca73ef 100644 --- a/src/ffi/c.rs +++ b/src/ffi/c.rs @@ -50,27 +50,38 @@ impl Default for StreamWrapper { reserved: 0, opaque: ptr::null_mut(), state: ptr::null_mut(), - #[cfg(all( - feature = "any_zlib", - not(any(feature = "cloudflare-zlib-sys", feature = "libz-rs-sys")) + + #[cfg(any( + // zlib-ng + feature = "zlib-ng", + // libz-sys + all(not(feature = "cloudflare_zlib"), not(feature = "zlib-ng"), not(feature = "zlib-rs")) ))] zalloc: allocator::zalloc, - #[cfg(all( - feature = "any_zlib", - not(any(feature = "cloudflare-zlib-sys", feature = "libz-rs-sys")) + #[cfg(any( + // zlib-ng + feature = "zlib-ng", + // libz-sys + all(not(feature = "cloudflare_zlib"), not(feature = "zlib-ng"), not(feature = "zlib-rs")) ))] zfree: allocator::zfree, - #[cfg(all(feature = "any_zlib", feature = "cloudflare-zlib-sys"))] + #[cfg( + // cloudflare-zlib + all(feature = "cloudflare_zlib", not(feature = "zlib-rs"), not(feature = "zlib-ng")), + )] zalloc: Some(allocator::zalloc), - #[cfg(all(feature = "any_zlib", feature = "cloudflare-zlib-sys"))] + #[cfg( + // cloudflare-zlib + all(feature = "cloudflare_zlib", not(feature = "zlib-rs"), not(feature = "zlib-ng")), + )] zfree: Some(allocator::zfree), // for zlib-rs, it is most efficient to have it provide the allocator. // The libz-rs-sys dependency is configured to use the rust system allocator - #[cfg(all(feature = "any_zlib", feature = "libz-rs-sys"))] + #[cfg(all(feature = "zlib-rs", not(feature = "zlib-ng")))] zalloc: None, - #[cfg(all(feature = "any_zlib", feature = "libz-rs-sys"))] + #[cfg(all(feature = "zlib-rs", not(feature = "zlib-ng")))] zfree: None, })), } @@ -87,7 +98,14 @@ impl Drop for StreamWrapper { } } -#[cfg(all(feature = "any_zlib", not(feature = "libz-rs-sys")))] +#[cfg(any( + // zlib-ng + feature = "zlib-ng", + // cloudflare-zlib + all(feature = "cloudflare_zlib", not(feature = "zlib-rs"), not(feature = "zlib-ng")), + // libz-sys + all(not(feature = "cloudflare_zlib"), not(feature = "zlib-ng"), not(feature = "zlib-rs")), +))] mod allocator { use super::*; @@ -405,17 +423,19 @@ mod c_backend { #[cfg(feature = "zlib-ng")] use libz_ng_sys as libz; - #[cfg(all(not(feature = "zlib-ng"), feature = "zlib-rs"))] + #[cfg(all(feature = "zlib-rs", not(feature = "zlib-ng")))] use libz_rs_sys as libz; - #[cfg(all(not(feature = "zlib-ng"), feature = "cloudflare_zlib"))] + #[cfg( + // cloudflare-zlib + all(feature = "cloudflare_zlib", not(feature = "zlib-rs"), not(feature = "zlib-ng")), + )] use cloudflare_zlib_sys as libz; - #[cfg(all( - not(feature = "cloudflare_zlib"), - not(feature = "zlib-ng"), - not(feature = "zlib-rs") - ))] + #[cfg( + // libz-sys + all(not(feature = "cloudflare_zlib"), not(feature = "zlib-ng"), not(feature = "zlib-rs")), + )] use libz_sys as libz; pub use libz::deflate as mz_deflate; From fe579094f36b609b7695af88931af6d8b69e5885 Mon Sep 17 00:00:00 2001 From: oyvindln Date: Mon, 13 Jan 2025 16:44:54 +0100 Subject: [PATCH 02/32] fix(ci): update to use new wasi target --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e3fc98a4..a76829bc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -68,7 +68,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - target: [wasm32-unknown-unknown, wasm32-wasi] + target: [wasm32-unknown-unknown, wasm32-wasip1] steps: - uses: actions/checkout@v4 - name: Install Rust From c0d2fff914fe95d47960d721b4a59aac4baac7cd Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Sat, 25 Jan 2025 11:54:15 -0500 Subject: [PATCH 03/32] Implement `Clone` for `CompressError` and `DecompressError` --- src/ffi/c.rs | 2 +- src/ffi/rust.rs | 2 +- src/mem.rs | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ffi/c.rs b/src/ffi/c.rs index eeca73ef..173701bd 100644 --- a/src/ffi/c.rs +++ b/src/ffi/c.rs @@ -8,7 +8,7 @@ use std::ptr; use super::*; use crate::mem; -#[derive(Default)] +#[derive(Clone, Default)] pub struct ErrorMessage(Option<&'static str>); impl ErrorMessage { diff --git a/src/ffi/rust.rs b/src/ffi/rust.rs index bed6629a..7f40fe78 100644 --- a/src/ffi/rust.rs +++ b/src/ffi/rust.rs @@ -17,7 +17,7 @@ use super::*; use crate::mem; // miniz_oxide doesn't provide any error messages (yet?) -#[derive(Default)] +#[derive(Clone, Default)] pub struct ErrorMessage; impl ErrorMessage { diff --git a/src/mem.rs b/src/mem.rs index 86fa8d3b..3ce3d140 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -109,7 +109,7 @@ pub enum FlushDecompress { } /// The inner state for an error when decompressing -#[derive(Debug)] +#[derive(Clone, Debug)] pub(crate) enum DecompressErrorInner { General { msg: ErrorMessage }, NeedsDictionary(u32), @@ -117,7 +117,7 @@ pub(crate) enum DecompressErrorInner { /// Error returned when a decompression object finds that the input stream of /// bytes was not a valid input stream of bytes. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct DecompressError(pub(crate) DecompressErrorInner); impl DecompressError { @@ -147,7 +147,7 @@ pub(crate) fn decompress_need_dict(adler: u32) -> Result /// Error returned when a compression object is used incorrectly or otherwise /// generates an error. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct CompressError { pub(crate) msg: ErrorMessage, } From 4029c3f3effd2345bfe6b73324ddf1bec0d39673 Mon Sep 17 00:00:00 2001 From: Maxim Evtush <154841002+maximevtush@users.noreply.github.com> Date: Wed, 29 Jan 2025 10:27:06 +0100 Subject: [PATCH 04/32] Update LICENSE-MIT --- LICENSE-MIT | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE-MIT b/LICENSE-MIT index 39e0ed66..297ed8c1 100644 --- a/LICENSE-MIT +++ b/LICENSE-MIT @@ -1,4 +1,4 @@ -Copyright (c) 2014 Alex Crichton +Copyright (c) 2025 Alex Crichton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated From d3f9e7aa500a3db16558348d96be4e18e4edcd2b Mon Sep 17 00:00:00 2001 From: Maxim Evtush <154841002+maximevtush@users.noreply.github.com> Date: Thu, 30 Jan 2025 10:11:59 +0100 Subject: [PATCH 05/32] Update LICENSE-MIT --- LICENSE-MIT | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE-MIT b/LICENSE-MIT index 297ed8c1..0bb8a971 100644 --- a/LICENSE-MIT +++ b/LICENSE-MIT @@ -1,4 +1,4 @@ -Copyright (c) 2025 Alex Crichton +Copyright (c) 2014-2025 Alex Crichton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated From ea7932264b48d5a431203ae49a2e94c5f2947d88 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 8 Feb 2025 19:46:30 +0100 Subject: [PATCH 06/32] Disable cloudflare testing on CI ubuntu-latest and Windows On MacOS it works, and it's unclear why that happens. For now just give up and make it work. --- .github/workflows/main.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a76829bc..cb89776c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -45,7 +45,8 @@ jobs: - run: cargo test --features zlib-rs --no-default-features if: matrix.build != 'mingw' - run: cargo test --features cloudflare_zlib --no-default-features - if: matrix.build != 'mingw' + # cloudflare doesn't seem to work on CI anymore for some reason, it's not reproducible locally. + if: matrix.build != 'mingw' && matrix.os != 'ubuntu-latest' && matrix.os != 'windows-2022' - run: | if ! cargo check --no-default-features 2>&1 | grep "You need to choose"; then echo "expected message stating a zlib backend must be chosen" From 2468c49f685988f736542666b049631c5e719e6c Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Sat, 8 Feb 2025 23:52:59 +0200 Subject: [PATCH 07/32] feat: replace custom u16 le parser with existent rust method --- src/gz/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gz/mod.rs b/src/gz/mod.rs index 31a69611..157c517c 100644 --- a/src/gz/mod.rs +++ b/src/gz/mod.rs @@ -163,7 +163,7 @@ impl GzHeaderParser { if let Some(crc) = crc { crc.update(buffer); } - let xlen = parse_le_u16(&buffer); + let xlen = parse_le_u16(buffer); self.header.extra = Some(vec![0; xlen as usize]); self.state = GzHeaderState::Extra(crc.take(), 0); } else { @@ -209,7 +209,7 @@ impl GzHeaderParser { while (*count as usize) < buffer.len() { *count += read_into(r, &mut buffer[*count as usize..])? as u8; } - let stored_crc = parse_le_u16(&buffer); + let stored_crc = parse_le_u16(buffer); let calced_crc = crc.sum() as u16; if stored_crc != calced_crc { return Err(corrupt()); @@ -277,7 +277,7 @@ fn read_to_nul(r: &mut R, buffer: &mut Vec) -> Result<()> { } fn parse_le_u16(buffer: &[u8; 2]) -> u16 { - (buffer[0] as u16) | ((buffer[1] as u16) << 8) + u16::from_le_bytes(*buffer) } fn bad_header() -> Error { From 7d66bf55f974f7adb63d9abba62fbccecd27aacb Mon Sep 17 00:00:00 2001 From: Jonathan Giddy Date: Sun, 9 Feb 2025 20:23:42 +0000 Subject: [PATCH 08/32] Update minimum `cloudflare-zlib-sys` to 0.3.5 Avoid 0.3.4 which caused failures due to bad CPU feature detection. --- .github/workflows/main.yml | 3 +-- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cb89776c..a76829bc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -45,8 +45,7 @@ jobs: - run: cargo test --features zlib-rs --no-default-features if: matrix.build != 'mingw' - run: cargo test --features cloudflare_zlib --no-default-features - # cloudflare doesn't seem to work on CI anymore for some reason, it's not reproducible locally. - if: matrix.build != 'mingw' && matrix.os != 'ubuntu-latest' && matrix.os != 'windows-2022' + if: matrix.build != 'mingw' - run: | if ! cargo check --no-default-features 2>&1 | grep "You need to choose"; then echo "expected message stating a zlib backend must be chosen" diff --git a/Cargo.toml b/Cargo.toml index 6d648210..8379f7a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ libz-sys = { version = "1.1.20", optional = true, default-features = false } libz-ng-sys = { version = "1.1.16", optional = true } # this matches the default features, but we don't want to depend on the default features staying the same libz-rs-sys = { version = "0.4.0", optional = true, default-features = false, features = ["std", "rust-allocator"] } -cloudflare-zlib-sys = { version = "0.3.0", optional = true } +cloudflare-zlib-sys = { version = "0.3.5", optional = true } miniz_oxide = { version = "0.8.0", optional = true, default-features = false, features = ["with-alloc"] } crc32fast = "1.2.0" From 3c299bd57533eea0e92411e6b1a61e88651b245f Mon Sep 17 00:00:00 2001 From: Jonathan Giddy Date: Mon, 10 Feb 2025 21:58:56 +0000 Subject: [PATCH 09/32] Increase minimum compiler version to 1.67 --- .github/workflows/main.yml | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a76829bc..3cf9af6d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -82,7 +82,7 @@ jobs: matrix: os: [windows-2022, macos-latest, ubuntu-latest] env: - version: 1.63.0 + version: 1.67.0 steps: - uses: actions/checkout@v4 - name: Install Rust (rustup) @@ -105,7 +105,7 @@ jobs: runs-on: ubuntu-latest env: # If this is changed to pass tests, then set `rust-version` in `Cargo.toml` to the same version. - version: 1.56.1 + version: 1.67.0 steps: - uses: actions/checkout@v4 - name: Install Rust (rustup) diff --git a/Cargo.toml b/Cargo.toml index 8379f7a7..b60b83bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ version = "1.0.35" edition = "2018" license = "MIT OR Apache-2.0" readme = "README.md" -rust-version = "1.56.1" +rust-version = "1.67.0" keywords = ["gzip", "deflate", "zlib", "zlib-ng", "encoding"] categories = ["compression", "api-bindings"] repository = "https://github.com/rust-lang/flate2-rs" From b7bd31ad974a2e5d10c8409282a09b2ea4e8a2e6 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Wed, 12 Feb 2025 16:42:30 +0100 Subject: [PATCH 10/32] Change internal bounds from `R: Read` to `R: BufRead` --- src/gz/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gz/mod.rs b/src/gz/mod.rs index 157c517c..5b0e986d 100644 --- a/src/gz/mod.rs +++ b/src/gz/mod.rs @@ -120,7 +120,7 @@ impl GzHeaderParser { } } - fn parse<'a, R: Read>(&mut self, r: &'a mut R) -> Result<()> { + fn parse<'a, R: BufRead>(&mut self, r: &'a mut R) -> Result<()> { loop { match &mut self.state { GzHeaderState::Start(count, buffer) => { @@ -253,7 +253,7 @@ fn read_into(r: &mut R, buffer: &mut [u8]) -> Result { } // Read `r` up to the first nul byte, pushing non-nul bytes to `buffer`. -fn read_to_nul(r: &mut R, buffer: &mut Vec) -> Result<()> { +fn read_to_nul(r: &mut R, buffer: &mut Vec) -> Result<()> { let mut bytes = r.bytes(); loop { match bytes.next().transpose()? { From a0409e0bea9ea7db4a1faa9934ac9c88edc309bc Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Wed, 12 Feb 2025 22:00:35 +0200 Subject: [PATCH 11/32] feat: reduce CrcReader::sum calls in GzEncoder::read_footer --- src/gz/bufread.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/gz/bufread.rs b/src/gz/bufread.rs index e01e5284..fee11e03 100644 --- a/src/gz/bufread.rs +++ b/src/gz/bufread.rs @@ -82,11 +82,12 @@ impl GzEncoder { return Ok(0); } let crc = self.inner.get_ref().crc(); + let calced_crc = crc.sum(); let ref arr = [ - (crc.sum() >> 0) as u8, - (crc.sum() >> 8) as u8, - (crc.sum() >> 16) as u8, - (crc.sum() >> 24) as u8, + (calced_crc >> 0) as u8, + (calced_crc >> 8) as u8, + (calced_crc >> 16) as u8, + (calced_crc >> 24) as u8, (crc.amount() >> 0) as u8, (crc.amount() >> 8) as u8, (crc.amount() >> 16) as u8, From 93973acb77f9d58c3f943c50506bc4d77ff15aaa Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Wed, 12 Feb 2025 22:13:26 +0200 Subject: [PATCH 12/32] feat: use to_le_bytes, remove explicit ref in declaration --- src/gz/bufread.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/gz/bufread.rs b/src/gz/bufread.rs index fee11e03..bca0e64d 100644 --- a/src/gz/bufread.rs +++ b/src/gz/bufread.rs @@ -82,18 +82,18 @@ impl GzEncoder { return Ok(0); } let crc = self.inner.get_ref().crc(); - let calced_crc = crc.sum(); - let ref arr = [ - (calced_crc >> 0) as u8, - (calced_crc >> 8) as u8, - (calced_crc >> 16) as u8, - (calced_crc >> 24) as u8, + let calced_crc = crc.sum().to_le_bytes(); + let arr = [ + calced_crc[0], + calced_crc[1], + calced_crc[2], + calced_crc[3], (crc.amount() >> 0) as u8, (crc.amount() >> 8) as u8, (crc.amount() >> 16) as u8, (crc.amount() >> 24) as u8, ]; - Ok(copy(into, arr, &mut self.pos)) + Ok(copy(into, &arr, &mut self.pos)) } } From 1ccce6e78a8f4ddcabcf8257a4d65db7baf81aac Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Wed, 12 Feb 2025 22:20:55 +0200 Subject: [PATCH 13/32] feat: rename calced_crc to calced_crc_bytes --- src/gz/bufread.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/gz/bufread.rs b/src/gz/bufread.rs index bca0e64d..5cfc1008 100644 --- a/src/gz/bufread.rs +++ b/src/gz/bufread.rs @@ -82,12 +82,12 @@ impl GzEncoder { return Ok(0); } let crc = self.inner.get_ref().crc(); - let calced_crc = crc.sum().to_le_bytes(); + let calced_crc_bytes = crc.sum().to_le_bytes(); let arr = [ - calced_crc[0], - calced_crc[1], - calced_crc[2], - calced_crc[3], + calced_crc_bytes[0], + calced_crc_bytes[1], + calced_crc_bytes[2], + calced_crc_bytes[3], (crc.amount() >> 0) as u8, (crc.amount() >> 8) as u8, (crc.amount() >> 16) as u8, From 47bcd336f0b77f31b8cb47cfca99d3cc7fde5618 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Wed, 12 Feb 2025 22:35:36 +0200 Subject: [PATCH 14/32] docs: fix spelling mistake in flate2::zlib::write::ZlibDecoder --- src/zlib/write.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zlib/write.rs b/src/zlib/write.rs index 1d629b3c..77c1c899 100644 --- a/src/zlib/write.rs +++ b/src/zlib/write.rs @@ -180,7 +180,7 @@ impl Read for ZlibEncoder { /// This structure implements a [`Write`] and will emit a stream of decompressed /// data when fed a stream of compressed data. /// -/// After decoding a single member of the ZLIB data this writer will return the number of bytes up to +/// After decoding a single member of the ZLIB data this writer will return the number of bytes up /// to the end of the ZLIB member and subsequent writes will return Ok(0) allowing the caller to /// handle any data following the ZLIB member. /// From da68b825932ac0ebe598219d85a04ce9d4458a88 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Wed, 12 Feb 2025 23:28:52 +0200 Subject: [PATCH 15/32] feat: replace manual copy loop with rust-provided function --- src/gz/bufread.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/gz/bufread.rs b/src/gz/bufread.rs index e01e5284..dc685e50 100644 --- a/src/gz/bufread.rs +++ b/src/gz/bufread.rs @@ -10,9 +10,7 @@ use crate::Compression; fn copy(into: &mut [u8], from: &[u8], pos: &mut usize) -> usize { let min = cmp::min(into.len(), from.len() - *pos); - for (slot, val) in into.iter_mut().zip(from[*pos..*pos + min].iter()) { - *slot = *val; - } + into[..min].copy_from_slice(&from[*pos..*pos + min]); *pos += min; min } From 84ef29ce01616b97bded31639fcc62ccb0b0ee04 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Wed, 12 Feb 2025 23:58:30 +0200 Subject: [PATCH 16/32] feat: remove explicit clone impl for Crc --- src/crc.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/crc.rs b/src/crc.rs index 16f56019..0687d747 100644 --- a/src/crc.rs +++ b/src/crc.rs @@ -8,7 +8,7 @@ use crc32fast::Hasher; /// The CRC calculated by a [`CrcReader`]. /// /// [`CrcReader`]: struct.CrcReader.html -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Crc { amt: u32, hasher: Hasher, @@ -23,12 +23,6 @@ pub struct CrcReader { crc: Crc, } -impl Default for Crc { - fn default() -> Self { - Self::new() - } -} - impl Crc { /// Create a new CRC. pub fn new() -> Crc { From 872ab26f75ac7480c72869d435a4dc9f3374d2f8 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 00:09:29 +0200 Subject: [PATCH 17/32] feat: remove explicit clone impl on GzHeaderState and GzBuilder --- src/gz/mod.rs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/gz/mod.rs b/src/gz/mod.rs index 157c517c..eef4636b 100644 --- a/src/gz/mod.rs +++ b/src/gz/mod.rs @@ -87,7 +87,7 @@ impl GzHeader { } } -#[derive(Debug)] +#[derive(Debug, Default)] pub enum GzHeaderState { Start(u8, [u8; 10]), Xlen(Option>, u8, [u8; 2]), @@ -95,15 +95,10 @@ pub enum GzHeaderState { Filename(Option>), Comment(Option>), Crc(Option>, u8, [u8; 2]), + #[default] Complete, } -impl Default for GzHeaderState { - fn default() -> Self { - Self::Complete - } -} - #[derive(Debug, Default)] pub struct GzHeaderParser { state: GzHeaderState, @@ -317,7 +312,7 @@ fn corrupt() -> Error { /// # Ok(()) /// # } /// ``` -#[derive(Debug)] +#[derive(Debug, Default)] pub struct GzBuilder { extra: Option>, filename: Option, @@ -326,12 +321,6 @@ pub struct GzBuilder { mtime: u32, } -impl Default for GzBuilder { - fn default() -> Self { - Self::new() - } -} - impl GzBuilder { /// Create a new blank builder with no header by default. pub fn new() -> GzBuilder { From c3ec0f8d5333c9fa8510ee1577e57de699b851d1 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 00:20:49 +0200 Subject: [PATCH 18/32] feat: use default on new --- src/crc.rs | 7 ++----- src/gz/mod.rs | 8 +------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/crc.rs b/src/crc.rs index 0687d747..991cc957 100644 --- a/src/crc.rs +++ b/src/crc.rs @@ -25,11 +25,8 @@ pub struct CrcReader { impl Crc { /// Create a new CRC. - pub fn new() -> Crc { - Crc { - amt: 0, - hasher: Hasher::new(), - } + pub fn new() -> Self { + Self::default() } /// Returns the current crc32 checksum. diff --git a/src/gz/mod.rs b/src/gz/mod.rs index eef4636b..92440279 100644 --- a/src/gz/mod.rs +++ b/src/gz/mod.rs @@ -324,13 +324,7 @@ pub struct GzBuilder { impl GzBuilder { /// Create a new blank builder with no header by default. pub fn new() -> GzBuilder { - GzBuilder { - extra: None, - filename: None, - comment: None, - operating_system: None, - mtime: 0, - } + Self::default() } /// Configure the `mtime` field in the gzip header. From cf9c1a0e5b438bab7d8e4a8784bf7a59a4f0085c Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 00:33:55 +0200 Subject: [PATCH 19/32] deps(dev): update rand to 0.9 --- Cargo.toml | 2 +- src/deflate/mod.rs | 6 +++--- src/gz/mod.rs | 4 ++-- src/lib.rs | 2 +- src/zlib/mod.rs | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b60b83bf..c54c55fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ crc32fast = "1.2.0" miniz_oxide = { version = "0.8.0", default-features = false, features = ["with-alloc"] } [dev-dependencies] -rand = "0.8" +rand = "0.9" quickcheck = { version = "1.0", default-features = false } [features] diff --git a/src/deflate/mod.rs b/src/deflate/mod.rs index 7f3bf70f..b5c5b6ca 100644 --- a/src/deflate/mod.rs +++ b/src/deflate/mod.rs @@ -6,7 +6,7 @@ pub mod write; mod tests { use std::io::prelude::*; - use rand::{thread_rng, Rng}; + use rand::{rng, Rng}; use super::{read, write}; use crate::Compression; @@ -17,7 +17,7 @@ mod tests { let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); let v = crate::random_bytes().take(1024).collect::>(); for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0..v.len())]; + let to_write = &v[..rng().random_range(0..v.len())]; real.extend(to_write.iter().copied()); w.write_all(to_write).unwrap(); } @@ -46,7 +46,7 @@ mod tests { let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); let v = crate::random_bytes().take(1024).collect::>(); for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0..v.len())]; + let to_write = &v[..rng().random_range(0..v.len())]; real.extend(to_write.iter().copied()); w.write_all(to_write).unwrap(); } diff --git a/src/gz/mod.rs b/src/gz/mod.rs index 157c517c..1fbebfa5 100644 --- a/src/gz/mod.rs +++ b/src/gz/mod.rs @@ -464,7 +464,7 @@ mod tests { use super::{read, write, GzBuilder, GzHeaderParser}; use crate::{Compression, GzHeader}; - use rand::{thread_rng, Rng}; + use rand::{rng, Rng}; #[test] fn roundtrip() { @@ -493,7 +493,7 @@ mod tests { let mut w = write::GzEncoder::new(Vec::new(), Compression::default()); let v = crate::random_bytes().take(1024).collect::>(); for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0..v.len())]; + let to_write = &v[..rng().random_range(0..v.len())]; real.extend(to_write.iter().copied()); w.write_all(to_write).unwrap(); } diff --git a/src/lib.rs b/src/lib.rs index c827413f..16838c83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -242,5 +242,5 @@ fn random_bytes() -> impl Iterator { use rand::Rng; use std::iter; - iter::repeat(()).map(|_| rand::thread_rng().gen()) + iter::repeat(()).map(|_| rand::rng().random()) } diff --git a/src/zlib/mod.rs b/src/zlib/mod.rs index 1a293ba0..5a99b0eb 100644 --- a/src/zlib/mod.rs +++ b/src/zlib/mod.rs @@ -7,7 +7,7 @@ mod tests { use std::io; use std::io::prelude::*; - use rand::{thread_rng, Rng}; + use rand::{rng, Rng}; use crate::zlib::{read, write}; use crate::Compression; @@ -18,7 +18,7 @@ mod tests { let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default()); let v = crate::random_bytes().take(1024).collect::>(); for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0..v.len())]; + let to_write = &v[..rng().random_range(0..v.len())]; real.extend(to_write.iter().copied()); w.write_all(to_write).unwrap(); } @@ -47,7 +47,7 @@ mod tests { let mut w = write::ZlibEncoder::new(Vec::new(), Compression::default()); let v = crate::random_bytes().take(1024).collect::>(); for _ in 0..200 { - let to_write = &v[..thread_rng().gen_range(0..v.len())]; + let to_write = &v[..rng().random_range(0..v.len())]; real.extend(to_write.iter().copied()); w.write_all(to_write).unwrap(); } From d5603e2ae7c5a90827ffb6f6199b361d43cbbbcf Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 00:47:14 +0200 Subject: [PATCH 20/32] deps: bump miniz_oxide to 0.8.4 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b60b83bf..8f63a9cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,11 +24,11 @@ libz-ng-sys = { version = "1.1.16", optional = true } # this matches the default features, but we don't want to depend on the default features staying the same libz-rs-sys = { version = "0.4.0", optional = true, default-features = false, features = ["std", "rust-allocator"] } cloudflare-zlib-sys = { version = "0.3.5", optional = true } -miniz_oxide = { version = "0.8.0", optional = true, default-features = false, features = ["with-alloc"] } +miniz_oxide = { version = "0.8.4", optional = true, default-features = false, features = ["with-alloc"] } crc32fast = "1.2.0" [target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies] -miniz_oxide = { version = "0.8.0", default-features = false, features = ["with-alloc"] } +miniz_oxide = { version = "0.8.4", default-features = false, features = ["with-alloc"] } [dev-dependencies] rand = "0.8" From dcac32fb1dc4144afa8dd40c6b32eed00ce0cd99 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 12:00:47 +0200 Subject: [PATCH 21/32] add error traits --- src/deflate/write.rs | 6 +++--- src/gz/write.rs | 6 +++--- src/zio.rs | 45 ++++++++++++++++++++++---------------------- src/zlib/write.rs | 6 +++--- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/deflate/write.rs b/src/deflate/write.rs index 402899a9..5f659aa3 100644 --- a/src/deflate/write.rs +++ b/src/deflate/write.rs @@ -1,7 +1,7 @@ use std::io; use std::io::prelude::*; -use crate::zio; +use crate::{zio, CompressError, DecompressError}; use crate::{Compress, Decompress}; /// A DEFLATE encoder, or compressor. @@ -28,7 +28,7 @@ use crate::{Compress, Decompress}; /// ``` #[derive(Debug)] pub struct DeflateEncoder { - inner: zio::Writer, + inner: zio::Writer, } impl DeflateEncoder { @@ -206,7 +206,7 @@ impl Read for DeflateEncoder { /// ``` #[derive(Debug)] pub struct DeflateDecoder { - inner: zio::Writer, + inner: zio::Writer, } impl DeflateDecoder { diff --git a/src/gz/write.rs b/src/gz/write.rs index 74d6c5ac..b2ba7559 100644 --- a/src/gz/write.rs +++ b/src/gz/write.rs @@ -4,7 +4,7 @@ use std::io::prelude::*; use super::{corrupt, GzBuilder, GzHeader, GzHeaderParser}; use crate::crc::{Crc, CrcWriter}; -use crate::zio; +use crate::{zio, CompressError, DecompressError}; use crate::{Compress, Compression, Decompress, Status}; /// A gzip streaming encoder @@ -31,7 +31,7 @@ use crate::{Compress, Compression, Decompress, Status}; /// ``` #[derive(Debug)] pub struct GzEncoder { - inner: zio::Writer, + inner: zio::Writer, crc: Crc, crc_bytes_written: usize, header: Vec, @@ -209,7 +209,7 @@ impl Drop for GzEncoder { /// ``` #[derive(Debug)] pub struct GzDecoder { - inner: zio::Writer, Decompress>, + inner: zio::Writer, DecompressError, Decompress>, crc_bytes: Vec, header_parser: GzHeaderParser, } diff --git a/src/zio.rs b/src/zio.rs index ea25922a..206e5b3f 100644 --- a/src/zio.rs +++ b/src/zio.rs @@ -1,17 +1,19 @@ use std::io; use std::io::prelude::*; +use std::marker::PhantomData; use std::mem; -use crate::{Compress, Decompress, DecompressError, FlushCompress, FlushDecompress, Status}; +use crate::{Compress, CompressError, Decompress, DecompressError, FlushCompress, FlushDecompress, Status}; #[derive(Debug)] -pub struct Writer { +pub struct Writer, D: Ops> where io::Error: From { obj: Option, pub data: D, buf: Vec, + _writer_error: PhantomData, } -pub trait Ops { +pub trait Ops { type Flush: Flush; fn total_in(&self) -> u64; fn total_out(&self) -> u64; @@ -20,16 +22,16 @@ pub trait Ops { input: &[u8], output: &mut [u8], flush: Self::Flush, - ) -> Result; + ) -> Result; fn run_vec( &mut self, input: &[u8], output: &mut Vec, flush: Self::Flush, - ) -> Result; + ) -> Result; } -impl Ops for Compress { +impl Ops for Compress { type Flush = FlushCompress; fn total_in(&self) -> u64 { self.total_in() @@ -42,20 +44,20 @@ impl Ops for Compress { input: &[u8], output: &mut [u8], flush: FlushCompress, - ) -> Result { - Ok(self.compress(input, output, flush).unwrap()) + ) -> Result { + self.compress(input, output, flush) } fn run_vec( &mut self, input: &[u8], output: &mut Vec, flush: FlushCompress, - ) -> Result { - Ok(self.compress_vec(input, output, flush).unwrap()) + ) -> Result { + self.compress_vec(input, output, flush) } } -impl Ops for Decompress { +impl Ops for Decompress { type Flush = FlushDecompress; fn total_in(&self) -> u64 { self.total_in() @@ -115,10 +117,10 @@ impl Flush for FlushDecompress { } } -pub fn read(obj: &mut R, data: &mut D, dst: &mut [u8]) -> io::Result +pub fn read(obj: &mut R, data: &mut D, dst: &mut [u8]) -> io::Result where R: BufRead, - D: Ops, + D: Ops, { loop { let (read, consumed, ret, eof); @@ -156,12 +158,13 @@ where } } -impl Writer { - pub fn new(w: W, d: D) -> Writer { +impl, D: Ops> Writer where io::Error: From { + pub fn new(w: W, d: D) -> Writer { Writer { obj: Some(w), data: d, buf: Vec::with_capacity(32 * 1024), + _writer_error: PhantomData, } } @@ -247,15 +250,14 @@ impl Writer { } } -impl Write for Writer { +impl, D: Ops> Write for Writer where io::Error: From { fn write(&mut self, buf: &[u8]) -> io::Result { self.write_with_status(buf).map(|res| res.0) } fn flush(&mut self) -> io::Result<()> { self.data - .run_vec(&[], &mut self.buf, D::Flush::sync()) - .unwrap(); + .run_vec(&[], &mut self.buf, D::Flush::sync())?; // Unfortunately miniz doesn't actually tell us when we're done with // pulling out all the data from the internal stream. To remedy this we @@ -266,18 +268,17 @@ impl Write for Writer { self.dump()?; let before = self.data.total_out(); self.data - .run_vec(&[], &mut self.buf, D::Flush::none()) - .unwrap(); + .run_vec(&[], &mut self.buf, D::Flush::none())?; if before == self.data.total_out() { break; } } - self.obj.as_mut().unwrap().flush() + self.obj.as_mut().map(Write::flush).unwrap_or(Ok(())) } } -impl Drop for Writer { +impl, D: Ops> Drop for Writer where io::Error: From { fn drop(&mut self) { if self.obj.is_some() { let _ = self.finish(); diff --git a/src/zlib/write.rs b/src/zlib/write.rs index 77c1c899..69e989b1 100644 --- a/src/zlib/write.rs +++ b/src/zlib/write.rs @@ -1,7 +1,7 @@ use std::io; use std::io::prelude::*; -use crate::zio; +use crate::{zio, CompressError, DecompressError}; use crate::{Compress, Decompress}; /// A ZLIB encoder, or compressor. @@ -29,7 +29,7 @@ use crate::{Compress, Decompress}; /// ``` #[derive(Debug)] pub struct ZlibEncoder { - inner: zio::Writer, + inner: zio::Writer, } impl ZlibEncoder { @@ -216,7 +216,7 @@ impl Read for ZlibEncoder { /// ``` #[derive(Debug)] pub struct ZlibDecoder { - inner: zio::Writer, + inner: zio::Writer, } impl ZlibDecoder { From f8c910fec2d9a4d1c39f2db68014e6ed7baea061 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 18:36:09 +0200 Subject: [PATCH 22/32] move to associated types --- src/deflate/write.rs | 6 +++--- src/gz/write.rs | 6 +++--- src/zio.rs | 39 ++++++++++++++++++++------------------- src/zlib/write.rs | 6 +++--- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/deflate/write.rs b/src/deflate/write.rs index 5f659aa3..402899a9 100644 --- a/src/deflate/write.rs +++ b/src/deflate/write.rs @@ -1,7 +1,7 @@ use std::io; use std::io::prelude::*; -use crate::{zio, CompressError, DecompressError}; +use crate::zio; use crate::{Compress, Decompress}; /// A DEFLATE encoder, or compressor. @@ -28,7 +28,7 @@ use crate::{Compress, Decompress}; /// ``` #[derive(Debug)] pub struct DeflateEncoder { - inner: zio::Writer, + inner: zio::Writer, } impl DeflateEncoder { @@ -206,7 +206,7 @@ impl Read for DeflateEncoder { /// ``` #[derive(Debug)] pub struct DeflateDecoder { - inner: zio::Writer, + inner: zio::Writer, } impl DeflateDecoder { diff --git a/src/gz/write.rs b/src/gz/write.rs index b2ba7559..74d6c5ac 100644 --- a/src/gz/write.rs +++ b/src/gz/write.rs @@ -4,7 +4,7 @@ use std::io::prelude::*; use super::{corrupt, GzBuilder, GzHeader, GzHeaderParser}; use crate::crc::{Crc, CrcWriter}; -use crate::{zio, CompressError, DecompressError}; +use crate::zio; use crate::{Compress, Compression, Decompress, Status}; /// A gzip streaming encoder @@ -31,7 +31,7 @@ use crate::{Compress, Compression, Decompress, Status}; /// ``` #[derive(Debug)] pub struct GzEncoder { - inner: zio::Writer, + inner: zio::Writer, crc: Crc, crc_bytes_written: usize, header: Vec, @@ -209,7 +209,7 @@ impl Drop for GzEncoder { /// ``` #[derive(Debug)] pub struct GzDecoder { - inner: zio::Writer, DecompressError, Decompress>, + inner: zio::Writer, Decompress>, crc_bytes: Vec, header_parser: GzHeaderParser, } diff --git a/src/zio.rs b/src/zio.rs index 206e5b3f..305b0e37 100644 --- a/src/zio.rs +++ b/src/zio.rs @@ -1,19 +1,18 @@ use std::io; use std::io::prelude::*; -use std::marker::PhantomData; use std::mem; use crate::{Compress, CompressError, Decompress, DecompressError, FlushCompress, FlushDecompress, Status}; #[derive(Debug)] -pub struct Writer, D: Ops> where io::Error: From { +pub struct Writer { obj: Option, pub data: D, buf: Vec, - _writer_error: PhantomData, } -pub trait Ops { +pub trait Ops { + type Error: Into; type Flush: Flush; fn total_in(&self) -> u64; fn total_out(&self) -> u64; @@ -22,16 +21,17 @@ pub trait Ops { input: &[u8], output: &mut [u8], flush: Self::Flush, - ) -> Result; + ) -> Result; fn run_vec( &mut self, input: &[u8], output: &mut Vec, flush: Self::Flush, - ) -> Result; + ) -> Result; } -impl Ops for Compress { +impl Ops for Compress { + type Error = CompressError; type Flush = FlushCompress; fn total_in(&self) -> u64 { self.total_in() @@ -57,7 +57,8 @@ impl Ops for Compress { } } -impl Ops for Decompress { +impl Ops for Decompress { + type Error = DecompressError; type Flush = FlushDecompress; fn total_in(&self) -> u64 { self.total_in() @@ -117,10 +118,10 @@ impl Flush for FlushDecompress { } } -pub fn read(obj: &mut R, data: &mut D, dst: &mut [u8]) -> io::Result +pub fn read(obj: &mut R, data: &mut D, dst: &mut [u8]) -> io::Result where R: BufRead, - D: Ops, + D: Ops, { loop { let (read, consumed, ret, eof); @@ -158,13 +159,12 @@ where } } -impl, D: Ops> Writer where io::Error: From { - pub fn new(w: W, d: D) -> Writer { +impl Writer { + pub fn new(w: W, d: D) -> Writer { Writer { obj: Some(w), data: d, buf: Vec::with_capacity(32 * 1024), - _writer_error: PhantomData, } } @@ -173,7 +173,7 @@ impl, D: Ops> Writer where io::Error self.dump()?; let before = self.data.total_out(); - self.data.run_vec(&[], &mut self.buf, D::Flush::finish())?; + self.data.run_vec(&[], &mut self.buf, Flush::finish()).map_err(Into::into)?; if before == self.data.total_out() { return Ok(()); } @@ -250,14 +250,15 @@ impl, D: Ops> Writer where io::Error } } -impl, D: Ops> Write for Writer where io::Error: From { +impl Write for Writer { fn write(&mut self, buf: &[u8]) -> io::Result { self.write_with_status(buf).map(|res| res.0) } - fn flush(&mut self) -> io::Result<()> { + fn flush(&mut self) -> io::Result<()> + { self.data - .run_vec(&[], &mut self.buf, D::Flush::sync())?; + .run_vec(&[], &mut self.buf, Flush::sync()).map_err(Into::into)?; // Unfortunately miniz doesn't actually tell us when we're done with // pulling out all the data from the internal stream. To remedy this we @@ -268,7 +269,7 @@ impl, D: Ops> Write for Writer where self.dump()?; let before = self.data.total_out(); self.data - .run_vec(&[], &mut self.buf, D::Flush::none())?; + .run_vec(&[], &mut self.buf, Flush::none()).map_err(Into::into)?; if before == self.data.total_out() { break; } @@ -278,7 +279,7 @@ impl, D: Ops> Write for Writer where } } -impl, D: Ops> Drop for Writer where io::Error: From { +impl Drop for Writer { fn drop(&mut self) { if self.obj.is_some() { let _ = self.finish(); diff --git a/src/zlib/write.rs b/src/zlib/write.rs index 69e989b1..77c1c899 100644 --- a/src/zlib/write.rs +++ b/src/zlib/write.rs @@ -1,7 +1,7 @@ use std::io; use std::io::prelude::*; -use crate::{zio, CompressError, DecompressError}; +use crate::zio; use crate::{Compress, Decompress}; /// A ZLIB encoder, or compressor. @@ -29,7 +29,7 @@ use crate::{Compress, Decompress}; /// ``` #[derive(Debug)] pub struct ZlibEncoder { - inner: zio::Writer, + inner: zio::Writer, } impl ZlibEncoder { @@ -216,7 +216,7 @@ impl Read for ZlibEncoder { /// ``` #[derive(Debug)] pub struct ZlibDecoder { - inner: zio::Writer, + inner: zio::Writer, } impl ZlibDecoder { From 1fb943a0a39bc09b5fa36d5c227a3388a42c0e1e Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 18:37:32 +0200 Subject: [PATCH 23/32] cargo fmt --- src/zio.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/zio.rs b/src/zio.rs index 305b0e37..2ea9fb18 100644 --- a/src/zio.rs +++ b/src/zio.rs @@ -2,7 +2,9 @@ use std::io; use std::io::prelude::*; use std::mem; -use crate::{Compress, CompressError, Decompress, DecompressError, FlushCompress, FlushDecompress, Status}; +use crate::{ + Compress, CompressError, Decompress, DecompressError, FlushCompress, FlushDecompress, Status, +}; #[derive(Debug)] pub struct Writer { @@ -173,7 +175,9 @@ impl Writer { self.dump()?; let before = self.data.total_out(); - self.data.run_vec(&[], &mut self.buf, Flush::finish()).map_err(Into::into)?; + self.data + .run_vec(&[], &mut self.buf, Flush::finish()) + .map_err(Into::into)?; if before == self.data.total_out() { return Ok(()); } @@ -255,10 +259,10 @@ impl Write for Writer { self.write_with_status(buf).map(|res| res.0) } - fn flush(&mut self) -> io::Result<()> - { + fn flush(&mut self) -> io::Result<()> { self.data - .run_vec(&[], &mut self.buf, Flush::sync()).map_err(Into::into)?; + .run_vec(&[], &mut self.buf, Flush::sync()) + .map_err(Into::into)?; // Unfortunately miniz doesn't actually tell us when we're done with // pulling out all the data from the internal stream. To remedy this we @@ -269,7 +273,8 @@ impl Write for Writer { self.dump()?; let before = self.data.total_out(); self.data - .run_vec(&[], &mut self.buf, Flush::none()).map_err(Into::into)?; + .run_vec(&[], &mut self.buf, Flush::none()) + .map_err(Into::into)?; if before == self.data.total_out() { break; } From 5adcf634aa7499fd2d87b45314fcd44f63fa9a6a Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 19:26:57 +0200 Subject: [PATCH 24/32] restore a changed line --- src/zio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zio.rs b/src/zio.rs index 2ea9fb18..3e6b9eb3 100644 --- a/src/zio.rs +++ b/src/zio.rs @@ -280,7 +280,7 @@ impl Write for Writer { } } - self.obj.as_mut().map(Write::flush).unwrap_or(Ok(())) + self.obj.as_mut().unwrap().flush() } } From 4fd338c3b3eed564c03401536e74707ddd67acf6 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 19:51:08 +0200 Subject: [PATCH 25/32] feat: manually map Flush to MZFlush to avoid redundant unwrap usage --- src/ffi/rust.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/ffi/rust.rs b/src/ffi/rust.rs index 7f40fe78..e3078be1 100644 --- a/src/ffi/rust.rs +++ b/src/ffi/rust.rs @@ -50,6 +50,14 @@ impl fmt::Debug for Inflate { } } +fn flush_decompress_into_mzflush(flush: FlushDecompress) -> MZFlush { + match flush { + FlushDecompress::None => MZFlush::None, + FlushDecompress::Sync => MZFlush::Sync, + FlushDecompress::Finish => MZFlush::Finish, + } +} + impl InflateBackend for Inflate { fn make(zlib_header: bool, _window_bits: u8) -> Self { let format = format_from_bool(zlib_header); @@ -67,8 +75,7 @@ impl InflateBackend for Inflate { output: &mut [u8], flush: FlushDecompress, ) -> Result { - let flush = MZFlush::new(flush as i32).unwrap(); - + let flush = flush_decompress_into_mzflush(flush); let res = inflate::stream::inflate(&mut self.inner, input, output, flush); self.total_in += res.bytes_consumed as u64; self.total_out += res.bytes_written as u64; @@ -123,6 +130,15 @@ impl fmt::Debug for Deflate { } } +fn flush_compress_into_mzflush(flush: FlushCompress) -> MZFlush { + match flush { + FlushCompress::None => MZFlush::None, + FlushCompress::Sync | FlushCompress::Partial => MZFlush::Sync, + FlushCompress::Full => MZFlush::Full, + FlushCompress::Finish => MZFlush::Finish, + } +} + impl DeflateBackend for Deflate { fn make(level: Compression, zlib_header: bool, _window_bits: u8) -> Self { // Check in case the integer value changes at some point. @@ -145,7 +161,7 @@ impl DeflateBackend for Deflate { output: &mut [u8], flush: FlushCompress, ) -> Result { - let flush = MZFlush::new(flush as i32).unwrap(); + let flush = flush_compress_into_mzflush(flush); let res = deflate::stream::deflate(&mut self.inner, input, output, flush); self.total_in += res.bytes_consumed as u64; self.total_out += res.bytes_written as u64; From a6cac4931dd0cbe4f14a4085544ceb10ce37570b Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Thu, 13 Feb 2025 19:54:13 +0200 Subject: [PATCH 26/32] feat: replace fns with From impl --- src/ffi/rust.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/ffi/rust.rs b/src/ffi/rust.rs index e3078be1..ba5e9e69 100644 --- a/src/ffi/rust.rs +++ b/src/ffi/rust.rs @@ -50,11 +50,13 @@ impl fmt::Debug for Inflate { } } -fn flush_decompress_into_mzflush(flush: FlushDecompress) -> MZFlush { - match flush { - FlushDecompress::None => MZFlush::None, - FlushDecompress::Sync => MZFlush::Sync, - FlushDecompress::Finish => MZFlush::Finish, +impl From for MZFlush { + fn from(value: FlushDecompress) -> Self { + match value { + FlushDecompress::None => Self::None, + FlushDecompress::Sync => Self::Sync, + FlushDecompress::Finish => Self::Finish, + } } } @@ -75,8 +77,8 @@ impl InflateBackend for Inflate { output: &mut [u8], flush: FlushDecompress, ) -> Result { - let flush = flush_decompress_into_mzflush(flush); - let res = inflate::stream::inflate(&mut self.inner, input, output, flush); + let mz_flush = flush.into(); + let res = inflate::stream::inflate(&mut self.inner, input, output, mz_flush); self.total_in += res.bytes_consumed as u64; self.total_out += res.bytes_written as u64; @@ -130,12 +132,14 @@ impl fmt::Debug for Deflate { } } -fn flush_compress_into_mzflush(flush: FlushCompress) -> MZFlush { - match flush { - FlushCompress::None => MZFlush::None, - FlushCompress::Sync | FlushCompress::Partial => MZFlush::Sync, - FlushCompress::Full => MZFlush::Full, - FlushCompress::Finish => MZFlush::Finish, +impl From for MZFlush { + fn from(value: FlushCompress) -> Self { + match value { + FlushCompress::None => Self::None, + FlushCompress::Sync | FlushCompress::Partial => Self::Sync, + FlushCompress::Full => Self::Full, + FlushCompress::Finish => Self::Finish, + } } } @@ -161,8 +165,8 @@ impl DeflateBackend for Deflate { output: &mut [u8], flush: FlushCompress, ) -> Result { - let flush = flush_compress_into_mzflush(flush); - let res = deflate::stream::deflate(&mut self.inner, input, output, flush); + let mz_flush = flush.into(); + let res = deflate::stream::deflate(&mut self.inner, input, output, mz_flush); self.total_in += res.bytes_consumed as u64; self.total_out += res.bytes_written as u64; From d4aebe1f7648d728b386d4ffa326700c9f2420d2 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Fri, 14 Feb 2025 13:56:37 +0200 Subject: [PATCH 27/32] feat: reorder FlushCompress variants to be in order with value --- src/ffi/rust.rs | 2 +- src/mem.rs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ffi/rust.rs b/src/ffi/rust.rs index ba5e9e69..5b3c1937 100644 --- a/src/ffi/rust.rs +++ b/src/ffi/rust.rs @@ -136,7 +136,7 @@ impl From for MZFlush { fn from(value: FlushCompress) -> Self { match value { FlushCompress::None => Self::None, - FlushCompress::Sync | FlushCompress::Partial => Self::Sync, + FlushCompress::Partial | FlushCompress::Sync => Self::Sync, FlushCompress::Full => Self::Full, FlushCompress::Finish => Self::Finish, } diff --git a/src/mem.rs b/src/mem.rs index 3ce3d140..6f6857de 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -49,15 +49,6 @@ pub enum FlushCompress { /// accumulate before producing output in order to maximize compression. None = ffi::MZ_NO_FLUSH as isize, - /// All pending output is flushed to the output buffer and the output is - /// aligned on a byte boundary so that the decompressor can get all input - /// data available so far. - /// - /// Flushing may degrade compression for some compression algorithms and so - /// it should only be used when necessary. This will complete the current - /// deflate block and follow it with an empty stored block. - Sync = ffi::MZ_SYNC_FLUSH as isize, - /// All pending output is flushed to the output buffer, but the output is /// not aligned to a byte boundary. /// @@ -68,6 +59,15 @@ pub enum FlushCompress { /// block before the empty fixed code block. Partial = ffi::MZ_PARTIAL_FLUSH as isize, + /// All pending output is flushed to the output buffer and the output is + /// aligned on a byte boundary so that the decompressor can get all input + /// data available so far. + /// + /// Flushing may degrade compression for some compression algorithms and so + /// it should only be used when necessary. This will complete the current + /// deflate block and follow it with an empty stored block. + Sync = ffi::MZ_SYNC_FLUSH as isize, + /// All output is flushed as with `Flush::Sync` and the compression state is /// reset so decompression can restart from this point if previous /// compressed data has been damaged or if random access is desired. From be59b78575afc55b0be7c0ba2bb09fdc1ea3aee2 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Fri, 14 Feb 2025 19:23:11 +0200 Subject: [PATCH 28/32] docs: fix spelling mistake --- src/mem.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mem.rs b/src/mem.rs index 6f6857de..63542d43 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -52,9 +52,9 @@ pub enum FlushCompress { /// All pending output is flushed to the output buffer, but the output is /// not aligned to a byte boundary. /// - /// All of the input data so far will be available to the decompressor (as - /// with `Flush::Sync`. This completes the current deflate block and follows - /// it with an empty fixed codes block that is 10 bites long, and it assures + /// All input data so far will be available to the decompressor (as with + /// `Flush::Sync`). This completes the current deflate block and follows it + /// with an empty fixed codes block that is 10 bites long, and it assures /// that enough bytes are output in order for the decompressor to finish the /// block before the empty fixed code block. Partial = ffi::MZ_PARTIAL_FLUSH as isize, From 1be8e37c32c7f02b9a95d938f37cb6d8a7efa101 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Mon, 17 Feb 2025 21:23:05 +0200 Subject: [PATCH 29/32] feat: remove redundant if guard on option value match --- src/gz/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/gz/mod.rs b/src/gz/mod.rs index 8bbdbf82..a5e23556 100644 --- a/src/gz/mod.rs +++ b/src/gz/mod.rs @@ -252,9 +252,7 @@ fn read_to_nul(r: &mut R, buffer: &mut Vec) -> Result<()> { let mut bytes = r.bytes(); loop { match bytes.next().transpose()? { - Some(byte) if byte == 0 => { - return Ok(()); - } + Some(0) => return Ok(()), Some(_) if buffer.len() == MAX_HEADER_BUF => { return Err(Error::new( ErrorKind::InvalidInput, From 40c2e0f56a4be991d43ed5ec91a1c43b1ba50ec4 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Tue, 18 Feb 2025 11:09:42 +0200 Subject: [PATCH 30/32] docs: fix spelling mistake in FlushCompress::Partial --- src/mem.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mem.rs b/src/mem.rs index 63542d43..60f8f89d 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -54,7 +54,7 @@ pub enum FlushCompress { /// /// All input data so far will be available to the decompressor (as with /// `Flush::Sync`). This completes the current deflate block and follows it - /// with an empty fixed codes block that is 10 bites long, and it assures + /// with an empty fixed codes block that is 10 bytes long, and it assures /// that enough bytes are output in order for the decompressor to finish the /// block before the empty fixed code block. Partial = ffi::MZ_PARTIAL_FLUSH as isize, From bf5bf566980f549e2042229a0e6495a6d79137c0 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Tue, 18 Feb 2025 11:19:31 +0200 Subject: [PATCH 31/32] feat: remove explicit redundant lifetime --- src/gz/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gz/mod.rs b/src/gz/mod.rs index a5e23556..3a24a754 100644 --- a/src/gz/mod.rs +++ b/src/gz/mod.rs @@ -115,7 +115,7 @@ impl GzHeaderParser { } } - fn parse<'a, R: BufRead>(&mut self, r: &'a mut R) -> Result<()> { + fn parse(&mut self, r: &mut R) -> Result<()> { loop { match &mut self.state { GzHeaderState::Start(count, buffer) => { From 13104a545503cca8e08cc0751e55702a7e1a8232 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Thu, 20 Feb 2025 14:30:27 +0100 Subject: [PATCH 32/32] upgrade zlib-rs to version `0.4.2` bump flate2 to 1.1.0 (minor version bump because of an MSRV bump) --- Cargo.toml | 4 ++-- src/ffi/c.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e73acb9..ed0e7235 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "flate2" authors = ["Alex Crichton ", "Josh Triplett "] -version = "1.0.35" +version = "1.1.0" edition = "2018" license = "MIT OR Apache-2.0" readme = "README.md" @@ -22,7 +22,7 @@ exclude = [".*"] libz-sys = { version = "1.1.20", optional = true, default-features = false } libz-ng-sys = { version = "1.1.16", optional = true } # this matches the default features, but we don't want to depend on the default features staying the same -libz-rs-sys = { version = "0.4.0", optional = true, default-features = false, features = ["std", "rust-allocator"] } +libz-rs-sys = { version = "0.4.2", optional = true, default-features = false, features = ["std", "rust-allocator"] } cloudflare-zlib-sys = { version = "0.3.5", optional = true } miniz_oxide = { version = "0.8.4", optional = true, default-features = false, features = ["with-alloc"] } crc32fast = "1.2.0" diff --git a/src/ffi/c.rs b/src/ffi/c.rs index 173701bd..3ffebbc4 100644 --- a/src/ffi/c.rs +++ b/src/ffi/c.rs @@ -467,7 +467,7 @@ mod c_backend { #[cfg(feature = "zlib-ng")] const ZLIB_VERSION: &'static str = "2.1.0.devel\0"; #[cfg(all(not(feature = "zlib-ng"), feature = "zlib-rs"))] - const ZLIB_VERSION: &'static str = "1.3.0-zlib-rs-0.4.0\0"; + const ZLIB_VERSION: &'static str = "1.3.0-zlib-rs-0.4.2\0"; #[cfg(not(any(feature = "zlib-ng", feature = "zlib-rs")))] const ZLIB_VERSION: &'static str = "1.2.8\0";