Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lzma support #40

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .direnv/flake-profile
1 change: 1 addition & 0 deletions .direnv/flake-profile-1-link
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.features": ["default", "lzma"]
}
29 changes: 29 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/jean/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ humansize = "2.1.3"
positioned-io.workspace = true
indicatif = "0.17.7"
tracing-subscriber = "0.3.18"

[features]
default = ["lzma"]
deflate = ["rc-zip/deflate"]
lzma = ["rc-zip/lzma"]
4 changes: 4 additions & 0 deletions crates/rc-zip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ thiserror = "1.0.56"
chardetng = "0.1.17"
flate2 = { version = "1.0.28", optional = true }
num_enum = "0.7.2"
byteorder = "1.5.0"
xz2 = { version = "0.1.7", optional = true }
cfg-if = "1.0.0"

[features]
default = ["sync", "file", "deflate"]
sync = []
file = ["positioned-io"]
deflate = ["flate2"]
lzma = ["xz2"]
7 changes: 7 additions & 0 deletions crates/rc-zip/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub enum UnsupportedError {
UnsupportedCompressionMethod(crate::format::Method),
#[error("compression method supported, but not enabled in this build: {0:?}")]
CompressionMethodNotEnabled(crate::format::Method),
#[error("only LZMA2.0 is supported, found LZMA{minor}.{major}")]
LzmaVersionUnsupported { minor: u8, major: u8 },
#[error("LZMA properties header too short: expected {expected} bytes, got {actual} bytes")]
LzmaPropertiesHeaderTooShort { expected: u16, actual: u16 },
}

/// Specific zip format errors, mostly due to invalid zip archives but that could also stem from
Expand Down Expand Up @@ -93,6 +97,9 @@ pub enum FormatError {
/// The CRC-32 checksum didn't match.
#[error("checksum didn't match: expected {expected:x?}, got {actual:x?}")]
WrongChecksum { expected: u32, actual: u32 },

#[error("lzma properties larger than max")]
LzmaPropertiesLargerThanMax,
}

impl From<Error> for std::io::Error {
Expand Down
18 changes: 16 additions & 2 deletions crates/rc-zip/src/reader/sync/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,25 @@ where
R: io::Read,
{
fn into_inner(self: Box<Self>) -> R {
DeflateDecoder::into_inner(*self)
Self::into_inner(*self)
}

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

#[cfg(feature = "lzma")]
impl<R> Decoder<R> for xz2::read::XzDecoder<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)
}
}

Expand Down
147 changes: 133 additions & 14 deletions crates/rc-zip/src/reader/sync/entry_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use crate::{
transition,
};

#[cfg(feature = "deflate")]
use flate2::read::DeflateDecoder;
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 @@ -72,18 +74,10 @@ where

trace!("local file header: {:#?}", header);
transition!(self.state => (S::ReadLocalHeader { buffer }) {
let limited_reader = LimitedReader::new(buffer, self.inner.compressed_size);
let decoder: Box<dyn Decoder<LimitedReader>> = match self.method {
Method::Store => Box::new(StoreDecoder::new(limited_reader)),
Method::Deflate => {
#[cfg(feature = "deflate")]
{ Box::new(DeflateDecoder::new(limited_reader)) }

#[cfg(not(feature = "deflate"))]
{ return Err(Error::Unsupported(UnsupportedError::CompressionMethodNotEnabled(Method::Deflate)).into()) }
},
method => return Err(Error::Unsupported(UnsupportedError::UnsupportedCompressionMethod(method)).into()),
};
// allow unnecessary mut for some feature combinations
#[allow(unused_mut)]
let mut limited_reader = LimitedReader::new(buffer, self.inner.compressed_size);
let decoder: Box<dyn Decoder<LimitedReader>> = self.get_decoder(limited_reader)?;

S::ReadData {
hasher: crc32fast::Hasher::new(),
Expand Down Expand Up @@ -258,4 +252,129 @@ where
inner: entry.inner,
}
}

fn get_decoder(
&self,
#[allow(unused_mut)] mut limited_reader: LimitedReader,
) -> std::io::Result<Box<dyn Decoder<LimitedReader>>> {
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))
} else {
return Err(
Error::Unsupported(UnsupportedError::CompressionMethodNotEnabled(
Method::Deflate,
))
.into(),
);
}
}
}
Method::Lzma => {
cfg_if! {
if #[cfg(feature = "lzma")] {
// TODO: use a parser combinator library for this probably?

// read LZMA properties header first.
use byteorder::{LittleEndian, ReadBytesExt};
let major: u8 = limited_reader.read_u8()?;
let minor: u8 = limited_reader.read_u8()?;
if (major, minor) != (2, 0) {
return Err(
Error::Unsupported(UnsupportedError::LzmaVersionUnsupported {
minor,
major,
})
.into(),
);
}

let props_size: u16 = limited_reader.read_u16::<LittleEndian>()?;

const LZMA_2_0_PROPS_SIZE: u16 = 5;
if props_size != LZMA_2_0_PROPS_SIZE {
return Err(Error::Unsupported(
UnsupportedError::LzmaPropertiesHeaderTooShort {
expected: 5,
actual: props_size,
},
)
.into());
}
let bits_byte: u8 = limited_reader.read_u8()?;

#[derive(Debug, Clone, Copy)]
struct LzmaProperties {
literal_context_bits: u8,
literal_pos_state_bits: u8,
pos_state_bits: u8,
}

// from `lzma-specification.txt`
fn decode_properties(mut d: u8) -> Result<LzmaProperties, FormatError> {
if d >= (9 * 5 * 5) {
return Err(FormatError::LzmaPropertiesLargerThanMax);
}

let lc = d % 9;
d /= 9;
let pb = d / 5;
let lp = d % 5;

Ok(LzmaProperties {
literal_context_bits: lc,
literal_pos_state_bits: lp,
pos_state_bits: pb,
})
}

let props = decode_properties(bits_byte);
trace!("LZMA properties: {:#?}", props);

const LZMA_DIC_MIN: u32 = 1 << 12;
let dict_size_read = limited_reader.read_u32::<LittleEndian>()?;
trace!("LZMA dictionary size (raw): {}", dict_size_read);
let dict_size: u32 =
std::cmp::max(LZMA_DIC_MIN, dict_size_read);
trace!("LZMA dictionary size: {}", dict_size);

// let mut opts = xz2::stream::LzmaOptions::new_preset(0)?;
// opts.dict_size(dict_size);
// opts.position_bits(props.pos_state_bits as _);
// opts.literal_position_bits(props.literal_pos_state_bits as _);
// opts.literal_context_bits(props.literal_context_bits as _);

// let mut filters = xz2::stream::Filters::new();
// filters.lzma2(&opts);

// uncompressed size is stored as a little-endian 64-bit integer
let uncompressed_size: u64 = limited_reader.read_u64::<LittleEndian>()?;
trace!("LZMA uncompressed size: {}", uncompressed_size);

let stream = xz2::stream::Stream::new_lzma_decoder(128 * 1024 * 1024)?;

Box::new(xz2::read::XzDecoder::new_stream(limited_reader, stream))
} else {
return Err(
Error::Unsupported(UnsupportedError::CompressionMethodNotEnabled(
Method::Lzma,
))
.into(),
);
}
}
}
method => {
return Err(
Error::Unsupported(UnsupportedError::UnsupportedCompressionMethod(method))
.into(),
)
}
};

Ok(decoder)
}
}
12 changes: 12 additions & 0 deletions crates/rc-zip/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ fn test_cases() -> Vec<ZipTest> {
}],
..Default::default()
},
#[cfg(feature = "lzma")]
ZipTest {
source: ZipSource::File("found-me-lzma.zip"),
expected_encoding: Some(Encoding::Utf8),
files: vec![ZipTestFile {
name: "found-me.txt",
content: FileContent::Bytes("Oh no, you found me!\n".repeat(5000).into()),
modified: Some(date((2024, 1, 26), (17, 14, 36), 0, time_zone(0)).unwrap()),
..Default::default()
}],
..Default::default()
},
]
}

Expand Down
Loading
Loading