Skip to content

Commit

Permalink
fix blocksize for compressed blocks, a block may only contain 1<<18 -…
Browse files Browse the repository at this point in the history
… 1 uncompressed literals
  • Loading branch information
KillingSpark committed Oct 16, 2024
1 parent a4558e8 commit 98f45b8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/encoding/blocks/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn compress_literals(literals: &[u8], writer: &mut BitWriter) {
0..6 => unimplemented!("should probably just be a raw block"),
6..1024 => (0b01u8, 10),
1024..16384 => (0b10, 14),
16384..26144 => (0b11, 18),
16384..262144 => (0b11, 18),
_ => unimplemented!("too many literals"),
};
writer.write_bits(size_format, 2);
Expand Down
11 changes: 7 additions & 4 deletions src/encoding/frame_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'input> FrameCompressor<'input> {
// the block in repetition until the last block is reached.
let mut index = 0;
while index < self.uncompressed_data.len() {
let last_block = index + MAX_BLOCK_SIZE > self.uncompressed_data.len();
let last_block = index + MAX_BLOCK_SIZE >= self.uncompressed_data.len();
// We read till the end of the data, or till the max block size, whichever comes sooner
let block_size = if last_block {
self.uncompressed_data.len() - index
Expand All @@ -122,7 +122,8 @@ impl<'input> FrameCompressor<'input> {
CompressionLevel::Fastest => {
let mut index = 0;
while index < self.uncompressed_data.len() {
let last_block = index + MAX_BLOCK_SIZE > self.uncompressed_data.len();
const MAX_BLOCK_SIZE: usize = (1 << 18) - 1;
let last_block = index + MAX_BLOCK_SIZE >= self.uncompressed_data.len();
// We read till the end of the data, or till the max block size, whichever comes sooner
let block_size = if last_block {
self.uncompressed_data.len() - index
Expand Down Expand Up @@ -178,8 +179,10 @@ mod tests {

#[test]
fn very_simple_compress() {
let mut mock_data = vec![0;1024];
mock_data.extend(vec![1;1024]);
let mut mock_data = vec![0; 1 << 17];
mock_data.extend(vec![1; (1 << 17) - 1]);
mock_data.extend(vec![2; 1 << 17]);
mock_data.extend(vec![3; (1 << 17) - 1]);
let compressor = FrameCompressor::new(&mock_data, super::CompressionLevel::Fastest);
let mut output: Vec<u8> = Vec::new();
compressor.compress(&mut output);
Expand Down

0 comments on commit 98f45b8

Please sign in to comment.