Fix building without compress features

This commit is contained in:
Luke Street 2024-10-03 01:00:51 -06:00
parent 54890674a2
commit 8abe674cb9
2 changed files with 27 additions and 23 deletions

View File

@ -19,7 +19,6 @@ use crate::{
}, },
static_assert, static_assert,
util::{ util::{
compress::{lzma2_props_decode, lzma_props_decode, new_lzma2_decoder, new_lzma_decoder},
lfg::LaggedFibonacci, lfg::LaggedFibonacci,
read::{read_box_slice, read_from, read_u16_be, read_vec}, read::{read_box_slice, read_from, read_u16_be, read_vec},
take_seek::TakeSeekExt, take_seek::TakeSeekExt,
@ -459,15 +458,15 @@ pub enum Decompressor {
impl Decompressor { impl Decompressor {
pub fn new(disc: &WIADisc) -> Result<Self> { pub fn new(disc: &WIADisc) -> Result<Self> {
let data = &disc.compr_data[..disc.compr_data_len as usize]; let _data = &disc.compr_data[..disc.compr_data_len as usize];
match disc.compression() { match disc.compression() {
WIACompression::None => Ok(Self::None), WIACompression::None => Ok(Self::None),
#[cfg(feature = "compress-bzip2")] #[cfg(feature = "compress-bzip2")]
WIACompression::Bzip2 => Ok(Self::Bzip2), WIACompression::Bzip2 => Ok(Self::Bzip2),
#[cfg(feature = "compress-lzma")] #[cfg(feature = "compress-lzma")]
WIACompression::Lzma => Ok(Self::Lzma(Box::from(data))), WIACompression::Lzma => Ok(Self::Lzma(Box::from(_data))),
#[cfg(feature = "compress-lzma")] #[cfg(feature = "compress-lzma")]
WIACompression::Lzma2 => Ok(Self::Lzma2(Box::from(data))), WIACompression::Lzma2 => Ok(Self::Lzma2(Box::from(_data))),
#[cfg(feature = "compress-zstd")] #[cfg(feature = "compress-zstd")]
WIACompression::Zstandard => Ok(Self::Zstandard), WIACompression::Zstandard => Ok(Self::Zstandard),
comp => Err(Error::DiscFormat(format!("Unsupported WIA/RVZ compression: {:?}", comp))), comp => Err(Error::DiscFormat(format!("Unsupported WIA/RVZ compression: {:?}", comp))),
@ -482,11 +481,13 @@ impl Decompressor {
Decompressor::Bzip2 => Box::new(bzip2::read::BzDecoder::new(reader)), Decompressor::Bzip2 => Box::new(bzip2::read::BzDecoder::new(reader)),
#[cfg(feature = "compress-lzma")] #[cfg(feature = "compress-lzma")]
Decompressor::Lzma(data) => { Decompressor::Lzma(data) => {
use crate::util::compress::{lzma_props_decode, new_lzma_decoder};
let options = lzma_props_decode(data)?; let options = lzma_props_decode(data)?;
Box::new(new_lzma_decoder(reader, &options)?) Box::new(new_lzma_decoder(reader, &options)?)
} }
#[cfg(feature = "compress-lzma")] #[cfg(feature = "compress-lzma")]
Decompressor::Lzma2(data) => { Decompressor::Lzma2(data) => {
use crate::util::compress::{lzma2_props_decode, new_lzma2_decoder};
let options = lzma2_props_decode(data)?; let options = lzma2_props_decode(data)?;
Box::new(new_lzma2_decoder(reader, &options)?) Box::new(new_lzma2_decoder(reader, &options)?)
} }

View File

@ -1,13 +1,14 @@
use std::{io, io::Read};
/// Decodes the LZMA Properties byte (lc/lp/pb). /// Decodes the LZMA Properties byte (lc/lp/pb).
/// See `lzma_lzma_lclppb_decode` in `liblzma/lzma/lzma_decoder.c`. /// See `lzma_lzma_lclppb_decode` in `liblzma/lzma/lzma_decoder.c`.
#[cfg(feature = "compress-lzma")] #[cfg(feature = "compress-lzma")]
pub fn lzma_lclppb_decode(options: &mut liblzma::stream::LzmaOptions, byte: u8) -> io::Result<()> { pub fn lzma_lclppb_decode(
options: &mut liblzma::stream::LzmaOptions,
byte: u8,
) -> std::io::Result<()> {
let mut d = byte as u32; let mut d = byte as u32;
if d >= (9 * 5 * 5) { if d >= (9 * 5 * 5) {
return Err(io::Error::new( return Err(std::io::Error::new(
io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,
format!("Invalid LZMA props byte: {}", d), format!("Invalid LZMA props byte: {}", d),
)); ));
} }
@ -21,11 +22,11 @@ pub fn lzma_lclppb_decode(options: &mut liblzma::stream::LzmaOptions, byte: u8)
/// Decodes LZMA properties. /// Decodes LZMA properties.
/// See `lzma_lzma_props_decode` in `liblzma/lzma/lzma_decoder.c`. /// See `lzma_lzma_props_decode` in `liblzma/lzma/lzma_decoder.c`.
#[cfg(feature = "compress-lzma")] #[cfg(feature = "compress-lzma")]
pub fn lzma_props_decode(props: &[u8]) -> io::Result<liblzma::stream::LzmaOptions> { pub fn lzma_props_decode(props: &[u8]) -> std::io::Result<liblzma::stream::LzmaOptions> {
use crate::array_ref; use crate::array_ref;
if props.len() != 5 { if props.len() != 5 {
return Err(io::Error::new( return Err(std::io::Error::new(
io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,
format!("Invalid LZMA props length: {}", props.len()), format!("Invalid LZMA props length: {}", props.len()),
)); ));
} }
@ -38,11 +39,11 @@ pub fn lzma_props_decode(props: &[u8]) -> io::Result<liblzma::stream::LzmaOption
/// Decodes LZMA2 properties. /// Decodes LZMA2 properties.
/// See `lzma_lzma2_props_decode` in `liblzma/lzma/lzma2_decoder.c`. /// See `lzma_lzma2_props_decode` in `liblzma/lzma/lzma2_decoder.c`.
#[cfg(feature = "compress-lzma")] #[cfg(feature = "compress-lzma")]
pub fn lzma2_props_decode(props: &[u8]) -> io::Result<liblzma::stream::LzmaOptions> { pub fn lzma2_props_decode(props: &[u8]) -> std::io::Result<liblzma::stream::LzmaOptions> {
use std::cmp::Ordering; use std::cmp::Ordering;
if props.len() != 1 { if props.len() != 1 {
return Err(io::Error::new( return Err(std::io::Error::new(
io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,
format!("Invalid LZMA2 props length: {}", props.len()), format!("Invalid LZMA2 props length: {}", props.len()),
)); ));
} }
@ -50,8 +51,8 @@ pub fn lzma2_props_decode(props: &[u8]) -> io::Result<liblzma::stream::LzmaOptio
let mut options = liblzma::stream::LzmaOptions::new(); let mut options = liblzma::stream::LzmaOptions::new();
options.dict_size(match d.cmp(&40) { options.dict_size(match d.cmp(&40) {
Ordering::Greater => { Ordering::Greater => {
return Err(io::Error::new( return Err(std::io::Error::new(
io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,
format!("Invalid LZMA2 props byte: {}", d), format!("Invalid LZMA2 props byte: {}", d),
)); ));
} }
@ -66,13 +67,14 @@ pub fn lzma2_props_decode(props: &[u8]) -> io::Result<liblzma::stream::LzmaOptio
pub fn new_lzma_decoder<R>( pub fn new_lzma_decoder<R>(
reader: R, reader: R,
options: &liblzma::stream::LzmaOptions, options: &liblzma::stream::LzmaOptions,
) -> io::Result<liblzma::read::XzDecoder<R>> ) -> std::io::Result<liblzma::read::XzDecoder<R>>
where where
R: Read, R: std::io::Read,
{ {
let mut filters = liblzma::stream::Filters::new(); let mut filters = liblzma::stream::Filters::new();
filters.lzma1(options); filters.lzma1(options);
let stream = liblzma::stream::Stream::new_raw_decoder(&filters).map_err(io::Error::from)?; let stream =
liblzma::stream::Stream::new_raw_decoder(&filters).map_err(std::io::Error::from)?;
Ok(liblzma::read::XzDecoder::new_stream(reader, stream)) Ok(liblzma::read::XzDecoder::new_stream(reader, stream))
} }
@ -81,12 +83,13 @@ where
pub fn new_lzma2_decoder<R>( pub fn new_lzma2_decoder<R>(
reader: R, reader: R,
options: &liblzma::stream::LzmaOptions, options: &liblzma::stream::LzmaOptions,
) -> io::Result<liblzma::read::XzDecoder<R>> ) -> std::io::Result<liblzma::read::XzDecoder<R>>
where where
R: Read, R: std::io::Read,
{ {
let mut filters = liblzma::stream::Filters::new(); let mut filters = liblzma::stream::Filters::new();
filters.lzma2(options); filters.lzma2(options);
let stream = liblzma::stream::Stream::new_raw_decoder(&filters).map_err(io::Error::from)?; let stream =
liblzma::stream::Stream::new_raw_decoder(&filters).map_err(std::io::Error::from)?;
Ok(liblzma::read::XzDecoder::new_stream(reader, stream)) Ok(liblzma::read::XzDecoder::new_stream(reader, stream))
} }