From 7bcdc3b4ca460aec2b98fb2dca6165788c562b05 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Sat, 20 Jul 2024 16:52:39 +0900 Subject: Partial aes implementation and others improvements --- src/zip/file/info.rs | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'src/zip/file/info.rs') diff --git a/src/zip/file/info.rs b/src/zip/file/info.rs index 599dcc3..38ea984 100644 --- a/src/zip/file/info.rs +++ b/src/zip/file/info.rs @@ -1,5 +1,5 @@ use crate::driver::ArchiveFileInfo; -use crate::zip::{ZipError, ZipResult}; +use crate::zip::datetime::DosDateTime; use chrono::{DateTime, Local}; #[derive(Debug, PartialEq, Eq, Clone)] @@ -14,42 +14,30 @@ pub enum CompressionMethod { } impl CompressionMethod { - pub(crate) fn from_struct_id(id: u16) -> ZipResult { - Ok(match id { + #[inline] + pub(crate) fn from_struct_id(id: u16) -> Self { + match id { 0 => Self::Store, 8 => Self::Deflate, 12 => Self::BZip2, 14 => Self::Lzma, 93 => Self::Zstd, 95 => Self::Xz, - 1..=7 | 9..=11 | 13 | 15..=20 | 94 | 96..=99 => Self::Unsupported(id), - 21..=92 | 100.. => return Err(ZipError::InvalidCompressionMethod(id).into()), - }) + _ => Self::Unsupported(id), + } } } #[derive(Debug, PartialEq, Eq, Clone)] pub enum EncryptionMethod { None, - Weak(u8), + Weak, // ZipCrypto + Aes128, // WinZip encryption + Aes192, + Aes256, Unsupported, } -impl EncryptionMethod { - pub(crate) fn from_bif_flag(bit_flag: BitFlag, crc: u32, dos_time: u16) -> EncryptionMethod { - match (bit_flag.is_encrypted(), bit_flag.is_strong_encryption()) { - (false, false) => EncryptionMethod::None, - (true, false) => EncryptionMethod::Weak(if bit_flag.is_has_data_descriptor() { - (dos_time >> 8) as u8 // Info-ZIP modification - } else { - (crc >> 24) as u8 - }), - (true, true) => EncryptionMethod::Unsupported, - _ => panic!("impossible"), - } - } -} - #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct BitFlag { flag: u16, @@ -159,7 +147,7 @@ pub struct ZipFileInfo { } impl ZipFileInfo { - pub fn new( + pub(crate) fn new( compression_method: CompressionMethod, encryption_method: EncryptionMethod, bit_flag: BitFlag, @@ -189,6 +177,15 @@ impl ZipFileInfo { } } + #[inline] + pub(crate) fn password_check(&self) -> u8 { + if self.bit_flag.is_has_data_descriptor() { + (self.mtime.to_dos_time() >> 8) as u8 + } else { + (self.crc >> 24) as u8 + } + } + pub fn is_dir(&self) -> bool { self.name.ends_with("/") } -- cgit v1.2.3