| 1 | //! Buffer wrappers implementing default so we can allocate the buffers with `Box::default()` |
| 2 | //! to avoid stack copies. Box::new() doesn't at the moment, and using a vec means we would lose |
| 3 | //! static length info. |
| 4 | |
| 5 | use crate::deflate::core::{LZ_DICT_SIZE, MAX_MATCH_LEN}; |
| 6 | use alloc::boxed::Box; |
| 7 | use alloc::vec; |
| 8 | |
| 9 | /// Size of the buffer of lz77 encoded data. |
| 10 | pub const LZ_CODE_BUF_SIZE: usize = 64 * 1024; |
| 11 | pub const LZ_CODE_BUF_MASK: usize = LZ_CODE_BUF_SIZE - 1; |
| 12 | /// Size of the output buffer. |
| 13 | pub const OUT_BUF_SIZE: usize = (LZ_CODE_BUF_SIZE * 13) / 10; |
| 14 | pub const LZ_DICT_FULL_SIZE: usize = LZ_DICT_SIZE + MAX_MATCH_LEN - 1 + 1; |
| 15 | |
| 16 | /// Size of hash values in the hash chains. |
| 17 | pub const LZ_HASH_BITS: i32 = 15; |
| 18 | /// How many bits to shift when updating the current hash value. |
| 19 | pub const LZ_HASH_SHIFT: i32 = (LZ_HASH_BITS + 2) / 3; |
| 20 | /// Size of the chained hash tables. |
| 21 | pub const LZ_HASH_SIZE: usize = 1 << LZ_HASH_BITS; |
| 22 | |
| 23 | #[inline ] |
| 24 | pub const fn update_hash(current_hash: u16, byte: u8) -> u16 { |
| 25 | ((current_hash << LZ_HASH_SHIFT) ^ byte as u16) & (LZ_HASH_SIZE as u16 - 1) |
| 26 | } |
| 27 | |
| 28 | pub struct HashBuffers { |
| 29 | pub dict: Box<[u8; LZ_DICT_FULL_SIZE]>, |
| 30 | pub next: Box<[u16; LZ_DICT_SIZE]>, |
| 31 | pub hash: Box<[u16; LZ_DICT_SIZE]>, |
| 32 | } |
| 33 | |
| 34 | impl HashBuffers { |
| 35 | #[inline ] |
| 36 | pub fn reset(&mut self) { |
| 37 | self.dict.fill(0); |
| 38 | self.next.fill(0); |
| 39 | self.hash.fill(0); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl Default for HashBuffers { |
| 44 | fn default() -> HashBuffers { |
| 45 | HashBuffers { |
| 46 | dict: vecResult, …>![0; LZ_DICT_FULL_SIZE] |
| 47 | .into_boxed_slice() |
| 48 | .try_into() |
| 49 | .unwrap(), |
| 50 | next: vec![0; LZ_DICT_SIZE].into_boxed_slice().try_into().unwrap(), |
| 51 | hash: vec![0; LZ_DICT_SIZE].into_boxed_slice().try_into().unwrap(), |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pub struct LocalBuf { |
| 57 | pub b: [u8; OUT_BUF_SIZE], |
| 58 | } |
| 59 | |
| 60 | impl Default for LocalBuf { |
| 61 | fn default() -> LocalBuf { |
| 62 | LocalBuf { |
| 63 | b: [0; OUT_BUF_SIZE], |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |