| 1 | // Copyright (c) 2021 Harry [Majored] [[email protected]] |
| 2 | // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) |
| 3 | |
| 4 | use crate::error::{Result, ZipError}; |
| 5 | use crate::spec::header::{ |
| 6 | CentralDirectoryRecord, EndOfCentralDirectoryHeader, ExtraField, GeneralPurposeFlag, HeaderId, LocalFileHeader, |
| 7 | Zip64EndOfCentralDirectoryLocator, Zip64EndOfCentralDirectoryRecord, |
| 8 | }; |
| 9 | |
| 10 | use futures_lite::io::{AsyncRead, AsyncReadExt}; |
| 11 | |
| 12 | impl LocalFileHeader { |
| 13 | pub fn as_slice(&self) -> [u8; 26] { |
| 14 | let mut array = [0; 26]; |
| 15 | let mut cursor = 0; |
| 16 | |
| 17 | array_push!(array, cursor, self.version.to_le_bytes()); |
| 18 | array_push!(array, cursor, self.flags.as_slice()); |
| 19 | array_push!(array, cursor, self.compression.to_le_bytes()); |
| 20 | array_push!(array, cursor, self.mod_time.to_le_bytes()); |
| 21 | array_push!(array, cursor, self.mod_date.to_le_bytes()); |
| 22 | array_push!(array, cursor, self.crc.to_le_bytes()); |
| 23 | array_push!(array, cursor, self.compressed_size.to_le_bytes()); |
| 24 | array_push!(array, cursor, self.uncompressed_size.to_le_bytes()); |
| 25 | array_push!(array, cursor, self.file_name_length.to_le_bytes()); |
| 26 | array_push!(array, cursor, self.extra_field_length.to_le_bytes()); |
| 27 | |
| 28 | array |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl GeneralPurposeFlag { |
| 33 | pub fn as_slice(&self) -> [u8; 2] { |
| 34 | let encrypted: u16 = match self.encrypted { |
| 35 | false => 0x0, |
| 36 | true => 0b1, |
| 37 | }; |
| 38 | let data_descriptor: u16 = match self.data_descriptor { |
| 39 | false => 0x0, |
| 40 | true => 0x8, |
| 41 | }; |
| 42 | let filename_unicode: u16 = match self.filename_unicode { |
| 43 | false => 0x0, |
| 44 | true => 0x800, |
| 45 | }; |
| 46 | |
| 47 | (encrypted | data_descriptor | filename_unicode).to_le_bytes() |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl CentralDirectoryRecord { |
| 52 | pub fn as_slice(&self) -> [u8; 42] { |
| 53 | let mut array = [0; 42]; |
| 54 | let mut cursor = 0; |
| 55 | |
| 56 | array_push!(array, cursor, self.v_made_by.to_le_bytes()); |
| 57 | array_push!(array, cursor, self.v_needed.to_le_bytes()); |
| 58 | array_push!(array, cursor, self.flags.as_slice()); |
| 59 | array_push!(array, cursor, self.compression.to_le_bytes()); |
| 60 | array_push!(array, cursor, self.mod_time.to_le_bytes()); |
| 61 | array_push!(array, cursor, self.mod_date.to_le_bytes()); |
| 62 | array_push!(array, cursor, self.crc.to_le_bytes()); |
| 63 | array_push!(array, cursor, self.compressed_size.to_le_bytes()); |
| 64 | array_push!(array, cursor, self.uncompressed_size.to_le_bytes()); |
| 65 | array_push!(array, cursor, self.file_name_length.to_le_bytes()); |
| 66 | array_push!(array, cursor, self.extra_field_length.to_le_bytes()); |
| 67 | array_push!(array, cursor, self.file_comment_length.to_le_bytes()); |
| 68 | array_push!(array, cursor, self.disk_start.to_le_bytes()); |
| 69 | array_push!(array, cursor, self.inter_attr.to_le_bytes()); |
| 70 | array_push!(array, cursor, self.exter_attr.to_le_bytes()); |
| 71 | array_push!(array, cursor, self.lh_offset.to_le_bytes()); |
| 72 | |
| 73 | array |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl EndOfCentralDirectoryHeader { |
| 78 | pub fn as_slice(&self) -> [u8; 18] { |
| 79 | let mut array = [0; 18]; |
| 80 | let mut cursor = 0; |
| 81 | |
| 82 | array_push!(array, cursor, self.disk_num.to_le_bytes()); |
| 83 | array_push!(array, cursor, self.start_cent_dir_disk.to_le_bytes()); |
| 84 | array_push!(array, cursor, self.num_of_entries_disk.to_le_bytes()); |
| 85 | array_push!(array, cursor, self.num_of_entries.to_le_bytes()); |
| 86 | array_push!(array, cursor, self.size_cent_dir.to_le_bytes()); |
| 87 | array_push!(array, cursor, self.cent_dir_offset.to_le_bytes()); |
| 88 | array_push!(array, cursor, self.file_comm_length.to_le_bytes()); |
| 89 | |
| 90 | array |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl From<[u8; 26]> for LocalFileHeader { |
| 95 | fn from(value: [u8; 26]) -> LocalFileHeader { |
| 96 | LocalFileHeader { |
| 97 | version: u16::from_le_bytes(value[0..2].try_into().unwrap()), |
| 98 | flags: GeneralPurposeFlag::from(u16::from_le_bytes(value[2..4].try_into().unwrap())), |
| 99 | compression: u16::from_le_bytes(value[4..6].try_into().unwrap()), |
| 100 | mod_time: u16::from_le_bytes(value[6..8].try_into().unwrap()), |
| 101 | mod_date: u16::from_le_bytes(value[8..10].try_into().unwrap()), |
| 102 | crc: u32::from_le_bytes(value[10..14].try_into().unwrap()), |
| 103 | compressed_size: u32::from_le_bytes(value[14..18].try_into().unwrap()), |
| 104 | uncompressed_size: u32::from_le_bytes(value[18..22].try_into().unwrap()), |
| 105 | file_name_length: u16::from_le_bytes(value[22..24].try_into().unwrap()), |
| 106 | extra_field_length: u16::from_le_bytes(value[24..26].try_into().unwrap()), |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl From<u16> for GeneralPurposeFlag { |
| 112 | fn from(value: u16) -> GeneralPurposeFlag { |
| 113 | let encrypted = !matches!(value & 0x1, 0); |
| 114 | let data_descriptor = !matches!((value & 0x8) >> 3, 0); |
| 115 | let filename_unicode = !matches!((value & 0x800) >> 11, 0); |
| 116 | |
| 117 | GeneralPurposeFlag { encrypted, data_descriptor, filename_unicode } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl From<[u8; 42]> for CentralDirectoryRecord { |
| 122 | fn from(value: [u8; 42]) -> CentralDirectoryRecord { |
| 123 | CentralDirectoryRecord { |
| 124 | v_made_by: u16::from_le_bytes(value[0..2].try_into().unwrap()), |
| 125 | v_needed: u16::from_le_bytes(value[2..4].try_into().unwrap()), |
| 126 | flags: GeneralPurposeFlag::from(u16::from_le_bytes(value[4..6].try_into().unwrap())), |
| 127 | compression: u16::from_le_bytes(value[6..8].try_into().unwrap()), |
| 128 | mod_time: u16::from_le_bytes(value[8..10].try_into().unwrap()), |
| 129 | mod_date: u16::from_le_bytes(value[10..12].try_into().unwrap()), |
| 130 | crc: u32::from_le_bytes(value[12..16].try_into().unwrap()), |
| 131 | compressed_size: u32::from_le_bytes(value[16..20].try_into().unwrap()), |
| 132 | uncompressed_size: u32::from_le_bytes(value[20..24].try_into().unwrap()), |
| 133 | file_name_length: u16::from_le_bytes(value[24..26].try_into().unwrap()), |
| 134 | extra_field_length: u16::from_le_bytes(value[26..28].try_into().unwrap()), |
| 135 | file_comment_length: u16::from_le_bytes(value[28..30].try_into().unwrap()), |
| 136 | disk_start: u16::from_le_bytes(value[30..32].try_into().unwrap()), |
| 137 | inter_attr: u16::from_le_bytes(value[32..34].try_into().unwrap()), |
| 138 | exter_attr: u32::from_le_bytes(value[34..38].try_into().unwrap()), |
| 139 | lh_offset: u32::from_le_bytes(value[38..42].try_into().unwrap()), |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl From<[u8; 18]> for EndOfCentralDirectoryHeader { |
| 145 | fn from(value: [u8; 18]) -> EndOfCentralDirectoryHeader { |
| 146 | EndOfCentralDirectoryHeader { |
| 147 | disk_num: u16::from_le_bytes(value[0..2].try_into().unwrap()), |
| 148 | start_cent_dir_disk: u16::from_le_bytes(value[2..4].try_into().unwrap()), |
| 149 | num_of_entries_disk: u16::from_le_bytes(value[4..6].try_into().unwrap()), |
| 150 | num_of_entries: u16::from_le_bytes(value[6..8].try_into().unwrap()), |
| 151 | size_cent_dir: u32::from_le_bytes(value[8..12].try_into().unwrap()), |
| 152 | cent_dir_offset: u32::from_le_bytes(value[12..16].try_into().unwrap()), |
| 153 | file_comm_length: u16::from_le_bytes(value[16..18].try_into().unwrap()), |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | impl From<[u8; 52]> for Zip64EndOfCentralDirectoryRecord { |
| 159 | fn from(value: [u8; 52]) -> Self { |
| 160 | Self { |
| 161 | size_of_zip64_end_of_cd_record: u64::from_le_bytes(value[0..8].try_into().unwrap()), |
| 162 | version_made_by: u16::from_le_bytes(value[8..10].try_into().unwrap()), |
| 163 | version_needed_to_extract: u16::from_le_bytes(value[10..12].try_into().unwrap()), |
| 164 | disk_number: u32::from_le_bytes(value[12..16].try_into().unwrap()), |
| 165 | disk_number_start_of_cd: u32::from_le_bytes(value[16..20].try_into().unwrap()), |
| 166 | num_entries_in_directory_on_disk: u64::from_le_bytes(value[20..28].try_into().unwrap()), |
| 167 | num_entries_in_directory: u64::from_le_bytes(value[28..36].try_into().unwrap()), |
| 168 | directory_size: u64::from_le_bytes(value[36..44].try_into().unwrap()), |
| 169 | offset_of_start_of_directory: u64::from_le_bytes(value[44..52].try_into().unwrap()), |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | impl From<[u8; 16]> for Zip64EndOfCentralDirectoryLocator { |
| 175 | fn from(value: [u8; 16]) -> Self { |
| 176 | Self { |
| 177 | number_of_disk_with_start_of_zip64_end_of_central_directory: u32::from_le_bytes( |
| 178 | value[0..4].try_into().unwrap(), |
| 179 | ), |
| 180 | relative_offset: u64::from_le_bytes(value[4..12].try_into().unwrap()), |
| 181 | total_number_of_disks: u32::from_le_bytes(value[12..16].try_into().unwrap()), |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | impl LocalFileHeader { |
| 187 | pub async fn from_reader<R: AsyncRead + Unpin>(reader: &mut R) -> Result<LocalFileHeader> { |
| 188 | let mut buffer: [u8; 26] = [0; 26]; |
| 189 | reader.read_exact(&mut buffer).await?; |
| 190 | Ok(LocalFileHeader::from(buffer)) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | impl EndOfCentralDirectoryHeader { |
| 195 | pub async fn from_reader<R: AsyncRead + Unpin>(reader: &mut R) -> Result<EndOfCentralDirectoryHeader> { |
| 196 | let mut buffer: [u8; 18] = [0; 18]; |
| 197 | reader.read_exact(&mut buffer).await?; |
| 198 | Ok(EndOfCentralDirectoryHeader::from(buffer)) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | impl CentralDirectoryRecord { |
| 203 | pub async fn from_reader<R: AsyncRead + Unpin>(reader: &mut R) -> Result<CentralDirectoryRecord> { |
| 204 | let mut buffer: [u8; 42] = [0; 42]; |
| 205 | reader.read_exact(&mut buffer).await?; |
| 206 | Ok(CentralDirectoryRecord::from(buffer)) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | impl Zip64EndOfCentralDirectoryRecord { |
| 211 | pub async fn from_reader<R: AsyncRead + Unpin>(reader: &mut R) -> Result<Zip64EndOfCentralDirectoryRecord> { |
| 212 | let mut buffer: [u8; 52] = [0; 52]; |
| 213 | reader.read_exact(&mut buffer).await?; |
| 214 | Ok(Self::from(buffer)) |
| 215 | } |
| 216 | |
| 217 | pub fn as_bytes(&self) -> [u8; 52] { |
| 218 | let mut array = [0; 52]; |
| 219 | let mut cursor = 0; |
| 220 | |
| 221 | array_push!(array, cursor, self.size_of_zip64_end_of_cd_record.to_le_bytes()); |
| 222 | array_push!(array, cursor, self.version_made_by.to_le_bytes()); |
| 223 | array_push!(array, cursor, self.version_needed_to_extract.to_le_bytes()); |
| 224 | array_push!(array, cursor, self.disk_number.to_le_bytes()); |
| 225 | array_push!(array, cursor, self.disk_number_start_of_cd.to_le_bytes()); |
| 226 | array_push!(array, cursor, self.num_entries_in_directory_on_disk.to_le_bytes()); |
| 227 | array_push!(array, cursor, self.num_entries_in_directory.to_le_bytes()); |
| 228 | array_push!(array, cursor, self.directory_size.to_le_bytes()); |
| 229 | array_push!(array, cursor, self.offset_of_start_of_directory.to_le_bytes()); |
| 230 | |
| 231 | array |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | impl Zip64EndOfCentralDirectoryLocator { |
| 236 | /// Read 4 bytes from the reader and check whether its signature matches that of the EOCDL. |
| 237 | /// If it does, return Some(EOCDL), otherwise return None. |
| 238 | pub async fn try_from_reader<R: AsyncRead + Unpin>( |
| 239 | reader: &mut R, |
| 240 | ) -> Result<Option<Zip64EndOfCentralDirectoryLocator>> { |
| 241 | let signature = { |
| 242 | let mut buffer = [0; 4]; |
| 243 | reader.read_exact(&mut buffer).await?; |
| 244 | u32::from_le_bytes(buffer) |
| 245 | }; |
| 246 | if signature != ZIP64_EOCDL_SIGNATURE { |
| 247 | return Ok(None); |
| 248 | } |
| 249 | let mut buffer: [u8; 16] = [0; 16]; |
| 250 | reader.read_exact(&mut buffer).await?; |
| 251 | Ok(Some(Self::from(buffer))) |
| 252 | } |
| 253 | |
| 254 | pub fn as_bytes(&self) -> [u8; 16] { |
| 255 | let mut array = [0; 16]; |
| 256 | let mut cursor = 0; |
| 257 | |
| 258 | array_push!(array, cursor, self.number_of_disk_with_start_of_zip64_end_of_central_directory.to_le_bytes()); |
| 259 | array_push!(array, cursor, self.relative_offset.to_le_bytes()); |
| 260 | array_push!(array, cursor, self.total_number_of_disks.to_le_bytes()); |
| 261 | |
| 262 | array |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /// Parse the extra fields. |
| 267 | pub fn parse_extra_fields(data: Vec<u8>, uncompressed_size: u32, compressed_size: u32) -> Result<Vec<ExtraField>> { |
| 268 | let mut cursor = 0; |
| 269 | let mut extra_fields = Vec::new(); |
| 270 | while cursor + 4 < data.len() { |
| 271 | let header_id: HeaderId = u16::from_le_bytes(data[cursor..cursor + 2].try_into().unwrap()).into(); |
| 272 | let field_size = u16::from_le_bytes(data[cursor + 2..cursor + 4].try_into().unwrap()); |
| 273 | if cursor + 4 + field_size as usize > data.len() { |
| 274 | return Err(ZipError::InvalidExtraFieldHeader(field_size, data.len() - cursor - 8 - field_size as usize)); |
| 275 | } |
| 276 | let data = &data[cursor + 4..cursor + 4 + field_size as usize]; |
| 277 | extra_fields.push(extra_field_from_bytes(header_id, field_size, data, uncompressed_size, compressed_size)?); |
| 278 | cursor += 4 + field_size as usize; |
| 279 | } |
| 280 | Ok(extra_fields) |
| 281 | } |
| 282 | |
| 283 | /// Replace elements of an array at a given cursor index for use with a zero-initialised array. |
| 284 | macro_rules! array_push { |
| 285 | ($arr:ident, $cursor:ident, $value:expr) => {{ |
| 286 | for entry in $value { |
| 287 | $arr[$cursor] = entry; |
| 288 | $cursor += 1; |
| 289 | } |
| 290 | }}; |
| 291 | } |
| 292 | |
| 293 | use crate::spec::consts::ZIP64_EOCDL_SIGNATURE; |
| 294 | use crate::spec::extra_field::extra_field_from_bytes; |
| 295 | pub(crate) use array_push; |
| 296 | |
| 297 | #[cfg(test)] |
| 298 | mod tests { |
| 299 | use super::*; |
| 300 | |
| 301 | #[test] |
| 302 | fn test_parse_zip64_eocdr() { |
| 303 | let eocdr: [u8; 56] = [ |
| 304 | 0x50, 0x4B, 0x06, 0x06, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x03, 0x2D, 0x00, 0x00, 0x00, |
| 305 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, |
| 306 | 0x00, 0x00, 0x00, 0x00, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 307 | 0x00, 0x00, |
| 308 | ]; |
| 309 | |
| 310 | let without_signature: [u8; 52] = eocdr[4..56].try_into().unwrap(); |
| 311 | let zip64eocdr = Zip64EndOfCentralDirectoryRecord::from(without_signature); |
| 312 | assert_eq!( |
| 313 | zip64eocdr, |
| 314 | Zip64EndOfCentralDirectoryRecord { |
| 315 | size_of_zip64_end_of_cd_record: 44, |
| 316 | version_made_by: 798, |
| 317 | version_needed_to_extract: 45, |
| 318 | disk_number: 0, |
| 319 | disk_number_start_of_cd: 0, |
| 320 | num_entries_in_directory_on_disk: 1, |
| 321 | num_entries_in_directory: 1, |
| 322 | directory_size: 47, |
| 323 | offset_of_start_of_directory: 64, |
| 324 | } |
| 325 | ) |
| 326 | } |
| 327 | |
| 328 | #[tokio::test] |
| 329 | async fn test_parse_zip64_eocdl() { |
| 330 | let eocdl: [u8; 20] = [ |
| 331 | 0x50, 0x4B, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, |
| 332 | 0x00, 0x00, |
| 333 | ]; |
| 334 | let mut cursor = futures_lite::io::Cursor::new(eocdl); |
| 335 | let zip64eocdl = Zip64EndOfCentralDirectoryLocator::try_from_reader(&mut cursor).await.unwrap().unwrap(); |
| 336 | assert_eq!( |
| 337 | zip64eocdl, |
| 338 | Zip64EndOfCentralDirectoryLocator { |
| 339 | number_of_disk_with_start_of_zip64_end_of_central_directory: 0, |
| 340 | relative_offset: 111, |
| 341 | total_number_of_disks: 1, |
| 342 | } |
| 343 | ) |
| 344 | } |
| 345 | } |