Skip to content

Commit

Permalink
Move fn build_tables to impl CompressedBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
anforowicz committed Jan 7, 2025
1 parent 8be0d4e commit 042a5b1
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,56 +544,51 @@ impl Decompressor {
self.header.code_lengths[i] = 0;
}

Self::build_tables(
self.header.hlit,
&self.header.code_lengths,
&mut self.compression,
)?;
self.compression
.build_tables(self.header.hlit, &self.header.code_lengths)?;
self.state = State::CompressedData;
Ok(())
}
}

fn build_tables(
hlit: usize,
code_lengths: &[u8],
compression: &mut CompressedBlock,
) -> Result<(), DecompressionError> {
impl CompressedBlock {
fn build_tables(&mut self, hlit: usize, code_lengths: &[u8]) -> Result<(), DecompressionError> {
// If there is no code assigned for the EOF symbol then the bitstream is invalid.
if code_lengths[256] == 0 {
// TODO: Return a dedicated error in this case.
return Err(DecompressionError::BadLiteralLengthHuffmanTree);
}

let mut codes = [0; 288];
compression.secondary_table.clear();
self.secondary_table.clear();
if !huffman::build_table(
&code_lengths[..hlit],
&LITLEN_TABLE_ENTRIES,
&mut codes[..hlit],
&mut *compression.litlen_table,
&mut compression.secondary_table,
&mut *self.litlen_table,
&mut self.secondary_table,
false,
true,
) {
return Err(DecompressionError::BadCodeLengthHuffmanTree);
}

compression.eof_code = codes[256];
compression.eof_mask = (1 << code_lengths[256]) - 1;
compression.eof_bits = code_lengths[256];
self.eof_code = codes[256];
self.eof_mask = (1 << code_lengths[256]) - 1;
self.eof_bits = code_lengths[256];

// Build the distance code table.
let lengths = &code_lengths[288..320];
if lengths == [0; 32] {
compression.dist_table.fill(0);
self.dist_table.fill(0);
} else {
let mut dist_codes = [0; 32];
if !huffman::build_table(
lengths,
&tables::DISTANCE_TABLE_ENTRIES,
&mut dist_codes,
&mut *compression.dist_table,
&mut compression.dist_secondary_table,
&mut *self.dist_table,
&mut self.dist_secondary_table,
true,
false,
) {
Expand All @@ -603,9 +598,7 @@ impl Decompressor {

Ok(())
}
}

impl CompressedBlock {
/// Returns:
/// - Whether this compressed block ended or not
/// - The new value of `output_index`
Expand Down Expand Up @@ -1199,7 +1192,7 @@ mod tests {
eof_mask: 0,
eof_bits: 0,
};
Decompressor::build_tables(288, &FIXED_CODE_LENGTHS, &mut compression).unwrap();
compression.build_tables(288, &FIXED_CODE_LENGTHS).unwrap();

assert_eq!(compression.litlen_table[..512], FIXED_LITLEN_TABLE);
assert_eq!(compression.dist_table[..32], FIXED_DIST_TABLE);
Expand Down

0 comments on commit 042a5b1

Please sign in to comment.