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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! rust-compression
//!
//! # Licensing
//! This Source Code is subject to the terms of the Mozilla Public License
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! <http://mozilla.org/MPL/2.0/>.

use crate::bitio::direction::left::Left;
use crate::bitio::reader::{BitRead, BitReader};
use crate::error::CompressionError;
use crate::huffman::decoder::HuffmanDecoder;
use crate::lzhuf::{LzhufMethod, LZSS_MIN_MATCH};
use crate::lzss::decoder::LzssDecoder;
use crate::lzss::LzssCode;
use crate::traits::decoder::{
    BitDecodeService, BitDecoder, BitDecoderImpl, DecodeIterator, Decoder,
};
#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use alloc::vec;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[derive(Debug)]
enum LzhufHuffmanDecoder {
    HuffmanDecoder(HuffmanDecoder<Left>),
    Default(u16),
}

impl LzhufHuffmanDecoder {
    pub(crate) fn dec<R: BitRead, I: Iterator<Item = u8>>(
        &mut self,
        reader: &mut R,
        iter: &mut I,
    ) -> Result<Option<u16>, CompressionError> {
        match *self {
            LzhufHuffmanDecoder::HuffmanDecoder(ref mut hd) => hd
                .dec(reader, iter)
                .map_err(|_| CompressionError::DataError),
            LzhufHuffmanDecoder::Default(s) => Ok(Some(s)),
        }
    }
}

#[derive(Debug)]
pub(crate) struct LzhufDecoderInner {
    offset_len: usize,
    min_match: usize,
    block_len: usize,
    symbol_decoder: Option<LzhufHuffmanDecoder>,
    offset_decoder: Option<LzhufHuffmanDecoder>,
}

impl LzhufDecoderInner {
    const SEARCH_TAB_LEN: usize = 12;

    pub(crate) fn new(method: LzhufMethod) -> Self {
        Self {
            offset_len: method.offset_bits(),
            min_match: LZSS_MIN_MATCH,
            block_len: 0,

            symbol_decoder: None,
            offset_decoder: None,
        }
    }

    fn dec_len<R: BitRead, I: Iterator<Item = u8>>(
        &mut self,
        reader: &mut R,
        iter: &mut I,
    ) -> Result<u8, CompressionError> {
        let mut c = reader
            .read_bits::<u8, _>(3, iter)
            .map_err(|_| CompressionError::UnexpectedEof)?
            .data();
        if c == 7 {
            while reader
                .read_bits::<u8, _>(1, iter)
                .map_err(|_| CompressionError::UnexpectedEof)?
                .data()
                == 1
            {
                c += 1;
            }
        }
        Ok(c)
    }

    fn dec_len_tree<R: BitRead, I: Iterator<Item = u8>>(
        &mut self,
        tbit_len: usize,
        reader: &mut R,
        iter: &mut I,
    ) -> Result<LzhufHuffmanDecoder, CompressionError> {
        let len = reader
            .read_bits::<u16, _>(tbit_len, iter)
            .map_err(|_| CompressionError::UnexpectedEof)?
            .data() as usize;
        if len == 0 {
            Ok(LzhufHuffmanDecoder::Default(
                reader
                    .read_bits::<u16, _>(tbit_len, iter)
                    .map_err(|_| CompressionError::UnexpectedEof)?
                    .data(),
            ))
        } else {
            let mut ll = Vec::new();
            while ll.len() < len {
                if ll.len() == 3 {
                    for _ in 0..reader
                        .read_bits::<u8, _>(2, iter)
                        .map_err(|_| CompressionError::UnexpectedEof)?
                        .data()
                    {
                        ll.push(0);
                    }
                    if ll.len() > len {
                        return Err(CompressionError::DataError);
                    }
                    if ll.len() == len {
                        break;
                    }
                }
                ll.push(self.dec_len(reader, iter)?);
            }
            Ok(LzhufHuffmanDecoder::HuffmanDecoder(
                HuffmanDecoder::new(&ll, 5)
                    .map_err(|_| CompressionError::DataError)?,
            ))
        }
    }

    fn dec_symb_tree<R: BitRead, I: Iterator<Item = u8>>(
        &mut self,
        len_decoder: &mut LzhufHuffmanDecoder,
        reader: &mut R,
        iter: &mut I,
    ) -> Result<LzhufHuffmanDecoder, CompressionError> {
        let len = reader
            .read_bits::<u16, _>(9, iter)
            .map_err(|_| CompressionError::UnexpectedEof)?
            .data() as usize;
        if len == 0 {
            Ok(LzhufHuffmanDecoder::Default(
                reader
                    .read_bits::<u16, _>(9, iter)
                    .map_err(|_| CompressionError::UnexpectedEof)?
                    .data(),
            ))
        } else {
            let mut ll = Vec::new();
            while ll.len() < len {
                match len_decoder.dec(reader, iter)? {
                    None => return Err(CompressionError::UnexpectedEof),
                    Some(0) => ll.push(0),
                    Some(1) => {
                        for _ in 0..(3 + reader
                            .read_bits::<u8, _>(4, iter)
                            .map_err(|_| CompressionError::UnexpectedEof)?
                            .data())
                        {
                            ll.push(0);
                        }
                    }
                    Some(2) => {
                        for _ in 0..(20
                            + reader
                                .read_bits::<u16, _>(9, iter)
                                .map_err(|_| CompressionError::UnexpectedEof)?
                                .data())
                        {
                            ll.push(0);
                        }
                    }
                    Some(n) => ll.push((n - 2) as u8),
                }
            }
            Ok(LzhufHuffmanDecoder::HuffmanDecoder(
                HuffmanDecoder::new(&ll, Self::SEARCH_TAB_LEN)
                    .map_err(|_| CompressionError::DataError)?,
            ))
        }
    }

    fn dec_offs_tree<R: BitRead, I: Iterator<Item = u8>>(
        &mut self,
        pbit_len: usize,
        reader: &mut R,
        iter: &mut I,
    ) -> Result<LzhufHuffmanDecoder, CompressionError> {
        let len = reader
            .read_bits::<u16, _>(pbit_len, iter)
            .map_err(|_| CompressionError::UnexpectedEof)?
            .data() as usize;
        if len == 0 {
            Ok(LzhufHuffmanDecoder::Default(
                reader
                    .read_bits::<u16, _>(pbit_len, iter)
                    .map_err(|_| CompressionError::UnexpectedEof)?
                    .data(),
            ))
        } else {
            Ok(LzhufHuffmanDecoder::HuffmanDecoder(
                HuffmanDecoder::new(
                    &(0..len)
                        .map(|_| self.dec_len(reader, iter))
                        .collect::<Result<Vec<u8>, CompressionError>>()?,
                    Self::SEARCH_TAB_LEN,
                )
                .map_err(|_| CompressionError::DataError)?,
            ))
        }
    }

    fn init_block<R: BitRead, I: Iterator<Item = u8>>(
        &mut self,
        reader: &mut R,
        iter: &mut I,
    ) -> Result<bool, CompressionError> {
        match reader
            .read_bits::<u16, _>(16, iter)
            .map(|x| (x.data(), x.len()))
            .map_err(|_| CompressionError::UnexpectedEof)?
        {
            (s, 16) if s != 0 => {
                self.block_len = s as usize;
                let mut lt = self.dec_len_tree(5, reader, iter)?;
                self.symbol_decoder =
                    Some(self.dec_symb_tree(&mut lt, reader, iter)?);
                let offlen = self.offset_len;
                self.offset_decoder =
                    Some(self.dec_offs_tree(offlen, reader, iter)?);
                Ok(true)
            }
            _ => Ok(false),
        }
    }
}

impl BitDecodeService for LzhufDecoderInner {
    type Direction = Left;
    type Error = CompressionError;
    type Output = LzssCode;

    fn next<I: Iterator<Item = u8>>(
        &mut self,
        reader: &mut BitReader<Self::Direction>,
        iter: &mut I,
    ) -> Result<Option<LzssCode>, CompressionError> {
        if self.block_len == 0 && !self.init_block(reader, iter)? {
            return Ok(None);
        }
        self.block_len -= 1;
        let sym = self
            .symbol_decoder
            .as_mut()
            .unwrap()
            .dec(reader, iter)?
            .ok_or_else(|| CompressionError::UnexpectedEof)?
            as usize;
        if sym <= 255 {
            Ok(Some(LzssCode::Symbol(sym as u8)))
        } else {
            let len = sym - 256 + self.min_match;
            let mut pos = self
                .offset_decoder
                .as_mut()
                .unwrap()
                .dec(reader, iter)?
                .ok_or_else(|| CompressionError::UnexpectedEof)?
                as usize;
            if pos > 1 {
                pos = (1 << (pos - 1))
                    | reader
                        .read_bits::<u16, _>(pos - 1, iter)
                        .map_err(|_| CompressionError::UnexpectedEof)?
                        .data() as usize;
            }
            Ok(Some(LzssCode::Reference { len, pos }))
        }
    }
}

#[derive(Debug)]
pub(crate) struct LzhufDecoderBase {
    lzss_decoder: LzssDecoder,
    inner: LzhufDecoderInner,
}

impl LzhufDecoderBase {
    const MAX_BLOCK_SIZE: usize = 0x1_0000;

    pub(crate) fn new(method: LzhufMethod) -> Self {
        Self {
            lzss_decoder: LzssDecoder::new(Self::MAX_BLOCK_SIZE),
            inner: LzhufDecoderInner::new(method),
        }
    }
}

impl BitDecodeService for LzhufDecoderBase {
    type Direction = Left;
    type Error = CompressionError;
    type Output = u8;

    fn next<I: Iterator<Item = u8>>(
        &mut self,
        reader: &mut BitReader<Self::Direction>,
        iter: &mut I,
    ) -> Result<Option<u8>, Self::Error> {
        let mut bd = BitDecoder::<LzhufDecoderInner, _, _>::with_service(
            &mut self.inner,
            reader,
        );
        self.lzss_decoder
            .next(&mut DecodeIterator::<I, _, _>::new(iter, &mut bd).flatten())
            .transpose()
    }
}

#[derive(Debug)]
pub struct LzhufDecoder {
    inner: BitDecoderImpl<LzhufDecoderBase>,
}

impl LzhufDecoder {
    pub fn new(method: &LzhufMethod) -> Self {
        Self {
            inner: BitDecoderImpl::<LzhufDecoderBase>::with_service(
                LzhufDecoderBase::new(*method),
                BitReader::new(),
            ),
        }
    }
}

impl Decoder for LzhufDecoder {
    type Input = u8;
    type Output = u8;
    type Error = CompressionError;

    fn next<I: Iterator<Item = Self::Input>>(
        &mut self,
        iter: &mut I,
    ) -> Option<Result<Self::Output, Self::Error>> {
        self.inner.next(iter)
    }
}