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