Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions crates/compression-codecs/src/gzip/decoder.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,7 +8,7 @@ use std::io::{Error, ErrorKind, Result};
enum State {
Header(header::Parser),
Decoding,
Footer(PartialBuffer<Vec<u8>>),
Footer(PartialBuffer<[u8; 8]>),
Done,
}

Expand All @@ -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();

Expand All @@ -54,7 +46,6 @@ impl Default for GzipDecoder {
inner: FlateDecoder::new(false),
crc: Crc::new(),
state: State::Header(header::Parser::default()),
header: Header::default(),
}
}
}
Expand All @@ -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;
}
}
Expand All @@ -92,16 +82,16 @@ impl GzipDecoder {
let done = res?;

if done {
self.state = State::Footer(vec![0; 8].into())
self.state = State::Footer([0; 8].into());
}
}

State::Footer(footer) => {
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;
}
}

Expand All @@ -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(())
}

Expand Down