Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where wrong data is emitted for gzip fixed blocks #24

Merged
merged 3 commits into from
Jul 25, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix a bug where the gzip compressor may output incorrect data when emitting
"fixed blocks" (which are emitted when compressing high-entropy data).

## [0.5.0] - 2024-06-04

### Added
Expand Down
29 changes: 19 additions & 10 deletions lib/src/gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,16 +1022,34 @@ pub fn compress(bytes: &[u8], level: usize, small_mem: bool) -> Result<Box<[u8]>
pos += prev_match_len - 1;
lookahead -= prev_match_len - 1;

if should_flush {
if pos >= block_length {
writer.flush_block(&mut output, Some(&window[pos - block_length..pos]), false);
} else {
writer.flush_block(&mut output, None, false);
}
block_length = 0;
}

has_prev_char = false;
prev_match_len = MIN_MATCH - 1;
prev_match_dist = 0;
} else {
// Remember current match and emit previous character as literal if it exists
// Emit previous character as literal (if it exists) and remember current match
if has_prev_char {
writer.add_literal(window[pos - 1]);
should_flush = writer.should_flush_block(block_length);
}

if should_flush {
if pos >= block_length {
writer.flush_block(&mut output, Some(&window[pos - block_length..pos]), false);
} else {
writer.flush_block(&mut output, None, false);
}
block_length = 0;
}

block_length += 1;
pos += 1;
lookahead -= 1;
Expand All @@ -1041,15 +1059,6 @@ pub fn compress(bytes: &[u8], level: usize, small_mem: bool) -> Result<Box<[u8]>
prev_match_dist = pos - 1 - best_pos;
}

if should_flush {
if pos >= block_length {
writer.flush_block(&mut output, Some(&window[pos - block_length..pos]), false);
} else {
writer.flush_block(&mut output, None, false);
}
block_length = 0;
}

// Refill window
if lookahead < MIN_LOOKAHEAD && !eof && pos >= WINDOW_SIZE + MAX_DIST {
window.copy_within(WINDOW_SIZE..2 * WINDOW_SIZE, 0);
Expand Down
Binary file modified test_data/dirt.png.gzip-6-small-mem
Binary file not shown.
Binary file modified test_data/dirt.png.gzip-9
Binary file not shown.
Binary file modified test_data/dirt.png.gzip-9-small-mem
Binary file not shown.
Binary file modified test_data/ground.png.gzip-6-small-mem
Binary file not shown.
Binary file modified test_data/ground.png.gzip-9
Binary file not shown.
Binary file modified test_data/ground.png.gzip-9-small-mem
Binary file not shown.
Binary file modified test_data/stones.png.gzip-6-small-mem
Binary file not shown.
Binary file modified test_data/stones.png.gzip-9
Binary file not shown.
Binary file modified test_data/stones.png.gzip-9-small-mem
Binary file not shown.
Binary file modified test_data/tile.png.gzip-6-small-mem
Binary file not shown.
Binary file modified test_data/tile.png.gzip-9
Binary file not shown.
Binary file modified test_data/tile.png.gzip-9-small-mem
Binary file not shown.
Loading