#![forbid(unsafe_code)]
mod budget;
pub mod constants;
mod extra;
mod host;
mod index;
mod kind;
mod record;
mod wire;
pub mod write;
use std::io;
use thiserror::Error;
pub use budget::{Budget, BudgetError, LimitExceeded, Limits};
pub use extra::{ExtraFieldPolicy, ExtraHeaderId, ExtraHeaderIdSet};
pub use host::HostSystem;
pub use index::{
CentralDirectoryEntry, CommentPolicy, CommentRule, Entry, Index, IndexPolicy, IndexedEntry,
};
pub use kind::{DeviceNumbers, EntryKind};
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
#[repr(u16)]
pub enum CompressionMethod {
Stored = 0,
#[default]
Deflate = 8,
}
impl CompressionMethod {
pub(crate) fn parse(value: u16, position: u64) -> Result<Self, Error> {
match value {
value if value == Self::Stored as u16 => Ok(Self::Stored),
value if value == Self::Deflate as u16 => Ok(Self::Deflate),
_ => Err(Error::Unsupported {
position,
feature: "compression method",
}),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Error)]
#[non_exhaustive]
pub enum IndexPolicyViolation {
#[error("extra field {:#06x}", u16::from(*identifier))]
ExtraField {
identifier: ExtraHeaderId,
},
#[error("archive comment rejected by {rule:?}")]
ArchiveComment {
rule: CommentRule,
},
#[error("member comment rejected by {rule:?}")]
MemberComment {
rule: CommentRule,
},
#[error("redundant local ZIP64 sizes")]
RedundantZip64Sizes,
#[error("data descriptor for Stored member")]
StoredDataDescriptor,
#[error("empty DEFLATE stream")]
EmptyDeflateStream,
#[error("extraction version {}.{} is below the required {}.{}", declared / 10, declared % 10, minimum / 10, minimum % 10)]
IncompatibleExtractionVersion {
declared: u16,
minimum: u16,
},
#[error("symlink mode from a non-Unix creator system")]
AmbiguousSymlinkMode,
#[error("backslash in member name")]
BackslashInName,
}
#[derive(Debug, Error)]
pub enum Error {
#[error("ZIP I/O failed: {0}")]
Io(#[from] io::Error),
#[error("at byte {position}: invalid ZIP: {reason}")]
Invalid {
position: u64,
reason: &'static str,
},
#[error("at byte {position}: unsupported ZIP {feature}")]
Unsupported {
position: u64,
feature: &'static str,
},
#[error("at byte {position}: unsupported ZIP extra field {identifier:#06x}")]
UnsupportedExtraField {
position: u64,
identifier: u16,
},
#[error("at byte {position}: index policy rejected input: {violation}")]
PolicyViolation {
position: u64,
violation: IndexPolicyViolation,
},
#[error(transparent)]
Limit(#[from] LimitExceeded),
}
impl From<BudgetError> for Error {
fn from(error: BudgetError) -> Self {
match error {
BudgetError::Limit(limit) => Self::Limit(limit),
BudgetError::Overflow(usage) => invalid(usage, "offset or size overflow"),
}
}
}
pub(crate) fn invalid(position: u64, reason: &'static str) -> Error {
Error::Invalid { position, reason }
}
pub(crate) fn policy_violation(position: u64, violation: IndexPolicyViolation) -> Error {
Error::PolicyViolation {
position,
violation,
}
}
pub(crate) fn add(left: u64, right: u64) -> Result<u64, Error> {
left.checked_add(right)
.ok_or_else(|| invalid(left, "offset or size overflow"))
}