Skip to main content

async_compression_issue_150_workaround/codec/gzip/
decoder.rs

1use crate::{
2    codec::{
3        gzip::header::{self, Header},
4        Decode,
5    },
6    util::PartialBuffer,
7};
8use std::io::{Error, ErrorKind, Result};
9
10use flate2::Crc;
11
12#[derive(Debug)]
13enum State {
14    Header(header::Parser),
15    Decoding,
16    Footer(PartialBuffer<Vec<u8>>),
17    Done,
18}
19
20#[derive(Debug)]
21pub struct GzipDecoder {
22    inner: crate::codec::FlateDecoder,
23    crc: Crc,
24    state: State,
25    header: Header,
26}
27
28fn check_footer(crc: &Crc, input: &[u8]) -> Result<()> {
29    if input.len() < 8 {
30        return Err(Error::new(
31            ErrorKind::InvalidData,
32            "Invalid gzip footer length",
33        ));
34    }
35
36    let crc_sum = crc.sum().to_le_bytes();
37    let bytes_read = crc.amount().to_le_bytes();
38
39    if crc_sum != input[0..4] {
40        return Err(Error::new(
41            ErrorKind::InvalidData,
42            "CRC computed does not match",
43        ));
44    }
45
46    if bytes_read != input[4..8] {
47        return Err(Error::new(
48            ErrorKind::InvalidData,
49            "amount of bytes read does not match",
50        ));
51    }
52
53    Ok(())
54}
55
56impl GzipDecoder {
57    pub fn new() -> Self {
58        Self {
59            inner: crate::codec::FlateDecoder::new(false),
60            crc: Crc::new(),
61            state: State::Header(header::Parser::default()),
62            header: Header::default(),
63        }
64    }
65
66    fn process<I: AsRef<[u8]>, O: AsRef<[u8]> + AsMut<[u8]>>(
67        &mut self,
68        input: &mut PartialBuffer<I>,
69        output: &mut PartialBuffer<O>,
70        inner: impl Fn(&mut Self, &mut PartialBuffer<I>, &mut PartialBuffer<O>) -> Result<bool>,
71    ) -> Result<bool> {
72        loop {
73            match &mut self.state {
74                State::Header(parser) => {
75                    if let Some(header) = parser.input(input)? {
76                        self.header = header;
77                        self.state = State::Decoding;
78                    }
79                }
80
81                State::Decoding => {
82                    let prior = output.written().len();
83                    let done = inner(self, input, output)?;
84                    self.crc.update(&output.written()[prior..]);
85                    if done {
86                        self.state = State::Footer(vec![0; 8].into())
87                    }
88                }
89
90                State::Footer(footer) => {
91                    footer.copy_unwritten_from(input);
92
93                    if footer.unwritten().is_empty() {
94                        check_footer(&self.crc, footer.written())?;
95                        self.state = State::Done
96                    }
97                }
98
99                State::Done => {}
100            };
101
102            if let State::Done = self.state {
103                return Ok(true);
104            }
105
106            if input.unwritten().is_empty() || output.unwritten().is_empty() {
107                return Ok(false);
108            }
109        }
110    }
111}
112
113impl Decode for GzipDecoder {
114    fn reinit(&mut self) -> Result<()> {
115        self.inner.reinit()?;
116        self.crc = Crc::new();
117        self.state = State::Header(header::Parser::default());
118        self.header = Header::default();
119        Ok(())
120    }
121
122    fn decode(
123        &mut self,
124        input: &mut PartialBuffer<impl AsRef<[u8]>>,
125        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
126    ) -> Result<bool> {
127        self.process(input, output, |this, input, output| {
128            this.inner.decode(input, output)
129        })
130    }
131
132    fn flush(
133        &mut self,
134        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
135    ) -> Result<bool> {
136        loop {
137            match self.state {
138                State::Header(_) | State::Footer(_) | State::Done => return Ok(true),
139
140                State::Decoding => {
141                    let prior = output.written().len();
142                    let done = self.inner.flush(output)?;
143                    self.crc.update(&output.written()[prior..]);
144                    if done {
145                        return Ok(true);
146                    }
147                }
148            };
149
150            if output.unwritten().is_empty() {
151                return Ok(false);
152            }
153        }
154    }
155
156    fn finish(
157        &mut self,
158        _output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
159    ) -> Result<bool> {
160        // Because of the footer we have to have already flushed all the data out before we get here
161        if let State::Done = self.state {
162            Ok(true)
163        } else {
164            Err(Error::new(
165                ErrorKind::UnexpectedEof,
166                "unexpected end of file",
167            ))
168        }
169    }
170}