Skip to content

Commit

Permalink
Don't hardcode deflate tables (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia authored Oct 14, 2024
1 parent de4c8ff commit 797304d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 570 deletions.
54 changes: 6 additions & 48 deletions src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use simd_adler32::Adler32;
use crate::{
huffman::{self, build_table},
tables::{
self, CLCL_ORDER, DIST_SYM_TO_DIST_BASE, DIST_SYM_TO_DIST_EXTRA,
FDEFLATE_DIST_DECODE_TABLE, FDEFLATE_LITLEN_DECODE_TABLE, FIXED_CODE_LENGTHS,
self, CLCL_ORDER, DIST_SYM_TO_DIST_BASE, DIST_SYM_TO_DIST_EXTRA, FIXED_CODE_LENGTHS,
LEN_SYM_TO_LEN_BASE, LEN_SYM_TO_LEN_EXTRA, LITLEN_TABLE_ENTRIES,
},
};
Expand Down Expand Up @@ -77,16 +76,6 @@ struct CompressedBlock {
eof_bits: u8,
}

const FDEFLATE_COMPRESSED_BLOCK: CompressedBlock = CompressedBlock {
litlen_table: FDEFLATE_LITLEN_DECODE_TABLE,
secondary_table: Vec::new(),
dist_table: FDEFLATE_DIST_DECODE_TABLE,
dist_secondary_table: Vec::new(),
eof_code: 0x8ff,
eof_mask: 0xfff,
eof_bits: 0xc,
};

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum State {
ZlibHeader,
Expand Down Expand Up @@ -360,18 +349,11 @@ impl Decompressor {
self.header.code_lengths[i] = 0;
}

if self.header.hdist == 1
&& self.header.code_lengths[..286] == tables::HUFFMAN_LENGTHS
&& self.header.code_lengths[288] == 1
{
self.compression = FDEFLATE_COMPRESSED_BLOCK;
} else {
Self::build_tables(
self.header.hlit,
&self.header.code_lengths,
&mut self.compression,
)?;
}
Self::build_tables(
self.header.hlit,
&self.header.code_lengths,
&mut self.compression,
)?;
self.state = State::CompressedData;
Ok(())
}
Expand Down Expand Up @@ -998,30 +980,6 @@ mod tests {
}
}

#[test]
fn fdeflate_table() {
let mut compression = CompressedBlock {
litlen_table: [0; 4096],
dist_table: [0; 512],
secondary_table: Vec::new(),
dist_secondary_table: Vec::new(),
eof_code: 0,
eof_mask: 0,
eof_bits: 0,
};
let mut lengths = tables::HUFFMAN_LENGTHS.to_vec();
lengths.resize(288, 0);
lengths.push(1);
lengths.resize(320, 0);
Decompressor::build_tables(286, &lengths, &mut compression).unwrap();

assert_eq!(
compression, FDEFLATE_COMPRESSED_BLOCK,
"{:#x?}",
compression
);
}

#[test]
fn it_works() {
roundtrip(b"Hello world!");
Expand Down
Loading

0 comments on commit 797304d

Please sign in to comment.