1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
use crate::driver::{ArchiveRead, ArchiveWrite, Driver};
use crate::zip::file::{BitFlag, CompressionMethod};
use crate::zip::structs::{deserialize, EOCDR64Locator, ExtraHeader, CDR, EOCDR, EOCDR64};
use crate::zip::{ZipError, ZipFile, ZipResult};
use chrono::{Local, NaiveDate, NaiveDateTime, NaiveTime};
use std::collections::HashMap as Map;
use std::io::{Read, Seek, SeekFrom, Write};
pub struct Zip<IO> {
io: IO,
files: Map<String, ZipFile>,
comment: String,
}
impl<IO> Driver for Zip<IO> {
type Error = ZipError;
type IO = IO;
type File = ZipFile;
}
impl<IO: Read + Seek> ArchiveRead for Zip<IO> {
fn read(mut io: Self::IO) -> ZipResult<Self> {
// Search eocdr
let limit = 65557.min(io.seek(SeekFrom::End(0))?) as i64;
let start = io.seek(SeekFrom::End(-limit))?;
let pos = start + {
let mut buf = vec![0; limit as usize];
io.read(&mut buf)?;
buf[..buf.len() - 18]
.windows(4)
.rposition(|v| u32::from_le_bytes(v.try_into().unwrap()) == 0x06054b50)
.ok_or(ZipError::EOCDRNotFound)? as u64
};
// Read eocdr
io.seek(SeekFrom::Start(pos + 4))?;
let buf = {
let mut buf = [0; 18];
io.read(&mut buf)?;
buf
};
let eocdr: EOCDR = deserialize(&buf).unwrap();
let comment = {
let mut buf: Vec<u8> = vec![0; eocdr.comment_len as usize];
io.read(&mut buf)?;
String::from_utf8(buf).map_err(|_| ZipError::InvalidArchiveComment)?
};
// Try to find eocdr64locator
io.seek(SeekFrom::Start(pos - 20))?;
let buf = {
let mut buf = [0; 20];
io.read(&mut buf)?;
buf
};
let (cd_pointer, cd_size, cd_records) =
if u32::from_le_bytes(buf[0..4].try_into().unwrap()) == 0x07064b50 {
let eocdr64locator: EOCDR64Locator = deserialize(&buf[4..]).unwrap();
io.seek(SeekFrom::Start(eocdr64locator.eocdr64_pointer))?;
let buf = {
let mut buf = [0; 56];
io.read(&mut buf)?;
buf
};
if u32::from_le_bytes(buf[0..4].try_into().unwrap()) != 0x06064b50 {
return Err(ZipError::InvalidEOCDR64Signature.into());
}
let eocdr64: EOCDR64 = deserialize(&buf[4..]).unwrap();
(eocdr64.cd_pointer, eocdr64.cd_size, eocdr64.cd_records)
} else {
(
eocdr.cd_pointer as u64,
eocdr.cd_size as u64,
eocdr.cd_records as u64,
)
};
// Read cd records
let mut files = Map::with_capacity(cd_records as usize);
io.seek(SeekFrom::Start(cd_pointer))?;
let buf = {
let mut buf = vec![0; cd_size as usize];
io.read(&mut buf)?;
buf
};
let mut p: usize = 0;
for _ in 0..cd_records {
if u32::from_le_bytes(buf[p..p + 4].try_into().unwrap()) != 0x02014b50 {
return Err(ZipError::InvalidCDRSignature.into());
}
p += 4;
let cdr: CDR = deserialize(&buf[p..p + 42]).unwrap();
p += 42;
let name = String::from_utf8(buf[p..p + cdr.name_len as usize].into())
.map_err(|_| ZipError::InvalidFileName)?;
p += cdr.name_len as usize;
let extra_fields: Vec<u8> = buf[p..p + cdr.extra_field_len as usize].into();
p += cdr.extra_field_len as usize;
let comment = String::from_utf8(buf[p..p + cdr.comment_len as usize].into())
.map_err(|_| ZipError::InvalidFileComment)?;
p += cdr.comment_len as usize;
let mut compressed_size = cdr.compressed_size as u64;
let mut size = cdr.size as u64;
let mut header_pointer = cdr.header_pointer as u64;
let mut ep: usize = 0;
while ep < cdr.extra_field_len as usize {
let header: ExtraHeader = deserialize(&extra_fields[ep..ep + 4]).unwrap();
ep += 4;
match header.id {
0x0001 => {
if size == 0xFFFFFFFF {
compressed_size = deserialize(&extra_fields[ep..ep + 8]).unwrap();
ep += 8;
}
if compressed_size == 0xFFFFFFFF {
size = deserialize(&extra_fields[ep..ep + 8]).unwrap();
ep += 8;
}
if header_pointer == 0xFFFFFFFF {
header_pointer = deserialize(&extra_fields[ep..ep + 8]).unwrap();
ep += 8;
}
if cdr.disk == 0xFFFF {
ep += 4
}
}
_ => ep += header.size as usize,
}
}
files.insert(
name.clone(),
ZipFile::new(
CompressionMethod::from_struct_id(cdr.compression_method)?,
BitFlag::new(cdr.bit_flag),
NaiveDateTime::new(
NaiveDate::from_ymd_opt(
(cdr.dos_date as i32 >> 9 & 0x7F) + 1980,
cdr.dos_date as u32 >> 5 & 0xF,
cdr.dos_date as u32 & 0x1F,
)
.ok_or(ZipError::InvalidDate)?,
NaiveTime::from_hms_opt(
(cdr.dos_time as u32 >> 11) & 0x1F,
(cdr.dos_time as u32 >> 5) & 0x3F,
(cdr.dos_time as u32 & 0x1F) * 2,
)
.ok_or(ZipError::InvalidTime)?,
)
.and_local_timezone(Local)
.unwrap(),
cdr.crc,
compressed_size,
size,
header_pointer,
name,
comment,
),
);
}
Ok(Self { io, files, comment })
}
fn files(&self) -> Vec<&Self::File> {
let mut files: Vec<&Self::File> = self.files.values().collect();
files.sort_by_key(|f| &f.name);
files
}
fn get_file(&self, name: &str) -> Option<&Self::File> {
self.files.get(name)
}
}
impl<IO: Read + Seek> Zip<IO> {
pub fn comment(&self) -> &String {
&self.comment
}
}
impl<IO: Read + Write + Seek> ArchiveWrite for Zip<IO> {}
impl<IO: Read + Write + Seek> Zip<IO> {}
|