From afb8ab448949eb19e09e1bdb4b263dc487a9be21 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Fri, 21 Jun 2024 02:11:16 +0900 Subject: Implement deserialize Remove bincode crate and replace it by own written serializer --- src/zip/driver.rs | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) (limited to 'src/zip/driver.rs') diff --git a/src/zip/driver.rs b/src/zip/driver.rs index d575509..e5aa58d 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs @@ -1,6 +1,6 @@ use crate::driver::{ArchiveRead, ArchiveWrite, Driver}; use crate::zip::error::{ZipError, ZipResult}; -use crate::zip::structs::{EOCDR64Locator, CDR, EOCDR, EOCDR64}; +use crate::zip::structs::{deserialize, EOCDR64Locator, CDR, EOCDR, EOCDR64}; use crate::zip::ZipFile; use std::collections::HashMap as Map; use std::fs::File; @@ -41,7 +41,7 @@ impl ArchiveRead for Zip { io.read(&mut buf)?; buf }; - let eocdr: EOCDR = bincode::deserialize(&buf).map_err(|_| ZipError::InvalidEOCDR)?; + let eocdr: EOCDR = deserialize(&buf).map_err(|_| ZipError::InvalidEOCDR)?; let comment = { let mut buf: Vec = vec![0; eocdr.comment_len as usize]; io.read(&mut buf)?; @@ -55,31 +55,31 @@ impl ArchiveRead for Zip { io.read(&mut buf)?; buf }; - let (cd_pointer, cd_size, cd_records) = - if u32::from_le_bytes(buf[0..4].try_into().unwrap()) == 0x07064b50 { - let eocdr64locator: EOCDR64Locator = - bincode::deserialize(&buf[4..]).map_err(|_| ZipError::InvalidEOCDR64Locator)?; - - io.seek(SeekFrom::Start(eocdr64locator.eocdr64_pointer))?; - let buf = { - let mut buf = [0; 56]; - io.read(&mut buf)?; - buf - }; - if u32::from_le_bytes(buf[0..4].try_into().unwrap()) != 0x06064b50 { - return Err(ZipError::InvalidEOCDR64Signature.into()); - } - let eocdr64: EOCDR64 = - bincode::deserialize(&buf[4..]).map_err(|_| ZipError::InvalidEOCDR64)?; - - (eocdr64.cd_pointer, eocdr64.cd_size, eocdr64.cd_records) - } else { - ( - eocdr.cd_pointer as u64, - eocdr.cd_size as u64, - eocdr.cd_records as u64, - ) + let (cd_pointer, cd_size, cd_records) = if u32::from_le_bytes(buf[0..4].try_into().unwrap()) + == 0x07064b50 + { + let eocdr64locator: EOCDR64Locator = + deserialize(&buf[4..]).map_err(|_| ZipError::InvalidEOCDR64Locator)?; + + io.seek(SeekFrom::Start(eocdr64locator.eocdr64_pointer))?; + let buf = { + let mut buf = [0; 56]; + io.read(&mut buf)?; + buf }; + if u32::from_le_bytes(buf[0..4].try_into().unwrap()) != 0x06064b50 { + return Err(ZipError::InvalidEOCDR64Signature.into()); + } + let eocdr64: EOCDR64 = deserialize(&buf[4..]).map_err(|_| ZipError::InvalidEOCDR64)?; + + (eocdr64.cd_pointer, eocdr64.cd_size, eocdr64.cd_records) + } else { + ( + eocdr.cd_pointer as u64, + eocdr.cd_size as u64, + eocdr.cd_records as u64, + ) + }; // Read cd records let mut files = Map::with_capacity(cd_records as usize); @@ -96,8 +96,7 @@ impl ArchiveRead for Zip { return Err(ZipError::InvalidCDRSignature.into()); } p += 4; - let cdr: CDR = - bincode::deserialize(&buf[p..p + 42]).map_err(|_| ZipError::InvalidCDR)?; + let cdr: CDR = deserialize(&buf[p..p + 42]).map_err(|_| ZipError::InvalidCDR)?; p += 42; let name = String::from_utf8(buf[p..p + cdr.name_len as usize].into()).unwrap(); p += cdr.name_len as usize; -- cgit v1.2.3