Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lookup tables safely #36

Merged
merged 2 commits into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/deflate/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Decoder {
}
length_code => {
let (base, extra_bits) =
unsafe { *LENGTH_TABLE.get_unchecked(length_code as usize - 257) };
LENGTH_TABLE[length_code as usize - 257];
let extra = reader.read_bits_unchecked(extra_bits);
Symbol::Share {
length: base + extra,
Expand All @@ -227,7 +227,7 @@ impl Decoder {
R: io::Read,
{
let decoded = self.distance.decode_unchecked(reader) as usize;
let (base, extra_bits) = unsafe { *DISTANCE_TABLE.get_unchecked(decoded) };
let (base, extra_bits) = DISTANCE_TABLE[decoded];
let extra = reader.read_bits_unchecked(extra_bits);
base + extra
}
Expand Down
10 changes: 4 additions & 6 deletions src/huffman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ impl Builder for DecoderBuilder {
);
return Err(io::Error::new(io::ErrorKind::InvalidData, message));
}
unsafe {
*self.table.get_unchecked_mut(i) = value;
}
self.table[i] = value;
}
Ok(())
}
Expand Down Expand Up @@ -137,11 +135,11 @@ impl Decoder {
R: io::Read,
{
let code = reader.peek_bits_unchecked(self.eob_bitwidth);
let mut value = unsafe { *self.table.get_unchecked(code as usize) };
let mut value = self.table[code as usize];
let mut bitwidth = (value & 0b1_1111) as u8;
if bitwidth > self.eob_bitwidth {
let code = reader.peek_bits_unchecked(self.max_bitwidth);
value = unsafe { *self.table.get_unchecked(code as usize) };
value = self.table[code as usize];
bitwidth = (value & 0b1_1111) as u8;
if bitwidth > self.max_bitwidth {
reader.set_last_error(invalid_data_error!("Invalid huffman coded stream"));
Expand Down Expand Up @@ -216,7 +214,7 @@ impl Encoder {
symbol,
self.table.len()
);
unsafe { self.table.get_unchecked(symbol as usize) }.clone()
self.table[symbol as usize].clone()
}
pub fn used_max_symbol(&self) -> Option<u16> {
self.table
Expand Down
2 changes: 1 addition & 1 deletion src/lz77/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl LargePrefixTable {
let p2 = prefix[2];

let i = (p0 << 8) + p1;
let positions = unsafe { self.table.get_unchecked_mut(i) };
let positions = &mut self.table[i];
for &mut (key, ref mut value) in positions.iter_mut() {
if key == p2 {
let old = *value;
Expand Down