use std::ops::Range;
use super::{LayoutFault, WriteError};
pub(super) const LOCAL_SIGNATURE: u32 = 0x0403_4b50;
pub(super) const CENTRAL_SIGNATURE: u32 = 0x0201_4b50;
const END_SIGNATURE: u32 = 0x0605_4b50;
const ZIP64_LOCATOR_SIGNATURE: u32 = 0x0706_4b50;
const DESCRIPTOR_SIGNATURE: u32 = 0x0807_4b50;
pub(super) const LOCAL_HEADER: usize = 30;
pub(super) const CENTRAL_HEADER: usize = 46;
const END_RECORD: usize = 22;
const ZIP64_LOCATOR: usize = 20;
const ZIP64_EXTRA: u16 = 0x0001;
const ZIP64_MARK_32: u32 = u32::MAX;
const ZIP64_MARK_16: u16 = u16::MAX;
const DESCRIPTOR_FLAG: u16 = 1 << 3;
#[derive(Debug)]
pub(super) struct Layout {
pub entries: Vec<Entry>,
pub central: Range<usize>,
pub end: Range<usize>,
}
#[derive(Debug)]
pub(super) struct Entry {
pub record: Range<usize>,
pub name: Range<usize>,
pub local: Range<usize>,
pub local_extra: Range<usize>,
}
pub(super) fn u16_at(bytes: &[u8], at: usize) -> Option<u16> {
let field = bytes.get(at..at.checked_add(2)?)?;
Some(u16::from_le_bytes(field.try_into().ok()?))
}
pub(super) fn u32_at(bytes: &[u8], at: usize) -> Option<u32> {
let field = bytes.get(at..at.checked_add(4)?)?;
Some(u32::from_le_bytes(field.try_into().ok()?))
}
fn length_at(bytes: &[u8], at: usize) -> Result<usize, WriteError> {
u16_at(bytes, at)
.map(usize::from)
.ok_or(WriteError::Layout(LayoutFault::Truncated))
}
fn offset_at(bytes: &[u8], at: usize) -> Result<usize, WriteError> {
let value = u32_at(bytes, at).ok_or(WriteError::Layout(LayoutFault::Truncated))?;
if value == ZIP64_MARK_32 {
return Err(WriteError::Zip64);
}
usize::try_from(value).map_err(|_| WriteError::TooLarge)
}
pub(super) fn read(bytes: &[u8]) -> Result<Layout, WriteError> {
let end = end_record(bytes)?;
let field = |offset: usize| length_at(bytes, end.start + offset);
if field(4)? != 0 || field(6)? != 0 || field(8)? != field(10)? {
return Err(WriteError::Layout(LayoutFault::MultiDisk));
}
let locator = end.start.checked_sub(ZIP64_LOCATOR);
let zip64_locator = locator.and_then(|at| u32_at(bytes, at)) == Some(ZIP64_LOCATOR_SIGNATURE);
if zip64_locator || field(10)? == usize::from(ZIP64_MARK_16) {
return Err(WriteError::Zip64);
}
let size = offset_at(bytes, end.start + 12)?;
let start = offset_at(bytes, end.start + 16)?;
let central = start..start.checked_add(size).ok_or(WriteError::TooLarge)?;
if central.end > end.start {
return Err(WriteError::Layout(LayoutFault::CentralDirectory));
}
let mut entries = Vec::new();
let mut at = central.start;
for _ in 0..field(10)? {
let entry = central_record(bytes, at, ¢ral)?;
at = entry.record.end;
entries.push(entry);
}
if at != central.end {
return Err(WriteError::Layout(LayoutFault::CentralDirectory));
}
check_apart(&entries)?;
Ok(Layout {
entries,
central,
end,
})
}
fn end_record(bytes: &[u8]) -> Result<Range<usize>, WriteError> {
let last = bytes
.len()
.checked_sub(END_RECORD)
.ok_or(WriteError::Layout(LayoutFault::NoEndRecord))?;
let first = last.saturating_sub(usize::from(u16::MAX));
(first..=last)
.rev()
.find(|&at| {
u32_at(bytes, at) == Some(END_SIGNATURE)
&& u16_at(bytes, at + 20).map(usize::from) == Some(last - at)
})
.map(|at| at..bytes.len())
.ok_or(WriteError::Layout(LayoutFault::NoEndRecord))
}
fn central_record(bytes: &[u8], at: usize, central: &Range<usize>) -> Result<Entry, WriteError> {
if u32_at(bytes, at) != Some(CENTRAL_SIGNATURE) {
return Err(WriteError::Layout(LayoutFault::CentralDirectory));
}
let name = at + CENTRAL_HEADER..at + CENTRAL_HEADER + length_at(bytes, at + 28)?;
let extra = name.end..name.end + length_at(bytes, at + 30)?;
let record = at..extra.end + length_at(bytes, at + 32)?;
if record.end > central.end || length_at(bytes, at + 34)? != 0 {
return Err(WriteError::Layout(LayoutFault::CentralDirectory));
}
if has_zip64_field(bytes, &extra) || u32_at(bytes, at + 24) == Some(ZIP64_MARK_32) {
return Err(WriteError::Zip64);
}
let compressed = offset_at(bytes, at + 20)?;
let flags = u16_at(bytes, at + 8).ok_or(WriteError::Layout(LayoutFault::Truncated))?;
let (local, local_extra) = local_entry(bytes, offset_at(bytes, at + 42)?, (compressed, flags))?;
if local.end > central.start {
return Err(WriteError::Layout(LayoutFault::LocalHeader));
}
Ok(Entry {
record,
name,
local,
local_extra,
})
}
fn local_entry(
bytes: &[u8],
at: usize,
(compressed, flags): (usize, u16),
) -> Result<(Range<usize>, Range<usize>), WriteError> {
if u32_at(bytes, at) != Some(LOCAL_SIGNATURE) {
return Err(WriteError::Layout(LayoutFault::LocalHeader));
}
let name_end = at + LOCAL_HEADER + length_at(bytes, at + 26)?;
let extra = name_end..name_end + length_at(bytes, at + 28)?;
if has_zip64_field(bytes, &extra) {
return Err(WriteError::Zip64);
}
let data_end = extra
.end
.checked_add(compressed)
.ok_or(WriteError::TooLarge)?;
let descriptor = if flags & DESCRIPTOR_FLAG == 0 {
0
} else if u32_at(bytes, data_end) == Some(DESCRIPTOR_SIGNATURE) {
16
} else {
12
};
let end = data_end + descriptor;
if end > bytes.len() {
return Err(WriteError::Layout(LayoutFault::LocalHeader));
}
Ok((at..end, extra))
}
fn has_zip64_field(bytes: &[u8], extra: &Range<usize>) -> bool {
let mut at = extra.start;
while at + 4 <= extra.end {
let (Some(id), Some(size)) = (u16_at(bytes, at), u16_at(bytes, at + 2)) else {
return false;
};
if id == ZIP64_EXTRA {
return true;
}
at += 4 + usize::from(size);
}
false
}
fn check_apart(entries: &[Entry]) -> Result<(), WriteError> {
let mut locals: Vec<&Range<usize>> = entries.iter().map(|entry| &entry.local).collect();
locals.sort_by_key(|local| local.start);
if locals.windows(2).any(|pair| match pair {
[before, after] => after.start < before.end,
_ => false,
}) {
return Err(WriteError::Layout(LayoutFault::Overlap));
}
Ok(())
}