Skip to content

Commit

Permalink
flush data still available in the decoder when no input (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
nl5887 authored and robjtede committed Jan 17, 2024
1 parent 5926e78 commit 98eae35
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/codec/gzip/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,20 @@ impl GzipDecoder {

State::Decoding => {
let prior = output.written().len();
let done = inner(self, input, output)?;

let done = inner(self, input, output).or_else(|e| match e {
// we need to make an exception, if output flushed
// from the decoder, to update the crc
e if e.kind() == std::io::ErrorKind::Other
&& output.written().len() > 0 =>
{
Ok(false)
}
_ => Err(e),
})?;

self.crc.update(&output.written()[prior..]);

if done {
self.state = State::Footer(vec![0; 8].into())
}
Expand Down
25 changes: 23 additions & 2 deletions src/futures/bufread/generic/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,39 @@ impl<R: AsyncBufRead, D: Decode> Decoder<R, D> {
) -> Poll<Result<()>> {
let mut this = self.project();

let mut first = true;

loop {
*this.state = match this.state {
State::Decoding => {
let input = ready!(this.reader.as_mut().poll_fill_buf(cx))?;
let input = if first {
&[][..]
} else {
ready!(this.reader.as_mut().poll_fill_buf(cx))?
};

if input.is_empty() {
// Avoid attempting to reinitialise the decoder if the reader
// has returned EOF.
*this.multiple_members = false;
}

if input.is_empty() && !first {
State::Flushing
} else {
let mut input = PartialBuffer::new(input);
let done = this.decoder.decode(&mut input, output)?;
let done = this.decoder.decode(&mut input, output).or_else(|e| {
// ignore the first error, occurs when input is empty
// but we need to run decode to flush
if first {
Ok(false)
} else {
Err(e)
}
})?;

first = false;

let len = input.written().len();
this.reader.as_mut().consume(len);
if done {
Expand Down

0 comments on commit 98eae35

Please sign in to comment.