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
use crate::{codec::Decode, util::PartialBuffer};

use std::io::{Error, ErrorKind, Result};

#[derive(Debug)]
pub struct XzDecoder {
    inner: crate::codec::Xz2Decoder,
    skip_padding: Option<u8>,
}

impl XzDecoder {
    pub fn new() -> Self {
        Self {
            inner: crate::codec::Xz2Decoder::new(u64::MAX),
            skip_padding: None,
        }
    }

    pub fn with_memlimit(memlimit: u64) -> Self {
        Self {
            inner: crate::codec::Xz2Decoder::new(memlimit),
            skip_padding: None,
        }
    }
}

impl Decode for XzDecoder {
    fn reinit(&mut self) -> Result<()> {
        self.skip_padding = Some(4);
        self.inner.reinit()
    }

    fn decode(
        &mut self,
        input: &mut PartialBuffer<impl AsRef<[u8]>>,
        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
    ) -> Result<bool> {
        if let Some(ref mut count) = self.skip_padding {
            while input.unwritten().first() == Some(&0) {
                input.advance(1);
                *count -= 1;
                if *count == 0 {
                    *count = 4;
                }
            }
            if input.unwritten().is_empty() {
                return Ok(true);
            }
            // If this is non-padding then it cannot start with null bytes, so it must be invalid
            // padding
            if *count != 4 {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    "stream padding was not a multiple of 4 bytes",
                ));
            }
            self.skip_padding = None;
        }
        self.inner.decode(input, output)
    }

    fn flush(
        &mut self,
        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
    ) -> Result<bool> {
        if self.skip_padding.is_some() {
            return Ok(true);
        }
        self.inner.flush(output)
    }

    fn finish(
        &mut self,
        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
    ) -> Result<bool> {
        if self.skip_padding.is_some() {
            return Ok(true);
        }
        self.inner.finish(output)
    }
}