From a83767f9fbd51df654901b52bdba7838f6a10bf9 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Tue, 16 Jul 2024 01:59:53 +0900 Subject: Add traditional PKWARE decryption. - Compression and encryption may not work together - Password check is not yet implemented - Unoptimized crc32 function --- src/zip/file/info.rs | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'src/zip/file/info.rs') diff --git a/src/zip/file/info.rs b/src/zip/file/info.rs index 4e1b293..f5d4d8a 100644 --- a/src/zip/file/info.rs +++ b/src/zip/file/info.rs @@ -2,7 +2,7 @@ use crate::driver::ArchiveFileInfo; use crate::zip::{ZipError, ZipResult}; use chrono::{DateTime, Local}; -#[derive(Debug, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum CompressionMethod { Store, Deflate, @@ -10,31 +10,49 @@ pub enum CompressionMethod { Lzma, Zstd, Xz, - Unsupported, + Unsupported(u16), } impl CompressionMethod { pub(crate) fn from_struct_id(id: u16) -> ZipResult { - match id { - 0 => Ok(Self::Store), - 8 => Ok(Self::Deflate), - 12 => Ok(Self::BZip2), - 14 => Ok(Self::Lzma), - 93 => Ok(Self::Zstd), - 95 => Ok(Self::Xz), - 1..=7 | 9..=11 | 13 | 15..=20 | 94 | 96..=99 => Ok(Self::Unsupported), - 21..=92 | 100.. => Err(ZipError::InvalidCompressionMethod.into()), + Ok(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()), + }) + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub enum EncryptionMethod { + None, + Weak, + Unsupported, +} + +impl EncryptionMethod { + pub(crate) fn from_bit_flag(bit_flag: BitFlag) -> EncryptionMethod { + match (bit_flag.is_encrypted(), bit_flag.is_strong_encryption()) { + (false, false) => EncryptionMethod::None, + (true, false) => EncryptionMethod::Weak, + (true, true) => EncryptionMethod::Unsupported, + _ => panic!("impossible"), } } } -#[derive(Debug, Clone)] +#[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct BitFlag { flag: u16, } pub mod bit { - #[derive(Debug, PartialEq, Eq)] + #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum DeflateMode { Normal, Maximum, @@ -123,6 +141,7 @@ impl BitFlag { #[derive(Debug, Clone)] pub struct ZipFileInfo { pub compression_method: CompressionMethod, + pub encryption_method: EncryptionMethod, pub bit_flag: BitFlag, pub mtime: DateTime, pub atime: Option>, @@ -138,6 +157,7 @@ pub struct ZipFileInfo { impl ZipFileInfo { pub fn new( compression_method: CompressionMethod, + encryption_method: EncryptionMethod, bit_flag: BitFlag, mtime: DateTime, atime: Option>, @@ -151,6 +171,7 @@ impl ZipFileInfo { ) -> Self { Self { compression_method, + encryption_method, bit_flag, mtime, atime, -- cgit v1.2.3