aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTolmachev Igor <me@igorek.dev>2024-09-02 19:54:34 +0300
committerTolmachev Igor <me@igorek.dev>2024-09-02 20:27:28 +0300
commit28208498429784f8ab9c62a12ddbf79ce8f19e4b (patch)
treeba527ab36b5e0eeec490ed31f89baf509f6d7ccb
parentdafe3b01d7dfe5f314dea37c312beae20e017f4e (diff)
downloadarchivator-development.tar.gz
archivator-development.zip
Add struct size constantsdevelopment
-rw-r--r--src/zip/driver.rs12
-rw-r--r--src/zip/file/read.rs4
-rw-r--r--src/zip/structs.rs5
3 files changed, 14 insertions, 7 deletions
diff --git a/src/zip/driver.rs b/src/zip/driver.rs
index b44c453..31f86b6 100644
--- a/src/zip/driver.rs
+++ b/src/zip/driver.rs
@@ -4,7 +4,8 @@ use crate::zip::cp437::FromCp437;
4use crate::zip::datetime::DosDateTime; 4use crate::zip::datetime::DosDateTime;
5use crate::zip::structs::{ 5use crate::zip::structs::{
6 deserialize, AesField, Cdr, Eocdr, Eocdr64, Eocdr64Locator, ExtraHeader, CDR_SIGNATURE, 6 deserialize, AesField, Cdr, Eocdr, Eocdr64, Eocdr64Locator, ExtraHeader, CDR_SIGNATURE,
7 EOCDR64_LOCATOR_SIGNATURE, EOCDR64_SIGNATURE, EOCDR_SIGNATURE, 7 CDR_SIZE, EOCDR64_LOCATOR_SIGNATURE, EOCDR64_LOCATOR_SIZE, EOCDR64_SIGNATURE, EOCDR64_SIZE,
8 EOCDR_SIGNATURE, EOCDR_SIZE,
8}; 9};
9use crate::zip::{ 10use crate::zip::{
10 BitFlag, CompressionMethod, EncryptionMethod, ZipError, ZipFileInfo, ZipFileReader, 11 BitFlag, CompressionMethod, EncryptionMethod, ZipError, ZipFileInfo, ZipFileReader,
@@ -91,7 +92,7 @@ impl<Io: Read + Seek> ArchiveRead for Zip<Io> {
91 92
92 // Read eocdr 93 // Read eocdr
93 io.seek(SeekFrom::Start(eocdr_pos + 4))?; 94 io.seek(SeekFrom::Start(eocdr_pos + 4))?;
94 let buf = io.read_arr::<18>()?; 95 let buf = io.read_arr::<{ EOCDR_SIZE - 4 }>()?;
95 let eocdr: Eocdr = deserialize(&buf).unwrap(); 96 let eocdr: Eocdr = deserialize(&buf).unwrap();
96 let comment = String::from_cp437(io.read_vec(eocdr.comment_len as usize)?); 97 let comment = String::from_cp437(io.read_vec(eocdr.comment_len as usize)?);
97 98
@@ -102,7 +103,7 @@ impl<Io: Read + Seek> ArchiveRead for Zip<Io> {
102 // Try to find eocdr64locator 103 // Try to find eocdr64locator
103 if eocdr_pos >= 20 { 104 if eocdr_pos >= 20 {
104 io.seek(SeekFrom::Start(eocdr_pos - 20))?; 105 io.seek(SeekFrom::Start(eocdr_pos - 20))?;
105 let buf = io.read_arr::<20>()?; 106 let buf = io.read_arr::<EOCDR64_LOCATOR_SIZE>()?;
106 107
107 if buf[..4] == EOCDR64_LOCATOR_SIGNATURE { 108 if buf[..4] == EOCDR64_LOCATOR_SIGNATURE {
108 let locator: Eocdr64Locator = deserialize(&buf[4..]).unwrap(); 109 let locator: Eocdr64Locator = deserialize(&buf[4..]).unwrap();
@@ -116,7 +117,8 @@ impl<Io: Read + Seek> ArchiveRead for Zip<Io> {
116 return Err(ZipError::Overlapping("Eocdr64", "Eocdr64Locator")); 117 return Err(ZipError::Overlapping("Eocdr64", "Eocdr64Locator"));
117 } 118 }
118 119
119 let eocdr64: Eocdr64 = deserialize(&io.read_arr::<54>()?).unwrap(); 120 let eocdr64: Eocdr64 =
121 deserialize(&io.read_arr::<{ EOCDR64_SIZE - 4 }>()?).unwrap();
120 if locator.eocdr64_pointer + eocdr64.eocdr64_size + 32 > eocdr_pos { 122 if locator.eocdr64_pointer + eocdr64.eocdr64_size + 32 > eocdr_pos {
121 return Err(ZipError::Overlapping("Eocdr64", "Eocdr64Locator")); 123 return Err(ZipError::Overlapping("Eocdr64", "Eocdr64Locator"));
122 } 124 }
@@ -148,7 +150,7 @@ impl<Io: Read + Seek> ArchiveRead for Zip<Io> {
148 if cd_reader.read_arr()? != CDR_SIGNATURE { 150 if cd_reader.read_arr()? != CDR_SIGNATURE {
149 return Err(ZipError::InvalidSignature("Cdr")); 151 return Err(ZipError::InvalidSignature("Cdr"));
150 } 152 }
151 let cdr: Cdr = deserialize(&cd_reader.read_arr::<42>()?).unwrap(); 153 let cdr: Cdr = deserialize(&cd_reader.read_arr::<{ CDR_SIZE - 4 }>()?).unwrap();
152 let bit_flag = BitFlag::new(cdr.bit_flag); 154 let bit_flag = BitFlag::new(cdr.bit_flag);
153 155
154 let name = cd_reader.read_vec(cdr.name_len as usize)?; 156 let name = cd_reader.read_vec(cdr.name_len as usize)?;
diff --git a/src/zip/file/read.rs b/src/zip/file/read.rs
index 567fb75..1ac0dc1 100644
--- a/src/zip/file/read.rs
+++ b/src/zip/file/read.rs
@@ -1,7 +1,7 @@
1use crate::driver::FileDriver; 1use crate::driver::FileDriver;
2use crate::utils::{IoCursor, ReadUtils}; 2use crate::utils::{IoCursor, ReadUtils};
3use crate::zip::encryption::{AesDecoder, Keys, WeakDecoder}; 3use crate::zip::encryption::{AesDecoder, Keys, WeakDecoder};
4use crate::zip::structs::FILE_HEADER_SIGNATURE; 4use crate::zip::structs::{FILE_HEADER_SIGNATURE, FILE_HEADER_SIZE};
5use crate::zip::{CompressionMethod, EncryptionMethod, ZipError, ZipFileInfo, ZipResult}; 5use crate::zip::{CompressionMethod, EncryptionMethod, ZipError, ZipFileInfo, ZipResult};
6use aes::cipher::KeyInit; 6use aes::cipher::KeyInit;
7use aes::{Aes128, Aes192, Aes256}; 7use aes::{Aes128, Aes192, Aes256};
@@ -235,7 +235,7 @@ impl<'d, Io: Read + Seek> ZipFileReader<'d, Io> {
235 ) -> ZipResult<Self> { 235 ) -> ZipResult<Self> {
236 io.seek(SeekFrom::Start(info.header_pointer))?; 236 io.seek(SeekFrom::Start(info.header_pointer))?;
237 237
238 let buf = io.read_arr::<30>()?; 238 let buf = io.read_arr::<FILE_HEADER_SIZE>()?;
239 if buf[..4] != FILE_HEADER_SIGNATURE { 239 if buf[..4] != FILE_HEADER_SIGNATURE {
240 return Err(ZipError::InvalidSignature("FileHeader")); 240 return Err(ZipError::InvalidSignature("FileHeader"));
241 } 241 }
diff --git a/src/zip/structs.rs b/src/zip/structs.rs
index 4b4524f..ccd5687 100644
--- a/src/zip/structs.rs
+++ b/src/zip/structs.rs
@@ -2,8 +2,10 @@ use crate::structs::{ByteOrder, Settings, StructResult, VariantIndexType};
2use serde::{Deserialize, Serialize}; 2use serde::{Deserialize, Serialize};
3 3
4pub const FILE_HEADER_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x03, 0x04]; 4pub const FILE_HEADER_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x03, 0x04];
5pub const FILE_HEADER_SIZE: usize = 30;
5 6
6pub const EOCDR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x05, 0x06]; 7pub const EOCDR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x05, 0x06];
8pub const EOCDR_SIZE: usize = 22;
7#[derive(Serialize, Deserialize)] 9#[derive(Serialize, Deserialize)]
8pub struct Eocdr { 10pub struct Eocdr {
9 pub eocdr_disk: u16, 11 pub eocdr_disk: u16,
@@ -16,6 +18,7 @@ pub struct Eocdr {
16} 18}
17 19
18pub const EOCDR64_LOCATOR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x06, 0x07]; 20pub const EOCDR64_LOCATOR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x06, 0x07];
21pub const EOCDR64_LOCATOR_SIZE: usize = 20;
19#[derive(Serialize, Deserialize)] 22#[derive(Serialize, Deserialize)]
20pub struct Eocdr64Locator { 23pub struct Eocdr64Locator {
21 pub eocdr64_disk: u32, 24 pub eocdr64_disk: u32,
@@ -24,6 +27,7 @@ pub struct Eocdr64Locator {
24} 27}
25 28
26pub const EOCDR64_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x06, 0x06]; 29pub const EOCDR64_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x06, 0x06];
30pub const EOCDR64_SIZE: usize = 56;
27#[derive(Serialize, Deserialize)] 31#[derive(Serialize, Deserialize)]
28pub struct Eocdr64 { 32pub struct Eocdr64 {
29 pub eocdr64_size: u64, 33 pub eocdr64_size: u64,
@@ -38,6 +42,7 @@ pub struct Eocdr64 {
38} 42}
39 43
40pub const CDR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x01, 0x02]; 44pub const CDR_SIGNATURE: [u8; 4] = [0x50, 0x4b, 0x01, 0x02];
45pub const CDR_SIZE: usize = 46;
41#[derive(Serialize, Deserialize)] 46#[derive(Serialize, Deserialize)]
42pub struct Cdr { 47pub struct Cdr {
43 pub version: u16, 48 pub version: u16,