use cfg_if::cfg_if;
use num_enum::{FromPrimitive, IntoPrimitive};
use std::convert::TryInto;
use std::ops::{Range, RangeInclusive};
use std::path;
#[cfg(doc)]
use {crate::read::ZipFile, crate::write::FileOptions};
mod ffi {
pub const S_IFDIR: u32 = 0o0040000;
pub const S_IFREG: u32 = 0o0100000;
}
cfg_if! {
if #[cfg(any(
all(target_arch = "arm", target_pointer_width = "32"),
target_arch = "mips",
target_arch = "powerpc"
))] {
mod atomic {
use crossbeam_utils::sync::ShardedLock;
pub use std::sync::atomic::Ordering;
#[derive(Debug, Default)]
pub struct AtomicU64 {
value: ShardedLock<u64>,
}
impl AtomicU64 {
pub fn new(v: u64) -> Self {
Self {
value: ShardedLock::new(v),
}
}
pub fn get_mut(&mut self) -> &mut u64 {
self.value.get_mut().unwrap()
}
pub fn load(&self, _: Ordering) -> u64 {
*self.value.read().unwrap()
}
pub fn store(&self, value: u64, _: Ordering) {
*self.value.write().unwrap() = value;
}
}
}
} else {
use std::sync::atomic;
}
}
cfg_if! {
if #[cfg(feature = "time")] {
use crate::result::DateTimeRangeError;
use time::{
Date,
Month,
OffsetDateTime,
PrimitiveDateTime,
Time,
UtcOffset,
error::ComponentRange,
};
} else {
use std::time::SystemTime;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum System {
Dos = 0,
Unix = 3,
#[num_enum(default)]
Unknown,
}
#[derive(Debug, Clone, Copy)]
pub struct DateTime {
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
}
impl ::std::default::Default for DateTime {
fn default() -> DateTime {
Self::zero()
}
}
impl DateTime {
pub const fn zero() -> Self {
Self {
year: 1980,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0,
}
}
pub const YEAR_RANGE: RangeInclusive<u16> = 1980..=2107;
pub const MONTH_RANGE: RangeInclusive<u8> = 1..=12;
pub const DAY_RANGE: RangeInclusive<u8> = 1..=31;
pub const HOUR_RANGE: Range<u8> = 0..24;
pub const MINUTE_RANGE: Range<u8> = 0..60;
pub const SECOND_RANGE: RangeInclusive<u8> = 0..=60;
fn check_year(year: u16) -> Result<(), DateTimeRangeError> {
if Self::YEAR_RANGE.contains(&year) {
Ok(())
} else {
Err(DateTimeRangeError::InvalidYear(year, Self::YEAR_RANGE))
}
}
fn check_month(month: u8) -> Result<(), DateTimeRangeError> {
if Self::MONTH_RANGE.contains(&month) {
Ok(())
} else {
Err(DateTimeRangeError::InvalidMonth(month, Self::MONTH_RANGE))
}
}
fn check_day(day: u8) -> Result<(), DateTimeRangeError> {
if Self::DAY_RANGE.contains(&day) {
Ok(())
} else {
Err(DateTimeRangeError::InvalidDay(day, Self::DAY_RANGE))
}
}
fn check_hour(hour: u8) -> Result<(), DateTimeRangeError> {
if Self::HOUR_RANGE.contains(&hour) {
Ok(())
} else {
Err(DateTimeRangeError::InvalidHour(hour, Self::HOUR_RANGE))
}
}
fn check_minute(minute: u8) -> Result<(), DateTimeRangeError> {
if Self::MINUTE_RANGE.contains(&minute) {
Ok(())
} else {
Err(DateTimeRangeError::InvalidMinute(
minute,
Self::MINUTE_RANGE,
))
}
}
fn check_second(second: u8) -> Result<(), DateTimeRangeError> {
if Self::SECOND_RANGE.contains(&second) {
Ok(())
} else {
Err(DateTimeRangeError::InvalidSecond(
second,
Self::SECOND_RANGE,
))
}
}
pub fn from_msdos(datepart: u16, timepart: u16) -> DateTime {
let seconds = (timepart & 0b0000000000011111) << 1;
let minutes = (timepart & 0b0000011111100000) >> 5;
let hours = (timepart & 0b1111100000000000) >> 11;
let days = datepart & 0b0000000000011111;
let months = (datepart & 0b0000000111100000) >> 5;
let years = (datepart & 0b1111111000000000) >> 9;
DateTime {
year: years + 1980,
month: months as u8,
day: days as u8,
hour: hours as u8,
minute: minutes as u8,
second: seconds as u8,
}
}
pub fn from_date_and_time(
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Result<DateTime, DateTimeRangeError> {
Self::check_year(year)?;
Self::check_month(month)?;
Self::check_day(day)?;
Self::check_hour(hour)?;
Self::check_minute(minute)?;
Self::check_second(second)?;
Ok(Self {
year,
month,
day,
hour,
minute,
second,
})
}
pub fn timepart(&self) -> u16 {
((self.second as u16) >> 1) | ((self.minute as u16) << 5) | ((self.hour as u16) << 11)
}
pub fn datepart(&self) -> u16 {
(self.day as u16) | ((self.month as u16) << 5) | ((self.year - 1980) << 9)
}
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub fn to_time(&self, offset: UtcOffset) -> Result<OffsetDateTime, ComponentRange> {
let date =
Date::from_calendar_date(self.year as i32, Month::try_from(self.month)?, self.day)?;
let time = Time::from_hms(self.hour, self.minute, self.second)?;
Ok(PrimitiveDateTime::new(date, time).assume_offset(offset))
}
pub fn year(&self) -> u16 {
self.year
}
pub fn month(&self) -> u8 {
self.month
}
pub fn day(&self) -> u8 {
self.day
}
pub fn hour(&self) -> u8 {
self.hour
}
pub fn minute(&self) -> u8 {
self.minute
}
pub fn second(&self) -> u8 {
self.second
}
}
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
impl TryFrom<OffsetDateTime> for DateTime {
type Error = DateTimeRangeError;
fn try_from(dt: OffsetDateTime) -> Result<Self, Self::Error> {
let year: u16 = dt
.year()
.try_into()
.map_err(|e| DateTimeRangeError::NumericConversion("year", e))?;
let month: u8 = dt.month().into();
let day: u8 = dt.day().into();
let hour: u8 = dt.hour().into();
let minute: u8 = dt.minute().into();
let second: u8 = dt.second().into();
Self::from_date_and_time(year, month, day, hour, minute, second)
}
}
pub const DEFAULT_VERSION: u8 = 46;
#[derive(Debug)]
pub struct AtomicU64(atomic::AtomicU64);
impl AtomicU64 {
pub fn new(v: u64) -> Self {
Self(atomic::AtomicU64::new(v))
}
pub fn load(&self) -> u64 {
self.0.load(atomic::Ordering::Relaxed)
}
pub fn store(&self, val: u64) {
self.0.store(val, atomic::Ordering::Relaxed)
}
pub fn get_mut(&mut self) -> &mut u64 {
self.0.get_mut()
}
}
impl Clone for AtomicU64 {
fn clone(&self) -> Self {
Self(atomic::AtomicU64::new(self.load()))
}
}
#[derive(Debug, Clone)]
pub struct ZipFileData {
pub system: System,
pub version_made_by: u8,
pub encrypted: bool,
pub using_data_descriptor: bool,
pub compression_method: crate::compression::CompressionMethod,
pub compression_level: Option<i32>,
pub last_modified_time: DateTime,
pub crc32: u32,
pub compressed_size: u64,
pub uncompressed_size: u64,
pub file_name: String,
pub file_name_raw: Vec<u8>,
pub extra_field: Vec<u8>,
pub file_comment: String,
pub header_start: u64,
pub central_header_start: u64,
pub data_start: AtomicU64,
pub external_attributes: u32,
pub large_file: bool,
pub aes_mode: Option<(AesMode, AesVendorVersion)>,
}
impl ZipFileData {
pub fn file_name_sanitized(&self) -> ::std::path::PathBuf {
let no_null_filename = match self.file_name.find('\0') {
Some(index) => &self.file_name[0..index],
None => &self.file_name,
}
.to_string();
let separator = ::std::path::MAIN_SEPARATOR;
let opposite_separator = match separator {
'/' => '\\',
_ => '/',
};
let filename =
no_null_filename.replace(&opposite_separator.to_string(), &separator.to_string());
::std::path::Path::new(&filename)
.components()
.filter(|component| matches!(*component, ::std::path::Component::Normal(..)))
.fold(::std::path::PathBuf::new(), |mut path, ref cur| {
path.push(cur.as_os_str());
path
})
}
pub(crate) fn enclosed_name(&self) -> Option<&path::Path> {
if self.file_name.contains('\0') {
return None;
}
let path = path::Path::new(&self.file_name);
let mut depth = 0usize;
for component in path.components() {
match component {
path::Component::Prefix(_) | path::Component::RootDir => return None,
path::Component::ParentDir => depth = depth.checked_sub(1)?,
path::Component::Normal(_) => depth += 1,
path::Component::CurDir => (),
}
}
Some(path)
}
pub(crate) fn unix_mode(&self) -> Option<u32> {
if self.external_attributes == 0 {
return None;
}
match self.system {
System::Unix => Some(self.external_attributes >> 16),
System::Dos => {
let mut mode = if 0x10 == (self.external_attributes & 0x10) {
ffi::S_IFDIR | 0o0775
} else {
ffi::S_IFREG | 0o0664
};
if 0x01 == (self.external_attributes & 0x01) {
mode &= 0o0555;
}
Some(mode)
}
_ => None,
}
}
pub fn zip64_extension(&self) -> bool {
self.uncompressed_size > 0xFFFFFFFF
|| self.compressed_size > 0xFFFFFFFF
|| self.header_start > 0xFFFFFFFF
}
pub fn version_needed(&self) -> u16 {
match (self.zip64_extension(), self.compression_method) {
#[cfg(feature = "bzip2")]
(_, crate::compression::CompressionMethod::Bzip2) => 46,
(true, _) => 45,
_ => 20,
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum AesVendorVersion {
Ae1,
Ae2,
}
#[derive(Copy, Clone, Debug)]
pub enum AesMode {
Aes128,
Aes192,
Aes256,
}
#[cfg(feature = "aes-crypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "aes-crypto")))]
impl AesMode {
pub fn salt_length(&self) -> usize {
self.key_length() / 2
}
pub fn key_length(&self) -> usize {
match self {
Self::Aes128 => 16,
Self::Aes192 => 24,
Self::Aes256 => 32,
}
}
}
#[cfg(test)]
mod test {
#[test]
fn system() {
use super::System;
assert_eq!(u8::from(System::Dos), 0u8);
assert_eq!(System::Dos as u8, 0u8);
assert_eq!(System::Unix as u8, 3u8);
assert_eq!(u8::from(System::Unix), 3u8);
assert_eq!(System::from(0), System::Dos);
assert_eq!(System::from(3), System::Unix);
assert_eq!(u8::from(System::Unknown), 4u8);
assert_eq!(System::Unknown as u8, 4u8);
}
#[test]
fn sanitize() {
use super::*;
let file_name = "/path/../../../../etc/./passwd\0/etc/shadow".to_string();
let data = ZipFileData {
system: System::Dos,
version_made_by: 0,
encrypted: false,
using_data_descriptor: false,
compression_method: crate::compression::CompressionMethod::Stored,
compression_level: None,
last_modified_time: DateTime::default(),
crc32: 0,
compressed_size: 0,
uncompressed_size: 0,
file_name: file_name.clone(),
file_name_raw: file_name.into_bytes(),
extra_field: Vec::new(),
file_comment: String::new(),
header_start: 0,
data_start: AtomicU64::new(0),
central_header_start: 0,
external_attributes: 0,
large_file: false,
aes_mode: None,
};
assert_eq!(
data.file_name_sanitized(),
::std::path::PathBuf::from("path/etc/passwd")
);
}
#[test]
#[allow(clippy::unusual_byte_groupings)]
fn datetime_default() {
use super::DateTime;
let dt = DateTime::default();
assert_eq!(dt.timepart(), 0);
assert_eq!(dt.datepart(), 0b0000000_0001_00001);
}
#[test]
#[allow(clippy::unusual_byte_groupings)]
fn datetime_max() {
use super::DateTime;
let dt = DateTime::from_date_and_time(2107, 12, 31, 23, 59, 60).unwrap();
assert_eq!(dt.timepart(), 0b10111_111011_11110);
assert_eq!(dt.datepart(), 0b1111111_1100_11111);
}
#[test]
fn datetime_bounds() {
use super::DateTime;
assert!(DateTime::from_date_and_time(2000, 1, 1, 23, 59, 60).is_ok());
assert!(DateTime::from_date_and_time(2000, 1, 1, 24, 0, 0).is_err());
assert!(DateTime::from_date_and_time(2000, 1, 1, 0, 60, 0).is_err());
assert!(DateTime::from_date_and_time(2000, 1, 1, 0, 0, 61).is_err());
assert!(DateTime::from_date_and_time(2107, 12, 31, 0, 0, 0).is_ok());
assert!(DateTime::from_date_and_time(1980, 1, 1, 0, 0, 0).is_ok());
assert!(DateTime::from_date_and_time(1979, 1, 1, 0, 0, 0).is_err());
assert!(DateTime::from_date_and_time(1980, 0, 1, 0, 0, 0).is_err());
assert!(DateTime::from_date_and_time(1980, 1, 0, 0, 0, 0).is_err());
assert!(DateTime::from_date_and_time(2108, 12, 31, 0, 0, 0).is_err());
assert!(DateTime::from_date_and_time(2107, 13, 31, 0, 0, 0).is_err());
assert!(DateTime::from_date_and_time(2107, 12, 32, 0, 0, 0).is_err());
}
#[cfg(feature = "time")]
use time::{format_description::well_known::Rfc3339, OffsetDateTime, UtcOffset};
#[cfg(feature = "time")]
#[test]
fn datetime_try_from_bounds() {
use std::convert::TryFrom;
use super::DateTime;
use time::macros::datetime;
assert!(DateTime::try_from(datetime!(1979-12-31 23:59:59 UTC)).is_err());
assert!(DateTime::try_from(datetime!(1980-01-01 00:00:00 UTC)).is_ok());
assert!(DateTime::try_from(datetime!(2107-12-31 23:59:59 UTC)).is_ok());
assert!(DateTime::try_from(datetime!(2108-01-01 00:00:00 UTC)).is_err());
}
#[test]
fn time_conversion() {
use super::DateTime;
let dt = DateTime::from_msdos(0x4D71, 0x54CF);
assert_eq!(dt.year(), 2018);
assert_eq!(dt.month(), 11);
assert_eq!(dt.day(), 17);
assert_eq!(dt.hour(), 10);
assert_eq!(dt.minute(), 38);
assert_eq!(dt.second(), 30);
#[cfg(feature = "time")]
assert_eq!(
dt.to_time(UtcOffset::UTC)
.unwrap()
.format(&Rfc3339)
.unwrap(),
"2018-11-17T10:38:30Z"
);
}
#[test]
fn time_out_of_bounds() {
use super::DateTime;
let dt = DateTime::from_msdos(0xFFFF, 0xFFFF);
assert_eq!(dt.year(), 2107);
assert_eq!(dt.month(), 15);
assert_eq!(dt.day(), 31);
assert_eq!(dt.hour(), 31);
assert_eq!(dt.minute(), 63);
assert_eq!(dt.second(), 62);
#[cfg(feature = "time")]
assert!(dt.to_time(UtcOffset::UTC).is_err());
let dt = DateTime::from_msdos(0x0000, 0x0000);
assert_eq!(dt.year(), 1980);
assert_eq!(dt.month(), 0);
assert_eq!(dt.day(), 0);
assert_eq!(dt.hour(), 0);
assert_eq!(dt.minute(), 0);
assert_eq!(dt.second(), 0);
#[cfg(feature = "time")]
assert!(dt.to_time(UtcOffset::UTC).is_err());
}
#[cfg(feature = "time")]
#[test]
fn time_at_january() {
use super::DateTime;
use std::convert::TryFrom;
let clock = OffsetDateTime::from_unix_timestamp(1_577_836_800).unwrap();
assert!(DateTime::try_from(clock).is_ok());
}
}