Skip to content

Commit

Permalink
Re-add deflate64 support
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Feb 2, 2024
1 parent e43f575 commit a8b0c4f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 26 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rc-zip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ num_enum = "0.7.2"
cfg-if = "1.0.0"
crc32fast = "1.3.2"
miniz_oxide = { version = "0.7.1", optional = true }
deflate64 = { version = "0.1.7", optional = true }
bzip2 = { version = "0.4.4", optional = true }
lzma-rs = { version = "0.3.0", optional = true, features = ["stream"] }
zstd = { version = "0.13.0", optional = true }

[features]
corpus = []
deflate = ["dep:miniz_oxide"]
deflate64 = []
deflate64 = ["dep:deflate64"]
bzip2 = ["dep:bzip2"]
lzma = ["dep:lzma-rs"]
zstd = ["dep:zstd"]
Expand Down
47 changes: 47 additions & 0 deletions rc-zip/src/fsm/entry/deflate64_dec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use deflate64::InflaterManaged;

use crate::{error::Error, parse::Method};

use super::{DecompressOutcome, Decompressor, HasMoreInput};

pub(crate) struct Deflate64Dec {
inflater: InflaterManaged,
}

impl Default for Deflate64Dec {
fn default() -> Self {
Self {
inflater: InflaterManaged::new(),
}
}
}

impl Decompressor for Deflate64Dec {
fn decompress(
&mut self,
in_buf: &[u8],
out: &mut [u8],
_has_more_input: HasMoreInput,
) -> Result<DecompressOutcome, Error> {
tracing::trace!(
in_buf_len = in_buf.len(),
out_len = out.len(),
remain_in_internal_buffer = self.inflater.available_output(),
"decompress",
);

let res = self.inflater.inflate(in_buf, out);
if res.data_error {
return Err(Error::Decompression {
method: Method::Deflate64,
msg: "data error".into(),
});
}

let outcome = DecompressOutcome {
bytes_read: res.bytes_consumed,
bytes_written: res.bytes_written,
};
Ok(outcome)
}
}
2 changes: 1 addition & 1 deletion rc-zip/src/fsm/entry/deflate_dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Decompressor for DeflateDec {
out_len = out.len(),
remain_in_internal_buffer = self.remain_in_internal_buffer,
out_pos = self.out_pos,
"DeflateDec::decompress",
"decompress",
);

let mut outcome: DecompressOutcome = Default::default();
Expand Down
21 changes: 9 additions & 12 deletions rc-zip/src/fsm/entry/lzma_dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ impl Decompressor for LzmaDec {
in_buf_len = in_buf.len(),
out_len = out.len(),
remain_in_internal_buffer = self.internal_buf_mut().len(),
"DeflateDec::decompress",
"decompress",
);

let mut outcome: DecompressOutcome = Default::default();

self.copy_to_out(out, &mut outcome);
if outcome.bytes_written > 0 {
trace!(
"LzmaDec: still draining internal buffer, just copied {} bytes",
"still draining internal buffer, just copied {} bytes",
outcome.bytes_written
);
return Ok(outcome);
Expand All @@ -66,7 +66,7 @@ impl Decompressor for LzmaDec {
State::Writing(stream) => {
let n = stream.write(in_buf).map_err(dec_err)?;
trace!(
"LzmaDec: wrote {} bytes to decompressor (of {} available)",
"wrote {} bytes to decompressor (of {} available)",
n,
in_buf.len()
);
Expand All @@ -79,20 +79,20 @@ impl Decompressor for LzmaDec {
// trailer after LZMA compressed data? and the decoder will _refuse_
// to let us write them, so when we have just these 10 bytes left,
// it's good to just let the decoder finish up.
trace!("LzmaDec: didn't write all output AND no output yet, so keep going");
trace!("didn't write all output AND no output yet, so keep going");
return self.decompress(&in_buf[n..], out, has_more_input);
}

match has_more_input {
HasMoreInput::Yes => {
// keep going
trace!("LzmaDec: more input to come");
trace!("more input to come");
}
HasMoreInput::No => {
trace!("LzmaDec: no more input to come");
trace!("no more input to come");
match std::mem::take(&mut self.state) {
State::Writing(stream) => {
trace!("LzmaDec: finishing...");
trace!("finishing...");
self.state = State::Draining(stream.finish().map_err(dec_err)?);
}
_ => unreachable!(),
Expand All @@ -107,10 +107,7 @@ impl Decompressor for LzmaDec {
}

self.copy_to_out(out, &mut outcome);
trace!(
"LzmaDec: decompressor gave us {} bytes",
outcome.bytes_written
);
trace!("decompressor gave us {} bytes", outcome.bytes_written);
Ok(outcome)
}
}
Expand All @@ -137,7 +134,7 @@ impl LzmaDec {

while !out.is_empty() && !internal_buf.is_empty() {
let to_copy = cmp::min(out.len(), internal_buf.len());
trace!("LzmaDec: copying {} bytes from internal buffer", to_copy);
trace!("copying {} bytes from internal buffer", to_copy);
out[..to_copy].copy_from_slice(&internal_buf[..to_copy]);
out = &mut out[to_copy..];

Expand Down
15 changes: 15 additions & 0 deletions rc-zip/src/fsm/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ mod store_dec;
#[cfg(feature = "deflate")]
mod deflate_dec;

#[cfg(feature = "deflate64")]
mod deflate64_dec;

#[cfg(feature = "bzip2")]
mod bzip2_dec;

Expand Down Expand Up @@ -317,6 +320,8 @@ enum AnyDecompressor {
Store(store_dec::StoreDec),
#[cfg(feature = "deflate")]
Deflate(Box<deflate_dec::DeflateDec>),
#[cfg(feature = "deflate64")]
Deflate64(Box<deflate64_dec::Deflate64Dec>),
#[cfg(feature = "bzip2")]
Bzip2(bzip2_dec::Bzip2Dec),
#[cfg(feature = "lzma")]
Expand Down Expand Up @@ -361,6 +366,14 @@ impl AnyDecompressor {
return Err(err);
}

#[cfg(feature = "deflate64")]
Method::Deflate64 => Self::Deflate64(Default::default()),
#[cfg(not(feature = "deflate64"))]
Method::Deflate64 => {
let err = Error::Unsupported(UnsupportedError::MethodNotEnabled(method));
return Err(err);
}

#[cfg(feature = "bzip2")]
Method::Bzip2 => Self::Bzip2(Default::default()),
#[cfg(not(feature = "bzip2"))]
Expand Down Expand Up @@ -407,6 +420,8 @@ impl Decompressor for AnyDecompressor {
Self::Store(dec) => dec.decompress(in_buf, out, has_more_input),
#[cfg(feature = "deflate")]
Self::Deflate(dec) => dec.decompress(in_buf, out, has_more_input),
#[cfg(feature = "deflate64")]
Self::Deflate64(dec) => dec.decompress(in_buf, out, has_more_input),
#[cfg(feature = "bzip2")]
Self::Bzip2(dec) => dec.decompress(in_buf, out, has_more_input),
#[cfg(feature = "lzma")]
Expand Down
21 changes: 9 additions & 12 deletions rc-zip/src/fsm/entry/zstd_dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ impl Decompressor for ZstdDec {
in_buf_len = in_buf.len(),
out_len = out.len(),
remain_in_internal_buffer = self.internal_buf_mut().len(),
"DeflateDec::decompress",
"decompress",
);

let mut outcome: DecompressOutcome = Default::default();

self.copy_to_out(out, &mut outcome);
if outcome.bytes_written > 0 {
trace!(
"ZstdDec: still draining internal buffer, just copied {} bytes",
"still draining internal buffer, just copied {} bytes",
outcome.bytes_written
);
return Ok(outcome);
Expand All @@ -57,7 +57,7 @@ impl Decompressor for ZstdDec {
State::Writing(stream) => {
let n = stream.write(in_buf).map_err(dec_err)?;
trace!(
"ZstdDec: wrote {} bytes to decompressor (of {} available)",
"wrote {} bytes to decompressor (of {} available)",
n,
in_buf.len()
);
Expand All @@ -70,20 +70,20 @@ impl Decompressor for ZstdDec {
// trailer after LZMA compressed data? and the decoder will _refuse_
// to let us write them, so when we have just these 10 bytes left,
// it's good to just let the decoder finish up.
trace!("ZstdDec: didn't write all output AND no output yet, so keep going");
trace!("didn't write all output AND no output yet, so keep going");
return self.decompress(&in_buf[n..], out, has_more_input);
}

match has_more_input {
HasMoreInput::Yes => {
// keep going
trace!("ZstdDec: more input to come");
trace!("more input to come");
}
HasMoreInput::No => {
trace!("ZstdDec: no more input to come");
trace!("no more input to come");
match std::mem::take(&mut self.state) {
State::Writing(mut stream) => {
trace!("ZstdDec: finishing...");
trace!("finishing...");
stream.flush().map_err(dec_err)?;
self.state = State::Draining(stream.into_inner());
}
Expand All @@ -99,10 +99,7 @@ impl Decompressor for ZstdDec {
}

self.copy_to_out(out, &mut outcome);
trace!(
"ZstdDec: decompressor gave us {} bytes",
outcome.bytes_written
);
trace!("decompressor gave us {} bytes", outcome.bytes_written);
Ok(outcome)
}
}
Expand All @@ -129,7 +126,7 @@ impl ZstdDec {

while !out.is_empty() && !internal_buf.is_empty() {
let to_copy = cmp::min(out.len(), internal_buf.len());
trace!("ZstdDec: copying {} bytes from internal buffer", to_copy);
trace!("copying {} bytes from internal buffer", to_copy);
out[..to_copy].copy_from_slice(&internal_buf[..to_copy]);
out = &mut out[to_copy..];

Expand Down

0 comments on commit a8b0c4f

Please sign in to comment.