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