async_compression/codec/xz2/
decoder.rs

1use std::{fmt, io};
2
3use xz2::stream::{Action, Status, Stream};
4
5use crate::{codec::Decode, util::PartialBuffer};
6
7pub struct Xz2Decoder {
8    stream: Stream,
9}
10
11impl fmt::Debug for Xz2Decoder {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        f.debug_struct("Xz2Decoder").finish_non_exhaustive()
14    }
15}
16
17impl Xz2Decoder {
18    pub fn new(mem_limit: u64) -> Self {
19        Self {
20            stream: Stream::new_auto_decoder(mem_limit, 0).unwrap(),
21        }
22    }
23}
24
25impl Decode for Xz2Decoder {
26    fn reinit(&mut self) -> io::Result<()> {
27        *self = Self::new(self.stream.memlimit());
28        Ok(())
29    }
30
31    fn decode(
32        &mut self,
33        input: &mut PartialBuffer<impl AsRef<[u8]>>,
34        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
35    ) -> io::Result<bool> {
36        let previous_in = self.stream.total_in() as usize;
37        let previous_out = self.stream.total_out() as usize;
38
39        let status = self
40            .stream
41            .process(input.unwritten(), output.unwritten_mut(), Action::Run)?;
42
43        input.advance(self.stream.total_in() as usize - previous_in);
44        output.advance(self.stream.total_out() as usize - previous_out);
45
46        match status {
47            Status::Ok => Ok(false),
48            Status::StreamEnd => Ok(true),
49            Status::GetCheck => Err(io::Error::new(
50                io::ErrorKind::Other,
51                "Unexpected lzma integrity check",
52            )),
53            Status::MemNeeded => Err(io::Error::new(io::ErrorKind::Other, "More memory needed")),
54        }
55    }
56
57    fn flush(
58        &mut self,
59        _output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
60    ) -> io::Result<bool> {
61        // While decoding flush is a noop
62        Ok(true)
63    }
64
65    fn finish(
66        &mut self,
67        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
68    ) -> io::Result<bool> {
69        let previous_out = self.stream.total_out() as usize;
70
71        let status = self
72            .stream
73            .process(&[], output.unwritten_mut(), Action::Finish)?;
74
75        output.advance(self.stream.total_out() as usize - previous_out);
76
77        match status {
78            Status::Ok => Ok(false),
79            Status::StreamEnd => Ok(true),
80            Status::GetCheck => Err(io::Error::new(
81                io::ErrorKind::Other,
82                "Unexpected lzma integrity check",
83            )),
84            Status::MemNeeded => Err(io::Error::new(io::ErrorKind::Other, "More memory needed")),
85        }
86    }
87}