Skip to content

Commit

Permalink
Move deflate_dec to its own module, too
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jan 26, 2024
1 parent 7038eb1 commit ebe7b47
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 54 deletions.
21 changes: 18 additions & 3 deletions crates/rc-zip/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Method;

use super::encoding;

/// Any zip-related error, from invalid archives to encoding problems.
Expand All @@ -24,12 +26,25 @@ pub enum Error {
UnknownSize,
}

impl Error {
pub(crate) fn method_not_supported(method: Method) -> Self {
Self::Unsupported(UnsupportedError::MethodNotSupported(method))
}

#[allow(unused)]
pub(crate) fn method_not_enabled(method: Method) -> Self {
Self::Unsupported(UnsupportedError::MethodNotEnabled(method))
}
}

#[derive(Debug, thiserror::Error)]
pub enum UnsupportedError {
#[error("unsupported compression method: {0:?}")]
UnsupportedCompressionMethod(crate::format::Method),
#[error("compression method not supported: {0:?}")]
MethodNotSupported(crate::format::Method),

#[error("compression method supported, but not enabled in this build: {0:?}")]
CompressionMethodNotEnabled(crate::format::Method),
MethodNotEnabled(crate::format::Method),

#[error("only LZMA2.0 is supported, found LZMA{minor}.{major}")]
LzmaVersionUnsupported { minor: u8, major: u8 },
#[error("LZMA properties header wrong size: expected {expected} bytes, got {actual} bytes")]
Expand Down
17 changes: 0 additions & 17 deletions crates/rc-zip/src/reader/sync/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(feature = "deflate")]
use flate2::read::DeflateDecoder;

use std::{cmp, io};

pub trait Decoder<R>: io::Read
Expand All @@ -15,20 +12,6 @@ where
fn get_mut(&mut self) -> &mut R;
}

#[cfg(feature = "deflate")]
impl<R> Decoder<R> for DeflateDecoder<R>
where
R: io::Read,
{
fn into_inner(self: Box<Self>) -> R {
Self::into_inner(*self)
}

fn get_mut(&mut self) -> &mut R {
Self::get_mut(self)
}
}

pub struct StoreDecoder<R>
where
R: io::Read,
Expand Down
22 changes: 22 additions & 0 deletions crates/rc-zip/src/reader/sync/entry_reader/deflate_dec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::io::Read;

use flate2::read::DeflateDecoder;

use crate::reader::sync::{Decoder, LimitedReader};

impl<R> Decoder<R> for DeflateDecoder<R>
where
R: Read,
{
fn into_inner(self: Box<Self>) -> R {
Self::into_inner(*self)
}

fn get_mut(&mut self) -> &mut R {
Self::get_mut(self)
}
}

pub(crate) fn mk_decoder(r: LimitedReader) -> impl Decoder<LimitedReader> {
DeflateDecoder::new(r)
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ where
pub(crate) fn mk_decoder(
mut r: LimitedReader,
uncompressed_size: u64,
flags: u16,
) -> std::io::Result<Box<dyn Decoder<LimitedReader>>> {
) -> std::io::Result<impl Decoder<LimitedReader>> {
use byteorder::{LittleEndian, ReadBytesExt};

// see `appnote.txt` section 5.8
Expand Down Expand Up @@ -155,14 +154,10 @@ pub(crate) fn mk_decoder(
memlimit: Some(memlimit),
};

// general-purpose bit flag 1 indicates that the stream has an EOS marker
let has_eos = flags & 0b01 != 0;
trace!(?has_eos, "EOS marker?, flags = {flags:x?}");

let stream = Stream::new_with_options(&opts, vec![]);
Ok(Box::new(LzmaDecoderAdapter {
Ok(LzmaDecoderAdapter {
input: r,
total_write_count: 0,
state: LzmaDecoderState::Writing(Box::new(stream)),
}))
})
}
36 changes: 10 additions & 26 deletions crates/rc-zip/src/reader/sync/entry_reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use crate::{
};

#[cfg(feature = "lzma")]
mod lzma_support;
mod lzma_dec;

#[cfg(feature = "deflate")]
mod deflate_dec;

use cfg_if::cfg_if;
use nom::Offset;
use std::io;
use tracing::trace;

#[cfg(feature = "deflate")]
use flate2::read::DeflateDecoder;

struct EntryReadMetrics {
uncompressed_size: u64,
crc32: u32,
Expand Down Expand Up @@ -55,8 +55,6 @@ where
eof: bool,
state: State,
inner: StoredEntryInner,
/// General-purpose bit flag
flags: u16,
method: Method,
}

Expand Down Expand Up @@ -255,49 +253,35 @@ where
},
method: entry.method(),
inner: entry.inner,
flags: entry.flags,
}
}

fn get_decoder(
&self,
#[allow(unused_mut)] mut limited_reader: LimitedReader,
) -> std::io::Result<Box<dyn Decoder<LimitedReader>>> {
) -> Result<Box<dyn Decoder<LimitedReader>>, Error> {
let decoder: Box<dyn Decoder<LimitedReader>> = match self.method {
Method::Store => Box::new(StoreDecoder::new(limited_reader)),
Method::Deflate => {
cfg_if! {
if #[cfg(feature = "deflate")] {
Box::new(DeflateDecoder::new(limited_reader))
Box::new(deflate_dec::mk_decoder(limited_reader))
} else {
return Err(
Error::Unsupported(UnsupportedError::CompressionMethodNotEnabled(
Method::Deflate,
))
.into(),
);
return Err(Error::method_not_enabled(method));
}
}
}
Method::Lzma => {
cfg_if! {
if #[cfg(feature = "lzma")] {
lzma_support::mk_decoder(limited_reader, self.inner.uncompressed_size, self.flags)?
Box::new(lzma_dec::mk_decoder(limited_reader,self.inner.uncompressed_size)?)
} else {
return Err(
Error::Unsupported(UnsupportedError::CompressionMethodNotEnabled(
Method::Lzma,
))
.into(),
);
return Err(Error::method_not_enabled(method));
}
}
}
method => {
return Err(
Error::Unsupported(UnsupportedError::UnsupportedCompressionMethod(method))
.into(),
)
return Err(Error::method_not_supported(method));
}
};

Expand Down

0 comments on commit ebe7b47

Please sign in to comment.