chore: Add check for wrong-length blocks, and incorporate fixed-size requirement into the trait name

This commit is contained in:
Chris Hennick 2024-06-02 21:48:21 -07:00
parent 9218599b40
commit eacc320fe0
No known key found for this signature in database
GPG key ID: DA47AABA4961C509
5 changed files with 15 additions and 13 deletions

View file

@ -8,7 +8,7 @@ use crate::crc32::Crc32Reader;
use crate::extra_fields::{ExtendedTimestamp, ExtraField}; use crate::extra_fields::{ExtendedTimestamp, ExtraField};
use crate::read::zip_archive::Shared; use crate::read::zip_archive::Shared;
use crate::result::{ZipError, ZipResult}; use crate::result::{ZipError, ZipResult};
use crate::spec::{self, Block}; use crate::spec::{self, FixedSizeBlock};
use crate::types::{ use crate::types::{
AesMode, AesVendorVersion, DateTime, System, ZipCentralEntryBlock, ZipFileData, AesMode, AesVendorVersion, DateTime, System, ZipCentralEntryBlock, ZipFileData,
ZipLocalEntryBlock, ZipLocalEntryBlock,

View file

@ -6,7 +6,7 @@ use super::{
central_header_to_zip_file_inner, read_zipfile_from_stream, ZipCentralEntryBlock, ZipError, central_header_to_zip_file_inner, read_zipfile_from_stream, ZipCentralEntryBlock, ZipError,
ZipFile, ZipFileData, ZipResult, ZipFile, ZipFileData, ZipResult,
}; };
use crate::spec::Block; use crate::spec::FixedSizeBlock;
/// Stream decoder for zip. /// Stream decoder for zip.
#[derive(Debug)] #[derive(Debug)]

View file

@ -97,7 +97,7 @@ impl ExtraFieldMagic {
pub const ZIP64_BYTES_THR: u64 = u32::MAX as u64; pub const ZIP64_BYTES_THR: u64 = u32::MAX as u64;
pub const ZIP64_ENTRY_THR: usize = u16::MAX as usize; 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; const MAGIC: Magic;
fn magic(self) -> Magic; fn magic(self) -> Magic;
@ -113,8 +113,10 @@ pub(crate) trait Block: Sized + Copy {
Ok(block) Ok(block)
} }
fn deserialize(block: &[u8]) -> Self { fn deserialize(block: &[u8]) -> ZipResult<Self> {
assert_eq!(block.len(), mem::size_of::<Self>()); if block.len() != mem::size_of::<Self>() {
return Err(ZipError::InvalidArchive("Block is wrong size"));
}
let block_ptr: *const Self = block.as_ptr().cast(); let block_ptr: *const Self = block.as_ptr().cast();
unsafe { block_ptr.read() } unsafe { block_ptr.read() }
} }
@ -212,7 +214,7 @@ pub(crate) struct Zip32CDEBlock {
pub zip_file_comment_length: u16, pub zip_file_comment_length: u16,
} }
impl Block for Zip32CDEBlock { impl FixedSizeBlock for Zip32CDEBlock {
const MAGIC: Magic = Magic::CENTRAL_DIRECTORY_END_SIGNATURE; const MAGIC: Magic = Magic::CENTRAL_DIRECTORY_END_SIGNATURE;
#[inline(always)] #[inline(always)]
@ -391,7 +393,7 @@ pub(crate) struct Zip64CDELocatorBlock {
pub number_of_disks: u32, pub number_of_disks: u32,
} }
impl Block for Zip64CDELocatorBlock { impl FixedSizeBlock for Zip64CDELocatorBlock {
const MAGIC: Magic = Magic::ZIP64_CENTRAL_DIRECTORY_END_LOCATOR_SIGNATURE; const MAGIC: Magic = Magic::ZIP64_CENTRAL_DIRECTORY_END_LOCATOR_SIGNATURE;
#[inline(always)] #[inline(always)]
@ -467,7 +469,7 @@ pub(crate) struct Zip64CDEBlock {
pub central_directory_offset: u64, pub central_directory_offset: u64,
} }
impl Block for Zip64CDEBlock { impl FixedSizeBlock for Zip64CDEBlock {
const MAGIC: Magic = Magic::ZIP64_CENTRAL_DIRECTORY_END_SIGNATURE; const MAGIC: Magic = Magic::ZIP64_CENTRAL_DIRECTORY_END_SIGNATURE;
fn magic(self) -> Magic { fn magic(self) -> Magic {
@ -708,7 +710,7 @@ mod test {
pub file_name_length: u16, pub file_name_length: u16,
} }
impl Block for TestBlock { impl FixedSizeBlock for TestBlock {
const MAGIC: Magic = Magic::literal(0x01111); const MAGIC: Magic = Magic::literal(0x01111);
fn magic(self) -> Magic { fn magic(self) -> Magic {

View file

@ -11,7 +11,7 @@ use std::sync::{Arc, OnceLock};
use chrono::{Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike}; use chrono::{Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike};
use crate::result::{ZipError, ZipResult}; use crate::result::{ZipError, ZipResult};
use crate::spec::{self, Block}; use crate::spec::{self, FixedSizeBlock};
pub(crate) mod ffi { pub(crate) mod ffi {
pub const S_IFDIR: u32 = 0o0040000; pub const S_IFDIR: u32 = 0o0040000;
@ -891,7 +891,7 @@ pub(crate) struct ZipCentralEntryBlock {
pub offset: u32, pub offset: u32,
} }
impl Block for ZipCentralEntryBlock { impl FixedSizeBlock for ZipCentralEntryBlock {
const MAGIC: spec::Magic = spec::Magic::CENTRAL_DIRECTORY_HEADER_SIGNATURE; const MAGIC: spec::Magic = spec::Magic::CENTRAL_DIRECTORY_HEADER_SIGNATURE;
#[inline(always)] #[inline(always)]
@ -938,7 +938,7 @@ pub(crate) struct ZipLocalEntryBlock {
pub extra_field_length: u16, pub extra_field_length: u16,
} }
impl Block for ZipLocalEntryBlock { impl FixedSizeBlock for ZipLocalEntryBlock {
const MAGIC: spec::Magic = spec::Magic::LOCAL_FILE_HEADER_SIGNATURE; const MAGIC: spec::Magic = spec::Magic::LOCAL_FILE_HEADER_SIGNATURE;
#[inline(always)] #[inline(always)]

View file

@ -5,7 +5,7 @@ use crate::aes::AesWriter;
use crate::compression::CompressionMethod; use crate::compression::CompressionMethod;
use crate::read::{find_content, Config, ZipArchive, ZipFile, ZipFileReader}; use crate::read::{find_content, Config, ZipArchive, ZipFile, ZipFileReader};
use crate::result::{ZipError, ZipResult}; use crate::result::{ZipError, ZipResult};
use crate::spec::{self, Block}; use crate::spec::{self, FixedSizeBlock};
#[cfg(feature = "aes-crypto")] #[cfg(feature = "aes-crypto")]
use crate::types::AesMode; use crate::types::AesMode;
use crate::types::{ use crate::types::{