Skip to content

Commit

Permalink
Use Decoder Enum for NSIS over Boxed dyn Read
Browse files Browse the repository at this point in the history
  • Loading branch information
russellbanks committed Feb 7, 2025
1 parent c5fb679 commit fb0c4ff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
22 changes: 22 additions & 0 deletions src/installers/nsis/header/decoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use bzip2::read::BzDecoder;
use flate2::read::DeflateDecoder;
use liblzma::read::XzDecoder;
use std::io::Read;

pub enum Decoder<R: Read> {
Lzma(XzDecoder<R>),
BZip2(BzDecoder<R>),
Zlib(DeflateDecoder<R>),
None(R),
}

impl<R: Read> Read for Decoder<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
Decoder::Lzma(reader) => reader.read(buf),
Decoder::BZip2(reader) => reader.read(buf),
Decoder::Zlib(reader) => reader.read(buf),
Decoder::None(reader) => reader.read(buf),
}
}
}
14 changes: 8 additions & 6 deletions src/installers/nsis/header/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod block;
pub mod compression;
pub mod decoder;
pub mod flags;

use crate::installers::nsis::first_header::FirstHeader;
use crate::installers::nsis::header::compression::Compression;
use crate::installers::nsis::header::decoder::Decoder;
use crate::installers::utils::read_lzma_stream_header;
use byteorder::{ByteOrder, ReadBytesExt, LE};
use bzip2::read::BzDecoder;
Expand Down Expand Up @@ -56,7 +58,7 @@ pub struct Decompressed<'data> {
pub is_solid: bool,
pub non_solid_start_offset: u32,
pub compression: Compression,
pub decoder: Box<dyn Read + 'data>,
pub decoder: Decoder<&'data [u8]>,
}

fn is_lzma(data: &[u8]) -> Option<Compression> {
Expand Down Expand Up @@ -120,14 +122,14 @@ impl Header {
&data[NON_SOLID_EXTRA_BYTES as usize..]
};

let mut decoder: Box<dyn Read> = match compression {
let mut decoder = match compression {
Compression::Lzma(_) => {
let stream = read_lzma_stream_header(&mut data)?;
Box::new(XzDecoder::new_stream(data, stream))
Decoder::Lzma(XzDecoder::new_stream(data, stream))
}
Compression::BZip2 => Box::new(BzDecoder::new(data)),
Compression::Zlib => Box::new(DeflateDecoder::new(data)),
Compression::None => Box::new(data),
Compression::BZip2 => Decoder::BZip2(BzDecoder::new(data)),
Compression::Zlib => Decoder::Zlib(DeflateDecoder::new(data)),
Compression::None => Decoder::None(data),
};

if is_solid && decoder.read_u32::<LE>()? != first_header.length_of_header.get() {
Expand Down
13 changes: 8 additions & 5 deletions src/installers/nsis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::installers::nsis::entry::Entry;
use crate::installers::nsis::first_header::FirstHeader;
use crate::installers::nsis::header::block::BlockHeaders;
use crate::installers::nsis::header::compression::Compression;
use crate::installers::nsis::header::decoder::Decoder;
use crate::installers::nsis::header::flags::CommonHeaderFlags;
use crate::installers::nsis::header::{Decompressed, Header};
use crate::installers::utils::{read_lzma_stream_header, RELATIVE_PROGRAM_FILES_64};
Expand Down Expand Up @@ -157,20 +158,22 @@ impl Nsis {
position
})
.and_then(|position| {
let mut decoder: Box<dyn Read> = if is_solid {
let mut decoder = if is_solid {
solid_decoder
} else {
match compression {
Compression::Lzma(filter_flag) => {
let mut data = &data[position + usize::from(filter_flag)..];
let stream = read_lzma_stream_header(&mut data).ok()?;
Box::new(XzDecoder::new_stream(data, stream))
Decoder::Lzma(XzDecoder::new_stream(data, stream))
}
Compression::BZip2 => {
Decoder::BZip2(BzDecoder::new(&data[position..]))
}
Compression::BZip2 => Box::new(BzDecoder::new(&data[position..])),
Compression::Zlib => {
Box::new(DeflateDecoder::new(&data[position..]))
Decoder::Zlib(DeflateDecoder::new(&data[position..]))
}
Compression::None => Box::new(&data[position..]),
Compression::None => Decoder::None(&data[position..]),
}
};
let mut void = io::sink();
Expand Down

0 comments on commit fb0c4ff

Please sign in to comment.