Skip to content

Commit

Permalink
Fix bug where wrong data is emitted for gzip fixed blocks (#24)
Browse files Browse the repository at this point in the history
* Fix bug where wrong data is emitted for gzip fixed blocks

* Update test data

* Add CHANGELOG entry
  • Loading branch information
cadmic authored Jul 25, 2024
1 parent 6db5f72 commit a51c4d8
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 10 deletions.
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.

0 comments on commit a51c4d8

Please sign in to comment.