Skip to content

Commit cb97144

Browse files
committed
fix: first pass on maintainer comments
1 parent bacaf3e commit cb97144

File tree

3 files changed

+45
-28
lines changed

3 files changed

+45
-28
lines changed

src/read.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::crc32::Crc32Reader;
88
use crate::extra_fields::{ExtendedTimestamp, ExtraField};
99
use crate::read::zip_archive::{Shared, SharedBuilder};
1010
use crate::result::{ZipError, ZipResult};
11-
use crate::spec::{self, CentralDirectoryEndInfo, FixedSizeBlock, Pod};
11+
use crate::spec::{self, CentralDirectoryEndInfo, DataAndPosition, FixedSizeBlock, Pod};
1212
use crate::types::{
1313
AesMode, AesVendorVersion, DateTime, System, ZipCentralEntryBlock, ZipFileData,
1414
ZipLocalEntryBlock,
@@ -450,7 +450,7 @@ impl<'a> TryFrom<&'a CentralDirectoryEndInfo> for CentralDirectoryInfo {
450450

451451
let (relative_cd_offset, number_of_files, disk_number, disk_with_central_directory) =
452452
match &value.eocd64 {
453-
Some((eocd64, _)) => {
453+
Some(DataAndPosition { data: eocd64, .. }) => {
454454
if eocd64.number_of_files_on_this_disk > eocd64.number_of_files {
455455
return Err(InvalidArchive(
456456
"ZIP64 footer indicates more files on this disk than in the whole archive",
@@ -469,10 +469,10 @@ impl<'a> TryFrom<&'a CentralDirectoryEndInfo> for CentralDirectoryInfo {
469469
)
470470
}
471471
_ => (
472-
value.eocd.0.central_directory_offset as u64,
473-
value.eocd.0.number_of_files_on_this_disk as usize,
474-
value.eocd.0.disk_number as u32,
475-
value.eocd.0.disk_with_central_directory as u32,
472+
value.eocd.data.central_directory_offset as u64,
473+
value.eocd.data.number_of_files_on_this_disk as usize,
474+
value.eocd.data.disk_number as u32,
475+
value.eocd.data.disk_with_central_directory as u32,
476476
),
477477
};
478478

@@ -608,8 +608,8 @@ impl<R: Read + Seek> ZipArchive<R> {
608608
let shared = Self::read_central_header(info, config, reader)?;
609609

610610
Ok(shared.build(
611-
cde.eocd.0.zip_file_comment,
612-
cde.eocd64.map(|(v, _)| v.extensible_data_sector),
611+
cde.eocd.data.zip_file_comment,
612+
cde.eocd64.map(|v| v.data.extensible_data_sector),
613613
))
614614
}
615615

@@ -1551,7 +1551,7 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult<Opt
15511551
let mut block = ZipLocalEntryBlock::zeroed();
15521552
reader.read_exact(block.as_bytes_mut())?;
15531553

1554-
match block.magic().from_le() {
1554+
match block.magic() {
15551555
spec::Magic::LOCAL_FILE_HEADER_SIGNATURE => (),
15561556
spec::Magic::CENTRAL_DIRECTORY_HEADER_SIGNATURE => return Ok(None),
15571557
_ => return Err(ZipLocalEntryBlock::WRONG_MAGIC_ERROR),

src/read/magic_finder.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@ pub struct MagicFinder<'a> {
1717

1818
impl<'a> MagicFinder<'a> {
1919
/// Create a new magic bytes finder to look within specific bounds.
20-
pub fn new(magic_bytes: &'a [u8], bounds: (u64, u64)) -> Self {
20+
pub fn new(magic_bytes: &'a [u8], start_inclusive: u64, end_exclusive: u64) -> Self {
2121
const BUFFER_SIZE: usize = 2048;
2222

2323
// Smaller buffer size would be unable to locate bytes.
2424
// Equal buffer size would stall (the window could not be moved).
25-
debug_assert!(BUFFER_SIZE > magic_bytes.len());
25+
debug_assert!(BUFFER_SIZE >= magic_bytes.len());
2626

2727
Self {
2828
buffer: vec![0; BUFFER_SIZE].into_boxed_slice(),
2929
finder: FinderRev::new(magic_bytes),
30-
cursor: bounds
31-
.1
30+
cursor: end_exclusive
3231
.saturating_sub(BUFFER_SIZE as u64)
33-
.clamp(bounds.0, bounds.1),
32+
.clamp(start_inclusive, end_exclusive),
3433
mid_buffer_offset: None,
35-
bounds,
34+
bounds: (start_inclusive, end_exclusive),
3635
}
3736
}
3837

@@ -109,7 +108,7 @@ impl<'a> MagicFinder<'a> {
109108
/* Move cursor to the next chunk, cover magic at boundary by shifting by needle length. */
110109
self.cursor = self
111110
.cursor
112-
.saturating_add(self.finder.needle().len() as u64)
111+
.saturating_add(self.finder.needle().len() as u64 - 1)
113112
.saturating_sub(self.buffer.len() as u64)
114113
.clamp(self.bounds.0, self.bounds.1);
115114
}
@@ -139,7 +138,7 @@ impl<'a> OptimisticMagicFinder<'a> {
139138
/// Create a new empty optimistic magic bytes finder.
140139
pub fn new_empty() -> Self {
141140
Self {
142-
inner: MagicFinder::new(&[], (0, 0)),
141+
inner: MagicFinder::new(&[], 0, 0),
143142
initial_guess: None,
144143
}
145144
}
@@ -183,6 +182,9 @@ impl<'a> OptimisticMagicFinder<'a> {
183182
// If a match is not found, but the initial guess was mandatory, return an error.
184183
if mandatory {
185184
return Ok(None);
185+
} else {
186+
// If the initial guess was not mandatory, remove it, as it was not found.
187+
self.initial_guess.take();
186188
}
187189
}
188190

src/spec.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub(crate) struct Zip32CentralDirectoryEnd {
290290
}
291291

292292
impl Zip32CentralDirectoryEnd {
293-
fn block_and_comment(self) -> ZipResult<(Zip32CDEBlock, Box<[u8]>)> {
293+
fn try_into_block_and_comment(self) -> ZipResult<(Zip32CDEBlock, Box<[u8]>)> {
294294
let Self {
295295
disk_number,
296296
disk_with_central_directory,
@@ -344,7 +344,7 @@ impl Zip32CentralDirectoryEnd {
344344
}
345345

346346
pub fn write<T: Write>(self, writer: &mut T) -> ZipResult<()> {
347-
let (block, comment) = self.block_and_comment()?;
347+
let (block, comment) = self.try_into_block_and_comment()?;
348348
block.write(writer)?;
349349
writer.write_all(&comment)?;
350350
Ok(())
@@ -513,7 +513,7 @@ impl Zip64CentralDirectoryEnd {
513513
})
514514
}
515515

516-
pub fn block_and_comment(self) -> (Zip64CDEBlock, Box<[u8]>) {
516+
pub fn into_block_and_comment(self) -> (Zip64CDEBlock, Box<[u8]>) {
517517
let Self {
518518
record_size,
519519
version_made_by,
@@ -545,16 +545,31 @@ impl Zip64CentralDirectoryEnd {
545545
}
546546

547547
pub fn write<T: Write>(self, writer: &mut T) -> ZipResult<()> {
548-
let (block, comment) = self.block_and_comment();
548+
let (block, comment) = self.into_block_and_comment();
549549
block.write(writer)?;
550550
writer.write_all(&comment)?;
551551
Ok(())
552552
}
553553
}
554554

555+
pub(crate) struct DataAndPosition<T> {
556+
pub data: T,
557+
#[allow(dead_code)]
558+
pub position: u64,
559+
}
560+
561+
impl<T> From<(T, u64)> for DataAndPosition<T> {
562+
fn from(value: (T, u64)) -> Self {
563+
Self {
564+
data: value.0,
565+
position: value.1,
566+
}
567+
}
568+
}
569+
555570
pub(crate) struct CentralDirectoryEndInfo {
556-
pub eocd: (Zip32CentralDirectoryEnd, u64),
557-
pub eocd64: Option<(Zip64CentralDirectoryEnd, u64)>,
571+
pub eocd: DataAndPosition<Zip32CentralDirectoryEnd>,
572+
pub eocd64: Option<DataAndPosition<Zip64CentralDirectoryEnd>>,
558573

559574
pub archive_offset: u64,
560575
}
@@ -580,7 +595,7 @@ pub fn find_central_directory<R: Read + Seek>(
580595
let file_length = reader.seek(io::SeekFrom::End(0))?;
581596

582597
// Instantiate the mandatory finder
583-
let mut eocd_finder = MagicFinder::new(&EOCD_SIG_BYTES, (0, file_length));
598+
let mut eocd_finder = MagicFinder::new(&EOCD_SIG_BYTES, 0, file_length);
584599
let mut subfinder: Option<OptimisticMagicFinder<'static>> = None;
585600

586601
// Keep the last errors for cases of improper EOCD instances.
@@ -612,7 +627,7 @@ pub fn find_central_directory<R: Read + Seek>(
612627
// If the archive is empty, there is nothing more to be checked, the archive is correct.
613628
if eocd.number_of_files == 0 {
614629
return Ok(CentralDirectoryEndInfo {
615-
eocd: (eocd, eocd_offset),
630+
eocd: (eocd, eocd_offset).into(),
616631
eocd64: None,
617632
archive_offset: eocd_offset - relative_cd_offset,
618633
});
@@ -646,7 +661,7 @@ pub fn find_central_directory<R: Read + Seek>(
646661
let archive_offset = cd_offset - relative_cd_offset;
647662

648663
return Ok(CentralDirectoryEndInfo {
649-
eocd: (eocd, eocd_offset),
664+
eocd: (eocd, eocd_offset).into(),
650665
eocd64: None,
651666
archive_offset,
652667
});
@@ -737,8 +752,8 @@ pub fn find_central_directory<R: Read + Seek>(
737752
) {
738753
Ok(eocd64) => {
739754
return Ok(CentralDirectoryEndInfo {
740-
eocd: (eocd, eocd_offset),
741-
eocd64: Some((eocd64, eocd64_offset)),
755+
eocd: (eocd, eocd_offset).into(),
756+
eocd64: Some((eocd64, eocd64_offset).into()),
742757
archive_offset,
743758
})
744759
}

0 commit comments

Comments
 (0)