Skip to content

Commit

Permalink
fix(inflate): use built in fill instead of custom memset function
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindln committed Dec 16, 2024
1 parent f6c3b45 commit e6ee54e
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions miniz_oxide/src/inflate/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,6 @@ const fn num_extra_bits_for_distance_code(code: u8) -> u8 {
/// The mask used when indexing the base/extra arrays.
const BASE_EXTRA_MASK: usize = 32 - 1;

/// Sets the value of all the elements of the slice to `val`.
#[inline]
fn memset<T: Copy>(slice: &mut [T], val: T) {
for x in slice {
*x = val
}
}

/// Read an le u16 value from the slice iterator.
///
/// # Panics
Expand Down Expand Up @@ -683,11 +675,11 @@ fn undo_bytes(l: &mut LocalVars, max: u32) -> u32 {
fn start_static_table(r: &mut DecompressorOxide) {
r.table_sizes[LITLEN_TABLE] = 288;
r.table_sizes[DIST_TABLE] = 32;
memset(&mut r.code_size_literal[0..144], 8);
memset(&mut r.code_size_literal[144..256], 9);
memset(&mut r.code_size_literal[256..280], 7);
memset(&mut r.code_size_literal[280..288], 8);
memset(&mut r.code_size_dist[0..32], 5);
r.code_size_literal[0..144].fill(8);
r.code_size_literal[144..256].fill(9);
r.code_size_literal[256..280].fill(7);
r.code_size_literal[280..288].fill(8);
r.code_size_dist[0..32].fill(5);
}

#[cfg(any(
Expand Down Expand Up @@ -749,14 +741,14 @@ fn init_tree(r: &mut DecompressorOxide, l: &mut LocalVars) -> Option<Action> {
// code length in the hot code path later
// and can instead error out on the invalid symbol check
// on bogus input.
memset(&mut table.look_up[..], INVALID_CODE);
table.look_up.fill(INVALID_CODE);
// If we are initializing the huffman code length we can skip
// this since these codes can't be longer than 3 bits
// and thus only use the fast table and this table won't be accessed so
// there is no point clearing it.
// TODO: Avoid creating this table at all.
if bt != HUFFLEN_TABLE {
memset(&mut table.tree[..], 0);
table.tree.fill(0);
}

let table_size = r.table_sizes[bt] as usize;
Expand Down Expand Up @@ -1422,7 +1414,7 @@ pub fn decompress(
Action::None
})
} else {
memset(&mut r.code_size_huffman[..], 0);
r.code_size_huffman.fill(0);
l.counter = 0;
// Check that the litlen and distance are within spec.
// litlen table should be <=286 acc to the RFC and
Expand Down Expand Up @@ -1504,12 +1496,9 @@ pub fn decompress(
0
};

memset(
&mut r.len_codes[
r.len_codes[
l.counter as usize..l.counter as usize + extra_bits as usize
],
val,
);
].fill(val);
l.counter += extra_bits as u32;
Action::Jump(ReadLitlenDistTablesCodeSize)
})
Expand Down

0 comments on commit e6ee54e

Please sign in to comment.