diff --git a/crates/compression-codecs/src/gzip/decoder.rs b/crates/compression-codecs/src/gzip/decoder.rs index 5a72b049..647959cf 100644 --- a/crates/compression-codecs/src/gzip/decoder.rs +++ b/crates/compression-codecs/src/gzip/decoder.rs @@ -1,4 +1,4 @@ -use super::header::{self, Header}; +use super::header; use crate::{DecodeV2, FlateDecoder}; use compression_core::util::{PartialBuffer, WriteBuffer}; use flate2::Crc; @@ -8,7 +8,7 @@ use std::io::{Error, ErrorKind, Result}; enum State { Header(header::Parser), Decoding, - Footer(PartialBuffer>), + Footer(PartialBuffer<[u8; 8]>), Done, } @@ -17,17 +17,9 @@ pub struct GzipDecoder { inner: FlateDecoder, crc: Crc, state: State, - header: Header, } -fn check_footer(crc: &Crc, input: &[u8]) -> Result<()> { - if input.len() < 8 { - return Err(Error::new( - ErrorKind::InvalidData, - "Invalid gzip footer length", - )); - } - +fn check_footer(crc: &Crc, input: &[u8; 8]) -> Result<()> { let crc_sum = crc.sum().to_le_bytes(); let bytes_read = crc.amount().to_le_bytes(); @@ -54,7 +46,6 @@ impl Default for GzipDecoder { inner: FlateDecoder::new(false), crc: Crc::new(), state: State::Header(header::Parser::default()), - header: Header::default(), } } } @@ -73,8 +64,7 @@ impl GzipDecoder { loop { match &mut self.state { State::Header(parser) => { - if let Some(header) = parser.input(input)? { - self.header = header; + if parser.input(input)?.is_some() { self.state = State::Decoding; } } @@ -92,7 +82,7 @@ impl GzipDecoder { let done = res?; if done { - self.state = State::Footer(vec![0; 8].into()) + self.state = State::Footer([0; 8].into()); } } @@ -100,8 +90,8 @@ impl GzipDecoder { footer.copy_unwritten_from(input); if footer.unwritten().is_empty() { - check_footer(&self.crc, footer.written())?; - self.state = State::Done + check_footer(&self.crc, footer.get_mut())?; + self.state = State::Done; } } @@ -122,9 +112,8 @@ impl GzipDecoder { impl DecodeV2 for GzipDecoder { fn reinit(&mut self) -> Result<()> { self.inner.reinit()?; - self.crc = Crc::new(); + self.crc.reset(); self.state = State::Header(header::Parser::default()); - self.header = Header::default(); Ok(()) }