Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ jobs:
- run: cargo test --features zlib-ng --no-default-features
if: matrix.build != 'mingw'
- 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'
- run: |
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "flate2"
authors = ["Alex Crichton <[email protected]>", "Josh Triplett <[email protected]>"]
version = "1.1.7"
version = "1.1.8"
edition = "2018"
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -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
zlib-rs = { version = "0.5.3", optional = true, default-features = false, features = ["std", "rust-allocator"] }
zlib-rs = { version = "0.5.5", optional = true, default-features = false, features = ["std", "rust-allocator"] }
cloudflare-zlib-sys = { version = "0.3.6", optional = true }
miniz_oxide = { version = "0.8.5", optional = true, default-features = false, features = ["with-alloc", "simd"] }
crc32fast = "1.2.0"
Expand Down
96 changes: 84 additions & 12 deletions src/ffi/zlib_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub const MZ_FINISH: isize = DeflateFlush::Finish as isize;
pub const MZ_DEFAULT_WINDOW_BITS: core::ffi::c_int = 15;

use super::*;
use crate::mem::{compress_failed, decompress_failed};

impl From<::zlib_rs::Status> for crate::mem::Status {
fn from(value: ::zlib_rs::Status) -> Self {
Expand All @@ -52,15 +53,18 @@ impl ErrorMessage {

pub struct Inflate {
pub(crate) inner: ::zlib_rs::Inflate,
// NOTE: these counts do not count the dictionary.
total_in: u64,
total_out: u64,
}

impl fmt::Debug for Inflate {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"zlib_rs inflate internal state. total_in: {}, total_out: {}",
self.inner.total_in(),
self.inner.total_out(),
self.total_in(),
self.total_out(),
)
}
}
Expand All @@ -79,6 +83,8 @@ impl InflateBackend for Inflate {
fn make(zlib_header: bool, window_bits: u8) -> Self {
Inflate {
inner: ::zlib_rs::Inflate::new(zlib_header, window_bits),
total_in: 0,
total_out: 0,
}
}

Expand All @@ -94,41 +100,67 @@ impl InflateBackend for Inflate {
FlushDecompress::Finish => InflateFlush::Finish,
};

match self.inner.decompress(input, output, flush) {
let total_in_start = self.inner.total_in();
let total_out_start = self.inner.total_out();

let result = self.inner.decompress(input, output, flush);

self.total_in += self.inner.total_in() - total_in_start;
self.total_out += self.inner.total_out() - total_out_start;

match result {
Ok(status) => Ok(status.into()),
Err(InflateError::NeedDict { dict_id }) => crate::mem::decompress_need_dict(dict_id),
Err(e) => crate::mem::decompress_failed(ErrorMessage(Some(e.as_str()))),
Err(_) => self.decompress_error(),
}
}

fn reset(&mut self, zlib_header: bool) {
self.total_in = 0;
self.total_out = 0;
self.inner.reset(zlib_header);
}
}

impl Backend for Inflate {
#[inline]
fn total_in(&self) -> u64 {
self.inner.total_in()
self.total_in
}

#[inline]
fn total_out(&self) -> u64 {
self.inner.total_out()
self.total_out
}
}

impl Inflate {
fn decompress_error<T>(&self) -> Result<T, DecompressError> {
decompress_failed(ErrorMessage(self.inner.error_message()))
}

pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<u32, DecompressError> {
match self.inner.set_dictionary(dictionary) {
Ok(v) => Ok(v),
Err(_) => self.decompress_error(),
}
}
}

pub struct Deflate {
pub(crate) inner: ::zlib_rs::Deflate,
// NOTE: these counts do not count the dictionary.
total_in: u64,
total_out: u64,
}

impl fmt::Debug for Deflate {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"zlib_rs deflate internal state. total_in: {}, total_out: {}",
self.inner.total_in(),
self.inner.total_out(),
self.total_in(),
self.total_out(),
)
}
}
Expand All @@ -140,6 +172,8 @@ impl DeflateBackend for Deflate {

Deflate {
inner: ::zlib_rs::Deflate::new(level.level() as i32, zlib_header, window_bits),
total_in: 0,
total_out: 0,
}
}

Expand All @@ -157,25 +191,63 @@ impl DeflateBackend for Deflate {
FlushCompress::Finish => DeflateFlush::Finish,
};

match self.inner.compress(input, output, flush) {
let total_in_start = self.inner.total_in();
let total_out_start = self.inner.total_out();

let result = self.inner.compress(input, output, flush);

self.total_in += self.inner.total_in() - total_in_start;
self.total_out += self.inner.total_out() - total_out_start;

match result {
Ok(status) => Ok(status.into()),
Err(e) => crate::mem::compress_failed(ErrorMessage(Some(e.as_str()))),
Err(_) => self.compress_error(),
}
}

fn reset(&mut self) {
self.total_in = 0;
self.total_out = 0;
self.inner.reset();
}
}

impl Backend for Deflate {
#[inline]
fn total_in(&self) -> u64 {
self.inner.total_in()
self.total_in
}

#[inline]
fn total_out(&self) -> u64 {
self.inner.total_out()
self.total_out
}
}

impl Deflate {
fn compress_error<T>(&self) -> Result<T, CompressError> {
compress_failed(ErrorMessage(self.inner.error_message()))
}

pub fn set_dictionary(&mut self, dictionary: &[u8]) -> Result<u32, CompressError> {
match self.inner.set_dictionary(dictionary) {
Ok(v) => Ok(v),
Err(_) => self.compress_error(),
}
}

pub fn set_level(&mut self, level: Compression) -> Result<(), CompressError> {
use ::zlib_rs::Status;

match self.inner.set_level(level.level() as i32) {
Ok(status) => match status {
Status::Ok => Ok(()),
Status::BufError => compress_failed(ErrorMessage(Some("insufficient space"))),
Status::StreamEnd => {
unreachable!("zlib-rs is known to never return the StreamEnd status")
}
},
Err(_) => self.compress_error(),
}
}
}
Loading