use crate::{
CompressionMethod, Error, ExtraHeaderId, HostSystem, add,
constants::{extra, signature, size, version},
invalid,
kind::{ExternalAttributes, UnixFileType},
record::{
Common, GeneralPurposeFlags, MemberHeader, MemberPayload, PayloadFields, SizeField,
validate_name,
},
};
const VERSION_MADE_BY: u16 = ((HostSystem::Unix.to_wire() as u16) << 8) | version::ZIP64;
const DEFAULT_TIME: u16 = 0;
const DEFAULT_DATE: u16 = 0x0021;
#[derive(Clone, Copy, Debug)]
pub enum EntryKind {
File { executable: bool },
Directory,
SymbolicLink,
}
pub struct PendingMember<'a> {
path: &'a str,
method: CompressionMethod,
kind: EntryKind,
}
impl<'a> PendingMember<'a> {
pub fn new(path: &'a str, method: CompressionMethod, kind: EntryKind) -> Result<Self, Error> {
let member = Self { path, method, kind };
if path.is_empty() || member.central_header_size() > usize::from(u16::MAX) {
return Err(invalid(0, "empty or oversized filename"));
}
validate_name(path, 0)?;
if path.contains('\\') {
return Err(invalid(0, "invalid ZIP filename"));
}
if path.ends_with('/') != matches!(kind, EntryKind::Directory) {
return Err(invalid(0, "filename suffix disagrees with member kind"));
}
if matches!(kind, EntryKind::Directory) && method != CompressionMethod::Stored {
return Err(invalid(0, "directories must be stored without file data"));
}
Ok(member)
}
pub fn method(&self) -> CompressionMethod {
self.method
}
pub fn local_header_size(&self) -> usize {
size::LOCAL + extra::HEADER_SIZE + extra::ZIP64_LOCAL_SIZE + self.path.len()
}
pub fn central_header_size(&self) -> usize {
size::CENTRAL + extra::HEADER_SIZE + extra::ZIP64_CENTRAL_SIZE + self.path.len()
}
pub fn finish(
self,
crc: u32,
compressed: u64,
uncompressed: u64,
offset: u64,
) -> Result<CompletedMember<'a>, Error> {
let payload = PayloadFields {
crc,
compressed,
uncompressed,
}
.validate(self.method, offset)?;
if payload.has_empty_deflate_stream() {
return Err(invalid(offset, "empty member has file data"));
}
if matches!(self.kind, EntryKind::Directory) && uncompressed != 0 {
return Err(invalid(offset, "directory has file data"));
}
if matches!(self.kind, EntryKind::SymbolicLink) && uncompressed == 0 {
return Err(invalid(offset, "empty symbolic-link target"));
}
Ok(CompletedMember {
header: self,
payload,
offset,
})
}
}
pub struct CompletedMember<'a> {
header: PendingMember<'a>,
payload: MemberPayload,
offset: u64,
}
impl CompletedMember<'_> {
pub fn local_header(&self) -> Vec<u8> {
let mut bytes = Vec::with_capacity(self.header.local_header_size());
push32(&mut bytes, signature::LOCAL);
self.append_common(&mut bytes, extra::ZIP64_LOCAL_SIZE);
self.append_name_and_sizes(&mut bytes, extra::ZIP64_LOCAL_SIZE);
bytes
}
pub fn central_header(&self) -> Vec<u8> {
let mut bytes = Vec::new();
self.append_central_header(&mut bytes);
bytes
}
pub fn append_central_header(&self, bytes: &mut Vec<u8>) {
bytes.reserve(self.header.central_header_size());
push32(bytes, signature::CENTRAL);
push16(bytes, VERSION_MADE_BY);
self.append_common(bytes, extra::ZIP64_CENTRAL_SIZE);
push16(bytes, 0); push16(bytes, 0); push16(bytes, 0);
let (file_type, permissions) = match self.header.kind {
EntryKind::File { executable: true } => (UnixFileType::Regular, 0o755),
EntryKind::File { executable: false } => (UnixFileType::Regular, 0o644),
EntryKind::Directory => (UnixFileType::Directory, 0o755),
EntryKind::SymbolicLink => (UnixFileType::SymbolicLink, 0o777),
};
let dos = if matches!(self.header.kind, EntryKind::Directory) {
ExternalAttributes::DOS_DIRECTORY
} else {
0
};
push32(
bytes,
(u32::from(u16::from(file_type) | permissions) << ExternalAttributes::UNIX_MODE_SHIFT)
| dos,
);
push32(bytes, u32::MAX);
self.append_name_and_sizes(bytes, extra::ZIP64_CENTRAL_SIZE);
push64(bytes, self.offset);
}
fn append_name_and_sizes(&self, bytes: &mut Vec<u8>, zip64_size: usize) {
bytes.extend_from_slice(self.header.path.as_bytes());
push16(bytes, u16::from(ExtraHeaderId::Zip64));
push16(bytes, zip64_size as u16);
push64(bytes, self.payload.fields().uncompressed);
push64(bytes, self.payload.fields().compressed);
}
fn append_common(&self, bytes: &mut Vec<u8>, zip64_size: usize) {
let header = MemberHeader {
version: version::ZIP64,
common: Common {
flags: GeneralPurposeFlags::UTF8,
method: self.header.method,
},
time: DEFAULT_TIME,
date: DEFAULT_DATE,
payload: PayloadFields {
crc: self.payload.fields().crc,
compressed: SizeField::Zip64,
uncompressed: SizeField::Zip64,
},
};
bytes.extend_from_slice(&header.to_bytes());
push16(bytes, self.header.path.len() as u16);
push16(bytes, (extra::HEADER_SIZE + zip64_size) as u16);
}
}
pub fn end_records(count: u64, offset: u64, size: u64) -> Result<Vec<u8>, Error> {
let position = add(offset, size)?;
let mut bytes = Vec::with_capacity(size::ZIP64_END + size::ZIP64_LOCATOR + size::END);
push32(&mut bytes, signature::ZIP64_END);
push64(&mut bytes, size::ZIP64_END_BODY as u64);
push16(&mut bytes, VERSION_MADE_BY);
push16(&mut bytes, version::ZIP64);
push32(&mut bytes, 0); push32(&mut bytes, 0); push64(&mut bytes, count); push64(&mut bytes, count); push64(&mut bytes, size);
push64(&mut bytes, offset);
push32(&mut bytes, signature::ZIP64_LOCATOR);
push32(&mut bytes, 0);
push64(&mut bytes, position);
push32(&mut bytes, 1);
push32(&mut bytes, signature::END);
push16(&mut bytes, 0);
push16(&mut bytes, 0);
push16(&mut bytes, u16::MAX);
push16(&mut bytes, u16::MAX);
push32(&mut bytes, u32::MAX);
push32(&mut bytes, u32::MAX);
push16(&mut bytes, 0);
Ok(bytes)
}
fn push16(bytes: &mut Vec<u8>, value: u16) {
bytes.extend_from_slice(&value.to_le_bytes());
}
fn push32(bytes: &mut Vec<u8>, value: u32) {
bytes.extend_from_slice(&value.to_le_bytes());
}
fn push64(bytes: &mut Vec<u8>, value: u64) {
bytes.extend_from_slice(&value.to_le_bytes());
}