diff --git a/miniz_oxide/src/deflate/buffer.rs b/miniz_oxide/src/deflate/buffer.rs index f246c07..64a6f83 100644 --- a/miniz_oxide/src/deflate/buffer.rs +++ b/miniz_oxide/src/deflate/buffer.rs @@ -2,6 +2,10 @@ //! to avoid stack copies. Box::new() doesn't at the moment, and using a vec means we would lose //! static length info. +use alloc::boxed::Box; +use alloc::vec; +use core::convert::TryInto; + use crate::deflate::core::{LZ_DICT_SIZE, MAX_MATCH_LEN}; /// Size of the buffer of lz77 encoded data. @@ -23,36 +27,41 @@ pub fn update_hash(current_hash: u16, byte: u8) -> u16 { } pub struct HashBuffers { - pub dict: [u8; LZ_DICT_FULL_SIZE], - pub next: [u16; LZ_DICT_SIZE], - pub hash: [u16; LZ_DICT_SIZE], + pub dict: Box<[u8; LZ_DICT_FULL_SIZE]>, + pub next: Box<[u16; LZ_DICT_SIZE]>, + pub hash: Box<[u16; LZ_DICT_SIZE]>, } impl HashBuffers { #[inline] pub fn reset(&mut self) { - *self = HashBuffers::default(); + self.dict.fill(0); + self.next.fill(0); + self.hash.fill(0); } } impl Default for HashBuffers { fn default() -> HashBuffers { HashBuffers { - dict: [0; LZ_DICT_FULL_SIZE], - next: [0; LZ_DICT_SIZE], - hash: [0; LZ_DICT_SIZE], + dict: vec![0; LZ_DICT_FULL_SIZE] + .into_boxed_slice() + .try_into() + .unwrap(), + next: vec![0; LZ_DICT_SIZE].into_boxed_slice().try_into().unwrap(), + hash: vec![0; LZ_DICT_SIZE].into_boxed_slice().try_into().unwrap(), } } } pub struct LocalBuf { - pub b: [u8; OUT_BUF_SIZE], + pub b: Box<[u8; OUT_BUF_SIZE]>, } impl Default for LocalBuf { fn default() -> LocalBuf { LocalBuf { - b: [0; OUT_BUF_SIZE], + b: vec![0; OUT_BUF_SIZE].into_boxed_slice().try_into().unwrap(), } } } diff --git a/miniz_oxide/src/deflate/core.rs b/miniz_oxide/src/deflate/core.rs index b0a532d..a9d4e5e 100644 --- a/miniz_oxide/src/deflate/core.rs +++ b/miniz_oxide/src/deflate/core.rs @@ -1195,7 +1195,7 @@ struct DictOxide { pub max_probes: [u32; 2], /// Buffer of input data. /// Padded with 1 byte to simplify matching code in `compress_fast`. - pub b: Box, + pub b: HashBuffers, pub code_buf_dict_pos: usize, pub lookahead_size: usize, @@ -1214,7 +1214,7 @@ impl DictOxide { fn new(flags: u32) -> Self { DictOxide { max_probes: probes_from_flags(flags), - b: Box::default(), + b: HashBuffers::default(), code_buf_dict_pos: 0, lookahead_size: 0, lookahead_pos: 0, @@ -1401,7 +1401,7 @@ struct ParamsOxide { pub saved_bit_buffer: u32, pub saved_bits_in: u32, - pub local_buf: Box, + pub local_buf: LocalBuf, } impl ParamsOxide { @@ -1423,7 +1423,7 @@ impl ParamsOxide { prev_return_status: TDEFLStatus::Okay, saved_bit_buffer: 0, saved_bits_in: 0, - local_buf: Box::default(), + local_buf: LocalBuf::default(), } } @@ -1448,7 +1448,7 @@ impl ParamsOxide { self.prev_return_status = TDEFLStatus::Okay; self.saved_bit_buffer = 0; self.saved_bits_in = 0; - self.local_buf.b = [0; OUT_BUF_SIZE]; + self.local_buf.b.fill(0); } } @@ -1628,7 +1628,7 @@ fn flush_block( { let mut output = callback .out - .new_output_buffer(&mut d.params.local_buf.b, d.params.out_buf_ofs); + .new_output_buffer(d.params.local_buf.b.as_mut(), d.params.out_buf_ofs); output.bit_buffer = d.params.saved_bit_buffer; output.bits_in = d.params.saved_bits_in;