Skip to content
Draft
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ any_impl = []
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[patch.crates-io]
miniz_oxide = { git = "https://github.com/Frommi/miniz_oxide", branch = "master" }
Comment on lines +115 to +117
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing left should be getting a miniz_oxide 0.8.10 release, so this patch isn't necessary for the tests to pass

3 changes: 2 additions & 1 deletion src/ffi/miniz_oxide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ impl From<FlushCompress> for MZFlush {
fn from(value: FlushCompress) -> Self {
match value {
FlushCompress::None => Self::None,
FlushCompress::Partial | FlushCompress::Sync => Self::Sync,
FlushCompress::Partial => Self::Partial,
FlushCompress::Sync => Self::Sync,
FlushCompress::Full => Self::Full,
FlushCompress::Finish => Self::Finish,
}
Expand Down
63 changes: 62 additions & 1 deletion src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,6 @@ mod tests {
use crate::write;
use crate::{Compression, Decompress, FlushDecompress};

#[cfg(feature = "any_zlib")]
use crate::{Compress, FlushCompress};

#[test]
Expand Down Expand Up @@ -819,4 +818,66 @@ mod tests {

assert_eq!(err.message(), Some("invalid stored block lengths"));
}

fn compress_with_flush(flush: FlushCompress) -> Vec<u8> {
let incompressible = (0..=255).collect::<Vec<u8>>();
let mut output = vec![0; 1024];

// Feed in the incompressible data followed by the indicated flush type.
let mut w = Compress::new(Compression::default(), false);
w.compress(&incompressible, &mut output, flush).unwrap();

if flush != FlushCompress::None {
// The first instance of incompressible input should have been written uncompressed.
assert!(w.total_out() >= 261);
assert_eq!(&output[0..5], &[0, 0, 1, 0xff, !1]);
assert_eq!(&output[5..261], &incompressible);
}

// Feed in the same data again.
let len = w.total_out() as usize;
w.compress(&incompressible, &mut output[len..], FlushCompress::Finish)
.unwrap();

if flush != FlushCompress::Full {
// This time, the data should have been compressed (because it is an exact duplicate of
// the earlier block).
assert!(w.total_out() < 300);
}

// Assert that all input has been processed.
assert_eq!(w.total_in(), 256 * 2);

output.resize(w.total_out() as usize, 0);
output
}

#[test]
fn test_partial_flush() {
let output = compress_with_flush(FlushCompress::Partial);

// Check for partial flush marker.
assert_eq!(output[261], 0x2);
assert_eq!(output[262] & 0x7, 0x4);
}

#[test]
fn test_sync_flush() {
let output = compress_with_flush(FlushCompress::Sync);

// Check for sync flush marker.
assert_eq!(&output[261..][..5], &[0, 0, 0, 0xff, 0xff]);
}

#[test]
fn test_full_flush() {
let output = compress_with_flush(FlushCompress::Full);
assert_eq!(output.len(), 527);

// Check for flush flush marker.
assert_eq!(&output[261..][..5], &[0, 0, 0, 0xff, 0xff]);

// Check that the second instance of incompressible input was also written uncompressed.
assert_eq!(&output[266..][..5], &[1, 0, 1, 0xff, !1]);
}
}