Skip to content

Commit

Permalink
added support for literals less than 5
Browse files Browse the repository at this point in the history
  • Loading branch information
KillingSpark committed Oct 16, 2024
1 parent aca6d8a commit a84c149
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/encoding/blocks/compressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ fn compress_literals(literals: &[u8], writer: &mut BitWriter) {

let encoder_table = huff0_encoder::HuffmanTable::build_from_data(literals);
let mut encoder = huff0_encoder::HuffmanEncoder::new(encoder_table);
let encoded = encoder.encode4x(literals);

let (size_format, size_bits) = match literals.len() {
0..6 => unimplemented!("should probably just be a raw block"),
6..1024 => (0b01u8, 10),
0..6 => (0b00u8, 10),
6..1024 => (0b01, 10),
1024..16384 => (0b10, 14),
16384..262144 => (0b11, 18),
_ => unimplemented!("too many literals"),
};

let encoded;
if size_format == 0 {
encoded = encoder.encode(literals);
} else {
encoded = encoder.encode4x(literals);
}

writer.write_bits(size_format, 2);
writer.write_bits(literals.len() as u32, size_bits);
writer.write_bits(encoded.len() as u32, size_bits);
Expand Down
1 change: 1 addition & 0 deletions src/huff0/huff0_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl HuffmanEncoder {
writer.dump()
}
pub fn encode4x(&mut self, data: &[u8]) -> Vec<u8> {
assert!(data.len() >= 4);
let split_size = (data.len() + 3) / 4;
let src1 = &data[..split_size];
let src2 = &data[split_size..split_size * 2];
Expand Down

0 comments on commit a84c149

Please sign in to comment.