Skip to content

Commit

Permalink
chore: Continue to accept archives with invalid DateTime, and use `no…
Browse files Browse the repository at this point in the history
…w_utc()` as default only when writing, not reading
  • Loading branch information
Pr0methean committed May 23, 2024
1 parent 7a34aa5 commit b0c666a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,8 @@ fn central_header_to_zip_file_inner<R: Read>(
CompressionMethod::from_u16(compression_method)
},
compression_level: None,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)?,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)
.unwrap_or_else(|_| DateTime::default()),
crc32,
compressed_size: compressed_size as u64,
uncompressed_size: uncompressed_size as u64,
Expand Down Expand Up @@ -1396,7 +1397,8 @@ pub fn read_zipfile_from_stream<'a, R: Read>(reader: &'a mut R) -> ZipResult<Opt
using_data_descriptor: false,
compression_method,
compression_level: None,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)?,
last_modified_time: DateTime::try_from_msdos(last_mod_date, last_mod_time)
.unwrap_or_else(|_| DateTime::default()),
crc32,
compressed_size: compressed_size as u64,
uncompressed_size: uncompressed_size as u64,
Expand Down
16 changes: 16 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ pub struct DateTime {
second: u8,
}

impl DateTime {
/// Returns the current time if possible, otherwise the default of 1980-01-01.
#[cfg(feature = "time")]
pub fn default_for_write() -> Self {
OffsetDateTime::now_utc()
.try_into()
.unwrap_or_else(|_| DateTime::default())
}

/// Returns the current time if possible, otherwise the default of 1980-01-01.
#[cfg(not(feature = "time"))]
pub fn default_for_write() -> Self {
DateTime::default()
}
}

#[cfg(fuzzing)]
impl arbitrary::Arbitrary<'_> for DateTime {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {
Expand Down
8 changes: 1 addition & 7 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ use flate2::{write::DeflateEncoder, Compression};
#[cfg(feature = "bzip2")]
use bzip2::write::BzEncoder;

#[cfg(feature = "time")]
use time::OffsetDateTime;

#[cfg(feature = "deflate-zopfli")]
use zopfli::Options;

Expand Down Expand Up @@ -444,10 +441,7 @@ impl<'k, T: FileOptionExtension> Default for FileOptions<'k, T> {
Self {
compression_method: Default::default(),
compression_level: None,
#[cfg(feature = "time")]
last_modified_time: OffsetDateTime::now_utc().try_into().unwrap_or_default(),
#[cfg(not(feature = "time"))]
last_modified_time: DateTime::default(),
last_modified_time: DateTime::default_for_write(),
permissions: None,
large_file: false,
encrypt_with: None,
Expand Down

0 comments on commit b0c666a

Please sign in to comment.