Skip to content

Commit

Permalink
Fix wrong zip decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbegamerxx954 committed Aug 3, 2024
1 parent a515cec commit 78de49e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 25 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ materialbin = { version = "0.1.1", git = "https://github.com/mcbegamerxx954/mate
scroll = "0.12.0"
tempfile = "3.10.1"
zip = { version = "2.1.3", default-features = false, features = ["deflate", "time"] }
zune-inflate = "0.2.54"


19 changes: 5 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use zip::{
write::{ExtendedFileOptions, FileOptions},
ZipArchive, ZipWriter,
};
use zune_inflate::DeflateOptions;

#[derive(Parser)]
#[clap(name = "Material Updater", version = "0.1.6")]
#[command(version, about, long_about = None, styles = get_style())]
Expand Down Expand Up @@ -185,13 +185,14 @@ where
let mut output_zip = ZipWriter::new(output);
let mut translated_shaders = 0;
for index in 0..input_zip.len() {
let mut file = input_zip.by_index_raw(index)?;
let mut file = input_zip.by_index(index)?;
if !file.name().ends_with(".material.bin") {
output_zip.raw_copy_file(file)?;
continue;
}
print!("Processing file {}", style(file.name()).cyan());
let data = fast_decompress(&mut file)?;
let mut data = Vec::with_capacity(file.size().try_into()?);
file.read_to_end(&mut data)?;
let material = match read_material(&data) {
Ok(material) => material,
Err(_) => {
Expand All @@ -212,17 +213,7 @@ where
);
Ok(())
}
fn fast_decompress(zipfile: &mut ZipFile) -> anyhow::Result<Vec<u8>> {
let mut output = Vec::with_capacity(zipfile.size().try_into()?);
let _data = zipfile.read_to_end(&mut output)?;
const LIMIT: usize = 100_000_000;
let options = DeflateOptions::default()
.set_size_hint(zipfile.size().try_into()?)
.set_limit(LIMIT);
let mut decoder = zune_inflate::DeflateDecoder::new_with_options(&output, options);
let decompressed = decoder.decode_deflate()?;
Ok(decompressed)
}

fn read_material(data: &[u8]) -> anyhow::Result<CompiledMaterialDefinition> {
for version in materialbin::ALL_VERSIONS {
if let Ok(material) = data.pread_with(0, version) {
Expand Down

0 comments on commit 78de49e

Please sign in to comment.