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

lzma: fix clippy warnings #57

Merged
merged 1 commit into from
Jul 31, 2020
Merged
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
25 changes: 8 additions & 17 deletions src/decode/lzma.rs
Original file line number Diff line number Diff line change
@@ -24,12 +24,9 @@ impl LZMAParams {
R: io::BufRead,
{
// Properties
let props = input.read_u8().or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA header too short: {}",
e
)))
})?;
let props = input
.read_u8()
.map_err(|e| error::Error::LZMAError(format!("LZMA header too short: {}", e)))?;

let mut pb = props as u32;
if pb >= 225 {
@@ -47,12 +44,9 @@ impl LZMAParams {
lzma_info!("Properties {{ lc: {}, lp: {}, pb: {} }}", lc, lp, pb);

// Dictionary
let dict_size_provided = input.read_u32::<LittleEndian>().or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA header too short: {}",
e
)))
})?;
let dict_size_provided = input
.read_u32::<LittleEndian>()
.map_err(|e| error::Error::LZMAError(format!("LZMA header too short: {}", e)))?;
let dict_size = if dict_size_provided < 0x1000 {
0x1000
} else {
@@ -64,11 +58,8 @@ impl LZMAParams {
// Unpacked size
let unpacked_size: Option<u64> = match options.unpacked_size {
UnpackedSize::ReadFromHeader => {
let unpacked_size_provided = input.read_u64::<LittleEndian>().or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA header too short: {}",
e
)))
let unpacked_size_provided = input.read_u64::<LittleEndian>().map_err(|e| {
error::Error::LZMAError(format!("LZMA header too short: {}", e))
})?;
let marker_mandatory: bool = unpacked_size_provided == 0xFFFF_FFFF_FFFF_FFFF;
if marker_mandatory {
57 changes: 19 additions & 38 deletions src/decode/lzma2.rs
Original file line number Diff line number Diff line change
@@ -16,12 +16,9 @@ where
let mut decoder = lzma::new_accum(accum, 0, 0, 0, None);

loop {
let status = input.read_u8().or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA2 expected new status: {}",
e
)))
})?;
let status = input
.read_u8()
.map_err(|e| error::Error::LZMAError(format!("LZMA2 expected new status: {}", e)))?;

lzma_info!("LZMA2 status: {}", status);

@@ -81,20 +78,14 @@ where
_ => unreachable!(),
}

let unpacked_size = input.read_u16::<BigEndian>().or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA2 expected unpacked size: {}",
e
)))
})?;
let unpacked_size = input
.read_u16::<BigEndian>()
.map_err(|e| error::Error::LZMAError(format!("LZMA2 expected unpacked size: {}", e)))?;
let unpacked_size = ((((status & 0x1F) as u64) << 16) | (unpacked_size as u64)) + 1;

let packed_size = input.read_u16::<BigEndian>().or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA2 expected packed size: {}",
e
)))
})?;
let packed_size = input
.read_u16::<BigEndian>()
.map_err(|e| error::Error::LZMAError(format!("LZMA2 expected packed size: {}", e)))?;
let packed_size = (packed_size as u64) + 1;

lzma_info!(
@@ -116,11 +107,8 @@ where
let mut pb: u32;

if reset_props {
let props = input.read_u8().or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA2 expected new properties: {}",
e
)))
let props = input.read_u8().map_err(|e| {
error::Error::LZMAError(format!("LZMA2 expected new properties: {}", e))
})?;

pb = props as u32;
@@ -156,12 +144,8 @@ where
decoder.set_unpacked_size(Some(unpacked_size));

let mut taken = input.take(packed_size);
let mut rangecoder = rangecoder::RangeDecoder::new(&mut taken).or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA input too short: {}",
e
)))
})?;
let mut rangecoder = rangecoder::RangeDecoder::new(&mut taken)
.map_err(|e| error::Error::LZMAError(format!("LZMA input too short: {}", e)))?;
decoder.process(&mut rangecoder)
}

@@ -174,12 +158,9 @@ where
R: io::BufRead,
W: io::Write,
{
let unpacked_size = input.read_u16::<BigEndian>().or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA2 expected unpacked size: {}",
e
)))
})?;
let unpacked_size = input
.read_u16::<BigEndian>()
.map_err(|e| error::Error::LZMAError(format!("LZMA2 expected unpacked size: {}", e)))?;
let unpacked_size = (unpacked_size as usize) + 1;

lzma_info!(
@@ -193,11 +174,11 @@ where
}

let mut buf = vec![0; unpacked_size];
input.read_exact(buf.as_mut_slice()).or_else(|e| {
Err(error::Error::LZMAError(format!(
input.read_exact(buf.as_mut_slice()).map_err(|e| {
error::Error::LZMAError(format!(
"LZMA2 expected {} uncompressed bytes: {}",
unpacked_size, e
)))
))
})?;
decoder.output.append_bytes(buf.as_slice());

6 changes: 3 additions & 3 deletions src/decode/xz.rs
Original file line number Diff line number Diff line change
@@ -415,11 +415,11 @@ where
}

let mut buf = vec![0; size_of_properties as usize];
input.read_exact(buf.as_mut_slice()).or_else(|e| {
Err(error::Error::XZError(format!(
input.read_exact(buf.as_mut_slice()).map_err(|e| {
error::Error::XZError(format!(
"Could not read filter properties of size {}: {}",
size_of_properties, e
)))
))
})?;

lzma_info!("XZ filter properties: {:?}", buf);
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -46,12 +46,8 @@ pub fn lzma_decompress_with_options<R: io::BufRead, W: io::Write>(
decode::lzma::new_circular(output, params)?
};

let mut rangecoder = decode::rangecoder::RangeDecoder::new(input).or_else(|e| {
Err(error::Error::LZMAError(format!(
"LZMA stream too short: {}",
e
)))
})?;
let mut rangecoder = decode::rangecoder::RangeDecoder::new(input)
.map_err(|e| error::Error::LZMAError(format!("LZMA stream too short: {}", e)))?;
decoder.process(&mut rangecoder)?;
decoder.output.finish()?;
Ok(())