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

use std::fmt::{Debug, Formatter, Result as FmtResult};
use std::io::Result;
use xz2::stream::{Action, Status, Stream};

pub struct Xz2Decoder {
    stream: Stream,
}

impl Debug for Xz2Decoder {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "LzmaDecoder")
    }
}

impl Xz2Decoder {
    pub fn new() -> Self {
        Self {
            stream: Stream::new_auto_decoder(u64::max_value(), 0).unwrap(),
        }
    }
}

impl Decode for Xz2Decoder {
    fn reinit(&mut self) -> Result<()> {
        *self = Self::new();
        Ok(())
    }

    fn decode(
        &mut self,
        input: &mut PartialBuffer<impl AsRef<[u8]>>,
        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
    ) -> Result<bool> {
        let previous_in = self.stream.total_in() as usize;
        let previous_out = self.stream.total_out() as usize;

        let status = self
            .stream
            .process(input.unwritten(), output.unwritten_mut(), Action::Run)?;

        input.advance(self.stream.total_in() as usize - previous_in);
        output.advance(self.stream.total_out() as usize - previous_out);

        match status {
            Status::Ok => Ok(false),
            Status::StreamEnd => Ok(true),
            Status::GetCheck => panic!("Unexpected lzma integrity check"),
            Status::MemNeeded => Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                "More memory needed",
            )),
        }
    }

    fn flush(
        &mut self,
        _output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
    ) -> Result<bool> {
        // While decoding flush is a noop
        Ok(true)
    }

    fn finish(
        &mut self,
        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
    ) -> Result<bool> {
        let previous_out = self.stream.total_out() as usize;

        let status = self
            .stream
            .process(&[], output.unwritten_mut(), Action::Finish)?;

        output.advance(self.stream.total_out() as usize - previous_out);

        match status {
            Status::Ok => Ok(false),
            Status::StreamEnd => Ok(true),
            Status::GetCheck => panic!("Unexpected lzma integrity check"),
            Status::MemNeeded => Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                "More memory needed",
            )),
        }
    }
}