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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use crate::{error::*, fields, format::*};
use nom::{
bytes::streaming::tag,
combinator::map,
multi::length_data,
number::streaming::{le_u16, le_u32, le_u64},
sequence::{preceded, tuple},
};
use tracing::trace;
pub struct EndOfCentralDirectoryRecord {
pub disk_nbr: u16,
pub dir_disk_nbr: u16,
pub dir_records_this_disk: u16,
pub directory_records: u16,
pub directory_size: u32,
pub directory_offset: u32,
pub comment: ZipString,
}
impl EndOfCentralDirectoryRecord {
const MIN_LENGTH: usize = 20;
const SIGNATURE: &'static str = "PK\x05\x06";
pub fn find_in_block(b: &[u8]) -> Option<Located<Self>> {
for i in (0..(b.len() - Self::MIN_LENGTH + 1)).rev() {
let slice = &b[i..];
if let Ok((_, directory)) = Self::parse(slice) {
return Some(Located {
offset: i as u64,
inner: directory,
});
}
}
None
}
pub fn parse(i: &[u8]) -> parse::Result<'_, Self> {
preceded(
tag(Self::SIGNATURE),
map(
tuple((
le_u16::<&[u8], _>,
le_u16,
le_u16,
le_u16,
le_u32,
le_u32,
length_data(le_u16),
)),
|(
disk_nbr,
dir_disk_nbr,
dir_records_this_disk,
directory_records,
directory_size,
directory_offset,
comment,
)| Self {
disk_nbr,
dir_disk_nbr,
dir_records_this_disk,
directory_records,
directory_size,
directory_offset,
comment: comment.into(),
},
),
)(i)
}
}
pub struct EndOfCentralDirectory64Locator {
pub dir_disk_number: u32,
pub directory_offset: u64,
pub total_disks: u32,
}
impl EndOfCentralDirectory64Locator {
pub const LENGTH: usize = 20;
const SIGNATURE: &'static str = "PK\x06\x07";
pub fn parse(i: &[u8]) -> parse::Result<'_, Self> {
preceded(
tag(Self::SIGNATURE),
fields!(Self {
dir_disk_number: le_u32,
directory_offset: le_u64,
total_disks: le_u32,
}),
)(i)
}
}
pub struct EndOfCentralDirectory64Record {
pub record_size: u64,
pub creator_version: u16,
pub reader_version: u16,
pub disk_nbr: u32,
pub dir_disk_nbr: u32,
pub dir_records_this_disk: u64,
pub directory_records: u64,
pub directory_size: u64,
pub directory_offset: u64,
}
impl EndOfCentralDirectory64Record {
const SIGNATURE: &'static str = "PK\x06\x06";
pub fn parse(i: &[u8]) -> parse::Result<'_, Self> {
preceded(
tag(Self::SIGNATURE),
fields!(Self {
record_size: le_u64,
creator_version: le_u16,
reader_version: le_u16,
disk_nbr: le_u32,
dir_disk_nbr: le_u32,
dir_records_this_disk: le_u64,
directory_records: le_u64,
directory_size: le_u64,
directory_offset: le_u64,
}),
)(i)
}
}
pub struct Located<T> {
pub offset: u64,
pub inner: T,
}
impl<T> std::ops::Deref for Located<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> std::ops::DerefMut for Located<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
pub struct EndOfCentralDirectory {
pub dir: Located<EndOfCentralDirectoryRecord>,
pub dir64: Option<Located<EndOfCentralDirectory64Record>>,
pub global_offset: i64,
}
impl EndOfCentralDirectory {
pub fn new(
size: u64,
dir: Located<EndOfCentralDirectoryRecord>,
dir64: Option<Located<EndOfCentralDirectory64Record>>,
) -> Result<Self, Error> {
let mut res = Self {
dir,
dir64,
global_offset: 0,
};
let computed_directory_offset = res.located_directory_offset() - res.directory_size();
if (0..size).contains(&computed_directory_offset) {
if computed_directory_offset != res.directory_offset() {
res.global_offset =
computed_directory_offset as i64 - res.directory_offset() as i64;
res.set_directory_offset(computed_directory_offset);
}
}
trace!(
"directory offset = {}, valid range = 0..{}",
res.directory_offset(),
size
);
if !(0..size).contains(&res.directory_offset()) {
return Err(FormatError::DirectoryOffsetPointsOutsideFile.into());
}
Ok(res)
}
pub fn located_directory_offset(&self) -> u64 {
match self.dir64.as_ref() {
Some(d64) => d64.offset,
None => self.dir.offset,
}
}
pub fn directory_offset(&self) -> u64 {
match self.dir64.as_ref() {
Some(d64) => d64.directory_offset,
None => self.dir.directory_offset as u64,
}
}
pub fn directory_size(&self) -> u64 {
match self.dir64.as_ref() {
Some(d64) => d64.directory_size,
None => self.dir.directory_size as u64,
}
}
pub fn set_directory_offset(&mut self, offset: u64) {
match self.dir64.as_mut() {
Some(d64) => d64.directory_offset = offset,
None => self.dir.directory_offset = offset as u32,
};
}
pub fn directory_records(&self) -> u64 {
match self.dir64.as_ref() {
Some(d64) => d64.directory_records,
None => self.dir.directory_records as u64,
}
}
pub fn comment(&self) -> &ZipString {
&self.dir.comment
}
}