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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use super::*;
use crate::Crc32;
use crate::reader_at::{FileReader, MutexReader, RangeReader, ReaderAt, ReaderAtExt};
use std::io::{Read, Seek};
#[derive(Debug, Clone)]
pub struct ZipSliceVerifier<Decompressor>(pub(super) ZipVerifier<Decompressor>);
impl<Decompressor> ZipSliceVerifier<Decompressor> {
/// Consumes the [`ZipSliceVerifier`], returning the underlying decompressor
/// without verifying.
pub fn into_inner(self) -> Decompressor {
self.0.into_inner()
}
}
impl<Decompressor> std::io::Read for ZipSliceVerifier<Decompressor>
where
Decompressor: std::io::Read,
{
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0.read(buf)
}
}
impl ZipArchive<()> {
/// Parses an archive from a file by reading the End of Central Directory.
///
/// A buffer is required to read parts of the file.
/// [`RECOMMENDED_BUFFER_SIZE`] can be used to construct this buffer.
pub fn from_file(
file: std::fs::File,
buffer: &mut [u8],
) -> Result<ZipArchive<FileReader>, Error> {
ZipLocator::new()
.locate_in_file(file, buffer)
.map_err(|(_, e)| e)
}
/// Parses an archive from a seekable reader.
///
/// Prefer [`ZipArchive::from_file`] and [`ZipArchive::from_slice`] when
/// possible, as they are more efficient due to not wrapping the underlying
/// reader in a mutex to support positioned io.
///
/// ```rust
/// # use rawzip::{ZipArchive, Error, RECOMMENDED_BUFFER_SIZE, ZipFileHeaderRecord};
/// # use std::io::Cursor;
/// fn example(zip_data: &[u8]) -> Result<(), Error> {
/// let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
/// let archive = ZipArchive::from_seekable(Cursor::new(zip_data), &mut buffer)?;
/// Ok(())
/// }
/// ```
pub fn from_seekable<R>(
mut reader: R,
buffer: &mut [u8],
) -> Result<ZipArchive<MutexReader<R>>, Error>
where
R: Read + Seek,
{
let end_offset = reader.seek(std::io::SeekFrom::End(0))?;
let reader = MutexReader::new(reader);
ZipLocator::new()
.locate_in_reader(reader, buffer, end_offset)
.map_err(|(_, e)| e)
}
}
impl<R> ZipArchive<R> {
pub(crate) fn new(reader: R, eocd: EndOfCentralDirectory) -> Self {
ZipArchive { reader, eocd }
}
/// Returns a reference to the underlying reader.
pub fn get_ref(&self) -> &R {
&self.reader
}
/// Returns a mutable reference to the underlying reader.
///
/// This is in contrast to [`ZipSliceArchive`], which cannot safely expose
/// mutable access, as it relies on offsets and direct indexing.
pub fn get_mut(&mut self) -> &mut R {
&mut self.reader
}
/// Consumes this archive and returns the underlying reader.
pub fn into_inner(self) -> R {
self.reader
}
/// Returns a lending iterator over the entries in the central directory of
/// the archive.
///
/// Requires a mutable buffer to read directory entries from the underlying
/// reader.
///
/// ```rust
/// # use rawzip::{ZipArchive, Error, RECOMMENDED_BUFFER_SIZE, ZipFileHeaderRecord};
/// # use std::fs::File;
/// fn example(file: File) -> Result<(), Error> {
/// let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
/// let archive = ZipArchive::from_file(file, &mut buffer)?;
/// let entries_hint = archive.entries_hint();
/// let mut actual_entries = 0;
/// let mut entries_iterator = archive.entries(&mut buffer);
/// while let Some(_) = entries_iterator.next_entry()? {
/// actual_entries += 1;
/// }
/// println!("Found {} entries (hint: {})", actual_entries, entries_hint);
/// Ok(())
/// }
/// ```
pub fn entries<'archive, 'buf>(
&'archive self,
buffer: &'buf mut [u8],
) -> ZipEntries<'archive, 'buf, R> {
ZipEntries {
buffer,
archive: self,
pos: 0,
end: 0,
offset: self.eocd.directory_offset(),
base_offset: self.eocd.base_offset(),
central_dir_end_pos: self.eocd.head_eocd_offset(),
}
}
/// Returns a hint for the total number of entries in the archive.
///
/// This value is read from the End of Central Directory record.
pub fn entries_hint(&self) -> u64 {
self.eocd.entries()
}
/// Returns a Read implementation for the comment of the zip archive.
///
/// Use [`RangeReader::remaining()`] to get the comment length before
/// reading. It is guaranteed to be less than `u16::MAX`.
///
/// # Examples
///
/// ```rust
/// use rawzip::{ZipArchive, ZipStr, RECOMMENDED_BUFFER_SIZE};
/// use std::io::Read;
/// use std::fs::File;
///
/// let file = File::open("assets/test.zip")?;
/// let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
/// let archive = ZipArchive::from_file(file, &mut buffer)?;
///
/// let mut comment_reader = archive.comment();
/// let comment_len = comment_reader.remaining() as usize;
/// comment_reader.read_exact(&mut buffer[..comment_len])?;
///
/// let actual = ZipStr::new(&buffer[..comment_len]);
/// let expected = ZipStr::new(b"This is a zipfile comment.");
/// assert_eq!(expected, actual);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn comment(&self) -> RangeReader<&R> {
let comment_start =
self.eocd.tail_eocd_offset() + EndOfCentralDirectoryRecordFixed::SIZE as u64;
let comment_end = comment_start + self.eocd.comment_len() as u64;
RangeReader::new(&self.reader, comment_start..comment_end)
}
/// Returns the offset of the End of Central Directory (EOCD) signature.
///
/// This has the same semantics as [`ZipSliceArchive::eocd_offset`].
///
/// # Examples
///
/// ```rust
/// # use rawzip::{ZipArchive, ZipLocator, RECOMMENDED_BUFFER_SIZE};
/// # use std::fs::File;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let file = File::open("assets/test.zip")?;
/// # let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
/// let archive = ZipArchive::from_file(file, &mut buffer)?;
/// let eocd_position = archive.eocd_offset();
///
/// let locator = ZipLocator::new();
/// let reader = archive.get_ref();
/// let maybe_previous = locator.locate_in_reader(reader, &mut buffer, eocd_position);
/// # Ok(())
/// # }
/// ```
pub fn eocd_offset(&self) -> u64 {
self.eocd.tail_eocd_offset()
}
/// The declared offset of the start of the central directory.
///
/// This has the same semantics as [`ZipSliceArchive::directory_offset`].
/// To verify the validity of this offset, start iterating through the
/// central directory via [`ZipArchive::entries`]. Ensure no errors are
/// returned on the first entry.
pub fn directory_offset(&self) -> u64 {
self.eocd.directory_offset()
}
/// Returns the offset where the ZIP archive ends.
///
/// This has the same semantics as [`ZipSliceArchive::end_offset`].
///
/// This can be used in conjunction with the starting offset calculation
/// start offset as shown in [`RangeReader`] to determine the exact byte
/// range (and thus size) of the ZIP archive within a context of a larger
/// file.
pub fn end_offset(&self) -> u64 {
self.eocd.tail_eocd_offset()
+ EndOfCentralDirectoryRecordFixed::SIZE as u64
+ self.comment().remaining()
}
}
impl<R> ZipArchive<R>
where
R: ReaderAt,
{
/// Seeks to the given file entry in the zip archive.
///
/// This is the reader-backed equivalent of [`ZipSliceArchive::get_entry`].
pub fn get_entry(&self, entry: ZipArchiveEntryWayfinder) -> Result<ZipEntry<'_, R>, Error> {
let mut buffer = [0u8; ZipLocalFileHeaderFixed::SIZE];
self.reader
.read_exact_at(&mut buffer, entry.local_header_offset)?;
// The central directory is the source of truth so we really only parse
// out the local file header to verify the signature and understand the
// variable length. Not everyone uses this as the source of truth:
// https://labs.redyops.com/index.php/2020/04/30/spending-a-night-reading-the-zip-file-format-specification/
let file_header = ZipLocalFileHeaderFixed::parse(&buffer)?;
let (body_offset, o1) = entry
.local_header_offset
.overflowing_add(ZipLocalFileHeaderFixed::SIZE as u64);
let (body_offset, o2) = body_offset.overflowing_add(file_header.variable_length() as u64);
let (body_end_offset, o3) = body_offset.overflowing_add(entry.compressed_size);
if o1 || o2 || o3 {
return Err(Error::from(ErrorKind::Eof));
}
Ok(ZipEntry {
archive: self,
entry,
body_offset,
body_end_offset,
})
}
}
impl<T: ReaderAt> ZipSliceArchive<T> {
/// Converts the [`ZipSliceArchive`] into a general [`ZipArchive`].
///
/// This is useful for unifying code that might handle both slice-based and
/// reader-based archives. Because the underlying data already implements
/// [`ReaderAt`], the conversion is zero-cost. For `AsRef<[u8]>` types that
/// do not implement [`ReaderAt`], use
/// [`ZipSliceArchive::into_cursor_archive`] instead.
pub fn into_reader_archive(self) -> ZipArchive<T> {
ZipArchive::from(self)
}
}
impl<R> From<ZipSliceArchive<R>> for ZipArchive<R>
where
R: ReaderAt,
{
fn from(slice_archive: ZipSliceArchive<R>) -> Self {
ZipArchive {
reader: slice_archive.data,
eocd: slice_archive.eocd,
}
}
}
/// Represents a single entry (file or directory) within a [`ZipArchive`]
#[derive(Debug, Clone)]
pub struct ZipEntry<'archive, R> {
archive: &'archive ZipArchive<R>,
body_offset: u64,
body_end_offset: u64,
entry: ZipArchiveEntryWayfinder,
}
impl<'archive, R> ZipEntry<'archive, R>
where
R: ReaderAt,
{
/// Returns a [`ZipReader`] for reading the compressed data of this entry.
pub fn reader(&self) -> ZipReader<&'archive R> {
ZipReader {
entry: self.entry,
range_reader: RangeReader::new(
self.archive.get_ref(),
self.body_offset..self.body_end_offset,
),
}
}
/// Returns a reader that wraps a decompressor and verify the size and CRC
/// of the decompressed data once finished.
///
/// For slice-backed entries, use [`ZipSliceEntry::verifying_reader`].
pub fn verifying_reader<D>(&self, reader: D) -> ZipVerifier<D>
where
D: std::io::Read,
{
ZipVerifier {
reader,
crc: Crc32::new(),
size: 0,
verifier: ZipVerification {
crc: self.entry.crc,
uncompressed_size: self.entry.uncompressed_size_hint(),
},
}
}
/// Returns a tuple of start and end byte offsets for the compressed data
/// within the underlying reader.
///
/// This has the same semantics as
/// [`ZipSliceEntry::compressed_data_range`], which carries a worked example
/// of using these ranges to detect overlapping (zip bomb) entries.
pub fn compressed_data_range(&self) -> (u64, u64) {
(self.body_offset, self.body_end_offset)
}
/// Returns the local file header information.
///
/// This has the same semantics as [`ZipSliceEntry::local_header`], but
/// requires a caller-provided buffer because the local header is read from
/// the underlying reader.
///
/// The buffer argument must be large enough to hold both the filename and
/// extra fields from the local header or a too small error will be
/// returned.
///
/// # Examples
///
/// ```rust
/// # use rawzip::{ZipArchive, RECOMMENDED_BUFFER_SIZE, extra_fields::ExtraFieldId};
/// # use std::fs::File;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Test with filename mismatch test fixture
/// let file = File::open("assets/filename_mismatch_test.zip")?;
/// let mut buf = vec![0u8; RECOMMENDED_BUFFER_SIZE];
/// let archive = ZipArchive::from_file(file, &mut buf)?;
///
/// let mut entries = archive.entries(&mut buf);
/// let entry_header = entries.next_entry()?.unwrap();
///
/// // Central directory shows one filename
/// assert_eq!(entry_header.file_path().as_ref(), b"malware.exe");
/// let wayfinder = entry_header.wayfinder();
/// let entry = archive.get_entry(wayfinder)?;
///
/// // Read the local header
/// let mut local_buffer = vec![0u8; 1024];
/// let local_header = entry.local_header(&mut local_buffer)?;
///
/// // Local header shows different filename
/// assert_eq!(local_header.file_path().as_ref(), b"safe_file.txt");
///
/// // Access extra fields from local header
/// let mut found_fields = 0;
/// for (field_id, _data) in local_header.extra_fields() {
/// found_fields += 1;
/// // Could check for specific extra field types here
/// println!("Found extra field: {:04x}", field_id.as_u16());
/// }
/// # Ok(())
/// # }
/// ```
pub fn local_header<'a>(&self, buffer: &'a mut [u8]) -> Result<ZipLocalFileHeader<'a>, Error> {
let mut header_buffer = [0u8; ZipLocalFileHeaderFixed::SIZE];
// Read the local file header
self.archive
.get_ref()
.read_exact_at(&mut header_buffer, self.entry.local_header_offset)?;
let local_header_fixed = ZipLocalFileHeaderFixed::parse(&header_buffer)?;
let file_name_len = local_header_fixed.file_name_len as usize;
let extra_field_len = local_header_fixed.extra_field_len as usize;
let total_variable_len = file_name_len + extra_field_len;
// Check if buffer is large enough for both filename and extra fields
if buffer.len() < total_variable_len {
return Err(Error::from(ErrorKind::BufferTooSmall {
required: total_variable_len,
}));
}
let variable_data = &mut buffer[..total_variable_len];
let variable_data_offset =
self.entry.local_header_offset + ZipLocalFileHeaderFixed::SIZE as u64;
self.archive
.get_ref()
.read_exact_at(variable_data, variable_data_offset)?;
let (filename_data, extra_field_data) = variable_data.split_at(file_name_len);
let (compressed_size, uncompressed_size) =
local_header_size_hints(&local_header_fixed, extra_field_data);
Ok(ZipLocalFileHeader {
fixed: local_header_fixed,
compressed_size,
uncompressed_size,
file_path: ZipFilePath::from_bytes(filename_data),
extra_field: extra_field_data,
})
}
}
/// Verifies the checksum of the decompressed data matches the checksum listed
/// in the central directory.
#[derive(Debug, Clone)]
pub struct ZipVerifier<Decompressor> {
pub(super) reader: Decompressor,
pub(super) crc: Crc32,
pub(super) size: u64,
pub(super) verifier: ZipVerification,
}
impl<Decompressor> ZipVerifier<Decompressor> {
/// Consumes the [`ZipVerifier`], returning the underlying decompressor
/// without verifying.
pub fn into_inner(self) -> Decompressor {
self.reader
}
}
impl<Decompressor> std::io::Read for ZipVerifier<Decompressor>
where
Decompressor: std::io::Read,
{
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}
let read = self.reader.read(buf)?;
self.crc.update(&buf[..read]);
self.size += read as u64;
if read == 0 || self.size >= self.verifier.uncompressed_size {
self.verifier
.valid(ZipVerification {
crc: self.crc.checksum(),
uncompressed_size: self.size,
})
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
}
Ok(read)
}
}
/// A reader for a Zip entry's compressed data.
#[derive(Debug, Clone)]
pub struct ZipReader<R> {
entry: ZipArchiveEntryWayfinder,
range_reader: RangeReader<R>,
}
impl<R> ZipReader<R>
where
R: ReaderAt,
{
/// Returns the expected data verification of the inflated data.
///
/// This has the same semantics as [`ZipSliceEntry::claim_verifier`].
pub fn claim_verifier(&self) -> ZipVerification {
ZipVerification {
crc: self.entry.crc,
uncompressed_size: self.entry.uncompressed_size_hint(),
}
}
/// Reads the trailing [`ZipDataDescriptor`] for this entry, if present.
pub fn data_descriptor(&self) -> Result<Option<ZipDataDescriptor>, Error> {
if !self.entry.has_data_descriptor {
return Ok(None);
}
let descriptor = DataDescriptor::read_at(
self.range_reader.get_ref(),
self.range_reader.end_offset(),
self.entry.data_descriptor_uses_zip64_sizes,
)?;
Ok(Some(ZipDataDescriptor {
crc: descriptor.crc,
compressed_size: descriptor.compressed_size,
uncompressed_size: descriptor.uncompressed_size,
}))
}
}
impl<R> Read for ZipReader<R>
where
R: ReaderAt,
{
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.range_reader.read(buf)
}
}
/// A lending iterator over file header records in a [`ZipArchive`].
#[derive(Debug)]
pub struct ZipEntries<'archive, 'buf, R> {
buffer: &'buf mut [u8],
archive: &'archive ZipArchive<R>,
pos: usize,
end: usize,
offset: u64,
base_offset: u64,
central_dir_end_pos: u64,
}
impl<R> ZipEntries<'_, '_, R>
where
R: ReaderAt,
{
/// Yield the next zip file entry in the central directory if there is any
///
/// This method reads from the underlying archive reader into the provided
/// buffer to parse entry headers.
#[inline]
pub fn next_entry(&mut self) -> Result<Option<ZipFileHeaderRecord<'_>>, Error> {
self.next_entry_impl()
}
// While inline always for the reader variant isn't as strong as the slice for the
// extraction benchmark, it is still yielded as 12% improvement when just iterating.
#[inline(always)]
fn next_entry_impl(&mut self) -> Result<Option<ZipFileHeaderRecord<'_>>, Error> {
if self.pos + ZipFileHeaderFixed::SIZE > self.end {
if self.offset >= self.central_dir_end_pos {
return if self.pos == self.end {
Ok(None)
} else if self.buffer[self.pos..self.end].starts_with(&DIGITAL_SIGNATURE_BYTES) {
self.pos = self.end;
Ok(None)
} else {
Err(Error::from(ErrorKind::Eof))
};
}
let remaining = self.end - self.pos;
self.buffer.copy_within(self.pos..self.end, 0);
let max_read = ((self.central_dir_end_pos - self.offset) as usize)
.min(self.buffer.len() - remaining);
let min_read = ZipFileHeaderFixed::SIZE.min(remaining + max_read) - remaining;
let read = self.archive.reader.read_at_least_at(
&mut self.buffer[remaining..][..max_read],
min_read,
self.offset,
)?;
self.offset += read as u64;
self.pos = 0;
self.end = remaining + read;
}
let central_directory_offset = self.offset - (self.end - self.pos) as u64;
let data = &self.buffer[self.pos..self.end];
let file_header = match ZipFileHeaderFixed::parse(data) {
Ok(file_header) => file_header,
Err(err) => return self.next_entry_terminator(err),
};
self.pos += ZipFileHeaderFixed::SIZE;
let variable_length = file_header.variable_length();
if self.pos + variable_length > self.end {
// Need to read more data
let remaining = self.end - self.pos;
// The buffer can never hold this record's variable section.
if variable_length > self.buffer.len() {
return Err(Error::from(ErrorKind::BufferTooSmall {
required: variable_length,
}));
}
// The variable section runs past the end of the central directory,
// so the archive is truncated or corrupt.
let cd_remaining = remaining + (self.central_dir_end_pos - self.offset) as usize;
if variable_length > cd_remaining {
return Err(Error::from(ErrorKind::Eof));
}
self.buffer.copy_within(self.pos..self.end, 0);
let max_read = ((self.central_dir_end_pos - self.offset) as usize)
.min(self.buffer.len() - remaining);
let read = self.archive.reader.read_at_least_at(
&mut self.buffer[remaining..][..max_read],
variable_length - remaining,
self.offset,
)?;
self.offset += read as u64;
self.pos = 0;
self.end = remaining + read;
}
let data = &self.buffer[self.pos..self.end];
let (file_name, extra_field, file_comment, _) = file_header
.parse_variable_length(data)
.expect("variable length precheck failed");
let mut file_header = ZipFileHeaderRecord::from_parts(
file_header,
file_name,
extra_field,
file_comment,
central_directory_offset,
);
file_header.local_header_offset += self.base_offset;
self.pos += variable_length;
Ok(Some(file_header))
}
/// Handle a record that does not start with the central directory file
/// header signature.
///
/// A digital signature record cleanly terminates iteration; anything else
/// is a genuine parse error.
#[cold]
#[inline(never)]
fn next_entry_terminator(
&mut self,
err: Error,
) -> Result<Option<ZipFileHeaderRecord<'_>>, Error> {
let central_directory_offset = self.offset - (self.end - self.pos) as u64;
if self.central_dir_end_pos - central_directory_offset <= MAX_DIGITAL_SIGNATURE_SIZE as u64
&& self.buffer[self.pos..self.end].starts_with(&DIGITAL_SIGNATURE_BYTES)
{
self.pos = self.end;
self.offset = self.central_dir_end_pos;
return Ok(None);
}
Err(err)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
pub fn blank_zip_archive() {
let data = [80, 75, 5, 6];
let mut buf = vec![0u8; RECOMMENDED_BUFFER_SIZE];
let archive = ZipArchive::from_seekable(Cursor::new(data), &mut buf);
assert!(archive.is_err());
}
#[test]
pub fn trunc_comment_zips() {
let data = [
80, 75, 6, 7, 21, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 10, 0, 59, 59, 80, 75, 5, 6, 0,
255, 255, 255, 255, 255, 255, 0, 0, 0, 80, 75, 6, 6, 0, 0, 0, 10,
];
let mut buf = vec![0u8; RECOMMENDED_BUFFER_SIZE];
let archive = ZipArchive::from_seekable(Cursor::new(data), &mut buf);
assert!(archive.is_err());
let archive = ZipArchive::from_slice(data);
assert!(archive.is_err());
}
#[test]
pub fn trunc_eocd64() {
let data = [
80, 75, 6, 7, 21, 0, 0, 0, 34, 0, 0, 0, 0, 0, 0, 0, 10, 0, 59, 59, 80, 75, 5, 6, 0,
255, 255, 255, 255, 255, 255, 0, 0, 0, 80, 75, 6, 6, 0, 0, 6, 0, 0, 250, 255, 255, 255,
255, 251, 0, 0, 0, 0, 80, 5, 6, 0, 0, 0, 0, 56, 0, 0, 0, 0, 10,
];
let archive = ZipArchive::from_slice(data);
assert!(archive.is_err());
let mut buf = vec![0u8; RECOMMENDED_BUFFER_SIZE];
let archive = ZipArchive::from_seekable(Cursor::new(data), &mut buf);
assert!(archive.is_err());
}
#[test]
pub fn trunc_eocd_entry() {
let data = [
80, 75, 1, 2, 159, 159, 159, 159, 159, 159, 159, 159, 159, 0, 241, 205, 0, 80, 75, 5,
6, 0, 48, 249, 0, 250, 255, 255, 255, 255, 251, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35, 0,
];
let archive = ZipArchive::from_slice(data).unwrap();
let mut entries = archive.entries();
assert!(entries.next_entry().is_err());
let mut buf = vec![0u8; RECOMMENDED_BUFFER_SIZE];
let archive = ZipArchive::from_seekable(Cursor::new(data), &mut buf).unwrap();
let mut entries = archive.entries(&mut buf);
assert!(entries.next_entry().is_err());
}
#[test]
fn test_compressed_data_range() {
let test_zip = std::fs::read("assets/test.zip").unwrap();
// Test ZipSliceEntry API (from slice)
let slice_archive = ZipArchive::from_slice(&test_zip).unwrap();
let slice_header_records: Vec<_> = slice_archive
.entries()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(slice_header_records.len(), 2);
let entry1_wayfinder = slice_header_records[0].wayfinder();
let slice_entry1 = slice_archive.get_entry(entry1_wayfinder).unwrap();
let slice_range1 = slice_entry1.compressed_data_range();
assert_eq!(
slice_range1,
(66, 91),
"test.txt compressed data should be at bytes 66-91"
);
let entry2_wayfinder = slice_header_records[1].wayfinder();
let slice_entry2 = slice_archive.get_entry(entry2_wayfinder).unwrap();
let slice_range2 = slice_entry2.compressed_data_range();
assert_eq!(
slice_range2,
(169, 954),
"gophercolor16x16.png compressed data should be at bytes 169-954"
);
let (s1, e1) = slice_range1;
assert!(std::ptr::eq(
slice_entry1.data(),
&test_zip[s1 as usize..e1 as usize]
));
let (s2, e2) = slice_range2;
assert!(std::ptr::eq(
slice_entry2.data(),
&test_zip[s2 as usize..e2 as usize]
));
// Test ZipEntry API
let file = std::fs::File::open("assets/test.zip").unwrap();
let mut buffer = vec![0u8; RECOMMENDED_BUFFER_SIZE];
let reader_archive = ZipArchive::from_file(file, &mut buffer).unwrap();
// Get wayfinders from the slice archive since they should be identical
let reader_entry1 = reader_archive.get_entry(entry1_wayfinder).unwrap();
let reader_range1 = reader_entry1.compressed_data_range();
let reader_entry2 = reader_archive.get_entry(entry2_wayfinder).unwrap();
let reader_range2 = reader_entry2.compressed_data_range();
// Verify both APIs return identical ranges
assert_eq!(slice_range1, reader_range1);
assert_eq!(slice_range2, reader_range2);
}
}