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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use crate::decode::lzbuffer::LzBuffer;
use crate::decode::lzma::{DecoderState, LzmaProperties};
use crate::decode::{lzbuffer, rangecoder};
use crate::error;
use byteorder::{BigEndian, ReadBytesExt};
use std::io;
use std::io::Read;

#[derive(Debug)]
/// Raw decoder for LZMA2.
pub struct Lzma2Decoder {
    lzma_state: DecoderState,
}

impl Lzma2Decoder {
    /// Creates a new object ready for decompressing data that it's given.
    pub fn new() -> Lzma2Decoder {
        Lzma2Decoder {
            lzma_state: DecoderState::new(
                LzmaProperties {
                    lc: 0,
                    lp: 0,
                    pb: 0,
                },
                None,
            ),
        }
    }

    /// Performs the equivalent of replacing this decompression state with a freshly allocated copy.
    ///
    /// This function may not allocate memory and will attempt to reuse any previously allocated resources.
    #[cfg(feature = "raw_decoder")]
    pub fn reset(&mut self) {
        self.lzma_state.reset_state(LzmaProperties {
            lc: 0,
            lp: 0,
            pb: 0,
        });
    }

    /// Decompresses the input data into the output, consuming only as much input as needed and writing as much output as possible.
    pub fn decompress<W: io::Write, R: io::BufRead>(
        &mut self,
        input: &mut R,
        output: &mut W,
    ) -> error::Result<()> {
        let mut accum = lzbuffer::LzAccumBuffer::from_stream(output, usize::MAX);

        loop {
            let status = input.read_u8().map_err(|e| {
                error::Error::LzmaError(format!("LZMA2 expected new status: {}", e))
            })?;

            lzma_info!("LZMA2 status: {}", status);

            if status == 0 {
                lzma_info!("LZMA2 end of input");
                break;
            } else if status == 1 {
                // uncompressed reset dict
                Self::parse_uncompressed(&mut accum, input, true)?;
            } else if status == 2 {
                // uncompressed no reset
                Self::parse_uncompressed(&mut accum, input, false)?;
            } else {
                self.parse_lzma(&mut accum, input, status)?;
            }
        }

        accum.finish()?;
        Ok(())
    }

    fn parse_lzma<R, W>(
        &mut self,
        accum: &mut lzbuffer::LzAccumBuffer<W>,
        input: &mut R,
        status: u8,
    ) -> error::Result<()>
    where
        R: io::BufRead,
        W: io::Write,
    {
        if status & 0x80 == 0 {
            return Err(error::Error::LzmaError(format!(
                "LZMA2 invalid status {}, must be 0, 1, 2 or >= 128",
                status
            )));
        }

        let reset_dict: bool;
        let reset_state: bool;
        let reset_props: bool;
        match (status >> 5) & 0x3 {
            0 => {
                reset_dict = false;
                reset_state = false;
                reset_props = false;
            }
            1 => {
                reset_dict = false;
                reset_state = true;
                reset_props = false;
            }
            2 => {
                reset_dict = false;
                reset_state = true;
                reset_props = true;
            }
            3 => {
                reset_dict = true;
                reset_state = true;
                reset_props = true;
            }
            _ => unreachable!(),
        }

        let unpacked_size = input
            .read_u16::<BigEndian>()
            .map_err(|e| error::Error::LzmaError(format!("LZMA2 expected unpacked size: {}", e)))?;
        let unpacked_size = ((((status & 0x1F) as u64) << 16) | (unpacked_size as u64)) + 1;

        let packed_size = input
            .read_u16::<BigEndian>()
            .map_err(|e| error::Error::LzmaError(format!("LZMA2 expected packed size: {}", e)))?;
        let packed_size = (packed_size as u64) + 1;

        lzma_info!(
            "LZMA2 compressed block {{ unpacked_size: {}, packed_size: {}, reset_dict: {}, reset_state: {}, reset_props: {} }}",
            unpacked_size,
            packed_size,
            reset_dict,
            reset_state,
            reset_props
        );

        if reset_dict {
            accum.reset()?;
        }

        if reset_state {
            let new_props = if reset_props {
                let props = input.read_u8().map_err(|e| {
                    error::Error::LzmaError(format!("LZMA2 expected new properties: {}", e))
                })?;

                let mut pb = props as u32;
                if pb >= 225 {
                    return Err(error::Error::LzmaError(format!(
                        "LZMA2 invalid properties: {} must be < 225",
                        pb
                    )));
                }

                let lc = pb % 9;
                pb /= 9;
                let lp = pb % 5;
                pb /= 5;

                if lc + lp > 4 {
                    return Err(error::Error::LzmaError(format!(
                        "LZMA2 invalid properties: lc + lp ({} + {}) must be <= 4",
                        lc, lp
                    )));
                }

                lzma_info!("Properties {{ lc: {}, lp: {}, pb: {} }}", lc, lp, pb);
                LzmaProperties { lc, lp, pb }
            } else {
                self.lzma_state.lzma_props
            };

            self.lzma_state.reset_state(new_props);
        }

        self.lzma_state
            .set_unpacked_size(Some(unpacked_size + accum.len() as u64));

        let mut taken = input.take(packed_size);
        let mut rangecoder = rangecoder::RangeDecoder::new(&mut taken)
            .map_err(|e| error::Error::LzmaError(format!("LZMA input too short: {}", e)))?;
        self.lzma_state.process(accum, &mut rangecoder)
    }

    fn parse_uncompressed<R, W>(
        accum: &mut lzbuffer::LzAccumBuffer<W>,
        input: &mut R,
        reset_dict: bool,
    ) -> error::Result<()>
    where
        R: io::BufRead,
        W: io::Write,
    {
        let unpacked_size = input
            .read_u16::<BigEndian>()
            .map_err(|e| error::Error::LzmaError(format!("LZMA2 expected unpacked size: {}", e)))?;
        let unpacked_size = (unpacked_size as usize) + 1;

        lzma_info!(
            "LZMA2 uncompressed block {{ unpacked_size: {}, reset_dict: {} }}",
            unpacked_size,
            reset_dict
        );

        if reset_dict {
            accum.reset()?;
        }

        let mut buf = vec![0; unpacked_size];
        input.read_exact(buf.as_mut_slice()).map_err(|e| {
            error::Error::LzmaError(format!(
                "LZMA2 expected {} uncompressed bytes: {}",
                unpacked_size, e
            ))
        })?;
        accum.append_bytes(buf.as_slice());

        Ok(())
    }
}