aboutsummaryrefslogtreecommitdiff
path: root/src/zip/file/info.rs
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-07-20 16:52:39 +0900
committerIgor Tolmachev <me@igorek.dev>2024-07-20 16:52:39 +0900
commit7bcdc3b4ca460aec2b98fb2dca6165788c562b05 (patch)
tree63f9616fc1b7f9ca6e414a4d32910720e155690c /src/zip/file/info.rs
parent5f4ceda88c7299deb317f8d22a99ab2521c5a380 (diff)
downloadarchivator-7bcdc3b4ca460aec2b98fb2dca6165788c562b05.tar.gz
archivator-7bcdc3b4ca460aec2b98fb2dca6165788c562b05.zip
Partial aes implementation and others improvements
Diffstat (limited to 'src/zip/file/info.rs')
-rw-r--r--src/zip/file/info.rs43
1 files changed, 20 insertions, 23 deletions
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 @@
1use crate::driver::ArchiveFileInfo; 1use crate::driver::ArchiveFileInfo;
2use crate::zip::{ZipError, ZipResult}; 2use crate::zip::datetime::DosDateTime;
3use chrono::{DateTime, Local}; 3use chrono::{DateTime, Local};
4 4
5#[derive(Debug, PartialEq, Eq, Clone)] 5#[derive(Debug, PartialEq, Eq, Clone)]
@@ -14,42 +14,30 @@ pub enum CompressionMethod {
14} 14}
15 15
16impl CompressionMethod { 16impl CompressionMethod {
17 pub(crate) fn from_struct_id(id: u16) -> ZipResult<Self> { 17 #[inline]
18 Ok(match id { 18 pub(crate) fn from_struct_id(id: u16) -> Self {
19 match id {
19 0 => Self::Store, 20 0 => Self::Store,
20 8 => Self::Deflate, 21 8 => Self::Deflate,
21 12 => Self::BZip2, 22 12 => Self::BZip2,
22 14 => Self::Lzma, 23 14 => Self::Lzma,
23 93 => Self::Zstd, 24 93 => Self::Zstd,
24 95 => Self::Xz, 25 95 => Self::Xz,
25 1..=7 | 9..=11 | 13 | 15..=20 | 94 | 96..=99 => Self::Unsupported(id), 26 _ => Self::Unsupported(id),
26 21..=92 | 100.. => return Err(ZipError::InvalidCompressionMethod(id).into()), 27 }
27 })
28 } 28 }
29} 29}
30 30
31#[derive(Debug, PartialEq, Eq, Clone)] 31#[derive(Debug, PartialEq, Eq, Clone)]
32pub enum EncryptionMethod { 32pub enum EncryptionMethod {
33 None, 33 None,
34 Weak(u8), 34 Weak, // ZipCrypto
35 Aes128, // WinZip encryption
36 Aes192,
37 Aes256,
35 Unsupported, 38 Unsupported,
36} 39}
37 40
38impl EncryptionMethod {
39 pub(crate) fn from_bif_flag(bit_flag: BitFlag, crc: u32, dos_time: u16) -> EncryptionMethod {
40 match (bit_flag.is_encrypted(), bit_flag.is_strong_encryption()) {
41 (false, false) => EncryptionMethod::None,
42 (true, false) => EncryptionMethod::Weak(if bit_flag.is_has_data_descriptor() {
43 (dos_time >> 8) as u8 // Info-ZIP modification
44 } else {
45 (crc >> 24) as u8
46 }),
47 (true, true) => EncryptionMethod::Unsupported,
48 _ => panic!("impossible"),
49 }
50 }
51}
52
53#[derive(Debug, PartialEq, Eq, Clone, Copy)] 41#[derive(Debug, PartialEq, Eq, Clone, Copy)]
54pub struct BitFlag { 42pub struct BitFlag {
55 flag: u16, 43 flag: u16,
@@ -159,7 +147,7 @@ pub struct ZipFileInfo {
159} 147}
160 148
161impl ZipFileInfo { 149impl ZipFileInfo {
162 pub fn new( 150 pub(crate) fn new(
163 compression_method: CompressionMethod, 151 compression_method: CompressionMethod,
164 encryption_method: EncryptionMethod, 152 encryption_method: EncryptionMethod,
165 bit_flag: BitFlag, 153 bit_flag: BitFlag,
@@ -189,6 +177,15 @@ impl ZipFileInfo {
189 } 177 }
190 } 178 }
191 179
180 #[inline]
181 pub(crate) fn password_check(&self) -> u8 {
182 if self.bit_flag.is_has_data_descriptor() {
183 (self.mtime.to_dos_time() >> 8) as u8
184 } else {
185 (self.crc >> 24) as u8
186 }
187 }
188
192 pub fn is_dir(&self) -> bool { 189 pub fn is_dir(&self) -> bool {
193 self.name.ends_with("/") 190 self.name.ends_with("/")
194 } 191 }