aboutsummaryrefslogtreecommitdiff
path: root/src/zip
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-07-16 20:08:01 +0900
committerIgor Tolmachev <me@igorek.dev>2024-07-16 20:08:01 +0900
commit9c0e544e79a4f7874dab449674a11d899bf61963 (patch)
tree89ce5177ede27b3966f9589e012887e8614a7b38 /src/zip
parentcc18a545a87ca616f05114d174690e5cc9614669 (diff)
downloadarchivator-9c0e544e79a4f7874dab449674a11d899bf61963.tar.gz
archivator-9c0e544e79a4f7874dab449674a11d899bf61963.zip
Add tests and fix bugs
Diffstat (limited to 'src/zip')
-rw-r--r--src/zip/driver.rs20
-rw-r--r--src/zip/encryption.rs2
-rw-r--r--src/zip/error.rs6
3 files changed, 14 insertions, 14 deletions
diff --git a/src/zip/driver.rs b/src/zip/driver.rs
index 62da39f..631c4ed 100644
--- a/src/zip/driver.rs
+++ b/src/zip/driver.rs
@@ -68,23 +68,23 @@ impl<Io: Read + Seek> ArchiveRead for Zip<Io> {
68 let limit = 65557.min(io.seek(SeekFrom::End(0))?) as i64; 68 let limit = 65557.min(io.seek(SeekFrom::End(0))?) as i64;
69 let start = io.seek(SeekFrom::End(-limit))?; 69 let start = io.seek(SeekFrom::End(-limit))?;
70 let pos = start 70 let pos = start
71 + io.read_vec(limit as usize - 18)? 71 + io.read_vec(
72 .windows(4) 72 (limit as usize)
73 .rposition(|v| u32::from_le_bytes(v.try_into().unwrap()) == 0x06054b50) 73 .checked_sub(18)
74 .ok_or(ZipError::EOCDRNotFound)? as u64; 74 .ok_or(ZipError::EocdrNotFound)?,
75 )?
76 .windows(4)
77 .rposition(|v| u32::from_le_bytes(v.try_into().unwrap()) == 0x06054b50)
78 .ok_or(ZipError::EocdrNotFound)? as u64;
75 79
76 // Read eocdr 80 // Read eocdr
77 io.seek(SeekFrom::Start(pos + 4))?; 81 io.seek(SeekFrom::Start(pos + 4))?;
78 let buf = io.read_arr::<18>()?; 82 let buf = io.read_arr::<18>()?;
79 let eocdr: Eocdr = deserialize(&buf).unwrap(); 83 let eocdr: Eocdr = deserialize(&buf).unwrap();
80 let comment = { 84 let comment = String::from_cp437(io.read_vec(eocdr.comment_len as usize)?);
81 let mut buf: Vec<u8> = vec![0; eocdr.comment_len as usize];
82 io.read(&mut buf)?;
83 String::from_cp437(buf)
84 };
85 85
86 // Try to find eocdr64locator 86 // Try to find eocdr64locator
87 io.seek(SeekFrom::Start(pos - 20))?; 87 io.seek(SeekFrom::Start(pos.saturating_sub(20)))?;
88 let buf = io.read_arr::<20>()?; 88 let buf = io.read_arr::<20>()?;
89 let (cd_pointer, cd_size, cd_records) = 89 let (cd_pointer, cd_size, cd_records) =
90 // If locator found then read eocdr64 90 // If locator found then read eocdr64
diff --git a/src/zip/encryption.rs b/src/zip/encryption.rs
index 28a6bdb..76824a1 100644
--- a/src/zip/encryption.rs
+++ b/src/zip/encryption.rs
@@ -72,7 +72,7 @@ impl<Io: Read> WeakDecoder<Io> {
72 72
73 fn decode_byte(&mut self, byte: u8) -> u8 { 73 fn decode_byte(&mut self, byte: u8) -> u8 {
74 let key = self.key2 | 2; 74 let key = self.key2 | 2;
75 let byte = byte ^ ((key * (key ^ 1)) >> 8) as u8; 75 let byte = byte ^ ((key.wrapping_mul(key ^ 1)) >> 8) as u8;
76 self.update_keys(byte); 76 self.update_keys(byte);
77 byte 77 byte
78 } 78 }
diff --git a/src/zip/error.rs b/src/zip/error.rs
index a4b8c2b..525a67b 100644
--- a/src/zip/error.rs
+++ b/src/zip/error.rs
@@ -4,9 +4,9 @@ use std::fmt::Display;
4 4
5pub type ZipResult<T> = ArchiveResult<T, ZipError>; 5pub type ZipResult<T> = ArchiveResult<T, ZipError>;
6 6
7#[derive(Debug)] 7#[derive(Debug, PartialEq, Eq)]
8pub enum ZipError { 8pub enum ZipError {
9 EOCDRNotFound, 9 EocdrNotFound,
10 InvalidEOCDR64Signature, 10 InvalidEOCDR64Signature,
11 InvalidFileHeaderSignature, 11 InvalidFileHeaderSignature,
12 InvalidCDRSignature, 12 InvalidCDRSignature,
@@ -38,7 +38,7 @@ impl From<ZipError> for ArchiveError<ZipError> {
38impl Display for ZipError { 38impl Display for ZipError {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 match self { 40 match self {
41 Self::EOCDRNotFound => write!(f, "End of central directory record not found"), 41 Self::EocdrNotFound => write!(f, "End of central directory record not found"),
42 Self::InvalidEOCDR64Signature => { 42 Self::InvalidEOCDR64Signature => {
43 write!( 43 write!(
44 f, 44 f,