Skip to content

Commit

Permalink
lzma: fix clippy warnings
Browse files Browse the repository at this point in the history
This fixes all linting warnings that were introduced on latest clippy.
  • Loading branch information
lucab committed Jul 31, 2020
1 parent b820a29 commit a2dd852
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 64 deletions.
25 changes: 8 additions & 17 deletions src/decode/lzma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
57 changes: 19 additions & 38 deletions src/decode/lzma2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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!(
Expand All @@ -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;
Expand Down Expand Up @@ -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)
}

Expand All @@ -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!(
Expand All @@ -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());

Expand Down
6 changes: 3 additions & 3 deletions src/decode/xz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down

0 comments on commit a2dd852

Please sign in to comment.