diff --git a/src/read.rs b/src/read.rs index 119f51ba..0ab5acd2 100644 --- a/src/read.rs +++ b/src/read.rs @@ -8,7 +8,7 @@ use crate::crc32::Crc32Reader; use crate::extra_fields::{ExtendedTimestamp, ExtraField}; use crate::read::zip_archive::Shared; use crate::result::{ZipError, ZipResult}; -use crate::spec::{self, Block}; +use crate::spec::{self, FixedSizeBlock}; use crate::types::{ AesMode, AesVendorVersion, DateTime, System, ZipCentralEntryBlock, ZipFileData, ZipLocalEntryBlock, diff --git a/src/read/stream.rs b/src/read/stream.rs index d87e22fc..8f2ffa0c 100644 --- a/src/read/stream.rs +++ b/src/read/stream.rs @@ -6,7 +6,7 @@ use super::{ central_header_to_zip_file_inner, read_zipfile_from_stream, ZipCentralEntryBlock, ZipError, ZipFile, ZipFileData, ZipResult, }; -use crate::spec::Block; +use crate::spec::FixedSizeBlock; /// Stream decoder for zip. #[derive(Debug)] diff --git a/src/spec.rs b/src/spec.rs index 8738a7bd..387259fd 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -97,7 +97,7 @@ impl ExtraFieldMagic { pub const ZIP64_BYTES_THR: u64 = u32::MAX as u64; pub const ZIP64_ENTRY_THR: usize = u16::MAX as usize; -pub(crate) trait Block: Sized + Copy { +pub(crate) trait FixedSizeBlock: Sized + Copy { const MAGIC: Magic; fn magic(self) -> Magic; @@ -113,8 +113,10 @@ pub(crate) trait Block: Sized + Copy { Ok(block) } - fn deserialize(block: &[u8]) -> Self { - assert_eq!(block.len(), mem::size_of::()); + fn deserialize(block: &[u8]) -> ZipResult { + if block.len() != mem::size_of::() { + return Err(ZipError::InvalidArchive("Block is wrong size")); + } let block_ptr: *const Self = block.as_ptr().cast(); unsafe { block_ptr.read() } } @@ -212,7 +214,7 @@ pub(crate) struct Zip32CDEBlock { pub zip_file_comment_length: u16, } -impl Block for Zip32CDEBlock { +impl FixedSizeBlock for Zip32CDEBlock { const MAGIC: Magic = Magic::CENTRAL_DIRECTORY_END_SIGNATURE; #[inline(always)] @@ -391,7 +393,7 @@ pub(crate) struct Zip64CDELocatorBlock { pub number_of_disks: u32, } -impl Block for Zip64CDELocatorBlock { +impl FixedSizeBlock for Zip64CDELocatorBlock { const MAGIC: Magic = Magic::ZIP64_CENTRAL_DIRECTORY_END_LOCATOR_SIGNATURE; #[inline(always)] @@ -467,7 +469,7 @@ pub(crate) struct Zip64CDEBlock { pub central_directory_offset: u64, } -impl Block for Zip64CDEBlock { +impl FixedSizeBlock for Zip64CDEBlock { const MAGIC: Magic = Magic::ZIP64_CENTRAL_DIRECTORY_END_SIGNATURE; fn magic(self) -> Magic { @@ -708,7 +710,7 @@ mod test { pub file_name_length: u16, } - impl Block for TestBlock { + impl FixedSizeBlock for TestBlock { const MAGIC: Magic = Magic::literal(0x01111); fn magic(self) -> Magic { diff --git a/src/types.rs b/src/types.rs index 181592e6..f4d17acc 100644 --- a/src/types.rs +++ b/src/types.rs @@ -11,7 +11,7 @@ use std::sync::{Arc, OnceLock}; use chrono::{Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike}; use crate::result::{ZipError, ZipResult}; -use crate::spec::{self, Block}; +use crate::spec::{self, FixedSizeBlock}; pub(crate) mod ffi { pub const S_IFDIR: u32 = 0o0040000; @@ -891,7 +891,7 @@ pub(crate) struct ZipCentralEntryBlock { pub offset: u32, } -impl Block for ZipCentralEntryBlock { +impl FixedSizeBlock for ZipCentralEntryBlock { const MAGIC: spec::Magic = spec::Magic::CENTRAL_DIRECTORY_HEADER_SIGNATURE; #[inline(always)] @@ -938,7 +938,7 @@ pub(crate) struct ZipLocalEntryBlock { pub extra_field_length: u16, } -impl Block for ZipLocalEntryBlock { +impl FixedSizeBlock for ZipLocalEntryBlock { const MAGIC: spec::Magic = spec::Magic::LOCAL_FILE_HEADER_SIGNATURE; #[inline(always)] diff --git a/src/write.rs b/src/write.rs index 151258a8..acf9ac98 100644 --- a/src/write.rs +++ b/src/write.rs @@ -5,7 +5,7 @@ use crate::aes::AesWriter; use crate::compression::CompressionMethod; use crate::read::{find_content, Config, ZipArchive, ZipFile, ZipFileReader}; use crate::result::{ZipError, ZipResult}; -use crate::spec::{self, Block}; +use crate::spec::{self, FixedSizeBlock}; #[cfg(feature = "aes-crypto")] use crate::types::AesMode; use crate::types::{