Skip to content

Commit

Permalink
Merge pull request #36 from Shnatsel/safe-lookup-tables
Browse files Browse the repository at this point in the history
Use lookup tables safely
  • Loading branch information
sile authored Jun 29, 2019
2 parents cbf1289 + 659ffd8 commit a2971a4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
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

0 comments on commit a2971a4

Please sign in to comment.