aboutsummaryrefslogtreecommitdiff
path: root/src/zip
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-06-10 23:38:08 +0900
committerIgor Tolmachev <me@igorek.dev>2024-06-23 15:34:34 +0900
commitbd77f62e99a5300dfa52aef3a7040414b28ebfd6 (patch)
tree4965b7fc0392264f9a8c44c00ed254019747b4b5 /src/zip
parent9003b81813ff171edfc6101868c226c5c7d1957c (diff)
downloadarchivator-bd77f62e99a5300dfa52aef3a7040414b28ebfd6.tar.gz
archivator-bd77f62e99a5300dfa52aef3a7040414b28ebfd6.zip
Reset branch
Diffstat (limited to 'src/zip')
-rw-r--r--src/zip/archive.rs0
-rw-r--r--src/zip/datatypes.rs166
-rw-r--r--src/zip/file.rs124
-rw-r--r--src/zip/io.rs155
-rw-r--r--src/zip/mod.rs6
5 files changed, 0 insertions, 451 deletions
diff --git a/src/zip/archive.rs b/src/zip/archive.rs
deleted file mode 100644
index e69de29..0000000
--- a/src/zip/archive.rs
+++ /dev/null
diff --git a/src/zip/datatypes.rs b/src/zip/datatypes.rs
deleted file mode 100644
index 5ce1045..0000000
--- a/src/zip/datatypes.rs
+++ /dev/null
@@ -1,166 +0,0 @@
1pub use crate::datatypes::ArchiveDatatype;
2use crate::result::{ArchiveError, ArchiveResult};
3use crate::utils::{archive_datatype, ReadHelper};
4use std::io::{Read, Seek, SeekFrom};
5
6archive_datatype! {
7 pub struct LocalFileHeader {
8 [const] SIGNATURE: u32 = 0x04034b50,
9 signature: u32,
10 version_needed: u16,
11 general_purpose_bit_flag: u16,
12 compression_method: u16,
13 last_mod_file_time: u16,
14 last_mod_file_date: u16,
15 crc32: u32,
16 compressed_size: u32,
17 uncompressed_size: u32,
18 file_name_length: u16,
19 extra_field_length: u16,
20 }
21}
22
23archive_datatype! {
24 pub struct DataDescriptor {
25 [const] SIGNATURE: u32 = 0x08074b50,
26 crc32: u32,
27 compressed_size: u32,
28 uncompressed_size: u32,
29 }
30}
31
32archive_datatype! {
33 pub struct ArchiveExtraDataRecord {
34 [const] SIGNATURE: u32 = 0x08064b50,
35 signature: u32,
36 extra_field_length: u32,
37 }
38}
39
40archive_datatype! {
41 pub struct CentralDirectoryRecord {
42 [const] SIGNATURE: u32 = 0x02014b50,
43 signature: u32,
44 version_made_by: u16,
45 version_needed: u16,
46 general_purpose_bit_flag: u16,
47 compression_method: u16,
48 last_mod_file_time: u16,
49 last_mod_file_date: u16,
50 crc32: u32,
51 compressed_size: u32,
52 uncompressed_size: u32,
53 file_name_length: u16,
54 extra_field_length: u16,
55 file_comment_length: u16,
56 disk_number: u16,
57 internal_file_attributes: u16,
58 external_file_attributes: u32,
59 header_offset: u32,
60 }
61}
62
63archive_datatype! {
64 pub struct DigitalSignature{
65 [const] SIGNATURE: u32 = 0x05054b50,
66 signature: u32,
67 data_size: u16,
68 }
69}
70
71archive_datatype! {
72 pub struct Zip64EndOfCentralDirectoryRecord {
73 [const] SIGNATURE: u32 = 0x06064b50,
74 signature: u32,
75 size_of_zip64_eocd: u64,
76 version_made_by: u16,
77 version_needed: u16,
78 disk: u32,
79 disk_where_starts_cd: u32,
80 entries_in_cd_on_disk: u64,
81 entries_in_cd: u64,
82 size_of_cd_records: u64,
83 offset_of_cd_entries: u64,
84 }
85}
86
87impl Zip64EndOfCentralDirectoryRecord {
88 pub fn find(
89 mut reader: impl Read + Seek,
90 eocd_offset: u64,
91 ) -> ArchiveResult<Option<(u64, Self, Vec<u8>)>> {
92 let locator_offset = eocd_offset - Zip64EndOfCentralDirectoryLocator::SIZE as u64;
93 reader.seek(SeekFrom::Start(locator_offset))?;
94
95 let locator = Zip64EndOfCentralDirectoryLocator::read(&mut reader)?;
96 if locator.signature != Zip64EndOfCentralDirectoryLocator::SIGNATURE {
97 return Ok(None);
98 }
99
100 reader.seek(SeekFrom::Start(locator.offset_of_zip64_eocd))?;
101 let eocd64 = Self::read(&mut reader)?;
102 if eocd64.signature != Self::SIGNATURE {
103 return Err(ArchiveError::IncorrectSignature {
104 expected: Self::SIGNATURE,
105 received: eocd64.signature,
106 });
107 }
108
109 let ext_data = reader.read2vec(eocd64.size_of_zip64_eocd as usize + 12 - Self::SIZE)?;
110 Ok(Some((locator.offset_of_zip64_eocd, eocd64, ext_data)))
111 }
112}
113
114archive_datatype! {
115 pub struct Zip64EndOfCentralDirectoryLocator {
116 [const] SIGNATURE: u32 = 0x07064b50,
117 signature: u32,
118 disk_where_starts_zip64_eocd: u32,
119 offset_of_zip64_eocd: u64,
120 total_disks_number: u32,
121 }
122}
123
124archive_datatype! {
125 pub struct EndOfCentralDirectoryRecord {
126 [const] SIGNATURE: u32 = 0x06054b50,
127 signature: u32,
128 disk: u16,
129 disk_where_starts_cd: u16,
130 entries_in_cd_on_disk: u16,
131 entries_in_cd: u16,
132 size_of_cd_records: u32,
133 offset_of_cd_entries: u32,
134 comment_length: u16,
135 }
136}
137
138impl EndOfCentralDirectoryRecord {
139 pub fn find(mut reader: impl Read + Seek) -> ArchiveResult<(u64, Self, String)> {
140 let file_size = reader.seek(SeekFrom::End(0))? as usize;
141 let limit: usize = (u16::MAX as usize + Self::SIZE).min(file_size);
142 let mut buf = vec![0; limit];
143
144 reader.seek(SeekFrom::End(-(limit as i64)))?;
145 reader.read(&mut buf)?;
146
147 for byte in 0..limit - 4 {
148 if u32::from_le_bytes(buf[byte..byte + 4].try_into().unwrap()) == Self::SIGNATURE {
149 let eocd = Self::parse(buf[byte..byte + Self::SIZE].try_into().unwrap());
150
151 let comment = String::from_utf8(
152 buf[byte + Self::SIZE..byte + Self::SIZE + eocd.comment_length as usize].into(),
153 )
154 .map_err(|_| ArchiveError::IncorrectString {
155 location: "archive_comment",
156 })?;
157
158 return Ok(((file_size - limit + byte) as u64, eocd, comment));
159 }
160 }
161
162 Err(ArchiveError::BadArchive {
163 reason: "end of central directory record not found",
164 })
165 }
166}
diff --git a/src/zip/file.rs b/src/zip/file.rs
deleted file mode 100644
index 261390f..0000000
--- a/src/zip/file.rs
+++ /dev/null
@@ -1,124 +0,0 @@
1use crate::file::{ArchiveFile, ArchiveFileRead, ArchiveFileWrite};
2use crate::result::{ArchiveError, ArchiveResult};
3use chrono::NaiveDateTime;
4use std::io::{Read, Write};
5
6pub struct GeneralPurposeBitFlag {}
7
8pub enum CompressionMethod {
9 Store,
10 Deflate,
11 Bzip2,
12 LZMA,
13 Zstandard,
14}
15
16impl TryFrom<u16> for CompressionMethod {
17 type Error = ArchiveError;
18
19 fn try_from(value: u16) -> ArchiveResult<Self> {
20 Ok(match value {
21 0 => Self::Store,
22 8 => Self::Deflate,
23 12 => Self::Bzip2,
24 14 => Self::LZMA,
25 93 | 20 => Self::Zstandard,
26 _ => return Err(ArchiveError::UnsupportedCompressionMethod { method: value }),
27 })
28 }
29}
30
31pub struct FileInfo {
32 number: u64,
33 version_made_by: u16,
34 version_needed: u16,
35 general_purpose_bit_flag: GeneralPurposeBitFlag,
36 compression_method: CompressionMethod,
37 file_modified_at: NaiveDateTime,
38 crc32: u32,
39 compressed_size: u64,
40 uncompressed_size: u64,
41 name: String,
42 comment: String,
43 header_offset: u64,
44}
45
46impl FileInfo {
47 pub fn new(
48 number: u64,
49 version_made_by: u16,
50 version_needed: u16,
51 general_purpose_bit_flag: GeneralPurposeBitFlag,
52 compression_method: CompressionMethod,
53 file_modified_at: NaiveDateTime,
54 crc32: u32,
55 compressed_size: u64,
56 uncompressed_size: u64,
57 name: String,
58 comment: String,
59 header_offset: u64,
60 ) -> Self {
61 Self {
62 number,
63 version_made_by,
64 version_needed,
65 general_purpose_bit_flag,
66 compression_method,
67 file_modified_at,
68 crc32,
69 compressed_size,
70 uncompressed_size,
71 name,
72 comment,
73 header_offset,
74 }
75 }
76}
77
78pub struct FileReader {
79 info: FileInfo,
80}
81
82impl ArchiveFile for FileReader {
83 type Info = FileInfo;
84
85 fn new(info: Self::Info) -> Self {
86 Self { info }
87 }
88}
89
90impl Read for FileReader {
91 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
92 todo!()
93 }
94}
95
96impl ArchiveFileRead for FileReader {}
97
98impl FileReader {}
99
100pub struct FileWriter {
101 info: FileInfo,
102}
103
104impl ArchiveFile for FileWriter {
105 type Info = FileInfo;
106
107 fn new(info: Self::Info) -> Self {
108 Self { info }
109 }
110}
111
112impl Write for FileWriter {
113 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
114 todo!()
115 }
116
117 fn flush(&mut self) -> std::io::Result<()> {
118 todo!()
119 }
120}
121
122impl ArchiveFileWrite for FileWriter {}
123
124impl FileWriter {}
diff --git a/src/zip/io.rs b/src/zip/io.rs
deleted file mode 100644
index c41607f..0000000
--- a/src/zip/io.rs
+++ /dev/null
@@ -1,155 +0,0 @@
1use super::datatypes::*;
2use super::file::{FileInfo, FileReader, FileWriter};
3use crate::io::{ArchiveRead, ArchiveWrite};
4use crate::result::{ArchiveError, ArchiveResult};
5use crate::utils::ReadHelper;
6use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
7use std::collections::HashMap;
8use std::fs::File;
9use std::io::Read;
10use std::io::{Seek, SeekFrom, Write};
11
12pub struct Reader<R: Read + Seek = File> {
13 reader: R,
14 files: HashMap<String, FileInfo>,
15 comment: String,
16}
17
18impl<R: Read + Seek> ArchiveRead for Reader<R> {
19 type Reader = R;
20 type FileInfo = FileInfo;
21 type FileReader = FileReader;
22
23 fn new(mut reader: Self::Reader) -> ArchiveResult<Self> {
24 let cd_size: u64;
25 let cd_offset: u64;
26 let cd_entries: u64;
27
28 let (eocd_offset, eocd, comment) = EndOfCentralDirectoryRecord::find(&mut reader)?;
29 if let Some((eocd64_offset, eocd64, eocd64_extensible)) =
30 Zip64EndOfCentralDirectoryRecord::find(&mut reader, eocd_offset)?
31 {
32 cd_size = eocd64.size_of_cd_records;
33 cd_offset = eocd64.offset_of_cd_entries;
34 cd_entries = eocd64.entries_in_cd;
35 } else {
36 cd_size = eocd.size_of_cd_records as u64;
37 cd_offset = eocd.offset_of_cd_entries as u64;
38 cd_entries = eocd.entries_in_cd as u64;
39 }
40
41 let mut buf = vec![0; cd_size as usize];
42 reader.seek(SeekFrom::Start(cd_offset))?;
43 reader.read(&mut buf)?;
44
45 let mut buf: &[u8] = &buf;
46 let mut files = HashMap::with_capacity(cd_entries as usize);
47
48 for entry_number in 0..cd_entries {
49 let cd = CentralDirectoryRecord::read(&mut buf)?;
50
51 let file_name = String::from_utf8(buf.read2vec(cd.file_name_length as usize)?).or(
52 Err(ArchiveError::IncorrectString {
53 location: "file_name",
54 }),
55 )?;
56 let mut extra_field: &[u8] = &buf.read2vec(cd.extra_field_length as usize)?;
57 let file_comment = String::from_utf8(buf.read2vec(cd.file_comment_length as usize)?)
58 .or(Err(ArchiveError::IncorrectString {
59 location: "file_comment",
60 }))?;
61
62 let mut uncompressed_size: u64 = cd.uncompressed_size as u64;
63 let mut compressed_size: u64 = cd.compressed_size as u64;
64 let mut header_offset: u64 = cd.header_offset as u64;
65
66 while extra_field.len() > 0 {
67 let header = u16::from_le_bytes(extra_field.read2buf()?);
68 let size = u16::from_le_bytes(extra_field.read2buf()?);
69 let mut data: &[u8] = &extra_field.read2vec(size as usize)?;
70
71 match header {
72 0x0001 => {
73 if uncompressed_size == 0xFFFFFFFF {
74 uncompressed_size = u64::from_le_bytes(data.read2buf()?);
75 }
76 if compressed_size == 0xFFFFFFFF {
77 compressed_size = u64::from_le_bytes(data.read2buf()?);
78 }
79 if header_offset == 0xFFFFFFFF {
80 header_offset = u64::from_le_bytes(data.read2buf()?);
81 }
82 }
83 _ => {}
84 };
85 }
86
87 let year = ((cd.last_mod_file_date >> 9) & 0x7F) + 1980;
88 let month = (cd.last_mod_file_date >> 5) & 0xF;
89 let day = cd.last_mod_file_date & 0x1F;
90
91 let hour = (cd.last_mod_file_time >> 11) & 0x1F;
92 let min = (cd.last_mod_file_time >> 5) & 0x3F;
93 let sec = (cd.last_mod_file_time & 0x1F) * 2;
94
95 files.insert(
96 file_name.clone(),
97 FileInfo::new(
98 entry_number,
99 cd.version_made_by,
100 cd.version_needed,
101 super::file::GeneralPurposeBitFlag {},
102 cd.compression_method.try_into()?,
103 NaiveDateTime::new(
104 NaiveDate::from_ymd_opt(year as i32, month as u32, day as u32)
105 .ok_or(ArchiveError::IncorrectDate { year, month, day })?,
106 NaiveTime::from_hms_opt(hour as u32, min as u32, sec as u32)
107 .ok_or(ArchiveError::IncorrectTime { hour, min, sec })?,
108 ),
109 cd.crc32,
110 compressed_size,
111 uncompressed_size,
112 file_name,
113 file_comment,
114 header_offset,
115 ),
116 );
117 }
118
119 Ok(Self {
120 reader,
121 files,
122 comment,
123 })
124 }
125
126 fn files(&self) -> ArchiveResult<Vec<Self::FileInfo>> {
127 todo!()
128 }
129
130 fn open_file(&self, name: &str) -> ArchiveResult<Self::FileReader> {
131 todo!()
132 }
133}
134
135impl<R: Read + Seek> Reader<R> {}
136
137pub struct Writer<W: Write = File> {
138 writer: W,
139}
140
141impl<W: Write + Seek> ArchiveWrite for Writer<W> {
142 type Writer = W;
143 type FileInfo = FileInfo;
144 type FileWriter = FileWriter;
145
146 fn new(writer: Self::Writer) -> ArchiveResult<Self> {
147 todo!()
148 }
149
150 fn create_file(&self, name: &str) -> ArchiveResult<Self::FileWriter> {
151 todo!()
152 }
153}
154
155impl<W: Write> Writer<W> {}
diff --git a/src/zip/mod.rs b/src/zip/mod.rs
deleted file mode 100644
index b668b6b..0000000
--- a/src/zip/mod.rs
+++ /dev/null
@@ -1,6 +0,0 @@
1mod archive;
2mod datatypes;
3mod file;
4mod io;
5
6pub use io::{Reader, Writer};