mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
Add craft-deckcraft-format: the .deckcraft package layer from DeckCraft (#1748)
The package layer of DeckCraft's native format, moved from storytold/deckcraft crates/format at 0d5c0b85 (lib.rs, finite.rs): the mimetype and legacy SlideCraft type, sniffing, the presentation.json envelope (generic over the app's presentation), the NaN-safe JSON writer (Finite), the reader that patches null numbers in older files, member names and compression choice, and the bounded, budget-charged member reads with the format's limits. The presentation model, repair, media and chart-part mapping, and outline/RTF stay in DeckCraft. Writes go through craft-zip's deterministic writer; the consumer's golden test shows the package bytes are unchanged. Part of #778.
This commit is contained in:
Generated
+11
@@ -1480,6 +1480,17 @@ dependencies = [
|
||||
"proptest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-deckcraft-format"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-zip",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-designcraft-format"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -55,6 +55,7 @@ craft-doc-binary = { path = "crates/doc-binary" }
|
||||
craft-docx = { path = "crates/docx" }
|
||||
craft-document-bundle = { path = "crates/document-bundle" }
|
||||
craft-mcmat = { path = "crates/mcmat" }
|
||||
craft-deckcraft-format = { path = "crates/deckcraft-format" }
|
||||
craft-designcraft-format = { path = "crates/designcraft-format" }
|
||||
craft-dxf = { path = "crates/dxf" }
|
||||
craft-drawingml = { path = "crates/drawingml" }
|
||||
|
||||
@@ -120,6 +120,10 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
pages and version detection, and written back; and `model`, the drawing above the tags (header
|
||||
variables, tables, blocks, layouts, entities) and its writer (R12 to 2018), merged from CADCraft's and VectorCraft's readers. Bounded
|
||||
on hostile input. Mapping the model onto a document stays in each app.
|
||||
- [`craft-deckcraft-format`](crates/deckcraft-format/README.md): the DeckCraft `.deckcraft` package:
|
||||
identity and sniffing, the `presentation.json` envelope with the presentation as a type
|
||||
parameter, NaN-safe JSON and the reader for older files with `null` numbers, member names and
|
||||
compression, and bounded, budgeted member reads. The presentation model and its mapping stay in DeckCraft.
|
||||
- [`craft-dwg`](crates/dwg/README.md): bounded byte-level DWG↔DXF conversion with diagnostics,
|
||||
capped output and an optional child-process worker; the `acadrust` dependency remains isolated
|
||||
behind bytes and is unavailable on wasm32 and FreeBSD/aarch64.
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "craft-deckcraft-format"
|
||||
description = "The DeckCraft .deckcraft package: identity, manifest envelope, NaN-safe JSON, member names and bounded reads"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
craft-zip = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
zip = { version = "9", default-features = false, features = ["deflate-flate2"] }
|
||||
@@ -0,0 +1,25 @@
|
||||
# craft-deckcraft-format
|
||||
|
||||
The DeckCraft `.deckcraft` package. A file is a ZIP that holds, in this order:
|
||||
|
||||
- `mimetype`, stored: `application/vnd.storyteller.deckcraft+zip`. Files from before the
|
||||
SlideCraft rename carry the legacy type, and `sniff` accepts both.
|
||||
- `presentation.json`, deflated: `{"format":"deckcraft","version":1,"generator":…,"presentation":…}`.
|
||||
It is compact JSON, and non-finite numbers are written as `0` (`Finite`).
|
||||
- `media/<id>` and `chart-parts/<index>` binary members. A member whose content type is already
|
||||
compressed (JPEG, PNG, video, MP3/MP4 audio, anything `zip`) is stored, and any other member is deflated.
|
||||
|
||||
The presentation is a type parameter: DeckCraft owns the model, decides which media and chart
|
||||
parts become members, and repairs a loaded deck. This crate owns the bytes:
|
||||
|
||||
- `write(presentation, generator, members)` writes the package with `craft-zip`'s deterministic writer.
|
||||
- `Package::open_manifest(bytes, max)` reads and checks the manifest. A file with no
|
||||
`presentation.json` or a different `format` is `NotOurs`. `parse_manifest` reads files written
|
||||
before `Finite`, where serde_json saved NaN as `null`, as 0 within fixed patch and re-parse budgets.
|
||||
- `Package::read_max` refuses a member that declares more than its limit and stops one that
|
||||
inflates past it. `read_charged` charges a shared budget for every byte it inflates, including
|
||||
the bytes of a read that then fails its CRC.
|
||||
- `Budget` and the `MAX_*` constants are the format's limits: 2 GiB per member, 4 GiB of media and
|
||||
chart parts, 100,000 items and a 256 MiB manifest.
|
||||
|
||||
There is no `unsafe` and no panics, and it builds for `wasm32-unknown-unknown`.
|
||||
@@ -0,0 +1,185 @@
|
||||
//! A serde serializer adapter that writes non-finite floats as 0.
|
||||
//!
|
||||
//! serde_json writes NaN and ±infinity as `null`, which doesn't read back as a number: a single
|
||||
//! NaN anywhere in the model (a rotation, a crop, a stroke width) made a saved file fail to open
|
||||
//! as "damaged". [`Finite`] wraps any `Serialize` value and replaces those floats on the way out;
|
||||
//! everything else passes through unchanged.
|
||||
|
||||
use serde::ser::{self, Serialize, Serializer};
|
||||
|
||||
/// `T`, serialized with non-finite floats written as 0.
|
||||
pub struct Finite<'a, T: ?Sized>(pub &'a T);
|
||||
|
||||
impl<T: ?Sized + Serialize> Serialize for Finite<'_, T> {
|
||||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize(Wrap(s))
|
||||
}
|
||||
}
|
||||
|
||||
struct Wrap<S>(S);
|
||||
|
||||
macro_rules! pass {
|
||||
($($m:ident($t:ty)),* $(,)?) => {
|
||||
$(fn $m(self, v: $t) -> Result<S::Ok, S::Error> { self.0.$m(v) })*
|
||||
};
|
||||
}
|
||||
|
||||
impl<S: Serializer> Serializer for Wrap<S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
type SerializeSeq = Wrap<S::SerializeSeq>;
|
||||
type SerializeTuple = Wrap<S::SerializeTuple>;
|
||||
type SerializeTupleStruct = Wrap<S::SerializeTupleStruct>;
|
||||
type SerializeTupleVariant = Wrap<S::SerializeTupleVariant>;
|
||||
type SerializeMap = Wrap<S::SerializeMap>;
|
||||
type SerializeStruct = Wrap<S::SerializeStruct>;
|
||||
type SerializeStructVariant = Wrap<S::SerializeStructVariant>;
|
||||
|
||||
pass!(
|
||||
serialize_bool(bool),
|
||||
serialize_i8(i8),
|
||||
serialize_i16(i16),
|
||||
serialize_i32(i32),
|
||||
serialize_i64(i64),
|
||||
serialize_i128(i128),
|
||||
serialize_u8(u8),
|
||||
serialize_u16(u16),
|
||||
serialize_u32(u32),
|
||||
serialize_u64(u64),
|
||||
serialize_u128(u128),
|
||||
serialize_char(char),
|
||||
serialize_str(&str),
|
||||
serialize_bytes(&[u8]),
|
||||
);
|
||||
|
||||
fn serialize_f32(self, v: f32) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_f32(if v.is_finite() { v } else { 0.0 })
|
||||
}
|
||||
fn serialize_f64(self, v: f64) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_f64(if v.is_finite() { v } else { 0.0 })
|
||||
}
|
||||
fn serialize_none(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_none()
|
||||
}
|
||||
fn serialize_some<T: ?Sized + Serialize>(self, v: &T) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_some(&Finite(v))
|
||||
}
|
||||
fn serialize_unit(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_unit()
|
||||
}
|
||||
fn serialize_unit_struct(self, name: &'static str) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_unit_struct(name)
|
||||
}
|
||||
fn serialize_unit_variant(self, name: &'static str, i: u32, variant: &'static str) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_unit_variant(name, i, variant)
|
||||
}
|
||||
fn serialize_newtype_struct<T: ?Sized + Serialize>(self, name: &'static str, v: &T) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_newtype_struct(name, &Finite(v))
|
||||
}
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(self, name: &'static str, i: u32, variant: &'static str, v: &T) -> Result<S::Ok, S::Error> {
|
||||
self.0.serialize_newtype_variant(name, i, variant, &Finite(v))
|
||||
}
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, S::Error> {
|
||||
self.0.serialize_seq(len).map(Wrap)
|
||||
}
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, S::Error> {
|
||||
self.0.serialize_tuple(len).map(Wrap)
|
||||
}
|
||||
fn serialize_tuple_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeTupleStruct, S::Error> {
|
||||
self.0.serialize_tuple_struct(name, len).map(Wrap)
|
||||
}
|
||||
fn serialize_tuple_variant(self, name: &'static str, i: u32, variant: &'static str, len: usize) -> Result<Self::SerializeTupleVariant, S::Error> {
|
||||
self.0.serialize_tuple_variant(name, i, variant, len).map(Wrap)
|
||||
}
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, S::Error> {
|
||||
self.0.serialize_map(len).map(Wrap)
|
||||
}
|
||||
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct, S::Error> {
|
||||
self.0.serialize_struct(name, len).map(Wrap)
|
||||
}
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
i: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, S::Error> {
|
||||
self.0.serialize_struct_variant(name, i, variant, len).map(Wrap)
|
||||
}
|
||||
fn is_human_readable(&self) -> bool {
|
||||
self.0.is_human_readable()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! compound {
|
||||
($tr:ident, $m:ident) => {
|
||||
impl<S: ser::$tr> ser::$tr for Wrap<S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn $m<T: ?Sized + Serialize>(&mut self, v: &T) -> Result<(), S::Error> {
|
||||
self.0.$m(&Finite(v))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
compound!(SerializeSeq, serialize_element);
|
||||
compound!(SerializeTuple, serialize_element);
|
||||
compound!(SerializeTupleStruct, serialize_field);
|
||||
compound!(SerializeTupleVariant, serialize_field);
|
||||
|
||||
impl<S: ser::SerializeMap> ser::SerializeMap for Wrap<S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_key<T: ?Sized + Serialize>(&mut self, k: &T) -> Result<(), S::Error> {
|
||||
self.0.serialize_key(&Finite(k))
|
||||
}
|
||||
fn serialize_value<T: ?Sized + Serialize>(&mut self, v: &T) -> Result<(), S::Error> {
|
||||
self.0.serialize_value(&Finite(v))
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! fields {
|
||||
($tr:ident) => {
|
||||
impl<S: ser::$tr> ser::$tr for Wrap<S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self, key: &'static str, v: &T) -> Result<(), S::Error> {
|
||||
self.0.serialize_field(key, &Finite(v))
|
||||
}
|
||||
fn skip_field(&mut self, key: &'static str) -> Result<(), S::Error> {
|
||||
self.0.skip_field(key)
|
||||
}
|
||||
fn end(self) -> Result<S::Ok, S::Error> {
|
||||
self.0.end()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
fields!(SerializeStruct);
|
||||
fields!(SerializeStructVariant);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn non_finite_floats_are_written_as_zero_and_nothing_else_changes() {
|
||||
#[derive(serde::Serialize)]
|
||||
struct T {
|
||||
a: f64,
|
||||
b: Option<f64>,
|
||||
c: Vec<(f32, f64)>,
|
||||
d: Option<String>,
|
||||
e: std::collections::BTreeMap<String, f64>,
|
||||
}
|
||||
let t = T { a: f64::NAN, b: Some(f64::INFINITY), c: vec![(f32::NEG_INFINITY, 1.5)], d: None, e: [("k".to_string(), f64::NAN)].into() };
|
||||
let s = serde_json::to_string(&Finite(&t)).unwrap_or_default();
|
||||
assert_eq!(s, r#"{"a":0.0,"b":0.0,"c":[[0.0,1.5]],"d":null,"e":{"k":0.0}}"#);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
//! The native `.deckcraft` package: a ZIP holding a stored `mimetype`, `presentation.json` (the
|
||||
//! manifest: format name, version, generator and the presentation, minus media bytes), and
|
||||
//! `media/<id>` and `chart-parts/<index>` binary members. Readers ignore unknown fields, so newer
|
||||
//! files open in older builds with what those builds understand.
|
||||
//!
|
||||
//! DeckCraft owns the presentation model and its mapping: which media and chart parts become
|
||||
//! members, and how a loaded deck is repaired. This crate owns the bytes: the package identity,
|
||||
//! the manifest envelope, the NaN-safe JSON writer, the reader that opens files with NaN saved as
|
||||
//! `null`, member names and compression, and the bounded member reads and their budgets.
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
mod finite;
|
||||
mod manifest;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use std::io::{Cursor, Read};
|
||||
|
||||
pub use finite::Finite;
|
||||
pub use manifest::parse_manifest;
|
||||
|
||||
pub const MIMETYPE: &str = "application/vnd.storyteller.deckcraft+zip";
|
||||
/// Files written before the SlideCraft → DeckCraft rename (`.slidecraft`) are still read.
|
||||
pub const LEGACY_MIMETYPE: &str = concat!("application/vnd.storyteller.", "slide", "craft+zip");
|
||||
pub const EXTENSION: &str = "deckcraft";
|
||||
/// The manifest's `format`.
|
||||
pub const FORMAT: &str = "deckcraft";
|
||||
/// Format version written into the manifest.
|
||||
pub const VERSION: u32 = 1;
|
||||
/// The stored member naming the package type.
|
||||
pub const MIMETYPE_ENTRY: &str = "mimetype";
|
||||
/// The manifest member.
|
||||
pub const MANIFEST_ENTRY: &str = "presentation.json";
|
||||
/// Largest entry we will inflate (guards against zip bombs).
|
||||
pub const MAX_ENTRY: u64 = 2 << 30;
|
||||
/// All embedded media and chart sidecars together, inflated (storytold/deckcraft#393).
|
||||
pub const MAX_TOTAL_MEDIA: u64 = 4 << 30;
|
||||
/// Media items and chart sidecar parts one file may list.
|
||||
pub const MAX_MEDIA_ITEMS: usize = 100_000;
|
||||
/// Largest `presentation.json` we will inflate. The manifest holds no media bytes, so it is far
|
||||
/// smaller than a media entry may be; inflating and parsing one is what dominates memory.
|
||||
pub const MAX_MANIFEST: u64 = 256 << 20;
|
||||
|
||||
const MEDIA_PREFIX: &str = "media/";
|
||||
const CHART_PARTS_PREFIX: &str = "chart-parts/";
|
||||
|
||||
/// What one load may inflate.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Budget {
|
||||
/// Bytes all media and chart members may inflate to together.
|
||||
pub total: u64,
|
||||
/// Media items and chart parts the manifest may list.
|
||||
pub items: usize,
|
||||
/// Bytes `presentation.json` may inflate to.
|
||||
pub manifest: u64,
|
||||
}
|
||||
|
||||
impl Default for Budget {
|
||||
fn default() -> Self {
|
||||
Budget { total: MAX_TOTAL_MEDIA, items: MAX_MEDIA_ITEMS, manifest: MAX_MANIFEST }
|
||||
}
|
||||
}
|
||||
|
||||
impl Budget {
|
||||
/// A media budget with the default manifest limit.
|
||||
pub const fn media(total: u64, items: usize) -> Self {
|
||||
Budget { total, items, manifest: MAX_MANIFEST }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum FormatError {
|
||||
#[error("not a DeckCraft file: {0}")]
|
||||
NotOurs(String),
|
||||
#[error("damaged file: {0}")]
|
||||
Damaged(String),
|
||||
#[error("write failed: {0}")]
|
||||
Write(String),
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, FormatError>;
|
||||
|
||||
/// `presentation.json`: the envelope around the app's presentation.
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct Manifest<T> {
|
||||
pub format: String,
|
||||
pub version: u32,
|
||||
pub generator: String,
|
||||
pub presentation: T,
|
||||
}
|
||||
|
||||
/// The member holding media item `id`'s bytes.
|
||||
pub fn media_entry(id: u32) -> String {
|
||||
format!("{MEDIA_PREFIX}{id}")
|
||||
}
|
||||
|
||||
/// The member holding the `index`th chart part a save separated out.
|
||||
pub fn chart_part_entry(index: usize) -> String {
|
||||
format!("{CHART_PARTS_PREFIX}{index}")
|
||||
}
|
||||
|
||||
/// Whether `entry` is a name [`chart_part_entry`] gives, exactly: no other member, no path
|
||||
/// tricks, no leading zeros.
|
||||
pub fn is_chart_part_entry(entry: &str) -> bool {
|
||||
entry.strip_prefix(CHART_PARTS_PREFIX).and_then(|s| s.parse::<usize>().ok()).is_some_and(|n| entry == chart_part_entry(n))
|
||||
}
|
||||
|
||||
/// Whether a member of this content type is stored rather than deflated: it is compressed already.
|
||||
pub fn is_compressed(content_type: &str) -> bool {
|
||||
content_type.starts_with("image/jpeg")
|
||||
|| content_type.starts_with("image/png")
|
||||
|| content_type.starts_with("video/")
|
||||
|| content_type.starts_with("audio/mpeg")
|
||||
|| content_type.starts_with("audio/mp4")
|
||||
|| content_type.contains("zip")
|
||||
}
|
||||
|
||||
/// Does this look like a `.deckcraft` file?
|
||||
pub fn sniff(bytes: &[u8]) -> bool {
|
||||
bytes.starts_with(b"PK") && [MIMETYPE, LEGACY_MIMETYPE].iter().any(|m| bytes.windows(m.len()).take(200).any(|w| w == m.as_bytes()))
|
||||
}
|
||||
|
||||
/// A binary member of a package being written.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Member<'a> {
|
||||
/// Its name, from [`media_entry`] or [`chart_part_entry`].
|
||||
pub name: &'a str,
|
||||
/// Its content type, which decides whether it is stored or deflated.
|
||||
pub content_type: &'a str,
|
||||
pub bytes: &'a [u8],
|
||||
}
|
||||
|
||||
/// The package: `mimetype` (stored), the manifest naming `generator` around `presentation`
|
||||
/// (deflated, with non-finite numbers written as 0), then `members` in order.
|
||||
pub fn write<T: serde::Serialize>(presentation: &T, generator: &str, members: &[Member<'_>]) -> Result<Vec<u8>> {
|
||||
use craft_zip::Compression::{Deflated, Stored};
|
||||
let w = |e: craft_zip::Error| FormatError::Write(e.to_string());
|
||||
let mut z = craft_zip::Writer::new();
|
||||
z.add(MIMETYPE_ENTRY, MIMETYPE.as_bytes(), Stored).map_err(w)?;
|
||||
let m = Manifest { format: FORMAT.to_string(), version: VERSION, generator: generator.to_string(), presentation };
|
||||
let json = serde_json::to_vec(&Finite(&m)).map_err(|e| FormatError::Write(e.to_string()))?;
|
||||
z.add(MANIFEST_ENTRY, &json, Deflated).map_err(w)?;
|
||||
for member in members {
|
||||
let compression = if is_compressed(member.content_type) { Stored } else { Deflated };
|
||||
z.add(member.name, member.bytes, compression).map_err(w)?;
|
||||
}
|
||||
z.finish().map_err(w)
|
||||
}
|
||||
|
||||
/// A package open for reading. Nothing is inflated until a member is read.
|
||||
pub struct Package<'a> {
|
||||
zip: zip::ZipArchive<Cursor<&'a [u8]>>,
|
||||
}
|
||||
|
||||
impl<'a> Package<'a> {
|
||||
/// Index `bytes` as a ZIP.
|
||||
pub fn open(bytes: &'a [u8]) -> Result<Self> {
|
||||
let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| FormatError::NotOurs(e.to_string()))?;
|
||||
Ok(Package { zip })
|
||||
}
|
||||
|
||||
/// Open `bytes` and read its manifest, inflating at most `max_manifest` bytes of it. Missing
|
||||
/// `presentation.json` or a different `format` is [`FormatError::NotOurs`].
|
||||
pub fn open_manifest<T: serde::de::DeserializeOwned>(bytes: &'a [u8], max_manifest: u64) -> Result<(Manifest<T>, Self)> {
|
||||
let mut package = Package::open(bytes)?;
|
||||
if !package.contains(MANIFEST_ENTRY) {
|
||||
return Err(FormatError::NotOurs(format!("no {MANIFEST_ENTRY}")));
|
||||
}
|
||||
let json = package.read_max(MANIFEST_ENTRY, max_manifest)?;
|
||||
let m: Manifest<T> = parse_manifest(&json)?;
|
||||
if m.format != FORMAT {
|
||||
return Err(FormatError::NotOurs(m.format));
|
||||
}
|
||||
Ok((m, package))
|
||||
}
|
||||
|
||||
/// Whether the package has a member named `name`.
|
||||
pub fn contains(&self, name: &str) -> bool {
|
||||
self.zip.index_for_name(name).is_some()
|
||||
}
|
||||
|
||||
/// The size `name` declares, inflated; `None` when there is no such readable member.
|
||||
pub fn declared_size(&mut self, name: &str) -> Option<u64> {
|
||||
self.zip.by_name(name).ok().map(|f| f.size())
|
||||
}
|
||||
|
||||
/// Read one member within [`MAX_ENTRY`].
|
||||
pub fn read(&mut self, name: &str) -> Result<Vec<u8>> {
|
||||
self.read_max(name, MAX_ENTRY)
|
||||
}
|
||||
|
||||
/// Read one member, refusing it before inflating if it declares more than `max` bytes, and
|
||||
/// stopping at `max` if it inflates further than it declared.
|
||||
pub fn read_max(&mut self, name: &str, max: u64) -> Result<Vec<u8>> {
|
||||
self.read_counted(name, max, &mut 0)
|
||||
}
|
||||
|
||||
/// [`Package::read_max`] within `left` (and [`MAX_ENTRY`]), charging `left` for every byte
|
||||
/// inflated, including those inflated before a decode or CRC error, so a member that fails
|
||||
/// late still spends the shared budget it used.
|
||||
pub fn read_charged(&mut self, name: &str, left: &mut u64) -> Result<Vec<u8>> {
|
||||
let mut spent = 0;
|
||||
let out = self.read_counted(name, (*left).min(MAX_ENTRY), &mut spent);
|
||||
*left = left.saturating_sub(spent);
|
||||
out
|
||||
}
|
||||
|
||||
/// [`Package::read_max`], adding the bytes inflated to `spent` whether or not the read succeeds.
|
||||
fn read_counted(&mut self, name: &str, max: u64, spent: &mut u64) -> Result<Vec<u8>> {
|
||||
let f = self.zip.by_name(name).map_err(|e| FormatError::Damaged(format!("{name}: {e}")))?;
|
||||
if f.size() > max {
|
||||
return Err(FormatError::Damaged(format!("{name} is too large")));
|
||||
}
|
||||
let mut out = Vec::with_capacity(usize::try_from(f.size().min(64 << 20)).unwrap_or(0));
|
||||
let mut f = f.take(max.saturating_add(1));
|
||||
let read = f.read_to_end(&mut out);
|
||||
*spent = spent.saturating_add(out.len() as u64);
|
||||
read.map_err(|e| FormatError::Damaged(format!("{name}: {e}")))?;
|
||||
if out.len() as u64 > max {
|
||||
return Err(FormatError::Damaged(format!("{name} is too large")));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
//! Reading `presentation.json`, including files written with NaN or infinity saved as `null`.
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::{FormatError, Manifest, Result};
|
||||
|
||||
/// Most `null`s standing in for numbers that [`parse_manifest`] patches in one file, and the
|
||||
/// candidates it tries for each.
|
||||
const MAX_NULL_FIXES: usize = 256;
|
||||
const MAX_TRIES_PER_FIX: usize = 32;
|
||||
/// Bytes [`parse_manifest`] may re-parse in all while patching, so a hostile file full of `null`s
|
||||
/// can't keep it busy: past this the file is reported damaged.
|
||||
const MAX_REPARSE_BYTES: usize = 256 * 1024 * 1024;
|
||||
|
||||
/// Where a parse failed, comparable: later is progress.
|
||||
fn failure<T: DeserializeOwned>(src: &[u8]) -> std::result::Result<Manifest<T>, (String, (usize, usize))> {
|
||||
serde_json::from_slice::<Manifest<T>>(src).map_err(|e| (e.to_string(), (e.line(), e.column())))
|
||||
}
|
||||
|
||||
/// Parse `presentation.json`. serde_json writes a NaN or infinite number as `null`, which then
|
||||
/// fails to read back as a number ("invalid type: null, expected f64"), so DeckCraft could save
|
||||
/// a file it couldn't open. Each such `null` is read as 0 instead (the app then brings the
|
||||
/// value into range); anything else that doesn't parse is damage.
|
||||
///
|
||||
/// serde reports the failing `null` exactly, except inside tagged enums (buffered), where it
|
||||
/// reports the end of the enclosing object: there the `null`s before that point are tried
|
||||
/// nearest first, and a patch is kept only if the parse then gets further. (Files saved since
|
||||
/// [`crate::Finite`] never contain such `null`s; this reads older ones.)
|
||||
pub fn parse_manifest<T: DeserializeOwned>(json: &[u8]) -> Result<Manifest<T>> {
|
||||
// Parse the inflated text as it is: only a file that fails on a `null` number is copied for
|
||||
// patching (the copy is bounded by `MAX_MANIFEST`).
|
||||
let mut last = match failure(json) {
|
||||
Ok(m) => return Ok(m),
|
||||
Err(f) => f,
|
||||
};
|
||||
if !is_null_number(&last.0) {
|
||||
return Err(FormatError::Damaged(last.0));
|
||||
}
|
||||
let mut src = json.to_vec();
|
||||
let mut budget = MAX_REPARSE_BYTES;
|
||||
'fixes: for _ in 0..MAX_NULL_FIXES {
|
||||
let (msg, pos) = &last;
|
||||
if !is_null_number(msg) {
|
||||
break;
|
||||
}
|
||||
let end = offset(&src, pos.0, pos.1).unwrap_or(src.len());
|
||||
let mut progressed = false;
|
||||
let candidates: Vec<usize> = value_nulls_before(&src, end + 4).take(MAX_TRIES_PER_FIX).collect();
|
||||
for at in candidates {
|
||||
let (Some(head), Some(tail)) = (src.get(..at), src.get(at.saturating_add(4)..)) else { continue };
|
||||
let Some(left) = budget.checked_sub(src.len()) else { break 'fixes };
|
||||
budget = left;
|
||||
let mut next = Vec::with_capacity(src.len());
|
||||
next.extend_from_slice(head);
|
||||
next.push(b'0');
|
||||
next.extend_from_slice(tail);
|
||||
match failure(&next) {
|
||||
Ok(m) => return Ok(m),
|
||||
// The patch shortened the text by 3 bytes before the error: compare positions
|
||||
// allowing for that.
|
||||
Err(f) if f.1 > (pos.0, pos.1.saturating_sub(3)) => {
|
||||
src = next;
|
||||
last = f;
|
||||
progressed = true;
|
||||
break;
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
if !progressed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(FormatError::Damaged(last.0))
|
||||
}
|
||||
|
||||
/// Is this parse error a `null` where a number belongs (a NaN or infinity serde_json wrote)?
|
||||
fn is_null_number(msg: &str) -> bool {
|
||||
msg.starts_with("invalid type: null, expected f") || msg.starts_with("invalid type: null, expected a number")
|
||||
}
|
||||
|
||||
/// Byte offset of a 1-based `line`:`column`.
|
||||
fn offset(src: &[u8], line: usize, column: usize) -> Option<usize> {
|
||||
let mut start = 0;
|
||||
for _ in 1..line {
|
||||
start += src.get(start..)?.iter().position(|b| *b == b'\n')? + 1;
|
||||
}
|
||||
Some((start + column).min(src.len()))
|
||||
}
|
||||
|
||||
/// Starts of `null` tokens in value position (after `:`, `[` or `,`) before `end`, nearest first.
|
||||
fn value_nulls_before(src: &[u8], end: usize) -> impl Iterator<Item = usize> + '_ {
|
||||
let end = end.min(src.len());
|
||||
(0..end.saturating_sub(3)).rev().filter(move |&i| {
|
||||
src.get(i..i + 4) == Some(b"null")
|
||||
&& src[..i].iter().rev().find(|b| !b.is_ascii_whitespace()).is_some_and(|b| matches!(b, b':' | b'[' | b','))
|
||||
&& src.get(i + 4).is_none_or(|b| !b.is_ascii_alphanumeric())
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A stand-in for an app's presentation: plain numbers, an optional number and a tagged enum
|
||||
/// (whose `null`s serde reports only at the end of the enclosing object).
|
||||
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
struct Deck {
|
||||
#[serde(default)]
|
||||
width: f64,
|
||||
#[serde(default)]
|
||||
zoom: Option<f64>,
|
||||
#[serde(default)]
|
||||
shapes: Vec<Shape>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind")]
|
||||
enum Shape {
|
||||
Box { x: f64, y: f64 },
|
||||
Line { len: f64 },
|
||||
}
|
||||
|
||||
fn package(members: &[Member<'_>]) -> Vec<u8> {
|
||||
write(&Deck { width: 960.0, ..Default::default() }, "Test 1", members).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_package_holds_mimetype_then_manifest_then_members_in_order() {
|
||||
let png = [0x89, b'P', b'N', b'G'];
|
||||
let svg = b"<svg/>".repeat(20);
|
||||
let bytes = package(&[
|
||||
Member { name: &media_entry(7), content_type: "image/png", bytes: &png },
|
||||
Member { name: &media_entry(8), content_type: "image/svg+xml", bytes: &svg },
|
||||
Member { name: &chart_part_entry(0), content_type: "application/zip", bytes: b"PK" },
|
||||
]);
|
||||
assert!(sniff(&bytes));
|
||||
let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap();
|
||||
assert_eq!(z.len(), 5);
|
||||
let methods: Vec<_> = (0..z.len())
|
||||
.map(|i| {
|
||||
let f = z.by_index(i).unwrap();
|
||||
(f.name().unwrap().into_owned(), f.compression())
|
||||
})
|
||||
.collect();
|
||||
use zip::CompressionMethod::{Deflated, Stored};
|
||||
assert_eq!(
|
||||
methods,
|
||||
[("mimetype", Stored), ("presentation.json", Deflated), ("media/7", Stored), ("media/8", Deflated), ("chart-parts/0", Stored)]
|
||||
.map(|(n, m)| (n.to_string(), m))
|
||||
);
|
||||
// The mimetype is the first, stored member, readable in place.
|
||||
assert_eq!(craft_zip::stored_mimetype(&bytes), Some(MIMETYPE.as_bytes()));
|
||||
let (m, mut p) = Package::open_manifest::<Deck>(&bytes, MAX_MANIFEST).unwrap();
|
||||
assert_eq!((m.format.as_str(), m.version, m.generator.as_str(), m.presentation.width), (FORMAT, VERSION, "Test 1", 960.0));
|
||||
assert_eq!(p.read(&media_entry(8)).unwrap(), svg);
|
||||
assert_eq!(p.declared_size(&media_entry(7)), Some(4));
|
||||
assert_eq!(p.declared_size("media/99"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_manifest_is_compact_json_with_its_fields_in_order() {
|
||||
let bytes = package(&[]);
|
||||
let mut p = Package::open(&bytes).unwrap();
|
||||
let json = String::from_utf8(p.read(MANIFEST_ENTRY).unwrap()).unwrap();
|
||||
assert_eq!(json, r#"{"format":"deckcraft","version":1,"generator":"Test 1","presentation":{"width":960.0,"zoom":null,"shapes":[]}}"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_finite_numbers_are_written_as_zero_and_read_back() {
|
||||
let deck = Deck { width: f64::NAN, zoom: Some(f64::INFINITY), shapes: vec![Shape::Box { x: f64::NEG_INFINITY, y: 2.0 }] };
|
||||
let bytes = write(&deck, "t", &[]).unwrap();
|
||||
let (m, _) = Package::open_manifest::<Deck>(&bytes, MAX_MANIFEST).unwrap();
|
||||
assert_eq!(m.presentation, Deck { width: 0.0, zoom: Some(0.0), shapes: vec![Shape::Box { x: 0.0, y: 2.0 }] });
|
||||
}
|
||||
|
||||
/// Files written before [`Finite`] (plain serde_json: NaN as `null`) open too, including a
|
||||
/// `null` inside a tagged enum, where serde reports only the enclosing object.
|
||||
#[test]
|
||||
fn older_manifests_with_null_numbers_parse() {
|
||||
let cases: [(&str, Deck); 3] = [
|
||||
(r#"{"format":"deckcraft","version":1,"generator":"old","presentation":{"width":null}}"#, Deck::default()),
|
||||
(
|
||||
r#"{"format":"deckcraft","version":1,"generator":"old","presentation":{"width":3,"shapes":[{"kind":"Box","x":null,"y":4}]}}"#,
|
||||
Deck { width: 3.0, shapes: vec![Shape::Box { x: 0.0, y: 4.0 }], ..Default::default() },
|
||||
),
|
||||
(
|
||||
"{\n \"format\": \"deckcraft\",\n \"version\": 1,\n \"generator\": \"t\",\n \"presentation\": {\"shapes\": [{\"kind\": \"Line\", \"len\": null}]}\n}",
|
||||
Deck { shapes: vec![Shape::Line { len: 0.0 }], ..Default::default() },
|
||||
),
|
||||
];
|
||||
for (json, want) in cases {
|
||||
let m = parse_manifest::<Deck>(json.as_bytes()).unwrap_or_else(|e| panic!("{json}: {e}"));
|
||||
assert_eq!(m.presentation, want, "{json}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Only `null`s where numbers belong are patched: other damage still fails.
|
||||
#[test]
|
||||
fn other_manifest_damage_is_still_reported() {
|
||||
let ok = br#"{"format": "deckcraft", "version": 1, "generator": "t", "presentation": {}}"#;
|
||||
assert!(parse_manifest::<Deck>(ok).is_ok());
|
||||
let bad = br#"{"format": null, "version": 1, "generator": "t", "presentation": {}}"#;
|
||||
assert!(matches!(parse_manifest::<Deck>(bad), Err(FormatError::Damaged(_))));
|
||||
assert!(parse_manifest::<Deck>(&ok[..ok.len() - 3]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_manifest_full_of_nulls_is_refused_within_bounds() {
|
||||
let shapes = (0..2000).map(|_| r#"{"kind":"Box","x":1,"y":"not a number"}"#).collect::<Vec<_>>().join(",");
|
||||
let json = format!(r#"{{"format":"deckcraft","version":1,"generator":"t","presentation":{{"width":null,"shapes":[{shapes}]}}}}"#);
|
||||
assert!(matches!(parse_manifest::<Deck>(json.as_bytes()), Err(FormatError::Damaged(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn what_is_not_a_package_is_not_ours() {
|
||||
for bytes in [&b""[..], b"PK\x03\x04garbage", &[0u8; 1000]] {
|
||||
assert!(matches!(Package::open_manifest::<Deck>(bytes, MAX_MANIFEST), Err(FormatError::NotOurs(_))));
|
||||
}
|
||||
// A ZIP without the manifest, and one from another app.
|
||||
let mut z = craft_zip::Writer::new();
|
||||
z.add("mimetype", b"application/zip", craft_zip::Compression::Stored).unwrap();
|
||||
let no_manifest = z.finish().unwrap();
|
||||
let err = Package::open_manifest::<Deck>(&no_manifest, MAX_MANIFEST).err().unwrap();
|
||||
assert_eq!(err.to_string(), "not a DeckCraft file: no presentation.json");
|
||||
let mut z = craft_zip::Writer::new();
|
||||
z.add(MANIFEST_ENTRY, br#"{"format":"other","version":1,"generator":"x","presentation":{}}"#, craft_zip::Compression::Deflated).unwrap();
|
||||
let other = z.finish().unwrap();
|
||||
let err = Package::open_manifest::<Deck>(&other, MAX_MANIFEST).err().unwrap();
|
||||
assert_eq!(err.to_string(), "not a DeckCraft file: other");
|
||||
// A truncated package is an error, not a panic.
|
||||
let mut b = package(&[]);
|
||||
b.truncate(b.len() / 2);
|
||||
assert!(Package::open_manifest::<Deck>(&b, MAX_MANIFEST).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_slidecraft_packages_are_recognised() {
|
||||
let mut z = craft_zip::Writer::new();
|
||||
z.add(MIMETYPE_ENTRY, LEGACY_MIMETYPE.as_bytes(), craft_zip::Compression::Stored).unwrap();
|
||||
assert!(sniff(&z.finish().unwrap()));
|
||||
assert!(!sniff(b"PK\x03\x04 application/zip"));
|
||||
assert!(!sniff(MIMETYPE.as_bytes()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_oversized_manifest_is_refused_before_inflating() {
|
||||
let bytes = package(&[]);
|
||||
let size = Package::open(&bytes).unwrap().declared_size(MANIFEST_ENTRY).unwrap();
|
||||
assert!(Package::open_manifest::<Deck>(&bytes, size).is_ok());
|
||||
let err = Package::open_manifest::<Deck>(&bytes, size - 1).err().unwrap();
|
||||
assert_eq!(err.to_string(), "damaged file: presentation.json is too large");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_member_that_inflates_past_its_declared_size_stops_at_the_limit() {
|
||||
// Declared sizes are the file's word: a stored member whose header lies is still capped.
|
||||
let data = vec![7u8; 100];
|
||||
let bytes = package(&[Member { name: &media_entry(1), content_type: "image/png", bytes: &data }]);
|
||||
let mut p = Package::open(&bytes).unwrap();
|
||||
assert_eq!(p.read_max(&media_entry(1), 100).unwrap().len(), 100);
|
||||
assert!(p.read_max(&media_entry(1), 99).is_err());
|
||||
let err = p.read("media/2").unwrap_err();
|
||||
assert!(matches!(err, FormatError::Damaged(ref m) if m.starts_with("media/2: ")), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupt_members_are_charged_for_what_they_inflated() {
|
||||
let a = b"PICTURE-AAAAAAAAAAAA";
|
||||
let b = b"PICTURE-BBBBBBBBBBBB";
|
||||
let mut bytes = package(&[
|
||||
Member { name: &media_entry(1), content_type: "image/png", bytes: a },
|
||||
Member { name: &media_entry(2), content_type: "image/png", bytes: b },
|
||||
]);
|
||||
// Corrupt both stored members, so each fails its CRC only after inflating all its bytes.
|
||||
for tag in [b"PICTURE-AAAA", b"PICTURE-BBBB"] {
|
||||
let at = bytes.windows(tag.len()).position(|w| w == tag).unwrap();
|
||||
bytes[at] ^= 0xff;
|
||||
}
|
||||
let mut p = Package::open(&bytes).unwrap();
|
||||
let mut left = 30;
|
||||
assert!(p.read_charged(&media_entry(1), &mut left).is_err());
|
||||
assert_eq!(left, 10, "a failed read still spends what it inflated");
|
||||
// The second fits a fresh budget on its own, but not what is left: refused before inflating.
|
||||
let err = p.read_charged(&media_entry(2), &mut left).unwrap_err();
|
||||
assert_eq!(err.to_string(), "damaged file: media/2 is too large");
|
||||
assert_eq!(left, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chart_part_names_are_exact() {
|
||||
assert_eq!(chart_part_entry(3), "chart-parts/3");
|
||||
assert!(is_chart_part_entry("chart-parts/0"));
|
||||
assert!(is_chart_part_entry("chart-parts/12"));
|
||||
for not in ["media/1", "chart-parts/../0", "chart-parts/00", "chart-parts/-1", "chart-parts/", "chart-parts/+1", "chart-parts/1 "] {
|
||||
assert!(!is_chart_part_entry(not), "{not}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compressed_content_types_are_stored() {
|
||||
for stored in [
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"video/mp4",
|
||||
"audio/mpeg",
|
||||
"audio/mp4",
|
||||
"application/zip",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet+zip",
|
||||
] {
|
||||
assert!(is_compressed(stored), "{stored}");
|
||||
}
|
||||
for deflated in ["image/svg+xml", "image/gif", "audio/wav", "application/octet-stream", "text/plain"] {
|
||||
assert!(!is_compressed(deflated), "{deflated}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_members_are_a_write_error() {
|
||||
let err = write(
|
||||
&Deck::default(),
|
||||
"t",
|
||||
&[Member { name: "media/1", content_type: "x", bytes: b"a" }, Member { name: "media/1", content_type: "x", bytes: b"b" }],
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, FormatError::Write(_)), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_default_budget_is_the_formats_limits() {
|
||||
assert_eq!(Budget::default(), Budget { total: 4 << 30, items: 100_000, manifest: 256 << 20 });
|
||||
assert_eq!(Budget::media(10, 2), Budget { total: 10, items: 2, manifest: MAX_MANIFEST });
|
||||
}
|
||||
Reference in New Issue
Block a user