Skip to content

Commit 797304d

Browse files
authored
Don't hardcode deflate tables (#32)
1 parent de4c8ff commit 797304d

File tree

2 files changed

+6
-570
lines changed

2 files changed

+6
-570
lines changed

src/decompress.rs

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use simd_adler32::Adler32;
33
use crate::{
44
huffman::{self, build_table},
55
tables::{
6-
self, CLCL_ORDER, DIST_SYM_TO_DIST_BASE, DIST_SYM_TO_DIST_EXTRA,
7-
FDEFLATE_DIST_DECODE_TABLE, FDEFLATE_LITLEN_DECODE_TABLE, FIXED_CODE_LENGTHS,
6+
self, CLCL_ORDER, DIST_SYM_TO_DIST_BASE, DIST_SYM_TO_DIST_EXTRA, FIXED_CODE_LENGTHS,
87
LEN_SYM_TO_LEN_BASE, LEN_SYM_TO_LEN_EXTRA, LITLEN_TABLE_ENTRIES,
98
},
109
};
@@ -77,16 +76,6 @@ struct CompressedBlock {
7776
eof_bits: u8,
7877
}
7978

80-
const FDEFLATE_COMPRESSED_BLOCK: CompressedBlock = CompressedBlock {
81-
litlen_table: FDEFLATE_LITLEN_DECODE_TABLE,
82-
secondary_table: Vec::new(),
83-
dist_table: FDEFLATE_DIST_DECODE_TABLE,
84-
dist_secondary_table: Vec::new(),
85-
eof_code: 0x8ff,
86-
eof_mask: 0xfff,
87-
eof_bits: 0xc,
88-
};
89-
9079
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
9180
enum State {
9281
ZlibHeader,
@@ -360,18 +349,11 @@ impl Decompressor {
360349
self.header.code_lengths[i] = 0;
361350
}
362351

363-
if self.header.hdist == 1
364-
&& self.header.code_lengths[..286] == tables::HUFFMAN_LENGTHS
365-
&& self.header.code_lengths[288] == 1
366-
{
367-
self.compression = FDEFLATE_COMPRESSED_BLOCK;
368-
} else {
369-
Self::build_tables(
370-
self.header.hlit,
371-
&self.header.code_lengths,
372-
&mut self.compression,
373-
)?;
374-
}
352+
Self::build_tables(
353+
self.header.hlit,
354+
&self.header.code_lengths,
355+
&mut self.compression,
356+
)?;
375357
self.state = State::CompressedData;
376358
Ok(())
377359
}
@@ -998,30 +980,6 @@ mod tests {
998980
}
999981
}
1000982

1001-
#[test]
1002-
fn fdeflate_table() {
1003-
let mut compression = CompressedBlock {
1004-
litlen_table: [0; 4096],
1005-
dist_table: [0; 512],
1006-
secondary_table: Vec::new(),
1007-
dist_secondary_table: Vec::new(),
1008-
eof_code: 0,
1009-
eof_mask: 0,
1010-
eof_bits: 0,
1011-
};
1012-
let mut lengths = tables::HUFFMAN_LENGTHS.to_vec();
1013-
lengths.resize(288, 0);
1014-
lengths.push(1);
1015-
lengths.resize(320, 0);
1016-
Decompressor::build_tables(286, &lengths, &mut compression).unwrap();
1017-
1018-
assert_eq!(
1019-
compression, FDEFLATE_COMPRESSED_BLOCK,
1020-
"{:#x?}",
1021-
compression
1022-
);
1023-
}
1024-
1025983
#[test]
1026984
fn it_works() {
1027985
roundtrip(b"Hello world!");

0 commit comments

Comments
 (0)