diff --git a/src/encoding/blocks/compressed.rs b/src/encoding/blocks/compressed.rs index 1ecedf9..00524f8 100644 --- a/src/encoding/blocks/compressed.rs +++ b/src/encoding/blocks/compressed.rs @@ -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); diff --git a/src/huff0/huff0_encoder.rs b/src/huff0/huff0_encoder.rs index 7659773..19adc4b 100644 --- a/src/huff0/huff0_encoder.rs +++ b/src/huff0/huff0_encoder.rs @@ -32,6 +32,7 @@ impl HuffmanEncoder { writer.dump() } pub fn encode4x(&mut self, data: &[u8]) -> Vec { + assert!(data.len() >= 4); let split_size = (data.len() + 3) / 4; let src1 = &data[..split_size]; let src2 = &data[split_size..split_size * 2];