From 28208498429784f8ab9c62a12ddbf79ce8f19e4b Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Mon, 2 Sep 2024 19:54:34 +0300 Subject: Add struct size constants --- src/zip/driver.rs | 12 +++++++----- src/zip/file/read.rs | 4 ++-- src/zip/structs.rs | 5 +++++ 3 files changed, 14 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/zip/driver.rs b/src/zip/driver.rs index b44c453..31f86b6 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs @@ -4,7 +4,8 @@ use crate::zip::cp437::FromCp437; use crate::zip::datetime::DosDateTime; use crate::zip::structs::{ deserialize, AesField, Cdr, Eocdr, Eocdr64, Eocdr64Locator, ExtraHeader, CDR_SIGNATURE, - EOCDR64_LOCATOR_SIGNATURE, EOCDR64_SIGNATURE, EOCDR_SIGNATURE, + CDR_SIZE, EOCDR64_LOCATOR_SIGNATURE, EOCDR64_LOCATOR_SIZE, EOCDR64_SIGNATURE, EOCDR64_SIZE, + EOCDR_SIGNATURE, EOCDR_SIZE, }; use crate::zip::{ BitFlag, CompressionMethod, EncryptionMethod, ZipError, ZipFileInfo, ZipFileReader, @@ -91,7 +92,7 @@ impl ArchiveRead for Zip { // Read eocdr io.seek(SeekFrom::Start(eocdr_pos + 4))?; - let buf = io.read_arr::<18>()?; + let buf = io.read_arr::<{ EOCDR_SIZE - 4 }>()?; let eocdr: Eocdr = deserialize(&buf).unwrap(); let comment = String::from_cp437(io.read_vec(eocdr.comment_len as usize)?); @@ -102,7 +103,7 @@ impl ArchiveRead for Zip { // Try to find eocdr64locator if eocdr_pos >= 20 { io.seek(SeekFrom::Start(eocdr_pos - 20))?; - let buf = io.read_arr::<20>()?; + let buf = io.read_arr::()?; if buf[..4] == EOCDR64_LOCATOR_SIGNATURE { let locator: Eocdr64Locator = deserialize(&buf[4..]).unwrap(); @@ -116,7 +117,8 @@ impl ArchiveRead for Zip { return Err(ZipError::Overlapping("Eocdr64", "Eocdr64Locator")); } - let eocdr64: Eocdr64 = deserialize(&io.read_arr::<54>()?).unwrap(); + let eocdr64: Eocdr64 = + deserialize(&io.read_arr::<{ EOCDR64_SIZE - 4 }>()?).unwrap(); if locator.eocdr64_pointer + eocdr64.eocdr64_size + 32 > eocdr_pos { return Err(ZipError::Overlapping("Eocdr64", "Eocdr64Locator")); } @@ -148,7 +150,7 @@ impl ArchiveRead for Zip { if cd_reader.read_arr()? != CDR_SIGNATURE { return Err(ZipError::InvalidSignature("Cdr")); } - let cdr: Cdr = deserialize(&cd_reader.read_arr::<42>()?).unwrap(); + let cdr: Cdr = deserialize(&cd_reader.read_arr::<{ CDR_SIZE - 4 }>()?).unwrap(); let bit_flag = BitFlag::new(cdr.bit_flag); let name = cd_reader.read_vec(cdr.name_len as usize)?; diff --git a/src/zip/file/read.rs b/src/zip/file/read.rs index 567fb75..1ac0dc1 100644 --- a/src/zip/file/read.rs +++ b/src/zip/file/read.rs @@ -1,7 +1,7 @@ use crate::driver::FileDriver; use crate::utils::{IoCursor, ReadUtils}; use crate::zip::encryption::{AesDecoder, Keys, WeakDecoder}; -use crate::zip::structs::FILE_HEADER_SIGNATURE; +use crate::zip::structs::{FILE_HEADER_SIGNATURE, FILE_HEADER_SIZE}; use crate::zip::{CompressionMethod, EncryptionMethod, ZipError, ZipFileInfo, ZipResult}; use aes::cipher::KeyInit; use aes::{Aes128, Aes192, Aes256}; @@ -235,7 +235,7 @@ impl<'d, Io: Read + Seek> ZipFileReader<'d, Io> { ) -> ZipResult { io.seek(SeekFrom::Start(info.header_pointer))?; - let buf = io.read_arr::<30>()?; + let buf = io.read_arr::()?; if buf[..4] != FILE_HEADER_SIGNATURE { return Err(ZipError::InvalidSignature("FileHeader")); } diff --git a/src/zip/structs.rs b/src/zip/structs.rs index 4b4524f..ccd5687 100644 --- a/src/zip/structs.rs +++ b/src/zip/structs.rs @@ -2,8 +2,10 @@ use crate::structs::{ByteOrder, Settings, StructResult, VariantIndexType}; use serde::{Deserialize, Serialize}; pub const FILE_HEADER_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x03, 0x04]; +pub const FILE_HEADER_SIZE: usize = 30; pub const EOCDR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x05, 0x06]; +pub const EOCDR_SIZE: usize = 22; #[derive(Serialize, Deserialize)] pub struct Eocdr { pub eocdr_disk: u16, @@ -16,6 +18,7 @@ pub struct Eocdr { } pub const EOCDR64_LOCATOR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x06, 0x07]; +pub const EOCDR64_LOCATOR_SIZE: usize = 20; #[derive(Serialize, Deserialize)] pub struct Eocdr64Locator { pub eocdr64_disk: u32, @@ -24,6 +27,7 @@ pub struct Eocdr64Locator { } pub const EOCDR64_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x06, 0x06]; +pub const EOCDR64_SIZE: usize = 56; #[derive(Serialize, Deserialize)] pub struct Eocdr64 { pub eocdr64_size: u64, @@ -38,6 +42,7 @@ pub struct Eocdr64 { } pub const CDR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x01, 0x02]; +pub const CDR_SIZE: usize = 46; #[derive(Serialize, Deserialize)] pub struct Cdr { pub version: u16, -- cgit v1.2.3