Skip to content
Merged
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
17 changes: 10 additions & 7 deletions miniz_oxide/src/deflate/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1681,6 +1681,16 @@ pub(crate) fn flush_block(
output.bit_buffer = d.params.saved_bit_buffer;
output.bits_in = d.params.saved_bits_in;

// If we are at the start of the stream, write the zlib header
// if requested. Note: Even if block-writing is skipped
// below, `block_index` is still incremented, so this is done
// only once
if d.params.flags & TDEFL_WRITE_ZLIB_HEADER != 0 && d.params.block_index == 0 {
let header = zlib::header_from_flags(d.params.flags);
output.put_bits_no_flush(header[0].into(), 8);
output.put_bits(header[1].into(), 8);
}

if d.lz.total_bytes > 0 || flush == TDEFLFlush::Finish {
// TODO: Don't think this second condition should be here but need to verify.
let use_raw_block = (d.params.flags & TDEFL_FORCE_ALL_RAW_BLOCKS != 0)
Expand All @@ -1696,13 +1706,6 @@ pub(crate) fn flush_block(

d.lz.init_flag();

// If we are at the start of the stream, write the zlib header if requested.
if d.params.flags & TDEFL_WRITE_ZLIB_HEADER != 0 && d.params.block_index == 0 {
let header = zlib::header_from_flags(d.params.flags);
output.put_bits_no_flush(header[0].into(), 8);
output.put_bits(header[1].into(), 8);
}

// Output the block header.
output.put_bits((flush == TDEFLFlush::Finish) as u32, 1);

Expand Down
15 changes: 15 additions & 0 deletions miniz_oxide/tests/flush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ fn test_flush() {
}
}

/// Test that a sync at start of a stream inserts the Zlib header
/// correctly
#[test]
fn check_zlib_with_sync_at_start() {
// Test sync gets zlib header
compress(b"", &[TDEFLFlush::Sync]);
compress(b"", &[TDEFLFlush::Partial]);

// Test that second sync DOESN'T get zlib header
// (i.e. `block_index` handling is correct)
compress(b"", &[TDEFLFlush::Sync, TDEFLFlush::Sync]);
compress(b"", &[TDEFLFlush::Partial, TDEFLFlush::Partial]);
compress(b"", &[TDEFLFlush::NoSync, TDEFLFlush::Sync]);
}

// Low-quality RNG, copied from test.rs
struct Rng(u64);

Expand Down