diff options
Diffstat (limited to 'src/zip')
| -rw-r--r-- | src/zip/datatypes.rs | 278 | ||||
| -rw-r--r-- | src/zip/file.rs | 100 | ||||
| -rw-r--r-- | src/zip/io.rs | 127 |
3 files changed, 293 insertions, 212 deletions
diff --git a/src/zip/datatypes.rs b/src/zip/datatypes.rs index b280e5d..5ce1045 100644 --- a/src/zip/datatypes.rs +++ b/src/zip/datatypes.rs | |||
| @@ -1,9 +1,11 @@ | |||
| 1 | use crate::datatypes::{utils, ArchiveDatatype}; | 1 | pub use crate::datatypes::ArchiveDatatype; |
| 2 | use crate::result::{ArchiveError, ArchiveResult}; | 2 | use crate::result::{ArchiveError, ArchiveResult}; |
| 3 | use std::io::{Read, Write}; | 3 | use crate::utils::{archive_datatype, ReadHelper}; |
| 4 | use std::io::{Read, Seek, SeekFrom}; | ||
| 4 | 5 | ||
| 5 | utils::create_archive_datatype! { | 6 | archive_datatype! { |
| 6 | pub struct LocalFileHeader { | 7 | pub struct LocalFileHeader { |
| 8 | [const] SIGNATURE: u32 = 0x04034b50, | ||
| 7 | signature: u32, | 9 | signature: u32, |
| 8 | version_needed: u16, | 10 | version_needed: u16, |
| 9 | general_purpose_bit_flag: u16, | 11 | general_purpose_bit_flag: u16, |
| @@ -16,109 +18,28 @@ utils::create_archive_datatype! { | |||
| 16 | file_name_length: u16, | 18 | file_name_length: u16, |
| 17 | extra_field_length: u16, | 19 | extra_field_length: u16, |
| 18 | } | 20 | } |
| 19 | |||
| 20 | let file_name: String { | ||
| 21 | size: file_name_length, | ||
| 22 | read: utils::read_string, | ||
| 23 | write: utils::write_string, | ||
| 24 | } | ||
| 25 | |||
| 26 | let extra_field: Vec<u8> { | ||
| 27 | size: file_name_length, | ||
| 28 | read: utils::vec, | ||
| 29 | write: utils::vec, | ||
| 30 | } | ||
| 31 | |||
| 32 | const { | ||
| 33 | SIGNATURE: u32 = 0x04034b50; | ||
| 34 | } | ||
| 35 | |||
| 36 | read { | ||
| 37 | if signature != Self::SIGNATURE { | ||
| 38 | return Err(ArchiveError::WrongSignature { expected: Self::SIGNATURE, received: signature }) | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | 21 | } |
| 42 | 22 | ||
| 43 | pub struct DataDescriptor { | 23 | archive_datatype! { |
| 44 | pub signature: Option<u32>, | 24 | pub struct DataDescriptor { |
| 45 | pub crc32: u32, | 25 | [const] SIGNATURE: u32 = 0x08074b50, |
| 46 | pub compressed_size: u32, | 26 | crc32: u32, |
| 47 | pub uncompressed_size: u32, | 27 | compressed_size: u32, |
| 48 | } | 28 | uncompressed_size: u32, |
| 49 | |||
| 50 | impl DataDescriptor { | ||
| 51 | const SIGNATURE: u32 = 0x04034b50; | ||
| 52 | } | ||
| 53 | |||
| 54 | impl ArchiveDatatype for DataDescriptor { | ||
| 55 | const SIZE: usize = 12; | ||
| 56 | |||
| 57 | fn read(mut reader: impl Read) -> ArchiveResult<Self> { | ||
| 58 | let mut buf = [0; Self::SIZE]; | ||
| 59 | reader.read(&mut buf)?; | ||
| 60 | |||
| 61 | let signature = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]); | ||
| 62 | if signature == Self::SIGNATURE { | ||
| 63 | return Ok(Self { | ||
| 64 | signature: Some(signature), | ||
| 65 | crc32: u32::from_le_bytes([buf[4], buf[5], buf[6], buf[7]]), | ||
| 66 | compressed_size: u32::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]), | ||
| 67 | uncompressed_size: { | ||
| 68 | let mut buf = [0; 4]; | ||
| 69 | reader.read(&mut buf)?; | ||
| 70 | u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) | ||
| 71 | }, | ||
| 72 | }); | ||
| 73 | } else { | ||
| 74 | return Ok(Self { | ||
| 75 | signature: None, | ||
| 76 | crc32: signature, | ||
| 77 | compressed_size: u32::from_le_bytes([buf[4], buf[5], buf[6], buf[7]]), | ||
| 78 | uncompressed_size: u32::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]), | ||
| 79 | }); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | fn write(&self, mut writer: impl Write) -> ArchiveResult<()> { | ||
| 84 | writer.write( | ||
| 85 | &[ | ||
| 86 | Self::SIGNATURE.to_le_bytes(), | ||
| 87 | self.crc32.to_le_bytes(), | ||
| 88 | self.compressed_size.to_le_bytes(), | ||
| 89 | self.uncompressed_size.to_le_bytes(), | ||
| 90 | ] | ||
| 91 | .concat(), | ||
| 92 | )?; | ||
| 93 | Ok(()) | ||
| 94 | } | 29 | } |
| 95 | } | 30 | } |
| 96 | 31 | ||
| 97 | utils::create_archive_datatype! { | 32 | archive_datatype! { |
| 98 | pub struct ArchiveExtraDataRecord { | 33 | pub struct ArchiveExtraDataRecord { |
| 34 | [const] SIGNATURE: u32 = 0x08064b50, | ||
| 99 | signature: u32, | 35 | signature: u32, |
| 100 | extra_field_length: u32, | 36 | extra_field_length: u32, |
| 101 | } | 37 | } |
| 102 | |||
| 103 | let extra_field: Vec<u8> { | ||
| 104 | size: extra_field_length, | ||
| 105 | read: utils::vec, | ||
| 106 | write: utils::vec, | ||
| 107 | } | ||
| 108 | |||
| 109 | const { | ||
| 110 | SIGNATURE: u32 = 0x08064b50; | ||
| 111 | } | ||
| 112 | |||
| 113 | read { | ||
| 114 | if signature != Self::SIGNATURE { | ||
| 115 | return Err(ArchiveError::WrongSignature { expected: Self::SIGNATURE, received: signature }) | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | 38 | } |
| 119 | 39 | ||
| 120 | utils::create_archive_datatype! { | 40 | archive_datatype! { |
| 121 | pub struct CentralDirectoryRecord { | 41 | pub struct CentralDirectoryRecord { |
| 42 | [const] SIGNATURE: u32 = 0x02014b50, | ||
| 122 | signature: u32, | 43 | signature: u32, |
| 123 | version_made_by: u16, | 44 | version_made_by: u16, |
| 124 | version_needed: u16, | 45 | version_needed: u16, |
| @@ -135,136 +56,111 @@ utils::create_archive_datatype! { | |||
| 135 | disk_number: u16, | 56 | disk_number: u16, |
| 136 | internal_file_attributes: u16, | 57 | internal_file_attributes: u16, |
| 137 | external_file_attributes: u32, | 58 | external_file_attributes: u32, |
| 138 | relative_header_offset: u32, | 59 | header_offset: u32, |
| 139 | } | ||
| 140 | |||
| 141 | let file_name: String { | ||
| 142 | size: file_name_length, | ||
| 143 | read: utils::read_string, | ||
| 144 | write: utils::write_string, | ||
| 145 | } | ||
| 146 | |||
| 147 | let extra_field: Vec<u8> { | ||
| 148 | size: external_file_attributes, | ||
| 149 | read: utils::vec, | ||
| 150 | write: utils::vec, | ||
| 151 | } | ||
| 152 | |||
| 153 | let file_comment: String { | ||
| 154 | size: file_comment_length, | ||
| 155 | read: utils::read_string, | ||
| 156 | write: utils::write_string, | ||
| 157 | } | ||
| 158 | |||
| 159 | const { | ||
| 160 | SIGNATURE: u32 = 0x02014b50; | ||
| 161 | } | ||
| 162 | |||
| 163 | read { | ||
| 164 | if signature != Self::SIGNATURE { | ||
| 165 | return Err(ArchiveError::WrongSignature { expected: Self::SIGNATURE, received: signature }) | ||
| 166 | } | ||
| 167 | } | 60 | } |
| 168 | } | 61 | } |
| 169 | 62 | ||
| 170 | utils::create_archive_datatype! { | 63 | archive_datatype! { |
| 171 | pub struct DigitalSignature{ | 64 | pub struct DigitalSignature{ |
| 65 | [const] SIGNATURE: u32 = 0x05054b50, | ||
| 172 | signature: u32, | 66 | signature: u32, |
| 173 | data_size: u16, | 67 | data_size: u16, |
| 174 | } | 68 | } |
| 175 | |||
| 176 | let data: Vec<u8> { | ||
| 177 | size: data_size, | ||
| 178 | read: utils::vec, | ||
| 179 | write: utils::vec, | ||
| 180 | } | ||
| 181 | |||
| 182 | const { | ||
| 183 | SIGNATURE: u32 = 0x05054b50; | ||
| 184 | } | ||
| 185 | |||
| 186 | read { | ||
| 187 | if signature != Self::SIGNATURE { | ||
| 188 | return Err(ArchiveError::WrongSignature { expected: Self::SIGNATURE, received: signature }) | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | 69 | } |
| 192 | 70 | ||
| 193 | utils::create_archive_datatype! { | 71 | archive_datatype! { |
| 194 | pub struct Zip64EndOfCentralDirectoryRecord { | 72 | pub struct Zip64EndOfCentralDirectoryRecord { |
| 73 | [const] SIGNATURE: u32 = 0x06064b50, | ||
| 195 | signature: u32, | 74 | signature: u32, |
| 196 | size_of_struct: u64, | 75 | size_of_zip64_eocd: u64, |
| 197 | version_made_by: u16, | 76 | version_made_by: u16, |
| 198 | version_needed: u16, | 77 | version_needed: u16, |
| 199 | disk_number: u32, | 78 | disk: u32, |
| 200 | disk_number_where_starts_central_directory: u32, | 79 | disk_where_starts_cd: u32, |
| 201 | entries_in_central_directory_on_disk: u64, | 80 | entries_in_cd_on_disk: u64, |
| 202 | entries_in_central_directory: u64, | 81 | entries_in_cd: u64, |
| 203 | size_of_central_directory_records: u64, | 82 | size_of_cd_records: u64, |
| 204 | offset_of_central_directory_entries: u64, | 83 | offset_of_cd_entries: u64, |
| 205 | } | ||
| 206 | |||
| 207 | let extensible_data: Vec<u8> { | ||
| 208 | size: size_of_struct - Self::SIZE as u64 + 12, | ||
| 209 | read: utils::vec, | ||
| 210 | write: utils::vec, | ||
| 211 | } | 84 | } |
| 85 | } | ||
| 212 | 86 | ||
| 213 | const { | 87 | impl Zip64EndOfCentralDirectoryRecord { |
| 214 | SIGNATURE: u32 = 0x08064b50; | 88 | pub fn find( |
| 215 | } | 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 | } | ||
| 216 | 99 | ||
| 217 | read { | 100 | reader.seek(SeekFrom::Start(locator.offset_of_zip64_eocd))?; |
| 218 | if signature != Self::SIGNATURE { | 101 | let eocd64 = Self::read(&mut reader)?; |
| 219 | return Err(ArchiveError::WrongSignature { expected: Self::SIGNATURE, received: signature }) | 102 | if eocd64.signature != Self::SIGNATURE { |
| 103 | return Err(ArchiveError::IncorrectSignature { | ||
| 104 | expected: Self::SIGNATURE, | ||
| 105 | received: eocd64.signature, | ||
| 106 | }); | ||
| 220 | } | 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))) | ||
| 221 | } | 111 | } |
| 222 | } | 112 | } |
| 223 | 113 | ||
| 224 | utils::create_archive_datatype! { | 114 | archive_datatype! { |
| 225 | pub struct Zip64EndOfCentralDirectoryLocator { | 115 | pub struct Zip64EndOfCentralDirectoryLocator { |
| 116 | [const] SIGNATURE: u32 = 0x07064b50, | ||
| 226 | signature: u32, | 117 | signature: u32, |
| 227 | disk_number_where_starts_zip64_eocd: u32, | 118 | disk_where_starts_zip64_eocd: u32, |
| 228 | relative_offset_of_zip64_eocd: u64, | 119 | offset_of_zip64_eocd: u64, |
| 229 | total_disks_number: u32, | 120 | total_disks_number: u32, |
| 230 | } | 121 | } |
| 231 | |||
| 232 | const { | ||
| 233 | SIGNATURE: u32 = 0x07064b50; | ||
| 234 | } | ||
| 235 | |||
| 236 | read { | ||
| 237 | if signature != Self::SIGNATURE { | ||
| 238 | return Err(ArchiveError::WrongSignature { expected: Self::SIGNATURE, received: signature }) | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | 122 | } |
| 242 | 123 | ||
| 243 | utils::create_archive_datatype! { | 124 | archive_datatype! { |
| 244 | pub struct EndOfCentralDirectoryRecord { | 125 | pub struct EndOfCentralDirectoryRecord { |
| 126 | [const] SIGNATURE: u32 = 0x06054b50, | ||
| 245 | signature: u32, | 127 | signature: u32, |
| 246 | disk_number: u16, | 128 | disk: u16, |
| 247 | disk_number_where_starts_central_directory: u16, | 129 | disk_where_starts_cd: u16, |
| 248 | entries_in_central_directory_on_disk: u16, | 130 | entries_in_cd_on_disk: u16, |
| 249 | entries_in_central_directory: u16, | 131 | entries_in_cd: u16, |
| 250 | size_of_central_directory_records: u32, | 132 | size_of_cd_records: u32, |
| 251 | offset_of_central_directory_entries: u32, | 133 | offset_of_cd_entries: u32, |
| 252 | comment_length: u16, | 134 | comment_length: u16, |
| 253 | } | 135 | } |
| 136 | } | ||
| 254 | 137 | ||
| 255 | let comment: String { | 138 | impl EndOfCentralDirectoryRecord { |
| 256 | size: comment_length, | 139 | pub fn find(mut reader: impl Read + Seek) -> ArchiveResult<(u64, Self, String)> { |
| 257 | read: utils::read_string, | 140 | let file_size = reader.seek(SeekFrom::End(0))? as usize; |
| 258 | write: utils::write_string, | 141 | let limit: usize = (u16::MAX as usize + Self::SIZE).min(file_size); |
| 259 | } | 142 | let mut buf = vec![0; limit]; |
| 260 | 143 | ||
| 261 | const { | 144 | reader.seek(SeekFrom::End(-(limit as i64)))?; |
| 262 | SIGNATURE: u32 = 0x06054b50; | 145 | reader.read(&mut buf)?; |
| 263 | } | 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()); | ||
| 264 | 150 | ||
| 265 | read { | 151 | let comment = String::from_utf8( |
| 266 | if signature != Self::SIGNATURE { | 152 | buf[byte + Self::SIZE..byte + Self::SIZE + eocd.comment_length as usize].into(), |
| 267 | return Err(ArchiveError::WrongSignature { expected: Self::SIGNATURE, received: signature }) | 153 | ) |
| 154 | .map_err(|_| ArchiveError::IncorrectString { | ||
| 155 | location: "archive_comment", | ||
| 156 | })?; | ||
| 157 | |||
| 158 | return Ok(((file_size - limit + byte) as u64, eocd, comment)); | ||
| 159 | } | ||
| 268 | } | 160 | } |
| 161 | |||
| 162 | Err(ArchiveError::BadArchive { | ||
| 163 | reason: "end of central directory record not found", | ||
| 164 | }) | ||
| 269 | } | 165 | } |
| 270 | } | 166 | } |
diff --git a/src/zip/file.rs b/src/zip/file.rs index dba8d06..261390f 100644 --- a/src/zip/file.rs +++ b/src/zip/file.rs | |||
| @@ -1,44 +1,124 @@ | |||
| 1 | use crate::file::{ArchiveFile, ArchiveFileRead, ArchiveFileWrite}; | 1 | use crate::file::{ArchiveFile, ArchiveFileRead, ArchiveFileWrite}; |
| 2 | use crate::result::{ArchiveError, ArchiveResult}; | ||
| 3 | use chrono::NaiveDateTime; | ||
| 2 | use std::io::{Read, Write}; | 4 | use std::io::{Read, Write}; |
| 3 | 5 | ||
| 4 | pub struct FileInfo {} | 6 | pub struct GeneralPurposeBitFlag {} |
| 5 | 7 | ||
| 6 | pub struct FileReader {} | 8 | pub enum CompressionMethod { |
| 9 | Store, | ||
| 10 | Deflate, | ||
| 11 | Bzip2, | ||
| 12 | LZMA, | ||
| 13 | Zstandard, | ||
| 14 | } | ||
| 15 | |||
| 16 | impl 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 | } | ||
| 7 | 30 | ||
| 8 | pub struct FileWriter {} | 31 | pub 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 | |||
| 46 | impl 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 | |||
| 78 | pub struct FileReader { | ||
| 79 | info: FileInfo, | ||
| 80 | } | ||
| 9 | 81 | ||
| 10 | impl ArchiveFile for FileReader { | 82 | impl ArchiveFile for FileReader { |
| 11 | type Info = FileInfo; | 83 | type Info = FileInfo; |
| 12 | 84 | ||
| 13 | fn info() -> Self::Info { | 85 | fn new(info: Self::Info) -> Self { |
| 14 | Self::Info {} | 86 | Self { info } |
| 15 | } | 87 | } |
| 16 | } | 88 | } |
| 17 | 89 | ||
| 18 | impl Read for FileReader { | 90 | impl Read for FileReader { |
| 19 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | 91 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 20 | return Ok(0); | 92 | todo!() |
| 21 | } | 93 | } |
| 22 | } | 94 | } |
| 23 | 95 | ||
| 24 | impl ArchiveFileRead for FileReader {} | 96 | impl ArchiveFileRead for FileReader {} |
| 25 | 97 | ||
| 98 | impl FileReader {} | ||
| 99 | |||
| 100 | pub struct FileWriter { | ||
| 101 | info: FileInfo, | ||
| 102 | } | ||
| 103 | |||
| 26 | impl ArchiveFile for FileWriter { | 104 | impl ArchiveFile for FileWriter { |
| 27 | type Info = FileInfo; | 105 | type Info = FileInfo; |
| 28 | 106 | ||
| 29 | fn info() -> Self::Info { | 107 | fn new(info: Self::Info) -> Self { |
| 30 | Self::Info {} | 108 | Self { info } |
| 31 | } | 109 | } |
| 32 | } | 110 | } |
| 33 | 111 | ||
| 34 | impl Write for FileWriter { | 112 | impl Write for FileWriter { |
| 35 | fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | 113 | fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 36 | return Ok(0); | 114 | todo!() |
| 37 | } | 115 | } |
| 38 | 116 | ||
| 39 | fn flush(&mut self) -> std::io::Result<()> { | 117 | fn flush(&mut self) -> std::io::Result<()> { |
| 40 | Ok(()) | 118 | todo!() |
| 41 | } | 119 | } |
| 42 | } | 120 | } |
| 43 | 121 | ||
| 44 | impl ArchiveFileWrite for FileWriter {} | 122 | impl ArchiveFileWrite for FileWriter {} |
| 123 | |||
| 124 | impl FileWriter {} | ||
diff --git a/src/zip/io.rs b/src/zip/io.rs index b79ad0d..c41607f 100644 --- a/src/zip/io.rs +++ b/src/zip/io.rs | |||
| @@ -1,12 +1,18 @@ | |||
| 1 | use super::datatypes::*; | ||
| 1 | use super::file::{FileInfo, FileReader, FileWriter}; | 2 | use super::file::{FileInfo, FileReader, FileWriter}; |
| 2 | use crate::io::{ArchiveRead, ArchiveWrite}; | 3 | use crate::io::{ArchiveRead, ArchiveWrite}; |
| 3 | use crate::result::ArchiveResult; | 4 | use crate::result::{ArchiveError, ArchiveResult}; |
| 5 | use crate::utils::ReadHelper; | ||
| 6 | use chrono::{NaiveDate, NaiveDateTime, NaiveTime}; | ||
| 7 | use std::collections::HashMap; | ||
| 4 | use std::fs::File; | 8 | use std::fs::File; |
| 5 | use std::io::Read; | 9 | use std::io::Read; |
| 6 | use std::io::{Seek, Write}; | 10 | use std::io::{Seek, SeekFrom, Write}; |
| 7 | 11 | ||
| 8 | pub struct Reader<R: Read + Seek = File> { | 12 | pub struct Reader<R: Read + Seek = File> { |
| 9 | reader: R, | 13 | reader: R, |
| 14 | files: HashMap<String, FileInfo>, | ||
| 15 | comment: String, | ||
| 10 | } | 16 | } |
| 11 | 17 | ||
| 12 | impl<R: Read + Seek> ArchiveRead for Reader<R> { | 18 | impl<R: Read + Seek> ArchiveRead for Reader<R> { |
| @@ -14,16 +20,115 @@ impl<R: Read + Seek> ArchiveRead for Reader<R> { | |||
| 14 | type FileInfo = FileInfo; | 20 | type FileInfo = FileInfo; |
| 15 | type FileReader = FileReader; | 21 | type FileReader = FileReader; |
| 16 | 22 | ||
| 17 | fn new(reader: Self::Reader) -> ArchiveResult<Self> { | 23 | fn new(mut reader: Self::Reader) -> ArchiveResult<Self> { |
| 18 | Ok(Self { reader }) | 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 | }) | ||
| 19 | } | 124 | } |
| 20 | 125 | ||
| 21 | fn files(&self) -> ArchiveResult<Vec<Self::FileInfo>> { | 126 | fn files(&self) -> ArchiveResult<Vec<Self::FileInfo>> { |
| 22 | Ok(Vec::new()) | 127 | todo!() |
| 23 | } | 128 | } |
| 24 | 129 | ||
| 25 | fn file_reader(&self, name: &str) -> ArchiveResult<Self::FileReader> { | 130 | fn open_file(&self, name: &str) -> ArchiveResult<Self::FileReader> { |
| 26 | Ok(Self::FileReader {}) | 131 | todo!() |
| 27 | } | 132 | } |
| 28 | } | 133 | } |
| 29 | 134 | ||
| @@ -33,17 +138,17 @@ pub struct Writer<W: Write = File> { | |||
| 33 | writer: W, | 138 | writer: W, |
| 34 | } | 139 | } |
| 35 | 140 | ||
| 36 | impl<W: Write> ArchiveWrite for Writer<W> { | 141 | impl<W: Write + Seek> ArchiveWrite for Writer<W> { |
| 37 | type Writer = W; | 142 | type Writer = W; |
| 38 | type FileInfo = FileInfo; | 143 | type FileInfo = FileInfo; |
| 39 | type FileWriter = FileWriter; | 144 | type FileWriter = FileWriter; |
| 40 | 145 | ||
| 41 | fn new(writer: Self::Writer) -> ArchiveResult<Self> { | 146 | fn new(writer: Self::Writer) -> ArchiveResult<Self> { |
| 42 | Ok(Self { writer }) | 147 | todo!() |
| 43 | } | 148 | } |
| 44 | 149 | ||
| 45 | fn file_writer(&self, name: &str) -> ArchiveResult<Self::FileWriter> { | 150 | fn create_file(&self, name: &str) -> ArchiveResult<Self::FileWriter> { |
| 46 | Ok(Self::FileWriter {}) | 151 | todo!() |
| 47 | } | 152 | } |
| 48 | } | 153 | } |
| 49 | 154 | ||
