miniz_oxide/deflate/
buffer.rs

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