Skip to content

Commit

Permalink
More unification
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Feb 5, 2024
1 parent d713ee8 commit 33cda19
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 33 deletions.
6 changes: 3 additions & 3 deletions rc-zip-sync/src/entry_reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rc_zip::{
fsm::{EntryFsm, FsmResult},
parse::StoredEntry,
parse::Entry,
};
use std::io;
use tracing::trace;
Expand All @@ -17,10 +17,10 @@ impl<R> EntryReader<R>
where
R: io::Read,
{
pub(crate) fn new(entry: &StoredEntry, rd: R) -> Self {
pub(crate) fn new(entry: &Entry, rd: R) -> Self {
Self {
rd,
fsm: Some(EntryFsm::new(Some(entry.inner))),
fsm: Some(EntryFsm::new(Some(entry.clone()))),
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions rc-zip-sync/src/read_zip.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use rc_zip::chrono::{DateTime, TimeZone, Utc};
use rc_zip::{
chrono::{DateTime, TimeZone, Utc},
parse::Entry,
};
use rc_zip::{
error::{Error, FormatError},
fsm::{ArchiveFsm, FsmResult},
parse::{Archive, ExtraField, ExtraFieldSettings, LocalFileHeader, NtfsAttr, StoredEntry},
parse::{Archive, ExtraField, ExtraFieldSettings, LocalFileHeader, NtfsAttr},
};
use tracing::trace;
use winnow::{
Expand Down Expand Up @@ -96,7 +99,7 @@ impl ReadZip for Vec<u8> {
///
/// This only contains metadata for the archive and its entries. Separate
/// readers can be created for arbitraries entries on-demand using
/// [SyncStoredEntry::reader].
/// [SyncEntry::reader].
pub struct SyncArchive<'a, F>
where
F: HasCursor,
Expand Down Expand Up @@ -133,7 +136,7 @@ where
pub fn by_name<N: AsRef<str>>(&self, name: N) -> Option<SyncEntry<'_, F>> {
self.archive
.entries()
.find(|&x| x.name() == name.as_ref())
.find(|&x| x.name == name.as_ref())
.map(|entry| SyncEntry {
file: self.file,
entry,
Expand All @@ -144,11 +147,11 @@ where
/// A zip entry, read synchronously from a file or other I/O resource.
pub struct SyncEntry<'a, F> {
file: &'a F,
entry: &'a StoredEntry,
entry: &'a Entry,
}

impl<F> Deref for SyncEntry<'_, F> {
type Target = StoredEntry;
type Target = Entry;

fn deref(&self) -> &Self::Target {
self.entry
Expand Down
3 changes: 1 addition & 2 deletions rc-zip/src/fsm/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,10 @@ impl ArchiveFsm {
}
};

let is_zip64 = eocd.dir64.is_some();
let global_offset = eocd.global_offset as u64;
let entries: Result<Vec<Entry>, Error> = directory_headers
.iter()
.map(|x| x.as_entry(is_zip64, encoding, global_offset))
.map(|x| x.as_entry(encoding, global_offset))
.collect();
let entries = entries?;

Expand Down
21 changes: 11 additions & 10 deletions rc-zip/src/fsm/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@ impl EntryFsm {
self.buffer.consume(consumed);
let decompressor = AnyDecompressor::new(
header.method,
self.entry.map(|entry| entry.uncompressed_size),
self.entry.as_ref().map(|entry| entry.uncompressed_size),
)?;
let compressed_size = match &self.entry {
Some(entry) => entry.compressed_size,
None => {
// FIXME: the zip64 extra field is here for that
if header.compressed_size == u32::MAX {
return Err(Error::Decompression {
method: header.method,
Expand Down Expand Up @@ -258,14 +259,11 @@ impl EntryFsm {

Ok(FsmResult::Continue((self, outcome)))
}
S::ReadDataDescriptor { .. } => {
S::ReadDataDescriptor { header, .. } => {
let mut input = Partial::new(self.buffer.data());

// if we don't have entry info, we're dangerously assuming the
// file isn't zip64. oh well.
// FIXME: we can just read until the next local file header and
// determine whether the file is zip64 or not from there?
let is_zip64 = self.entry.as_ref().map(|e| e.is_zip64).unwrap_or(false);
let is_zip64 =
header.compressed_size == u32::MAX || header.uncompressed_size == u32::MAX;

match DataDescriptorRecord::mk_parser(is_zip64).parse_next(&mut input) {
Ok(descriptor) => {
Expand All @@ -288,7 +286,7 @@ impl EntryFsm {
metrics,
descriptor,
} => {
let entry_crc32 = self.entry.map(|e| e.crc32).unwrap_or_default();
let entry_crc32 = self.entry.as_ref().map(|e| e.crc32).unwrap_or_default();
let expected_crc32 = if entry_crc32 != 0 {
entry_crc32
} else if let Some(descriptor) = descriptor.as_ref() {
Expand All @@ -297,8 +295,11 @@ impl EntryFsm {
header.crc32
};

let entry_uncompressed_size =
self.entry.map(|e| e.uncompressed_size).unwrap_or_default();
let entry_uncompressed_size = self
.entry
.as_ref()
.map(|e| e.uncompressed_size)
.unwrap_or_default();
let expected_size = if entry_uncompressed_size != 0 {
entry_uncompressed_size
} else if let Some(descriptor) = descriptor.as_ref() {
Expand Down
5 changes: 2 additions & 3 deletions rc-zip/src/parse/central_directory_file_header.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use chrono::{offset::TimeZone, DateTime, Utc};
use tracing::trace;
use winnow::{
binary::{le_u16, le_u32},
Expand All @@ -13,11 +12,11 @@ use crate::{
error::{Error, FormatError},
parse::{
zero_datetime, Entry, ExtraField, ExtraFieldSettings, HostSystem, Mode, MsdosMode,
MsdosTimestamp, NtfsAttr, UnixMode, Version, ZipBytes, ZipString,
MsdosTimestamp, UnixMode, Version, ZipBytes, ZipString,
},
};

use super::{EntryCdFields, Method};
use super::Method;

/// 4.3.12 Central directory structure: File header
pub struct CentralDirectoryFileHeader {
Expand Down
12 changes: 3 additions & 9 deletions rc-zip/src/parse/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use winnow::{binary::le_u8, seq, PResult, Parser, Partial};
/// which features are required when reading a file.
///
/// For more information, see the [.ZIP Application Note](https://support.pkware.com/display/PKZIP/APPNOTE), section 4.4.2.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Clone, Copy)]
pub struct Version {
/// The host system on which
pub host_system: HostSystem,
Expand All @@ -20,21 +20,15 @@ pub struct Version {

impl fmt::Debug for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{:?} v{}.{}",
self.host_system(),
self.major(),
self.minor()
)
write!(f, "{:?} v{}", self.host_system, self.version)
}
}

impl Version {
/// Parse a version from a byte slice
pub fn parser(i: &mut Partial<&'_ [u8]>) -> PResult<Self> {
seq! {Self {
host_system: le_u8.map(HostSystem::from_u8),
host_system: le_u8.map(HostSystem::from),
version: le_u8,
}}
.parse_next(i)
Expand Down

0 comments on commit 33cda19

Please sign in to comment.