summaryrefslogtreecommitdiffstats
path: root/vendor/ruzstd/src/decoding/scratch.rs
blob: 2bd753bdedf05822555c9160ea86f987af3c41cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use super::super::blocks::sequence_section::Sequence;
use super::decodebuffer::Decodebuffer;
use crate::decoding::dictionary::Dictionary;
use crate::fse::FSETable;
use crate::huff0::HuffmanTable;

pub struct DecoderScratch {
    pub huf: HuffmanScratch,
    pub fse: FSEScratch,
    pub buffer: Decodebuffer,
    pub offset_hist: [u32; 3],

    pub literals_buffer: Vec<u8>,
    pub sequences: Vec<Sequence>,
    pub block_content_buffer: Vec<u8>,
}

impl DecoderScratch {
    pub fn new(window_size: usize) -> DecoderScratch {
        DecoderScratch {
            huf: HuffmanScratch {
                table: HuffmanTable::new(),
            },
            fse: FSEScratch {
                offsets: FSETable::new(),
                of_rle: None,
                literal_lengths: FSETable::new(),
                ll_rle: None,
                match_lengths: FSETable::new(),
                ml_rle: None,
            },
            buffer: Decodebuffer::new(window_size),
            offset_hist: [1, 4, 8],

            block_content_buffer: Vec::new(),
            literals_buffer: Vec::new(),
            sequences: Vec::new(),
        }
    }

    pub fn reset(&mut self, window_size: usize) {
        self.offset_hist = [1, 4, 8];
        self.literals_buffer.clear();
        self.sequences.clear();
        self.block_content_buffer.clear();

        self.buffer.reset(window_size);

        self.fse.literal_lengths.reset();
        self.fse.match_lengths.reset();
        self.fse.offsets.reset();
        self.fse.ll_rle = None;
        self.fse.ml_rle = None;
        self.fse.of_rle = None;

        self.huf.table.reset();
    }

    pub fn use_dict(&mut self, dict: &Dictionary) {
        self.fse = dict.fse.clone();
        self.huf = dict.huf.clone();
        self.offset_hist = dict.offset_hist;
        self.buffer.dict_content = dict.dict_content.clone();
    }

    /// parses the dictionary and set the tables
    /// it returns the dict_id for checking with the frame's dict_id
    pub fn load_dict(
        &mut self,
        raw: &[u8],
    ) -> Result<u32, super::dictionary::DictionaryDecodeError> {
        let dict = super::dictionary::Dictionary::decode_dict(raw)?;

        self.huf = dict.huf.clone();
        self.fse = dict.fse.clone();
        self.offset_hist = dict.offset_hist;
        self.buffer.dict_content = dict.dict_content.clone();
        Ok(dict.id)
    }
}

#[derive(Clone)]
pub struct HuffmanScratch {
    pub table: HuffmanTable,
}

impl HuffmanScratch {
    pub fn new() -> HuffmanScratch {
        HuffmanScratch {
            table: HuffmanTable::new(),
        }
    }
}

impl Default for HuffmanScratch {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone)]
pub struct FSEScratch {
    pub offsets: FSETable,
    pub of_rle: Option<u8>,
    pub literal_lengths: FSETable,
    pub ll_rle: Option<u8>,
    pub match_lengths: FSETable,
    pub ml_rle: Option<u8>,
}

impl FSEScratch {
    pub fn new() -> FSEScratch {
        FSEScratch {
            offsets: FSETable::new(),
            of_rle: None,
            literal_lengths: FSETable::new(),
            ll_rle: None,
            match_lengths: FSETable::new(),
            ml_rle: None,
        }
    }
}

impl Default for FSEScratch {
    fn default() -> Self {
        Self::new()
    }
}