gix_features/zlib/
mod.rs

1pub use flate2::{Decompress, Status};
2
3/// non-streaming interfaces for decompression
4pub mod inflate {
5    /// The error returned by various [Inflate methods][super::Inflate]
6    #[derive(Debug, thiserror::Error)]
7    #[allow(missing_docs)]
8    pub enum Error {
9        #[error("Could not write all bytes when decompressing content")]
10        WriteInflated(#[from] std::io::Error),
11        #[error("Could not decode zip stream, status was '{0:?}'")]
12        Inflate(#[from] flate2::DecompressError),
13        #[error("The zlib status indicated an error, status was '{0:?}'")]
14        Status(flate2::Status),
15    }
16}
17
18/// Decompress a few bytes of a zlib stream without allocation
19pub struct Inflate {
20    /// The actual decompressor doing all the work.
21    pub state: Decompress,
22}
23
24impl Default for Inflate {
25    fn default() -> Self {
26        Inflate {
27            state: Decompress::new(true),
28        }
29    }
30}
31
32impl Inflate {
33    /// Run the decompressor exactly once. Cannot be run multiple times
34    pub fn once(&mut self, input: &[u8], out: &mut [u8]) -> Result<(flate2::Status, usize, usize), inflate::Error> {
35        let before_in = self.state.total_in();
36        let before_out = self.state.total_out();
37        let status = self.state.decompress(input, out, flate2::FlushDecompress::None)?;
38        Ok((
39            status,
40            (self.state.total_in() - before_in) as usize,
41            (self.state.total_out() - before_out) as usize,
42        ))
43    }
44
45    /// Ready this instance for decoding another data stream.
46    pub fn reset(&mut self) {
47        self.state.reset(true);
48    }
49}
50
51///
52pub mod stream;