feat(xfl): sound effects and envelopes, named reports, Screen Video stills, top-folder and self-extracting .fla archives (M4)

This commit is contained in:
Tanish Manku committed 2026-11-13 02:36:14 -07:00
1 parent 5d3466b0b9
commit 1f3e603b7f
26 files changed
+1002 -52

No files matched your search

+4
View File
@@ -16,3 +16,7 @@ scripts/git-hooks/* text eol=lf
*.tiff binary
*.DS_Store binary
*.ico binary
# XFL corpus media (ADR 0086)
*.flv binary
*.aif binary
*.dat binary
+12
View File
@@ -81,6 +81,18 @@ impl Bitmap {
Bitmap::from_png(id, name, png).ok_or(ImageError::Unreadable)
}
/// A bitmap from straight RGBA8 pixels, row by row (a decoded video frame, say). `None`
/// when the size is empty, over [`MAX_BITMAP_EDGE`] or doesn't match the pixels.
pub fn from_rgba(id: BitmapId, name: impl Into<String>, width: u32, height: u32, rgba: Vec<u8>) -> Option<Bitmap> {
if width == 0 || height == 0 || width > MAX_BITMAP_EDGE || height > MAX_BITMAP_EDGE || rgba.len() != width as usize * height as usize * 4 {
return None;
}
let png = encode_png(width, height, &rgba)?;
let decoded = OnceLock::new();
let _ = decoded.set(Some(Arc::new(Pixels { width, height, rgba })));
Some(Bitmap { id, name: name.into(), width, height, png: Arc::new(png), smoothing: true, decoded: Arc::new(decoded) })
}
/// The decoded pixels (decoded once and shared by every clone). `None` for unreadable data.
pub fn pixels(&self) -> Option<Arc<Pixels>> {
self.decoded.get_or_init(|| decode_png(&self.png).map(Arc::new)).clone()
+2
View File
@@ -212,6 +212,8 @@ pub(crate) fn open(s: &mut Session, p: &Value) -> Result<Value> {
info["kept"] = json!(report.kept);
info["runs"] = json!(report.runs);
info["missing_fonts"] = json!(report.missing_fonts);
// The sounds and videos that didn't come across, by name and why (ADR 0086).
info["named"] = json!(report.names);
info["merged_keyframes"] = json!(merged);
return Ok(info);
}
+29
View File
@@ -40,10 +40,12 @@ const FRAMES: &[(&str, &[u32])] = &[
("scenes", &[0, 3, 7, 8, 10]),
("scripts", &[0, 4]),
("shapes", &[0]),
("sounds", &[0]),
("symbols", &[0, 5]),
("text", &[0]),
("textfields", &[0]),
("tweens", &[0, 6, 12]),
("video", &[0]),
];
/// Render scale: the corpus stages are 320 × 180.
@@ -59,6 +61,18 @@ const PROBES: &[(&str, u32, f32, f32, u32)] = &[
("shapes", 0, 80.0, 30.0, 0x2B6CD9),
("shapes", 0, 95.0, 50.0, 0xF4F1EA),
("shapes", 0, 270.0, 60.0, 0xF29E1F),
// ADR 0086: the sounds package's artwork under its sound layers, on its own stage colour.
("sounds", 0, 40.0, 60.0, 0x2A9D8F),
("sounds", 0, 160.0, 90.0, 0xE9C46A),
("sounds", 0, 10.0, 10.0, 0xF4F1EA),
// The Screen Video's first frame at 1.5 times (20..116 × 20..92), its quarters the right
// way up and round; the VP6 video draws nothing; the rectangle beside it.
("video", 0, 35.0, 35.0, 0xE63946),
("video", 0, 100.0, 35.0, 0x2A9D8F),
("video", 0, 35.0, 80.0, 0x2646C8),
("video", 0, 100.0, 80.0, 0xE9C46A),
("video", 0, 200.0, 50.0, 0x264653),
("video", 0, 10.0, 150.0, 0xFFFFFF),
("primitives", 0, 40.0, 50.0, 0xF2CC8F),
("primitives", 0, 210.0, 40.0, 0xE07A5F),
("primitives", 0, 6.0, 170.0, 0xFFFFFF),
@@ -270,6 +284,7 @@ fn every_package_opens_from_a_fla_with_the_same_golden_frames() {
let mut t = Session::default();
let got = t.execute("file.open", &json!({"path": path.display().to_string()})).unwrap_or_else(|e| panic!("{case}, {}: {e}", v.name));
assert_eq!(got["report"], want["report"], "{case}, {}", v.name);
assert_eq!(got["named"], want["named"], "{case}, {}", v.name);
assert!(t.doc == s.doc, "{case}, {}: the .fla opens differently from the folder", v.name);
}
// The goldens (imported without fonts, as they were made), rendered from the .fla in
@@ -342,3 +357,17 @@ fn every_entry_point_opens_the_corpus_as_a_fla() {
.unwrap();
}
}
/// ADR 0086: opening a document headless (the CLI and MCP) says which sounds and videos didn't
/// come across, by name, and the sounds that did are in the Library with their waveforms.
#[test]
fn file_open_names_the_sounds_and_videos_that_did_not_come_across() {
let mut s = Session::default();
let got = s.execute("file.open", &json!({"path": corpus().join("sounds").display().to_string()})).unwrap();
assert_eq!(got["named"]["missing sound files"], json!(["lost.wav"]));
assert!(got["report"].as_str().unwrap().contains("3 sounds in formats AniCraft can't decode (theme.aif: AIFF; voice.wav: WAV in IMA ADPCM; Sounds/loop: Animate's own sound data)"), "{}", got["report"]);
assert_eq!(s.doc.sounds.len(), 2);
assert!(s.doc.sounds.iter().all(|x| x.decoded().is_some_and(|d| d.pcm.frames() > 0)), "waveforms ready to draw");
let got = s.execute("file.open", &json!({"path": corpus().join("video").display().to_string(), "confirm": true})).unwrap();
assert_eq!(got["named"]["videos AniCraft can't play"], json!(["intro.flv: FLV, On2 VP6, 4.5 s", "gone.mp4: its file is missing, 1.0 s"]));
}
+287 -25
View File
@@ -25,6 +25,7 @@ use anicraft_doc::{
Cap, Dash, Join, StrokeStyle, Bitmap, BitmapId, BlendMode, ColorEffect, ColorTransform, Filter, FilterSide, Document, Easing, Element, ElementId, ElementKind, GradientStop, Keyframe, Layer, LayerId, LayerKind, LoopMode, Paint, Rgba, Shape, Stage, Stroke, Symbol, SymbolId,
Scene, ShapeHint, SymbolKind, Timeline, Tween, TweenKind, CustomEase, EaseGroup, FrameScript, FrameSound, Language, Linkage, ScaleGrid, SoundSync, SourceScript, Spread, MAX_FOCAL,
};
use anicraft_doc::sound::{EnvelopePoint, SoundEffect, clean_points};
use anicraft_geom::{Affine, FillRule, Path, Vec2};
use anicraft_text::{FaceMatch, Fonts};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
@@ -66,12 +67,29 @@ pub struct Report {
pub missing_fonts: BTreeSet<String>,
/// The family that text was laid out in instead.
pub fallback_font: Option<String>,
/// The items behind some kinds of `skipped`, by name ("intro.flv, On2 VP6, 4.0 s"), so an
/// artist can tell which sound or video didn't come across (ADR 0086). At most
/// [`MAX_NAMED`] per kind; the count in `skipped` is always whole.
pub names: BTreeMap<String, Vec<String>>,
}
/// Most names kept per kind of skipped item.
pub const MAX_NAMED: usize = 64;
/// Most names the summary line shows per kind.
const SHOWN_NAMES: usize = 3;
impl Report {
pub(crate) fn skip(&mut self, what: &str) {
*self.skipped.entry(what.to_string()).or_default() += 1;
}
/// Counts `what` and keeps the item's `name` for the report.
pub(crate) fn skip_named(&mut self, what: &str, name: String) {
self.skip(what);
let names = self.names.entry(what.to_string()).or_default();
if names.len() < MAX_NAMED {
names.push(name);
}
}
pub(crate) fn keep(&mut self, what: &str) {
*self.kept.entry(what.to_string()).or_default() += 1;
}
@@ -94,7 +112,20 @@ impl Report {
return format!("Imported everything in the file.{kept}");
}
let mut out = format!("Imported.{kept}");
let mut parts: Vec<String> = self.skipped.iter().map(|(k, n)| format!("{n} {k}")).collect();
let mut parts: Vec<String> = self
.skipped
.iter()
.map(|(k, n)| match self.names.get(k).filter(|v| !v.is_empty()) {
Some(names) => {
let mut shown: Vec<&str> = names.iter().take(SHOWN_NAMES).map(String::as_str).collect();
let more = n.saturating_sub(shown.len());
let more = if more > 0 { format!(" and {more} more") } else { String::new() };
shown.truncate(SHOWN_NAMES);
format!("{n} {k} ({}{more})", shown.join("; "))
}
None => format!("{n} {k}"),
})
.collect();
parts.extend(self.notes.iter().cloned());
if !parts.is_empty() {
out.push_str(&format!(" Not yet supported: {}.", parts.join(", ")));
@@ -116,6 +147,70 @@ const MAX_LAYERS: usize = 5_000;
const MAX_FRAME: u32 = 100_000;
const MAX_BITMAPS: usize = 10_000;
const MAX_SOUNDS: usize = 10_000;
const MAX_VIDEOS: usize = 10_000;
/// Names the format of sound data that didn't decode, and why when it is a kind AniCraft reads:
/// "AIFF", "WAV in IMA ADPCM", "Animate's own sound data".
fn sound_format(data: &[u8], from_bin: bool, err: &anicraft_doc::audio::AudioError) -> String {
use anicraft_doc::audio::{AudioError, Format};
if data.starts_with(b"OggS") && data.windows(8).any(|w| w == b"OpusHead") {
return "Ogg Opus".into();
}
if let Some(f) = Format::sniff(data) {
let kind = match f {
Format::Wav => "WAV",
Format::Vorbis => "Ogg",
Format::Mp3 => "MP3",
};
return match err {
AudioError::Unsupported(_) if let Some(codec) = wav_codec(data).filter(|c| !matches!(c, 1 | 3 | 0xFFFE)) => {
let name = match codec {
0x0002 => "Microsoft ADPCM".to_string(),
0x0006 => "A-law".to_string(),
0x0007 => "µ-law".to_string(),
0x0011 => "IMA ADPCM".to_string(),
0x0031 => "GSM 6.10".to_string(),
0x0055 => "MP3".to_string(),
c => format!("codec 0x{c:04X}"),
};
format!("WAV in {name}")
}
AudioError::Unsupported(what) => format!("{kind} with {what}"),
AudioError::TooLong => format!("{kind} longer than AniCraft plays"),
AudioError::Empty => format!("{kind} holding no audio"),
_ => format!("a damaged {kind} file"),
};
}
let name = if data.starts_with(b"FORM") && matches!(data.get(8..12), Some(b"AIFF" | b"AIFC")) {
"AIFF"
} else if data.starts_with(b"fLaC") {
"FLAC"
} else if data.get(4..8) == Some(b"ftyp") {
"AAC or MPEG-4 audio"
} else if data.starts_with(b".snd") {
"Sun AU"
} else if from_bin {
"Animate's own sound data"
} else {
"an unknown format"
};
name.into()
}
/// The codec of a WAV file's `fmt ` chunk (1 PCM, 3 float, 0x11 IMA ADPCM, …), looked for in
/// its first chunks.
fn wav_codec(data: &[u8]) -> Option<u16> {
let mut at = 12usize;
for _ in 0..64 {
let id = data.get(at..at + 4)?;
let size = u32::from_le_bytes(data.get(at + 4..at + 8)?.try_into().ok()?) as usize;
if id == b"fmt " {
return Some(u16::from_le_bytes(data.get(at + 8..at + 10)?.try_into().ok()?));
}
at = at.checked_add(8)?.checked_add(size)?.checked_add(size & 1)?;
}
None
}
/// Bitmap fill matrices scale image pixels by twips (1/20 px): `a = 20` is one pixel per pixel.
const TWIPS: f32 = 20.0;
@@ -134,6 +229,9 @@ struct Builder<'a> {
bitmaps: HashMap<String, BitmapId>,
/// Library item name → sound id, for the sounds whose file is in the package (ADR 0057).
sounds: HashMap<String, anicraft_doc::SoundId>,
/// Library item name -> the still of its first frame, for every video item (`None` when the
/// video could not be read; ADR 0086).
videos: HashMap<String, Option<BitmapId>>,
report: Report,
/// The fonts text is laid out with; the computer's, found on the first text field.
fonts: Option<&'a Fonts>,
@@ -327,6 +425,23 @@ impl<'a> Builder<'a> {
out.push(self.element(ElementKind::Shape(shape), parent.mul(&matrix(el)), ColorEffect::default()));
}
/// A video instance (ADR 0086): its video's first frame, as a bitmap rectangle sized to the
/// instance's frame (`frameRight` and `frameBottom`, in twips) when the video came across
/// as a still; otherwise counted, the video itself being named in the report.
fn video_instance(&mut self, el: &X, parent: &Affine, out: &mut Vec<Arc<Element>>) {
let still = el.attr("libraryItemName").and_then(|n| self.videos.get(n)).copied();
let found = still.flatten().and_then(|id| self.doc.bitmap(id).map(|b| (id, b.width as f32, b.height as f32)));
let Some((bitmap, w, h)) = found else {
self.report.skip(if still.is_some() { "instances of videos AniCraft can't play" } else { "instances of missing videos" });
return;
};
let size = |k: &str, natural: f32| el.f32(k).map(|t| t / TWIPS).filter(|v| v.is_finite() && *v > 0.0 && *v <= 100_000.0).unwrap_or(natural);
let (fw, fh) = (size("frameRight", w), size("frameBottom", h));
let fit = Affine::scale(fw / w, fh / h);
let shape = Shape { path: Arc::new(Path::rect(0.0, 0.0, w, h)), fill: Some(Paint::Bitmap { bitmap, repeat: false, matrix: Affine::IDENTITY }), stroke: None, fill_rule: FillRule::NonZero, merge: false, warp: None };
out.push(self.element(ElementKind::Shape(shape), parent.mul(&matrix(el)).mul(&fit), ColorEffect::default()));
}
/// An instance's filters, by Animate's element names; switched-off filters are left out,
/// and unknown ones reported.
fn filters(&mut self, el: &X) -> Vec<Filter> {
@@ -497,7 +612,7 @@ impl<'a> Builder<'a> {
"DOMRectangleObject" | "DOMOvalObject" => self.primitive(el, parent, out),
"DOMBitmapInstance" => self.bitmap_instance(el, parent, out),
"DOMStaticText" | "DOMDynamicText" | "DOMInputText" => self.text_field(el, parent, out),
"DOMVideoInstance" => self.report.skip("videos"),
"DOMVideoInstance" => self.video_instance(el, parent, out),
_ => self.report.skip("other elements"),
}
}
@@ -799,24 +914,27 @@ impl<'a> Builder<'a> {
Ok(())
}
/// Reads the document's sound library items (ADR 0057). Like a bitmap, each one's sound is
/// looked for as the original file in `LIBRARY/` and then as its data file in `bin/`, and is
/// used when it is a WAV, Ogg Vorbis or MP3 file. Animate's own encoding of the data file is
/// not public, so those sounds are reported instead (ADR 0039).
/// Reads the document's sound library items (ADR 0057, ADR 0086). Like a bitmap, each one's
/// sound is looked for as the original file in `LIBRARY/` and then as its data file in
/// `bin/`, and is used when it is a WAV, Ogg Vorbis or MP3 file the decoders read. Animate's
/// own encoding of the data file is not public, so those sounds are reported by name, as is
/// every other sound that doesn't come across, with its format and why (ADR 0039).
fn register_sounds(&mut self, root: &X) -> Result<(), XflError> {
for item in root.list("media").iter().filter(|i| i.name == "DOMSoundItem").take(MAX_SOUNDS) {
let Some(name) = item.attr("name").filter(|n| !self.sounds.contains_key(*n)) else { continue };
let mut candidates = Vec::new();
if let Some(href) = item.attr("href") {
candidates.push(format!("LIBRARY/{href}"));
candidates.push((format!("LIBRARY/{href}"), false));
}
if let Some(data) = item.attr("soundDataHRef") {
candidates.push(format!("bin/{data}"));
candidates.push((format!("bin/{data}"), true));
}
let short: String = name.rsplit('/').next().unwrap_or(name).chars().take(256).collect();
let (mut found_data, mut unreadable) = (false, false);
let shown: String = name.chars().take(256).collect();
// Why the first file found didn't decode, and whether one couldn't be read at all.
let (mut why, mut unreadable) = (None, false);
let mut sound = None;
for path in candidates {
for (path, from_bin) in candidates {
let bytes = match (self.read)(&path) {
Ok(Some(bytes)) => bytes,
Ok(None) => continue,
@@ -827,29 +945,114 @@ impl<'a> Builder<'a> {
continue;
}
};
found_data = true;
if let Ok(s) = anicraft_doc::Sound::from_data(anicraft_doc::SoundId(0), short.clone(), bytes) {
sound = Some(s);
break;
// Named from the first bytes, before the data moves into the sound.
let head: Vec<u8> = bytes.iter().take(4096).copied().collect();
match anicraft_doc::Sound::from_data(anicraft_doc::SoundId(0), short.clone(), bytes) {
Ok(s) => {
sound = Some(s);
break;
}
Err(e) => {
why.get_or_insert_with(|| sound_format(&head, from_bin, &e));
}
}
}
match sound {
Some(mut s) => {
match (sound, why) {
(Some(mut s), _) => {
s.id = anicraft_doc::SoundId(self.doc.alloc_id());
self.place_in_folder(name, anicraft_doc::LibraryItem::Sound(s.id));
self.sounds.insert(name.to_string(), s.id);
self.doc.sounds.push(Arc::new(s));
}
None if found_data => self.report.skip("sounds stored in Animate's own format"),
None if unreadable => self.report.skip("sounds whose files couldn't be read"),
None => self.report.skip("missing sound files"),
(None, Some(why)) => self.report.skip_named("sounds in formats AniCraft can't decode", format!("{shown}: {why}")),
(None, None) if unreadable => self.report.skip_named("sounds whose files couldn't be read", shown),
(None, None) => self.report.skip_named("missing sound files", shown),
}
}
Ok(())
}
/// Reads the document's video items (`DOMVideoItem`, ADR 0086). AniCraft has no video
/// playback: a video whose data is an FLV file in the Screen Video codec comes across as a
/// bitmap of its first frame (in the Library beside where the video was, named
/// "<video> (first frame)"), which its instances show; every video is named in the report
/// with its format and length, so nothing is dropped unseen. The data is looked for as the
/// original file (`href`, or `sourceExternalFilepath` inside the package) in `LIBRARY/`, then
/// as its data file in `bin/` (`videoDataHRef`).
fn register_videos(&mut self, root: &X) {
for item in root.list("media").iter().filter(|i| i.name == "DOMVideoItem").take(MAX_VIDEOS) {
let Some(name) = item.attr("name").filter(|n| !self.videos.contains_key(*n)) else { continue };
let mut candidates = Vec::new();
if let Some(href) = item.attr("href") {
candidates.push(format!("LIBRARY/{href}"));
}
if let Some(src) = item.attr("sourceExternalFilepath").and_then(|p| p.strip_prefix("./")) {
candidates.push(src.replace('\\', "/"));
}
if let Some(data) = item.attr("videoDataHRef") {
candidates.push(format!("bin/{data}"));
}
let shown: String = name.chars().take(256).collect();
let short: String = name.rsplit('/').next().unwrap_or(name).chars().take(240).collect();
let (mut still, mut format, mut unreadable) = (None, None, false);
for path in candidates {
let bytes = match (self.read)(&path) {
Ok(Some(bytes)) => bytes,
Ok(None) => continue,
Err(_) => {
unreadable = true;
continue;
}
};
match crate::video::read(&bytes) {
crate::video::Video::Frame { width, height, rgba } => {
still = Bitmap::from_rgba(BitmapId(0), format!("{short} (first frame)"), width, height, rgba);
if still.is_some() {
break;
}
}
crate::video::Video::Unreadable(f) => {
format.get_or_insert(f);
}
}
}
// Its length as the item gives it (seconds), else from its frames and rate.
let seconds = item.f32("length").filter(|s| s.is_finite() && *s >= 0.0).or_else(|| {
let (frames, fps) = (item.f32("frameCount")?, item.f32("fps")?);
(frames.is_finite() && fps.is_finite() && fps > 0.0).then(|| frames.max(0.0) / fps)
});
let length = seconds.map(|s| format!(", {:.1} s", s.min(1.0e7))).unwrap_or_default();
let id = match still {
Some(mut b) => {
b.id = BitmapId(self.doc.alloc_id());
let id = b.id;
let folder = name.rsplit_once('/').map(|(f, _)| f);
let path = match folder {
Some(f) => format!("{f}/{}", b.name),
None => b.name.clone(),
};
self.place_in_folder(&path, anicraft_doc::LibraryItem::Bitmap(id));
self.doc.bitmaps.push(Arc::new(b));
self.report.skip_named("videos (first frame kept as a still)", format!("{shown}: Screen Video{length}"));
Some(id)
}
None => {
let why = match (format, unreadable) {
(Some(f), _) => f,
(None, true) => "its file couldn't be read".into(),
(None, false) => "its file is missing".into(),
};
self.report.skip_named("videos AniCraft can't play", format!("{shown}: {why}{length}"));
None
}
};
self.videos.insert(name.to_string(), id);
}
}
/// The sound on a frame (`soundName`, the library item), with its sync (`soundSync`: event,
/// start, stop or stream) and repeats (`soundLoopMode` loop or repeat, `soundLoop` times).
/// start, stop or stream), repeats (`soundLoopMode` loop or repeat, `soundLoop` times) and
/// effect (`soundEffect`, and the volume points of a custom one in `SoundEnvelope`).
/// Buttons' frames included: they play as the pointer changes the button's state.
fn frame_sound(&mut self, f: &X, name: &str) -> Option<FrameSound> {
let Some(id) = self.sounds.get(name).copied() else {
@@ -865,15 +1068,51 @@ impl<'a> Builder<'a> {
};
fs.looping = f.attr("soundLoopMode").map(str::trim) == Some("loop");
fs.repeat = f.u32("soundLoop").unwrap_or(1).clamp(1, anicraft_doc::sound::MAX_REPEAT);
if f.attr("soundEffect").is_some_and(|e| !matches!(e.trim(), "" | "none")) || f.child("SoundEnvelope").is_some() {
self.report.skip("sound effects and envelopes (played without)");
}
fs.effect = self.sound_effect(f);
if f.attr("inPoint44").is_some() || f.attr("outPoint44").is_some() {
self.report.skip("sound in and out points (played whole)");
}
Some(fs)
}
/// A frame sound's effect: a preset by its name in the Effect menu (`soundEffect`), or the
/// volume points of its envelope (`SoundEnvelope`; each `SoundEnvelopePoint` a time in
/// samples at 44.1 kHz, `mark44`, and a level per channel, `level0` left and `level1` right,
/// `0..=32768`, as the SWF format's `SOUNDENVELOPE` keeps them).
fn sound_effect(&mut self, f: &X) -> SoundEffect {
let key: String = f.attr("soundEffect").unwrap_or("").chars().filter(char::is_ascii_alphanumeric).map(|c| c.to_ascii_lowercase()).collect();
let preset = match key.as_str() {
"" | "none" => Some(SoundEffect::None),
"left" | "leftchannel" => Some(SoundEffect::LeftChannel),
"right" | "rightchannel" => Some(SoundEffect::RightChannel),
"fadelefttoright" => Some(SoundEffect::FadeLeftToRight),
"faderighttoleft" => Some(SoundEffect::FadeRightToLeft),
"fadein" => Some(SoundEffect::FadeIn),
"fadeout" => Some(SoundEffect::FadeOut),
_ => None,
};
let points: Vec<EnvelopePoint> = f
.child("SoundEnvelope")
.map(|e| e.children.iter().filter(|p| p.name == "SoundEnvelopePoint").take(anicraft_doc::sound::MAX_ENVELOPE_POINTS).collect::<Vec<_>>())
.unwrap_or_default()
.into_iter()
.map(|p| {
let level = |k: &str| p.f32(k).map_or(1.0, |v| v / 32_768.0);
EnvelopePoint::new(f64::from(p.f32("mark44").unwrap_or(0.0)) / 44_100.0, level("level0"), level("level1"))
})
.collect();
match preset {
// An envelope without a preset name (or named "custom") plays as its points.
Some(SoundEffect::None) | None if !points.is_empty() => SoundEffect::Custom(clean_points(&points)),
Some(p) => p,
None if key == "custom" => SoundEffect::None,
None => {
self.report.skip("unknown sound effects (played without)");
SoundEffect::None
}
}
}
/// Allocates ids for every symbol the document lists, parsing their files.
fn register_symbols(&mut self, root: &X) -> Result<(), XflError> {
let mut seen = HashSet::new();
@@ -1031,7 +1270,7 @@ fn import_from<'a>(read: &'a Reader<'a>, fonts: Option<&'a Fonts>) -> Result<(Do
if root.name != "DOMDocument" {
return Err(XflError::NotXfl(format!("the root element is <{}>", root.name)));
}
let mut b = Builder { doc: Document::default(), read, ids: HashMap::new(), pending: Vec::new(), language: Language::ActionScript3, bitmaps: HashMap::new(), sounds: HashMap::new(), report: Report::default(), fonts, faces: HashMap::new() };
let mut b = Builder { doc: Document::default(), read, ids: HashMap::new(), pending: Vec::new(), language: Language::ActionScript3, bitmaps: HashMap::new(), sounds: HashMap::new(), videos: HashMap::new(), report: Report::default(), fonts, faces: HashMap::new() };
b.publish_settings()?;
// Only symbols keep a script class yet.
for _ in root.list("media").iter().filter(|m| m.attr("linkageClassName").is_some_and(|c| !c.trim().is_empty())) {
@@ -1049,6 +1288,7 @@ fn import_from<'a>(read: &'a Reader<'a>, fonts: Option<&'a Fonts>) -> Result<(Do
b.register_folders(&root);
b.register_bitmaps(&root)?;
b.register_sounds(&root)?;
b.register_videos(&root);
b.register_symbols(&root)?;
b.build_symbols();
// Every DOMTimeline is a scene, in order.
@@ -1094,10 +1334,32 @@ fn fla_from(data: &[u8], fonts: Option<&Fonts>) -> Result<(Document, Report), Xf
return Err(XflError::NotXfl("this .fla was saved by Flash CS4 or earlier, in a format AniCraft can't read: open it in Animate and save it again".into()));
}
let archive = crate::zip::Archive::open(data)?;
let read = |p: &str| -> Result<Option<Vec<u8>>, XflError> { Ok(archive.read(p)?) };
let root = package_root(&archive)?;
let read = |p: &str| -> Result<Option<Vec<u8>>, XflError> { Ok(archive.read(&format!("{root}{p}"))?) };
import_from(&read, fonts)
}
/// Where the package sits in an archive (ADR 0086): at its root, as Animate saves a `.fla`, or
/// inside one top folder (`walk/DOMDocument.xml`), as zipping an XFL folder makes it. The
/// `__MACOSX` folder some zip tools add beside it is ignored. An archive holding several
/// documents in top folders is refused with their names rather than one picked.
fn package_root(archive: &crate::zip::Archive<'_>) -> Result<String, XflError> {
if archive.names().any(|n| n == "DOMDocument.xml") {
return Ok(String::new());
}
let mut tops: Vec<&str> = archive.names().filter_map(|n| n.strip_suffix("/DOMDocument.xml")).filter(|top| !top.contains('/') && *top != "__MACOSX").collect();
tops.sort_unstable();
match tops.as_slice() {
[] => Ok(String::new()),
[top] => Ok(format!("{top}/")),
many => {
let names: Vec<String> = many.iter().take(5).map(|t| t.chars().take(80).collect()).collect();
let more = if many.len() > 5 { format!(" and {} more", many.len() - 5) } else { String::new() };
Err(XflError::NotXfl(format!("the archive holds {} Animate documents ({}{more}): unzip it and open one of them", many.len(), names.join(", "))))
}
}
}
/// Whether the file at `path` starts as a zip archive or an old binary `.fla` does.
fn looks_like_fla(path: &std::path::Path) -> bool {
use std::io::Read;
+2 -1
View File
@@ -9,11 +9,12 @@
pub mod edges;
pub mod import;
pub mod text;
pub mod video;
/// The shared XML reader (`anicraft-xml`), under its old path.
pub use anicraft_xml as xml;
pub mod zip;
pub use import::{MAX_FLA_BYTES, Report, XflError, import_fla, import_fla_with_fonts, import_path, import_with, import_with_fonts};
pub use import::{MAX_FLA_BYTES, MAX_NAMED, Report, XflError, import_fla, import_fla_with_fonts, import_path, import_with, import_with_fonts};
#[cfg(test)]
mod tests;
+76 -3
View File
@@ -574,10 +574,12 @@ fn sounds_in_the_package_come_across_on_frames_and_buttons_states() {
assert_eq!(on(1).map(|s| (s.sound, s.sync)), Some((click, anicraft_doc::SoundSync::Event)));
assert_eq!(on(2).map(|s| (s.sound, s.sync)), Some((click, anicraft_doc::SoundSync::Stream)));
assert_eq!(on(3), None);
assert_eq!(report.skipped.get("sounds stored in Animate's own format"), Some(&1));
assert_eq!(report.skipped.get("missing sound files"), Some(&1));
assert_eq!(on(2).map(|s| s.effect), Some(anicraft_doc::sound::SoundEffect::FadeIn), "the effect comes across (ADR 0086)");
// Sounds that don't come across are named, with their format (ADR 0086).
assert_eq!(report.names.get("sounds in formats AniCraft can't decode"), Some(&vec!["private.mp3: Animate's own sound data".to_string()]));
assert_eq!(report.names.get("missing sound files"), Some(&vec!["gone.wav".to_string()]));
assert_eq!(report.skipped.get("frame sounds whose sound isn't in the package"), Some(&1));
assert_eq!(report.skipped.get("sound effects and envelopes (played without)"), Some(&1));
assert!(report.summary().contains("1 missing sound files (gone.wav)"), "{}", report.summary());
// In the player, going over the button starts its Over sound.
use anicraft_doc::player::{Event, NoScripts, input, start};
let s = start(&doc, &mut NoScripts);
@@ -596,3 +598,74 @@ fn a_document_without_a_background_colour_has_a_white_stage() {
assert_eq!(doc.stage.background, Rgba::WHITE);
assert_eq!((doc.stage.width, doc.stage.height), (550, 400), "Animate's default size, also left out");
}
/// ADR 0086: hostile sound data (cut short, sizes that lie, a sample count far past the file)
/// never panics, never allocates past the file, and is either read for what it holds or named
/// in the report.
#[test]
fn hostile_sound_files_are_bounded_and_named() {
let doc = r#"<DOMDocument><media><DOMSoundItem name="s.wav" href="s.wav" sampleCount="99999999999999" format="44kHz 16bit Stereo"/></media>
<timelines><DOMTimeline name="Scene 1"><layers><DOMLayer name="L"><frames><DOMFrame index="0" soundName="s.wav" soundLoop="4294967295"/></frames></DOMLayer></layers></DOMTimeline></timelines></DOMDocument>"#;
let open = |wav: Vec<u8>| {
let files: HashMap<String, Vec<u8>> = HashMap::from([("DOMDocument.xml".to_string(), doc.as_bytes().to_vec()), ("LIBRARY/s.wav".to_string(), wav)]);
let read = |p: &str| -> Result<Option<Vec<u8>>, XflError> { Ok(files.get(p).cloned()) };
import_with(&read).unwrap()
};
let good = anicraft_doc::audio::wav::encode(8000, 2, &[0.5; 1600], anicraft_doc::audio::wav::Bits::Int16);
let (d, _) = open(good.clone());
assert_eq!(d.sounds[0].frames, 800);
assert_eq!(d.timeline(0).layers[0].keyframes[0].sound.as_ref().unwrap().repeat, anicraft_doc::sound::MAX_REPEAT, "repeats bounded");
// Every cut: read for what it holds, or named.
for cut in 0..good.len() {
let (d, r) = open(good[..cut].to_vec());
assert!(d.sounds.len() == 1 || r.names.values().flatten().any(|n| n.starts_with("s.wav")), "cut at {cut}: {r:?}");
assert!(d.sounds.iter().all(|s| s.frames <= 800));
}
// The data chunk claiming 4 GB, and the RIFF size lying: only what is there is read.
let mut lying = good.clone();
lying[40..44].copy_from_slice(&0xFFFF_FFF0u32.to_le_bytes());
lying[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
let (d, _) = open(lying);
assert_eq!(d.sounds[0].frames, 800);
// 32 channels of one byte each claimed in a tiny file: no frames, so named, not imported.
let mut wide = good;
wide[22..24].copy_from_slice(&32u16.to_le_bytes());
wide[32..34].copy_from_slice(&64u16.to_le_bytes());
wide.truncate(44 + 10);
let (d, r) = open(wide);
assert!(d.sounds.is_empty() && r.skipped.get("sounds in formats AniCraft can't decode") == Some(&1), "{r:?}");
}
/// ADR 0086: the report names what didn't come across, a few per kind in the summary line.
#[test]
fn the_summary_names_a_few_items_and_counts_the_rest() {
let mut media = String::new();
for i in 0..5 {
media.push_str(&format!(r#"<DOMSoundItem name="gone{i}.wav" href="gone{i}.wav"/>"#));
}
let doc = format!(r#"<DOMDocument><media>{media}</media><timelines><DOMTimeline name="Scene 1"/></timelines></DOMDocument>"#);
let read = |p: &str| -> Result<Option<Vec<u8>>, XflError> { Ok((p == "DOMDocument.xml").then(|| doc.as_bytes().to_vec())) };
let (_, r) = import_with(&read).unwrap();
assert_eq!(r.names["missing sound files"].len(), 5);
assert!(r.summary().contains("5 missing sound files (gone0.wav; gone1.wav; gone2.wav and 2 more)"), "{}", r.summary());
}
/// ADR 0086: a `.fla` whose package sits in one top folder opens; one with the package at its
/// root and a folder beside it opens the root one; several documents in top folders are
/// refused with their names.
#[test]
fn a_package_in_one_top_folder_opens_and_several_are_named() {
use crate::zip::pack::{Raw, write};
let doc = br#"<DOMDocument width="99"><timelines><DOMTimeline name="Scene 1"/></timelines></DOMDocument>"#;
let other = br#"<DOMDocument width="42"><timelines><DOMTimeline name="Scene 1"/></timelines></DOMDocument>"#;
let fla = |entries: &[(&str, &[u8])]| write(&entries.iter().map(|(n, b)| Raw::new(n, b, true)).collect::<Vec<_>>(), false);
let (d, _) = crate::import_fla(&fla(&[("Walk/DOMDocument.xml", doc), ("__MACOSX/Walk/._DOMDocument.xml", b"\0\x05\x16\x07")])).unwrap();
assert_eq!(d.stage.width, 99);
let (d, _) = crate::import_fla(&fla(&[("DOMDocument.xml", doc), ("Old/DOMDocument.xml", other)])).unwrap();
assert_eq!(d.stage.width, 99, "the package at the root wins");
let e = crate::import_fla(&fla(&[("Walk/DOMDocument.xml", doc), ("Run/DOMDocument.xml", other)])).err().unwrap().to_string();
assert!(e.contains("2 Animate documents (Run, Walk)"), "{e}");
// Deeper than one folder isn't looked for.
let e = crate::import_fla(&fla(&[("a/b/DOMDocument.xml", doc)])).err().unwrap().to_string();
assert!(e.contains("no DOMDocument.xml"), "{e}");
}
+224
View File
@@ -0,0 +1,224 @@
//! Video items in Animate documents (ADR 0086). AniCraft has no video playback, so an embedded
//! video comes across as a still of its first frame when its stream is one AniCraft can read,
//! and is otherwise named in the report with its codec and length.
//!
//! Written from the public *Video File Format Specification* (FLV, version 10) and the *SWF File
//! Format Specification* (version 19), both published by Adobe as open specifications: the FLV
//! header and tags, and the **Screen Video** codec (codec 3), whose frames are blocks of
//! zlib-compressed BGR pixels. That is the one embedded-video codec here that is both public and
//! free of patents; Sorenson H.263, On2 VP6, Screen Video 2 and H.264 are named, not decoded.
//!
//! Every length is checked against the bytes it comes from; tags, blocks and pixels are bounded.
//! Never panics.
use anicraft_doc::MAX_BITMAP_EDGE;
/// Most FLV tags looked through for the first video frame (audio and script tags come first).
const MAX_TAGS: usize = 4096;
/// What a video's data turned out to be.
#[derive(Debug, Clone, PartialEq)]
pub enum Video {
/// Its first frame, as straight RGBA8 pixels.
Frame { width: u32, height: u32, rgba: Vec<u8> },
/// A video AniCraft can't show, by the name of its format ("FLV, On2 VP6").
Unreadable(String),
}
fn u24(d: &[u8], at: usize) -> Option<usize> {
let b = d.get(at..at.checked_add(3)?)?;
Some((usize::from(b[0]) << 16) | (usize::from(b[1]) << 8) | usize::from(b[2]))
}
fn u32_be(d: &[u8], at: usize) -> Option<usize> {
let b = d.get(at..at.checked_add(4)?)?;
usize::try_from(u32::from_be_bytes([b[0], b[1], b[2], b[3]])).ok()
}
/// The FLV codec ids, by name (FLV specification, `VIDEODATA`).
fn codec_name(id: u8) -> &'static str {
match id {
1 => "JPEG",
2 => "Sorenson H.263",
3 => "Screen Video",
4 => "On2 VP6",
5 => "On2 VP6 with alpha",
6 => "Screen Video 2",
7 => "H.264",
_ => "an unknown codec",
}
}
/// Reads a video's data: an FLV file's first Screen Video key frame, else the format's name.
pub fn read(data: &[u8]) -> Video {
if data.starts_with(b"FLV") {
return flv(data).unwrap_or_else(|| Video::Unreadable("a damaged FLV file".into()));
}
let name = if data.get(4..8) == Some(b"ftyp") {
"MPEG-4 or QuickTime video"
} else if data.starts_with(&[0x1A, 0x45, 0xDF, 0xA3]) {
"WebM or Matroska video"
} else if data.starts_with(b"RIFF") && data.get(8..12) == Some(b"AVI ") {
"AVI video"
} else {
"Animate's own video data"
};
Video::Unreadable(name.into())
}
/// The first video tag of an FLV file. `None` when the file is cut short or lies.
fn flv(data: &[u8]) -> Option<Video> {
// Header: "FLV", version, flags, header size; then the size of the tag before (0).
let mut at = u32_be(data, 5)?.checked_add(4)?;
for _ in 0..MAX_TAGS {
// Tag: type, data size (24 bits), time stamp (24 + 8 bits), stream id (24 bits).
let kind = *data.get(at)?;
let size = u24(data, at + 1)?;
let body = data.get(at.checked_add(11)?..at.checked_add(11)?.checked_add(size)?)?;
if kind & 0x1f == 9 {
let first = *body.first()?;
let (frame_type, codec) = (first >> 4, first & 0x0f);
if codec != 3 {
return Some(Video::Unreadable(format!("FLV, {}", codec_name(codec))));
}
if frame_type != 1 {
return Some(Video::Unreadable("FLV, Screen Video starting without a key frame".into()));
}
return Some(screen_video(body.get(1..)?).unwrap_or_else(|| Video::Unreadable("FLV, damaged Screen Video".into())));
}
// The next tag, after this one's trailing size.
at = at.checked_add(11)?.checked_add(size)?.checked_add(4)?;
}
Some(Video::Unreadable("FLV with no video frame near its start".into()))
}
/// A Screen Video key frame (SWF specification, `SCREENVIDEOPACKET`): block and image sizes,
/// then every block bottom row first, left to right, each a 16-bit size and zlib-compressed BGR
/// pixels, bottom row first. A key frame has every block.
fn screen_video(p: &[u8]) -> Option<Video> {
let h0 = u16::from_be_bytes([*p.first()?, *p.get(1)?]);
let h1 = u16::from_be_bytes([*p.get(2)?, *p.get(3)?]);
let (bw, w) = ((usize::from(h0 >> 12) + 1) * 16, usize::from(h0 & 0x0fff));
let (bh, h) = ((usize::from(h1 >> 12) + 1) * 16, usize::from(h1 & 0x0fff));
if w == 0 || h == 0 || w > MAX_BITMAP_EDGE as usize || h > MAX_BITMAP_EDGE as usize {
return None;
}
let mut rgba = vec![0u8; w * h * 4];
let mut at = 4usize;
// Block rows from the bottom of the picture.
for by in 0..h.div_ceil(bh) {
for bx in 0..w.div_ceil(bw) {
let (x0, y0) = (bx * bw, by * bh);
let (cw, ch) = (bw.min(w - x0), bh.min(h - y0));
let size = usize::from(u16::from_be_bytes([*p.get(at)?, *p.get(at + 1)?]));
let packed = p.get(at + 2..at + 2 + size)?;
at += 2 + size;
if size == 0 {
// An unchanged block can't be in a key frame.
return None;
}
let want = cw * ch * 3;
let bgr = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(packed, want).ok()?;
if bgr.len() != want {
return None;
}
for (row, line) in bgr.chunks_exact(cw * 3).enumerate() {
// Rows count up from the bottom; the bitmap's rows count down from the top.
let y = h - 1 - (y0 + row);
for (col, px) in line.chunks_exact(3).enumerate() {
let i = (y * w + x0 + col) * 4;
rgba.get_mut(i..i + 4)?.copy_from_slice(&[px[2], px[1], px[0], 255]);
}
}
}
}
Some(Video::Frame { width: w as u32, height: h as u32, rgba })
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
/// An FLV file holding one Screen Video key frame of `w × h` pixels coloured by `px(x, y)`
/// (from the top left), in `block`-pixel blocks, after an audio tag.
pub(crate) fn screen_flv(w: usize, h: usize, block: usize, px: impl Fn(usize, usize) -> [u8; 3]) -> Vec<u8> {
let mut packet = vec![0x13u8];
let n = (block / 16 - 1) as u16;
packet.extend_from_slice(&((n << 12) | w as u16).to_be_bytes());
packet.extend_from_slice(&((n << 12) | h as u16).to_be_bytes());
for by in 0..h.div_ceil(block) {
for bx in 0..w.div_ceil(block) {
let (x0, y0) = (bx * block, by * block);
let mut bgr = Vec::new();
for row in 0..block.min(h - y0) {
for col in 0..block.min(w - x0) {
let [r, g, b] = px(x0 + col, h - 1 - (y0 + row));
bgr.extend_from_slice(&[b, g, r]);
}
}
let z = miniz_oxide::deflate::compress_to_vec_zlib(&bgr, 6);
packet.extend_from_slice(&(z.len() as u16).to_be_bytes());
packet.extend_from_slice(&z);
}
}
let mut out = b"FLV\x01\x05\x00\x00\x00\x09\x00\x00\x00\x00".to_vec();
let mut tag = |kind: u8, body: &[u8]| {
out.push(kind);
out.extend_from_slice(&(body.len() as u32).to_be_bytes()[1..]);
out.extend_from_slice(&[0; 7]);
out.extend_from_slice(body);
out.extend_from_slice(&(body.len() as u32 + 11).to_be_bytes());
};
tag(8, &[0x2a, 0, 0, 0]);
tag(9, &packet);
out
}
#[test]
fn a_screen_video_key_frame_decodes_with_its_blocks_in_place() {
// 40 × 20 in 16-pixel blocks: partial blocks on the right and at the top.
let px = |x: usize, y: usize| [(x * 6) as u8, (y * 12) as u8, if x < 20 { 255 } else { 0 }];
let Video::Frame { width, height, rgba } = read(&screen_flv(40, 20, 16, px)) else { panic!("no frame") };
assert_eq!((width, height), (40, 20));
for (x, y) in [(0, 0), (39, 0), (0, 19), (39, 19), (17, 3), (21, 18)] {
let i = (y * 40 + x) * 4;
let [r, g, b] = px(x, y);
assert_eq!(&rgba[i..i + 4], &[r, g, b, 255], "({x}, {y})");
}
}
#[test]
fn other_codecs_and_formats_are_named() {
let mut flv = screen_flv(16, 16, 16, |_, _| [0; 3]);
// The video tag's first byte, after the header, the audio tag and its size.
let video = 13 + 11 + 4 + 4 + 11;
for (codec, name) in [(2u8, "FLV, Sorenson H.263"), (4, "FLV, On2 VP6"), (7, "FLV, H.264")] {
flv[video] = 0x10 | codec;
assert_eq!(read(&flv), Video::Unreadable(name.into()));
}
assert_eq!(read(b"\0\0\0\x18ftypmp42"), Video::Unreadable("MPEG-4 or QuickTime video".into()));
assert_eq!(read(b"garbage"), Video::Unreadable("Animate's own video data".into()));
}
#[test]
fn damaged_screen_video_never_panics() {
let good = screen_flv(40, 20, 32, |x, y| [x as u8, y as u8, 7]);
for cut in 0..good.len() {
let _ = read(&good[..cut]);
}
let mut rng = 0x2545_F491_4F6C_DD1Du64;
for _ in 0..2000 {
let mut bad = good.clone();
rng ^= rng << 13;
rng ^= rng >> 7;
rng ^= rng << 17;
let i = (rng % bad.len() as u64) as usize;
bad[i] = (rng >> 32) as u8;
let _ = read(&bad);
}
// A tag size far past the end, and an image claiming the largest size the bits allow.
let mut lying = good.clone();
lying[14..17].copy_from_slice(&[0xff, 0xff, 0xff]);
assert!(matches!(read(&lying), Video::Unreadable(_)));
}
}
+31 -7
View File
@@ -5,7 +5,9 @@
//! 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.
//! error. An archive with data before it (a self-extracting stub) is read too: its end record
//! is found within the bounded search at the end, and offsets that count from the zip data
//! are moved by the stub's length (ADR 0086).
//!
//! 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
@@ -240,10 +242,13 @@ fn find_end(data: &[u8]) -> Result<(u64, u64, u64, usize), ZipError> {
// 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 stated = u64_at(data, locator + 8).and_then(|a| usize::try_from(a).ok()).filter(|&a| a < locator).ok_or(missing.clone())?;
// A self-extracting archive's stub moves every offset: the ZIP64 end record (with no
// extensible data, as writers make it) then sits just before its locator instead.
let at = match stated {
a if u32_at(data, a) == Some(END64) => a,
_ => locator.checked_sub(56).filter(|&a| a > stated && u32_at(data, a) == Some(END64)).ok_or(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())?);
@@ -253,11 +258,30 @@ fn find_end(data: &[u8]) -> Result<(u64, u64, u64, usize), ZipError> {
Ok((count, u64_at(data, at + 40).ok_or(cut.clone())?, u64_at(data, at + 48).ok_or(cut)?, at))
}
/// Where the central directory really starts, and how far every offset the archive gives is
/// moved: `0` for a plain archive, the stub's length for a self-extracting one whose offsets
/// count from the zip data rather than from the file's start (a stub, a program or another
/// file, put before the archive without fixing its offsets). The directory must end where the
/// end record begins for the stated offsets to be corrected, and a central header must be
/// there: nothing else is guessed.
fn locate_directory(data: &[u8], count: u64, size: u64, stated: u64, limit: usize) -> Option<(usize, usize)> {
let stated = usize::try_from(stated).ok()?;
if count == 0 || u32_at(data, stated) == Some(CENTRAL) {
return Some((stated, 0));
}
let actual = limit.checked_sub(usize::try_from(size).ok()?)?;
if actual > stated && u32_at(data, actual) == Some(CENTRAL) {
return Some((actual, actual - stated));
}
// Neither: the checks that follow name what is wrong.
Some((stated, 0))
}
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_at, shift) = locate_directory(data, count, dir_size, dir_at, dir_limit).ok_or(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 {
@@ -304,7 +328,7 @@ impl<'a> Archive<'a> {
}
}
let outside = || ZipError::DamagedEntry(shown(), "is outside the archive");
let offset = usize::try_from(offset).map_err(|_| outside())?;
let offset = usize::try_from(offset).ok().and_then(|o| o.checked_add(shift)).ok_or_else(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);
+35 -2
View File
@@ -42,9 +42,11 @@ fn every_layout_a_writer_uses_reads_the_same() {
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);
assert_eq!(a.counts().0, 3 + usize::from(v.mimetype) + 2 * usize::from(v.macosx), "{}", v.name);
// The zip layer reads paths as they are; the importer finds a top folder (ADR 0086).
let top = v.top.map(|t| format!("{t}/")).unwrap_or_default();
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);
assert_eq!(a.read(&format!("{top}{path}")).unwrap_or_else(|e| panic!("{}: {e}", v.name)).as_deref(), Some(body.as_slice()), "{}: {path}", v.name);
}
}
}
@@ -282,3 +284,34 @@ fn errors_read_as_sentences() {
}
assert_eq!(ZipError::TooMuch.to_string(), "the archive asks for more than 1024 MB in all");
}
/// ADR 0086: a self-extracting archive (a program before the zip data) reads the same whether
/// its offsets count from the zip data or from the file's start, and the stub really does move
/// them in the first case.
#[test]
fn a_stub_before_the_archive_is_found_past() {
let files: std::collections::BTreeMap<String, Vec<u8>> = [("DOMDocument.xml", "<DOMDocument/>"), ("LIBRARY/a.xml", "<DOMSymbolItem/>")].iter().map(|(p, b)| (p.to_string(), b.as_bytes().to_vec())).collect();
let stub = pack::stub_bytes().len();
for zip64 in [false, true] {
let base = pack::Variant { name: "", zip64, ..pack::VARIANTS[0] };
for stub_kind in [pack::Stub::Shifted, pack::Stub::Adjusted] {
let z = pack::pack(&files, &pack::Variant { stub: stub_kind, ..base });
let a = Archive::open(&z).unwrap_or_else(|e| panic!("{stub_kind:?}, ZIP64 {zip64}: {e}"));
assert_eq!(a.read("LIBRARY/a.xml").unwrap().unwrap(), b"<DOMSymbolItem/>");
}
if !zip64 {
// The plain end record's directory offset counts from the zip data: past the stub.
let z = pack::pack(&files, &pack::Variant { stub: pack::Stub::Shifted, ..base });
let end = z.len() - 22;
let stated = u32::from_le_bytes(z[end + 16..end + 20].try_into().unwrap()) as usize;
assert_eq!(&z[stated + stub..stated + stub + 4], b"PK\x01\x02");
}
}
// A stub and a directory that doesn't end where the end record begins: nothing to correct
// by, so refused readably rather than guessed at.
let mut z = pack::pack(&files, &pack::Variant { stub: pack::Stub::Shifted, ..pack::VARIANTS[0] });
let end = z.len() - 22;
z.splice(end..end, *b"junk");
let e = Archive::open(&z).err().unwrap();
assert!(matches!(e, ZipError::DamagedEntry(..) | ZipError::Damaged(_)), "{e}");
}
+101 -1
View File
@@ -66,6 +66,16 @@ const REPORTS: &[(&str, &[(&str, usize)])] = &[
("scenes", &[("frame comments", 1)]),
("scripts", &[]),
("shapes", &[]),
(
"sounds",
&[
("frame sounds whose sound isn't in the package", 1),
("missing sound files", 1),
("sound in and out points (played whole)", 1),
("sounds in formats AniCraft can't decode", 3),
("unknown sound effects (played without)", 1),
],
),
(
"symbols",
&[("instances of missing symbols", 1), ("missing library files", 1), ("reverse loop modes (played forward)", 1)],
@@ -73,6 +83,32 @@ const REPORTS: &[(&str, &[(&str, usize)])] = &[
("text", &[("text fields (no fonts on this computer)", 4)]),
("textfields", &[("text fields (no fonts on this computer)", 4)]),
("tweens", &[("motion tweens (kept as keyframes)", 1), ("tween spins (turned the shortest way)", 1)]),
("video", &[("instances of videos AniCraft can't play", 1), ("videos (first frame kept as a still)", 1), ("videos AniCraft can't play", 2)]),
];
/// Items named under one kind of report line.
type Named = (&'static str, &'static [&'static str]);
/// The items each package's report names (ADR 0086), by kind, in the order the document lists
/// them; packages not listed name none.
const NAMES: &[(&str, &[Named])] = &[
(
"sounds",
&[
("missing sound files", &["lost.wav"]),
(
"sounds in formats AniCraft can't decode",
&["theme.aif: AIFF", "voice.wav: WAV in IMA ADPCM", "Sounds/loop: Animate's own sound data"],
),
],
),
(
"video",
&[
("videos (first frame kept as a still)", &["Clips/screen.flv: Screen Video, 2.0 s"]),
("videos AniCraft can't play", &["intro.flv: FLV, On2 VP6, 4.5 s", "gone.mp4: its file is missing, 1.0 s"]),
],
),
];
/// What each package keeps without running (ADR 0013 tier 1: scripts outside the subset, and
@@ -81,8 +117,10 @@ const KEPT: &[(&str, &[(&str, usize)])] = &[
("bitmaps", &[("library folders", 1)]),
("nineslice", &[("library folders", 1)]),
("scripts", &[("document class", 1), ("frame scripts in ActionScript 3.0", 1), ("linkage classes", 1)]),
("sounds", &[("library folders", 1)]),
("symbols", &[("library folders", 1)]),
("textfields", &[("text field variables (ActionScript 1.0 and 2.0)", 1)]),
("video", &[("library folders", 1)]),
];
/// The frame scripts each package brings that run (ADR 0013 tier 2: in the subset).
@@ -110,6 +148,8 @@ fn each_package_reports_exactly_what_it_uses_that_does_not_come_across() {
assert_eq!(report.kept, kept, "{case}: kept, not run");
let runs: BTreeMap<String, usize> = RUNS.iter().find(|(c, _)| c == case).map(|(_, k)| k.iter().map(|(k, n)| (k.to_string(), *n)).collect()).unwrap_or_default();
assert_eq!(report.runs, runs, "{case}: frame scripts that run");
let names: BTreeMap<String, Vec<String>> = NAMES.iter().find(|(c, _)| c == case).map(|(_, k)| k.iter().map(|(k, n)| (k.to_string(), n.iter().map(|s| s.to_string()).collect())).collect()).unwrap_or_default();
assert_eq!(report.names, names, "{case}: the items named");
assert!(report.notes.is_empty() && report.missing_fonts.is_empty(), "{case}: {report:?}");
}
}
@@ -323,6 +363,65 @@ fn bitmaps_come_from_png_and_jpeg_files() {
assert_eq!(evaluate(&doc, 0).items.iter().filter(|i| i.image.is_some()).count(), 5);
}
/// ADR 0086: the sounds AniCraft decodes come into the Library, in their folder, and every
/// frame keeps its sound's sync, repeats and effect.
#[test]
fn sounds_come_across_with_their_sync_repeats_and_effects() {
use anicraft_doc::sound::{EnvelopePoint, SoundEffect};
use anicraft_doc::{LibraryEntry, LibraryItem, SoundSync};
let (doc, _) = import("sounds");
let sounds: Vec<(&str, u32, u16)> = doc.sounds.iter().map(|s| (s.name.as_str(), s.rate, s.channels)).collect();
assert_eq!(sounds, [("beep.wav", 11025, 1), ("tone.mp3", 22050, 1)]);
assert_eq!(doc.sounds[0].frames, 11025 / 4);
for s in &doc.sounds {
assert_eq!(doc.entry_path(LibraryEntry::Item(LibraryItem::Sound(s.id))), format!("Sounds/{}", s.name));
}
let (beep, tone) = (doc.sounds[0].id, doc.sounds[1].id);
let music = doc.timeline(0).layers[0].keyframes[0].sound.clone().unwrap();
assert_eq!((music.sound, music.sync, music.looping), (tone, SoundSync::Stream, true));
let got: Vec<_> = doc.timeline(0).layers[1].keyframes.iter().map(|k| (k.start, k.sound.as_ref().map(|s| (s.sound, s.sync, s.repeat, s.effect.clone())))).collect();
let custom = SoundEffect::Custom(vec![EnvelopePoint::new(0.0, 1.0, 0.0), EnvelopePoint::new(0.25, 0.25, 1.0)]);
assert_eq!(
got,
[
(0, Some((beep, SoundSync::Event, 1, SoundEffect::LeftChannel))),
(4, Some((beep, SoundSync::Event, 1, SoundEffect::FadeRightToLeft))),
(8, Some((beep, SoundSync::Event, 1, custom))),
(12, Some((beep, SoundSync::Start, 2, SoundEffect::FadeOut))),
// An effect AniCraft doesn't know plays without one (reported).
(16, Some((beep, SoundSync::Event, 1, SoundEffect::None))),
(18, Some((beep, SoundSync::Stop, 1, SoundEffect::None))),
// In and out points are reported; the sound plays whole.
(20, Some((beep, SoundSync::Event, 1, SoundEffect::None))),
// Its sound is in a format AniCraft can't decode.
(22, None),
]
);
// The soundtrack hears them: the stream across the movie and the beeps where they start.
let voices = anicraft_doc::sound::voices(&doc, 44_100);
assert!(voices.len() >= 6, "{}", voices.len());
}
/// ADR 0086: a Screen Video comes across as a still of its first frame, which its instance
/// shows at the instance's frame size; the other videos are named, not shown.
#[test]
fn a_screen_video_comes_across_as_its_first_frame() {
use anicraft_doc::{LibraryEntry, LibraryItem};
let (doc, _) = import("video");
let b: Vec<(&str, u32, u32)> = doc.bitmaps.iter().map(|b| (b.name.as_str(), b.width, b.height)).collect();
assert_eq!(b, [("screen.flv (first frame)", 64, 48)]);
assert_eq!(doc.entry_path(LibraryEntry::Item(LibraryItem::Bitmap(doc.bitmaps[0].id))), "Clips/screen.flv (first frame)");
let px = doc.bitmaps[0].pixels().unwrap();
let at = |x: i64, y: i64| px.at(x, y).map(|c| (c * 255.0).round() as u8);
// Top left red, top right green, bottom left blue, bottom right yellow: the blocks (32 px,
// bottom row first, the top row partial) and their rows (bottom first) in place.
assert_eq!([at(0, 0), at(63, 0), at(0, 47), at(63, 47), at(31, 23), at(32, 24)], [[230, 57, 70, 255], [42, 157, 143, 255], [38, 70, 200, 255], [233, 196, 106, 255], [230, 57, 70, 255], [233, 196, 106, 255]]);
let frame = evaluate(&doc, 0);
let still: Vec<_> = frame.items.iter().filter(|i| i.image.is_some()).collect();
assert_eq!(still.len(), 1, "only the Screen Video shows");
assert_eq!(still[0].transform.m, [1.5, 0.0, 0.0, 1.5, 20.0, 20.0], "sized to its frame (96 × 72) and moved");
}
#[test]
fn filters_and_blend_modes_come_across() {
let (doc, _) = import("filters");
@@ -434,7 +533,8 @@ fn hostile_variants_of_every_package_never_panic() {
let mut variants = 0;
for case in cases() {
let original = files(&case);
for (path, bytes) in original.iter().filter(|(p, _)| p.ends_with(".xml") || p.ends_with(".png") || p.ends_with(".jpg")) {
let media = [".xml", ".png", ".jpg", ".wav", ".mp3", ".aif", ".flv", ".dat"];
for (path, bytes) in original.iter().filter(|(p, _)| media.iter().any(|m| p.ends_with(m))) {
let mut rng = Rng(0x9E37_79B9_7F4A_7C15 ^ (path.len() as u64 * 0x1000_0001) ^ (case.len() as u64) << 32);
// Cut short at evenly spaced points (and just after the start and before the end).
let cuts = (0..24).map(|k| bytes.len() * k / 24).chain([1, bytes.len().saturating_sub(1)]);
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- AniCraft XFL corpus: sounds (ADR 0057, ADR 0086). The sound files are original:
LIBRARY/Sounds/beep.wav (a quarter second of 880 Hz, 16-bit mono at 11025 Hz, made by a
script), LIBRARY/Sounds/tone.mp3 (the audio crate's test tone), LIBRARY/theme.aif (an AIFF
header) and LIBRARY/voice.wav (a WAV in IMA ADPCM), and bin/M 3 1.dat (bytes in none of
the formats AniCraft reads, standing in for Animate's own sound data). A streamed, looping
soundtrack; event, start and stop sounds with every kind of effect (presets by name, a
custom envelope, an unknown name); repeats; in and out points; a frame whose sound is in a
format AniCraft can't decode, and a sound whose file is missing. Self-authored. -->
<DOMDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ns.adobe.com/xfl/2008/" width="320" height="180" frameRate="24" backgroundColor="#F4F1EA">
<folders>
<DOMFolderItem name="Sounds" itemID="00000000-0000-0000-0000-000000000101"/>
</folders>
<media>
<DOMSoundItem name="Sounds/beep.wav" itemID="00000000-0000-0000-0000-000000000102" sourceExternalFilepath="./LIBRARY/Sounds/beep.wav" href="Sounds/beep.wav" soundDataHRef="M 1 1.dat" format="11kHz 16bit Mono" sampleCount="2756"/>
<DOMSoundItem name="Sounds/tone.mp3" itemID="00000000-0000-0000-0000-000000000103" sourceExternalFilepath="./LIBRARY/Sounds/tone.mp3" href="Sounds/tone.mp3" soundDataHRef="M 2 2.dat" format="22kHz 16bit Mono" exportFormat="1" exportBits="7"/>
<DOMSoundItem name="theme.aif" itemID="00000000-0000-0000-0000-000000000104" href="theme.aif" format="44kHz 16bit Mono"/>
<DOMSoundItem name="voice.wav" itemID="00000000-0000-0000-0000-000000000105" href="voice.wav" format="8kHz 4bit Mono"/>
<DOMSoundItem name="Sounds/loop" itemID="00000000-0000-0000-0000-000000000106" soundDataHRef="M 3 1.dat" format="22kHz 16bit Stereo"/>
<DOMSoundItem name="lost.wav" itemID="00000000-0000-0000-0000-000000000107" href="lost.wav" soundDataHRef="M 9 9.dat"/>
</media>
<timelines>
<DOMTimeline name="Scene 1">
<layers>
<DOMLayer name="Music" color="#4F80FF">
<frames>
<DOMFrame index="0" duration="24" keyMode="9728" soundName="Sounds/tone.mp3" soundSync="stream" soundLoopMode="loop"/>
</frames>
</DOMLayer>
<DOMLayer name="Effects" color="#FF4F4F">
<frames>
<DOMFrame index="0" duration="4" keyMode="9728" soundName="Sounds/beep.wav" soundEffect="left channel"/>
<DOMFrame index="4" duration="4" keyMode="9728" soundName="Sounds/beep.wav" soundEffect="fade right to left"/>
<DOMFrame index="8" duration="4" keyMode="9728" soundName="Sounds/beep.wav" soundEffect="custom">
<SoundEnvelope>
<SoundEnvelopePoint mark44="0" level0="32768" level1="0"/>
<SoundEnvelopePoint mark44="11025" level0="8192" level1="32768"/>
</SoundEnvelope>
</DOMFrame>
<DOMFrame index="12" duration="4" keyMode="9728" soundName="Sounds/beep.wav" soundSync="start" soundLoopMode="repeat" soundLoop="2" soundEffect="fade out"/>
<DOMFrame index="16" duration="2" keyMode="9728" soundName="Sounds/beep.wav" soundEffect="wobble"/>
<DOMFrame index="18" duration="2" keyMode="9728" soundName="Sounds/beep.wav" soundSync="stop"/>
<DOMFrame index="20" duration="2" keyMode="9728" soundName="Sounds/beep.wav" inPoint44="441" outPoint44="8820"/>
<DOMFrame index="22" duration="2" keyMode="9728" soundName="voice.wav"/>
</frames>
</DOMLayer>
<DOMLayer name="Art" color="#9933CC">
<frames>
<DOMFrame index="0" duration="24" keyMode="9728">
<elements>
<DOMRectangleObject x="20" y="40" objectWidth="280" objectHeight="100">
<fill><SolidColor color="#2A9D8F"/></fill>
</DOMRectangleObject>
<DOMOvalObject x="140" y="70" objectWidth="40" objectHeight="40">
<fill><SolidColor color="#E9C46A"/></fill>
</DOMOvalObject>
</elements>
</DOMFrame>
</frames>
</DOMLayer>
</layers>
</DOMTimeline>
</timelines>
</DOMDocument>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

@@ -0,0 +1 @@
PROXY-CS5
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- AniCraft XFL corpus: video (ADR 0086). LIBRARY/Clips/screen.flv is original: one 64x48
Screen Video key frame in four coloured quarters, in 32-pixel blocks, made by a script;
bin/M 4 2.dat is an FLV whose video is On2 VP6 (only its header is real). A Screen Video
instance scaled by its frame size and moved, an instance of the VP6 video, and a video
whose file is missing. Self-authored. -->
<DOMDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ns.adobe.com/xfl/2008/" width="320" height="180" frameRate="12" backgroundColor="#FFFFFF">
<folders>
<DOMFolderItem name="Clips" itemID="00000000-0000-0000-0000-000000000201"/>
</folders>
<media>
<DOMVideoItem name="Clips/screen.flv" itemID="00000000-0000-0000-0000-000000000202" sourceExternalFilepath="./LIBRARY/Clips/screen.flv" videoDataHRef="M 3 1.dat" videoType="screen share" fps="12" width="64" height="48" length="2"/>
<DOMVideoItem name="intro.flv" itemID="00000000-0000-0000-0000-000000000203" videoDataHRef="M 4 2.dat" videoType="on2 vp6 media" fps="25" width="320" height="180" length="4.5"/>
<DOMVideoItem name="gone.mp4" itemID="00000000-0000-0000-0000-000000000204" href="gone.mp4" fps="24" frameCount="24"/>
</media>
<timelines>
<DOMTimeline name="Scene 1">
<layers>
<DOMLayer name="Video" color="#4F80FF">
<frames>
<DOMFrame index="0" duration="24" keyMode="9728">
<elements>
<DOMVideoInstance libraryItemName="Clips/screen.flv" frameRight="1920" frameBottom="1440">
<matrix><Matrix tx="20" ty="20"/></matrix>
</DOMVideoInstance>
<DOMVideoInstance libraryItemName="intro.flv" frameRight="6400" frameBottom="3600">
<matrix><Matrix tx="0" ty="0"/></matrix>
</DOMVideoInstance>
</elements>
</DOMFrame>
</frames>
</DOMLayer>
<DOMLayer name="Frame" color="#FF4F4F">
<frames>
<DOMFrame index="0" duration="24" keyMode="9728">
<elements>
<DOMRectangleObject x="140" y="20" objectWidth="160" objectHeight="72">
<fill><SolidColor color="#264653"/></fill>
</DOMRectangleObject>
</elements>
</DOMFrame>
</frames>
</DOMLayer>
</layers>
</DOMTimeline>
</timelines>
</DOMDocument>
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

+1
View File
@@ -0,0 +1 @@
PROXY-CS5
+12 -6
View File
@@ -183,14 +183,16 @@ fn survive(fla: &[u8], fonts: &Fonts, what: &str, errors: &mut BTreeMap<String,
}
}
/// Offsets of every central record in `z` (found through the end record, or its ZIP64 form).
fn central_records(z: &[u8]) -> Vec<usize> {
/// Offsets of every central record in `z` (found through the end record, or its ZIP64 form),
/// whose offsets count from `shift` bytes into it (a self-extracting stub's length).
fn central_records(z: &[u8], shift: usize) -> Vec<usize> {
let end = z.len() - 22;
let mut at = u32::from_le_bytes(z[end + 16..end + 20].try_into().unwrap()) as usize;
if at == u32::MAX as usize {
let record = u64::from_le_bytes(z[end - 12..end - 4].try_into().unwrap()) as usize;
let record = u64::from_le_bytes(z[end - 12..end - 4].try_into().unwrap()) as usize + shift;
at = u64::from_le_bytes(z[record + 48..record + 56].try_into().unwrap()) as usize;
}
at += shift;
let mut out = Vec::new();
let mut i = at;
while z.get(i..i + 4) == Some(&[0x50, 0x4b, 1, 2]) {
@@ -215,10 +217,14 @@ fn hostile_fla_archives_never_panic_and_fail_readably() {
let mut variants = 0;
for case in cases() {
let files = pack::package(&corpus().join(&case));
for v in [&pack::VARIANTS[0], &pack::VARIANTS[7]] {
// As Animate writes it, ZIP64, and the new layouts (ADR 0086): a top folder, and a
// self-extracting stub whose offsets count from the zip data.
let fuzzed = ["as Animate writes it (mimetype first, deflated)", "ZIP64", "inside a top folder, with __MACOSX beside it", "self-extracting, offsets from the zip data", "self-extracting ZIP64, offsets from the zip data"];
for v in pack::VARIANTS.iter().filter(|v| fuzzed.contains(&v.name)) {
let z = pack::pack(&files, v);
let mut rng = Rng(0x9E37_79B9_7F4A_7C15 ^ (case.len() as u64) << 20 ^ z.len() as u64);
let records = central_records(&z);
let shift = if v.stub == pack::Stub::Shifted { pack::stub_bytes().len() } else { 0 };
let records = central_records(&z, shift);
assert!(records.len() >= files.len(), "{case}");
// Header fields at edge values: (offset in the central record, width).
const FIELDS: &[(usize, usize)] = &[(8, 2), (10, 2), (16, 4), (20, 4), (24, 4), (28, 2), (30, 2), (32, 2), (34, 2), (42, 4)];
@@ -232,7 +238,7 @@ fn hostile_fla_archives_never_panic_and_fail_readably() {
}
}
// The local header's lengths too.
let local = u32::from_le_bytes(z[rec + 42..rec + 46].try_into().unwrap()) as usize;
let local = u32::from_le_bytes(z[rec + 42..rec + 46].try_into().unwrap()) as usize + shift;
for (off, value) in [(26usize, 0xffffu16), (28, 0xffff), (28, 0), (18, 0xffff), (22, 0)] {
let mut t = z.clone();
if let Some(f) = t.get_mut(local + off..local + off + 2) {
+74 -7
View File
@@ -31,6 +31,24 @@ pub enum Names {
UnicodeExtra,
}
/// Data before the archive, as a self-extracting archive has its program (ADR 0086).
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Stub {
None,
/// A stub put in front without fixing the offsets: they count from the zip data.
Shifted,
/// A stub with the offsets fixed to count from the file's start (`zip -A`).
Adjusted,
}
/// The stand-in program before a self-extracting variant: an `MZ` header and bytes with no
/// zip signature in them.
pub fn stub_bytes() -> Vec<u8> {
let mut out = b"MZ\x90\x00 this stands in for a self-extracting program ".to_vec();
out.extend((0..3000u32).map(|i| b"abcdefghijklmno"[(i * 7 % 15) as usize]));
out
}
/// One way of packing a package.
#[derive(Clone, Copy, Debug)]
pub struct Variant {
@@ -47,9 +65,15 @@ pub struct Variant {
pub descriptors: bool,
/// ZIP64 sizes on every entry and a ZIP64 end record.
pub zip64: bool,
/// Every entry inside one top folder (`<top>/DOMDocument.xml`), as zipping an XFL
/// folder makes it (ADR 0086).
pub top: Option<&'static str>,
/// A `__MACOSX/` folder of resource forks beside the top folder, as macOS's Compress adds.
pub macosx: bool,
pub stub: Stub,
}
const BASE: Variant = Variant { name: "", compression: Compression::Deflate, folders: false, mimetype: false, backslashes: false, names: Names::Utf8Flag, descriptors: false, zip64: false };
const BASE: Variant = Variant { name: "", compression: Compression::Deflate, folders: false, mimetype: false, backslashes: false, names: Names::Utf8Flag, descriptors: false, zip64: false, top: None, macosx: false, stub: Stub::None };
/// Every variant each corpus package is checked in.
pub const VARIANTS: &[Variant] = &[
@@ -61,7 +85,24 @@ pub const VARIANTS: &[Variant] = &[
Variant { name: "Unicode path fields", names: Names::UnicodeExtra, ..BASE },
Variant { name: "data descriptors", descriptors: true, compression: Compression::Mixed, ..BASE },
Variant { name: "ZIP64", zip64: true, compression: Compression::Mixed, ..BASE },
Variant { name: "everything at once", compression: Compression::Mixed, folders: true, mimetype: true, backslashes: true, names: Names::Cp437, descriptors: true, zip64: true },
Variant { name: "inside a top folder", top: Some("Walk Cycle"), folders: true, ..BASE },
Variant { name: "inside a top folder, with __MACOSX beside it", top: Some("walk"), macosx: true, compression: Compression::Mixed, ..BASE },
Variant { name: "self-extracting, offsets from the zip data", stub: Stub::Shifted, mimetype: true, ..BASE },
Variant { name: "self-extracting, offsets from the file's start", stub: Stub::Adjusted, mimetype: true, ..BASE },
Variant { name: "self-extracting ZIP64, offsets from the zip data", stub: Stub::Shifted, zip64: true, ..BASE },
Variant {
name: "everything at once",
compression: Compression::Mixed,
folders: true,
mimetype: true,
backslashes: true,
names: Names::Cp437,
descriptors: true,
zip64: true,
top: Some("Ünïcödé walk"),
macosx: true,
stub: Stub::Shifted,
},
];
/// One entry as written. Fields a hostile test may change are public.
@@ -125,6 +166,12 @@ fn le64(out: &mut Vec<u8>, v: u64) {
/// Writes `entries` as a zip archive, with a ZIP64 end record if `zip64_end`.
pub fn write(entries: &[Raw], zip64_end: bool) -> Vec<u8> {
write_at(entries, zip64_end, 0)
}
/// Writes `entries` as a zip archive whose offsets count from `base` bytes before it (the
/// length of a stub that will be put in front, for offsets fixed to the file's start).
pub fn write_at(entries: &[Raw], zip64_end: bool, base: u64) -> Vec<u8> {
let mut out = Vec::new();
let mut central = Vec::new();
let mut offsets = Vec::new();
@@ -142,7 +189,7 @@ pub fn write(entries: &[Raw], zip64_end: bool) -> Vec<u8> {
let comp = data.len() as u64;
let flags = e.flags | if e.descriptor { 8 } else { 0 } | if e.utf8 { 0x800 } else { 0 };
let version: u16 = if e.zip64 { 45 } else { 20 };
let offset = out.len() as u64;
let offset = base + out.len() as u64;
offsets.push(offset);
// The local header.
le32(&mut out, 0x0403_4b50);
@@ -216,11 +263,11 @@ pub fn write(entries: &[Raw], zip64_end: bool) -> Vec<u8> {
central.extend_from_slice(&e.name);
central.extend_from_slice(&extra);
}
let dir_at = out.len() as u64;
let dir_at = base + out.len() as u64;
out.extend_from_slice(&central);
let n = entries.len() as u64;
if zip64_end {
let record = out.len() as u64;
let record = base + out.len() as u64;
le32(&mut out, 0x0606_4b50);
le64(&mut out, 44);
le16(&mut out, 45);
@@ -251,7 +298,12 @@ pub fn write(entries: &[Raw], zip64_end: bool) -> Vec<u8> {
pub fn entries(files: &BTreeMap<String, Vec<u8>>, v: &Variant) -> Vec<Raw> {
let mut out = Vec::new();
let named = |path: &str, body: &[u8], deflate: bool| {
let shown = if v.backslashes { path.replace('/', "\\") } else { path.to_string() };
let path = match v.top {
Some(top) if path.is_empty() => format!("{top}/"),
Some(top) if !path.starts_with("__MACOSX/") => format!("{top}/{path}"),
_ => path.to_string(),
};
let shown = if v.backslashes { path.replace('/', "\\") } else { path };
let mut r = Raw::new(&shown, body, deflate);
r.descriptor = v.descriptors;
r.zip64 = v.zip64;
@@ -279,6 +331,17 @@ pub fn entries(files: &BTreeMap<String, Vec<u8>>, v: &Variant) -> Vec<Raw> {
out.push(named("mimetype", b"application/vnd.adobe.xfl", false));
}
let mut made = std::collections::BTreeSet::new();
let mut files = files.clone();
if v.macosx {
// AppleDouble files for the document and its folder, as macOS's Compress writes them.
let top = v.top.unwrap_or("walk");
files.insert(format!("__MACOSX/{top}/._DOMDocument.xml"), b"\x00\x05\x16\x07\x00\x02\x00\x00Mac OS X ".to_vec());
files.insert(format!("__MACOSX/._{top}"), b"\x00\x05\x16\x07\x00\x02\x00\x00Mac OS X ".to_vec());
}
if let Some(top) = v.top.filter(|_| v.folders) {
made.insert(format!("{top}/"));
out.push(named("", b"", false));
}
for (i, (path, body)) in files.iter().enumerate() {
if v.folders {
let parts: Vec<&str> = path.split('/').collect();
@@ -302,7 +365,11 @@ pub fn entries(files: &BTreeMap<String, Vec<u8>>, v: &Variant) -> Vec<Raw> {
/// `files` packed as a `.fla` in variant `v`.
pub fn pack(files: &BTreeMap<String, Vec<u8>>, v: &Variant) -> Vec<u8> {
write(&entries(files, v), v.zip64)
let stub = if v.stub == Stub::None { Vec::new() } else { stub_bytes() };
let base = if v.stub == Stub::Adjusted { stub.len() as u64 } else { 0 };
let mut out = stub;
out.extend_from_slice(&write_at(&entries(files, v), v.zip64, base));
out
}
/// A package folder's files by their path inside it, leaving out its `golden/` renders.