feat(xfl): use shared bounded FLA package reader (M4)

This commit is contained in:
Dylan M. Taylor committed 2026-11-13 22:53:03 +00:00
1 parent daeb2f680a
commit 6ee60c4464
9 files changed
+48 -692

No files matched your search

Generated
+10
View File
@@ -394,6 +394,7 @@ dependencies = [
"anicraft-geom",
"anicraft-text",
"anicraft-xml",
"craft-xfl",
"miniz_oxide 0.8.9",
"png 0.17.16",
"thiserror 2.0.21",
@@ -1187,6 +1188,15 @@ dependencies = [
"zip",
]
[[package]]
name = "craft-xfl"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=92afe4039623ef71d5f155f8cfcb76acad7ac542#92afe4039623ef71d5f155f8cfcb76acad7ac542"
dependencies = [
"miniz_oxide 0.8.9",
"thiserror 2.0.21",
]
[[package]]
name = "crc32fast"
version = "1.5.3"
+1
View File
@@ -26,6 +26,7 @@ anicraft-text = { path = "crates/text" }
anicraft-plugins = { path = "crates/plugins" }
anicraft-audio = { path = "crates/audio" }
anicraft-gpu = { path = "crates/gpu" }
craft-xfl = { git = "https://github.com/storytold/craft-libs", rev = "92afe4039623ef71d5f155f8cfcb76acad7ac542" }
anicraft-guide = { path = "crates/guide" }
# The GPU stage (ADR 0019) uses the wgpu that eframe's egui-wgpu brings, with the same
# (default) features, so the tree keeps one wgpu build.
+3 -3
View File
@@ -13,12 +13,12 @@ anicraft-doc.workspace = true
anicraft-xml.workspace = true
# Lays imported text out with the fonts on this computer (ADR 0012, ADR 0014).
anicraft-text.workspace = true
# Inflate for .fla (zip) archives; already in the tree through png (MIT OR Zlib OR Apache-2.0).
miniz_oxide = "0.8"
craft-xfl.workspace = true
thiserror.workspace = true
[dev-dependencies]
miniz_oxide = "0.8"
png.workspace = true
[lints]
workspace = true
workspace = true
+14 -1
View File
@@ -42,7 +42,7 @@ struct RawHint {
pub enum XflError {
#[error("{0}")]
Xml(#[from] xml::XmlError),
#[error("{0}")]
#[error("{}", ani_zip_error(.0))]
Zip(#[from] crate::zip::ZipError),
#[error("I/O: {0}")]
Io(#[from] std::io::Error),
@@ -50,6 +50,19 @@ pub enum XflError {
NotXfl(String),
}
/// Keep AniCraft's established import diagnostics while the shared XFL reader stays neutral.
fn ani_zip_error(error: &crate::zip::ZipError) -> String {
match error {
crate::zip::ZipError::UnsupportedArchive(feature) => {
format!("the archive uses a zip feature AniCraft can't read ({feature})")
}
crate::zip::ZipError::Unsupported(name, feature) => {
format!("'{name}' uses a zip feature AniCraft can't read ({feature})")
}
other => other.to_string(),
}
}
/// What the import couldn't bring across, by kind ("3 bitmaps"), the scripts it brought that
/// run and those it kept without running, and the fonts it couldn't find.
#[derive(Debug, Default, Clone, PartialEq)]
+10 -1
View File
@@ -11,10 +11,19 @@ pub mod import;
pub mod text;
/// The shared XML reader (`anicraft-xml`), under its old path.
pub use anicraft_xml as xml;
pub mod zip;
/// XFL/FLA ZIP package reader, shared with other consumers through `craft-xfl`.
pub mod zip {
pub use craft_xfl::{Archive, MAX_DEPTH, MAX_ENTRIES, MAX_ENTRY, MAX_RATIO, MAX_TOTAL, ZipError};
#[cfg(test)]
pub(crate) use crate::pack;
}
pub use import::{MAX_FLA_BYTES, Report, XflError, import_fla, import_fla_with_fonts, import_path, import_with, import_with_fonts};
#[cfg(test)]
#[path = "../tests/support/fla.rs"]
pub(crate) mod pack;
#[cfg(test)]
mod tests;
#[cfg(test)]
+8
View File
@@ -4,6 +4,14 @@ use crate::import::{XflError, import_with};
use anicraft_doc::{ElementKind, LayerKind, LoopMode, Paint, Rgba, SymbolKind, evaluate};
use std::collections::HashMap;
#[test]
fn shared_zip_errors_keep_anicraft_import_wording() {
let entry = XflError::from(crate::zip::ZipError::Unsupported("a.xml".into(), "encryption"));
assert_eq!(entry.to_string(), "'a.xml' uses a zip feature AniCraft can't read (encryption)");
let archive = XflError::from(crate::zip::ZipError::UnsupportedArchive("split across several files"));
assert_eq!(archive.to_string(), "the archive uses a zip feature AniCraft can't read (split across several files)");
}
const DOC: &str = r##"<DOMDocument xmlns="http://ns.adobe.com/xfl/2008/" width="640" height="360" frameRate="30" backgroundColor="#336699">
<symbols><Include href="Characters/Ball.xml"/><Include href="Missing.xml"/></symbols>
<timelines>
-401
View File
@@ -1,401 +0,0 @@
//! Reading files out of a `.fla` (a zip archive of an XFL folder), ADR 0009 and ADR 0054.
//!
//! Implements the parts of the published ZIP format (PKWARE APPNOTE 6.3) that archive tools
//! write: the end-of-central-directory record (and its ZIP64 form), central directory entries
//! with ZIP64 sizes, local headers, data descriptors, "stored" and "deflate" entries (inflated
//! by `miniz_oxide`), UTF-8 names (flag bit 11 or the Info-ZIP Unicode path field) and CP437
//! names. Encryption, other compression methods and split archives are refused with a clear
//! error.
//!
//! The archive is treated as hostile. Every offset and size is checked against the bytes it
//! comes from; entries may not overlap each other or the central directory; a name that would
//! leave the package (`..`, absolute or drive paths), is listed twice or is nested deeper than
//! [`MAX_DEPTH`] refuses the archive; an entry may not claim more than deflate can produce from
//! its bytes ([`MAX_RATIO`]), more than [`MAX_ENTRY`], or a size or checksum it doesn't have;
//! and the archive hands out at most [`MAX_TOTAL`] bytes in all. Nothing is ever written to
//! disk. Never panics.
use std::cell::Cell;
use std::collections::HashMap;
/// Largest single entry inflated (bytes).
pub const MAX_ENTRY: usize = 256 * 1024 * 1024;
/// Most bytes one archive hands out, over every read (the import's memory budget).
pub const MAX_TOTAL: u64 = 1024 * 1024 * 1024;
/// Most entries an archive may list (files and folders).
pub const MAX_ENTRIES: usize = 100_000;
/// Most folders a path may be inside (Animate's library nests a few deep).
pub const MAX_DEPTH: usize = 32;
/// Largest size-to-compressed-size ratio believed: deflate itself can't pass about 1032 : 1,
/// so an entry claiming more is a zip bomb or a damaged size, refused before inflating.
pub const MAX_RATIO: u64 = 1024;
#[derive(Debug, Clone, thiserror::Error, PartialEq)]
pub enum ZipError {
#[error("not a zip archive (no end-of-directory record)")]
NotZip,
#[error("the archive is damaged: {0}")]
Damaged(&'static str),
#[error("the archive is damaged: '{0}' {1}")]
DamagedEntry(String, &'static str),
#[error("the archive uses a zip feature AniCraft can't read ({0})")]
UnsupportedArchive(&'static str),
#[error("'{0}' uses a zip feature AniCraft can't read ({1})")]
Unsupported(String, &'static str),
#[error("'{0}' is too large to read")]
TooLarge(String),
#[error("the archive asks for more than {} MB in all", MAX_TOTAL / (1024 * 1024))]
TooMuch,
#[error("the archive lists more than {MAX_ENTRIES} files")]
TooMany,
#[error("'{0}' claims to expand more than compression allows (a zip bomb or a damaged archive)")]
Bomb(String),
#[error("'{0}' and '{1}' share bytes in the archive (a zip bomb or a damaged archive)")]
Overlap(String, String),
#[error("'{0}' points outside the document")]
Unsafe(String),
#[error("'{0}' is in the archive twice")]
Duplicate(String),
#[error("'{0}' is inside more than {MAX_DEPTH} folders")]
TooDeep(String),
}
#[derive(Debug, Clone)]
struct Entry {
method: u16,
flags: u16,
crc: u32,
comp: usize,
size: usize,
/// Where its (compressed) bytes start.
data: usize,
}
pub struct Archive<'a> {
data: &'a [u8],
entries: HashMap<String, Entry>,
folders: usize,
/// Bytes still to hand out ([`MAX_TOTAL`] at first).
budget: Cell<u64>,
}
const END: u32 = 0x0605_4b50;
const END64: u32 = 0x0606_4b50;
const LOCATOR64: u32 = 0x0706_4b50;
const CENTRAL: u32 = 0x0201_4b50;
const LOCAL: u32 = 0x0403_4b50;
fn get(d: &[u8], at: usize, len: usize) -> Option<&[u8]> {
d.get(at..at.checked_add(len)?)
}
fn u16_at(d: &[u8], i: usize) -> Option<u16> {
Some(u16::from_le_bytes(get(d, i, 2)?.try_into().ok()?))
}
fn u32_at(d: &[u8], i: usize) -> Option<u32> {
Some(u32::from_le_bytes(get(d, i, 4)?.try_into().ok()?))
}
fn u64_at(d: &[u8], i: usize) -> Option<u64> {
Some(u64::from_le_bytes(get(d, i, 8)?.try_into().ok()?))
}
/// The body of the extra field `id`, if `extra` has one.
fn extra_field(mut extra: &[u8], id: u16) -> Option<&[u8]> {
while extra.len() >= 4 {
let (tag, len) = (u16_at(extra, 0)?, u16_at(extra, 2)? as usize);
let body = get(extra, 4, len)?;
if tag == id {
return Some(body);
}
extra = extra.get(4 + len..)?;
}
None
}
/// CRC-32 tables for eight bytes at a time ("slicing by 8"): `TABLES[0]` is the classic
/// byte table, `TABLES[k][b]` the checksum of byte `b` followed by `k` zero bytes.
const TABLES: [[u32; 256]; 8] = {
let mut t = [[0u32; 256]; 8];
let mut i = 0;
while i < 256 {
let mut c = i as u32;
let mut k = 0;
while k < 8 {
c = if c & 1 != 0 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 };
k += 1;
}
t[0][i] = c;
i += 1;
}
let mut k = 1;
while k < 8 {
let mut i = 0;
while i < 256 {
let prev = t[k - 1][i];
t[k][i] = (prev >> 8) ^ t[0][(prev & 0xff) as usize];
i += 1;
}
k += 1;
}
t
};
/// CRC-32 (ISO-HDLC, the zip checksum), eight bytes at a time.
pub fn crc32(data: &[u8]) -> u32 {
let t = &TABLES;
let mut c = !0u32;
let (blocks, rest) = data.as_chunks::<8>();
for b in blocks {
let lo = c ^ u32::from_le_bytes([b[0], b[1], b[2], b[3]]);
c = t[7][(lo & 0xff) as usize] ^ t[6][((lo >> 8) & 0xff) as usize] ^ t[5][((lo >> 16) & 0xff) as usize] ^ t[4][(lo >> 24) as usize] ^ t[3][b[4] as usize] ^ t[2][b[5] as usize] ^ t[1][b[6] as usize] ^ t[0][b[7] as usize];
}
for &b in rest {
c = t[0][((c ^ b as u32) & 0xff) as usize] ^ (c >> 8);
}
!c
}
/// Code page 437, bytes 0x80 to 0xFF: the zip format's name encoding without the UTF-8 flag.
const CP437: [char; 128] = [
'Ç', 'ü', 'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å', //
'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', '¢', '£', '¥', '₧', 'ƒ', //
'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', '¿', '⌐', '¬', '½', '¼', '¡', '«', '»', //
'░', '▒', '▓', '│', '┤', '╡', '╢', '╖', '╕', '╣', '║', '╗', '╝', '╜', '╛', '┐', //
'└', '┴', '┬', '├', '─', '┼', '╞', '╟', '╚', '╔', '╩', '╦', '╠', '═', '╬', '╧', //
'╨', '╤', '╥', '╙', '╘', '╒', '╓', '╫', '╪', '┘', '┌', '█', '▄', '▌', '▐', '▀', //
'α', 'ß', 'Γ', 'π', 'Σ', 'σ', 'µ', 'τ', 'Φ', 'Θ', 'Ω', 'δ', '∞', 'φ', 'ε', '∩', //
'≡', '±', '≥', '≤', '⌠', '⌡', '÷', '≈', '°', '∙', '·', '√', 'ⁿ', '²', '■', '\u{a0}',
];
/// An entry's name as text: the Info-ZIP Unicode path field when it matches the name, UTF-8
/// when flag bit 11 says so, else CP437 as the format specifies. Many writers store UTF-8
/// without setting the flag, so a name that is valid UTF-8 is read as UTF-8 (ASCII, which
/// nearly every name is, reads the same either way).
fn decode_name(raw: &[u8], utf8: bool, extra: &[u8]) -> Result<String, ZipError> {
if let Some(up) = extra_field(extra, 0x7075)
&& up.first() == Some(&1)
&& u32_at(up, 1) == Some(crc32(raw))
&& let Some(Ok(s)) = up.get(5..).map(std::str::from_utf8)
{
return Ok(s.to_string());
}
match std::str::from_utf8(raw) {
Ok(s) => Ok(s.to_string()),
Err(_) if utf8 => Err(ZipError::Damaged("an entry's name is not the UTF-8 it says it is")),
Err(_) => Ok(raw.iter().map(|&b| if b < 0x80 { b as char } else { CP437.get((b - 0x80) as usize).copied().unwrap_or('\u{fffd}') }).collect()),
}
}
/// A name as a plain path inside the package: `\` read as `/`, empty and `.` parts dropped.
/// Names that would leave the package are refused, though nothing is ever extracted: every
/// lookup stays unambiguous, as the folder form's reader also refuses them.
fn clean_path(name: &str) -> Result<String, ZipError> {
let unix = name.replace('\\', "/");
let shown = || name.chars().take(200).collect::<String>();
if unix.starts_with('/') || unix.contains('\0') {
return Err(ZipError::Unsafe(shown()));
}
let mut parts = Vec::new();
for (i, part) in unix.split('/').enumerate() {
match part {
"" | "." => {}
".." => return Err(ZipError::Unsafe(shown())),
// `C:`, `C:x`: a drive.
p if i == 0 && p.contains(':') => return Err(ZipError::Unsafe(shown())),
p => parts.push(p),
}
}
if parts.len() > MAX_DEPTH + 1 {
return Err(ZipError::TooDeep(shown()));
}
Ok(parts.join("/"))
}
fn method_name(method: u16) -> &'static str {
match method {
9 => "Deflate64 compression",
12 => "bzip2 compression",
14 => "LZMA compression",
93 => "Zstandard compression",
95 => "XZ compression",
99 => "AES encryption",
_ => "an unknown compression method",
}
}
/// The end of the central directory: (entry count, its size, its offset, where it must end).
fn find_end(data: &[u8]) -> Result<(u64, u64, u64, usize), ZipError> {
// The end record is in the last 22 + 65535 bytes (it may carry a comment).
let lo = data.len().saturating_sub(22 + 65_535);
let eocd = (lo..data.len().saturating_sub(21)).rev().find(|&i| u32_at(data, i) == Some(END)).ok_or(ZipError::NotZip)?;
let field16 = |o: usize| u16_at(data, eocd + o).ok_or(ZipError::Damaged("the end record is cut short"));
let field32 = |o: usize| u32_at(data, eocd + o).ok_or(ZipError::Damaged("the end record is cut short"));
let (disk, dir_disk, on_disk, count) = (field16(4)?, field16(6)?, field16(8)?, field16(10)?);
let (size, offset) = (field32(12)?, field32(16)?);
if count != 0xffff && size != 0xffff_ffff && offset != 0xffff_ffff {
if disk != 0 || dir_disk != 0 || on_disk != count {
return Err(ZipError::UnsupportedArchive("split across several files"));
}
return Ok((count.into(), size.into(), offset.into(), eocd));
}
// ZIP64: a locator just before the end record points at the ZIP64 end record.
let locator = eocd.checked_sub(20).filter(|&l| u32_at(data, l) == Some(LOCATOR64)).ok_or(ZipError::Damaged("the ZIP64 end record is missing"))?;
let missing = ZipError::Damaged("the ZIP64 end record is missing");
let at = u64_at(data, locator + 8).and_then(|a| usize::try_from(a).ok()).filter(|&a| a < locator).ok_or(missing.clone())?;
if u32_at(data, at) != Some(END64) {
return Err(missing);
}
let cut = ZipError::Damaged("the ZIP64 end record is cut short");
let (disk, dir_disk) = (u32_at(data, at + 16).ok_or(cut.clone())?, u32_at(data, at + 20).ok_or(cut.clone())?);
let (on_disk, count) = (u64_at(data, at + 24).ok_or(cut.clone())?, u64_at(data, at + 32).ok_or(cut.clone())?);
if disk != 0 || dir_disk != 0 || on_disk != count {
return Err(ZipError::UnsupportedArchive("split across several files"));
}
Ok((count, u64_at(data, at + 40).ok_or(cut.clone())?, u64_at(data, at + 48).ok_or(cut)?, at))
}
impl<'a> Archive<'a> {
pub fn open(data: &'a [u8]) -> Result<Archive<'a>, ZipError> {
let (count, dir_size, dir_at, dir_limit) = find_end(data)?;
let past = ZipError::Damaged("the central directory is outside the archive");
let dir_at = usize::try_from(dir_at).map_err(|_| past.clone())?;
let dir_end = usize::try_from(dir_size).ok().and_then(|s| dir_at.checked_add(s)).filter(|&e| e <= dir_limit).ok_or(past.clone())?;
let dir = data.get(dir_at..dir_end).ok_or(past)?;
if count > MAX_ENTRIES as u64 {
return Err(ZipError::TooMany);
}
let count = count as usize;
// Every central entry is at least 46 bytes.
let cut = ZipError::Damaged("the central directory is cut short");
if count.saturating_mul(46) > dir.len() {
return Err(cut);
}
let mut entries = HashMap::with_capacity(count);
let mut folders = 0;
// (start, end, name) of every entry's local header and data.
let mut spans: Vec<(usize, usize, String)> = Vec::with_capacity(count);
let mut i = 0usize;
for _ in 0..count {
if u32_at(dir, i) != Some(CENTRAL) {
return Err(cut);
}
let short = |o: usize| u16_at(dir, i + o).ok_or(cut.clone());
let long = |o: usize| u32_at(dir, i + o).ok_or(cut.clone());
let (flags, method) = (short(8)?, short(10)?);
let crc = long(16)?;
let (comp32, size32, offset32) = (long(20)?, long(24)?, long(42)?);
let (nlen, xlen, clen) = (short(28)? as usize, short(30)? as usize, short(32)? as usize);
let raw_name = get(dir, i + 46, nlen).ok_or(cut.clone())?;
let extra = get(dir, i + 46 + nlen, xlen).ok_or(cut.clone())?;
get(dir, i + 46 + nlen + xlen, clen).ok_or(cut.clone())?;
i += 46 + nlen + xlen + clen;
let name = decode_name(raw_name, flags & 0x0800 != 0, extra)?;
let shown = || name.chars().take(200).collect::<String>();
// ZIP64: each 32-bit field that overflowed is in the extra field, in this order.
let (mut size, mut comp, mut offset) = (u64::from(size32), u64::from(comp32), u64::from(offset32));
if size32 == u32::MAX || comp32 == u32::MAX || offset32 == u32::MAX {
let z = extra_field(extra, 0x0001).ok_or_else(|| ZipError::DamagedEntry(shown(), "has no ZIP64 sizes"))?;
let mut at = 0;
for (wide, value) in [(size32, &mut size), (comp32, &mut comp), (offset32, &mut offset)] {
if wide == u32::MAX {
*value = u64_at(z, at).ok_or_else(|| ZipError::DamagedEntry(shown(), "has its ZIP64 sizes cut short"))?;
at += 8;
}
}
}
let outside = || ZipError::DamagedEntry(shown(), "is outside the archive");
let offset = usize::try_from(offset).map_err(|_| outside())?;
let comp = usize::try_from(comp).map_err(|_| outside())?;
// An entry's size is checked when it is read: one too large to read may sit unused.
let size = usize::try_from(size).unwrap_or(usize::MAX);
// Its local header, then its data, all before the central directory.
if u32_at(data, offset) != Some(LOCAL) {
return Err(ZipError::DamagedEntry(shown(), "has no local header"));
}
let lnlen = u16_at(data, offset + 26).ok_or_else(outside)? as usize;
let lxlen = u16_at(data, offset + 28).ok_or_else(outside)? as usize;
let start = offset + 30 + lnlen + lxlen;
let end = start.checked_add(comp).filter(|&e| e <= dir_at).ok_or_else(outside)?;
spans.push((offset, end, shown()));
let folder = name.ends_with('/') || name.ends_with('\\');
let path = clean_path(&name)?;
if folder || path.is_empty() {
if size != 0 || comp > 2 {
return Err(ZipError::DamagedEntry(shown(), "is a folder with data"));
}
folders += 1;
continue;
}
if method == 8 && size as u64 > (comp as u64).saturating_mul(MAX_RATIO).saturating_add(4096) {
return Err(ZipError::Bomb(shown()));
}
if method == 0 && flags & 0x41 == 0 && size != comp {
return Err(ZipError::DamagedEntry(shown(), "is stored with two different sizes"));
}
if entries.insert(path, Entry { method, flags, crc, comp, size, data: start }).is_some() {
return Err(ZipError::Duplicate(shown()));
}
}
// No two entries may share bytes (the overlapping-entries zip bomb).
spans.sort_unstable_by_key(|s| s.0);
if let Some(w) = spans.windows(2).find(|w| w[0].1 > w[1].0) {
return Err(ZipError::Overlap(w[0].2.clone(), w[1].2.clone()));
}
Ok(Archive { data, entries, folders, budget: Cell::new(MAX_TOTAL) })
}
/// The files' paths (`/`-separated, folders left out).
pub fn names(&self) -> impl Iterator<Item = &str> {
self.entries.keys().map(String::as_str)
}
/// How many files and folder entries the archive lists.
pub fn counts(&self) -> (usize, usize) {
(self.entries.len(), self.folders)
}
/// The bytes of entry `name` (path inside the archive, `/`-separated), checked against
/// its size and checksum.
pub fn read(&self, name: &str) -> Result<Option<Vec<u8>>, ZipError> {
let Some(e) = self.entries.get(name) else { return Ok(None) };
if e.flags & 0x41 != 0 {
return Err(ZipError::Unsupported(name.into(), "encryption"));
}
if !matches!(e.method, 0 | 8) {
return Err(ZipError::Unsupported(name.into(), method_name(e.method)));
}
if e.size > MAX_ENTRY || e.comp > MAX_ENTRY {
return Err(ZipError::TooLarge(name.into()));
}
let left = self.budget.get();
if e.size as u64 > left {
return Err(ZipError::TooMuch);
}
let raw = get(self.data, e.data, e.comp).ok_or_else(|| ZipError::DamagedEntry(name.into(), "is outside the archive"))?;
let out = match e.method {
0 => raw.to_vec(),
_ => miniz_oxide::inflate::decompress_to_vec_with_limit(raw, e.size.max(1)).map_err(|err| {
let why = if err.status == miniz_oxide::inflate::TINFLStatus::HasMoreOutput { "expands past the size it gives" } else { "has damaged compressed data" };
ZipError::DamagedEntry(name.into(), why)
})?,
};
if out.len() != e.size {
return Err(ZipError::DamagedEntry(name.into(), "is not the size it gives"));
}
if crc32(&out) != e.crc {
return Err(ZipError::DamagedEntry(name.into(), "fails its checksum"));
}
self.budget.set(left - e.size as u64);
Ok(Some(out))
}
}
#[cfg(test)]
#[path = "../tests/support/fla.rs"]
pub(crate) mod pack;
#[cfg(test)]
#[path = "zip_tests.rs"]
mod tests;
-284
View File
@@ -1,284 +0,0 @@
//! The zip reader against the layouts writers produce and the archives an attacker would
//! (ADR 0054). Archives are made by the independent writer in `tests/support/fla.rs`.
use super::pack::{self, Raw, write};
use super::*;
fn zip(files: &[(&str, &[u8], bool)]) -> Vec<u8> {
write(&files.iter().map(|(n, b, d)| Raw::new(n, b, *d)).collect::<Vec<_>>(), false)
}
/// The error opening `z`, or reading every file in it.
fn error(z: &[u8]) -> ZipError {
match Archive::open(z) {
Err(e) => e,
Ok(a) => {
let names: Vec<String> = a.names().map(str::to_string).collect();
names.iter().find_map(|n| a.read(n).err()).expect("an error")
}
}
}
#[test]
fn reads_stored_and_deflated_entries() {
let z = zip(&[("DOMDocument.xml", b"<DOMDocument/>", true), ("LIBRARY\\Ball.xml", b"<DOMSymbolItem/>", false)]);
let a = Archive::open(&z).unwrap();
assert_eq!(a.read("DOMDocument.xml").unwrap().unwrap(), b"<DOMDocument/>");
assert_eq!(a.read("LIBRARY/Ball.xml").unwrap().unwrap(), b"<DOMSymbolItem/>", "backslashes normalised");
assert_eq!(a.read("missing").unwrap(), None);
}
#[test]
fn the_checksum_matches_the_published_check_value() {
assert_eq!(crc32(b"123456789"), 0xCBF4_3926);
assert_eq!(crc32(b""), 0);
let bytes: Vec<u8> = (0..5000u32).map(|i| (i * 7 + i / 13) as u8).collect();
assert_eq!(crc32(&bytes), pack::crc32(&bytes), "the table matches the bitwise form");
}
#[test]
fn every_layout_a_writer_uses_reads_the_same() {
let files: std::collections::BTreeMap<String, Vec<u8>> = [("DOMDocument.xml", "<DOMDocument/>"), ("LIBRARY/Café/Ünïcödé ★.xml", "<DOMSymbolItem/>"), ("bin/M 1 1.dat", "data")].iter().map(|(p, b)| (p.to_string(), b.as_bytes().to_vec())).collect();
for v in pack::VARIANTS {
let z = pack::pack(&files, v);
let a = Archive::open(&z).unwrap_or_else(|e| panic!("{}: {e}", v.name));
assert_eq!(a.counts().0, 3 + usize::from(v.mimetype), "{}", v.name);
for (path, body) in &files {
assert_eq!(a.read(path).unwrap_or_else(|e| panic!("{}: {e}", v.name)).as_deref(), Some(body.as_slice()), "{}: {path}", v.name);
}
}
}
#[test]
fn names_are_cp437_unless_flagged_or_utf8() {
// "Café.xml" in CP437 (é is 0x82), not valid UTF-8, without the flag.
let mut r = Raw::new("", b"x", false);
(r.name, r.utf8) = (b"LIBRARY/Caf\x82.xml".to_vec(), false);
let z = write(&[r.clone()], false);
assert_eq!(Archive::open(&z).unwrap().names().collect::<Vec<_>>(), ["LIBRARY/Café.xml"]);
assert_eq!(pack::cp437("LIBRARY/Café.xml").unwrap(), b"LIBRARY/Caf\x82.xml");
// Flagged as UTF-8 but isn't: refused, not guessed.
r.utf8 = true;
assert!(matches!(Archive::open(&write(&[r], false)).err(), Some(ZipError::Damaged(_))));
// A Unicode path field whose checksum doesn't match the name it stands in for is ignored.
let mut r = Raw::new("plain.xml", b"x", false);
r.utf8 = false;
r.extra = [0x75, 0x70, 9, 0, 1, 0, 0, 0, 0, b'o', b't', b'h', b'r'].to_vec();
assert_eq!(Archive::open(&write(&[r], false)).unwrap().names().collect::<Vec<_>>(), ["plain.xml"]);
}
#[test]
fn damaged_archives_are_errors() {
assert_eq!(Archive::open(b"not a zip").err(), Some(ZipError::NotZip));
assert_eq!(Archive::open(b"").err(), Some(ZipError::NotZip));
let mut z = zip(&[("a", b"hello", true)]);
let n = z.len();
// Point the directory past the end.
z[n - 6] = 0xff;
assert!(Archive::open(&z).is_err());
}
#[test]
fn a_truncated_central_directory_is_damaged() {
let z = zip(&[("DOMDocument.xml", b"<DOMDocument/>", true), ("b.xml", b"<b/>", true), ("c.xml", b"<c/>", false)]);
let dir_at = u32_at(&z, z.len() - 6).unwrap() as usize;
let end = z.len() - 22;
// Every cut inside the directory, keeping the end record (which still claims 3 entries).
for cut in dir_at..end {
let mut t = z[..cut].to_vec();
t.extend_from_slice(&z[end..]);
let e = Archive::open(&t).err().unwrap_or_else(|| panic!("cut at {cut} opened"));
assert!(!e.to_string().is_empty());
}
// The end record says more entries than there are.
let mut t = z.clone();
t[end + 8] = 9;
t[end + 10] = 9;
assert_eq!(Archive::open(&t).err(), Some(ZipError::Damaged("the central directory is cut short")));
// Cut short anywhere at all: an error, never a panic.
for cut in 0..z.len() {
let _ = Archive::open(&z[..cut]).map(|a| a.read("DOMDocument.xml"));
}
}
#[test]
fn overlapping_entries_are_refused() {
// Two names for one local header (the overlapping-file zip bomb).
let mut b = Raw::new("b.xml", b"<a/>", true);
b.share = Some(0);
let z = write(&[Raw::new("a.xml", b"<a/>", true), b], false);
assert!(matches!(Archive::open(&z).err(), Some(ZipError::Overlap(..))), "{:?}", Archive::open(&z).err());
// An entry whose stated compressed size runs into the next one.
let mut z = zip(&[("a.xml", b"aaaaaaaaaa", false), ("b.xml", b"bbbbbbbbbb", false)]);
let dir_at = u32_at(&z, z.len() - 6).unwrap() as usize;
z[dir_at + 20] = 20; // a's compressed size, in its central record
z[dir_at + 24] = 20;
assert!(matches!(Archive::open(&z).err(), Some(ZipError::Overlap(..))));
}
#[test]
fn entries_never_reach_past_their_data() {
// An entry's data running into the central directory, or past the end.
for size in [64u32, 0xffff_fff0] {
let mut z = zip(&[("a.xml", b"aaaaaaaaaa", false)]);
let dir_at = u32_at(&z, z.len() - 6).unwrap() as usize;
z[dir_at + 20..dir_at + 24].copy_from_slice(&size.to_le_bytes());
assert_eq!(Archive::open(&z).err(), Some(ZipError::DamagedEntry("a.xml".into(), "is outside the archive")));
}
// A local header offset past the end.
let mut z = zip(&[("a.xml", b"aaaaaaaaaa", false)]);
let dir_at = u32_at(&z, z.len() - 6).unwrap() as usize;
z[dir_at + 42..dir_at + 46].copy_from_slice(&0x7fff_0000u32.to_le_bytes());
assert_eq!(Archive::open(&z).err(), Some(ZipError::DamagedEntry("a.xml".into(), "has no local header")));
}
#[test]
fn lying_sizes_and_checksums_are_damaged() {
// Says 100 bytes, inflates to 1000.
let body = vec![b'x'; 1000];
let mut r = Raw::new("a.xml", &body, true);
r.size = Some(100);
assert_eq!(error(&write(&[r], false)), ZipError::DamagedEntry("a.xml".into(), "expands past the size it gives"));
// Says 2000, inflates to 1000.
let mut r = Raw::new("a.xml", &body, true);
r.size = Some(2000);
assert_eq!(error(&write(&[r], false)), ZipError::DamagedEntry("a.xml".into(), "is not the size it gives"));
// A stored entry with two sizes.
let mut r = Raw::new("a.xml", &body, false);
r.size = Some(10);
assert_eq!(error(&write(&[r], false)), ZipError::DamagedEntry("a.xml".into(), "is stored with two different sizes"));
// The wrong checksum.
let mut r = Raw::new("a.xml", &body, true);
r.crc = Some(12345);
assert_eq!(error(&write(&[r], false)), ZipError::DamagedEntry("a.xml".into(), "fails its checksum"));
// Garbage instead of deflate data.
let mut r = Raw::new("a.xml", &body, true);
r.compressed = Some(vec![0xff; 40]);
assert_eq!(error(&write(&[r], false)), ZipError::DamagedEntry("a.xml".into(), "has damaged compressed data"));
}
#[test]
fn compression_bombs_are_refused() {
// A real 64 MB of zeros deflates past the ratio cap: refused before inflating.
let zeros = vec![0u8; 64 << 20];
let z = zip(&[("bomb.xml", &zeros, true)]);
assert!(z.len() < 100_000, "{}", z.len());
assert_eq!(Archive::open(&z).err(), Some(ZipError::Bomb("bomb.xml".into())));
// A size claim no deflate stream of that length could meet.
let mut r = Raw::new("a.xml", &[7; 100], true);
r.size = Some(200 << 20);
assert_eq!(Archive::open(&write(&[r], false)).err(), Some(ZipError::Bomb("a.xml".into())));
// One entry larger than an entry may be, at a believable ratio: refused before inflating.
let mut r = Raw::new("big.dat", b"", true);
r.compressed = Some(vec![0; 300 << 10]);
r.size = Some(MAX_ENTRY as u64 + 1);
let z = write(&[r], false);
assert_eq!(error(&z), ZipError::TooLarge("big.dat".into()));
}
#[test]
fn the_archive_hands_out_at_most_its_budget() {
let body = vec![b'a'; 4096];
let z = zip(&[("a.xml", &body, true)]);
let a = Archive::open(&z).unwrap();
a.budget.set(10_000);
assert!(a.read("a.xml").is_ok() && a.read("a.xml").is_ok());
assert_eq!(a.read("a.xml").err(), Some(ZipError::TooMuch), "a third read passes 10,000 bytes");
}
#[test]
fn unsafe_paths_are_refused() {
for name in ["../escape.xml", "LIBRARY/../../escape.xml", "/etc/passwd", "\\Windows\\win.ini", "C:\\Windows\\win.ini", "C:escape.xml", "LIBRARY\\..\\..\\x", "a\0b"] {
let z = zip(&[("DOMDocument.xml", b"<DOMDocument/>", true), (name, b"x", false)]);
assert!(matches!(Archive::open(&z).err(), Some(ZipError::Unsafe(_))), "{name:?}: {:?}", Archive::open(&z).err());
}
// Harmless odd names are tidied, not refused.
let z = zip(&[("./LIBRARY//a.xml", b"x", false)]);
assert_eq!(Archive::open(&z).unwrap().names().collect::<Vec<_>>(), ["LIBRARY/a.xml"]);
}
#[test]
fn duplicate_names_are_refused() {
let z = zip(&[("LIBRARY/a.xml", b"one", false), ("LIBRARY\\a.xml", b"two", false)]);
assert_eq!(Archive::open(&z).err(), Some(ZipError::Duplicate("LIBRARY\\a.xml".into())));
// Folders listed twice are harmless.
let z = zip(&[("LIBRARY/", b"", false), ("LIBRARY\\", b"", false), ("LIBRARY/a.xml", b"one", false)]);
assert_eq!(Archive::open(&z).unwrap().counts(), (1, 2));
}
#[test]
fn deeply_nested_folders_are_refused() {
let ok = format!("{}a.xml", "f/".repeat(MAX_DEPTH));
assert!(Archive::open(&zip(&[(&ok, b"x", false)])).is_ok());
let deep = format!("{}a.xml", "f/".repeat(MAX_DEPTH + 1));
assert!(matches!(Archive::open(&zip(&[(&deep, b"x", false)])).err(), Some(ZipError::TooDeep(_))));
// As deep as a name can be: refused quickly, not recursed into.
let deepest = "f/".repeat(30_000);
assert!(matches!(Archive::open(&zip(&[(&deepest, b"", false)])).err(), Some(ZipError::TooDeep(_))));
}
#[test]
fn unsupported_features_say_what_they_are() {
let mut r = Raw::new("a.xml", b"secret", false);
r.flags = 1;
assert_eq!(error(&write(&[r], false)), ZipError::Unsupported("a.xml".into(), "encryption"));
let mut r = Raw::new("a.xml", b"x", false);
r.method = Some(14);
assert_eq!(error(&write(&[r], false)), ZipError::Unsupported("a.xml".into(), "LZMA compression"));
// Split archives.
let mut z = zip(&[("a.xml", b"x", false)]);
let end = z.len() - 22;
z[end + 4] = 1;
assert_eq!(Archive::open(&z).err(), Some(ZipError::UnsupportedArchive("split across several files")));
// Too many entries for the directory to hold, and more than the cap.
let mut z = zip(&[("a.xml", b"x", false)]);
let end = z.len() - 22;
z[end + 8..end + 12].copy_from_slice(&[0xfe, 0xff, 0xfe, 0xff]);
assert!(Archive::open(&z).is_err());
}
#[test]
fn zip64_archives_open_and_their_damage_is_named() {
let mut a = Raw::new("a.xml", b"<a/>", true);
a.zip64 = true;
let mut b = Raw::new("b.xml", b"<b/>", false);
(b.zip64, b.descriptor) = (true, true);
let z = write(&[a.clone(), b], true);
let arch = Archive::open(&z).unwrap();
assert_eq!(arch.read("b.xml").unwrap().unwrap(), b"<b/>");
// The locator gone.
let mut t = z.clone();
let loc = t.len() - 22 - 20;
t[loc] = 0;
assert_eq!(Archive::open(&t).err(), Some(ZipError::Damaged("the ZIP64 end record is missing")));
// An entry that says ZIP64 without the extra field.
a.zip64 = false;
let mut t = write(&[a], false);
let dir_at = u32_at(&t, t.len() - 6).unwrap() as usize;
t[dir_at + 24..dir_at + 28].copy_from_slice(&u32::MAX.to_le_bytes());
assert_eq!(Archive::open(&t).err(), Some(ZipError::DamagedEntry("a.xml".into(), "has no ZIP64 sizes")));
}
#[test]
fn errors_read_as_sentences() {
let all = [
ZipError::NotZip,
ZipError::Damaged("the central directory is cut short"),
ZipError::DamagedEntry("a.xml".into(), "fails its checksum"),
ZipError::UnsupportedArchive("split across several files"),
ZipError::Unsupported("a.xml".into(), "encryption"),
ZipError::TooLarge("a.xml".into()),
ZipError::TooMuch,
ZipError::TooMany,
ZipError::Bomb("a.xml".into()),
ZipError::Overlap("a.xml".into(), "b.xml".into()),
ZipError::Unsafe("../a.xml".into()),
ZipError::Duplicate("a.xml".into()),
ZipError::TooDeep("f/f/a.xml".into()),
];
for e in all {
let s = e.to_string();
assert!(s.len() > 12 && !s.contains("{") && s.chars().next().is_some_and(|c| !c.is_uppercase() || c == 'A'), "{s}");
}
assert_eq!(ZipError::TooMuch.to_string(), "the archive asks for more than 1024 MB in all");
}
+2 -2
View File
@@ -1,8 +1,8 @@
//! Test support: packs an XFL package into `.fla` (zip) archives in memory, in each of the ways
//! zip writers lay one out (ADR 0054), and writes hand-made entries for hostile archives.
//!
//! Written from the published format (PKWARE APPNOTE 6.3), independently of the reader in
//! `src/zip.rs` (it has its own checksum), so a misreading in one is caught by the other.
//! Written from the published format (PKWARE APPNOTE 6.3), independently of the shared
//! `craft-xfl` reader (it has its own checksum), so a misreading in one is caught by the other.
//! Shared by the xfl crate's unit and corpus tests and the engine's golden tests (`#[path]`).
#![allow(dead_code)]