From 60083b0a6dc76c890aa3684cf35e87340ebfae5e Mon Sep 17 00:00:00 2001 From: Will Hedgecock Date: Wed, 2 Oct 2024 12:46:59 -0500 Subject: [PATCH] Switch from libflate to miniz_oxide --- musicxml/Cargo.toml | 3 +-- musicxml/src/parser/zip_parser.rs | 16 +++++++--------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/musicxml/Cargo.toml b/musicxml/Cargo.toml index 64c74d0..ca9d45c 100644 --- a/musicxml/Cargo.toml +++ b/musicxml/Cargo.toml @@ -17,8 +17,7 @@ publish.workspace = true rustdoc-args = ["--extend-css", "assets/docs.css"] [dependencies] -core2 = { version = "0.4", default-features = false } -libflate = { version = "2.1", default-features = false } +miniz_oxide = { version = "0.8", default-features = false, features = ["with-alloc"] } regex = { version = "1.10", default-features = false, features = ["perf-cache", "perf-dfa", "perf-onepass", "perf-backtrack", "perf-inline"] } musicxml_internal.workspace = true musicxml_macros.workspace = true diff --git a/musicxml/src/parser/zip_parser.rs b/musicxml/src/parser/zip_parser.rs index d0fabc6..63221d5 100644 --- a/musicxml/src/parser/zip_parser.rs +++ b/musicxml/src/parser/zip_parser.rs @@ -1,15 +1,14 @@ #![allow(dead_code)] use alloc::{collections::BTreeMap, vec::Vec}; -use core2::io::Read; -use libflate::deflate::Decoder; +use miniz_oxide::inflate::decompress_to_vec; const DEFLATE_METHOD_CODE: u16 = 8; const LOCAL_FILE_HEADER_LEN: usize = core::mem::size_of::(); const CENTRAL_FILE_HEADER_LEN: usize = core::mem::size_of::(); const CENTRAL_DIR_END_LEN: usize = core::mem::size_of::(); -#[repr(packed)] +#[repr(C, packed)] struct LocalFileHeader { signature: u32, version_needed_to_extract: u16, @@ -34,7 +33,7 @@ impl LocalFileHeader { } } -#[repr(packed)] +#[repr(C, packed)] struct CentralFileHeader { signature: u32, version_made_by: u16, @@ -68,7 +67,7 @@ impl CentralFileHeader { } } -#[repr(packed)] +#[repr(C, packed)] struct CentralDirEnd { signature: u32, number_of_disk: u16, @@ -233,14 +232,13 @@ impl<'a> ZipArchive<'a> { } pub fn read_file_to_string(&self, file_name: &str) -> Result { - let mut decoded_data = Vec::new(); let file = self .file_map .get(file_name) .ok_or(format!("File \"{file_name}\" not found within compressed archive"))?; - let mut decoder = - Decoder::new(&self.zip_data.content[file.relative_offset..(file.relative_offset + file.compressed_size)]); - decoder.read_to_end(&mut decoded_data).unwrap_or(0); + let decoded_data = + decompress_to_vec(&self.zip_data.content[file.relative_offset..(file.relative_offset + file.compressed_size)]) + .map_err(|e| e.to_string())?; String::from_utf8(decoded_data).map_err(|e| e.to_string()) }