Skip to content

Commit

Permalink
Yaz0 bugfix (#15)
Browse files Browse the repository at this point in the history
* Fix Yaz0 issue of adding extra zero sometimes

* Cleanup

* changelog
  • Loading branch information
AngheloAlf authored Jan 20, 2024
1 parent 005b7e6 commit 6887fba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix some rare cases where the Yaz0 compressor may append an extra 0 at the end of the compressed data.

## [0.3.0] - 2024-1-19

### Added
Expand Down Expand Up @@ -42,7 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Python bindings.
- C bindings.

[unreleased]: https://github.com/decompals/crunch64/compare/0.2.0...HEAD
[unreleased]: https://github.com/decompals/crunch64/compare/0.3.0...HEAD
[0.3.0]: https://github.com/decompals/crunch64/compare/0.2.0...0.3.0
[0.2.0]: https://github.com/decompals/crunch64/compare/0.1.1...0.2.0
[0.1.1]: https://github.com/decompals/crunch64/compare/0.1.0...0.1.1
[0.1.0]: https://github.com/decompals/crunch64/releases/tag/0.1.0
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions lib/src/yaz0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,22 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {

write_header(&mut output, input_size)?;

output.push(0);
let mut index_cur_layout_byte: usize = 0x10;
let mut index_out_ptr: usize = index_cur_layout_byte + 1;
let mut index_out_ptr: usize = index_cur_layout_byte;
let mut input_pos: usize = 0;
let mut cur_layout_bit: u8 = 0x80;
let mut cur_layout_bit: u8 = 1;

while input_pos < input_size {
// Advance to the next layout bit
cur_layout_bit >>= 1;

if cur_layout_bit == 0 {
cur_layout_bit = 0x80;
index_cur_layout_byte = index_out_ptr;
output.push(0);
index_out_ptr += 1;
}

let (mut group_pos, mut group_size) = utils::search(input_pos, bytes, 0x111);

// If the group isn't larger than 2 bytes, copying the input without compression is smaller
Expand Down Expand Up @@ -166,16 +175,6 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
// Move forward in the input by the size of the group
input_pos += group_size as usize;
}

// Advance to the next layout bit
cur_layout_bit >>= 1;

if cur_layout_bit == 0 {
cur_layout_bit = 0x80;
index_cur_layout_byte = index_out_ptr;
output.push(0);
index_out_ptr += 1;
}
}

Ok(output.into_boxed_slice())
Expand Down

0 comments on commit 6887fba

Please sign in to comment.