chore: use streaming in deflate64 crate

This commit is contained in:
anatawa12 2023-08-04 22:55:48 +09:00
parent 065058d30d
commit 70c0b164be
No known key found for this signature in database
GPG key ID: 9CA909848B8E4EA6
4 changed files with 4 additions and 67 deletions

View file

@ -23,7 +23,7 @@ pbkdf2 = {version = "0.11.0", optional = true }
sha1 = {version = "0.10.1", optional = true }
time = { version = "0.3.7", optional = true, default-features = false, features = ["std"] }
zstd = { version = "0.11.2", optional = true }
deflate64 = { version = "0.1.3", optional = true }
deflate64 = { version = "0.1.4", optional = true }
[target.'cfg(any(all(target_arch = "arm", target_pointer_width = "32"), target_arch = "mips", target_arch = "powerpc"))'.dependencies]
crossbeam-utils = "0.8.8"

View file

@ -1,61 +0,0 @@
// TODO: move this module to deflate64 crate
use deflate64::InflaterManaged;
use std::io;
use std::io::{BufRead, BufReader, Read};
const IN_BUFFER_MAX_SIZE: usize = 8192;
pub(crate) struct BufferedDeflate64Decoder<R> {
inner: R,
inflator: Box<InflaterManaged>,
}
impl<R: Read> BufferedDeflate64Decoder<BufReader<R>> {
pub(crate) fn new(inner: R) -> Self {
Self {
inner: BufReader::new(inner),
inflator: Box::new(InflaterManaged::new()),
}
}
}
impl<R> BufferedDeflate64Decoder<R> {
pub(crate) fn into_inner(self) -> R {
self.inner
}
}
struct State {
inflater: InflaterManaged,
in_buffer_size: u16,
in_buffer: [u8; IN_BUFFER_MAX_SIZE],
}
impl<R: BufRead> Read for BufferedDeflate64Decoder<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
loop {
let input = self.inner.fill_buf()?;
let eof = input.is_empty();
let result = self.inflator.inflate(input, buf);
self.inner.consume(result.bytes_consumed);
if result.data_error {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid deflate64",
));
}
if result.bytes_written == 0 && !eof && !self.inflator.finished() {
// if we haven't ready any data and we haven't hit EOF yet,
// ask again. We must not return 0 in such case
continue;
}
Ok(result.bytes_written)
}
}
}

View file

@ -36,8 +36,6 @@ mod aes_ctr;
mod compression;
mod cp437;
mod crc32;
#[cfg(feature = "deflate64")]
mod deflate64;
pub mod read;
pub mod result;
mod spec;

View file

@ -24,7 +24,7 @@ use std::sync::Arc;
use flate2::read::DeflateDecoder;
#[cfg(feature = "deflate64")]
use crate::deflate64::BufferedDeflate64Decoder;
use deflate64::Deflate64Decoder;
#[cfg(feature = "bzip2")]
use bzip2::read::BzDecoder;
@ -133,7 +133,7 @@ enum ZipFileReader<'a> {
))]
Deflated(Crc32Reader<flate2::read::DeflateDecoder<CryptoReader<'a>>>),
#[cfg(feature = "deflate64")]
Deflate64(Crc32Reader<BufferedDeflate64Decoder<io::BufReader<CryptoReader<'a>>>>),
Deflate64(Crc32Reader<Deflate64Decoder<io::BufReader<CryptoReader<'a>>>>),
#[cfg(feature = "bzip2")]
Bzip2(Crc32Reader<BzDecoder<CryptoReader<'a>>>),
#[cfg(feature = "zstd")]
@ -288,7 +288,7 @@ fn make_reader(
}
#[cfg(feature = "deflate64")]
CompressionMethod::Deflate64 => {
let deflate64_reader = BufferedDeflate64Decoder::new(reader);
let deflate64_reader = Deflate64Decoder::new(reader);
ZipFileReader::Deflate64(Crc32Reader::new(deflate64_reader, crc32, ae2_encrypted))
}
#[cfg(feature = "bzip2")]