From e4a6b68e5af3e0dcf6878f70443a6605fb07229c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:36:52 -0500 Subject: [PATCH 001/120] ci: lint and test the format crates, and measure the codec features in coverage (#849) Twelve format crates had no workflow of their own: xml, mime, cfb, mesh, metafile, dxf, gcode, svg-paths, silhouette-studio, tiff, heif and cms (cms ran only under the PDF workflow's paths). formats.yml runs fmt, clippy (default, each feature alone, all features), the tests and a wasm32 check for each. The coverage build compiled none of the codec code that sits behind features that are off by default, so new code in craft-video, craft-audio and craft-media read as untested and failed the quality gate. The SonarCloud job now passes those features to the shared workflow. video.yml's per-feature clippy also covers vp9-decode and vp9-encode. --- .github/workflows/formats.yml | 111 +++++++++++++++++++++++++++++++ .github/workflows/sonarcloud.yml | 4 ++ .github/workflows/video.yml | 2 +- 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/formats.yml diff --git a/.github/workflows/formats.yml b/.github/workflows/formats.yml new file mode 100644 index 000000000..e19902e8c --- /dev/null +++ b/.github/workflows/formats.yml @@ -0,0 +1,111 @@ +name: Shared format crates + +on: + push: + branches: [main] + paths: + - "crates/xml/**" + - "crates/mime/**" + - "crates/cfb/**" + - "crates/mesh/**" + - "crates/metafile/**" + - "crates/dxf/**" + - "crates/gcode/**" + - "crates/svg-paths/**" + - "crates/silhouette-studio/**" + - "crates/tiff/**" + - "crates/heif/**" + - "crates/cms/**" + - "Cargo.toml" + - "Cargo.lock" + - ".github/workflows/formats.yml" + pull_request: + paths: + - "crates/xml/**" + - "crates/mime/**" + - "crates/cfb/**" + - "crates/mesh/**" + - "crates/metafile/**" + - "crates/dxf/**" + - "crates/gcode/**" + - "crates/svg-paths/**" + - "crates/silhouette-studio/**" + - "crates/tiff/**" + - "crates/heif/**" + - "crates/cms/**" + - "Cargo.toml" + - "Cargo.lock" + - ".github/workflows/formats.yml" + workflow_dispatch: + +permissions: + contents: read + +concurrency: + # Pull requests share one group per branch, so a newer push cancels the older run. Every other + # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it + # always gets to save the cache. + group: shared-formats-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + check: + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + include: + # `features`: each is linted alone, so code that needs another feature fails here. + - { crate: craft-xml, features: "" } + - { crate: craft-mime, features: "" } + - { crate: craft-cfb, features: "" } + - { crate: craft-mesh, features: "stl obj ply gltf" } + - { crate: craft-metafile, features: "write" } + - { crate: craft-dxf, features: "" } + - { crate: craft-gcode, features: "" } + - { crate: craft-svg-paths, features: "" } + - { crate: craft-silhouette-studio, features: "" } + - { crate: craft-tiff, features: "" } + - { crate: craft-heif, features: "testdata" } + - { crate: craft-cms, features: "" } + name: ${{ matrix.crate }} lint, tests, WASM + runs-on: ubuntu-latest + steps: + - name: Harden runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + with: + components: rustfmt, clippy + targets: wasm32-unknown-unknown + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + key: ${{ matrix.crate }} + save-if: ${{ github.ref == 'refs/heads/main' }} + - run: cargo fmt -p "$CRATE" --check + env: + CRATE: ${{ matrix.crate }} + - name: Clippy, default features and each feature alone + env: + CRATE: ${{ matrix.crate }} + FEATURES: ${{ matrix.features }} + run: | + cargo clippy -p "$CRATE" --all-targets --locked -- -D warnings + for f in $FEATURES; do + echo "::group::feature: $f" + cargo clippy -p "$CRATE" --all-targets --locked --no-default-features --features "$f" -- -D warnings + echo "::endgroup::" + done + - run: cargo clippy -p "$CRATE" --all-targets --locked --all-features -- -D warnings + env: + CRATE: ${{ matrix.crate }} + - run: cargo test -p "$CRATE" --all-features --locked + env: + CRATE: ${{ matrix.crate }} + - run: cargo check -p "$CRATE" --all-features --locked --target wasm32-unknown-unknown + env: + CRATE: ${{ matrix.crate }} diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 1f88bd5d1..654fe4c02 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -20,4 +20,8 @@ jobs: uses: storytold/craft-common-actions/.github/workflows/sonarcloud.yml@main with: pdf-test-fonts: true + # The codec and format code behind features that are off by default. Without these the + # coverage build never compiles it, and new code there reads as untested. + cargo-features: >- + craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write secrets: inherit diff --git a/.github/workflows/video.yml b/.github/workflows/video.yml index 1c4e063d1..ec1c16eda 100644 --- a/.github/workflows/video.yml +++ b/.github/workflows/video.yml @@ -40,7 +40,7 @@ jobs: # Each feature on its own, so code that needs another feature fails here. - name: Clippy, each feature alone run: | - for f in "" dnx vp8 prores h264 hevc vp9 av1 threads; do + for f in "" dnx vp8 prores h264 hevc vp9 vp9-decode vp9-encode av1 threads; do echo "::group::features: ${f:-none}" cargo clippy -p craft-video --all-targets --locked --no-default-features ${f:+--features "$f"} -- -D warnings echo "::endgroup::" From 628a235ae535a462adc462d479c150afc5f3bfc0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:37:23 -0500 Subject: [PATCH 002/120] craft-cms: the changes PhotoCraft made to its copy since extraction (#858) * craft-cms: the changes PhotoCraft made to its copy since extraction PhotoCraft kept editing crates/cms after craft-cms was extracted from it at storytold/photocraft@b3222ee8. This ports its 41 commits up to storytold/photocraft@0143b148 with a three-way merge per file, keeping the changes craft-cms made on its own (Result-returning API, white preservation, FOGRA39, named colours, profile ID). Ported: the .3dl shaper, Mesh header, UTF-16 and .look fixes in lutfile; the checked ICC offsets and the per-profile curve sample budget; same_bytes and the byte-confirmed transform cache; the blocked matrix/TRC path and the 8-bit first-stage table; GpuMatrixShaper; cicp_rgb; the ACEScg and Dot Gain 20 % built-ins (the latter now the default gray); bisection for a table's flat ends; PhotoCraft spelled with a capital C in the built-in descriptions, with the shipped coated CMYK profile's bytes as PhotoCraft regenerated them. Not taken, because craft-cms already does it another way: Curve::inverse_table (inverse64_many), the effective-dimension loop in multilinear interpolation, the curve-count and mluc checks, and the .cube DOMAIN writer. The 8-bit first-stage table checks device white as the exact path does. Refs #779 * craft-cms: Lut3d::from_fn PhotoCraft's proof and HDR display tables are built from a chain of several transforms, which it wrote into Lut3d's fields. Those are private here, so from_fn samples any RGB function, with the same size bounds as from_transform. Refs #779 * craft-cms: README names Lut3d::from_fn --- crates/cms/README.md | 21 +- .../cms/profiles/photocraft-coated-cmyk.icc | Bin 236844 -> 236844 bytes crates/cms/src/builtin.rs | 117 ++- crates/cms/src/clut.rs | 14 + crates/cms/src/curve.rs | 26 +- crates/cms/src/gpu_matrix_shaper.rs | 131 ++++ crates/cms/src/lib.rs | 3 + crates/cms/src/lut3d.rs | 32 + crates/cms/src/lutfile.rs | 738 +++++++++++++++++- crates/cms/src/pipeline.rs | 10 + crates/cms/src/profile.rs | 179 ++++- crates/cms/src/synth.rs | 2 +- crates/cms/src/transform.rs | 148 +++- crates/cms/tests/cms.rs | 90 +++ crates/cms/tests/gpu_matrix_shaper.rs | 201 +++++ crates/cms/tests/profile_id.rs | 2 +- 16 files changed, 1620 insertions(+), 94 deletions(-) create mode 100644 crates/cms/src/gpu_matrix_shaper.rs create mode 100644 crates/cms/tests/gpu_matrix_shaper.rs diff --git a/crates/cms/README.md b/crates/cms/README.md index 7953539ba..55bbc6b82 100644 --- a/crates/cms/README.md +++ b/crates/cms/README.md @@ -30,8 +30,14 @@ Buffers are checked once per call, before any pixel is written. * `Transform::proof(src, proof, display, intent, bpc, simulate_paper)`: soft-proof chain. * `GamutCheck::new(src, proof, ΔE)`: out-of-gamut test (gamut warning). -* `Lut3d::from_transform(&t, 33)?.to_rgba16f_bytes()`: display transform as a 3D texture. +* `Lut3d::from_transform(&t, 33)?.to_rgba16f_bytes()`: display transform as a 3D texture; + `Lut3d::from_fn(size, f)` samples any RGB function. +* `Transform::gpu_matrix_shaper()`: accurate RGB input curves + matrix + output curves for + display shaders. Float tables retain near-black detail; unsupported chains return `None` + for the 3D LUT fallback. No GPU dependency is required by this crate. * `transform::cached(src, dst, opts)`: process-wide transform cache. +* `builtin::cicp_rgb(primaries, transfer, linear)`: the RGB profile for an ITU-T H.273 colour + description (a HEIF `nclx` box, a video stream). * `lutfile::parse(name, bytes)` / `lutfile::write_cube(&lut)`: `.cube`, `.3dl` and `.look` LUT files; `lutfile::builtin(id)` returns generated looks (`lutfile::BUILTIN`). * `Profile::to_bytes()`: original bytes of parsed profiles (byte-exact), or an ICC v4.3 encoding @@ -59,6 +65,10 @@ Buffers are checked once per call, before any pixel is written. tetrahedral interpolation (4D: linear on the first input between two tetrahedra); purely analytic chains (e.g. Lab ↔ RGB) are evaluated exactly. Buffers ≥ 16k pixels run on rayon (native only). +* GPU matrix/TRC display transforms reuse the 4097-entry input curves, 3×3 matrix plus + offsets, and 4096-entry output curves indexed by `x^(1/4)`. Each channel is interpolated + separately using float texels; SDR input/output are clamped to `[0, 1]`. A final RGB + 33³ table cannot resolve steep inverse gamma curves near black, even with float texels. * Rendering intents: AToB0/1/2 and BToA0/1/2 by intent with AToB0/BToA0 fallback; absolute colorimetric scales by media white (`wtpt`; v2 display profiles treated as D50; a zero, negative or non-finite white is refused with `CmsError::Invalid`); BPC uses @@ -73,7 +83,8 @@ Buffers are checked once per call, before any pixel is written. ## Built-in profiles (all CC0-1.0, generated by this crate) The profile descriptions and copyright text keep the PhotoCraft names they were first -published with, so profiles embedded in existing files still match byte for byte. +published with (spelled "PhotoCraft" since PhotoCraft's #5614), so profiles embedded in +existing files still match byte for byte. | id | description | |---|---| @@ -82,10 +93,12 @@ published with, so profiles embedded in existing files still match byte for byte | `adobe-rgb-compat` | Adobe RGB (1998)-compatible (published primaries, gamma 563/256) | | `prophoto-compat` | ProPhoto-compatible (ROMM primaries, D50, gamma 1.8) | | `linear-srgb` | sRGB primaries, linear | +| `aces-cg` | ACEScg: AP1 primaries, ACES white (~D60), linear | | `rec2020` | Rec. 2020 primaries, Rec. 709 OETF | -| `gray-gamma-2.2`, `sgray` | gray gamma 2.2; gray with the sRGB curve (default gray working space) | +| `gray-gamma-2.2`, `sgray` | gray gamma 2.2; gray with the sRGB curve | +| `dot-gain-20-compat` | gray, gamma 1.732, fitted to Photoshop's Dot Gain 20 % (default gray working space) | | `lab-d50` | Lab identity (v4 encoding) | -| `coated-cmyk` | **Photocraft Coated CMYK (synthetic, 300% TAC, medium GCR)**: default CMYK | +| `coated-cmyk` | **PhotoCraft Coated CMYK (synthetic, 300% TAC, medium GCR)**: default CMYK | | `coated-fogra39` | Coated FOGRA39 (fitted, craft-cms): fitted to Fogra's FOGRA39 data, 330% TAC, medium GCR | ### The CMYK profiles diff --git a/crates/cms/profiles/photocraft-coated-cmyk.icc b/crates/cms/profiles/photocraft-coated-cmyk.icc index 0d2d1da16da0eb612c735a6f8d589a1907e7a16c..b553fe9cecae7da389930d865073f6e3d40c2331 100644 GIT binary patch delta 48 zcmV-00MGxdx(=+m4zL#i0Yifr0k;?d0RdwHthX9u0pVN$s<-^W0r3n0xQFh|0k`hW G0zS{DO%s0r delta 48 zcmZ3pi*L;?z767xjLFRsjN2s`fegmA+oe(%AI31Q-2UeP<3}FGo$c?=GH!o&mdWls E0IVPrpa1{> diff --git a/crates/cms/src/builtin.rs b/crates/cms/src/builtin.rs index 361f228c1..ca7034286 100644 --- a/crates/cms/src/builtin.rs +++ b/crates/cms/src/builtin.rs @@ -34,12 +34,18 @@ pub enum Builtin { ProPhotoCompat, /// sRGB primaries with a linear transfer function. LinearSrgb, + /// ACEScg: AP1 primaries, the ACES white point (~D60), linear transfer. The ACES working space for CG renders. + AcesCg, /// Rec. 2020 primaries, D65, Rec. 709 transfer function. Rec2020, /// Gray, gamma 2.2. GrayGamma22, /// Gray with the sRGB transfer function (sGray): gray `v` looks like RGB `(v, v, v)` in sRGB. SGray, + /// Gray matching Photoshop's default working gray (Dot Gain 20 %) with our own curve: a pure + /// gamma of 1.732, fitted on Photoshop 27.11's RGB → Grayscale (sRGB 64 / 128 / 192 and pure red + /// become 46 / 105 / 177 / 107, all within 1 level). Not Adobe's profile. + DotGain20Compat, /// CIE L*a*b* D50 identity (device Lab = PCS Lab, ICC v4 encoding). LabD50, /// Synthetic coated-offset CMYK (300 % total ink, medium GCR); see [`crate::synth`]. @@ -51,15 +57,17 @@ pub enum Builtin { } impl Builtin { - pub const ALL: [Builtin; 11] = [ + pub const ALL: [Builtin; 13] = [ Builtin::Srgb, Builtin::DisplayP3, Builtin::AdobeRgbCompat, Builtin::ProPhotoCompat, Builtin::LinearSrgb, + Builtin::AcesCg, Builtin::Rec2020, Builtin::GrayGamma22, Builtin::SGray, + Builtin::DotGain20Compat, Builtin::LabD50, Builtin::CoatedCmyk, Builtin::CoatedFogra39, @@ -73,9 +81,11 @@ impl Builtin { Builtin::AdobeRgbCompat => "adobe-rgb-compat", Builtin::ProPhotoCompat => "prophoto-compat", Builtin::LinearSrgb => "linear-srgb", + Builtin::AcesCg => "aces-cg", Builtin::Rec2020 => "rec2020", Builtin::GrayGamma22 => "gray-gamma-2.2", Builtin::SGray => "sgray", + Builtin::DotGain20Compat => "dot-gain-20-compat", Builtin::LabD50 => "lab-d50", Builtin::CoatedCmyk => "coated-cmyk", Builtin::CoatedFogra39 => "coated-fogra39", @@ -87,13 +97,15 @@ impl Builtin { match self { Builtin::Srgb => "sRGB IEC61966-2.1", Builtin::DisplayP3 => "Display P3", - Builtin::AdobeRgbCompat => "Adobe RGB (1998)-compatible (Photocraft)", - Builtin::ProPhotoCompat => "ProPhoto RGB-compatible (ROMM primaries, Photocraft)", - Builtin::LinearSrgb => "Linear sRGB (Photocraft)", - Builtin::Rec2020 => "Rec. ITU-R BT.2020 (Photocraft)", - Builtin::GrayGamma22 => "Gray Gamma 2.2 (Photocraft)", - Builtin::SGray => "sGray (sRGB tone curve, Photocraft)", - Builtin::LabD50 => "Lab D50 identity (Photocraft)", + Builtin::AdobeRgbCompat => "Adobe RGB (1998)-compatible (PhotoCraft)", + Builtin::ProPhotoCompat => "ProPhoto RGB-compatible (ROMM primaries, PhotoCraft)", + Builtin::LinearSrgb => "Linear sRGB (PhotoCraft)", + Builtin::AcesCg => "ACEScg (AP1, linear, PhotoCraft)", + Builtin::Rec2020 => "Rec. ITU-R BT.2020 (PhotoCraft)", + Builtin::GrayGamma22 => "Gray Gamma 2.2 (PhotoCraft)", + Builtin::SGray => "sGray (sRGB tone curve, PhotoCraft)", + Builtin::DotGain20Compat => "Dot Gain 20%-compatible (gamma 1.732, PhotoCraft)", + Builtin::LabD50 => "Lab D50 identity (PhotoCraft)", Builtin::CoatedCmyk => crate::synth::DESCRIPTION, Builtin::CoatedFogra39 => COATED_FOGRA39_DESCRIPTION, } @@ -111,6 +123,8 @@ impl Builtin { "rec2020" | "bt2020" => Some(Builtin::Rec2020), "gray" | "graygamma22" | "gamma22" => Some(Builtin::GrayGamma22), "sgray" | "graysrgb" => Some(Builtin::SGray), + "dotgain20" | "dotgain20compat" | "dotgain" => Some(Builtin::DotGain20Compat), + "aces-cg" | "acescg" => Some(Builtin::AcesCg), "lab" | "labd50" | "cielab" => Some(Builtin::LabD50), "cmyk" | "coatedcmyk" | "defaultcmyk" | "photocraftcoatedcmyk" => Some(Builtin::CoatedCmyk), "fogra39" | "coatedfogra39" => Some(Builtin::CoatedFogra39), @@ -151,7 +165,7 @@ impl Builtin { /// The profile (built once per process). pub fn profile(self) -> &'static Profile { - static CELLS: [OnceLock; 11] = [const { OnceLock::new() }; 11]; + static CELLS: [OnceLock; 13] = [const { OnceLock::new() }; 13]; // `ALL` lists the variants in declaration order (checked by `all_in_declaration_order`), // so the discriminant is the cell index. CELLS[self as usize].get_or_init(|| build(self)) @@ -163,11 +177,12 @@ fn profile_name_key(s: &str) -> String { s.chars().filter(|c| !c.is_whitespace()).flat_map(char::to_lowercase).collect() } -/// The default profile for a colour space: sRGB, sGray, the coated CMYK profile, Lab D50. +/// The default profile for a colour space: sRGB, Dot Gain 20 %-compatible gray (Photoshop's default +/// working gray), the coated CMYK profile, Lab D50. pub fn default_for(cs: ColorSpace) -> Option<&'static Profile> { Some(match cs { ColorSpace::Rgb => Builtin::Srgb.profile(), - ColorSpace::Gray => Builtin::SGray.profile(), + ColorSpace::Gray => Builtin::DotGain20Compat.profile(), ColorSpace::Cmyk => Builtin::CoatedCmyk.profile(), ColorSpace::Lab => Builtin::LabD50.profile(), _ => return None, @@ -182,7 +197,7 @@ pub fn srgb() -> &'static Profile { /// only assigned to such files, never offered as a working space. pub fn linear_gray() -> &'static Profile { static PROFILE: OnceLock = OnceLock::new(); - PROFILE.get_or_init(|| gray("Linear Gray (Photocraft)", Curve::Gamma(1.0))) + PROFILE.get_or_init(|| gray("Linear Gray (PhotoCraft)", Curve::Gamma(1.0))) } /// The ICC D65 used for the RGB spaces (chromaticity 0.3127, 0.3290). @@ -211,6 +226,8 @@ const P3_SPACE: RgbSpace = rgb_space!([[0.680, 0.320], [0.265, 0.690], [0.150, 0 const ADOBE_SPACE: RgbSpace = rgb_space!([[0.64, 0.33], [0.21, 0.71], [0.15, 0.06]], D65); const ROMM_SPACE: RgbSpace = rgb_space!([[0.7347, 0.2653], [0.1596, 0.8404], [0.0366, 0.0001]], math::D50); const BT2020_SPACE: RgbSpace = rgb_space!([[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]], D65); +/// ACEScg: the AP1 primaries with the ACES white point (~D60, xy 0.32168/0.33767, the ACES 2065-1 neutral). +const AP1_SPACE: RgbSpace = rgb_space!([[0.713, 0.293], [0.165, 0.830], [0.128, 0.044]], math::xy_to_xyz([0.32168, 0.33767])); /// Colorant matrix adapted to D50 and quantized to s15Fixed16, with each row nudged so that /// RGB (1, 1, 1) maps exactly to the stored D50 white (keeps neutrals neutral). @@ -321,6 +338,46 @@ pub const COATED_FOGRA39_ICC: &[u8] = include_bytes!("../profiles/craft-coated-f /// with `CRAFT_CMS_REGEN_PROFILES=1 cargo test -p craft-cms --release regen`). pub const COATED_CMYK_ICC: &[u8] = include_bytes!("../profiles/photocraft-coated-cmyk.icc"); +/// Rec. 709 OETF inverse: V = 1.099 L^0.45 − 0.099 (L ≥ 0.018), 4.5 L below. +fn rec709_trc() -> Curve { + let a = 1.099; + Curve::Parametric { kind: 3, p: [1.0 / 0.45, 1.0 / a, (a - 1.0) / a, 1.0 / 4.5, 0.081, 0.0, 0.0] } +} + +/// The RGB profile for an ITU-T H.273 (CICP) colour description, as a HEIF `nclx` or a video +/// stream declares it: BT.709, BT.2020 or P3-D65 primaries (codes 1, 9, 12) with the sRGB, +/// BT.709, linear or gamma 2.2/2.8 transfer (13; 1, 6, 14, 15; 8; 4; 5). `linear` means the +/// samples were already decoded to linear light, which is how PQ and HLG (16, 18) images are +/// held, since no ICC curve represents those transfers. `None` for anything else. +pub fn cicp_rgb(primaries: u16, transfer: u16, linear: bool) -> Option { + let (prim, name) = match primaries { + 1 => (SRGB_SPACE, "BT.709"), + 9 => (BT2020_SPACE, "Rec. ITU-R BT.2020"), + 12 => (P3_SPACE, "Display P3"), + _ => return None, + }; + let transfer = if linear { 8 } else { transfer }; + let builtin = match (primaries, transfer) { + (1, 13) => Some(Builtin::Srgb), + (1, 8) => Some(Builtin::LinearSrgb), + (12, 13) => Some(Builtin::DisplayP3), + (9, 1 | 6 | 14 | 15) => Some(Builtin::Rec2020), + _ => None, + }; + if let Some(b) = builtin { + return Some(b.profile().clone()); + } + let (trc, curve) = match transfer { + 8 => (Curve::Gamma(1.0), "linear"), + 13 => (srgb_trc(), "sRGB curve"), + 1 | 6 | 14 | 15 => (rec709_trc(), "BT.709 curve"), + 4 => (Curve::Gamma(2.2), "gamma 2.2"), + 5 => (Curve::Gamma(2.8), "gamma 2.8"), + _ => return None, + }; + Some(rgb(&format!("{name} {curve} (PhotoCraft)"), prim, trc)) +} + fn build(b: Builtin) -> Profile { match b { Builtin::Srgb => rgb(b.description(), SRGB_SPACE, srgb_trc()), @@ -328,14 +385,11 @@ fn build(b: Builtin) -> Profile { Builtin::AdobeRgbCompat => rgb(b.description(), ADOBE_SPACE, Curve::Gamma(563.0 / 256.0)), Builtin::ProPhotoCompat => rgb(b.description(), ROMM_SPACE, Curve::Gamma(1.8)), Builtin::LinearSrgb => rgb(b.description(), SRGB_SPACE, Curve::Gamma(1.0)), - Builtin::Rec2020 => { - // Rec. 709 OETF inverse: V = 1.099 L^0.45 − 0.099 (L ≥ 0.018), 4.5 L below. - let a = 1.099; - let trc = Curve::Parametric { kind: 3, p: [1.0 / 0.45, 1.0 / a, (a - 1.0) / a, 1.0 / 4.5, 0.081, 0.0, 0.0] }; - rgb(b.description(), BT2020_SPACE, trc) - } + Builtin::AcesCg => rgb(b.description(), AP1_SPACE, Curve::Gamma(1.0)), + Builtin::Rec2020 => rgb(b.description(), BT2020_SPACE, rec709_trc()), Builtin::GrayGamma22 => gray(b.description(), Curve::Gamma(2.2)), Builtin::SGray => gray(b.description(), srgb_trc()), + Builtin::DotGain20Compat => gray(b.description(), Curve::Gamma(1.732)), Builtin::LabD50 => lab(), // The shipped bytes are `synth::coated_cmyk()` (checked by the `regen` test). Builtin::CoatedCmyk => shipped_cmyk(COATED_CMYK_ICC, b), @@ -369,3 +423,30 @@ fn shipped_cmyk(bytes: &[u8], b: Builtin) -> Profile { hash: Default::default(), }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cicp_maps_onto_the_builtins_and_linear_bt2020() { + let desc = |p: Option| p.map(|p| p.description); + assert_eq!(desc(cicp_rgb(1, 13, false)), Some(Builtin::Srgb.description().to_string())); + assert_eq!(desc(cicp_rgb(12, 13, false)), Some(Builtin::DisplayP3.description().to_string())); + assert_eq!(desc(cicp_rgb(9, 14, false)), Some(Builtin::Rec2020.description().to_string())); + assert_eq!(desc(cicp_rgb(1, 18, true)), Some(Builtin::LinearSrgb.description().to_string())); + + // An HLG BT.2020 image decoded to linear light: BT.2020's colorants, a linear curve. + let hlg = cicp_rgb(9, 18, true).unwrap(); + assert_eq!(hlg.trc, Some([Curve::Gamma(1.0), Curve::Gamma(1.0), Curve::Gamma(1.0)])); + assert_eq!(hlg.matrix, Builtin::Rec2020.profile().matrix); + assert!(hlg.description.contains("BT.2020") && hlg.description.contains("linear"), "{}", hlg.description); + assert!(Profile::parse(&hlg.to_bytes()).is_ok(), "encodes as a valid ICC profile"); + + // HLG and PQ have no ICC curve; unknown primaries have no profile. + assert!(cicp_rgb(9, 18, false).is_none()); + assert!(cicp_rgb(9, 16, false).is_none()); + assert!(cicp_rgb(2, 13, false).is_none()); + assert!(cicp_rgb(u16::MAX, u16::MAX, true).is_none()); + } +} diff --git a/crates/cms/src/clut.rs b/crates/cms/src/clut.rs index dd0315c20..766fa837e 100644 --- a/crates/cms/src/clut.rs +++ b/crates/cms/src/clut.rs @@ -350,4 +350,18 @@ mod tests { narrow.eval_multilinear(&[0.3, 0.7], &mut b).unwrap(); assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "{a:?} {b:?}"); } + + #[test] + fn degenerate_dimensions_do_not_change_the_result() { + // Axes 1 and 3 have a single grid point (degenerate); axes 0, 2, 4 vary. Interpolating + // over the effective dimensions only gives the same answer as the function sampled. + let f = |p: &[f32], o: &mut [f32]| o[0] = 0.25 * p[0] + 0.5 * p[2] + 0.125 * p[4]; + let c = Clut::sample(vec![3, 1, 4, 1, 5], 1, f).unwrap(); + let mut got = [0.0]; + for p in [[0.2f32, 0.0, 0.7, 0.0, 0.4], [1.0, 0.0, 0.0, 0.0, 1.0], [0.5, 0.0, 0.5, 0.0, 0.5]] { + c.eval_multilinear(&p, &mut got).unwrap(); + let want = 0.25 * p[0] + 0.5 * p[2] + 0.125 * p[4]; + assert!((got[0] - want).abs() < 1e-5, "got {} want {want}", got[0]); + } + } } diff --git a/crates/cms/src/curve.rs b/crates/cms/src/curve.rs index a26ec428b..5da1be801 100644 --- a/crates/cms/src/curve.rs +++ b/crates/cms/src/curve.rs @@ -305,15 +305,22 @@ fn table_inverse_with(t: &[f32], y: f32, edge: impl Fn(bool) -> f32) -> f32 { /// For a flat run at the table start (`start = true`) the last index of the run, or for a flat /// run at the end the first index of it, as a normalised input. +/// +/// A table that is inverted is monotonic, so the run is found by bisection, not by a scan: a long +/// constant table is inverted once per pixel, and scanning it each time made the cost the curve's +/// length times the image's. fn edge_index(t: &[f32], start: bool) -> f32 { let n = t.len(); if start { let v = t[0]; - let i = t.iter().position(|x| (*x - v).abs() > 1e-7).map_or(n - 1, |i| i - 1); + // The first value that differs, given they do so from there on. + let first_other = t.partition_point(|x| (*x - v).abs() <= 1e-7); + let i = if first_other >= n { n - 1 } else { first_other.saturating_sub(1) }; i as f32 / (n - 1) as f32 } else { let v = t[n - 1]; - let i = t.iter().rposition(|x| (*x - v).abs() > 1e-7).map_or(0, |i| i + 1); + // The start of the trailing run equal to the last value. + let i = t.partition_point(|x| (*x - v).abs() > 1e-7); i as f32 / (n - 1) as f32 } } @@ -390,4 +397,19 @@ mod tests { let g = Curve::Gamma(2.2); assert!((g.eval64(-0.5) + 0.5f64.powf(2.2)).abs() < 1e-12); } + + #[test] + fn inverting_a_long_flat_table_does_not_rescan_it_for_every_pixel() { + // Four million zeros and a one: a scan per call would be 8·10¹¹ steps for these calls. + let mut t = vec![0.0f32; 4_000_000]; + t.push(1.0); + let c = Curve::Table(t); + let started = std::time::Instant::now(); + let mut last = 0.0; + for _ in 0..200_000 { + last = c.eval_inverse(0.0); + } + assert!(last > 0.99, "the flat start ends at the last zero: {last}"); + assert!(started.elapsed().as_secs() < 10, "{:?}", started.elapsed()); + } } diff --git a/crates/cms/src/gpu_matrix_shaper.rs b/crates/cms/src/gpu_matrix_shaper.rs new file mode 100644 index 000000000..0d5c0f078 --- /dev/null +++ b/crates/cms/src/gpu_matrix_shaper.rs @@ -0,0 +1,131 @@ +//! Separable RGB display transforms without a coarse 3D interpolation near black. + +/// RGB input curves, a 3×3 matrix with offsets, and RGB output curves for a GPU display +/// transform. Values are floats independent of the document's pixel depth. +/// +/// The input table is sampled linearly at `clamp(rgb, 0, 1) * INPUT_INTERVALS`. Apply +/// [`Self::matrix_rows`] to that result, then sample the output table at +/// `clamp(linear_rgb, 0, 1)^(1/4) * (OUTPUT_ENTRIES - 1)`. Interpolate each channel +/// separately between its two adjacent table entries. These nonuniform output samples +/// retain shadow detail where a pure gamma curve's inverse has an infinite slope. +/// +/// Tables contain RGB values and a padding alpha of 1, suitable for RGBA32Float texels. +/// Use unfiltered texture loads and manual interpolation if float filtering is unavailable. +/// This representation covers SDR display output; input and output are clamped to `[0, 1]`. +/// Construction validates all sizes and values; arbitrary CLUT/proofing chains retain the +/// [`crate::Lut3d`] fallback. +#[derive(Clone, Debug, PartialEq)] +pub struct GpuMatrixShaper { + matrix: [[f32; 4]; 3], + input: Vec<[f32; 4]>, + output: Vec<[f32; 4]>, +} + +impl GpuMatrixShaper { + /// The input table has one more entry than this number of equal intervals. + pub const INPUT_INTERVALS: usize = 4096; + /// Output entries are spaced uniformly in the fourth root of the linear value. + pub const OUTPUT_ENTRIES: usize = 4096; + + /// Row-major matrix: each row contains three RGB coefficients followed by its offset. + pub fn matrix_rows(&self) -> [[f32; 4]; 3] { + self.matrix + } + + /// RGB input curves at `i / INPUT_INTERVALS`, with padding alpha 1. + pub fn input_table(&self) -> &[[f32; 4]] { + &self.input + } + + /// RGB output curves at `(i / (OUTPUT_ENTRIES - 1))^4`, with padding alpha 1. + pub fn output_table(&self) -> &[[f32; 4]] { + &self.output + } + + pub(crate) fn from_parts(matrix: [[f32; 4]; 3], input: &[Vec], output: Option<&[Vec]>) -> Option { + if !matrix.iter().flatten().all(|v| v.is_finite()) { + return None; + } + let input = pack_table(if input.is_empty() { None } else { Some(input) }, Self::INPUT_INTERVALS + 1, |u| u)?; + let output = pack_table(output, Self::OUTPUT_ENTRIES, |u| u * u * u * u)?; + let maxima: [f64; 3] = std::array::from_fn(|channel| input.iter().map(|entry| f64::from(entry[channel]).abs()).fold(0.0, f64::max)); + for row in matrix { + let magnitude = row[..3].iter().zip(maxima).map(|(coefficient, maximum)| f64::from(*coefficient).abs() * maximum).sum::() + + f64::from(row[3]).abs(); + if magnitude > f64::from(f32::MAX) { + return None; + } + } + Some(Self { matrix, input, output }) + } +} + +fn pack_table(tables: Option<&[Vec]>, entries: usize, identity: impl Fn(f32) -> f32) -> Option> { + if let Some(tables) = tables + && (tables.len() != 3 || tables.iter().any(|t| t.len() != entries)) + { + return None; + } + let mut packed: Vec<[f32; 4]> = Vec::new(); + // Both lengths are fixed constants, but a failed allocation still means the caller + // should retain its existing fallback rather than abort while opening a document. + packed.try_reserve_exact(entries).ok()?; + for i in 0..entries { + let rgb = match tables { + Some(tables) => [*tables.first()?.get(i)?, *tables.get(1)?.get(i)?, *tables.get(2)?.get(i)?], + None => [identity(i as f32 / (entries - 1) as f32); 3], + }; + if !rgb.iter().all(|v| v.is_finite()) { + return None; + } + if let Some(previous) = packed.last() + && rgb.iter().zip(previous).any(|(next, before)| !(*next - *before).is_finite()) + { + return None; + } + packed.push([rgb[0], rgb[1], rgb[2], 1.0]); + } + Some(packed) +} + +#[cfg(test)] +mod tests { + use super::*; + + const IDENTITY: [[f32; 4]; 3] = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]; + + #[test] + fn rejects_invalid_tables_and_nonfinite_values() { + let inputs = vec![vec![0.0; GpuMatrixShaper::INPUT_INTERVALS + 1]; 3]; + let outputs = vec![vec![0.0; GpuMatrixShaper::OUTPUT_ENTRIES]; 3]; + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, Some(&outputs)).is_some()); + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs[..2], Some(&outputs)).is_none()); + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, Some(&outputs[..2])).is_none()); + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, Some(&[])).is_none()); + let mut invalid = inputs.clone(); + invalid[0].pop(); + assert!(GpuMatrixShaper::from_parts(IDENTITY, &invalid, Some(&outputs)).is_none()); + for v in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] { + let mut invalid = inputs.clone(); + invalid[1][123] = v; + assert!(GpuMatrixShaper::from_parts(IDENTITY, &invalid, Some(&outputs)).is_none()); + let mut invalid = outputs.clone(); + invalid[2][321] = v; + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, Some(&invalid)).is_none()); + let mut invalid = IDENTITY; + invalid[1][3] = v; + assert!(GpuMatrixShaper::from_parts(invalid, &inputs, Some(&outputs)).is_none()); + } + } + + #[test] + fn rejects_finite_data_that_overflows_shader_arithmetic() { + let mut matrix = IDENTITY; + matrix[0] = [f32::MAX; 4]; + assert!(GpuMatrixShaper::from_parts(matrix, &[], None).is_none()); + let mut inputs = vec![vec![0.0; GpuMatrixShaper::INPUT_INTERVALS + 1]; 3]; + inputs[0][100] = -f32::MAX; + inputs[0][101] = f32::MAX; + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, None).is_none()); + } +} diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index e484f8fd8..8d4f99f49 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -13,6 +13,7 @@ //! * [`NamedColors`] reads a named-colour profile's spot colours and converts them into a //! destination profile. //! * [`Lut3d`] samples a display transform into a 3D texture for the GPU canvas. +//! * [`GpuMatrixShaper`] preserves near-black precision for separable RGB display transforms. //! //! Implemented from the public ICC.1:2010 (v4.3) and ICC.1:2001-04 (v2) specifications, //! CIE 15, and Adobe's published black point compensation paper. @@ -22,6 +23,7 @@ pub mod builtin; pub mod clut; pub mod curve; pub mod gamut; +mod gpu_matrix_shaper; pub mod lut3d; pub mod lutfile; pub mod math; @@ -36,6 +38,7 @@ pub use builtin::Builtin; pub use clut::Clut; pub use curve::Curve; pub use gamut::GamutCheck; +pub use gpu_matrix_shaper::GpuMatrixShaper; pub use lut3d::Lut3d; pub use named::{NamedColor, NamedColors}; pub use profile::{ColorSpace, Pcs, Profile, ProfileClass}; diff --git a/crates/cms/src/lut3d.rs b/crates/cms/src/lut3d.rs index 9103b2388..b1a010e74 100644 --- a/crates/cms/src/lut3d.rs +++ b/crates/cms/src/lut3d.rs @@ -37,6 +37,25 @@ impl Lut3d { Ok(Lut3d { size, data }) } + /// Samples `f` (RGB in, RGB out) on a `size`³ grid, `size` from 2 to [`MAX_SIZE`]: a display + /// chain that is more than one [`Transform`]. + pub fn from_fn(size: usize, mut f: impl FnMut([f32; 3]) -> [f32; 3]) -> Result { + if !(2..=MAX_SIZE).contains(&size) { + return Err(invalid_arg(format!("3D LUT size {size} (2 to {MAX_SIZE})"))); + } + let s = (size - 1) as f32; + let mut data = Vec::with_capacity(size * size * size); + for b in 0..size { + for g in 0..size { + for r in 0..size { + let v = f([r as f32 / s, g as f32 / s, b as f32 / s]); + data.push([v[0], v[1], v[2], 1.0]); + } + } + } + Ok(Lut3d { size, data }) + } + /// Edge length. pub fn size(&self) -> usize { self.size @@ -156,4 +175,17 @@ mod tests { assert_eq!(f32_to_f16(1.0), 0x3C00); assert_eq!(f32_to_f16(1e6), 0x7C00); } + + #[test] + fn from_fn_samples_red_fastest_and_bounds_the_size() { + let lut = Lut3d::from_fn(3, |[r, g, b]| [r, g * 0.5, 1.0 - b]).unwrap(); + assert_eq!(lut.size(), 3); + assert_eq!(lut.data().len(), 27); + assert_eq!(lut.data()[1], [0.5, 0.0, 1.0, 1.0]); + assert_eq!(lut.data()[3], [0.0, 0.25, 1.0, 1.0]); + assert_eq!(lut.data()[26], [1.0, 0.5, 0.0, 1.0]); + for size in [0, 1, MAX_SIZE + 1, usize::MAX] { + assert!(matches!(Lut3d::from_fn(size, |v| v), Err(CmsError::InvalidArgument(_))), "{size}"); + } + } } diff --git a/crates/cms/src/lutfile.rs b/crates/cms/src/lutfile.rs index 1bd0e6e30..c2261325d 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -88,16 +88,63 @@ impl LutFile { } } +/// A LUT file format. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Format { + Cube, + ThreeDl, + Look, +} + +impl Format { + /// The format [`parse`] reads `bytes` as: from `file_name`'s extension, else sniffed + /// (XML is `.look`, anything else `.cube`). + pub fn detect(file_name: &str, bytes: &[u8]) -> Self { + let lower = file_name.to_ascii_lowercase(); + if lower.ends_with(".3dl") { + Format::ThreeDl + } else if lower.ends_with(".look") || decode_text(bytes).trim_start().starts_with('<') { + Format::Look + } else { + Format::Cube + } + } + + /// The file extension, without the dot. + pub fn extension(self) -> &'static str { + match self { + Format::Cube => "cube", + Format::ThreeDl => "3dl", + Format::Look => "look", + } + } +} + /// Parses a LUT, picking the format from `file_name`'s extension (falls back to sniffing). pub fn parse(file_name: &str, bytes: &[u8]) -> Result { - let lower = file_name.to_ascii_lowercase(); - let text = String::from_utf8_lossy(bytes); - if lower.ends_with(".3dl") { - parse_3dl(&text) - } else if lower.ends_with(".look") || text.trim_start().starts_with('<') { - parse_look(&text) - } else { - parse_cube(&text) + parse_as(Format::detect(file_name, bytes), bytes) +} + +/// Parses a LUT in a known format. +pub fn parse_as(format: Format, bytes: &[u8]) -> Result { + let text = decode_text(bytes); + match format { + Format::Cube => parse_cube(&text), + Format::ThreeDl => parse_3dl(&text), + Format::Look => parse_look(&text), + } +} + +/// UTF-16 with a byte-order mark (Photoshop loads such `.look` files, #9118), else UTF-8. +fn decode_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> { + let utf16 = |rest: &[u8], from: fn([u8; 2]) -> u16| { + let units: Vec = rest.as_chunks::<2>().0.iter().map(|&c| from(c)).collect(); + String::from_utf16_lossy(&units).into() + }; + match bytes { + [0xFF, 0xFE, rest @ ..] => utf16(rest, u16::from_le_bytes), + [0xFE, 0xFF, rest @ ..] => utf16(rest, u16::from_be_bytes), + _ => String::from_utf8_lossy(bytes), } } @@ -108,7 +155,14 @@ pub fn parse_cube(text: &str) -> Result { let (mut dmin, mut dmax) = ([0.0f32; 3], [1.0f32; 3]); let mut rows: Vec = Vec::new(); for line in text.lines() { - let line = line.split('#').next().unwrap_or("").trim(); + // A '#' inside a quoted TITLE is part of the title, not a comment. + let raw = line.trim_start(); + let skip = if raw.starts_with("TITLE") { raw.find('"').and_then(|q| raw[q + 1..].find('"').map(|e| q + e + 2)) } else { None }.unwrap_or(0); + let line = match raw[skip..].find('#') { + Some(i) => &raw[..skip + i], + None => raw, + } + .trim(); if line.is_empty() { continue; } @@ -172,41 +226,226 @@ pub fn parse_cube(text: &str) -> Result { err("no LUT_3D_SIZE in .cube file") } -/// Lustre `.3dl`: an optional shaper line of input values, then integer RGB rows with **blue** -/// varying fastest; the output bit depth is inferred from the largest value. +/// Lustre `.3dl`: an optional `3DMESH` magic and `Mesh ` line, an optional +/// shaper line of input values, then integer RGB rows with **blue** varying fastest. The +/// `Mesh` line declares the cube edge (2^inBits + 1) and the output code range (up to +/// 2^outBits − 1), and Photoshop honours both (#9073, #9078); without it the edge is the cube +/// root of the row count and the range is inferred from the largest value (#9121). A shaper that +/// isn't the even ramp is applied before the table, as Photoshop does (#8982): see [`apply_shaper`]. pub fn parse_3dl(text: &str) -> Result { + let mut mesh: Option<(u32, u32)> = None; let mut rows: Vec<[f64; 3]> = Vec::new(); + // How many numbers the first numeric line carried: a shaper of exactly `n` values (n ≤ 3) + // is otherwise indistinguishable from a node row. + let mut first_len: Option = None; + // Any text line before the rows is a header: a headerless table peaking at 255 or 1023 is + // on the 12-bit scale, as Photoshop 27.11 reads it (#9121). + let mut header = false; + let mut shaper: Option> = None; for line in text.lines() { let line = line.split('#').next().unwrap_or("").trim(); - if line.is_empty() || line.starts_with(|c: char| c.is_ascii_alphabetic()) { + // A header is `3DMESH` or starts with a word (`Mesh 4 12`): a letter, and not a number. + // `NaN` and `inf` are numbers, and `0oops` starts like one: those rows are read strictly. + let Some(first) = line.split_whitespace().next() else { continue }; + if first.eq_ignore_ascii_case("3DMESH") || first.starts_with(char::is_alphabetic) && first.parse::().is_err() { + header = true; + // The only header keyword that carries data. A malformed or out-of-range `Mesh` + // line is ignored, as every text line always was. + let mut it = line.split_whitespace(); + if it.next().is_some_and(|k| k.eq_ignore_ascii_case("Mesh")) { + // A declaration is exactly two integer fields after the keyword. Anything else + // (a non-numeric field, too few or too many) is not a declaration: `mesh` stays + // unset so headerless inference applies, rather than recovering numbers from + // the wrong positions (#9078). + let fields: Vec<&str> = it.collect(); + if let [a, b] = fields.as_slice() + && let (Ok(n), Ok(bits)) = (a.parse::(), b.parse::()) + && (1..=7).contains(&n) + && (1..=16).contains(&bits) + { + mesh = Some((n, bits)); + } + } continue; } - let v: Vec = line.split_whitespace().filter_map(|s| s.parse().ok()).collect(); + // Every token is a number: a malformed one is refused rather than dropped, which could + // turn a shaper into an even ramp and lose it silently. + let v: Vec = line.split_whitespace().map(str::parse).collect::>().map_err(|_| LutError(format!("bad line `{line}`")))?; + first_len.get_or_insert(v.len()); if v.len() == 3 { rows.push([v[0], v[1], v[2]]); + } else if v.len() >= 2 { + // Two values are a 2-point shaper (a 2³ table's), not something to drop. + // The input shaper: one row of values ahead of the table, and only one. + if shaper.is_some() || !rows.is_empty() { + return err(format!("a second shaper row `{line}`")); + } + shaper = Some(v); } - // Lines with more than three numbers are the input shaper; it is assumed linear. } - let n = (rows.len() as f64).cbrt().round() as usize; - if n < 2 || n * n * n != rows.len() { - return err(format!("{} rows is not a cube", rows.len())); - } - let max = rows.iter().flatten().fold(0.0f64, |a, &b| a.max(b)); - let scale = if max <= 1.0 { 1.0 } else { [1023.0, 4095.0, 16383.0, 65535.0].into_iter().find(|&s| max <= s).unwrap_or(max) }; + let (n, nodes, scale) = match mesh { + Some((in_bits, out_bits)) => { + let n = 2usize.pow(in_bits) + 1; + let expected = n.checked_pow(3).ok_or_else(|| LutError(format!("unsupported 3dl cube size {n}")))?; + // One row more than the declared cube, and the first row has exactly `n` values: + // that row is the shaper, not a 28th node of a 3³ cube (#9078). + let nodes = if shaper.is_none() && rows.len() == expected + 1 && first_len == Some(n) { + shaper = rows.first().map(|row| row.to_vec()); + &rows[1..] + } else { + &rows[..] + }; + if nodes.len() != expected { + return err(format!("expected {expected} rows for the declared {n}³ cube, found {}", nodes.len())); + } + // Under a valid declaration the node codes are integers in the declared range, so + // the declaration wins for every in-range value — including a small max, where 1 is + // 1/declared of full output, not full output itself (#9073). Only a node beyond the + // declared range is a wrong declaration, and the old inference keeps such a file + // loading as before. + let max = nodes.iter().flatten().fold(0.0f64, |a, &b| a.max(b)); + let declared = 2f64.powi(i32::try_from(out_bits).unwrap_or(16)) - 1.0; + (n, nodes, if max > declared { inferred_3dl_scale(max, header) } else { declared }) + } + None => { + let n = (rows.len() as f64).cbrt().round() as usize; + if n < 2 || n * n * n != rows.len() { + return err(format!("{} rows is not a cube", rows.len())); + } + let max = rows.iter().flatten().fold(0.0f64, |a, &b| a.max(b)); + (n, &rows[..], inferred_3dl_scale(max, header)) + } + }; let mut data = vec![0.0f32; n * n * n * 3]; - for (i, row) in rows.iter().enumerate() { + for (i, row) in nodes.iter().enumerate() { let (r, g, b) = (i / (n * n), (i / n) % n, i % n); let at = ((b * n + g) * n + r) * 3; for k in 0..3 { data[at + k] = (row[k] / scale) as f32; } } - LutFile { title: String::new(), size: n, data, domain_min: [0.0; 3], domain_max: [1.0; 3] }.check() + let lut = LutFile { title: String::new(), size: n, data, domain_min: [0.0; 3], domain_max: [1.0; 3] }.check()?; + Ok(match shaper { + Some(shaper) => apply_shaper(lut, &shaper)?, + None => lut, + }) +} + +/// Bakes a `.3dl` shaper into the table. Photoshop applies the shaper row as a 1-D curve on each +/// input channel ahead of the 3-D table (input `x` reads the shaper, sampled evenly over 0..1, +/// at `x`, interpolated linearly), so each node of the table becomes the table's own trilinear +/// value at the shaped input. An even ramp (the usual shaper) changes nothing and is skipped. +/// The table is baked on a grid holding both its own nodes and the shaper's knots: exact for an +/// identity table, an approximation at that resolution otherwise. A shaper that would need more +/// than [`MAX_SIZE`] nodes for that, or that holds a value that isn't finite, is refused. +fn apply_shaper(lut: LutFile, shaper: &[f64]) -> Result { + let m = shaper.len(); + if m < 2 { + return Ok(lut); + } + // Apply or refuse: a shaper that can't be applied never leaves the table to render alone. + if shaper.iter().any(|v| !v.is_finite()) { + return err("the .3dl shaper holds a value that isn't finite"); + } + // The shaper's own depth, from 10 bits up: a 0..1023 ramp is the even ramp with or without a header. + let scale = inferred_3dl_scale(shaper.iter().fold(0.0f64, |a, &b| a.max(b)), true); + let even = |k: usize| scale * k as f64 / (m - 1) as f64; + // Shapers round their ramp: within one code is still the even ramp. An integer shaper's code + // is 1; a float shaper (0..1) is held to one 10-bit step. + let code = if scale > 1.0 { 1.0 } else { 1.0 / 1023.0 }; + if shaper.iter().enumerate().all(|(k, &v)| (v - even(k)).abs() <= code) { + return Ok(lut); + } + // Scaled to 0..1 first, so interpolating between two extreme values can't overflow. + let shaped: Vec = shaper.iter().map(|v| v / scale).collect(); + let curve = |x: f64| -> f32 { + let p = x.clamp(0.0, 1.0) * (m - 1) as f64; + let i = (p.floor() as usize).min(m - 2); + let f = p - i as f64; + let (a, b) = (shaped.get(i).copied().unwrap_or(0.0), shaped.get(i + 1).copied().unwrap_or(0.0)); + (a + (b - a) * f).clamp(0.0, 1.0) as f32 + }; + // The bake grid holds both the table's nodes and the shaper's knots (lcm of their intervals), + // so the curve keeps its breakpoints whatever the table's size: exact on an identity table. A + // grid that would pass MAX_SIZE is refused: sampled more coarsely, a bent shaper could read as + // even and be lost without a word. + let gcd = |mut a: usize, mut b: usize| { + while b != 0 { + (a, b) = (b, a % b); + } + a + }; + let (a, k) = (lut.size - 1, m - 1); + let aligned = (a / gcd(a, k)).checked_mul(k).map(|l| l + 1).filter(|&l| l <= MAX_SIZE); + let Some(n) = aligned else { + return err(format!("the .3dl shaper's {m} points can't be kept on a table of at most {MAX_SIZE} nodes")); + }; + let step = (n - 1) as f64; + let mut out = LutFile { size: n, data: vec![0.0; n * n * n * 3], ..lut.clone() }; + for b in 0..n { + for g in 0..n { + for r in 0..n { + let at = ((b * n + g) * n + r) * 3; + let v = sample(&lut, [curve(r as f64 / step), curve(g as f64 / step), curve(b as f64 / step)]); + if let Some(dst) = out.data.get_mut(at..at + 3) { + dst.copy_from_slice(&v); + } + } + } + } + out.check() +} + +/// Trilinear lookup of `rgb` (0..1) in a [`LutFile`] (red fastest). +fn sample(l: &LutFile, rgb: [f32; 3]) -> [f32; 3] { + let n = l.size; + let loc = |v: f32| { + let p = v.clamp(0.0, 1.0) * (n - 1) as f32; + let i = (p as usize).min(n.saturating_sub(2)); + (i, p - i as f32) + }; + let (r0, fr) = loc(rgb[0]); + let (g0, fg) = loc(rgb[1]); + let (b0, fb) = loc(rgb[2]); + let at = |r: usize, g: usize, b: usize, k: usize| l.data.get(((b * n + g) * n + r) * 3 + k).copied().unwrap_or(0.0); + let mut o = [0.0f32; 3]; + for (k, ov) in o.iter_mut().enumerate() { + let c = |r, g, b| at(r, g, b, k); + let x00 = c(r0, g0, b0) + (c(r0 + 1, g0, b0) - c(r0, g0, b0)) * fr; + let x10 = c(r0, g0 + 1, b0) + (c(r0 + 1, g0 + 1, b0) - c(r0, g0 + 1, b0)) * fr; + let x01 = c(r0, g0, b0 + 1) + (c(r0 + 1, g0, b0 + 1) - c(r0, g0, b0 + 1)) * fr; + let x11 = c(r0, g0 + 1, b0 + 1) + (c(r0 + 1, g0 + 1, b0 + 1) - c(r0, g0 + 1, b0 + 1)) * fr; + let y0 = x00 + (x10 - x00) * fg; + let y1 = x01 + (x11 - x01) * fg; + *ov = y0 + (y1 - y0) * fb; + } + o +} + +/// The code range a `.3dl` node table without a declared output depth was written with: the +/// smallest common integer depth that covers the largest value; a table of 0..1 floats passes +/// through. A headerless table has no 10-bit reading — 255 and 1023 are on the 12-bit scale, as +/// Photoshop 27.11 reads them (#9121). +fn inferred_3dl_scale(max: f64, header: bool) -> f64 { + if max <= 1.0 { + return 1.0; + } + let depths: &[f64] = if header { &[1023.0, 4095.0, 16383.0, 65535.0] } else { &[4095.0, 16383.0, 65535.0] }; + depths.iter().copied().find(|&s| max <= s).unwrap_or(max) } /// SpeedGrade `.look`: `` and a hex `` string of little-endian float32 RGB triplets /// (red fastest). pub fn parse_look(text: &str) -> Result { + // Comments may hold tag-like text, so drop them before looking for tags (#9109). + let mut clean = String::with_capacity(text.len()); + let mut rest = text; + while let Some(i) = rest.find("").map_or("", |e| &rest[i + 4 + e + 3..]); + } + clean.push_str(rest); + let text = clean.as_str(); let tag = |name: &str| -> Option { let open = format!("<{name}>"); let start = text.find(&open)? + open.len(); @@ -215,7 +454,14 @@ pub fn parse_look(text: &str) -> Result { }; let size: usize = tag("size").and_then(|s| s.parse().ok()).ok_or_else(|| LutError("no in .look".into()))?; let (entries, _) = checked_data_len(size)?; - let hex: Vec = tag("data").ok_or_else(|| LutError("no in .look".into()))?.bytes().filter(u8::is_ascii_hexdigit).collect(); + let hex: Vec = tag("data").ok_or_else(|| LutError("no in .look".into()))?.bytes().filter(|b| !b.is_ascii_whitespace()).collect(); + if hex.iter().any(|b| !b.is_ascii_hexdigit()) { + return err("invalid hexadecimal in .look"); + } + // Each float needs eight hex digits; as_chunks would silently discard a partial value. + if !hex.len().is_multiple_of(8) { + return err("incomplete float in .look "); + } let nib = |c: u8| (c as char).to_digit(16).unwrap_or(0) as u8; let bytes: Vec = hex.as_chunks::<2>().0.iter().map(|p| nib(p[0]) << 4 | nib(p[1])).collect(); let floats: Vec = bytes.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect(); @@ -308,6 +554,226 @@ pub fn builtin(id: &str) -> Option { mod tests { use super::*; + /// An identity `.3dl` of edge `n` (12-bit nodes, blue fastest) under `shaper`. + fn identity_3dl(shaper: &[u32], n: usize) -> String { + let mut t = shaper.iter().map(u32::to_string).collect::>().join(" ") + "\n"; + let node = |i: usize| (i * 4095 / (n - 1)).to_string(); + for r in 0..n { + for g in 0..n { + for b in 0..n { + t += &format!("{} {} {}\n", node(r), node(g), node(b)); + } + } + } + t + } + + #[test] + fn three_dl_shaper_applies_before_the_table() { + // #8982: Photoshop applies a .3dl's shaper row as a curve ahead of the 3-D table. On grey + // 128 an identity table with the shaper 0 114 455 1023 gives 71 there, and a 17-point + // squared shaper 64 (reported measurements); even ramps leave grey 128 as it is. + let grey = |l: &LutFile| (sample(l, [128.0 / 255.0; 3])[0] * 255.0).round(); + let bent = parse_3dl(&identity_3dl(&[0, 114, 455, 1023], 4)).unwrap(); + assert_eq!(grey(&bent), 71.0); + let squared: Vec = (0..17).map(|i| (1023.0 * (i as f64 / 16.0).powi(2)).round() as u32).collect(); + assert_eq!(grey(&parse_3dl(&identity_3dl(&squared, 17)).unwrap()), 64.0); + let even4 = parse_3dl(&identity_3dl(&[0, 341, 682, 1023], 4)).unwrap(); + assert_eq!(grey(&even4), 128.0); + assert_eq!(even4, parse_3dl(&identity_3dl(&[0, 341, 682, 1023], 4).lines().skip(1).collect::>().join("\n")).unwrap()); + let even17: Vec = (0..17).map(|i| (1023.0 * i as f64 / 16.0).round() as u32).collect(); + assert_eq!(grey(&parse_3dl(&identity_3dl(&even17, 17)).unwrap()), 128.0); + // The ends stay put, and the shaper bends each channel on its own. + assert_eq!(sample(&bent, [0.0, 1.0, 0.0]), [0.0, 1.0, 0.0]); + let c = sample(&bent, [1.0 / 3.0, 0.0, 2.0 / 3.0]); + assert!((c[0] - 114.0 / 1023.0).abs() < 1e-4 && (c[2] - 455.0 / 1023.0).abs() < 1e-4, "{c:?}"); + // A shaper that isn't finite is refused rather than dropped (see + // `three_dl_non_finite_shaper_is_refused`). + assert!(parse_3dl(&identity_3dl(&[0, 341, 682, 1023], 4).replacen("341", "NaN", 1)).is_err()); + } + + /// #9170 review: a float shaper (values in 0..1) is held to a float ramp, not to one integer + /// code, so a bent float shaper is applied. + #[test] + fn three_dl_float_shaper_is_applied() { + let grey = |l: &LutFile| (sample(l, [128.0 / 255.0; 3])[0] * 255.0).round(); + let with = |shaper: &str| { + let table = identity_3dl(&[0, 1, 2, 3], 4); + parse_3dl(&format!("{shaper}\n{}", table.split_once('\n').unwrap().1)).unwrap() + }; + // 0.502 lands between 0.1 and 0.3 at 0.506 of the way: 0.2012, so grey 51. + assert_eq!(grey(&with("0 0.1 0.3 1.0")), 51.0); + // An even float ramp, written to three decimals, still changes nothing. + assert_eq!(grey(&with("0 0.333 0.667 1.0")), 128.0); + } + + /// #9170 review: a shaper with more points than the table keeps its curve: the table is + /// resampled at the shaper's resolution rather than reading the shaper at its own two nodes. + #[test] + fn three_dl_shaper_finer_than_the_table_keeps_its_curve() { + let grey = |l: &LutFile| (sample(l, [128.0 / 255.0; 3])[0] * 255.0).round(); + let small = parse_3dl(&identity_3dl(&[0, 0, 512, 1023], 2)).unwrap(); + // 0.502 lands between 0 and 512 at 0.506 of the way: 259 / 1023, so grey 65. + assert_eq!(grey(&small), 65.0); + assert_eq!(sample(&small, [1.0, 0.0, 1.0]), [1.0, 0.0, 1.0], "the ends stay put"); + } + + /// #9170 review: a finite but extreme shaper can't overflow into NaN when it is baked. + #[test] + fn three_dl_extreme_shaper_bakes_only_finite_values() { + let table = identity_3dl(&[0, 1, 2, 3], 2); + let text = format!("0 1e308 -1e308 0\n{}", table.split_once('\n').unwrap().1); + // Refused or baked, but never a table holding NaN. + if let Ok(l) = parse_3dl(&text) { + assert!(l.data.iter().all(|v| v.is_finite()), "baked a non-finite value"); + } + } + + /// #9170 review (bommerts): the shaper's breakpoints survive a table whose grid doesn't line + /// up with them. The bake grid holds both the table's nodes and the shaper's knots, so on an + /// identity table the curve is exact: `0 0 512 1023` is 0 at ⅓ over 5³ and 17³ tables. + #[test] + fn three_dl_shaper_knots_survive_a_nonaligned_table() { + for n in [5, 17] { + let l = parse_3dl(&identity_3dl(&[0, 0, 512, 1023], n)).unwrap(); + let at = |x: f32| sample(&l, [x, 0.0, 0.0])[0]; + assert!(at(1.0 / 3.0).abs() < 1e-6, "{n}³: {}", at(1.0 / 3.0)); + // The helper's 12-bit nodes are truncated, so the table itself is within a 12-bit step. + assert!((at(2.0 / 3.0) - 512.0 / 1023.0).abs() < 1.0 / 4095.0, "{n}³: {}", at(2.0 / 3.0)); + assert!((at(0.5) - 256.0 / 1023.0).abs() < 1.0 / 4095.0, "{n}³: halfway between the knots"); + } + } + + /// A two-value shaper row (a 2³ table's) is applied like a longer one, not dropped; the even + /// two-point ramp changes nothing. + #[test] + fn three_dl_two_point_shaper_is_applied() { + let bent = parse_3dl(&identity_3dl(&[0, 512], 2)).unwrap(); + let top = sample(&bent, [1.0, 1.0, 1.0]); + assert!(top.iter().all(|v| (v - 512.0 / 1023.0).abs() < 1e-4), "{top:?}"); + assert!((sample(&bent, [0.5, 0.0, 0.0])[0] - 256.0 / 1023.0).abs() < 1e-4); + let even = parse_3dl(&identity_3dl(&[0, 1023], 2)).unwrap(); + assert_eq!( + even, + parse_3dl(&identity_3dl(&[0, 1023], 2).lines().skip(1).collect::>().join( + " +" + )) + .unwrap() + ); + // A second two-value row is a second shaper, refused like a longer one. + let body = identity_3dl(&[0, 512], 2); + assert!( + parse_3dl(&format!( + "{body}0 512 +" + )) + .is_err() + ); + } + + /// #9170 review (bommerts): a shaper row with a token that isn't a number, or a second + /// shaper row, is refused rather than silently dropped. + #[test] + fn three_dl_malformed_shaper_rows_are_refused() { + let table = identity_3dl(&[0, 1, 2, 3], 4); + let body = table.split_once('\n').unwrap().1; + assert!(parse_3dl(&format!("0 341 junk 682 1023\n{body}")).is_err(), "a junk token"); + assert!(parse_3dl(&format!("0 114 455 1023\n0 114 455 1023\n{body}")).is_err(), "two shaper rows"); + assert!(parse_3dl(&format!("0 114 455 1023\n{body}")).is_ok()); + } + + /// #9170 review (bommerts): the standard `3DMESH` / `Title lut 12` preamble still reads; only + /// the rows after it are parsed strictly. + #[test] + fn three_dl_mesh_preamble_is_accepted() { + let grey = |l: &LutFile| (sample(l, [128.0 / 255.0; 3])[0] * 255.0).round(); + let plain = identity_3dl(&[0, 114, 455, 1023], 4); + let headed = parse_3dl(&format!("3DMESH\nTitle lut 12\n{plain}")).unwrap(); + assert_eq!(headed, parse_3dl(&plain).unwrap(), "the preamble changes nothing"); + assert_eq!(grey(&headed), 71.0, "the bent shaper still applies"); + let even = identity_3dl(&[0, 341, 682, 1023], 4); + assert_eq!(grey(&parse_3dl(&format!("3dmesh\nTitle lut 12\n{even}")).unwrap()), 128.0); + assert!( + parse_3dl(&format!("3DMESH\nTitle lut 12\n0 341 junk 682 1023\n{}", even.split_once('\n').unwrap().1)).is_err(), + "still strict after it" + ); + } + + /// #9170 review: a shaper that isn't finite is refused, not dropped, so a Color Lookup never + /// renders the table alone for a file whose shaper it can't apply. + #[test] + fn three_dl_non_finite_shaper_is_refused() { + let body = identity_3dl(&[0, 1, 2, 3], 4); + let body = body.split_once('\n').unwrap().1; + for shaper in ["0 NaN 682 1023", "0 341 inf 1023", "-inf 341 682 1023"] { + assert!(parse_3dl(&format!("{shaper}\n{body}")).is_err(), "{shaper}"); + } + } + + /// #9170 review: a shaper row that starts with `NaN` or `inf` is a shaper, not a header, so + /// it reaches the non-finite refusal instead of being skipped. + #[test] + fn three_dl_shaper_starting_non_finite_is_refused() { + let body = identity_3dl(&[0, 1, 2, 3], 4); + let body = body.split_once('\n').unwrap().1; + for shaper in ["NaN 341 682 1023", "inf 341 682 1023", "-inf 341 682 1023"] { + assert!(parse_3dl(&format!("{shaper}\n{body}")).is_err(), "{shaper}"); + } + } + + /// #9170 review: only a line starting with a word is a header. One whose first token looks + /// like a number but isn't (`0oops`) is read strictly and refused, not skipped with its shaper. + #[test] + fn three_dl_malformed_first_token_is_refused() { + let body = identity_3dl(&[0, 1, 2, 3], 4); + let body = body.split_once('\n').unwrap().1; + for shaper in ["0oops 114 455 1023", "-x 114 455 1023", ".5x 114 455 1023", "+ 114 455 1023"] { + assert!(parse_3dl(&format!("{shaper}\n{body}")).is_err(), "{shaper}"); + } + let (first, rest) = body.split_once('\n').unwrap(); + assert!(parse_3dl(&format!("0 114 455 1023\n{first}\n1x 0 0\n{rest}")).is_err(), "a table row too"); + assert!(parse_3dl(&format!("3DMESH\nTitle lut 12\n0 114 455 1023\n{body}")).is_ok(), "headers still skip"); + } + + /// #9170 review: a bent shaper whose knots can't all be kept within MAX_SIZE is refused rather + /// than sampled where it may look even (here a 257-point shaper bent only at odd knots, whose + /// 129-node fallback would read the identity). + #[test] + fn three_dl_shaper_too_fine_to_keep_is_refused() { + let shaper: Vec = (0..257u32).map(|i| (i * 1023 / 256 + if i % 2 == 1 { 40 } else { 0 }).min(1023)).collect(); + assert!(parse_3dl(&identity_3dl(&shaper, 2)).is_err()); + // As many knots as nodes stays within reach. + let same: Vec = (0..17u32).map(|i| (1023.0 * (f64::from(i) / 16.0).powi(2)).round() as u32).collect(); + assert!(parse_3dl(&identity_3dl(&same, 17)).is_ok()); + } + + #[test] + fn cube_title_with_hash_round_trips() { + let l = LutFile::from_fn("Look #3", 2, |c| c).unwrap(); + assert_eq!(parse_cube(&write_cube(&l)).unwrap().title, "Look #3"); + let t = "TITLE \"A #b\" # note\nLUT_3D_SIZE 2 # c\n# comment\n"; + let rows = "0 0 0 # x\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n"; + let p = parse_cube(&format!("{t}{rows}")).unwrap(); + assert_eq!(p.title, "A #b"); + } + + #[test] + fn write_cube_keeps_non_default_domain() { + let mut l = LutFile::identity(2).unwrap(); + l.domain_min = [0.1; 3]; + l.domain_max = [0.9; 3]; + let back = parse_cube(&write_cube(&l)).unwrap(); + assert_eq!(back.domain_min, [0.1; 3]); + assert_eq!(back.domain_max, [0.9; 3]); + l.domain_min = [0.0, 0.0, 0.5]; + l.domain_max = [1.0; 3]; + let back = parse_cube(&write_cube(&l)).unwrap(); + assert_eq!(back.domain_min, [0.0, 0.0, 0.5]); + assert_eq!(back.domain_max, [1.0; 3]); + assert!(!write_cube(&LutFile::identity(2).unwrap()).contains("DOMAIN")); + } + #[test] fn cube_domain_is_kept_and_validated() { let base = "TITLE \"t\" @@ -403,6 +869,26 @@ DOMAIN_MAX 0.5 1 1 assert!(parse_cube(&text).is_err()); } + #[test] + fn three_dl_without_a_header_reads_low_nodes_as_12_bit() { + let table = |peak: u32| -> String { + let mut text = String::from("0 1023\n"); + for i in 0..8 { + let on = |bit: u32| if i & bit != 0 { peak } else { 0 }; + text.push_str(&format!("{} {} {}\n", on(4), on(2), on(1))); + } + text + }; + for peak in [255, 1023, 4095] { + let white = parse_3dl(&table(peak)).unwrap().data[21..24].to_vec(); + assert_eq!(white, [peak as f32 / 4095.0; 3], "peak {peak}"); + } + // A header keeps the inferred 10-bit depth. The bare `3DMESH` magic is a header without a + // cube declaration; `Mesh 1 10` would declare a 3³ cube and refuse this 2³ table (#9078). + let white = parse_3dl(&format!("3DMESH\n{}", table(1023))).unwrap().data[21..24].to_vec(); + assert_eq!(white, [1.0; 3]); + } + #[test] fn three_dl_blue_fastest_and_bit_depth() { let mut text = String::from("0 1023\n"); @@ -418,6 +904,143 @@ DOMAIN_MAX 0.5 1 1 assert_eq!(l.data, LutFile::identity(2).unwrap().data); } + /// A `size`³ inverse-identity `.3dl` (blue fastest): node (r,g,b) outputs `max - max·v/(size-1)` + /// per channel, with a uniform shaper of `size` codes up to 1023. + fn mesh_3dl(n_bits: u32, out_bits: u32, size: usize, max: f64, shaper: bool) -> String { + let mut text = format!("3DMESH\nMesh {n_bits} {out_bits}\n"); + if shaper { + for i in 0..size { + if i > 0 { + text.push(' '); + } + text.push_str(&format!("{}", (1023.0 * i as f64 / (size - 1) as f64).round())); + } + text.push('\n'); + } + for r in 0..size { + for g in 0..size { + for b in 0..size { + let v = [r, g, b].map(|c| (max - max * c as f64 / (size - 1) as f64).round()); + text.push_str(&format!("{} {} {}\n", v[0], v[1], v[2])); + } + } + } + text + } + + #[test] + fn three_dl_mesh_three_point_shaper_is_not_a_node() { + // #9078: Photoshop loads a 3³ cube whose shaper line has three values; counting it as a + // 28th node rejected the whole file. + let l = parse_3dl(&mesh_3dl(1, 12, 3, 4095.0, true)).unwrap(); + assert_eq!(l.size, 3); + assert_eq!(l.data.len(), 3 * 3 * 3 * 3); + // The inverse identity: node (0,0,0) is full, node (2,2,2) black. + assert!((l.data[0] - 1.0).abs() < 1e-6 && (l.data[1] - 1.0).abs() < 1e-6 && (l.data[2] - 1.0).abs() < 1e-6); + let last = &l.data[l.data.len() - 3..]; + assert_eq!(last, [0.0, 0.0, 0.0]); + // The same 27 nodes without the shaper row still load (the node-only control). + assert_eq!(parse_3dl(&mesh_3dl(1, 12, 3, 4095.0, false)).unwrap().data, l.data); + } + + #[test] + fn three_dl_mesh_declares_the_output_depth() { + // #9073: identical node codes divide by the declared range, not by the largest value. + let mut text_10 = String::new(); + let mut text_12 = String::new(); + for (n_bits, out_bits, dst) in [(2, 10, &mut text_10), (2, 12, &mut text_12)] { + // A 5³ identity scaled to 10-bit codes (max 1023). + *dst = format!("3DMESH\nMesh {n_bits} {out_bits}\n0 256 512 768 1023\n"); + for r in 0..5 { + for g in 0..5 { + for b in 0..5 { + dst.push_str(&format!("{} {} {}\n", r * 255, g * 255, b * 255)); + } + } + } + } + let white_10 = parse_3dl(&text_10).unwrap(); + let white_12 = parse_3dl(&text_12).unwrap(); + assert_eq!(white_10.size, 5); + let last_10 = &white_10.data[white_10.data.len() - 3..]; + let last_12 = &white_12.data[white_12.data.len() - 3..]; + assert!((last_10[0] - 1020.0 / 1023.0).abs() < 1e-6, "{last_10:?}"); + // 1020/4095 ≈ 0.249: white renders grey, as Photoshop measures it. + assert!((last_12[0] - 1020.0 / 4095.0).abs() < 1e-6, "{last_12:?}"); + } + + #[test] + fn three_dl_mesh_header_mismatch_is_rejected() { + // A Mesh header that disagrees with the row count is refused, not silently re-inferred. + assert!(parse_3dl("3DMESH\nMesh 1 12\n0 0 0\n0 0 1\n").is_err()); + // A node beyond the declared range is a wrong declaration: the old inference from the + // largest value keeps the file loading as it did before the header was honoured. + let mut text = String::from("3DMESH\nMesh 1 10\n"); + for r in 0..3 { + for g in 0..3 { + for b in 0..3 { + text.push_str(&format!("{} {} {}\n", r * 2047, g * 2047, b * 2047)); + } + } + } + let l = parse_3dl(&text).unwrap(); + let last = &l.data[l.data.len() - 3..]; + assert!((last[0] - 4094.0 / 4095.0).abs() < 1e-6, "{last:?}"); + } + + #[test] + fn three_dl_mesh_extra_node_cannot_replace_a_captured_shaper() { + for (bits, size) in [(2, 5), (3, 9)] { + let mut text = mesh_3dl(bits, 12, size, 4095.0, true); + let valid = parse_3dl(&text).unwrap(); + assert_eq!(valid.size, size); + assert_eq!(valid.data, parse_3dl(&mesh_3dl(bits, 12, size, 4095.0, false)).unwrap().data); + text.push_str("4095 4095 4095\n"); + let error = parse_3dl(&text).unwrap_err().to_string(); + assert!(error.contains(&format!("expected {} rows", size * size * size)), "{error}"); + } + } + + #[test] + fn three_dl_malformed_mesh_header_falls_back_to_inference() { + // A `Mesh` line that is not exactly two integer fields is not a declaration: it is + // ignored and the table is inferred as if headerless, rather than recovering numbers + // from the wrong positions (#9078). A 2³ cube would otherwise be read as a 3³ cube. + for header in ["Mesh invalid 1 12", "Mesh 1 12 13", "Mesh 1 x", "Mesh 1"] { + let mut text = format!("3DMESH\n{header}\n"); + for r in 0..2 { + for g in 0..2 { + for b in 0..2 { + text.push_str(&format!("{} {} {}\n", r * 1023, g * 1023, b * 1023)); + } + } + } + let l = parse_3dl(&text).unwrap(); + assert_eq!(l.size, 2, "{header}"); + assert_eq!(l.data, LutFile::identity(2).unwrap().data, "{header}"); + } + } + + #[test] + fn three_dl_mesh_uses_declared_range_for_small_integer_codes() { + // #9073: under a valid `Mesh` declaration the node codes are integers in the declared + // range, so a max value of 1 is 1/4095 of full output — not full output itself. + let mut text = String::from("3DMESH\nMesh 1 12\n"); + for r in 0..3 { + for g in 0..3 { + for b in 0..3 { + let v = u8::from(r == 0 && g == 0 && b == 0); + text.push_str(&format!("{v} {v} {v}\n")); + } + } + } + let l = parse_3dl(&text).unwrap(); + assert_eq!(l.size, 3); + // blue fastest: node (0,0,0) is the first entry, code 1 → 1/4095. + assert!((l.data[0] - 1.0 / 4095.0).abs() < 1e-6, "{:?}", &l.data[0..3]); + assert!(l.data[3..].iter().all(|&v| v == 0.0), "{:?}", l.data); + } + #[test] fn look_hex_floats() { let id = LutFile::identity(2).unwrap(); @@ -426,12 +1049,81 @@ DOMAIN_MAX 0.5 1 1 assert_eq!(parse("a.look", xml.as_bytes()).unwrap().data, id.data); } + #[test] + fn look_reads_utf16_with_either_byte_order() { + let id = LutFile::identity(2).unwrap(); + let hex: String = id.data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect(); + let xml = format!("\"2\"\"{hex}\""); + let le: Vec = [0xFF, 0xFE].into_iter().chain(xml.encode_utf16().flat_map(u16::to_le_bytes)).collect(); + let be: Vec = [0xFE, 0xFF].into_iter().chain(xml.encode_utf16().flat_map(u16::to_be_bytes)).collect(); + assert_eq!(parse("a.look", &le).unwrap().data, id.data); + assert_eq!(parse("a.look", &be).unwrap().data, id.data); + // Sniffed as XML through the byte-order mark too, so the recorded format is the one parsed. + assert_eq!(Format::detect("a.lut", &le), Format::Look); + assert_eq!(parse_as(Format::Look, &be).unwrap().data, id.data); + } + + #[test] + fn look_ignores_tags_inside_xml_comments() { + let hex = |l: &LutFile| -> String { l.data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect() }; + let inverse = LutFile::from_fn("", 2, |[r, g, b]| [1.0 - r, 1.0 - g, 1.0 - b]).unwrap(); + let xml = format!( + "\"2\"\"{}\" after a write and parse; the writer now adds one amp; level and the reader removes exactly one. * craft-captions: a WebVTT speaker name with > no longer spills into the cue text Ported from storytold/filmcraft#4470. The writer puts the lookalike U+203A in place of > inside the voice tag. Also from that pull request's review: a line break in a name is written as a space, and a name ending in - is kept apart from the tag's closing >. * craft-captions: an SRT text line that looks like a timing line survives a round trip Ported from storytold/filmcraft#4448. The writer marks such a line with a leading zero-width space and the reader removes it, so the cue is not split. * craft-captions: back-to-back SCC captions with the same text stay separate cues Ported from storytold/filmcraft#3808. Every End of Caption now closes the open cue. MCC's CEA-608 path uses the same decoder. * craft-captions: an SCC pop-on load may run across the previous caption's erase Ported from storytold/filmcraft#1837 (filmcraft#1791). A caption that closely follows another popped on about a second late, because its load had to be one unbroken run of free frames. Doubled codes stay adjacent; pauses are allowed between them. * craft-captions: CEA-708 export starts each caption at its window's top left Ported from storytold/filmcraft#4043 (filmcraft#4042). Each window load now sends DeleteWindows before DefineWindow in place of the ClearWindows after it: a window made anew is empty with the pen at its top left in any decoder, for the same number of bytes, so cue timing is unchanged. * craft-captions: TTML/DFXP export keeps literal < and > text Ported from storytold/filmcraft#2830. The writer treated every < with a later > as a tag and wrote nothing for unknown names, so "1 < 2 and 3 > 2" lost its middle. Only a well-formed tag is markup now; a > inside a quoted attribute value does not end the tag. * craft-captions: TTML/DFXP export keeps cue IDs unique and valid Unicode IDs as written Ported from the TTML commit of storytold/filmcraft#1839. A cue named s1 could collide with the style ID, and a generated c1 could take the ID of a later cue. Valid explicit IDs are reserved first; missing, invalid or repeated ones get a replacement that is in no one's way. IDs are checked as XML NCNames. * craft-captions: compare the craft-xml and roxmltree views of TTML documents The TTML reader moved from roxmltree to craft-xml. This dev-only test checks that both hand it the same elements, attributes and text, by namespace, on TTML documents and on the writer's output. roxmltree becomes a dev-dependency, which the cue ID tests use too. --------- Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Co-authored-by: SeeRay11 Co-authored-by: jnohclee-rgb <336857373+jnohclee-rgb@users.noreply.github.com> Co-authored-by: JonRC Co-authored-by: sanyamsingh001 <314051937+sanyamsingh001@users.noreply.github.com> --- Cargo.lock | 1 + crates/captions/Cargo.toml | 2 + crates/captions/README.md | 11 + crates/captions/src/mcc.rs | 108 +++++- crates/captions/src/scc.rs | 66 +++- crates/captions/src/srt.rs | 39 +- crates/captions/src/ttml.rs | 167 ++++++-- crates/captions/src/vtt.rs | 80 +++- crates/captions/tests/it/main.rs | 5 + crates/captions/tests/it/mcc_708_timing.rs | 27 ++ crates/captions/tests/it/roundtrip.rs | 47 +++ .../captions/tests/it/ttml_control_chars.rs | 19 + crates/captions/tests/it/ttml_cue_ids.rs | 362 ++++++++++++++++++ .../captions/tests/it/ttml_literal_angle.rs | 76 ++++ crates/captions/tests/it/ttml_xml_views.rs | 119 ++++++ 15 files changed, 1078 insertions(+), 51 deletions(-) create mode 100644 crates/captions/tests/it/mcc_708_timing.rs create mode 100644 crates/captions/tests/it/ttml_control_chars.rs create mode 100644 crates/captions/tests/it/ttml_cue_ids.rs create mode 100644 crates/captions/tests/it/ttml_literal_angle.rs create mode 100644 crates/captions/tests/it/ttml_xml_views.rs diff --git a/Cargo.lock b/Cargo.lock index 460fdfd92..1b7bfd666 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -903,6 +903,7 @@ version = "0.1.0" dependencies = [ "craft-xml", "proptest", + "roxmltree 0.21.2", "thiserror 2.0.21", ] diff --git a/crates/captions/Cargo.toml b/crates/captions/Cargo.toml index 81f0e6575..70293e2d3 100644 --- a/crates/captions/Cargo.toml +++ b/crates/captions/Cargo.toml @@ -16,3 +16,5 @@ thiserror = { workspace = true } [dev-dependencies] proptest = { workspace = true } +# A second XML reader for the tests: checks of written TTML, and the comparison in tests/it/ttml_xml_views.rs. +roxmltree = "0.21" diff --git a/crates/captions/README.md b/crates/captions/README.md index 927a5dc44..6fd2a4005 100644 --- a/crates/captions/README.md +++ b/crates/captions/README.md @@ -86,6 +86,17 @@ This mapping follows [W3C TTML1, third edition (2018), §§12.1.5–12.2.1](http The [April 2006 TTaf1 draft, §§7.2.1 and 12.1.5–12.2.1](https://www.w3.org/TR/2006/WD-ttaf1-dfxp-20060427/) defines its earlier metadata namespace and agent ID form. +### TTML cue identifiers + +TTML and DFXP writers keep valid explicit cue IDs verbatim unless a style, region or earlier cue +already uses them. Missing, invalid or repeated IDs receive deterministic replacements that also +avoid valid IDs on later cues. Emitted cue, style, region and speaker IDs are unique across the +document. Unicode spelling is preserved without normalization. + +Identifier validation follows the `NCName` production in [Namespaces in XML 1.0, third edition +(2009), §3](https://www.w3.org/TR/2009/REC-xml-names-20091208/#NT-NCName), using the character +ranges in [XML 1.0, fifth edition (2008), §2.3](https://www.w3.org/TR/2008/REC-xml-20081126/#NT-NameStartChar). + ### SCC writer Each caption is sent as a pop-on load (RCL, ENM, then per row a preamble address code and tab diff --git a/crates/captions/src/mcc.rs b/crates/captions/src/mcc.rs index 0b780ccc1..7169a4ec7 100644 --- a/crates/captions/src/mcc.rs +++ b/crates/captions/src/mcc.rs @@ -23,9 +23,9 @@ //! carries a CDP with 20 cc_data triplets; empty frames are omitted. Each CDP contains field 1 CEA-608 //! pop-on data (the same schedule as [SCC](crate::scc)), a null field-2 pair, CEA-708 //! service 1 data and padding. The CEA-708 captions emulate pop-on with two windows: each -//! caption is defined (hidden) and loaded into one window while the other shows, then a -//! DisplayWindows / HideWindows pair switches them on the caption's first frame; HideWindows -//! clears it at its end. +//! caption goes into one window, deleted and defined anew first (hidden, empty, the pen at its +//! top left), while the other shows, then a DisplayWindows / HideWindows pair switches them on +//! the caption's first frame; HideWindows clears it at its end. //! - **Reading:** every CDP's `cc_data`; channel 1 CEA-608 field-1 data is decoded with the SCC //! decoder when present, otherwise CEA-708 service 1 (windows, text, CR, clear / display / //! hide / toggle / delete windows, DefineWindow visibility); a cue starts whenever the visible @@ -240,15 +240,18 @@ pub fn shown_as_708(c: char) -> char { if char_atom(c).is_some() { c } else { '?' } } -/// Atoms that define window `win` hidden and load `rows` into it. +/// Atoms that make window `win` anew, hidden, and load `rows` into it. fn load_atoms(win: u8, rows: &[String]) -> Vec> { let rc = rows.len().clamp(1, 12) as u8 - 1; let cols = rows.iter().map(|r| r.chars().count()).max().unwrap_or(1).clamp(1, 42) as u8 - 1; let mut v = vec![ + // DeleteWindows: the window holds the caption before last. Decoders differ on whether + // redefining it or ClearWindows moves the pen back (CCExtractor keeps it), but a new + // window starts empty with the pen at its top left, for the same bytes as ClearWindows. + vec![0x8c, 1 << win], // DefineWindow: hidden, row/column lock, relative position 90 % down / 50 % across, // anchored at its bottom centre, rows / columns, window style 1, pen style 1 vec![0x98 + win, 0x18, 0x80 | 90, 50, 7 << 4 | rc, cols, 1 << 3 | 1], - vec![0x88, 1 << win], // ClearWindows ]; for (i, r) in rows.iter().enumerate() { if i > 0 { @@ -788,6 +791,13 @@ mod tests { assert!(parse("garbage").is_err()); } + /// Keep back-to-back captions with the same text separate on the CEA-608 path (the SCC decoder). + #[test] + fn identical_back_to_back_cues_stay_separate_on_608() { + let doc = Document { cues: vec![cue(90, 135, "Ht"), cue(135, 180, "Ht")], ..Default::default() }; + assert_eq!(parse(&write_with(&doc, false, true, false, "test")).unwrap().cues, doc.cues); + } + /// Reject malformed fields instead of silently dropping them from the timecode. #[test] fn rejects_timecodes_with_unparseable_extra_fields() { @@ -891,4 +901,92 @@ mod tests { assert!(decoder.cues.iter().map(|c| c.2.len()).sum::() <= MAX_708_TEXT_BYTES); assert!(decoder.open.is_none()); } + + /// Start every CEA-708 caption at the top left of its window, also for a decoder that sets + /// the pen only when a window is created and moves it only on SetPenLocation, carriage return + /// and text. CCExtractor keeps the pen through ClearWindows and a redefined window, so the + /// third caption below once started at the end of the first one, past its smaller window. + #[test] + fn each_708_caption_starts_at_its_window_top_left() { + let doc = Document { + cues: vec![ + cue(60, 120, "Por que você não admite que\nminha mão robótica te assusta?"), + cue(150, 200, "Você partiu meu coração!"), + cue(230, 250, "Eu sei."), + cue(260, 300, "Você me esqueceu na Terra."), + ], + ..Default::default() + }; + // Service 1 bytes in order, out of the scheduled DTVCC packets. + let mut packets: Vec> = Vec::new(); + for (start, a, b) in schedule_708(&doc).into_values().flatten() { + if start { + packets.push(Vec::new()); + } + packets.last_mut().unwrap().extend([a, b]); + } + let mut s = Vec::new(); + for p in &packets { + let mut body = &p[1..]; + while let [h, rest @ ..] = body { + let n = (h & 0x1f) as usize; + if h >> 5 == 1 { + s.extend_from_slice(&rest[..n]); + } + body = &rest[n..]; + } + } + // Pen (row, column), size (rows, columns) and whether defined, per window; where each + // caption's text starts (its first character after a DefineWindow). + let (mut pen, mut size, mut defined, mut cur) = ([(0usize, 0usize); 8], [(0usize, 0usize); 8], [false; 8], 0usize); + let (mut starts, mut loaded, mut i) = (Vec::new(), false, 0); + while let Some(&c) = s.get(i) { + let mut text = false; + i += match c { + 0x80..=0x87 => { + cur = (c - 0x80) as usize; + 1 + } + 0x8c => { + (0..8).filter(|w| s[i + 1] & 1 << w != 0).for_each(|w| defined[w] = false); + 2 + } + 0x88..=0x8b => 2, + 0x92 => { + pen[cur] = (s[i + 1] as usize, s[i + 2] as usize); + 3 + } + 0x98..=0x9f => { + cur = (c - 0x98) as usize; + if !std::mem::replace(&mut defined[cur], true) { + pen[cur] = (0, 0); + } + size[cur] = ((s[i + 4] & 0x0f) as usize + 1, (s[i + 5] & 0x3f) as usize + 1); + loaded = true; + 7 + } + 0x0d => { + pen[cur] = (pen[cur].0 + 1, 0); + 1 + } + 0x10 => { + text = true; + 2 + } + 0x20..=0x7f | 0xa0..=0xff => { + text = true; + 1 + } + _ => 1, + }; + if text { + if std::mem::take(&mut loaded) { + starts.push((cur, pen[cur])); + } + assert!(pen[cur].0 < size[cur].0 && pen[cur].1 < size[cur].1, "window {cur} {:?}: text at {:?}", size[cur], pen[cur]); + pen[cur].1 += 1; + } + } + assert_eq!(starts, [(0, (0, 0)), (1, (0, 0)), (0, (0, 0)), (1, (0, 0))]); + } } diff --git a/crates/captions/src/scc.rs b/crates/captions/src/scc.rs index 3b285ead0..e0000cff9 100644 --- a/crates/captions/src/scc.rs +++ b/crates/captions/src/scc.rs @@ -11,7 +11,8 @@ //! A tab separates the timecode from the code words. Each line's pairs are sent one per frame //! starting at its timecode. **Reading** runs a channel-1 //! CEA-608 decoder (pop-on, roll-up, paint-on; redundant control codes ignored) and emits a cue -//! whenever the displayed memory changes. **Writing** produces pop-on captions: for each cue the +//! whenever the displayed memory changes or an End of Caption swaps it, so neighbours with the +//! same text stay separate cues. **Writing** produces pop-on captions: for each cue the //! load (RCL, ENM, PAC + tab offset per row, text) is scheduled into free frames before the cue so //! its End of Caption lands exactly on the cue start; an Erase Displayed Memory clears it at the //! cue end (omitted when the next caption replaces it). Rows are bottom-aligned (row 15 last), @@ -116,14 +117,19 @@ impl Decoder { if now == cur { return; } + self.close(f); + if !now.is_empty() { + self.open = Some((f, now)); + } + } + + /// End the cue being shown at frame `f` (dropped if it never reached a frame). + fn close(&mut self, f: i64) { if let Some((s, text)) = self.open.take() && f > s { self.cues.push((s, f, text)); } - if !now.is_empty() { - self.open = Some((f, now)); - } } fn pair(&mut self, f: i64, b1: u8, b2: u8) { @@ -186,6 +192,8 @@ impl Decoder { misc::EOC => { std::mem::swap(&mut self.displayed, &mut self.hidden); self.mode = Mode::PopOn; + // each swap presents its own caption, even one reading the same as the last + self.close(f); } misc::CR => { if let Mode::RollUp(n) = self.mode { @@ -438,14 +446,32 @@ pub fn schedule(doc: &Document) -> BTreeMap { } let load = load_pairs(&c.text); let n = load.len() as i64; - // Doubled codes must stay adjacent, so the load is one contiguous run of free frames: - // the latest run ending by `s`, else the first run after `cursor` (delaying the caption). - let run_free = |occ: &BTreeMap, b: i64| (b..b + n).all(|f| !occ.contains_key(&f)); - let b = (cursor..=s - n).rev().find(|&b| run_free(&occ, b)).unwrap_or_else(|| (cursor..).find(|&b| run_free(&occ, b)).unwrap_or(cursor)); - for (k, p) in load.into_iter().enumerate() { - occ.insert(b + k as i64, p); + // Erasing displayed memory can interrupt loading non-displayed memory. Keep each + // doubled control/character code adjacent, but allow pauses between those groups. + let plan_load = |mut f: i64| { + let mut plan = Vec::with_capacity(load.len()); + let mut k = 0; + while let Some(&p) = load.get(k) { + let doubled = (0x10..=0x1f).contains(&p.0) && load.get(k + 1) == Some(&p); + let width = if doubled { 2 } else { 1 }; + while (f..f + width).any(|at| occ.contains_key(&at)) { + f += 1; + } + for offset in 0..width { + plan.push((f + offset, p)); + } + f += width; + k += width as usize; + } + (plan, f) + }; + // Prefer the latest load that finishes by the requested start; if it cannot fit, + // use the earliest available frames rather than waiting until the prior cue ends. + let (plan, end) = (cursor..=s - n).rev().map(plan_load).find(|(_, end)| *end <= s).unwrap_or_else(|| plan_load(cursor)); + for (f, p) in plan { + occ.insert(f, p); } - let mut d = s.max(b + n); + let mut d = s.max(end); while occ.contains_key(&d) { d += 1; } @@ -561,6 +587,24 @@ mod tests { assert_eq!(got, vec![(60, 120, "Hello, world!"), (120, 200, "Second caption\nwith two lines"), (260, 300, "Ñandú ♪ {x} * café “q”")]); } + /// Replaying the `scc_back_to_back` proptest failure: neighbours with the same text merged. + #[test] + fn identical_back_to_back_cues_stay_separate() { + let doc = Document { + cues: vec![cue(90, 135, "Ht"), cue(135, 180, "Ht"), cue(180, 225, "Ht"), cue(225, 270, "Other"), cue(270, 315, "Ht")], + ..Default::default() + }; + // The stream carries each boundary: every cue has its own End of Caption on its start frame. + let occ = schedule(&doc); + for start in [90, 135, 180, 225, 270] { + assert_eq!(occ.get(&start), Some(&(0x14, misc::EOC)), "frame {start}"); + } + let s = write(&doc, false); + let back = parse(&s).unwrap(); + assert_eq!(back.cues, doc.cues); + assert_eq!(write(&back, false), s); + } + #[test] fn wraps_long_lines() { let rows = wrap_rows("the quick brown fox jumps over the lazy dog and keeps running far away"); diff --git a/crates/captions/src/srt.rs b/crates/captions/src/srt.rs index cab7fa8f6..0c51eede7 100644 --- a/crates/captions/src/srt.rs +++ b/crates/captions/src/srt.rs @@ -34,6 +34,21 @@ fn is_index(line: &str) -> bool { !l.is_empty() && l.len() <= 10 && l.bytes().all(|b| b.is_ascii_digit()) } +const ZWSP: char = '\u{200B}'; + +/// Keep a text line from reading as a timing line: such a line gains a leading zero-width space. +fn escape(line: &str) -> String { + if parse_timing_line(line.trim_start_matches(ZWSP)).is_some() { format!("{ZWSP}{line}") } else { line.to_string() } +} + +/// Reverse [`escape`]: drop one leading zero-width space when the rest still looks like a timing line. +fn unescape_line(line: &str) -> &str { + match line.strip_prefix(ZWSP) { + Some(r) if parse_timing_line(r.trim_start_matches(ZWSP)).is_some() => r, + _ => line, + } +} + pub fn parse(text: &str) -> Result { let text = text.strip_prefix('\u{feff}').unwrap_or(text); let lines: Vec<&str> = text.lines().collect(); @@ -63,7 +78,7 @@ pub fn parse(text: &str) -> Result { if is_index(l) && lines.get(i + 1).is_some_and(|n| parse_timing_line(n).is_some()) { break; } - text_lines.push(l.trim_end()); + text_lines.push(unescape_line(l.trim_end())); i += 1; } if end <= start { @@ -84,7 +99,7 @@ pub fn write(doc: &Document) -> String { out.push_str(&format!("{}\n{} --> {}\n", n + 1, format_clock(c.start, ','), format_clock(c.end, ','))); // blank lines inside text would end the cue: drop them for l in c.text.lines().filter(|l| !l.trim().is_empty()) { - out.push_str(l); + out.push_str(&escape(l)); out.push('\n'); } out.push('\n'); @@ -120,6 +135,26 @@ mod tests { assert_eq!(got, vec![(1500, 2250, "A"), (3000, 4000, "B"), (5000, 6000, "42")]); } + #[test] + fn round_trips_text_line_that_looks_like_timing() { + let mut d = parse("1\n00:00:01,000 --> 00:00:02,000\nx\n").unwrap(); + d.cues[0].text = "Shift:\n9:00 --> 10:00".into(); + let back = parse(&write(&d)).unwrap(); + assert_eq!(back.cues.len(), 1); + assert_eq!(back.cues[0].text, "Shift:\n9:00 --> 10:00"); + } + + #[test] + fn round_trips_arrow_and_marker_text() { + for text in ["a --> b", "a --&gt; b", "a --> b", "Shift:\n9:00 --> 10:00\n\u{200B}9:00 --> 10:00", "5\n9:00 --> 10:00"] { + let mut d = parse("1\n00:00:01,000 --> 00:00:02,000\nx\n").unwrap(); + d.cues[0].text = text.into(); + let back = parse(&write(&d)).unwrap(); + assert_eq!(back.cues.len(), 1, "{text:?}"); + assert_eq!(back.cues[0].text, text); + } + } + #[test] fn ignores_byte_order_mark() { let d = parse("\u{feff}00:00:01,000 --> 00:00:02,000\nHi\n").unwrap(); diff --git a/crates/captions/src/ttml.rs b/crates/captions/src/ttml.rs index 797d86c9c..c439c2ff4 100644 --- a/crates/captions/src/ttml.rs +++ b/crates/captions/src/ttml.rs @@ -88,6 +88,8 @@ fn esc(s: &str) -> String { '>' => o.push_str(">"), '"' => o.push_str("""), '\'' => o.push_str("'"), + // XML 1.0 forbids these (C0 controls, U+FFFE, U+FFFF), even escaped + c if !craft_xml::is_xml_char(c) => {} c => o.push(c), } } @@ -106,6 +108,75 @@ fn time_expr(t: Tick, rate: Option) -> String { } } +/// Memoised "next occurrence of a quote byte" searches, so unterminated quotes are not rescanned. +#[derive(Default)] +struct QuoteCache { + found: [Option<(usize, Option)>; 2], +} + +impl QuoteCache { + /// Index of the next `quote` byte at or after `from`. + fn next(&mut self, bytes: &[u8], quote: u8, from: usize) -> Option { + let slot = usize::from(quote == b'\''); + if let Some((searched, result)) = self.found[slot] + && searched <= from + && result.is_none_or(|r| r >= from) + { + return result; + } + let result = bytes.get(from..).and_then(|t| t.iter().position(|&c| c == quote)).map(|i| from + i); + self.found[slot] = Some((from, result)); + result + } +} + +/// Parses a tag starting at `bytes[start] == b'<'`: optional `/` (closing), a name of ASCII +/// letters/digits starting with a letter immediately after, then whitespace-separated +/// `name="value"` / `name='value'` attributes, optional `/` before `>`. A `>` inside a quoted +/// value does not end the tag. Returns `(index after '>', closing, lowercase name)`; the name is empty for a self-closing tag. +fn parse_tag(bytes: &[u8], start: usize, cache: &mut QuoteCache) -> Option<(usize, bool, String)> { + let mut i = start + 1; + let closing = bytes.get(i) == Some(&b'/'); + if closing { + i += 1; + } + let name_start = i; + if !bytes.get(i)?.is_ascii_alphabetic() { + return None; + } + while bytes.get(i).is_some_and(u8::is_ascii_alphanumeric) { + i += 1; + } + let name = String::from_utf8_lossy(bytes.get(name_start..i)?).to_ascii_lowercase(); + loop { + let ws_start = i; + while bytes.get(i).is_some_and(u8::is_ascii_whitespace) { + i += 1; + } + match bytes.get(i)? { + b'>' => return Some((i + 1, closing, name)), + // self-closing: no markup + b'/' if !closing && bytes.get(i + 1) == Some(&b'>') => return Some((i + 2, closing, String::new())), + _ => {} + } + if i == ws_start { + return None; + } + let key_start = i; + while bytes.get(i).is_some_and(|c| c.is_ascii_alphanumeric() || matches!(c, b'-' | b':' | b'_')) { + i += 1; + } + if i == key_start || bytes.get(i) != Some(&b'=') { + return None; + } + let quote = *bytes.get(i + 1)?; + if quote != b'"' && quote != b'\'' { + return None; + } + i = cache.next(bytes, quote, i + 2)? + 1; + } +} + /// Caption text (`\n` lines, ``/``/`` tags) → TTML inline content. fn inline(text: &str) -> String { let mut out = String::new(); @@ -127,25 +198,12 @@ fn inline(text: &str) -> String { } }; let mut rest = text; - // Once no `>` remains, no later `<` can start a tag either. Remember that instead of searching - // the shrinking suffix again after every literal `<`, which made `<<<<…` quadratic. - let mut closer_remains = true; + let mut cache = QuoteCache::default(); while !rest.is_empty() { - let tag_end = match rest.strip_prefix('<') { - Some(after) if closer_remains => { - let end = after.find('>'); - closer_remains = end.is_some(); - end - } - _ => None, - }; - if let Some(end) = tag_end { - let after = &rest[1..]; - let tag = after[..end].trim().to_ascii_lowercase(); - let (closing, name) = match tag.strip_prefix('/') { - Some(n) => (true, n.trim().to_string()), - None => (false, tag.split_whitespace().next().unwrap_or("").to_string()), - }; + // Only a well-formed tag is markup; "1 < 2 and 3 > 2" and "ad" are text. + if rest.starts_with('<') + && let Some((end, closing, name)) = parse_tag(text.as_bytes(), text.len() - rest.len(), &mut cache) + { let attr = match name.as_str() { "i" => Some("tts:fontStyle=\"italic\""), "b" => Some("tts:fontWeight=\"bold\""), @@ -159,7 +217,7 @@ fn inline(text: &str) -> String { active.push(a); } } - rest = &after[end + 1..]; + rest = text.get(end..).unwrap_or(""); continue; } let Some(ch) = rest.chars().next() else { break }; @@ -177,13 +235,34 @@ fn inline(text: &str) -> String { out } +/// An XML name without a colon (Namespaces in XML, NCName). fn is_ncname(s: &str) -> bool { - let mut c = s.chars(); - c.next().is_some_and(|f| f.is_alphabetic() || f == '_') && c.all(|x| x.is_alphanumeric() || matches!(x, '_' | '-' | '.')) + craft_xml::is_name(s) && !s.contains(':') } -fn cue_id(cue: &Cue, index: usize) -> String { - cue.id.as_deref().filter(|x| is_ncname(x)).map(str::to_string).unwrap_or_else(|| format!("c{}", index + 1)) +fn cue_ids(doc: &Document) -> Vec { + // Reserve every valid explicit ID before allocating fallbacks, including IDs on later cues. + let reserved: HashSet<&str> = + ["s1", "r1"].into_iter().chain(doc.cues.iter().filter_map(|cue| cue.id.as_deref()).filter(|id| is_ncname(id))).collect(); + let mut used: HashSet = ["s1".into(), "r1".into()].into_iter().collect(); + doc.cues + .iter() + .enumerate() + .map(|(index, cue)| { + if let Some(id) = cue.id.as_deref().filter(|id| is_ncname(id)) + && used.insert(id.to_string()) + { + return id.to_string(); + } + let mut id = format!("c{}", index.saturating_add(1)); + // Each collision consumes a distinct reserved or used ID, so this loop is bounded by their combined size. + while reserved.contains(id.as_str()) || used.contains(&id) { + id.push('_'); + } + used.insert(id.clone()); + id + }) + .collect() } /// Write `doc` as TTML. `rate` (IMSC1 only) makes times exact frame offsets; `lang` is the @@ -205,8 +284,9 @@ pub fn write(doc: &Document, flavor: Flavor, rate: Option, lang: &str } o.push_str(&format!(" xml:lang=\"{}\">\n", esc(if lang.is_empty() { "en" } else { lang }))); // Keep agent identifiers distinct from the IDs already emitted for cues, style and region. + let cue_ids = cue_ids(doc); let mut ids: HashSet = ["s1".into(), "r1".into()].into_iter().collect(); - ids.extend(doc.cues.iter().enumerate().map(|(i, c)| cue_id(c, i))); + ids.extend(cue_ids.iter().cloned()); let mut speakers = Vec::new(); let mut speaker_ids = HashMap::new(); for speaker in doc.cues.iter().filter_map(|c| c.speaker.as_deref()) { @@ -235,8 +315,7 @@ pub fn write(doc: &Document, flavor: Flavor, rate: Option, lang: &str " \n \n \n", ); o.push_str(" \n \n
\n"); - for (i, c) in doc.cues.iter().enumerate() { - let id = cue_id(c, i); + for (c, id) in doc.cues.iter().zip(cue_ids) { let agent = c.speaker.as_deref().and_then(|s| speaker_ids.get(s)).map(|id| format!(" ttm:role=\"x-speaker\" ttm:agent=\"{id}\"")).unwrap_or_default(); o.push_str(&format!( @@ -541,7 +620,7 @@ fn collect(n: &El, styles: &std::collections::HashMap, fmt: Fmt, ou for c in &n.children { if let Node::Text(text) | Node::CData(text) = c { for ch in text.chars() { - if ch.is_whitespace() { + if is_xml_space(ch) { out.pending = true; } else { out.flush(); @@ -578,8 +657,14 @@ fn collect(n: &El, styles: &std::collections::HashMap, fmt: Fmt, ou } } +/// XML whitespace: only these characters collapse (U+00A0 and other Unicode spaces are content). +fn is_xml_space(c: char) -> bool { + matches!(c, ' ' | '\t' | '\n' | '\r') +} + fn clean(s: &str) -> String { - s.lines().map(str::trim).collect::>().join("\n").trim().to_string() + // `split` (not `lines`) keeps the empty edge lines produced by a leading or trailing `
`. + s.split('\n').map(|l| l.trim_matches(is_xml_space)).collect::>().join("\n") } pub fn parse(text: &str) -> Result { @@ -718,7 +803,7 @@ fn walk(n: &El, parent: Scope, tm: &Timing, styles: &HashMap, speak o.s.push_str(&format!("")); } let text = clean(&o.s); - if e > b && !text.is_empty() { + if e > b && !text.trim_matches(is_xml_space).is_empty() { doc.cues.push(Cue { start: b, end: e, text, speaker: cue_speaker, id, ..Default::default() }); } } @@ -732,7 +817,7 @@ fn walk(n: &El, parent: Scope, tm: &Timing, styles: &HashMap, speak let mut o = Out { s: String::new(), pending: false }; collect(sp, styles, style_of(sp, styles, f), &mut o); let text = clean(&o.s); - if se > sb && !text.is_empty() { + if se > sb && !text.trim_matches(is_xml_space).is_empty() { doc.cues.push(Cue { start: sb, end: se, @@ -759,6 +844,28 @@ mod tests { Timing { rate, base: rate.timecode_base(), sub_frame_rate: 1, tick_rate: FrameRate::new(1, 1) } } + #[test] + fn only_xml_whitespace_collapses_in_cue_text() { + let xml = "

\t 10\u{a0}km\u{2009}a\u{3000}b \t\r\n c \n

"; + let doc = parse(xml).unwrap(); + assert_eq!(doc.cues[0].text, "10\u{a0}km\u{2009}a\u{3000}b c"); + let xml = "

\u{a0}x
y\u{a0}

"; + let doc = parse(xml).unwrap(); + assert_eq!(doc.cues[0].text, "\u{a0}x\ny\u{a0}"); + } + + #[test] + fn edge_line_breaks_survive_round_trip() { + for (body, want) in [("
text", "\ntext"), ("text
", "text\n"), ("
text
", "\ntext\n")] { + let xml = format!("

{body}

"); + let doc = parse(&xml).unwrap(); + assert_eq!(doc.cues[0].text, want); + let out = write(&doc, Flavor::Dfxp, None, "en"); + let again = parse(&out).unwrap(); + assert_eq!(again.cues[0].text, want); + } + } + #[test] fn invalid_timing_parameters_return_an_error() { for params in [ diff --git a/crates/captions/src/vtt.rs b/crates/captions/src/vtt.rs index e173f925a..e7f770679 100644 --- a/crates/captions/src/vtt.rs +++ b/crates/captions/src/vtt.rs @@ -52,7 +52,53 @@ pub fn split_voice(text: &str) -> (Option, String) { /// as a timing line to anything scanning the block, so it is written `-->`. Other entities /// (`&`, `<`, …) are cue markup and stay verbatim, as `parse` has always kept them. fn unescape_text(text: &str) -> String { - text.replace("-->", "-->") + rewrite_arrows(text, false) +} + +/// Apply [`write`]'s cue-text escape. `-->` becomes `-->`, and a literal `-->` (or the same +/// with more `&` in front of `gt;`) gains one `&`, so the escape is reversible. +fn escape_text(text: &str) -> String { + rewrite_arrows(text, true) +} + +/// Treat `--&` + `amp;`×k + `gt;` as level k, and `-->` as level -1. Escaping raises the level by +/// one, unescaping lowers it by one; anything else is copied unchanged. +fn rewrite_arrows(text: &str, escape: bool) -> String { + let mut out = String::with_capacity(text.len()); + let mut rest = text; + while let Some(c) = rest.chars().next() { + if let Some(after) = rest.strip_prefix("--") { + if escape && after.starts_with('>') { + out.push_str("-->"); + rest = &after[1..]; + continue; + } + let mut tail = after.strip_prefix('&').unwrap_or("\0"); + let mut k = 0usize; + while let Some(t) = tail.strip_prefix("amp;") { + tail = t; + k += 1; + } + if let Some(t) = tail.strip_prefix("gt;") { + if escape { + out.push_str("--&"); + out.push_str(&"amp;".repeat(k + 1)); + out.push_str("gt;"); + } else if k == 0 { + out.push_str("-->"); + } else { + out.push_str("--&"); + out.push_str(&"amp;".repeat(k - 1)); + out.push_str("gt;"); + } + rest = t; + continue; + } + } + out.push(c); + rest = &rest[c.len_utf8()..]; + } + out } pub fn parse(text: &str) -> Result { @@ -142,11 +188,15 @@ pub fn write(doc: &Document) -> String { out.push('\n'); let mut text = String::new(); if let Some(s) = c.speaker.as_deref().filter(|s| !s.is_empty()) { - text.push_str(&format!("")); + // `>` would end the voice tag early and spill the rest of the name into the cue text, so it + // is written as the lookalike `›` (U+203A), and a line break as a space. A name ending + // in `-` gets a space before the closing `>`, so the two never read as an arrow. + let name = s.replace('>', "\u{203a}").replace(['\r', '\n'], " "); + text.push_str(&format!("", if name.ends_with('-') { " " } else { "" })); } text.push_str(&c.text); for l in text.lines().filter(|l| !l.trim().is_empty()) { - out.push_str(&l.replace("-->", "-->")); + out.push_str(&escape_text(l)); out.push('\n'); } out.push('\n'); @@ -196,6 +246,19 @@ mod tests { assert_eq!(parse(&w).unwrap().cues, d.cues); } + #[test] + fn speaker_with_gt_keeps_cue_text_intact() { + for (name, back_name) in [("A > B", "A \u{203a} B"), ("Tom & Jerry", "Tom & Jerry"), ("A > --", "A \u{203a} --"), ("A\nB", "A B")] { + let mut doc = Document::default(); + doc.cues.push(Cue { start: Tick(0), end: Tick(TICKS_PER_MS), text: "Hi".into(), speaker: Some(name.into()), ..Default::default() }); + let written = write(&doc); + assert!(!written.contains("-->"), "the voice tag keeps its own `>`: {written}"); + let back = parse(&written).unwrap(); + assert_eq!(back.cues[0].speaker.as_deref(), Some(back_name)); + assert_eq!(back.cues[0].text, "Hi"); + } + } + #[test] fn newline_in_cue_id_round_trips() { let mut doc = Document::default(); @@ -267,6 +330,17 @@ mod tests { assert_eq!(write(&back), w, "stable from there on"); } + /// A literal `-->` in cue text must not turn into `-->` on a round trip. + #[test] + fn a_literal_escaped_arrow_survives_a_round_trip() { + for text in ["a --> b", "a --> b", "--&gt;", "--->-->", "x --&amp;gt; y"] { + let mut d = Document::default(); + d.cues.push(Cue { start: Tick(0), end: Tick(1000 * TICKS_PER_MS), text: text.into(), ..Cue::default() }); + let back = parse(&write(&d)).unwrap(); + assert_eq!(back.cues[0].text, text); + } + } + /// Only the writer's own `-->` escape is reversed: cue markup keeps its entities verbatim, /// as `parse` has always done. #[test] diff --git a/crates/captions/tests/it/main.rs b/crates/captions/tests/it/main.rs index f1b66ae62..92560a1b2 100644 --- a/crates/captions/tests/it/main.rs +++ b/crates/captions/tests/it/main.rs @@ -1,6 +1,11 @@ mod limits; +mod mcc_708_timing; mod roundtrip; mod ttml_body_timing; mod ttml_clock_overflow; +mod ttml_control_chars; +mod ttml_cue_ids; mod ttml_language; +mod ttml_literal_angle; mod ttml_speakers; +mod ttml_xml_views; diff --git a/crates/captions/tests/it/mcc_708_timing.rs b/crates/captions/tests/it/mcc_708_timing.rs new file mode 100644 index 000000000..77289f0b0 --- /dev/null +++ b/crates/captions/tests/it/mcc_708_timing.rs @@ -0,0 +1,27 @@ +//! CEA-708 timing of back-to-back one-frame captions in MCC. + +use craft_captions::{Cue, Document, Format, mcc, parse}; + +/// A frame carries 18 DTVCC pairs, and with one-frame captions back to back it holds one +/// caption's display (3 pairs) and the next one's load. A load of up to 19 characters (15 pairs) +/// fits beside it. A SetPenLocation in every load made it 16 pairs from 17 characters on, and +/// the display a frame late. +#[test] +fn back_to_back_one_frame_708_captions_keep_their_frames() { + for len in [1, 16, 17, 18, 19] { + let cues: Vec = (0..4i64) + .map(|k| Cue { + start: mcc::RATE.tick_of(60 + k), + end: mcc::RATE.tick_of(61 + k), + // a different text each time, so the reader sees every change + text: ('a'..='z').cycle().skip(k as usize).take(len).collect(), + ..Default::default() + }) + .collect(); + let doc = Document { cues, ..Default::default() }; + let back = parse(mcc::write_with(&doc, true, false, true, "test").as_bytes(), Format::Mcc).unwrap(); + let frames: Vec<(i64, i64)> = back.cues.iter().map(|c| (mcc::RATE.frame_at(c.start), mcc::RATE.frame_at(c.end))).collect(); + assert_eq!(frames, [(60, 61), (61, 62), (62, 63), (63, 64)], "{len} characters"); + assert_eq!(back.cues, doc.cues, "{len} characters"); + } +} diff --git a/crates/captions/tests/it/roundtrip.rs b/crates/captions/tests/it/roundtrip.rs index cbb7bdcd7..effb81e9a 100644 --- a/crates/captions/tests/it/roundtrip.rs +++ b/crates/captions/tests/it/roundtrip.rs @@ -260,3 +260,50 @@ fn srt_to_vtt_to_scc_keeps_text_and_frames() { assert_eq!(scc::frame_of(doc3.cues[0].start), 60); assert_eq!(scc::frame_of(doc3.cues[1].end), 210); } + +#[test] +fn tight_pop_on_load_can_cross_the_previous_caption_end() { + let text = "A second caption that needs more time to load"; + let doc = Document { + cues: vec![ + Cue { start: scc::RATE.tick_of(30), end: scc::RATE.tick_of(60), text: "First".into(), ..Default::default() }, + Cue { start: scc::RATE.tick_of(67), end: scc::RATE.tick_of(150), text: text.into(), ..Default::default() }, + ], + ..Default::default() + }; + for format in [Format::Scc, Format::Mcc] { + let back = parse(&write(&doc, format, WriteOptions::default()), format).unwrap(); + assert_eq!(back.cues.len(), 2); + assert_eq!(scc::frame_of(back.cues[0].end), 60); + assert_eq!(scc::frame_of(back.cues[1].start), 67, "{format:?}"); + assert_eq!(scc::frame_of(back.cues[1].end), 150); + } +} + +#[test] +fn interrupted_load_keeps_extended_characters_and_repeated_basic_pairs() { + for text in + ["A second caption that needs more time to load", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "ÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ\nÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀÀ"] + { + let doc = Document { + cues: vec![ + Cue { start: scc::RATE.tick_of(30), end: scc::RATE.tick_of(60), text: "First".into(), ..Default::default() }, + Cue { start: scc::RATE.tick_of(67), end: scc::RATE.tick_of(180), text: text.into(), ..Default::default() }, + ], + ..Default::default() + }; + for format in [Format::Scc, Format::Mcc] { + let back = parse(&write(&doc, format, WriteOptions::default()), format).unwrap(); + assert_eq!(back.cues.len(), 2); + assert_eq!(back.cues[1].text.replace(['\n', ' '], ""), text.replace(['\n', ' '], ""), "{format:?}"); + assert_eq!(scc::frame_of(back.cues[0].end), 60); + assert_eq!(scc::frame_of(back.cues[1].end), 180); + } + let pairs = scc::schedule(&doc); + for (&frame, &pair) in &pairs { + if (0x10..=0x1f).contains(&pair.0) { + assert!(pairs.get(&(frame - 1)) == Some(&pair) || pairs.get(&(frame + 1)) == Some(&pair), "split doubled code at {frame}"); + } + } + } +} diff --git a/crates/captions/tests/it/ttml_control_chars.rs b/crates/captions/tests/it/ttml_control_chars.rs new file mode 100644 index 000000000..bb2eb8437 --- /dev/null +++ b/crates/captions/tests/it/ttml_control_chars.rs @@ -0,0 +1,19 @@ +use craft_captions::{Cue, Document, Format, WriteOptions, parse, write}; +use craft_captions::{TICKS_PER_SECOND, Tick}; + +/// Control characters that XML 1.0 forbids must not make the export unreadable. +#[test] +fn ttml_export_drops_xml_illegal_control_chars() { + // every forbidden character, each between two letters that must survive + let forbidden = (0u32..0x20).filter(|c| ![0x9, 0xA, 0xD].contains(c)).chain([0xFFFE, 0xFFFF]).filter_map(char::from_u32); + let text: String = forbidden.flat_map(|c| ['a', c]).chain(['e']).collect(); + let want = format!("{}e", "a".repeat(31)); + for format in [Format::Ttml, Format::Dfxp] { + let cue = Cue { start: Tick(TICKS_PER_SECOND), end: Tick(2 * TICKS_PER_SECOND), text: text.clone(), ..Default::default() }; + let doc = Document { cues: vec![cue], ..Default::default() }; + let xml = String::from_utf8(write(&doc, format, WriteOptions::default())).unwrap(); + let back = parse(xml.as_bytes(), format); + assert!(back.is_ok(), "{format:?}: {xml}"); + assert_eq!(back.unwrap().cues[0].text, want, "{format:?}"); + } +} diff --git a/crates/captions/tests/it/ttml_cue_ids.rs b/crates/captions/tests/it/ttml_cue_ids.rs new file mode 100644 index 000000000..ea2238955 --- /dev/null +++ b/crates/captions/tests/it/ttml_cue_ids.rs @@ -0,0 +1,362 @@ +use std::collections::BTreeSet; + +use craft_captions::{Cue, Document, Format, WriteOptions, parse, write}; +use craft_captions::{FrameRate, TICKS_PER_SECOND, Tick}; + +const XML: &str = "http://www.w3.org/XML/1998/namespace"; +const TT: &str = "http://www.w3.org/ns/ttml"; + +fn timeline(doc: &Document) -> Vec<(Tick, Tick, &str)> { + doc.cues.iter().map(|cue| (cue.start, cue.end, cue.text.as_str())).collect() +} + +fn distinct_vtt_ids_do_not_collide_with_document_ids(format: Format) { + // Every WebVTT identifier is distinct. Spaces are legal in WebVTT identifiers. + // The fourth identifier requires an XML-name fallback, which must not reuse c4. + let vtt = br#"WEBVTT + +s1 +00:00:01.000 --> 00:00:02.000 +Opening + +r1 +00:00:03.000 --> 00:00:04.000 +Reply + +c4 +00:00:05.000 --> 00:00:06.000 +Explicit identifier + +display label +00:00:07.000 --> 00:00:08.000 +Display identifier + +keep +00:00:09.000 --> 00:00:10.000 +Closing +"#; + let doc = parse(vtt, Format::WebVtt).unwrap(); + assert!(doc.warnings.is_empty(), "{:?}", doc.warnings); + let input_ids: Vec<_> = doc.cues.iter().map(|cue| cue.id.as_deref().unwrap()).collect(); + assert_eq!(input_ids, ["s1", "r1", "c4", "display label", "keep"]); + assert_eq!(input_ids.iter().collect::>().len(), input_ids.len(), "the source identifiers are unique"); + let expected = [ + (Tick(TICKS_PER_SECOND), Tick(2 * TICKS_PER_SECOND), "Opening"), + (Tick(3 * TICKS_PER_SECOND), Tick(4 * TICKS_PER_SECOND), "Reply"), + (Tick(5 * TICKS_PER_SECOND), Tick(6 * TICKS_PER_SECOND), "Explicit identifier"), + (Tick(7 * TICKS_PER_SECOND), Tick(8 * TICKS_PER_SECOND), "Display identifier"), + (Tick(9 * TICKS_PER_SECOND), Tick(10 * TICKS_PER_SECOND), "Closing"), + ]; + assert_eq!(timeline(&doc), expected); + + // Check all document IDs independently of the production allocator. + let options = WriteOptions { rate: Some(FrameRate::FPS_25), ..Default::default() }; + let bytes = write(&doc, format, options); + assert_eq!(bytes, write(&doc, format, options), "stable export for {format:?}"); + let text = std::str::from_utf8(&bytes).unwrap(); + let xml = roxmltree::Document::parse(text).unwrap(); + let paragraphs: Vec<_> = xml.descendants().filter(|node| node.has_tag_name((TT, "p"))).collect(); + assert_eq!(paragraphs.len(), expected.len()); + assert!(paragraphs.iter().all(|node| node.attribute((XML, "id")).is_some_and(|id| !id.is_empty()))); + for paragraph in ¶graphs { + assert_ncname(paragraph.attribute((XML, "id")).unwrap()); + } + assert_eq!(paragraphs.last().unwrap().attribute((XML, "id")), Some("keep"), "an unrelated valid ID stays intact"); + assert_eq!(paragraphs[2].attribute((XML, "id")), Some("c4"), "the existing explicit collision ID stays intact"); + assert_ne!( + paragraphs[3].attribute((XML, "id")), + Some("display label"), + "{format:?}: do not emit the space-containing display label verbatim as xml:id" + ); + let mut seen = BTreeSet::new(); + let repeated: Vec<_> = + xml.descendants().filter_map(|node| node.attribute((XML, "id"))).filter(|id| !seen.insert(*id)).map(str::to_owned).collect(); + let back = parse(&bytes, format).unwrap(); + assert_eq!(timeline(&back), expected, "{format:?}: renaming IDs must preserve every cue and its literal timing"); + assert!(repeated.is_empty(), "{format:?}: all document xml:id values must be unique: {repeated:?}"); +} + +// Validate the public writer's observed ID independently of its allocator and +// without requiring ASCII: an NCName is an XML name with no colon. +fn assert_ncname(id: &str) { + assert!(!id.contains(':'), "an xml:id must not contain a colon: {id:?}"); + let witness = format!("<{id}/>"); + let xml = roxmltree::Document::parse(&witness).expect("the generated ID must be a valid XML name"); + assert_eq!(xml.root_element().tag_name().name(), id, "the entire generated ID must be the XML name"); +} + +fn valid_ascii_and_unicode_cue_ids_survive_export(format: Format) { + // U+00E9 followed by ASCII pisode_1 is a valid Unicode NCName. + let ids = ["c4", "épisode_1"]; + for id in ids { + assert_ncname(id); + } + let doc = Document { + cues: vec![ + Cue { + start: Tick(TICKS_PER_SECOND), + end: Tick(2 * TICKS_PER_SECOND), + text: "ASCII identifier".into(), + id: Some(ids[0].into()), + ..Default::default() + }, + Cue { + start: Tick(3 * TICKS_PER_SECOND), + end: Tick(4 * TICKS_PER_SECOND), + text: "Unicode identifier".into(), + id: Some(ids[1].into()), + ..Default::default() + }, + ], + ..Default::default() + }; + let expected = [ + (Tick(TICKS_PER_SECOND), Tick(2 * TICKS_PER_SECOND), "ASCII identifier"), + (Tick(3 * TICKS_PER_SECOND), Tick(4 * TICKS_PER_SECOND), "Unicode identifier"), + ]; + assert_eq!(timeline(&doc), expected); + let expected_ids: Vec<_> = ids.into_iter().map(|id| Some(id.to_string())).collect(); + let options = WriteOptions { rate: Some(FrameRate::FPS_25), ..Default::default() }; + let bytes = write(&doc, format, options); + assert_eq!(bytes, write(&doc, format, options), "stable export for {format:?}"); + let xml = roxmltree::Document::parse(std::str::from_utf8(&bytes).unwrap()).unwrap(); + let exported_ids: Vec<_> = + xml.descendants().filter(|node| node.has_tag_name((TT, "p"))).map(|node| node.attribute((XML, "id")).map(str::to_string)).collect(); + let mut seen = BTreeSet::new(); + let duplicates: Vec<_> = + xml.descendants().filter_map(|node| node.attribute((XML, "id"))).filter(|id| !seen.insert(*id)).map(str::to_string).collect(); + let back = parse(&bytes, format).unwrap(); + assert_eq!(timeline(&back), expected, "{format:?}: valid IDs must preserve literal cue timing and text"); + let imported_ids: Vec<_> = back.cues.iter().map(|cue| cue.id.clone()).collect(); + assert_eq!(exported_ids, expected_ids, "{format:?}: valid ASCII and Unicode IDs must survive verbatim in XML"); + assert_eq!(imported_ids, expected_ids, "{format:?}: valid ASCII and Unicode IDs must survive owned-reader import"); + assert!(duplicates.is_empty(), "{format:?}: all document xml:id values must be unique: {duplicates:?}"); +} + +fn a_generated_id_does_not_take_a_later_explicit_cue_id(format: Format) { + let earlier = Cue { start: Tick(TICKS_PER_SECOND), end: Tick(2 * TICKS_PER_SECOND), text: "Earlier unlabeled cue".into(), ..Default::default() }; + assert!(earlier.id.is_none()); + let probe = Document { cues: vec![earlier.clone()], ..Default::default() }; + let expected = [ + (Tick(TICKS_PER_SECOND), Tick(2 * TICKS_PER_SECOND), "Earlier unlabeled cue"), + (Tick(3 * TICKS_PER_SECOND), Tick(4 * TICKS_PER_SECOND), "Later explicit cue"), + ]; + assert_eq!(timeline(&probe), expected[..1]); + let options = WriteOptions { rate: Some(FrameRate::FPS_25), ..Default::default() }; + // The first ordinary export selects a collision input, not the expected + // replacement spelling. An unsuitable generated ID is a failure, not a skip. + let probe_bytes = write(&probe, format, options); + let probe_xml = roxmltree::Document::parse(std::str::from_utf8(&probe_bytes).unwrap()).unwrap(); + let probe_paragraphs: Vec<_> = probe_xml.descendants().filter(|node| node.has_tag_name((TT, "p"))).collect(); + assert_eq!(probe_paragraphs.len(), 1); + let observed = probe_paragraphs[0].attribute((XML, "id")).expect("an unlabeled cue must receive an ID").to_string(); + assert_ncname(&observed); + let probe_back = parse(&probe_bytes, format).unwrap(); + assert_eq!(timeline(&probe_back), expected[..1], "{format:?}: the ordinary probe must preserve literal timing and text"); + + let doc = Document { + cues: vec![ + earlier.clone(), + Cue { + start: Tick(3 * TICKS_PER_SECOND), + end: Tick(4 * TICKS_PER_SECOND), + text: "Later explicit cue".into(), + id: Some(observed.clone()), + ..Default::default() + }, + ], + ..Default::default() + }; + assert_eq!(timeline(&doc), expected); + assert!(doc.cues[0].id.is_none()); + assert_eq!(doc.cues[1].id.as_deref(), Some(observed.as_str())); + let bytes = write(&doc, format, options); + assert_eq!(bytes, write(&doc, format, options), "stable export for {format:?}"); + let xml = roxmltree::Document::parse(std::str::from_utf8(&bytes).unwrap()).unwrap(); + let paragraphs: Vec<_> = xml.descendants().filter(|node| node.has_tag_name((TT, "p"))).collect(); + assert_eq!(paragraphs.len(), expected.len()); + let exported_ids: Vec<_> = paragraphs.iter().map(|node| node.attribute((XML, "id")).expect("each cue must have an ID").to_string()).collect(); + for id in &exported_ids { + assert_ncname(id); + } + let mut seen = BTreeSet::new(); + let duplicates: Vec<_> = + xml.descendants().filter_map(|node| node.attribute((XML, "id"))).filter(|id| !seen.insert(*id)).map(str::to_string).collect(); + let back = parse(&bytes, format).unwrap(); + assert_eq!(timeline(&back), expected, "{format:?}: reserving the later ID must preserve every cue and its literal timing"); + assert_eq!(exported_ids[1], observed, "{format:?}: the later explicit ID must survive verbatim"); + assert_ne!(exported_ids[0], observed, "{format:?}: the earlier generated ID must not take the later explicit ID"); + assert_eq!(back.cues[1].id.as_deref(), Some(observed.as_str()), "{format:?}: the later explicit ID must survive owned-reader import"); + assert!(duplicates.is_empty(), "{format:?}: all document xml:id values must be unique: {duplicates:?}"); +} + +#[test] +fn ttml_distinct_vtt_ids_do_not_collide_with_document_ids() { + distinct_vtt_ids_do_not_collide_with_document_ids(Format::Ttml); +} + +#[test] +fn dfxp_distinct_vtt_ids_do_not_collide_with_document_ids() { + distinct_vtt_ids_do_not_collide_with_document_ids(Format::Dfxp); +} + +#[test] +fn ttml_valid_ascii_and_unicode_cue_ids_survive_export() { + valid_ascii_and_unicode_cue_ids_survive_export(Format::Ttml); +} + +#[test] +fn dfxp_valid_ascii_and_unicode_cue_ids_survive_export() { + valid_ascii_and_unicode_cue_ids_survive_export(Format::Dfxp); +} + +#[test] +fn ttml_generated_id_reserves_later_explicit_cue_id() { + a_generated_id_does_not_take_a_later_explicit_cue_id(Format::Ttml); +} + +#[test] +fn dfxp_generated_id_reserves_later_explicit_cue_id() { + a_generated_id_does_not_take_a_later_explicit_cue_id(Format::Dfxp); +} + +fn duplicate_parser_accepted_cue_ids_keep_first_id(format: Format) { + // FilmCraft tolerates duplicate input IDs; WebVTT itself requires unique identifiers. + // Keep the first usable ID and give the later cue a distinct ID when writing XML. + let vtt = br#"WEBVTT + +shared +00:00:01.000 --> 00:00:02.000 +First duplicate identifier + +shared +00:00:03.000 --> 00:00:04.000 +Second duplicate identifier +"#; + assert_ncname("shared"); + let doc = parse(vtt, Format::WebVtt).unwrap(); + let imported_ids: Vec<_> = doc.cues.iter().map(|cue| cue.id.as_deref()).collect(); + assert_eq!(imported_ids, [Some("shared"), Some("shared")], "both input IDs must reach the writer unchanged"); + let expected = [ + (Tick(TICKS_PER_SECOND), Tick(2 * TICKS_PER_SECOND), "First duplicate identifier"), + (Tick(3 * TICKS_PER_SECOND), Tick(4 * TICKS_PER_SECOND), "Second duplicate identifier"), + ]; + assert_eq!(timeline(&doc), expected); + + let options = WriteOptions { rate: Some(FrameRate::FPS_25), ..Default::default() }; + let bytes = write(&doc, format, options); + assert_eq!(bytes, write(&doc, format, options), "stable export for {format:?}"); + let xml = roxmltree::Document::parse(std::str::from_utf8(&bytes).unwrap()).unwrap(); + let paragraphs: Vec<_> = xml.descendants().filter(|node| node.has_tag_name((TT, "p"))).collect(); + assert_eq!(paragraphs.len(), expected.len()); + let exported_ids: Vec<_> = paragraphs.iter().map(|node| node.attribute((XML, "id")).expect("each cue must have an ID")).collect(); + for id in &exported_ids { + assert_ncname(id); + } + assert_eq!(exported_ids[0], "shared", "{format:?}: preserve the first usable explicit ID verbatim"); + assert_ne!(exported_ids[1], "shared", "{format:?}: the second explicit ID must become distinct"); + let mut seen = BTreeSet::new(); + let duplicates: Vec<_> = + xml.descendants().filter_map(|node| node.attribute((XML, "id"))).filter(|id| !seen.insert(*id)).map(str::to_owned).collect(); + let back = parse(&bytes, format).unwrap(); + assert_eq!(timeline(&back), expected, "{format:?}: duplicate-ID tolerance must preserve literal cue timing and text"); + assert_eq!(back.cues[0].id.as_deref(), Some("shared"), "{format:?}: the first explicit ID must survive owned-reader import"); + assert!(duplicates.is_empty(), "{format:?}: all document xml:id values must be unique: {duplicates:?}"); +} + +#[test] +fn ttml_duplicate_parser_accepted_cue_ids_keep_first_id() { + duplicate_parser_accepted_cue_ids_keep_first_id(Format::Ttml); +} + +#[test] +fn dfxp_duplicate_parser_accepted_cue_ids_keep_first_id() { + duplicate_parser_accepted_cue_ids_keep_first_id(Format::Dfxp); +} + +fn decomposed_and_composed_cue_ids_stay_distinct(format: Format) { + // XML 1.0 Fifth Edition NameChar permits U+0301 after e. Do not normalize either ID. + let ids = ["e\u{301}_1", "é_1"]; + assert_ne!(ids[0], ids[1]); + for id in ids { + assert_ncname(id); + } + let vtt = format!( + "WEBVTT\n\n{}\n00:00:01.000 --> 00:00:02.000\nDecomposed identifier\n\n{}\n00:00:03.000 --> 00:00:04.000\nComposed identifier\n", + ids[0], ids[1] + ); + let doc = parse(vtt.as_bytes(), Format::WebVtt).unwrap(); + assert!(doc.warnings.is_empty(), "{:?}", doc.warnings); + let input_ids: Vec<_> = doc.cues.iter().map(|cue| cue.id.as_deref()).collect(); + assert_eq!(input_ids, [Some(ids[0]), Some(ids[1])], "the parser must preserve both exact input IDs"); + let expected = [ + (Tick(TICKS_PER_SECOND), Tick(2 * TICKS_PER_SECOND), "Decomposed identifier"), + (Tick(3 * TICKS_PER_SECOND), Tick(4 * TICKS_PER_SECOND), "Composed identifier"), + ]; + assert_eq!(timeline(&doc), expected); + let options = WriteOptions { rate: Some(FrameRate::FPS_25), ..Default::default() }; + let bytes = write(&doc, format, options); + assert_eq!(bytes, write(&doc, format, options), "stable export for {format:?}"); + let xml = roxmltree::Document::parse(std::str::from_utf8(&bytes).unwrap()).unwrap(); + let exported_ids: Vec<_> = xml.descendants().filter(|node| node.has_tag_name((TT, "p"))).map(|node| node.attribute((XML, "id"))).collect(); + assert_eq!(exported_ids, input_ids, "{format:?}: preserve the decomposed and composed IDs verbatim without normalization"); + let mut seen = BTreeSet::new(); + assert!(xml.descendants().filter_map(|node| node.attribute((XML, "id"))).all(|id| seen.insert(id)), "all document xml:id values must be unique"); + let back = parse(&bytes, format).unwrap(); + assert!(back.warnings.is_empty(), "{:?}", back.warnings); + assert_eq!(timeline(&back), expected, "{format:?}: preserve literal cue timing and text"); + let imported_ids: Vec<_> = back.cues.iter().map(|cue| cue.id.as_deref()).collect(); + assert_eq!(imported_ids, input_ids, "{format:?}: both exact IDs must survive owned-reader import"); +} + +fn non_xml_numeric_cue_id_uses_fallback(format: Format) { + // Leading combining marks and colons are unchanged invalid controls. + // U+00B2 is numeric in Rust, but is not XML 1.0 Fifth Edition NameChar. + for id in ["\u{301}_1", "a:b", "a\u{b2}"] { + let vtt = format!("WEBVTT\n\n{id}\n00:00:01.000 --> 00:00:02.000\nIdentifier fallback\n"); + let doc = parse(vtt.as_bytes(), Format::WebVtt).unwrap(); + assert!(doc.warnings.is_empty(), "{:?}", doc.warnings); + assert_eq!(doc.cues.len(), 1); + assert_eq!(doc.cues[0].id.as_deref(), Some(id)); + let expected = [(Tick(TICKS_PER_SECOND), Tick(2 * TICKS_PER_SECOND), "Identifier fallback")]; + assert_eq!(timeline(&doc), expected); + let options = WriteOptions { rate: Some(FrameRate::FPS_25), ..Default::default() }; + let bytes = write(&doc, format, options); + assert_eq!(bytes, write(&doc, format, options), "stable export for {format:?}: {id:?}"); + let xml = roxmltree::Document::parse(std::str::from_utf8(&bytes).unwrap()).unwrap(); + let paragraphs: Vec<_> = xml.descendants().filter(|node| node.has_tag_name((TT, "p"))).collect(); + assert_eq!(paragraphs.len(), 1); + let output_id = paragraphs[0].attribute((XML, "id")).expect("the cue must have an ID"); + assert_ne!(output_id, id, "{format:?}: an invalid XML NCName must receive a fallback: {id:?}"); + assert_ncname(output_id); + let mut seen = BTreeSet::new(); + assert!( + xml.descendants().filter_map(|node| node.attribute((XML, "id"))).all(|id| seen.insert(id)), + "all document xml:id values must be unique" + ); + let back = parse(&bytes, format).unwrap(); + assert!(back.warnings.is_empty(), "{:?}", back.warnings); + assert_eq!(timeline(&back), expected, "{format:?}: fallback must preserve literal cue timing and text"); + assert_eq!(back.cues[0].id.as_deref(), Some(output_id)); + } +} + +#[test] +fn ttml_decomposed_and_composed_cue_ids_stay_distinct() { + decomposed_and_composed_cue_ids_stay_distinct(Format::Ttml); +} + +#[test] +fn dfxp_decomposed_and_composed_cue_ids_stay_distinct() { + decomposed_and_composed_cue_ids_stay_distinct(Format::Dfxp); +} + +#[test] +fn ttml_non_xml_numeric_cue_id_uses_fallback() { + non_xml_numeric_cue_id_uses_fallback(Format::Ttml); +} + +#[test] +fn dfxp_non_xml_numeric_cue_id_uses_fallback() { + non_xml_numeric_cue_id_uses_fallback(Format::Dfxp); +} diff --git a/crates/captions/tests/it/ttml_literal_angle.rs b/crates/captions/tests/it/ttml_literal_angle.rs new file mode 100644 index 000000000..0e4843901 --- /dev/null +++ b/crates/captions/tests/it/ttml_literal_angle.rs @@ -0,0 +1,76 @@ +//! TTML / DFXP export keeps literal `<` and `>` text that is not markup. + +use craft_captions::FrameRate; +use craft_captions::{Cue, Document, Format, WriteOptions, write}; + +fn export(text: &str, format: Format) -> String { + let rate = FrameRate::FPS_25; + let doc = Document { + cues: vec![Cue { start: rate.tick_of(25), end: rate.tick_of(75), text: text.into(), ..Default::default() }], + ..Default::default() + }; + String::from_utf8(write(&doc, format, WriteOptions::default())).unwrap() +} + +#[test] +fn literal_angle_brackets_keep_middle_text() { + for format in [Format::Ttml, Format::Dfxp] { + for (input, expected) in [("1 < 2 and 3 > 2", "1 < 2 and 3 > 2"), ("ad", "a<b and c>d")] { + let text = export(input, format); + assert!(text.contains(expected), "{format:?}: {text}"); + } + } +} + +#[test] +fn real_tags_still_produce_markup() { + for format in [Format::Ttml, Format::Dfxp] { + let text = export("text x z it", format); + assert!(text.contains("text"), "{format:?}: {text}"); + assert!(text.contains("it"), "{format:?}: {text}"); + assert!(text.contains(" x z "), "{format:?}: {text}"); + } +} + +#[test] +fn self_closing_tags_are_still_stripped() { + for format in [Format::Ttml, Format::Dfxp] { + let text = export("a
b c", format); + assert!(text.contains(">ab c

"), "{format:?}: {text}"); + } +} + +#[test] +fn quoted_gt_in_attribute_does_not_end_tag() { + for format in [Format::Ttml, Format::Dfxp] { + let text = export("\">text", format); + assert!(text.contains("text"), "{format:?}: {text}"); + } +} + +#[test] +fn single_quoted_attributes_are_stripped() { + for format in [Format::Ttml, Format::Dfxp] { + let text = export("word", format); + assert!(text.contains("word"), "{format:?}: {text}"); + assert!(!text.contains("caption"), "{format:?}: {text}"); + } +} + +#[test] +fn whitespace_after_lt_is_literal_text() { + for format in [Format::Ttml, Format::Dfxp] { + let text = export("1 < b > 2", format); + assert!(text.contains("1 < b > 2"), "{format:?}: {text}"); + assert!(!text.contains("fontWeight"), "{format:?}: {text}"); + } +} + +#[test] +fn many_unmatched_lt_is_fast() { + let input = format!("{}>", "<".repeat(256_000)); + let start = std::time::Instant::now(); + let text = export(&input, Format::Ttml); + assert!(start.elapsed() < std::time::Duration::from_secs(2), "took {:?}", start.elapsed()); + assert!(text.contains(&format!("{}>", "<".repeat(256_000)))); +} diff --git a/crates/captions/tests/it/ttml_xml_views.rs b/crates/captions/tests/it/ttml_xml_views.rs new file mode 100644 index 000000000..8173039e7 --- /dev/null +++ b/crates/captions/tests/it/ttml_xml_views.rs @@ -0,0 +1,119 @@ +//! The TTML reader used to sit on `roxmltree` and now sits on `craft-xml`. Its logic is the same; +//! what changed is the tree it is handed. This compares the two trees on TTML documents: element +//! names by namespace, attributes by namespace, and character data, in document order. + +use craft_captions::{Cue, Document, Format, FrameRate, WriteOptions, write}; + +const NAMESPACES: [(&str, &str); 6] = [ + ("tt", "http://www.w3.org/ns/ttml"), + ("tt10", "http://www.w3.org/2006/10/ttaf1"), + ("tt04", "http://www.w3.org/2006/04/ttaf1"), + ("ttm", "http://www.w3.org/ns/ttml#metadata"), + ("ttm10", "http://www.w3.org/2006/10/ttaf1#metadata"), + ("ttm04", "http://www.w3.org/2006/04/ttaf1#metadata"), +]; + +/// A name the way the reader's canonical names spell it. +fn name(namespace: Option<&str>, local: &str) -> String { + match namespace { + None => local.to_string(), + Some("http://www.w3.org/XML/1998/namespace") => format!("xml:{local}"), + Some(uri) => format!("{}:{local}", NAMESPACES.iter().find(|(_, u)| *u == uri).map_or("?", |(p, _)| p)), + } +} + +fn dump_rox(n: roxmltree::Node, out: &mut Vec) { + let mut attrs: Vec = n.attributes().map(|a| format!("@{}={}", name(a.namespace(), a.name()), a.value())).collect(); + attrs.sort(); + out.push(format!("<{} {}", name(n.tag_name().namespace(), n.tag_name().name()), attrs.join(" "))); + let mut text = String::new(); + for c in n.children() { + if c.is_text() { + text.push_str(c.text().unwrap_or("")); + } else if c.is_element() { + out.push(format!("#{}", std::mem::take(&mut text))); + dump_rox(c, out); + } + } + out.push(format!("#{text}")); + out.push(">".into()); +} + +fn dump_craft(n: &craft_xml::El, out: &mut Vec) { + let mut attrs: Vec = n.attrs.iter().map(|(k, v)| format!("@{k}={v}")).collect(); + attrs.sort(); + out.push(format!("<{} {}", n.name, attrs.join(" "))); + let mut text = String::new(); + for c in &n.children { + match c { + craft_xml::Node::Text(t) | craft_xml::Node::CData(t) => text.push_str(t), + craft_xml::Node::El(e) => { + out.push(format!("#{}", std::mem::take(&mut text))); + dump_craft(e, out); + } + _ => {} + } + } + out.push(format!("#{text}")); + out.push(">".into()); +} + +fn documents() -> Vec { + let mut docs: Vec = [ + r#"

Hi

"#, + // prefixed content, styling and metadata; xml:lang, xml:id, xml:space + r#" + + Ann Lee + + onetwo three four +"#, + // the 2006 drafts + r#" +Bo

x

"#, + r#"Bo +

x

"#, + // no namespace at all, an unknown namespace, and a foreign element inside + r#"

x

"#, + r#"

x

"#, + // character data: references, CDATA, comments, processing instructions, mixed whitespace + "

\n a <b> & © 😀 & ]]>tail\u{a0}more end\r\n

", + // a namespace redeclared in an inner scope + r#"no"#, + ] + .into_iter() + .map(str::to_string) + .collect(); + // and what the writer produces, in both flavours + let rate = FrameRate::FPS_25; + let cue = |f: i64, text: &str, speaker: Option<&str>| Cue { + start: rate.tick_of(f), + end: rate.tick_of(f + 25), + text: text.into(), + speaker: speaker.map(str::to_string), + ..Default::default() + }; + let doc = Document { + cues: vec![cue(25, "Olá & tchau\n1 < 2", Some("Ana \"A\" ")), cue(75, "second\u{a0}line", None)], + language: Some("pt".into()), + ..Default::default() + }; + for format in [Format::Ttml, Format::Dfxp] { + docs.push(String::from_utf8(write(&doc, format, WriteOptions { rate: Some(rate), ..Default::default() })).unwrap()); + } + docs +} + +#[test] +fn craft_xml_hands_the_ttml_reader_the_tree_roxmltree_did() { + let canonical = craft_xml::Canonical::new(&NAMESPACES); + for text in documents() { + let rox = roxmltree::Document::parse(&text).unwrap(); + let craft = craft_xml::Options::canonical(&canonical).strict().parse(text.as_bytes()).unwrap(); + let (mut a, mut b) = (Vec::new(), Vec::new()); + dump_rox(rox.root_element(), &mut a); + dump_craft(&craft, &mut b); + assert_eq!(a, b, "{text}"); + } +} From 2e0bbc3a9cfd0724a27d6dbc7c1c459f92ee0893 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:44:44 -0500 Subject: [PATCH 018/120] craft-y4m: YUV4MPEG2 reader from FilmCraft (#933) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * craft-y4m: YUV4MPEG2 reader FilmCraft's Y4M reader as a crate: the stream header, colour space layouts, a frame index built from the FRAME lines through a read callback, and plane ranges. The media source and frame types stay in the app. * craft-y4m: a header parameter starting with a multi-byte character is ignored Port of storytold/filmcraft#4562: the tag of a stream header parameter is its first character, not its first byte, so a parameter such as `éX` is skipped like any other unknown tag instead of being split inside the character. --------- Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> --- Cargo.lock | 7 + Cargo.toml | 1 + README.md | 3 + crates/y4m/Cargo.toml | 14 ++ crates/y4m/src/lib.rs | 310 ++++++++++++++++++++++++++++++++++++++++ crates/y4m/src/tests.rs | 220 ++++++++++++++++++++++++++++ 6 files changed, 555 insertions(+) create mode 100644 crates/y4m/Cargo.toml create mode 100644 crates/y4m/src/lib.rs create mode 100644 crates/y4m/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 1b7bfd666..8121690c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1703,6 +1703,13 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-y4m" +version = "0.1.0" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "crc32fast" version = "1.5.2" diff --git a/Cargo.toml b/Cargo.toml index 301515bea..dbbaa40e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,6 +95,7 @@ craft-vectorcraft-format = { path = "crates/vectorcraft-format" } craft-vectordrawable = { path = "crates/vectordrawable" } craft-xls = { path = "crates/xls" } craft-xml = { path = "crates/xml" } +craft-y4m = { path = "crates/y4m" } craft-epub = { path = "crates/epub" } craft-video = { path = "crates/video" } craft-web-api = { path = "crates/web-api" } diff --git a/README.md b/README.md index 0cf34dace..37f11b033 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,9 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " = std::result::Result; + +/// Caps on what a file may ask for. `#[non_exhaustive]`: start from [`Limits::default`] and set +/// the fields you change. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] +pub struct Limits { + /// Most luma samples (width × height) a frame may have. Default 2^28. + pub max_pixels: u64, + /// Longest stream header or `FRAME` line read, in bytes. Default 64 KiB. + pub max_line: usize, + /// Most frames indexed; a longer file ends there. Each takes 8 bytes of index. Default 2^24. + pub max_frames: usize, +} + +impl Default for Limits { + fn default() -> Self { + Self { max_pixels: 1 << 28, max_line: 64 << 10, max_frames: 1 << 24 } + } +} + +/// Whether `head` starts like a YUV4MPEG2 file. +pub fn sniff(head: &[u8]) -> bool { + head.starts_with(SIGNATURE) && matches!(head.get(SIGNATURE.len()), Some(b' ' | b'\n')) +} + +/// Chroma subsampling. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Chroma { + /// Half width, half height. + C420, + /// Half width. + C422, + /// Full size. + C444, +} + +impl Chroma { + /// The horizontal and vertical subsampling as shifts. + pub fn shifts(self) -> (u32, u32) { + match self { + Chroma::C420 => (1, 1), + Chroma::C422 => (1, 0), + Chroma::C444 => (0, 0), + } + } +} + +/// The sample layout a `C` parameter names. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Layout { + /// `None` for luma only (`mono`). + pub chroma: Option, + /// Bits per sample, 8 to 16. Above 8, samples are 16-bit little-endian. + pub bits: u32, + /// Whether an alpha plane follows the chroma planes (`444alpha`). + pub alpha: bool, +} + +impl Layout { + /// The layout of a colour space name (`420jpeg`, `422p10`, `444alpha`, `mono16`, …), `None` + /// for one this reader does not cover. + pub fn parse(c: &str) -> Option { + let (base, bits) = match c.split_once('p') { + Some((base, bits)) if !bits.is_empty() && bits.bytes().all(|b| b.is_ascii_digit()) => (base, bits.parse().ok()?), + _ => (c, 8), + }; + let (base, bits) = match base.strip_prefix("mono") { + Some("") => ("mono", bits), + Some(digits) if bits == 8 => ("mono", digits.parse().ok()?), + _ => (base, bits), + }; + if !(8..=16).contains(&bits) { + return None; + } + let (chroma, alpha) = match base { + "420" | "420jpeg" | "420paldv" | "420mpeg2" => (Some(Chroma::C420), false), + "422" => (Some(Chroma::C422), false), + "444" => (Some(Chroma::C444), false), + "444alpha" if bits == 8 => (Some(Chroma::C444), true), + "mono" => (None, false), + _ => return None, + }; + Some(Layout { chroma, bits, alpha }) + } + + /// Bytes of one stored sample: 1 at 8 bits, 2 above. + pub fn bytes_per_sample(self) -> usize { + if self.bits > 8 { 2 } else { 1 } + } +} + +/// The planes of one frame, as byte ranges within the frame's data. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Planes { + pub y: Range, + /// Empty for `mono`. + pub cb: Range, + /// Empty for `mono`. + pub cr: Range, + /// Empty without an alpha plane. + pub alpha: Range, +} + +/// The stream header. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Header { + pub width: u32, + pub height: u32, + /// Frame rate as numerator and denominator, both positive; `None` when the file names none. + pub frame_rate: Option<(i64, i64)>, + /// Pixel aspect ratio, both parts positive; `None` when absent or unknown (`0:0`). + pub pixel_aspect: Option<(u32, u32)>, + pub layout: Layout, + /// Whether the file says `XCOLORRANGE=FULL`. + pub full_range: bool, +} + +fn parse_ratio(s: &str) -> Option<(i64, i64)> { + let (n, d) = s.split_once(':')?; + Some((n.parse().ok()?, d.parse().ok()?)) +} + +impl Header { + /// Parses the stream header line (without its newline). + pub fn parse(line: &[u8], limits: &Limits) -> Result
{ + let header = String::from_utf8_lossy(line); + let mut params = header.split(' '); + if params.next() != Some("YUV4MPEG2") { + return Err(Error::Malformed("not a YUV4MPEG2 file")); + } + let (mut width, mut height, mut frame_rate, mut pixel_aspect, mut colour, mut full_range) = (0u32, 0u32, None, None, "420jpeg", false); + for p in params { + // The tag is the first character, whatever its length in bytes; an unknown one is skipped. + let Some(tag) = p.chars().next() else { continue }; + let v = p.get(tag.len_utf8()..).unwrap_or_default(); + match tag { + 'W' => width = v.parse().map_err(|_| Error::Malformed("invalid width"))?, + 'H' => height = v.parse().map_err(|_| Error::Malformed("invalid height"))?, + 'F' => match parse_ratio(v) { + Some((n, d)) if n > 0 && d > 0 => frame_rate = Some((n, d)), + _ => return Err(Error::Malformed("invalid frame rate")), + }, + 'A' => { + if let Some((n, d)) = parse_ratio(v).filter(|&(n, d)| n > 0 && d > 0) + && let (Ok(n), Ok(d)) = (u32::try_from(n), u32::try_from(d)) + { + pixel_aspect = Some((n, d)); + } + } + 'C' => colour = v, + 'X' => full_range |= v.eq_ignore_ascii_case("COLORRANGE=FULL"), + _ => {} + } + } + if width == 0 || height == 0 { + return Err(Error::Malformed("missing or zero size")); + } + if u64::from(width) * u64::from(height) > limits.max_pixels { + return Err(Error::LimitExceeded("pixel count")); + } + let layout = Layout::parse(colour).ok_or_else(|| Error::UnsupportedColourSpace(colour.to_string()))?; + Ok(Header { width, height, frame_rate, pixel_aspect, layout, full_range }) + } + + /// Sizes of one frame's planes in samples: luma, each chroma plane (0 for mono), alpha. + pub fn plane_samples(&self) -> (usize, usize, usize) { + plane_samples(self.width, self.height, self.layout) + } + + /// Samples of a chroma plane of this size at `chroma` subsampling (what a caller needs to give + /// a `mono` file neutral chroma). + pub fn chroma_samples(&self, chroma: Chroma) -> usize { + plane_samples(self.width, self.height, Layout { chroma: Some(chroma), ..self.layout }).1 + } + + /// Bytes of one frame's planes. + pub fn frame_bytes(&self) -> usize { + let (luma, chroma, alpha) = self.plane_samples(); + luma.saturating_add(chroma.saturating_mul(2)).saturating_add(alpha).saturating_mul(self.layout.bytes_per_sample()) + } + + /// Where each plane is within one frame's data. + pub fn planes(&self) -> Planes { + let (luma, chroma, alpha) = self.plane_samples(); + let b = self.layout.bytes_per_sample(); + let (y_end, cb_end, cr_end) = (luma * b, (luma + chroma) * b, (luma + 2 * chroma) * b); + Planes { y: 0..y_end, cb: y_end..cb_end, cr: cb_end..cr_end, alpha: cr_end..cr_end + alpha * b } + } +} + +fn plane_samples(w: u32, h: u32, l: Layout) -> (usize, usize, usize) { + let luma = (w as usize).saturating_mul(h as usize); + let chroma = l.chroma.map_or(0, |c| { + let (sx, sy) = c.shifts(); + (w.div_ceil(1 << sx) as usize).saturating_mul(h.div_ceil(1 << sy) as usize) + }); + (luma, chroma, if l.alpha { luma } else { 0 }) +} + +/// The samples of a plane stored above 8 bits (16-bit little-endian), each clamped to the largest +/// value `bits` allows. +pub fn samples_u16(plane: &[u8], bits: u32) -> Vec { + let max = u16::MAX >> (16 - bits.clamp(1, 16)); + plane.as_chunks::<2>().0.iter().map(|&s| u16::from_le_bytes(s).min(max)).collect() +} + +/// An opened file: its header and where its frames are. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Stream { + pub header: Header, + /// File offset of each complete frame's planes, in order. + pub frames: Vec, + /// Bytes of one frame's planes ([`Header::frame_bytes`]). + pub frame_bytes: usize, +} + +/// The line starting at `pos` (without its newline) and the offset after it. +fn line_at(len: u64, read: &mut impl FnMut(u64, usize) -> Option>, pos: u64, max_line: usize) -> Option<(Vec, u64)> { + let mut want = 128usize.min(max_line.max(1)); + loop { + let take = len.saturating_sub(pos).min(want as u64) as usize; + let mut buf = read(pos, take)?; + buf.truncate(take); + if let Some(end) = buf.iter().position(|&b| b == b'\n') { + buf.truncate(end); + return Some((buf, pos.checked_add(end as u64 + 1)?)); + } + if take < want || want >= max_line { + return None; + } + want = want.saturating_mul(8).min(max_line); + } +} + +impl Stream { + /// Opens a file of `len` bytes through `read(offset, length)`, which returns that range of the + /// file (`None` on failure). Only the stream header and the `FRAME` lines are read. The index + /// ends at the first frame that is damaged or cut short, or at [`Limits::max_frames`]; a file + /// without one complete frame is an error. + pub fn open(len: u64, mut read: impl FnMut(u64, usize) -> Option>, limits: &Limits) -> Result { + let (line, mut pos) = line_at(len, &mut read, 0, limits.max_line).ok_or(Error::Malformed("no stream header"))?; + let header = Header::parse(&line, limits)?; + let frame_bytes = header.frame_bytes(); + let mut frames = Vec::new(); + while pos < len && frames.len() < limits.max_frames { + let Some((line, data)) = line_at(len, &mut read, pos, limits.max_line) else { break }; + if !line.starts_with(b"FRAME") { + break; + } + let Some(end) = data.checked_add(frame_bytes as u64).filter(|&e| e <= len) else { break }; + frames.push(data); + pos = end; + } + if frames.is_empty() { + return Err(Error::Malformed("no complete frames")); + } + Ok(Stream { header, frames, frame_bytes }) + } + + /// Opens a file held in memory. + pub fn from_bytes(bytes: &[u8], limits: &Limits) -> Result { + Self::open(bytes.len() as u64, |pos, n| usize::try_from(pos).ok().and_then(|p| bytes.get(p..p.checked_add(n)?)).map(<[u8]>::to_vec), limits) + } + + /// The file range of frame `index`'s planes, `None` past the last frame. + pub fn frame(&self, index: usize) -> Option> { + let at = *self.frames.get(index)?; + Some(at..at.checked_add(self.frame_bytes as u64)?) + } +} diff --git a/crates/y4m/src/tests.rs b/crates/y4m/src/tests.rs new file mode 100644 index 000000000..0afb59c8c --- /dev/null +++ b/crates/y4m/src/tests.rs @@ -0,0 +1,220 @@ +//! Ported from FilmCraft's Y4M tests (`crates/codecs/src/y4m.rs`, `tests/it/y4m_file.rs`). +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use super::*; + +/// A Y4M file: `header` after the signature, `frames` as raw plane bytes, each after `frame_line`. +fn y4m(header: &str, frame_line: &str, frames: &[Vec]) -> Vec { + let mut out = format!("YUV4MPEG2 {header}\n").into_bytes(); + for f in frames { + out.extend_from_slice(frame_line.as_bytes()); + out.push(b'\n'); + out.extend_from_slice(f); + } + out +} + +/// One 8-bit frame: every Y sample `y`, every Cb `cb`, every Cr `cr`. +fn planes8(luma: usize, chroma: usize, y: u8, cb: u8, cr: u8) -> Vec { + [vec![y; luma], vec![cb; chroma], vec![cr; chroma]].concat() +} + +fn open(bytes: &[u8]) -> Result { + Stream::from_bytes(bytes, &Limits::default()) +} + +fn frame<'a>(s: &Stream, bytes: &'a [u8], index: usize) -> &'a [u8] { + let r = s.frame(index).unwrap(); + &bytes[r.start as usize..r.end as usize] +} + +#[test] +fn colour_space_names() { + let l = |c: Option, bits, alpha| Some(Layout { chroma: c, bits, alpha }); + assert_eq!(Layout::parse("420jpeg"), l(Some(Chroma::C420), 8, false)); + assert_eq!(Layout::parse("420p10"), l(Some(Chroma::C420), 10, false)); + assert_eq!(Layout::parse("422p12"), l(Some(Chroma::C422), 12, false)); + assert_eq!(Layout::parse("444alpha"), l(Some(Chroma::C444), 8, true)); + assert_eq!(Layout::parse("mono"), l(None, 8, false)); + assert_eq!(Layout::parse("mono16"), l(None, 16, false)); + for bad in ["411", "420p", "420p7", "420p17", "mono10p12", "444alphap10", "", "p10"] { + assert_eq!(Layout::parse(bad), None, "{bad}"); + } +} + +#[test] +fn a_420_file_is_indexed_frame_by_frame() { + // 6 × 4, 4:2:0 (chroma 3 × 2), three frames at 25 fps. + let frames: Vec> = (0..3).map(|k| planes8(24, 6, 16 + 50 * k, 100 + k, 200 - k)).collect(); + let bytes = y4m("W6 H4 F25:1 Ip A1:1 C420jpeg", "FRAME", &frames); + assert!(sniff(&bytes)); + let s = open(&bytes).unwrap(); + let h = &s.header; + assert_eq!((h.width, h.height, h.frame_rate, h.pixel_aspect, h.full_range), (6, 4, Some((25, 1)), Some((1, 1)), false)); + assert_eq!(h.layout, Layout { chroma: Some(Chroma::C420), bits: 8, alpha: false }); + assert_eq!((h.plane_samples(), s.frame_bytes, s.frames.len()), ((24, 6, 0), 36, 3)); + let p = h.planes(); + assert_eq!((p.y.clone(), p.cb.clone(), p.cr.clone(), p.alpha.clone()), (0..24, 24..30, 30..36, 36..36)); + for k in 0..3u8 { + let f = frame(&s, &bytes, usize::from(k)); + assert!( + f[p.y.clone()].iter().all(|&v| v == 16 + 50 * k) + && f[p.cb.clone()].iter().all(|&v| v == 100 + k) + && f[p.cr.clone()].iter().all(|&v| v == 200 - k) + ); + } + assert_eq!(s.frame(3), None); +} + +#[test] +fn high_bit_depth_frame_parameters_aspect_and_range() { + // 4 × 2, 4:2:2 10-bit (little-endian), 30000:1001, 4:3 pixels, full range, and frame lines + // carrying their own parameters. + let mut data = Vec::new(); + for (n, v) in [(8, 940u16), (4, 512), (4, 64)] { + for _ in 0..n { + data.extend(v.to_le_bytes()); + } + } + let bytes = y4m("W4 H2 F30000:1001 It A4:3 C422p10 XCOLORRANGE=FULL", "FRAME Ip XFOO=1", &[data.clone(), data]); + let s = open(&bytes).unwrap(); + let h = &s.header; + assert_eq!((h.frame_rate, h.pixel_aspect, h.full_range), (Some((30000, 1001)), Some((4, 3)), true)); + assert_eq!((h.layout, h.layout.bytes_per_sample(), s.frames.len()), (Layout { chroma: Some(Chroma::C422), bits: 10, alpha: false }, 2, 2)); + let (p, f) = (h.planes(), frame(&s, &bytes, 1)); + assert_eq!(samples_u16(&f[p.y], 10), [940; 8]); + assert_eq!(samples_u16(&f[p.cb], 10), [512; 4]); + assert_eq!(samples_u16(&f[p.cr], 10), [64; 4]); + // Samples above the depth's largest value are clamped. + assert_eq!(samples_u16(&[0xFF, 0xFF, 0x00, 0x04, 0x01], 10), [1023, 1023]); +} + +#[test] +fn mono_alpha_and_defaults() { + let bytes = y4m("W3 H3 Cmono", "FRAME", &[vec![77; 9]]); + let s = open(&bytes).unwrap(); + assert_eq!((s.header.layout.chroma, s.header.frame_rate, s.header.pixel_aspect), (None, None, None)); + assert_eq!((s.header.plane_samples(), s.header.chroma_samples(Chroma::C420), s.header.chroma_samples(Chroma::C444)), ((9, 0, 0), 4, 9)); + let p = s.header.planes(); + assert!(p.cb.is_empty() && p.cr.is_empty() && p.alpha.is_empty()); + // No colour space means 4:2:0; an unknown aspect (0:0) is none. + let s = open(&y4m("W2 H2 A0:0", "FRAME", &[vec![0; 6]])).unwrap(); + assert_eq!((s.header.layout.chroma, s.header.pixel_aspect), (Some(Chroma::C420), None)); + let bytes = y4m("W2 H1 C444alpha", "FRAME", &[vec![1, 1, 2, 2, 3, 3, 4, 4]]); + let s = open(&bytes).unwrap(); + let p = s.header.planes(); + assert_eq!((s.header.plane_samples(), p.alpha.clone()), ((2, 2, 2), 6..8)); + assert_eq!(&frame(&s, &bytes, 0)[p.alpha], &[4, 4]); +} + +#[test] +fn a_file_opens_through_a_reader_in_small_reads() { + let bytes = y4m("W2 H2 F25:1 C444", "FRAME", &[planes8(4, 4, 50, 60, 70), planes8(4, 4, 90, 60, 70)]); + let mut longest = 0; + let s = Stream::open( + bytes.len() as u64, + |pos, n| { + longest = longest.max(n); + bytes.get(pos as usize..pos as usize + n).map(<[u8]>::to_vec) + }, + &Limits::default(), + ) + .unwrap(); + assert_eq!(frame(&s, &bytes, 1)[0], 90); + // Only lines are read, never a frame's planes. + assert!(longest <= 128, "{longest}"); + // A read that fails ends the index, or the open when it is the header's. + assert_eq!(Stream::open(bytes.len() as u64, |_, _| None, &Limits::default()), Err(Error::Malformed("no stream header"))); + let first = s.frames[0]; + let s = Stream::open( + bytes.len() as u64, + |pos, n| if pos > first { None } else { bytes.get(pos as usize..pos as usize + n).map(<[u8]>::to_vec) }, + &Limits::default(), + ) + .unwrap(); + assert_eq!(s.frames.len(), 1); +} + +#[test] +fn a_cut_or_damaged_frame_ends_the_file() { + let frames = [planes8(4, 1, 1, 2, 3), planes8(4, 1, 4, 5, 6)]; + let good = y4m("W2 H2", "FRAME", &frames); + assert_eq!(open(&good).unwrap().frames.len(), 2); + assert_eq!(open(&good[..good.len() - 1]).unwrap().frames.len(), 1); + let mut junk = good.clone(); + let second = open(&good).unwrap().frames[1] as usize - 6; + junk[second] = b'X'; + assert_eq!(open(&junk).unwrap().frames.len(), 1); + assert_eq!(open(&y4m("W2 H2", "FRAME", &[vec![0; 5]])), Err(Error::Malformed("no complete frames"))); + assert_eq!(open(b"YUV4MPEG2 W2 H2"), Err(Error::Malformed("no stream header"))); +} + +#[test] +fn bad_headers_are_errors() { + let h = |header: &str| open(&y4m(header, "FRAME", &[vec![0; 36]])); + assert_eq!(h("W0 H4 F25:1"), Err(Error::Malformed("missing or zero size"))); + assert_eq!(h("H4 F25:1"), Err(Error::Malformed("missing or zero size"))); + assert_eq!(h("Wx H4"), Err(Error::Malformed("invalid width"))); + assert_eq!(h("W6 H-4"), Err(Error::Malformed("invalid height"))); + for rate in ["F0:0", "F25:0", "F-5:1", "F25", "Fa:b"] { + assert_eq!(h(&format!("W6 H4 {rate}")), Err(Error::Malformed("invalid frame rate")), "{rate}"); + } + assert_eq!(h("W6 H4 F25:1 C411"), Err(Error::UnsupportedColourSpace("411".into()))); + assert_eq!(h("W6 H4 F25:1 Cbogus"), Err(Error::UnsupportedColourSpace("bogus".into()))); + assert!(h("W6 H4 F1:9223372036854775807").is_ok()); + assert_eq!(Header::parse(b"YUV4MPEG W2 H2", &Limits::default()), Err(Error::Malformed("not a YUV4MPEG2 file"))); + assert!(!sniff(b"YUV4MPEG2X") && !sniff(b"YUV4MPEG") && sniff(b"YUV4MPEG2\n")); +} + +#[test] +fn limits() { + let h = |header: &str| open(&y4m(header, "FRAME", &[vec![0; 36]])); + assert_eq!(h("W99999999 H99999999 F25:1"), Err(Error::LimitExceeded("pixel count"))); + assert_eq!(h("W4294967295 H4294967295"), Err(Error::LimitExceeded("pixel count"))); + let bytes = y4m("W2 H2", "FRAME", &[vec![0; 6], vec![0; 6], vec![0; 6]]); + assert!(Stream::from_bytes(&bytes, &Limits { max_pixels: 4, ..Limits::default() }).is_ok()); + assert_eq!(Stream::from_bytes(&bytes, &Limits { max_pixels: 3, ..Limits::default() }), Err(Error::LimitExceeded("pixel count"))); + // The index stops at the frame limit. + assert_eq!(Stream::from_bytes(&bytes, &Limits { max_frames: 2, ..Limits::default() }).unwrap().frames.len(), 2); + // A header or frame line longer than the line limit is not read. + let long = y4m(&format!("W2 H2 X{}", "y".repeat(300)), "FRAME", &[vec![0; 6]]); + assert!(open(&long).is_ok()); + assert_eq!(Stream::from_bytes(&long, &Limits { max_line: 256, ..Limits::default() }), Err(Error::Malformed("no stream header"))); + let long_frame = y4m("W2 H2", &format!("FRAME X{}", "y".repeat(300)), &[vec![0; 6]]); + assert!(open(&long_frame).is_ok()); + assert_eq!(Stream::from_bytes(&long_frame, &Limits { max_line: 256, ..Limits::default() }), Err(Error::Malformed("no complete frames"))); + assert_eq!(Limits::default(), Limits { max_pixels: 1 << 28, max_line: 64 << 10, max_frames: 1 << 24 }); +} + +#[test] +fn damaged_and_hostile_files_never_panic() { + let good = y4m("W6 H4 F25:1 C420jpeg", "FRAME", &[planes8(24, 6, 1, 2, 3), planes8(24, 6, 4, 5, 6)]); + let mut cases: Vec> = (0..good.len()).map(|n| good[..n].to_vec()).collect(); + for i in 0..good.len() { + let mut b = good.clone(); + b[i] ^= 0x41; + cases.push(b); + } + for b in cases { + if let Ok(s) = open(&b) { + for k in 0..s.frames.len() { + let r = s.frame(k).unwrap(); + assert!(r.end <= b.len() as u64); + } + } + } +} + +/// A header parameter that starts with a multi-byte character is skipped like any unknown tag +/// (splitting it at byte 1 would cut the character in two). +#[test] +fn multibyte_header_param_is_ignored() { + let mut data = "YUV4MPEG2 W2 H2 éX\nFRAME\n".as_bytes().to_vec(); + data.extend([0u8; 6]); + let s = open(&data).unwrap(); + assert_eq!((s.header.width, s.header.height, s.frames.len()), (2, 2, 1)); + // Bytes that are not UTF-8 at all are skipped the same way. + let mut data = b"YUV4MPEG2 W2 H2 \xFF\xFEX\nFRAME\n".to_vec(); + data.extend([0u8; 6]); + assert!(open(&data).is_ok()); +} From 090f121943d23e29da048daa32418982c1a1fef5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:45:10 -0500 Subject: [PATCH 019/120] craft-mime: strict base64, mailto links and code page labels from MailCraft (#934) What MailCraft's mime and charset crates still had that craft-mime did not: strict RFC 4648 base64 (crates/charset), mailto: links (RFC 6068, crates/mime) and the Windows code page to charset label table (crates/pst). With these, MailCraft's charset crate has nothing craft-mime lacks. Co-authored-by: Martin Ball <98639962+benandez@users.noreply.github.com> --- crates/mime/README.md | 17 ++- crates/mime/src/base64.rs | 147 +++++++++++++++++++ crates/mime/src/base64_tests.rs | 81 +++++++++++ crates/mime/src/charset.rs | 62 ++++++++ crates/mime/src/lib.rs | 6 +- crates/mime/src/mailto.rs | 245 ++++++++++++++++++++++++++++++++ 6 files changed, 551 insertions(+), 7 deletions(-) create mode 100644 crates/mime/src/base64.rs create mode 100644 crates/mime/src/base64_tests.rs create mode 100644 crates/mime/src/mailto.rs diff --git a/crates/mime/README.md b/crates/mime/README.md index 9fd1207bb..34316b7bb 100644 --- a/crates/mime/README.md +++ b/crates/mime/README.md @@ -16,6 +16,10 @@ MHTML web archives and mbox files. qp_decode}`, `charset::{decode, guess}`, `parse_address_list` / `format_address_list`, `parse_date` / `format_date`, `split_multipart`, `mbox_messages` / `write_mbox_message`, and `Resources` / `start_part` / `resolve_location` for `multipart/related`. +- **Beside MIME**, what the same callers need: `parse_mailto` reads a `mailto:` link (RFC 6068: + recipients, subject and body only, sizes capped, no line break into a header), `base64` is + strict RFC 4648 (both alphabets, padded or not, one spelling per value) for keys, tokens and + credentials, and `charset::code_page_label` names the character set of a Windows code page. Written from the public RFCs: 5322 (messages), 2045–2049 (MIME), 2047 (encoded-words), 2231 (parameter values), 2387 and 2557 (`multipart/related`, MHTML), 4155 (mbox, `mboxrd` quoting). @@ -89,10 +93,7 @@ always there as `headers.get("date")`. - S/MIME and PGP: detecting protected messages, canonical CRLF form, keeping the original bytes. `split_headers` and `split_multipart` give the raw slices those need. - File-name-to-media-type tables and default attachment names. -- `mailto:` links (RFC 6068), which are not MIME. - For MHT: picking the page, reading its HTML, and the budget for pictures loaded. -- Strict base64 (RFC 4648 with refusals, URL-safe alphabet) for keys and tokens. This crate has - the mail kind only: an encoder, and a decoder that skips whatever is not base64. ## Migration @@ -113,7 +114,8 @@ No app uses this crate yet. These tables say what replaces what. | `parse::Walk` (the recursion over parts, depth and part caps) | `parse` / `parse_entity`, then a walk over `Part`s | | `parse::is_media_type`, content id and file name extraction | `Part::declared_media_type`, `content_id`, `file_name`, `disposition` | | `charset::decode`, `guess`, `unix_lines` (`mailcraft-charset`) | `charset::` the same names | -| `mailcraft_charset::base64` (strict) | stays | +| `mailcraft_charset::base64` (strict) | `base64::` the same names (`STANDARD`, `STANDARD_NO_PAD`, `URL_SAFE`, `URL_SAFE_NO_PAD`, `Config`, `DecodeError`) | +| `mailcraft_pst::code_page_label` | `charset::code_page_label` | | `address::parse_list`, `format_address`, `format_list` | `parse_address_list`, `format_address`, `format_address_list`, on `Mailbox { name, email }` | | `mailcraft_time::parse_rfc5322`, `format_rfc5322` | `parse_date`, `format_date`, on `DateTime` | | `mbox::mbox_messages`, `parse_mbox` | `mbox_messages` gives each message's bytes; MailCraft parses them | @@ -126,7 +128,8 @@ No app uses this crate yet. These tables say what replaces what. | `media::safe_media_type` | `safe_media_type` | | `media::media_type_for`, `default_name` | stay | | `parse::read_headers`, `write::write_header`, `calendar_form`, `body` | stay (model mapping) | -| `protected`, `signed`, `rich`, `mailto` | stay | +| `mailto::{Mailto, is_mailto, parse_mailto}` | the same names; `mailto::cut`, which `rich` borrows, stays | +| `protected`, `signed`, `rich` | stay | ### WordCraft (`crates/formats/src/mht.rs`) @@ -153,7 +156,9 @@ settable). | Part | Source | |---|---| | Headers, parameters, encoded-words, transfer encodings, address lists, multipart splitting, mbox, the writer's parts and boundaries, their tests | MailCraft `crates/mime` [a44a450](https://github.com/UCS-Care-Group/mailcraft/commit/a44a45013d1c6396229c40ab60172d1882b368d5) | -| Character sets | MailCraft `crates/charset`, same commit | +| Character sets, strict base64 | MailCraft `crates/charset`, same commit | +| `mailto:` links | MailCraft `crates/mime` (`mailto.rs`), same commit | +| Code page labels | MailCraft `crates/pst` (`codepage.rs`), same commit | | Date parsing and formatting | MailCraft `crates/time` (`text.rs`), same commit; the calendar arithmetic replaced by `chrono` | | `multipart/related` lookups, location resolving, comment-aware parameters, the MHT tests | WordCraft `crates/formats/src/mht.rs` [639b813](https://github.com/storytold/wordcraft/commit/639b8133ea31efb63aac381a7ced8b8aed32aa30) | diff --git a/crates/mime/src/base64.rs b/crates/mime/src/base64.rs new file mode 100644 index 000000000..a93a6fdfb --- /dev/null +++ b/crates/mime/src/base64.rs @@ -0,0 +1,147 @@ +//! Base64 (RFC 4648 sections 4 and 5), written from the RFC. +//! +//! Four ready-made [`Config`]s: [`STANDARD`] (the `+/` alphabet, padded with `=`), +//! [`STANDARD_NO_PAD`], [`URL_SAFE`] (the `-_` alphabet, padded) and [`URL_SAFE_NO_PAD`]. +//! +//! Encoding is the RFC's. **Decoding is strict**: nothing but the config's alphabet (no white +//! space, no line breaks), padding exactly as the config says (a padded config requires it and +//! counts it; an unpadded one refuses `=`), a length that can be, and the unused bits of the last +//! symbol zero (so every value has one spelling). A caller that reads mail, where line breaks and +//! missing padding are everyday, cleans its input first, as it always has; the strictness is what +//! the signature, key and credential checks want. No input panics, and the output is never longer +//! than three quarters of the input. + +use std::fmt; + +/// Which alphabet and whether padded. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Config { + url_safe: bool, + padded: bool, +} + +/// RFC 4648 section 4: `A-Za-z0-9+/`, padded with `=`. +pub const STANDARD: Config = Config { url_safe: false, padded: true }; +/// Section 4 without padding. +pub const STANDARD_NO_PAD: Config = Config { url_safe: false, padded: false }; +/// Section 5: `A-Za-z0-9-_`, padded with `=`. +pub const URL_SAFE: Config = Config { url_safe: true, padded: true }; +/// Section 5 without padding (OAuth's PKCE and JSON Web Tokens). +pub const URL_SAFE_NO_PAD: Config = Config { url_safe: true, padded: false }; + +/// Why a text is not base64 of the config's kind. Says nothing of the text itself. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct DecodeError; + +impl fmt::Display for DecodeError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("not valid base64") + } +} + +impl std::error::Error for DecodeError {} + +impl Config { + /// The symbol for the six bits `v` (only the low six are looked at). + fn symbol(self, v: u32) -> char { + let v = (v & 63) as u8; + let c = match v { + 0..=25 => b'A'.saturating_add(v), + 26..=51 => b'a'.saturating_add(v.saturating_sub(26)), + 52..=61 => b'0'.saturating_add(v.saturating_sub(52)), + 62 if self.url_safe => b'-', + 62 => b'+', + _ if self.url_safe => b'_', + _ => b'/', + }; + char::from(c) + } + + /// The six bits a symbol stands for, if it is one. + fn value(self, c: u8) -> Option { + let v = match c { + b'A'..=b'Z' => c.saturating_sub(b'A'), + b'a'..=b'z' => c.saturating_sub(b'a').saturating_add(26), + b'0'..=b'9' => c.saturating_sub(b'0').saturating_add(52), + b'+' if !self.url_safe => 62, + b'/' if !self.url_safe => 63, + b'-' if self.url_safe => 62, + b'_' if self.url_safe => 63, + _ => return None, + }; + Some(u32::from(v)) + } + + /// The text for `data`. + pub fn encode(self, data: impl AsRef<[u8]>) -> String { + let data = data.as_ref(); + let mut out = String::with_capacity(data.len().div_ceil(3).saturating_mul(4)); + let (groups, remainder) = data.as_chunks::<3>(); + for [a, b, c] in groups { + let n = u32::from(*a) << 16 | u32::from(*b) << 8 | u32::from(*c); + for shift in [18, 12, 6, 0] { + out.push(self.symbol(n >> shift)); + } + } + match *remainder { + [a] => { + let n = u32::from(a) << 16; + out.push(self.symbol(n >> 18)); + out.push(self.symbol(n >> 12)); + if self.padded { + out.push_str("=="); + } + } + [a, b] => { + let n = u32::from(a) << 16 | u32::from(b) << 8; + out.push(self.symbol(n >> 18)); + out.push(self.symbol(n >> 12)); + out.push(self.symbol(n >> 6)); + if self.padded { + out.push('='); + } + } + _ => {} + } + out + } + + /// The bytes of `text`, which must be exactly base64 of this config's kind (see the module). + pub fn decode(self, text: impl AsRef<[u8]>) -> Result, DecodeError> { + let text = text.as_ref(); + let (symbols, pad) = if self.padded { + if text.len() % 4 != 0 { + return Err(DecodeError); + } + let pad = text.iter().rev().take(2).take_while(|c| **c == b'=').count(); + (text.get(..text.len().saturating_sub(pad)).unwrap_or_default(), pad) + } else { + (text, 0) + }; + // A last group of one symbol holds under eight bits; with padding, `=` fills it to four. + let rest = symbols.len() % 4; + if rest == 1 || (self.padded && pad != (4 - rest) % 4) { + return Err(DecodeError); + } + let mut out = Vec::with_capacity(symbols.len().saturating_mul(3) / 4); + let (mut acc, mut bits) = (0u32, 0u32); + for c in symbols { + acc = acc << 6 | self.value(*c).ok_or(DecodeError)?; + bits = bits.saturating_add(6); + if bits >= 8 { + bits -= 8; + out.push((acc >> bits) as u8); + acc &= (1 << bits) - 1; + } + } + // What is left over must be zero: one spelling for one value. + if acc != 0 { + return Err(DecodeError); + } + Ok(out) + } +} + +#[cfg(test)] +#[path = "base64_tests.rs"] +mod base64_tests; diff --git a/crates/mime/src/base64_tests.rs b/crates/mime/src/base64_tests.rs new file mode 100644 index 000000000..e3d52794e --- /dev/null +++ b/crates/mime/src/base64_tests.rs @@ -0,0 +1,81 @@ +//! Base64: the RFC 4648 test vectors, the strict refusals, and that only the one spelling of a +//! value decodes, on every config. + +use super::*; +use proptest::prelude::*; + +#[test] +fn the_rfc_vectors_round_trip_in_every_config() { + let vectors = [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")]; + for (plain, coded) in vectors { + assert_eq!(STANDARD.encode(plain), coded); + assert_eq!(URL_SAFE.encode(plain), coded); + assert_eq!(STANDARD_NO_PAD.encode(plain), coded.trim_end_matches('=')); + assert_eq!(URL_SAFE_NO_PAD.encode(plain), coded.trim_end_matches('=')); + assert_eq!(STANDARD.decode(coded).unwrap(), plain.as_bytes()); + assert_eq!(URL_SAFE_NO_PAD.decode(coded.trim_end_matches('=')).unwrap(), plain.as_bytes()); + } +} + +#[test] +fn the_two_alphabets_differ_in_the_last_two_symbols() { + assert_eq!(STANDARD.encode([0xfb, 0xff]), "+/8="); + assert_eq!(URL_SAFE.encode([0xfb, 0xff]), "-_8="); + assert!(URL_SAFE.decode("+/8=").is_err()); + assert!(STANDARD.decode("-_8=").is_err()); +} + +#[test] +fn what_is_not_exactly_base64_is_refused() { + for bad in ["Zg", "Zg=", "Zg===", "Z", "Z===", "====", "Zm9v\n", " Zm9v", "Zm 9v", "Zh==", "Zm9=", "Zm=v", "Zm9v=", "é", "Zm9\u{0}"] { + assert!(STANDARD.decode(bad).is_err(), "{bad:?}"); + } + for bad in ["Zg==", "Z", "Zh", "Zm9v\n", "Zm 9v", "Zg=", "Zm9="] { + assert!(STANDARD_NO_PAD.decode(bad).is_err(), "{bad:?}"); + } + assert!(STANDARD_NO_PAD.decode("Zm8").is_ok()); +} + +#[test] +fn hostile_inputs_do_not_panic() { + let big = vec![b'A'; 1 << 20]; + for cfg in [STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD] { + assert_eq!(cfg.decode(&big).map(|b| b.len()).unwrap_or(0), 786_432); + for bytes in [&b""[..], b"=", b"==", b"===", b"\xff\xfe", b"=A=A", b"AA=A"] { + let _ = cfg.decode(bytes); + } + } +} + +const CONFIGS: [Config; 4] = [STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD]; + +proptest! { + #[test] + fn what_is_encoded_decodes_to_itself(data in proptest::collection::vec(any::(), 0..400)) { + for config in CONFIGS { + prop_assert_eq!(config.decode(config.encode(&data)).ok(), Some(data.clone())); + } + } + + /// Strict: a text is accepted only when it is the one spelling of its bytes. + #[test] + fn what_decodes_is_the_one_spelling_of_its_bytes( + data in proptest::collection::vec(any::(), 0..400), + junk in proptest::collection::vec(any::(), 0..64), + text in "[A-Za-z0-9+/=_ -]{0,40}", + ) { + for config in CONFIGS { + let mut cut = config.encode(&data).into_bytes(); + cut.pop(); + let mut pad = cut.clone(); + if let Some(first) = pad.first_mut() { + *first = b'='; + } + for candidate in [junk.as_slice(), text.as_bytes(), cut.as_slice(), pad.as_slice()] { + if let Ok(bytes) = config.decode(candidate) { + prop_assert_eq!(config.encode(&bytes).into_bytes(), candidate.to_vec()); + } + } + } + } +} diff --git a/crates/mime/src/charset.rs b/crates/mime/src/charset.rs index 59f45cd67..9eacb1462 100644 --- a/crates/mime/src/charset.rs +++ b/crates/mime/src/charset.rs @@ -51,6 +51,52 @@ pub fn guess(bytes: &[u8]) -> String { } } +/// The character set a Windows code page stands for (as Outlook items, RTF and +/// Windows APIs number them), by the label [`decode`] reads it under. `None` for a page that has no +/// character set here. +pub fn code_page_label(cp: i64) -> Option<&'static str> { + Some(match cp { + 65001 => "utf-8", + 1200 => "utf-16le", + 1201 => "utf-16be", + 874 => "windows-874", + 1250 => "windows-1250", + 1251 => "windows-1251", + 1252 | 28591 => "windows-1252", + 1253 => "windows-1253", + 1254 => "windows-1254", + 1255 => "windows-1255", + 1256 => "windows-1256", + 1257 => "windows-1257", + 1258 => "windows-1258", + 932 => "shift_jis", + 936 => "gbk", + 54936 => "gb18030", + 949 | 51949 => "euc-kr", + 950 => "big5", + 51932 => "euc-jp", + 50220..=50222 => "iso-2022-jp", + 866 => "ibm866", + 20866 => "koi8-r", + 21866 => "koi8-u", + 10000 => "macintosh", + 10007 => "x-mac-cyrillic", + 28592 => "iso-8859-2", + 28593 => "iso-8859-3", + 28594 => "iso-8859-4", + 28595 => "iso-8859-5", + 28596 => "iso-8859-6", + 28597 => "iso-8859-7", + 28598 => "iso-8859-8", + 38598 => "iso-8859-8-i", + 28599 => "iso-8859-9", + 28603 => "iso-8859-13", + 28605 => "iso-8859-15", + 20127 => "us-ascii", + _ => return None, + }) +} + /// Line endings as `\n`: CRLF and a lone CR both become LF. pub fn unix_lines(text: &str) -> String { if !text.contains('\r') { @@ -98,6 +144,22 @@ mod tests { assert_eq!(guess(b"\xE2\x82"), "\u{e2}\u{201a}"); } + #[test] + fn code_pages_name_character_sets_the_decoder_knows() { + assert_eq!(code_page_label(1252), Some("windows-1252")); + assert_eq!(code_page_label(28591), Some("windows-1252")); + assert_eq!(code_page_label(65001), Some("utf-8")); + assert_eq!(code_page_label(0), None); + assert_eq!(code_page_label(-1), None); + // Every label is one the Encoding Standard knows (US-ASCII reads as windows-1252 there). + for cp in 0..66_000 { + if let Some(label) = code_page_label(cp) { + assert!(encoding_rs::Encoding::for_label(label.as_bytes()).is_some(), "{cp} {label}"); + } + } + assert_eq!(decode(&[0xC0, 0xC1], code_page_label(1251).unwrap_or_default()), "АБ"); + } + #[test] fn line_endings() { assert_eq!(unix_lines("a\r\nb\rc\nd\r\n"), "a\nb\nc\nd\n"); diff --git a/crates/mime/src/lib.rs b/crates/mime/src/lib.rs index c59d6e9c8..964eb6f06 100644 --- a/crates/mime/src/lib.rs +++ b/crates/mime/src/lib.rs @@ -12,7 +12,8 @@ //! ([`transfer`]), character sets ([`charset`]), address lists ([`parse_address_list`], //! [`format_address_list`]), dates ([`parse_date`], [`format_date`]), mbox files //! ([`mbox_messages`], [`write_mbox_message`]) and the lookups of `multipart/related` -//! ([`Resources`]). +//! ([`Resources`]). Beside MIME: `mailto:` links ([`parse_mailto`], RFC 6068) and strict +//! base64 ([`base64`], RFC 4648). //! //! Written from the public RFCs: 5322 (messages), 2045–2049 (MIME), 2047 (encoded-words), 2231 //! (parameter values), 2387 and 2557 (`multipart/related`, MHTML) and 4155 (mbox). @@ -25,12 +26,14 @@ //! What a message means (which part is the body, which parts are files, what a flag header //! says) is the caller's: this crate has no notion of a mailbox or a document. +pub mod base64; pub mod charset; pub mod transfer; mod address; mod date; mod header; +mod mailto; mod mbox; mod part; mod related; @@ -40,6 +43,7 @@ mod write; pub use address::{MAX_ADDRESSES, Mailbox, format_address, format_address_list, parse_address_list}; pub use date::{DateTime, format_date, parse_date}; pub use header::{Field, Headers, MAX_PARAMS, Params, clean, is_field_name, parse_params, split_headers, write_field}; +pub use mailto::{Mailto, is_mailto, parse_mailto}; pub use mbox::{MboxMessages, mbox_messages, write_mbox_message}; pub use part::{Body, Message, Part, parse, parse_entity, split_multipart}; pub use related::{Resources, resolve_location, start_part}; diff --git a/crates/mime/src/mailto.rs b/crates/mime/src/mailto.rs new file mode 100644 index 000000000..36a8f1c70 --- /dev/null +++ b/crates/mime/src/mailto.rs @@ -0,0 +1,245 @@ +//! `mailto:` links (RFC 6068): who a new message is for and how it starts. +//! +//! A link is hostile input like any other: it comes from a web page, a document or a message. +//! Only the recipients, the subject and the body are taken from it. Every other header field a +//! link names is ignored (RFC 6068 section 7: a link must not get to set headers of its choice), +//! nothing taken from it can carry a line break into a header, and sizes are capped. + +/// What a `mailto:` link asks a new message to start with. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Mailto { + pub to: Vec, + pub cc: Vec, + pub bcc: Vec, + pub subject: String, + pub body: String, +} + +/// The most of a link that is read. +const MAX_LINK: usize = 64 * 1024; +/// The most recipients taken for one field. +const MAX_ADDRESSES: usize = 200; +/// The longest subject taken (a header line's limit in RFC 5322). +const MAX_SUBJECT: usize = 998; + +/// Whether `text` is a `mailto:` link (by its scheme, whatever its case). +pub fn is_mailto(text: &str) -> bool { + rest_of(text).is_some() +} + +/// What follows the scheme, when `text` is a `mailto:` link. +fn rest_of(text: &str) -> Option<&str> { + let text = text.trim(); + let scheme = text.get(..7)?; + if scheme.eq_ignore_ascii_case("mailto:") { text.get(7..) } else { None } +} + +/// Read a `mailto:` link. `None` when `link` isn't one; a link with nothing after the scheme is +/// an empty message. +pub fn parse_mailto(link: &str) -> Option { + let rest = cut(rest_of(link)?, MAX_LINK); + // A fragment is no part of the message. + let rest = rest.split('#').next().unwrap_or(""); + let (path, query) = rest.split_once('?').unwrap_or((rest, "")); + let mut m = Mailto::default(); + // Commas separate the addresses before they are decoded: an encoded comma belongs to its address. + for address in path.split(',') { + add(&mut m.to, &decode(address)); + } + let (mut subject, mut body) = (None, None); + for field in query.split('&') { + let (name, value) = field.split_once('=').unwrap_or((field, "")); + let value = decode(value); + match decode(name).to_ascii_lowercase().as_str() { + "to" => add_list(&mut m.to, &value), + "cc" => add_list(&mut m.cc, &value), + "bcc" => add_list(&mut m.bcc, &value), + // The first of each counts; a link that repeats one is not to be trusted with the rest. + "subject" if subject.is_none() => subject = Some(one_line(&value, MAX_SUBJECT)), + "body" if body.is_none() => body = Some(lines(&value)), + _ => {} + } + } + m.subject = subject.unwrap_or_default(); + m.body = body.unwrap_or_default(); + Some(m) +} + +/// `text` up to `max` bytes, cut where a character ends. +pub(crate) fn cut(text: &str, max: usize) -> &str { + if text.len() <= max { + return text; + } + let mut end = max; + while end > 0 && !text.is_char_boundary(end) { + end -= 1; + } + text.get(..end).unwrap_or("") +} + +/// Percent-decoding. An escape that isn't one stays as it is written, a plus sign is a plus sign +/// (RFC 6068 section 5), and bytes that aren't UTF-8 become the replacement character. +fn decode(text: &str) -> String { + let bytes = text.as_bytes(); + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0; + while let Some(&b) = bytes.get(i) { + let escaped = if b == b'%' { hex(bytes.get(i + 1)).zip(hex(bytes.get(i + 2))) } else { None }; + match escaped { + Some((high, low)) => { + out.push((high << 4) | low); + i += 3; + } + None => { + out.push(b); + i += 1; + } + } + } + String::from_utf8_lossy(&out).into_owned() +} + +fn hex(b: Option<&u8>) -> Option { + char::from(*b?).to_digit(16).and_then(|d| u8::try_from(d).ok()) +} + +/// One header line's worth of text: line breaks and other control characters become spaces. +fn one_line(text: &str, max: usize) -> String { + let line: String = text.chars().map(|c| if c.is_control() { ' ' } else { c }).collect(); + cut(line.trim(), max).trim_end().to_string() +} + +/// Body text: every kind of line break becomes `\n`, and no other control character but the tab +/// is kept. +fn lines(text: &str) -> String { + text.replace("\r\n", "\n").chars().map(|c| if c == '\r' { '\n' } else { c }).filter(|c| !c.is_control() || matches!(c, '\n' | '\t')).collect() +} + +/// One address, if anything is left of it once it is a single clean line. +fn add(list: &mut Vec, address: &str) { + let address = one_line(address, MAX_SUBJECT); + if !address.is_empty() && list.len() < MAX_ADDRESSES { + list.push(address); + } +} + +/// The addresses of a decoded header value: separated by commas, except inside a quoted name or +/// an angle-bracketed address. +fn add_list(list: &mut Vec, value: &str) { + let (mut start, mut quoted, mut angle, mut escaped) = (0, false, false, false); + for (i, c) in value.char_indices() { + if escaped { + escaped = false; + continue; + } + match c { + '\\' if quoted => escaped = true, + '"' => quoted = !quoted, + '<' if !quoted => angle = true, + '>' if !quoted => angle = false, + ',' if !quoted && !angle => { + add(list, value.get(start..i).unwrap_or("")); + start = i + 1; + } + _ => {} + } + } + add(list, value.get(start..).unwrap_or("")); +} + +#[cfg(test)] +mod tests { + use proptest::prelude::*; + + use super::*; + + fn s(list: &[&str]) -> Vec { + list.iter().map(|a| a.to_string()).collect() + } + + #[test] + fn reads_the_examples_of_rfc_6068() { + assert_eq!(parse_mailto("mailto:chris@example.org"), Some(Mailto { to: s(&["chris@example.org"]), ..Default::default() })); + let m = parse_mailto("mailto:infobot@example.org?subject=current-issue").unwrap(); + assert_eq!((m.to, m.subject.as_str()), (s(&["infobot@example.org"]), "current-issue")); + let m = parse_mailto("mailto:infobot@example.org?body=send%20current-issue%0D%0Asend%20index").unwrap(); + assert_eq!(m.body, "send current-issue\nsend index"); + let m = parse_mailto("mailto:joe@example.org?cc=bob@example.org&body=hello").unwrap(); + assert_eq!((m.to, m.cc, m.body.as_str()), (s(&["joe@example.org"]), s(&["bob@example.org"]), "hello")); + // Addresses in the path, in `to` fields, or both. + let m = parse_mailto("mailto:addr1@an.example,addr2@an.example").unwrap(); + assert_eq!(m.to, s(&["addr1@an.example", "addr2@an.example"])); + let m = parse_mailto("mailto:?to=addr1@an.example,addr2@an.example").unwrap(); + assert_eq!(m.to, s(&["addr1@an.example", "addr2@an.example"])); + let m = parse_mailto("mailto:addr1@an.example?to=addr2@an.example").unwrap(); + assert_eq!(m.to, s(&["addr1@an.example", "addr2@an.example"])); + // Encoded characters in an address, and UTF-8 in a subject. + assert_eq!(parse_mailto("mailto:gorby%25kremvax@example.org").unwrap().to, s(&["gorby%kremvax@example.org"])); + assert_eq!(parse_mailto("mailto:user@example.org?subject=caf%C3%A9").unwrap().subject, "café"); + assert_eq!(parse_mailto("mailto:%22not%40me%22@example.org").unwrap().to, s(&["\"not@me\"@example.org"])); + } + + #[test] + fn knows_a_link_from_anything_else() { + assert!(is_mailto(" MailTo:someone@example.org ")); + assert!(!is_mailto("mailbox.example")); + assert!(!is_mailto("mail")); + assert!(!is_mailto("https://example.org/?mailto:x")); + assert_eq!(parse_mailto("C:\\mail\\mailto.eml"), None); + assert_eq!(parse_mailto("mailto:"), Some(Mailto::default())); + assert_eq!(parse_mailto("MAILTO:a@example.org#top").unwrap().to, s(&["a@example.org"])); + } + + #[test] + fn a_link_sets_nothing_but_recipients_subject_and_body() { + let m = + parse_mailto("mailto:a@example.org?from=boss@example.org&reply-to=x@evil.example&attach=C:%5Csecrets.txt&X-Mailer=m&subject=Hi").unwrap(); + assert_eq!(m, Mailto { to: s(&["a@example.org"]), subject: "Hi".into(), ..Default::default() }); + // No line break reaches a header, whatever the link encodes. + let m = parse_mailto( + "mailto:a@example.org%0D%0ABcc:%20x@evil.example?subject=Hi%0D%0ABcc:%20y@evil.example&cc=c@example.org%0Abcc:z@evil.example", + ) + .unwrap(); + assert!(m.bcc.is_empty()); + for text in m.to.iter().chain(&m.cc).chain([&m.subject]) { + assert!(!text.contains(['\r', '\n', '\0']), "{text:?}"); + } + // The first subject and body count. + let m = parse_mailto("mailto:?subject=one&subject=two&body=first&body=second").unwrap(); + assert_eq!((m.subject.as_str(), m.body.as_str()), ("one", "first")); + // A plus sign is not a space, a broken escape is what it looks like. + let m = parse_mailto("mailto:a+b@example.org?subject=1+1%3D2%&body=100%25%zz").unwrap(); + assert_eq!((m.to, m.subject.as_str(), m.body.as_str()), (s(&["a+b@example.org"]), "1+1=2%", "100%%zz")); + } + + #[test] + fn splits_recipient_lists_the_way_addresses_are_written() { + let m = parse_mailto("mailto:?cc=%22Rivera%2C%20Tom%C3%A1s%22%20%3Ctomas@example.org%3E,%20lin@example.org&bcc=a@example.org,,b@example.org") + .unwrap(); + assert_eq!(m.cc, s(&["\"Rivera, Tomás\" ", "lin@example.org"])); + assert_eq!(m.bcc, s(&["a@example.org", "b@example.org"])); + } + + #[test] + fn sizes_are_capped() { + let many = format!("mailto:{}", "a@example.org,".repeat(1000)); + assert_eq!(parse_mailto(&many).unwrap().to.len(), MAX_ADDRESSES); + let long = format!("mailto:a@example.org?subject={}&body={}", "é".repeat(5000), "x".repeat(200_000)); + let m = parse_mailto(&long).unwrap(); + assert!(m.subject.len() <= MAX_SUBJECT && m.subject.chars().all(|c| c == 'é')); + assert!(m.body.len() <= MAX_LINK); + } + + proptest! { + #[test] + fn any_text_is_read_without_trouble(text in ".{0,300}", tail in proptest::collection::vec(any::(), 0..300)) { + let _ = parse_mailto(&text); + let link = format!("mailto:{text}?{}", String::from_utf8_lossy(&tail)); + let m = parse_mailto(&link).unwrap(); + for line in m.to.iter().chain(&m.cc).chain(&m.bcc).chain([&m.subject]) { + prop_assert!(!line.chars().any(char::is_control)); + } + prop_assert!(!m.body.contains('\r')); + } + } +} From 7d4d2f46480057392db605ef0d2a23bcda84d5d5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:45:38 -0500 Subject: [PATCH 020/120] Add craft-ora: OpenRaster archives and stack.xml, read and written, from PhotoCraft (#935) The format half of PhotoCraft's OpenRaster import and export (crates/io/src/ora.rs at abeb16c96): the archive and its mimetype, stack.xml as a tree of layers and stacks with the attributes the file has, the 0.0.6 group isolation rule, and a writer. PNG coding, blend-mode mapping, rendering and warnings stay in the app. stack.xml is read with craft-xml and the archive with the zip crate. --- Cargo.lock | 10 + Cargo.toml | 1 + README.md | 1 + crates/ora/Cargo.toml | 20 ++ crates/ora/README.md | 37 ++++ crates/ora/src/lib.rs | 425 ++++++++++++++++++++++++++++++++++++++++ crates/ora/src/tests.rs | 303 ++++++++++++++++++++++++++++ 7 files changed, 797 insertions(+) create mode 100644 crates/ora/Cargo.toml create mode 100644 crates/ora/README.md create mode 100644 crates/ora/src/lib.rs create mode 100644 crates/ora/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 8121690c4..e1a798260 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1305,6 +1305,16 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-ora" +version = "0.1.0" +dependencies = [ + "craft-xml", + "flate2", + "thiserror 2.0.21", + "zip", +] + [[package]] name = "craft-pcraft-format" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index dbbaa40e4..65f501457 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,7 @@ craft-menu = { path = "crates/menu" } craft-metafile = { path = "crates/metafile" } craft-offcrypto = { path = "crates/offcrypto" } craft-opc = { path = "crates/opc" } +craft-ora = { path = "crates/ora" } craft-penpot = { path = "crates/penpot" } craft-pcraft-format = { path = "crates/pcraft-format" } craft-plotter = { path = "crates/plotter" } diff --git a/README.md b/README.md index 37f11b033..c1ab7967e 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = ", `stack.xml` 0.0.x). + +An OpenRaster document is a ZIP whose first, stored entry is `mimetype` = `image/openraster`. It +holds `stack.xml` (the layer tree), one PNG per layer, `mergedimage.png` (the whole image) and +`Thumbnails/thumbnail.png`. + +- `is_ora`, `zip_mimetype`: recognise an archive by its first entry, without unzipping. +- `Archive::open`: checks the mimetype and reads `stack.xml` into an `Image`: `Node::Layer`, + `Node::Stack` and `Node::Other`, topmost first, with the attributes the file has (`name`, + `visibility`, `opacity`, `composite-op`, `edit-locked`, `alpha-preserve`, `x`, `y`, `src`, + `isolation`). `Archive::entry` reads a layer image or any other entry under a size limit. +- `Image::explicit_group_isolation`: whether the file's version (0.0.6 and later) makes + `isolation` explicit, or leaves pass-through to be inferred as older files do. +- `Image::to_xml` and `Writer`: write `stack.xml` and the archive, mimetype first, entries + stored. + +Layer images are PNG; decoding and encoding them is the caller's, as is what a `composite-op` +means. `SVG_COMPOSITE_OPS` lists the names the specification defines. + +## Limits + +| Limit | Value | +| --- | --- | +| Canvas side | 300,000 px (`MAX_SIDE`); layer offsets up to four times that | +| Canvas area | 1 GiB at 8 bytes per pixel (`MAX_PIXEL_BYTES`) | +| Elements under the root stack | 8192 (`MAX_NODES`) | +| `stack.xml` | 16 MiB, 512 levels of nesting, no document type declaration | +| An entry | the caller's limit; `MAX_ENTRY` (512 MiB) is the suggested one for a layer image | + +## ZIP + +The archive is read and written with the `zip` crate directly. `zip_mimetype` (the leading stored +`mimetype` entry), the bounded entry read and the mimetype-first writer are the same in every +mimetype-first ZIP format and belong in a shared ZIP crate once there is one. diff --git a/crates/ora/src/lib.rs b/crates/ora/src/lib.rs new file mode 100644 index 000000000..cd7258a3a --- /dev/null +++ b/crates/ora/src/lib.rs @@ -0,0 +1,425 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +//! OpenRaster (`.ora`) read and write. Layout from the public OpenRaster specification +//! (, `stack.xml` 0.0.x): a ZIP whose first, stored entry is +//! `mimetype` = `image/openraster`, a `stack.xml` layer tree, one PNG per layer, and a +//! `mergedimage.png` / `Thumbnails/thumbnail.png` rendering. +//! +//! - [`Archive::open`] checks the mimetype and reads `stack.xml` into an [`Image`]: the tree of +//! [`Node`]s with the attributes the file has. [`Archive::entry`] reads a layer's image. +//! - [`Writer`] builds an archive: the mimetype first, then the entries it is given and the +//! `stack.xml` of an [`Image`]. +//! +//! Layer images are PNG files; decoding and encoding them is the caller's. So is what a +//! composite operation means: `composite-op` is kept as written ([`SVG_COMPOSITE_OPS`] lists the +//! specification's names). + +#[cfg(test)] +mod tests; + +use std::io::{Cursor, Read, Write}; + +use craft_xml::El; + +/// Why an OpenRaster document was not read or written. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The file is not an OpenRaster archive, is damaged, or exceeds a limit. + #[error("invalid OpenRaster document: {0}")] + Invalid(String), + /// The archive could not be written. + #[error("OpenRaster archive not written: {0}")] + Write(String), +} + +pub type Result = std::result::Result; + +fn invalid(message: impl Into) -> Error { + Error::Invalid(message.into()) +} + +/// The `mimetype` entry of an OpenRaster archive. +pub const MIMETYPE: &[u8] = b"image/openraster"; +/// The `stack.xml` version [`Image::to_xml`] writes when the image names none. +pub const VERSION: &str = "0.0.6"; +/// Canvas side limit; layer offsets may be up to four times this. +pub const MAX_SIDE: u32 = 300_000; +/// The canvas may hold at most this many bytes at 8 bytes per pixel (16-bit RGBA). +pub const MAX_PIXEL_BYTES: usize = 1 << 30; +/// Elements under the root stack: layers, stacks and unknown ones. +pub const MAX_NODES: usize = 8192; +/// Largest `stack.xml`. +pub const MAX_STACK_XML: usize = 16 << 20; +/// Largest layer image file a reader should ask [`Archive::entry`] for. +pub const MAX_ENTRY: usize = 512 << 20; +/// Deepest element nesting of `stack.xml`. +pub const MAX_XML_DEPTH: usize = 512; + +/// The compositing operations the specification names for `composite-op`. Applications also +/// write `svg:plus`, `svg:exclusion` and their own (`krita:`). +pub const SVG_COMPOSITE_OPS: [&str; 15] = [ + "svg:src-over", + "svg:multiply", + "svg:screen", + "svg:overlay", + "svg:darken", + "svg:lighten", + "svg:color-dodge", + "svg:color-burn", + "svg:hard-light", + "svg:soft-light", + "svg:difference", + "svg:color", + "svg:luminosity", + "svg:hue", + "svg:saturation", +]; + +/// The payload of a ZIP's first entry when it is a stored file named `mimetype` of at most 256 +/// bytes, which is how OpenRaster (and Krita, OpenDocument, EPUB) mark their archives so the +/// type is readable without unzipping. +pub fn zip_mimetype(bytes: &[u8]) -> Option<&[u8]> { + let u16_at = |o: usize| bytes.get(o..o + 2).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_le_bytes); + let u32_at = |o: usize| bytes.get(o..o + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).map(u32::from_le_bytes); + if u32_at(0)? != 0x0403_4b50 || u16_at(8)? != 0 { + return None; + } + let size = usize::try_from(u32_at(18)?).ok()?; + let name_len = usize::from(u16_at(26)?); + let extra_len = usize::from(u16_at(28)?); + if bytes.get(30..30 + name_len)? != b"mimetype" || size > 256 { + return None; + } + let start = 30 + name_len + extra_len; + bytes.get(start..start.checked_add(size)?) +} + +/// `true` for an OpenRaster archive (by its `mimetype` entry). +pub fn is_ora(bytes: &[u8]) -> bool { + zip_mimetype(bytes) == Some(MIMETYPE) +} + +/// The attributes a `` and a `` share. +#[derive(Clone, Debug, PartialEq)] +pub struct Props { + /// `name`, exactly as written (surrounding whitespace kept). + pub name: Option, + /// `visibility`: everything but `hidden` is visible. + pub visible: bool, + /// `opacity`, as stored (not clamped). 1 when missing. + pub opacity: f32, + /// `composite-op`, trimmed. Missing means `svg:src-over`. + pub composite_op: Option, + /// `edit-locked`. + pub edit_locked: bool, + /// `alpha-preserve`. + pub alpha_preserve: bool, +} + +impl Default for Props { + fn default() -> Props { + Props { name: None, visible: true, opacity: 1.0, composite_op: None, edit_locked: false, alpha_preserve: false } + } +} + +/// A ``: one image at a position. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Layer { + pub props: Props, + /// `x`, `y`: where the image's top-left pixel sits (fractions are rounded). + pub x: i32, + pub y: i32, + /// `src`: the archive entry of the layer's image, trimmed. + pub src: Option, +} + +/// A ``: a group. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Stack { + pub props: Props, + /// `isolation`, trimmed: `isolate` or `auto`. See [`Image::explicit_group_isolation`]. + pub isolation: Option, + /// The children, topmost first as the file lists them. + pub children: Vec, +} + +/// An element of a stack. +#[derive(Clone, Debug, PartialEq)] +pub enum Node { + Layer(Layer), + Stack(Stack), + /// An element that is neither (``, ...): its tag and `name` attribute. Not written. + Other { + tag: String, + name: Option, + }, +} + +/// The `` root of `stack.xml`. +#[derive(Clone, Debug, PartialEq)] +pub struct Image { + /// `version`, as written. + pub version: Option, + /// Canvas size in pixels (`w`, `h`), 1 to [`MAX_SIDE`]. + pub width: u32, + pub height: u32, + /// `xres`, `yres` in pixels per inch: 72 when `xres` is missing, `yres` defaults to `xres`. + pub xres: f64, + pub yres: f64, + /// The children of the root ``, topmost first. The root stack's own attributes are + /// not read. + pub stack: Vec, +} + +/// A number attribute; a missing one is `default`, an unparsable one an error. +fn num(el: &El, name: &str, default: f64) -> Result { + match el.attr(name) { + None => Ok(default), + Some(v) => v.trim().parse::().ok().filter(|v| v.is_finite()).ok_or_else(|| invalid(format!("invalid {name} attribute `{v}`"))), + } +} + +fn int(el: &El, name: &str) -> Result { + let v = num(el, name, 0.0)?.round(); + if v.abs() > f64::from(MAX_SIDE) * 4.0 { + return Err(invalid(format!("{name} offset {v} is out of range"))); + } + Ok(v as i32) +} + +fn flag(el: &El, name: &str) -> bool { + el.attr(name).is_some_and(|v| matches!(v.trim(), "true" | "1")) +} + +fn props(el: &El) -> Result { + Ok(Props { + name: el.attr("name").map(str::to_string), + visible: el.attr("visibility").is_none_or(|v| v.trim() != "hidden"), + opacity: num(el, "opacity", 1.0)? as f32, + composite_op: el.attr("composite-op").map(|v| v.trim().to_string()), + edit_locked: flag(el, "edit-locked"), + alpha_preserve: flag(el, "alpha-preserve"), + }) +} + +/// The elements of `stack`. Recursion is bounded by [`MAX_XML_DEPTH`]. +fn stack(el: &El, count: &mut usize) -> Result> { + let mut out = Vec::new(); + for child in el.elements() { + *count += 1; + if *count > MAX_NODES { + return Err(invalid(format!("more than {MAX_NODES} layers"))); + } + out.push(match child.name.as_str() { + "layer" => Node::Layer(Layer { + props: props(child)?, + x: int(child, "x")?, + y: int(child, "y")?, + src: child.attr("src").map(|s| s.trim().to_string()), + }), + "stack" => Node::Stack(Stack { + props: props(child)?, + isolation: child.attr("isolation").map(|s| s.trim().to_string()), + children: stack(child, count)?, + }), + other => Node::Other { tag: other.to_string(), name: child.attr("name").map(str::to_string) }, + }); + } + Ok(out) +} + +impl Image { + /// Reads `stack.xml`. + pub fn parse(xml: &[u8]) -> Result { + if std::str::from_utf8(xml).is_err() { + return Err(invalid("stack.xml is not UTF-8")); + } + let options = craft_xml::Options { + doctype: craft_xml::Doctype::Reject, + limits: craft_xml::Limits { max_input: MAX_STACK_XML, max_depth: MAX_XML_DEPTH, ..craft_xml::Limits::DEFAULT }, + ..craft_xml::Options::local().strict() + }; + let image = options.parse(xml).map_err(|e| invalid(format!("stack.xml: {e}")))?; + if image.name != "image" { + return Err(invalid("stack.xml has no root")); + } + let side = |attr: &str| -> Result { + let v = num(&image, attr, 0.0)?; + if !(1.0..=f64::from(MAX_SIDE)).contains(&v) || v.fract() != 0.0 { + return Err(invalid(format!("image {attr} {v} must be a whole number of pixels from 1 to {MAX_SIDE}"))); + } + Ok(v as u32) + }; + let (width, height) = (side("w")?, side("h")?); + if (width as usize).saturating_mul(height as usize).saturating_mul(8) > MAX_PIXEL_BYTES { + return Err(invalid("the canvas exceeds the 1 GiB pixel limit")); + } + let root = image.child("stack").ok_or_else(|| invalid("stack.xml has no root "))?; + let nodes = stack(root, &mut 0)?; + let xres = num(&image, "xres", 72.0)?; + let yres = num(&image, "yres", xres)?; + Ok(Image { version: image.attr("version").map(str::to_string), width, height, xres, yres, stack: nodes }) + } + + /// Does the file's version make group isolation explicit? OpenRaster 0.0.6 defaults to + /// isolated groups and makes `isolation="auto"` pass-through whatever the group's + /// `composite-op`. Older files (and ones with no or an unrecognised version) leave it to the + /// reader, by convention: a group passes through unless it says `isolate` or has a + /// composite operation other than `svg:src-over`. The numeric core is compared, not the + /// version string. + pub fn explicit_group_isolation(&self) -> bool { + let Some(core) = self.version.as_deref().and_then(|v| v.trim().split(['-', '+']).next()) else { return false }; + let mut parts = core.split('.'); + let number = |part: Option<&str>| part.and_then(|s| s.parse::().ok()); + match (number(parts.next()), number(parts.next()), number(parts.next()), parts.next()) { + (Some(major), Some(minor), Some(patch), None) => [major, minor, patch] >= [0, 0, 6], + _ => false, + } + } + + /// `stack.xml` for this image: `version` (or [`VERSION`]), the canvas, the resolution, and + /// the layers and stacks with every attribute of [`Props`]. A stack with no `isolation` + /// is written `isolate`; [`Node::Other`] is left out. + pub fn to_xml(&self) -> String { + let mut xml = format!( + "\n", + craft_xml::esc_attr(self.version.as_deref().unwrap_or(VERSION)), + self.width, + self.height, + self.xres, + self.yres + ); + // Iterative, so a tree built by hand may be as deep as its builder made it. + let mut open: Vec> = vec![self.stack.iter()]; + while let Some(level) = open.last_mut() { + match level.next() { + Some(Node::Layer(l)) => { + xml.push_str("", l.x, l.y)); + } + Some(Node::Stack(s)) => { + xml.push_str("", craft_xml::esc_attr(s.isolation.as_deref().unwrap_or("isolate")))); + open.push(s.children.iter()); + } + Some(Node::Other { .. }) => {} + None => { + open.pop(); + xml.push_str(if open.is_empty() { "\n" } else { "" }); + } + } + } + xml + } +} + +fn push_props(xml: &mut String, p: &Props) { + if let Some(name) = &p.name { + xml.push_str(&format!(" name=\"{}\"", craft_xml::esc_attr(name))); + } + xml.push_str(&format!(" visibility=\"{}\" opacity=\"{}\"", if p.visible { "visible" } else { "hidden" }, p.opacity)); + xml.push_str(&format!(" composite-op=\"{}\"", craft_xml::esc_attr(p.composite_op.as_deref().unwrap_or("svg:src-over")))); + if p.edit_locked { + xml.push_str(" edit-locked=\"true\""); + } + if p.alpha_preserve { + xml.push_str(" alpha-preserve=\"true\""); + } +} + +/// An open OpenRaster archive: the parsed `stack.xml` and access to the archive's entries. +pub struct Archive<'a> { + zip: zip::ZipArchive>, + image: Image, +} + +impl<'a> Archive<'a> { + /// Opens the archive, checks its `mimetype` and reads `stack.xml`. + pub fn open(bytes: &'a [u8]) -> Result> { + let mut zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| invalid(e.to_string()))?; + let mime = read(&mut zip, "mimetype", 256)?.ok_or_else(|| invalid("the archive has no mimetype"))?; + if mime.trim_ascii() != MIMETYPE { + return Err(invalid("the archive's mimetype is not image/openraster")); + } + let xml = read(&mut zip, "stack.xml", MAX_STACK_XML)?.ok_or_else(|| invalid("the archive has no stack.xml"))?; + Ok(Archive { zip, image: Image::parse(&xml)? }) + } + + /// The document's ``. + pub fn image(&self) -> &Image { + &self.image + } + + /// An entry by name (a layer's `src`, `mergedimage.png`, `Thumbnails/thumbnail.png`), + /// refusing one that inflates past `max` bytes ([`MAX_ENTRY`] for a layer image). `None` + /// when the archive has no such entry. + pub fn entry(&mut self, name: &str, max: usize) -> Result>> { + read(&mut self.zip, name, max) + } +} + +fn read(zip: &mut zip::ZipArchive>, name: &str, max: usize) -> Result>> { + let Some(index) = zip.index_for_name(name) else { return Ok(None) }; + let file = zip.by_index(index).map_err(|e| invalid(format!("{name}: {e}")))?; + let too_large = || invalid(format!("{name}: larger than {max} bytes")); + if file.size() > max as u64 { + return Err(too_large()); + } + // The declared size is the archive's claim; the read is what is bounded. + let mut out = Vec::new(); + file.take(max as u64 + 1).read_to_end(&mut out).map_err(|e| invalid(format!("{name}: {e}")))?; + if out.len() > max { + return Err(too_large()); + } + Ok(Some(out)) +} + +/// Writes an OpenRaster archive. Entries are stored (PNG is already compressed), in the order +/// they are added, after the `mimetype`. +pub struct Writer { + zip: zip::ZipWriter>>, + failed: Option, +} + +impl Default for Writer { + fn default() -> Writer { + Writer::new() + } +} + +impl Writer { + /// An archive holding the `mimetype`: it must be the first entry, stored, so the type is + /// readable at a fixed offset. + pub fn new() -> Writer { + let mut w = Writer { zip: zip::ZipWriter::new(Cursor::new(Vec::new())), failed: None }; + w.failed = w.add("mimetype", MIMETYPE).err(); + w + } + + /// Adds an entry: a layer image under the name its [`Layer::src`] gives, `mergedimage.png` + /// (the whole image, which the specification requires) or `Thumbnails/thumbnail.png` (at + /// most 256 pixels a side, also required). + pub fn add(&mut self, name: &str, data: &[u8]) -> Result<()> { + let stored = + zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored).large_file(data.len() > u32::MAX as usize); + self.zip.start_file(name, stored).map_err(|e| Error::Write(format!("{name}: {e}")))?; + self.zip.write_all(data).map_err(|e| Error::Write(format!("{name}: {e}"))) + } + + /// Adds `stack.xml` for `image`. + pub fn add_stack(&mut self, image: &Image) -> Result<()> { + self.add("stack.xml", image.to_xml().as_bytes()) + } + + /// The finished archive. + pub fn finish(self) -> Result> { + if let Some(e) = self.failed { + return Err(e); + } + self.zip.finish().map(Cursor::into_inner).map_err(|e| Error::Write(e.to_string())) + } +} diff --git a/crates/ora/src/tests.rs b/crates/ora/src/tests.rs new file mode 100644 index 000000000..490084441 --- /dev/null +++ b/crates/ora/src/tests.rs @@ -0,0 +1,303 @@ +use super::*; + +fn zip_of(files: &[(&str, &[u8])], deflate: bool) -> Vec { + let mut z = zip::ZipWriter::new(Cursor::new(Vec::new())); + let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); + let deflated = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); + for (i, (name, data)) in files.iter().enumerate() { + z.start_file(*name, if deflate && i > 0 { deflated } else { stored }).unwrap(); + z.write_all(data).unwrap(); + } + z.finish().unwrap().into_inner() +} + +/// An ORA archive from a hand-written stack.xml and named entries. +fn archive(stack: &str, images: &[(&str, Vec)]) -> Vec { + let mut all: Vec<(&str, &[u8])> = vec![("mimetype", MIMETYPE), ("stack.xml", stack.as_bytes())]; + all.extend(images.iter().map(|(n, d)| (*n, d.as_slice()))); + zip_of(&all, false) +} + +fn image(stack: &str) -> Result { + Archive::open(&archive(stack, &[])).map(|a| a.image().clone()) +} + +fn layer(node: &Node) -> &Layer { + match node { + Node::Layer(l) => l, + other => panic!("not a layer: {other:?}"), + } +} + +fn group(node: &Node) -> &Stack { + match node { + Node::Stack(s) => s, + other => panic!("not a stack: {other:?}"), + } +} + +#[test] +fn reads_other_applications_stacks() { + // The shapes Krita, MyPaint and GIMP write: a root stack with attributes, `auto` + // isolation, svg:plus, krita: ids, unknown elements, fractional positions. + let xml = r#" + + + + + + + + hello + + + + +"#; + let bytes = archive(xml, &[("data/a.png", b"png a".to_vec()), ("data/b.png", b"png b".to_vec())]); + assert!(is_ora(&bytes)); + let mut a = Archive::open(&bytes).unwrap(); + let image = a.image().clone(); + assert_eq!((image.version.as_deref(), image.width, image.height, image.xres, image.yres), (Some("0.0.3"), 8, 6, 150.0, 151.0)); + assert!(!image.explicit_group_isolation()); + assert_eq!(image.stack.len(), 7, "topmost first, unknown elements kept in place"); + let plus = layer(&image.stack[0]); + assert_eq!((plus.props.name.as_deref(), plus.x, plus.y, plus.src.as_deref()), (Some("Plus"), 2, -2, Some("data/a.png"))); + assert_eq!((plus.props.composite_op.as_deref(), plus.props.opacity, plus.props.visible), (Some("svg:plus"), 0.5, true)); + assert_eq!(a.entry("data/a.png", MAX_ENTRY).unwrap().as_deref(), Some(&b"png a"[..])); + let folder = group(&image.stack[1]); + assert_eq!( + (folder.isolation.as_deref(), folder.props.composite_op.as_deref(), folder.props.opacity), + (Some("auto"), Some("krita:darker color"), 1.5) + ); + let inside = layer(&folder.children[0]); + assert!(!inside.props.visible && inside.props.alpha_preserve && !inside.props.edit_locked); + let through = group(&image.stack[2]); + assert_eq!( + (through.props.name.as_deref(), through.isolation.as_deref(), through.props.composite_op.as_deref()), + (Some(" Through "), None, None) + ); + assert_eq!(layer(&through.children[0]).props, Props::default()); + assert_eq!(image.stack[3], Node::Other { tag: "text".into(), name: Some("Caption".into()) }); + assert!(layer(&image.stack[4]).props.edit_locked); + let lost = layer(&image.stack[5]); + assert_eq!(a.entry(lost.src.as_deref().unwrap(), MAX_ENTRY).unwrap(), None, "a layer may name an entry the archive lacks"); + assert_eq!(layer(&image.stack[6]), &Layer::default()); + assert!(a.entry("data/b.png", 4).is_err(), "an entry past the caller's limit is refused"); +} + +#[test] +fn resolution_defaults_to_72_and_yres_to_xres() { + let i = image("").unwrap(); + assert_eq!((i.xres, i.yres, i.version), (72.0, 72.0, None)); + let i = image("").unwrap(); + assert_eq!((i.xres, i.yres), (300.0, 300.0)); +} + +#[test] +fn group_isolation_is_explicit_since_version_0_0_6() { + let explicit = |v: Option<&str>| { + Image { version: v.map(str::to_string), width: 1, height: 1, xres: 72.0, yres: 72.0, stack: vec![] }.explicit_group_isolation() + }; + for v in ["0.0.6", " 0.0.6 ", "0.0.7", "0.1.0", "1.0.0", "0.0.6-beta", "0.0.10+build"] { + assert!(explicit(Some(v)), "{v}"); + } + for v in ["0.0.5", "0.0.3", "0.0", "0.0.6.1", "six", "", "0.0.x"] { + assert!(!explicit(Some(v)), "{v}"); + } + assert!(!explicit(None)); +} + +#[test] +fn a_written_archive_reads_back_the_same() { + let p = |name: &str, op: Option<&str>| Props { name: Some(name.into()), composite_op: op.map(str::to_string), ..Props::default() }; + let image = Image { + version: None, + width: 70, + height: 50, + xres: 300.0, + yres: 300.0, + stack: vec![ + Node::Layer(Layer { + props: Props { opacity: 0.25, visible: false, ..p(" Top\t&\n<\"'> \u{1} ", Some("svg:multiply")) }, + x: -3, + y: 7, + src: Some("data/layer0.png".into()), + }), + Node::Stack(Stack { + props: Props { edit_locked: true, alpha_preserve: true, ..p("Pass <&> \"through\"", Some("krita:linear light")) }, + isolation: Some("auto".into()), + children: vec![ + Node::Layer(Layer { props: p("Inner", None), x: 0, y: 0, src: Some("data/layer1.png".into()) }), + Node::Stack(Stack::default()), + ], + }), + Node::Other { tag: "text".into(), name: None }, + ], + }; + let mut w = Writer::new(); + w.add("data/layer0.png", b"zero").unwrap(); + w.add("data/layer1.png", b"one").unwrap(); + w.add_stack(&image).unwrap(); + w.add("mergedimage.png", b"merged").unwrap(); + w.add("Thumbnails/thumbnail.png", b"thumb").unwrap(); + let bytes = w.finish().unwrap(); + // A stored `mimetype` first, readable at a fixed offset. + assert_eq!(bytes.get(30..38), Some(&b"mimetype"[..])); + assert_eq!(bytes.get(38..54), Some(MIMETYPE)); + assert!(is_ora(&bytes)); + let mut a = Archive::open(&bytes).unwrap(); + for (entry, data) in + [("data/layer0.png", "zero"), ("data/layer1.png", "one"), ("mergedimage.png", "merged"), ("Thumbnails/thumbnail.png", "thumb")] + { + assert_eq!(a.entry(entry, 16).unwrap().as_deref(), Some(data.as_bytes()), "{entry}"); + } + let xml = String::from_utf8(a.entry("stack.xml", 1 << 20).unwrap().unwrap()).unwrap(); + assert!( + xml.starts_with( + "\n"), + "{xml}" + ); + assert!(xml.contains("composite-op=\"krita:linear light\" edit-locked=\"true\" alpha-preserve=\"true\" isolation=\"auto\">"), "{xml}"); + assert!( + xml.ends_with( + "\n" + ), + "{xml}" + ); + // What comes back: the version and the defaults written out, the unknown element gone, and + // the control character XML cannot carry dropped from the name. Tab and newline survive. + let back = a.image(); + assert!(back.explicit_group_isolation()); + assert_eq!((back.version.as_deref(), back.width, back.height, back.xres, back.yres, back.stack.len()), (Some(VERSION), 70, 50, 300.0, 300.0, 2)); + let top = layer(&back.stack[0]); + assert_eq!(top.props.name.as_deref(), Some(" Top\t&\n<\"'> ")); + assert_eq!((top.props.opacity, top.props.visible, top.x, top.y), (0.25, false, -3, 7)); + let pass = group(&back.stack[1]); + assert_eq!((pass.props.name.as_deref(), pass.isolation.as_deref()), (Some("Pass <&> \"through\""), Some("auto"))); + assert_eq!(layer(&pass.children[0]).props.composite_op.as_deref(), Some("svg:src-over")); + assert_eq!(group(&pass.children[1]).isolation.as_deref(), Some("isolate")); + assert_eq!(back.to_xml(), xml, "writing what was read is stable"); +} + +#[test] +fn a_stack_built_ten_thousand_deep_is_written_without_recursion() { + let mut node = Stack::default(); + for _ in 0..10_000 { + node = Stack { children: vec![Node::Stack(node)], ..Stack::default() }; + } + let image = Image { version: None, width: 1, height: 1, xres: 72.0, yres: 72.0, stack: vec![Node::Stack(node)] }; + let xml = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || { + let xml = image.to_xml(); + std::mem::forget(image); // dropping a hand-built tree recurses; that is the builder's concern + xml + }); + assert_eq!(xml.unwrap().join().unwrap().matches("").count(), 10_002); +} + +/// A stack.xml nested 20 000 deep is an error. The small stack makes a regression abort instead +/// of passing. +#[test] +fn deeply_nested_stack_xml_is_an_error_not_a_stack_overflow() { + let stack = format!("{}{}", "".repeat(20_000), "".repeat(20_000)); + let bytes = archive(&stack, &[]); + let e = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || Archive::open(&bytes).err()).unwrap().join().unwrap(); + assert!(matches!(&e, Some(Error::Invalid(m)) if m.contains("deeper")), "{e:?}"); + let nested = |n: usize| format!("{}{}", "".repeat(n), "".repeat(n)); + let bytes = archive(&nested(MAX_XML_DEPTH - 1), &[]); + let depth = std::thread::Builder::new() + .stack_size(512 * 1024) + .spawn(move || { + let a = Archive::open(&bytes).unwrap(); + std::iter::successors(a.image().stack.first(), |n| group(n).children.first()).count() + }) + .unwrap() + .join() + .unwrap(); + assert_eq!(depth, MAX_XML_DEPTH - 2); + assert!(image(&nested(MAX_XML_DEPTH)).is_err()); +} + +#[test] +fn malformed_archives_fail_cleanly() { + let cases: Vec<(&str, Vec)> = vec![ + ("not a zip", b"definitely not a zip archive at all".to_vec()), + ("no stack", zip_of(&[("mimetype", MIMETYPE)], false)), + ("no mimetype", zip_of(&[("stack.xml", b"")], false)), + ("wrong mimetype", zip_of(&[("mimetype", b"image/png"), ("stack.xml", b"")], false)), + ("bad xml", archive("", &[])), + ("doctype", archive("", &[])), + ("wrong root", archive("", &[])), + ("no root stack", archive("", &[])), + ("zero width", archive("", &[])), + ("fractional width", archive("", &[])), + ("too tall", archive("", &[])), + ("huge canvas", archive("", &[])), + ("bad resolution", archive("", &[])), + ("bad opacity", archive("", &[])), + ("nan opacity", archive("", &[])), + ("far offset", archive("", &[])), + ("just too far", archive("", &[])), + ("not utf-8", archive("", &[]).iter().map(|b| if *b == 0 { 0xff } else { *b }).collect()), + ("too many", archive(&format!("{}", "".repeat(MAX_NODES + 1)), &[])), + ]; + for (what, bytes) in cases { + assert!(matches!(Archive::open(&bytes), Err(Error::Invalid(_))), "{what}"); + } + assert!(image(&format!("{}", "".repeat(MAX_NODES))).is_ok()); + assert!(image("").is_ok()); +} + +#[test] +fn deflated_entries_are_read_under_the_limit() { + let big = vec![b' '; MAX_STACK_XML + 1]; + let bytes = zip_of(&[("mimetype", MIMETYPE), ("stack.xml", &big)], true); + assert!(bytes.len() < 1 << 20); + assert_eq!(Archive::open(&bytes).err(), Some(Error::Invalid(format!("stack.xml: larger than {MAX_STACK_XML} bytes")))); + let bytes = zip_of(&[("mimetype", MIMETYPE), ("stack.xml", b""), ("a.png", &[9; 70_000])], true); + let mut a = Archive::open(&bytes).unwrap(); + assert_eq!(a.entry("a.png", 70_000).unwrap(), Some(vec![9; 70_000])); + assert!(a.entry("a.png", 69_999).is_err()); +} + +#[test] +fn truncated_and_corrupted_files_never_panic() { + let xml = ""; + let bytes = zip_of(&[("mimetype", MIMETYPE), ("stack.xml", xml.as_bytes()), ("a.png", &[1; 300]), ("b.png", &[2; 300])], true); + let read_all = |bytes: &[u8]| -> Result { + let mut a = Archive::open(bytes)?; + Ok(a.entry("a.png", MAX_ENTRY)?.map_or(0, |d| d.len()) + a.entry("b.png", MAX_ENTRY)?.map_or(0, |d| d.len())) + }; + assert_eq!(read_all(&bytes), Ok(600)); + for cut in 0..bytes.len() { + let _ = read_all(bytes.get(..cut).unwrap_or_default()); + } + let mut state = 0x9e37_79b9_u32; + for _ in 0..300 { + let mut b = bytes.clone(); + for _ in 0..4 { + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + let i = state as usize % b.len(); + b[i] ^= (state >> 24) as u8 | 1; + } + let _ = read_all(&b); + } +} + +#[test] +fn zip_mimetype_reads_only_a_leading_stored_entry() { + assert_eq!(zip_mimetype(&archive("", &[])), Some(MIMETYPE)); + assert_eq!(zip_mimetype(b"PK\x03\x04"), None); + assert_eq!(zip_mimetype(&[]), None); + assert_eq!(zip_mimetype(&zip_of(&[("stack.xml", b"x"), ("mimetype", MIMETYPE)], false)), None); + assert!(!is_ora(&zip_of(&[("mimetype", b"application/x-krita")], false))); + assert_eq!(zip_mimetype(&zip_of(&[("mimetype", &[b'a'; 257])], false)), None); +} From 78800c234968ce690f00a557137b5058ea91afb5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:46:09 -0500 Subject: [PATCH 021/120] craft-captions: use craft-timeline's exact time instead of a private copy (#936) craft-captions carried a 290-line subset of the tick, frame rate and timecode field code because craft-libs had none when it was added. craft-timeline now has the whole module with the same tick unit. craft_captions::time is a re-export of craft_timeline::time, so every public path still resolves and the types are now the same ones craft-timeline's callers hold. --- Cargo.lock | 1 + crates/captions/Cargo.toml | 1 + crates/captions/README.md | 8 +- crates/captions/src/lib.rs | 3 +- crates/captions/src/time.rs | 291 ------------------------------------ 5 files changed, 9 insertions(+), 295 deletions(-) delete mode 100644 crates/captions/src/time.rs diff --git a/Cargo.lock b/Cargo.lock index e1a798260..c2dca8156 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -901,6 +901,7 @@ dependencies = [ name = "craft-captions" version = "0.1.0" dependencies = [ + "craft-timeline", "craft-xml", "proptest", "roxmltree 0.21.2", diff --git a/crates/captions/Cargo.toml b/crates/captions/Cargo.toml index 70293e2d3..df665336d 100644 --- a/crates/captions/Cargo.toml +++ b/crates/captions/Cargo.toml @@ -11,6 +11,7 @@ repository.workspace = true workspace = true [dependencies] +craft-timeline = { workspace = true } craft-xml = { workspace = true } thiserror = { workspace = true } diff --git a/crates/captions/README.md b/crates/captions/README.md index 6fd2a4005..e91972554 100644 --- a/crates/captions/README.md +++ b/crates/captions/README.md @@ -1,7 +1,8 @@ # craft-captions Caption and subtitle files behind one neutral model: bytes in, a `Document` of `Cue`s out, and -back. One module per format. Depends on `craft-xml` (TTML) and `thiserror`. +back. One module per format. Depends on `craft-timeline` (exact time), +`craft-xml` (TTML) and `thiserror`. ```rust use craft_captions::{Format, WriteOptions, detect, parse, write}; @@ -31,8 +32,9 @@ assert!(scc.starts_with(b"Scenarist_SCC V1.0")); `Tick` is an integer count of 1/254 016 000 000 s: a whole number of ticks per millisecond and per frame at every broadcast rate, so no format's times are rounded on the way in. `FrameRate` -is an exact rational. There are no floating-point seconds anywhere. An application with its own -time type converts at the boundary (`Tick(t).0` is the tick count). +is an exact rational. Both are `craft-timeline`'s (`craft_timeline::time`), re-exported here as +`craft_captions::time`, so a caller that already uses that crate passes its values straight in. +No format computes with floating-point seconds. ## Limits diff --git a/crates/captions/src/lib.rs b/crates/captions/src/lib.rs index 6b9da1d72..db543dbad 100644 --- a/crates/captions/src/lib.rs +++ b/crates/captions/src/lib.rs @@ -38,7 +38,8 @@ pub mod scc; pub mod srt; pub mod stl; mod text; -pub mod time; +/// Exact time: `craft-timeline`'s ticks, frame rates and timecode fields. +pub use craft_timeline::time; pub mod ttml; pub mod vtt; diff --git a/crates/captions/src/time.rs b/crates/captions/src/time.rs deleted file mode 100644 index 51bfdca78..000000000 --- a/crates/captions/src/time.rs +++ /dev/null @@ -1,291 +0,0 @@ -//! Exact caption time. -//! -//! All time is an integer number of [`Tick`]s at [`TICKS_PER_SECOND`] = 254 016 000 000/s. That -//! rate divides evenly into a millisecond and into every broadcast frame duration (23.976, 24, -//! 25, 29.97, 30, 50, 59.94, 60 …), so SRT/WebVTT milliseconds and SCC/MCC/STL/TTML frames all -//! convert without rounding. -//! -//! Every conversion is total: a zero or negative rate (from a damaged file) counts as 23.976 fps -//! instead of dividing by zero, and results outside `i64` saturate. - -use std::ops::{Add, AddAssign, Neg, Sub, SubAssign}; - -/// Ticks per second. -pub const TICKS_PER_SECOND: i64 = 254_016_000_000; - -/// A point or duration in time, in ticks. -/// Arithmetic saturates at the i64 storage limits; representable results remain exact. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Tick(pub i64); - -impl Tick { - pub const ZERO: Tick = Tick(0); - - /// Seconds as a float, for display only: nothing in this crate computes with it. - pub fn seconds(self) -> f64 { - self.0 as f64 / TICKS_PER_SECOND as f64 - } - pub fn min(self, o: Tick) -> Tick { - Tick(self.0.min(o.0)) - } - pub fn max(self, o: Tick) -> Tick { - Tick(self.0.max(o.0)) - } -} - -impl Add for Tick { - type Output = Tick; - fn add(self, o: Tick) -> Tick { - Tick(self.0.saturating_add(o.0)) - } -} -impl Sub for Tick { - type Output = Tick; - fn sub(self, o: Tick) -> Tick { - Tick(self.0.saturating_sub(o.0)) - } -} -impl Neg for Tick { - type Output = Tick; - fn neg(self) -> Tick { - Tick(self.0.saturating_neg()) - } -} -impl AddAssign for Tick { - fn add_assign(&mut self, o: Tick) { - self.0 = self.0.saturating_add(o.0); - } -} -impl SubAssign for Tick { - fn sub_assign(&mut self, o: Tick) { - self.0 = self.0.saturating_sub(o.0); - } -} - -/// A frame rate as an exact rational (`num / den` frames per second). -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct FrameRate { - pub num: i64, - pub den: i64, -} - -impl Default for FrameRate { - fn default() -> Self { - FrameRate::FPS_23_976 - } -} - -impl FrameRate { - pub const FPS_23_976: FrameRate = FrameRate { num: 24000, den: 1001 }; - pub const FPS_24: FrameRate = FrameRate { num: 24, den: 1 }; - pub const FPS_25: FrameRate = FrameRate { num: 25, den: 1 }; - pub const FPS_29_97: FrameRate = FrameRate { num: 30000, den: 1001 }; - pub const FPS_30: FrameRate = FrameRate { num: 30, den: 1 }; - pub const FPS_50: FrameRate = FrameRate { num: 50, den: 1 }; - pub const FPS_59_94: FrameRate = FrameRate { num: 60000, den: 1001 }; - pub const FPS_60: FrameRate = FrameRate { num: 60, den: 1 }; - - /// The rate in lowest terms. - pub fn new(num: i64, den: i64) -> Self { - let g = i128::from(gcd(num.unsigned_abs(), den.unsigned_abs()).max(1)); - FrameRate { num: (i128::from(num) / g) as i64, den: (i128::from(den) / g) as i64 } - } - - /// This rate if it is positive, else the default (a damaged file can carry a zero - /// or negative rate; every frame computation uses this so it can't divide by zero). - pub fn sane(self) -> FrameRate { - if self.num > 0 && self.den > 0 { self } else { FrameRate::default() } - } - - /// Exact duration of one frame (rounded down only for exotic rates). - /// Values outside the integer tick range saturate at its limit. - pub fn frame_duration(self) -> Tick { - let r = self.sane(); - Tick(clamp_i64((TICKS_PER_SECOND as i128 * r.den as i128) / r.num as i128)) - } - - /// Index of the frame containing `t` (floor), saturated to the integer frame range. - pub fn frame_at(self, t: Tick) -> i64 { - clamp_i64(frame_index(self.sane(), t)) - } - - /// Start tick of frame `f` (ceiling division). - /// - /// Uses ceiling (not floor) so that `frame_at(tick_of(f)) == f` holds for every - /// representable frame boundary with at least one tick per frame, even when the frame duration is not an integer number of ticks - /// (exotic rates like 37.516 fps where `TICKS_PER_SECOND * den / num` has a - /// fractional remainder). Floor division truncates the remainder, causing each - /// frame boundary to drift backward by one tick until `frame_at` returns `f - 1` - /// instead of `f` — frame stepping then stalls or moves backward. - /// Values outside the integer tick range saturate at its limit. - pub fn tick_of(self, f: i64) -> Tick { - Tick(clamp_i64(frame_boundary(self.sane(), i128::from(f)))) - } - - /// Snap `t` down to a frame boundary. - pub fn snap(self, t: Tick) -> Tick { - let r = self.sane(); - Tick(clamp_i64(frame_boundary(r, frame_index(r, t)))) - } - - /// Snap `t` to the nearest frame boundary. - pub fn snap_nearest(self, t: Tick) -> Tick { - let r = self.sane(); - // Keep the frame index and both boundaries wide until choosing the nearest. Saturating - // an unrepresentable upper boundary first would make it seem closer than it really is. - let f = frame_index(r, t); - let a = frame_boundary(r, f); - let b = frame_boundary(r, f.saturating_add(1)); - let t = i128::from(t.0); - Tick(clamp_i64(if t - a <= b - t { a } else { b })) - } - - /// Timecode base (frames counted per timecode second): 30 for 29.97, 24 for 23.976. - pub fn timecode_base(self) -> i64 { - let r = self.sane(); - (r.num / r.den).saturating_add(i64::from(r.num % r.den != 0)).max(1) - } - - /// NTSC (x/1001) rates can use drop-frame timecode. - pub fn is_ntsc(self) -> bool { - self.den == 1001 - } - - /// Whether drop-frame counting applies (29.97 / 59.94 / 119.88). - pub fn supports_drop_frame(self) -> bool { - self.is_ntsc() && self.timecode_base() % 30 == 0 - } -} - -/// Narrow only after applying the storage bounds, never by a wrapping integer cast. -fn clamp_i64(value: i128) -> i64 { - value.clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64 -} - -/// Callers pass a positive rate. Products of two i64 values fit in i128. -fn frame_index(rate: FrameRate, t: Tick) -> i128 { - (i128::from(t.0) * i128::from(rate.num)).div_euclid(i128::from(TICKS_PER_SECOND) * i128::from(rate.den)) -} - -/// Ceiling of the rational boundary. An overflowing numerator divided by an i64 rate cannot -/// fit in an i64 tick, so saturating that intermediate preserves the public result. Boundaries -/// derived from frame_index remain within i128 (at most one frame away from an i64 tick). -fn frame_boundary(rate: FrameRate, frame: i128) -> i128 { - let Some(n) = frame.checked_mul(i128::from(TICKS_PER_SECOND)).and_then(|n| n.checked_mul(i128::from(rate.den))) else { - return if frame < 0 { i128::MIN } else { i128::MAX }; - }; - let d = i128::from(rate.num); - // Flooring a fractional boundary puts the tick in the preceding frame. - n.div_euclid(d) + i128::from(n.rem_euclid(d) != 0) -} - -fn gcd(mut a: u64, mut b: u64) -> u64 { - while b != 0 { - (a, b) = (b, a % b); - } - a -} - -/// Drop-frame parameters: frames dropped per minute and nominal base. -fn df_params(rate: FrameRate) -> (i64, i64) { - let base = rate.timecode_base(); - (base / 15, base) // 30 → 2, 60 → 4, 120 → 8 -} - -/// Convert a frame count to SMPTE fields `(negative, h, m, s, f)`. -pub fn frames_to_fields(frame: i64, rate: FrameRate, drop_frame: bool) -> (bool, i64, i64, i64, i64) { - let neg = frame < 0; - let mut n = i128::from(frame).abs(); - let (drop, base) = df_params(rate); - let (drop, base) = (i128::from(drop), i128::from(base)); - if drop_frame && rate.supports_drop_frame() { - let per_min = base * 60 - drop; - let per_10 = per_min * 10 + drop; - let d = n / per_10; - let m = n % per_10; - n = n.saturating_add(drop * 9 * d); - if m > drop { - n = n.saturating_add(drop * ((m - drop) / per_min)); - } - } - let f = n % base; - let s = (n / base) % 60; - let mi = (n / (base * 60)) % 60; - let h = n / (base * 3600); - (neg, clamp_i64(h), clamp_i64(mi), clamp_i64(s), clamp_i64(f)) -} - -/// Convert SMPTE fields back to a frame count. -pub fn fields_to_frames(h: i64, m: i64, s: i64, f: i64, rate: FrameRate, drop_frame: bool) -> i64 { - let (drop, base) = df_params(rate); - let seconds = i128::from(h) * 3600 + i128::from(m) * 60 + i128::from(s); - // Nominal drop-frame labels can exceed i64 even when the final frame count fits. - let mut n = seconds.saturating_mul(i128::from(base)).saturating_add(i128::from(f)); - if drop_frame && rate.supports_drop_frame() { - let total_min = i128::from(h) * 60 + i128::from(m); - n = n.saturating_sub(i128::from(drop) * (total_min - total_min / 10)); - } - clamp_i64(n) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn broadcast_rates_and_milliseconds_are_exact() { - for r in [ - FrameRate::FPS_23_976, - FrameRate::FPS_24, - FrameRate::FPS_25, - FrameRate::FPS_29_97, - FrameRate::FPS_30, - FrameRate::FPS_50, - FrameRate::FPS_59_94, - FrameRate::FPS_60, - ] { - let d = r.frame_duration(); - assert_eq!(i128::from(d.0) * i128::from(r.num), i128::from(TICKS_PER_SECOND) * i128::from(r.den), "{r:?}"); - for f in [-1000, -1, 0, 1, 1799, 107_892, 10_000_000] { - assert_eq!(r.frame_at(r.tick_of(f)), f, "{r:?} {f}"); - } - } - assert_eq!(TICKS_PER_SECOND % 1000, 0); - } - - #[test] - fn drop_frame_known_values() { - let r = FrameRate::FPS_29_97; - let fields = |f, df| frames_to_fields(f, r, df); - assert_eq!(fields(1799, true), (false, 0, 0, 59, 29)); - assert_eq!(fields(1800, true), (false, 0, 1, 0, 2)); - assert_eq!(fields(17982, true), (false, 0, 10, 0, 0)); - assert_eq!(fields(107_892, true), (false, 1, 0, 0, 0)); - assert_eq!(fields(1800, false), (false, 0, 1, 0, 0)); - assert_eq!(frames_to_fields(3600, FrameRate::FPS_59_94, true), (false, 0, 1, 0, 4)); - for f in (0..24 * 107_892).step_by(997) { - let (_, h, m, s, ff) = fields(f, true); - assert_eq!(fields_to_frames(h, m, s, ff, r, true), f); - } - } - - /// A damaged file can carry a zero or negative rate or a huge time: nothing divides by zero - /// or wraps. - #[test] - fn hostile_rates_and_times_saturate() { - for r in [FrameRate { num: 0, den: 0 }, FrameRate { num: 0, den: 1 }, FrameRate { num: 25, den: 0 }, FrameRate { num: -30, den: 1 }] { - assert_eq!(r.tick_of(10), FrameRate::default().tick_of(10)); - assert_eq!(r.frame_at(Tick(TICKS_PER_SECOND)), FrameRate::default().frame_at(Tick(TICKS_PER_SECOND))); - assert!(r.frame_duration() > Tick::ZERO); - assert!(r.timecode_base() >= 1); - let _ = r.snap_nearest(Tick(12345)); - } - assert_eq!(Tick(i64::MAX) + Tick(1), Tick(i64::MAX)); - assert_eq!(Tick(i64::MIN) - Tick(1), Tick(i64::MIN)); - assert_eq!(-Tick(i64::MIN), Tick(i64::MAX)); - assert_eq!(FrameRate::FPS_25.tick_of(i64::MAX), Tick(i64::MAX)); - assert_eq!(FrameRate::new(i64::MAX, 1).frame_at(Tick(i64::MAX)), i64::MAX); - let _ = frames_to_fields(i64::MIN, FrameRate::FPS_29_97, true); - let _ = fields_to_frames(i64::MAX, i64::MAX, i64::MAX, i64::MAX, FrameRate::FPS_29_97, true); - } -} From 43f65a9210218d035b9400b3078acf15d3123dca Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:46:38 -0500 Subject: [PATCH 022/120] craft-svg: usvg behind a bounded `tree` feature; attribute guard; Inkscape layer names (#937) * craft-svg paths: a shape carries the name of its Inkscape layer Shape::layer is the inkscape:label (else the id) of the innermost around the element, so a CAD or cutter import can keep the drawing's layers. * craft-svg guard: bound attributes per element and over the document; find entities holding markup too_wide is the attribute check of storytold/figcraft#2251: an XML reader checks each attribute against the earlier ones on its element, so an element with millions of them is quadratic work before any other limit applies. entity_holds_markup lets a caller refuse a document type whose entities become elements, as PhotoCraft does. * craft-svg: feature `tree`, usvg behind one bounded entry point usvg 0.48.1 (Apache-2.0 OR MIT), re-exported as craft_svg::tree::usvg, so every app parses with one version and one set of checks: tree::parse runs svgz, the entity, attribute and nesting guards, the reference-chain height (VectorCraft's depth::height, on this crate's cascade), then usvg. tree::image_resolver bounds embedded SVGs (FigCraft's budget::resolver). tree::draw bounds what drawing a tree with resvg allocates (PhotoCraft's svg/limits.rs). Part of #778 and #789. --- Cargo.lock | 77 ++++++- README.md | 5 +- crates/svg/Cargo.toml | 11 + crates/svg/src/guard.rs | 116 +++++++++- crates/svg/src/lib.rs | 4 + crates/svg/src/paths.rs | 54 ++++- crates/svg/src/tree.rs | 327 ++++++++++++++++++++++++++ crates/svg/src/tree/draw.rs | 431 +++++++++++++++++++++++++++++++++++ crates/svg/src/tree/tests.rs | 304 ++++++++++++++++++++++++ 9 files changed, 1320 insertions(+), 9 deletions(-) create mode 100644 crates/svg/src/tree.rs create mode 100644 crates/svg/src/tree/draw.rs create mode 100644 crates/svg/src/tree/tests.rs diff --git a/Cargo.lock b/Cargo.lock index c2dca8156..21909a8af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1528,6 +1528,7 @@ dependencies = [ "kurbo 0.13.1", "roxmltree 0.21.2", "thiserror 2.0.21", + "usvg 0.48.1", ] [[package]] @@ -2453,6 +2454,27 @@ dependencies = [ "serde", ] +[[package]] +name = "fontconfig-parser" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc773e24e02d4ddd8395fd30dc147524273a83e54e0f312d986ea30de5f5646" +dependencies = [ + "roxmltree 0.20.0", +] + +[[package]] +name = "fontdb" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2660c5e9157bf76d2db1294e4a9feba604ef610819a3b591088d0d8392a3290f" +dependencies = [ + "fontconfig-parser", + "log", + "slotmap", + "tinyvec", +] + [[package]] name = "fontique" version = "0.11.1" @@ -3428,6 +3450,12 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09e54e57b4c48b40f7aec75635392b12b3421fa26fe8b4332e63138ed278459c" +[[package]] +name = "imagesize" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" + [[package]] name = "indexmap" version = "2.14.2" @@ -5359,9 +5387,9 @@ dependencies = [ "log", "pico-args", "rgb", - "svgtypes", + "svgtypes 0.15.3", "tiny-skia", - "usvg", + "usvg 0.45.1", ] [[package]] @@ -6230,6 +6258,16 @@ dependencies = [ "siphasher", ] +[[package]] +name = "svgtypes" +version = "0.16.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f12b12995e3638c70618c1facb1ce65302f6a6f706b675f07c58a5d7391120e" +dependencies = [ + "kurbo 0.13.1", + "siphasher", +] + [[package]] name = "symphonia" version = "0.6.1" @@ -6988,6 +7026,12 @@ version = "1.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" +[[package]] +name = "unicode-vo" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d386ff53b415b7fe27b50bb44679e2cc4660272694b7b6f3326d8480823a94" + [[package]] name = "unicode-width" version = "0.2.2" @@ -7065,11 +7109,38 @@ dependencies = [ "simplecss", "siphasher", "strict-num", - "svgtypes", + "svgtypes 0.15.3", "tiny-skia-path 0.11.4", "xmlwriter", ] +[[package]] +name = "usvg" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "977d0a4abdef933f424a99fe09f95576e089b90aebc6f016a3bc813762493e91" +dependencies = [ + "base64 0.23.1", + "data-url", + "fontdb", + "harfrust", + "imagesize 0.15.0", + "kurbo 0.13.1", + "log", + "pico-args", + "roxmltree 0.21.2", + "simplecss", + "siphasher", + "skrifa 0.44.0", + "strict-num", + "svgtypes 0.16.2", + "tiny-skia-path 0.12.0", + "unicode-bidi", + "unicode-script", + "unicode-vo", + "xmlwriter", +] + [[package]] name = "utf16_iter" version = "1.0.5" diff --git a/README.md b/README.md index c1ab7967e..a29bbfeb0 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,10 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = "`/`` instances and the CSS cascade as Bézier paths in millimetres, each tagged by the caller); `color` parses CSS colours; `write` writes documents - (elements with escaping, path data, numbers). + (elements with escaping, path data, numbers). Feature `tree`: the full parser, usvg at the one + version every Craft app uses, re-exported behind an entry point that runs those checks first + (plus nesting with references followed, attribute counts, bounded embedded SVGs), and a budget + check for drawing a tree with resvg. Apps take usvg from here, not as their own dependency. - [`craft-svg-paths`](crates/svg-paths/src/lib.rs): the former name of `craft-svg`'s `paths`, `color` and `write`; re-exports them for existing users. - [`craft-text`](crates/text/README.md): text processing below the composer. Its `hyphen` module finds diff --git a/crates/svg/Cargo.toml b/crates/svg/Cargo.toml index 3baa0c734..49b22e213 100644 --- a/crates/svg/Cargo.toml +++ b/crates/svg/Cargo.toml @@ -17,3 +17,14 @@ flate2 = { version = "1", default-features = false, features = ["rust_backend"] kurbo = { workspace = true } roxmltree = "0.21.2" thiserror = { workspace = true } +# The full SVG parser (Apache-2.0 OR MIT). The one version every Craft app uses: apps depend on +# craft-svg's `tree` feature and take usvg from its re-export, never directly. +usvg = { version = "=0.48.1", default-features = false, optional = true } + +[features] +# `craft_svg::tree`: usvg behind the bounded entry point. +tree = ["dep:usvg"] +# Text converted to paths (needs a font database in the options). +tree-text = ["tree", "usvg/text"] +# The system's fonts in `usvg::fontdb`. +tree-system-fonts = ["tree-text", "usvg/system-fonts"] diff --git a/crates/svg/src/guard.rs b/crates/svg/src/guard.rs index ba6f520df..073bfe34c 100644 --- a/crates/svg/src/guard.rs +++ b/crates/svg/src/guard.rs @@ -4,7 +4,8 @@ //! entities with no total limit, so a short hostile file can overflow the stack (an abort, not //! an error) or allocate far more than it holds. [`prescan`] measures how deep the elements nest //! in one pass over the text; [`entities_expand_past`] bounds what the entity references expand -//! to. Both read the text as it is written and never build a tree. +//! to; [`too_wide`] bounds the attributes an element has. All read the text as it is written and +//! never build a tree. use std::borrow::Cow; use std::collections::HashMap; @@ -94,6 +95,85 @@ pub fn entities_expand_past(svg: &str, most: usize) -> bool { false } +/// Whether the document type of `svg` declares an entity whose value holds markup (a `<`). Such +/// an entity becomes elements where it is referenced, so what the text shows of the tree is not +/// what a reader builds; drawing apps declare entities for namespace names only. A caller may +/// refuse these outright instead of counting them ([`prescan`] counts them ten times). Text that +/// only looks like a declaration (in a comment) counts too. `<` is text, not markup. +pub fn entity_holds_markup(svg: &str) -> bool { + declared(svg).values().flatten().any(|value| value.contains('<')) +} + +/// The most attributes one element may have, as a limit for [`too_wide`]: real elements have a +/// few dozen at most, and SVG's attribute set is a few hundred names in total. +pub const MAX_ATTRIBUTES: usize = 4096; + +/// The most the duplicate-attribute checks of an XML reader may cost over a whole document, as +/// a limit for [`too_wide`]: the sum of each element's attribute count squared (a real file +/// with 100,000 elements of 20 attributes is 40 million). +pub const MAX_ATTRIBUTE_WORK: usize = 1 << 28; + +/// Whether an element of `svg` has more than `most` attributes, or all elements cost more than +/// `most_work` (the sum of each element's attribute count squared). An XML reader checks each +/// attribute against the earlier ones on its element, so an element with millions of them is +/// quadratic work before any other limit applies. Counted as quoted values per start tag, +/// before a reader sees them; the declarations of a document type count as one element, as +/// entities can hold elements. +pub fn too_wide(svg: &str, most: usize, most_work: usize) -> bool { + let data = svg.as_bytes(); + let find = |from: usize, pat: &[u8]| -> usize { + data.get(from..).and_then(|r| r.windows(pat.len()).position(|w| w == pat)).map_or(data.len(), |p| from + p + pat.len()) + }; + let (mut i, mut work) = (0usize, 0usize); + let mut over = |attrs: usize| { + work = work.saturating_add(attrs.saturating_mul(attrs)); + attrs > most || work > most_work + }; + while let Some(off) = data.get(i..).and_then(|r| r.iter().position(|b| *b == b'<')) { + i += off; + let rest = data.get(i..).unwrap_or(&[]); + if rest.starts_with(b""); + } else if rest.starts_with(b""); + } else if rest.starts_with(b"` closing its subset (the first `>` when it has none). + let end = match (rest.iter().position(|b| *b == b'['), rest.iter().position(|b| *b == b'>')) { + (Some(o), Some(g)) if o < g => find(i, b"]>"), + _ => find(i, b">"), + }; + let quotes = data.get(i..end).unwrap_or(&[]).iter().filter(|b| matches!(**b, b'"' | b'\'')).count(); + if over(quotes / 2) { + return true; + } + i = end; + } else if rest.starts_with(b""); + } else { + // A start tag: to its `>` outside quotes. + let (mut quote, mut j, mut attrs) = (None, i + 1, 0usize); + while let Some(&c) = data.get(j) { + match quote { + Some(q) if c == q => quote = None, + Some(_) => {} + None if c == b'"' || c == b'\'' => { + quote = Some(c); + attrs += 1; + } + None if c == b'>' => break, + None => {} + } + j += 1; + } + if over(attrs) { + return true; + } + i = j + 1; + } + } + false +} + /// What one pass over the XML text of an SVG finds before any XML reader ([`prescan`]). pub struct Prescan<'s> { /// Whether the elements nest deeper than the cap. The pass stops there. @@ -259,6 +339,40 @@ mod tests { assert!(start.elapsed() < std::time::Duration::from_secs(120), "{:?}", start.elapsed()); } + #[test] + fn an_entity_holding_markup_is_found_and_a_namespace_entity_is_not() { + assert!(entity_holds_markup("\">]>&a;")); + assert!(entity_holds_markup("'>]>")); + assert!(!entity_holds_markup("]>")); + assert!(!entity_holds_markup("]>")); + assert!(!entity_holds_markup("")); + } + + fn wide(svg: &str) -> bool { + too_wide(svg, MAX_ATTRIBUTES, MAX_ATTRIBUTE_WORK) + } + + #[test] + fn an_element_with_thousands_of_attributes_is_too_wide() { + let many = |n: usize| (0..n).map(|i| format!(" a{i}=\"1\"")).collect::(); + assert!(!wide(&format!("", many(4096)))); + assert!(wide(&format!("", many(4097)))); + assert!(!wide(&format!("{}", "".repeat(10_000))), "per element, not per file"); + assert!(too_wide("", 2, usize::MAX) && !too_wide("", 3, usize::MAX)); + assert!(!wide(" '' \"\" "), "quotes outside tags are no attributes"); + } + + #[test] + fn attribute_work_is_bounded_over_the_whole_document_and_its_dtd() { + let many = |n: usize| (0..n).map(|i| format!(" a{i}=\"1\"")).collect::(); + let tag = format!("", many(4000)); + assert!(!wide(&format!("{}", tag.repeat(10)))); + assert!(wide(&format!("{}", tag.repeat(40))), "each element is within the limit, all together are not"); + let dtd = format!("'>]>&e;", many(5000)); + assert!(wide(&dtd), "entities can hold elements"); + assert!(!wide("]>")); + } + #[test] fn declarations_in_comments_and_long_names_cannot_hide_a_large_entity() { let big = "x".repeat(1 << 20); diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rs index e057b9b1f..fd6ca685e 100644 --- a/crates/svg/src/lib.rs +++ b/crates/svg/src/lib.rs @@ -9,6 +9,8 @@ //! and CAD import. //! - [`color`]: CSS colours. //! - [`write`]: a writer for SVG documents, their path data and numbers. +//! - `tree` (feature `tree`): the full parser. usvg at the one version the Craft apps share, +//! re-exported, behind an entry point that runs the checks above before it parses. //! //! Input is treated as hostile throughout: every size, count and depth is bounded, and a file //! past a bound is an error, never a panic or an unbounded allocation. @@ -20,4 +22,6 @@ pub mod guard; pub mod paths; pub mod sniff; pub mod svgz; +#[cfg(feature = "tree")] +pub mod tree; pub mod write; diff --git a/crates/svg/src/paths.rs b/crates/svg/src/paths.rs index d09620e57..e909283fd 100644 --- a/crates/svg/src/paths.rs +++ b/crates/svg/src/paths.rs @@ -72,8 +72,16 @@ pub struct Shape { pub stroke: Option, /// The regions the shape is clipped by, as indices into [`Imported::clips`], outermost first. pub clips: Vec, + /// The name of the innermost Inkscape layer the element is in: a `` with + /// `inkscape:groupmode="layer"`, named by its `inkscape:label`, else its `id`, else empty. + /// `None` outside any layer. At most [`MAX_LAYER_NAME`] characters. + pub layer: Option, } +/// Most characters of a layer name kept in [`Shape::layer`]. +pub const MAX_LAYER_NAME: usize = 256; +const INKSCAPE_NS: &str = "http://www.inkscape.org/namespaces/inkscape"; + /// What [`import`] read. Shapes may lie outside the `width_mm` × `height_mm` viewport. #[derive(Debug, Clone, PartialEq)] pub struct Imported { @@ -102,6 +110,8 @@ struct ImportState { inherited_style: std::collections::HashMap, /// Active `clip-path` regions (indices into the context's list), outermost first. clips: Vec, + /// The innermost Inkscape layer's name ([`Shape::layer`]). + layer: Option>, } impl Default for ImportState { @@ -113,6 +123,7 @@ impl Default for ImportState { visibility_hidden: false, inherited_style: std::collections::HashMap::new(), clips: Vec::new(), + layer: None, } } } @@ -127,7 +138,7 @@ const MAX_SVG_CLIP_PIECES: usize = 100_000; const MAX_SVG_SEGMENTS: usize = 4_000_000; /// Selector tests across the whole document: rules × elements styled, plus each parent, ancestor or /// sibling a combinator walks. -const MAX_CSS_SELECTOR_CHECKS: usize = 5_000_000; +pub(crate) const MAX_CSS_SELECTOR_CHECKS: usize = 5_000_000; /// Reads the drawable geometry of an SVG document, in document order. /// @@ -299,6 +310,10 @@ fn import_node<'a, 'input, K: Kind>( let local = parse_transform(transform).map_err(|e| svg_error(context.path_name, &format!("<{}> transform: {e}", tag)))?; state.transform = compose(inherited.transform, local); } + if tag == "g" && node.attribute((INKSCAPE_NS, "groupmode")) == Some("layer") { + let name = node.attribute((INKSCAPE_NS, "label")).or_else(|| node.attribute("id")).unwrap_or(""); + state.layer = Some(name.chars().take(MAX_LAYER_NAME).collect::().into()); + } context.charge_cascade()?; let style = computed_style(node, &state.inherited_style, &context.css_rules, K::PROPERTIES, &mut context.selector_checks); context.check_selector_budget()?; @@ -406,6 +421,7 @@ fn import_node<'a, 'input, K: Kind>( label: label(node), stroke: stroke_paint(&style), clips: state.clips.clone(), + layer: state.layer.as_deref().map(str::to_string), }); } for child in node.children().filter(|n| n.is_element()) { @@ -849,7 +865,7 @@ type CssPriority = (bool, u32, u32, u32, usize); type CascadeWinners = std::collections::HashMap; #[derive(Clone)] -struct CssRule { +pub(crate) struct CssRule { selectors: Vec, declarations: StyleMap, important: std::collections::HashSet, @@ -985,7 +1001,12 @@ impl SimpleSelector { /// CSS block at-rules whose content never styles geometry; skipped without a warning. const QUIET_AT_RULES: &[&str] = &["font-face", "keyframes", "-webkit-keyframes", "page", "counter-style", "charset", "namespace"]; -fn parse_stylesheets(root: roxmltree::Node<'_, '_>, extra: &[&str], path_name: &str, warnings: &mut Vec) -> Result, Error> { +pub(crate) fn parse_stylesheets( + root: roxmltree::Node<'_, '_>, + extra: &[&str], + path_name: &str, + warnings: &mut Vec, +) -> Result, Error> { let mut rules = Vec::new(); let mut dropped = 0usize; let mut warn = |message: String| push_warning(warnings, &mut dropped, message); @@ -1292,7 +1313,7 @@ fn parse_declarations(raw: &str, extra: &[&str], path_name: &str) -> Result<(Sty Ok((declarations, important)) } -fn computed_style(node: roxmltree::Node<'_, '_>, inherited: &StyleMap, rules: &[CssRule], extra: &[&str], checks: &mut usize) -> StyleMap { +pub(crate) fn computed_style(node: roxmltree::Node<'_, '_>, inherited: &StyleMap, rules: &[CssRule], extra: &[&str], checks: &mut usize) -> StyleMap { let mut result = inherited.clone(); result.remove("display"); let mut winners = CascadeWinners::new(); @@ -2081,4 +2102,29 @@ mod tests { let got = read(r#""#); assert_eq!(got.shapes[0].kind, Tag::Engrave); } + + #[test] + fn inkscape_layers_name_the_shapes_inside_them() { + let read = read( + r#" + + + + + + + + + + "#, + ); + let layers: Vec> = read.shapes.iter().map(|s| s.layer.as_deref()).collect(); + assert_eq!(layers, [None, Some("Cut & score"), Some("Cut & score"), Some("layer2"), Some(""), None, None]); + // A long name is cut, by characters. + let long = format!( + r#""#, + "é".repeat(MAX_LAYER_NAME + 50) + ); + assert_eq!(import::(&long, "test.svg").unwrap().shapes[0].layer.as_ref().map(|n| n.chars().count()), Some(MAX_LAYER_NAME)); + } } diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs new file mode 100644 index 000000000..6c79c33a7 --- /dev/null +++ b/crates/svg/src/tree.rs @@ -0,0 +1,327 @@ +//! The full SVG parser: usvg, behind one bounded entry point. +//! +//! usvg (re-exported as [`usvg`]) turns an SVG into a tree with styles resolved, `use` expanded, +//! text laid out and units converted. It is the parser every Craft app that reads SVG in full +//! shares, at the one version this crate pins; apps take it from here and not as a dependency of +//! their own, so they also share the checks it needs. usvg trusts its input more than a file +//! from anywhere deserves: it unpacks SVGZ with no limit, expands entities with no total limit, +//! and converts the tree by recursion, a few stack frames an element, so a small hostile file +//! can exhaust memory or overflow the stack (an abort, not an error). +//! +//! [`parse`] is the entry point: the bytes of an SVG or SVGZ file and [`Limits`] in, a +//! [`usvg::Tree`] out. It runs, in order: [`crate::svgz::text`] (bounded unpacking, encodings), +//! [`crate::guard::entities_expand_past`], [`crate::guard::too_wide`] (attributes an element), +//! [`crate::guard::prescan`] (nesting in the text), +//! [`height`] (nesting with references followed, as usvg walks them), then usvg. +//! [`image_resolver`] bounds the SVG documents embedded in `` data URLs the same way. +//! [`draw`] bounds what drawing a tree with resvg allocates. +//! +//! Stack: usvg uses about 15 kB of stack for each level of nesting (measured natively by +//! VectorCraft). [`Limits::depth`] levels, times one more than [`Limits::embedded_depth`], must +//! fit the stack of the thread that calls [`parse`]: the default of 256 needs a thread with +//! 16 MB or so, and a 1 MB stack takes a depth of about 34. + +use std::borrow::Cow; +use std::collections::HashMap; +use std::collections::hash_map::Entry; +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; + +pub use usvg; + +use crate::paths::{MAX_CSS_SELECTOR_CHECKS, StyleMap, computed_style, parse_stylesheets}; +use crate::{guard, svgz}; + +pub mod draw; + +/// What one import may use. The defaults suit a desktop app that parses on a thread with a +/// large stack (see the module text). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + /// Bytes of the document, unpacked if it is SVGZ. + pub bytes: usize, + /// How deep elements may nest, references followed (the root alone is 1). + pub depth: usize, + /// Bytes the document type's entity references may expand to in all. + pub entity_bytes: usize, + /// Refuse a document type that declares an entity holding markup, instead of counting what + /// it expands to ([`guard::entity_holds_markup`]). + pub refuse_entity_markup: bool, + /// Attributes one element may have ([`guard::too_wide`]). + pub attributes: usize, + /// The sum over all elements of their attribute count squared ([`guard::too_wide`]). + pub attribute_work: usize, + /// Nodes the XML may hold once entities are expanded. + pub xml_nodes: u32, + /// Bytes of all SVG documents embedded in `` data URLs, unpacked, for + /// [`image_resolver`]. + pub embedded_bytes: usize, + /// SVG documents embedded inside one another, for [`image_resolver`]. + pub embedded_depth: usize, +} + +impl Default for Limits { + fn default() -> Self { + Limits { + bytes: 64 << 20, + depth: 256, + entity_bytes: guard::MAX_ENTITY_EXPANSION, + refuse_entity_markup: false, + attributes: guard::MAX_ATTRIBUTES, + attribute_work: guard::MAX_ATTRIBUTE_WORK, + xml_nodes: 4_000_000, + embedded_bytes: 32 << 20, + embedded_depth: 4, + } + } +} + +/// Why a file was not parsed. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// Too large, a damaged or nested SVGZ, or text in no encoding SVG files use. + #[error(transparent)] + Text(#[from] svgz::Error), + #[error("the SVG's entities expand to too much text")] + Entities, + #[error("the SVG's document type declares an entity holding markup, which isn't supported")] + EntityMarkup, + /// Elements nest deeper than this many levels, references followed. + #[error("the SVG's elements are nested more than {0} levels deep")] + TooDeep(usize), + /// An element has more than this many attributes, or all elements together too many. + #[error("an element of the SVG has more than {0} attributes, or its elements have too many in all")] + TooWide(usize), + /// A style sheet that names other elements (`url(#…)`) and that this crate cannot read as + /// usvg will, so the nesting it makes cannot be measured. + #[error("the SVG's style sheet can't be checked: {0}")] + Stylesheet(String), + #[error("invalid SVG: {0}")] + Xml(String), + /// usvg's own error. + #[error("invalid SVG: {0}")] + Parse(String), +} + +/// The SVG text of the bytes of an SVG or SVGZ file, checked against `limits`: what [`parse`] +/// hands usvg. For a caller that edits the text before it parses ([`parse_text`]). +pub fn text<'a>(bytes: &'a [u8], limits: &Limits) -> Result, Error> { + let text = svgz::text(bytes, limits.bytes)?; + checked(&text, limits)?; + Ok(text) +} + +/// The tree of an SVG or SVGZ file, within `limits`. `options` are usvg's: fonts, the default +/// size, and the image resolver ([`image_resolver`] unless the caller reads linked files). +pub fn parse(bytes: &[u8], limits: &Limits, options: &usvg::Options<'_>) -> Result { + parse_text(&svgz::text(bytes, limits.bytes)?, limits, options) +} + +/// [`parse`] for SVG text the caller already holds. +pub fn parse_text(svg: &str, limits: &Limits, options: &usvg::Options<'_>) -> Result { + let xml = checked(svg, limits)?; + usvg::Tree::from_xmltree(&xml, options).map_err(|e| Error::Parse(e.to_string())) +} + +/// Every check on SVG text, cheapest first; the XML it parses to on the way. +fn checked<'s>(svg: &'s str, limits: &Limits) -> Result, Error> { + if svg.len() > limits.bytes { + return Err(Error::Text(svgz::Error::TooLarge(limits.bytes))); + } + if limits.refuse_entity_markup && guard::entity_holds_markup(svg) { + return Err(Error::EntityMarkup); + } + if guard::entities_expand_past(svg, limits.entity_bytes) { + return Err(Error::Entities); + } + if guard::too_wide(svg, limits.attributes, limits.attribute_work) { + return Err(Error::TooWide(limits.attributes)); + } + if guard::deeper_than(svg, limits.depth) { + return Err(Error::TooDeep(limits.depth)); + } + let options = roxmltree::ParsingOptions { allow_dtd: true, nodes_limit: limits.xml_nodes, ..Default::default() }; + let xml = roxmltree::Document::parse_with_options(svg, options).map_err(|e| Error::Xml(e.to_string()))?; + if height(&xml, limits.depth)?.is_none() { + return Err(Error::TooDeep(limits.depth)); + } + Ok(xml) +} + +/// Properties whose value an element's descendants use too: the paint server they name is +/// converted at each descendant, under everything between. +const INHERITED: [&str; 6] = ["fill", "stroke", "marker", "marker-start", "marker-mid", "marker-end"]; + +/// Properties that name other elements and that the cascade does not track by itself. +const MARKERS: [&str; 4] = ["marker", "marker-start", "marker-mid", "marker-end"]; + +/// The ids of the `url(#id)` references in a property value. +fn urls(value: &str) -> impl Iterator { + value.split("url(").skip(1).filter_map(|rest| { + let id = rest.trim_start().trim_start_matches(['"', '\'']).strip_prefix('#')?; + id.split([')', '"', '\'']).next().map(str::trim).filter(|id| !id.is_empty()) + }) +} + +type XNode<'a, 'i> = roxmltree::Node<'a, 'i>; + +/// How deep the elements of `xml` nest as usvg walks them (the root alone is 1); `None` past +/// `most`. Without recursion. +/// +/// What counts is the tree usvg converts, not the XML alone: a `` holds a copy of what it +/// refers to, and a clip path, mask, pattern, marker or filter is converted where it is used +/// (`clip-path: url(#a)` on an element of a clip path makes a chain as deep as there are clip +/// paths). So each element's depth is the deepest of what it holds and what it refers to, by +/// attribute, `style` and style sheet. A loop of references is not deep: usvg leaves it out. +/// +/// An error when a style sheet names elements and cannot be read here as usvg reads it (a +/// selector or at-rule this crate's cascade does not apply, a sheet for another medium, a sheet +/// past its size or work limits): the nesting such a sheet makes cannot be measured. +pub fn height(xml: &roxmltree::Document<'_>, most: usize) -> Result, Error> { + let elements = || xml.descendants().filter(|n| n.is_element()); + // The first element of an id, as usvg resolves references: a later one of the same id would + // hide a deep target behind a shallow one. + let mut ids: HashMap<&str, XNode> = HashMap::new(); + for n in elements() { + if let Some(id) = n.attribute("id") { + ids.entry(id).or_insert(n); + } + } + let mut warnings = Vec::new(); + let rules = + parse_stylesheets(xml.root_element(), &MARKERS, "", &mut warnings).map_err(|e| Error::Stylesheet(e.0.trim_start_matches(": ").to_string())); + let sheets = || elements().filter(|n| n.tag_name().name() == "style"); + let names_elements = sheets().any(|n| n.text().is_some_and(|t| t.contains("url("))); + let rules = match rules { + Ok(rules) => rules, + // A sheet that names no element adds no nesting, whatever else is in it. + Err(_) if !names_elements => Vec::new(), + Err(e) => return Err(e), + }; + if names_elements { + if let Some(w) = warnings.first() { + return Err(Error::Stylesheet(w.clone())); + } + if sheets().any(|n| n.attribute("media").is_some_and(|m| !matches!(m.trim(), "" | "all" | "screen"))) { + return Err(Error::Stylesheet("a style sheet for another medium names elements".into())); + } + } + let nothing = StyleMap::new(); + let mut checks = 0usize; + // The elements `n` refers to, each with whether `n`'s descendants use it too. + let mut references = |n: XNode, out: &mut Vec<(roxmltree::NodeId, bool)>| -> Result<(), Error> { + for a in n.attributes().filter(|a| a.name() == "href") { + out.extend(a.value().trim().strip_prefix('#').and_then(|id| ids.get(id)).map(|t| (t.id(), false))); + } + for (name, value) in computed_style(n, ¬hing, &rules, &MARKERS, &mut checks) { + let inherited = INHERITED.contains(&name.as_str()); + out.extend(urls(&value).filter_map(|id| ids.get(id)).map(|t| (t.id(), inherited))); + } + if checks > MAX_CSS_SELECTOR_CHECKS { + return Err(Error::Stylesheet("the style sheet is too large for this many elements".into())); + } + Ok(()) + }; + // How deep each element's own tree goes, itself included; 0 while it is being worked out: a + // reference back to it then is a loop, which usvg leaves out. + let mut heights: HashMap = HashMap::new(); + enum Step { + Enter(roxmltree::NodeId), + Leave(roxmltree::NodeId, Vec<(roxmltree::NodeId, bool)>), + } + let mut stack = vec![Step::Enter(xml.root_element().id())]; + while let Some(step) = stack.pop() { + match step { + Step::Enter(id) => { + let Some(n) = xml.get_node(id) else { continue }; + match heights.entry(id) { + Entry::Occupied(_) => continue, + Entry::Vacant(v) => v.insert(0), + }; + let mut refs = vec![]; + references(n, &mut refs)?; + let targets: Vec = refs.iter().map(|(t, _)| *t).collect(); + stack.push(Step::Leave(id, refs)); + stack.extend(n.children().filter(|c| c.is_element()).map(|c| c.id()).chain(targets).map(Step::Enter)); + } + Step::Leave(id, refs) => { + let Some(n) = xml.get_node(id) else { continue }; + let of = |id: roxmltree::NodeId| heights.get(&id).copied().unwrap_or(0); + let held = n.children().filter(|c| c.is_element()).map(|c| of(c.id())).max().unwrap_or(0); + let used = |inherited: bool| refs.iter().filter(|(_, i)| *i == inherited).map(|(t, _)| of(*t)).max().unwrap_or(0); + let height = 1 + held.max(used(false)) + used(true); + if height > most { + return Ok(None); + } + heights.insert(id, height); + } + } + } + Ok(Some(heights.get(&xml.root_element().id()).copied().unwrap_or(0))) +} + +/// What the embedded SVGs of one import have used so far. +struct Used { + bytes: AtomicUsize, + depth: AtomicUsize, +} + +fn raster(data: &[u8]) -> bool { + data.starts_with(b"\x89PNG") + || data.starts_with(&[0xff, 0xd8, 0xff]) + || data.starts_with(b"GIF8") + || (data.len() > 12 && data.get(8..12) == Some(b"WEBP")) +} + +/// An embedded SVG document, if the budgets allow it. +fn embedded(used: &Used, limits: &Limits, data: &[u8], options: &usvg::Options<'_>) -> Option { + if used.depth.load(Ordering::Relaxed) >= limits.embedded_depth { + return None; + } + let left = limits.embedded_bytes.saturating_sub(used.bytes.load(Ordering::Relaxed)); + let text = match svgz::text(data, left.min(limits.bytes)) { + Ok(text) => text, + // What a refused document unpacked before it was refused was work too: it uses up what + // is left, so the next image can't repeat it. + Err(_) => { + used.bytes.fetch_add(left, Ordering::Relaxed); + return None; + } + }; + used.bytes.fetch_add(text.len(), Ordering::Relaxed); + let xml = checked(&text, limits).ok()?; + used.depth.fetch_add(1, Ordering::Relaxed); + let tree = usvg::Tree::from_xmltree(&xml, options); + used.depth.fetch_sub(1, Ordering::Relaxed); + tree.ok().map(usvg::ImageKind::SVG) +} + +/// An image resolver for `usvg::Options::image_href_resolver` that reads `` data URLs +/// only: raster images as they are (PNG, JPEG, GIF, WebP), SVG documents (SVGZ too) through the +/// same checks as [`parse`] and within [`Limits::embedded_bytes`] in all and +/// [`Limits::embedded_depth`] inside one another. An image past a limit is left out. Linked +/// files are never read. One resolver is one import's budget: make a new one for each [`parse`]. +pub fn image_resolver(limits: Limits) -> usvg::ImageHrefResolver<'static> { + let used = Arc::new(Used { bytes: AtomicUsize::new(0), depth: AtomicUsize::new(0) }); + usvg::ImageHrefResolver { + resolve_data: Box::new(move |mime, data, options| match mime { + "image/jpg" | "image/jpeg" => Some(usvg::ImageKind::JPEG(data)), + "image/png" => Some(usvg::ImageKind::PNG(data)), + "image/gif" => Some(usvg::ImageKind::GIF(data)), + "image/webp" => Some(usvg::ImageKind::WEBP(data)), + "image/svg+xml" => embedded(&used, &limits, &data, options), + "text/plain" if raster(&data) => match data.first() { + Some(0x89) => Some(usvg::ImageKind::PNG(data)), + Some(0xff) => Some(usvg::ImageKind::JPEG(data)), + Some(b'G') => Some(usvg::ImageKind::GIF(data)), + _ => Some(usvg::ImageKind::WEBP(data)), + }, + "text/plain" => embedded(&used, &limits, &data, options), + _ => None, + }), + resolve_string: Box::new(|_, _| None), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/svg/src/tree/draw.rs b/crates/svg/src/tree/draw.rs new file mode 100644 index 000000000..fed7ac569 --- /dev/null +++ b/crates/svg/src/tree/draw.rs @@ -0,0 +1,431 @@ +//! Hard caps on the memory resvg needs to draw a subtree of a [`usvg::Tree`], checked before it +//! draws. +//! +//! A cap on the target pixmap bounds the target only. While it draws, resvg also allocates, and +//! a failed allocation aborts the process rather than returning an error: +//! - a layer for every isolated group (opacity, blend mode, clip, mask, filter), clamped to 5 × 5 +//! times the target, and one more per link of a clip-path chain, per clip child that has its own +//! clip-path, and per link of a mask-on-mask chain; +//! - for a filter, a result per primitive (all kept until the filter ends) and a clone of the layer. +//! The filter region is NOT clamped: `x`, `y`, `width` and `height` in the file set the size; +//! - a pattern tile of `width × height × scale`; +//! - for an embedded SVG image or an `feImage`, a pixmap of the current layer or filter region with +//! a nested drawing inside. +//! +//! The checks are worst-case products, not a model of resvg's phases. A node is checked against +//! the layers of its ancestors (which are alive while it is drawn) and nothing else, and each +//! shared definition is walked once per size class, so the walk is linear in the file. +//! +//! The allocation counts were read from resvg 0.45.1 (PhotoCraft's `svg/limits.rs`, where this +//! comes from). This crate does not depend on resvg; a caller that draws with another version +//! should treat the byte figures as an estimate with the same shape. + +use std::collections::HashMap; + +use usvg::Transform; + +/// Filter primitives per filter. A result of every primitive is kept; real filters have 1 to 12 +/// (Inkscape's drop shadow has 5), and 64 leaves headroom for generated ones. +const MAX_PRIMITIVES: usize = 64; +/// Links of one clip-path chain, mask-on-mask chain or nested clip children with their own +/// clip-path. Each link keeps a layer-sized buffer alive while the next is applied; real files use +/// one or two, and 16 layers of a 24 MP canvas (1.5 GiB) is the whole budget anyway. +const MAX_LINKS: usize = 16; +/// Patterns drawn inside patterns. Each tile is alive while the inner one is drawn. +const MAX_PATTERN_DEPTH: usize = 8; +/// Embedded SVG images inside embedded SVG images. Each draws on a pixmap of the enclosing +/// layer, which the live-bytes check charges, so deep chains of small images are harmless and +/// chains of big ones stop at the budget long before this. +const MAX_IMAGE_DEPTH: usize = 32; +/// Nested isolated groups, patterns, embedded images and filter images followed. It keeps this +/// walk, and resvg's own recursion, far from a stack overflow; real drawings stay under a few dozen. +const MAX_DEPTH: usize = 200; +/// Nested groups of any kind. usvg expands `` chains up to its own 1024-level limit, possibly +/// two groups per level; resvg recurses three frames per level too, so a walk that stops here is +/// lighter on the stack than the drawing it guards. +const MAX_NEST: usize = 2_100; +/// Nodes the walk visits before it gives up as too complex to check. A node costs a few +/// nanoseconds, so this bounds the walk to well under a second. +const MAX_NODES: usize = 2_000_000; + +/// Why a drawing should not be drawn. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Refusal { + /// Too many elements to check, rather than too much memory. + pub complex: bool, + /// Over the memory budget, as opposed to a nesting, chain or element-count cap. + pub memory: bool, + pub message: String, +} + +fn mib(bytes: f64) -> String { + if bytes < 1e15 { format!("about {} MiB", (bytes / 1_048_576.0).ceil()) } else { "an enormous amount of memory".to_string() } +} + +fn memory(what: &str, bytes: f64, budget: f64) -> Refusal { + let limit = budget as u64 >> 20; + Refusal { complex: false, memory: true, message: format!("{what} needs {}, over the {limit} MiB limit", mib(bytes)) } +} + +fn limit(what: &str, value: usize) -> Refusal { + Refusal { complex: false, memory: false, message: format!("{what} (limit {value})") } +} + +/// A pixel count that is never NaN: unknown sizes count as unbounded. +pub fn px(v: f64) -> f64 { + if v.is_nan() { f64::INFINITY } else { v } +} + +/// What is alive where a node is drawn. +#[derive(Clone, Copy)] +struct Cx { + /// Bytes held by the target and the layers of the ancestors. + live: f64, + /// Pixels of the pixmap being drawn on. + layer: f64, + /// Pixels of the largest group layer resvg makes in it (5 × 5 times its first target). + cap: f64, + /// Nested layers (isolated groups, patterns, embedded images, filter images). + depth: usize, + patterns: usize, + images: usize, + /// Nested groups of any kind: resvg and this walk recurse once per level. + nest: usize, +} + +/// What a subtree needs, relative to what is alive where it starts. A shared definition is walked +/// once per transform and size, and every later reference re-checks its profile against where it +/// is used, so nothing is skipped and the walk stays linear. +#[derive(Clone, Copy, Default)] +struct Profile { + /// Most bytes it holds beyond the incoming live bytes. + extra: f64, + /// Most extra levels of nested layers, patterns, embedded images and groups below it. + depth: usize, + patterns: usize, + images: usize, + nest: usize, +} + +impl Profile { + fn max(self, o: Self) -> Self { + Self { + extra: self.extra.max(o.extra), + depth: self.depth.max(o.depth), + patterns: self.patterns.max(o.patterns), + images: self.images.max(o.images), + nest: self.nest.max(o.nest), + } + } + + /// This profile seen from a parent that adds `bytes` and one level of each kind in `levels` + /// (layers, patterns, images, groups). + fn under(self, bytes: f64, levels: [usize; 4]) -> Self { + Self { + extra: self.extra + bytes, + depth: self.depth + levels[0], + patterns: self.patterns + levels[1], + images: self.images + levels[2], + nest: self.nest + levels[3], + } + } +} + +/// A shared definition at one linear transform, layer size and clamp. +type Key = (usize, [u32; 4], u64, u64); + +fn key(ptr: usize, ts: Transform, cx: Cx) -> Key { + (ptr, [ts.sx.to_bits(), ts.kx.to_bits(), ts.ky.to_bits(), ts.sy.to_bits()], cx.layer.to_bits(), cx.cap.to_bits()) +} + +struct Walk { + /// Bytes of RGBA8 buffers alive at once that the drawing may need. + budget: f64, + visited: usize, + shared: HashMap, + clips: HashMap, + masks: HashMap, +} + +/// Check that drawing `nodes` at `ts` onto a `width × height` pixmap keeps the RGBA8 buffers alive +/// at once inside `budget` bytes, given `held` bytes already in use (anything the caller keeps; +/// the target itself is counted here). +pub fn check(nodes: &[usvg::Node], ts: Transform, width: u32, height: u32, held: u64, budget: u64) -> Result<(), Refusal> { + let target = f64::from(width) * f64::from(height); + let cx = Cx { live: held as f64 + target * 4.0, layer: target, cap: 25.0 * target, depth: 0, patterns: 0, images: 0, nest: 0 }; + let mut walk = Walk { budget: budget as f64, visited: 0, shared: HashMap::new(), clips: HashMap::new(), masks: HashMap::new() }; + walk.nodes(nodes, ts, cx).map(|_| ()) +} + +impl Walk { + fn need(&self, live: f64, what: impl FnOnce() -> String) -> Result<(), Refusal> { + if live > self.budget { Err(memory(&what(), live, self.budget)) } else { Ok(()) } + } +} + +impl Walk { + fn nodes(&mut self, nodes: &[usvg::Node], ts: Transform, cx: Cx) -> Result { + let mut profile = Profile::default(); + for n in nodes { + profile = profile.max(self.node(n, ts, cx)?); + } + Ok(profile) + } + + // The recursion through plain groups is the deep one (usvg allows 1,000+ levels), so only small + // functions sit on it; everything with many locals is kept off it with `inline(never)`. + fn node(&mut self, node: &usvg::Node, ts: Transform, cx: Cx) -> Result { + self.visited += 1; + if self.visited > MAX_NODES { + return Err(Refusal { complex: true, memory: false, message: format!("the drawing has more than {MAX_NODES} elements to check") }); + } + match node { + usvg::Node::Group(g) if !g.should_isolate() => self.plain(g, ts, cx), + usvg::Node::Group(g) => self.group(g, ts, cx), + usvg::Node::Text(t) => self.group(t.flattened(), ts, cx), + usvg::Node::Path(p) => self.path(p, ts, cx), + usvg::Node::Image(image) => self.image(image, ts, cx), + } + } + + /// A plain group is no layer. + fn plain(&mut self, g: &usvg::Group, ts: Transform, cx: Cx) -> Result { + if cx.nest >= MAX_NEST { + return Err(limit("groups are nested too deeply", MAX_NEST)); + } + let inside = self.nodes(g.children(), ts.pre_concat(g.transform()), Cx { nest: cx.nest + 1, ..cx })?; + Ok(inside.under(0.0, [0, 0, 0, 1])) + } + + #[inline(never)] + fn path(&mut self, p: &usvg::Path, ts: Transform, cx: Cx) -> Result { + let mut profile = Profile::default(); + if !p.is_visible() { + return Ok(profile); + } + for paint in p.fill().map(|f| f.paint()).into_iter().chain(p.stroke().map(|s| s.paint())) { + if let usvg::Paint::Pattern(pattern) = paint { + profile = profile.max(self.pattern(pattern, ts, cx)?); + } + } + Ok(profile) + } + + /// resvg draws an embedded SVG on a pixmap the size of the current one, with its own 5 × 5 clamp. + #[inline(never)] + fn image(&mut self, image: &usvg::Image, ts: Transform, cx: Cx) -> Result { + let usvg::ImageKind::SVG(tree) = image.kind() else { return Ok(Profile::default()) }; + if cx.images >= MAX_IMAGE_DEPTH { + return Err(limit("embedded SVG images are nested too deeply", MAX_IMAGE_DEPTH)); + } + let bytes = cx.layer * 4.0; + self.need(cx.live + bytes, || "an embedded SVG image".to_string())?; + let nested = Cx { live: cx.live + bytes, cap: 25.0 * cx.layer, depth: cx.depth + 1, images: cx.images + 1, nest: cx.nest + 1, ..cx }; + deeper(nested)?; + let inside = self.nodes(tree.root().children(), ts, nested)?; + Ok(Profile { extra: bytes, ..Profile::default() }.max(inside.under(bytes, [1, 0, 1, 1]))) + } + + /// Walk a definition shared by reference (pattern, mask, filter image) once per transform and + /// size; a repeat only re-checks its profile where it is used now. + fn shared(&mut self, key: Key, cx: Cx, walk: impl FnOnce(&mut Self) -> Result) -> Result { + let Some(p) = self.shared.get(&key).copied() else { + let p = walk(self)?; + self.shared.insert(key, p); + return Ok(p); + }; + self.need(cx.live + p.extra, || "a shared definition used here".to_string())?; + if cx.depth + p.depth > MAX_DEPTH { + return Err(limit("layers are nested too deeply", MAX_DEPTH)); + } + if cx.patterns + p.patterns > MAX_PATTERN_DEPTH { + return Err(limit("patterns are nested too deeply", MAX_PATTERN_DEPTH)); + } + if cx.images + p.images > MAX_IMAGE_DEPTH { + return Err(limit("embedded SVG images are nested too deeply", MAX_IMAGE_DEPTH)); + } + if cx.nest + p.nest > MAX_NEST { + return Err(limit("groups are nested too deeply", MAX_NEST)); + } + Ok(p) + } + + #[inline(never)] + fn group(&mut self, g: &usvg::Group, ts: Transform, cx: Cx) -> Result { + if !g.should_isolate() { + return self.plain(g, ts, cx); + } + let ts = ts.pre_concat(g.transform()); + if cx.nest >= MAX_NEST { + return Err(limit("groups are nested too deeply", MAX_NEST)); + } + let Some(bb) = g.layer_bounding_box().transform(ts) else { return Ok(Profile::default()) }; + let own = px((f64::from(bb.width()).ceil() + 4.0) * (f64::from(bb.height()).ceil() + 4.0)).min(cx.cap); + let inner = Cx { live: cx.live + own * 4.0, layer: own, depth: cx.depth + 1, nest: cx.nest + 1, ..cx }; + deeper(inner)?; + // Filter, clip and mask are applied one after another; each needs its own extra buffers + // beside the group's layer. A clip or mask link is a 4 B/px pixmap and a 1 B/px mask. + let clip = g.clip_path().map_or(Ok(0), |c| self.clip_links(c, 0))?; + let mask = g.mask().map_or(Ok(0), |m| self.mask_links(m, 0))?; + let links = clip.max(mask); + let layers = own * 4.0 + own * 5.0 * links as f64; + self.need(cx.live + layers, || format!("a group with {links} clip or mask layers of {} px", own as u64))?; + let mut profile = Profile { extra: layers, ..Profile::default() }; + for filter in g.filters() { + profile = profile.max(self.filter(filter, ts, cx, own)?); + } + let mut mask_ref = g.mask(); + let mut link = 1.0; + while let Some(mask) = mask_ref { + // The mask content is drawn while this and every earlier link's pixmap and mask are alive. + let bytes = own * 4.0 + own * 5.0 * link; + let content = Cx { live: cx.live + bytes, ..inner }; + let inside = + self.shared(key(std::ptr::from_ref(mask) as usize, ts, content), content, |w| w.nodes(mask.root().children(), ts, content))?; + profile = profile.max(inside.under(bytes, [1, 0, 0, 1])); + mask_ref = mask.mask(); + link += 1.0; + } + let inside = self.nodes(g.children(), ts, inner)?; + Ok(profile.max(inside.under(own * 4.0, [1, 0, 0, 1]))) + } + + /// Layers a clip-path keeps alive: its own, the deepest clip child with a clip-path of its own, + /// or the next link of the chain, whichever is deepest. Nothing is walked twice. + fn clip_links(&mut self, clip: &usvg::ClipPath, depth: usize) -> Result { + let key = std::ptr::from_ref(clip) as usize; + if let Some(n) = self.clips.get(&key) { + return Ok(*n); + } + let too_long = || limit("clip-paths are chained or nested too deeply", MAX_LINKS); + if depth >= MAX_LINKS { + return Err(too_long()); + } + let inside = self.clip_children(clip.root(), depth + 1)?; + let next = clip.clip_path().map_or(Ok(0), |c| self.clip_links(c, depth + 1))?; + let n = 1 + inside.max(next); + if n > MAX_LINKS { + return Err(too_long()); + } + self.clips.insert(key, n); + Ok(n) + } + + /// resvg's `clip_group`: a clip child with its own clip-path is drawn on a layer of its own, + /// kept while its descendants are drawn and its clip is applied. + fn clip_children(&mut self, group: &usvg::Group, depth: usize) -> Result { + let mut deepest = 0; + for child in group.children() { + let inner = match child { + usvg::Node::Group(g) => { + let inside = self.clip_children(g, depth + usize::from(g.clip_path().is_some()))?; + match g.clip_path() { + Some(c) => 1 + inside.max(self.clip_links(c, depth + 1)?), + None => inside, + } + } + usvg::Node::Text(t) => self.clip_children(t.flattened(), depth)?, + _ => 0, + }; + if inner + depth > MAX_LINKS { + return Err(limit("clip-paths are chained or nested too deeply", MAX_LINKS)); + } + deepest = deepest.max(inner); + } + Ok(deepest) + } + + /// Layers a mask chain keeps alive: a mask pixmap per link while the next is applied. + fn mask_links(&mut self, mask: &usvg::Mask, depth: usize) -> Result { + let key = std::ptr::from_ref(mask) as usize; + if let Some(n) = self.masks.get(&key) { + return Ok(*n); + } + if depth >= MAX_LINKS { + return Err(limit("masks are chained too deeply", MAX_LINKS)); + } + let n = 1 + mask.mask().map_or(Ok(0), |m| self.mask_links(m, depth + 1))?; + // A cached tail can make the chain longer than `depth` shows; the cap is on the whole chain. + if n > MAX_LINKS { + return Err(limit("masks are chained too deeply", MAX_LINKS)); + } + self.masks.insert(key, n); + Ok(n) + } + + /// A filter keeps a result per primitive, each as large as the filter region or the layer it + /// filters, whichever is larger (usvg makes the layer the union of all of a group's filter + /// regions, and `SourceGraphic` clones the whole layer). `own` is the size of that layer. + fn filter(&mut self, f: &usvg::filter::Filter, ts: Transform, cx: Cx, own: f64) -> Result { + use usvg::filter::Kind; + let prims = f.primitives().len(); + if prims > MAX_PRIMITIVES { + return Err(limit("a filter has too many primitives", MAX_PRIMITIVES)); + } + let Some(region) = f.rect().transform(ts) else { return Ok(Profile::default()) }; + let region = px(f64::from(region.width()).ceil() * f64::from(region.height()).ceil()); + let slot = region.max(own) * 4.0; + // Buffers a primitive allocates beside its result (resvg 0.45.1 `filter/`): a blur works + // on one copy, an IIR blur (sigma under 2 after scaling) also on f64 scratch; blend, + // composite and displacement map convert two inputs; a drop shadow holds the input, its + // blur, the shadow and the result. + let (sx, sy) = ts.get_scale(); + let transient = |kind: &Kind| match kind { + Kind::GaussianBlur(b) if b.std_dev_x().get() * sx >= 2.0 || b.std_dev_y().get() * sy >= 2.0 => 1.0, + Kind::GaussianBlur(_) => 2.0, + Kind::Blend(_) | Kind::Composite(_) | Kind::DisplacementMap(_) => 2.0, + // A tile of the source graphic converts and tiles two copies. + Kind::Tile(t) if matches!(t.input(), usvg::filter::Input::SourceGraphic) => 2.0, + Kind::DropShadow(_) => 4.0, + _ => 1.0, + }; + let most = f.primitives().iter().map(|p| transient(p.kind())).fold(0.0, f64::max); + let extra = own * 4.0 + slot * (prims as f64 + most); + self.need(cx.live + extra, || format!("a filter with {prims} primitives over a region of {} px", region.min(1e18) as u64))?; + let mut profile = Profile { extra, ..Profile::default() }; + for (i, primitive) in f.primitives().iter().enumerate() { + if let Kind::Image(fe) = primitive.kind() { + // What an feImage draws sees the results kept so far. + let bytes = own * 4.0 + slot * (i + 1) as f64; + let (sx, sy) = ts.get_scale(); + let scaled = Transform::from_scale(sx, sy); + let nested = Cx { live: cx.live + bytes, layer: region, cap: region, depth: cx.depth + 1, nest: cx.nest + 1, ..cx }; + deeper(nested)?; + let inside = + self.shared(key(std::ptr::from_ref(fe) as usize, scaled, nested), nested, |w| w.nodes(fe.root().children(), scaled, nested))?; + profile = profile.max(inside.under(bytes, [1, 0, 0, 1])); + } + } + Ok(profile) + } + + /// A pattern tile is `width × height × scale` pixels, whatever the target. + fn pattern(&mut self, pattern: &usvg::Pattern, ts: Transform, cx: Cx) -> Result { + let (sx, sy) = ts.pre_concat(pattern.transform()).get_scale(); + let rect = pattern.rect(); + let (tw, th) = (f64::from(rect.width() * sx).round(), f64::from(rect.height() * sy).round()); + // resvg draws no tile smaller than a pixel (or not a number). + if !(tw >= 1.0 && th >= 1.0) { + return Ok(Profile::default()); + } + let tile = tw * th; + let bytes = tile * 4.0; + self.need(cx.live + bytes, || format!("a pattern tile of {tw} × {th} px"))?; + if cx.patterns >= MAX_PATTERN_DEPTH { + return Err(limit("patterns are nested too deeply", MAX_PATTERN_DEPTH)); + } + let nested = Cx { live: cx.live + bytes, layer: tile, depth: cx.depth + 1, patterns: cx.patterns + 1, nest: cx.nest + 1, ..cx }; + deeper(nested)?; + let inner_ts = Transform::from_scale(sx, sy); + let inside = self + .shared(key(std::ptr::from_ref(pattern) as usize, inner_ts, nested), nested, |w| w.nodes(pattern.root().children(), inner_ts, nested))?; + Ok(Profile { extra: bytes, ..Profile::default() }.max(inside.under(bytes, [1, 1, 0, 1]))) + } +} + +fn deeper(cx: Cx) -> Result<(), Refusal> { + if cx.depth > MAX_DEPTH { + return Err(limit("layers are nested too deeply", MAX_DEPTH)); + } + Ok(()) +} diff --git a/crates/svg/src/tree/tests.rs b/crates/svg/src/tree/tests.rs new file mode 100644 index 000000000..751e7726b --- /dev/null +++ b/crates/svg/src/tree/tests.rs @@ -0,0 +1,304 @@ +#![allow(clippy::unwrap_used, clippy::panic, clippy::indexing_slicing)] + +use super::*; + +const DEPTH: usize = 256; + +fn svg(body: &str) -> String { + format!(r#"{body}"#) +} + +/// `n` groups in one another around a rectangle. +fn groups(n: usize) -> String { + format!("{}{}", "".repeat(n), "".repeat(n)) +} + +fn deep(svg: &str) -> bool { + height(&roxmltree::Document::parse(svg).unwrap(), DEPTH).unwrap().is_none() +} + +fn options() -> usvg::Options<'static> { + usvg::Options { image_href_resolver: image_resolver(Limits::default()), ..Default::default() } +} + +/// A limit small enough for usvg to convert on a test thread's stack. +fn shallow() -> Limits { + Limits { depth: 24, ..Limits::default() } +} + +fn try_parse(svg: &str, limits: &Limits) -> Result { + parse(svg.as_bytes(), limits, &options()) +} + +/// Every image in the tree, as whether it is an SVG document, nested ones after their parent. +fn images(group: &usvg::Group, out: &mut Vec) { + for node in group.children() { + match node { + usvg::Node::Group(g) => images(g, out), + usvg::Node::Image(image) => match image.kind() { + usvg::ImageKind::SVG(tree) => { + out.push(true); + images(tree.root(), out); + } + _ => out.push(false), + }, + _ => {} + } + } +} + +/// `` showing the SVG text `inner` as a data URL. +fn embed(inner: &str) -> String { + let encoded: String = inner.bytes().map(|b| if b.is_ascii_alphanumeric() { char::from(b).to_string() } else { format!("%{b:02X}") }).collect(); + format!(r#""#) +} + +#[test] +fn an_svg_and_an_svgz_parse_to_the_same_tree() { + let text = svg(""); + let tree = try_parse(&text, &shallow()).unwrap(); + assert_eq!((tree.size().width(), tree.size().height()), (10.0, 10.0)); + assert_eq!(tree.root().children().len(), 1); + let packed = parse(&svgz::compress(text.as_bytes()), &shallow(), &options()).unwrap(); + assert_eq!(packed.root().children().len(), 1); + assert_eq!(super::text(text.as_bytes(), &shallow()).unwrap(), text); + assert_eq!(parse_text(&text, &shallow(), &options()).unwrap().root().children().len(), 1); +} + +#[test] +fn every_limit_refuses_the_file_past_it() { + let text = svg(&groups(4)); + // Bytes, plain and unpacked. + let small = Limits { bytes: text.len() - 1, ..shallow() }; + assert_eq!(try_parse(&text, &small).err(), Some(Error::Text(svgz::Error::TooLarge(text.len() - 1)))); + assert_eq!(parse(&svgz::compress(text.as_bytes()), &small, &options()).err(), Some(Error::Text(svgz::Error::TooLarge(text.len() - 1)))); + assert!(matches!(parse_text(&text, &small, &options()), Err(Error::Text(_)))); + assert!(try_parse(&text, &Limits { bytes: text.len(), ..shallow() }).is_ok()); + // Depth in the text: the root, four groups and the rectangle are six levels. + assert!(try_parse(&text, &Limits { depth: 6, ..shallow() }).is_ok()); + assert_eq!(try_parse(&text, &Limits { depth: 5, ..shallow() }).err(), Some(Error::TooDeep(5))); + // Depth by reference: no element is nested, each holds the next. + let chain: String = (0..20).map(|i| format!("", i + 1)).collect(); + let uses = svg(&format!("{chain}")); + assert!(!guard::deeper_than(&uses, 24)); + assert_eq!(try_parse(&uses, &shallow()).err(), Some(Error::TooDeep(24))); + // Attributes on one element. + let wide = svg(&format!("", (0..40).map(|i| format!(" a{i}='1'")).collect::())); + assert!(try_parse(&wide, &Limits { attributes: 40, ..shallow() }).is_ok()); + assert_eq!(try_parse(&wide, &Limits { attributes: 39, ..shallow() }).err(), Some(Error::TooWide(39))); + assert_eq!(try_parse(&wide, &Limits { attribute_work: 1000, ..shallow() }).err(), Some(Error::TooWide(4096))); + // Entities. + let bomb = format!("]>{}", "x".repeat(1000), svg(&format!("{}", "&a;".repeat(10)))); + assert_eq!(try_parse(&bomb, &Limits { entity_bytes: 9_999, ..shallow() }).err(), Some(Error::Entities)); + assert!(try_parse(&bomb, &Limits { entity_bytes: 10_000, ..shallow() }).is_ok()); + // XML nodes. + assert!(matches!(try_parse(&svg(&"".repeat(100)), &Limits { xml_nodes: 50, ..shallow() }), Err(Error::Xml(_)))); + // Not XML, and XML that is not an SVG. + assert!(matches!(try_parse("", &shallow()), Err(Error::Parse(_)))); + assert!(matches!(parse(&[0xfe, 0xff, 0xd8], &shallow(), &options()), Err(Error::Text(svgz::Error::Encoding)))); +} + +#[test] +fn an_entity_holding_markup_is_refused_only_when_asked() { + let text = format!("\">]>{}", svg("&a;")); + assert_eq!(try_parse(&text, &Limits { refuse_entity_markup: true, ..shallow() }).err(), Some(Error::EntityMarkup)); + let tree = try_parse(&text, &shallow()).unwrap(); + assert_eq!(tree.root().children().len(), 1, "the entity's rectangle is drawn"); + // A namespace entity, as drawing apps write, is not markup. + let names = "]>"; + assert!(try_parse(names, &Limits { refuse_entity_markup: true, ..shallow() }).is_ok()); +} + +#[test] +fn elements_nest_up_to_the_cap() { + let (at, past) = (svg(&groups(DEPTH - 2)), svg(&groups(DEPTH - 1))); + assert!(!deep(&at)); + assert!(deep(&past)); + let ten = svg(&groups(8)); + let xml = roxmltree::Document::parse(&ten).unwrap(); + assert_eq!((height(&xml, 10), height(&xml, 9)), (Ok(Some(10)), Ok(None))); + assert_eq!(height(&roxmltree::Document::parse("").unwrap(), 1), Ok(Some(1))); +} + +#[test] +fn a_chain_of_uses_is_as_deep_as_what_it_copies() { + let chain = |n: usize| { + let defs: String = (0..n).map(|i| format!("", i + 1)).collect(); + svg(&format!("{defs}")) + }; + assert!(!deep(&chain(10))); + assert!(deep(&chain(DEPTH / 2)), "two elements a link"); + assert!(deep(&chain(5000))); +} + +#[test] +fn chains_of_clips_masks_and_patterns_count_by_attribute_style_and_style_sheet() { + let by_attribute = |n: usize| -> String { + (0..n).map(|i| format!("", i + 1)).collect() + }; + let by_style = |n: usize| -> String { + (0..n).map(|i| format!("", i + 1)).collect() + }; + let by_sheet = |n: usize| -> String { + let rules: String = (0..n).map(|i| format!(".p{i} {{ fill: url(#c{}) }}", i + 1)).collect(); + let patterns: String = + (0..n).map(|i| format!("")).collect(); + format!("{patterns}") + }; + let by_marker = + |n: usize| -> String { (0..n).map(|i| format!("", i + 1)).collect() }; + for chain in [&by_attribute as &dyn Fn(usize) -> String, &by_style, &by_sheet, &by_marker] { + let file = |n| { + svg(&format!( + "{}", + chain(n) + )) + }; + assert!(!deep(&file(5))); + assert!(deep(&file(DEPTH))); + } +} + +#[test] +fn many_paint_servers_used_side_by_side_are_not_a_chain() { + // As drawing apps write gradients: a class each, used by objects that aren't inside one another. + let rules: String = (0..300).map(|i| format!(".c{i}{{fill:url(#g{i})}}")).collect(); + let defs: String = (0..300).map(|i| format!("")).collect(); + let art: String = (0..300).map(|i| format!("")).collect(); + assert!(!deep(&svg(&format!("{defs}{art}")))); +} + +#[test] +fn a_reference_follows_the_first_element_of_its_id() { + // A chain of ``s as deep as the cap, each link's id given again later to an empty group: + // usvg follows the first, so the chain is deep. + let chain: String = (1..DEPTH).map(|i| format!("", i - 1)).collect(); + let shallow: String = (1..DEPTH).map(|i| format!("")).collect(); + let top = format!("", DEPTH - 1); + assert!(deep(&svg(&format!("{chain}{shallow}{top}")))); +} + +#[test] +fn a_loop_of_references_is_not_deep() { + assert!(!deep(&svg(""))); +} + +#[test] +fn an_inherited_paint_is_converted_under_everything_between() { + // Neither the pattern nor the art is too deep; the pattern under the art's last object is. + let half = DEPTH / 2; + let pattern = format!("{}", groups(half)); + assert!(!deep(&svg(&format!("{pattern}{}", groups(half / 2))))); + assert!(deep(&svg(&format!("{pattern}{}", groups(half))))); +} + +#[test] +fn a_style_sheet_that_names_elements_and_cannot_be_read_is_refused() { + let sheet = |css: &str, media: &str| { + svg(&format!("{css}")) + }; + let check = |text: &str| height(&roxmltree::Document::parse(text).unwrap(), DEPTH); + // The root, the rectangle, and under it the pattern its fill names. + assert_eq!(check(&sheet(".a{fill:url(#p)}", "")), Ok(Some(3))); + // An at-rule block, a sheet for another medium and a broken rule: read here they would add + // nothing, read by usvg they might. + assert!(matches!(check(&sheet("@media print{.a{fill:url(#p)}}", "")), Err(Error::Stylesheet(_)))); + assert!(matches!(check(&sheet(".a{fill:url(#p)}", " media='print'")), Err(Error::Stylesheet(_)))); + assert!(matches!(check(&sheet(".a{fill:url(#p)", "")), Err(Error::Stylesheet(_)))); + assert!(matches!(try_parse(&sheet("@media print{.a{fill:url(#p)}}", ""), &shallow()), Err(Error::Stylesheet(_)))); + // The same sheets naming no element add no nesting and are usvg's to read. + assert_eq!(check(&sheet("@media print{.a{fill:red}}", "")), Ok(Some(2))); + assert_eq!(check(&sheet(".a{fill:red", "")), Ok(Some(2))); + assert!(try_parse(&sheet(".a{fill:red}", " media='print'"), &shallow()).is_ok()); +} + +#[test] +fn embedded_svgs_are_read_within_their_budgets() { + let inner = svg(""); + let found = |text: &str, limits: Limits| { + let options = usvg::Options { image_href_resolver: image_resolver(limits), ..Default::default() }; + let mut out = vec![]; + images(parse(text.as_bytes(), &limits, &options).unwrap().root(), &mut out); + out + }; + let one = svg(&embed(&inner)); + assert_eq!(found(&one, shallow()), [true]); + // Bytes: one fits, a second does not; what a refused one would have used is spent. + let two = svg(&format!("{}{}", embed(&inner), embed(&inner))); + assert_eq!(found(&two, shallow()), [true, true]); + assert_eq!(found(&two, Limits { embedded_bytes: inner.len() * 2 - 1, ..shallow() }), [true]); + assert_eq!(found(&one, Limits { embedded_bytes: inner.len() - 1, ..shallow() }), [false; 0]); + // Depth: a document in a document in a document. + let three = svg(&embed(&svg(&embed(&inner)))); + assert_eq!(found(&three, shallow()), [true, true]); + assert_eq!(found(&three, Limits { embedded_depth: 1, ..shallow() }), [true]); + assert_eq!(found(&three, Limits { embedded_depth: 0, ..shallow() }), [false; 0]); + // An embedded document goes through the same checks: too deep, it is left out. + let nested = svg(&embed(&svg(&groups(30)))); + assert_eq!(found(&nested, shallow()), [false; 0]); + let wide = svg(&embed(&svg(&format!("", (0..40).map(|i| format!(" a{i}='1'")).collect::())))); + assert_eq!(found(&wide, shallow()), [true]); + assert_eq!(found(&wide, Limits { attributes: 39, ..shallow() }), [false; 0]); + // A linked file is never read. + assert_eq!(found(&svg(r#""#), shallow()), [false; 0]); +} + +mod draw_checks { + use super::*; + + const GIB: u64 = 1 << 30; + + fn check(body: &str, budget: u64) -> Result<(), draw::Refusal> { + let tree = try_parse(&svg(body), &Limits { depth: 40, ..Limits::default() }).unwrap(); + draw::check(tree.root().children(), usvg::Transform::identity(), 100, 100, 0, budget) + } + + #[test] + fn an_ordinary_drawing_fits() { + let body = " + + + "; + assert_eq!(check(body, GIB), Ok(())); + // The target alone is 100 × 100 × 4 bytes. + assert!(check("", 40_000).is_ok()); + assert!(check("", 39_999).is_ok(), "nothing is charged where nothing draws on a layer"); + } + + #[test] + fn a_filter_region_the_file_sets_is_charged_whatever_the_target() { + let body = " + "; + let refusal = check(body, GIB).unwrap_err(); + assert!(refusal.memory && !refusal.complex && refusal.message.contains("filter"), "{refusal:?}"); + } + + #[test] + fn a_pattern_tile_is_charged_at_its_own_size() { + let body = " + "; + let refusal = check(body, GIB).unwrap_err(); + assert!(refusal.memory && refusal.message.contains("pattern tile"), "{refusal:?}"); + let small = " + "; + assert_eq!(check(small, GIB), Ok(())); + } + + #[test] + fn counts_are_capped_apart_from_memory() { + let primitives = + |n: usize| format!("{}", "".repeat(n)); + assert_eq!(check(&primitives(64), GIB), Ok(())); + let refusal = check(&primitives(65), GIB).unwrap_err(); + assert!(!refusal.memory && refusal.message.contains("primitives"), "{refusal:?}"); + let clips = |n: usize| { + let chain: String = + (0..n).map(|i| format!("", i + 1)).collect(); + format!("{chain}") + }; + assert_eq!(check(&clips(15), GIB), Ok(())); + let refusal = check(&clips(16), GIB).unwrap_err(); + assert!(!refusal.memory && refusal.message.contains("clip-paths"), "{refusal:?}"); + } +} From bd79e326301b1700265b33e86283594fa338ae74 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:47:08 -0500 Subject: [PATCH 023/120] craft-lottie: the document model, with a bounded reader and a writer (#938) `craft_lottie::model` is the Lottie document as the file has it: one struct per object of the lottie-spec 1.0 object model (animation, assets, layers by type, transform, masks, effects, shape items, text, markers, fonts, glyphs), one field per key. `Animation::read` reads within `Limits` (bytes, nesting, layers, assets, shape items); `to_json` writes. Keys the model does not name, values of another type than expected, and layers and shape items of an unknown type stay in `extra` and are written back, so a document survives a read and a write. Animatable properties stay JSON (`Animated`) and are read with `read_prop`. --- README.md | 9 +- crates/lottie/Cargo.toml | 2 +- crates/lottie/README.md | 59 ++++- crates/lottie/src/lib.rs | 32 ++- crates/lottie/src/model/layer.rs | 224 +++++++++++++++++ crates/lottie/src/model/mod.rs | 413 +++++++++++++++++++++++++++++++ crates/lottie/src/model/shape.rs | 393 +++++++++++++++++++++++++++++ crates/lottie/src/model/text.rs | 137 ++++++++++ crates/lottie/src/model_tests.rs | 320 ++++++++++++++++++++++++ 9 files changed, 1560 insertions(+), 29 deletions(-) create mode 100644 crates/lottie/src/model/layer.rs create mode 100644 crates/lottie/src/model/mod.rs create mode 100644 crates/lottie/src/model/shape.rs create mode 100644 crates/lottie/src/model/text.rs create mode 100644 crates/lottie/src/model_tests.rs diff --git a/README.md b/README.md index a29bbfeb0..45361e66e 100644 --- a/README.md +++ b/README.md @@ -174,10 +174,11 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " Header<'a> { } } +#[cfg(test)] +mod model_tests; #[cfg(test)] mod tests; diff --git a/crates/lottie/src/model/layer.rs b/crates/lottie/src/model/layer.rs new file mode 100644 index 000000000..f350234ae --- /dev/null +++ b/crates/lottie/src/model/layer.rs @@ -0,0 +1,224 @@ +//! Layers, their transform, masks and effects. + +use super::{Animated, Lenient, Map, Shape, TextData, fields, object, take}; +use crate::Json; + +object! { + /// A transform (`ks` of a layer, a `tr` shape item). + Transform { + anchor / "a": Animated, + /// The position; see [`Animated::split`] for one split in dimensions. + position / "p": Animated, + /// Percent. + scale / "s": Animated, + /// Degrees. A three-dimensional layer has `rx`, `ry`, `rz` and `or` instead. + rotation / "r": Animated, + rotation_x / "rx": Animated, + rotation_y / "ry": Animated, + rotation_z / "rz": Animated, + orientation / "or": Animated, + /// 0..=100. + opacity / "o": Animated, + skew / "sk": Animated, + skew_axis / "sa": Animated, + } +} + +object! { + Mask { + name / "nm": String, + match_name / "mn": String, + /// `n` none, `a` add, `s` subtract, `i` intersect, `l` lighten, `d` darken, `f` difference. + mode / "mode": String, + inverted / "inv": bool, + /// The path. + path / "pt": Animated, + /// 0..=100. + opacity / "o": Animated, + expansion / "x": Animated, + /// Whether the path is closed, in files that keep the flag outside the path. + closed / "cl": bool, + } +} + +object! { + /// An effect, or one value of an effect. + Effect { + /// The effect type (5 custom, 20 tint, 21 fill, 25 drop shadow, 29 gaussian blur, ...) or the + /// value type (0 slider, 1 angle, 2 colour, 3 point, 4 checkbox, 7 dropdown, 10 layer). + kind / "ty": i64, + name / "nm": String, + match_name / "mn": String, + index / "ix": i64, + /// How many values the effect has. + count / "np": i64, + /// 0 when the effect is switched off. + enabled / "en": i64, + /// The value of an effect value. + value / "v": Animated, + /// The values of an effect. + values / "ef": Vec, + } +} + +fields! { + /// A layer that shows a precomposition. + PrecompLayer { + /// The id of the asset. + ref_id / "refId": String, + width / "w": f64, + height / "h": f64, + /// Time remapping: the precomposition's time in seconds. + time_remap / "tm": Animated, + } +} + +fields! { + SolidLayer { + /// `#rrggbb`. + color / "sc": String, + width / "sw": f64, + height / "sh": f64, + } +} + +/// What a layer shows, by its `ty`. +#[derive(Debug, Clone, PartialEq)] +pub enum LayerContent { + /// `ty` 0. + Precomp(PrecompLayer), + /// `ty` 1. + Solid(SolidLayer), + /// `ty` 2: the id of the image asset (`refId`). + Image(Option), + /// `ty` 3. + Null, + /// `ty` 4: its `shapes`, the topmost first. + Shape(Vec), + /// `ty` 5: its `t`. + Text(Box), + /// Any other `ty` (6 audio, 13 camera, ...), as written; `Null` when the layer has none. The + /// layer's own keys are in its `extra`. + Other(Json), +} + +impl Default for LayerContent { + fn default() -> Self { + Self::Other(Json::Null) + } +} + +fields! { + /// A layer. + Layer { + name / "nm": String, + match_name / "mn": String, + /// The number other layers name it by (`parent`, `tp`). + index / "ind": i64, + parent / "parent": i64, + hidden / "hd": bool, + three_d / "ddd": i64, + /// The first frame the layer shows at, in composition frames. + in_point / "ip": f64, + /// The frame after the last. + out_point / "op": f64, + /// The composition frame of the layer's frame 0. + start_time / "st": f64, + /// Time stretch: 1 is normal speed, 2 half speed. + stretch / "sr": f64, + auto_orient / "ao": i64, + /// The blend mode, 0 normal. + blend_mode / "bm": i64, + transform / "ks": Transform, + has_mask / "hasMask": bool, + masks / "masksProperties": Vec, + effects / "ef": Vec, + /// How the layer is matted: 1 alpha, 2 inverted alpha, 3 luma, 4 inverted luma. + matte_mode / "tt": i64, + /// The `ind` of the matte layer; without it, the layer above. + matte_parent / "tp": i64, + /// 1 when the layer is a matte for another. + is_matte / "td": i64, + collapse_transform / "ct": i64, + /// A CSS class and an element id, for players that render to a page. + class / "cl": String, + element_id / "ln": String, + } + { + content: LayerContent, + /// Every other key of the object, as it was. + extra: Map, + } +} + +impl LayerContent { + fn take(m: &mut Map) -> Self { + let Some(ty) = m.remove("ty") else { return Self::default() }; + match ty.as_i64() { + Some(0) => Self::Precomp(PrecompLayer::take(m)), + Some(1) => Self::Solid(SolidLayer::take(m)), + Some(2) => Self::Image(take(m, "refId")), + Some(3) => Self::Null, + // A shape or text layer without its content is kept as written. + Some(4) => take(m, "shapes").map_or(Self::Other(ty), Self::Shape), + Some(5) => take::(m, "t").map_or(Self::Other(ty), |t| Self::Text(Box::new(t))), + _ => Self::Other(ty), + } + } + + fn put(&self, m: &mut Map) { + let ty = match self { + Self::Precomp(p) => { + p.put(m); + 0 + } + Self::Solid(s) => { + s.put(m); + 1 + } + Self::Image(id) => { + if let Some(id) = id { + m.insert("refId".to_string(), id.to_json()); + } + 2 + } + Self::Null => 3, + Self::Shape(shapes) => { + m.insert("shapes".to_string(), shapes.to_json()); + 4 + } + Self::Text(t) => { + m.insert("t".to_string(), t.to_json()); + 5 + } + Self::Other(ty) => { + if !ty.is_null() { + m.insert("ty".to_string(), ty.clone()); + } + return; + } + }; + m.insert("ty".to_string(), Json::from(ty)); + } +} + +impl Lenient for Layer { + fn from_json(j: Json) -> Result { + match j { + Json::Object(mut m) => { + let content = LayerContent::take(&mut m); + let mut l = Self::take(&mut m); + l.content = content; + l.extra = m; + Ok(l) + } + other => Err(other), + } + } + + fn to_json(&self) -> Json { + let mut m = self.extra.clone(); + self.put(&mut m); + self.content.put(&mut m); + Json::Object(m) + } +} diff --git a/crates/lottie/src/model/mod.rs b/crates/lottie/src/model/mod.rs new file mode 100644 index 000000000..fe7535eee --- /dev/null +++ b/crates/lottie/src/model/mod.rs @@ -0,0 +1,413 @@ +//! The document model: the objects of a Lottie file as the file has them. +//! +//! Each struct is one object of the lottie-spec 1.0 object model, with a field per key it defines. +//! Every field is optional, because files leave keys out. A key the struct does not name, and a +//! key whose value has another type than the struct expects, stay in `extra` as they were, so +//! reading a document and writing it back keeps all of it. +//! +//! Animatable properties are [`Animated`]: the JSON of the property, read with +//! [`Animated::read`]. The meaning of a value depends on the property, which the file does not +//! say, so the model does not decode values. + +mod layer; +mod shape; +mod text; + +pub use layer::{Effect, Layer, LayerContent, Mask, PrecompLayer, SolidLayer, Transform}; +pub use shape::{ + Dash, Ellipse, Fill, GradientColors, GradientFill, GradientStroke, Group, Merge, OffsetPath, Path, PolyStar, PuckerBloat, Rect, Repeater, + RepeaterTransform, RoundCorners, Shape, ShapeKind, Stroke, Trim, Twist, ZigZag, +}; +pub use text::{TextAlign, TextData, TextDocKey, TextDocs, TextDocument, TextFollowPath, TextRange, TextSelector, TextStyle}; + +use crate::{Json, KeyOut, LottieError, Prop, read_prop, write_prop}; + +/// A JSON object. +pub type Map = serde_json::Map; + +/// A value of the model as JSON. `from_json` gives the value back when it has another type, so +/// the caller can keep it. +pub(crate) trait Lenient: Sized { + fn from_json(j: Json) -> Result; + fn to_json(&self) -> Json; +} + +/// A number as JSON: whole numbers without a fraction part, as files have them. +fn number(v: f64) -> Json { + if v.fract() == 0.0 && v.abs() < 9.0e15 { Json::from(v as i64) } else { Json::from(v) } +} + +impl Lenient for f64 { + fn from_json(j: Json) -> Result { + j.as_f64().ok_or(j) + } + fn to_json(&self) -> Json { + number(*self) + } +} + +impl Lenient for i64 { + /// A whole number, however it is written (`1`, `1.0`). + fn from_json(j: Json) -> Result { + let whole = j.as_i64().or_else(|| j.as_f64().filter(|f| f.fract() == 0.0 && f.abs() < 9.0e15).map(|f| f as i64)); + whole.ok_or(j) + } + fn to_json(&self) -> Json { + Json::from(*self) + } +} + +impl Lenient for bool { + /// `true` / `false`, or 1 / 0 (older files). + fn from_json(j: Json) -> Result { + match (j.as_bool(), j.as_f64()) { + (Some(b), _) => Ok(b), + (None, Some(n)) if n == 0.0 || n == 1.0 => Ok(n == 1.0), + _ => Err(j), + } + } + fn to_json(&self) -> Json { + Json::Bool(*self) + } +} + +impl Lenient for String { + fn from_json(j: Json) -> Result { + match j { + Json::String(s) => Ok(s), + other => Err(other), + } + } + fn to_json(&self) -> Json { + Json::String(self.clone()) + } +} + +impl Lenient for Map { + fn from_json(j: Json) -> Result { + match j { + Json::Object(m) => Ok(m), + other => Err(other), + } + } + fn to_json(&self) -> Json { + Json::Object(self.clone()) + } +} + +impl Lenient for Vec { + /// A list. Entries of another type than `T` are left out. + fn from_json(j: Json) -> Result { + match j { + Json::Array(a) => Ok(a.into_iter().filter_map(|v| T::from_json(v).ok()).collect()), + other => Err(other), + } + } + fn to_json(&self) -> Json { + Json::Array(self.iter().map(T::to_json).collect()) + } +} + +/// Takes key `k` out of `m` as a `T`; a value of another type stays in `m`. +fn take(m: &mut Map, k: &str) -> Option { + match T::from_json(m.remove(k)?) { + Ok(v) => Some(v), + Err(j) => { + m.insert(k.to_string(), j); + None + } + } +} + +/// A struct of optional fields, each one key of a JSON object: `field / "key": Type`. `take` +/// moves the keys out of an object and `put` writes them back. Fields after `+` are plain. +macro_rules! fields { + ($(#[$m:meta])* $name:ident { $($(#[$fm:meta])* $field:ident / $json:literal : $ty:ty,)* } $(+ { $($(#[$xm:meta])* $xf:ident : $xt:ty,)* })?) => { + $(#[$m])* + #[derive(Debug, Clone, Default, PartialEq)] + pub struct $name { + $($(#[$fm])* pub $field: Option<$ty>,)* + $($($(#[$xm])* pub $xf: $xt,)*)? + } + + impl $name { + #[allow(clippy::needless_update, dead_code)] + pub(crate) fn take(m: &mut $crate::model::Map) -> Self { + Self { $($field: $crate::model::take(m, $json),)* ..Default::default() } + } + + #[allow(dead_code)] + pub(crate) fn put(&self, m: &mut $crate::model::Map) { + $(if let Some(v) = &self.$field { + m.insert($json.to_string(), $crate::model::Lenient::to_json(v)); + })* + } + } + }; +} + +/// A [`fields!`] struct that is a whole JSON object: the keys it does not name stay in `extra`. +macro_rules! object { + ($(#[$m:meta])* $name:ident { $($body:tt)* }) => { + $crate::model::fields! { + $(#[$m])* + $name { $($body)* } + { + /// Every other key of the object, as it was. + extra: $crate::model::Map, + } + } + + impl $crate::model::Lenient for $name { + fn from_json(j: $crate::Json) -> Result { + match j { + $crate::Json::Object(mut m) => { + let mut o = Self::take(&mut m); + o.extra = m; + Ok(o) + } + other => Err(other), + } + } + + fn to_json(&self) -> $crate::Json { + let mut m = self.extra.clone(); + self.put(&mut m); + $crate::Json::Object(m) + } + } + }; +} +pub(crate) use {fields, object}; + +/// An animatable property as the file has it: `{a, k}` with keyframes or one value, sometimes +/// the bare value. +#[derive(Debug, Clone, PartialEq)] +pub struct Animated(pub Json); + +impl Animated { + /// The property's value or keyframes, and its expression. + pub fn read(&self) -> Prop<'_> { + read_prop(&self.0) + } + + /// A property with one value. + pub fn fixed(value: impl Into) -> Self { + Self(write_prop(value.into(), &[], None)) + } + + /// A keyframed property. + pub fn keyed(keys: &[KeyOut], expression: Option<&str>) -> Self { + Self(write_prop(Json::Null, keys, expression)) + } + + /// The `x`, `y` and `z` properties of a position split in dimensions (`{s: true, x, y, z}`). + pub fn split(&self) -> Option<[Option<&Json>; 3]> { + let on = self.0.get("s").is_some_and(|s| s.as_bool() == Some(true) || s.as_f64() == Some(1.0)); + on.then(|| ["x", "y", "z"].map(|k| self.0.get(k))) + } +} + +impl Lenient for Animated { + fn from_json(j: Json) -> Result { + if j.is_null() { Err(j) } else { Ok(Self(j)) } + } + fn to_json(&self) -> Json { + self.0.clone() + } +} + +object! { + /// The document. + Animation { + /// The version of the exporter that wrote the file. + version / "v": String, + name / "nm": String, + match_name / "mn": String, + /// Frames per second. + frame_rate / "fr": f64, + /// The first frame. + in_point / "ip": f64, + /// The frame after the last. + out_point / "op": f64, + width / "w": f64, + height / "h": f64, + /// 1 when a layer is three-dimensional. + three_d / "ddd": i64, + /// Images and precompositions the layers name by id. + assets / "assets": Vec, + /// The layers, the topmost first. + layers / "layers": Vec, + markers / "markers": Vec, + fonts / "fonts": Fonts, + /// Glyph outlines for players without the fonts. + chars / "chars": Vec, + /// Generator, author, keywords, description. + meta / "meta": Map, + slots / "slots": Map, + } +} + +object! { + /// An asset: an image (`p`, `u`, `e`) or a precomposition (`layers`). + Asset { + id / "id": String, + name / "nm": String, + /// The folder of an image file. + dir / "u": String, + /// The file name of an image, or a `data:` URI when `embedded` is 1. + path / "p": String, + width / "w": f64, + height / "h": f64, + embedded / "e": i64, + /// `"seq"` for an image sequence. + kind / "t": String, + /// The frame rate of a precomposition, when it has its own. + frame_rate / "fr": f64, + /// The layers of a precomposition. + layers / "layers": Vec, + } +} + +object! { + /// A marker on the time line. + Marker { + comment / "cm": String, + /// The frame. + time / "tm": f64, + /// Frames. + duration / "dr": f64, + } +} + +object! { + /// The fonts the text layers use. + Fonts { + list / "list": Vec, + } +} + +object! { + Font { + /// The name text documents refer to. + name / "fName": String, + family / "fFamily": String, + style / "fStyle": String, + path / "fPath": String, + weight / "fWeight": String, + class / "fClass": String, + /// Where the font comes from (0 local, 1 CSS URL, 2 script URL, 3 font URL). + origin / "origin": i64, + ascent / "ascent": f64, + } +} + +object! { + /// The outline of one character. + Char { + character / "ch": String, + family / "fFamily": String, + style / "style": String, + size / "size": f64, + /// The advance width at size 100. + width / "w": f64, + data / "data": CharData, + } +} + +object! { + CharData { + shapes / "shapes": Vec, + } +} + +/// What a document may hold. Each is checked by [`Animation::read_with_limits`]. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Limits { + /// Bytes of JSON. + pub max_bytes: usize, + /// Nesting of arrays and objects. + pub max_depth: usize, + /// Layers of the document and of every precomposition together. + pub max_layers: usize, + pub max_assets: usize, + /// Shape items of every layer and character together, groups and their contents counted. + pub max_shapes: usize, +} + +impl Default for Limits { + fn default() -> Self { + Self { max_bytes: 256 << 20, max_depth: 128, max_layers: 100_000, max_assets: 20_000, max_shapes: 2_000_000 } + } +} + +/// Whether `j` nests deeper than `max` (a scalar is depth 0). Not recursive. +fn deeper_than(j: &Json, max: usize) -> bool { + let mut stack = vec![(j, 0usize)]; + while let Some((v, depth)) = stack.pop() { + match v { + Json::Array(_) | Json::Object(_) if depth >= max => return true, + Json::Array(a) => stack.extend(a.iter().map(|c| (c, depth + 1))), + Json::Object(m) => stack.extend(m.values().map(|c| (c, depth + 1))), + _ => {} + } + } + false +} + +fn count_shapes(shapes: &[Shape]) -> usize { + shapes.iter().map(|s| 1 + if let ShapeKind::Group(g) = &s.kind { count_shapes(g.items.as_deref().unwrap_or_default()) } else { 0 }).sum() +} + +fn count_layer_shapes(layers: &[Layer]) -> usize { + layers.iter().map(|l| if let LayerContent::Shape(s) = &l.content { count_shapes(s) } else { 0 }).sum() +} + +impl Animation { + /// Reads a document with the default [`Limits`]. + pub fn read(bytes: &[u8]) -> Result { + Self::read_with_limits(bytes, &Limits::default()) + } + + /// Reads a document. Any JSON object reads: keys of another type than the model expects are + /// kept in `extra`, and a document without layers is a document without layers. + pub fn read_with_limits(bytes: &[u8], limits: &Limits) -> Result { + if bytes.len() > limits.max_bytes { + return Err(LottieError::TooLarge { what: "bytes", limit: limits.max_bytes }); + } + Self::from_document(crate::parse_document(bytes)?, limits) + } + + /// The model of parsed JSON. + pub fn from_document(doc: Json, limits: &Limits) -> Result { + if deeper_than(&doc, limits.max_depth) { + return Err(LottieError::TooLarge { what: "nesting levels", limit: limits.max_depth }); + } + let doc = ::from_json(doc).map_err(|_| LottieError::Parse("the document is not an object".into()))?; + let assets = doc.assets.as_deref().unwrap_or_default(); + let top = doc.layers.as_deref().unwrap_or_default(); + let nested = || assets.iter().map(|a| a.layers.as_deref().unwrap_or_default()); + let chars = doc.chars.as_deref().unwrap_or_default().iter().filter_map(|c| c.data.as_ref()?.shapes.as_deref()); + let check = |what, n: usize, limit| if n > limit { Err(LottieError::TooLarge { what, limit }) } else { Ok(()) }; + check("assets", assets.len(), limits.max_assets)?; + check("layers", top.len() + nested().map(<[Layer]>::len).sum::(), limits.max_layers)?; + let shapes = count_layer_shapes(top) + nested().map(count_layer_shapes).sum::() + chars.map(count_shapes).sum::(); + check("shape items", shapes, limits.max_shapes)?; + Ok(doc) + } + + /// The document as JSON. + pub fn to_json(&self) -> Json { + Lenient::to_json(self) + } + + /// The document as compact JSON text. + pub fn to_vec(&self) -> Vec { + // Writing a `Value` to a `Vec` cannot fail. + serde_json::to_vec(&self.to_json()).unwrap_or_default() + } + + /// The precomposition or image asset with this id. + pub fn asset(&self, id: &str) -> Option<&Asset> { + self.assets.as_deref()?.iter().find(|a| a.id.as_deref() == Some(id)) + } +} diff --git a/crates/lottie/src/model/shape.rs b/crates/lottie/src/model/shape.rs new file mode 100644 index 000000000..e1e95747e --- /dev/null +++ b/crates/lottie/src/model/shape.rs @@ -0,0 +1,393 @@ +//! Shape items: the contents of a shape layer. + +use super::{Animated, Lenient, Map, Transform, fields, object}; +use crate::Json; + +fields! { + /// `gr`: a group. Its last item is usually its transform (`tr`). + Group { + /// How many items it has. + count / "np": f64, + items / "it": Vec, + } +} + +fields! { + /// `rc`. + Rect { + /// The drawing direction: 1 clockwise, 3 counter-clockwise. + direction / "d": i64, + /// The centre. + position / "p": Animated, + size / "s": Animated, + /// The corner radius. + roundness / "r": Animated, + } +} + +fields! { + /// `el`. + Ellipse { + direction / "d": i64, + position / "p": Animated, + size / "s": Animated, + } +} + +fields! { + /// `sr`: a star or a polygon. + PolyStar { + direction / "d": i64, + /// 1 star, 2 polygon. + star_type / "sy": i64, + position / "p": Animated, + points / "pt": Animated, + rotation / "r": Animated, + outer_radius / "or": Animated, + /// Percent. + outer_roundness / "os": Animated, + /// Stars only. + inner_radius / "ir": Animated, + inner_roundness / "is": Animated, + } +} + +fields! { + /// `sh`: a bezier path. + Path { + direction / "d": i64, + path / "ks": Animated, + /// Whether the path is closed, in files that keep the flag outside the path. + closed / "closed": bool, + } +} + +fields! { + /// `fl`. + Fill { + color / "c": Animated, + /// 0..=100. + opacity / "o": Animated, + /// 1 non-zero, 2 even-odd. + rule / "r": i64, + } +} + +object! { + /// One entry of a dash pattern. + Dash { + /// `d` dash, `g` gap, `o` offset. + kind / "n": String, + name / "nm": String, + match_name / "mn": String, + length / "v": Animated, + } +} + +fields! { + /// `st`. + Stroke { + color / "c": Animated, + opacity / "o": Animated, + width / "w": Animated, + /// 1 butt, 2 round, 3 square. + cap / "lc": i64, + /// 1 miter, 2 round, 3 bevel. + join / "lj": i64, + miter_limit / "ml": f64, + /// The miter limit as a property. + miter_limit_animated / "ml2": Animated, + dashes / "d": Vec, + } +} + +object! { + /// The colours of a gradient: the stop list is read with [`Gradient`](crate::Gradient). + GradientColors { + /// How many colour stops the list has. + count / "p": i64, + stops / "k": Animated, + } +} + +fields! { + /// `gf`. + GradientFill { + colors / "g": GradientColors, + start / "s": Animated, + end / "e": Animated, + /// 1 linear, 2 radial. + gradient_type / "t": i64, + /// Radial: where the highlight is along the radius, percent. + highlight_length / "h": Animated, + highlight_angle / "a": Animated, + opacity / "o": Animated, + rule / "r": i64, + } +} + +fields! { + /// `gs`. + GradientStroke { + colors / "g": GradientColors, + start / "s": Animated, + end / "e": Animated, + gradient_type / "t": i64, + highlight_length / "h": Animated, + highlight_angle / "a": Animated, + opacity / "o": Animated, + width / "w": Animated, + cap / "lc": i64, + join / "lj": i64, + miter_limit / "ml": f64, + miter_limit_animated / "ml2": Animated, + dashes / "d": Vec, + } +} + +fields! { + /// `tm`: trim paths. + Trim { + /// Percent. + start / "s": Animated, + end / "e": Animated, + /// Degrees. + offset / "o": Animated, + /// 1 all paths together, 2 each on its own. + multiple / "m": i64, + } +} + +fields! { + /// `rd`. + RoundCorners { + radius / "r": Animated, + } +} + +object! { + /// The transform a repeater applies to each copy. + RepeaterTransform { + anchor / "a": Animated, + position / "p": Animated, + scale / "s": Animated, + rotation / "r": Animated, + start_opacity / "so": Animated, + end_opacity / "eo": Animated, + skew / "sk": Animated, + skew_axis / "sa": Animated, + } +} + +fields! { + /// `rp`. + Repeater { + copies / "c": Animated, + offset / "o": Animated, + /// 1 each copy above the one before, 2 below. + composite / "m": i64, + transform / "tr": RepeaterTransform, + } +} + +fields! { + /// `mm`: merge paths. + Merge { + /// 1 merge, 2 add, 3 subtract, 4 intersect, 5 exclude intersections. + mode / "mm": i64, + } +} + +fields! { + /// `pb`. + PuckerBloat { + /// Percent. + amount / "a": Animated, + } +} + +fields! { + /// `tw`. + Twist { + angle / "a": Animated, + center / "c": Animated, + } +} + +fields! { + /// `zz`. + ZigZag { + /// The size of a ridge. + amount / "r": Animated, + /// Ridges per segment. + frequency / "s": Animated, + /// 1 corner, 2 smooth. + point_type / "pt": Animated, + } +} + +fields! { + /// `op`. + OffsetPath { + amount / "a": Animated, + join / "lj": i64, + miter_limit / "ml": Animated, + } +} + +/// What a shape item is, by its `ty`. +#[derive(Debug, Clone, PartialEq)] +pub enum ShapeKind { + Group(Group), + Rect(Rect), + Ellipse(Ellipse), + PolyStar(PolyStar), + Path(Path), + Fill(Fill), + Stroke(Stroke), + GradientFill(GradientFill), + GradientStroke(GradientStroke), + /// `tr`: the transform of the group it is in. Its keys are those of a layer transform. + Transform(Box), + Trim(Trim), + RoundCorners(RoundCorners), + Repeater(Repeater), + Merge(Merge), + PuckerBloat(PuckerBloat), + Twist(Twist), + ZigZag(ZigZag), + OffsetPath(OffsetPath), + /// Any other `ty`, as written; `Null` when the item has none. The item's own keys are in its + /// `extra`. + Other(Json), +} + +impl Default for ShapeKind { + fn default() -> Self { + Self::Other(Json::Null) + } +} + +fields! { + /// A shape item. + Shape { + name / "nm": String, + match_name / "mn": String, + hidden / "hd": bool, + blend_mode / "bm": i64, + /// The property index expressions refer to. + index / "ix": i64, + class / "cl": String, + element_id / "ln": String, + } + { + kind: ShapeKind, + /// Every other key of the object, as it was. + extra: Map, + } +} + +impl ShapeKind { + fn take(m: &mut Map) -> Self { + let Some(ty) = m.remove("ty") else { return Self::default() }; + match ty.as_str().unwrap_or_default() { + "gr" => Self::Group(Group::take(m)), + "rc" => Self::Rect(Rect::take(m)), + "el" => Self::Ellipse(Ellipse::take(m)), + "sr" => Self::PolyStar(PolyStar::take(m)), + "sh" => Self::Path(Path::take(m)), + "fl" => Self::Fill(Fill::take(m)), + "st" => Self::Stroke(Stroke::take(m)), + "gf" => Self::GradientFill(GradientFill::take(m)), + "gs" => Self::GradientStroke(GradientStroke::take(m)), + // Only the transform's keys: the item's others (`nm`, `hd`, ...) stay with the item. + "tr" => Self::Transform(Box::new(Transform::take(m))), + "tm" => Self::Trim(Trim::take(m)), + "rd" => Self::RoundCorners(RoundCorners::take(m)), + "rp" => Self::Repeater(Repeater::take(m)), + "mm" => Self::Merge(Merge::take(m)), + "pb" => Self::PuckerBloat(PuckerBloat::take(m)), + "tw" => Self::Twist(Twist::take(m)), + "zz" => Self::ZigZag(ZigZag::take(m)), + "op" => Self::OffsetPath(OffsetPath::take(m)), + _ => Self::Other(ty), + } + } + + /// The `ty` of the item; `None` for [`ShapeKind::Other`]. + pub fn tag(&self) -> Option<&'static str> { + Some(match self { + Self::Group(_) => "gr", + Self::Rect(_) => "rc", + Self::Ellipse(_) => "el", + Self::PolyStar(_) => "sr", + Self::Path(_) => "sh", + Self::Fill(_) => "fl", + Self::Stroke(_) => "st", + Self::GradientFill(_) => "gf", + Self::GradientStroke(_) => "gs", + Self::Transform(_) => "tr", + Self::Trim(_) => "tm", + Self::RoundCorners(_) => "rd", + Self::Repeater(_) => "rp", + Self::Merge(_) => "mm", + Self::PuckerBloat(_) => "pb", + Self::Twist(_) => "tw", + Self::ZigZag(_) => "zz", + Self::OffsetPath(_) => "op", + Self::Other(_) => return None, + }) + } + + fn put(&self, m: &mut Map) { + match self { + Self::Group(v) => v.put(m), + Self::Rect(v) => v.put(m), + Self::Ellipse(v) => v.put(m), + Self::PolyStar(v) => v.put(m), + Self::Path(v) => v.put(m), + Self::Fill(v) => v.put(m), + Self::Stroke(v) => v.put(m), + Self::GradientFill(v) => v.put(m), + Self::GradientStroke(v) => v.put(m), + Self::Transform(v) => v.put(m), + Self::Trim(v) => v.put(m), + Self::RoundCorners(v) => v.put(m), + Self::Repeater(v) => v.put(m), + Self::Merge(v) => v.put(m), + Self::PuckerBloat(v) => v.put(m), + Self::Twist(v) => v.put(m), + Self::ZigZag(v) => v.put(m), + Self::OffsetPath(v) => v.put(m), + Self::Other(ty) => { + if !ty.is_null() { + m.insert("ty".to_string(), ty.clone()); + } + } + } + if let Some(tag) = self.tag() { + m.insert("ty".to_string(), Json::from(tag)); + } + } +} + +impl Lenient for Shape { + fn from_json(j: Json) -> Result { + match j { + Json::Object(mut m) => { + let kind = ShapeKind::take(&mut m); + let mut s = Self::take(&mut m); + s.kind = kind; + s.extra = m; + Ok(s) + } + other => Err(other), + } + } + + fn to_json(&self) -> Json { + let mut m = self.extra.clone(); + self.put(&mut m); + self.kind.put(&mut m); + Json::Object(m) + } +} diff --git a/crates/lottie/src/model/text.rs b/crates/lottie/src/model/text.rs new file mode 100644 index 000000000..8807c01d8 --- /dev/null +++ b/crates/lottie/src/model/text.rs @@ -0,0 +1,137 @@ +//! Text layers: the documents, the range animators and the path options. + +use super::{Animated, object}; + +object! { + /// The `t` of a text layer. + TextData { + documents / "d": TextDocs, + /// Range animators. + ranges / "a": Vec, + alignment / "m": TextAlign, + follow_path / "p": TextFollowPath, + } +} + +object! { + /// The text over time. It changes in steps: each key holds until the next. + TextDocs { + keys / "k": Vec, + expression / "x": String, + } +} + +object! { + TextDocKey { + /// The frame. + time / "t": f64, + document / "s": TextDocument, + } +} + +object! { + /// The text and how it is set. + TextDocument { + /// The font: the `fName` of an entry of the document's font list. + font / "f": String, + size / "s": f64, + /// `[r, g, b]`, 0..=1. + fill / "fc": Vec, + stroke / "sc": Vec, + stroke_width / "sw": f64, + /// Whether the stroke is drawn over the fill. + stroke_over_fill / "of": bool, + line_height / "lh": f64, + baseline_shift / "ls": f64, + /// Thousandths of an em. + tracking / "tr": f64, + /// 0 left, 1 right, 2 centre, 3 to 6 justified. + justify / "j": i64, + /// Lines are separated by `\r`. + text / "t": String, + /// The size of the text box. + box_size / "sz": Vec, + box_position / "ps": Vec, + /// 0 as typed, 1 all caps, 2 small caps. + caps / "ca": i64, + } +} + +object! { + /// A range animator: what changes (`a`) and for which characters (`s`). + TextRange { + name / "nm": String, + selector / "s": TextSelector, + style / "a": TextStyle, + } +} + +object! { + TextSelector { + /// 1 the range selector. + kind / "t": i64, + start / "s": Animated, + end / "e": Animated, + offset / "o": Animated, + /// The largest amount, percent. + amount / "a": Animated, + /// 1 characters, 2 characters without spaces, 3 words, 4 lines. + based_on / "b": i64, + /// 1 percent, 2 index. + units / "r": i64, + /// 1 square, 2 ramp up, 3 ramp down, 4 triangle, 5 round, 6 smooth. + shape / "sh": i64, + randomize / "rn": i64, + ease_high / "xe": Animated, + ease_low / "ne": Animated, + smoothness / "sm": Animated, + } +} + +object! { + /// What a range animator changes. + TextStyle { + position / "p": Animated, + anchor / "a": Animated, + scale / "s": Animated, + rotation / "r": Animated, + rotation_x / "rx": Animated, + rotation_y / "ry": Animated, + skew / "sk": Animated, + skew_axis / "sa": Animated, + opacity / "o": Animated, + fill / "fc": Animated, + fill_hue / "fh": Animated, + fill_saturation / "fs": Animated, + fill_brightness / "fb": Animated, + fill_opacity / "fo": Animated, + stroke / "sc": Animated, + stroke_width / "sw": Animated, + stroke_opacity / "so": Animated, + tracking / "t": Animated, + line_spacing / "ls": Animated, + blur / "bl": Animated, + } +} + +object! { + TextAlign { + /// What the anchor point is of: 1 characters, 2 words, 3 lines, 4 all. + grouping / "g": i64, + /// Percent. + anchor / "a": Animated, + } +} + +object! { + /// Text on a mask path. + TextFollowPath { + /// The index of the mask. + mask / "m": i64, + first_margin / "f": Animated, + last_margin / "l": Animated, + reverse / "r": Animated, + force_alignment / "a": Animated, + perpendicular / "p": Animated, + } +} diff --git a/crates/lottie/src/model_tests.rs b/crates/lottie/src/model_tests.rs new file mode 100644 index 000000000..e670aa18e --- /dev/null +++ b/crates/lottie/src/model_tests.rs @@ -0,0 +1,320 @@ +use serde_json::json; + +use crate::model::{Animated, Animation, Asset, Fill, Group, Layer, LayerContent, Limits, Marker, Rect, Shape, ShapeKind, SolidLayer, Transform}; +use crate::{Json, KeyOut, LottieError, PropValue, Tangents}; + +fn read(doc: &Json) -> Animation { + Animation::from_document(doc.clone(), &Limits::default()).unwrap() +} + +/// A document with every layer type, every shape item, masks, effects, text, markers, fonts, +/// glyphs and both kinds of asset. +fn everything() -> Json { + let p = |k: Json| json!({"a": 0, "k": k}); + let ks = json!({"a": p(json!([0, 0, 0])), "p": p(json!([50, 50, 0])), "s": p(json!([100, 100, 100])), "r": p(json!(0)), "o": p(json!(100))}); + let shapes = json!([ + {"ty": "gr", "nm": "Group", "np": 3, "it": [ + {"ty": "rc", "nm": "Rect", "d": 1, "p": p(json!([0, 0])), "s": p(json!([10, 20])), "r": p(json!(2))}, + {"ty": "el", "d": 3, "p": p(json!([0, 0])), "s": p(json!([5, 5]))}, + {"ty": "sr", "sy": 1, "d": 1, "p": p(json!([0, 0])), "pt": p(json!(5)), "r": p(json!(0)), "or": p(json!(10)), "os": p(json!(0)), "ir": p(json!(4)), "is": p(json!(0))}, + {"ty": "sh", "d": 1, "ks": p(json!({"c": true, "v": [[0, 0], [1, 0]], "i": [[0, 0], [0, 0]], "o": [[0, 0], [0, 0]]}))}, + {"ty": "fl", "c": p(json!([1, 0, 0, 1])), "o": p(json!(100)), "r": 1, "bm": 0}, + {"ty": "st", "c": p(json!([0, 0, 1, 1])), "o": p(json!(50)), "w": p(json!(2)), "lc": 2, "lj": 1, "ml": 4, + "d": [{"n": "d", "nm": "dash", "v": p(json!(3))}, {"n": "g", "nm": "gap", "v": p(json!(1))}, {"n": "o", "nm": "offset", "v": p(json!(0))}]}, + {"ty": "gf", "g": {"p": 2, "k": p(json!([0, 1, 0, 0, 1, 0, 0, 1]))}, "s": p(json!([0, 0])), "e": p(json!([10, 0])), "t": 2, "h": p(json!(0)), "a": p(json!(0)), "o": p(json!(100)), "r": 2}, + {"ty": "gs", "g": {"p": 2, "k": p(json!([0, 1, 0, 0, 1, 0, 0, 1]))}, "s": p(json!([0, 0])), "e": p(json!([10, 0])), "t": 1, "o": p(json!(100)), "w": p(json!(1)), "lc": 1, "lj": 2, "ml2": p(json!(4))}, + {"ty": "tm", "s": p(json!(0)), "e": p(json!(50)), "o": p(json!(0)), "m": 1}, + {"ty": "rd", "r": p(json!(3))}, + {"ty": "rp", "c": p(json!(3)), "o": p(json!(0)), "m": 1, "tr": {"p": p(json!([10, 0])), "s": p(json!([100, 100])), "so": p(json!(100)), "eo": p(json!(0))}}, + {"ty": "mm", "mm": 2}, + {"ty": "pb", "a": p(json!(10))}, + {"ty": "tw", "a": p(json!(30)), "c": p(json!([0, 0]))}, + {"ty": "zz", "r": p(json!(5)), "s": p(json!(3)), "pt": p(json!(1))}, + {"ty": "op", "a": p(json!(2)), "lj": 1, "ml": p(json!(4))}, + {"ty": "tr", "nm": "Transform", "a": p(json!([0, 0])), "p": p(json!([0, 0])), "s": p(json!([100, 100])), "r": p(json!(0)), "o": p(json!(100)), "sk": p(json!(0)), "sa": p(json!(0))} + ]}, + {"ty": "future", "nm": "Later", "q": [1, 2]} + ]); + let text = json!({ + "d": {"k": [{"t": 0, "s": {"f": "Sans-Bold", "s": 24, "fc": [1, 1, 1], "sc": [0, 0, 0], "sw": 1, "of": true, "lh": 28.8, "ls": 0, "tr": 20, "j": 2, "t": "Hi\rthere", "sz": [200, 80], "ps": [-100, -40], "ca": 0}}]}, + "a": [{"nm": "Fade", "s": {"t": 1, "s": p(json!(0)), "e": p(json!(100)), "o": p(json!(0)), "a": p(json!(100)), "b": 1, "r": 1, "sh": 1, "rn": 0}, "a": {"o": p(json!(0)), "p": p(json!([0, 10])), "t": p(json!(5))}}], + "m": {"g": 1, "a": p(json!([0, 0]))}, + "p": {"m": 0, "f": p(json!(0)), "l": p(json!(0)), "r": p(json!(0)), "a": p(json!(0)), "p": p(json!(1))} + }); + json!({ + "v": "5.9.0", "nm": "All", "fr": 29.97, "ip": 0, "op": 90, "w": 320, "h": 240, "ddd": 0, + "meta": {"g": "test"}, + "markers": [{"cm": "start", "tm": 0, "dr": 10}], + "fonts": {"list": [{"fName": "Sans-Bold", "fFamily": "Sans", "fStyle": "Bold", "ascent": 75.5, "origin": 0}]}, + "chars": [{"ch": "H", "fFamily": "Sans", "style": "Bold", "size": 24, "w": 70, "data": {"shapes": [{"ty": "gr", "it": [{"ty": "sh", "ks": p(json!({"c": true, "v": [], "i": [], "o": []}))}]}]}}], + "assets": [ + {"id": "img", "nm": "Photo", "u": "images/", "p": "a.png", "w": 8, "h": 8, "e": 0}, + {"id": "pre", "nm": "Inner", "fr": 30, "layers": [{"ty": 3, "nm": "Null", "ind": 1, "ip": 0, "op": 90, "st": 0, "sr": 1, "ks": ks}]} + ], + "layers": [ + {"ty": 0, "nm": "Precomp", "ind": 1, "refId": "pre", "w": 320, "h": 240, "tm": p(json!(0)), "ip": 0, "op": 90, "st": 0, "sr": 1, "ks": ks, "tt": 1, "tp": 2}, + {"ty": 1, "nm": "Solid", "ind": 2, "sc": "#ff8000", "sw": 320, "sh": 240, "td": 1, "ip": 0, "op": 90, "st": 0, "sr": 1, "ks": ks, "hasMask": true, + "masksProperties": [{"nm": "M", "mode": "s", "inv": true, "pt": p(json!({"c": true, "v": [], "i": [], "o": []})), "o": p(json!(100)), "x": p(json!(0))}], + "ef": [{"ty": 29, "nm": "Blur", "np": 3, "en": 1, "ef": [{"ty": 0, "nm": "Blurriness", "v": p(json!(4))}, {"ty": 7, "nm": "Dimensions", "v": p(json!(1))}]}]}, + {"ty": 2, "nm": "Image", "ind": 3, "refId": "img", "parent": 5, "hd": false, "bm": 3, "ao": 0, "ip": 0, "op": 90, "st": 0, "sr": 1, "ks": ks}, + {"ty": 3, "nm": "Null", "ind": 5, "ddd": 1, "ip": 0, "op": 90, "st": 0, "sr": 1, "ks": {"p": {"s": true, "x": p(json!(1)), "y": p(json!(2))}, "rx": p(json!(0)), "ry": p(json!(0)), "rz": p(json!(0)), "or": p(json!([0, 0, 0]))}}, + {"ty": 4, "nm": "Shapes", "ind": 6, "ip": 0, "op": 90, "st": 0, "sr": 2.5, "ct": 0, "ks": ks, "shapes": shapes}, + {"ty": 5, "nm": "Text", "ind": 7, "ip": 0, "op": 90, "st": 0, "sr": 1, "ks": ks, "t": text}, + {"ty": 13, "nm": "Camera", "ind": 8, "pe": p(json!(500))} + ] + }) +} + +#[test] +fn every_object_reads_typed_and_writes_back_the_same() { + let doc = everything(); + let a = read(&doc); + assert_eq!((a.version.as_deref(), a.frame_rate, a.width, a.three_d), (Some("5.9.0"), Some(29.97), Some(320.0), Some(0))); + assert!(a.extra.is_empty(), "{:?}", a.extra); + assert_eq!( + a.markers.as_deref(), + Some(&[Marker { comment: Some("start".into()), time: Some(0.0), duration: Some(10.0), ..Marker::default() }][..]) + ); + assert_eq!(a.fonts.as_ref().unwrap().list.as_ref().unwrap()[0].ascent, Some(75.5)); + assert_eq!(a.chars.as_ref().unwrap()[0].data.as_ref().unwrap().shapes.as_ref().unwrap().len(), 1); + assert_eq!(a.asset("img").unwrap().path.as_deref(), Some("a.png")); + assert_eq!(a.asset("pre").unwrap().layers.as_ref().unwrap()[0].content, LayerContent::Null); + assert!(a.asset("none").is_none()); + + assert_eq!(a.to_json(), doc); + assert_eq!(Animation::read(&a.to_vec()).unwrap(), a); +} + +#[test] +fn every_layer_type_reads_typed() { + let a = read(&everything()); + let layers = a.layers.as_deref().unwrap(); + let LayerContent::Precomp(pre) = &layers[0].content else { panic!("precomp") }; + assert_eq!((pre.ref_id.as_deref(), pre.width, layers[0].matte_mode, layers[0].matte_parent), (Some("pre"), Some(320.0), Some(1), Some(2))); + assert_eq!(layers[1].content, LayerContent::Solid(SolidLayer { color: Some("#ff8000".into()), width: Some(320.0), height: Some(240.0) })); + let mask = &layers[1].masks.as_ref().unwrap()[0]; + assert_eq!((mask.mode.as_deref(), mask.inverted), (Some("s"), Some(true))); + let blur = &layers[1].effects.as_ref().unwrap()[0]; + assert_eq!((blur.kind, blur.values.as_ref().unwrap().len(), blur.values.as_ref().unwrap()[1].kind), (Some(29), 2, Some(7))); + assert_eq!(layers[2].content, LayerContent::Image(Some("img".into()))); + assert_eq!((layers[2].parent, layers[2].hidden, layers[2].blend_mode), (Some(5), Some(false), Some(3))); + let split = layers[3].transform.as_ref().unwrap().position.as_ref().unwrap().split().unwrap(); + assert!(split[0].is_some() && split[1].is_some() && split[2].is_none()); + assert!(layers[0].transform.as_ref().unwrap().position.as_ref().unwrap().split().is_none()); + assert_eq!(layers[4].stretch, Some(2.5)); + let LayerContent::Text(t) = &layers[5].content else { panic!("text") }; + let d = t.documents.as_ref().unwrap().keys.as_ref().unwrap()[0].document.as_ref().unwrap(); + assert_eq!( + (d.text.as_deref(), d.justify, d.fill.as_deref(), d.stroke_over_fill), + (Some("Hi\rthere"), Some(2), Some(&[1.0, 1.0, 1.0][..]), Some(true)) + ); + assert!(t.ranges.as_ref().unwrap()[0].style.as_ref().unwrap().tracking.is_some()); + assert_eq!(t.follow_path.as_ref().unwrap().mask, Some(0)); + assert_eq!(layers[6].content, LayerContent::Other(json!(13))); + assert!(layers[6].extra.contains_key("pe"), "an unknown layer keeps its own keys"); +} + +#[test] +fn every_shape_item_reads_typed() { + let a = read(&everything()); + let LayerContent::Shape(shapes) = &a.layers.as_deref().unwrap()[4].content else { panic!("shapes") }; + let ShapeKind::Group(g) = &shapes[0].kind else { panic!("group") }; + let tags: Vec<_> = g.items.as_ref().unwrap().iter().map(|s| s.kind.tag().unwrap()).collect(); + assert_eq!(tags, ["rc", "el", "sr", "sh", "fl", "st", "gf", "gs", "tm", "rd", "rp", "mm", "pb", "tw", "zz", "op", "tr"]); + let items = g.items.as_ref().unwrap(); + assert!(items.iter().all(|s| s.extra.is_empty()), "every key of every item is modelled"); + let ShapeKind::Stroke(st) = &items[5].kind else { panic!("stroke") }; + assert_eq!((st.cap, st.miter_limit, st.dashes.as_ref().unwrap().len()), (Some(2), Some(4.0), 3)); + let ShapeKind::GradientFill(gf) = &items[6].kind else { panic!("gradient") }; + let colors = gf.colors.as_ref().unwrap(); + let PropValue::Static(stops) = colors.stops.as_ref().unwrap().read().value else { panic!("static") }; + assert_eq!(crate::Gradient::from_flat(stops, colors.count.unwrap() as usize).unwrap().colors.len(), 2); + assert_eq!((items[16].name.as_deref(), matches!(items[16].kind, ShapeKind::Transform(_))), (Some("Transform"), true)); + assert_eq!(shapes[1].kind, ShapeKind::Other(json!("future"))); + assert_eq!(shapes[1].extra.get("q"), Some(&json!([1, 2]))); +} + +#[test] +fn what_the_model_does_not_know_is_kept() { + // Unknown keys at every level, known keys of another type, entries that are not objects. + let doc = json!({ + "w": "wide", "h": 10, "nm": 7, "tiny": {"x": 1}, "assets": {"not": "a list"}, + "layers": [ + {"ty": 4, "nm": ["n"], "ind": 1.5, "hd": 2, "ks": 5, "shapes": [{"ty": "rc", "d": "cw", "p": null, "zzz": 1}, {"nm": "no type"}], "custom": true}, + {"ty": 4, "nm": "no shapes"}, + {"ty": 5, "t": "not an object"}, + {"ty": "4", "shapes": []}, + {"nm": "no type at all"} + ] + }); + let a = read(&doc); + assert_eq!((a.width, a.height, &a.name, &a.assets), (None, Some(10.0), &None, &None)); + assert_eq!(a.extra.get("w"), Some(&json!("wide"))); + let layers = a.layers.as_deref().unwrap(); + assert_eq!((&layers[0].name, layers[0].index, layers[0].hidden, &layers[0].transform), (&None, None, None, &None)); + let LayerContent::Shape(shapes) = &layers[0].content else { panic!("shapes") }; + let ShapeKind::Rect(rc) = &shapes[0].kind else { panic!("rect") }; + assert_eq!(rc, &Rect::default()); + assert_eq!(shapes[1].kind, ShapeKind::Other(Json::Null)); + assert_eq!(layers[1].content, LayerContent::Other(json!(4))); + assert_eq!(layers[2].content, LayerContent::Other(json!(5))); + assert_eq!(layers[3].content, LayerContent::Other(json!("4"))); + assert_eq!(layers[4].content, LayerContent::Other(Json::Null)); + assert_eq!(a.to_json(), doc); + + // List entries of another type are the one thing left out. + let a = read(&json!({"layers": [1, "two", {"ty": 3}, null], "markers": [[], {"tm": 3}]})); + assert_eq!(a.to_json(), json!({"layers": [{"ty": 3}], "markers": [{"tm": 3}]})); +} + +#[test] +fn numbers_and_flags_are_read_however_written() { + let a = read( + &json!({"fr": 30.0, "ddd": 1.0, "w": 1e3, "layers": [{"ty": 3.0, "ind": 4.0, "hd": 1, "hasMask": 0, "sr": 0.5, "parent": 9007199254740993u64}]}), + ); + let l = &a.layers.as_ref().unwrap()[0]; + assert_eq!((a.three_d, l.index, l.hidden, l.has_mask, l.stretch), (Some(1), Some(4), Some(true), Some(false), Some(0.5))); + assert_eq!(l.content, LayerContent::Other(json!(3.0)), "a `ty` is an integer"); + // Written back: whole numbers without a fraction, flags as booleans. + let out = a.to_json(); + assert_eq!(serde_json::to_string(&out["fr"]).unwrap(), "30"); + assert_eq!(serde_json::to_string(&out["w"]).unwrap(), "1000"); + assert_eq!(out["layers"][0]["hd"], json!(true)); + assert_eq!(out["layers"][0]["sr"], json!(0.5)); + assert_eq!(out["layers"][0]["parent"], json!(9007199254740993u64)); + // Numbers that are not finite have no JSON form. + let a = Animation { frame_rate: Some(f64::NAN), width: Some(f64::INFINITY), ..Animation::default() }; + assert_eq!(a.to_json(), json!({"fr": null, "w": null})); +} + +#[test] +fn a_document_built_in_code_reads_back() { + let lin = Tangents { ox: 0.25, oy: 0.25, ix: 0.75, iy: 0.75 }; + let keys = [ + KeyOut { t: 0.0, s: json!([0]), hold: false, ease: vec![lin], spatial: None }, + KeyOut { t: 30.0, s: json!([100]), hold: false, ease: vec![], spatial: None }, + ]; + let rect = Shape { + name: Some("R".into()), + kind: ShapeKind::Rect(Rect { size: Some(Animated::fixed(json!([10, 10]))), ..Rect::default() }), + ..Shape::default() + }; + let fill = Shape { kind: ShapeKind::Fill(Fill { color: Some(Animated::fixed(json!([1, 0, 0, 1]))), ..Fill::default() }), ..Shape::default() }; + let group = Shape { kind: ShapeKind::Group(Group { count: Some(2.0), items: Some(vec![rect, fill]) }), ..Shape::default() }; + let layer = Layer { + name: Some("L".into()), + index: Some(1), + in_point: Some(0.0), + out_point: Some(30.0), + transform: Some(Transform { opacity: Some(Animated::keyed(&keys, Some("time"))), ..Transform::default() }), + content: LayerContent::Shape(vec![group]), + ..Layer::default() + }; + let a = Animation { + version: Some("5.9.0".into()), + frame_rate: Some(30.0), + in_point: Some(0.0), + out_point: Some(30.0), + width: Some(100.0), + height: Some(100.0), + assets: Some(vec![Asset { id: Some("x".into()), ..Asset::default() }]), + layers: Some(vec![layer]), + ..Animation::default() + }; + let back = Animation::read(&a.to_vec()).unwrap(); + assert_eq!(back, a); + let out = a.to_json(); + assert_eq!(out["layers"][0]["ty"], json!(4)); + assert_eq!(out["layers"][0]["shapes"][0]["it"][1]["ty"], json!("fl")); + let o = back.layers.as_ref().unwrap()[0].transform.as_ref().unwrap().opacity.as_ref().unwrap().read(); + assert_eq!(o.expression, Some("time")); + let PropValue::Keys(k) = o.value else { panic!("keys") }; + assert_eq!((k.len(), k[1].t), (2, 30.0)); + // A layer with no type and nothing else is an empty object. + assert_eq!(Animation { layers: Some(vec![Layer::default()]), ..Animation::default() }.to_json(), json!({"layers": [{}]})); +} + +#[test] +fn fixtures_round_trip() { + for name in ["bouncing-ball.json", "legacy-masks.json", "precomp-matte-text.json", "shapes-gradient.json"] { + let bytes = std::fs::read(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures").join(name)).unwrap(); + let doc: Json = serde_json::from_slice(&bytes).unwrap(); + let a = Animation::read(&bytes).unwrap(); + assert!(a.layers.as_ref().is_some_and(|l| !l.is_empty()), "{name}"); + assert_eq!(a.to_json(), doc, "{name}"); + } +} + +#[test] +fn every_limit_is_enforced() { + let too = |what, limit| Err(LottieError::TooLarge { what, limit }); + let at = |doc: &Json, limits: Limits| Animation::from_document(doc.clone(), &limits).map(|_| ()); + + let bytes = br#"{"w":1,"h":1}"#; + assert!(Animation::read_with_limits(bytes, &Limits { max_bytes: bytes.len(), ..Limits::default() }).is_ok()); + assert_eq!( + Animation::read_with_limits(bytes, &Limits { max_bytes: bytes.len() - 1, ..Limits::default() }).map(|_| ()), + too("bytes", bytes.len() - 1) + ); + + // Two layers at the top and two in a precomposition. + let doc = json!({"assets": [{"id": "a", "layers": [{"ty": 3}, {"ty": 3}]}, {"id": "b"}], "layers": [{"ty": 3}, {"ty": 3}]}); + assert_eq!(at(&doc, Limits { max_layers: 4, max_assets: 2, ..Limits::default() }), Ok(())); + assert_eq!(at(&doc, Limits { max_layers: 3, ..Limits::default() }), too("layers", 3)); + assert_eq!(at(&doc, Limits { max_assets: 1, ..Limits::default() }), too("assets", 1)); + + // Shape items: a group, the two in it, one in a precomposition's layer and one in a glyph. + let doc = json!({ + "chars": [{"data": {"shapes": [{"ty": "sh"}]}}], + "assets": [{"layers": [{"ty": 4, "shapes": [{"ty": "el"}]}]}], + "layers": [{"ty": 4, "shapes": [{"ty": "gr", "it": [{"ty": "rc"}, {"ty": "fl"}]}]}] + }); + assert_eq!(at(&doc, Limits { max_shapes: 5, ..Limits::default() }), Ok(())); + assert_eq!(at(&doc, Limits { max_shapes: 4, ..Limits::default() }), too("shape items", 4)); + + // Nesting: `{"layers": [{"x": 1}]}` is three levels. + let doc = json!({"layers": [{"x": 1}]}); + assert_eq!(at(&doc, Limits { max_depth: 3, ..Limits::default() }), Ok(())); + assert_eq!(at(&doc, Limits { max_depth: 2, ..Limits::default() }), too("nesting levels", 2)); + // A value built in code, deeper than a parser lets through, is refused and not walked. + let mut deep = json!(1); + for _ in 0..300 { + deep = json!({"it": [deep], "ty": "gr"}); + } + assert_eq!(at(&json!({"layers": [{"ty": 4, "shapes": [deep]}]}), Limits::default()), too("nesting levels", 128)); + // The same from text: the parser stops it. + let text = format!("{{\"layers\":{}{}}}", "[".repeat(300), "]".repeat(300)); + assert!(matches!(Animation::read(text.as_bytes()), Err(LottieError::Parse(_)))); +} + +#[test] +fn what_is_not_a_document_is_an_error() { + for bad in [&b"[1, 2]"[..], b"5", b"null", b"\"text\"", b"", b"{\"w\":", b"PK\x03\x04"] { + assert!(matches!(Animation::read(bad), Err(LottieError::Parse(_))), "{bad:?}"); + } + assert_eq!(Animation::read(b"{}").unwrap(), Animation::default()); +} + +#[test] +fn damaged_documents_never_panic() { + let doc = serde_json::to_vec(&everything()).unwrap(); + // Every prefix, and every byte replaced by a few that change the structure. + for cut in 0..doc.len() { + let _ = Animation::read(&doc[..cut]); + } + let mut seed = 0x2545_f491_4f6c_dd1du64; + for _ in 0..4000 { + let mut d = doc.clone(); + for _ in 0..3 { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + let at = (seed >> 8) as usize % d.len(); + d[at] = b"{}[]\",:0n-e. t"[(seed >> 40) as usize % 14]; + } + if let Ok(a) = Animation::read(&d) { + // Whatever read writes back to something that reads the same. + assert_eq!(Animation::read(&a.to_vec()).unwrap(), a); + } + } +} From 471abca087cc0447431ae84b09dcec9ad77a1920 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:47:39 -0500 Subject: [PATCH 024/120] craft-media: MPEG-2 transport and program stream demuxer from FilmCraft (`mpegts`) (#861) * craft-media: MPEG-2 transport and program stream demuxer from FilmCraft (feature `mpegts`) Moves filmcraft-mpegts (storytold/filmcraft@4bec22b) into craft_media::mpegts. ByteSource is the crate's trait, Error is written without thiserror so the feature has no dependencies, and Codec::codec_id() and Stream::codec_params() map streams to CodecId. Adds Container::MpegTs and Container::MpegPs, an ffprobe oracle test over ten ffmpeg-made files, and hostile-input tests. * craft-media: module order after merging main --- README.md | 4 +- crates/media/Cargo.toml | 8 +- crates/media/README.md | 103 ++- crates/media/src/id.rs | 6 + crates/media/src/lib.rs | 6 +- crates/media/src/mpegts/es.rs | 902 +++++++++++++++++++++++ crates/media/src/mpegts/hostile_tests.rs | 209 ++++++ crates/media/src/mpegts/mod.rs | 458 ++++++++++++ crates/media/src/mpegts/pes.rs | 126 ++++ crates/media/src/mpegts/ps.rs | 370 ++++++++++ crates/media/src/mpegts/tests.rs | 650 ++++++++++++++++ crates/media/src/mpegts/ts.rs | 559 ++++++++++++++ crates/media/src/mpegts/work_tests.rs | 115 +++ crates/media/tests/oracle_mpegts.rs | 278 +++++++ 14 files changed, 3785 insertions(+), 9 deletions(-) create mode 100644 crates/media/src/mpegts/es.rs create mode 100644 crates/media/src/mpegts/hostile_tests.rs create mode 100644 crates/media/src/mpegts/mod.rs create mode 100644 crates/media/src/mpegts/pes.rs create mode 100644 crates/media/src/mpegts/ps.rs create mode 100644 crates/media/src/mpegts/tests.rs create mode 100644 crates/media/src/mpegts/ts.rs create mode 100644 crates/media/src/mpegts/work_tests.rs create mode 100644 crates/media/tests/oracle_mpegts.rs diff --git a/README.md b/README.md index 45361e66e..ab45c1a99 100644 --- a/README.md +++ b/README.md @@ -171,8 +171,8 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " = file.read_unit(&bytes, v, 0)?; // the first access unit +``` + +- **Transport streams**: 188-byte packets, 192-byte BDAV packets (sync byte after a 4-byte + arrival timestamp) and 204-byte packets; the packet size is sniffed from five consecutive sync + bytes. Lost sync resynchronises on the next aligned run. PSI sections are reassembled across + packets and checked with the MPEG CRC-32; the first program of the PAT is demuxed. Continuity + counter errors, CRC errors and resyncs are reported as warnings. +- **Program streams**: MPEG-2 and MPEG-1 packs, system headers, the program stream map (stream + types for elementary stream ids), padding, private stream 2 (DVD navigation) skipped, private + stream 1 split by sub-stream id. Garbage resynchronises on the next pack or PES start code. +- **Streams**: MPEG-1/2 video, H.264, HEVC, MPEG audio, AAC (ADTS, LATM/LOAS), AC-3, E-AC-3, + DTS, TrueHD, Blu-ray LPCM, DVD LPCM (format from the sub-stream header), PGS / DVB / teletext + subtitles; ISO 639 language from the PMT. +- **Access units**, found while the file is scanned (one pass, streaming; only a few bytes of + each stream are buffered): + - MPEG-1/2 video: a coded frame — a frame picture, or two field pictures (paired by + picture_structure) — with the sequence / GOP headers before it; picture type, temporal + reference, field structure, top_field_first, repeat_first_field, progressive_frame, closed + GOP / broken link. A PTS belongs to the unit whose picture start code is in that PES. + - H.264 / HEVC: PES packets with a timestamp (a PES without one continues the previous unit); + random access from IDR / IRAP NAL types; disposable when every slice is a non-reference. + - Audio: one frame per unit from the frame headers (sync re-hunted after garbage; a header + found while hunting is confirmed by the next one); a PTS belongs to the first frame starting + in the PES. + - LPCM and everything else: one PES packet per unit. +- **Timestamps**: 33-bit PTS/DTS unwrapped across the wrap (and back, for B pictures shown + before a wrap that came earlier); PCR / SCR range. +- **Reading**: a unit records only the position of its first packet and its offset in that + packet's payload; `read_unit` re-reads from there (the index costs a few dozen bytes per + unit, not per packet). + +`frame_bytes`, `frame_info` and `sei_has_recovery_point` are public: other containers that store +MPEG audio or AC-3 frames in chunks that do not follow them (AVI) find the frames again with them. + +`Codec::codec_id()` and `Stream::codec_params()` give the crate-wide `CodecId` / `CodecParams`. +MPEG-1 video reports `Mpeg2Video`; MPEG audio reports `Mp2` whatever its layer (the layer is in +each frame header: `frame_info(..).variant`); ADTS and LATM AAC both report `Aac`; DTS, TrueHD and +subtitles have no identifier (`Unknown`). The streams carry their configuration in band, so +`extradata` is empty; DVD LPCM fills `sample_rate`, `channels` and `pcm`. + +Limits, each with a test: 2²³ access units per stream (the rest are not indexed, with a warning), +128 MiB per unit read (`MAX_UNIT_BYTES`), a PES header given up after 1024 bytes, 16 stuffing bytes +in an MPEG-1 packet header, a pack header sniffed behind at most 64 zero bytes, and E-AC-3 frames of +at least 8 bytes. + +### Specifications + +| Document | Edition | Used for | +|---|---|---| +| ITU-T H.222.0 \| ISO/IEC 13818-1 | 2000 (second edition) | TS packets, adaptation fields, PCR, PSI (PAT / PMT, CRC-32 of Annex A), PES headers, PTS/DTS, program streams (pack, system header, program stream map), stream types | +| ISO/IEC 11172-1 | 1993 | MPEG-1 system streams (pack header, PES header stuffing / STD buffer / timestamps) | +| Blu-ray / AVCHD BDAV MPEG-2 TS layout (192-byte source packets with a 4-byte TP_extra_header; registration `HDMV`; stream types 0x80-0x92) | public descriptions | `.m2ts` / `.mts` | +| DVD-Video private stream 1 sub-stream layout (AC-3 / DTS 0x80-0x8F with a 3-byte header, LPCM 0xA0-0xAF with a 6-byte header, sub-pictures 0x20-0x3F) | public descriptions | `.vob` | +| ETSI EN 300 468 (descriptors 0x56, 0x59, 0x6A, 0x7A, 0x7B), ATSC A/52 Annex A (AC-3 in TS) | — | identifying private PES streams | +| ISO/IEC 11172-3, 13818-3 (frame headers), ISO/IEC 13818-7 (ADTS), 14496-3 (LOAS), ATSC A/52 (syncinfo, E-AC-3 frmsiz) | — | audio frame lengths for splitting | + +### Gaps + +- One program per TS (the first in the PAT); no program switching, no section-based + metadata (SDT / EIT), no scrambled streams. +- H.264 / HEVC access units follow PES packets: streams that put several pictures in one PES + packet (rare in practice; not seen in broadcast, AVCHD or FFmpeg output) would be indexed as + one unit. +- DVD navigation (IFO / NAV packets), multiple VOB concatenation and angle interleaving are not + handled; each file opens on its own. + +Differences from `filmcraft-mpegts`: `ByteSource` is `craft_media::ByteSource`; +`Error` is written by hand, without `thiserror`, and is `#[non_exhaustive]`; +`Codec::codec_id()` and `Stream::codec_params()` are new. + +Tests: a small TS muxer and PS writer (every access unit reads back byte-identical with its PTS, +key and disposable flags), 33-bit unwrap, truncation and mutation, streams of every codec filled +with random start codes and sync words, damaged PSI sections with valid CRCs, and the frame-header +helpers on arbitrary bytes. `tests/oracle_mpegts.rs` compares ten ffmpeg-made files (MPEG-2, H.264 +and HEVC video; MP2, ADTS and LATM AAC, AC-3, E-AC-3, Blu-ray and DVD LPCM; TS, M2TS, VOB and +MPEG-1 system streams) with ffprobe's packets: unit counts, sizes, timestamps and key flags. ## `mxf` @@ -664,7 +757,7 @@ sound. Always built, with no dependencies, so codec crates share them: - `Container`: the file format (`Wav`, `Aiff`, `Flac`, `Mp4`, `QuickTime`, `Matroska`, `WebM`, - `Ogg`, `Caf`, `Adts`, `MpegAudio`, `Ac3`, `Avi`, `Mxf`), with `name()`. + `Ogg`, `Caf`, `Adts`, `MpegAudio`, `Ac3`, `Avi`, `Mxf`, `MpegTs`, `MpegPs`), with `name()`. - `CodecId`: audio and video codecs (`Aac`, `HeAac`, `Opus`, `Eac3`, `Speex`, …, `H264`, `Hevc`, `Av1`, `ProRes`, `Dnxhd`, `Apv`, `Theora`, …, `Unknown`), with `name()`, `is_audio()`, `is_video()`. @@ -686,7 +779,7 @@ All are `#[non_exhaustive]`: new formats and fields are minor changes. `craft_media::ByteSource` (`len`, `read_at(offset, buf)`) is what every demuxer reads from. It is always built, so a crate that uses only `bitstream` can name it too. `isobmff::ByteSource`, -`matroska::ByteSource`, `ogg::ByteSource`, `avi::ByteSource` and `mxf::ByteSource` are the same trait. It is implemented for `[u8]`, +`matroska::ByteSource`, `ogg::ByteSource`, `avi::ByteSource`, `mxf::ByteSource` and `mpegts::ByteSource` are the same trait. It is implemented for `[u8]`, `Vec`, `&T`, `Arc`, `Box` and `std::fs::File` (not on wasm). `ReadSeekSource` adapts any `Read + Seek`; it is `Send + Sync` when the reader is `Send`. Implement the trait once for a custom source (a web `Blob`, a network range reader) to use every container with it. diff --git a/crates/media/src/id.rs b/crates/media/src/id.rs index 7862546f3..00b63d004 100644 --- a/crates/media/src/id.rs +++ b/crates/media/src/id.rs @@ -40,6 +40,10 @@ pub enum Container { Avi, /// MXF (SMPTE ST 377-1): OP1a, OP-Atom. Mxf, + /// MPEG-2 transport stream (TS, M2TS). + MpegTs, + /// MPEG-1/2 program stream (MPG, VOB). + MpegPs, } impl Container { @@ -60,6 +64,8 @@ impl Container { Container::Ac3 => "AC-3", Container::Avi => "AVI", Container::Mxf => "MXF", + Container::MpegTs => "MPEG-TS", + Container::MpegPs => "MPEG-PS", } } } diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs index eaaea04ed..a9c812318 100644 --- a/crates/media/src/lib.rs +++ b/crates/media/src/lib.rs @@ -10,10 +10,12 @@ //! - `ogg` (feature `ogg`): a clean-room Ogg demuxer with Ogg Opus and Ogg Vorbis stream mapping. //! - `avi` (feature `avi`): a clean-room AVI (RIFF) and OpenDML demuxer. //! - `mxf` (feature `mxf`, enables `chrono`): a clean-room MXF demuxer and writer (OP1a, OP-Atom). +//! - `mpegts` (feature `mpegts`): a clean-room MPEG-2 Systems demuxer (transport streams and +//! program streams). //! - [`Container`] and [`CodecId`] (no feature): the identifiers the codec crates report a file's //! container and codec with. //! -//! `bitstream` and `isobmff` are on by default; `matroska`, `ogg`, `avi` and `mxf` are opt-in. A crate that needs +//! `bitstream` and `isobmff` are on by default; `matroska`, `ogg`, `avi`, `mxf` and `mpegts` are opt-in. A crate that needs //! only bit I/O uses `default-features = false, features = ["bitstream"]`. #[cfg(feature = "avi")] @@ -25,6 +27,8 @@ mod id; pub mod isobmff; #[cfg(feature = "matroska")] pub mod matroska; +#[cfg(feature = "mpegts")] +pub mod mpegts; #[cfg(feature = "mxf")] pub mod mxf; #[cfg(feature = "ogg")] diff --git a/crates/media/src/mpegts/es.rs b/crates/media/src/mpegts/es.rs new file mode 100644 index 000000000..8568c7cba --- /dev/null +++ b/crates/media/src/mpegts/es.rs @@ -0,0 +1,902 @@ +//! Splitting elementary streams into access units while the file is scanned. +//! +//! The scanners feed each stream's payload bytes chunk by chunk (a TS packet's payload, a PS +//! packet's payload). A chunk is identified by the position the reader restarts from (`pos`) and +//! the elementary-stream offset of its first byte. Splitting needs only a few bytes beyond a +//! boundary, so only the most recent chunks are remembered. + +use std::collections::VecDeque; + +use super::{Codec, PictureInfo, Unit}; + +/// How a stream is cut into units. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Mode { + /// MPEG-1/2 video: one coded frame (a frame picture or two field pictures) per unit. + MpegVideo, + /// H.264 / HEVC: one access unit per unit (the first of a PES packet keeps its timestamps); + /// NAL types give the flags. + Nal { hevc: bool }, + /// Framed audio with self-describing frame headers. + Audio(AudioSync), + /// One PES packet per unit. + Pes, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum AudioSync { + Mpa, + Adts, + Loas, + Ac3, +} + +impl Mode { + fn of(codec: &Codec) -> Mode { + match codec { + Codec::Mpeg1Video | Codec::Mpeg2Video => Mode::MpegVideo, + Codec::H264 => Mode::Nal { hevc: false }, + Codec::Hevc => Mode::Nal { hevc: true }, + Codec::MpegAudio => Mode::Audio(AudioSync::Mpa), + Codec::AacAdts => Mode::Audio(AudioSync::Adts), + Codec::AacLatm => Mode::Audio(AudioSync::Loas), + Codec::Ac3 | Codec::Eac3 => Mode::Audio(AudioSync::Ac3), + _ => Mode::Pes, + } + } +} + +#[derive(Clone, Copy)] +struct PesMark { + es_start: u64, + pts: Option, + dts: Option, + used: bool, +} + +/// A unit whose end is not known yet. +#[derive(Clone, Copy)] +struct Open { + start: u64, + pos: u64, + offset: u32, + pts: Option, + dts: Option, + key: bool, + disposable: bool, + /// A coded slice has been seen (H.264 / HEVC). + vcl: bool, + picture: Option, +} + +/// MPEG video picture waiting for its coding extension (to tell fields from frames). +#[derive(Clone, Copy)] +struct PendingPicture { + /// Start of the unit if the picture begins one (its first sequence / GOP header). + unit_start: u64, + /// The picture start code (timestamps belong to the unit whose picture starts in the PES). + at: u64, + info: PictureInfo, +} + +pub(crate) struct Splitter { + mode: Mode, + /// Elementary-stream bytes fed so far. + es_pos: u64, + /// Recent chunks: (reader position, ES offset of the first byte). + chunks: VecDeque<(u64, u64)>, + pes: VecDeque, + /// Unconsumed bytes (start codes / frame headers spanning chunks), starting at `buf_start`. + buf: Vec, + buf_start: u64, + open: Option, + pub units: Vec, + pub skipped: u64, + /// Units beyond [`MAX_UNITS`] were dropped. + pub truncated: bool, + max_units: usize, + // MPEG video state + header_start: Option, + seq_in_unit: bool, + gop: Option<(bool, bool)>, + pending: Option, + open_field: Option, + // audio state + next_frame: Option, +} + +/// AC-3 frame sizes in 16-bit words for 48 / 44.1 / 32 kHz (A/52 Table 5.18), by +/// frmsizecod / 2; 44.1 kHz adds a word for odd frmsizecod. +const AC3_KBPS: [u32; 19] = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640]; + +pub(crate) fn ac3_frame_bytes(fscod: u8, frmsizecod: u8) -> Option { + let kbps = *AC3_KBPS.get((frmsizecod / 2) as usize)?; + let words = match fscod { + 0 => kbps * 2, + 1 => kbps * 96_000 / 44_100 + (frmsizecod & 1) as u32, + 2 => kbps * 3, + _ => return None, + }; + Some(words as usize * 2) +} + +const MPA_KBPS: [[u16; 15]; 5] = [ + [0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448], // MPEG-1 layer I + [0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384], // MPEG-1 layer II + [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320], // MPEG-1 layer III + [0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256], // MPEG-2/2.5 layer I + [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160], // MPEG-2/2.5 layers II, III +]; + +/// MPEG audio frame header (ISO/IEC 11172-3 §2.4.2.3, 13818-3 LSF): (frame bytes, sample rate, +/// samples per frame). +pub(crate) fn mpa_header(b: &[u8]) -> Option<(usize, u32, u32)> { + if b.len() < 4 || b[0] != 0xFF || b[1] & 0xE0 != 0xE0 { + return None; + } + let version = (b[1] >> 3) & 3; // 0: 2.5, 2: MPEG-2, 3: MPEG-1 + let layer = (b[1] >> 1) & 3; // 3: I, 2: II, 1: III + let br = (b[2] >> 4) as usize; + let sr = (b[2] >> 2) & 3; + let pad = ((b[2] >> 1) & 1) as usize; + if version == 1 || layer == 0 || br == 0 || br == 15 || sr == 3 { + return None; + } + let rate = [44_100u32, 48_000, 32_000][sr as usize] + >> match version { + 3 => 0, + 2 => 1, + _ => 2, + }; + let table = match (version == 3, layer) { + (true, 3) => 0, + (true, 2) => 1, + (true, _) => 2, + (false, 3) => 3, + (false, _) => 4, + }; + let kbps = MPA_KBPS[table][br] as usize; + let (bytes, samples) = match layer { + 3 => ((12 * kbps * 1000 / rate as usize + pad) * 4, 384), + 2 => (144 * kbps * 1000 / rate as usize + pad, 1152), + _ if version == 3 => (144 * kbps * 1000 / rate as usize + pad, 1152), + _ => (72 * kbps * 1000 / rate as usize + pad, 576), + }; + (bytes >= 4).then_some((bytes, rate, samples)) +} + +/// What an audio frame header says. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct FrameInfo { + /// Samples per second. + pub sample_rate: u32, + /// Channel count. + pub channels: u32, + /// Samples per channel in the frame. + pub samples: u32, + /// MPEG audio layer (1-3); AC-3 bsid; ADTS object type (profile + 1). + pub variant: u8, +} + +const ADTS_RATES: [u32; 13] = [96_000, 88_200, 64_000, 48_000, 44_100, 32_000, 24_000, 22_050, 16_000, 12_000, 11_025, 8_000, 7_350]; + +/// Bytes of the audio frame whose header starts `b` (MPEG audio, ADTS, LATM, AC-3, E-AC-3), or +/// `None` without a valid header there. For containers that store frames in chunks that need not +/// follow them (AVI): the frames are found again with it. +pub fn frame_bytes(codec: &Codec, b: &[u8]) -> Option { + let sync = match codec { + Codec::MpegAudio => AudioSync::Mpa, + Codec::AacAdts => AudioSync::Adts, + Codec::AacLatm => AudioSync::Loas, + Codec::Ac3 | Codec::Eac3 => AudioSync::Ac3, + _ => return None, + }; + frame_len(sync, b) +} + +/// Sample rate, channels and samples of the audio frame whose header starts `b` (the codecs of +/// [`frame_bytes`] except LATM), or `None` without a valid header there. +pub fn frame_info(codec: &Codec, b: &[u8]) -> Option { + match codec { + Codec::MpegAudio => { + let (_, sample_rate, samples) = mpa_header(b)?; + let layer = 4 - ((b[1] >> 1) & 3); + Some(FrameInfo { sample_rate, channels: if b[3] >> 6 == 3 { 1 } else { 2 }, samples, variant: layer }) + } + Codec::AacAdts => { + frame_len(AudioSync::Adts, b)?; + let sf = ((b[2] >> 2) & 15) as usize; + let ch = (((b[2] & 1) << 2) | (b[3] >> 6)) as u32; + Some(FrameInfo { + sample_rate: *ADTS_RATES.get(sf)?, + channels: if ch == 0 { + 2 + } else if ch == 7 { + 8 + } else { + ch + }, + samples: 1024 * ((b[6] & 3) as u32 + 1), + variant: (b[2] >> 6) + 1, + }) + } + Codec::Ac3 | Codec::Eac3 => { + frame_len(AudioSync::Ac3, b)?; + let bsid = b[5] >> 3; + if bsid > 10 { + // E-AC-3: fscod(2) numblkscod(2) acmod(3) lfeon(1) + let fscod = b[4] >> 6; + let (rate, blocks) = if fscod == 3 { + // fscod2 3 is reserved (no sample rate) + (*[24_000, 22_050, 16_000].get(((b[4] >> 4) & 3) as usize)?, 6) + } else { + ([48_000, 44_100, 32_000][fscod as usize], [1, 2, 3, 6][((b[4] >> 4) & 3) as usize]) + }; + let acmod = (b[4] >> 1) & 7; + return Some(FrameInfo { + sample_rate: rate, + channels: AC3_CHANNELS[acmod as usize] + (b[4] & 1) as u32, + samples: 256 * blocks, + variant: bsid, + }); + } + let fscod = b[4] >> 6; + let rate = *[48_000u32, 44_100, 32_000].get(fscod as usize)?; + // bsi: bsid(5) bsmod(3) acmod(3) [cmixlev(2)] [surmixlev(2)] [dsurmod(2)] lfeon(1) + let b6 = *b.get(6)?; + let acmod = b6 >> 5; + let mut bit = 3; // bits of byte 6 consumed + if acmod & 1 != 0 && acmod != 1 { + bit += 2; + } + if acmod & 4 != 0 { + bit += 2; + } + if acmod == 2 { + bit += 2; + } + let word = u16::from_be_bytes([b6, *b.get(7)?]); + let lfe = (word >> (15 - bit)) & 1; + Some(FrameInfo { sample_rate: rate, channels: AC3_CHANNELS[acmod as usize] + lfe as u32, samples: 1536, variant: bsid }) + } + _ => None, + } +} + +/// Whether the syncframe at the start of `b` is E-AC-3 (bsid 11-16) other than independent +/// substream 0: a dependent substream (strmtyp 1) or another program (A/52 §E2.3.1.1-2). +fn eac3_joins_unit(b: &[u8]) -> bool { + match b.get(2..6) { + Some(&[b2, _, _, b5]) => (11..=16).contains(&(b5 >> 3)) && (b2 >> 6 == 1 || (b2 >> 3) & 7 != 0), + _ => false, + } +} + +/// Bytes of an SEI NAL scanned for a recovery point message; a longer NAL is judged by its start. +const SEI_WINDOW: usize = 2048; + +/// Whether an H.264 SEI NAL (`nal` is the NAL header byte and the payload, as in the stream) +/// holds a recovery point message (payloadType 6, H.264 D.1.7) with recovery_frame_cnt 0. +pub fn sei_has_recovery_point(nal: &[u8]) -> bool { + // undo emulation prevention so that payload sizes are in RBSP bytes + let mut b = Vec::with_capacity(nal.len()); + let mut zeros = 0; + for &x in nal.iter().skip(1) { + if zeros >= 2 && x == 3 { + zeros = 0; + continue; + } + zeros = if x == 0 { zeros + 1 } else { 0 }; + b.push(x); + } + // payloadType / payloadSize: bytes of 0xFF followed by the last byte + let read = |i: &mut usize| { + let mut v = 0usize; + while let Some(&x) = b.get(*i) { + *i += 1; + v = v.saturating_add(x as usize); + if x != 0xFF { + break; + } + } + v + }; + let mut i = 0; + // each message takes at least two bytes, so the loop ends at the end of the NAL + while i + 2 <= b.len() { + let ty = read(&mut i); + let size = read(&mut i); + // recovery_frame_cnt is ue(v): a leading 1 bit is the value 0, i.e. decoding from here is + // correct at once. A count above 0 (gradual intra refresh) is not a safe seek point. + if ty == 6 && b.get(i).is_some_and(|x| x & 0x80 != 0) { + return true; + } + i = i.saturating_add(size); + } + false +} + +/// Shortest E-AC-3 syncframe accepted. A real syncframe holds the 6 header bytes read here plus +/// bsi and audio blocks (the smallest real ones are tens of bytes), so shorter "frames" +/// (`0B 77 00 01` is 4 bytes) are noise; they would otherwise turn every 4 input bytes into a +/// 64-byte `Unit`. +const EAC3_MIN_FRAME: usize = 8; + +/// Most units one stream may index. A `Unit` is 64 bytes, so this bounds a stream's table to +/// 512 MiB. It is 47 hours of 48 kHz AAC (the densest audio, 46.9 units/s), 9 hours of 240 fps +/// video: far beyond any real file, while a hostile file (a unit per 7 input bytes) cannot grow +/// the table without limit. +pub(crate) const MAX_UNITS: usize = 1 << 23; + +/// Full-bandwidth channels per AC-3 audio coding mode (A/52 Table 5.8). +const AC3_CHANNELS: [u32; 8] = [2, 1, 2, 3, 3, 4, 4, 5]; + +/// Frame length at the start of `b` for the given sync type (`None`: no valid header; `Some(0)` +/// is never returned). +fn frame_len(sync: AudioSync, b: &[u8]) -> Option { + match sync { + AudioSync::Mpa => mpa_header(b).map(|h| h.0), + AudioSync::Adts => { + if b.len() < 7 || b[0] != 0xFF || b[1] & 0xF6 != 0xF0 { + return None; + } + let len = ((b[3] as usize & 3) << 11) | (b[4] as usize) << 3 | (b[5] as usize) >> 5; + (len >= 7).then_some(len) + } + AudioSync::Loas => { + if b.len() < 3 || b[0] != 0x56 || b[1] & 0xE0 != 0xE0 { + return None; + } + let len = ((b[1] as usize & 0x1F) << 8) | b[2] as usize; + (len > 0).then_some(len + 3) + } + AudioSync::Ac3 => { + if b.len() < 6 || b[0] != 0x0B || b[1] != 0x77 { + return None; + } + let bsid = b[5] >> 3; + if bsid <= 10 { + ac3_frame_bytes(b[4] >> 6, b[4] & 0x3F) + } else if bsid <= 16 { + // E-AC-3: frmsiz + let len = ((((b[2] & 7) as usize) << 8) | b[3] as usize) * 2 + 2; + (len >= EAC3_MIN_FRAME).then_some(len) + } else { + None + } + } + } +} + +impl Splitter { + pub fn new(codec: &Codec) -> Splitter { + Splitter { + mode: Mode::of(codec), + es_pos: 0, + chunks: VecDeque::new(), + pes: VecDeque::new(), + buf: Vec::new(), + buf_start: 0, + open: None, + units: Vec::new(), + skipped: 0, + truncated: false, + max_units: MAX_UNITS, + header_start: None, + seq_in_unit: false, + gop: None, + pending: None, + open_field: None, + next_frame: None, + } + } + + /// A PES packet starts (its payload follows in the next chunks). + pub fn pes_start(&mut self, pts: Option, dts: Option) { + self.pes.push_back(PesMark { es_start: self.es_pos, pts, dts, used: false }); + while self.pes.len() > 64 { + self.pes.pop_front(); + } + match self.mode { + Mode::Pes => self.begin_pes_unit(true), + Mode::Nal { .. } => self.begin_pes_unit(pts.is_some() || dts.is_some()), + _ => {} + } + } + + /// PES-delimited units: a packet with a timestamp (or any packet in plain PES mode) starts a + /// new unit; one without continues the previous unit. + fn begin_pes_unit(&mut self, new: bool) { + if !new && self.open.is_some() { + return; + } + let at = self.es_pos; + self.close(at); + // the chunk of this position arrives with the next feed: resolved there + let mut o = Open { start: at, pos: u64::MAX, offset: 0, pts: None, dts: None, key: false, disposable: false, vcl: false, picture: None }; + if let Some((pts, dts)) = self.timestamps_at(at) { + o.pts = pts; + o.dts = dts; + } + if self.mode == Mode::Pes { + o.key = true; + } + self.open = Some(o); + } + + /// The unused timestamps of the PES packet holding `at` (consumed). + fn timestamps_at(&mut self, at: u64) -> Option<(Option, Option)> { + let m = self.pes.iter_mut().rev().find(|m| m.es_start <= at)?; + if m.used || (m.pts.is_none() && m.dts.is_none()) { + return None; + } + m.used = true; + Some((m.pts, m.dts)) + } + + /// Reader position and offset of elementary-stream byte `at`. + fn locate(&self, at: u64) -> Option<(u64, u32)> { + let c = self.chunks.iter().rev().find(|c| c.1 <= at)?; + Some((c.0, (at - c.1) as u32)) + } + + /// End the open unit at `end`. + fn close(&mut self, end: u64) { + let Some(o) = self.open.take() else { return }; + if end <= o.start || o.pos == u64::MAX { + return; + } + if self.units.len() >= self.max_units { + self.truncated = true; + return; + } + let size = (end - o.start).min(u32::MAX as u64) as u32; + self.units.push(Unit { + pts: o.pts, + dts: o.dts, + key: o.key, + disposable: o.disposable, + size, + pos: o.pos, + offset: o.offset, + picture: o.picture, + }); + } + + /// Payload bytes of one chunk (`pos`: where the reader restarts to find them). + pub fn feed(&mut self, pos: u64, data: &[u8]) { + if data.is_empty() { + return; + } + // PES-delimited modes resolve a unit's position from `pos` right here and never look a + // chunk up again, so remembering chunks there would only grow without bound (NAL streams + // keep theirs: an access unit can start inside a PES; `keep_from` prunes them) + if matches!(self.mode, Mode::MpegVideo | Mode::Audio(_) | Mode::Nal { .. }) { + self.chunks.push_back((pos, self.es_pos)); + } + if let Some(o) = self.open.as_mut() + && o.pos == u64::MAX + && o.start == self.es_pos + { + o.pos = pos; + o.offset = 0; + } + let start = self.es_pos; + self.es_pos += data.len() as u64; + match self.mode { + Mode::Pes => {} + Mode::Nal { hevc } => self.scan_nal(start, data, hevc), + Mode::MpegVideo => self.scan_mpeg(start, data), + Mode::Audio(sync) => self.scan_audio(start, data, sync), + } + // forget chunks no pending position can refer to + let keep_from = self.buf_start.min(self.pending.map_or(u64::MAX, |p| p.unit_start.min(p.at))).min(self.header_start.unwrap_or(u64::MAX)); + while self.chunks.len() > 2 && self.chunks[1].1 <= keep_from { + self.chunks.pop_front(); + } + } + + /// Append to the window and return it with its start. + fn window(&mut self, start: u64, data: &[u8]) { + if self.buf.is_empty() || self.buf_start + self.buf.len() as u64 != start { + self.buf.clear(); + self.buf_start = start; + } + self.buf.extend_from_slice(data); + } + + /// Keep only the last `keep` bytes of the window. + fn trim(&mut self, keep: usize) { + if self.buf.len() > keep { + let drop = self.buf.len() - keep; + self.buf.drain(..drop); + self.buf_start += drop as u64; + } + } + + /// Start codes in the window from index `from`: (window index of `00 00 01`, code). + fn next_start_code(&self, from: usize) -> Option { + let b = &self.buf; + let mut i = from; + while i + 3 <= b.len() { + match b[i + 2] { + 1 if b[i] == 0 && b[i + 1] == 0 => return Some(i), + 0 => i += 1, + _ => i += 3, + } + } + None + } + + fn scan_nal(&mut self, start: u64, data: &[u8], hevc: bool) { + self.window(start, data); + let mut i = 0; + let mut blocked = None; + while let Some(k) = self.next_start_code(i) { + // NAL header byte(s) and the first byte of the slice header needed + if k + if hevc { 6 } else { 5 } > self.buf.len() { + blocked = Some(k); + break; + } + // a four-byte start code begins with the zero_byte that belongs to its NAL + let at = self.buf_start + k as u64 - (k > 0 && self.buf.get(k - 1) == Some(&0)) as u64; + let (Some(&h), Some(&h1)) = (self.buf.get(k + 3), self.buf.get(k + if hevc { 5 } else { 4 })) else { + blocked = Some(k); + break; + }; + let (key, vcl, non_ref, au_start) = if hevc { + let t = (h >> 1) & 0x3F; + ( + (16..=23).contains(&t), + t < 32, + // sub-layer non-reference pictures (TRAIL_N, TSA_N, STSA_N, RADL_N, RASL_N) + t < 16 && t.is_multiple_of(2), + // first_slice_segment_in_pic_flag, or a NAL that precedes the first slice + // (VPS, SPS, PPS, AUD, prefix SEI, 41..=44, 48..=55; H.265 7.4.2.4.4) + if t < 32 { h1 & 0x80 != 0 } else { matches!(t, 32..=35 | 39 | 41..=44 | 48..=55) }, + ) + } else { + let t = h & 0x1F; + ( + t == 5, + (1..=5).contains(&t), + h & 0x60 == 0, + // first_mb_in_slice == 0 (ue(v) "1"), or a NAL that precedes the first slice + // (SEI, SPS, PPS, AUD, 14..=18; H.264 7.4.1.2.3) + match t { + 1 | 2 | 5 => h1 & 0x80 != 0, + 6..=9 | 14..=18 => true, + _ => false, + }, + ) + }; + let mut recovery = false; + if !hevc && h & 0x1F == 6 { + // the whole SEI NAL is needed to find a recovery point message among others + let end = self.next_start_code(k + 3); + if end.is_none() && self.buf.len() - k < SEI_WINDOW { + blocked = Some(k); + break; + } + let nal = self.buf.get(k + 3..end.unwrap_or(self.buf.len())).unwrap_or(&[]); + recovery = sei_has_recovery_point(nal); + } + // a unit holds one access unit: a new one starts at the first NAL that cannot belong + // to the slices already seen (a unit opened later than this NAL is left alone) + if au_start && self.open.as_ref().is_some_and(|o| o.vcl && at > o.start) { + self.begin_access_unit(at); + } + if let Some(o) = self.open.as_mut() + && at >= o.start + { + // a recovery point is a random-access point (the pictures before its recovery + // frame count completes may be imperfect) + o.key |= recovery; + if vcl { + o.key |= key; + // disposable while every slice is non-reference + o.disposable = if o.vcl { o.disposable && non_ref } else { non_ref }; + o.vcl = true; + } + } + i = k + 3; + } + self.keep_tail(i, blocked); + } + + /// Close the open unit and start the next access unit at `at`: it has no timestamp (the PES + /// timestamps belong to the first access unit of the packet). + fn begin_access_unit(&mut self, at: u64) { + self.close(at); + let (pos, offset) = self.locate(at).unwrap_or((u64::MAX, 0)); + self.open = Some(Open { start: at, pos, offset, pts: None, dts: None, key: false, disposable: false, vcl: false, picture: None }); + } + + /// Drop the scanned part of the window: keep from a start code waiting for more bytes, or + /// the last bytes a split start code may begin in. + fn keep_tail(&mut self, i: usize, blocked: Option) { + let len = self.buf.len(); + let resume = blocked.unwrap_or_else(|| i.max(len.saturating_sub(3))); + self.trim(len - resume.min(len)); + } + + fn scan_mpeg(&mut self, start: u64, data: &[u8]) { + self.window(start, data); + let mut i = 0; + let mut blocked = None; + while let Some(k) = self.next_start_code(i) { + // up to 5 bytes after the code are inspected + if k + 9 > self.buf.len() { + blocked = Some(k); + break; + } + let at = self.buf_start + k as u64; + let code = self.buf[k + 3]; + let p = [self.buf[k + 4], self.buf[k + 5], self.buf[k + 6], self.buf[k + 7], self.buf[k + 8]]; + match code { + 0xB3 => { + self.settle_pending(); + self.header_start.get_or_insert(at); + self.seq_in_unit = true; + } + 0xB8 => { + self.settle_pending(); + self.header_start.get_or_insert(at); + self.gop = Some((p[3] & 0x40 != 0, p[3] & 0x20 != 0)); + } + 0x00 => { + self.settle_pending(); + let info = PictureInfo { + coding_type: (p[1] >> 3) & 7, + temporal_reference: ((p[0] as u16) << 2) | (p[1] >> 6) as u16, + structure: 3, + progressive_frame: true, + sequence_header: self.seq_in_unit, + gop: self.gop.is_some(), + closed_gop: self.gop.is_some_and(|g| g.0), + broken_link: self.gop.is_some_and(|g| g.1), + ..Default::default() + }; + let unit_start = self.header_start.take().unwrap_or(at); + self.seq_in_unit = false; + self.gop = None; + self.pending = Some(PendingPicture { unit_start, at, info }); + } + 0xB5 if p[0] >> 4 == 8 => { + if let Some(pp) = self.pending.as_mut() { + pp.info.structure = p[2] & 3; + pp.info.top_field_first = p[3] & 0x80 != 0; + pp.info.repeat_first_field = p[3] & 0x02 != 0; + pp.info.progressive_frame = p[4] & 0x80 != 0; + } + self.settle_pending(); + } + 0xB5 | 0xB2 => {} + _ => self.settle_pending(), + } + i = k + 3; + } + self.keep_tail(i, blocked); + } + + /// A picture's structure is known: start a unit, or join a first field. + fn settle_pending(&mut self) { + let Some(pp) = self.pending.take() else { return }; + let s = pp.info.structure; + if let Some(first) = self.open_field + && s != 3 + && first != s + && s != 0 + { + self.open_field = None; + return; + } + self.close(pp.unit_start); + let (pos, offset) = self.locate(pp.unit_start).unwrap_or((u64::MAX, 0)); + let mut o = Open { + start: pp.unit_start, + pos, + offset, + pts: None, + dts: None, + key: matches!(pp.info.coding_type, 1 | 4), + disposable: pp.info.coding_type == 3, + vcl: true, + picture: Some(pp.info), + }; + if let Some((pts, dts)) = self.timestamps_at(pp.at) { + o.pts = pts; + o.dts = dts; + } + self.open = Some(o); + self.open_field = (s == 1 || s == 2).then_some(s); + } + + fn scan_audio(&mut self, start: u64, data: &[u8], sync: AudioSync) { + let end = start + data.len() as u64; + // skip whole chunks inside the current frame + if let Some(n) = self.next_frame + && n >= end + { + return; + } + self.window(start, data); + loop { + let at = match self.next_frame { + Some(n) if n >= self.buf_start => n, + _ => self.buf_start, + }; + let i = (at - self.buf_start) as usize; + if i + 8 > self.buf.len() { + // not enough bytes for a header yet + self.trim(self.buf.len().saturating_sub(i)); + return; + } + match frame_len(sync, &self.buf[i..]) { + Some(len) if self.next_frame == Some(at) || self.confirmed(sync, i, len) => { + // an E-AC-3 dependent substream or further program joins the access unit of + // the independent substream 0 before it, as in MP4 and Matroska + if !(sync == AudioSync::Ac3 && self.open.is_some() && eac3_joins_unit(&self.buf[i..])) { + self.close(at); + let (pos, offset) = self.locate(at).unwrap_or((u64::MAX, 0)); + let mut o = Open { start: at, pos, offset, pts: None, dts: None, key: true, disposable: false, vcl: false, picture: None }; + if let Some((pts, dts)) = self.timestamps_at(at) { + o.pts = pts; + o.dts = dts; + } + self.open = Some(o); + } + self.next_frame = Some(at + len as u64); + if at + len as u64 >= self.buf_start + self.buf.len() as u64 { + self.buf.clear(); + self.buf_start = at + len as u64; + return; + } + } + _ => { + // lost sync: look for the next header from the following byte + self.next_frame = None; + self.skipped += 1; + let next = self.buf[i + 1..].iter().position(|&b| { + matches!((sync, b), (AudioSync::Ac3, 0x0B) | (AudioSync::Loas, 0x56) | (AudioSync::Mpa | AudioSync::Adts, 0xFF)) + }); + match next { + Some(d) => { + self.skipped += d as u64; + // Keep a cursor into the current window rather than draining its + // prefix for every false sync candidate. + self.next_frame = Some(at + 1 + d as u64); + } + None => { + self.skipped += (self.buf.len() - i - 1) as u64; + // A split header can begin in the final few bytes; compact once. + self.trim(7); + return; + } + } + } + } + } + } + + /// A header found while hunting for sync is accepted when the next frame's header follows + /// it (or the data ends before it). + fn confirmed(&self, sync: AudioSync, i: usize, len: usize) -> bool { + let j = i + len; + if j + 8 > self.buf.len() { + return true; + } + frame_len(sync, &self.buf[j..]).is_some() + } + + /// End of stream: close the open unit. + pub fn finish(&mut self) { + self.settle_pending(); + let end = match (self.mode, self.next_frame) { + (Mode::Audio(_), Some(n)) => n.min(self.es_pos), + _ => self.es_pos, + }; + if let (Mode::Audio(_), Some(o)) = (self.mode, self.open) { + // a truncated last frame is dropped + if self.next_frame.is_some_and(|n| n > self.es_pos) { + self.skipped += self.es_pos - o.start; + self.open = None; + } + } + self.close(end); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn audio_frame_sizes() { + // MPEG-1 layer II 48 kHz 192 kb/s: 576 bytes; layer III 44.1 kHz 128 kb/s with padding: 418 + assert_eq!(mpa_header(&[0xFF, 0xFD, 0xA4, 0x00]), Some((576, 48_000, 1152))); + assert_eq!(mpa_header(&[0xFF, 0xFB, 0x92, 0x00]), Some((418, 44_100, 1152))); + // MPEG-2 layer III 24 kHz 64 kb/s: 192 bytes, 576 samples + assert_eq!(mpa_header(&[0xFF, 0xF3, 0x84, 0x00]), Some((192, 24_000, 576))); + // AC-3 448 kb/s at 48 kHz = 1792 bytes; 44.1 kHz 192 kb/s odd code = 836 bytes + assert_eq!(ac3_frame_bytes(0, 30), Some(1792)); + assert_eq!(ac3_frame_bytes(1, 21), Some(836)); + assert_eq!(ac3_frame_bytes(2, 0), Some(192)); + } + + #[test] + fn eac3_dependent_substreams_join_the_access_unit() { + // 16-byte E-AC-3 syncframes (frmsiz 7), 48 kHz six blocks stereo: independent, dependent + // (7.1 extension), independent, dependent + let frame = |strmtyp: u8| { + let mut f = vec![0x0B, 0x77, strmtyp << 6, 7, (3 << 4) | (2 << 1), 16 << 3]; + f.resize(16, 0); + f + }; + let es: Vec = [frame(0), frame(1), frame(0), frame(1)].concat(); + let mut s = Splitter::new(&Codec::Eac3); + s.feed(0, &es); + s.finish(); + assert_eq!(s.units.iter().map(|u| u.size).collect::>(), vec![32, 32]); + assert_eq!(frame_info(&Codec::Eac3, &es).map(|f| f.samples), Some(1536)); + // AC-3 frames are never joined + assert!(!eac3_joins_unit(&[0x0B, 0x77, 0x40, 0, 0, 8 << 3])); + } + + #[test] + fn ac3_header_prefix_of_six_bytes_is_not_indexed_out_of_bounds() { + // exactly the six bytes frame_len() guarantees: byte 6 (acmod) is missing + assert_eq!(frame_info(&Codec::Ac3, &[0x0B, 0x77, 0, 0, 0x1E, 0x40]), None); + assert_eq!(frame_info(&Codec::Ac3, &[0x0B, 0x77, 0, 0, 0x1E, 0x40, 0]), None); + } + + #[test] + fn eac3_reserved_sample_rate_is_refused() { + // fscod 3 and fscod2 3: no sample rate + let mut f = vec![0x0B, 0x77, 0, 7, 0xF0, 16 << 3]; + f.resize(16, 0); + assert_eq!(frame_info(&Codec::Eac3, &f), None); + f[4] = 0xE0; // fscod2 2: 16 kHz + assert_eq!(frame_info(&Codec::Eac3, &f).map(|i| i.sample_rate), Some(16_000)); + } + + #[test] + fn four_byte_eac3_frames_make_no_units() { + let es: Vec = [0x0B, 0x77, 0x00, 0x01].repeat(10_000); + let mut s = Splitter::new(&Codec::Eac3); + s.feed(0, &es); + s.finish(); + assert!(s.units.is_empty(), "{} units from 40000 noise bytes", s.units.len()); + } + + #[test] + fn pes_delimited_modes_do_not_retain_chunks() { + for codec in [Codec::LpcmBluray, Codec::Unknown(0x99)] { + let mut s = Splitter::new(&codec); + for k in 0..5000u64 { + s.pes_start(Some(k as i64), None); + s.feed(k * 188, &[0x11; 184]); + } + s.finish(); + assert!(s.chunks.len() <= 2, "{codec:?}: {} chunks retained", s.chunks.len()); + assert_eq!(s.units.len(), 5000, "{codec:?}"); + assert!(s.units.iter().enumerate().all(|(k, u)| u.pos == k as u64 * 188 && u.offset == 0), "{codec:?}"); + } + } + + #[test] + fn unit_table_is_capped() { + let frame = |k: u8| { + let mut v = vec![0x0B, 0x77, 0, 7, (3 << 4) | (2 << 1), 16 << 3]; + v.resize(16, k); + v + }; + let mut s = Splitter::new(&Codec::Eac3); + s.max_units = 10; + let es: Vec = (0..100).flat_map(frame).collect(); + s.feed(0, &es); + s.finish(); + assert_eq!(s.units.len(), 10); + assert!(s.truncated); + } +} diff --git a/crates/media/src/mpegts/hostile_tests.rs b/crates/media/src/mpegts/hostile_tests.rs new file mode 100644 index 000000000..3990acaef --- /dev/null +++ b/crates/media/src/mpegts/hostile_tests.rs @@ -0,0 +1,209 @@ +//! Hostile input: streams of every codec filled with start codes and sync words at random, +//! damaged containers, and the public frame-header helpers on arbitrary bytes. Errors and empty +//! tables are fine; a panic (an index out of range, an overflow in a debug build) is not. + +use super::tests::{TsMux, pes}; +use super::*; + +/// Deterministic xorshift. +struct Rng(u64); + +impl Rng { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + fn below(&mut self, n: usize) -> usize { + (self.next() % n.max(1) as u64) as usize + } +} + +/// Random bytes with the patterns the splitters look for scattered through them: start codes of +/// every kind, MPEG audio / ADTS / LOAS / AC-3 sync words, with header bytes at the extremes. +/// At least one byte, at most about `max`. +fn soup(r: &mut Rng, max: usize) -> Vec { + let n = 1 + r.below(max); + let mut v = Vec::with_capacity(n + 16); + while v.len() < n { + let edge = [0x00u8, 0xFF, 0x80, 0x7F, r.next() as u8][r.below(5)]; + match r.below(12) { + 0 => v.extend([0, 0, 1, r.next() as u8]), + 1 => v.extend([0, 0, 1, [0x00, 0xB3, 0xB5, 0xB8, 0x09, 0x65, 0x41, 0x06, 0x26, 0x40, 0x46][r.below(11)], edge, edge]), + 2 => v.extend([0, 0, 0, 1, r.next() as u8, edge]), + 3 => v.extend([0xFF, 0xF0 | (r.next() as u8 & 0x0F), r.next() as u8, edge, edge, edge, edge]), + 4 => v.extend([0x0B, 0x77, edge, edge, r.next() as u8, r.next() as u8, edge]), + 5 => v.extend([0x56, 0xE0 | (r.next() as u8 & 0x1F), r.next() as u8]), + 6 => v.extend([edge; 3]), + _ => v.push(r.next() as u8), + } + } + v +} + +/// Opens and reads; returns how many units the tables hold. +fn open_and_read_all(data: &[u8]) -> usize { + let Ok(f) = open(data) else { return 0 }; + let _ = (f.format.name(), f.pcr_range); + for (s, st) in f.streams.iter().enumerate() { + let _ = (st.codec_params(), st.codec.name(), st.pts_range()); + let n = st.units.len(); + // every unit of a small table, a spread of a large one + for i in (0..n).step_by((n / 64).max(1)).chain(n.checked_sub(1)) { + let _ = f.read_unit(data, s, i); + } + } + let _ = f.read_unit(data, f.streams.len(), 0); + f.streams.iter().map(|s| s.units.len()).sum() +} + +/// Every stream type the PMT maps, each on its own PID. +const TYPES: [u8; 16] = [0x01, 0x02, 0x03, 0x04, 0x0F, 0x11, 0x1B, 0x24, 0x80, 0x81, 0x83, 0x84, 0x87, 0x90, 0x06, 0x42]; + +fn soup_ts(r: &mut Rng, m2ts: bool, packets: usize) -> Vec { + let mut mux = TsMux::new(m2ts); + let streams: Vec<(u8, u16)> = TYPES.iter().enumerate().map(|(i, &t)| (t, 0x200 + i as u16)).collect(); + mux.psi(&streams); + for _ in 0..packets { + let (_, pid) = streams[r.below(streams.len())]; + let payload = soup(r, 700); + let pts = (r.below(3) > 0).then(|| r.next() as i64 & ((1 << 33) - 1)); + let dts = pts.filter(|_| r.below(2) == 0).map(|p| p.wrapping_sub(3600) & ((1 << 33) - 1)); + mux.pes(pid, &pes([0xE0, 0xC0, 0xBD, 0xFD][r.below(4)], pts, dts, &payload, r.below(2) == 0)); + } + mux.out +} + +fn soup_ps(r: &mut Rng, mpeg1: bool, packets: usize) -> Vec { + let mut out = Vec::new(); + for _ in 0..packets { + if mpeg1 { + out.extend([0, 0, 1, 0xBA, 0x21, 0, 1, 0, 1, 0x80, 0, 1]); + } else { + out.extend([0, 0, 1, 0xBA, 0x44, 0, 4, 0, 4, 1, 1, 0x89, 0xC3, 0xF8 | r.below(8) as u8]); + out.extend(std::iter::repeat_n(0xFF, r.below(8))); + } + let mut payload = soup(r, 1500); + let id = [0xE0, 0xE1, 0xC0, 0xC1, 0xBD, 0xBD, 0xBD, 0xBB, 0xBC, 0xBE, 0xBF][r.below(11)]; + if id == 0xBD { + // DVD sub-streams: AC-3, DTS, LPCM (every format byte), sub-pictures + payload[0] = [0x80, 0x88, 0xA0, 0xA1, 0x20, r.next() as u8][r.below(6)]; + } + let pts = (r.below(3) > 0).then(|| r.next() as i64 & ((1 << 33) - 1)); + out.extend(pes(id, pts, None, &payload, true)); + } + out.extend([0, 0, 1, 0xB9]); + out +} + +#[test] +fn streams_of_start_codes_and_sync_words_never_panic() { + let mut r = Rng(0x1234_5678_9ABC_DEF1); + let (mut ts, mut ps) = (0, 0); + for round in 0..40 { + ts += open_and_read_all(&soup_ts(&mut r, round % 2 == 1, 150)); + ps += open_and_read_all(&soup_ps(&mut r, round % 2 == 1, 60)); + } + // the streams reach the splitters: they are cut into units, not rejected up front + assert!(ts > 2000 && ps > 500, "{ts} transport stream units, {ps} program stream units"); +} + +#[test] +fn damaged_containers_never_panic() { + let mut r = Rng(0xDEAD_BEEF_0BAD_F00D); + let bases = [soup_ts(&mut r, false, 60), soup_ts(&mut r, true, 60), soup_ps(&mut r, false, 40), soup_ps(&mut r, true, 40)]; + for base in &bases { + for _ in 0..300 { + let mut d = base.clone(); + for _ in 0..1 + r.below(24) { + let at = r.below(d.len()); + // header bytes at their extremes, lengths that run past the packet, lost sync + d[at] = [0x00, 0xFF, 0x47, 0x01, 0x80, 0xBA, r.next() as u8][r.below(7)]; + } + match r.below(4) { + 0 => d.truncate(r.below(d.len())), + // a run cut out of the middle: the packet grid shifts + 1 => { + let at = r.below(d.len()); + d.drain(at..(at + r.below(300)).min(d.len())); + } + _ => {} + } + open_and_read_all(&d); + } + } +} + +/// Program-specific information with every length field wrong. +#[test] +fn damaged_psi_sections_never_panic() { + let mut r = Rng(0x0F0F_1234_5555_AAAA); + for _ in 0..3000 { + let mut mux = TsMux::new(false); + // sections the muxer signs with a valid CRC, so the parsers see the damage + let mut pat = vec![0x00, 0xB0, 13, 0, 1, 0xC1, 0, 0, 0, 1, 0xE1, 0x00]; + let mut pmt = vec![0x02, 0xB0, 0, 0, 1, 0xC1, 0, 0, 0xE1, 0x00, 0xF0, 0]; + for _ in 0..r.below(6) { + pmt.extend([TYPES[r.below(TYPES.len())], 0xE2, r.next() as u8, 0xF0 | r.below(16) as u8, r.next() as u8]); + pmt.extend(soup(&mut r, 12)); + } + let len = pmt.len() - 3 + 4; + pmt[1] = 0xB0 | (len >> 8) as u8; + pmt[2] = len as u8; + for sec in [&mut pat, &mut pmt] { + for _ in 0..r.below(3) { + let at = r.below(sec.len()); + sec[at] = [0x00, 0xFF, 0x0F, 0xF0, r.next() as u8][r.below(5)]; + } + } + mux.section(0, &pat); + mux.section(0x100, &pmt); + for i in 0..8u16 { + mux.pes(0x200 + i, &pes(0xE0, Some(0), None, &soup(&mut r, 200), false)); + } + // enough packets for the sniffer + open_and_read_all(&mux.out); + } +} + +#[test] +fn frame_header_helpers_never_panic_on_any_bytes() { + let mut r = Rng(0x5EED_5EED_5EED_5EED); + let codecs = [Codec::MpegAudio, Codec::AacAdts, Codec::AacLatm, Codec::Ac3, Codec::Eac3, Codec::H264, Codec::LpcmDvd]; + for _ in 0..20_000 { + let b = soup(&mut r, 24); + for cut in 0..=b.len() { + let b = &b[..cut]; + for c in &codecs { + if let Some(n) = frame_bytes(c, b) { + assert!(n > 0, "{c:?}: an empty frame would never advance"); + } + let _ = frame_info(c, b); + } + let _ = (sei_has_recovery_point(b), pes::parse(b), sniff(b)); + let _ = ts::codec_of(b.first().copied().unwrap_or(6), b, cut % 2 == 0); + } + } + // every MPEG audio, ADTS and AC-3 header value in its first four bytes + for h in 0..=0xFFFFu32 { + for sync in [[0xFFu8, 0xE0], [0x0B, 0x77]] { + let b = [sync[0], sync[1] | (h >> 12) as u8, (h >> 4) as u8, (h << 4) as u8, h as u8, (h >> 8) as u8, 0xFF, 0x00]; + for c in &codecs[..5] { + let _ = (frame_bytes(c, &b), frame_info(c, &b)); + } + } + } +} + +/// A pack header is looked for behind at most 64 zero bytes. +#[test] +fn a_pack_header_behind_too_many_zero_bytes_is_not_sniffed() { + let pack = [0u8, 0, 1, 0xBA, 0x44, 0, 4, 0, 4, 1, 1, 0x89, 0xC3, 0xF8]; + let behind = |zeros: usize| [vec![0u8; zeros], pack.to_vec()].concat(); + assert_eq!(sniff(&behind(0)), Some(Format::Ps { mpeg1: false })); + assert_eq!(sniff(&behind(62)), Some(Format::Ps { mpeg1: false })); + assert_eq!(sniff(&behind(63)), None); + assert_eq!(sniff(&behind(5000)), None); + assert!(matches!(open(&behind(5000)), Err(Error::NotMpeg))); +} diff --git a/crates/media/src/mpegts/mod.rs b/crates/media/src/mpegts/mod.rs new file mode 100644 index 000000000..2e100f336 --- /dev/null +++ b/crates/media/src/mpegts/mod.rs @@ -0,0 +1,458 @@ +//! Clean-room MPEG-2 Systems demuxer (ITU-T H.222.0 | ISO/IEC 13818-1). +//! +//! - **Transport Stream**: 188-byte packets, 192-byte BDAV / AVCHD packets (`.m2ts`, `.mts`: +//! a 4-byte arrival timestamp before each packet) and 204-byte packets (Reed-Solomon parity). +//! PAT / PMT (sections reassembled across packets, CRC checked), PES reassembly, PCR, PTS/DTS +//! (33-bit wrap-around unwrapped), discontinuity indicators. +//! - **Program Stream** (`.mpg`, `.vob`, `.mod`, `.tod`): MPEG-2 and ISO/IEC 11172-1 (MPEG-1) +//! packs, system headers, program stream maps, DVD private stream 1 sub-streams (AC-3, DTS, +//! LPCM). +//! - **Elementary streams** are split into access units on open — coded frames for MPEG-1/2 +//! video (picture start codes, field pairs), H.264 and HEVC (access unit delimiters / first +//! slice of a picture), audio frames for MPEG audio, ADTS / LATM AAC, AC-3 and E-AC-3, PES +//! packets for LPCM — each with its PTS/DTS (a PES timestamp belongs to the first access unit +//! that starts in the packet), random-access flag and, for MPEG video, picture type, temporal +//! reference and field structure. That index is the seeking table. +//! - Units are read back on demand by re-reading the file from the unit's first packet, so the +//! index costs a few dozen bytes per access unit. +//! +//! The module does no decoding. No dependencies beyond `std`, no `unsafe`, builds for +//! `wasm32-unknown-unknown`. +//! +//! From FilmCraft's `filmcraft-mpegts` (storytold/filmcraft@4bec22b). Public items keep their names. +//! `ByteSource` is the crate's one source trait. + +#![warn(missing_docs)] + +mod es; +pub use es::{FrameInfo, frame_bytes, frame_info, sei_has_recovery_point}; +mod pes; +mod ps; +mod ts; + +use std::fmt; +use std::io; + +pub use crate::source::{ByteSource, ReadSeekSource}; + +/// Errors from reading a transport or program stream. +#[non_exhaustive] +#[derive(Debug)] +pub enum Error { + /// Neither a transport stream nor a program stream. + NotMpeg, + /// Reading the source failed. + Io(io::Error), + /// Structurally invalid data. + Invalid(String), +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::NotMpeg => write!(f, "not an MPEG transport or program stream"), + Error::Io(e) => write!(f, "I/O: {e}"), + Error::Invalid(s) => write!(f, "invalid stream: {s}"), + } + } +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Error::Io(e) => Some(e), + _ => None, + } + } +} + +impl From for Error { + fn from(e: io::Error) -> Self { + Error::Io(e) + } +} + +/// Result alias for this module. +pub type Result = std::result::Result; + +/// Read as much as is available at `offset` (up to `buf.len()`); returns the count. +pub(crate) fn read_some(src: &S, offset: u64, buf: &mut [u8]) -> io::Result { + let len = src.len(); + if offset >= len { + return Ok(0); + } + let n = ((len - offset) as usize).min(buf.len()); + src.read_at(offset, &mut buf[..n])?; + Ok(n) +} + +/// The container layout. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Format { + /// Transport stream with `packet_size`-byte packets whose 0x47 sync byte is at `sync_offset` + /// (4 for 192-byte BDAV packets). + Ts { + /// 188, 192 or 204. + packet_size: usize, + /// Offset of the sync byte in a packet. + sync_offset: usize, + }, + /// Program stream (`mpeg1`: ISO/IEC 11172-1 packs). + Ps { + /// ISO/IEC 11172-1 packs. + mpeg1: bool, + }, +} + +impl Format { + /// A display name: `"MPEG-2 TS"`, `"MPEG-2 PS"`… + pub fn name(&self) -> &'static str { + match self { + Format::Ts { packet_size: 192, .. } => "MPEG-2 TS (BDAV/AVCHD)", + Format::Ts { .. } => "MPEG-2 TS", + Format::Ps { mpeg1: true } => "MPEG-1 System", + Format::Ps { mpeg1: false } => "MPEG-2 PS", + } + } +} + +/// Recognise a transport stream (five consecutive sync bytes at one of the packet sizes, within +/// the first packets) or a program stream (a pack header). +pub fn sniff(head: &[u8]) -> Option { + for (size, off) in [(188usize, 0usize), (192, 4), (204, 0)] { + for start in 0..size.min(head.len()) { + let ok = (0..5).all(|k| head.get(start + off + k * size) == Some(&0x47)); + if ok { + return Some(Format::Ts { packet_size: size, sync_offset: off }); + } + } + } + // a pack header, possibly after a few zero bytes + let z = head.iter().take(64).take_while(|&&b| b == 0).count(); + let p = z.saturating_sub(2); + if head.len() >= p + 5 && head[p..p + 4] == [0, 0, 1, 0xBA] { + return Some(Format::Ps { mpeg1: head[p + 4] >> 6 != 1 }); + } + None +} + +/// What kind of media a stream carries. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Kind { + /// Pictures. + Video, + /// Sound. + Audio, + /// Subtitles (PGS, DVB, teletext). + Subtitle, + /// Anything else. + Other, +} + +/// What an elementary stream carries. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Codec { + /// MPEG-1 video (ISO/IEC 11172-2). + Mpeg1Video, + /// MPEG-2 video (H.262). + Mpeg2Video, + /// H.264 / AVC, Annex B. + H264, + /// H.265 / HEVC, Annex B. + Hevc, + /// MPEG-1/2 audio (layers I-III; the layer is in each frame header). + MpegAudio, + /// AAC in ADTS frames. + AacAdts, + /// AAC in LATM/LOAS frames. + AacLatm, + /// AC-3. + Ac3, + /// E-AC-3. + Eac3, + /// DTS. + Dts, + /// Dolby TrueHD. + TrueHd, + /// Blu-ray / AVCHD LPCM (a 4-byte header per PES packet, big-endian samples). + LpcmBluray, + /// DVD LPCM (program stream private stream 1; format in [`Stream::lpcm`]). + LpcmDvd, + /// Subtitles; the text names the format. + Subtitle(&'static str), + /// Anything else: the stream type. + Unknown(u8), +} + +impl Codec { + /// A display name: `"H.264"`, `"AAC (ADTS)"`, `"stream type 0x42"`. + pub fn name(&self) -> String { + match self { + Codec::Mpeg1Video => "MPEG-1 Video".into(), + Codec::Mpeg2Video => "MPEG-2 Video".into(), + Codec::H264 => "H.264".into(), + Codec::Hevc => "HEVC".into(), + Codec::MpegAudio => "MPEG Audio".into(), + Codec::AacAdts => "AAC (ADTS)".into(), + Codec::AacLatm => "AAC (LATM)".into(), + Codec::Ac3 => "AC-3".into(), + Codec::Eac3 => "E-AC-3".into(), + Codec::Dts => "DTS".into(), + Codec::TrueHd => "Dolby TrueHD".into(), + Codec::LpcmBluray => "LPCM (Blu-ray)".into(), + Codec::LpcmDvd => "LPCM (DVD)".into(), + Codec::Subtitle(s) => (*s).into(), + Codec::Unknown(t) => format!("stream type 0x{t:02x}"), + } + } + /// The crate-wide identifier. MPEG-1 video reports [`CodecId::Mpeg2Video`] (an MPEG-2 + /// decoder decodes it), MPEG audio reports [`CodecId::Mp2`] whatever its layer (the layer is + /// in each frame header: [`frame_info`]'s `variant`), and DTS, TrueHD and subtitles have no + /// identifier. + /// + /// [`CodecId::Mpeg2Video`]: crate::CodecId::Mpeg2Video + /// [`CodecId::Mp2`]: crate::CodecId::Mp2 + pub fn codec_id(&self) -> crate::CodecId { + use crate::CodecId as Id; + match self { + Codec::Mpeg1Video | Codec::Mpeg2Video => Id::Mpeg2Video, + Codec::H264 => Id::H264, + Codec::Hevc => Id::Hevc, + Codec::MpegAudio => Id::Mp2, + Codec::AacAdts | Codec::AacLatm => Id::Aac, + Codec::Ac3 => Id::Ac3, + Codec::Eac3 => Id::Eac3, + Codec::LpcmBluray | Codec::LpcmDvd => Id::Pcm, + Codec::Dts | Codec::TrueHd | Codec::Subtitle(_) | Codec::Unknown(_) => Id::Unknown, + } + } + + /// The kind of media. + pub fn kind(&self) -> Kind { + match self { + Codec::Mpeg1Video | Codec::Mpeg2Video | Codec::H264 | Codec::Hevc => Kind::Video, + Codec::MpegAudio + | Codec::AacAdts + | Codec::AacLatm + | Codec::Ac3 + | Codec::Eac3 + | Codec::Dts + | Codec::TrueHd + | Codec::LpcmBluray + | Codec::LpcmDvd => Kind::Audio, + Codec::Subtitle(_) => Kind::Subtitle, + Codec::Unknown(_) => Kind::Other, + } + } +} + +/// MPEG-1/2 video picture properties of an access unit (its first picture). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct PictureInfo { + /// 1 I, 2 P, 3 B, 4 D. + pub coding_type: u8, + /// `temporal_reference`: display order within the GOP. + pub temporal_reference: u16, + /// 1 top field, 2 bottom field, 3 frame (two field pictures form one unit). + pub structure: u8, + /// `top_field_first` of the picture coding extension. + pub top_field_first: bool, + /// `repeat_first_field` (pulldown). + pub repeat_first_field: bool, + /// `progressive_frame`. + pub progressive_frame: bool, + /// A sequence header precedes the picture in this unit. + pub sequence_header: bool, + /// A GOP header precedes the picture (and its flags). + pub gop: bool, + /// `closed_gop` of that GOP header. + pub closed_gop: bool, + /// `broken_link` of that GOP header. + pub broken_link: bool, +} + +/// One access unit (a coded frame, an audio frame, or an LPCM PES packet). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Unit { + /// Presentation / decoding time stamps, 90 kHz, 33-bit wrap-around unwrapped. + pub pts: Option, + /// See `pts`. + pub dts: Option, + /// Decoding can start here (I / IDR / IRAP pictures; every audio frame). + pub key: bool, + /// No other unit depends on it (B pictures of MPEG-1/2 video, non-reference H.264/HEVC). + pub disposable: bool, + /// Bytes of elementary stream. + pub size: u32, + /// TS: byte offset of the packet holding the first byte; PS: byte offset of its PES packet. + pub(crate) pos: u64, + /// Offset of the first byte within that packet's elementary-stream payload. + pub(crate) offset: u32, + /// MPEG-1/2 video: the picture's properties. + pub picture: Option, +} + +/// DVD LPCM format (from the private stream 1 header). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct LpcmFormat { + /// Samples per second. + pub sample_rate: u32, + /// Channel count. + pub channels: u32, + /// Bits per sample (16, 20 or 24). + pub bits: u32, +} + +/// Where a stream's PES packets come from. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StreamId { + /// Transport stream PID. + Pid(u16), + /// Program stream stream_id, and the private stream 1 sub-stream id. + Ps { + /// PES stream_id. + stream_id: u8, + /// Private stream 1 sub-stream id. + sub_id: Option, + }, +} + +/// One elementary stream and its access-unit table. +#[derive(Clone, Debug)] +pub struct Stream { + /// Where its PES packets come from. + pub id: StreamId, + /// PMT stream_type (TS), or the equivalent for program streams. + pub stream_type: u8, + /// What it carries. + pub codec: Codec, + /// ISO 639 language code from the PMT. + pub language: Option, + /// The access units, in stream order. + pub units: Vec, + /// DVD LPCM: the sample format. + pub lpcm: Option, + /// PES packets seen. + pub pes_packets: u64, + /// Bytes of payload ignored because the stream could not be split (e.g. before the first + /// frame header). + pub skipped_bytes: u64, +} + +impl Stream { + /// The kind of media. + pub fn kind(&self) -> Kind { + self.codec.kind() + } + /// What a decoder needs for this stream, in the container-neutral form the codec crates take: + /// the codec ([`Codec::codec_id`]) and, for DVD LPCM, the sample format (big-endian). These + /// streams carry their configuration in band, so `extradata` is empty: H.264 and HEVC are + /// Annex B with their parameter sets, AAC is ADTS or LATM frames, and a Blu-ray LPCM packet + /// starts with its own 4-byte format header. + pub fn codec_params(&self) -> crate::CodecParams { + let mut p = crate::CodecParams::new(self.codec.codec_id()); + if let Some(f) = self.lpcm { + let n = |v: u32| u16::try_from(v).unwrap_or(0); + p = p.with_sample_rate(f.sample_rate).with_channels(n(f.channels)).with_pcm(crate::PcmFormat::int(n(f.bits), true, true)); + } + p + } + /// First and last presentation timestamps (90 kHz) of the stream's units. + pub fn pts_range(&self) -> Option<(i64, i64)> { + let mut it = self.units.iter().filter_map(|u| u.pts); + let first = it.next()?; + Some(it.fold((first, first), |(a, b), p| (a.min(p), b.max(p)))) + } +} + +/// A demuxed file: its streams with their access-unit tables. +#[derive(Clone, Debug)] +pub struct File { + /// The container layout. + pub format: Format, + /// The elementary streams (TS: in PMT order). + pub streams: Vec, + /// Program number of the program the streams belong to (TS). + pub program: Option, + /// First and last PCR (27 MHz, unwrapped) or SCR. + pub pcr_range: Option<(i64, i64)>, + /// Problems found while scanning (lost sync, CRC errors, continuity errors…). + pub warnings: Vec, +} + +/// Largest unit `read_unit` returns. The biggest real units are intra pictures (a few MB at +/// 4K/8K broadcast rates); a stream without PES timestamps can make one unit of a whole PID. +pub const MAX_UNIT_BYTES: u32 = 128 << 20; + +/// Bytes reserved up front by `read_unit`; the buffer grows beyond this only as data is read. +const UNIT_PREALLOC: usize = 1 << 20; + +impl File { + /// The first stream of `kind` whose codec satisfies `f`. + pub fn find(&self, kind: Kind, f: impl Fn(&Codec) -> bool) -> Option { + self.streams.iter().position(|s| s.kind() == kind && !s.units.is_empty() && f(&s.codec)) + } + + /// Read access unit `i` of stream `s`. + pub fn read_unit(&self, src: &S, s: usize, i: usize) -> Result> { + let st = self.streams.get(s).ok_or_else(|| Error::Invalid("no such stream".into()))?; + let u = st.units.get(i).ok_or_else(|| Error::Invalid("no such unit".into()))?; + if u.size > MAX_UNIT_BYTES { + return Err(Error::Invalid(format!("unit {i} is {} bytes (limit {MAX_UNIT_BYTES})", u.size))); + } + // grow as bytes arrive: a damaged table must not reserve more than the file can supply + let mut out = Vec::with_capacity((u.size as usize).min(UNIT_PREALLOC)); + match self.format { + Format::Ts { packet_size, sync_offset } => ts::read(src, packet_size, sync_offset, st, u, &mut out)?, + Format::Ps { .. } => ps::read(src, st, u, &mut out)?, + } + if out.len() < u.size as usize { + return Err(Error::Invalid(format!("unit {i} truncated ({} of {} bytes)", out.len(), u.size))); + } + Ok(out) + } +} + +/// Scan a transport or program stream and index its access units. +pub fn open(src: &S) -> Result { + let mut head = vec![0u8; 64 * 1024]; + let n = read_some(src, 0, &mut head)?; + head.truncate(n); + match sniff(&head).ok_or(Error::NotMpeg)? { + Format::Ts { packet_size, sync_offset } => ts::scan(src, packet_size, sync_offset), + Format::Ps { mpeg1 } => ps::scan(src, mpeg1), + } +} + +/// Unwraps 33-bit timestamps into a monotonic-ish i64 timeline (relative to the first one seen). +#[derive(Default, Clone, Copy)] +pub(crate) struct Unwrap { + last: Option, + base: i64, +} + +impl Unwrap { + const WRAP: i64 = 1 << 33; + pub fn unwrap(&mut self, raw: i64) -> i64 { + let mut v = raw + self.base; + if let Some(l) = self.last { + if v < l - Self::WRAP / 2 { + self.base += Self::WRAP; + v += Self::WRAP; + } else if v > l + Self::WRAP / 2 && self.base > 0 { + // a timestamp from before the wrap (B pictures, slightly out-of-order audio) + v -= Self::WRAP; + return v; + } + } + self.last = Some(v); + v + } +} + +#[cfg(test)] +mod hostile_tests; +#[cfg(test)] +mod tests; +#[cfg(test)] +mod work_tests; diff --git a/crates/media/src/mpegts/pes.rs b/crates/media/src/mpegts/pes.rs new file mode 100644 index 000000000..06c7a2abf --- /dev/null +++ b/crates/media/src/mpegts/pes.rs @@ -0,0 +1,126 @@ +//! PES packet headers (H.222.0 §2.4.3.6; ISO/IEC 11172-1 §2.4.3.3 for MPEG-1 packets). + +/// A parsed PES header. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct PesHeader { + /// Bytes from the packet start code to the first payload byte. + pub header_len: usize, + /// Raw 33-bit timestamps. + pub pts: Option, + pub dts: Option, +} + +/// Stream ids whose packets have no optional header (§2.4.3.7). +pub(crate) fn plain_stream(id: u8) -> bool { + matches!(id, 0xBC | 0xBE | 0xBF | 0xF0 | 0xF1 | 0xF2 | 0xF8 | 0xFF) +} + +pub(crate) fn timestamp(b: &[u8]) -> i64 { + (((b[0] >> 1) & 7) as i64) << 30 | (b[1] as i64) << 22 | ((b[2] >> 1) as i64) << 15 | (b[3] as i64) << 7 | (b[4] >> 1) as i64 +} + +/// Parse a PES header at the start of `b` (which begins with `00 00 01 stream_id`). `None` when +/// `b` is too short to hold the whole header or it is malformed. +pub(crate) fn parse(b: &[u8]) -> Option { + if b.len() < 6 || b[0] != 0 || b[1] != 0 || b[2] != 1 { + return None; + } + if plain_stream(b[3]) { + return Some(PesHeader { header_len: 6, pts: None, dts: None }); + } + if b.len() >= 9 && b[6] & 0xC0 == 0x80 { + // ISO/IEC 13818-1 syntax + let flags = b[7]; + let header_len = 9 + b[8] as usize; + if b.len() < header_len { + return None; + } + let (mut pts, mut dts) = (None, None); + if flags & 0x80 != 0 && header_len >= 14 { + pts = Some(timestamp(&b[9..14])); + if flags & 0x40 != 0 && header_len >= 19 { + dts = Some(timestamp(&b[14..19])); + } + } + return Some(PesHeader { header_len, pts, dts }); + } + // ISO/IEC 11172-1 syntax: stuffing, STD buffer, timestamps + let mut p = 6; + let mut stuffing = 0; + while b.get(p) == Some(&0xFF) { + p += 1; + stuffing += 1; + if stuffing > 16 { + return None; + } + } + let c = *b.get(p)?; + if c >> 6 == 1 { + p += 2; + } + let c = *b.get(p)?; + let (mut pts, mut dts) = (None, None); + match c >> 4 { + 2 => { + pts = Some(timestamp(b.get(p..p + 5)?)); + p += 5; + } + 3 => { + pts = Some(timestamp(b.get(p..p + 5)?)); + dts = Some(timestamp(b.get(p + 5..p + 10)?)); + p += 10; + } + 0 if c == 0x0F => p += 1, + _ => return None, + } + Some(PesHeader { header_len: p, pts, dts }) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ts_bytes(prefix: u8, v: i64) -> [u8; 5] { + [ + (prefix << 4) | (((v >> 30) & 7) as u8) << 1 | 1, + (v >> 22) as u8, + (((v >> 15) & 0x7F) as u8) << 1 | 1, + (v >> 7) as u8, + ((v & 0x7F) as u8) << 1 | 1, + ] + } + + #[test] + fn mpeg2_and_mpeg1_headers() { + let pts = 0x1_2345_6789i64; + let dts = 0x0_8765_4321i64; + let mut b = vec![0, 0, 1, 0xE0, 0, 0, 0x80, 0xC0, 10]; + b.extend(ts_bytes(3, pts)); + b.extend(ts_bytes(1, dts)); + b.push(0xAA); + let h = parse(&b).unwrap(); + assert_eq!((h.header_len, h.pts, h.dts), (19, Some(pts), Some(dts))); + // MPEG-1: stuffing, STD buffer, PTS only + let mut b = vec![0, 0, 1, 0xC0, 0, 20, 0xFF, 0xFF, 0x40, 0x20]; + b.extend(ts_bytes(2, 90_000)); + let h = parse(&b).unwrap(); + assert_eq!((h.header_len, h.pts, h.dts), (15, Some(90_000), None)); + assert_eq!(parse(&[0, 0, 1, 0xC0, 0, 1, 0x0F]).unwrap().header_len, 7); + assert_eq!(parse(&[0, 0, 1, 0xBE, 0, 4]).unwrap().header_len, 6); + assert!(parse(&[0, 0, 1, 0xE0, 0, 0, 0x80, 0x80]).is_none()); + } + + /// MPEG-1 packets: at most 16 stuffing bytes before the timestamps. + #[test] + fn mpeg1_stuffing_past_the_limit_is_refused() { + let with = |stuffing: usize| { + let mut b = vec![0, 0, 1, 0xC0, 0, 40]; + b.extend(std::iter::repeat_n(0xFF, stuffing)); + b.extend(ts_bytes(2, 90_000)); + parse(&b) + }; + assert_eq!(with(16).map(|h| (h.header_len, h.pts)), Some((6 + 16 + 5, Some(90_000)))); + assert_eq!(with(17), None); + assert_eq!(with(5000), None); + } +} diff --git a/crates/media/src/mpegts/ps.rs b/crates/media/src/mpegts/ps.rs new file mode 100644 index 000000000..41c5486a9 --- /dev/null +++ b/crates/media/src/mpegts/ps.rs @@ -0,0 +1,370 @@ +//! Program streams (H.222.0 §2.5; ISO/IEC 11172-1 §2.4.3): packs, system headers, the program +//! stream map, PES packets, and the DVD sub-streams of private stream 1. + +use std::collections::HashMap; + +use super::es::Splitter; +use super::pes; +use super::{ByteSource, Codec, Error, File, Format, LpcmFormat, Result, Stream, StreamId, Unit, Unwrap, read_some}; + +/// Sequential reader with a refillable window. +struct Win<'a, S: ByteSource + ?Sized> { + src: &'a S, + buf: Vec, + start: u64, + /// Bytes zero-filled by window refills (tests count them). + #[cfg(test)] + zero_filled: usize, +} + +impl<'a, S: ByteSource + ?Sized> Win<'a, S> { + fn new(src: &'a S) -> Self { + Self { + src, + buf: Vec::new(), + start: 0, + #[cfg(test)] + zero_filled: 0, + } + } + /// The bytes [pos, pos + n) (fewer at the end of the file). + fn get(&mut self, pos: u64, n: usize) -> Result<&[u8]> { + let have = pos >= self.start && pos + n as u64 <= self.start + self.buf.len() as u64; + if !have { + // never more than the file still holds past `pos`: `read_some` would not fill it, and + // zeroing a megabyte for every packet near the end of the file is wasted work + let remaining = self.src.len().saturating_sub(pos); + let want = (n.max(1 << 20) as u64).min(remaining) as usize; + #[cfg(test)] + { + self.zero_filled += want.saturating_sub(self.buf.len()); + } + self.buf.resize(want, 0); + let got = read_some(self.src, pos, &mut self.buf)?; + self.buf.truncate(got); + self.start = pos; + } + let i = (pos - self.start) as usize; + let end = (i + n).min(self.buf.len()); + Ok(&self.buf[i.min(end)..end]) + } +} + +/// How many bytes of a private stream 1 payload precede the elementary stream, by sub-stream id +/// (DVD-Video: AC-3 / DTS 4, LPCM 7, sub-pictures 1). +fn sub_header(sub: u8) -> usize { + match sub { + 0x80..=0x8F => 4, + 0xA0..=0xAF => 7, + _ => 1, + } +} + +fn codec_for(stream_id: u8, sub: Option, psm: &HashMap, mpeg1: bool) -> Codec { + if let Some(&t) = psm.get(&stream_id) { + let c = super::ts::codec_of(t, &[], false); + if !matches!(c, Codec::Unknown(_)) { + return c; + } + } + match (stream_id, sub) { + (0xE0..=0xEF, _) => { + if mpeg1 { + Codec::Mpeg1Video + } else { + Codec::Mpeg2Video + } + } + (0xC0..=0xDF, _) => Codec::MpegAudio, + (0xBD, Some(0x80..=0x87)) => Codec::Ac3, + (0xBD, Some(0x88..=0x8F)) => Codec::Dts, + (0xBD, Some(0xA0..=0xAF)) => Codec::LpcmDvd, + (0xBD, Some(0x20..=0x3F)) => Codec::Subtitle("DVD sub-pictures"), + (id, _) => Codec::Unknown(id), + } +} + +/// DVD LPCM header bytes after the sub-stream id (and the frame count / pointer). +fn lpcm_format(h: &[u8]) -> Option { + let b = *h.get(4)?; + let bits = [16, 20, 24, 0][(b >> 6) as usize]; + let sample_rate = [48_000, 96_000, 44_100, 32_000][((b >> 4) & 3) as usize]; + let channels = (b & 7) as u32 + 1; + (bits > 0).then_some(LpcmFormat { sample_rate, channels, bits }) +} + +struct EsState { + stream: Stream, + split: Splitter, +} + +/// Length of the packet (pack header, system header, PES…) starting at `b` (which begins with a +/// start code), `None` if it is not one. +fn packet_len(b: &[u8]) -> Option { + if b.len() < 6 || b[0] != 0 || b[1] != 0 || b[2] != 1 { + return None; + } + match b[3] { + 0xBA => { + if b[4] >> 6 == 1 { + let stuff = (*b.get(13)? & 7) as usize; + Some(14 + stuff) + } else if b[4] >> 4 == 2 { + // MPEG-1 pack header: start code, 5-byte SCR, 3-byte mux rate. A file cut inside + // it is not a pack header yet (`scan` reads the SCR from the first 9 bytes). + b.get(..12)?; + Some(12) + } else { + None + } + } + 0xB9 => Some(4), + 0xBB..=0xFF => Some(6 + u16::from_be_bytes([b[4], b[5]]) as usize), + _ => None, + } +} + +pub(crate) fn scan(src: &S, mpeg1: bool) -> Result { + let len = src.len(); + let mut win = Win::new(src); + let mut pos = 0u64; + let mut streams: HashMap<(u8, Option), EsState> = HashMap::new(); + let mut order = Vec::new(); + let mut psm: HashMap = HashMap::new(); + let mut unwrap = Unwrap::default(); + let mut scr: Option<(i64, i64)> = None; + let mut resyncs = 0u64; + while pos + 4 <= len { + let head = win.get(pos, 32)?; + let Some(plen) = packet_len(head) else { + // resynchronise on the next pack header or PES start code + resyncs += 1; + let mut p = pos + 1; + let found = loop { + let w = win.get(p, 1 << 16)?; + if w.len() < 4 { + break None; + } + if let Some(i) = w.windows(4).position(|x| x[0] == 0 && x[1] == 0 && x[2] == 1 && (x[3] == 0xBA || (0xBD..=0xEF).contains(&x[3]))) { + break Some(p + i as u64); + } + p += (w.len() - 3) as u64; + }; + match found { + Some(f) => { + pos = f; + continue; + } + None => break, + } + }; + let id = head[3]; + if id == 0xBA { + // SCR (MPEG-2: 33 bits base, 9 bits extension; MPEG-1: 33 bits) + let v = if head[4] >> 6 == 1 { + let b = &head[4..10]; + let base = (((b[0] >> 3) & 7) as i64) << 30 + | ((b[0] & 3) as i64) << 28 + | (b[1] as i64) << 20 + | ((b[2] >> 3) as i64) << 15 + | ((b[2] & 3) as i64) << 13 + | (b[3] as i64) << 5 + | (b[4] >> 3) as i64; + let ext = (((b[4] & 3) as i64) << 7) | (b[5] >> 1) as i64; + base * 300 + ext + } else { + pes::timestamp(&head[4..9]) * 300 + }; + scr = Some(scr.map_or((v, v), |(a, b): (i64, i64)| (a.min(v), b.max(v)))); + } else if id == 0xBC { + // program stream map: elementary stream id → stream_type + let body = win.get(pos, plen)?; + if body.len() >= 12 { + let info = u16::from_be_bytes([body[8], body[9]]) as usize; + let mut i = 10 + info; + if i + 2 <= body.len() { + let map_len = u16::from_be_bytes([body[i], body[i + 1]]) as usize; + i += 2; + let end = (i + map_len).min(body.len()); + while i + 4 <= end { + psm.insert(body[i + 1], body[i]); + i += 4 + u16::from_be_bytes([body[i + 2], body[i + 3]]) as usize; + } + } + } + } else if id == 0xBD || (0xC0..=0xEF).contains(&id) || id == 0xFD { + let pkt = win.get(pos, plen)?; + if pkt.len() < plen { + break; // truncated + } + if let Some(h) = pes::parse(pkt) { + let payload = &pkt[h.header_len.min(plen)..]; + let (sub, skip) = if id == 0xBD { + match payload.first() { + Some(&s) => (Some(s), sub_header(s).min(payload.len())), + None => (None, 0), + } + } else { + (None, 0) + }; + let key = (id, sub); + let e = streams.entry(key).or_insert_with(|| { + let codec = codec_for(id, sub, &psm, mpeg1); + let lpcm = if codec == Codec::LpcmDvd { lpcm_format(&payload[1.min(payload.len())..]) } else { None }; + order.push(key); + let stream = Stream { + id: StreamId::Ps { stream_id: id, sub_id: sub }, + stream_type: psm.get(&id).copied().unwrap_or(0), + codec: codec.clone(), + language: None, + units: Vec::new(), + lpcm, + pes_packets: 0, + skipped_bytes: 0, + }; + EsState { stream, split: Splitter::new(&codec) } + }); + e.stream.pes_packets += 1; + e.split.pes_start(h.pts.map(|t| unwrap.unwrap(t)), h.dts.map(|t| unwrap.unwrap(t))); + e.split.feed(pos, &payload[skip..]); + } + } + pos += plen as u64; + } + let mut warnings = Vec::new(); + if resyncs > 0 { + warnings.push(format!("resynchronised {resyncs} time(s)")); + } + let mut out = Vec::new(); + for key in order { + let Some(mut e) = streams.remove(&key) else { continue }; + e.split.finish(); + e.stream.units = std::mem::take(&mut e.split.units); + e.stream.skipped_bytes = e.split.skipped; + out.push(e.stream); + } + if out.is_empty() { + return Err(Error::Invalid("program stream without elementary streams".into())); + } + Ok(File { format: Format::Ps { mpeg1 }, streams: out, program: None, pcr_range: scr, warnings }) +} + +/// Collect unit `u` of stream `st`, starting at the PES packet at `u.pos`. +pub(crate) fn read(src: &S, st: &Stream, u: &Unit, out: &mut Vec) -> Result<()> { + let StreamId::Ps { stream_id, sub_id } = st.id else { return Err(Error::Invalid("not a PS stream".into())) }; + let need = u.size as usize; + let mut skip = u.offset as usize; + let mut pos = u.pos; + let len = src.len(); + let mut win = Win::new(src); + while out.len() < need && pos + 4 <= len { + let head = win.get(pos, 32)?; + let Some(plen) = packet_len(head) else { + // corrupt: stop at what we have + return Ok(()); + }; + if head[3] == stream_id { + let pkt = win.get(pos, plen)?; + if let Some(h) = pes::parse(pkt) { + let payload = &pkt[h.header_len.min(pkt.len())..]; + let matches = stream_id != 0xBD || payload.first().copied() == sub_id; + if matches { + let s0 = if stream_id == 0xBD { sub_id.map_or(0, sub_header).min(payload.len()) } else { 0 }; + let mut bytes = &payload[s0..]; + if skip > 0 { + let s = skip.min(bytes.len()); + skip -= s; + bytes = &bytes[s..]; + } + let take = bytes.len().min(need - out.len()); + out.extend_from_slice(&bytes[..take]); + } + } + } + pos += plen as u64; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The window refill before the zero-fill cap: always `max(n, 1 MiB)` bytes, counted when the + /// buffer has to grow. + struct OldWin<'a> { + src: &'a [u8], + buf: Vec, + start: u64, + zero_filled: usize, + } + + impl OldWin<'_> { + fn get(&mut self, pos: u64, n: usize) -> Vec { + let have = pos >= self.start && pos + n as u64 <= self.start + self.buf.len() as u64; + if !have { + let want = n.max(1 << 20); + self.zero_filled += want.saturating_sub(self.buf.len()); + self.buf.resize(want, 0); + let got = read_some(self.src, pos, &mut self.buf).unwrap(); + self.buf.truncate(got); + self.start = pos; + } + let i = (pos - self.start) as usize; + let end = (i + n).min(self.buf.len()); + self.buf[i.min(end)..end].to_vec() + } + } + + fn data(len: usize) -> Vec { + (0..len).map(|i| (i * 31 + i / 7) as u8).collect() + } + + #[test] + fn capped_window_returns_what_the_uncapped_window_returned() { + for len in [0usize, 1, 31, 32, 33, 1000, (1 << 20) - 1, 1 << 20, (1 << 20) + 5, (3 << 20) + 17] { + let d = data(len); + let mut new = Win::new(d.as_slice()); + let mut old = OldWin { src: &d, buf: Vec::new(), start: 0, zero_filled: 0 }; + let len64 = len as u64; + let mut positions = vec![ + 0, + 1, + 31, + 32, + 1 << 19, + 1 << 20, + (1 << 20) - 1, + (1 << 20) + 1, + 2 << 20, + len64.saturating_sub(40), + len64.saturating_sub(1), + len64, + len64 + 1, + len64 + 100, + ]; + positions.extend((0..10).map(|k| len64.saturating_sub(100) + k * 13)); + for &pos in &positions { + for n in [0usize, 1, 33, 1000, 65_541, 1 << 20, (1 << 20) + 1] { + assert_eq!(new.get(pos, n).unwrap(), old.get(pos, n).as_slice(), "len {len} pos {pos} n {n}"); + } + } + } + } + + #[test] + fn misses_near_the_end_zero_at_most_the_bytes_that_are_left() { + let len = (2 << 20) + 1000; + let d = data(len); + let mut new = Win::new(d.as_slice()); + let mut old = OldWin { src: &d, buf: Vec::new(), start: 0, zero_filled: 0 }; + // a PES packet whose length runs past the end of the file, asked for once per packet + let first = (2u64 << 20) as usize; + for k in 0..500 { + let pos = (first + k) as u64; + assert_eq!(new.get(pos, 2000).unwrap(), old.get(pos, 2000).as_slice()); + } + assert!(new.zero_filled <= 1000, "zeroed {} bytes, only 1000 are left in the file", new.zero_filled); + assert!(old.zero_filled > 499 * ((1 << 20) - 1000), "the reference zeroes about a megabyte per miss: {}", old.zero_filled); + } +} diff --git a/crates/media/src/mpegts/tests.rs b/crates/media/src/mpegts/tests.rs new file mode 100644 index 000000000..cec62ca90 --- /dev/null +++ b/crates/media/src/mpegts/tests.rs @@ -0,0 +1,650 @@ +//! Synthetic transport and program streams: a small muxer writes known access units, the +//! demuxer must give them back exactly (bytes, timestamps, flags); truncation and mutation never +//! panic. + +use super::*; + +fn ts_bytes(prefix: u8, v: i64) -> [u8; 5] { + let v = v & ((1 << 33) - 1); + [ + (prefix << 4) | (((v >> 30) & 7) as u8) << 1 | 1, + (v >> 22) as u8, + (((v >> 15) & 0x7F) as u8) << 1 | 1, + (v >> 7) as u8, + ((v & 0x7F) as u8) << 1 | 1, + ] +} + +pub(super) fn pes(stream_id: u8, pts: Option, dts: Option, payload: &[u8], bounded: bool) -> Vec { + let mut h = Vec::new(); + match (pts, dts) { + (Some(p), Some(d)) => { + h.extend(ts_bytes(3, p)); + h.extend(ts_bytes(1, d)); + } + (Some(p), None) => h.extend(ts_bytes(2, p)), + _ => {} + } + let flags = match (pts, dts) { + (Some(_), Some(_)) => 0xC0, + (Some(_), None) => 0x80, + _ => 0, + }; + let len = if bounded { 3 + h.len() + payload.len() } else { 0 }; + let mut v = vec![0, 0, 1, stream_id, (len >> 8) as u8, len as u8, 0x80, flags, h.len() as u8]; + v.extend(h); + v.extend_from_slice(payload); + v +} + +/// A tiny TS muxer: PAT, PMT and PES packets cut into 188-byte packets (192 with `m2ts`). +pub(super) struct TsMux { + pub(super) out: Vec, + cc: std::collections::HashMap, + m2ts: bool, +} + +impl TsMux { + pub(super) fn new(m2ts: bool) -> Self { + Self { out: Vec::new(), cc: Default::default(), m2ts } + } + fn packet(&mut self, pid: u16, pusi: bool, payload: &[u8]) -> usize { + let cc = self.cc.entry(pid).or_insert(0); + let mut p = vec![0x47, (pusi as u8) << 6 | (pid >> 8) as u8, pid as u8]; + let n = payload.len().min(184); + if n < 184 { + // adaptation field stuffing + let al = 183 - n; + p.push(0x30 | *cc); + p.push(al as u8); + if al > 0 { + p.push(0); + p.extend(std::iter::repeat_n(0xFF, al - 1)); + } + } else { + p.push(0x10 | *cc); + } + *cc = (*cc + 1) & 15; + p.extend_from_slice(&payload[..n]); + assert_eq!(p.len(), 188); + if self.m2ts { + self.out.extend_from_slice(&[0, 0, 0, 0]); + } + self.out.extend(p); + n + } + pub(super) fn section(&mut self, pid: u16, body: &[u8]) { + let mut s = body.to_vec(); + let crc = super::ts::crc32_mpeg(&s); + s.extend(crc.to_be_bytes()); + let mut payload = vec![0]; + payload.extend(s); + self.packet(pid, true, &payload); + } + pub(super) fn psi(&mut self, streams: &[(u8, u16)]) { + // PAT: program 1 → PMT PID 0x100 + let pat = [0x00, 0xB0, 13, 0, 1, 0xC1, 0, 0, 0, 1, 0xE1, 0x00]; + self.section(0, &pat); + let mut pmt = vec![0x02, 0xB0, 0, 0, 1, 0xC1, 0, 0, 0xE1, 0x00, 0xF0, 0]; + for &(st, pid) in streams { + pmt.extend([st, 0xE0 | (pid >> 8) as u8, pid as u8, 0xF0, 0]); + } + let len = pmt.len() - 3 + 4; + pmt[1] = 0xB0 | (len >> 8) as u8; + pmt[2] = len as u8; + self.section(0x100, &pmt); + } + pub(super) fn pes(&mut self, pid: u16, data: &[u8]) { + let mut rest = data; + let mut first = true; + while !rest.is_empty() { + let n = self.packet(pid, first, rest); + rest = &rest[n..]; + first = false; + } + } +} + +/// An MPEG-2 coded frame: sequence header (on I), GOP, picture header, picture coding +/// extension and a slice of filler; or a field pair. +fn mpeg2_frame(coding_type: u8, tref: u16, fields: bool, filler: usize) -> Vec { + let mut v = Vec::new(); + if coding_type == 1 { + v.extend([0, 0, 1, 0xB3, 0x14, 0x00, 0xF0, 0x13, 0xFF, 0xFF, 0xE0, 0x18]); + v.extend([0, 0, 1, 0xB8, 0, 8, 0, 0x40]); + } + let pics: &[u8] = if fields { &[1, 2] } else { &[3] }; + for &s in pics { + v.extend([0, 0, 1, 0x00, (tref >> 2) as u8, ((tref & 3) << 6) as u8 | coding_type << 3, 0xFF, 0xF8]); + v.extend([0, 0, 1, 0xB5, 0x8F, 0xFF, 0xF0 | s, 0x80, 0x80]); + v.extend([0, 0, 1, 0x01]); + v.extend((0..filler).map(|i| (i % 200) as u8 | 0x10)); + } + v +} + +/// MPEG-1 layer II frame, 48 kHz 192 kb/s (576 bytes). +fn mp2_frame(k: u8) -> Vec { + let mut v = vec![0xFF, 0xFD, 0xA4, 0x00]; + v.extend((0..572).map(|i| (i as u8) ^ k)); + v +} + +#[test] +fn transport_stream_units_round_trip() { + for m2ts in [false, true] { + let mut mux = TsMux::new(m2ts); + mux.psi(&[(0x02, 0x1011), (0x03, 0x1100)]); + // decode order I0 P3 B1 B2 with PTS = display time, DTS on anchors + let frames = [(1u8, 0u16, false), (2, 3, false), (3, 1, true), (3, 2, false)]; + let mut expect_video = Vec::new(); + for (k, &(t, tref, fields)) in frames.iter().enumerate() { + let f = mpeg2_frame(t, tref, fields, 300 + 97 * k); + let pts = 90_000 + tref as i64 * 3600; + let dts = (t != 3).then_some(90_000 - 3600 + k as i64 * 3600); + mux.pes(0x1011, &pes(0xE0, Some(pts), dts, &f, false)); + expect_video.push((f, pts, t)); + // two audio frames per PES, the second without its own timestamp + let a: Vec = [mp2_frame(2 * k as u8), mp2_frame(2 * k as u8 + 1)].concat(); + mux.pes(0x1100, &pes(0xC0, Some(90_000 + 2160 * 2 * k as i64), None, &a, true)); + } + let data = mux.out; + let f = open(&data).unwrap(); + assert_eq!(f.format, Format::Ts { packet_size: if m2ts { 192 } else { 188 }, sync_offset: if m2ts { 4 } else { 0 } }); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); + assert_eq!(f.program, Some(1)); + let v = f.find(Kind::Video, |_| true).unwrap(); + let s = &f.streams[v]; + assert_eq!(s.codec, Codec::Mpeg2Video); + assert_eq!(s.units.len(), 4); + for (i, (bytes, pts, t)) in expect_video.iter().enumerate() { + let u = &s.units[i]; + assert_eq!(f.read_unit(&data, v, i).unwrap(), *bytes, "unit {i}"); + assert_eq!(u.pts, Some(*pts)); + assert_eq!(u.key, *t == 1); + assert_eq!(u.disposable, *t == 3); + assert_eq!(u.picture.unwrap().coding_type, *t); + } + assert_eq!(s.units[2].picture.unwrap().structure, 1, "field pair in one unit"); + assert!(s.units[0].picture.unwrap().sequence_header && s.units[0].picture.unwrap().gop); + let a = f.find(Kind::Audio, |c| *c == Codec::MpegAudio).unwrap(); + let s = &f.streams[a]; + assert_eq!(s.units.len(), 8); + for (i, u) in s.units.iter().enumerate() { + assert_eq!(f.read_unit(&data, a, i).unwrap(), mp2_frame(i as u8)); + assert_eq!(u.pts, (i % 2 == 0).then_some(90_000 + 2160 * i as i64)); + } + } +} + +#[test] +fn program_stream_units_round_trip() { + // packs of ≤ 2048 bytes: frames are split across PES packets + let frames: Vec> = (0..5).map(|k| mpeg2_frame(if k == 0 { 1 } else { 2 }, k as u16, k == 3, 2500 + 300 * k)).collect(); + let es: Vec = frames.concat(); + let mut starts = Vec::new(); + let mut acc = 0; + for f in &frames { + starts.push(acc); + acc += f.len(); + } + let mut out = Vec::new(); + let mut p = 0; + let mut a = 0u8; + while p < es.len() { + out.extend([0, 0, 1, 0xBA, 0x44, 0, 4, 0, 4, 1, 1, 0x89, 0xC3, 0xF8]); + let n = (es.len() - p).min(2000); + // a timestamp when a frame starts in this packet + let k = starts.iter().position(|&s| s >= p && s < p + n); + out.extend(pes(0xE0, k.map(|k| 3600 * k as i64), None, &es[p..p + n], true)); + p += n; + // AC-3 in private stream 1 (sub-stream 0x80, 4-byte header), 448 kb/s frames + let mut ac3 = vec![0x80, 1, 0, 1, 0x0B, 0x77, 0, 0, 30, 0x40]; + ac3.extend(std::iter::repeat_n(a, 1792 - 6)); + a += 1; + out.extend(pes(0xBD, Some(1000 + 2880 * a as i64), None, &ac3, true)); + } + out.extend([0, 0, 1, 0xB9]); + let f = open(&out).unwrap(); + assert_eq!(f.format, Format::Ps { mpeg1: false }); + let v = f.find(Kind::Video, |_| true).unwrap(); + assert_eq!(f.streams[v].units.len(), 5); + for (i, fr) in frames.iter().enumerate() { + assert_eq!(&f.read_unit(&out, v, i).unwrap(), fr); + assert_eq!(f.streams[v].units[i].pts, Some(3600 * i as i64)); + } + let a = f.find(Kind::Audio, |c| *c == Codec::Ac3).unwrap(); + assert_eq!(f.streams[a].id, StreamId::Ps { stream_id: 0xBD, sub_id: Some(0x80) }); + assert_eq!(f.streams[a].units.len(), a_count(&out)); + let u0 = f.read_unit(&out, a, 0).unwrap(); + assert_eq!(&u0[..2], &[0x0B, 0x77]); + assert_eq!(u0.len(), 1792); +} + +fn a_count(ps: &[u8]) -> usize { + ps.windows(4).filter(|w| *w == [0, 0, 1, 0xBD]).count() +} + +#[test] +fn sniffing() { + assert_eq!(sniff(&[0, 0, 1, 0xBA, 0x44]), Some(Format::Ps { mpeg1: false })); + assert_eq!(sniff(&[0, 0, 1, 0xBA, 0x21]), Some(Format::Ps { mpeg1: true })); + assert_eq!(sniff(&[0, 0, 1, 0xB3, 0x44]), None); + let mut ts = vec![0u8; 188 * 6]; + for k in 0..6 { + ts[k * 188] = 0x47; + } + assert!(matches!(sniff(&ts), Some(Format::Ts { packet_size: 188, .. }))); +} + +#[test] +fn truncation_and_mutation_never_panic() { + let mut mux = TsMux::new(false); + mux.psi(&[(0x02, 0x1011), (0x0F, 0x1100), (0x1B, 0x1200)]); + for k in 0..6u16 { + mux.pes(0x1011, &pes(0xE0, Some(3600 * k as i64), None, &mpeg2_frame(if k == 0 { 1 } else { 3 }, k, k % 2 == 1, 400), false)); + let mut adts = vec![0xFF, 0xF1, 0x50, 0x80, 0x0C, 0x9F, 0xFC]; + adts.extend([0x21u8; 93]); + mux.pes(0x1100, &pes(0xC0, Some(1920 * k as i64), None, &adts, true)); + mux.pes(0x1200, &pes(0xE1, Some(3600 * k as i64), None, &[0, 0, 0, 1, 0x09, 0xF0, 0, 0, 1, 0x65, 0x88, 1, 2, 3], false)); + } + let data = mux.out; + let full = open(&data).unwrap(); + assert_eq!(full.streams.len(), 3); + assert_eq!(full.streams[1].units.len(), 6, "ADTS frames"); + assert!(full.streams[2].units.iter().all(|u| u.key), "IDR access units"); + for cut in (0..data.len()).step_by(37) { + if let Ok(f) = open(&data[..cut].to_vec()) { + for (s, st) in f.streams.iter().enumerate() { + for i in 0..st.units.len() { + let _ = f.read_unit(&data[..cut].to_vec(), s, i); + } + } + } + } + let mut x = 0x9E37_79B9u32; + for _ in 0..400 { + let mut m = data.clone(); + for _ in 0..8 { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + let i = x as usize % m.len(); + m[i] = (x >> 8) as u8; + } + if let Ok(f) = open(&m) { + for (s, st) in f.streams.iter().enumerate() { + for i in 0..st.units.len() { + let _ = f.read_unit(&m, s, i); + } + } + } + } +} + +/// Opens `data` and reads every unit; errors are fine, panics are not. +fn open_and_read_all(data: &[u8]) { + let data = data.to_vec(); + if let Ok(f) = open(&data) { + for (s, st) in f.streams.iter().enumerate() { + for i in 0..st.units.len() { + let _ = f.read_unit(&data, s, i); + } + } + } +} + +#[test] +fn program_stream_cut_inside_a_pack_header_never_panics() { + // MPEG-1 pack header cut after the 4-byte start code + 3 bytes (once panicked in `scan`). + open_and_read_all(&[0, 0, 1, 0xBA, 0x21, 0, 0]); + for n in 4..=12 { + let mpeg1 = [0u8, 0, 1, 0xBA, 0x21, 0, 1, 0, 1, 0x80, 0, 1]; + open_and_read_all(&mpeg1[..n]); + } +} + +#[test] +fn program_stream_truncation_at_every_length_never_panics() { + let frames: Vec> = (0..3).map(|k| mpeg2_frame(if k == 0 { 1 } else { 2 }, k as u16, false, 300)).collect(); + let es: Vec = frames.concat(); + for mpeg1 in [false, true] { + let mut out = Vec::new(); + for (k, chunk) in es.chunks(500).enumerate() { + if mpeg1 { + out.extend([0, 0, 1, 0xBA, 0x21, 0, 1, 0, 1, 0x80, 0, 1]); + } else { + out.extend([0, 0, 1, 0xBA, 0x44, 0, 4, 0, 4, 1, 1, 0x89, 0xC3, 0xF8]); + } + out.extend(pes(0xE0, Some(3600 * k as i64), None, chunk, true)); + } + out.extend([0, 0, 1, 0xB9]); + assert!(open(&out).is_ok(), "the untruncated stream opens (mpeg1: {mpeg1})"); + for cut in 0..=out.len() { + open_and_read_all(&out[..cut]); + } + } +} + +#[test] +fn timestamps_unwrap_across_the_33_bit_wrap() { + let mut u = Unwrap::default(); + let near = (1i64 << 33) - 3000; + assert_eq!(u.unwrap(near), near); + assert_eq!(u.unwrap(600), (1 << 33) + 600); + // a B picture timestamp from just before the wrap + assert_eq!(u.unwrap(near + 1000), near + 1000); + assert_eq!(u.unwrap(4200), (1 << 33) + 4200); +} + +/// An Annex B NAL unit: start code, header bytes and `n` bytes of payload (no zero runs). +fn nal(header: &[u8], body: &[u8]) -> Vec { + let mut v = vec![0, 0, 0, 1]; + v.extend(header); + v.extend(body); + v +} + +/// A slice of `n` filler bytes after the first byte of the slice header (0x88: first_mb 0). +fn slice(header: &[u8], first: u8, n: usize) -> Vec { + let mut body = vec![first]; + body.extend((0..n).map(|i| 0x40 | (i % 31) as u8)); + nal(header, &body) +} + +const AUD: [u8; 2] = [0x09, 0xF0]; + +fn h264_idr(n: usize) -> Vec { + [nal(&AUD, &[]), nal(&[0x67], &[0x42, 0x80, 0x1E]), nal(&[0x68], &[0xCE, 0x38, 0x80]), slice(&[0x65], 0x88, n)].concat() +} + +fn h264_p(n: usize) -> Vec { + [nal(&AUD, &[]), slice(&[0x41], 0x88, n)].concat() +} + +/// A non-reference B picture (nal_ref_idc 0). +fn h264_b(n: usize) -> Vec { + [nal(&AUD, &[]), slice(&[0x01], 0x88, n)].concat() +} + +/// A non-IDR I picture introduced by SEI with a buffering period message and a recovery point. +fn h264_recovery_i(n: usize) -> Vec { + let sei = nal(&[0x06], &[0x00, 0x02, 0xAA, 0xBB, 0x06, 0x01, 0x84, 0x80]); + [nal(&AUD, &[]), sei, slice(&[0x41], 0x88, n)].concat() +} + +fn h264_units(aus: &[(Option, Vec)]) -> (Vec, File) { + let mut mux = TsMux::new(false); + mux.psi(&[(0x1B, 0x1011)]); + for (pts, au) in aus { + mux.pes(0x1011, &pes(0xE0, *pts, None, au, false)); + } + let data = mux.out; + let f = open(&data).unwrap(); + (data, f) +} + +#[test] +fn h264_recovery_point_is_a_random_access_point() { + let aus = [(Some(90_000), h264_idr(300)), (Some(93_600), h264_p(300)), (Some(97_200), h264_recovery_i(300)), (Some(100_800), h264_p(300))]; + let (data, f) = h264_units(&aus); + let s = &f.streams[0]; + assert_eq!(s.codec, Codec::H264); + assert_eq!(s.units.iter().map(|u| u.key).collect::>(), [true, false, true, false]); + for (i, (pts, au)) in aus.iter().enumerate() { + assert_eq!(f.read_unit(&data, 0, i).unwrap(), *au, "unit {i}"); + assert_eq!(s.units[i].pts, *pts); + } + // a stream cut so that it starts at a recovery point (no IDR at all) has a key unit + let (_, f) = h264_units(&[(Some(1000), h264_recovery_i(400)), (Some(4600), h264_p(400)), (Some(8200), h264_p(400))]); + assert_eq!(f.streams[0].units.iter().map(|u| u.key).collect::>(), [true, false, false]); + // SEI without a recovery point message is not one + let sei = nal(&[0x06], &[0x00, 0x02, 0xAA, 0xBB, 0x80]); + let au = [nal(&AUD, &[]), sei, slice(&[0x41], 0x88, 600)].concat(); + let (_, f) = h264_units(&[(Some(1000), au)]); + assert!(!f.streams[0].units[0].key); +} + +#[test] +fn h264_intra_refresh_recovery_point_is_not_a_random_access_point() { + // recovery_frame_cnt 1 (ue(v) "010"): decoding from here shows broken pictures + let sei = nal(&[0x06], &[0x00, 0x02, 0xAA, 0xBB, 0x06, 0x01, 0x44, 0x80]); + let au = [nal(&AUD, &[]), sei, slice(&[0x41], 0x88, 300)].concat(); + let (_, f) = h264_units(&[(Some(1000), au), (Some(4600), h264_p(300))]); + assert_eq!(f.streams[0].units.iter().map(|u| u.key).collect::>(), [false, false]); +} + +#[test] +fn h264_several_access_units_in_one_pes_are_separate_units() { + // IDR, then in one PES a non-reference B picture and a reference P picture, only the first + // access unit of the PES has a timestamp + let (idr, b, p) = (h264_idr(400), h264_b(300), h264_p(250)); + let (data, f) = h264_units(&[(Some(90_000), idr.clone()), (Some(93_600), [b.clone(), p.clone()].concat()), (Some(100_800), h264_p(100))]); + let s = &f.streams[0]; + assert_eq!(s.units.len(), 4); + assert_eq!(s.units.iter().map(|u| u.pts).collect::>(), [Some(90_000), Some(93_600), None, Some(100_800)]); + assert_eq!(s.units.iter().map(|u| u.key).collect::>(), [true, false, false, false]); + assert_eq!(s.units.iter().map(|u| u.disposable).collect::>(), [false, true, false, false]); + assert_eq!(f.read_unit(&data, 0, 0).unwrap(), idr); + assert_eq!(f.read_unit(&data, 0, 1).unwrap(), b); + assert_eq!(f.read_unit(&data, 0, 2).unwrap(), p); +} + +#[test] +fn h264_units_without_delimiters_split_at_the_first_slice() { + // no AUD or SEI: a slice with first_mb_in_slice 0 starts the next access unit, a further + // slice of the same picture (first_mb != 0) does not + let two_slices = [slice(&[0x41], 0x88, 300), slice(&[0x41], 0x42, 300)].concat(); + let idr = slice(&[0x65], 0x88, 200); + let (data, f) = h264_units(&[(Some(0), [idr.clone(), two_slices.clone(), two_slices.clone()].concat())]); + let s = &f.streams[0]; + assert_eq!(s.units.len(), 3); + assert_eq!(s.units.iter().map(|u| u.key).collect::>(), [true, false, false]); + assert_eq!(f.read_unit(&data, 0, 1).unwrap(), two_slices); + assert_eq!(f.read_unit(&data, 0, 0).unwrap(), idr); +} + +#[test] +fn h264_one_access_unit_per_pes_is_unchanged() { + // parameter sets inside the access unit (after the AUD, before the slices) do not split it + let aus: Vec<(Option, Vec)> = + (0..6).map(|k| (Some(90_000 + 3600 * k), if k % 3 == 0 { h264_idr(500) } else { h264_p(150 + 70 * k as usize) })).collect(); + let (data, f) = h264_units(&aus); + let s = &f.streams[0]; + assert_eq!(s.units.len(), 6); + for (i, (pts, au)) in aus.iter().enumerate() { + assert_eq!(f.read_unit(&data, 0, i).unwrap(), *au); + assert_eq!(s.units[i].pts, *pts); + assert_eq!(s.units[i].key, i % 3 == 0); + assert!(!s.units[i].disposable); + } +} + +#[test] +fn hevc_several_access_units_in_one_pes_are_separate_units() { + let aud = nal(&[0x46, 0x01], &[0x50]); + let idr = [aud.clone(), slice(&[0x26, 0x01], 0x80, 300)].concat(); + // TRAIL_N picture: sub-layer non-reference + let trail_n = [aud.clone(), slice(&[0x00, 0x01], 0x80, 300)].concat(); + let trail_r = [aud, slice(&[0x02, 0x01], 0x80, 200)].concat(); + let mut mux = TsMux::new(false); + mux.psi(&[(0x24, 0x1011)]); + mux.pes(0x1011, &pes(0xE0, Some(90_000), None, &idr, false)); + mux.pes(0x1011, &pes(0xE0, Some(93_600), None, &[trail_n.clone(), trail_r.clone()].concat(), false)); + let f = open(&mux.out).unwrap(); + let s = &f.streams[0]; + assert_eq!(s.codec, Codec::Hevc); + assert_eq!(s.units.iter().map(|u| u.pts).collect::>(), [Some(90_000), Some(93_600), None]); + assert_eq!(s.units.iter().map(|u| u.key).collect::>(), [true, false, false]); + assert_eq!(s.units.iter().map(|u| u.disposable).collect::>(), [false, true, false]); + assert_eq!(f.read_unit(&mux.out, 0, 1).unwrap(), trail_n); + assert_eq!(f.read_unit(&mux.out, 0, 2).unwrap(), trail_r); +} + +fn adts_frame(k: u8) -> Vec { + let mut v = vec![0xFF, 0xF1, 0x50, 0x80, 0x0C, 0x9F, 0xFC]; + v.extend([k; 93]); + v +} + +#[test] +fn non_pes_payload_is_dropped_until_the_next_unit_start() { + // a unit start whose payload is not a PES header, then continuation packets that happen to + // hold ADTS frames: nothing of it may become a unit + let mut mux = TsMux::new(false); + mux.psi(&[(0x0F, 0x1100)]); + mux.packet(0x1100, true, &[0x55; 184]); + for k in 0..10 { + mux.packet(0x1100, false, &adts_frame(k)); + } + let f = open(&mux.out).unwrap(); + assert_eq!(f.streams.first().map(|s| s.units.len()), Some(0)); + // a real PES packet afterwards is indexed again + let mut mux2 = TsMux::new(false); + mux2.psi(&[(0x0F, 0x1100)]); + mux2.packet(0x1100, true, &[0x55; 184]); + mux2.packet(0x1100, false, &adts_frame(1)); + mux2.pes(0x1100, &pes(0xC0, Some(9000), None, &adts_frame(2), true)); + let f = open(&mux2.out).unwrap(); + let u = &f.streams[0].units; + assert_eq!(u.len(), 1); + assert_eq!(u[0].pts, Some(9000)); +} + +#[test] +fn adaptation_field_only_unit_start_keeps_the_pes_in_progress() { + let mut mux = TsMux::new(false); + mux.psi(&[(0x03, 0x1100)]); + let data = pes(0xC0, Some(9000), None, &[mp2_frame(0), mp2_frame(1)].concat(), true); + mux.packet(0x1100, true, &data[..184]); + // PUSI set, adaptation field only (no payload) + let cc = mux.cc.get(&0x1100).copied().unwrap_or(0); + let mut af = vec![0x47, 0x40 | 0x11, 0x00, 0x20 | cc, 183, 0x00]; + af.resize(188, 0xFF); + mux.out.extend(af); + for chunk in data[184..].chunks(184) { + mux.packet(0x1100, false, chunk); + } + let f = open(&mux.out).unwrap(); + let s = &f.streams[0]; + assert_eq!(s.units.len(), 2); + assert_eq!(s.units[0].pts, Some(9000)); + assert_eq!(f.read_unit(&mux.out, 0, 1).unwrap(), mp2_frame(1)); +} + +#[test] +fn oversized_unit_is_refused_without_reserving_it() { + let f = File { + format: Format::Ts { packet_size: 188, sync_offset: 0 }, + streams: vec![Stream { + id: StreamId::Pid(0x100), + stream_type: 0x1B, + codec: Codec::H264, + language: None, + units: vec![Unit { pts: None, dts: None, key: true, disposable: false, size: u32::MAX, pos: 0, offset: 0, picture: None }], + lpcm: None, + pes_packets: 0, + skipped_bytes: 0, + }], + program: None, + pcr_range: None, + warnings: Vec::new(), + }; + assert!(matches!(f.read_unit(&Vec::::new(), 0, 0), Err(Error::Invalid(_)))); +} + +/// A `ByteSource` that counts the bytes requested through `read_at`. +struct Counting<'a> { + data: &'a [u8], + read: std::cell::Cell, +} + +impl ByteSource for Counting<'_> { + fn len(&self) -> u64 { + self.data.len() as u64 + } + fn read_at(&self, offset: u64, buf: &mut [u8]) -> std::io::Result<()> { + self.read.set(self.read.get() + buf.len() as u64); + self.data.read_at(offset, buf) + } +} + +#[test] +fn bad_adaptation_fields_do_not_amplify_reads() { + // aligned packets whose adaptation_field_length (255) overruns the packet: the packet grid is + // intact, so this must cost one pass over the file, not one block read per packet + let mut data = Vec::new(); + for _ in 0..3000 { + data.extend([0x47, 0x11, 0x00, 0x30]); + data.extend(std::iter::repeat_n(0xFF, 184)); + } + let src = Counting { data: &data, read: Default::default() }; + let f = open(&src).unwrap(); + assert!(f.warnings.iter().any(|w| w.contains("adaptation field")), "{:?}", f.warnings); + assert!(!f.warnings.iter().any(|w| w.contains("lost packet sync")), "{:?}", f.warnings); + let read = src.read.get(); + assert!(read <= 4 * data.len() as u64, "read {read} bytes of a {}-byte file", data.len()); +} + +#[test] +fn garbage_without_sync_does_not_amplify_reads() { + // a sync byte every 189 bytes: five-in-a-row never lines up, every 0x47 is a false candidate + let mut data = Vec::new(); + for _ in 0..3000 { + data.extend([0x47]); + data.extend(std::iter::repeat_n(0x11, 188)); + } + let head: Vec = (0..188 * 8).map(|i| if i % 188 == 0 { 0x47 } else { 0 }).collect(); + let mut all = head; + all.extend(data); + let src = Counting { data: &all, read: Default::default() }; + let f = open(&src).unwrap(); + assert!(f.warnings.iter().any(|w| w.contains("lost packet sync")), "{:?}", f.warnings); + let read = src.read.get(); + assert!(read <= 4 * all.len() as u64, "read {read} bytes of a {}-byte file", all.len()); +} + +#[test] +fn transport_stream_resyncs_after_garbage() { + const BLOCK_PACKETS: usize = 4096; + for m2ts in [false, true] { + let ps = if m2ts { 192 } else { 188 }; + // garbage in front of the stream (shorter than 8 packets) and, at a few places around the + // first block boundary, between two frames; lengths that are not a multiple of the packet + for (delta, glen) in [(0usize, 100usize), (1, 37), (2, 188), (3, 301), (5, 5), (8, 1000)] { + let edge = (BLOCK_PACKETS - delta) * ps; + let mut mux = TsMux::new(m2ts); + mux.out.extend([0x11, 0x47, 0x22]); // garbage prefix with a lone 0x47 + mux.psi(&[(0x02, 0x1011)]); + let mut expect = Vec::new(); + let mut placed = false; + let mut k = 0usize; + while k < 3000 || !placed { + if !placed && mux.out.len() >= edge { + // garbage between two PES packets, with lone sync bytes inside + let mut g: Vec = (0..glen).map(|i| 0x10 + (i % 7) as u8).collect(); + if let Some(b) = g.get_mut(3) { + *b = 0x47; + } + mux.out.extend(g); + placed = true; + } + let f = mpeg2_frame(if k == 0 { 1 } else { 2 }, (k % 1000) as u16, false, 40 + k % 300); + mux.pes(0x1011, &pes(0xE0, Some(90_000 + 3600 * k as i64), None, &f, false)); + expect.push(f); + k += 1; + } + assert!(mux.out.len() > (BLOCK_PACKETS + 4) * ps, "stream must cross a block boundary"); + let data = mux.out; + let f = open(&data).unwrap(); + assert!(f.warnings.iter().any(|w| w.contains("lost packet sync")), "{:?}", f.warnings); + let v = f.find(Kind::Video, |_| true).unwrap(); + let s = &f.streams[v]; + assert_eq!(s.units.len(), expect.len(), "m2ts {m2ts} delta {delta} glen {glen}"); + for (i, fr) in expect.iter().enumerate() { + assert_eq!(&f.read_unit(&data, v, i).unwrap(), fr, "unit {i} (m2ts {m2ts} delta {delta} glen {glen})"); + assert_eq!(s.units[i].pts, Some(90_000 + 3600 * i as i64)); + } + } + } +} diff --git a/crates/media/src/mpegts/ts.rs b/crates/media/src/mpegts/ts.rs new file mode 100644 index 000000000..52405e14d --- /dev/null +++ b/crates/media/src/mpegts/ts.rs @@ -0,0 +1,559 @@ +//! Transport streams (H.222.0 §2.4.3): packets, PSI (PAT/PMT), PES reassembly. + +use std::collections::HashMap; + +use super::es::Splitter; +use super::pes::{self, PesHeader}; +use super::{ByteSource, Codec, Error, File, Format, Result, Stream, StreamId, Unit, Unwrap, read_some}; + +/// Byte-wise table for the MSB-first CRC-32 (polynomial 0x04C11DB7): entry `i` is the CRC register +/// after shifting the byte `i` through eight bit steps. +const CRC_TABLE: [u32; 256] = { + let mut t = [0u32; 256]; + let mut i = 0; + while i < 256 { + let mut crc = (i as u32) << 24; + let mut bit = 0; + while bit < 8 { + crc = if crc & 0x8000_0000 != 0 { (crc << 1) ^ 0x04C1_1DB7 } else { crc << 1 }; + bit += 1; + } + t[i] = crc; + i += 1; + } + t +}; + +/// CRC-32 of PSI sections (MSB-first, polynomial 0x04C11DB7, initial value all ones; Annex A). +pub(crate) fn crc32_mpeg(data: &[u8]) -> u32 { + let mut crc = 0xFFFF_FFFFu32; + for &b in data { + crc = (crc << 8) ^ CRC_TABLE[((crc >> 24) as u8 ^ b) as usize]; + } + crc +} + +/// A packet's fields. +struct Packet<'a> { + pusi: bool, + pid: u16, + cc: u8, + has_payload: bool, + discontinuity: bool, + pcr: Option, + payload: &'a [u8], +} + +/// Why a packet could not be parsed. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Bad { + /// No sync byte (or too short): the packet grid is lost. + Sync, + /// Sync byte present but the adaptation field overruns the packet: the grid is intact, the + /// packet carries nothing usable. + Adaptation, +} + +fn packet(p: &[u8]) -> std::result::Result, Bad> { + if p.len() < 188 || p[0] != 0x47 { + return Err(Bad::Sync); + } + let pusi = p[1] & 0x40 != 0; + let pid = (((p[1] & 0x1F) as u16) << 8) | p[2] as u16; + let afc = (p[3] >> 4) & 3; + let cc = p[3] & 15; + let mut start = 4; + let mut pcr = None; + let mut discontinuity = false; + if afc & 2 != 0 { + let al = p[4] as usize; + start = 5 + al; + if start > 188 { + return Err(Bad::Adaptation); + } + if al > 0 { + let flags = p[5]; + discontinuity = flags & 0x80 != 0; + if flags & 0x10 != 0 && al >= 7 { + let base = ((p[6] as i64) << 25) | ((p[7] as i64) << 17) | ((p[8] as i64) << 9) | ((p[9] as i64) << 1) | (p[10] as i64 >> 7); + let ext = (((p[10] & 1) as i64) << 8) | p[11] as i64; + pcr = Some(base * 300 + ext); + } + } + } + let has_payload = afc & 1 != 0; + Ok(Packet { pusi, pid, cc, has_payload, discontinuity, pcr, payload: if has_payload { &p[start..188] } else { &[] } }) +} + +/// Reassembles a PES header that may span packets; yields each packet's elementary-stream bytes. +#[derive(Default)] +pub(crate) struct PesAsm { + hdr: Vec, + waiting: bool, + /// The packet that started the current unit was not a PES packet: its continuation packets + /// are dropped until the next unit start. + dropping: bool, +} + +impl PesAsm { + /// The PES header completed by this packet (if any) and the packet's ES payload bytes. + pub fn packet<'a>(&mut self, pusi: bool, payload: &'a [u8]) -> (Option, &'a [u8]) { + if pusi { + if payload.is_empty() { + // an adaptation-field-only packet carries no PES data: it must not cut short + // the PES packet in progress + return (None, &[]); + } + self.hdr.clear(); + self.waiting = true; + self.dropping = false; + } else if self.dropping { + return (None, &[]); + } else if !self.waiting { + return (None, payload); + } + let before = self.hdr.len(); + self.hdr.extend_from_slice(payload); + // `pes::parse` only succeeds once the whole header (`header_len` bytes) is buffered + if let Some(h) = pes::parse(&self.hdr) { + self.waiting = false; + let skip = h.header_len.saturating_sub(before).min(payload.len()); + return (Some(h), &payload[skip..]); + } + if self.hdr.len() > 1024 || (self.hdr.len() >= 6 && !(self.hdr[0] == 0 && self.hdr[1] == 0 && self.hdr[2] == 1)) { + // not a PES packet after all: drop until the next unit start + self.waiting = false; + self.dropping = true; + self.hdr.clear(); + return (None, &[]); + } + (None, &[]) + } +} + +/// PSI section reassembly for one PID. +#[derive(Default)] +struct Psi { + buf: Vec, +} + +impl Psi { + /// Feed a packet payload; returns completed sections. + fn feed(&mut self, pusi: bool, payload: &[u8]) -> Vec> { + let mut out = Vec::new(); + if pusi { + let Some(&ptr) = payload.first() else { return out }; + let ptr = ptr as usize; + if !self.buf.is_empty() { + self.buf.extend_from_slice(payload.get(1..1 + ptr).unwrap_or(&[])); + self.drain(&mut out); + } + self.buf = payload.get(1 + ptr..).unwrap_or(&[]).to_vec(); + } else if !self.buf.is_empty() { + self.buf.extend_from_slice(payload); + } + self.drain(&mut out); + out + } + + fn drain(&mut self, out: &mut Vec>) { + while self.buf.len() >= 3 { + if self.buf[0] == 0xFF { + self.buf.clear(); + return; + } + let total = 3 + ((((self.buf[1] & 0x0F) as usize) << 8) | self.buf[2] as usize); + if self.buf.len() < total { + if total > 4096 { + self.buf.clear(); + } + return; + } + out.push(self.buf[..total].to_vec()); + self.buf.drain(..total); + } + } +} + +/// Elementary stream of the selected program. +struct EsState { + stream: Stream, + split: Splitter, + asm: PesAsm, + cc: Option, +} + +/// Map a PMT stream_type (and the ES descriptors) to a codec. `hdmv`: a Blu-ray / AVCHD stream +/// (registration descriptor "HDMV", or 192-byte packets). +pub(crate) fn codec_of(stream_type: u8, desc: &[u8], hdmv: bool) -> Codec { + let mut tags = Vec::new(); + let mut reg = None; + let mut i = 0; + while i + 2 <= desc.len() { + let (tag, len) = (desc[i], desc[i + 1] as usize); + let body = desc.get(i + 2..i + 2 + len).unwrap_or(&[]); + tags.push(tag); + if tag == 5 && body.len() >= 4 { + reg = Some([body[0], body[1], body[2], body[3]]); + } + i += 2 + len; + } + match stream_type { + 0x01 => Codec::Mpeg1Video, + 0x02 => Codec::Mpeg2Video, + 0x03 | 0x04 => Codec::MpegAudio, + 0x0F => Codec::AacAdts, + 0x11 => Codec::AacLatm, + 0x1B => Codec::H264, + 0x24 => Codec::Hevc, + 0x80 if hdmv => Codec::LpcmBluray, + 0x80 => Codec::Mpeg2Video, + 0x81 => Codec::Ac3, + 0x82 | 0x85 | 0x86 | 0xA2 if hdmv => Codec::Dts, + 0x83 if hdmv => Codec::TrueHd, + 0x84 | 0xA1 if hdmv => Codec::Eac3, + 0x87 => Codec::Eac3, + 0x90 if hdmv => Codec::Subtitle("PGS subtitles"), + 0x92 if hdmv => Codec::Subtitle("Text subtitles"), + 0x06 => { + if tags.contains(&0x6A) || reg == Some(*b"AC-3") { + Codec::Ac3 + } else if tags.contains(&0x7A) || reg == Some(*b"EAC3") { + Codec::Eac3 + } else if tags.contains(&0x7B) || matches!(reg, Some([b'D', b'T', b'S', _])) { + Codec::Dts + } else if tags.contains(&0x59) { + Codec::Subtitle("DVB subtitles") + } else if tags.contains(&0x56) { + Codec::Subtitle("Teletext") + } else { + Codec::Unknown(6) + } + } + t => Codec::Unknown(t), + } +} + +fn language(desc: &[u8]) -> Option { + let mut i = 0; + while i + 2 <= desc.len() { + let (tag, len) = (desc[i], desc[i + 1] as usize); + if tag == 0x0A && len >= 3 { + let b = desc.get(i + 2..i + 5)?; + return Some(String::from_utf8_lossy(b).trim().to_string()).filter(|s| !s.is_empty()); + } + i += 2 + len; + } + None +} + +/// First file offset at which `packet_size`-byte packets line up (sync byte at `sync_offset`). +fn first_sync(head: &[u8], packet_size: usize, sync_offset: usize) -> Option { + (0..packet_size.min(head.len())).find(|&s| (0..5).all(|k| head.get(s + sync_offset + k * packet_size) == Some(&0x47))) +} + +/// Find where packets line up again, searching `block` from byte `from`: the first packet start +/// `i` whose sync byte and the next four packets' sync bytes are all 0x47. Returns `(i, false)` +/// when found. Returns `(i, true)` when the block ran out first: `i` is where to refill from (a +/// candidate whose five-packet window does not fit yet, or the last place a whole packet fits). +/// At the end of the file (`at_end`) a candidate is accepted with as many sync bytes as exist. +fn resync(block: &[u8], from: usize, packet_size: usize, sync_offset: usize, at_end: bool) -> (usize, bool) { + let mut i = from; + while i + packet_size <= block.len() { + if block.get(i + sync_offset) == Some(&0x47) { + let mut aligned = true; + let mut complete = true; + for k in 1..5 { + match block.get(i + sync_offset + k * packet_size) { + Some(&0x47) => {} + Some(_) => { + aligned = false; + break; + } + None => { + complete = false; + break; + } + } + } + if aligned { + return (i, !complete && !at_end); + } + } + i += 1; + } + (i, true) +} + +/// PCR values unwrapped across the 33-bit base wrap, with the range seen. +#[derive(Default)] +struct PcrTrack { + unwrap: i64, + last: Option, + range: Option<(i64, i64)>, +} + +impl PcrTrack { + fn update(&mut self, pcr: i64) { + // 33-bit base × 300 wraps at 2^33·300 + let wrap = (1i64 << 33) * 300; + let mut v = pcr + self.unwrap; + if let Some(l) = self.last + && v < l - wrap / 2 + { + self.unwrap += wrap; + v += wrap; + } + self.last = Some(v); + self.range = Some(self.range.map_or((v, v), |(a, b)| (a.min(v), b.max(v)))); + } +} + +/// Register the elementary streams a (CRC-checked) PMT section announces and that are not yet known. +fn add_pmt_streams(sec: &[u8], packet_size: usize, es: &mut HashMap, order: &mut Vec) { + let pinfo = (((sec[10] & 0x0F) as usize) << 8) | sec[11] as usize; + let prog_desc = sec.get(12..12 + pinfo).unwrap_or(&[]); + let hdmv = packet_size == 192 || prog_desc.windows(4).any(|w| w == b"HDMV"); + let mut i = 12 + pinfo; + let end = sec.len() - 4; + while i + 5 <= end { + let st = sec[i]; + let pid = (((sec[i + 1] & 0x1F) as u16) << 8) | sec[i + 2] as u16; + let ilen = (((sec[i + 3] & 0x0F) as usize) << 8) | sec[i + 4] as usize; + let desc = sec.get(i + 5..(i + 5 + ilen).min(end)).unwrap_or(&[]); + i += 5 + ilen; + if es.contains_key(&pid) { + continue; + } + let mut codec = codec_of(st, desc, hdmv); + if codec == Codec::Unknown(6) && prog_desc.windows(4).any(|w| w == b"AC-3") { + codec = Codec::Ac3; + } + let stream = Stream { + id: StreamId::Pid(pid), + stream_type: st, + codec: codec.clone(), + language: language(desc), + units: Vec::new(), + lpcm: None, + pes_packets: 0, + skipped_bytes: 0, + }; + es.insert(pid, EsState { stream, split: Splitter::new(&codec), asm: PesAsm::default(), cc: None }); + order.push(pid); + } +} + +pub(crate) fn scan(src: &S, packet_size: usize, sync_offset: usize) -> Result { + let len = src.len(); + let mut head = vec![0u8; packet_size * 8]; + let n = read_some(src, 0, &mut head)?; + let mut off = first_sync(&head[..n], packet_size, sync_offset).ok_or(Error::NotMpeg)? as u64; + let mut warnings: Vec = Vec::new(); + let mut pat = Psi::default(); + let mut pmt_pid: Option = None; + let mut program = None; + let mut pmt = Psi::default(); + let mut es: HashMap = HashMap::new(); + let mut order: Vec = Vec::new(); + let mut pmt_version: Option = None; + let mut unwrap = Unwrap::default(); + let mut pcr_track = PcrTrack::default(); + let (mut lost_sync, mut cc_errors, mut crc_errors) = (0u64, 0u64, 0u64); + const BLOCK: usize = 4096; + let mut block = vec![0u8; BLOCK * packet_size]; + let mut bad_adaptation = 0u64; + while off + packet_size as u64 <= len { + let got = read_some(src, off, &mut block)?; + if got < packet_size { + break; + } + let at_end = off + got as u64 >= len; + // `p`: byte index of the next packet in `block`. Packets are parsed in place; a lost + // sync is repaired inside the loaded block, and the block is refilled from the file + // (at `off + p`) only when it is used up, so the bytes read stay close to the file size. + let mut p = 0usize; + while p + packet_size <= got { + let pos = off + p as u64; + let raw = &block[p..p + packet_size]; + let pk = match packet(&raw[sync_offset..]) { + Ok(pk) => pk, + Err(Bad::Adaptation) => { + // the packet grid is intact: this packet just has no usable payload + bad_adaptation += 1; + p += packet_size; + continue; + } + Err(Bad::Sync) => { + lost_sync += 1; + let (next, exhausted) = resync(&block[..got], p + 1, packet_size, sync_offset, at_end); + p = next; + if exhausted { + break; + } + continue; + } + }; + p += packet_size; + if let Some(pcr) = pk.pcr { + pcr_track.update(pcr); + } + if pk.pid == 0 { + for sec in pat.feed(pk.pusi, pk.payload) { + if sec.len() < 12 || sec[0] != 0 { + continue; + } + if crc32_mpeg(&sec) != 0 { + crc_errors += 1; + continue; + } + let body = &sec[8..sec.len() - 4]; + for e in body.as_chunks::<4>().0 { + let num = u16::from_be_bytes([e[0], e[1]]); + let pid = (((e[2] & 0x1F) as u16) << 8) | e[3] as u16; + if num != 0 && pmt_pid.is_none() { + pmt_pid = Some(pid); + program = Some(num); + } + } + } + continue; + } + if Some(pk.pid) == pmt_pid { + for sec in pmt.feed(pk.pusi, pk.payload) { + if sec.len() < 16 || sec[0] != 2 { + continue; + } + if crc32_mpeg(&sec) != 0 { + crc_errors += 1; + continue; + } + let version = (sec[5] >> 1) & 0x1F; + if pmt_version == Some(version) { + continue; + } + pmt_version = Some(version); + add_pmt_streams(&sec, packet_size, &mut es, &mut order); + } + continue; + } + let Some(e) = es.get_mut(&pk.pid) else { continue }; + if pk.has_payload { + if let Some(prev) = e.cc + && !pk.discontinuity + && pk.cc != (prev + 1) & 15 + && pk.cc != prev + { + cc_errors += 1; + } + e.cc = Some(pk.cc); + } + let (hdr, bytes) = e.asm.packet(pk.pusi, pk.payload); + if let Some(h) = hdr { + e.stream.pes_packets += 1; + let pts = h.pts.map(|t| unwrap.unwrap(t)); + let dts = h.dts.map(|t| unwrap.unwrap(t)); + e.split.pes_start(pts, dts); + } + e.split.feed(pos, bytes); + } + // `p >= 1` here (the loop ran at least once or `resync` moved past the failed packet) + off += p.max(1) as u64; + } + if lost_sync > 0 { + warnings.push(format!("lost packet sync {lost_sync} time(s)")); + } + if bad_adaptation > 0 { + warnings.push(format!("{bad_adaptation} packet(s) with an invalid adaptation field")); + } + if cc_errors > 0 { + warnings.push(format!("{cc_errors} continuity counter error(s)")); + } + if crc_errors > 0 { + warnings.push(format!("{crc_errors} PSI section(s) with bad CRC")); + } + if pmt_pid.is_none() { + warnings.push("no program association table".into()); + } + let mut streams = Vec::new(); + for pid in order { + let Some(mut e) = es.remove(&pid) else { continue }; + e.split.finish(); + e.stream.units = std::mem::take(&mut e.split.units); + e.stream.skipped_bytes = e.split.skipped; + if e.split.truncated { + warnings.push(format!("PID {pid}: more than {} access units, the rest are not indexed", super::es::MAX_UNITS)); + } + streams.push(e.stream); + } + Ok(File { format: Format::Ts { packet_size, sync_offset }, streams, program, pcr_range: pcr_track.range, warnings }) +} + +/// Collect unit `u` of stream `st`: from the packet at `u.pos`, the PID's elementary-stream bytes. +pub(crate) fn read(src: &S, packet_size: usize, sync_offset: usize, st: &Stream, u: &Unit, out: &mut Vec) -> Result<()> { + let StreamId::Pid(pid) = st.id else { return Err(Error::Invalid("not a TS stream".into())) }; + let need = u.size as usize; + let mut skip = u.offset as usize; + let mut off = u.pos; + let mut asm = PesAsm::default(); + let mut first = true; + let mut block = vec![0u8; 512 * packet_size]; + while out.len() < need { + let got = read_some(src, off, &mut block)?; + let whole = got / packet_size; + if whole == 0 { + break; + } + for k in 0..whole { + let raw = &block[k * packet_size..(k + 1) * packet_size]; + let pk = match packet(&raw[sync_offset..]) { + Ok(pk) => pk, + Err(Bad::Sync) => return Ok(()), + Err(Bad::Adaptation) => continue, + }; + if pk.pid != pid { + continue; + } + // a unit starting in a PES packet's first packet: the header is skipped + let (_, bytes) = if first && !pk.pusi { (None, pk.payload) } else { asm.packet(pk.pusi, pk.payload) }; + first = false; + let bytes = if skip > 0 { + let s = skip.min(bytes.len()); + skip -= s; + &bytes[s..] + } else { + bytes + }; + let take = bytes.len().min(need - out.len()); + out.extend_from_slice(&bytes[..take]); + if out.len() >= need { + return Ok(()); + } + } + off += (whole * packet_size) as u64; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A unit start that never completes a PES header (MPEG-1 syntax, stuffing without end) is + /// given up after 1024 bytes: the bytes are not kept, and the next unit start is read again. + #[test] + fn a_pes_header_that_never_ends_is_not_buffered_past_the_limit() { + let mut asm = PesAsm::default(); + let mut first = vec![0, 0, 1, 0xC0, 0, 0]; + first.resize(184, 0xFF); + assert_eq!(asm.packet(true, &first), (None, &[][..])); + let stuffing = [0xFFu8; 184]; + for k in 0..200 { + assert_eq!(asm.packet(false, &stuffing), (None, &[][..]), "packet {k}"); + assert!(asm.hdr.len() <= 1024 + 184, "{} header bytes buffered", asm.hdr.len()); + } + assert!(asm.dropping && asm.hdr.is_empty()); + let (h, es) = asm.packet(true, &[0, 0, 1, 0xC0, 0, 0, 0x80, 0, 0, 0xAA, 0xBB]); + assert_eq!((h.map(|h| h.header_len), es), (Some(9), &[0xAA, 0xBB][..])); + } +} diff --git a/crates/media/src/mpegts/work_tests.rs b/crates/media/src/mpegts/work_tests.rs new file mode 100644 index 000000000..f34aecb9f --- /dev/null +++ b/crates/media/src/mpegts/work_tests.rs @@ -0,0 +1,115 @@ +//! Equality and counting tests for the redundant-work removals: the old implementation is kept +//! here as the reference, the new one must give the same results. + +use super::*; + +/// The bitwise CRC-32 that `ts::crc32_mpeg` replaced. +fn crc32_bitwise(data: &[u8]) -> u32 { + let mut crc = 0xFFFF_FFFFu32; + for &b in data { + crc ^= (b as u32) << 24; + for _ in 0..8 { + crc = if crc & 0x8000_0000 != 0 { (crc << 1) ^ 0x04C1_1DB7 } else { crc << 1 }; + } + } + crc +} + +/// Deterministic xorshift bytes. +fn noise(seed: u32, n: usize) -> Vec { + let mut x = seed | 1; + (0..n) + .map(|_| { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + (x >> 8) as u8 + }) + .collect() +} + +#[test] +fn table_crc_equals_the_bitwise_crc() { + // the standard check value of CRC-32/MPEG-2 + assert_eq!(ts::crc32_mpeg(b"123456789"), 0x0376_E6E7); + assert_eq!(ts::crc32_mpeg(&[]), 0xFFFF_FFFF); + for b in 0..=255u8 { + assert_eq!(ts::crc32_mpeg(&[b]), crc32_bitwise(&[b])); + } + for n in [0usize, 1, 2, 3, 4, 5, 15, 16, 17, 183, 184, 1023, 1024, 4096] { + for seed in 1..6u32 { + let d = noise(seed * 7919 + n as u32, n); + assert_eq!(ts::crc32_mpeg(&d), crc32_bitwise(&d), "len {n} seed {seed}"); + } + } + // a section followed by its own CRC checks to zero + let mut sec = noise(5, 40); + let crc = ts::crc32_mpeg(&sec); + sec.extend(crc.to_be_bytes()); + assert_eq!(ts::crc32_mpeg(&sec), 0); +} + +/// MPEG-1 layer II frame, 48 kHz 192 kb/s (576 bytes), distinct per `k` and `salt`. +fn mp2_frame(k: usize, salt: u8) -> Vec { + let mut v = vec![0xFF, 0xFD, 0xA4, 0x00]; + v.extend((0..572).map(|i| (i as u8) ^ (k as u8) ^ salt)); + v +} + +fn pes_packet(stream_id: u8, payload: &[u8]) -> Vec { + let len = 3 + payload.len(); + let mut v = vec![0, 0, 1, stream_id, (len >> 8) as u8, len as u8, 0x80, 0, 0]; + v.extend_from_slice(payload); + v +} + +/// Two audio streams interleaved in maximal PES packets over more than three window refills, +/// with a pack header per PES packet; MPEG-2 or MPEG-1 pack headers. +fn big_program_stream(mpeg1: bool, frames: usize) -> (Vec, Vec>, Vec>) { + let a: Vec> = (0..frames).map(|k| mp2_frame(k, 0)).collect(); + let b: Vec> = (0..frames).map(|k| mp2_frame(k, 0x55)).collect(); + let (ea, eb) = (a.concat(), b.concat()); + let mut out = Vec::new(); + for (ca, cb) in ea.chunks(60_000).zip(eb.chunks(60_000)) { + for (id, chunk) in [(0xC0, ca), (0xC1, cb)] { + if mpeg1 { + out.extend([0, 0, 1, 0xBA, 0x21, 0, 1, 0, 1, 0x80, 0, 1]); + } else { + out.extend([0, 0, 1, 0xBA, 0x44, 0, 4, 0, 4, 1, 1, 0x89, 0xC3, 0xF8]); + } + out.extend(pes_packet(id, chunk)); + } + } + out.extend([0, 0, 1, 0xB9]); + (out, a, b) +} + +#[test] +fn program_stream_with_large_pes_packets_gives_back_every_frame() { + for mpeg1 in [false, true] { + let (data, a, b) = big_program_stream(mpeg1, 2000); // about 1.2 MB per stream + assert!(data.len() > 2 << 20, "more than the 1 MiB window twice"); + let f = open(&data).unwrap(); + assert_eq!(f.format, Format::Ps { mpeg1 }); + assert_eq!(f.streams.len(), 2); + for (s, frames) in f.streams.iter().zip([&a, &b]) { + assert_eq!(s.units.len(), frames.len()); + // a read refills a 1 MiB window: every seventh unit and the last ones keep a debug build quick + for (i, fr) in frames.iter().enumerate().filter(|(i, _)| i % 7 == 0 || i + 4 > frames.len()) { + assert_eq!(&f.read_unit(&data, f.streams.iter().position(|x| std::ptr::eq(x, s)).unwrap(), i).unwrap(), fr, "mpeg1 {mpeg1} unit {i}"); + } + } + // cut inside the last packets: never panics, and the units before the cut are intact + for cut in [data.len() - 1, data.len() - 4, data.len() - 30_000, data.len() - 61_000, 100_000, 1 << 20] { + let d = &data[..cut]; + let Ok(f) = open(d) else { continue }; + for (si, frames) in [&a, &b].into_iter().enumerate() { + let Some(s) = f.streams.get(si) else { continue }; + let n = s.units.len().saturating_sub(1); + for (i, frame) in frames.iter().enumerate().take(n).filter(|(i, _)| i % 61 == 0 || i + 8 > n) { + assert_eq!(&f.read_unit(d, si, i).unwrap(), frame, "cut {cut} stream {si} unit {i}"); + } + } + } + } +} diff --git a/crates/media/tests/oracle_mpegts.rs b/crates/media/tests/oracle_mpegts.rs new file mode 100644 index 000000000..46a51428b --- /dev/null +++ b/crates/media/tests/oracle_mpegts.rs @@ -0,0 +1,278 @@ +//! Oracle tests: ffmpeg-made transport and program streams (skipped without ffmpeg/ffprobe). The +//! demuxer's streams, access-unit counts, sizes, timestamps and key flags are compared with +//! ffprobe's packets, and every unit reads back. + +mod common; + +use craft_media::CodecId; +use craft_media::mpegts::{Codec, File, Format, Kind, open}; +use serde_json::Value; +use std::path::{Path, PathBuf}; +use std::process::Command; + +const PIC: &[&str] = &["-f", "lavfi", "-i", "testsrc2=size=320x240:rate=25"]; +/// Stereo audio with different content per channel. +const NOISE: &[&str] = + &["-f", "lavfi", "-i", "aevalsrc=exprs=0.3*sin(2*PI*441*t)+0.1*sin(2*PI*3100*t)|0.25*sin(2*PI*660*t)+0.1*sin(2*PI*(500+4000*t)*t):s=48000"]; + +const ALL: &[&str] = &[ + "ts_mpeg2_mp2.ts", + "ts_h264_aac.ts", + "m2ts_h264_lpcm.m2ts", + "m2ts_h264_ac3.mts", + "ts_hevc_latm.ts", + "ts_h264_eac3.ts", + "ps_mpeg2_mp2.vob", + "ps_mpeg2_lpcm.vob", + "ps_mpeg2_ac3.vob", + "mpeg1_system.mpg", +]; + +fn spec(name: &str) -> Vec<&'static str> { + let cat = |p: &[&[&'static str]]| p.concat(); + let x264: &[&str] = &["-c:v", "libx264", "-bf", "2", "-g", "12", "-pix_fmt", "yuv420p"]; + match name { + // broadcast-style: interlaced MPEG-2 and MPEG-1 layer II + "ts_mpeg2_mp2.ts" => cat(&[ + &["-f", "lavfi", "-i", "testsrc2=size=720x576:rate=50,tinterlace=mode=interleave_top,setfield=tff"], + NOISE, + &[ + "-t", + "1.2", + "-c:v", + "mpeg2video", + "-flags", + "+ilme+ildct", + "-bf", + "2", + "-g", + "12", + "-b:v", + "6M", + "-c:a", + "mp2", + "-b:a", + "192k", + "-f", + "mpegts", + ], + ]), + "ts_h264_aac.ts" => cat(&[PIC, NOISE, &["-t", "1.2"], x264, &["-c:a", "aac", "-b:a", "128k", "-f", "mpegts"]]), + // BDAV (192-byte packets) with Blu-ray LPCM, and with AC-3 as AVCHD has + "m2ts_h264_lpcm.m2ts" => { + cat(&[PIC, NOISE, &["-t", "1"], x264, &["-c:a", "pcm_bluray", "-ac", "2", "-f", "mpegts", "-mpegts_m2ts_mode", "1"]]) + } + "m2ts_h264_ac3.mts" => { + cat(&[PIC, NOISE, &["-t", "1"], x264, &["-c:a", "ac3", "-b:a", "192k", "-ac", "2", "-f", "mpegts", "-mpegts_m2ts_mode", "1"]]) + } + "ts_hevc_latm.ts" => cat(&[ + PIC, + NOISE, + &["-t", "1", "-c:v", "libx265", "-x265-params", "log-level=error:bframes=2:keyint=12"], + &["-c:a", "aac", "-b:a", "96k", "-f", "mpegts", "-mpegts_flags", "latm"], + ]), + "ts_h264_eac3.ts" => cat(&[PIC, NOISE, &["-t", "1"], x264, &["-c:a", "eac3", "-b:a", "192k", "-f", "mpegts"]]), + // DVD-style program streams + "ps_mpeg2_mp2.vob" => cat(&[ + &["-f", "lavfi", "-i", "testsrc2=size=720x480:rate=30000/1001"], + NOISE, + &["-t", "1.2", "-c:v", "mpeg2video", "-bf", "2", "-g", "15", "-b:v", "5M", "-c:a", "mp2", "-b:a", "224k", "-f", "vob"], + ]), + "ps_mpeg2_lpcm.vob" => cat(&[PIC, NOISE, &["-t", "1", "-c:v", "mpeg2video", "-bf", "2", "-c:a", "pcm_dvd", "-ac", "2", "-f", "vob"]]), + "ps_mpeg2_ac3.vob" => cat(&[PIC, NOISE, &["-t", "1", "-c:v", "mpeg2video", "-bf", "2", "-c:a", "ac3", "-b:a", "192k", "-f", "vob"]]), + // ISO/IEC 11172-1 system stream + "mpeg1_system.mpg" => { + cat(&[PIC, NOISE, &["-t", "1.2", "-c:v", "mpeg1video", "-bf", "2", "-b:v", "1500k", "-c:a", "mp2", "-b:a", "128k", "-f", "mpeg"]]) + } + other => panic!("unknown fixture {other}"), + } +} + +fn fixture(name: &str) -> Option { + let ffmpeg = common::ffmpeg_or_skip("mpegts oracle")?; + let out = common::fixtures_dir("mpegts").join(name); + common::fixtures::generate(&out, |tmp: &Path| { + // the temporary name keeps no known extension: every spec names its muxer with `-f` + Command::new(&ffmpeg).args(["-v", "error", "-y", "-nostdin"]).args(spec(name)).arg(tmp).status().is_ok_and(|s| s.success()) + }) +} + +/// ffprobe's packets of stream `index`: (pts, size, key). +fn packets(all: &[Value], index: i64) -> Vec<(Option, u64, bool)> { + all.iter() + .filter(|p| common::int(&p["stream_index"]) == Some(index)) + .map(|p| (common::int(&p["pts"]), common::int(&p["size"]).unwrap_or(0) as u64, p["flags"].as_str().is_some_and(|f| f.starts_with('K')))) + .collect() +} + +/// The codec ffprobe names, as this module's `Codec`. +fn expected_codec(name: &str, format: Format) -> Codec { + match name { + "mpeg1video" => Codec::Mpeg1Video, + "mpeg2video" => Codec::Mpeg2Video, + "h264" => Codec::H264, + "hevc" => Codec::Hevc, + "mp2" | "mp3" => Codec::MpegAudio, + "aac" => Codec::AacAdts, + "aac_latm" => Codec::AacLatm, + "ac3" => Codec::Ac3, + "eac3" => Codec::Eac3, + "pcm_bluray" => Codec::LpcmBluray, + "pcm_dvd" => Codec::LpcmDvd, + other => panic!("{format:?}: ffprobe codec {other} has no mapping in this test"), + } +} + +fn check(name: &str) -> Option { + let path = fixture(name)?; + let bytes = std::fs::read(&path).unwrap(); + let f = open(&bytes).unwrap(); + assert!(f.warnings.is_empty(), "{name}: {:?}", f.warnings); + let streams = common::streams(&path)?; + let all = common::packets(&path)?; + assert_eq!(f.streams.len(), streams.len(), "{name}: stream count"); + for kind in [Kind::Video, Kind::Audio] { + let what = if kind == Kind::Video { "video" } else { "audio" }; + // a program stream lists its streams in the order they appear, which need not be ffprobe's + let ours = f.streams.iter().position(|s| s.kind() == kind).unwrap_or_else(|| panic!("{name}: no {what} stream")); + let theirs = streams.iter().find(|s| s["codec_type"] == what).unwrap_or_else(|| panic!("{name}: ffprobe has no {what} stream")); + let s = &f.streams[ours]; + let codec = expected_codec(theirs["codec_name"].as_str().unwrap(), f.format); + assert_eq!(s.codec, codec, "{name}: {what} codec"); + let pk = packets(&all, common::int(&theirs["index"]).unwrap()); + assert_eq!(s.units.len(), pk.len(), "{name}: {what} unit count"); + if kind == Kind::Video { + // every picture ffprobe gives a PTS has the same one here (ffprobe fills in some a + // program stream leaves out), in the same order + for (i, (u, p)) in s.units.iter().zip(&pk).enumerate() { + if let Some(pts) = u.pts { + assert_eq!(Some(pts), p.0, "{name}: video unit {i} PTS"); + } + assert_eq!(u.key, p.2, "{name}: video unit {i} key flag"); + } + let stamped = s.units.iter().filter(|u| u.pts.is_some()).count(); + assert!(stamped > 0, "{name}: no video unit has a PTS"); + if matches!(f.format, Format::Ts { .. }) { + assert_eq!(stamped, s.units.len(), "{name}: every TS picture starts a PES packet"); + } + } else { + // LATM: ffprobe's packets are the AAC frames without the LOAS framing + if codec != Codec::AacLatm { + let (ours, theirs): (Vec, Vec) = (s.units.iter().map(|u| u64::from(u.size)).collect(), pk.iter().map(|p| p.1).collect()); + // a DVD LPCM unit is the samples alone; ffprobe's packet keeps the 3 format bytes + // before them + let kept = if codec == Codec::LpcmDvd { 3 } else { 0 }; + assert_eq!(ours.iter().map(|n| n + kept).collect::>(), theirs, "{name}: audio unit sizes"); + } + // a PTS belongs to the first frame that starts in a PES packet; ffprobe interpolates + // the others + for (i, (u, p)) in s.units.iter().zip(&pk).enumerate() { + if let Some(pts) = u.pts { + assert_eq!(Some(pts), p.0, "{name}: audio unit {i} PTS"); + } + } + assert!(s.units.first().is_some_and(|u| u.pts.is_some()), "{name}: the first audio unit has a PTS"); + assert!(s.units.iter().all(|u| u.key), "{name}: audio units are all key"); + } + // every unit reads back whole + for (i, u) in s.units.iter().enumerate() { + assert_eq!(f.read_unit(&bytes, ours, i).unwrap().len(), u.size as usize, "{name}: {what} unit {i}"); + } + } + Some(f) +} + +#[test] +fn transport_streams_match_ffprobe() { + for name in ["ts_mpeg2_mp2.ts", "ts_h264_aac.ts", "ts_hevc_latm.ts", "ts_h264_eac3.ts"] { + let Some(f) = check(name) else { return }; + assert_eq!(f.format, Format::Ts { packet_size: 188, sync_offset: 0 }, "{name}"); + assert_eq!(f.program, Some(1), "{name}"); + let (first, last) = f.pcr_range.unwrap(); + assert!(last > first, "{name}: PCR range"); + let ids: Vec = f.streams.iter().map(|s| s.codec_params().codec).collect(); + let want = match name { + "ts_mpeg2_mp2.ts" => [CodecId::Mpeg2Video, CodecId::Mp2], + "ts_h264_aac.ts" => [CodecId::H264, CodecId::Aac], + "ts_hevc_latm.ts" => [CodecId::Hevc, CodecId::Aac], + _ => [CodecId::H264, CodecId::Eac3], + }; + assert_eq!(ids, want, "{name}"); + } +} + +#[test] +fn mpeg2_pictures_carry_their_coding_type_and_field_order() { + let Some(f) = check("ts_mpeg2_mp2.ts") else { return }; + let v = &f.streams[0]; + let pics: Vec<_> = v.units.iter().map(|u| u.picture.unwrap()).collect(); + assert_eq!(pics[0].coding_type, 1); + assert!(pics[0].sequence_header && pics[0].gop); + assert!(pics.iter().any(|p| p.coding_type == 3) && pics.iter().any(|p| p.coding_type == 2)); + assert!(pics.iter().all(|p| p.top_field_first && !p.progressive_frame), "interlaced, top field first"); + // B pictures are the disposable ones, I pictures the keys + for (u, p) in v.units.iter().zip(&pics) { + assert_eq!((u.key, u.disposable), (p.coding_type == 1, p.coding_type == 3)); + } +} + +#[test] +fn bdav_streams_match_ffprobe() { + for name in ["m2ts_h264_lpcm.m2ts", "m2ts_h264_ac3.mts"] { + let Some(f) = check(name) else { return }; + assert_eq!(f.format, Format::Ts { packet_size: 192, sync_offset: 4 }, "{name}"); + assert_eq!(f.format.name(), "MPEG-2 TS (BDAV/AVCHD)"); + } +} + +#[test] +fn program_streams_match_ffprobe() { + for name in ["ps_mpeg2_mp2.vob", "ps_mpeg2_lpcm.vob", "ps_mpeg2_ac3.vob", "mpeg1_system.mpg"] { + let Some(f) = check(name) else { return }; + assert_eq!(f.format, Format::Ps { mpeg1: name == "mpeg1_system.mpg" }, "{name}"); + assert!(f.pcr_range.is_some(), "{name}: SCR range"); + if name == "ps_mpeg2_lpcm.vob" { + let a = f.streams.iter().find(|s| s.kind() == Kind::Audio).unwrap(); + let p = a.codec_params(); + assert_eq!((p.codec, p.sample_rate, p.channels), (CodecId::Pcm, Some(48_000), Some(2))); + assert!(p.pcm.is_some_and(|f| f.big_endian && f.signed && !f.float)); + } + } +} + +/// Every fixture, truncated and damaged: opening and reading never panics. +#[test] +fn damaged_ffmpeg_files_never_panic() { + for name in ALL { + let Some(path) = fixture(name) else { return }; + let bytes = std::fs::read(&path).unwrap(); + let mut x = 0x2545_F491u32; + let mut next = move || { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + x as usize + }; + for round in 0..12 { + let mut d = bytes.clone(); + if round % 3 == 0 { + d.truncate(next() % d.len()); + } + for _ in 0..(round * 40) { + let at = next() % d.len().max(1); + if let Some(b) = d.get_mut(at) { + *b = next() as u8; + } + } + let Ok(f) = open(&d) else { continue }; + for (s, st) in f.streams.iter().enumerate() { + let _ = st.codec_params(); + // the first, the last and a spread of units + let n = st.units.len(); + for i in (0..n).step_by((n / 16).max(1)).chain(n.checked_sub(1)) { + let _ = f.read_unit(&d, s, i); + } + } + } + } +} From 85f30007d73367cf457a95c8cd330f370089c8fd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:49:24 -0500 Subject: [PATCH 025/120] hayro-jbig2: bound a stream's total decode work and what a page retains; no i32 overflow (#831) Port of pdfcraft 85c10051 (patches 5-9), 82439525 (patch 10) and 10107d94 (patch 12). --- vendor/README.craft-reader.md | 17 + vendor/hayro-jbig2/SOURCE.json | 24 +- vendor/hayro-jbig2/src/bitmap.rs | 64 ++- .../src/decode/generic_refinement.rs | 36 +- vendor/hayro-jbig2/src/decode/halftone.rs | 34 +- vendor/hayro-jbig2/src/decode/pattern.rs | 37 +- vendor/hayro-jbig2/src/decode/symbol.rs | 77 ++- vendor/hayro-jbig2/src/decode/text.rs | 36 +- vendor/hayro-jbig2/src/error.rs | 13 +- vendor/hayro-jbig2/src/file.rs | 37 +- vendor/hayro-jbig2/src/gray_scale.rs | 4 +- vendor/hayro-jbig2/src/huffman_table.rs | 25 +- vendor/hayro-jbig2/src/integer_decoder.rs | 3 +- vendor/hayro-jbig2/src/lib.rs | 202 +++++-- vendor/hayro-jbig2/src/pdfcraft_tests.rs | 522 ++++++++++++++++++ 15 files changed, 1024 insertions(+), 107 deletions(-) create mode 100644 vendor/hayro-jbig2/src/pdfcraft_tests.rs diff --git a/vendor/README.craft-reader.md b/vendor/README.craft-reader.md index 2ce83e9ae..f9f281ab9 100644 --- a/vendor/README.craft-reader.md +++ b/vendor/README.craft-reader.md @@ -384,3 +384,20 @@ dictionary of revision 2–4 whose `/Length` is above 128 bits is refused with ` Those revisions take the file key from a 16-byte MD5 hash, so `/V 2 /R 3 /Length 256` sliced past the hash and panicked on open. Test: `crypto::pdfcraft_password_tests::legacy_key_length_above_128_bits_is_refused_not_a_panic`. + +JBIG2 decode bounds, ported from PdfCraft commits +[85c10051](https://github.com/storytold/pdfcraft/commit/85c100514) (hayro-jbig2 patches 5 to 9), +[82439525](https://github.com/storytold/pdfcraft/commit/824395256) (patch 10) and +[10107d94](https://github.com/storytold/pdfcraft/commit/10107d943) (patch 12), marked `PdfCraft patch` +in `hayro-jbig2`: a page's segments may hold at most 64 MiB (`MAX_RETAINED_WORDS`; regions, +dictionaries, Huffman tables, retained coding contexts), a segment may refer to at most 2^20 symbols +(`MAX_REFERRED_SYMBOLS`), a halftone grid has at most 2^25 cells, a custom Huffman table 2^16 lines, +a stream with its globals 2^20 segments and 2^22 references; a stream may ask for 2^30 units of +decoding work in total (`MAX_WORK`, charged item by item before each is decoded), and an image past a +bound is refused (not drawn) like the other caps; region locations, refinement offsets, symbol corners, +Huffman range lines and integers at the end of an `i32` no longer overflow. The `/JBIG2Globals` change +of 85c10051 for hayro-syntax was already carried here. PdfCraft's raster tests are adapted in +`hayro-jbig2/src/pdfcraft_tests.rs` (`jbig2_decoding_work_is_counted_item_by_item`, +`jbig2_placements_at_the_end_of_an_i32_draw_nothing`); the unit tests in `lib.rs`, `bitmap.rs` and +`decode/symbol.rs` come with the patches. PdfCraft's later JBIG2 follow-ups (b7ec3669, 72e763ad, +e3bc4e85, patch 13 and later) are not part of this port. diff --git a/vendor/hayro-jbig2/SOURCE.json b/vendor/hayro-jbig2/SOURCE.json index 6a972c129..5efee12a3 100644 --- a/vendor/hayro-jbig2/SOURCE.json +++ b/vendor/hayro-jbig2/SOURCE.json @@ -62,7 +62,7 @@ "path": "src/bitmap.rs", "source_sha256": "59ed83d8bac6c39e40270a9856ca3faee628de7d9efae1b01f714e140e2b6b7e", "source_git_blob": "35ed97561597d30a70fe819cf8cfbd3780538f75", - "sha256": "99dddbef5f7b36c55d0e914d07664326598919eb6d54cdfa93f300076a8ae69d" + "sha256": "dd330bda095350b91eeb15b029bdae209e1ed3d5c711dff279b79abd7832f847" }, { "path": "src/decode/generic.rs", @@ -74,13 +74,13 @@ "path": "src/decode/generic_refinement.rs", "source_sha256": "625075671e510e8ff1d7c3cade857792413c091d57a35561ad0591d7f04c2ea5", "source_git_blob": "a566b8de58c078dec874d5d1dea36c0f557f1a16", - "sha256": "625075671e510e8ff1d7c3cade857792413c091d57a35561ad0591d7f04c2ea5" + "sha256": "93e2ce1a40c35fed24d4d611cfc1763518e00167b4e3c0a0671a18afa37c7064" }, { "path": "src/decode/halftone.rs", "source_sha256": "0e4a5ef92e194b4eba2367196e904786dd7d346cdf0ab291c72a77b5aee5873a", "source_git_blob": "eb5c1c21e8a7d1a99c497e83ea59547b7dd19a96", - "sha256": "0e4a5ef92e194b4eba2367196e904786dd7d346cdf0ab291c72a77b5aee5873a" + "sha256": "16454b87a0c23e42d09936ae88f85cc03b4ea0e9d00ee409e751b4c94906c9d9" }, { "path": "src/decode/mod.rs", @@ -92,43 +92,43 @@ "path": "src/decode/pattern.rs", "source_sha256": "abdd326c08dee0488678d74915e6bb4138c9b0c07d44f24c3a5794ff640799ba", "source_git_blob": "2c15d70b8f9925638e41cec0eb1d73b91c1533f2", - "sha256": "abdd326c08dee0488678d74915e6bb4138c9b0c07d44f24c3a5794ff640799ba" + "sha256": "d0a431725919158734f047101d004001f2e58fc21af8f5e92ef03ae06041b9b1" }, { "path": "src/decode/symbol.rs", "source_sha256": "aa57e88cdc6fd1647d7996875c253a643fedfc4438d5137e326e00d17d0d29ee", "source_git_blob": "f1aa8a6639e53dd350057d8d078198d363daa918", - "sha256": "aa57e88cdc6fd1647d7996875c253a643fedfc4438d5137e326e00d17d0d29ee" + "sha256": "627de1c83267597efef6d6c0d49aec004f43fd6e63490f117f744ed9210b47f0" }, { "path": "src/decode/text.rs", "source_sha256": "01863cdcd48ab68b5230948d12212a815d293c7c9d5382a140772088bfb1c405", "source_git_blob": "bbc2ac55c7d7d5b8aebffbecfe93fda8b88526e8", - "sha256": "01863cdcd48ab68b5230948d12212a815d293c7c9d5382a140772088bfb1c405" + "sha256": "32ae695fe181ba07181fa9915b6bce2ca333031c392fae66dd15ce955a8d7b95" }, { "path": "src/error.rs", "source_sha256": "39b6ec8fa058bc5c1722892290882e123d193d3844ed5846b30b742b94ba72b0", "source_git_blob": "c579368b44b444566178e68a2705cf9561fad71f", - "sha256": "39b6ec8fa058bc5c1722892290882e123d193d3844ed5846b30b742b94ba72b0" + "sha256": "326dd926cc52b191d0c0398b3b2bb482ca267919e369045f129930cfa485e7fb" }, { "path": "src/file.rs", "source_sha256": "10467a415ab2e8b92a8951a86f4c02c5c74f5a005629a11865a20bc23b26ca55", "source_git_blob": "1440a22efa89f0deec0029968c3a20bad8526d63", - "sha256": "10467a415ab2e8b92a8951a86f4c02c5c74f5a005629a11865a20bc23b26ca55" + "sha256": "c96741babbfacfd78bd2b63f0f16541aaa235f14bb4344ecd4707e3ca5942df1" }, { "path": "src/gray_scale.rs", "source_sha256": "46f12893f4573610789015ba7036f6afb1d30b1454c0c674fc7ccdd9337d7487", "source_git_blob": "2836ec77a7c8d3970b13dd46f576778c64bfe2d9", - "sha256": "46f12893f4573610789015ba7036f6afb1d30b1454c0c674fc7ccdd9337d7487" + "sha256": "4bb0963338bfc9e3773db822465d5acbc1267c3349f04b30bff8c52e254874bb" }, { "path": "src/huffman_table.rs", "source_sha256": "8c0dc0681351b9573fedd00afed26173be8bd4583210063bff75c4e95e9d8054", "source_git_blob": "2183c8eea065ff4f6bd708fa92fa4ba66844e739", - "sha256": "8c0dc0681351b9573fedd00afed26173be8bd4583210063bff75c4e95e9d8054" + "sha256": "943fc4fe1e20d62df3908593571d1fa752de3c22a7d90b62dbcb59a7d3b04f1d" }, { "path": "src/huffman_tables_generated.rs", @@ -140,7 +140,7 @@ "path": "src/integer_decoder.rs", "source_sha256": "6a5a89d8df1ab6c011f7a92223fbd384bbf274ccb1f15705c994d7bd55d1a680", "source_git_blob": "8b7656d2964058618687ce2409142dddcdeb068a", - "sha256": "6a5a89d8df1ab6c011f7a92223fbd384bbf274ccb1f15705c994d7bd55d1a680" + "sha256": "6c41270b6ae1ef85bdfd26c37085c6ae7782f82b15951b53a0758b01ee175920" }, { "path": "src/integration.rs", @@ -158,7 +158,7 @@ "path": "src/lib.rs", "source_sha256": "22463230cc82543ee93791b642566a308f0f4eb14d3089b24dc2e7f06d0f784c", "source_git_blob": "ad77a6dd17920b2db66f6bb5a0f2cb8efaafd41a", - "sha256": "22463230cc82543ee93791b642566a308f0f4eb14d3089b24dc2e7f06d0f784c" + "sha256": "d0431d1c57c552b250b7b48895f0d768bfc75496b30cd0146b77764b09759c0f" }, { "path": "src/page_info.rs", diff --git a/vendor/hayro-jbig2/src/bitmap.rs b/vendor/hayro-jbig2/src/bitmap.rs index 6d1e3c55a..a240ca432 100644 --- a/vendor/hayro-jbig2/src/bitmap.rs +++ b/vendor/hayro-jbig2/src/bitmap.rs @@ -18,6 +18,35 @@ pub(crate) const MAX_DIMENSION: u32 = u16::MAX as u32; /// pixels, decoded one by one. pub(crate) const MAX_PIXELS: u64 = 1 << 28; +/// PdfCraft patch: the most cells a halftone grid may have, which still gives the largest page +/// (see [`MAX_PIXELS`]) cells of 3 × 3, unrotated. Each cell takes a 4-byte value while the grid decodes, so +/// the pixel bound alone let a 98-byte segment take 1 GiB; this one leaves 128 MiB. +pub(crate) const MAX_GRID_CELLS: u64 = 1 << 25; + +/// PdfCraft patch: the decoding work a stream may ask for, in pixels decoded from their code: four +/// of the largest pages, about 6 s on data the decoder predicts and about 17 s on data it can't, so +/// a render finishes inside its 20 s watchdog. Each bitmap is bounded on its own, but nothing +/// bounded how many there are: 1,140 bytes of generic regions took over 40 s, a 795-byte PDF's one +/// halftone 265 s, and a 923-byte PDF's regions would have taken days. +pub(crate) const MAX_WORK: u64 = 4 * MAX_PIXELS; + +/// PdfCraft patch: the work of a line of a Huffman table built for symbols, about 50 ns (see +/// [`MAX_WORK`]). +pub(crate) const HUFFMAN_LINE_WORK: u64 = 10; + +/// PdfCraft patch: the work of decoding a bitmap pixel by pixel (see [`MAX_WORK`]). A row costs up +/// to 21 ns however few pixels it has, about 4 pixels' worth, so one of no width isn't free: 16,384 +/// regions of 0 × 65535 took 9 s at a cost of nothing. +pub(crate) fn decode_work(width: u32, height: u32) -> u64 { + u64::from(height) * (u64::from(width) + 4) +} + +/// PdfCraft patch: the work of copying a bitmap (see [`MAX_WORK`]): a word at a time, and a row is +/// a copy of its own, so a tall thin bitmap costs its rows, not its pixels. +pub(crate) fn copy_work(width: u32, height: u32) -> u64 { + u64::from(height) * (u64::from(width.div_ceil(WORD_BITS)) + 1) +} + /// The underlying word type for packed pixel storage. /// Change this to `u32` to switch back to 32-bit words. pub(crate) type Word = u32; @@ -25,6 +54,23 @@ pub(crate) type Word = u32; pub(crate) const WORD_BITS: u32 = Word::BITS; pub(crate) const WORD_SHIFT: u32 = WORD_BITS - 1; +/// PdfCraft patch: the words a [`Bitmap`] takes before its pixels. A symbol of width 0 has no +/// pixels and still costs this: 5 KB of zero-width dictionaries held 781 MB. +pub(crate) const BITMAP_WORDS: usize = size_of::() / size_of::(); + +/// PdfCraft patch: refuse a page past the bounds on a bitmap (see `MAX_PIXELS`). A stream is held +/// to this when it is opened as well as when its page bitmap is sized: a caller sizes its own +/// output from the page header in between, and a 743-byte PDF's Indexed image asked for 137 GB. +pub(crate) fn check_page_size(width: u32, height: u32) -> Result<()> { + if width > MAX_DIMENSION || height > MAX_DIMENSION { + return Err(OverflowError::BitmapDimension.into()); + } + if u64::from(width) * u64::from(height) > MAX_PIXELS { + return Err(OverflowError::BitmapDimension.into()); + } + Ok(()) +} + /// A decoded bitmap with position information. /// /// "Pixels decoded by the MMR decoder having the value 'black' shall be treated @@ -58,6 +104,11 @@ fn fill_words(data: &mut Vec, len: usize, word: Word) -> Result<()> { } impl Bitmap { + /// PdfCraft patch: the words this bitmap holds, its header included. + pub(crate) fn words(&self) -> usize { + self.data.len().saturating_add(BITMAP_WORDS) + } + /// Create a new bitmap filled with white pixels (false). /// /// The bitmap is positioned at (0, 0). @@ -102,13 +153,7 @@ impl Bitmap { height: u32, default_pixel: bool, ) -> Result<()> { - if width > MAX_DIMENSION || height > MAX_DIMENSION { - return Err(OverflowError::BitmapDimension.into()); - } - // PdfCraft patch: see `MAX_PIXELS` (the page bitmap is sized here). - if u64::from(width) * u64::from(height) > MAX_PIXELS { - return Err(OverflowError::BitmapDimension.into()); - } + check_page_size(width, height)?; let stride = width.div_ceil(WORD_BITS); let default_word: Word = if default_pixel { !0 } else { 0 }; @@ -159,7 +204,8 @@ impl Bitmap { // TODO: Figure out how we can optimize this, especially since this is a // bottleneck for symbols/patterns. let dest_x_start = x.max(0); - let dest_x_end = (x + other.width as i32).min(self.width as i32); + // Saturating: a location can be anywhere an `i32` reaches, and past the end it is off the page. + let dest_x_end = x.saturating_add(other.width as i32).min(self.width as i32); if dest_x_start >= dest_x_end { return; @@ -170,7 +216,7 @@ impl Bitmap { let dest_x_end = dest_x_end as u32; for src_y in 0..other.height { - let dest_y = y + src_y as i32; + let dest_y = y.saturating_add(src_y as i32); if dest_y < 0 || dest_y >= self.height as i32 { continue; } diff --git a/vendor/hayro-jbig2/src/decode/generic_refinement.rs b/vendor/hayro-jbig2/src/decode/generic_refinement.rs index a566b8de5..a6634439d 100644 --- a/vendor/hayro-jbig2/src/decode/generic_refinement.rs +++ b/vendor/hayro-jbig2/src/decode/generic_refinement.rs @@ -179,10 +179,12 @@ impl<'a> RefinementContextGatherer<'a> { pub(crate) fn start_row(&mut self, region: &Bitmap, reference: &Bitmap, y: u32) { self.reg_y = y; - let ref_y = y as i32 - self.reference_dy; + // The offsets can be anywhere an `i32` reaches, so coordinates in the reference wrap here and + // below. Wrapped, they are off the reference, whose pixels there read as 0. + let ref_y = (y as i32).wrapping_sub(self.reference_dy); // This will on purpose wrap to a very large `u32` for negative values, such that // they resolve to 0 in `ContextGatherer::load_word`. - let ref_x0 = (-self.reference_dx) as u32; + let ref_x0 = self.reference_dx.wrapping_neg() as u32; self.reg_cur_x = 0; self.reg_m1 = if y >= 1 { @@ -193,9 +195,9 @@ impl<'a> RefinementContextGatherer<'a> { self.reg_cur = 0; self.ref_cur_x = 0; - self.ref_m1 = ContextGatherer::load_word(reference, (ref_y - 1) as u32, 0); + self.ref_m1 = ContextGatherer::load_word(reference, ref_y.wrapping_sub(1) as u32, 0); self.ref_cur = ContextGatherer::load_word(reference, ref_y as u32, 0); - self.ref_p1 = ContextGatherer::load_word(reference, (ref_y + 1) as u32, 0); + self.ref_p1 = ContextGatherer::load_word(reference, ref_y.wrapping_add(1) as u32, 0); self.ctx = match self.template { RefinementTemplate::Template1 => { @@ -257,7 +259,7 @@ impl<'a> RefinementContextGatherer<'a> { self.reg_cur = ContextGatherer::load_word(region, self.reg_y, new_start); } - let ref_x_signed = x as i32 - self.reference_dx; + let ref_x_signed = (x as i32).wrapping_sub(self.reference_dx); if ref_x_signed < 0 { self.ref_cur_x = 0; self.ref_m1 = 0; @@ -268,18 +270,20 @@ impl<'a> RefinementContextGatherer<'a> { if ref_x + 1 >= self.ref_cur_x + WORD_BITS || ref_x < self.ref_cur_x { let new_start = ref_x.saturating_sub(1); - let ref_y = self.reg_y as i32 - self.reference_dy; + let ref_y = (self.reg_y as i32).wrapping_sub(self.reference_dy); self.ref_cur_x = new_start; - self.ref_m1 = ContextGatherer::load_word(reference, (ref_y - 1) as u32, new_start); + self.ref_m1 = + ContextGatherer::load_word(reference, ref_y.wrapping_sub(1) as u32, new_start); self.ref_cur = ContextGatherer::load_word(reference, ref_y as u32, new_start); - self.ref_p1 = ContextGatherer::load_word(reference, (ref_y + 1) as u32, new_start); + self.ref_p1 = + ContextGatherer::load_word(reference, ref_y.wrapping_add(1) as u32, new_start); } } } #[inline(always)] pub(crate) fn tpgr_all_same(&self, x: u32) -> bool { - let ref_x = (x as i32 - self.reference_dx) as u32; + let ref_x = (x as i32).wrapping_sub(self.reference_dx) as u32; let rbx = ref_x.wrapping_sub(self.ref_cur_x); #[inline(always)] @@ -310,7 +314,7 @@ impl<'a> RefinementContextGatherer<'a> { #[inline(always)] pub(crate) fn ref_center_pixel(&self, x: u32) -> u8 { - let ref_x = (x as i32 - self.reference_dx) as u32; + let ref_x = (x as i32).wrapping_sub(self.reference_dx) as u32; let rbx = ref_x.wrapping_sub(self.ref_cur_x); ContextGatherer::get_buf_pixel(self.ref_cur, rbx) as u8 @@ -329,7 +333,7 @@ impl<'a> RefinementContextGatherer<'a> { #[inline(always)] fn gather_template0_default(&mut self, _region: &Bitmap, _reference: &Bitmap, x: u32) -> u16 { let bx = x - self.reg_cur_x; - let ref_x = (x as i32 - self.reference_dx) as u32; + let ref_x = (x as i32).wrapping_sub(self.reference_dx) as u32; let rbx = ref_x.wrapping_sub(self.ref_cur_x); let new_pixels = (ContextGatherer::get_buf_pixel(self.reg_m1, bx + 1) << 10) @@ -345,12 +349,12 @@ impl<'a> RefinementContextGatherer<'a> { #[inline(always)] fn gather_template0_custom(&mut self, region: &Bitmap, reference: &Bitmap, x: u32) -> u16 { let bx = x - self.reg_cur_x; - let ref_x_i = x as i32 - self.reference_dx; + let ref_x_i = (x as i32).wrapping_sub(self.reference_dx); let rbx = ref_x_i as u32 - self.ref_cur_x; let xi = x as i32; let yi = self.reg_y as i32; - let ref_y = yi - self.reference_dy; + let ref_y = yi.wrapping_sub(self.reference_dy); let at1 = self.at_pixels[0]; let at2 = self.at_pixels[1]; @@ -360,8 +364,8 @@ impl<'a> RefinementContextGatherer<'a> { | (ContextGatherer::get_buf_pixel(self.reg_m1, bx + 1) << 10) | (ContextGatherer::get_buf_pixel(self.reg_cur, bx.wrapping_sub(1)) << 9) | ((reference.get_pixel( - (ref_x_i + at2.x as i32) as u32, - (ref_y + at2.y as i32) as u32, + ref_x_i.wrapping_add(at2.x as i32) as u32, + ref_y.wrapping_add(at2.y as i32) as u32, ) as u16) << 8) | (ContextGatherer::get_buf_pixel(self.ref_m1, rbx.wrapping_add(1)) << 6) @@ -375,7 +379,7 @@ impl<'a> RefinementContextGatherer<'a> { #[inline(always)] fn gather_template1(&mut self, _region: &Bitmap, _reference: &Bitmap, x: u32) -> u16 { let bx = x - self.reg_cur_x; - let ref_x = (x as i32 - self.reference_dx) as u32; + let ref_x = (x as i32).wrapping_sub(self.reference_dx) as u32; let rbx = ref_x.wrapping_sub(self.ref_cur_x); let new_pixels = (ContextGatherer::get_buf_pixel(self.reg_m1, bx + 1) << 7) diff --git a/vendor/hayro-jbig2/src/decode/halftone.rs b/vendor/hayro-jbig2/src/decode/halftone.rs index eb5c1c21e..10000f801 100644 --- a/vendor/hayro-jbig2/src/decode/halftone.rs +++ b/vendor/hayro-jbig2/src/decode/halftone.rs @@ -7,7 +7,7 @@ use super::RegionBitmap; use super::pattern::PatternDictionary; use super::{CombinationOperator, RegionSegmentInfo, Template, parse_region_segment_info}; use crate::ScratchBuffers; -use crate::bitmap::{Bitmap, MAX_DIMENSION, WORD_BITS}; +use crate::bitmap::{Bitmap, MAX_DIMENSION, WORD_BITS, copy_work, decode_work}; use crate::error::{OverflowError, ParseError, RegionError, Result, bail}; use crate::gray_scale::{GrayScaleParams, decode_gray_scale_image}; use crate::reader::Reader; @@ -48,11 +48,7 @@ pub(crate) fn decode_into( None }; - // "3) Set HBPP to ⌈log₂(HNUMPATS)⌉." (6.6.5) - let bits_per_pixel = (pattern_dict.patterns.len() as u32) - .saturating_sub(1) - .checked_ilog2() - .map_or(1, |n| n + 1); + let bits_per_pixel = bits_per_pixel(pattern_dict); // "4) Decode an image GI of size HGW by HGH with HBPP bits per pixel using // the gray-scale image decoding procedure as described in Annex C." (6.6.5) @@ -73,6 +69,28 @@ pub(crate) fn decode_into( Ok(()) } +/// PdfCraft patch: the work of a halftone region (see `bitmap::MAX_WORK`): a bit plane of the grid +/// for each bit of a pattern's index, a pattern copied for each cell, and the region itself. One +/// such segment in a 795-byte PDF took 265 s. +pub(crate) fn work(header: &HalftoneRegionHeader<'_>, patterns: &PatternDictionary) -> u64 { + let grid = &header.grid_position_and_size; + let cells = u64::from(grid.width) * u64::from(grid.height); + let planes = u64::from(bits_per_pixel(patterns)); + let copied = copy_work(patterns.pattern_width, patterns.pattern_height); + planes + .saturating_mul(decode_work(grid.width, grid.height)) + .saturating_add(cells.saturating_mul(copied)) + .saturating_add(copy_work(header.region_info.width, header.region_info.height)) +} + +/// "3) Set HBPP to ⌈log₂(HNUMPATS)⌉." (6.6.5) +fn bits_per_pixel(patterns: &PatternDictionary) -> u32 { + (patterns.patterns.len() as u32) + .saturating_sub(1) + .checked_ilog2() + .map_or(1, |n| n + 1) +} + /// Parse a halftone region segment header (7.4.5.1). pub(crate) fn parse<'a>(reader: &mut Reader<'a>) -> Result> { let region_info = parse_region_segment_info(reader)?; @@ -97,8 +115,8 @@ pub(crate) fn parse<'a>(reader: &mut Reader<'a>) -> Result MAX_DIMENSION || grid_height > MAX_DIMENSION { bail!(OverflowError::GridDimension); } - // PdfCraft patch: the product is bounded too (see `bitmap::MAX_PIXELS`). - if u64::from(grid_width) * u64::from(grid_height) > crate::bitmap::MAX_PIXELS { + // PdfCraft patch: the product is bounded too (see `bitmap::MAX_GRID_CELLS`). + if u64::from(grid_width) * u64::from(grid_height) > crate::bitmap::MAX_GRID_CELLS { bail!(OverflowError::GridDimension); } diff --git a/vendor/hayro-jbig2/src/decode/pattern.rs b/vendor/hayro-jbig2/src/decode/pattern.rs index 2c15d70b8..03d4d8eb8 100644 --- a/vendor/hayro-jbig2/src/decode/pattern.rs +++ b/vendor/hayro-jbig2/src/decode/pattern.rs @@ -3,15 +3,20 @@ use super::{AdaptiveTemplatePixel, Template, generic}; use crate::ScratchBuffers; use crate::arithmetic_decoder::{ArithmeticDecoder, ArithmeticDecoderContext}; -use crate::bitmap::{Bitmap, WORD_BITS, Word}; +use crate::bitmap::{Bitmap, WORD_BITS, Word, copy_work}; use crate::error::{OverflowError, ParseError, Result}; use crate::reader::Reader; use alloc::vec::Vec; +/// PdfCraft patch: the most words of shifted copies a dictionary keeps, 16 MiB, where 256 patterns +/// of 32 rows take 2 MiB. A 48-byte dictionary of 65,535 such patterns asked for 539 MB. +const MAX_SHIFTED_WORDS: u64 = 1 << 22; + /// Decode a pattern dictionary segment (7.4.4.2, 6.7). pub(crate) fn decode( header: &PatternDictionaryHeader<'_>, scratch: &mut ScratchBuffers, + room: u64, ) -> Result { let pattern_width = header.pattern_width as u32; let pattern_height = header.pattern_height as u32; @@ -133,7 +138,19 @@ pub(crate) fn decode( // for all possible 32-bit alignments, which later on allows us to // very easily blit it into the backdrop, significantly speeding up // the whole process. - let shifted_patterns = if pattern_width <= WORD_BITS && pattern_height <= WORD_BITS { + // + // PdfCraft patch: only when the table is small, and fits what the page may still hold + // (`room`), so that a speed-up never gets an image refused. It can be left out: + // `shifted_pattern` then returns `None` and the patterns are combined the slow way. + let shifted_words = (patterns.len() as u64) + .saturating_mul(32 * 2) + .saturating_mul(u64::from(pattern_height)); + let pattern_words = patterns.iter().map(|p| p.words() as u64).fold(0, u64::saturating_add); + let shifted_patterns = if pattern_width <= WORD_BITS + && pattern_height <= WORD_BITS + && shifted_words <= MAX_SHIFTED_WORDS + && shifted_words.saturating_add(pattern_words) <= room + { let h = pattern_height as usize; let mut data = Vec::with_capacity(patterns.len() * 32 * h * 2); for pattern in &patterns { @@ -180,6 +197,11 @@ impl PatternDictionary { let start = (pattern_idx * 32 + bit_offset as usize) * len; self.shifted_patterns.get(start..start + len) } + + /// PdfCraft patch: the words the dictionary holds, its patterns and their shifted copies. + pub(crate) fn words(&self) -> usize { + self.patterns.iter().map(Bitmap::words).fold(self.shifted_patterns.len(), usize::saturating_add) + } } /// Parsed pattern dictionary segment header (7.4.4.1). @@ -196,6 +218,17 @@ pub(crate) struct PatternDictionaryHeader<'a> { pub(crate) data: &'a [u8], } +impl PatternDictionaryHeader<'_> { + /// PdfCraft patch: the work of decoding the patterns, side by side in one bitmap, and of + /// taking each out of it (see `bitmap::MAX_WORK`). + pub(crate) fn work(&self) -> u64 { + let (width, height) = (u32::from(self.pattern_width), u32::from(self.pattern_height)); + let patterns = u64::from(self.num_patterns) + 1; + let side_by_side = u64::from(height) * (patterns.saturating_mul(u64::from(width)) + 4); + side_by_side.saturating_add(patterns.saturating_mul(copy_work(width, height))) + } +} + /// Parse a pattern dictionary segment header (7.4.4.1). pub(crate) fn parse<'a>(reader: &mut Reader<'a>) -> Result> { let flags_byte = reader.read_byte().ok_or(ParseError::UnexpectedEof)?; diff --git a/vendor/hayro-jbig2/src/decode/symbol.rs b/vendor/hayro-jbig2/src/decode/symbol.rs index f1aa8a663..94250a3de 100644 --- a/vendor/hayro-jbig2/src/decode/symbol.rs +++ b/vendor/hayro-jbig2/src/decode/symbol.rs @@ -4,7 +4,7 @@ use alloc::vec; use alloc::vec::Vec; use crate::arithmetic_decoder::{ArithmeticDecoder, ArithmeticDecoderContext}; -use crate::bitmap::{Bitmap, WORD_BITS}; +use crate::bitmap::{BITMAP_WORDS, Bitmap, HUFFMAN_LINE_WORK, WORD_BITS, Word, decode_work}; use crate::decode::generic::{decode_bitmap_mmr, parse_adaptive_template_pixels}; use crate::decode::text::{ DecodeContext, ReferenceCorner, TextRegionContexts, TextRegionFlags, TextRegionHeader, @@ -45,6 +45,8 @@ pub(crate) fn decode( referred_tables: &[HuffmanTable], standard_tables: &StandardHuffmanTables, input_contexts: Option<&RetainedContexts>, + room: u64, + work_left: &mut u64, ) -> Result { let num_new_symbols = header.num_new_symbols; @@ -58,6 +60,9 @@ pub(crate) fn decode( total_width: 0, height_class_height: 0, pixels: 0, + new_words: 0, + room, + work_left, header, standard_tables, }; @@ -130,6 +135,16 @@ pub(crate) fn decode( if ctx.pixels > MAX_DICTIONARY_PIXELS { bail!(OverflowError::BitmapDimension); } + // PdfCraft patch: see `bitmap::MAX_WORK`. + let work = decode_work(symbol_width, ctx.height_class_height); + *ctx.work_left = ctx.work_left.checked_sub(work).ok_or(OverflowError::DecodingWork)?; + // PdfCraft patch: see `room`. Rows take a word at least, so symbols of one column + // held 270 MiB within the pixel bound, however few the dictionary exported. + let rows = u64::from(symbol_width.div_ceil(WORD_BITS)) * u64::from(ctx.height_class_height); + ctx.new_words = ctx.new_words.saturating_add(rows).saturating_add(BITMAP_WORDS as u64); + if ctx.new_words > ctx.room { + bail!(OverflowError::RetainedBitmaps); + } match (ctx.header.flags.use_huffman, ctx.header.flags.use_refagg) { (false, false) => { @@ -202,6 +217,18 @@ pub(crate) struct SymbolDictionary { pub(crate) retained_contexts: Option, } +impl SymbolDictionary { + /// PdfCraft patch: the words the dictionary holds, its retained contexts included. Those are + /// a byte each, and 1.1 MB of dictionaries that retain them, 103 KB behind Flate, held 2.89 GB. + pub(crate) fn words(&self) -> usize { + let contexts = self.retained_contexts.as_ref().map_or(0, |contexts| { + let bytes = contexts.generic_region.len().saturating_add(contexts.refinement_region.len()); + bytes.saturating_mul(size_of::()) / size_of::() + }); + self.exported_symbols.iter().map(Bitmap::words).fold(contexts, usize::saturating_add) + } +} + /// Decode a symbol bitmap using refinement/aggregate coding (6.5.8.2). fn decode_refinement_aggregation_bitmap( ctx: &mut SymbolDecodeContext<'_>, @@ -337,6 +364,11 @@ fn decode_aggregation_bitmap( ) -> Result { let use_huffman = ctx.header.flags.use_huffman; + // PdfCraft patch: see `bitmap::MAX_WORK`. What follows is sized by all the symbols, for each + // aggregated symbol, at about 50 ns a symbol: a 713-byte PDF asked for an hour. + let work = u64::from(ctx.total_symbols()).saturating_mul(HUFFMAN_LINE_WORK); + *ctx.work_left = ctx.work_left.checked_sub(work).ok_or(OverflowError::DecodingWork)?; + // Concatenate input and new symbols. let mut all_symbols: Vec<&Bitmap> = Vec::with_capacity(ctx.symbols.input.len() + ctx.symbols.new.len()); @@ -422,7 +454,7 @@ fn decode_aggregation_bitmap( header.region_info.y_location, header.flags.default_pixel, )?; - decode_with(decode_ctx, &all_symbols, &header, &mut bitmap)?; + decode_with(decode_ctx, &all_symbols, &header, &mut bitmap, ctx.work_left)?; Ok(bitmap) } @@ -463,6 +495,14 @@ struct SymbolDecodeContext<'a> { symbols_decoded_count: u32, total_width: u32, height_class_height: u32, + /// PdfCraft patch: the words the page may still hold, which neither the new symbols nor the + /// exported ones may pass. They are counted as they are made: counted only once the + /// dictionary was stored, 8 MiB symbols exported again and again killed the process. + room: u64, + /// PdfCraft patch: the words of the new symbols so far (see `room`). + new_words: u64, + /// PdfCraft patch: the decoding work the stream may still ask for (see `bitmap::MAX_WORK`). + work_left: &'a mut u64, /// PdfCraft patch: symbol pixels decoded so far (see `MAX_DICTIONARY_PIXELS`). pixels: u64, } @@ -708,6 +748,7 @@ fn export_symbols(ctx: &mut SymbolDecodeContext<'_>) -> Result> { u32::try_from(value).map_err(|_| HuffmanError::InvalidCode.into()) }; let mut exported = Vec::with_capacity(ctx.header.num_exported_symbols as usize); + let mut exported_words: u64 = 0; let mut index: u32 = 0; let mut should_export = false; // PdfCraft patch: see `EXTRA_EMPTY_STEPS`. @@ -730,9 +771,13 @@ fn export_symbols(ctx: &mut SymbolDecodeContext<'_>) -> Result> { let symbol = ctx .symbols .get(symbol_idx as usize) - .ok_or(SymbolError::OutOfRange)? - .clone(); - exported.push(symbol); + .ok_or(SymbolError::OutOfRange)?; + // PdfCraft patch: see `room`. + exported_words = exported_words.saturating_add(symbol.words() as u64); + if exported_words > ctx.room { + bail!(OverflowError::RetainedBitmaps); + } + exported.push(symbol.clone()); } } @@ -864,3 +909,25 @@ pub(crate) fn parse<'a>(reader: &mut Reader<'a>) -> Result Result { let mut bitmap = Bitmap::new_with( header.region_info.width, @@ -41,6 +42,7 @@ pub(crate) fn decode( standard_tables, &mut bitmap, scratch, + work_left, )?; Ok(RegionBitmap { @@ -56,12 +58,13 @@ pub(crate) fn decode_into( standard_tables: &StandardHuffmanTables, bitmap: &mut Bitmap, scratch: &mut ScratchBuffers, + work_left: &mut u64, ) -> Result<()> { if header.flags.use_huffman { let mut reader = Reader::new(header.data); let ctx = DecodeContext::new_huffman(&mut reader, header, referred_tables, standard_tables)?; - decode_with(ctx, symbols, header, bitmap)?; + decode_with(ctx, symbols, header, bitmap, work_left)?; } else { let mut decoder = ArithmeticDecoder::new(header.data); @@ -76,7 +79,7 @@ pub(crate) fn decode_into( .resize(num_gr_contexts, ArithmeticDecoderContext::default()); let ctx = DecodeContext::new_arithmetic(&mut decoder, &mut contexts, &mut scratch.contexts); - decode_with(ctx, symbols, header, bitmap)?; + decode_with(ctx, symbols, header, bitmap, work_left)?; } Ok(()) @@ -88,6 +91,7 @@ pub(crate) fn decode_with( symbols: &[&Bitmap], header: &TextRegionHeader<'_>, region: &mut Bitmap, + work_left: &mut u64, ) -> Result<()> { let strip_size = header.strip_size(); @@ -156,6 +160,17 @@ pub(crate) fn decode_with( let symbol_width = symbol_bitmap_ref.width as i32; let symbol_height = symbol_bitmap_ref.height as i32; + // PdfCraft patch: see `bitmap::MAX_WORK`. An instance of a large symbol costs its copy, + // and 10,000 of one took 64 s. A refined one was decoded as well. Even one with no + // pixels goes round this loop. + let (width, height) = (symbol_bitmap_ref.width, symbol_bitmap_ref.height); + let copied = copy_work(width, height).saturating_add(1); + let work = match &symbol_bitmap { + SymbolBitmap::Reference(_) => copied, + SymbolBitmap::Owned(_) => copied.saturating_add(decode_work(width, height)), + }; + *work_left = work_left.checked_sub(work).ok_or(OverflowError::DecodingWork)?; + if !header.flags.transposed && (header.flags.reference_corner == ReferenceCorner::TopRight || header.flags.reference_corner == ReferenceCorner::BottomRight) @@ -173,23 +188,26 @@ pub(crate) fn decode_with( } let symbol_s = current_s; + // From the corner a symbol is placed by to its far side. The strip and the offset can + // be anywhere an `i32` reaches, and wrapped, the symbol is off the region. + let back = |corner: i32, size: i32| corner.wrapping_sub(size).wrapping_add(1); let (x, y) = if !header.flags.transposed { match header.flags.reference_corner { ReferenceCorner::TopLeft => (symbol_s, symbol_t), - ReferenceCorner::TopRight => (symbol_s - symbol_width + 1, symbol_t), - ReferenceCorner::BottomLeft => (symbol_s, symbol_t - symbol_height + 1), + ReferenceCorner::TopRight => (back(symbol_s, symbol_width), symbol_t), + ReferenceCorner::BottomLeft => (symbol_s, back(symbol_t, symbol_height)), ReferenceCorner::BottomRight => { - (symbol_s - symbol_width + 1, symbol_t - symbol_height + 1) + (back(symbol_s, symbol_width), back(symbol_t, symbol_height)) } } } else { match header.flags.reference_corner { ReferenceCorner::TopLeft => (symbol_t, symbol_s), - ReferenceCorner::TopRight => (symbol_t - symbol_width + 1, symbol_s), - ReferenceCorner::BottomLeft => (symbol_t, symbol_s - symbol_height + 1), + ReferenceCorner::TopRight => (back(symbol_t, symbol_width), symbol_s), + ReferenceCorner::BottomLeft => (symbol_t, back(symbol_s, symbol_height)), ReferenceCorner::BottomRight => { - (symbol_t - symbol_width + 1, symbol_s - symbol_height + 1) + (back(symbol_t, symbol_width), back(symbol_s, symbol_height)) } } }; diff --git a/vendor/hayro-jbig2/src/error.rs b/vendor/hayro-jbig2/src/error.rs index c579368b4..c8bcf8b81 100644 --- a/vendor/hayro-jbig2/src/error.rs +++ b/vendor/hayro-jbig2/src/error.rs @@ -133,8 +133,14 @@ pub enum OverflowError { ReferenceOffset, /// Index computation overflowed. Index, - /// A symbol dictionary has too many symbols. + /// A symbol dictionary has too many symbols, or a segment refers to too many. SymbolCount, + /// PdfCraft patch: the segments retain more bitmaps than a page may hold. + RetainedBitmaps, + /// PdfCraft patch: a stream has more segments, or references, than it may. + SegmentCount, + /// PdfCraft patch: a stream asks for more decoding work than it may. + DecodingWork, } impl fmt::Display for DecodeError { @@ -161,7 +167,10 @@ impl fmt::Display for OverflowError { Self::ReferenceOffset => write!(f, "overflow in reference offset computation"), Self::Index => write!(f, "overflow in index computation"), Self::GridDimension => write!(f, "overflow in grid dimension computation"), - Self::SymbolCount => write!(f, "a symbol dictionary has too many symbols"), + Self::SymbolCount => write!(f, "too many symbols in a dictionary or referred to"), + Self::RetainedBitmaps => write!(f, "the segments retain too many bitmaps"), + Self::SegmentCount => write!(f, "too many segments or references in a stream"), + Self::DecodingWork => write!(f, "a stream asks for too much decoding work"), } } } diff --git a/vendor/hayro-jbig2/src/file.rs b/vendor/hayro-jbig2/src/file.rs index 1440a22ef..5b8bbe983 100644 --- a/vendor/hayro-jbig2/src/file.rs +++ b/vendor/hayro-jbig2/src/file.rs @@ -5,12 +5,39 @@ use alloc::vec::Vec; -use crate::error::{FormatError, ParseError, Result, bail}; +use crate::error::{FormatError, OverflowError, ParseError, Result, bail}; use crate::reader::Reader; use crate::segment::{ Segment, SegmentType, parse_segment, parse_segment_data, parse_segment_header, }; +/// PdfCraft patch: the most segments a stream, with its globals, may have. They are all parsed and +/// sorted before the first is decoded, at about 64 bytes for 11 of input, so behind nested Flate a +/// 4,765-byte PDF reached 5 GB and failed only afterwards. +const MAX_SEGMENTS: usize = 1 << 20; + +/// PdfCraft patch: the most references those segments may make, 4 bytes each for 1 of input. +const MAX_REFERENCES: usize = 1 << 22; + +/// PdfCraft patch: the segments of a stream so far (see [`MAX_SEGMENTS`], [`MAX_REFERENCES`]). +#[derive(Default)] +pub(crate) struct SegmentCount { + segments: usize, + references: usize, +} + +impl SegmentCount { + /// Count one more segment, making `references` references. + fn add(&mut self, references: usize) -> Result<()> { + self.segments = self.segments.saturating_add(1); + self.references = self.references.saturating_add(references); + if self.segments > MAX_SEGMENTS || self.references > MAX_REFERENCES { + bail!(OverflowError::SegmentCount); + } + Ok(()) + } +} + /// "There are two standalone file organizations possible for a JBIG2 bitstream." /// (Annex D) #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -140,7 +167,9 @@ fn parse_segments<'a>( let mut segments = Vec::new(); match organization { - FileOrganization::Sequential => parse_segments_sequential(reader, &mut segments)?, + FileOrganization::Sequential => { + parse_segments_sequential(reader, &mut segments, &mut SegmentCount::default())?; + } FileOrganization::RandomAccess => parse_segments_random_access(reader, &mut segments)?, } @@ -155,6 +184,7 @@ fn parse_segments<'a>( pub(crate) fn parse_segments_sequential<'a>( reader: &mut Reader<'a>, segments: &mut Vec>, + count: &mut SegmentCount, ) -> Result<()> { loop { if reader.at_end() { @@ -162,6 +192,7 @@ pub(crate) fn parse_segments_sequential<'a>( } let segment = parse_segment(reader)?; + count.add(segment.header.referred_to_segments.len())?; // "If a file contains an end of file segment, it must be the last segment." // (7.4.11) @@ -187,6 +218,7 @@ fn parse_segments_random_access<'a>( segments: &mut Vec>, ) -> Result<()> { let mut headers = Vec::new(); + let mut count = SegmentCount::default(); loop { if reader.at_end() { @@ -194,6 +226,7 @@ fn parse_segments_random_access<'a>( } let header = parse_segment_header(reader)?; + count.add(header.referred_to_segments.len())?; // "If a file contains an end of file segment, it must be the last segment." // (7.4.11) diff --git a/vendor/hayro-jbig2/src/gray_scale.rs b/vendor/hayro-jbig2/src/gray_scale.rs index 2836ec77a..4aa4229a5 100644 --- a/vendor/hayro-jbig2/src/gray_scale.rs +++ b/vendor/hayro-jbig2/src/gray_scale.rs @@ -83,8 +83,8 @@ fn decode_arithmetic( if width > MAX_DIMENSION || height > MAX_DIMENSION { bail!(OverflowError::BitmapDimension); } - // PdfCraft patch: the product is bounded too (see `bitmap::MAX_PIXELS`). - if u64::from(width) * u64::from(height) > crate::bitmap::MAX_PIXELS { + // PdfCraft patch: the product is bounded too (see `bitmap::MAX_GRID_CELLS`). + if u64::from(width) * u64::from(height) > crate::bitmap::MAX_GRID_CELLS { bail!(OverflowError::BitmapDimension); } diff --git a/vendor/hayro-jbig2/src/huffman_table.rs b/vendor/hayro-jbig2/src/huffman_table.rs index 2183c8eea..1a20da528 100644 --- a/vendor/hayro-jbig2/src/huffman_table.rs +++ b/vendor/hayro-jbig2/src/huffman_table.rs @@ -14,11 +14,27 @@ include!("huffman_tables_generated.rs"); /// Maximum number of nodes in an inline Huffman table. const INLINE_TABLE_SIZE: usize = 43; +/// PdfCraft patch: the most lines a custom table may have. A line takes 2 bits of input at least +/// and 8 bytes here, so a table reading 250 MiB behind nested Flate, a 1,297-byte PDF, was killed +/// at 8 GiB before it could be counted. +const MAX_TABLE_LINES: usize = 1 << 16; + /// A queryable Huffman table. #[derive(Debug, Clone)] pub(crate) struct HuffmanTable(Rc); impl HuffmanTable { + /// PdfCraft patch: the words this table holds. Every table takes the room of the inline + /// variant, however few nodes it has, and a dynamic one its nodes besides. + pub(crate) fn words(&self) -> usize { + let nodes = match &*self.0 { + InnerHuffmanTable::Inline { .. } => 0, + InnerHuffmanTable::Dynamic { nodes } => nodes.len(), + }; + let bytes = nodes.saturating_mul(size_of::()); + bytes.saturating_add(size_of::()) / size_of::() + } + /// Create a new inline Huffman table from a fixed-size node array. fn from_inline(nodes: [HuffmanNode; INLINE_TABLE_SIZE]) -> Self { Self(Rc::new(InnerHuffmanTable::Inline { nodes })) @@ -219,6 +235,9 @@ impl HuffmanTable { // 5) "Decode each table line as follows:" // d) "If CURRANGELOW ≥ HTHIGH then proceed to step 6." while current_range_low < maximum_value { + if lines.len() >= MAX_TABLE_LINES { + bail!(HuffmanError::InvalidCode); + } // a) "Read HTPS bits. Set PREFLEN[NTEMP] to the value decoded." let prefix_length = reader .read_bits(prefix_length_bits) @@ -448,11 +467,11 @@ impl HuffmanNode { // 4) "Otherwise, if table line I is the lower range table line for this // table, then set: HTVAL = RANGELOW[I] − HTOFFSET" // 5) "Otherwise, set: HTVAL = RANGELOW[I] + HTOFFSET" - // `HTVAL` + // `HTVAL`. Wrapping: a range line's offset can take it past what an `i32` holds. let value = if leaf.is_lower { - leaf.range_low - range_offset + leaf.range_low.wrapping_sub(range_offset) } else { - leaf.range_low + range_offset + leaf.range_low.wrapping_add(range_offset) }; return Ok(Some(value)); diff --git a/vendor/hayro-jbig2/src/integer_decoder.rs b/vendor/hayro-jbig2/src/integer_decoder.rs index 8b7656d29..7f9f7831a 100644 --- a/vendor/hayro-jbig2/src/integer_decoder.rs +++ b/vendor/hayro-jbig2/src/integer_decoder.rs @@ -70,7 +70,8 @@ impl IntegerDecoder { if s == 0 { Some(v as i32) } else if v > 0 { - Some(-(v as i32)) + // Wrapping: `V` can be 2^31, whose negative is `i32::MIN`, as the cast already reads it. + Some((v as i32).wrapping_neg()) } else { None } diff --git a/vendor/hayro-jbig2/src/lib.rs b/vendor/hayro-jbig2/src/lib.rs index ad77a6dd1..078370ed3 100644 --- a/vendor/hayro-jbig2/src/lib.rs +++ b/vendor/hayro-jbig2/src/lib.rs @@ -69,13 +69,16 @@ mod segment; mod simd; mod symbol_id_decoder; +#[cfg(test)] +mod pdfcraft_tests; + use error::bail; pub use error::{ DecodeError, FormatError, HuffmanError, OverflowError, ParseError, RegionError, Result, SegmentError, SymbolError, TemplateError, }; -use crate::file::parse_segments_sequential; +use crate::file::{SegmentCount, parse_segments_sequential}; use bitmap::Bitmap; use decode::CombinationOperator; use decode::generic; @@ -120,13 +123,14 @@ impl<'a> Image<'a> { /// Annex D.3. pub fn new_embedded(data: &'a [u8], globals: Option<&'a [u8]>) -> Result { let mut segments = Vec::new(); + let mut count = SegmentCount::default(); if let Some(globals_data) = globals { let mut reader = Reader::new(globals_data); - parse_segments_sequential(&mut reader, &mut segments)?; + parse_segments_sequential(&mut reader, &mut segments, &mut count)?; }; let mut reader = Reader::new(data); - parse_segments_sequential(&mut reader, &mut segments)?; + parse_segments_sequential(&mut reader, &mut segments, &mut count)?; segments.sort_by_key(|seg| seg.header.segment_number); @@ -162,6 +166,7 @@ impl<'a> Image<'a> { if page_info.width == 0 || height == 0 { bail!(FormatError::EmptyPage); } + bitmap::check_page_size(page_info.width, height)?; Ok(Self { segments, @@ -257,6 +262,18 @@ fn emit_bitmap(bitmap: &Bitmap, decoder: &mut D) { } } +/// PdfCraft patch: count `work` more decoding work, refusing past [`bitmap::MAX_WORK`] before what +/// asks for it is decoded. +fn charge_work(left: &mut u64, work: u64) -> Result<()> { + *left = left.checked_sub(work).ok_or(OverflowError::DecodingWork)?; + Ok(()) +} + +/// PdfCraft patch: the work of a region decoded pixel by pixel. +fn region_work(region: &decode::RegionSegmentInfo) -> u64 { + bitmap::decode_work(region.width, region.height) +} + fn decode_segments( segments: &[segment::Segment<'_>], height_from_stripes: Option, @@ -281,6 +298,7 @@ fn decode_segments( let page_bitmap = &mut decoder_ctx.page_bitmap; let page_state = &mut decoder_ctx.page_state; let scratch_buffers = &mut decoder_ctx.scratch_buffers; + let mut work_left = bitmap::MAX_WORK; // Process all segments. for seg in segments { @@ -293,6 +311,7 @@ fn decode_segments( SegmentType::ImmediateGenericRegion | SegmentType::ImmediateLosslessGenericRegion => { let had_unknown_length = seg.header.data_length.is_none(); let header = generic::parse(&mut reader, had_unknown_length)?; + charge_work(&mut work_left, region_work(&header.region_info))?; if page_state.can_decode_directly(page_bitmap, &header.region_info, false) { generic::decode_into(&header, page_bitmap, scratch_buffers)?; @@ -310,25 +329,22 @@ fn decode_segments( SegmentType::IntermediateGenericRegion => { // Intermediate segments cannot have unknown length. let header = generic::parse(&mut reader, false)?; + charge_work(&mut work_left, region_work(&header.region_info))?; let region = generic::decode(&header, scratch_buffers)?; - page_state.store_region(seg.header.segment_number, region.bitmap); + page_state.store_region(seg.header.segment_number, region.bitmap)?; } SegmentType::PatternDictionary => { let header = pattern::parse(&mut reader)?; - let dictionary = pattern::decode(&header, scratch_buffers)?; - page_state.store_pattern_dictionary(seg.header.segment_number, dictionary); + charge_work(&mut work_left, header.work())?; + let dictionary = pattern::decode(&header, scratch_buffers, page_state.retained_room())?; + page_state.store_pattern_dictionary(seg.header.segment_number, dictionary)?; } SegmentType::SymbolDictionary => { // "1) Concatenate all the input symbol dictionaries to form SDINSYMS." // (6.5.5, step 1) // Collect references to avoid cloning; symbols are only cloned if re-exported. - let input_symbols: Vec<&Bitmap> = seg - .header - .referred_to_segments - .iter() - .filter_map(|&num| page_state.get_symbol_dictionary(num)) - .flat_map(|dict| dict.exported_symbols.iter()) - .collect(); + let input_symbols = page_state.referred_symbols(&seg.header.referred_to_segments)?; + charge_work(&mut work_left, input_symbols.len() as u64)?; // Collect Huffman tables from referred table segments. let referred_tables: Vec = seg @@ -354,18 +370,15 @@ fn decode_segments( &referred_tables, &page_state.standard_tables, retained_contexts, + page_state.retained_room(), + &mut work_left, )?; - page_state.store_symbol_dictionary(seg.header.segment_number, dictionary); + page_state.store_symbol_dictionary(seg.header.segment_number, dictionary)?; } SegmentType::ImmediateTextRegion | SegmentType::ImmediateLosslessTextRegion => { // Collect symbols from referred symbol dictionaries (SBSYMS). - let symbols: Vec<&Bitmap> = seg - .header - .referred_to_segments - .iter() - .filter_map(|&num| page_state.get_symbol_dictionary(num)) - .flat_map(|dict| dict.exported_symbols.iter()) - .collect(); + let symbols = page_state.referred_symbols(&seg.header.referred_to_segments)?; + charge_work(&mut work_left, symbols.len() as u64)?; // Collect Huffman tables from referred table segments. // "These user-supplied Huffman decoding tables may be supplied either @@ -379,6 +392,11 @@ fn decode_segments( .collect(); let header = text::parse(&mut reader, symbols.len() as u32)?; + charge_work(&mut work_left, bitmap::copy_work(header.region_info.width, header.region_info.height))?; + if header.flags.use_huffman { + // Its symbol ID table has a line for each symbol. + charge_work(&mut work_left, (symbols.len() as u64).saturating_mul(bitmap::HUFFMAN_LINE_WORK))?; + } if page_state.can_decode_directly( page_bitmap, @@ -392,6 +410,7 @@ fn decode_segments( &page_state.standard_tables, page_bitmap, scratch_buffers, + &mut work_left, )?; } else { let region = text::decode( @@ -400,6 +419,7 @@ fn decode_segments( &referred_tables, &page_state.standard_tables, scratch_buffers, + &mut work_left, )?; page_bitmap.combine( ®ion.bitmap, @@ -412,13 +432,8 @@ fn decode_segments( } SegmentType::IntermediateTextRegion => { // Collect symbols from referred symbol dictionaries (SBSYMS). - let symbols: Vec<&Bitmap> = seg - .header - .referred_to_segments - .iter() - .filter_map(|&num| page_state.get_symbol_dictionary(num)) - .flat_map(|dict| dict.exported_symbols.iter()) - .collect(); + let symbols = page_state.referred_symbols(&seg.header.referred_to_segments)?; + charge_work(&mut work_left, symbols.len() as u64)?; // Collect Huffman tables from referred table segments. let referred_tables: Vec = seg @@ -430,14 +445,20 @@ fn decode_segments( .collect(); let header = text::parse(&mut reader, symbols.len() as u32)?; + charge_work(&mut work_left, bitmap::copy_work(header.region_info.width, header.region_info.height))?; + if header.flags.use_huffman { + // Its symbol ID table has a line for each symbol. + charge_work(&mut work_left, (symbols.len() as u64).saturating_mul(bitmap::HUFFMAN_LINE_WORK))?; + } let region = text::decode( &header, &symbols, &referred_tables, &page_state.standard_tables, scratch_buffers, + &mut work_left, )?; - page_state.store_region(seg.header.segment_number, region.bitmap); + page_state.store_region(seg.header.segment_number, region.bitmap)?; } SegmentType::ImmediateHalftoneRegion | SegmentType::ImmediateLosslessHalftoneRegion => { let pattern_dict = seg @@ -448,6 +469,7 @@ fn decode_segments( .ok_or(SegmentError::MissingPatternDictionary)?; let header = halftone::parse(&mut reader)?; + charge_work(&mut work_left, halftone::work(&header, pattern_dict))?; if page_state.can_decode_directly( page_bitmap, @@ -475,8 +497,9 @@ fn decode_segments( .ok_or(SegmentError::MissingPatternDictionary)?; let header = halftone::parse(&mut reader)?; + charge_work(&mut work_left, halftone::work(&header, pattern_dict))?; let region = halftone::decode(&header, pattern_dict, scratch_buffers)?; - page_state.store_region(seg.header.segment_number, region.bitmap); + page_state.store_region(seg.header.segment_number, region.bitmap)?; } SegmentType::IntermediateGenericRefinementRegion => { // Same logic as immediate refinement, but store result instead of combining. @@ -488,8 +511,9 @@ fn decode_segments( .unwrap_or(page_bitmap); let header = generic_refinement::parse(&mut reader)?; + charge_work(&mut work_left, region_work(&header.region_info))?; let region = generic_refinement::decode(&header, reference, scratch_buffers)?; - page_state.store_region(seg.header.segment_number, region.bitmap); + page_state.store_region(seg.header.segment_number, region.bitmap)?; } SegmentType::ImmediateGenericRefinementRegion | SegmentType::ImmediateLosslessGenericRefinementRegion => { @@ -505,6 +529,7 @@ fn decode_segments( .and_then(|&num| page_state.get_referred_segment(num)); let header = generic_refinement::parse(&mut reader)?; + charge_work(&mut work_left, region_work(&header.region_info))?; if let Some(referred_segment) = referred_segment && page_state.can_decode_directly(page_bitmap, &header.region_info, false) @@ -532,7 +557,7 @@ fn decode_segments( // "This segment contains data which defines one or more user-supplied // Huffman coding tables." (7.4.13) let table = HuffmanTable::read_custom(&mut reader)?; - page_state.store_huffman_table(seg.header.segment_number, table); + page_state.store_huffman_table(seg.header.segment_number, table)?; } SegmentType::EndOfPage | SegmentType::EndOfFile => { break; @@ -545,6 +570,23 @@ fn decode_segments( Ok(()) } +/// PdfCraft patch: the words a stored item costs in its list, at least, apart from what it holds. +const ENTRY_WORDS: u64 = 32; + +/// PdfCraft patch: the most words a page's segments may hold for later ones, 64 MiB: two square +/// bitmaps of 16,384 a side (`bitmap::MAX_PIXELS`). Each stored item is bounded on its own, but without a +/// bound on all of them a stream of a few dozen bytes a segment held 33 MB each (a 16,384-sample +/// square region of typical rows), and a few kilobytes could exhaust memory. A segment being +/// decoded takes more for a while, within its own bounds: the page bitmap isn't counted, nor is +/// what a region or a grid uses until it is stored or freed. +const MAX_RETAINED_WORDS: u64 = + 2 * (bitmap::MAX_PIXELS / bitmap::WORD_BITS as u64 + bitmap::BITMAP_WORDS as u64 + ENTRY_WORDS); + +/// PdfCraft patch: the most symbols one segment may refer to, 2^20, far beyond the 65,535 a +/// dictionary may export. References may repeat, so each names the same symbols again: without +/// this, 65,535 references to one dictionary in a 78 KB stream collected 17 GB of pointers. +const MAX_REFERRED_SYMBOLS: u64 = 1 << 20; + /// Page-level decoding state for a JBIG2 page. #[derive(Default)] pub(crate) struct PageState { @@ -561,6 +603,9 @@ pub(crate) struct PageState { /// Decoded Huffman tables from table segments, stored as (`segment_number`, table) pairs. /// "Tables – see 7.4.13." (type 53) pub(crate) huffman_tables: Vec<(u32, HuffmanTable)>, + /// PdfCraft patch: the words the regions, dictionaries and tables above hold, at most + /// [`MAX_RETAINED_WORDS`]. + pub(crate) retained_words: u64, /// Standard Huffman tables (`TABLE_A` through `TABLE_O`). pub(crate) standard_tables: StandardHuffmanTables, } @@ -573,6 +618,7 @@ impl PageState { self.pattern_dictionaries.clear(); self.symbol_dictionaries.clear(); self.huffman_tables.clear(); + self.retained_words = 0; // Standard tables are lazily built and reused across images. } @@ -610,9 +656,26 @@ impl PageState { } } + /// PdfCraft patch: the words the page may still hold for later segments. + fn retained_room(&self) -> u64 { + MAX_RETAINED_WORDS.saturating_sub(self.retained_words) + } + + /// PdfCraft patch: count a stored item holding `words` for later segments, refusing past + /// [`MAX_RETAINED_WORDS`]. + fn retain(&mut self, words: usize) -> Result<()> { + self.retained_words = self.retained_words.saturating_add(words as u64).saturating_add(ENTRY_WORDS); + if self.retained_words > MAX_RETAINED_WORDS { + bail!(OverflowError::RetainedBitmaps); + } + Ok(()) + } + /// Store a decoded region for later reference. - fn store_region(&mut self, segment_number: u32, region: Bitmap) { + fn store_region(&mut self, segment_number: u32, region: Bitmap) -> Result<()> { + self.retain(region.words())?; self.referred_segments.push((segment_number, region)); + Ok(()) } /// Look up a referred segment by number. @@ -624,8 +687,10 @@ impl PageState { } /// Store a decoded pattern dictionary for later reference. - fn store_pattern_dictionary(&mut self, segment_number: u32, dictionary: PatternDictionary) { + fn store_pattern_dictionary(&mut self, segment_number: u32, dictionary: PatternDictionary) -> Result<()> { + self.retain(dictionary.words())?; self.pattern_dictionaries.push((segment_number, dictionary)); + Ok(()) } /// Look up a pattern dictionary by segment number. @@ -637,8 +702,23 @@ impl PageState { } /// Store a decoded symbol dictionary for later reference. - fn store_symbol_dictionary(&mut self, segment_number: u32, dictionary: SymbolDictionary) { + fn store_symbol_dictionary(&mut self, segment_number: u32, dictionary: SymbolDictionary) -> Result<()> { + self.retain(dictionary.words())?; self.symbol_dictionaries.push((segment_number, dictionary)); + Ok(()) + } + + /// PdfCraft patch: the symbols of the dictionaries in `segments`, in order, refusing past + /// [`MAX_REFERRED_SYMBOLS`] before anything is collected. + fn referred_symbols(&self, segments: &[u32]) -> Result> { + let dictionaries = || segments.iter().filter_map(|&num| self.get_symbol_dictionary(num)); + let count = dictionaries() + .map(|dictionary| dictionary.exported_symbols.len() as u64) + .fold(0, u64::saturating_add); + if count > MAX_REFERRED_SYMBOLS { + bail!(OverflowError::SymbolCount); + } + Ok(dictionaries().flat_map(|dictionary| dictionary.exported_symbols.iter()).collect()) } /// Look up a symbol dictionary by segment number. @@ -650,8 +730,10 @@ impl PageState { } /// Store a decoded Huffman table for later reference. - fn store_huffman_table(&mut self, segment_number: u32, table: HuffmanTable) { + fn store_huffman_table(&mut self, segment_number: u32, table: HuffmanTable) -> Result<()> { + self.retain(table.words())?; self.huffman_tables.push((segment_number, table)); + Ok(()) } /// Look up a Huffman table by segment number. @@ -690,3 +772,51 @@ fn init_page( Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + /// A page past the bounds is refused when the stream is opened, since callers size their + /// output from it before decoding starts. 16,384 squared is exactly the bound on pixels. + #[test] + fn a_page_past_the_bounds_is_refused_when_opened() { + let stream = |width: u32, height: u32| { + let mut stream = vec![0, 0, 0, 0, 48, 0, 1, 0, 0, 0, 19]; + stream.extend_from_slice(&width.to_be_bytes()); + stream.extend_from_slice(&height.to_be_bytes()); + stream.extend_from_slice(&[0; 11]); // resolution, flags, striping + stream + }; + assert!(Image::new_embedded(&stream(16384, 16384), None).is_ok()); + assert!(Image::new_embedded(&stream(16385, 16384), None).is_err()); + assert!(Image::new_embedded(&stream(65536, 1), None).is_err(), "too wide, though few pixels"); + assert!(Image::new_embedded(&stream(1 << 20, 1 << 20), None).is_err()); + } + + /// A stream and its globals are one stream of segments, so they have one count. + #[test] + fn the_segments_of_a_stream_and_its_globals_count_together() { + let segment: &[u8] = &[0, 0, 0, 0, 62, 0, 1, 0, 0, 0, 0]; // an empty extension + let half = segment.repeat(1 << 19); + let more = segment.repeat((1 << 19) + 1); + let refused = |data: &[u8]| { + let error = Image::new_embedded(data, Some(&half)).err().map(|e| e.to_string()); + error.as_deref() == Some("too many segments or references in a stream") + }; + assert!(!refused(&half), "2^20 segments are allowed"); + assert!(refused(&more), "one more isn't"); + } + + /// A dictionary being decoded has what the page may still hold, not all of it: refused only + /// once it is stored, its copies would already have been made. + #[test] + fn the_room_left_shrinks_as_segments_are_stored() { + let mut page = PageState::default(); + assert_eq!(page.retained_room(), MAX_RETAINED_WORDS); + page.retain(1000).unwrap(); + assert_eq!(page.retained_room(), MAX_RETAINED_WORDS - 1000 - ENTRY_WORDS); + page.retained_words = MAX_RETAINED_WORDS; + assert_eq!(page.retained_room(), 0); + } +} diff --git a/vendor/hayro-jbig2/src/pdfcraft_tests.rs b/vendor/hayro-jbig2/src/pdfcraft_tests.rs new file mode 100644 index 000000000..e26d4e9cf --- /dev/null +++ b/vendor/hayro-jbig2/src/pdfcraft_tests.rs @@ -0,0 +1,522 @@ +//! Hostile-input tests for the work, memory and overflow bounds: ported from PdfCraft's +//! raster tests, which draw the same streams through a PDF. Here a stream "draws" when it decodes. + +use crate::{Decoder, Image}; + +#[derive(Default)] +struct Count { + black: u64, +} + +impl Decoder for Count { + fn push_pixel(&mut self, black: bool) { + self.black += u64::from(black); + } + + fn push_pixel_chunk(&mut self, black: bool, chunk_count: u32) { + self.black += u64::from(black) * 8 * u64::from(chunk_count); + } + + fn next_line(&mut self) {} +} + +/// The black pixels of an embedded stream; none when it is refused. +fn black_pixels(stream: &[u8]) -> u64 { + let mut count = Count::default(); + match Image::new_embedded(stream, None).and_then(|image| image.decode(&mut count)) { + Ok(()) => count.black, + Err(_) => 0, + } +} + +/// Whether a 16 x 2 stream whose page defaults to black decodes: refused, nothing is drawn. +fn jbig2_image_draws(stream: &[u8]) -> bool { + black_pixels(stream) > 0 +} + +/// Bits written from the top of each byte, as JBIG2's Huffman-coded data is. +#[derive(Default)] +struct Bits { + bytes: Vec, + used: u32, +} + +impl Bits { + fn put(&mut self, value: u32, count: u32) { + for i in (0..count).rev() { + if self.used.is_multiple_of(8) { + self.bytes.push(0); + } + if (value >> i) & 1 != 0 { + *self.bytes.last_mut().unwrap() |= 0x80 >> (self.used % 8); + } + self.used += 1; + } + } + + fn align(&mut self) { + self.used = self.used.div_ceil(8) * 8; + } + + /// `value` in table B.1 (ISO 14492 Annex B). + fn table_b1(&mut self, value: u32) { + match value { + 0..=15 => { + self.put(0, 1); + self.put(value, 4); + } + 16..=271 => { + self.put(0b10, 2); + self.put(value - 16, 8); + } + 272..=65807 => { + self.put(0b110, 3); + self.put(value - 272, 16); + } + _ => { + self.put(0b111, 3); + self.put(value - 65808, 32); + } + } + } +} + +/// One embedded JBIG2 segment (ISO 14492 §7.2) of page 1. More than four references take the long +/// form, whose retention flags are left clear. +fn jbig2_segment(out: &mut Vec, number: u32, kind: u8, refs: &[u8], data: &[u8]) { + out.extend_from_slice(&number.to_be_bytes()); + out.push(kind); + if refs.len() <= 4 { + out.push(u8::try_from(refs.len()).unwrap() << 5); + } else { + out.extend_from_slice(&(0xE000_0000u32 | u32::try_from(refs.len()).unwrap()).to_be_bytes()); + out.extend(std::iter::repeat_n(0u8, (refs.len() + 1).div_ceil(8))); + } + out.extend_from_slice(refs); + out.push(1); + out.extend_from_slice(&u32::try_from(data.len()).unwrap().to_be_bytes()); + out.extend_from_slice(data); +} + +/// The page information of a 16 × 2 JBIG2 page whose default pixel is black, so that the image, if +/// it decodes, draws black whatever its other segments hold. +fn black_jbig2_page() -> Vec { + let mut page = Vec::new(); + page.extend_from_slice(&16u32.to_be_bytes()); + page.extend_from_slice(&2u32.to_be_bytes()); + page.extend_from_slice(&[0; 8]); // resolution + page.extend_from_slice(&[4, 0, 0]); // flags (default pixel black), striping + let mut stream = Vec::new(); + jbig2_segment(&mut stream, 0, 48, &[], &page); + stream +} + +/// A region's information field, at the page's corner. +fn jbig2_region_info(width: u32, height: u32) -> Vec { + let mut region = Vec::new(); + region.extend_from_slice(&width.to_be_bytes()); + region.extend_from_slice(&height.to_be_bytes()); + region.extend_from_slice(&[0; 9]); // x, y, combination operator + region +} + +/// An intermediate generic region, white, every row typical: it decodes in a few bytes however +/// large it is. +fn jbig2_white_region(width: u32, height: u32) -> Vec { + let mut region = jbig2_region_info(width, height); + region.push(0x08); // arithmetic coding, template 0, typical prediction + region.extend_from_slice(&[3, 0xff, 0xfd, 0xff, 2, 0xfe, 0xfe, 0xfe]); // AT pixels + // Every row typical, the first white: pdfcraft-filters' coding of a white 16,384 square. + region.extend_from_slice(&[0xb6, 0xc2, 0x1f, 0xff, 0x7f, 0xff, 0xac]); + region +} + +/// A pattern dictionary of `patterns` patterns of width 1 and `height` rows, with nothing coded. +fn jbig2_pattern_dictionary(height: u8, patterns: u32) -> Vec { + let mut dictionary = vec![0, 1, height]; + dictionary.extend_from_slice(&(patterns - 1).to_be_bytes()); + dictionary +} + +/// A 1 × 1 halftone region over a grid of `width` × `height` cells, all at the region's corner, +/// coded by MMR or arithmetically. +fn jbig2_halftone(width: u32, height: u32, mmr: bool) -> Vec { + jbig2_halftone_of(1, width, height, mmr) +} + +/// [`jbig2_halftone`] for a square region of `side` pixels. +fn jbig2_halftone_of(side: u32, width: u32, height: u32, mmr: bool) -> Vec { + let mut halftone = jbig2_region_info(side, side); + halftone.push(u8::from(mmr)); // template 0 + halftone.extend_from_slice(&width.to_be_bytes()); + halftone.extend_from_slice(&height.to_be_bytes()); + halftone.extend_from_slice(&[0; 12]); // grid offset and vector + halftone +} + +/// A Huffman-coded symbol dictionary of `symbols` symbols of width 0 and height 1, exported or not: +/// each holds no pixels, and costs a bitmap's header all the same. +fn zero_width_jbig2_dictionary(symbols: u32, exported: bool) -> Vec { + let mut dictionary = vec![0x00, 0x01]; // Huffman, tables B.4, B.2, B.1, B.1 + dictionary.extend_from_slice(&(if exported { symbols } else { 0 }).to_be_bytes()); + dictionary.extend_from_slice(&symbols.to_be_bytes()); // new + let mut bits = Bits::default(); + bits.put(0, 1); // height class delta 1 (B.4) + for _ in 0..symbols { + bits.put(0, 1); // width delta 0 (B.2) + } + bits.put(0b111111, 6); // end of the class (B.2) + bits.put(0, 5); // BMSIZE 0, uncompressed (B.1) + bits.align(); + if exported { + bits.table_b1(0); // the first export run isn't exported + } + bits.table_b1(symbols); + dictionary.extend_from_slice(&bits.bytes); + dictionary +} + +/// A Huffman-coded symbol dictionary of one white symbol, `side` from 76 up, exported or not. +fn square_jbig2_dictionary(side: u32, exported: bool) -> Vec { + let mut bits = Bits::default(); + bits.put(0b11111, 5); // height class delta, the upper line of table B.4 + bits.put(side - 76, 32); + bits.put(0b111110, 6); // width delta, the upper line of table B.2 + bits.put(side - 75, 32); + bits.put(0b111111, 6); // end of the class (B.2) + let bytes = side.div_ceil(8) + 1; // an MMR-coded white bitmap: one bit a row + bits.table_b1(bytes); // BMSIZE (B.1) + bits.align(); + for _ in 0..bytes { + bits.put(0xFF, 8); + } + if exported { + bits.table_b1(0); + } + bits.table_b1(1); + let mut dictionary = vec![0x00, 0x01]; // Huffman, tables B.4, B.2, B.1, B.1 + dictionary.extend_from_slice(&u32::from(exported).to_be_bytes()); + dictionary.extend_from_slice(&1u32.to_be_bytes()); // new + dictionary.extend_from_slice(&bits.bytes); + dictionary +} + +/// A text region of `width` × `height` placing symbol 0 of one, Huffman-coded, `instances` of 0 or 1. +fn jbig2_text_region(width: u32, height: u32, instances: u32) -> Vec { + let mut text = jbig2_region_info(width, height); + text.extend_from_slice(&[0x00, 0x11, 0, 0]); // Huffman, top left; tables B.6, B.8, B.11 + text.extend_from_slice(&instances.to_be_bytes()); + let mut bits = Bits::default(); + for code in 0..35 { + bits.put(u32::from(code == 1), 4); // only run code 1, one bit long + } + bits.put(0, 1); // symbol 0 is one bit long + bits.align(); + bits.put(0, 1); // the first strip's T (B.11) + if instances > 0 { + bits.put(0, 1); // delta T 1 (B.11) + bits.put(0, 9); // first S 0 (B.6) + bits.put(0, 1); // symbol 0 + bits.put(0b01, 2); // end of the strip (B.8) + } + text.extend_from_slice(&bits.bytes); + text +} + +/// A 1 × 1 text region placing one instance of symbol 0 of one, refined, Huffman-coded. +fn jbig2_refined_text_region() -> Vec { + let mut text = jbig2_region_info(1, 1); + text.extend_from_slice(&[0x80, 0x13, 0, 0]); // Huffman, refined by template 1, top left + text.extend_from_slice(&1u32.to_be_bytes()); + let mut bits = Bits::default(); + for code in 0..35 { + bits.put(u32::from(code == 1), 4); // only run code 1, one bit long + } + bits.put(0, 1); // symbol 0 is one bit long + bits.align(); + bits.put(0, 1); // the first strip's T (B.11) + bits.put(0, 1); // delta T 1 (B.11) + bits.put(0, 9); // first S 0 (B.6) + bits.put(0, 1); // symbol 0 + bits.put(1, 1); // refined + bits.put(0, 4); // no change of width, height or offset (B.14) + bits.table_b1(0); // no refinement data (B.1) + bits.align(); + bits.put(0b01, 2); // end of the strip (B.8) + text.extend_from_slice(&bits.bytes); + text +} + +/// A generic refinement region of 1 × 1, with whatever its data decodes to. +fn jbig2_refinement_region() -> Vec { + let mut region = jbig2_region_info(1, 1); + region.push(1); // template 1, no typical prediction + region.extend_from_slice(&[0; 4]); + region +} + +/// Each item a JBIG2 stream decodes costs its work, and the stream may ask for 2^30: four of the +/// largest pages, some 6 s to 17 s. Each bitmap is bounded on its own, but nothing bounded how many +/// there were, so 1,140 bytes of generic regions took over 40 s, and a 795-byte PDF's one halftone +/// 265 s. Vendored hayro-jbig2 patch: `MAX_WORK`. A row costs 4 pixels, even in a bitmap of no +/// width, and a copy goes by the word and the row, so a tall thin one costs its rows. Here regions +/// of typical rows, cheap to decode though charged like any, spend all but exactly what the item +/// costs, so it draws with that left and is refused with one less, for each item at its own cost. +#[test] +fn jbig2_decoding_work_is_counted_item_by_item() { + struct Item { + what: &'static str, + /// What comes first, and what it costs. + before: Vec<(u8, Vec)>, + spent: u64, + kind: u8, + refs: &'static [u8], + data: Vec, + cost: u64, + } + let stream = |item: &Item, left: u64| { + let mut stream = black_jbig2_page(); + let mut number = 0; + for (kind, data) in &item.before { + number += 1; + jbig2_segment(&mut stream, number, *kind, &[], data); + } + // A square costs 16,384 rows of 16,388, and a single row what it has and 4 more. + let work = (1u64 << 30) - item.spent - left; + let (row, square) = (16388u64, 16384 * 16388u64); + let mut squares = work / square; + let mut rest = work % square; + if rest > 0 && rest < 5 { + squares -= 1; + rest += square; + } + let mut rows = rest / row; + let mut last = rest % row; + if last > 0 && last < 5 { + rows -= 1; + last += row; + } + let regions = std::iter::repeat_n((16384, 16384), usize::try_from(squares).unwrap()) + .chain((rows > 0).then_some((16384, rows))) + .chain((last > 0).then_some((last - 4, 1))); + for (width, height) in regions { + number += 1; + let region = jbig2_white_region(u32::try_from(width).unwrap(), u32::try_from(height).unwrap()); + jbig2_segment(&mut stream, number, 38, &[], ®ion); // immediate generic region + } + jbig2_segment(&mut stream, number + 1, item.kind, item.refs, &item.data); + stream + }; + let symbol = || vec![(0, zero_width_jbig2_dictionary(1, true))]; + let patterns = || vec![(16, jbig2_pattern_dictionary(1, 2))]; + let exporting_none = { + let mut dictionary = vec![0x00, 0x01]; + dictionary.extend_from_slice(&[0; 8]); // exported, new + let mut bits = Bits::default(); + bits.table_b1(1); // the first export run, not exported, is the one it refers to + dictionary.extend_from_slice(&bits.bytes); + dictionary + }; + let zero = |height: u32| { + let mut bits = Bits::default(); + bits.put(0b11111, 5); // height class delta, the upper line of table B.4 + bits.put(height - 76, 32); + bits.put(0, 1); // width delta 0 (B.2) + bits.put(0b111111, 6); // end of the class (B.2) + bits.table_b1(0); // BMSIZE 0, stored (B.1) + bits.align(); + bits.table_b1(1); // the first export run, not exported, is the symbol + let mut dictionary = vec![0x00, 0x01]; + dictionary.extend_from_slice(&0u32.to_be_bytes()); // exported + dictionary.extend_from_slice(&1u32.to_be_bytes()); // new + dictionary.extend_from_slice(&bits.bytes); + dictionary + }; + // The cost of a pattern dictionary of two patterns: rows of them side by side, then each taken out. + let two_patterns = |width: u64, height: u64| height * (2 * width + 4) + 2 * height * (width.div_ceil(32) + 1); + let thin_region = |side: u32| { + let mut region = jbig2_white_region(1, 1); + region.splice(0..8, [0u32.to_be_bytes(), side.to_be_bytes()].concat()); + region + }; + let items = vec![ + Item { what: "immediate generic", before: vec![], spent: 0, kind: 38, refs: &[], data: jbig2_white_region(1, 1), cost: 5 }, + Item { what: "intermediate generic", before: vec![], spent: 0, kind: 36, refs: &[], data: jbig2_white_region(1, 1), cost: 5 }, + Item { what: "a region of no width", before: vec![], spent: 0, kind: 38, refs: &[], data: thin_region(1000), cost: 4000 }, + Item { what: "immediate refinement", before: vec![], spent: 0, kind: 42, refs: &[], data: jbig2_refinement_region(), cost: 5 }, + Item { what: "intermediate refinement", before: vec![], spent: 0, kind: 40, refs: &[], data: jbig2_refinement_region(), cost: 5 }, + Item { + what: "pattern dictionary", + before: vec![], + spent: 0, + kind: 16, + refs: &[], + data: jbig2_pattern_dictionary(1, 2), + cost: two_patterns(1, 1), + }, + Item { + what: "tall patterns", + before: vec![], + spent: 0, + kind: 16, + refs: &[], + data: jbig2_pattern_dictionary(32, 2), + cost: two_patterns(1, 32), + }, + // Rows cost in a symbol of no pixels too. + Item { what: "symbols without pixels", before: vec![], spent: 0, kind: 0, refs: &[], data: zero_width_jbig2_dictionary(3, false), cost: 12 }, + Item { what: "a tall symbol of no width", before: vec![], spent: 0, kind: 0, refs: &[], data: zero(1000), cost: 4000 }, + Item { what: "new symbols", before: vec![], spent: 0, kind: 0, refs: &[], data: square_jbig2_dictionary(80, false), cost: 6720 }, + Item { what: "referred symbols", before: symbol(), spent: 4, kind: 0, refs: &[1], data: exporting_none, cost: 1 }, + // Its symbol, and its one row and word; Huffman-coded, a table line for the symbol. + Item { what: "immediate text", before: symbol(), spent: 4, kind: 6, refs: &[1], data: jbig2_text_region(1, 1, 0), cost: 13 }, + Item { what: "intermediate text", before: symbol(), spent: 4, kind: 4, refs: &[1], data: jbig2_text_region(1, 1, 0), cost: 13 }, + Item { what: "a text region's pixels", before: symbol(), spent: 4, kind: 6, refs: &[1], data: jbig2_text_region(32, 32, 0), cost: 75 }, + Item { + what: "an intermediate text region's pixels", + before: symbol(), + spent: 4, + kind: 4, + refs: &[1], + data: jbig2_text_region(32, 32, 0), + cost: 75, + }, + // The region's 13, a copy of the symbol's row and the round of the loop. + Item { what: "an instance", before: symbol(), spent: 4, kind: 6, refs: &[1], data: jbig2_text_region(1, 1, 1), cost: 15 }, + // The region's 13, 80 rows of 4 words copied, the round of the loop. + Item { + what: "an instance of a large symbol", + before: vec![(0, square_jbig2_dictionary(80, true))], + spent: 6720, + kind: 6, + refs: &[1], + data: jbig2_text_region(1, 1, 1), + cost: 334, + }, + // And decoded as well as copied. + Item { + what: "a refined instance", + before: vec![(0, square_jbig2_dictionary(80, true))], + spent: 6720, + kind: 6, + refs: &[1], + data: jbig2_refined_text_region(), + cost: 7054, + }, + // A bit plane, a cell's copy and the region's. + Item { + what: "immediate halftone", + before: patterns(), + spent: two_patterns(1, 1), + kind: 22, + refs: &[1], + data: jbig2_halftone(1, 1, false), + cost: 9, + }, + Item { + what: "intermediate halftone", + before: patterns(), + spent: two_patterns(1, 1), + kind: 20, + refs: &[1], + data: jbig2_halftone(1, 1, false), + cost: 9, + }, + // A plane of 2 cells, each a copy of 32 rows of 2 words' cost, and the region's. + Item { + what: "a halftone's cells", + before: vec![(16, jbig2_pattern_dictionary(32, 2))], + spent: two_patterns(1, 32), + kind: 22, + refs: &[1], + data: jbig2_halftone(2, 1, false), + cost: 136, + }, + Item { + what: "a grid of no width", + before: patterns(), + spent: two_patterns(1, 1), + kind: 22, + refs: &[1], + data: jbig2_halftone(0, 1000, false), + cost: 4002, + }, + Item { + what: "a halftone's pixels", + before: patterns(), + spent: two_patterns(1, 1), + kind: 22, + refs: &[1], + data: jbig2_halftone_of(32, 1, 1, false), + cost: 71, + }, + ]; + for item in &items { + assert!(jbig2_image_draws(&stream(item, item.cost)), "{}: with its cost left it draws", item.what); + assert!(!jbig2_image_draws(&stream(item, item.cost - 1)), "{}: with one less it's refused", item.what); + } +} + +/// A JBIG2 stream can put a region, a reference or a symbol at the very end of what an `i32` holds, +/// where adding to it overflowed: a panic wherever overflow is checked, a build's own tests included. +/// A region or a symbol out there is off the page, so nothing draws. Vendored hayro-jbig2 patch: the arithmetic wraps, as +/// a release build's did, or in `Bitmap::combine` saturates, which comes to the same. Three of the text +/// streams are arithmetic-coded: one symbol on a strip of -2^31 + 1, with its bottom left as the corner +/// placed, one on a strip of -2^31, and one refined by an offset of `i32::MIN`, which reads nothing of +/// the old symbol and still draws. Two are Huffman-coded, each with its first S at an offset of +/// 0x7FFFFFFF on a table's 32-bit range line, past what an `i32` holds, and are refused further on. +#[test] +fn jbig2_placements_at_the_end_of_an_i32_draw_nothing() { + const FAR_CORNER: &str = "0000000030000100000013000000100000000c0000000000000000420000000000010000010000001e000003fffdff02fefefe00000001000000014fdc3ce0205df55c36bfffac00000002062001010000001f000000100000000c000000000000000004000000000001969747cbe88bffac"; + const STRIP: &str = "0000000030000100000013000000100000000c0000000000000000420000000000010000010000001e000003fffdff02fefefe00000001000000014fdda25e46956d3e0d3fffac00000002062001010000001f000000100000000c000000000000000004001000000001969747cbe5dbffac"; + const UPPER_LINE: &str = "0000000030000100000013000000100000000c0000000000000000420000000000010000010000001d000003fffdff02fefefe00000001000000014fdaea38c99641e0c8ffac000000020620010100000038000000100000000c00000000000000000400110000000000010100000000000000000000000000000000003f7fffffff0000000000000000"; + const LOWER_LINE: &str = "0000000030000100000013000000100000000c0000000000000000420000000000010000010000001d000003fffdff02fefefe00000001000000014fdaea38c99641e0c8ffac000000020620010100000038000000100000000c00000000000000000400110000000000010100000000000000000000000000000000003e7fffffff0000000000000000"; + const OFFSET: &str = "0000000030000100000013000000100000000c0000000000000000420000000000010000010000001e000003fffdff02fefefe00000001000000014fdda9172f614a037ddfffac0000000206200101000000260000000800000006000000000000000004801200000001a9695690b3e5f44a2f105e30d3ffac"; + let segment = |number: u32, kind: u8, data: &[u8]| { + let mut out = number.to_be_bytes().to_vec(); + out.extend_from_slice(&[kind, 0, 1]); + out.extend_from_slice(&u32::try_from(data.len()).unwrap().to_be_bytes()); + out.extend_from_slice(data); + out + }; + let side = 8u32.to_be_bytes(); + let page = |width: u32| { + let mut page = [width.to_be_bytes(), side].concat(); + page.extend_from_slice(&[0; 8]); // resolution + page.extend_from_slice(&[0, 0, 0]); // flags, striping + segment(0, 48, &page) + }; + let region = |width: u32, kind: u8, x: u32, y: u32, header: &[u8]| { + let mut region = [width.to_be_bytes(), side, x.to_be_bytes(), y.to_be_bytes()].concat(); + region.push(0); // combination operator + region.extend_from_slice(header); + region.extend_from_slice(&[0x5a; 64]); + [page(width), segment(1, kind, ®ion)].concat() + }; + let generic = |x, y| region(8, 38, x, y, &[0, 3, 0xff, 0xfd, 0xff, 2, 0xfe, 0xfe, 0xfe]); // template 0, AT pixels + // Immediate refinements of the page: template 0 with its usual AT pixels, with others, with typical + // prediction, and template 1. + let refined = |width, x, y, form: usize| { + region(width, 42, x, y, [&[0, 0xff, 0xff, 0xff, 0xff][..], &[0, 0xff, 0xff, 127, 127], &[2, 0xff, 0xff, 0xff, 0xff], &[1]][form]) + }; + let hex = |text: &str| (0..text.len() / 2).map(|i| u8::from_str_radix(&text[i * 2..i * 2 + 2], 16).unwrap()).collect::>(); + let ink = |jbig2: Vec, _width: u32, _height: u32| black_pixels(&jbig2); + assert!(ink(generic(2, 0), 8, 8) > 0, "a region on the page draws"); + for form in 0..4 { + assert!(ink(refined(8, 0, 0, form), 8, 8) > 0, "a refinement on the page draws: {form}"); + } + let end = 0x7FFF_FFFFu32; + for (x, y) in [(end, 0), (0, end), (end - 7, 0), (0, end - 7), (end - 129, 0), (0, end - 129)] { + assert_eq!(ink(generic(x, y), 8, 8), 0, "generic at {x:#x}, {y:#x}"); + for form in 0..4 { + assert_eq!(ink(refined(8, x, y, form), 8, 8), 0, "refinement {form} at {x:#x}, {y:#x}"); + } + } + // Past a 32-bit word of the row, where the reference is read again. + assert_eq!(ink(refined(64, 0, end, 0), 64, 8), 0, "a wide refinement"); + assert_eq!(ink(hex(FAR_CORNER), 16, 12), 0, "a symbol's far side past the end"); + assert_eq!(ink(hex(STRIP), 16, 12), 0, "a strip of -2^31"); + assert!(ink(hex(OFFSET), 16, 12) > 0, "a symbol refined by an offset of i32::MIN"); + assert_eq!(ink(hex(UPPER_LINE), 16, 12), 0, "an upper range line past an i32"); + assert_eq!(ink(hex(LOWER_LINE), 16, 12), 0, "a lower range line past an i32"); +} From f3944ba991788b44056b6a4fea1284e50a451bcd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:50:38 -0500 Subject: [PATCH 026/120] Add craft-vcard: vCard reader and writer from MailCraft (#942) * craft-mime: strict base64, mailto links and code page labels from MailCraft What MailCraft's mime and charset crates still had that craft-mime did not: strict RFC 4648 base64 (crates/charset), mailto: links (RFC 6068, crates/mime) and the Windows code page to charset label table (crates/pst). With these, MailCraft's charset crate has nothing craft-mime lacks. * Add craft-vcard: vCard reader and writer from MailCraft MailCraft crates/vcard at a44a450: vCard 2.1, 3.0 and 4.0 read, 3.0 written, with its limits and tests. The model is the crate's own Contact (the vCard fields of MailCraft's); the birthday is a chrono::NaiveDate. Character sets and strict base64 come from craft-mime. --------- Co-authored-by: Martin Ball <98639962+benandez@users.noreply.github.com> --- Cargo.lock | 9 + Cargo.toml | 1 + README.md | 5 + crates/vcard/Cargo.toml | 19 + crates/vcard/README.md | 48 +++ crates/vcard/src/key.rs | 294 +++++++++++++ crates/vcard/src/key_tests.rs | 417 ++++++++++++++++++ crates/vcard/src/lib.rs | 26 ++ crates/vcard/src/lines.rs | 429 +++++++++++++++++++ crates/vcard/src/model.rs | 127 ++++++ crates/vcard/src/read.rs | 778 ++++++++++++++++++++++++++++++++++ crates/vcard/src/tests.rs | 391 +++++++++++++++++ crates/vcard/src/text.rs | 239 +++++++++++ crates/vcard/src/write.rs | 326 ++++++++++++++ 14 files changed, 3109 insertions(+) create mode 100644 crates/vcard/Cargo.toml create mode 100644 crates/vcard/README.md create mode 100644 crates/vcard/src/key.rs create mode 100644 crates/vcard/src/key_tests.rs create mode 100644 crates/vcard/src/lib.rs create mode 100644 crates/vcard/src/lines.rs create mode 100644 crates/vcard/src/model.rs create mode 100644 crates/vcard/src/read.rs create mode 100644 crates/vcard/src/tests.rs create mode 100644 crates/vcard/src/text.rs create mode 100644 crates/vcard/src/write.rs diff --git a/Cargo.lock b/Cargo.lock index 21909a8af..9ab6b0dd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1633,6 +1633,15 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-vcard" +version = "0.1.0" +dependencies = [ + "chrono", + "craft-mime", + "proptest", +] + [[package]] name = "craft-vectorcraft-format" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 65f501457..9adfeca74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -92,6 +92,7 @@ craft-timeline = { path = "crates/timeline" } craft-tiff = { path = "crates/tiff" } craft-ui = { path = "crates/ui" } craft-update = { path = "crates/update" } +craft-vcard = { path = "crates/vcard" } craft-vectorcraft-format = { path = "crates/vectorcraft-format" } craft-vectordrawable = { path = "crates/vectordrawable" } craft-xls = { path = "crates/xls" } diff --git a/README.md b/README.md index ab45c1a99..58d0758db 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,11 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " Option { + match name.trim().to_ascii_lowercase().as_str() { + "x509" | "x.509" | "x-x509" | "application/pkix-cert" | "application/x-x509-ca-cert" | "application/x-x509-user-cert" => Some(KeyKind::X509), + "pgp" | "x-pgp" | "gpg" | "openpgp" | "application/pgp-keys" | "application/pgp" | "application/x-pgp-keys" => Some(KeyKind::OpenPgp), + _ => None, + } +} + +/// The key of one `KEY` line; `None` for one that holds nothing kept (junk, secret key material, +/// a label the bytes don't bear out, a key too large, an address of another scheme). +pub(crate) fn read(line: &ContentLine) -> Option { + if line.value.len() > MAX_VALUE { + return None; + } + let mut stated = line.types().iter().chain(line.param("MEDIATYPE").map(str::to_string).iter()).find_map(|t| kind_named(t)); + let bytes = match line.param("ENCODING").unwrap_or("").to_ascii_uppercase().as_str() { + "B" | "BASE64" => base64_bytes(&line.value), + "QUOTED-PRINTABLE" | "Q" => quoted_printable(&line.value), + _ => { + let value = unescape(&line.value); + let value = value.trim(); + if starts_with_ci(value, "data:") { + let (named, bytes) = data_uri(value.get(5..).unwrap_or(""))?; + stated = named.or(stated); + bytes + } else if value.starts_with("-----BEGIN") { + value.as_bytes().to_vec() + } else if line.param("VALUE").is_some_and(|v| v.eq_ignore_ascii_case("text")) { + // Text that is neither armour nor PEM is no key (a fingerprint, a note); it is + // never taken for the bytes of one. + return None; + } else { + return address(value, stated); + } + } + }; + if bytes.len() > MAX_DECODED { + return None; + } + key_of(&bytes, stated) +} + +fn starts_with_ci(s: &str, start: &str) -> bool { + s.get(..start.len()).is_some_and(|head| head.eq_ignore_ascii_case(start)) +} + +/// An address of a key, with what the card says is there. +fn address(value: &str, stated: Option) -> Option { + let uri = checked_uri(value)?; + let media_type = match stated { + Some(KeyKind::X509) => ContactKey::X509_MEDIA_TYPE, + Some(KeyKind::OpenPgp) => ContactKey::OPENPGP_MEDIA_TYPE, + _ => "", + }; + Some(ContactKey { kind: KeyKind::Uri, uri, media_type: media_type.into(), ..Default::default() }) +} + +/// The key `bytes` hold, however they were spelled: a certificate as DER, an OpenPGP public key as +/// it came. `None` for anything else, for secret key material, for bytes that are not the kind +/// the card `stated`, and for a key larger than is kept. +pub(crate) fn key_of(bytes: &[u8], stated: Option) -> Option { + let text = std::str::from_utf8(bytes).ok().map(str::trim_start).filter(|t| t.starts_with("-----BEGIN")); + let (kind, data) = match text { + Some(t) if says_secret(t) => return None, + Some(t) if t.starts_with(PEM_BEGIN) => { + let body = t.get(PEM_BEGIN.len()..)?; + let (body, _) = body.split_once(PEM_END)?; + (KeyKind::X509, base64_bytes(body)) + } + Some(t) if t.starts_with(PGP_BEGIN) => { + // Up to the end of the armour; what follows it is not kept. + let end = t.find(PGP_END)?.checked_add(PGP_END.len())?; + let armour = t.get(..end)?; + openpgp_public(&armour_body(armour)?).then_some(())?; + (KeyKind::OpenPgp, armour.as_bytes().to_vec()) + } + Some(_) => return None, + None if is_certificate(bytes) => (KeyKind::X509, bytes.to_vec()), + None if openpgp_public(bytes) => (KeyKind::OpenPgp, bytes.to_vec()), + None => return None, + }; + let fits = !data.is_empty() && data.len() <= ContactKey::MAX_BYTES; + let agrees = stated.is_none_or(|s| s == kind); + let whole = kind != KeyKind::X509 || is_certificate(&data); + (fits && agrees && whole).then(|| ContactKey { kind, data, ..Default::default() }) +} + +/// Whether an armour or PEM label in the text says it holds secret key material +/// (`-----BEGIN PGP PRIVATE KEY BLOCK-----`, `-----BEGIN RSA PRIVATE KEY-----`, +/// `-----BEGIN ENCRYPTED PRIVATE KEY-----`, …), anywhere in it. +fn says_secret(text: &str) -> bool { + text.lines().map(str::trim).filter(|l| l.starts_with("-----")).any(|l| { + let label = l.to_ascii_uppercase(); + label.contains("PRIVATE") || label.contains("SECRET") + }) +} + +/// The bytes of an ASCII armour (RFC 9580 section 6): after the header lines and the blank line +/// that ends them, up to the checksum or the end line. `None` when that isn't base64. +fn armour_body(armour: &str) -> Option> { + let mut lines = armour.lines().skip(1).map(str::trim); + let mut body = String::new(); + // Header lines (`Version: …`, `Version:GnuPG`) end at a blank line; an armour without headers + // begins at once. (A colon is never base64.) + let mut first = lines.next()?; + if first.contains(':') { + first = lines.by_ref().find(|l| l.is_empty())?; + } + for line in std::iter::once(first).chain(lines) { + if line.starts_with('=') || line.starts_with("-----") { + break; + } + body.push_str(line); + } + let compact = body.trim_end_matches('='); + craft_mime::base64::STANDARD_NO_PAD.decode(compact).ok().filter(|b| !b.is_empty()) +} + +/// One DER element: its tag, its contents and what follows it. Only the definite lengths DER +/// allows, of at most four bytes. +fn element(bytes: &[u8]) -> Option<(u8, &[u8], &[u8])> { + let tag = *bytes.first()?; + let first = usize::from(*bytes.get(1)?); + let (length, start) = if first < 0x80 { + (first, 2usize) + } else { + let count = first.checked_sub(0x80).filter(|n| (1..=4).contains(n))?; + let digits = bytes.get(2..2usize.checked_add(count)?)?; + let length = digits.iter().try_fold(0usize, |n, d| n.checked_mul(256)?.checked_add(usize::from(*d)))?; + (length, 2usize.checked_add(count)?) + }; + let end = start.checked_add(length)?; + Some((tag, bytes.get(start..end)?, bytes.get(end..)?)) +} + +/// Whether the bytes have the outline of an X.509 certificate (RFC 5280 section 4.1): one +/// SEQUENCE of the to-be-signed SEQUENCE (beginning with its version or serial number), the +/// signature algorithm's SEQUENCE and the signature's BIT STRING, and nothing after it. A private +/// key (PKCS #1, PKCS #8, encrypted or not) or a PKCS #12 file does not have it. +fn is_certificate(bytes: &[u8]) -> bool { + let shape = || -> Option<()> { + let (0x30, certificate, []) = element(bytes)? else { return None }; + let (0x30, signed, rest) = element(certificate)? else { return None }; + matches!(signed.first(), Some(0xa0 | 0x02)).then_some(())?; + let (0x30, _, rest) = element(rest)? else { return None }; + let (0x03, _, []) = element(rest)? else { return None }; + Some(()) + }; + shape().is_some() +} + +/// Whether the bytes are OpenPGP packets (RFC 9580 section 4.2) that make a public key: a +/// public-key packet first, every packet whole, and no secret-key or secret-subkey packet +/// anywhere. +fn openpgp_public(bytes: &[u8]) -> bool { + let mut at = 0usize; + let mut first = true; + while at < bytes.len() { + let Some((tag, next)) = packet(bytes, at) else { return false }; + if matches!(tag, 5 | 7) || (first && tag != 6) { + return false; + } + first = false; + at = next; + } + !first +} + +/// The tag of the packet at `at` and where the next one begins; `None` for a header that isn't +/// one or a packet that runs past the end. A partial length (only data packets have one) is not a +/// key's. +fn packet(bytes: &[u8], at: usize) -> Option<(u8, usize)> { + let header = *bytes.get(at)?; + if header & 0x80 == 0 { + return None; + } + let byte = |i: usize| bytes.get(at.checked_add(i)?).map(|b| usize::from(*b)); + let big_endian = |from: usize, count: usize| (from..from.checked_add(count)?).try_fold(0usize, |n, i| n.checked_mul(256)?.checked_add(byte(i)?)); + let (tag, length, header_len) = if header & 0x40 == 0 { + // The old format: the tag in bits 5 to 2, the length's size in bits 1 and 0. + let tag = (header >> 2) & 0x0f; + match header & 0x03 { + 0 => (tag, byte(1)?, 2usize), + 1 => (tag, big_endian(1, 2)?, 3), + 2 => (tag, big_endian(1, 4)?, 5), + // An indeterminate length, which only data packets may have: not a key's. + _ => return None, + } + } else { + let tag = header & 0x3f; + match byte(1)? { + n @ 0..=191 => (tag, n, 2usize), + n @ 192..=223 => (tag, n.checked_sub(192)?.checked_mul(256)?.checked_add(byte(2)?)?.checked_add(192)?, 3), + 255 => (tag, big_endian(2, 4)?, 6), + _ => return None, + } + }; + let next = at.checked_add(header_len)?.checked_add(length)?; + (next <= bytes.len()).then_some((tag, next)) +} + +/// The kind its media type names and the bytes of a `data:` URI (after the scheme): +/// `application/pgp-keys;base64,mQIN…` or percent-encoded text. +fn data_uri(rest: &str) -> Option<(Option, Vec)> { + let (head, data) = rest.split_once(',')?; + let mut parts = head.split(';'); + let named = parts.next().and_then(kind_named); + let base64 = parts.any(|p| p.trim().eq_ignore_ascii_case("base64")); + Some((named, if base64 { base64_bytes(data) } else { percent_decoded(data) })) +} + +/// `%41` as `A`; a `%` without two hex digits after it is kept. +fn percent_decoded(s: &str) -> Vec { + let bytes = s.as_bytes(); + let hex = |at: usize| bytes.get(at).and_then(|b| char::from(*b).to_digit(16)).and_then(|d| u8::try_from(d).ok()); + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0usize; + while let Some(&b) = bytes.get(i) { + if b == b'%' + && let (Some(high), Some(low)) = (hex(i.saturating_add(1)), hex(i.saturating_add(2))) + { + out.push(high.saturating_mul(16).saturating_add(low)); + i = i.saturating_add(3); + } else { + out.push(b); + i = i.saturating_add(1); + } + } + out +} + +/// The address when it is one a key may be at: a known scheme, no white space, control or +/// format characters (a right-to-left override, a zero-width space) or backslashes, not too long. +pub(crate) fn checked_uri(value: &str) -> Option { + let (scheme, rest) = value.split_once(':')?; + let known = SCHEMES.iter().any(|s| s.eq_ignore_ascii_case(scheme)); + // (A backslash would be read back as an escape.) + let clean = !rest.is_empty() && !value.chars().any(|c| c.is_whitespace() || c.is_control() || crate::text::is_format(c) || c == '\\'); + (known && clean && value.len() <= ContactKey::MAX_URI).then(|| value.to_string()) +} + +/// The `KEY` line of a key, as vCard 3.0 writes it; `None` for a key that would not read back as +/// itself (an unknown kind, bytes that aren't a certificate or a public key, too many of them). +/// +/// An address of a key is not written: vCard 3.0 (RFC 2426 section 3.7.2) lets `KEY` be binary +/// or text only, and an address written as text would be read elsewhere as the key itself. It +/// stays on the contact. +pub(crate) fn line(key: &ContactKey) -> Option { + let kind = match key.kind { + KeyKind::X509 => "X509", + KeyKind::OpenPgp => "PGP", + KeyKind::Uri => return None, + }; + // Only what the reader would keep as this very key: never secret material a mailbox file + // may hold. + (key_of(&key.data, Some(key.kind)).as_ref() == Some(key)) + .then(|| format!("KEY;TYPE={kind};ENCODING=b:{}", craft_mime::base64::STANDARD.encode(&key.data))) +} diff --git a/crates/vcard/src/key_tests.rs b/crates/vcard/src/key_tests.rs new file mode 100644 index 000000000..18bfe2291 --- /dev/null +++ b/crates/vcard/src/key_tests.rs @@ -0,0 +1,417 @@ +//! `KEY`: certificates and OpenPGP public keys read in every version's form, known by their bytes, +//! secret key material never kept, written back as 3.0, capped, and never a panic. + +use crate::{Contact, ContactKey, KeyKind}; +use craft_mime::base64::STANDARD; +use proptest::prelude::*; + +use crate::{parse, write}; + +/// Made-up bytes with the outline of a DER certificate: SEQUENCE { SEQUENCE { INTEGER 7 }, +/// SEQUENCE {}, BIT STRING }. +const DER: &[u8] = &[0x30, 0x0a, 0x30, 0x03, 0x02, 0x01, 0x07, 0x30, 0x00, 0x03, 0x01, 0x00]; +/// Made-up OpenPGP packets: an old-format public-key packet (tag 6) of six bytes. +const PGP: &[u8] = &[0x99, 0x00, 0x06, 0x04, 0x5e, 0x00, 0x00, 0x01, 0x16]; + +/// One DER element, its length in the short or the long form. +fn der(tag: u8, content: &[u8]) -> Vec { + let mut out = vec![tag]; + match content.len() { + n if n < 0x80 => out.push(n as u8), + n if n < 0x100 => out.extend([0x81, n as u8]), + n => out.extend([0x82, (n >> 8) as u8, n as u8]), + } + out.extend(content); + out +} + +/// A certificate's outline whose signature is `signature`. +fn certificate(serial: &[u8], signature: &[u8]) -> Vec { + let signed = der(0x30, &der(0x02, serial)); + der(0x30, &[signed, der(0x30, &[]), der(0x03, signature)].concat()) +} + +/// An ASCII armour of OpenPGP packets, with a header and a checksum line. +fn armoured(packets: &[u8]) -> String { + format!("-----BEGIN PGP PUBLIC KEY BLOCK-----\nComment: made up\n\n{}\n=AAAA\n-----END PGP PUBLIC KEY BLOCK-----", STANDARD.encode(packets)) +} + +fn pem(der: &[u8]) -> String { + format!("-----BEGIN CERTIFICATE-----\n{}\n-----END CERTIFICATE-----\n", STANDARD.encode(der)) +} + +/// `text` with `\`, `;`, `,` and line breaks escaped as a vCard value. +fn escaped(text: &str) -> String { + crate::text::escape(text) +} + +fn keys(version: &str, properties: &str) -> Vec { + let got = parse(&format!("BEGIN:VCARD\r\nVERSION:{version}\r\nFN:Ada\r\n{properties}\r\nEND:VCARD\r\n")); + assert_eq!(got.len(), 1, "{properties}"); + got.into_iter().next().map(|c| c.keys).unwrap_or_default() +} + +fn x509(data: &[u8]) -> ContactKey { + ContactKey { kind: KeyKind::X509, data: data.to_vec(), ..Default::default() } +} + +fn openpgp(data: &[u8]) -> ContactKey { + ContactKey { kind: KeyKind::OpenPgp, data: data.to_vec(), ..Default::default() } +} + +fn at(uri: &str, media_type: &str) -> ContactKey { + ContactKey { kind: KeyKind::Uri, uri: uri.into(), media_type: media_type.into(), ..Default::default() } +} + +#[test] +fn reads_2_1_keys() { + let (der, pgp) = (STANDARD.encode(DER), STANDARD.encode(PGP)); + let (head, tail) = der.split_at(4); + let got = keys("2.1", &format!("KEY;X509;ENCODING=BASE64:{head}\r\n {tail}\r\n\r\nKEY;PGP;BASE64:{pgp}")); + assert_eq!(got, [x509(DER), openpgp(PGP)]); +} + +#[test] +fn reads_3_0_keys() { + let (der, pgp) = (STANDARD.encode(DER), STANDARD.encode(PGP)); + let (head, tail) = pgp.split_at(5); + let armour = armoured(PGP); + let got = keys( + "3.0", + &format!( + "KEY;ENCODING=b;TYPE=X509:{der}\r\nkey;type=pgp;encoding=B:{head}\r\n {tail}\r\nKEY;TYPE=PGP:{}\r\nKEY;VALUE=uri;TYPE=X509:ldap://ldap.example.org/cn=Ada", + escaped(&format!("{armour}\nWhat follows the armour is not kept")) + ), + ); + assert_eq!(got, [x509(DER), openpgp(PGP), openpgp(armour.as_bytes()), at("ldap://ldap.example.org/cn=Ada", ContactKey::X509_MEDIA_TYPE)]); +} + +#[test] +fn reads_4_0_keys() { + let (der, pgp) = (STANDARD.encode(DER), STANDARD.encode(PGP)); + let got = keys( + "4.0", + &format!( + "KEY:data:application/pgp-keys;base64,{pgp}\r\nKEY:data:application/pkix-cert;base64\\,{der}\r\nKEY:data:application/pgp-keys,%99%00%06%04%5E%00%00%01%16\r\n\ +KEY;MEDIATYPE=application/pgp-keys:https://keys.example.org/ada.asc\r\nKEY;TYPE=work:openpgp4fpr:ABCDEF0123456789ABCDEF0123456789ABCDEF01\r\nKEY:http://example.org/ada.cer" + ), + ); + assert_eq!( + got, + [ + openpgp(PGP), + x509(DER), + // (The same key twice is kept once.) + at("https://keys.example.org/ada.asc", ContactKey::OPENPGP_MEDIA_TYPE), + at("openpgp4fpr:ABCDEF0123456789ABCDEF0123456789ABCDEF01", ""), + at("http://example.org/ada.cer", ""), + ] + ); +} + +#[test] +fn a_key_without_a_type_is_known_by_its_bytes() { + let got = keys( + "3.0", + &format!( + "KEY;ENCODING=b:{}\r\nKEY;ENCODING=b:{}\r\nKEY:{}\r\nKEY:data:,{}\r\nKEY;ENCODING=b:{}", + STANDARD.encode(DER), + STANDARD.encode(PGP), + escaped(&pem(DER)), + armoured(PGP).replace('\n', "%0A"), + STANDARD.encode(b"neither") + ), + ); + assert_eq!(got, [x509(DER), openpgp(PGP), openpgp(armoured(PGP).as_bytes())]); +} + +#[test] +fn secret_key_material_is_never_kept() { + let pkcs8 = der(0x30, &[der(0x02, &[0]), der(0x30, &der(0x06, &[0x2a, 0x86, 0x48])), der(0x04, &[1, 2, 3, 4])].concat()); + let pkcs12 = der(0x30, &[der(0x02, &[3]), der(0x30, &der(0x06, &[0x2a])), der(0x30, &[])].concat()); + let encrypted_pkcs8 = der(0x30, &[der(0x30, &der(0x06, &[0x2a])), der(0x04, &[9, 9])].concat()); + // A public-key packet followed by a secret-subkey packet (old format, tag 7). + let with_secret_subkey = [PGP, &[0x9d, 0x00, 0x01, 0x00]].concat(); + for (what, line) in [ + ("an armoured secret key labelled PGP", format!("KEY;TYPE=PGP:{}", escaped(&armoured(PGP).replace("PUBLIC", "PRIVATE")))), + ( + "a PEM private key labelled X509", + format!("KEY;TYPE=X509:{}", escaped(&format!("-----BEGIN PRIVATE KEY-----\n{}\n-----END PRIVATE KEY-----", STANDARD.encode(&pkcs8)))), + ), + ("a PEM RSA key", format!("KEY:{}", escaped("-----BEGIN RSA PRIVATE KEY-----\nMIIB\n-----END RSA PRIVATE KEY-----"))), + ( + "a certificate with a key after it", + format!( + "KEY;TYPE=X509:{}", + escaped(&format!("{}-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIIB\n-----END ENCRYPTED PRIVATE KEY-----", pem(DER))) + ), + ), + ("a secret-key packet, old format (lQAA)", "KEY;TYPE=PGP;ENCODING=b:lQAA".to_string()), + ("a secret-key packet, new format", format!("KEY;ENCODING=b:{}", STANDARD.encode([0xc5, 0x01, 0x04]))), + ("a secret-subkey packet, new format", format!("KEY;ENCODING=b:{}", STANDARD.encode([PGP, &[0xc7, 0x00]].concat()))), + ("a secret subkey after a public key", format!("KEY;TYPE=PGP;ENCODING=b:{}", STANDARD.encode(&with_secret_subkey))), + ("a secret subkey inside a public armour", format!("KEY;TYPE=PGP:{}", escaped(&armoured(&with_secret_subkey)))), + ("PKCS #8 labelled X509", format!("KEY;TYPE=X509;ENCODING=b:{}", STANDARD.encode(&pkcs8))), + ("PKCS #8 without a type", format!("KEY;ENCODING=b:{}", STANDARD.encode(&pkcs8))), + ("encrypted PKCS #8", format!("KEY:data:application/pkix-cert;base64,{}", STANDARD.encode(&encrypted_pkcs8))), + ("PKCS #12", format!("KEY;TYPE=X509;ENCODING=b:{}", STANDARD.encode(&pkcs12))), + ("PKCS #8 in PEM as a certificate", format!("KEY:{}", escaped(&pem(&pkcs8)))), + ] { + assert!(keys("3.0", &line).is_empty(), "{what}"); + } + // And a mailbox file that holds such bytes never has them written to a card. + let c = + Contact { display_name: "Ada".into(), keys: vec![x509(&pkcs8), openpgp(&with_secret_subkey), openpgp(&[0x95, 0, 0])], ..Default::default() }; + assert!(!write(&[&c]).contains("KEY")); +} + +#[test] +fn text_is_never_taken_for_binary() { + for line in [ + "KEY;VALUE=text:0abc", + "KEY;TYPE=X509;VALUE=text:0\\n\\u{3}", + "KEY;TYPE=X509;ENCODING=QUOTED-PRINTABLE:0hello", + "KEY;TYPE=PGP;VALUE=text:ABCD 1234 EF56", + "KEY;TYPE=PGP:ABCD1234EF56", + // Text that looks like an address is text, not the address of a key. + "KEY;VALUE=text:https://keys.example.org/ada.asc", + "KEY;TYPE=PGP;VALUE=TEXT:openpgp4fpr:ABCDEF01", + ] { + assert!(keys("3.0", line).is_empty(), "{line}"); + } +} + +#[test] +fn an_indeterminate_length_is_no_keys() { + // Old format, tag 6, length type 3 (the rest of the bytes): only data packets may have it. + for header in [0x9b_u8, 0xbb] { + let bytes = [header, 0x04, 0x5e, 0x00, 0x00, 0x01, 0x16]; + assert!(keys("3.0", &format!("KEY;TYPE=PGP;ENCODING=b:{}", STANDARD.encode(bytes))).is_empty(), "{header:#x}"); + assert!(keys("3.0", &format!("KEY;ENCODING=b:{}", STANDARD.encode([PGP, &bytes].concat()))).is_empty(), "{header:#x}"); + } +} + +#[test] +fn armour_headers_without_a_space_are_headers() { + let armour = format!( + "-----BEGIN PGP PUBLIC KEY BLOCK-----\nVersion:GnuPG v2\nComment:made up\n\n{}\n=AAAA\n-----END PGP PUBLIC KEY BLOCK-----", + STANDARD.encode(PGP) + ); + assert_eq!(keys("3.0", &format!("KEY;TYPE=PGP:{}", escaped(&armour))), [openpgp(armour.as_bytes())]); +} + +#[test] +fn every_spelling_of_a_certificate_is_kept_as_der_and_the_bytes_say_the_kind() { + let quoted: String = pem(DER).bytes().map(|b| format!("={b:02X}")).collect(); + let percent: String = pem(DER).bytes().map(|b| format!("%{b:02X}")).collect(); + let got = keys( + "3.0", + &format!( + "KEY;TYPE=X509;ENCODING=QUOTED-PRINTABLE:{quoted}\r\nKEY:data:application/pkix-cert,{percent}\r\nKEY;TYPE=X509;ENCODING=b:{}\r\nKEY;ENCODING=b:{}", + STANDARD.encode(pem(DER)), + STANDARD.encode(pem(&certificate(&[8], &[0]))) + ), + ); + // The first three are the same certificate, kept once. + assert_eq!(got, [x509(DER), x509(&certificate(&[8], &[0]))]); + // A label the bytes don't bear out is dropped. + for line in [ + format!("KEY;TYPE=PGP:{}", escaped(&pem(DER))), + format!("KEY;TYPE=PGP;ENCODING=b:{}", STANDARD.encode(DER)), + format!("KEY;TYPE=X509;ENCODING=b:{}", STANDARD.encode(PGP)), + format!("KEY:data:application/pkix-cert;base64,{}", STANDARD.encode(PGP)), + format!("KEY;MEDIATYPE=application/pgp-keys:{}", escaped(&pem(DER))), + ] { + assert!(keys("4.0", &line).is_empty(), "{line}"); + } +} + +#[test] +fn what_is_not_a_key_is_dropped() { + for junk in [ + "KEY;TYPE=PGP;ENCODING=b:!!!", + "KEY;TYPE=PGP;ENCODING=b:", + "KEY;TYPE=X509:", + "KEY:javascript:alert(1)", + "KEY:file:///etc/passwd", + "KEY:cid:part1@example.org", + "KEY;VALUE=uri:https://exa mple.org/key", + "KEY:https:", + "KEY:no scheme at all", + "KEY:data:application/pgp-keys;base64", + "KEY;TYPE=X509:-----BEGIN CERTIFICATE-----\\n!!!\\n-----END CERTIFICATE-----", + "KEY;TYPE=X509:-----BEGIN CERTIFICATE-----\\nMAA=", + "KEY;TYPE=X-SOMETHING;ENCODING=b:AAEC", + // A packet that runs past the end, a partial length, a header that is none. + "KEY;ENCODING=b:mQAJBA==", + "KEY;ENCODING=b:xuAA", + "KEY;ENCODING=b:BgAA", + ] { + assert!(keys("3.0", junk).is_empty(), "{junk}"); + } + let long = format!("KEY:https://example.org/{}", "a".repeat(ContactKey::MAX_URI)); + assert!(keys("4.0", &long).is_empty()); +} + +#[test] +fn an_address_with_format_characters_is_dropped() { + for hidden in ['\u{202e}', '\u{200b}', '\u{2066}', '\u{feff}', '\u{ad}'] { + assert!(keys("4.0", &format!("KEY:https://keys.example.org/{hidden}ada.asc")).is_empty(), "{:?}", hidden); + } + assert_eq!(keys("4.0", "KEY:https://keys.example.org/ada.asc"), [at("https://keys.example.org/ada.asc", "")]); +} + +#[test] +fn the_size_counted_is_the_keys_own() { + // A quoted-printable certificate just under the cap is three times as long written, and kept. + let big = certificate(&[1], &vec![0; ContactKey::MAX_BYTES - 20]); + assert!(big.len() <= ContactKey::MAX_BYTES && big.len() > ContactKey::MAX_BYTES - 32); + let quoted: String = big.iter().map(|b| format!("={b:02X}")).collect(); + assert_eq!(keys("2.1", &format!("KEY;X509;QUOTED-PRINTABLE:{quoted}")), [x509(&big)]); + let percent: String = big.iter().map(|b| format!("%{b:02X}")).collect(); + assert_eq!(keys("4.0", &format!("KEY:data:application/pkix-cert,{percent}")), [x509(&big)]); + // One byte more is not. + let over = certificate(&[1], &vec![0; ContactKey::MAX_BYTES - 20 + (ContactKey::MAX_BYTES - big.len()) + 1]); + assert_eq!(over.len(), ContactKey::MAX_BYTES + 1); + assert!(keys("3.0", &format!("KEY;TYPE=X509;ENCODING=b:{}", STANDARD.encode(&over))).is_empty()); + let quoted: String = over.iter().map(|b| format!("={b:02X}")).collect(); + assert!(keys("2.1", &format!("KEY;X509;QUOTED-PRINTABLE:{quoted}")).is_empty()); +} + +#[test] +fn keys_are_capped_per_card_and_per_text() { + let many: String = (0..Contact::MAX_KEYS + 10) + .map(|i| format!("KEY;TYPE=X509;ENCODING=b:{}\r\n", STANDARD.encode(certificate(&[u8::try_from(i).unwrap()], &[0])))) + .collect(); + assert_eq!(keys("3.0", &many).len(), Contact::MAX_KEYS); + // What one text keeps of keys in all: the cards after the allowance is spent keep none. + let card = |n: u8| format!("BEGIN:VCARD\r\nFN:C{n}\r\nKEY;TYPE=X509;ENCODING=b:{}\r\nEND:VCARD\r\n", STANDARD.encode(certificate(&[n], &[0]))); + let text: String = (0..5).map(card).collect(); + let got = crate::read::parse_keeping(&text, 25); + assert_eq!(got.iter().map(|c| c.keys.len()).collect::>(), [1, 1, 0, 0, 0]); + assert_eq!(parse(&text).iter().map(|c| c.keys.len()).sum::(), 5); +} + +#[test] +fn a_card_of_nothing_but_keys_is_skipped() { + let only = format!( + "BEGIN:VCARD\r\nVERSION:3.0\r\nKEY;TYPE=X509;ENCODING=b:{}\r\nKEY:https://keys.example.org/a.asc\r\nEND:VCARD\r\n", + STANDARD.encode(DER) + ); + assert!(parse(&only).is_empty()); + assert_eq!(parse(&only.replace("VERSION:3.0", "VERSION:3.0\r\nFN:Ada"))[0].keys.len(), 2); +} + +#[test] +fn writes_keys_as_3_0() { + let c = Contact { + display_name: "Ada".into(), + keys: vec![ + x509(DER), + openpgp(PGP), + openpgp(armoured(PGP).as_bytes()), + // Never written: addresses (3.0 has no form for them), nothing in it, bytes that aren't the kind they say. + at("https://keys.example.org/ada.asc", ContactKey::OPENPGP_MEDIA_TYPE), + at("openpgp4fpr:ABCDEF01", ""), + openpgp(&[]), + openpgp(DER), + ContactKey { uri: "https://example.org/a".into(), ..x509(DER) }, + ], + ..Default::default() + }; + let out = write(&[&c]); + let lines: Vec<&str> = out.split("\r\n").filter(|l| l.starts_with("KEY")).collect(); + assert_eq!(lines.len(), 3, "{out}"); + assert_eq!(lines[0], format!("KEY;TYPE=X509;ENCODING=b:{}", STANDARD.encode(DER))); + assert_eq!(lines[1], format!("KEY;TYPE=PGP;ENCODING=b:{}", STANDARD.encode(PGP))); + assert!(lines[2].starts_with("KEY;TYPE=PGP;ENCODING=b:")); + assert!(!out.contains("VALUE=uri") && !out.contains("keys.example.org")); + assert_eq!(parse(&out)[0].keys, c.keys[..3]); +} + +#[test] +fn large_keys_fold_and_read_back() { + let signature: Vec = (0..ContactKey::MAX_BYTES - 32).map(|i| (i % 251) as u8).collect(); + let cert = certificate(&[1], &signature); + let body: Vec = (0..60_000u32).map(|i| (i % 251) as u8).collect(); + let pgp = [&[0x9a, 0x00, 0x00, 0xea, 0x60][..], &body].concat(); + let c = Contact { display_name: "Ada".into(), keys: vec![x509(&cert), openpgp(&pgp)], ..Default::default() }; + let out = write(&[&c]); + assert!(out.split("\r\n").all(|line| line.len() <= 75)); + assert_eq!(parse(&out)[0].keys, c.keys); + assert_eq!(crate::parse_bytes(out.as_bytes())[0].keys, c.keys); +} + +/// A certificate's outline or an OpenPGP public key (binary or armoured), from random bytes. +fn key() -> impl Strategy { + let bytes = || proptest::collection::vec(any::(), 1..200); + let user_ids = proptest::collection::vec(proptest::collection::vec(any::(), 0..60), 0..3); + let packets = (bytes(), user_ids).prop_map(|(body, ids)| { + let mut out = vec![0x99, 0, body.len() as u8]; + out.extend(body); + for id in ids { + // An old-format user ID packet (tag 13) with a one-byte length. + out.extend([0xb4, id.len() as u8]); + out.extend(id); + } + out + }); + prop_oneof![ + (bytes(), bytes()).prop_map(|(serial, signature)| x509(&certificate(&serial[..serial.len().min(100)], &signature))), + packets.clone().prop_map(|p| openpgp(&p)), + packets.prop_map(|p| openpgp(armoured(&p).as_bytes())), + ] +} + +proptest! { + #[test] + fn any_keys_round_trip(list in proptest::collection::vec(key(), 0..6)) { + let mut unique: Vec = Vec::new(); + for k in list { + if !unique.contains(&k) { + unique.push(k); + } + } + let c = Contact { display_name: "Ada".into(), keys: unique, ..Default::default() }; + let back = parse(&write(&[&c])); + prop_assert_eq!(back.len(), 1); + prop_assert_eq!(&back[0].keys, &c.keys); + } + + #[test] + fn arbitrary_key_values_never_panic( + params in proptest::sample::select(vec!["", ";ENCODING=b", ";TYPE=PGP", ";TYPE=X509;ENCODING=BASE64", ";X509;QUOTED-PRINTABLE", ";MEDIATYPE=application/pgp-keys", ";VALUE=text", ";VALUE=uri"]), + head in proptest::sample::select(vec!["", "data:", "data:application/pkix-cert;base64,", "data:,%", "-----BEGIN CERTIFICATE-----", "-----BEGIN PGP PUBLIC KEY BLOCK-----", "https:", "openpgp4fpr:", "0", "\u{99}"]), + value in any::(), + bytes in proptest::collection::vec(any::(), 0..512), + ) { + let text = format!("BEGIN:VCARD\r\nFN:x\r\nKEY{params}:{head}{value}\r\nKEY{params}:{head}{}\r\nEND:VCARD\r\n", STANDARD.encode(&bytes)); + let got = parse(&text); + for c in &got { + prop_assert!(c.keys.iter().all(|k| k.data.len() <= ContactKey::MAX_BYTES && k.uri.len() <= ContactKey::MAX_URI)); + // What was read writes and reads back as itself (addresses are not written to 3.0). + let again = parse(&write(&[c])); + let written: Vec<&ContactKey> = c.keys.iter().filter(|k| k.kind != KeyKind::Uri).collect(); + prop_assert_eq!(again.first().map(|a| a.keys.iter().collect::>()), Some(written)); + } + let mut raw = format!("BEGIN:VCARD\r\nFN:x\r\nKEY{params}:{head}").into_bytes(); + raw.extend(&bytes); + raw.extend(b"\r\nEND:VCARD\r\n"); + let _ = crate::parse_bytes(&raw); + } + + #[test] + fn random_bytes_are_never_kept_as_secret_material(body in proptest::collection::vec(any::(), 0..300)) { + // Whatever follows a secret-key or secret-subkey header, nothing is kept. + for header in [[0x95u8, 0x00], [0x94, 0x00], [0x9d, 0x00], [0xc5, 0x00], [0xc7, 0x00]] { + let mut packet = header.to_vec(); + if let Some(len) = packet.get_mut(1) { + *len = body.len().min(150) as u8; + } + let mut bytes = packet; + bytes.extend(body.iter().take(150)); + let line = format!("KEY;TYPE=PGP;ENCODING=b:{}", STANDARD.encode([PGP, &bytes].concat())); + prop_assert!(keys("3.0", &line).is_empty()); + } + } +} diff --git a/crates/vcard/src/lib.rs b/crates/vcard/src/lib.rs new file mode 100644 index 000000000..f2fc2fd6a --- /dev/null +++ b/crates/vcard/src/lib.rs @@ -0,0 +1,26 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! vCard: contacts as vCard text (`.vcf` files, CardDAV address data). +//! +//! [`parse`] reads vCard 2.1, 3.0 and 4.0, one card or many in a text ([`parse_bytes`] in a file, +//! each value in its own `CHARSET`); [`write()`] writes vCard 3.0, the version every address book +//! reads. Both work on [`Contact`], this crate's own plain model of a card. +//! +//! Input is hostile. Nothing here panics on any text: line lengths and counts are capped, and what +//! makes no sense is skipped. + +mod key; +#[cfg(test)] +mod key_tests; +mod lines; +mod model; +mod read; +#[cfg(test)] +mod tests; +mod text; +mod write; + +pub use model::{Contact, ContactKey, KeyKind, Labeled, PostalAddress}; +pub use read::{parse, parse_bytes}; +pub use write::write; diff --git a/crates/vcard/src/lines.rs b/crates/vcard/src/lines.rs new file mode 100644 index 000000000..7507026ef --- /dev/null +++ b/crates/vcard/src/lines.rs @@ -0,0 +1,429 @@ +//! Content lines: unfolding (and quoted-printable soft line breaks) on the way in, folding on the +//! way out, and the `group.NAME;PARAM=value;PARAM:value` shape in between. + +/// Longest logical line kept, in bytes; the rest of a longer line is dropped. +pub(crate) const MAX_LINE: usize = 1 << 20; +/// Most parameters read from one line, and most values of one parameter. +const MAX_PARAMS: usize = 64; +/// Octets per physical line when writing. +const FOLD_AT: usize = 75; +/// How far into a line the `:` that ends its parameters is looked for. +const MAX_HEAD: usize = 1024; + +/// The longest prefix of `s` of at most `max` bytes that ends on a char boundary. +pub(crate) fn prefix(s: &str, max: usize) -> &str { + if s.len() <= max { + return s; + } + let mut end = max; + while end > 0 && !s.is_char_boundary(end) { + end -= 1; + } + s.get(..end).unwrap_or("") +} + +/// The text of a vCard file from its bytes, each property's value read in the `CHARSET` it names. +/// +/// vCard 2.1 lets a value be raw 8-bit bytes in a character set of its own +/// (`FN;CHARSET=WINDOWS-1250:Gvo\xF0\xE6i\xE8`), so a file is read line by line: a value with +/// bytes above 0x7F is decoded with the `CHARSET` of its property (a folded line goes on with the +/// property above it), and without one it is UTF-8 when it is valid UTF-8, else Windows-1252. Names, +/// parameters and values in ASCII (quoted-printable and base64 among them, decoded later with +/// their charset) are kept as they are. A file that opens with a UTF-16 byte order mark is read as +/// UTF-16 throughout. +pub(crate) fn text_from_bytes(bytes: &[u8]) -> String { + if bytes.starts_with(&[0xFF, 0xFE]) || bytes.starts_with(&[0xFE, 0xFF]) { + return craft_mime::charset::decode(bytes, "utf-16"); + } + let bytes = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes); + if bytes.is_ascii() { + return String::from_utf8_lossy(bytes).into_owned(); + } + let mut out = String::with_capacity(bytes.len()); + // The charset of the property being read, for the folded lines that go on with it. + let mut charset = String::new(); + let mut rest = bytes; + while !rest.is_empty() { + let end = rest.iter().position(|b| matches!(b, b'\r' | b'\n')).unwrap_or(rest.len()); + let skip = match (rest.get(end), rest.get(end.saturating_add(1))) { + (Some(b'\r'), Some(b'\n')) => 2, + (Some(_), _) => 1, + (None, _) => 0, + }; + let line = rest.get(..end).unwrap_or(&[]); + let value = if line.starts_with(b" ") || line.starts_with(b"\t") { + line + } else if let Some(colon) = head_end(line) { + let head = line.get(..colon).unwrap_or(&[]); + charset = charset_of(head); + out.push_str(&String::from_utf8_lossy(head)); + line.get(colon..).unwrap_or(&[]) + } else { + charset.clear(); + line + }; + out.push_str(&value_text(value, &charset)); + // The line break itself, as it was. + out.push_str(&String::from_utf8_lossy(rest.get(end..end.saturating_add(skip)).unwrap_or(&[]))); + rest = rest.get(end.saturating_add(skip)..).unwrap_or(&[]); + } + out +} + +/// Where the `:` that ends a line's name and parameters is: outside quotes, in the first kilobyte. +fn head_end(line: &[u8]) -> Option { + let mut quoted = false; + for (i, b) in line.iter().enumerate().take(MAX_HEAD) { + match b { + b'"' => quoted = !quoted, + b':' if !quoted => return Some(i), + _ => {} + } + } + None +} + +/// The `CHARSET` named among a line's parameters; empty when there is none. +fn charset_of(head: &[u8]) -> String { + let head = String::from_utf8_lossy(head); + let named = head.split(';').skip(1).find_map(|param| { + let (name, value) = param.split_once('=')?; + name.trim().eq_ignore_ascii_case("CHARSET").then(|| value.trim().trim_matches('"').trim().to_string()) + }); + named.unwrap_or_default() +} + +/// A value's bytes as text in `charset`, or guessed without one. ASCII stays as it is; a set that +/// ASCII isn't part of (UTF-16, ISO-2022) can't be raw 8-bit bytes on a line, so it is guessed. +fn value_text(value: &[u8], charset: &str) -> String { + let label = charset.to_ascii_lowercase(); + if value.is_ascii() || label.starts_with("utf-16") || label.contains("2022") { + return craft_mime::charset::guess(value); + } + craft_mime::charset::decode(value, &label) +} + +/// A cursor over the lines of a text. Lines end at CRLF, LF or CR. +#[derive(Clone, Copy)] +pub(crate) struct Lines<'a> { + rest: &'a str, +} + +impl<'a> Lines<'a> { + pub(crate) fn new(text: &'a str) -> Lines<'a> { + Lines { rest: text.strip_prefix('\u{feff}').unwrap_or(text) } + } + + /// The next physical line, without its line break. + fn physical(&mut self) -> Option<&'a str> { + if self.rest.is_empty() { + return None; + } + let bytes = self.rest.as_bytes(); + let end = bytes.iter().position(|b| matches!(b, b'\r' | b'\n')).unwrap_or(bytes.len()); + let skip = match (bytes.get(end), bytes.get(end.saturating_add(1))) { + (Some(b'\r'), Some(b'\n')) => 2, + (Some(_), _) => 1, + (None, _) => 0, + }; + let line = self.rest.get(..end).unwrap_or(""); + self.rest = self.rest.get(end.saturating_add(skip)..).unwrap_or(""); + Some(line) + } + + /// The next logical line, or `None` at the end of the text. + /// + /// A line that starts with a space or a tab continues the one before. vCard 3.0 and 4.0 drop + /// that first character; vCard 2.1 (`v21`) keeps it, since there a line is folded where it has + /// white space anyway. A quoted-printable value that ends in `=` goes on with the next line, + /// whatever that starts with. + pub(crate) fn next_line(&mut self, v21: bool) -> Option { + let mut line = String::new(); + let mut last = self.physical()?; + // Whether `line` holds all that was read; a line past the cap is still read to its end. + let mut whole = push_capped(&mut line, last); + let mut quoted_printable = None; + loop { + if quoted_printable.is_none() { + quoted_printable = says_quoted_printable(&line); + } + let mut ahead = *self; + let Some(next) = ahead.physical() else { break }; + if quoted_printable == Some(true) && last.trim_end().ends_with('=') && !starts_or_ends_a_card(next) { + if whole { + line.truncate(line.trim_end().len()); + line.pop(); + } + whole &= push_capped(&mut line, next); + } else if next.starts_with([' ', '\t']) { + whole &= push_capped(&mut line, if v21 { next } else { next.get(1..).unwrap_or("") }); + } else { + break; + } + last = next; + *self = ahead; + } + Some(line) + } +} + +/// Append as much of `more` as the cap leaves room for; whether that was all of it. +fn push_capped(line: &mut String, more: &str) -> bool { + let piece = prefix(more, MAX_LINE.saturating_sub(line.len())); + line.push_str(piece); + piece.len() == more.len() +} + +/// Whether the parameters of `line` name the quoted-printable encoding; `None` while the `:` that +/// ends them hasn't been seen yet. +fn says_quoted_printable(line: &str) -> Option { + let head = prefix(line, MAX_HEAD); + match head.find(':') { + Some(colon) => Some(head.get(..colon).unwrap_or("").to_ascii_uppercase().contains("QUOTED-PRINTABLE")), + None if line.len() > MAX_HEAD => Some(false), + None => None, + } +} + +/// A soft line break never swallows the line that ends the card. +fn starts_or_ends_a_card(line: &str) -> bool { + let line = line.trim_start(); + let is = |word: &str| line.get(..word.len()).is_some_and(|start| start.eq_ignore_ascii_case(word)); + is("END:") || is("BEGIN:") +} + +/// One property parameter: `TYPE=WORK,VOICE`, `ENCODING=QUOTED-PRINTABLE`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Param { + /// Upper-cased. + pub name: String, + pub values: Vec, +} + +/// One property: `item1.TEL;TYPE=CELL:+1 555 0100`. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub(crate) struct ContentLine { + /// The group before the name (`item1`), lower-cased; empty when there is none. + pub group: String, + /// Upper-cased, without the group. + pub name: String, + pub params: Vec, + /// As written: still encoded and escaped. + pub value: String, +} + +impl ContentLine { + /// Parse an unfolded line; `None` for a line without a name or without a `:`. + /// + /// A parameter without a name (vCard 2.1: `TEL;WORK;VOICE:`) is a `TYPE`, or an `ENCODING` when + /// it names one. + pub(crate) fn parse(line: &str) -> Option { + // Every delimiter is ASCII, so the byte positions found here are char boundaries. + let bytes = line.as_bytes(); + let stop = |from: usize, at: &[u8]| { + bytes.get(from..).and_then(|b| b.iter().position(|c| at.contains(c))).map_or(bytes.len(), |i| from.saturating_add(i)) + }; + let mut pos = stop(0, b";:"); + let full = line.get(..pos)?.trim(); + let (group, name) = full.rsplit_once('.').unwrap_or(("", full)); + if name.is_empty() { + return None; + } + let mut params = Vec::new(); + while bytes.get(pos) == Some(&b';') { + let start = pos.saturating_add(1); + pos = stop(start, b"=;:"); + let written = line.get(start..pos)?.trim().to_ascii_uppercase(); + let param = if bytes.get(pos) == Some(&b'=') { + let mut values = Vec::new(); + while matches!(bytes.get(pos), Some(b'=' | b',')) { + let from = pos.saturating_add(1); + let value = if bytes.get(from) == Some(&b'"') { + let open = from.saturating_add(1); + let close = stop(open, b"\""); + // A quote that never closes: the line has no value. + bytes.get(close)?; + pos = stop(close, b",;:"); + line.get(open..close)? + } else { + pos = stop(from, b",;:"); + line.get(from..pos)? + }; + if values.len() < MAX_PARAMS { + values.push(value.trim().to_string()); + } + } + Param { name: written, values } + } else if matches!(written.as_str(), "QUOTED-PRINTABLE" | "BASE64" | "B" | "7BIT" | "8BIT") { + Param { name: "ENCODING".into(), values: vec![written] } + } else { + Param { name: "TYPE".into(), values: vec![written] } + }; + if !param.name.is_empty() && param.values.iter().any(|v| !v.is_empty()) && params.len() < MAX_PARAMS { + params.push(param); + } + } + if bytes.get(pos) != Some(&b':') { + return None; + } + Some(ContentLine { + group: group.trim().to_ascii_lowercase(), + name: name.trim().to_ascii_uppercase(), + params, + value: line.get(pos.saturating_add(1)..)?.to_string(), + }) + } + + /// The first value of the parameter `name` (upper case). + pub(crate) fn param(&self, name: &str) -> Option<&str> { + self.params.iter().find(|p| p.name == name).and_then(|p| p.values.first()).map(String::as_str) + } + + /// Every `TYPE`, upper-cased: `TYPE=work,voice`, `TYPE=work;TYPE=voice`, `TYPE="work,voice"` + /// and the bare `WORK;VOICE` all give `WORK` and `VOICE`. + pub(crate) fn types(&self) -> Vec { + let values = self.params.iter().filter(|p| p.name == "TYPE").flat_map(|p| p.values.iter()); + values.flat_map(|v| v.split(',')).map(|v| v.trim().to_ascii_uppercase()).filter(|v| !v.is_empty()).take(MAX_PARAMS).collect() + } +} + +/// Append `line` to `out` with CRLF, folded so no physical line is longer than 75 octets and no +/// character is split. +pub(crate) fn fold(out: &mut String, line: &str) { + let mut rest = line; + let mut room = FOLD_AT; + while rest.len() > room { + let head = prefix(rest, room); + if head.is_empty() { + break; + } + out.push_str(head); + out.push_str("\r\n "); + rest = rest.get(head.len()..).unwrap_or(""); + // The leading space counts. + room = FOLD_AT - 1; + } + out.push_str(rest); + out.push_str("\r\n"); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn lines(text: &str, v21: bool) -> Vec { + let mut cursor = Lines::new(text); + std::iter::from_fn(|| cursor.next_line(v21)).collect() + } + + #[test] + fn unfolds_every_kind_of_line_break() { + assert_eq!(lines("A:1\r\nB:2\nC:3\rD:4", false), ["A:1", "B:2", "C:3", "D:4"]); + assert_eq!(lines("NOTE:Lunch \r\n with Ada\r\n", false), ["NOTE:Lunch with Ada"]); + assert_eq!(lines("NOTE:Lun\n\tch\n twice\n", false), ["NOTE:Lunch twice"]); + assert_eq!(lines("\u{feff}BEGIN:VCARD\r\n\r\nEND:VCARD", false), ["BEGIN:VCARD", "", "END:VCARD"]); + assert!(lines("", false).is_empty()); + } + + #[test] + fn version_2_1_keeps_the_white_space_of_a_fold() { + let text = "NOTE:This is a long\r\n description\r\n\tin three pieces\r\nFN:x"; + assert_eq!(lines(text, true), ["NOTE:This is a long description\tin three pieces", "FN:x"]); + assert_eq!(lines(text, false), ["NOTE:This is a longdescriptionin three pieces", "FN:x"]); + } + + #[test] + fn quoted_printable_soft_breaks_join_lines() { + let text = "NOTE;ENCODING=QUOTED-PRINTABLE:first=0D=0A=\r\nsecond =\r\n third= \r\nfourth\r\nFN:x\r\n"; + assert_eq!(lines(text, true), ["NOTE;ENCODING=QUOTED-PRINTABLE:first=0D=0Asecond thirdfourth", "FN:x"]); + // The bare 2.1 spelling, in lower case. + assert_eq!(lines("note;quoted-printable:a=\nb\nFN:x", false), ["note;quoted-printable:ab", "FN:x"]); + // Without the encoding an `=` at the end is just an `=`. + assert_eq!(lines("NOTE:1+1=\r\nFN:x", false), ["NOTE:1+1=", "FN:x"]); + // The parameters may themselves be folded before the `:` comes. + assert_eq!(lines("NOTE;CHARSET=UTF-8;\r\n ENCODING=QUOTED-PRINTABLE:a=\r\nb", false), ["NOTE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:ab"]); + // A soft break at the very end of a card doesn't eat the END line, and one at the end of the text is harmless. + assert_eq!(lines("NOTE;QUOTED-PRINTABLE:a=\r\nEND:VCARD\r\n", false), ["NOTE;QUOTED-PRINTABLE:a=", "END:VCARD"]); + assert_eq!(lines("NOTE;QUOTED-PRINTABLE:a=", false), ["NOTE;QUOTED-PRINTABLE:a="]); + } + + #[test] + fn parses_groups_names_parameters_and_values() { + let l = ContentLine::parse("Item1.email;type=INTERNET;Type=pref:nadia@example.org").unwrap(); + assert_eq!((l.group.as_str(), l.name.as_str(), l.value.as_str()), ("item1", "EMAIL", "nadia@example.org")); + assert_eq!(l.types(), ["INTERNET", "PREF"]); + let plain = ContentLine::parse("NOTE:Budget: phase two; hiring").unwrap(); + assert_eq!( + (plain.group.as_str(), plain.name.as_str(), plain.params.len(), plain.value.as_str()), + ("", "NOTE", 0, "Budget: phase two; hiring") + ); + assert_eq!(ContentLine::parse("FN:").unwrap().value, ""); + for bad in ["no colon here", ":value without a name", "item1.:x", ";WORK:x", "TEL;TYPE=\"never closed:1", " ", ""] { + assert_eq!(ContentLine::parse(bad), None, "{bad}"); + } + } + + #[test] + fn types_come_in_every_spelling() { + let types = |line: &str| ContentLine::parse(line).unwrap().types(); + assert_eq!(types("TEL;TYPE=WORK,VOICE:1"), ["WORK", "VOICE"]); + assert_eq!(types("TEL;type=work;type=voice:1"), ["WORK", "VOICE"]); + assert_eq!(types("TEL;WORK;VOICE:1"), ["WORK", "VOICE"]); + assert_eq!(types("TEL;VALUE=uri;TYPE=\"voice,cell\";PREF=1:tel:+1-555-0100"), ["VOICE", "CELL"]); + assert_eq!(types("TEL;TYPE=:1").len(), 0); + assert_eq!(types("TEL:1").len(), 0); + let bare = ContentLine::parse("NOTE;CHARSET=utf-8;QUOTED-PRINTABLE;;X-THING:a").unwrap(); + assert_eq!(bare.param("ENCODING"), Some("QUOTED-PRINTABLE")); + assert_eq!(bare.param("CHARSET"), Some("utf-8")); + assert_eq!(bare.types(), ["X-THING"]); + assert_eq!(ContentLine::parse("PHOTO;BASE64;JPEG:AAAA").unwrap().param("ENCODING"), Some("BASE64")); + } + + #[test] + fn folds_at_75_octets_without_splitting_characters() { + let line = format!("NOTE:{}", "é東🎉x".repeat(40)); + let mut out = String::new(); + fold(&mut out, &line); + let physical: Vec<&str> = out.trim_end_matches("\r\n").split("\r\n").collect(); + assert!(physical.len() > 3); + assert!(physical.iter().all(|l| l.len() <= 75), "{physical:?}"); + assert!(physical.iter().skip(1).all(|l| l.starts_with(' '))); + assert_eq!(lines(&out, false), [line]); + let mut exact = String::new(); + fold(&mut exact, &"x".repeat(76)); + assert_eq!(exact, format!("{}\r\n x\r\n", "x".repeat(75))); + } + + #[test] + fn enormous_lines_are_capped() { + let text = format!("PHOTO;ENCODING=b:{}\r\n {}\r\nFN:after", "A".repeat(MAX_LINE + 5000), "B".repeat(1000)); + let got = lines(&text, false); + assert_eq!(got.len(), 2); + assert!(got[0].len() <= MAX_LINE); + assert_eq!(got[1], "FN:after"); + // A quoted-printable value that never stops. + let endless = format!("NOTE;QUOTED-PRINTABLE:{}", "é=\r\n".repeat(700_000)); + let got = lines(&endless, true); + assert_eq!(got.len(), 1); + assert!(got[0].len() <= MAX_LINE); + assert_eq!(prefix("héllo", 2), "h"); + } + + proptest::proptest! { + #[test] + fn folding_then_unfolding_gives_the_line_back(line in "[^\r\n]{0,400}") { + let mut out = String::new(); + fold(&mut out, &line); + proptest::prop_assert!(out.split("\r\n").all(|l| l.len() <= 75)); + // A byte order mark at the very start of a text isn't part of its first line. + proptest::prop_assert_eq!(lines(&out, false), vec![line.strip_prefix('\u{feff}').unwrap_or(&line).to_string()]); + } + + #[test] + fn any_text_splits_into_lines(text in proptest::prelude::any::(), v21 in proptest::prelude::any::()) { + for line in lines(&text, v21) { + let _ = ContentLine::parse(&line).map(|l| l.types()); + } + } + } +} diff --git a/crates/vcard/src/model.rs b/crates/vcard/src/model.rs new file mode 100644 index 000000000..2fd6b4fa6 --- /dev/null +++ b/crates/vcard/src/model.rs @@ -0,0 +1,127 @@ +//! What a card holds: a contact, as the vCard properties this crate reads and writes. + +use chrono::NaiveDate; + +/// A value with a label (`work`, `home`, `mobile`). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Labeled { + pub label: String, + pub value: String, +} + +impl Labeled { + pub fn new(label: impl Into, value: impl Into) -> Labeled { + Labeled { label: label.into(), value: value.into() } + } +} + +/// A postal address (`ADR`). The street holds the post office box and the extended address too. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct PostalAddress { + pub label: String, + pub street: String, + pub city: String, + pub region: String, + pub postal_code: String, + pub country: String, +} + +impl PostalAddress { + /// The address on one line, skipping empty parts. + pub fn one_line(&self) -> String { + let city = [self.city.as_str(), self.region.as_str(), self.postal_code.as_str()] + .into_iter() + .filter(|p| !p.trim().is_empty()) + .collect::>() + .join(" "); + [self.street.as_str(), city.as_str(), self.country.as_str()].into_iter().filter(|p| !p.trim().is_empty()).collect::>().join(", ") + } + + pub fn is_empty(&self) -> bool { + self.one_line().is_empty() + } +} + +/// What a key on a card is. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum KeyKind { + /// An S/MIME certificate (X.509, DER). + #[default] + X509, + /// An OpenPGP public key, binary or ASCII-armoured, as the card had it. + OpenPgp, + /// Where the card says a key is ([`ContactKey::uri`]); never fetched. + Uri, +} + +/// A public key or certificate a card carries (`KEY`). The reader keeps only what has the shape +/// of a certificate or of an OpenPGP public key, never secret key material. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct ContactKey { + pub kind: KeyKind, + /// The key's bytes; empty for a [`KeyKind::Uri`]. + pub data: Vec, + /// For a [`KeyKind::Uri`]: the address the card gave (`https:`, `ldap:`, `openpgp4fpr:`). + pub uri: String, + /// For a [`KeyKind::Uri`]: what the card says is there, [`ContactKey::X509_MEDIA_TYPE`] or + /// [`ContactKey::OPENPGP_MEDIA_TYPE`]; empty when it doesn't say. + pub media_type: String, +} + +impl ContactKey { + /// Most bytes of one key kept. + pub const MAX_BYTES: usize = 64 * 1024; + /// Longest address of a key kept, in bytes. + pub const MAX_URI: usize = 2048; + /// The media type of an S/MIME certificate. + pub const X509_MEDIA_TYPE: &str = "application/pkix-cert"; + /// The media type of OpenPGP keys. + pub const OPENPGP_MEDIA_TYPE: &str = "application/pgp-keys"; +} + +/// One card. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Contact { + /// `FN`; when empty, [`Contact::name`] builds one. + pub display_name: String, + pub first: String, + pub middle: String, + pub last: String, + pub nickname: String, + pub company: String, + pub title: String, + pub department: String, + pub emails: Vec, + pub phones: Vec, + pub addresses: Vec, + pub websites: Vec, + pub birthday: Option, + pub notes: String, + pub categories: Vec, + /// `X-FAVORITE` (and the forms other address books write it in). + pub favorite: bool, + /// `KEY`: certificates and OpenPGP public keys, at most [`Contact::MAX_KEYS`]. + pub keys: Vec, + /// `UID`, kept so a written card updates the same contact elsewhere. + pub uid: String, +} + +impl Contact { + /// Most keys kept for one contact. + pub const MAX_KEYS: usize = 32; + + /// The name to show: the display name, else first and last, the company, or the first address. + pub fn name(&self) -> String { + if !self.display_name.trim().is_empty() { + return self.display_name.trim().to_string(); + } + let full = [self.first.trim(), self.middle.trim(), self.last.trim()].into_iter().filter(|p| !p.is_empty()).collect::>().join(" "); + if !full.is_empty() { + return full; + } + if !self.company.trim().is_empty() { + return self.company.trim().to_string(); + } + self.emails.first().map(|e| e.value.clone()).unwrap_or_default() + } +} diff --git a/crates/vcard/src/read.rs b/crates/vcard/src/read.rs new file mode 100644 index 000000000..faa117f45 --- /dev/null +++ b/crates/vcard/src/read.rs @@ -0,0 +1,778 @@ +//! Reading vCards (2.1, 3.0 and 4.0) into contacts. + +use std::collections::HashMap; + +use crate::{Contact, Labeled, PostalAddress}; +use chrono::NaiveDate; + +use crate::lines::{ContentLine, Lines}; +use crate::text::{base64_bytes, component, multi, quoted_printable, single, split, unescape}; + +/// Most cards read from one text. +const MAX_CARDS: usize = 100_000; +/// Most e-mail addresses, phone numbers, postal addresses or web sites kept per card. +const MAX_ITEMS: usize = 100; +const MAX_CATEGORIES: usize = 500; +/// Most group labels (`item1.X-ABLabel`) kept per card. +const MAX_LABELS: usize = 400; +/// Deepest nesting of cards followed (a 2.1 `AGENT` holds a card of its own). +const MAX_DEPTH: usize = 8; +/// Most bytes of keys (`KEY`) kept from one text, all its cards together. +pub(crate) const MAX_KEY_BYTES: usize = 64 << 20; + +/// Parse one or many vCards (2.1, 3.0, 4.0). Never fails: junk gives an empty list, and a card +/// with nothing in it is skipped. Ids are left 0. +/// +/// `FN` gives the display name; without one it stays empty and the model derives a name. A label +/// comes from `TYPE` (`work`, `home`, `other`, and for phones `mobile`, `fax`, `pager`), or from the +/// `X-ABLabel` of the property's group (`item1.X-ABLabel:Family`); it is empty when neither says +/// anything. An S/MIME certificate or OpenPGP public key (`KEY`) is kept as bytes when its bytes +/// have the shape of one, and the address of one as an address, never fetched; secret key +/// material is never kept (`crate::key`). A card with nothing but keys is skipped. Photos, logos, +/// sounds and unknown properties are skipped. +/// +/// Work is bounded: at most 100 000 cards are read, 100 e-mail addresses, phone numbers, postal +/// addresses and web sites per card, 32 keys of at most 64 KiB per card and 64 MiB of keys in all, +/// and what a line holds beyond 1 MiB is dropped. +/// +/// A file's bytes go through [`parse_bytes`], which reads raw 8-bit values in their `CHARSET`. +pub fn parse(text: &str) -> Vec { + parse_keeping(text, MAX_KEY_BYTES) +} + +/// [`parse`], keeping at most `key_room` bytes of keys. +pub(crate) fn parse_keeping(text: &str, mut key_room: usize) -> Vec { + let mut lines = Lines::new(text); + // The cards that are open, outermost first, and the finished ones with the order they began in. + let mut open: Vec = Vec::new(); + let mut done: Vec<(usize, Contact)> = Vec::new(); + let mut begun = 0usize; + while done.len() < MAX_CARDS { + let v21 = open.last().is_some_and(|card| card.v21); + let Some(raw) = lines.next_line(v21) else { break }; + let Some(line) = ContentLine::parse(&raw) else { continue }; + let is_card = line.value.trim().eq_ignore_ascii_case("VCARD"); + match (line.name.as_str(), open.last_mut()) { + ("BEGIN", _) if is_card => { + // A card that never ended ends where too many are open. + if open.len() >= MAX_DEPTH { + done.extend(open.pop().and_then(Card::finish)); + } + open.push(Card { order: begun, ..Default::default() }); + begun = begun.saturating_add(1); + } + ("END", _) if is_card => done.extend(open.pop().and_then(Card::finish)), + ("BEGIN", Some(card)) => card.foreign = card.foreign.saturating_add(1), + ("END", Some(card)) => card.foreign = card.foreign.saturating_sub(1), + (_, Some(card)) if card.foreign == 0 => card.apply(&line, &mut key_room), + _ => {} + } + } + // Cards without an END are still cards. + while let Some(card) = open.pop() { + done.extend(card.finish()); + } + done.sort_by_key(|(order, _)| *order); + done.into_iter().map(|(_, contact)| contact).take(MAX_CARDS).collect() +} + +/// Parse a vCard file as [`parse`] does, from its bytes: a value written as raw 8-bit bytes +/// (vCard 2.1, `FN;CHARSET=WINDOWS-1250:…`) is read in the `CHARSET` of its property, and one +/// without a `CHARSET` as UTF-8 when it is valid UTF-8, else as Windows-1252. +pub fn parse_bytes(bytes: &[u8]) -> Vec { + parse(&crate::lines::text_from_bytes(bytes)) +} + +/// A card being read. +#[derive(Default)] +struct Card { + /// Its place among the cards of the text. + order: usize, + /// `VERSION:2.1`: other folding, no comma lists. + v21: bool, + /// How deep inside a component that isn't a card the reader is. + foreign: u32, + contact: Contact, + named: bool, + /// E-mail addresses, phones and postal addresses, each with the group it was written in. + emails: Vec<(String, Labeled)>, + phones: Vec<(String, Labeled)>, + addresses: Vec<(String, PostalAddress)>, + /// `X-ABLabel` by group. + labels: HashMap, +} + +impl Card { + /// Take in one property; `key_room` is what keys may still take of the text's allowance. + fn apply(&mut self, line: &ContentLine, key_room: &mut usize) { + let v21 = self.v21; + let contact = &mut self.contact; + // One line of text, decoded and unescaped. + let one = |line: &ContentLine| single(&unescape(&decoded(line))); + match line.name.as_str() { + "VERSION" => self.v21 = line.value.trim().starts_with("2."), + "FN" if contact.display_name.is_empty() => contact.display_name = one(line), + "N" if !self.named => { + self.named = true; + let value = decoded(line); + let parts = split(&value, ';', 8); + let part = |i: usize| parts.get(i).map(|p| component(p, " ", v21)).unwrap_or_default(); + (contact.last, contact.first, contact.middle) = (part(0), part(1), part(2)); + } + "NICKNAME" if contact.nickname.is_empty() => contact.nickname = one(line), + "ORG" if contact.company.is_empty() && contact.department.is_empty() => { + let value = decoded(line); + let parts = split(&value, ';', 16); + contact.company = parts.first().map(|p| component(p, ", ", v21)).unwrap_or_default(); + contact.department = parts.iter().skip(1).map(|p| component(p, ", ", v21)).filter(|p| !p.is_empty()).collect::>().join(", "); + } + "TITLE" if contact.title.is_empty() => contact.title = one(line), + "EMAIL" if self.emails.len() < MAX_ITEMS => { + let value = without_scheme(&one(line), "mailto:"); + if !value.is_empty() { + self.emails.push((line.group.clone(), Labeled::new(place_label(&line.types()), value))); + } + } + "TEL" if self.phones.len() < MAX_ITEMS => { + let value = without_scheme(&one(line), "tel:"); + if !value.is_empty() { + self.phones.push((line.group.clone(), Labeled::new(phone_label(&line.types()), value))); + } + } + "ADR" if self.addresses.len() < MAX_ITEMS => { + let value = decoded(line); + let parts = split(&value, ';', 8); + let part = |i: usize| parts.get(i).map(|p| component(p, ", ", v21)).unwrap_or_default(); + // Post office box, extended address, street: the lines above the city. + let street = [part(0), part(1), part(2)].into_iter().filter(|p| !p.is_empty()).collect::>().join(", "); + let address = PostalAddress { + label: place_label(&line.types()).into(), + street, + city: part(3), + region: part(4), + postal_code: part(5), + country: part(6), + }; + if !address.is_empty() { + self.addresses.push((line.group.clone(), address)); + } + } + "URL" if contact.websites.len() < MAX_ITEMS => { + let value = one(line); + if !value.is_empty() { + contact.websites.push(value); + } + } + "BDAY" if contact.birthday.is_none() => contact.birthday = birthday(&line.value), + "NOTE" => { + let note = multi(&unescape(&decoded(line))); + if !note.is_empty() { + if !contact.notes.is_empty() { + contact.notes.push('\n'); + } + contact.notes.push_str(¬e); + } + } + "CATEGORIES" => { + let value = decoded(line); + let room = MAX_CATEGORIES.saturating_sub(contact.categories.len()); + contact + .categories + .extend(split(&value, ',', MAX_CATEGORIES).into_iter().map(|c| single(&unescape(c))).filter(|c| !c.is_empty()).take(room)); + } + "UID" if contact.uid.is_empty() => contact.uid = one(line), + "KEY" if contact.keys.len() < Contact::MAX_KEYS && *key_room > 0 => { + if let Some(key) = crate::key::read(line) { + let cost = key.data.len().saturating_add(key.uri.len()); + if cost <= *key_room && !contact.keys.contains(&key) { + *key_room = key_room.saturating_sub(cost); + contact.keys.push(key); + } + } + } + "X-FAVORITE" | "X-MAILCRAFT-FAVORITE" => { + contact.favorite |= matches!(line.value.trim().to_ascii_uppercase().as_str(), "TRUE" | "YES" | "1") + } + "X-ABLABEL" if !line.group.is_empty() && self.labels.len() < MAX_LABELS => { + let label = one(line); + // The built-in labels of the program that invented this are wrapped: `_$!!$_`. + let label = label.strip_prefix("_$!<").and_then(|l| l.strip_suffix(">!$_")).map(str::to_lowercase).unwrap_or(label); + self.labels.entry(line.group.clone()).or_insert(label); + } + _ => {} + } + } + + /// The contact with the place it began at; `None` for a card with nothing in it. + fn finish(self) -> Option<(usize, Contact)> { + let Card { order, mut contact, emails, phones, addresses, labels, .. } = self; + // A label given to a group says more than the property's type. + let label = |group: &str, own: String| labels.get(group).filter(|_| !group.is_empty()).cloned().unwrap_or(own); + contact.emails = emails.into_iter().map(|(group, e)| Labeled { label: label(&group, e.label), value: e.value }).collect(); + contact.phones = phones.into_iter().map(|(group, p)| Labeled { label: label(&group, p.label), value: p.value }).collect(); + contact.addresses = addresses.into_iter().map(|(group, a)| PostalAddress { label: label(&group, a.label.clone()), ..a }).collect(); + // Keys alone make no contact: a card of nothing but keys is skipped as an empty one is. + let keys = std::mem::take(&mut contact.keys); + let empty = contact == Contact::default(); + contact.keys = keys; + (!empty).then_some((order, contact)) + } +} + +/// The value with its `ENCODING` (quoted-printable or base64) and `CHARSET` undone; still escaped. +fn decoded(line: &ContentLine) -> String { + let bytes = match line.param("ENCODING").unwrap_or("").to_ascii_uppercase().as_str() { + "QUOTED-PRINTABLE" | "Q" => quoted_printable(&line.value), + "BASE64" | "B" => base64_bytes(&line.value), + _ => return line.value.clone(), + }; + craft_mime::charset::decode(&bytes, line.param("CHARSET").unwrap_or("")) +} + +/// `tel:+1-555-0100` and `mailto:a@example.org` (vCard 4.0 URIs) without the scheme. +fn without_scheme(value: &str, scheme: &str) -> String { + match value.get(..scheme.len()) { + Some(start) if start.eq_ignore_ascii_case(scheme) => value.get(scheme.len()..).unwrap_or("").trim().to_string(), + _ => value.to_string(), + } +} + +/// The label of an e-mail or postal address. +fn place_label(types: &[String]) -> &'static str { + let has = |t: &str| types.iter().any(|x| x == t); + if has("WORK") { + "work" + } else if has("HOME") { + "home" + } else if has("OTHER") { + "other" + } else { + "" + } +} + +/// The label of a phone number: what kind of device it is says more than where it stands. +fn phone_label(types: &[String]) -> &'static str { + let has = |t: &str| types.iter().any(|x| x == t); + if has("CELL") || has("MOBILE") || has("IPHONE") { + "mobile" + } else if has("FAX") { + "fax" + } else if has("PAGER") { + "pager" + } else { + place_label(types) + } +} + +/// `1985-04-12`, `19850412`, or either with a time after it; `None` without a year (`--0412`). +fn birthday(value: &str) -> Option { + parse_iso(value.trim().split(['T', 't', ' ']).next()?) +} + +/// A date from `YYYY-MM-DD` or `YYYYMMDD`, years 1 to 9999. +fn parse_iso(s: &str) -> Option { + let s = s.trim(); + let digits: String = s.chars().filter(char::is_ascii_digit).collect(); + if digits.len() != 8 || s.chars().any(|c| !c.is_ascii_digit() && c != '-') { + return None; + } + let year: i32 = digits.get(0..4)?.parse().ok()?; + if year < 1 { + return None; + } + NaiveDate::from_ymd_opt(year, digits.get(4..6)?.parse().ok()?, digits.get(6..8)?.parse().ok()?) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn d(y: i32, m: u32, day: u32) -> NaiveDate { + NaiveDate::from_ymd_opt(y, m, day).unwrap() + } + + /// One contact from the properties of a 3.0 card. + fn card(properties: &str) -> Contact { + let got = parse(&format!("BEGIN:VCARD\r\nVERSION:3.0\r\n{properties}\r\nEND:VCARD\r\n")); + assert_eq!(got.len(), 1, "{properties}"); + got.into_iter().next().unwrap() + } + + /// A vCard 2.1 card as old phones and mail programs write them: bare type parameters, + /// quoted-printable with soft line breaks, a charset. + const V21: &str = "BEGIN:VCARD\r\n\ +VERSION:2.1\r\n\ +N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:M=C3=BCller;J=C3=BCrgen;;Dr.;\r\n\ +FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:Dr. J=C3=BCrgen M=C3=BCller\r\n\ +ORG:Beispiel Werke, GmbH;Entwicklung\r\n\ +TITLE;CHARSET=ISO-8859-1;ENCODING=QUOTED-PRINTABLE:Gesch=E4ftsf=FChrer\r\n\ +TEL;WORK;VOICE:+49 30 5550100\r\n\ +TEL;CELL:+49 170 5550101\r\n\ +TEL;WORK;FAX:+49 30 5550199\r\n\ +TEL;PAGER:+49 30 5550177\r\n\ +EMAIL;PREF;INTERNET:juergen.mueller@example.org\r\n\ +EMAIL;INTERNET;HOME:jm@privat.example.org\r\n\ +ADR;HOME;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;;Gartenstra=C3=9Fe 12;K=C3=B6ln;NW;50667;Deutschland\r\n\ +LABEL;HOME;ENCODING=QUOTED-PRINTABLE:Gartenstra=C3=9Fe 12=0D=0A50667 K=C3=B6ln\r\n\ +NOTE;ENCODING=QUOTED-PRINTABLE:Erste Zeile=0D=0AZweite Zeile, mit einem sehr langen Text der =\r\n\ +umbrochen wird=3B und noch einmal =\r\n\ +umbrochen.\r\n\ +URL;WORK:http://example.org/~jm\r\n\ +BDAY:19720314\r\n\ +REV:20260901T101500Z\r\n\ +END:VCARD\r\n"; + + #[test] + fn reads_a_2_1_card() { + let got = parse(V21); + assert_eq!(got.len(), 1); + assert_eq!( + got[0], + Contact { + display_name: "Dr. Jürgen Müller".into(), + first: "Jürgen".into(), + last: "Müller".into(), + company: "Beispiel Werke, GmbH".into(), + department: "Entwicklung".into(), + title: "Geschäftsführer".into(), + emails: vec![Labeled::new("", "juergen.mueller@example.org"), Labeled::new("home", "jm@privat.example.org")], + phones: vec![ + Labeled::new("work", "+49 30 5550100"), + Labeled::new("mobile", "+49 170 5550101"), + Labeled::new("fax", "+49 30 5550199"), + Labeled::new("pager", "+49 30 5550177") + ], + addresses: vec![PostalAddress { + label: "home".into(), + street: "Gartenstraße 12".into(), + city: "Köln".into(), + region: "NW".into(), + postal_code: "50667".into(), + country: "Deutschland".into() + }], + websites: vec!["http://example.org/~jm".into()], + birthday: Some(d(1972, 3, 14)), + notes: "Erste Zeile\nZweite Zeile, mit einem sehr langen Text der umbrochen wird; und noch einmal umbrochen.".into(), + ..Default::default() + } + ); + } + + /// A vCard 3.0 card as address books export them: grouped properties with custom labels, a + /// photo, folded lines. + const V30: &str = "BEGIN:VCARD\r\n\ +VERSION:3.0\r\n\ +PRODID:-//Example//Contacts 12.0//EN\r\n\ +N:Okafor;Nadia;Chidi,Rose;Dr.;\r\n\ +FN:Dr. Nadia Okafor\r\n\ +NICKNAME:Nadi\r\n\ +ORG:Example Research\\, Inc.;Field Lab;Unit 7\r\n\ +TITLE:Lead Scientist\r\n\ +item1.EMAIL;type=INTERNET;type=WORK;type=pref:nadia.okafor@example.org\r\n\ +item2.EMAIL;type=INTERNET:nadia@family.example.org\r\n\ +item2.X-ABLabel:Family\r\n\ +TEL;type=CELL;type=VOICE;type=pref:+1 (555) 010-0142\r\n\ +item3.TEL:+1 555 010 0143\r\n\ +item3.X-ABLabel:_$!!$_\r\n\ +TEL;TYPE=WORK,FAX:+1 555 010 0144\r\n\ +item4.ADR;type=WORK;type=pref:;Suite 300;41 Harbor Road;Riverton;CA;94000;USA\r\n\ +item4.X-ABADR:us\r\n\ +item5.URL;type=pref:https\\://example.org/~nadia\r\n\ +item5.X-ABLabel:_$!!$_\r\n\ +BDAY:1985-04-12\r\n\ +NOTE:Met at the 2024 symposium.\\nPrefers e-mail\\, not calls; see the long thread about the field s\r\n tation.\r\n\ +CATEGORIES:Research,Field Team\\, West\r\n\ +PHOTO;ENCODING=b;TYPE=JPEG:AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1\r\n Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1u\r\n b3BxcnN0dXZ3eHl6e3x9fn8=\r\n\ +UID:7f1c2f6e-0001-4a5b-9c3d-000000000042\r\n\ +X-FAVORITE:TRUE\r\n\ +REV:2026-09-01T10:15:00Z\r\n\ +END:VCARD\r\n"; + + #[test] + fn reads_a_3_0_card_with_grouped_properties() { + let got = parse(V30); + assert_eq!(got.len(), 1); + assert_eq!( + got[0], + Contact { + display_name: "Dr. Nadia Okafor".into(), + first: "Nadia".into(), + middle: "Chidi Rose".into(), + last: "Okafor".into(), + nickname: "Nadi".into(), + company: "Example Research, Inc.".into(), + department: "Field Lab, Unit 7".into(), + title: "Lead Scientist".into(), + emails: vec![Labeled::new("work", "nadia.okafor@example.org"), Labeled::new("Family", "nadia@family.example.org")], + phones: vec![ + Labeled::new("mobile", "+1 (555) 010-0142"), + Labeled::new("other", "+1 555 010 0143"), + Labeled::new("fax", "+1 555 010 0144") + ], + addresses: vec![PostalAddress { + label: "work".into(), + street: "Suite 300, 41 Harbor Road".into(), + city: "Riverton".into(), + region: "CA".into(), + postal_code: "94000".into(), + country: "USA".into() + }], + websites: vec!["https://example.org/~nadia".into()], + birthday: Some(d(1985, 4, 12)), + notes: "Met at the 2024 symposium.\nPrefers e-mail, not calls; see the long thread about the field station.".into(), + categories: vec!["Research".into(), "Field Team, West".into()], + favorite: true, + uid: "7f1c2f6e-0001-4a5b-9c3d-000000000042".into(), + ..Default::default() + } + ); + } + + /// A vCard 4.0 card: URIs for phones, quoted type lists, no `N` needed. + const V40: &str = "BEGIN:VCARD\r\n\ +VERSION:4.0\r\n\ +KIND:individual\r\n\ +FN:Mei Tanaka\r\n\ +N:Tanaka;Mei;;;\r\n\ +EMAIL;TYPE=work;PREF=1:mei.tanaka@example.org\r\n\ +EMAIL;TYPE=home:mailto:mei@home.example.org\r\n\ +TEL;VALUE=uri;TYPE=\"voice,cell\";PREF=1:tel:+81-90-5550-0101\r\n\ +TEL;VALUE=uri;TYPE=work:tel:+81-3-5550-0102;ext=12\r\n\ +ADR;TYPE=home;LABEL=\"1-2-3 Sakura\\nMinato, Tokyo\":;;1-2-3 Sakura;Minato;Tokyo;105-0000;Japan\r\n\ +BDAY:19910704T080000Z\r\n\ +ANNIVERSARY:20150620\r\n\ +GENDER:F\r\n\ +LANG;PREF=1:ja\r\n\ +URL:https://example.org/mei\r\n\ +URL;TYPE=work:https://design.example.org/team/mei\r\n\ +NOTE:Speaks Japanese\\, English.\r\n\ +NOTE:Second note.\r\n\ +CATEGORIES:Design,Tokyo office\r\n\ +CATEGORIES:Speakers\r\n\ +UID:urn:uuid:0d5c7e0a-0000-4000-8000-0000000000a1\r\n\ +X-MAILCRAFT-FAVORITE:true\r\n\ +END:VCARD\r\n"; + + #[test] + fn reads_a_4_0_card() { + let got = parse(V40); + assert_eq!(got.len(), 1); + assert_eq!( + got[0], + Contact { + display_name: "Mei Tanaka".into(), + first: "Mei".into(), + last: "Tanaka".into(), + emails: vec![Labeled::new("work", "mei.tanaka@example.org"), Labeled::new("home", "mei@home.example.org")], + phones: vec![Labeled::new("mobile", "+81-90-5550-0101"), Labeled::new("work", "+81-3-5550-0102;ext=12")], + addresses: vec![PostalAddress { + label: "home".into(), + street: "1-2-3 Sakura".into(), + city: "Minato".into(), + region: "Tokyo".into(), + postal_code: "105-0000".into(), + country: "Japan".into() + }], + websites: vec!["https://example.org/mei".into(), "https://design.example.org/team/mei".into()], + birthday: Some(d(1991, 7, 4)), + notes: "Speaks Japanese, English.\nSecond note.".into(), + categories: vec!["Design".into(), "Tokyo office".into(), "Speakers".into()], + favorite: true, + uid: "urn:uuid:0d5c7e0a-0000-4000-8000-0000000000a1".into(), + ..Default::default() + } + ); + } + + #[test] + fn reads_many_cards_in_order() { + let text = format!("{V21}\r\n{V30}junk between cards\r\n{V40}"); + let names: Vec = parse(&text).into_iter().map(|c| c.display_name).collect(); + assert_eq!(names, ["Dr. Jürgen Müller", "Dr. Nadia Okafor", "Mei Tanaka"]); + // LF only, lower case, no VERSION. + let plain = parse("begin:vcard\nfn:Ada Lovelace\nemail:ada@example.org\nend:vcard\nBEGIN:VCARD\nFN:Grace Hopper\nEND:VCARD\n"); + assert_eq!(plain.len(), 2); + assert_eq!((plain[0].display_name.as_str(), plain[0].emails[0].value.as_str()), ("Ada Lovelace", "ada@example.org")); + } + + #[test] + fn a_missing_name_leaves_the_display_name_empty() { + let c = card("N:Lovelace;Ada;;;\nEMAIL:ada@example.org"); + assert_eq!(c.display_name, ""); + assert_eq!(c.name(), "Ada Lovelace"); + let org = card("ORG:Analytical Engines Ltd"); + assert_eq!((org.display_name.as_str(), org.name().as_str()), ("", "Analytical Engines Ltd")); + // The first FN, N and so on count. + let twice = card("FN:First\nFN:Second\nN:A;B\nN:C;D\nTITLE:One\nTITLE:Two\nUID:1\nUID:2\nBDAY:2000-01-02\nBDAY:1999-01-02"); + assert_eq!( + (twice.display_name.as_str(), twice.last.as_str(), twice.title.as_str(), twice.uid.as_str(), twice.birthday), + ("First", "A", "One", "1", Some(d(2000, 1, 2))) + ); + } + + #[test] + fn labels_come_from_types_and_groups() { + let c = card( + "EMAIL;TYPE=INTERNET,OTHER:o@example.org\nEMAIL;TYPE=x-custom:x@example.org\nEMAIL;TYPE=HOME,WORK:hw@example.org\n\ +TEL;TYPE=HOME,VOICE:1\nTEL;TYPE=WORK,CELL:2\nTEL;TYPE=iPhone:3\nTEL;TYPE=HOME;TYPE=FAX:4\nTEL;TYPE=OTHER:5\nTEL;TYPE=VOICE:6\nTEL:7\n\ +Item7.TEL;TYPE=CELL:8\nITEM7.X-ABLabel:Car\nitem8.TEL:9\nitem8.X-ABLabel:_$!!$_\nitem8.X-ABLabel:Ignored\nX-ABLabel:No group\nitem9.X-ABLabel:Nothing to label\n\ +ADR;TYPE=dom,postal,parcel:;;1 Main St;Riverton\nitem2.ADR:;;2 Side St\nitem2.X-ABLabel:Cabin", + ); + let labels = |list: &[Labeled]| list.iter().map(|l| l.label.clone()).collect::>(); + assert_eq!(labels(&c.emails), ["other", "", "work"]); + assert_eq!(labels(&c.phones), ["home", "mobile", "mobile", "fax", "other", "", "", "Car", "mobile"]); + assert_eq!( + c.addresses.iter().map(|a| (a.label.as_str(), a.street.as_str())).collect::>(), + [("", "1 Main St"), ("Cabin", "2 Side St")] + ); + } + + #[test] + fn names_and_organisations_split_on_unescaped_separators() { + let c = card("N:van der Berg\\; Jr.;Anna\\,Maria;Q.,R.;;\nORG:Semi\\;colon & Co;R\\&D;;Lab 2\nNICKNAME:Annie\\, Am"); + assert_eq!((c.last.as_str(), c.first.as_str(), c.middle.as_str()), ("van der Berg; Jr.", "Anna,Maria", "Q. R.")); + assert_eq!((c.company.as_str(), c.department.as_str()), ("Semi;colon & Co", "R\\&D, Lab 2")); + assert_eq!(c.nickname, "Annie, Am"); + // Fewer parts than the format has, and none at all. + assert_eq!(card("N:Lovelace").last, "Lovelace"); + assert_eq!(card("N:;;;;\nFN:x").first, ""); + assert_eq!(card("ORG:;Only a department").department, "Only a department"); + // In 2.1 a comma is a comma. + let old = parse("BEGIN:VCARD\nVERSION:2.1\nN:Smith, Jr.;John\nADR:;;1 Main St, Apt 4;Riverton\nEND:VCARD\n").remove(0); + assert_eq!((old.last.as_str(), old.addresses[0].street.as_str()), ("Smith, Jr.", "1 Main St, Apt 4")); + } + + #[test] + fn addresses_join_the_lines_above_the_city() { + let c = + card("ADR:PO Box 9;Floor 2;41 Harbor Road,Building C;Riverton;CA;94000;USA\nADR:;;;;;;\nADR;TYPE=HOME:;;;;;;Japan\nADR:;;Just a street"); + assert_eq!(c.addresses.len(), 3); + assert_eq!(c.addresses[0].street, "PO Box 9, Floor 2, 41 Harbor Road, Building C"); + assert_eq!(c.addresses[0].one_line(), "PO Box 9, Floor 2, 41 Harbor Road, Building C, Riverton CA 94000, USA"); + assert_eq!((c.addresses[1].label.as_str(), c.addresses[1].country.as_str()), ("home", "Japan")); + assert_eq!(c.addresses[2].street, "Just a street"); + } + + #[test] + fn birthdays_in_every_form() { + for (text, want) in [ + ("1985-04-12", Some(d(1985, 4, 12))), + ("19850412", Some(d(1985, 4, 12))), + ("1985-04-12T10:30:00Z", Some(d(1985, 4, 12))), + ("19850412T103000", Some(d(1985, 4, 12))), + (" 1985-04-12 10:30 ", Some(d(1985, 4, 12))), + ("--0412", None), + ("1985-02-30", None), + ("spring", None), + ("", None), + ] { + assert_eq!(birthday(text), want, "{text}"); + } + assert_eq!(card("FN:x\nBDAY;VALUE=date:2000-02-29").birthday, Some(d(2000, 2, 29))); + // A later valid one counts when the first is junk. + assert_eq!(card("FN:x\nBDAY:--0412\nBDAY:1985-04-12").birthday, Some(d(1985, 4, 12))); + } + + #[test] + fn favorites_and_single_line_fields() { + for (line, want) in [ + ("X-FAVORITE:TRUE", true), + ("X-MAILCRAFT-FAVORITE:true", true), + ("X-FAVORITE:1", true), + ("X-FAVORITE: yes ", true), + ("X-FAVORITE:FALSE", false), + ("X-FAVORITE:", false), + ] { + assert_eq!(card(&format!("FN:x\n{line}")).favorite, want, "{line}"); + } + assert!(card("FN:x\nX-FAVORITE:TRUE\nX-FAVORITE:FALSE").favorite); + // Line breaks have no place in a name, an address or a number. + let c = + card("FN: Ada\\nLovelace \nEMAIL: ada@example.org \nTEL:+1 555\\n0100\nURL: https://example.org \nUID: abc \nCATEGORIES: a , ,b\\nc "); + assert_eq!(c.display_name, "Ada Lovelace"); + assert_eq!( + (c.emails[0].value.as_str(), c.phones[0].value.as_str(), c.websites[0].as_str(), c.uid.as_str()), + ("ada@example.org", "+1 555 0100", "https://example.org", "abc") + ); + assert_eq!(c.categories, ["a", "b c"]); + // Empty values are not entries. + let empty = card("FN:x\nEMAIL:\nEMAIL;TYPE=WORK: \nTEL:\nURL:\nNOTE:\nCATEGORIES:,,"); + assert!( + empty.emails.is_empty() && empty.phones.is_empty() && empty.websites.is_empty() && empty.categories.is_empty() && empty.notes.is_empty() + ); + } + + #[test] + fn base64_text_and_unknown_encodings() { + // "Hello, world" as an old 2.1 writer and a 3.0 writer encode it. + assert_eq!(card("NOTE;ENCODING=BASE64:SGVsbG8sIHdvcmxk").notes, "Hello, world"); + assert_eq!(card("NOTE;ENCODING=b:SGVsbG8s\n IHdvcmxk").notes, "Hello, world"); + assert_eq!(card("NOTE;ENCODING=8BIT:as it is").notes, "as it is"); + assert_eq!(card("FN:x\nNOTE;ENCODING=b:!!!").notes, ""); + // Quoted-printable in a named Western charset, and in none. + assert_eq!(card("FN;CHARSET=windows-1252;ENCODING=QUOTED-PRINTABLE:Zo=EB =96 caf=E9").display_name, "Zoë – café"); + assert_eq!(card("FN;QUOTED-PRINTABLE:Zo=EB").display_name, "Zoë"); + assert_eq!(card("FN;QUOTED-PRINTABLE:Zo=C3=AB").display_name, "Zoë"); + } + + #[test] + fn central_european_charsets() { + // A 2.1 card from a Central European phone or mail program, in Windows-1250 and in + // ISO-8859-2, was read as Windows-1252 or UTF-8. + let cards = parse( + "BEGIN:VCARD\r\n\ +VERSION:2.1\r\n\ +N;CHARSET=windows-1250;ENCODING=QUOTED-PRINTABLE:=8Eagar;=D0ur=F0ica;;;\r\n\ +FN;CHARSET=windows-1250;ENCODING=QUOTED-PRINTABLE:=D0ur=F0ica =8Eagar\r\n\ +ORG;CHARSET=windows-1250;ENCODING=QUOTED-PRINTABLE:Gvo=9E=F0e d.o.o.\r\n\ +NOTE;CHARSET=windows-1250;ENCODING=QUOTED-PRINTABLE:We=92ve met =96 twice, in =8Aibenik =\r\n\ +and =C8akovec.\r\n\ +EMAIL;INTERNET:durdica@example.org\r\n\ +END:VCARD\r\n\ +BEGIN:VCARD\r\n\ +VERSION:2.1\r\n\ +N;CHARSET=ISO-8859-2;ENCODING=QUOTED-PRINTABLE:Kova=E8i=E6;=AEeljko;;;\r\n\ +FN;CHARSET=ISO-8859-2;ENCODING=QUOTED-PRINTABLE:=AEeljko Kova=E8i=E6\r\n\ +TITLE;CHARSET=iso-8859-2;ENCODING=QUOTED-PRINTABLE:Gvo=BE=F0e i =B9ljunak\r\n\ +EMAIL;INTERNET:zeljko@example.org\r\n\ +END:VCARD\r\n", + ); + assert_eq!(cards.len(), 2); + let (first, second) = (&cards[0], &cards[1]); + assert_eq!((first.display_name.as_str(), first.first.as_str(), first.last.as_str()), ("Đurđica Žagar", "Đurđica", "Žagar")); + assert_eq!(first.company, "Gvožđe d.o.o."); + assert_eq!(first.notes, "We’ve met – twice, in Šibenik and Čakovec."); + assert_eq!((second.display_name.as_str(), second.first.as_str(), second.last.as_str()), ("Željko Kovačić", "Željko", "Kovačić")); + assert_eq!(second.title, "Gvožđe i šljunak"); + // A set no standard names is read as UTF-8, as mail is. + assert_eq!(card("FN;CHARSET=x-unknown;ENCODING=QUOTED-PRINTABLE:Zo=C3=AB").display_name, "Zoë"); + } + + #[test] + fn raw_8_bit_values_are_read_in_their_charset() { + // Raw Windows-1250 bytes (not quoted-printable) were read as UTF-8 before the charset + // was looked at, and đ ć č Š became U+FFFD. + let cards = parse_bytes( + b"BEGIN:VCARD\r\nVERSION:2.1\r\n\ +N;CHARSET=WINDOWS-1250:Gvo\xf0\xe6i\xe8;Ivana;;;\r\n\ +FN;CHARSET=WINDOWS-1250:Ivana Gvo\xf0\xe6i\xe8\r\n\ +ORG;CHARSET=WINDOWS-1250:\x8akola \xe8e\x9a\xe6e\r\n\ +NOTE;CHARSET=\"iso-8859-2\":Gvo\xbe\xf0e\r\n \xae\xb9\xe8\xe6\r\n\ +TITLE;CHARSET=windows-1250;ENCODING=QUOTED-PRINTABLE:=8Aef\r\n\ +X-SOMETHING:\xff\r\n\ +EMAIL;INTERNET:ivana@example.org\r\n\ +END:VCARD\r\n", + ); + assert_eq!(cards.len(), 1); + let c = &cards[0]; + assert_eq!((c.display_name.as_str(), c.last.as_str(), c.first.as_str()), ("Ivana Gvođćič", "Gvođćič", "Ivana")); + assert_eq!(c.company, "Škola češće"); + // A folded line goes on in the charset of the property it continues (2.1 keeps the space). + assert_eq!(c.notes, "Gvožđe Žščć"); + assert_eq!(c.title, "Šef"); + assert_eq!(c.emails[0].value, "ivana@example.org"); + // Without a charset: UTF-8 when it is UTF-8, else Windows-1252; in UTF-8 nothing changes. + assert_eq!(parse_bytes(b"BEGIN:VCARD\nFN:Zo\xc3\xab\nEND:VCARD")[0].display_name, "Zoë"); + assert_eq!(parse_bytes(b"BEGIN:VCARD\nFN:Zo\xeb \x96 caf\xe9\nEND:VCARD")[0].display_name, "Zoë – café"); + assert_eq!(parse_bytes("\u{feff}BEGIN:VCARD\r\nFN;CHARSET=UTF-8:Đurđica\r\nEND:VCARD".as_bytes())[0].display_name, "Đurđica"); + // A file in UTF-16 with its byte order mark. + let utf16: Vec = "\u{feff}BEGIN:VCARD\r\nFN:Željko\r\nEND:VCARD\r\n".encode_utf16().flat_map(u16::to_le_bytes).collect(); + assert_eq!(parse_bytes(&utf16)[0].display_name, "Željko"); + // Bytes and text give the same cards. + let text = crate::write(&cards.iter().collect::>()); + assert_eq!(parse_bytes(text.as_bytes()), parse(&text)); + } + + #[test] + fn skipped_properties_cost_nothing() { + let photo = "A".repeat(3 << 20); + let c = card(&format!( + "FN:Ada\nPHOTO;ENCODING=b;TYPE=JPEG:{photo}\nLOGO;VALUE=uri:https://example.org/logo.png\nSOUND;ENCODING=QUOTED-PRINTABLE:=41=42\nKEY;TYPE=PGP:{photo}\nX-UNKNOWN;ENCODING=b:{photo}\nTITLE:After" + )); + assert_eq!((c.display_name.as_str(), c.title.as_str()), ("Ada", "After")); + assert_eq!(c, Contact { display_name: "Ada".into(), title: "After".into(), ..Default::default() }); + } + + #[test] + fn junk_gives_an_empty_list() { + for junk in [ + "", + "\r\n\r\n", + "hello", + "BEGIN", + "BEGIN:", + "END:VCARD", + "BEGIN:VCARD", + "BEGIN:VCARD\nEND:VCARD", + "BEGIN:VCARD\nVERSION:3.0\nN:;;;;\nFN:\nEND:VCARD", + "FN:Nobody\nEMAIL:n@example.org", + "\u{0}\u{1}\u{2}\u{fffd}PK\u{3}\u{4}", + "::::;;;;====", + "BEGIN:VCALENDAR\nBEGIN:VEVENT\nSUMMARY:x\nEND:VEVENT\nEND:VCALENDAR", + ] { + assert!(parse(junk).is_empty(), "{junk:?}"); + } + let binary: String = (0u32..2000).filter_map(|i| char::from_u32(i.wrapping_mul(2_654_435_761) % 0x11_0000)).collect(); + assert!(parse(&binary).is_empty()); + } + + #[test] + fn a_missing_end_loses_nothing() { + // The text stops in the middle of the second card. + let cut = parse("BEGIN:VCARD\nFN:Ada\nEND:VCARD\nBEGIN:VCARD\nFN:Grace\nEMAIL:grace@exam"); + assert_eq!(cut.iter().map(|c| c.display_name.as_str()).collect::>(), ["Ada", "Grace"]); + assert_eq!(cut[1].emails[0].value, "grace@exam"); + // A card that never ends, and two that do: each keeps its own lines, in the order they began. + let run_on = parse("BEGIN:VCARD\nFN:Ada\nBEGIN:VCARD\nFN:Grace\nTITLE:Admiral\nEND:VCARD\nBEGIN:VCARD\nFN:Mei\nEND:VCARD\n"); + assert_eq!( + run_on.iter().map(|c| (c.display_name.as_str(), c.title.as_str())).collect::>(), + [("Ada", ""), ("Grace", "Admiral"), ("Mei", "")] + ); + } + + #[test] + fn nested_cards_and_foreign_components_are_contained() { + // A 2.1 AGENT holds a whole card; the outer card goes on after it. + let agent = parse( + "BEGIN:VCARD\nVERSION:2.1\nFN:Nadia Okafor\nAGENT:\nBEGIN:VCARD\nVERSION:2.1\nFN:Tomas Lindqvist\nTEL;WORK:+1 555 0100\nEND:VCARD\nTITLE:Director\nEND:VCARD\n", + ); + assert_eq!(agent.len(), 2); + assert_eq!((agent[0].display_name.as_str(), agent[0].title.as_str(), agent[0].phones.len()), ("Nadia Okafor", "Director", 0)); + assert_eq!((agent[1].display_name.as_str(), agent[1].title.as_str(), agent[1].phones.len()), ("Tomas Lindqvist", "", 1)); + // Something that isn't a card keeps its lines to itself, ended or not. + let foreign = parse( + "BEGIN:VCARD\nFN:Ada\nBEGIN:X-EXTRA\nTITLE:Inner\nBEGIN:X-DEEPER\nNOTE:Inner\nEND:X-DEEPER\nEND:X-EXTRA\nTITLE:Outer\nBEGIN:X-OPEN\nNOTE:Inner\nEND:VCARD\n", + ); + assert_eq!(foreign, [Contact { display_name: "Ada".into(), title: "Outer".into(), ..Default::default() }]); + // Stray ENDs, and nesting deeper than is followed: every card still comes out once. + assert!(parse("END:VCARD\nEND:VCARD\nEND:X\n").is_empty()); + let deep: String = (0..50).map(|i| format!("BEGIN:VCARD\nFN:Card {i}\n")).collect(); + let got = parse(&deep); + assert_eq!(got.len(), 50); + assert_eq!(got.iter().map(|c| c.display_name.clone()).collect::>(), (0..50).map(|i| format!("Card {i}")).collect::>()); + } + + #[test] + fn sizes_and_counts_are_capped() { + let note = "é".repeat(1 << 20); + let c = card(&format!("FN:{}\nNOTE:{note}", "x".repeat(3 << 20))); + assert!(c.display_name.len() <= 1 << 20 && c.display_name.len() > 1_000_000); + assert!(c.notes.len() <= 1 << 20); + let crowded = card(&format!( + "FN:x\n{}{}{}{}CATEGORIES:{}\n{}", + "EMAIL:someone@example.org\n".repeat(MAX_ITEMS + 20), + "TEL:+1 555 0100\n".repeat(MAX_ITEMS + 20), + "ADR:;;1 Main St\n".repeat(MAX_ITEMS + 20), + "URL:https://example.org\n".repeat(MAX_ITEMS + 20), + "a,".repeat(MAX_CATEGORIES + 20), + (0..MAX_LABELS + 20).map(|i| format!("g{i}.X-ABLabel:L{i}\n")).collect::(), + )); + assert_eq!( + (crowded.emails.len(), crowded.phones.len(), crowded.addresses.len(), crowded.websites.len(), crowded.categories.len()), + (MAX_ITEMS, MAX_ITEMS, MAX_ITEMS, MAX_ITEMS, MAX_CATEGORIES) + ); + let many = "BEGIN:VCARD\nFN:x\nEND:VCARD\n".repeat(MAX_CARDS + 10); + assert_eq!(parse(&many).len(), MAX_CARDS); + } +} diff --git a/crates/vcard/src/tests.rs b/crates/vcard/src/tests.rs new file mode 100644 index 000000000..22ee050a0 --- /dev/null +++ b/crates/vcard/src/tests.rs @@ -0,0 +1,391 @@ +//! Whole-crate properties: what is written reads back the same, and no text makes the reader panic. + +use crate::{Contact, Labeled, PostalAddress}; +use chrono::NaiveDate; +use proptest::prelude::*; + +use crate::{parse, write}; + +/// What reading back gives for a written contact: the name the card was written under. +fn as_read(c: &Contact) -> Contact { + Contact { display_name: c.name(), ..c.clone() } +} + +fn round_trip(contacts: &[Contact]) -> Vec { + parse(&write(&contacts.iter().collect::>())) +} + +fn sample_contacts() -> Vec { + let nadia = Contact { + display_name: "Dr. Nadia Okafor".into(), + first: "Nadia".into(), + middle: "Chidi Rose".into(), + last: "Okafor".into(), + nickname: "Nadi, \"the planner\"".into(), + company: "Example Research, Inc.".into(), + title: "Lead Scientist; Field".into(), + department: "Field Lab \\ Unit 7".into(), + emails: vec![ + Labeled::new("work", "nadia.okafor@example.org"), + Labeled::new("", "nadia@example.org"), + Labeled::new("Family", "nadia@family.example.org"), + Labeled::new("mobile", "nadia@phone.example.org"), + ], + phones: vec![ + Labeled::new("mobile", "+1 (555) 010-0142"), + Labeled::new("work", "+1 555 010 0143"), + Labeled::new("home", "+1 555 010 0144"), + Labeled::new("fax", "+1 555 010 0145"), + Labeled::new("pager", "+1 555 010 0146"), + Labeled::new("other", "+1 555 010 0147"), + Labeled::new("", "+1 555 010 0148"), + Labeled::new("Car; old, blue", "+1 555 010 0149"), + ], + addresses: vec![ + PostalAddress { + label: "work".into(), + street: "Suite 300, 41 Harbor Road".into(), + city: "Riverton".into(), + region: "CA".into(), + postal_code: "94000".into(), + country: "USA".into(), + }, + PostalAddress { label: "home".into(), street: "7; Rue de l'Étang".into(), city: "Saint-Rémy".into(), ..Default::default() }, + PostalAddress { label: "Cabin".into(), country: "Norway".into(), ..Default::default() }, + PostalAddress { postal_code: "12345".into(), ..Default::default() }, + ], + websites: vec!["https://example.org/~nadia?a=1,2;b=3".into(), "example.org".into()], + birthday: NaiveDate::from_ymd_opt(1985, 4, 12), + notes: "Met at the 2024 symposium.\nPrefers e-mail, not calls; never on Fridays.\n\n\tC:\\notes\\nadia.txt\n".into(), + categories: vec!["Research".into(), "Field Team, West".into()], + favorite: true, + uid: "7f1c2f6e-0001-4a5b-9c3d-000000000042".into(), + ..Default::default() + }; + let unnamed = + Contact { first: "Jürgen".into(), last: "Müller".into(), phones: vec![Labeled::new("mobile", "+49 170 5550101")], ..Default::default() }; + let company = + Contact { company: "東京デザイン株式会社".into(), websites: vec!["https://design.example.org".into()], ..Default::default() }; + let address_only = Contact { emails: vec![Labeled::new("other", "someone@example.org")], ..Default::default() }; + let bare = Contact { favorite: true, ..Default::default() }; + let emoji = Contact { + display_name: "Zoë 🎉 “Zed” Ødegård".into(), + notes: " leading and trailing spaces stay ".into(), + birthday: NaiveDate::from_ymd_opt(2000, 2, 29), + ..Default::default() + }; + vec![nadia, unnamed, company, address_only, bare, emoji] +} + +#[test] +fn sample_contacts_round_trip() { + let contacts = sample_contacts(); + let back = round_trip(&contacts); + assert_eq!(back, contacts.iter().map(as_read).collect::>()); + assert_eq!(back[1].display_name, "Jürgen Müller"); + assert_eq!(back[3].display_name, "someone@example.org"); + assert_eq!(back[4].display_name, ""); + // What was read writes the same text again. + let text = write(&back.iter().collect::>()); + assert_eq!(parse(&text), back); + assert_eq!(write(&parse(&text).iter().collect::>()), text); +} + +#[test] +fn an_empty_contact_does_not_come_back() { + assert!(round_trip(&[Contact::default(), Contact::default()]).is_empty()); + // Entries with nothing in them are left out. + let hollow = Contact { + display_name: "Ada".into(), + emails: vec![Labeled::new("work", ""), Labeled::new("home", "ada@example.org")], + phones: vec![Labeled::new("mobile", " ")], + addresses: vec![PostalAddress { label: "home".into(), ..Default::default() }], + websites: vec![String::new()], + categories: vec![" ".into()], + ..Default::default() + }; + let back = round_trip(std::slice::from_ref(&hollow)); + assert_eq!(back, [Contact { display_name: "Ada".into(), emails: vec![Labeled::new("home", "ada@example.org")], ..Default::default() }]); +} + +#[test] +fn cards_from_other_programs_survive_a_round_trip() { + // A 2.1 card read, written as 3.0 and read again says the same. + let old = "BEGIN:VCARD\r\nVERSION:2.1\r\nN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:M=C3=BCller;J=C3=BCrgen\r\nTEL;WORK;VOICE:+49 30 5550100\r\nTEL;CELL:+49 170 5550101\r\n\ +EMAIL;PREF;INTERNET:jm@example.org\r\nADR;HOME:;;Gartenweg 12, Hinterhaus;Berlin;;10115;Deutschland\r\nNOTE;ENCODING=QUOTED-PRINTABLE:Zeile eins=0D=0AZeile =\r\nzwei\r\nEND:VCARD\r\n"; + let first = parse(old); + assert_eq!(first.len(), 1); + assert_eq!(first[0].notes, "Zeile eins\nZeile zwei"); + assert_eq!(first[0].addresses[0].street, "Gartenweg 12, Hinterhaus"); + assert_eq!(round_trip(&first), first.iter().map(as_read).collect::>()); +} + +/// One line of text as a contact field holds it: printable, trimmed. +fn line() -> impl Strategy { + prop_oneof!["(\\PC|[;,\\\\:\"^=.]){0,24}", "[ -~]{0,60}", Just(String::new())].prop_map(|s| s.trim().to_string()) +} + +/// A line with something in it: an empty one gets a character. +fn filled_line() -> impl Strategy { + (line(), "[!-~]").prop_map(|(line, fallback)| if line.is_empty() { fallback } else { line }) +} + +fn label(known: &'static [&'static str]) -> impl Strategy { + prop_oneof![proptest::sample::select(known).prop_map(str::to_string), Just(String::new()), line()] +} + +fn date() -> impl Strategy { + (1i32..=9999, 1u32..=12, 1u32..=28).prop_map(|(y, m, day)| NaiveDate::from_ymd_opt(y, m, day).unwrap_or_default()) +} + +fn address() -> impl Strategy { + // One part, any of them, always says something. + (label(&["work", "home", "other"]), [line(), line(), line(), line(), line()], 0usize..5, filled_line()).prop_map( + |(label, mut parts, filled, text)| { + parts[filled] = text; + let [street, city, region, postal_code, country] = parts; + PostalAddress { label, street, city, region, postal_code, country } + }, + ) +} + +fn contact() -> impl Strategy { + let names = (line(), line(), line(), line(), line(), line(), line(), line()); + let emails = proptest::collection::vec((label(&["work", "home", "other"]), filled_line()), 0..4); + let phones = proptest::collection::vec((label(&["mobile", "work", "home", "fax", "pager", "other"]), filled_line()), 0..4); + let lists = ( + emails, + phones, + proptest::collection::vec(address(), 0..3), + proptest::collection::vec(filled_line(), 0..3), + proptest::collection::vec(filled_line(), 0..3), + ); + let rest = (proptest::option::of(date()), "(\\PC|[\n\t;,\\\\:]){0,120}", any::(), line()); + (names, lists, rest).prop_map(|(names, lists, rest)| { + let (display_name, first, middle, last, nickname, company, title, department) = names; + let (emails, phones, addresses, websites, categories) = lists; + let (birthday, notes, favorite, uid) = rest; + let labeled = |list: Vec<(String, String)>| list.into_iter().map(|(label, value)| Labeled { label, value }).collect(); + Contact { + display_name, + first, + middle, + last, + nickname, + company, + title, + department, + emails: labeled(emails), + phones: labeled(phones), + addresses, + websites, + birthday, + notes, + categories, + favorite, + uid, + ..Default::default() + } + }) +} + +/// Text that looks like a card without being one. +fn soup() -> impl Strategy { + let piece = prop_oneof![ + proptest::sample::select(vec![ + "BEGIN:VCARD", + "END:VCARD", + "BEGIN:", + "END:", + "BEGIN:X", + "END:X", + "VERSION:2.1", + "VERSION:3.0", + "VERSION:4.0", + "FN", + "N", + "NICKNAME", + "ORG", + "TITLE", + "EMAIL", + "TEL", + "ADR", + "URL", + "BDAY", + "NOTE", + "CATEGORIES", + "UID", + "PHOTO", + "X-ABLabel", + "X-FAVORITE", + "item1.", + "item2.", + ".", + ":", + ";", + ",", + "=", + "\"", + "\\", + "\r\n", + "\n", + "\r\n ", + "\n\t", + " ", + "=\r\n", + "=\n", + "=C3=A9", + "=C3", + "=ZZ", + "\\n", + "\\;", + "\\,", + ";TYPE=WORK,VOICE", + ";TYPE=\"cell,pref\"", + ";HOME", + ";CELL", + ";ENCODING=QUOTED-PRINTABLE", + ";QUOTED-PRINTABLE", + ";ENCODING=b", + ";BASE64", + ";CHARSET=ISO-8859-1", + ";CHARSET=UTF-8", + ";CHARSET=x", + "mailto:", + "tel:", + "a@example.org", + "+1 555 0100", + "1985-04-12", + "19850412T101010Z", + "--0412", + "0000-00-00", + "TRUE", + "SGVsbG8=", + "_$!!$_", + "_$!<", + ]) + .prop_map(str::to_string), + "\\PC{0,6}", + any::(), + ]; + proptest::collection::vec(piece, 0..60).prop_map(|pieces| pieces.concat()) +} + +fn valid_text() -> String { + write(&sample_contacts().iter().collect::>()) +} + +/// Whatever the reader gives must also be writable and readable again. +fn survives(text: &str) -> Vec { + let contacts = parse(text); + let again = write(&contacts.iter().collect::>()); + let _ = parse(&again); + contacts +} + +proptest! { + #[test] + fn any_contact_round_trips(contacts in proptest::collection::vec(contact(), 0..4)) { + let back = round_trip(&contacts); + // A contact with nothing in it isn't written back as one. + let want: Vec = contacts.iter().map(as_read).filter(|c| *c != Contact::default()).collect(); + prop_assert_eq!(back, want); + } + + #[test] + fn any_notes_and_names_round_trip(notes in "(\\PC|[\n\t]){0,400}", name in "\\PC{0,200}", street in "\\PC{0,100}") { + let name = name.trim().to_string(); + let street = street.trim().to_string(); + let c = Contact { display_name: name.clone(), last: name.clone(), company: name.clone(), notes, addresses: vec![PostalAddress { street, city: "Riverton".into(), ..Default::default() }], uid: "u".into(), ..Default::default() }; + prop_assert_eq!(round_trip(std::slice::from_ref(&c)), vec![as_read(&c)]); + } + + #[test] + fn text_with_control_characters_reads_back_clean_and_stable(name in any::(), notes in any::(), label in any::(), value in any::(), category in any::()) { + let c = Contact { + display_name: name.clone(), + first: name, + notes, + emails: vec![Labeled::new(label.clone(), value.clone())], + phones: vec![Labeled::new(label.clone(), value.clone())], + addresses: vec![PostalAddress { label, street: value.clone(), ..Default::default() }], + websites: vec![value], + categories: vec![category], + uid: "u".into(), + ..Default::default() + }; + let once = round_trip(std::slice::from_ref(&c)); + prop_assert_eq!(once.len(), 1); + // The first pass cleans (control characters, stray spaces); after that nothing changes but a + // display name that was empty. + prop_assert_eq!(round_trip(&once), once.iter().map(as_read).collect::>()); + prop_assert!(!once[0].display_name.contains(['\r', '\n']) && !once[0].notes.contains('\r')); + } + + #[test] + fn arbitrary_text_never_panics(text in any::()) { + survives(&text); + } + + #[test] + fn arbitrary_bytes_never_panic(bytes in proptest::collection::vec(any::(), 0..2048)) { + let contacts = crate::parse_bytes(&bytes); + let _ = parse(&write(&contacts.iter().collect::>())); + } + + #[test] + fn card_shaped_bytes_never_panic(text in soup(), high in proptest::collection::vec((any::(), 0x80u8..=0xFF), 0..12), charset in proptest::sample::select(vec!["", "windows-1250", "UTF-8", "utf-16", "iso-2022-jp", "shift_jis", "x-unknown"])) { + // Card-shaped text with a charset named and raw 8-bit bytes dropped in. + let mut bytes = text.replace("FN:", &format!("FN;CHARSET={charset}:")).into_bytes(); + for (at, byte) in high { + if !bytes.is_empty() { + let i = at.index(bytes.len()); + bytes.insert(i, byte); + } + } + let _ = crate::parse_bytes(&bytes); + } + + #[test] + fn utf8_bytes_read_as_the_text_does(text in soup()) { + // (A value in UTF-8 that says it is Latin-1 is read as Latin-1 from bytes.) + let text = text.replace("ISO-8859-1", "UTF-8"); + prop_assert_eq!(crate::parse_bytes(text.as_bytes()), parse(&text)); + } + + #[test] + fn card_shaped_junk_never_panics(text in soup()) { + survives(&text); + } + + #[test] + fn a_damaged_card_never_panics(cuts in proptest::collection::vec((any::(), 0usize..40, 0u8..5, "\\PC{0,8}|[\r\n:;,=\"\\\\ .]{1,4}"), 1..6)) { + let mut chars: Vec = valid_text().chars().collect(); + for (index, length, kind, junk) in cuts { + let from = index.index(chars.len().max(1)).min(chars.len()); + let to = from.saturating_add(length).min(chars.len()); + match kind { + // Cut a piece out, overwrite it, insert junk, double a piece, or stop the text there. + 0 => drop(chars.drain(from..to)), + 1 => drop(chars.splice(from..to, junk.chars())), + 2 => drop(chars.splice(from..from, junk.chars())), + 3 => { + let piece: Vec = chars[from..to].to_vec(); + drop(chars.splice(from..from, piece)); + } + _ => chars.truncate(from), + } + } + let damaged: String = chars.into_iter().collect(); + prop_assert!(survives(&damaged).len() <= 64); + } +} + +#[test] +fn a_card_cut_anywhere_never_panics() { + let text = valid_text(); + for (cut, _) in text.char_indices().step_by(5) { + survives(&text[..cut]); + survives(&text[cut..]); + } +} diff --git a/crates/vcard/src/text.rs b/crates/vcard/src/text.rs new file mode 100644 index 000000000..a82228b38 --- /dev/null +++ b/crates/vcard/src/text.rs @@ -0,0 +1,239 @@ +//! Text values: cleaning, escaping, splitting, and the 2.1 encodings (quoted-printable, base64; +//! a `CHARSET` is read by `craft_mime::charset`, as mail is). + +/// A one-line field: line breaks and other control characters become spaces, and the ends are +/// trimmed. +pub(crate) fn single(s: &str) -> String { + let spaced: String = s.chars().map(|c| if c.is_control() { ' ' } else { c }).collect(); + spaced.trim().to_string() +} + +/// Text of several lines: `\r\n` and `\r` become `\n`; other control characters except tab are +/// dropped. +pub(crate) fn multi(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + let mut after_cr = false; + for c in s.chars() { + let cr = c == '\r'; + match c { + '\n' if after_cr => {} + '\r' | '\n' => out.push('\n'), + '\t' => out.push(c), + c if c.is_control() => {} + c => out.push(c), + } + after_cr = cr; + } + out +} + +/// A value ready to write: `\`, `;`, `,` and line breaks escaped. +pub(crate) fn escape(s: &str) -> String { + let mut out = String::with_capacity(s.len().saturating_add(8)); + for c in s.chars() { + match c { + '\\' => out.push_str("\\\\"), + ';' => out.push_str("\\;"), + ',' => out.push_str("\\,"), + '\n' => out.push_str("\\n"), + '\r' => {} + c => out.push(c), + } + } + out +} + +/// Undo [`escape`]: `\n` and `\N` are line breaks, `\\`, `\;`, `\,` (and the `\:` some writers +/// add) their second character; any other backslash is kept. +pub(crate) fn unescape(s: &str) -> String { + if !s.contains('\\') { + return s.to_string(); + } + let mut out = String::with_capacity(s.len()); + let mut chars = s.chars(); + while let Some(c) = chars.next() { + if c != '\\' { + out.push(c); + continue; + } + match chars.next() { + Some('n' | 'N') => out.push('\n'), + Some(x @ ('\\' | ';' | ',' | ':')) => out.push(x), + Some(x) => { + out.push('\\'); + out.push(x); + } + None => out.push('\\'), + } + } + out +} + +/// The first `max` pieces of `s`, split on its unescaped `separator`s; the pieces are still +/// escaped. +pub(crate) fn split(s: &str, separator: char, max: usize) -> Vec<&str> { + let mut out = Vec::new(); + let (mut start, mut escaped) = (0usize, false); + for (i, c) in s.char_indices() { + if out.len() >= max { + return out; + } + if escaped { + escaped = false; + } else if c == '\\' { + escaped = true; + } else if c == separator { + out.push(s.get(start..i).unwrap_or("")); + start = i.saturating_add(c.len_utf8()); + } + } + if out.len() < max { + out.push(s.get(start..).unwrap_or("")); + } + out +} + +/// One component of a structured value (a name part, a street) as one line of text. vCard 3.0 and +/// 4.0 let a component list several values with commas, which are joined with `joiner`; in 2.1 +/// (`v21`) a comma is just a comma. +pub(crate) fn component(piece: &str, joiner: &str, v21: bool) -> String { + let values = if v21 { vec![piece] } else { split(piece, ',', 32) }; + match values.as_slice() { + [one] => single(&unescape(one)), + many => many.iter().map(|v| single(&unescape(v))).filter(|v| !v.is_empty()).collect::>().join(joiner), + } +} + +/// The bytes of a quoted-printable value (`=C3=A9`); soft line breaks are already gone, and a `=` +/// that isn't followed by two hex digits is kept. +pub(crate) fn quoted_printable(s: &str) -> Vec { + let bytes = s.as_bytes(); + let hex = |at: usize| bytes.get(at).and_then(|b| char::from(*b).to_digit(16)).and_then(|d| u8::try_from(d).ok()); + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0usize; + while let Some(&b) = bytes.get(i) { + if b == b'=' + && let (Some(high), Some(low)) = (hex(i.saturating_add(1)), hex(i.saturating_add(2))) + { + out.push(high.saturating_mul(16).saturating_add(low)); + i = i.saturating_add(3); + } else { + out.push(b); + i = i.saturating_add(1); + } + } + out +} + +/// The bytes of a base64 value, white space and padding or not; empty for junk. +pub(crate) fn base64_bytes(s: &str) -> Vec { + let compact: String = s.chars().filter(|c| !c.is_whitespace()).collect(); + craft_mime::base64::STANDARD_NO_PAD.decode(compact.trim_end_matches('=')).unwrap_or_default() +} + +/// Whether `c` is one of Unicode's format characters (general category Cf, Unicode 17.0): +/// bidirectional marks, zero-width marks and joiners, the byte-order mark, the soft hyphen, tags +/// and the rest. They show nothing and can turn the text around them. +pub(crate) fn is_format(c: char) -> bool { + matches!( + c, + '\u{00ad}' + | '\u{0600}'..='\u{0605}' + | '\u{061c}' + | '\u{06dd}' + | '\u{070f}' + | '\u{0890}'..='\u{0891}' + | '\u{08e2}' + | '\u{180e}' + | '\u{200b}'..='\u{200f}' + | '\u{202a}'..='\u{202e}' + | '\u{2060}'..='\u{2064}' + | '\u{2066}'..='\u{206f}' + | '\u{feff}' + | '\u{fff9}'..='\u{fffb}' + | '\u{110bd}' + | '\u{110cd}' + | '\u{13430}'..='\u{1343f}' + | '\u{1bca0}'..='\u{1bca3}' + | '\u{1d173}'..='\u{1d17a}' + | '\u{e0001}' + | '\u{e0020}'..='\u{e007f}' + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cleans_one_line_and_many_line_text() { + assert_eq!(single(" Ada\r\nLovelace\t\u{0}x "), "Ada Lovelace x"); + assert_eq!(single("\n\u{85}\u{7f} "), ""); + assert_eq!(multi("a\r\nb\rc\n\nd\u{0}\u{7f}\te "), "a\nb\nc\n\nd\te "); + assert_eq!(multi(""), ""); + } + + #[test] + fn escapes_and_unescapes() { + assert_eq!(escape("Research, Inc.; Lab \\ 7\nSecond\r\n"), "Research\\, Inc.\\; Lab \\\\ 7\\nSecond\\n"); + assert_eq!(unescape("Research\\, Inc.\\; Lab \\\\ 7\\nSecond\\NThird"), "Research, Inc.; Lab \\ 7\nSecond\nThird"); + assert_eq!(unescape("http\\://example.org C:\\Users\\x tail\\"), "http://example.org C:\\Users\\x tail\\"); + assert_eq!(unescape("plain"), "plain"); + } + + #[test] + fn splits_on_unescaped_separators() { + assert_eq!(split("Okafor;Nadia;;Dr.;", ';', 9), ["Okafor", "Nadia", "", "Dr.", ""]); + assert_eq!(split("a\\;b;c\\\\;d", ';', 9), ["a\\;b", "c\\\\", "d"]); + assert_eq!(split("a,b,c", ',', 2), ["a", "b"]); + assert_eq!(split("", ';', 9), [""]); + assert!(split("a;b", ';', 0).is_empty()); + assert_eq!(split("é;東;🎉", ';', 9), ["é", "東", "🎉"]); + } + + #[test] + fn components_join_their_lists() { + assert_eq!(component("Quinlan,Paul", " ", false), "Quinlan Paul"); + assert_eq!(component("Quinlan\\,Paul", " ", false), "Quinlan,Paul"); + assert_eq!(component(" 41 Harbor Road , Suite 300,, ", ", ", false), "41 Harbor Road, Suite 300"); + assert_eq!(component("Smith, Jr.", " ", true), "Smith, Jr."); + assert_eq!(component(" one\\nline ", " ", false), "one line"); + assert_eq!(component("", ", ", false), ""); + } + + #[test] + fn decodes_quoted_printable() { + assert_eq!(quoted_printable("J=C3=BCrgen M=c3=bcller"), "Jürgen Müller".as_bytes()); + assert_eq!(quoted_printable("a=3Db=0D=0Ac"), b"a=b\r\nc"); + // What isn't an escape stays. + assert_eq!(quoted_printable("1+1=2 =ZZ =4"), b"1+1=2 =ZZ =4"); + assert_eq!(quoted_printable("="), b"="); + assert_eq!(quoted_printable("é=41"), "éA".as_bytes()); + } + + #[test] + fn decodes_base64() { + assert_eq!(base64_bytes("SGVsbG8sIHdvcmxk"), b"Hello, world"); + assert_eq!(base64_bytes(" SGVs\r\n bG8=\n"), b"Hello"); + assert_eq!(base64_bytes("SGVsbG8"), b"Hello"); + assert!(base64_bytes("not base64!").is_empty()); + assert!(base64_bytes("").is_empty()); + } + + proptest::proptest! { + #[test] + fn clean_text_survives_escaping(s in "(\\PC|[\n\t;,\\\\:]){0,80}") { + proptest::prop_assert_eq!(unescape(&escape(&s)), s.clone()); + // And as one piece of a structured value or a list. + proptest::prop_assert_eq!(split(&format!("{};x", escape(&s)), ';', 9).first().map(|p| unescape(p)), Some(s.clone())); + proptest::prop_assert_eq!(split(&format!("{},x", escape(&s)), ',', 9).first().map(|p| unescape(p)), Some(s)); + } + + #[test] + fn text_helpers_never_panic(s in proptest::prelude::any::(), max in 0usize..5) { + let _ = (single(&s), multi(&s), unescape(&s), split(&s, ';', max), component(&s, ", ", false), quoted_printable(&s), base64_bytes(&s)); + proptest::prop_assert!(!escape(&multi(&s)).contains(['\r', '\n'])); + proptest::prop_assert!(!single(&s).contains(['\r', '\n'])); + } + } +} diff --git a/crates/vcard/src/write.rs b/crates/vcard/src/write.rs new file mode 100644 index 000000000..3d601eca3 --- /dev/null +++ b/crates/vcard/src/write.rs @@ -0,0 +1,326 @@ +//! Writing contacts as vCard 3.0. + +use crate::Contact; +use chrono::{Datelike, NaiveDate}; + +use crate::lines::fold; +use crate::text::{escape, multi, single}; + +/// Write vCard 3.0 cards, CRLF line endings, lines folded at 75 octets on char boundaries. +/// +/// `FN` is the contact's [`name`](Contact::name). Values are escaped. Every field but the notes is +/// one line: its line breaks and other control characters become spaces. A label the format has a +/// `TYPE` for (`work`, `home`, `other`; for phones also `mobile`, `fax`, `pager`) is written as +/// that type; any other label is written as the `X-ABLabel` of a group (`item1.TEL` + +/// `item1.X-ABLabel:Car`), which is how address books exchange custom labels. E-mail addresses, +/// phone numbers, postal addresses and web sites with nothing in them are left out. Keys are +/// written as `KEY;TYPE=X509;ENCODING=b:` and `KEY;TYPE=PGP;ENCODING=b:`; one that would not read +/// back as itself is left out, and so is the address of a key, which vCard 3.0 has no form for. +pub fn write(contacts: &[&Contact]) -> String { + let mut out = String::new(); + for contact in contacts { + card(&mut out, contact); + } + out +} + +/// A one-line field, escaped. +fn field(s: &str) -> String { + escape(&single(s)) +} + +/// `NAME:value` when there is a value. +fn optional(out: &mut String, name: &str, value: &str) { + let value = field(value); + if !value.is_empty() { + fold(out, &format!("{name}:{value}")); + } +} + +/// The `TYPE` of an e-mail or postal address label, when the format has one for it. +fn place_type(label: &str) -> Option<&'static str> { + match label { + "work" => Some("WORK"), + "home" => Some("HOME"), + "other" => Some("OTHER"), + _ => None, + } +} + +fn phone_type(label: &str) -> Option<&'static str> { + match label { + "mobile" => Some("CELL"), + "fax" => Some("FAX"), + "pager" => Some("PAGER"), + _ => place_type(label), + } +} + +/// One labelled property. `kind` is the type the property always has (`INTERNET`), `known` the +/// type its label stands for; a label without one goes into an `X-ABLabel` of the same group. +fn labeled(out: &mut String, groups: &mut u32, name: &str, kind: Option<&str>, known: Option<&str>, label: &str, value: &str) { + let types: Vec<&str> = kind.into_iter().chain(known).collect(); + let params = if types.is_empty() { String::new() } else { format!(";TYPE={}", types.join(",")) }; + let custom = if known.is_some() { String::new() } else { field(label) }; + if custom.is_empty() { + fold(out, &format!("{name}{params}:{value}")); + } else { + *groups = groups.saturating_add(1); + fold(out, &format!("item{groups}.{name}{params}:{value}")); + fold(out, &format!("item{groups}.X-ABLabel:{custom}")); + } +} + +fn card(out: &mut String, c: &Contact) { + fold(out, "BEGIN:VCARD"); + fold(out, "VERSION:3.0"); + fold(out, &format!("N:{};{};{};;", field(&c.last), field(&c.first), field(&c.middle))); + fold(out, &format!("FN:{}", field(&c.name()))); + optional(out, "NICKNAME", &c.nickname); + let (company, department) = (field(&c.company), field(&c.department)); + if !department.is_empty() { + fold(out, &format!("ORG:{company};{department}")); + } else if !company.is_empty() { + fold(out, &format!("ORG:{company}")); + } + optional(out, "TITLE", &c.title); + let mut groups = 0u32; + for email in &c.emails { + let value = field(&email.value); + if !value.is_empty() { + labeled(out, &mut groups, "EMAIL", Some("INTERNET"), place_type(&email.label), &email.label, &value); + } + } + for phone in &c.phones { + let value = field(&phone.value); + if !value.is_empty() { + labeled(out, &mut groups, "TEL", None, phone_type(&phone.label), &phone.label, &value); + } + } + for address in c.addresses.iter().filter(|a| !a.is_empty()) { + // Post office box and extended address stay empty: the model keeps them in the street. + let value = format!( + ";;{};{};{};{};{}", + field(&address.street), + field(&address.city), + field(&address.region), + field(&address.postal_code), + field(&address.country) + ); + labeled(out, &mut groups, "ADR", None, place_type(&address.label), &address.label, &value); + } + for site in &c.websites { + optional(out, "URL", site); + } + if let Some(day) = c.birthday { + // A date that exists, whatever the fields say. + // A year a card can say (1 to 9999), whatever the date's is. + let year = day.year().clamp(1, 9999); + let day = day.with_year(year).or_else(|| NaiveDate::from_ymd_opt(year, day.month(), 28)).unwrap_or_default(); + fold(out, &format!("BDAY:{}", day.format("%Y-%m-%d"))); + } + let notes = escape(&multi(&c.notes)); + if !notes.is_empty() { + fold(out, &format!("NOTE:{notes}")); + } + let categories: Vec = c.categories.iter().map(|name| field(name)).filter(|name| !name.is_empty()).collect(); + if !categories.is_empty() { + fold(out, &format!("CATEGORIES:{}", categories.join(","))); + } + for key in c.keys.iter().filter_map(crate::key::line).take(Contact::MAX_KEYS) { + fold(out, &key); + } + optional(out, "UID", &c.uid); + if c.favorite { + fold(out, "X-MAILCRAFT-FAVORITE:TRUE"); + } + fold(out, "END:VCARD"); +} + +#[cfg(test)] +mod tests { + use crate::{Labeled, PostalAddress}; + + use super::*; + + fn lines(c: &Contact) -> Vec { + let out = write(&[c]); + assert!(out.ends_with("END:VCARD\r\n")); + let mut cursor = crate::lines::Lines::new(&out); + std::iter::from_fn(|| cursor.next_line(false)).collect() + } + + fn nadia() -> Contact { + Contact { + display_name: "Dr. Nadia Okafor".into(), + first: "Nadia".into(), + middle: "Chidi".into(), + last: "Okafor".into(), + nickname: "Nadi".into(), + company: "Example Research, Inc.".into(), + title: "Lead Scientist; Field".into(), + department: "Field Lab".into(), + emails: vec![ + Labeled::new("work", "nadia.okafor@example.org"), + Labeled::new("", "nadia@example.org"), + Labeled::new("Family", "nadia@family.example.org"), + Labeled::new("home", " "), + ], + phones: vec![ + Labeled::new("mobile", "+1 (555) 010-0142"), + Labeled::new("fax", "+1 555 010 0144"), + Labeled::new("Car, old", "+1 555 010 0145"), + Labeled::new("", "+1 555 010 0146"), + ], + addresses: vec![ + PostalAddress { + label: "work".into(), + street: "Suite 300, 41 Harbor Road".into(), + city: "Riverton".into(), + region: "CA".into(), + postal_code: "94000".into(), + country: "USA".into(), + }, + PostalAddress { label: "home".into(), ..Default::default() }, + PostalAddress { label: "Cabin".into(), city: "Pinewood".into(), ..Default::default() }, + ], + websites: vec!["https://example.org/~nadia".into(), "".into()], + birthday: NaiveDate::from_ymd_opt(1985, 4, 12), + notes: "Met at the 2024 symposium.\r\nPrefers e-mail, not calls.".into(), + categories: vec!["Research".into(), "Field Team, West".into(), " ".into()], + favorite: true, + uid: "7f1c2f6e-0001-4a5b-9c3d-000000000042".into(), + ..Default::default() + } + } + + #[test] + fn writes_a_whole_card() { + assert_eq!( + lines(&nadia()), + [ + "BEGIN:VCARD", + "VERSION:3.0", + "N:Okafor;Nadia;Chidi;;", + "FN:Dr. Nadia Okafor", + "NICKNAME:Nadi", + "ORG:Example Research\\, Inc.;Field Lab", + "TITLE:Lead Scientist\\; Field", + "EMAIL;TYPE=INTERNET,WORK:nadia.okafor@example.org", + "EMAIL;TYPE=INTERNET:nadia@example.org", + "item1.EMAIL;TYPE=INTERNET:nadia@family.example.org", + "item1.X-ABLabel:Family", + "TEL;TYPE=CELL:+1 (555) 010-0142", + "TEL;TYPE=FAX:+1 555 010 0144", + "item2.TEL:+1 555 010 0145", + "item2.X-ABLabel:Car\\, old", + "TEL:+1 555 010 0146", + "ADR;TYPE=WORK:;;Suite 300\\, 41 Harbor Road;Riverton;CA;94000;USA", + "item3.ADR:;;;Pinewood;;;", + "item3.X-ABLabel:Cabin", + "URL:https://example.org/~nadia", + "BDAY:1985-04-12", + "NOTE:Met at the 2024 symposium.\\nPrefers e-mail\\, not calls.", + "CATEGORIES:Research,Field Team\\, West", + "UID:7f1c2f6e-0001-4a5b-9c3d-000000000042", + "X-MAILCRAFT-FAVORITE:TRUE", + "END:VCARD", + ] + ); + } + + #[test] + fn an_empty_contact_is_still_a_card() { + assert_eq!(write(&[&Contact::default()]), "BEGIN:VCARD\r\nVERSION:3.0\r\nN:;;;;\r\nFN:\r\nEND:VCARD\r\n"); + assert_eq!(write(&[]), ""); + } + + #[test] + fn the_formatted_name_falls_back_as_the_model_does() { + let named = Contact { first: "Ada".into(), last: "Lovelace".into(), ..Default::default() }; + assert!(lines(&named).contains(&"FN:Ada Lovelace".to_string())); + let company = Contact { company: "Analytical Engines Ltd".into(), ..Default::default() }; + assert_eq!(lines(&company).get(2..5).unwrap(), ["N:;;;;", "FN:Analytical Engines Ltd", "ORG:Analytical Engines Ltd"]); + let mail = Contact { emails: vec![Labeled::new("home", "x@example.org")], department: "Only a department".into(), ..Default::default() }; + assert_eq!(lines(&mail).get(3..6).unwrap(), ["FN:x@example.org", "ORG:;Only a department", "EMAIL;TYPE=INTERNET,HOME:x@example.org"]); + } + + #[test] + fn every_known_label_is_a_type() { + let c = Contact { + emails: ["work", "home", "other", "mobile", "Work"].map(|l| Labeled::new(l, "a@example.org")).to_vec(), + phones: ["mobile", "work", "home", "fax", "pager", "other", "HOME"].map(|l| Labeled::new(l, "1")).to_vec(), + ..Default::default() + }; + let got = lines(&c); + assert_eq!( + got.get(4..20).unwrap(), + [ + "EMAIL;TYPE=INTERNET,WORK:a@example.org", + "EMAIL;TYPE=INTERNET,HOME:a@example.org", + "EMAIL;TYPE=INTERNET,OTHER:a@example.org", + "item1.EMAIL;TYPE=INTERNET:a@example.org", + "item1.X-ABLabel:mobile", + "item2.EMAIL;TYPE=INTERNET:a@example.org", + "item2.X-ABLabel:Work", + "TEL;TYPE=CELL:1", + "TEL;TYPE=WORK:1", + "TEL;TYPE=HOME:1", + "TEL;TYPE=FAX:1", + "TEL;TYPE=PAGER:1", + "TEL;TYPE=OTHER:1", + "item3.TEL:1", + "item3.X-ABLabel:HOME", + "END:VCARD", + ] + ); + } + + #[test] + fn lines_end_in_crlf_and_stay_within_75_octets() { + let mut c = nadia(); + c.notes = "Ünïcödé — 東京 🎉 ".repeat(60); + c.display_name = "é".repeat(200); + let out = write(&[&c, &c]); + assert_eq!(out.matches("BEGIN:VCARD\r\n").count(), 2); + assert!(!out.replace("\r\n", "").contains(['\r', '\n'])); + assert!(out.split("\r\n").all(|line| line.len() <= 75), "{out}"); + assert!(out.contains("\r\n ")); + } + + #[test] + fn text_cannot_inject_lines() { + let c = Contact { + display_name: "Ada\r\nEND:VCARD\r\nBEGIN:VCARD".into(), + first: "A\nTEL:666".into(), + nickname: "x\rEMAIL:evil@example.org".into(), + emails: vec![Labeled::new("work\r\nTEL:1", "a@example.org\r\nTEL:2")], + notes: "one\r\nEND:VCARD\rtwo\u{0}".into(), + categories: vec!["a\nb".into()], + uid: "u\nUID:2".into(), + ..Default::default() + }; + let got = lines(&c); + assert_eq!(got.iter().filter(|l| l.starts_with("BEGIN:") || l.starts_with("END:")).count(), 2, "{got:?}"); + assert!(!got.iter().any(|l| l.starts_with("TEL")), "{got:?}"); + assert_eq!(got.iter().filter(|l| l.starts_with("UID") || l.contains("EMAIL")).count(), 3, "{got:?}"); + assert!(got.contains(&"FN:Ada END:VCARD BEGIN:VCARD".to_string())); + assert!(got.contains(&"NOTE:one\\nEND:VCARD\\ntwo".to_string())); + assert!(got.contains(&"item1.X-ABLabel:work TEL:1".to_string())); + } + + #[test] + fn hostile_birthdays_are_written_as_real_dates() { + for hostile in [ + NaiveDate::MIN, + NaiveDate::MAX, + NaiveDate::from_ymd_opt(0, 1, 1).unwrap(), + NaiveDate::from_ymd_opt(-4, 2, 29).unwrap(), + NaiveDate::from_ymd_opt(10_000, 2, 29).unwrap(), + ] { + let c = Contact { birthday: Some(hostile), ..Default::default() }; + let back = crate::parse(&write(&[&c])); + assert!(back[0].birthday.is_some(), "{hostile:?}"); + } + } +} From 046a9271715fcfdfc273cefe78fa32069ede131b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:51:10 -0500 Subject: [PATCH 027/120] craft-psd: engine_data, the text engine document of type layers (#895) * craft-psd: Photoshop PSD/PSB reader and writer, moved from PhotoCraft PhotoCraft's `photocraft-psd` crate (crates/psd at storytold/photocraft 63b641b55e54bf8f5c83efd977fdb16a2c255431) as `craft-psd`: the lossless PSD/PSB model, reader, writer and builder, descriptors, the Photoshop resource files that share its primitives (.abr, .asl, .aco, .act, .ase, .grd, .pat, .atn, .csh, .shc) and the Photoshop blocks of a layered TIFF. The crate had no dependency on any other PhotoCraft crate. Code is unchanged apart from the crate name, craft-libs' rustfmt width, comments that named other PhotoCraft crates, issue references (now `photocraft#N`), and the corpus tests' directory (`CRAFT_PSD_CORPUS` or `corpus/` at the workspace root). Tests, proptests, fuzz targets and fixtures move with it. Part of #778, #786. * craft-psd: engine_data, the text engine document of type layers The EngineData reader and writer from PhotoCraft's photocraft-text (crates/text/src/engine_data.rs at storytold/photocraft 63b641b5): a tree of values that is written back in Photoshop's layout, with strings stored as plain bytes kept as plain bytes. From EffectCraft's effectcraft-psd (crates/psd/src/engine_data.rs at storytold/effectcraft 072e1af9), ported onto that tree: `first_style`, the first style run as a typed `TextStyle` with bounded sizes and spacings, and `minimal`, a one-run document for writers and fixtures. `Value::index` is new. Both apps had their own parser for the same syntax. * craft-psd: engine_data::parse_lossy reads what it can of a damaged document EffectCraft's EngineData reader was best effort: a truncated or damaged document still gave the text and style it held. `parse_lossy` does that on the same parser: a truncated dictionary, array or string keeps what it has, a byte that starts no key is skipped, a token that is no number becomes a name, and garbage gives an empty dictionary. `parse` stays strict and is unchanged in what it accepts. The parser's `value` is split into one function per kind of value, and reads with `get` instead of indexing. --- crates/psd/README.md | 8 +- crates/psd/src/engine_data.rs | 700 ++++++++++++++++++++++++++++++++++ crates/psd/src/lib.rs | 2 + 3 files changed, 709 insertions(+), 1 deletion(-) create mode 100644 crates/psd/src/engine_data.rs diff --git a/crates/psd/README.md b/crates/psd/README.md index 80ced537a..f8ca51256 100644 --- a/crates/psd/README.md +++ b/crates/psd/README.md @@ -103,6 +103,12 @@ pixels::{samples_u16, samples_f32, u16_to_bytes, f32_to_bytes, unpack_bits, plan descriptor::{Descriptor, VersionedDescriptor, Value, Id, Class, ReferenceItem, UnicodeString, ObjectArray} Descriptor::from_bytes / to_bytes; VersionedDescriptor::from_bytes / to_bytes (version 16) +// Text engine data (`EngineData` in `TySh`, and the document's `Txt2`) +engine_data::parse(&[u8]) -> Result; parse_lossy(&[u8]) -> Value; write(&Value); write_bare(&[(String, Value)]) +engine_data::Value::{Dict, Array, String, RawString, Int, Real, Bool, Name} // RawString is written back as the same bytes +engine_data::first_style(&Value) -> TextStyle // font, size, fill, justification, tracking, leading, faux styles, box text +engine_data::minimal(text, font, size, rgb, justification) -> Vec + // Builder PsdBuilder::new(w, h).color_mode(..).depth(8|16).version(..).compression(..).resolution(dpi).icc_profile(..) builder.push_layer(LayerSpec) / begin_group(GroupSpec) / end_group()? / composite(PixelData) @@ -149,7 +155,7 @@ allocations for padded channels return an error on failure. ### Preserved raw, not yet typed (parsers planned for M8) * Layer effects: `lfx2`, `lrFX`, `lmfx`. The descriptors inside can be parsed on demand with `VersionedDescriptor::from_bytes(&block.data[4..])`. -* Type layers: `TySh`, `tySh`, and the EngineData inside them. +* Type layers: `TySh`, `tySh`. The EngineData inside them has a parser and writer (`engine_data`); the blocks themselves are not typed. * Smart objects: `SoLd`, `PlLd`, `SoLE`, `lnk2`, `lnkD`, `lnk3`. * Vector masks and shapes: `vmsk`, `vsms`, `vogk`, `vscg`. * Adjustment and fill layers: `curv`, `levl`, `hue2`, `SoCo`, `GdFl`, `PtFl`, …, plus `Patt`, `Txt2`, `FMsk`, 3D and video blocks. diff --git a/crates/psd/src/engine_data.rs b/crates/psd/src/engine_data.rs new file mode 100644 index 000000000..6e391e429 --- /dev/null +++ b/crates/psd/src/engine_data.rs @@ -0,0 +1,700 @@ +//! Photoshop's `EngineData`: the PostScript-like text engine document stored in `TySh`. +//! +//! Syntax (from observation of real files and public notes): dictionaries `<< /Key value … >>`, +//! arrays `[ … ]`, strings `( … )` holding UTF-16BE with a `FE FF` byte-order mark and `\` +//! escapes, numbers (`12`, `-1.5`, `.5`), `true`/`false`, and `/Name` values. +//! +//! [`parse`] and [`write`] keep the document as a tree of [`Value`]s; what was parsed is written +//! back in Photoshop's layout, and strings stored as plain bytes stay plain bytes. +//! [`first_style`] is a typed view of the first style run, for readers that show a type layer +//! without editing it; [`parse_lossy`] reads what it can of a damaged document for them. +//! Nesting is limited to 64 levels. + +use std::fmt::Write as _; + +/// One EngineData value. +#[derive(Clone, Debug, PartialEq)] +pub enum Value { + /// `<< /Key value … >>`, in file order. + Dict(Vec<(String, Value)>), + /// `[ … ]`. + Array(Vec), + /// A string stored as UTF-16BE after the `FE FF` mark. + String(String), + /// A string the file stored as plain bytes, without the `FE FF` mark (one char per byte). + /// Kept apart from [`Value::String`] so it is written back as the same bytes: Photoshop + /// rejects a `Txt2` whose raw strings come back as UTF-16 (photocraft#9091). + RawString(String), + /// A number written without a fraction or exponent. + Int(i64), + /// A number written with a fraction or exponent. + Real(f64), + /// `true` or `false`. + Bool(bool), + /// `/Name` in value position. + Name(String), +} + +impl Value { + /// The entry `key` of a dictionary. + pub fn get(&self, key: &str) -> Option<&Value> { + match self { + Value::Dict(items) => items.iter().find(|(k, _)| k == key).map(|(_, v)| v), + _ => None, + } + } + /// Item `i` of an array. + pub fn index(&self, i: usize) -> Option<&Value> { + self.as_array()?.get(i) + } + /// Follows a path of dictionary keys. + pub fn path(&self, keys: &[&str]) -> Option<&Value> { + keys.iter().try_fold(self, |v, k| v.get(k)) + } + /// The entry `key` of a dictionary, mutably. + pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> { + match self { + Value::Dict(items) => items.iter_mut().find(|(k, _)| k == key).map(|(_, v)| v), + _ => None, + } + } + /// Sets (or appends) a dictionary entry. + pub fn set(&mut self, key: &str, v: Value) { + if let Value::Dict(items) = self { + match items.iter_mut().find(|(k, _)| k == key) { + Some(e) => e.1 = v, + None => items.push((key.to_string(), v)), + } + } + } + /// A finite number. + pub fn as_f64(&self) -> Option { + match self { + Value::Int(i) => Some(*i as f64), + // `inf`, `nan` and `1e999` parse as reals but are never a valid style number. + Value::Real(r) if r.is_finite() => Some(*r), + _ => None, + } + } + /// A number as an integer (a real is truncated). + pub fn as_i64(&self) -> Option { + match self { + Value::Int(i) => Some(*i), + Value::Real(r) => Some(*r as i64), + _ => None, + } + } + /// A boolean; an integer counts as `true` when it is not zero. + pub fn as_bool(&self) -> Option { + match self { + Value::Bool(b) => Some(*b), + Value::Int(i) => Some(*i != 0), + _ => None, + } + } + /// The text of a string or a name. + pub fn as_str(&self) -> Option<&str> { + match self { + Value::String(s) | Value::RawString(s) | Value::Name(s) => Some(s), + _ => None, + } + } + /// The items of an array. + pub fn as_array(&self) -> Option<&[Value]> { + match self { + Value::Array(a) => Some(a), + _ => None, + } + } + /// An empty dictionary. + pub fn dict() -> Value { + Value::Dict(Vec::new()) + } +} + +/// Why an EngineData document did not parse: a message with the byte offset. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ParseError(pub String); + +impl std::fmt::Display for ParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "EngineData: {}", self.0) + } +} +impl std::error::Error for ParseError {} + +const MAX_DEPTH: usize = 64; + +struct Parser<'a> { + b: &'a [u8], + i: usize, + /// Best effort: damage ends the value being read instead of failing the document. + lenient: bool, +} + +/// Bytes that end a bare token or a `/Name`. +fn is_delimiter(c: u8) -> bool { + matches!(c, b' ' | b'\t' | b'\r' | b'\n' | b'[' | b']' | b'<' | b'>' | b'(' | b'/') +} + +impl Parser<'_> { + fn ws(&mut self) { + while matches!(self.b.get(self.i), Some(b' ' | b'\t' | b'\r' | b'\n' | 0)) { + self.i += 1; + } + } + fn err(&self, m: &str) -> Result { + Err(ParseError(format!("{m} at byte {}", self.i))) + } + fn value(&mut self, depth: usize) -> Result { + if depth > MAX_DEPTH { + return self.err("nesting too deep"); + } + self.ws(); + match self.b.get(self.i) { + None => self.err("unexpected end"), + Some(b'<') if self.b.get(self.i + 1) == Some(&b'<') => { + self.i += 2; + self.dict(depth) + } + Some(b'[') => { + self.i += 1; + self.array(depth) + } + Some(b'(') => { + self.i += 1; + self.string() + } + Some(b'/') => Ok(Value::Name(self.name())), + Some(_) => self.token(), + } + } + /// The entries after `<<`. + fn dict(&mut self, depth: usize) -> Result { + let mut items = Vec::new(); + loop { + self.ws(); + match self.b.get(self.i) { + Some(b'>') if self.b.get(self.i + 1) == Some(&b'>') => { + self.i += 2; + return Ok(Value::Dict(items)); + } + Some(b'/') => { + let k = self.name(); + match self.value(depth + 1) { + Ok(v) => items.push((k, v)), + Err(_) if self.lenient => return Ok(Value::Dict(items)), + Err(e) => return Err(e), + } + } + None if self.lenient => return Ok(Value::Dict(items)), + None => return self.err("unterminated dictionary"), + // Lenient: skip the byte that is not a key. + _ if self.lenient => self.i += 1, + _ => return self.err("expected key"), + } + } + } + /// The items after `[`. + fn array(&mut self, depth: usize) -> Result { + let mut items = Vec::new(); + loop { + self.ws(); + match self.b.get(self.i) { + Some(b']') => { + self.i += 1; + return Ok(Value::Array(items)); + } + None if self.lenient => return Ok(Value::Array(items)), + None => return self.err("unterminated array"), + _ => match self.value(depth + 1) { + Ok(v) => items.push(v), + Err(_) if self.lenient => return Ok(Value::Array(items)), + Err(e) => return Err(e), + }, + } + } + } + /// The string after `(`. + fn string(&mut self) -> Result { + let mut raw = Vec::new(); + loop { + match self.b.get(self.i) { + None if self.lenient => break, + None => return self.err("unterminated string"), + Some(b'\\') => { + if let Some(&c) = self.b.get(self.i + 1) { + raw.push(c); + } + self.i += 2; + } + Some(b')') => { + self.i += 1; + break; + } + Some(&c) => { + raw.push(c); + self.i += 1; + } + } + } + Ok(decode_string(&raw)) + } + /// A number, `true` or `false`. + fn token(&mut self) -> Result { + let start = self.i; + while self.b.get(self.i).is_some_and(|&c| !is_delimiter(c)) { + self.i += 1; + } + let tok = self.b.get(start..self.i).and_then(|t| std::str::from_utf8(t).ok()).unwrap_or(""); + let number = match tok { + "true" => return Ok(Value::Bool(true)), + "false" => return Ok(Value::Bool(false)), + "" => { + // A delimiter that starts no value (`>`, `]`), or bytes that are not UTF-8. + self.i = self.i.max(start + 1); + None + } + t if t.contains(['.', 'e', 'E']) => t.parse().ok().map(Value::Real), + t => t.parse().map(Value::Int).ok().or_else(|| t.parse().ok().map(Value::Real)), + }; + match number { + Some(v) => Ok(v), + None if self.lenient => Ok(Value::Name(tok.to_string())), + None => self.err("bad token"), + } + } + fn name(&mut self) -> String { + self.i += 1; // '/' + let start = self.i; + while self.b.get(self.i).is_some_and(|&c| !is_delimiter(c)) { + self.i += 1; + } + String::from_utf8_lossy(self.b.get(start..self.i).unwrap_or_default()).into_owned() + } +} + +fn decode_string(raw: &[u8]) -> Value { + if raw.len() >= 2 && raw[0] == 0xFE && raw[1] == 0xFF { + let u: Vec = raw[2..].as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); + Value::String(String::from_utf16_lossy(&u)) + } else { + Value::RawString(raw.iter().map(|&c| c as char).collect()) + } +} + +/// Parses an EngineData document (the top-level dictionary). +pub fn parse(data: &[u8]) -> Result { + let mut p = Parser { b: data, i: 0, lenient: false }; + p.value(0) +} + +/// Parses as much of a damaged EngineData document as can be read, for readers that only show +/// the text: a truncated dictionary, array or string keeps what it has, a byte that starts no +/// key is skipped, and a token that is no number becomes a [`Value::Name`]. Garbage gives an +/// empty dictionary. What [`parse`] accepts comes out the same. The result is not meant to be +/// written back. +pub fn parse_lossy(data: &[u8]) -> Value { + let mut p = Parser { b: data, i: 0, lenient: true }; + p.value(0).unwrap_or_else(|_| Value::dict()) +} + +/// Serializes in Photoshop's layout (tab-indented, one key per line). +pub fn write(v: &Value) -> Vec { + let mut out = b"\n\n".to_vec(); + write_value(v, 0, &mut out); + out.push(b'\n'); + out +} + +/// Serializes `/key value` pairs without an enclosing dictionary: the layout of the document's +/// `Txt2` block, which a reader wraps in `<< >>` to parse. +pub fn write_bare(items: &[(String, Value)]) -> Vec { + let mut out = b"\n\n".to_vec(); + for (k, v) in items { + out.push(b'/'); + out.extend_from_slice(k.as_bytes()); + if is_container(v) { + out.push(b'\n'); + indent(&mut out, 1); + } else { + out.push(b' '); + } + write_value(v, 1, &mut out); + out.push(b'\n'); + } + out +} + +fn indent(out: &mut Vec, n: usize) { + out.extend(std::iter::repeat_n(b'\t', n)); +} + +fn is_container(v: &Value) -> bool { + matches!(v, Value::Dict(_)) || matches!(v, Value::Array(a) if a.iter().any(|x| matches!(x, Value::Dict(_) | Value::Array(_)))) +} + +fn write_value(v: &Value, depth: usize, out: &mut Vec) { + match v { + Value::Dict(items) => { + out.extend_from_slice(b"<<\n"); + for (k, v) in items { + indent(out, depth + 1); + out.push(b'/'); + out.extend_from_slice(k.as_bytes()); + if is_container(v) { + out.push(b'\n'); + indent(out, depth + 1); + } else { + out.push(b' '); + } + write_value(v, depth + 1, out); + out.push(b'\n'); + } + indent(out, depth); + out.extend_from_slice(b">>"); + } + Value::Array(items) if is_container(v) => { + out.extend_from_slice(b"[\n"); + for x in items { + indent(out, depth); + write_value(x, depth, out); + out.push(b'\n'); + } + indent(out, depth); + out.push(b']'); + } + Value::Array(items) => { + out.push(b'['); + for x in items { + out.push(b' '); + write_value(x, depth, out); + } + out.extend_from_slice(b" ]"); + } + Value::RawString(s) if s.chars().all(|c| u32::from(c) <= 0xFF) => { + out.push(b'('); + for c in s.chars() { + let c = u32::from(c) as u8; + if matches!(c, b'(' | b')' | b'\\') { + out.push(b'\\'); + } + out.push(c); + } + out.push(b')'); + } + Value::String(s) | Value::RawString(s) => { + out.push(b'('); + let mut raw = vec![0xFE, 0xFF]; + for u in s.encode_utf16() { + raw.extend_from_slice(&u.to_be_bytes()); + } + for c in raw { + if matches!(c, b'(' | b')' | b'\\') { + out.push(b'\\'); + } + out.push(c); + } + out.push(b')'); + } + Value::Int(i) => { + let _ = write!(Wrap(out), "{i}"); + } + Value::Real(r) => out.extend_from_slice(format_real(*r).as_bytes()), + Value::Bool(b) => out.extend_from_slice(if *b { b"true" } else { b"false" }), + Value::Name(n) => { + out.push(b'/'); + out.extend_from_slice(n.as_bytes()); + } + } +} + +struct Wrap<'a>(&'a mut Vec); +impl std::fmt::Write for Wrap<'_> { + fn write_str(&mut self, s: &str) -> std::fmt::Result { + self.0.extend_from_slice(s.as_bytes()); + Ok(()) + } +} + +/// Photoshop style: `1.0`, `.5`, `-.25`. +fn format_real(r: f64) -> String { + if !r.is_finite() { + return "0.0".into(); + } + if r.fract() == 0.0 && r.abs() < 1e15 { + return format!("{r:.1}"); + } + let mut s = format!("{r:.5}"); + while s.ends_with('0') { + s.pop(); + } + if matches!(s.as_str(), "0." | "-0.") { + return "0.0".into(); + } + if s.ends_with('.') { + s.push('0'); + } + if let Some(rest) = s.strip_prefix("0.") { + s = format!(".{rest}"); + } else if let Some(rest) = s.strip_prefix("-0.") { + s = format!("-.{rest}"); + } + s +} + +/// The character and paragraph attributes of a type layer's first style run: what a reader that +/// shows the text in one style needs. Attributes the run leaves out come from the document's +/// first (normal) style sheet. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct TextStyle { + /// PostScript name of the font. + pub font: Option, + /// Font size in points, at most 10 000. + pub size: Option, + /// Fill colour, RGB in 0..1. + pub color: Option<[f64; 3]>, + /// 0 left, 1 right, 2 centre, 3 and above the justified variants. + pub justification: Option, + /// Tracking in 1/1000 em, within ±100 000. + pub tracking: Option, + /// Leading in points, within ±100 000; `None` for automatic leading. + pub leading: Option, + /// Faux bold. + pub faux_bold: bool, + /// Faux italic. + pub faux_italic: bool, + /// Paragraph (box) text rather than point text. + pub box_text: bool, +} + +/// Upper bound for a font size in points (Photoshop's own limit is 1296 pt). +const MAX_FONT_SIZE: f64 = 10_000.0; +/// Bound for tracking (1/1000 em) and leading (points). +const MAX_SPACING: f64 = 100_000.0; + +/// The first style run of a parsed EngineData document (see [`TextStyle`]). Missing and +/// non-finite values are `None`; sizes and spacings are clamped to sane bounds. +pub fn first_style(ed: &Value) -> TextStyle { + let engine = ed.get("EngineDict").unwrap_or(ed); + let resources = ed.get("ResourceDict").or_else(|| ed.get("DocumentResources")); + let run = engine.path(&["StyleRun", "RunArray"]).and_then(|a| a.index(0)).and_then(|r| r.path(&["StyleSheet", "StyleSheetData"])); + let normal = resources.and_then(|r| r.get("StyleSheetSet")).and_then(|a| a.index(0)).and_then(|s| s.get("StyleSheetData")); + let attr = |k: &str| run.and_then(|r| r.get(k)).or_else(|| normal.and_then(|n| n.get(k))); + let num = |k: &str| attr(k).and_then(Value::as_f64); + let font = num("Font") + .and_then(|i| resources?.get("FontSet")?.index(i as usize)?.get("Name")) + .and_then(Value::as_str) + .map(|s| s.trim_end_matches('\0').to_string()); + let color = attr("FillColor").and_then(|c| c.get("Values")).and_then(Value::as_array).and_then(|a| match a { + [_, r, g, b, ..] => Some([r.as_f64()?, g.as_f64()?, b.as_f64()?]), + _ => None, + }); + let para = engine.path(&["ParagraphRun", "RunArray"]).and_then(|a| a.index(0)).and_then(|r| r.path(&["ParagraphSheet", "Properties"])); + let para_normal = resources.and_then(|r| r.get("ParagraphSheetSet")).and_then(|a| a.index(0)).and_then(|s| s.get("Properties")); + let justification = para + .and_then(|p| p.get("Justification")) + .or_else(|| para_normal.and_then(|p| p.get("Justification"))) + .and_then(Value::as_f64) + .map(|n| n as u8); + let auto_leading = attr("AutoLeading").is_none_or(|v| *v != Value::Bool(false)); + let shape = engine.path(&["Rendered", "Shapes", "Children"]).and_then(|a| a.index(0)).and_then(|s| s.get("ShapeType")); + TextStyle { + font, + size: num("FontSize").filter(|s| *s > 0.0).map(|s| s.min(MAX_FONT_SIZE)), + color, + justification, + tracking: num("Tracking").map(|t| t.clamp(-MAX_SPACING, MAX_SPACING)), + leading: if auto_leading { None } else { num("Leading").map(|l| l.clamp(-MAX_SPACING, MAX_SPACING)) }, + faux_bold: attr("FauxBold") == Some(&Value::Bool(true)), + faux_italic: attr("FauxItalic") == Some(&Value::Bool(true)), + box_text: shape.and_then(Value::as_f64) == Some(1.0), + } +} + +/// A minimal EngineData document for a type layer with one style run: the text, one font, its +/// size in points, an RGB fill in 0..1 and the justification ([`TextStyle::justification`]). +pub fn minimal(text: &str, font: &str, size: f64, color: [f64; 3], justification: u8) -> Vec { + let dict = |items: Vec<(&str, Value)>| Value::Dict(items.into_iter().map(|(k, v)| (k.to_string(), v)).collect()); + let run = |sheet: Value| dict(vec![("RunArray", Value::Array(vec![sheet]))]); + let fill = Value::Array(vec![Value::Real(1.0), Value::Real(color[0]), Value::Real(color[1]), Value::Real(color[2])]); + let style = dict(vec![ + ("Font", Value::Int(0)), + ("FontSize", Value::Real(size)), + ("FillColor", dict(vec![("Type", Value::Int(1)), ("Values", fill)])), + ("AutoLeading", Value::Bool(true)), + ]); + let paragraph = dict(vec![("Properties", dict(vec![("Justification", Value::Int(i64::from(justification)))]))]); + let engine = dict(vec![ + ("Editor", dict(vec![("Text", Value::String(format!("{text}\r")))])), + ("ParagraphRun", run(dict(vec![("ParagraphSheet", paragraph)]))), + ("StyleRun", run(dict(vec![("StyleSheet", dict(vec![("StyleSheetData", style)]))]))), + ]); + let fonts = Value::Array(vec![dict(vec![("Name", Value::String(font.to_string())), ("Type", Value::Int(0))])]); + write(&dict(vec![("EngineDict", engine), ("ResourceDict", dict(vec![("FontSet", fonts)]))])) +} + +#[cfg(test)] +mod tests { + use super::*; + + const SAMPLE: &[u8] = b"\n\n<<\n\t/EngineDict\n\t<<\n\t\t/Editor\n\t\t<<\n\t\t\t/Text (\xFE\xFF\x00H\x00i\x00\\)\x00\r)\n\t\t>>\n\t\t/Nums [ 1.0 .5 -2 0 ]\n\t\t/Flag true\n\t\t/Kind /Roman\n\t\t/Runs [\n\t\t<<\n\t\t\t/A 1\n\t\t>>\n\t\t]\n\t>>\n>>"; + + #[test] + fn format_real_never_ends_with_bare_dot() { + assert_eq!(format_real(5.000001), "5.0"); + assert_eq!(format_real(12.000004), "12.0"); + assert_eq!(format_real(-5.000001), "-5.0"); + assert_eq!(format_real(1e20), "100000000000000000000.0"); + assert_eq!(format_real(0.5), ".5"); + assert_eq!(format_real(-0.25), "-.25"); + assert_eq!(format_real(1e-9), "0.0"); + assert_eq!(format_real(3.0), "3.0"); + } + + #[test] + fn parses_sample() { + let v = parse(SAMPLE).unwrap(); + assert_eq!(v.path(&["EngineDict", "Editor", "Text"]).and_then(Value::as_str), Some("Hi)\r")); + let nums: Vec = v.path(&["EngineDict", "Nums"]).unwrap().as_array().unwrap().iter().filter_map(Value::as_f64).collect(); + assert_eq!(nums, [1.0, 0.5, -2.0, 0.0]); + assert_eq!(v.path(&["EngineDict", "Flag"]).and_then(Value::as_bool), Some(true)); + assert_eq!(v.path(&["EngineDict", "Kind"]), Some(&Value::Name("Roman".into()))); + assert_eq!(v.path(&["EngineDict", "Runs"]).unwrap().as_array().unwrap()[0].get("A").and_then(Value::as_i64), Some(1)); + } + + #[test] + fn raw_strings_are_written_back_as_the_same_bytes() { + let v = parse(b"<< /2 (0100\\)) /3 (\xFE\xFF\x00h) >>").unwrap(); + assert_eq!(v.get("2"), Some(&Value::RawString("0100)".into()))); + assert_eq!(v.get("2").and_then(Value::as_str), Some("0100)")); + assert_eq!(v.get("3"), Some(&Value::String("h".into()))); + let bytes = write(&v); + assert!(bytes.windows(8).any(|w| w == b"(0100\\))"), "{:?}", String::from_utf8_lossy(&bytes)); + assert_eq!(parse(&bytes).unwrap(), v); + } + + #[test] + fn write_parse_roundtrip() { + let v = parse(SAMPLE).unwrap(); + let bytes = write(&v); + assert_eq!(parse(&bytes).unwrap(), v); + // Stable: writing twice gives identical bytes. + assert_eq!(write(&parse(&bytes).unwrap()), bytes); + } + + #[test] + fn reals_format_like_photoshop() { + assert_eq!(format_real(1.0), "1.0"); + assert_eq!(format_real(0.5), ".5"); + assert_eq!(format_real(-0.25), "-.25"); + assert_eq!(format_real(12.125), "12.125"); + } + + #[test] + fn tiny_reals_round_trip_as_numbers() { + for value in [0.000004, -0.000004] { + assert_eq!(format_real(value), "0.0"); + let source = Value::Dict(vec![( + "EngineDict".into(), + Value::Dict(vec![("StyleRun".into(), Value::Array(vec![Value::Dict(vec![("BaselineShift".into(), Value::Real(value))])]))]), + )]); + let encoded = write(&source); + let parsed = parse(&encoded).unwrap(); + let baseline_shift = parsed + .path(&["EngineDict", "StyleRun"]) + .and_then(Value::as_array) + .and_then(|runs| runs.first()) + .and_then(|run| run.get("BaselineShift")) + .and_then(Value::as_f64); + assert_eq!(baseline_shift, Some(0.0)); + } + } + + #[test] + fn first_style_reads_a_minimal_document() { + let b = minimal("Hi (there)", "Arial-BoldMT", 36.0, [1.0, 0.5, 0.0], 2); + let v = parse(&b).unwrap(); + assert_eq!(v.path(&["EngineDict", "Editor", "Text"]).and_then(Value::as_str), Some("Hi (there)\r")); + let st = first_style(&v); + assert_eq!(st.font.as_deref(), Some("Arial-BoldMT")); + assert_eq!(st.size, Some(36.0)); + assert_eq!(st.color, Some([1.0, 0.5, 0.0])); + assert_eq!(st.justification, Some(2)); + assert_eq!(st.leading, None); + assert!(!st.box_text && !st.faux_bold && !st.faux_italic); + } + + #[test] + fn first_style_falls_back_to_the_normal_style_sheet_and_reads_box_text() { + let v = parse( + b"<< /EngineDict << /StyleRun << /RunArray [ << /StyleSheet << /StyleSheetData << /FauxBold true >> >> >> ] >> \ + /Rendered << /Shapes << /Children [ << /ShapeType 1 >> ] >> >> >> \ + /ResourceDict << /FontSet [ << /Name (A) >> << /Name (B) >> ] \ + /StyleSheetSet [ << /StyleSheetData << /Font 1 /FontSize 12.0 /Tracking 50 /AutoLeading false /Leading 14.5 >> >> ] \ + /ParagraphSheetSet [ << /Properties << /Justification 1 >> >> ] >> >>", + ) + .unwrap(); + let st = first_style(&v); + assert_eq!(st.font.as_deref(), Some("B")); + assert_eq!((st.size, st.tracking, st.leading), (Some(12.0), Some(50.0), Some(14.5))); + assert_eq!(st.justification, Some(1)); + assert!(st.faux_bold && !st.faux_italic && st.box_text); + } + + /// Sizes and spacings are untrusted: non-finite values are dropped and huge ones clamped. + #[test] + fn first_style_drops_non_finite_numbers_and_clamps_the_rest() { + let sheet = |attrs: &str| { + let doc = format!("<< /EngineDict << /StyleRun << /RunArray [ << /StyleSheet << /StyleSheetData << {attrs} >> >> >> ] >> >> >>"); + first_style(&parse(doc.as_bytes()).unwrap()) + }; + let st = sheet("/FontSize nan /Tracking inf /Leading -inf /AutoLeading false"); + assert_eq!((st.size, st.tracking, st.leading), (None, None, None)); + let st = sheet("/FontSize 1e300 /Tracking -1e300 /AutoLeading false /Leading 1e300"); + assert_eq!((st.size, st.tracking, st.leading), (Some(MAX_FONT_SIZE), Some(-MAX_SPACING), Some(MAX_SPACING))); + assert_eq!(sheet("/FontSize -4").size, None); + assert_eq!(first_style(&Value::dict()), TextStyle::default()); + } + + #[test] + fn nesting_deeper_than_the_limit_is_an_error() { + let nested = |n: usize| [vec![b'['; n], vec![b']'; n]].concat(); + assert!(parse(&nested(MAX_DEPTH)).is_ok()); + assert!(parse(&nested(MAX_DEPTH + 2)).is_err()); + } + + #[test] + fn parse_lossy_keeps_what_a_damaged_document_holds() { + // What the strict parser accepts comes out the same. + assert_eq!(parse_lossy(SAMPLE), parse(SAMPLE).unwrap()); + // Truncated inside a string inside an array inside a dictionary. + let v = parse_lossy(b"<< /A [ 1 2 << /B (unterminated"); + assert_eq!(v.get("A").and_then(|a| a.index(1)).and_then(Value::as_i64), Some(2)); + assert_eq!(v.get("A").and_then(|a| a.index(2)).and_then(|d| d.get("B")).and_then(Value::as_str), Some("unterminated")); + // A byte that starts no key is skipped; a bad token is a name, not a number. + let v = parse_lossy(b"<< ] /Size 12.0 /Bad 1.2.3 /After true >>"); + assert_eq!(v.get("Size").and_then(Value::as_f64), Some(12.0)); + assert_eq!(v.get("Bad"), Some(&Value::Name("1.2.3".into()))); + assert_eq!(v.get("After"), Some(&Value::Bool(true))); + // Garbage and every truncation: no panic, no hang. + assert_eq!(parse_lossy(b""), Value::dict()); + let _ = parse_lossy(b"]]]>>>> /x"); + let _ = parse_lossy(&[0xFF; 64]); + for cut in 0..SAMPLE.len() { + let _ = parse_lossy(&SAMPLE[..cut]); + } + // Nesting past the limit ends there; the outer levels stay. + let deep = [b"<< /K ".as_slice(), &[b'['; 200]].concat(); + assert!(matches!(parse_lossy(&deep).get("K"), Some(Value::Array(_)))); + } + + #[test] + fn malformed_input_never_panics() { + for cut in 0..SAMPLE.len() { + let _ = parse(&SAMPLE[..cut]); + } + assert!(parse(b"<< /A").is_err()); + assert!(parse(&[b'['; 200]).is_err()); + } +} diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 17b081aa8..0aee070e0 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -15,6 +15,7 @@ //! * [`Head`] reads only the header, colour mode data and image resources (size, ICC profile, //! EXIF, XMP) from the start of a file, without the layers or pixels. //! * [`descriptor`] parses and writes ActionDescriptors. +//! * [`engine_data`] parses and writes the text engine document of type layers. //! //! Implemented clean-room from Adobe's public "Photoshop File Formats //! Specification". Undocumented details that follow MIT-licensed psd-tools @@ -55,6 +56,7 @@ mod codepage; pub mod compression; pub mod csh; pub mod descriptor; +pub mod engine_data; pub mod error; pub mod file; pub mod filter_effects; From 5ebc719ca2d3da534887dd3b2d2b1c4760b89c90 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:51:44 -0500 Subject: [PATCH 028/120] craft-drawingml: model types for fills, lines, text, tables, charts and diagrams (#901) * Add craft-drawingml: units, colours, preset shapes, custom geometry and text warps The parts of DrawingML that need no document model, moved from DeckCraft (60f91f78): crates/color, crates/geom (preset, warp, Xfrm and the EMU conversions) and crates/pptx/src/custgeom.rs with CustomPath from crates/model. The code is unchanged except that custgeom reads craft-xml trees and writes to a craft-xml Writer instead of DeckCraft's own XML helper, and matches elements by local name. The 59 tests of the moved code come with it; six more cover the custom geometry limits. * drawingml: evaluate a custom geometry's guide lists once per coordinate space read_cust_geom rebuilt Guides and evaluated avLst and gdLst again for every path: up to 1,000 paths times up to 10,000 guides, each name copied and hashed. Guides are now cached per path coordinate space (w, h). At most 16 spaces and 4 MiB of guide names are kept per geometry; paths in further spaces are left out. The first space is always kept, and a geometry without guides is not capped. Ported from storytold/deckcraft#1789. A list counts as having guides only when it has gd children, as that PR's review asked. * drawingml: the model types for fills, lines, text, tables, charts and diagrams Moved from DeckCraft (60f91f78) crates/model/src: style.rs, text.rs, table.rs, chart.rs, numfmt.rs and trendline.rs, plus Diagram, DiagramNode and the MediaId and SlideId newtypes from lib.rs. The files are unchanged except that deckcraft_color:: is crate::color:: and Fill and TableBorder allow clippy::large_enum_variant. Their 60 tests come with them. Readers and writers follow. --- Cargo.lock | 1 + crates/drawingml/Cargo.toml | 3 + crates/drawingml/README.md | 15 +- crates/drawingml/src/chart.rs | 1678 +++++++++++++++++++++++++++++ crates/drawingml/src/diagram.rs | 53 + crates/drawingml/src/lib.rs | 38 + crates/drawingml/src/numfmt.rs | 415 +++++++ crates/drawingml/src/style.rs | 572 ++++++++++ crates/drawingml/src/table.rs | 649 +++++++++++ crates/drawingml/src/text.rs | 819 ++++++++++++++ crates/drawingml/src/trendline.rs | 766 +++++++++++++ 11 files changed, 5008 insertions(+), 1 deletion(-) create mode 100644 crates/drawingml/src/chart.rs create mode 100644 crates/drawingml/src/diagram.rs create mode 100644 crates/drawingml/src/numfmt.rs create mode 100644 crates/drawingml/src/style.rs create mode 100644 crates/drawingml/src/table.rs create mode 100644 crates/drawingml/src/text.rs create mode 100644 crates/drawingml/src/trendline.rs diff --git a/Cargo.lock b/Cargo.lock index 9ab6b0dd0..c66f79209 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -985,6 +985,7 @@ dependencies = [ "craft-xml", "kurbo 0.13.1", "serde", + "serde_json", ] [[package]] diff --git a/crates/drawingml/Cargo.toml b/crates/drawingml/Cargo.toml index a643688ae..6873a91e1 100644 --- a/crates/drawingml/Cargo.toml +++ b/crates/drawingml/Cargo.toml @@ -14,3 +14,6 @@ workspace = true craft-xml = { workspace = true } kurbo = { workspace = true } serde = { workspace = true } + +[dev-dependencies] +serde_json = { workspace = true } diff --git a/crates/drawingml/README.md b/crates/drawingml/README.md index 3f7f1375d..a58b039c2 100644 --- a/crates/drawingml/README.md +++ b/crates/drawingml/README.md @@ -16,7 +16,20 @@ files store EMU (12 700 per point). | `custgeom` | `read_cust_geom` / `write_cust_geom` between `a:custGeom` and `CustomPath` (an `M L C Q Z` path string per sub-path), and `Guides`, the shape guide formula evaluator | `a:custGeom`, `a:gd` | | `warp` | `warp(name, adjusts, rect)`: the bands and guide curves of a preset text warp, and the mapping of laid-out lines onto them | `a:prstTxWarp` | -Fills, lines, effects, text bodies, tables, themes and charts are not here yet. +## The model + +These modules hold types only; their readers and writers are not here yet. + +| Module | Types | Markup they stand for | +|---|---|---| +| `style` | `ColorRef` (a base colour plus transforms, resolved against a `ColorScheme`), `Fill`, `Gradient`, `PictureFill`, `Line`, `Effects`, `ShapeStyle` | `a:solidFill`, `a:gradFill`, `a:blipFill`, `a:pattFill`, `a:ln`, `a:effectLst`, `p:style` | +| `text` | `TextBody`, `Paragraph`, `ParaProps`, `Run`, `RunProps`, `ListStyle`, hyperlinks and actions | `a:txBody`, `a:bodyPr`, `a:lstStyle`, `a:p`, `a:r` | +| `table` | `Table`, `Row`, `Cell`, `TableStyleDef` and the built-in table styles | `a:tbl`, `a:tblStyleLst` | +| `chart` | `Chart`, `Series`, axes, data labels, trendlines and error bars, with the values they compute | `c:chartSpace`, `cx:chartSpace` | +| `numfmt` | `format(code, value, lang)`: number format codes as charts and cells use them | `c:numFmt` | +| `diagram` | `Diagram`, `DiagramNode` | `dgm:dataModel` | + +A picture or sound is a `MediaId`; whoever owns the package keeps the bytes. Themes are not here yet. ## Preset shapes and text warps diff --git a/crates/drawingml/src/chart.rs b/crates/drawingml/src/chart.rs new file mode 100644 index 000000000..c93d12f25 --- /dev/null +++ b/crates/drawingml/src/chart.rs @@ -0,0 +1,1678 @@ +//! Charts: an embedded data grid plus type and element options. + +use serde::{Deserialize, Serialize}; + +use crate::style::{ColorRef, Fill, Line}; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum ChartType { + #[default] + Column, + StackedColumn, + PercentColumn, + Bar, + StackedBar, + PercentBar, + Line, + LineMarkers, + StackedLine, + Area, + StackedArea, + Pie, + Doughnut, + Scatter, + Bubble, + Radar, + FilledRadar, + Stock, + Surface, + Histogram, + Pareto, + BoxWhisker, + Waterfall, + Funnel, + Treemap, + Sunburst, + /// A filled (choropleth) map of regions coloured by value, kept and drawn from its data; it + /// is not offered in the chart menus (PowerPoint needs an online map service to make one). + RegionMap, + Combo, +} + +impl ChartType { + /// The Office 2016 (`chartEx`) types, offered after [`ChartType::MAIN`] in chart menus. + pub const OFFICE_2016: [ChartType; 7] = [ + ChartType::Waterfall, + ChartType::Funnel, + ChartType::Histogram, + ChartType::Pareto, + ChartType::BoxWhisker, + ChartType::Treemap, + ChartType::Sunburst, + ]; + + /// Whether the type is saved as a `chartEx` part (with a plain chart as the fallback). + pub fn is_chartex(self) -> bool { + Self::OFFICE_2016.contains(&self) || self == ChartType::RegionMap + } + + pub const MAIN: [ChartType; 12] = [ + ChartType::Column, + ChartType::Bar, + ChartType::Line, + ChartType::Area, + ChartType::Pie, + ChartType::Doughnut, + ChartType::Scatter, + ChartType::Radar, + ChartType::Waterfall, + ChartType::Funnel, + ChartType::Histogram, + ChartType::Treemap, + ]; + pub fn label(self) -> &'static str { + match self { + ChartType::Column => "Clustered Column", + ChartType::StackedColumn => "Stacked Column", + ChartType::PercentColumn => "100% Stacked Column", + ChartType::Bar => "Clustered Bar", + ChartType::StackedBar => "Stacked Bar", + ChartType::PercentBar => "100% Stacked Bar", + ChartType::Line => "Line", + ChartType::LineMarkers => "Line with Markers", + ChartType::StackedLine => "Stacked Line", + ChartType::Area => "Area", + ChartType::StackedArea => "Stacked Area", + ChartType::Pie => "Pie", + ChartType::Doughnut => "Doughnut", + ChartType::Scatter => "Scatter", + ChartType::Bubble => "Bubble", + ChartType::Radar => "Radar", + ChartType::FilledRadar => "Filled Radar", + ChartType::Stock => "Stock", + ChartType::Surface => "Surface", + ChartType::Histogram => "Histogram", + ChartType::Pareto => "Pareto", + ChartType::BoxWhisker => "Box and Whisker", + ChartType::Waterfall => "Waterfall", + ChartType::Funnel => "Funnel", + ChartType::Treemap => "Treemap", + ChartType::Sunburst => "Sunburst", + ChartType::RegionMap => "Map", + ChartType::Combo => "Combo", + } + } + pub fn is_bar_like(self) -> bool { + matches!( + self, + ChartType::Column + | ChartType::StackedColumn + | ChartType::PercentColumn + | ChartType::Bar + | ChartType::StackedBar + | ChartType::PercentBar + | ChartType::Histogram + | ChartType::Pareto + | ChartType::Waterfall + ) + } + pub fn horizontal(self) -> bool { + matches!(self, ChartType::Bar | ChartType::StackedBar | ChartType::PercentBar | ChartType::Funnel) + } + pub fn stacked(self) -> bool { + matches!( + self, + ChartType::StackedColumn + | ChartType::StackedBar + | ChartType::StackedLine + | ChartType::StackedArea + | ChartType::PercentColumn + | ChartType::PercentBar + ) + } + pub fn percent(self) -> bool { + matches!(self, ChartType::PercentColumn | ChartType::PercentBar) + } +} + +/// `c:trendlineType`: the six trendlines DrawingML charts store. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum TrendlineType { + Exp, + #[default] + Linear, + Log, + MovingAvg, + Poly, + Power, +} + +impl TrendlineType { + pub fn xml(self) -> &'static str { + match self { + TrendlineType::Exp => "exp", + TrendlineType::Linear => "linear", + TrendlineType::Log => "log", + TrendlineType::MovingAvg => "movingAvg", + TrendlineType::Poly => "poly", + TrendlineType::Power => "power", + } + } + pub fn from_xml(s: &str) -> Option { + Some(match s { + "exp" => TrendlineType::Exp, + "linear" => TrendlineType::Linear, + "log" => TrendlineType::Log, + "movingAvg" => TrendlineType::MovingAvg, + "poly" => TrendlineType::Poly, + "power" => TrendlineType::Power, + _ => return None, + }) + } +} + +/// One `c:trendline` on a series. Polynomials use `order` (2–6); moving averages use `period`. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Trendline { + pub kind: TrendlineType, + /// `c:name`, when the file sets one. + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + /// Polynomial order (`c:order`). + #[serde(skip_serializing_if = "Option::is_none")] + pub order: Option, + /// Moving-average period (`c:period`). + #[serde(skip_serializing_if = "Option::is_none")] + pub period: Option, + /// Forecast forward and backward, in category units (`c:forward`, `c:backward`). + #[serde(skip_serializing_if = "Option::is_none")] + pub forward: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub backward: Option, + /// Cross the value axis at this value (`c:intercept`). + #[serde(skip_serializing_if = "Option::is_none")] + pub intercept: Option, + /// Show the equation (`c:dispEq`). + pub disp_eq: bool, + /// Show R² (`c:dispRSqr`). + pub disp_r_sqr: bool, +} + +impl Default for Trendline { + fn default() -> Self { + Trendline { + kind: TrendlineType::Linear, + name: None, + order: None, + period: None, + forward: None, + backward: None, + intercept: None, + disp_eq: false, + disp_r_sqr: false, + } + } +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Series { + pub name: String, + pub values: Vec>, + /// X values for scatter/bubble. + pub x_values: Vec>, + pub sizes: Vec>, + pub fill: Option, + pub line: Option, + /// Per-point fill overrides (pie slices). + pub point_fills: Vec<(usize, Fill)>, + pub data_labels: Option, + pub smooth: bool, + pub marker: Option, + /// Combo charts: this series' own type. + pub kind: Option, + pub secondary_axis: bool, + /// The number format of the series' data labels (Excel code): the labels' own, else the + /// source data's. + #[serde(skip_serializing_if = "Option::is_none")] + pub number_format: Option, + /// Trendlines (`c:trendline`), in file order. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub trendlines: Vec, + /// `c:errBars` on the values (y bars; a bar chart's run along its value axis). + #[serde(skip_serializing_if = "Option::is_none")] + pub error_bars: Option, +} + +/// Error bars (`c:errBars`): how far each point's bar reaches below and above it. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct ErrorBars { + /// `both`, `plus` or `minus`. + pub bar_type: String, + /// `fixedVal`, `percentage`, `stdDev`, `stdErr` or `cust`. + pub val_type: String, + pub value: f64, + /// `cust`: the per-point plus and minus amounts. + pub plus: Vec>, + pub minus: Vec>, + pub end_cap: bool, +} + +impl Default for ErrorBars { + fn default() -> Self { + ErrorBars { bar_type: "both".into(), val_type: "fixedVal".into(), value: 1.0, plus: vec![], minus: vec![], end_cap: true } + } +} + +/// Count, mean and sample standard deviation of a series' finite values (for error bars). +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct SeriesStats { + pub n: f64, + pub mean: f64, + pub sd: f64, +} + +impl SeriesStats { + pub fn of(values: &[Option]) -> Self { + let vals = || values.iter().flatten().copied().filter(|v| v.is_finite()); + let n = vals().count() as f64; + let mean = vals().sum::() / n.max(1.0); + let sd = if n > 1.0 { (vals().map(|x| (x - mean).powi(2)).sum::() / (n - 1.0)).sqrt() } else { 0.0 }; + SeriesStats { n, mean, sd } + } +} + +impl ErrorBars { + /// Where point `i` (value `v`) of a series with `values` has its bar: (low, high) in data units. + /// Standard deviation bars stand around the series' mean, as Excel draws them; the others + /// around the point. `None` for an amount that isn't a finite number. + pub fn range(&self, values: &[Option], i: usize, v: f64) -> Option<(f64, f64)> { + self.range_in(&SeriesStats::of(values), i, v) + } + + /// [`ErrorBars::range`] with the series' statistics worked out once ([`SeriesStats::of`]), so + /// drawing every point of a long series stays linear. + pub fn range_in(&self, stats: &SeriesStats, i: usize, v: f64) -> Option<(f64, f64)> { + let SeriesStats { n, mean, sd } = *stats; + let (centre, minus, plus) = match self.val_type.as_str() { + "percentage" => (v, (v * self.value / 100.0).abs(), (v * self.value / 100.0).abs()), + "stdDev" => (mean, sd * self.value, sd * self.value), + "stdErr" => (v, sd / n.max(1.0).sqrt(), sd / n.max(1.0).sqrt()), + "cust" => (v, self.minus.get(i).copied().flatten().unwrap_or(0.0), self.plus.get(i).copied().flatten().unwrap_or(0.0)), + _ => (v, self.value, self.value), + }; + let (lo, hi) = match self.bar_type.as_str() { + "plus" => (centre, centre + plus), + "minus" => (centre - minus, centre), + _ => (centre - minus, centre + plus), + }; + (lo.is_finite() && hi.is_finite()).then_some((lo, hi)) + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Chart { + pub kind: ChartType, + pub title: Option, + pub categories: Vec, + pub series: Vec, + pub legend: Option, + /// `c:legend/c:overlay`: the legend is drawn over the plot instead of shrinking it. + pub legend_overlay: bool, + /// Text size in points of the axis labels and legend (from their `c:txPr`, else the chart's); + /// `None`: the file gives none. + #[serde(skip_serializing_if = "Option::is_none")] + pub text_size: Option, + /// Title text size in points (its rich text or `c:txPr`). + #[serde(skip_serializing_if = "Option::is_none")] + pub title_size: Option, + pub data_labels: bool, + pub gridlines: bool, + /// `c:minorGridlines` on the value axis: five to a major unit, between the major ones. + pub minor_gridlines: bool, + pub axis_titles: (Option, Option), + /// Chart style number (1–48) and colour palette (`colorful1`, `monochrome3`…). + pub style: u32, + pub palette: String, + pub plot_fill: Option, + pub chart_fill: Option, + pub text_color: Option, + pub hole_size: f64, + pub gap_width: f64, + pub overlap: f64, + pub vary_colors: bool, + pub y_min: Option, + pub y_max: Option, + /// The value axis' major unit, when the chart fixes it. + pub y_major: Option, + /// `c:scaling/c:logBase`: the value axis is logarithmic in this base (2–1000). + pub y_log_base: Option, + /// The value axis' number format (an Excel format code such as `#,##0` or `0.0%`). + pub y_format: Option, + /// The value axis is deleted (`c:delete`): no labels, though gridlines may stay. + pub y_hidden: bool, + /// The category axis runs backwards (`c:orientation val="maxMin"`): right to left, or on a + /// horizontal bar chart top to bottom (the default puts the first category at the bottom). + pub cat_reversed: bool, + /// A combo chart's secondary value axis: bounds, major unit and number format. + pub y2_min: Option, + pub y2_max: Option, + pub y2_major: Option, + pub y2_format: Option, + pub y2_hidden: bool, + /// The chart's language (`c:lang`, e.g. `ru-RU`): digit grouping and decimal mark. + pub lang: Option, + /// A waterfall's total points (`cx:subtotals`): bars that stand on the axis and restart + /// the running total, instead of floating. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub subtotals: Vec, + /// Outer category levels of a treemap or sunburst, innermost first (the leaves' parents, + /// then theirs…), each with one label per category. A blank label continues the one above it. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub category_levels: Vec>, + /// Box and whisker quartiles by the inclusive method (QUARTILE.INC) instead of exclusive. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub inclusive_quartiles: bool, + /// A stock chart's up/down bars (`c:upDownBars`): open-high-low-close, the box between + /// the first and last series, hollow when the price rose. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub up_down_bars: bool, + /// A histogram's bin count (`cx:binCount`); with neither this nor `bin_size` the bins are + /// automatic (Scott's rule, as Excel does). + #[serde(skip_serializing_if = "Option::is_none")] + pub bin_count: Option, + /// A histogram's bin width (`cx:binSize`). + #[serde(skip_serializing_if = "Option::is_none")] + pub bin_size: Option, + /// Original chart XML part kept for round-trip of unmodelled options. + #[serde(skip_serializing_if = "Option::is_none")] + pub raw: Option, + /// The chart was too large to model: `raw` and `kept` are written back unchanged on save. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub verbatim: bool, + /// Package parts the original chart still points at (embedded workbook, styles, drawings). + /// The PPTX writer clears this before comparing modelled state. Native saves keep it too. + #[serde(skip_serializing_if = "ChartKept::is_empty")] + pub kept: ChartKept, +} + +/// One relationship of a chart part, with the target string as it was written. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ChartRel { + pub id: String, + pub rel_type: String, + pub target: String, + /// Absolute name of the part an internal relationship reaches (matches a `ChartPart::name` + /// up to case); empty for external ones. The writer renames kept parts and uses this to + /// point the relationship at the new name. + pub part: String, + pub external: bool, +} + +/// A part reached from a chart (the workbook, a drawing, an image it uses). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ChartPart { + /// Absolute part name (`ppt/embeddings/Workbook.xlsx`). + pub name: String, + pub content_type: String, + /// Runtime bytes; older native files stored these inline in the manifest. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub bytes: Vec, + /// Native ZIP member while encoding/decoding. The native reader consumes this reference. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub native_entry: Option, +} + +/// Sidecar for an unchanged chart: relationships and the parts they reach. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct ChartKept { + pub rels: Vec, + pub parts: Vec, +} + +impl ChartKept { + fn is_empty(&self) -> bool { + self.rels.is_empty() && self.parts.is_empty() + } +} + +impl Default for Chart { + fn default() -> Self { + Chart { + kind: ChartType::Column, + title: Some("Chart Title".into()), + categories: vec![], + series: vec![], + legend: Some("b".into()), + legend_overlay: false, + text_size: None, + title_size: None, + data_labels: false, + gridlines: true, + minor_gridlines: false, + axis_titles: (None, None), + style: 2, + palette: "colorful1".into(), + plot_fill: None, + chart_fill: None, + text_color: None, + hole_size: 0.5, + gap_width: 2.19, + overlap: -0.27, + vary_colors: false, + y_min: None, + y_max: None, + y_major: None, + y_log_base: None, + y_format: None, + y_hidden: false, + cat_reversed: false, + y2_min: None, + y2_max: None, + y2_major: None, + y2_format: None, + y2_hidden: false, + lang: None, + subtotals: vec![], + category_levels: vec![], + inclusive_quartiles: false, + up_down_bars: false, + bin_count: None, + bin_size: None, + raw: None, + verbatim: false, + kept: ChartKept::default(), + } + } +} + +impl Chart { + fn sample_treemap_sunburst(kind: ChartType) -> Self { + let mut c = Chart { + kind, + categories: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"].iter().map(ToString::to_string).collect(), + category_levels: vec![["Q1", "Q1", "Q1", "Q2", "Q2", "Q2", "Q3", "Q3"].iter().map(ToString::to_string).collect()], + vary_colors: true, + ..Default::default() + }; + let v = [4.2, 2.8, 3.6, 4.7, 2.6, 4.1, 1.9, 3.0]; + c.series.push(Series { name: "Series 1".into(), values: v.iter().map(|x| Some(*x)).collect(), ..Default::default() }); + c + } + + fn sample_observation_chart(kind: ChartType) -> Self { + let obs = [12.0, 15.0, 17.0, 18.0, 21.0, 22.0, 22.0, 24.0, 25.0, 27.0, 28.0, 31.0, 33.0, 36.0, 41.0, 48.0]; + let n_series = if kind == ChartType::BoxWhisker { 2 } else { 1 }; + let categories = if kind == ChartType::BoxWhisker { + (0..obs.len()).map(|i| if i % 2 == 0 { "Group A" } else { "Group B" }.to_string()).collect() + } else { + vec![] + }; + let mut c = + Chart { kind, categories, gridlines: true, legend: (kind == ChartType::BoxWhisker).then(|| "b".to_string()), ..Default::default() }; + for s in 0..n_series { + c.series.push(Series { + name: format!("Series {}", s + 1), + values: obs.iter().map(|v| Some(v + s as f64 * 6.0)).collect(), + ..Default::default() + }); + } + c + } + + fn sample_stock() -> Self { + let mut c = Chart { + kind: ChartType::Stock, + categories: ["Mon", "Tue", "Wed", "Thu", "Fri"].iter().map(ToString::to_string).collect(), + ..Default::default() + }; + let hlc = [("High", [55.0, 57.0, 57.0, 58.0, 58.0]), ("Low", [11.0, 12.0, 13.0, 11.0, 25.0]), ("Close", [25.0, 38.0, 50.0, 35.0, 43.0])]; + for (name, v) in hlc { + c.series.push(Series { name: name.into(), values: v.iter().map(|x| Some(*x)).collect(), ..Default::default() }); + } + c + } + + /// The sample data a new chart starts with (our own numbers). + pub fn sample(kind: ChartType) -> Self { + if matches!(kind, ChartType::Treemap | ChartType::Sunburst) { + return Self::sample_treemap_sunburst(kind); + } + if matches!(kind, ChartType::Histogram | ChartType::Pareto | ChartType::BoxWhisker) { + return Self::sample_observation_chart(kind); + } + if kind == ChartType::Stock { + return Self::sample_stock(); + } + let cats = ["Q1", "Q2", "Q3", "Q4"]; + let data = [[4.2, 2.6, 2.1], [2.8, 4.1, 2.4], [3.6, 1.9, 3.3], [4.7, 3.0, 5.1]]; + let mut c = Chart { kind, categories: cats.iter().map(ToString::to_string).collect(), ..Default::default() }; + let n_series = + if matches!(kind, ChartType::Pie | ChartType::Doughnut | ChartType::Funnel | ChartType::Waterfall | ChartType::Treemap) { 1 } else { 3 }; + for s in 0..n_series { + c.series.push(Series { + name: format!("Series {}", s + 1), + values: data.iter().map(|row| row.get(s).copied()).collect(), + x_values: if kind == ChartType::Scatter { (1..=4).map(|x| Some(x as f64)).collect() } else { vec![] }, + ..Default::default() + }); + } + c.vary_colors = n_series == 1; + if kind == ChartType::Combo { + // Columns with the last series as a line, as PowerPoint's combo starts. + for (i, s) in c.series.iter_mut().enumerate() { + s.kind = Some(if i + 1 == n_series { ChartType::Line } else { ChartType::Column }); + } + } + c + } + /// A series' own chart type: its own in a combo chart (columns when it names none), else the + /// chart's. + pub fn series_kind(&self, s: &Series) -> ChartType { + match (self.kind, s.kind) { + (ChartType::Combo, Some(k)) if k != ChartType::Combo => k, + (ChartType::Combo, _) => ChartType::Column, + (k, _) => k, + } + } + + /// Whether a series is plotted against the secondary value axis (combo charts only). + pub fn on_secondary(&self, s: &Series) -> bool { + self.kind == ChartType::Combo && s.secondary_axis + } + + /// The value range of one value axis: the primary axis (`y_min`/`y_max` apply), or a combo + /// chart's secondary axis. A combo chart stacks only its stacked series. `None` when no series + /// uses that axis. + pub fn axis_range(&self, secondary: bool) -> Option<(f64, f64)> { + if self.kind != ChartType::Combo { + return (!secondary).then(|| self.value_range()); + } + let (lo, hi) = self.plotted_extent(secondary)?; + let (lo, hi) = (lo.min(0.0), hi.max(0.0)); + Some(if secondary { (lo, hi) } else { (self.y_min.unwrap_or(lo), self.y_max.unwrap_or(hi)) }) + } + + /// The smallest and largest value plotted against one value axis: plain values as they are, + /// stacked series as running sums from zero, 100 % series as 0–1. `(0, 0)` without finite + /// values; `None` when no series uses the axis. + fn plotted_extent(&self, secondary: bool) -> Option<(f64, f64)> { + if self.kind == ChartType::Waterfall { + return (!secondary).then(|| self.value_range()); + } + let ser: Vec<&Series> = self.series.iter().filter(|s| self.on_secondary(s) == secondary).collect(); + if ser.is_empty() { + return (!secondary && self.kind != ChartType::Combo).then_some((0.0, 0.0)); + } + let mut ext = (f64::INFINITY, f64::NEG_INFINITY); + let has_stacked_or_percent = ser.iter().any(|s| { + let k = self.series_kind(s); + k.stacked() || k.percent() + }); + let percent = if has_stacked_or_percent { + self.fold_stacked(&ser, &mut ext) + } else { + for s in &ser { + for v in s.values.iter().flatten().copied().filter(|v| v.is_finite()) { + widen(&mut ext, v); + } + } + false + }; + if !percent { + self.fold_trendlines(&ser, &mut ext); + } + if percent { + ext.0 = ext.0.min(0.0); + ext.1 = ext.1.max(1.0); + } + let (lo, hi) = ext; + Some(if lo <= hi { (lo, hi) } else { (0.0, 0.0) }) + } + + /// Widens `ext` by the points of the stacked and 100 % series in `ser`; returns whether a + /// 100 % series is among them. + fn fold_stacked(&self, ser: &[&Series], ext: &mut (f64, f64)) -> bool { + let mut percent = false; + let n = ser.iter().map(|s| s.values.len()).max().unwrap_or(0); + let budget = (ser.len() * n).min(10_000_000); + let n_clamped = if ser.is_empty() { 0 } else { n.min(budget / ser.len()) }; + for i in 0..n_clamped { + let (mut p, mut m, mut stacked) = (0.0, 0.0, false); + for s in ser { + let k = self.series_kind(s); + let Some(v) = s.values.get(i).copied().flatten().filter(|v| v.is_finite()) else { continue }; + if k.percent() { + percent = true; + } else if k.stacked() { + stacked = true; + if v >= 0.0 { p += v } else { m += v } + } else { + widen(ext, v); + } + } + if stacked { + ext.1 = ext.1.max(p); + ext.0 = ext.0.min(m); + } + } + percent + } + + /// Widens `ext` by the trendlines of the series in `ser` that are not stacked. + fn fold_trendlines(&self, ser: &[&Series], ext: &mut (f64, f64)) { + for s in ser { + if self.series_kind(s).stacked() { + continue; + } + for y in s.trendline_ys(crate::trendline::series_is_scatter(self.series_kind(s))) { + widen(ext, y); + } + } + } + + /// A waterfall's bars as (from, to) on the value axis: each point floats from the running + /// total, a total point stands on zero and sets it. + pub fn waterfall_bars(&self) -> Vec<(f64, f64)> { + let Some(s) = self.series.first() else { return vec![] }; + let mut run = 0.0; + s.values + .iter() + .enumerate() + .map(|(i, v)| { + let v = v.unwrap_or(0.0); + if self.subtotals.contains(&i) { + run = v; + (0.0, v) + } else { + let from = run; + run += v; + (from, run) + } + }) + .collect() + } + + /// A value axis' scale — (min, max, major unit) — with at most `max_intervals` intervals: + /// the bounds and unit the chart fixes, else Office's automatic ones ([`axis_scale`](fn@axis_scale)). + /// `None` when no series uses the axis. + pub fn axis_scale(&self, secondary: bool, max_intervals: usize) -> Option<(f64, f64, f64)> { + let (lo, hi) = self.plotted_extent(secondary)?; + let (fmin, fmax, major) = if secondary { (self.y2_min, self.y2_max, self.y2_major) } else { (self.y_min, self.y_max, self.y_major) }; + let full_percent = !secondary && self.kind.percent(); + Some(axis_scale(lo, hi, fmin, fmax, major, max_intervals, !full_percent)) + } + + /// A logarithmic value axis' scale in exponents — (min, max, unit), so its labels are + /// `base^min`, `base^(min+unit)`… — covering the positive values: whole powers, as PowerPoint + /// and LibreOffice draw them. `None` when the axis isn't logarithmic or nothing is positive. + pub fn log_axis_scale(&self, max_intervals: usize) -> Option<(f64, f64, f64)> { + let b = self.y_log_base.filter(|b| b.is_finite() && *b >= 2.0)?; + let vals = self.series.iter().filter(|s| !self.on_secondary(s)).flat_map(|s| s.values.iter().flatten().copied()); + let (lo, hi) = vals + .filter(|v| v.is_finite() && *v > 0.0) + .fold(None, |r: Option<(f64, f64)>, v| Some(r.map_or((v, v), |(a, z)| (a.min(v), z.max(v)))))?; + let e = |v: f64| v.log(b); + let lo = self.y_min.filter(|v| *v > 0.0).map_or(e(lo).floor(), e); + let hi = self.y_max.filter(|v| *v > 0.0).map_or(e(hi).ceil(), e).max(lo + 1.0); + let unit = ((hi - lo) / max_intervals.max(1) as f64).ceil().max(1.0); + Some((lo, lo + ((hi - lo) / unit).ceil() * unit, unit)) + } + + /// A value axis label in the axis' number format and the chart's language. + pub fn axis_label(&self, secondary: bool, v: f64) -> String { + let code = if secondary { &self.y2_format } else { &self.y_format }; + // A 100 % stacked chart's value axis counts in percent unless the file says otherwise. + let code = match code.as_deref() { + None | Some("General") if !secondary && self.kind.percent() => "0%", + c => c.unwrap_or("General"), + }; + crate::numfmt::format(code, v, self.lang.as_deref()) + } + + /// A data label of series `s`: its number format, else its axis', in the chart's language. + pub fn data_label(&self, s: &Series, v: f64) -> String { + let axis = if self.on_secondary(s) { &self.y2_format } else { &self.y_format }; + let code = s.number_format.as_ref().or(axis.as_ref()); + crate::numfmt::format(code.map_or("General", String::as_str), v, self.lang.as_deref()) + } + + /// Does series `s` show its data labels? The series' own `c:dLbls/c:showVal` wins — an + /// explicit `false` hides them even when the chart-wide setting is on (#868). + pub fn series_data_labels(&self, s: &Series) -> bool { + s.data_labels.unwrap_or(self.data_labels) + } + + fn waterfall_value_range(&self) -> (f64, f64) { + let mut lo: f64 = 0.0; + let mut hi: f64 = 0.0; + for (a, b) in self.waterfall_bars() { + lo = lo.min(a.min(b)); + hi = hi.max(a.max(b)); + } + (lo, hi) + } + + fn stacked_value_range(&self, n: usize) -> (f64, f64) { + // Sums per category, from the values the series hold (an absent one adds nothing). + let (mut pos, mut neg) = (vec![0.0f64; n], vec![0.0f64; n]); + for s in &self.series { + for (i, v) in s.values.iter().enumerate().take(n) { + let v = v.unwrap_or(0.0); + if v >= 0.0 { + pos[i] += v; + } else { + neg[i] += v; + } + } + } + let hi = pos.iter().fold(0.0f64, |a, &p| a.max(p)); + let lo = neg.iter().fold(0.0f64, |a, &m| a.min(m)); + (lo, hi) + } + + /// Value range across series (stacked sums for stacked types). + pub fn value_range(&self) -> (f64, f64) { + if self.kind == ChartType::Waterfall { + return self.waterfall_value_range(); + } + let n = self.categories.len().max(self.series.iter().map(|s| s.values.len()).max().unwrap_or(0)); + let (mut lo, mut hi) = if self.kind.stacked() { + self.stacked_value_range(n) + } else { + let mut l: f64 = 0.0; + let mut h: f64 = 0.0; + for s in &self.series { + for v in s.values.iter().flatten() { + if v.is_finite() { + h = h.max(*v); + l = l.min(*v); + } + } + } + (l, h) + }; + if self.kind.percent() { + return (0.0, 1.0); + } + if !self.kind.stacked() { + for s in &self.series { + for y in s.trendline_ys(crate::trendline::series_is_scatter(self.series_kind(s))) { + hi = hi.max(y); + lo = lo.min(y); + } + } + } + (self.y_min.unwrap_or(lo), self.y_max.unwrap_or(hi)) + } +} + +/// The (min, max, major unit) of an axis over the data range [lo, hi]: `fmin`/`fmax`/`major` +/// where the chart fixes them; otherwise Office's automatic limits, rounded out to the smallest +/// 1-2-5 unit giving at most `max_intervals` intervals: +/// The data range [lo, hi] an axis covers, clamped to ±1e100; (0, 1) without finite values. +/// +/// - **Maximum:** `hi + (hi − lo)/20` (with `headroom`), rounded up to the unit. +/// - **Minimum:** zero when every value is ≥ 0 and `lo` is below 5/6 of `hi`; otherwise +/// `lo − (hi − lo)/20`, rounded down to the unit, so values bunched high get an axis that starts +/// near them. +/// - All-negative values mirror this: the axis ends at zero when `hi` is above 5/6 of `lo`. +/// Mixed signs get headroom at both ends. +fn axis_data_range(lo: f64, hi: f64) -> (f64, f64) { + if lo.is_finite() && hi.is_finite() { (lo.min(hi).clamp(-1e100, 1e100), lo.max(hi).clamp(-1e100, 1e100)) } else { (0.0, 1.0) } +} + +/// Office's automatic (min, max) of the data range [lo, hi]. "How chart axis limits are determined", +/// as Jon Peltier summarises it: 5 % of the data range past the data, and zero when the values don't +/// sit within the top sixth of their largest magnitude. A single value is measured from zero. +fn auto_limits(lo: f64, hi: f64, headroom: bool) -> (f64, f64) { + let flat = hi - lo <= 1e-12 * lo.abs().max(hi.abs()); + let pad = if !headroom { + 0.0 + } else if flat { + hi.abs() / 20.0 + } else { + (hi - lo) / 20.0 + }; + let auto_lo = if lo >= 0.0 && (flat || lo < hi * 5.0 / 6.0) { 0.0 } else { lo - pad }; + let auto_hi = if hi <= 0.0 && (flat || hi > lo * 5.0 / 6.0) { 0.0 } else { hi + pad }; + (auto_lo, auto_hi) +} + +/// The ends of a span too small to show: with one fixed limit on the far side of the data, the +/// automatic end gives way; otherwise the span is one `unit` wide. +fn flat_span_ends(fmin: Option, fmax: Option, lo: f64, hi: f64, unit: f64) -> (f64, f64) { + match (fmin, fmax) { + (None, Some(_)) => (if hi > 0.0 { 0.0 } else { hi - hi.abs().max(unit) }, hi), + (Some(_), None) if lo < 0.0 => (lo, 0.0), + _ => (lo, lo + unit), + } +} + +/// The major step: `major` when it gives at most 1000 intervals, else the smallest 1-2-5 step +/// that fits `n` intervals over [lo, hi]. +fn axis_step(major: Option, lo: f64, hi: f64, n: f64, unit: f64) -> f64 { + match major.filter(|m| *m > 0.0 && (hi - lo) / m <= 1000.0) { + Some(m) => m, + None => { + let mut step = 10f64.powf(((hi - lo) / n).log10().floor()); + // Subnormal spans underflow to a zero (or non-finite) step: fall back to the unit. + if !(step > 0.0 && step.is_finite()) { + step = unit; + } + let mut k = 0; + // 1, 2, 5, 10, 20… × the magnitude until the intervals fit. + while ((hi / step).ceil() - (lo / step).floor()) > n && k < 30 { + step *= if matches!(k % 3, 1) { 2.5 } else { 2.0 }; + k += 1; + } + step + } + } +} + +pub fn axis_scale( + lo: f64, + hi: f64, + fmin: Option, + fmax: Option, + major: Option, + max_intervals: usize, + headroom: bool, +) -> (f64, f64, f64) { + // Values past ±1e100 aren't axis values; clamping keeps the arithmetic finite. + let fin = |v: Option| v.filter(|v| v.is_finite()).map(|v| v.clamp(-1e100, 1e100)); + let (fmin, fmax) = (fin(fmin), fin(fmax)); + let (lo, hi) = axis_data_range(lo, hi); + let (auto_lo, auto_hi) = auto_limits(lo, hi, headroom); + let (mut lo, mut hi) = (fmin.unwrap_or(auto_lo), fmax.unwrap_or(auto_hi)); + let mag = lo.abs().max(hi.abs()); + // Relative to the data's magnitude, so tiny values don't count as flat; all-zero falls back to 1. + let unit = if mag == 0.0 { 1.0 } else { mag }; + if hi - lo <= 1e-12 * mag { + (lo, hi) = flat_span_ends(fmin, fmax, lo, hi, unit); + } + let n = max_intervals.clamp(1, 50) as f64; + let step = axis_step(fin(major), lo, hi, n, unit); + let min = fmin.unwrap_or((lo / step + 1e-9).floor() * step); + let max = fmax.unwrap_or((hi / step - 1e-9).ceil() * step).max(min + step); + (min, max, step) +} + +/// "Nice" axis scale: (min, max, major step) covering [lo, hi]. +pub fn nice_scale(lo: f64, hi: f64) -> (f64, f64, f64) { + let (lo, hi) = if lo.is_finite() && hi.is_finite() { (lo.min(hi), lo.max(hi)) } else { (0.0, 1.0) }; + let data_mag = lo.abs().max(hi.abs()); + let span = if hi - lo <= 1e-12 * data_mag { data_mag } else { hi - lo }; + let span = if span == 0.0 { 1.0 } else { span }; + let raw = span / 5.0; + let mag = 10f64.powf(raw.log10().floor()); + let norm = raw / mag; + let step = if norm <= 1.0 { + 1.0 + } else if norm <= 2.0 { + 2.0 + } else if norm <= 2.5 { + 2.5 + } else if norm <= 5.0 { + 5.0 + } else { + 10.0 + } * mag; + // Subnormal spans underflow to a zero or NaN step: use the span itself. + let step = if step > 0.0 && step.is_finite() { step } else { span }; + let min = (lo / step).floor() * step; + let max = (hi / step).ceil() * step; + let max = if (max - min).abs() <= 1e-12 * data_mag { min + step } else { max }; + (min, max, step) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn tiny_data_keeps_its_own_scale() { + let (min, max, step) = axis_scale(1e-13, 5e-13, None, None, None, 8, true); + assert!(min == 0.0 && (5e-13..1e-11).contains(&max), "{min} {max} {step}"); + assert!(step > 0.0 && step < 1e-11); + let (min, max, step) = nice_scale(1e-13, 5e-13); + assert!(min <= 1e-13 && (5e-13..1e-11).contains(&max) && step < 1e-11, "{min} {max} {step}"); + assert_eq!(axis_scale(0.0, 0.0, None, None, None, 8, true).1, 1.0); + } + + #[test] + fn subnormal_data_gives_a_finite_scale() { + let (min, max, step) = axis_scale(0.0, 5e-324, None, None, None, 8, true); + assert!(min.is_finite() && max.is_finite() && step.is_finite(), "{min} {max} {step}"); + assert!(step > 0.0 && max >= min, "{min} {max} {step}"); + let (min, max, step) = nice_scale(0.0, f64::from_bits(1)); + assert!(min.is_finite() && max.is_finite() && step > 0.0 && step.is_finite() && max > min, "{min} {max} {step}"); + } + + #[test] + fn log_axes_run_over_whole_powers() { + let mut c = Chart::sample(ChartType::Line); + c.series.truncate(1); + c.series[0].values = vec![Some(1.0), Some(10.0), Some(1000.0), Some(50000.0)]; + assert_eq!(c.log_axis_scale(8), None); + c.y_log_base = Some(10.0); + assert_eq!(c.log_axis_scale(8), Some((0.0, 5.0, 1.0))); + assert_eq!(c.log_axis_scale(2), Some((0.0, 6.0, 3.0))); + } + + /// #868: a series' own label setting wins; an unset series takes the chart-wide one. + #[test] + fn series_data_labels_override_the_chart_wide_setting() { + let mut c = Chart::sample(ChartType::Column); + c.data_labels = false; + let mut s = c.series[0].clone(); + assert!(!c.series_data_labels(&s), "unset series follows the chart"); + s.data_labels = Some(true); + assert!(c.series_data_labels(&s), "a series that asks for labels gets them"); + c.data_labels = true; + s.data_labels = Some(false); + assert!(!c.series_data_labels(&s), "an explicit series false hides them anyway"); + } + + #[test] + fn error_bar_ranges() { + let vals = [Some(3.0), Some(5.0), Some(2.0), Some(4.0)]; + let mut e = ErrorBars::default(); + assert_eq!(e.range(&vals, 0, 3.0), Some((2.0, 4.0))); + e.val_type = "percentage".into(); + e.value = 20.0; + assert_eq!(e.range(&vals, 1, 5.0), Some((4.0, 6.0))); + e.bar_type = "plus".into(); + assert_eq!(e.range(&vals, 1, 5.0), Some((5.0, 6.0))); + e = ErrorBars { val_type: "cust".into(), plus: vec![Some(0.5)], minus: vec![Some(1.5)], ..Default::default() }; + assert_eq!(e.range(&vals, 0, 3.0), Some((1.5, 3.5))); + e = ErrorBars { val_type: "stdDev".into(), ..Default::default() }; + let (lo, hi) = e.range(&vals, 0, 3.0).unwrap(); + assert!((lo + hi - 7.0).abs() < 1e-9 && (hi - lo - 2.0 * 1.2909944487358056).abs() < 1e-9); + } + + #[test] + fn nice_scale_covers() { + let (a, b, s) = nice_scale(0.0, 5.1); + assert!(a <= 0.0 && b >= 5.1 && s > 0.0); + let (a, b, _) = nice_scale(f64::NAN, 3.0); + assert!(a.is_finite() && b.is_finite()); + let (a, b, _) = nice_scale(7.0, 7.0); + assert!(b > a); + } + + #[test] + fn automatic_axis_scales_like_powerpoint() { + // Weekly spend up to ~715 000 on a plot with room for 9 intervals: 0–800 000 by 100 000. + assert_eq!(axis_scale(0.0, 715_000.0, None, None, None, 9, true), (0.0, 800_000.0, 100_000.0)); + // Clicks up to 6 600: 0–7 000 by 1 000. + assert_eq!(axis_scale(0.0, 6_600.0, None, None, None, 9, true), (0.0, 7_000.0, 1_000.0)); + // Fewer intervals fit on a short axis. + assert_eq!(axis_scale(0.0, 6_600.0, None, None, None, 4, true), (0.0, 8_000.0, 2_000.0)); + // Negative data gets room below; fixed bounds and unit are kept. + let (min, max, _) = axis_scale(-30.0, 100.0, None, None, None, 8, true); + assert!(min <= -30.0 && max >= 105.0); + assert_eq!(axis_scale(0.0, 715_000.0, Some(100_000.0), Some(750_000.0), Some(50_000.0), 9, true), (100_000.0, 750_000.0, 50_000.0)); + // 100 % stacked stays at 0–1 without headroom. + assert_eq!(axis_scale(0.0, 1.0, None, None, None, 5, false), (0.0, 1.0, 0.2)); + // Nonsense never loops or divides by zero. + for (lo, hi) in [(f64::NAN, 1.0), (5.0, 5.0), (0.0, 0.0), (-1e300, 1e300)] { + let (a, b, s) = axis_scale(lo, hi, None, None, Some(0.0), 0, true); + assert!(a.is_finite() && b.is_finite() && s > 0.0 && b > a, "{lo} {hi}: {a} {b} {s}"); + } + } + + fn column(values: &[f64]) -> Chart { + let series = Series { name: "Sales".into(), values: values.iter().map(|v| Some(*v)).collect(), ..Default::default() }; + Chart { categories: (0..values.len()).map(|i| format!("C{i}")).collect(), series: vec![series], ..Default::default() } + } + + /// The primary axis of a chart with room for ten intervals. + fn scale(c: &Chart) -> (f64, f64, f64) { + c.axis_scale(false, 10).unwrap_or_default() + } + + /// Office's documented automatic limits (#144), with room for ten intervals. + #[test] + fn automatic_axis_matches_office() { + // #144: 42.1 / 18.7 / 6.3 → 0–45 in steps of 5, not 0–50 in steps of 10. + assert_eq!(scale(&column(&[42.1, 18.7, 6.3])), (0.0, 45.0, 5.0)); + // PowerPoint's default chart data (max 5): 0–6 in steps of 1. + let mut c = column(&[4.3, 2.5, 3.5, 4.5]); + c.series.push(Series { values: [2.4, 4.4, 1.8, 2.8].map(Some).to_vec(), ..Default::default() }); + c.series.push(Series { values: [2.0, 2.0, 3.0, 5.0].map(Some).to_vec(), ..Default::default() }); + assert_eq!(scale(&c), (0.0, 6.0, 1.0)); + // A maximum of 100 (or 10) gets 5 % headroom, which rounds up to the next major unit. + assert_eq!(scale(&column(&[100.0, 40.0])), (0.0, 120.0, 20.0)); + assert_eq!(scale(&column(&[10.0, 3.0])), (0.0, 12.0, 2.0)); + // Peltier's example: 2.95 + 2.95/20 ≈ 3.1 rounds up to 3.5. + assert_eq!(scale(&column(&[2.95, 0.0])), (0.0, 3.5, 0.5)); + } + + #[test] + fn automatic_axis_minimum() { + // Below 5/6 of the maximum the axis starts at zero; at or above it, a unit below + // min − (max − min)/20. + assert_eq!(scale(&column(&[59.0, 49.0])).0, 0.0); + assert_eq!(scale(&column(&[59.0, 50.0])), (48.0, 60.0, 2.0)); + assert_eq!(scale(&column(&[85.0, 92.0, 97.0])), (84.0, 98.0, 2.0)); + // All-negative data mirrors it: the axis ends at zero, or near the data. + assert_eq!(scale(&column(&[-42.1, -18.7, -6.3])), (-45.0, 0.0, 5.0)); + assert_eq!(scale(&column(&[-59.0, -49.0])).1, 0.0); + assert_eq!(scale(&column(&[-59.0, -50.0])), (-60.0, -48.0, 2.0)); + // Mixed signs get headroom at both ends. + assert_eq!(scale(&column(&[42.1, -18.7])), (-30.0, 50.0, 10.0)); + // Stacked bars build up from zero, so their axis keeps it. + let mut s = column(&[85.0, 92.0]); + s.kind = ChartType::StackedColumn; + assert_eq!(scale(&s).0, 0.0); + // A combo chart's secondary axis follows the same rule. + let mut c = Chart::sample(ChartType::Combo); + c.series.truncate(2); + c.series[0].values = vec![Some(200_000.0), Some(310_000.0)]; + c.series[1].kind = Some(ChartType::Line); + c.series[1].values = vec![Some(7_000.0), Some(7_400.0)]; + c.series[1].secondary_axis = true; + assert_eq!(c.axis_scale(false, 10), Some((0.0, 350_000.0, 50_000.0))); + assert_eq!(c.axis_scale(true, 10), Some((6_950.0, 7_450.0, 50.0))); + } + + #[test] + fn fixed_axis_limits_are_kept() { + let mut c = column(&[42.1, 18.7, 6.3]); + c.y_max = Some(50.0); + assert_eq!(scale(&c), (0.0, 50.0, 5.0)); + c.y_min = Some(10.0); + c.y_max = None; + assert_eq!(scale(&c), (10.0, 45.0, 5.0)); + // A fixed limit beyond the data: the automatic end gives way to it. + let mut c = column(&[85.0, 92.0, 97.0]); + c.y_max = Some(50.0); + assert_eq!(scale(&c), (0.0, 50.0, 5.0)); + let mut c = column(&[-50.0, -48.0]); + c.y_min = Some(-10.0); + assert_eq!(scale(&c), (-10.0, 0.0, 1.0)); + } + + #[test] + fn percent_axis_is_zero_to_one() { + let mut p = column(&[42.1, 18.7, 6.3]); + p.kind = ChartType::PercentColumn; + assert_eq!(scale(&p), (0.0, 1.0, 0.1)); + assert_eq!(p.axis_scale(false, 5), Some((0.0, 1.0, 0.2))); + } + + #[test] + fn degenerate_axes_are_finite() { + for (a, b) in [(0.0, 0.0), (7.0, 7.0), (-7.0, -7.0), (f64::NAN, 3.0), (f64::INFINITY, 1.0), (-f64::MAX, f64::MAX), (0.0, 1e-320)] { + let (min, max, step) = scale(&column(&[a, b])); + assert!(min.is_finite() && max > min && step > 0.0, "{a} {b}: {min} {max} {step}"); + } + let (min, max, _) = scale(&column(&[7.0, 7.0])); + assert!(min == 0.0 && max > 7.0); + assert_eq!(scale(&column(&[])), (0.0, 1.0, 0.1)); + for (fmin, fmax) in [(Some(5.0), Some(5.0)), (Some(5.0), Some(1.0)), (Some(f64::NAN), Some(f64::INFINITY)), (Some(-1e300), None)] { + let (a, b, s) = axis_scale(0.0, 1.0, fmin, fmax, None, 10, true); + assert!(a.is_finite() && b.is_finite() && b > a && s > 0.0, "{fmin:?} {fmax:?}: {a} {b} {s}"); + } + } + + #[test] + fn general_labels_keep_every_digit_the_data_has() { + // #324: labels were rounded to one decimal ("100000.0" for 99999.99). + for kind in [ChartType::Column, ChartType::Pie, ChartType::Doughnut, ChartType::Line, ChartType::Scatter] { + let c = Chart::sample(kind); + let s = &c.series[0]; + for (v, want) in + [(1220.82, "1220.82"), (99999.99, "99999.99"), (1500.05, "1500.05"), (1500.0, "1500"), (1234.567, "1234.567"), (999.99, "999.99")] + { + assert_eq!(c.data_label(s, v), want, "{kind:?}"); + } + } + // Small ranges keep distinct value-axis ticks (0.123 used to give 0, 0.0, 0.1, 0.1…). + let c = Chart::sample(ChartType::Column); + let ticks: Vec = (0..=5).map(|i| c.axis_label(false, f64::from(i) * 0.025)).collect(); + assert_eq!(ticks, ["0", "0.025", "0.05", "0.075", "0.1", "0.125"]); + } + + #[test] + fn a_forecast_grows_the_value_axis() { + let mut c = Chart::sample(ChartType::Line); + c.series.truncate(1); + c.series[0].values = vec![Some(3.0), Some(5.0), Some(7.0), Some(9.0)]; + let before = c.value_range().1; + c.series[0].trendlines.push(Trendline { forward: Some(1.0), ..Default::default() }); + let after = c.value_range().1; + assert!(after > before && after >= 11.0 - 1e-6, "{before} {after}"); + } + + #[test] + fn axis_labels_use_the_format_and_language() { + let mut c = Chart::sample(ChartType::Column); + c.y_format = Some(r#"_-* #\ ##0\ "₽"_-;\-* #\ ##0\ "₽"_-;_-* "-"??\ "₽"_-;_-@_-"#.into()); + c.lang = Some("ru-RU".into()); + assert_eq!(c.axis_label(false, 800_000.0), "800\u{a0}000 ₽"); + assert_eq!(c.axis_label(false, 0.0), "- ₽"); + // No format: General. + assert_eq!(c.axis_label(true, 2.5), "2,5"); + c.lang = None; + assert_eq!(c.axis_label(true, 2.5), "2.5"); + // Data labels: the series' format, else the axis'. + c.lang = Some("ru-RU".into()); + c.series[0].number_format = Some(r#"#,##0\ "₽""#.into()); + assert_eq!(c.data_label(&c.series[0], 516_600_000.0), "516\u{a0}600\u{a0}000 ₽"); + assert_eq!(c.data_label(&c.series[1], 0.0), "- ₽"); + } + + #[test] + fn full_percent_axes_count_in_percent() { + let mut c = Chart::sample(ChartType::PercentColumn); + assert_eq!(c.axis_label(false, 0.5), "50%"); + c.y_format = Some("0.0%".into()); + assert_eq!(c.axis_label(false, 0.5), "50.0%"); + let c = Chart::sample(ChartType::Column); + assert_eq!(c.axis_label(false, 0.5), "0.5"); + } + + #[test] + fn combo_axis_ranges() { + // Spend in columns on the primary axis, clicks as a line on the secondary one. + let mut c = Chart::sample(ChartType::Combo); + c.series.truncate(2); + c.series[0].kind = Some(ChartType::Column); + c.series[0].values = vec![Some(200_000.0), Some(310_000.0)]; + c.series[1].kind = Some(ChartType::Line); + c.series[1].values = vec![Some(6_000.0), Some(7_400.0)]; + c.series[1].secondary_axis = true; + assert_eq!(c.axis_range(false), Some((0.0, 310_000.0))); + assert_eq!(c.axis_range(true), Some((0.0, 7_400.0))); + assert_eq!(c.series_kind(&c.series[1]), ChartType::Line); + // Without a secondary series there is no secondary axis; a non-combo chart never has one. + c.series[1].secondary_axis = false; + assert_eq!(c.axis_range(true), None); + let col = Chart::sample(ChartType::Column); + assert_eq!(col.axis_range(false), Some(col.value_range())); + assert_eq!(col.axis_range(true), None); + assert!(!col.on_secondary(&col.series[0])); + } + + #[test] + fn sample_ranges() { + let c = Chart::sample(ChartType::StackedColumn); + let (_, hi) = c.value_range(); + assert!(hi > 10.0); + assert_eq!(Chart::sample(ChartType::Pie).series.len(), 1); + } +} + +/// One histogram bin: `[lo, hi]` (the first bin is closed on both sides, later ones open on the +/// left, as Excel bins) and how many observations fall in it. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Bin { + pub lo: f64, + pub hi: f64, + pub count: usize, +} + +/// Bins a histogram has at most (a hostile bin size cannot make millions). +pub const MAX_BINS: usize = 1000; + +/// A box and whisker summary of one group: quartiles by Excel's exclusive method, whiskers at +/// the furthest points within 1.5 IQR of the box, and the points beyond them. +#[derive(Clone, Debug, PartialEq)] +pub struct BoxStats { + pub low: f64, + pub q1: f64, + pub median: f64, + pub q3: f64, + pub high: f64, + pub mean: f64, + pub outliers: Vec, +} + +impl Chart { + /// The first series' finite observations. + fn observations(&self) -> Vec { + self.series.first().map(|s| s.values.iter().flatten().copied().filter(|v| v.is_finite()).collect()).unwrap_or_default() + } + + /// The histogram's bins over the first series: by `bin_size`, else `bin_count`, else + /// Scott's rule (3.49·σ·n^(-1/3)). Empty without observations. + pub fn histogram_bins(&self) -> Vec { + let mut v = self.observations(); + if v.is_empty() { + return vec![]; + } + v.sort_by(f64::total_cmp); + let (lo, hi) = (v[0], v[v.len() - 1]); + let span = hi - lo; + let n = v.len() as f64; + let width = if let Some(w) = self.bin_size.filter(|w| w.is_finite() && *w > 0.0) { + w + } else if let Some(k) = self.bin_count.filter(|k| *k > 0) { + span / f64::from(k) + } else { + let mean = v.iter().sum::() / n; + let sd = (v.iter().map(|x| (x - mean).powi(2)).sum::() / (n - 1.0).max(1.0)).sqrt(); + 3.49 * sd / n.cbrt() + }; + let fixed = self.bin_count.filter(|k| *k > 0).filter(|_| self.bin_size.filter(|w| w.is_finite() && *w > 0.0).is_none()); + let count = if span <= 0.0 || !width.is_finite() || width <= 0.0 { + 1 + } else if let Some(k) = fixed { + (k as usize).clamp(1, MAX_BINS) + } else { + ((span / width).ceil() as usize).clamp(1, MAX_BINS) + }; + let width = if count == 1 && (span <= 0.0 || !width.is_finite() || width <= 0.0) { span.max(1.0) } else { width.max(span / MAX_BINS as f64) }; + let mut bins: Vec = (0..count).map(|i| Bin { lo: lo + width * i as f64, hi: lo + width * (i + 1) as f64, count: 0 }).collect(); + for x in v { + // (lo, hi] bins, the first one also holding the minimum. + let mut i = (((x - lo) / width).ceil() as usize).saturating_sub(1).min(count - 1); + // The division can round across an edge: settle on the bin whose stored bounds hold x. + while i > 0 && x <= bins[i - 1].hi { + i -= 1; + } + while i + 1 < count && x > bins[i].hi { + i += 1; + } + if let Some(b) = bins.get_mut(i) { + b.count += 1; + } + } + bins + } + + /// A histogram (or Pareto) as the chart that draws it: a column chart of the bin counts with + /// touching columns, labelled by bin range. A Pareto sorts the bins by count and adds the + /// cumulative percentage as a line on a 0–100 % secondary axis. + pub fn histogram_view(&self) -> Chart { + let mut bins = self.histogram_bins(); + let pareto = self.kind == ChartType::Pareto; + if pareto { + bins.sort_by_key(|b| std::cmp::Reverse(b.count)); + } + let fmt = |v: f64| { + let r = (v * 100.0).round() / 100.0; + if (r - r.round()).abs() < 1e-9 { format!("{}", r.round() as i64) } else { format!("{r}") } + }; + let first_lo = bins.iter().map(|b| b.lo).fold(f64::INFINITY, f64::min); + let mut v = Chart { + kind: if pareto { ChartType::Combo } else { ChartType::Column }, + categories: bins + .iter() + .map(|b| if b.lo <= first_lo { format!("[{}, {}]", fmt(b.lo), fmt(b.hi)) } else { format!("({}, {}]", fmt(b.lo), fmt(b.hi)) }) + .collect(), + series: vec![], + gap_width: 0.0, + raw: None, + verbatim: false, + subtotals: vec![], + ..self.clone() + }; + let name = self.series.first().map(|s| s.name.clone()).unwrap_or_default(); + let base = self.series.first().cloned().unwrap_or_default(); + v.series.push(Series { + name, + values: bins.iter().map(|b| Some(b.count as f64)).collect(), + kind: pareto.then_some(ChartType::Column), + number_format: None, + ..base + }); + if pareto { + let total = bins.iter().map(|b| b.count).sum::().max(1) as f64; + let mut run = 0.0; + v.series.push(Series { + name: "Cumulative %".into(), + values: bins + .iter() + .map(|b| { + run += b.count as f64; + Some(run / total) + }) + .collect(), + kind: Some(ChartType::Line), + secondary_axis: true, + ..Default::default() + }); + v.y2_min = Some(0.0); + v.y2_max = Some(1.0); + v.y2_major = Some(0.2); + v.y2_format = Some("0%".into()); + } + v.vary_colors = false; + v + } + + /// The box chart's groups (distinct category labels in order of appearance; one unnamed + /// group without categories) and, per series, each group's summary (None when it has no + /// points). + /// + /// Each value is put in its group's bucket once (groups are found by name in a map), and the + /// series x groups result is held to [`MAX_BOX_CELLS`]: groups past it are left out. + pub fn box_groups(&self) -> (Vec, Vec>>) { + let max_groups = (MAX_BOX_CELLS / self.series.len().max(1)).max(1); + let mut groups: Vec = Vec::new(); + let mut index: std::collections::HashMap<&str, usize> = std::collections::HashMap::new(); + for c in &self.categories { + if !index.contains_key(c.as_str()) && groups.len() < max_groups { + index.insert(c, groups.len()); + groups.push(c.clone()); + } + } + if groups.is_empty() { + groups.push(String::new()); + index.insert("", 0); + } + // A value past the categories belongs to the unnamed group, when there is one. + let past_end = index.get("").copied(); + let stats = self + .series + .iter() + .map(|s| { + let mut buckets: Vec> = vec![Vec::new(); groups.len()]; + for (i, v) in s.values.iter().enumerate() { + let Some(v) = v.filter(|v| v.is_finite()) else { continue }; + let g = match self.categories.get(i) { + Some(c) => index.get(c.as_str()).copied(), + None => past_end, + }; + if let Some(b) = g.and_then(|g| buckets.get_mut(g)) { + b.push(v); + } + } + buckets.into_iter().map(|pts| box_stats_by(pts, self.inclusive_quartiles)).collect() + }) + .collect(); + (groups, stats) + } +} + +/// Most series x group summaries [`Chart::box_groups`] makes. +pub const MAX_BOX_CELLS: usize = 1_000_000; + +/// Widens the (min, max) extent `ext` to include `v`. +fn widen(ext: &mut (f64, f64), v: f64) { + ext.0 = ext.0.min(v); + ext.1 = ext.1.max(v); +} + +/// Excel's QUARTILE.EXC position `p·(n+1)` (1-based) on sorted data, clamped to the ends. +fn quantile_exc(v: &[f64], p: f64) -> f64 { + let n = v.len(); + let pos = (p * (n as f64 + 1.0)).clamp(1.0, n as f64); + let i = pos.floor() as usize; + let frac = pos - i as f64; + let a = v[i.saturating_sub(1).min(n - 1)]; + let b = v[i.min(n - 1)]; + a + (b - a) * frac +} + +/// Excel's QUARTILE.INC position `p·(n-1)` (0-based) on sorted data. +fn quantile_inc(v: &[f64], p: f64) -> f64 { + let n = v.len(); + let pos = (p * (n as f64 - 1.0)).clamp(0.0, (n - 1) as f64); + let i = pos.floor() as usize; + let a = v[i.min(n - 1)]; + let b = v[(i + 1).min(n - 1)]; + a + (b - a) * (pos - i as f64) +} + +/// The summary of one group's points, with exclusive quartiles. +pub fn box_stats(pts: Vec) -> Option { + box_stats_by(pts, false) +} + +/// The summary of one group's points; `inclusive` picks QUARTILE.INC over QUARTILE.EXC. +pub fn box_stats_by(mut pts: Vec, inclusive: bool) -> Option { + if pts.is_empty() { + return None; + } + pts.sort_by(f64::total_cmp); + let q = |p: f64| if inclusive { quantile_inc(&pts, p) } else { quantile_exc(&pts, p) }; + let (q1, median, q3) = (q(0.25), q(0.5), q(0.75)); + let iqr = q3 - q1; + let (lo_fence, hi_fence) = (q1 - 1.5 * iqr, q3 + 1.5 * iqr); + let inside: Vec = pts.iter().copied().filter(|v| *v >= lo_fence && *v <= hi_fence).collect(); + let low = inside.first().copied().unwrap_or(q1).min(q1); + let high = inside.last().copied().unwrap_or(q3).max(q3); + let mean = pts.iter().sum::() / pts.len() as f64; + let outliers = pts.iter().copied().filter(|v| *v < lo_fence || *v > hi_fence).collect(); + Some(BoxStats { low, q1, median, q3, high, mean, outliers }) +} + +/// A node of a treemap or sunburst hierarchy: its label, the total of the leaves under it, and +/// its children (none for a leaf, which names its category index). +#[derive(Clone, Debug, PartialEq)] +pub struct TreeNode { + pub name: String, + pub value: f64, + pub leaf: Option, + pub children: Vec, +} + +/// Levels of hierarchy a chart may have (a hostile file cannot nest deeper). +pub const MAX_LEVELS: usize = 16; + +impl Chart { + /// The first series as a hierarchy: top-level nodes, each holding the consecutive + /// categories that share its label, down to the leaves. Without outer levels every + /// category is a top-level leaf. Negative and missing values count as zero. + pub fn category_tree(&self) -> Vec { + let Some(s) = self.series.first() else { return vec![] }; + let n = self.categories.len().max(s.values.len()); + let levels: Vec<&Vec> = self.category_levels.iter().take(MAX_LEVELS).collect(); + // Each leaf's path from the top level down, blanks filled from the row above. + // Labels are shared, not copied: an outer label given once stands for every leaf under it. + use std::rc::Rc; + let empty: Rc = Rc::from(""); + let mut last: Vec> = vec![empty.clone(); levels.len()]; + let paths: Vec>> = (0..n) + .map(|i| { + let mut path = Vec::with_capacity(levels.len() + 1); + for (d, lv) in levels.iter().enumerate().rev() { + let l = lv.get(i).map(|x| x.trim()).unwrap_or(""); + if !l.is_empty() { + last[d] = Rc::from(l); + // A new outer label starts fresh inner groups. + for inner in last.iter_mut().take(d) { + *inner = empty.clone(); + } + } + path.push(last[d].clone()); + } + path.push(self.categories.get(i).map_or_else(|| empty.clone(), |c| Rc::from(c.as_str()))); + path + }) + .collect(); + let value = |i: usize| s.values.get(i).copied().flatten().filter(|v| v.is_finite()).unwrap_or(0.0).max(0.0); + fn build(rows: &[(usize, &[std::rc::Rc])], value: &dyn Fn(usize) -> f64) -> Vec { + let mut out: Vec = Vec::new(); + let mut i = 0; + while i < rows.len() { + let (idx, path) = rows[i]; + if path.len() <= 1 { + out.push(TreeNode { + name: path.first().map(|p| p.to_string()).unwrap_or_default(), + value: value(idx), + leaf: Some(idx), + children: vec![], + }); + i += 1; + continue; + } + let head = &path[0]; + let mut j = i; + while j < rows.len() && rows[j].1.len() > 1 && &rows[j].1[0] == head { + j += 1; + } + let sub: Vec<(usize, &[std::rc::Rc])> = rows[i..j].iter().map(|(k, p)| (*k, &p[1..])).collect(); + let children = build(&sub, value); + out.push(TreeNode { name: head.to_string(), value: children.iter().map(|c| c.value).sum(), leaf: None, children }); + i = j; + } + out + } + let rows: Vec<(usize, &[Rc])> = paths.iter().enumerate().map(|(i, p)| (i, p.as_slice())).collect(); + build(&rows, &value) + } +} + +#[cfg(test)] +mod chartex_tests { + use super::*; + + fn obs(v: &[f64]) -> Chart { + Chart { + kind: ChartType::Histogram, + series: vec![Series { values: v.iter().map(|x| Some(*x)).collect(), ..Default::default() }], + ..Default::default() + } + } + + #[test] + fn histogram_bins_by_count_size_and_scott() { + let mut c = obs(&[1.0, 2.0, 2.0, 3.0, 4.0, 5.0, 9.0]); + c.bin_count = Some(4); + let b = c.histogram_bins(); + assert_eq!(b.len(), 4); + assert_eq!((b[0].lo, b[0].hi), (1.0, 3.0)); + // (lo, hi] bins with the minimum in the first: 1, 2, 2, 3 | 4, 5 | - | 9. + assert_eq!(b.iter().map(|b| b.count).collect::>(), vec![4, 2, 0, 1]); + c.bin_count = None; + c.bin_size = Some(5.0); + assert_eq!(c.histogram_bins().iter().map(|b| b.count).collect::>(), vec![6, 1]); + c.bin_size = None; + let auto = c.histogram_bins(); + assert!(!auto.is_empty() && auto.iter().map(|b| b.count).sum::() == 7); + // A tiny bin size is capped; one value gives one bin. + c.bin_size = Some(1e-12); + assert!(c.histogram_bins().len() <= MAX_BINS); + assert_eq!(obs(&[3.0]).histogram_bins(), vec![Bin { lo: 3.0, hi: 4.0, count: 1 }]); + assert!(obs(&[]).histogram_bins().is_empty()); + } + + #[test] + fn histogram_fixed_bin_count_is_exact_despite_float_rounding() { + let mut c = obs(&[0.0, 1.0, 2.1]); + c.bin_count = Some(7); + assert_eq!(c.histogram_bins().len(), 7); + } + + #[test] + fn histogram_value_on_a_bin_edge_counts_in_that_bin() { + let mut c = obs(&[0.0, 2.1, 3.0]); + c.bin_size = Some(0.3); + let bins = c.histogram_bins(); + let own = bins.iter().find(|b| b.lo < 2.1 && 2.1 <= b.hi).expect("a bin holds 2.1"); + assert_eq!(own.count, 1, "2.1 must be counted in ({}, {}]", own.lo, own.hi); + assert_eq!(bins.iter().map(|b| b.count).sum::(), 3); + } + + #[test] + fn pareto_view_sorts_bins_and_adds_the_cumulative_line() { + let mut c = obs(&[1.0, 5.0, 5.0, 5.0, 9.0, 9.0]); + c.kind = ChartType::Pareto; + c.bin_count = Some(3); + let v = c.histogram_view(); + assert_eq!(v.kind, ChartType::Combo); + assert_eq!(v.series[0].values, vec![Some(3.0), Some(2.0), Some(1.0)]); + let cum = &v.series[1]; + assert!(cum.secondary_axis && cum.kind == Some(ChartType::Line)); + assert_eq!(cum.values.last(), Some(&Some(1.0))); + assert_eq!(v.categories[0], "(3.67, 6.33]"); + } + + #[test] + fn box_stats_use_exclusive_quartiles_and_fence_outliers() { + let b = box_stats(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0]).unwrap(); + // QUARTILE.EXC on 8 points: positions 2.25, 4.5, 6.75. + assert_eq!((b.q1, b.median, b.q3), (2.25, 4.5, 6.75)); + assert_eq!(b.outliers, vec![100.0]); + assert_eq!((b.low, b.high), (1.0, 7.0)); + assert!(box_stats(vec![]).is_none()); + let one = box_stats(vec![5.0]).unwrap(); + assert_eq!((one.low, one.q1, one.median, one.q3, one.high), (5.0, 5.0, 5.0, 5.0, 5.0)); + } + + #[test] + fn inclusive_quartiles_follow_quartile_inc() { + let b = box_stats_by(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 100.0], true).unwrap(); + // QUARTILE.INC on 8 points: positions 1.75, 3.5, 5.25 (0-based). + assert_eq!((b.q1, b.median, b.q3), (2.75, 4.5, 6.25)); + } + + #[test] + fn category_tree_groups_runs_and_fills_blank_parents() { + let mut c = Chart::sample(ChartType::Treemap); + let t = c.category_tree(); + assert_eq!(t.iter().map(|n| n.name.as_str()).collect::>(), ["Q1", "Q2", "Q3"]); + assert_eq!(t[0].children.len(), 3); + assert_eq!(t[0].children[1].leaf, Some(1)); + assert!((t[0].value - (4.2 + 2.8 + 3.6)).abs() < 1e-9); + // Blank parents continue the label above; two outer levels nest. + c.category_levels = vec![ + vec!["a".into(), "".into(), "b".into(), "".into(), "c".into(), "".into(), "".into(), "".into()], + vec!["H1".into(), "".into(), "".into(), "".into(), "H2".into(), "".into(), "".into(), "".into()], + ]; + let t = c.category_tree(); + assert_eq!(t.len(), 2); + assert_eq!(t[0].children.iter().map(|n| n.name.as_str()).collect::>(), ["a", "b"]); + assert_eq!(t[1].children[0].children.len(), 4); + // No levels: every category is a leaf. + c.category_levels.clear(); + assert!(c.category_tree().iter().all(|n| n.leaf.is_some())); + } + + #[test] + fn large_box_hierarchy_and_stacked_charts_take_linear_work() { + // 50,000 distinct categories in one series: grouped through a map, one pass. + let n = 50_000; + let mut c = Chart::sample(ChartType::BoxWhisker); + c.series.truncate(1); + c.categories = (0..n).map(|i| format!("g{i}")).collect(); + c.series[0].values = (0..n).map(|i| Some(i as f64)).collect(); + let (groups, stats) = c.box_groups(); + assert_eq!((groups.len(), stats[0].len()), (n, n)); + assert!(stats[0].iter().all(Option::is_some)); + // 2,000 series cap the groups so the result stays within MAX_BOX_CELLS. + c.series = vec![c.series[0].clone(); 2000]; + let (groups, stats) = c.box_groups(); + assert_eq!(groups.len(), MAX_BOX_CELLS / 2000); + assert!(stats.iter().all(|s| s.len() == groups.len())); + // One long outer label over 50,000 leaves is shared, not copied per leaf. + let mut t = Chart::sample(ChartType::Treemap); + t.series.truncate(1); + t.categories = (0..n).map(|i| format!("c{i}")).collect(); + t.series[0].values = (0..n).map(|_| Some(1.0)).collect(); + let mut outer = vec![String::new(); n]; + outer[0] = "o".repeat(1 << 16); + t.category_levels = vec![outer]; + let tree = t.category_tree(); + assert_eq!(tree.len(), 1); + assert_eq!(tree[0].children.len(), n); + // Stacked sums over many sparse series. + let mut st = Chart::sample(ChartType::StackedBar); + st.categories = (0..n).map(|i| i.to_string()).collect(); + st.series = (0..2000).map(|k| Series { values: vec![Some(1.0); if k == 0 { n } else { 1 }], ..Default::default() }).collect(); + assert!(st.value_range().1 >= 2000.0, "{:?}", st.value_range()); + } + + #[test] + fn box_groups_follow_category_labels() { + let c = Chart::sample(ChartType::BoxWhisker); + let (groups, stats) = c.box_groups(); + assert_eq!(groups, vec!["Group A".to_string(), "Group B".to_string()]); + assert_eq!(stats.len(), 2); + assert!(stats.iter().all(|s| s.len() == 2 && s.iter().all(Option::is_some))); + } +} diff --git a/crates/drawingml/src/diagram.rs b/crates/drawingml/src/diagram.rs new file mode 100644 index 000000000..30db71e88 --- /dev/null +++ b/crates/drawingml/src/diagram.rs @@ -0,0 +1,53 @@ +//! SmartArt (`dgm:`): a diagram's layout and its outline of nodes. + +use serde::{Deserialize, Serialize}; + +use crate::MediaId; + +/// A SmartArt diagram's data: its layout and its outline of nodes (the Text Pane). +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Diagram { + /// The layout: list, blockList, hBulletList, process, chevron, verticalProcess, stepUp, cycle, + /// blockCycle, radial, hierarchy, pyramid, invertedPyramid, matrix or titledMatrix. + pub kind: String, + pub nodes: Vec, + /// SmartArt Style: `flat` (default), `subtle`, `intense` or `outline`. + #[serde(skip_serializing_if = "String::is_empty")] + pub style: String, + /// Change Colors: `colorful` (default: each shape its own accent) or `accent1`…`accent6`. + #[serde(skip_serializing_if = "String::is_empty")] + pub colors: String, + /// The group's shapes are the file's drawing, not generated from `nodes`: text typed into them + /// isn't mapped back to nodes (the first outline edit regenerates them). + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub imported: bool, + /// PowerPoint's layout id (`layoutDef@uniqueId`) the diagram came with. + #[serde(skip_serializing_if = "String::is_empty")] + pub layout_id: String, + /// The file's own layout, quick style and colour definitions, written back unchanged while the + /// layout isn't switched. + #[serde(skip_serializing_if = "Option::is_none")] + pub layout_xml: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub style_xml: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub colors_xml: Option, +} + +/// One line of the Text Pane: its text and outline level (0 = top level). +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct DiagramNode { + pub text: String, + pub level: u8, + /// The picture shown in the node's picture placeholder (picture layouts). + #[serde(skip_serializing_if = "Option::is_none")] + pub picture: Option, + /// An assistant (organisation charts): drawn beside its parent, off the row of its siblings. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub assistant: bool, + /// The job title under the name (Name and Title Organization Chart). + #[serde(skip_serializing_if = "String::is_empty")] + pub title: String, +} diff --git a/crates/drawingml/src/lib.rs b/crates/drawingml/src/lib.rs index 0f847c990..47d6ee107 100644 --- a/crates/drawingml/src/lib.rs +++ b/crates/drawingml/src/lib.rs @@ -9,19 +9,57 @@ //! - [`preset`]: outlines of the preset shapes (`a:prstGeom`), by their file names. //! - [`custgeom`]: custom geometry (`a:custGeom`), with the shape guide formulas. //! - [`warp`]: preset text warps (`a:prstTxWarp`). +//! - The model, without readers and writers yet: [`style`] (colour references, fills, lines, +//! effects), [`text`] (text bodies, paragraphs, runs), [`table`], [`chart`] with [`numfmt`] +//! (number format codes) and [`diagram`] (SmartArt outlines). Media are named by [`MediaId`]; +//! the bytes stay with the caller. //! //! Nothing here opens a package: readers take a [`craft_xml::El`] and writers a //! [`craft_xml::Writer`], so the same code serves a slide, a document or a worksheet drawing. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod chart; pub mod color; pub mod custgeom; +pub mod diagram; +pub mod numfmt; pub mod preset; +pub mod style; +pub mod table; +pub mod text; +mod trendline; pub mod warp; use kurbo::{Affine, Point, Rect, Vec2}; use serde::{Deserialize, Serialize}; +pub(crate) fn yes() -> bool { + true +} + +macro_rules! id_type { + ($(#[$doc:meta])* $name:ident) => { + $(#[$doc])* + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] + #[serde(transparent)] + pub struct $name(pub u32); + impl std::fmt::Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } + } + }; +} +id_type!( + /// A picture, sound or other embedded file, by the id its owner gave it. The bytes stay with + /// the owner (a package part in a file). + MediaId +); +id_type!( + /// The target of a link to a slide. Only presentations have them. + SlideId +); + /// EMU per point. pub const EMU_PER_PT: f64 = 12_700.0; /// EMU per inch. diff --git a/crates/drawingml/src/numfmt.rs b/crates/drawingml/src/numfmt.rs new file mode 100644 index 000000000..b5db9667c --- /dev/null +++ b/crates/drawingml/src/numfmt.rs @@ -0,0 +1,415 @@ +//! Number formats: Excel format codes (`#,##0`, `0.0%`, `_-* # ##0 "₽"_-;…`, `General`) as chart +//! axes and data labels show them, in the chart's language (digit grouping and decimal mark). + +/// Digit grouping and decimal mark for a language tag (`ru-RU`, `de-DE`, `en-US`). +fn marks(lang: Option<&str>) -> (char, char) { + let l = lang.unwrap_or("en").split(['-', '_']).next().unwrap_or("en").to_ascii_lowercase(); + match l.as_str() { + // Space-grouped, comma decimal. + "ru" | "uk" | "be" | "kk" | "fr" | "pl" | "cs" | "sk" | "sv" | "fi" | "nb" | "nn" | "no" | "bg" | "lt" | "lv" | "et" | "hu" | "uz" | "hy" + | "ka" | "az" => ('\u{a0}', ','), + // Dot-grouped, comma decimal. + "de" | "es" | "it" | "pt" | "nl" | "da" | "tr" | "el" | "ro" | "id" | "sr" | "hr" | "sl" | "vi" => ('.', ','), + _ => (',', '.'), + } +} + +#[derive(Debug, PartialEq)] +enum Tok { + /// Text shown as is. + Lit(String), + /// A digit placeholder: `0`, `#` or `?`. + Digit(char), + Dot, + Comma, + Percent, +} + +/// A section's tokens (`[$₽-419]` gives its symbol; colours, conditions and locales drop out). +fn tokens(section: &str) -> Vec { + let mut out = vec![]; + let mut it = section.chars().peekable(); + while let Some(c) = it.next() { + match c { + '"' => { + let mut s = String::new(); + for d in it.by_ref() { + if d == '"' { + break; + } + s.push(d); + } + out.push(Tok::Lit(s)); + } + '\\' => { + if let Some(d) = it.next() { + out.push(Tok::Lit(d.to_string())); + } + } + // Padding the width of the next character: a space. + '_' => { + it.next(); + out.push(Tok::Lit(" ".into())); + } + // Repeat-to-fill: nothing on an axis. + '*' => { + it.next(); + } + '[' => { + let mut s = String::new(); + for d in it.by_ref() { + if d == ']' { + break; + } + s.push(d); + } + if let Some(sym) = s.strip_prefix('$') { + let sym = sym.split('-').next().unwrap_or(""); + if !sym.is_empty() { + out.push(Tok::Lit(sym.to_string())); + } + } + } + '0' | '#' | '?' => out.push(Tok::Digit(c)), + '.' => out.push(Tok::Dot), + ',' => out.push(Tok::Comma), + '%' => out.push(Tok::Percent), + '@' => {} + _ => out.push(Tok::Lit(c.to_string())), + } + } + out +} + +/// The format's sections split at `;` (outside quotes, brackets and escapes). +fn sections(code: &str) -> Vec<&str> { + let mut out = vec![]; + let (mut start, mut quoted, mut bracket, mut escaped) = (0, false, false, false); + for (i, c) in code.char_indices() { + if escaped { + escaped = false; + continue; + } + match c { + '\\' if !quoted => escaped = true, + '"' => quoted = !quoted, + '[' if !quoted => bracket = true, + ']' if !quoted => bracket = false, + ';' if !quoted && !bracket => { + out.push(code.get(start..i).unwrap_or("")); + start = i + 1; + } + _ => {} + } + } + out.push(code.get(start..).unwrap_or("")); + out +} + +/// `v` in the General format: up to 10 significant digits, no grouping. +fn general(v: f64, dec: char) -> String { + if v == 0.0 { + return "0".into(); + } + let a = v.abs(); + let s = if !(1e-9..1e11).contains(&a) { + let s = format!("{v:.5E}"); + match s.split_once('E') { + Some((m, e)) => { + let m = if m.contains('.') { m.trim_end_matches('0').trim_end_matches('.') } else { m }; + let e: i32 = e.parse().unwrap_or(0); + format!("{m}E{}{:02}", if e < 0 { '-' } else { '+' }, e.abs()) + } + None => s, + } + } else { + let digits = (10 - (a.log10().floor() as i32 + 1)).clamp(0, 10) as usize; + let s = format!("{v:.digits$}"); + if s.contains('.') { s.trim_end_matches('0').trim_end_matches('.').to_string() } else { s } + }; + s.replace('.', &dec.to_string()) +} + +/// Group an integer's digits in threes with `sep`. +fn group(digits: &str, sep: char) -> String { + let n = digits.chars().count(); + let mut out = String::new(); + for (i, d) in digits.chars().enumerate() { + if i > 0 && (n - i).is_multiple_of(3) { + out.push(sep); + } + out.push(d); + } + out +} + +/// Longest number format applied, as in Excel (255 characters); a longer one formats as +/// General, so a chart cannot make every label expand a huge format. +pub const MAX_CODE_LEN: usize = 255; + +/// `v` formatted with the Excel number format `code` in language `lang` (axis labels: the text +/// section of a format doesn't apply). +pub fn format(code: &str, v: f64, lang: Option<&str>) -> String { + let code = if code.len() > MAX_CODE_LEN * 4 || code.chars().count() > MAX_CODE_LEN { "General" } else { code }; + let (sep, dec) = marks(lang); + if !v.is_finite() { + return String::new(); + } + let secs = sections(code); + // The section for the value's sign; a negative or zero section shows the value unsigned. + let (sec, v, minus) = match (secs.as_slice(), v) { + ([_, neg, ..], v) if v < 0.0 => (*neg, -v, false), + ([_, _, zero, ..], v) if v == 0.0 => (*zero, v, false), + ([pos, ..], v) => (*pos, v.abs(), v < 0.0), + ([], v) => ("General", v.abs(), v < 0.0), + }; + if sec.trim().eq_ignore_ascii_case("general") || (secs.len() == 1 && sec.trim().is_empty()) { + let s = general(v, dec); + return if minus { format!("-{s}") } else { s }; + } + let toks = tokens(sec); + let first = toks.iter().position(|t| matches!(t, Tok::Digit(_))); + let last = toks.iter().rposition(|t| matches!(t, Tok::Digit(_))); + let (Some(first), Some(last)) = (first, last) else { + // No digits: just the section's text ("-" for zero, a label). + let s: String = toks.iter().filter_map(|t| if let Tok::Lit(s) = t { Some(s.as_str()) } else { None }).collect(); + return s.trim().to_string(); + }; + let out = sci(&toks, v, sep, dec).unwrap_or_else(|| fixed(&toks, first, last, v, sep, dec)); + let out = out.trim(); + if minus && !out.is_empty() { format!("-{out}") } else { out.to_string() } +} + +/// The text of the literal and percent tokens in `ts`. +fn lits(ts: &[Tok]) -> String { + ts.iter() + .filter_map(|t| match t { + Tok::Lit(s) => Some(s.clone()), + Tok::Percent => Some("%".into()), + _ => None, + }) + .collect() +} + +/// Scientific notation (`0.00E+00`, `##0.0E-0`): the mantissa takes the placeholders before the `E`, +/// the exponent's sign and minimum width come from what follows it; with several integer +/// placeholders the exponent is a multiple of their count (engineering notation). `None` if the +/// section has no exponent part. +fn sci(toks: &[Tok], v: f64, sep: char, dec: char) -> Option { + let e = toks.iter().position(|t| matches!(t, Tok::Lit(s) if s.as_str() == "E" || s.as_str() == "e"))?; + let e_char = if let Some(Tok::Lit(s)) = toks.get(e) { s.as_str() } else { "E" }; + let plus = match toks.get(e + 1)? { + Tok::Lit(s) if s.as_str() == "+" => true, + Tok::Lit(s) if s.as_str() == "-" => false, + _ => return None, + }; + let mant = toks.get(..e)?; + let mfirst = mant.iter().position(|t| matches!(t, Tok::Digit(_)))?; + let mlast = mant.iter().rposition(|t| matches!(t, Tok::Digit(_)))?; + let exp_toks = toks.get(e + 2..)?; + let exp_len = exp_toks.iter().take_while(|t| matches!(t, Tok::Digit(_))).count(); + if exp_len == 0 { + return None; + } + let width = exp_toks.iter().take(exp_len).filter(|t| **t == Tok::Digit('0')).count(); + let rest = exp_toks.get(exp_len..).unwrap_or(&[]); + let mant_frac = mant.iter().skip_while(|t| **t != Tok::Dot).filter(|t| matches!(t, Tok::Digit(_))).count(); + let (mut m, mut exp) = (0.0, 0i32); + if v != 0.0 { + exp = v.log10().floor() as i32; + m = if exp < -300 { + // 10^exp underflows for subnormals; scale in two steps. + v * 1e300 / 10f64.powi(exp + 300) + } else { + v / 10f64.powi(exp) + }; + if !m.is_finite() { + m = 0.0; + exp = 0; + } + if m >= 10.0 { + m /= 10.0; + exp += 1; + } else if m < 1.0 { + m *= 10.0; + exp -= 1; + } + // Rounding the mantissa to its digits can reach 10 (9.999 as `0.00`). + if format!("{m:.mant_frac$}").starts_with("10") { + m /= 10.0; + exp += 1; + } + } + let step = mant.iter().take_while(|t| **t != Tok::Dot).filter(|t| matches!(t, Tok::Digit(_))).count().max(1) as i32; + let shift = exp.rem_euclid(step); + exp -= shift; + m *= 10f64.powi(shift); + let mantissa = fixed(mant, mfirst, mlast, m, sep, dec); + let sign = if exp < 0 { + "-" + } else if plus { + "+" + } else { + "" + }; + let digits = exp.unsigned_abs().to_string(); + let pad = "0".repeat(width.saturating_sub(digits.len())); + Some(format!("{mantissa}{e_char}{sign}{pad}{digits}{}", lits(rest))) +} + +/// `v` (not negative) in the fixed-point format whose tokens are `toks`, with its digits from +/// `first` to `last`. +fn fixed(toks: &[Tok], first: usize, last: usize, v: f64, sep: char, dec: char) -> String { + let percent = toks.contains(&Tok::Percent); + let number = toks.get(first..=last).unwrap_or(&[]); + let dot = number.iter().position(|t| *t == Tok::Dot); + let (int_part, frac_part) = match dot { + Some(d) => (number.get(..d).unwrap_or(&[]), number.get(d + 1..).unwrap_or(&[])), + None => (number, &[][..]), + }; + // Grouping: a comma (or the locale's space, written literally) between integer digits. + let grouping = int_part.iter().any(|t| *t == Tok::Comma || matches!(t, Tok::Lit(s) if s.trim().is_empty())); + // Commas right after the last digit scale by a thousand each (`#,##0,` = thousands). + let after = toks.get(last + 1..).unwrap_or(&[]); + let scale_commas = after.iter().take_while(|t| **t == Tok::Comma).count(); + let min_int = int_part.iter().filter(|t| **t == Tok::Digit('0')).count(); + let frac_digits: Vec = frac_part.iter().filter_map(|t| if let Tok::Digit(c) = t { Some(*c) } else { None }).collect(); + let max_frac = frac_digits.len(); + let min_frac = frac_digits.iter().filter(|c| **c == '0').count(); + let mut x = v * if percent { 100.0 } else { 1.0 } / 1000f64.powi(scale_commas as i32); + if !x.is_finite() { + x = 0.0; + } + let s = format!("{x:.max_frac$}"); + let (ip, fp) = s.split_once('.').unwrap_or((s.as_str(), "")); + let mut fp = fp.to_string(); + while fp.len() > min_frac && fp.ends_with('0') { + fp.pop(); + } + let ip = ip.trim_start_matches('0'); + let ip = if ip.len() < min_int { format!("{}{ip}", "0".repeat(min_int - ip.len())) } else { ip.to_string() }; + let mut num = if grouping { group(&ip, sep) } else { ip }; + if !fp.is_empty() { + num.push(dec); + num.push_str(&fp); + } + let before = lits(toks.get(..first).unwrap_or(&[])); + let after = lits(toks.get(last + 1..).unwrap_or(&[])); + format!("{before}{num}{after}") +} + +#[cfg(test)] +mod tests { + use super::*; + + const RUB: &str = r#"_-* #\ ##0\ "₽"_-;\-* #\ ##0\ "₽"_-;_-* "-"??\ "₽"_-;_-@_-"#; + + #[test] + fn an_overlong_format_is_general() { + let code = "0".repeat(100_000); + assert_eq!(format(&code, 12.5, None), format("General", 12.5, None)); + let ok = "0".repeat(MAX_CODE_LEN); + assert_eq!(format(&ok, 7.0, None).len(), MAX_CODE_LEN); + } + + #[test] + fn scientific_subnormal() { + assert_eq!(format("0.00E+00", 5e-324, None), "4.94E-324"); + assert_eq!(format("0.00E+00", f64::MIN_POSITIVE, None), "2.23E-308"); + } + + #[test] + fn russian_accounting_roubles() { + assert_eq!(format(RUB, 800_000.0, Some("ru-RU")), "800\u{a0}000 ₽"); + assert_eq!(format(RUB, 1_234_567.0, Some("ru-RU")), "1\u{a0}234\u{a0}567 ₽"); + assert_eq!(format(RUB, -5_000.0, Some("ru-RU")), "-5\u{a0}000 ₽"); + assert_eq!(format(RUB, 0.0, Some("ru-RU")), "- ₽"); + } + + #[test] + fn common_codes() { + assert_eq!(format("#,##0", 7000.0, Some("ru-RU")), "7\u{a0}000"); + assert_eq!(format("#,##0", 7000.0, Some("en-US")), "7,000"); + assert_eq!(format("#,##0", 7000.0, Some("de-DE")), "7.000"); + assert_eq!(format("0.0%", 0.125, Some("ru-RU")), "12,5%"); + assert_eq!(format("0.0%", 0.125, None), "12.5%"); + assert_eq!(format("0%", 0.0, None), "0%"); + assert_eq!(format("General", 2.5, Some("ru-RU")), "2,5"); + assert_eq!(format("General", 400_000.0, Some("ru-RU")), "400000"); + assert_eq!(format("General", 1.5e12, None), "1.5E+12"); + assert_eq!(format("General", 1e-10, None), "1E-10"); + assert_eq!(format("General", -2.5e-12, None), "-2.5E-12"); + assert_eq!(format("General", 1e100, None), "1E+100"); + assert_eq!(format("0.00", 1.23456, None), "1.23"); + assert_eq!(format("#,##0.0#", 1234.5, None), "1,234.5"); + assert_eq!(format("#,##0,", 12_345_678.0, None), "12,346"); + assert_eq!(format("[$₽-419]#,##0", 1500.0, Some("ru-RU")), "₽1\u{a0}500"); + assert_eq!(format("0;[Red]-0", -3.0, None), "-3"); + assert_eq!(format("0;0", -3.0, None), "3"); + assert_eq!(format("0", -3.0, None), "-3"); + assert_eq!(format("\"x\"0", 5.0, None), "x5"); + } + + #[test] + fn trailing_commas_with_decimals() { + assert_eq!(format("0.0,,", 15_000_000.0, Some("en-US")), "15.0"); + assert_eq!(format("0.00,\"K\"", 15_000.0, Some("en-US")), "15.00K"); + } + + #[test] + fn explicit_empty_sections_hide_their_values() { + for v in [3.0, -3.0, 0.0, -0.0] { + assert_eq!(format(";;;", v, None), "", "all sections empty: {v}"); + } + for (code, positive, negative, zero) in [ + (";0;0", "", "3", "0"), + ("0;;0", "3", "", "0"), + ("0;-0;", "3", "-3", ""), + (";0", "", "3", ""), + (r#"";"0;;"#, ";3", "", ""), + (r"\;0;;", ";3", "", ""), + ] { + assert_eq!(format(code, 3.0, None), positive, "positive: {code}"); + assert_eq!(format(code, -3.0, None), negative, "negative: {code}"); + assert_eq!(format(code, 0.0, None), zero, "zero: {code}"); + } + assert_eq!(format("0;-0;", -0.0, None), ""); + } + + #[test] + fn blank_whole_codes_and_general_keep_their_fallback() { + for code in ["", " \t", "General", " general "] { + assert_eq!(format(code, 2.5, None), "2.5", "{code:?}"); + assert_eq!(format(code, -2.5, None), "-2.5", "{code:?}"); + assert_eq!(format(code, 0.0, None), "0", "{code:?}"); + assert_eq!(format(code, 2.5, Some("de-DE")), "2,5", "{code:?}"); + } + } + + #[test] + fn scientific_formats() { + assert_eq!(format("0.00E+00", 1234.0, None), "1.23E+03"); + assert_eq!(format("0.00E+00", 1234.0, Some("ru-RU")), "1,23E+03"); + assert_eq!(format("0.00E+00", 0.00012, None), "1.20E-04"); + assert_eq!(format("0.00E+00", -1234.0, None), "-1.23E+03"); + assert_eq!(format("0.00E+00", 0.0, None), "0.00E+00"); + assert_eq!(format("0.0E+000", 5e10, None), "5.0E+010"); + assert_eq!(format("0.0E-0", 1234.0, None), "1.2E3"); + assert_eq!(format("0.0E-0", 0.0012, None), "1.2E-3"); + assert_eq!(format("0E+00", 9999.0, None), "1E+04"); + assert_eq!(format("0.00E+00", 9.999, None), "1.00E+01"); + assert_eq!(format("0.00e+00", 1234.0, None), "1.23e+03"); + assert_eq!(format("##0.0E+0", 12_345.0, None), "12.3E+3"); + assert_eq!(format("##0.0E+0", 0.012, None), "12.0E-3"); + assert_eq!(format("##0.0E+0", 999.96, None), "1.0E+3"); + } + + #[test] + fn never_panics() { + for code in ["", ";", ";;;", "\"", "[", "\\", "0.", ".", "#,", "%", "_", "*", "@", "0E+00", "[$", "\"unterminated"] { + for v in [0.0, -1.5, 1e300, f64::NAN, f64::INFINITY, 1e-300] { + let _ = format(code, v, Some("ru")); + } + } + } +} diff --git a/crates/drawingml/src/style.rs b/crates/drawingml/src/style.rs new file mode 100644 index 000000000..915214de5 --- /dev/null +++ b/crates/drawingml/src/style.rs @@ -0,0 +1,572 @@ +//! Colours, fills, lines and effects. + +use crate::color::{ColorScheme, ColorTransform, Rgba, SchemeSlot}; +use serde::{Deserialize, Serialize}; + +use crate::MediaId; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum ColorBase { + Rgb { + rgb: Rgba, + }, + Scheme { + slot: SchemeSlot, + }, + Preset { + name: String, + }, + /// A system colour (`windowText`…) with the value it had when saved. + System { + name: String, + last: Rgba, + }, +} + +/// A colour as written in a document: a base (RGB, theme slot…) plus transforms. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ColorRef { + pub base: ColorBase, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub mods: Vec, +} + +impl ColorRef { + pub fn rgb(c: Rgba) -> Self { + ColorRef { base: ColorBase::Rgb { rgb: c }, mods: vec![] } + } + pub fn scheme(slot: SchemeSlot) -> Self { + ColorRef { base: ColorBase::Scheme { slot }, mods: vec![] } + } + pub fn with(mut self, m: ColorTransform) -> Self { + self.mods.push(m); + self + } + /// Resolve against a colour scheme; `ph` substitutes `phClr` (style matrix colour). + pub fn resolve(&self, scheme: &ColorScheme, ph: Option) -> Rgba { + let base = match &self.base { + ColorBase::Rgb { rgb } => *rgb, + ColorBase::Scheme { slot: SchemeSlot::PhClr } => ph.unwrap_or_else(|| scheme.get(SchemeSlot::Accent1)), + ColorBase::Scheme { slot } => scheme.get(*slot), + ColorBase::Preset { name } => crate::color::preset(name).unwrap_or(Rgba::BLACK), + ColorBase::System { last, .. } => *last, + }; + crate::color::apply(base, &self.mods) + } + /// Alpha (0–1) from the `alpha`, `alphaMod` and `alphaOff` transforms, applied in order. + pub fn alpha(&self) -> f64 { + self.mods.iter().fold(1.0, |a, m| match m { + ColorTransform::Alpha(v) => (*v as f64 / 100_000.0).clamp(0.0, 1.0), + ColorTransform::AlphaMod(v) => (a * *v as f64 / 100_000.0).clamp(0.0, 1.0), + ColorTransform::AlphaOff(v) => (a + *v as f64 / 100_000.0).clamp(0.0, 1.0), + _ => a, + }) + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct GradientStop { + /// 0–1. + pub pos: f64, + pub color: ColorRef, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum GradientFlip { + None, + X, + Y, + Xy, +} + +impl GradientFlip { + pub fn xml(self) -> &'static str { + match self { + GradientFlip::None => "none", + GradientFlip::X => "x", + GradientFlip::Y => "y", + GradientFlip::Xy => "xy", + } + } + + pub fn from_xml(value: &str) -> Option { + match value { + "none" => Some(GradientFlip::None), + "x" => Some(GradientFlip::X), + "y" => Some(GradientFlip::Y), + "xy" => Some(GradientFlip::Xy), + _ => None, + } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum GradientShape { + /// Angle in degrees clockwise from left→right. + Linear { + angle: f64, + #[serde(default)] + scaled: bool, + }, + /// `circle`, `rect` or `shape` path gradient; `focus` is the fill-to rect as fractions (l, t, r, b). + Path { path: String, focus: [f64; 4] }, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Gradient { + pub stops: Vec, + pub shape: GradientShape, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub flip: Option, + #[serde(default = "crate::yes")] + pub rotate_with_shape: bool, + /// `a:tileRect`: the box the gradient is laid out in, as insets from the shape's box + /// (fractions; negative reaches outside it). PowerPoint's "from corner" radial presets use it + /// to centre the gradient on the corner. + #[serde(default, skip_serializing_if = "is_zero4")] + pub tile: [f64; 4], +} + +fn is_zero4(v: &[f64; 4]) -> bool { + v.iter().all(|x| *x == 0.0) +} + +/// How a picture fills its box: stretched into a (fractional) inset rect, or tiled. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum PictureMode { + Stretch { + #[serde(default)] + fill_rect: [f64; 4], + }, + Tile { + tx: f64, + ty: f64, + sx: f64, + sy: f64, + #[serde(default)] + flip: String, + #[serde(default)] + align: String, + }, +} + +impl Default for PictureMode { + fn default() -> Self { + PictureMode::Stretch { fill_rect: [0.0; 4] } + } +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct PictureFill { + pub media: MediaId, + /// Source crop as fractions of the image (left, top, right, bottom); negative values pad. + pub crop: [f64; 4], + pub mode: PictureMode, + /// 0–1 opacity of the picture. + pub alpha: Option, + pub adjust: PictureAdjust, + /// `rotWithShape="0"`: the picture keeps to the slide's axes when its shape is rotated or + /// flipped. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub upright: bool, + /// An artistic effect (Picture Format ▸ Artistic Effects). `media` is then the processed + /// picture; the original is kept to change or remove the effect. + #[serde(skip_serializing_if = "Option::is_none")] + pub artistic: Option, +} + +/// An applied artistic effect: its `a14` name (e.g. `artisticBlur`) and the original picture. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Artistic { + pub effect: String, + pub original: MediaId, +} + +/// Picture corrections and colour (Picture Format ▸ Corrections / Color / Transparency). +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct PictureAdjust { + /// -1..1 + pub brightness: f64, + /// -1..1 + pub contrast: f64, + /// 0..4 (1 = unchanged) + pub saturation: Option, + pub grayscale: bool, + /// Recolour duotone (dark, light). + pub duotone: Option<(ColorRef, ColorRef)>, + /// Make a colour transparent (Set Transparent Color). + pub clear_color: Option, + pub sharpen: f64, + /// `a:biLevel`: black and white, split at this luminance (0–1). + #[serde(skip_serializing_if = "Option::is_none")] + pub bilevel: Option, + /// `a:alphaInv`: opacity inverted. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub alpha_inv: bool, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PatternFill { + pub preset: String, + pub fg: ColorRef, + pub bg: ColorRef, +} + +// Boxing the large variant would change every match in the apps' readers and writers. +#[allow(clippy::large_enum_variant)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum Fill { + None, + Solid { + color: ColorRef, + }, + Gradient(Gradient), + Picture(PictureFill), + Pattern(PatternFill), + /// Use the containing group's fill. + Group, + /// Use the slide background (`useBgFill`). + Background, +} + +impl Fill { + pub fn solid(c: ColorRef) -> Self { + Fill::Solid { color: c } + } + pub fn is_none(&self) -> bool { + matches!(self, Fill::None) + } +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum LineCap { + #[default] + Flat, + Round, + Square, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum LineJoin { + #[default] + Round, + Bevel, + Miter, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum LineAlign { + #[default] + Center, + Inside, +} + +impl LineAlign { + pub fn xml(self) -> &'static str { + match self { + LineAlign::Center => "ctr", + LineAlign::Inside => "in", + } + } + + pub fn from_xml(value: &str) -> Option { + match value { + "ctr" => Some(LineAlign::Center), + "in" => Some(LineAlign::Inside), + _ => None, + } + } +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Compound { + #[default] + Single, + Double, + ThickThin, + ThinThick, + Triple, +} + +impl Compound { + pub fn xml(self) -> &'static str { + match self { + Compound::Single => "sng", + Compound::Double => "dbl", + Compound::ThickThin => "thickThin", + Compound::ThinThick => "thinThick", + Compound::Triple => "tri", + } + } + pub fn from_xml(s: &str) -> Self { + match s { + "dbl" => Compound::Double, + "thickThin" => Compound::ThickThin, + "thinThick" => Compound::ThinThick, + "tri" => Compound::Triple, + _ => Compound::Single, + } + } +} + +/// Dash style: a preset name (`solid`, `dot`, `dash`, `lgDash`, `dashDot`, `lgDashDot`, +/// `lgDashDotDot`, `sysDash`, `sysDot`, `sysDashDot`, `sysDashDotDot`) or custom (dash, space) pairs +/// in multiples of the line width. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum Dash { + Preset { name: String }, + Custom { pattern: Vec<(f64, f64)> }, +} + +impl Dash { + pub const PRESETS: [&str; 11] = + ["solid", "sysDot", "sysDash", "dash", "dashDot", "lgDash", "lgDashDot", "lgDashDotDot", "dot", "sysDashDot", "sysDashDotDot"]; + /// Dash/space lengths in multiples of the line width. + pub fn pattern(&self) -> Vec { + match self { + Dash::Custom { pattern } => pattern.iter().flat_map(|(d, s)| [*d, *s]).collect(), + Dash::Preset { name } => match name.as_str() { + "dot" => vec![1.0, 3.0], + "dash" => vec![4.0, 3.0], + "lgDash" => vec![8.0, 3.0], + "dashDot" => vec![4.0, 3.0, 1.0, 3.0], + "lgDashDot" => vec![8.0, 3.0, 1.0, 3.0], + "lgDashDotDot" => vec![8.0, 3.0, 1.0, 3.0, 1.0, 3.0], + "sysDash" => vec![3.0, 1.0], + "sysDot" => vec![1.0, 1.0], + "sysDashDot" => vec![3.0, 1.0, 1.0, 1.0], + "sysDashDotDot" => vec![3.0, 1.0, 1.0, 1.0, 1.0, 1.0], + _ => vec![], + }, + } + } +} + +/// Arrowhead at a line end. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct LineEnd { + /// `none`, `triangle`, `stealth`, `diamond`, `oval`, `arrow`. + pub kind: String, + /// `sm`, `med`, `lg`. + #[serde(default = "med")] + pub w: String, + #[serde(default = "med")] + pub len: String, +} + +fn med() -> String { + "med".into() +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Line { + pub fill: Option, + /// Width in points. + pub width: Option, + pub dash: Option, + pub cap: Option, + pub join: Option, + pub miter_limit: Option, + pub align: Option, + pub compound: Option, + pub head: Option, + pub tail: Option, +} + +impl Line { + pub fn solid(c: ColorRef, width: f64) -> Self { + Line { fill: Some(Fill::solid(c)), width: Some(width), ..Default::default() } + } + pub fn none() -> Self { + Line { fill: Some(Fill::None), ..Default::default() } + } + /// Fill unset fields from `parent`. + pub fn inherit(&mut self, parent: &Line) { + if self.fill.is_none() { + self.fill.clone_from(&parent.fill); + } + self.width = self.width.or(parent.width); + if self.dash.is_none() { + self.dash.clone_from(&parent.dash); + } + self.cap = self.cap.or(parent.cap); + self.join = self.join.or(parent.join); + self.miter_limit = self.miter_limit.or(parent.miter_limit); + self.align = self.align.or(parent.align); + self.compound = self.compound.or(parent.compound); + if self.head.is_none() { + self.head.clone_from(&parent.head); + } + if self.tail.is_none() { + self.tail.clone_from(&parent.tail); + } + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Shadow { + pub color: ColorRef, + /// Blur radius, points. + pub blur: f64, + /// Distance, points. + pub dist: f64, + /// Direction, degrees (0 = right, 90 = down). + pub dir: f64, + #[serde(default)] + pub inner: bool, + /// Scale (1 = 100%) for perspective shadows. + #[serde(default = "one")] + pub sx: f64, + #[serde(default = "one")] + pub sy: f64, + #[serde(default)] + pub kx: f64, + #[serde(default)] + pub ky: f64, + #[serde(default)] + pub align: String, + #[serde(default = "crate::yes")] + pub rotate_with_shape: bool, +} + +fn one() -> f64 { + 1.0 +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Glow { + pub color: ColorRef, + pub radius: f64, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Reflection { + pub blur: f64, + pub start_alpha: f64, + pub end_alpha: f64, + pub end_pos: f64, + pub dist: f64, + /// DrawingML reflection transform values retain their native integer units. + pub st_pos: Option, + pub dir: Option, + pub fade_dir: Option, + pub sx: Option, + pub sy: Option, + pub kx: Option, + pub ky: Option, + pub align: Option, + pub rotate_with_shape: Option, +} + +impl Default for Reflection { + fn default() -> Self { + Self { + blur: 0.0, + start_alpha: 0.0, + end_alpha: 0.0, + end_pos: 0.0, + dist: 0.0, + st_pos: None, + dir: Some(5_400_000), + fade_dir: None, + sx: None, + sy: Some(-100_000), + kx: None, + ky: None, + align: Some("bl".into()), + rotate_with_shape: Some(false), + } + } +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Effects { + pub outer_shadow: Option, + pub inner_shadow: Option, + pub glow: Option, + /// Soft edges radius in points. + pub soft_edge: Option, + pub reflection: Option, + /// Bevel preset name for 3-D format (rendered as an approximation). + pub bevel: Option, + /// Unmodelled 3-D XML kept for round-trip (`scene3d`, `sp3d`). + pub raw3d: Option, + /// `a:fillOverlay`: a fill blended over the shape's fill (`blend`: `over`, `mult`, `screen`, + /// `darken`, `lighten`). Only solid and gradient overlays are kept. + pub fill_overlay: Option, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FillOverlay { + pub blend: String, + pub fill: Fill, +} + +impl Effects { + pub fn is_empty(&self) -> bool { + self.outer_shadow.is_none() + && self.inner_shadow.is_none() + && self.glow.is_none() + && self.soft_edge.is_none() + && self.reflection.is_none() + && self.bevel.is_none() + && self.fill_overlay.is_none() + } +} + +/// A shape's theme style references (`p:style`): which fill/line/effect of the theme's format scheme +/// to use, with which colour, and the theme font and colour for its text. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ShapeStyle { + pub line_ref: (u32, ColorRef), + pub fill_ref: (u32, ColorRef), + pub effect_ref: (u32, ColorRef), + /// `major`, `minor` or `none`, with the text colour. + pub font_ref: (String, Option), +} + +impl ShapeStyle { + /// The default for newly drawn shapes: accent 1 fill, darker accent 1 outline, light text. + pub fn accent(slot: SchemeSlot) -> Self { + ShapeStyle { + line_ref: (2, ColorRef::scheme(slot).with(ColorTransform::Shade(50000))), + fill_ref: (1, ColorRef::scheme(slot)), + effect_ref: (0, ColorRef::scheme(slot)), + font_ref: ("minor".into(), Some(ColorRef::scheme(SchemeSlot::Lt1))), + } + } + /// The default for lines/connectors. + pub fn line(slot: SchemeSlot) -> Self { + ShapeStyle { + line_ref: (1, ColorRef::scheme(slot)), + fill_ref: (0, ColorRef::scheme(slot)), + effect_ref: (0, ColorRef::scheme(slot)), + font_ref: ("minor".into(), Some(ColorRef::scheme(SchemeSlot::Tx1))), + } + } +} diff --git a/crates/drawingml/src/table.rs b/crates/drawingml/src/table.rs new file mode 100644 index 000000000..7772565e4 --- /dev/null +++ b/crates/drawingml/src/table.rs @@ -0,0 +1,649 @@ +//! Tables (graphic frames with `a:tbl`). + +use serde::{Deserialize, Serialize}; + +use crate::style::{ColorRef, Fill, Line}; +use crate::text::{Anchor, TextBody}; + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Cell { + pub text: TextBody, + pub fill: Option, + /// Left, right, top, bottom borders; `None` = from the table style. + pub borders: [Option; 4], + pub diag_down: Option, + pub diag_up: Option, + /// Margins (l, r, t, b) in points; `None` = defaults (7.2, 7.2, 3.6, 3.6). + pub margins: Option<[f64; 4]>, + pub anchor: Option, + pub grid_span: u32, + pub row_span: u32, + /// Covered by a merge to the left / above. + pub h_merge: bool, + pub v_merge: bool, + pub vertical: Option, + pub anchor_ctr: Option, + pub horz_overflow: Option, +} + +impl Cell { + pub fn new(text: &str) -> Self { + Cell { text: TextBody::from_text(text), grid_span: 1, row_span: 1, ..Default::default() } + } + pub fn is_covered(&self) -> bool { + self.h_merge || self.v_merge + } +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Row { + pub height: f64, + pub cells: Vec, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Table { + /// Column widths in points. + pub cols: Vec, + pub rows: Vec, + /// Table style id (our built-in style names or a PPTX style GUID). + pub style: String, + pub first_row: bool, + pub first_col: bool, + pub last_row: bool, + pub last_col: bool, + pub band_row: bool, + pub band_col: bool, + /// Whether columns are ordered right-to-left (`a:tblPr/@rtl`). + #[serde(skip_serializing_if = "Option::is_none")] + pub rtl: Option, + /// `a:tblPr` fill: a fill behind the whole table (over the style's own background). + #[serde(skip_serializing_if = "Option::is_none")] + pub background: Option, +} + +impl Default for Table { + fn default() -> Self { + Table { + cols: vec![], + rows: vec![], + style: "medium2-accent1".into(), + first_row: true, + first_col: false, + last_row: false, + last_col: false, + band_row: true, + band_col: false, + rtl: None, + background: None, + } + } +} + +impl Table { + pub fn new(rows: usize, cols: usize, width: f64, row_height: f64) -> Self { + let rows = rows.clamp(1, 1000); + let cols = cols.clamp(1, 1000); + let cw = if cols > 0 { width / cols as f64 } else { width }; + Table { + cols: vec![cw; cols], + rows: (0..rows).map(|_| Row { height: row_height, cells: (0..cols).map(|_| Cell::new("")).collect() }).collect(), + ..Default::default() + } + } + pub fn n_rows(&self) -> usize { + self.rows.len() + } + pub fn n_cols(&self) -> usize { + self.cols.len() + } + pub fn cell(&self, r: usize, c: usize) -> Option<&Cell> { + self.rows.get(r).and_then(|row| row.cells.get(c)) + } + pub fn cell_mut(&mut self, r: usize, c: usize) -> Option<&mut Cell> { + self.rows.get_mut(r).and_then(|row| row.cells.get_mut(c)) + } + pub fn width(&self) -> f64 { + self.cols.iter().sum() + } + pub fn height(&self) -> f64 { + self.rows.iter().map(|r| r.height).sum() + } + /// Insert an empty row before `at`. A merge the new row falls inside grows to cover it. + pub fn insert_row(&mut self, at: usize) { + let at = at.min(self.rows.len()); + let merges = self.merges(); + let h = self.rows.get(at.saturating_sub(1)).map(|r| r.height).unwrap_or(30.0); + self.rows.insert(at, Row { height: h, cells: (0..self.cols.len()).map(|_| Cell::new("")).collect() }); + self.set_merges(merges.into_iter().map(|m| m.insert_row(at))); + } + /// Insert an empty column before `at`. A merge the new column falls inside grows to cover it. + pub fn insert_col(&mut self, at: usize) { + let at = at.min(self.cols.len()); + let merges = self.merges(); + let w = self.cols.get(at.saturating_sub(1)).copied().unwrap_or(80.0); + self.cols.insert(at, w); + for r in &mut self.rows { + let i = at.min(r.cells.len()); + r.cells.insert(i, Cell::new("")); + } + self.set_merges(merges.into_iter().map(|m| m.insert_col(at))); + } + /// Delete row `at`. Merges through it shrink; a merge anchored on it moves its anchor (and + /// text) to the next row it covers. + pub fn delete_row(&mut self, at: usize) -> bool { + if self.rows.len() <= 1 || at >= self.rows.len() { + return false; + } + let merges = self.merges(); + for m in &merges { + if m.r == at && m.rs > 1 { + self.move_anchor((m.r, m.c), (m.r + 1, m.c)); + } + } + self.rows.remove(at); + self.set_merges(merges.into_iter().filter_map(|m| m.delete_row(at))); + true + } + /// Delete column `at`. Merges through it shrink; a merge anchored on it moves its anchor (and + /// text) to the next column it covers. + pub fn delete_col(&mut self, at: usize) -> bool { + if self.cols.len() <= 1 || at >= self.cols.len() { + return false; + } + let merges = self.merges(); + for m in &merges { + if m.c == at && m.gs > 1 { + self.move_anchor((m.r, m.c), (m.r, m.c + 1)); + } + } + self.cols.remove(at); + for r in &mut self.rows { + if at < r.cells.len() { + r.cells.remove(at); + } + } + self.set_merges(merges.into_iter().filter_map(|m| m.delete_col(at))); + true + } + /// The merged areas: every uncovered cell spanning more than one cell, clipped to the grid. + fn merges(&self) -> Vec { + let (nr, nc) = (self.rows.len(), self.cols.len()); + let mut out = vec![]; + for (r, row) in self.rows.iter().enumerate() { + for (c, cell) in row.cells.iter().enumerate().take(nc) { + if cell.is_covered() { + continue; + } + let rs = (cell.row_span.max(1) as usize).min(nr - r); + let gs = (cell.grid_span.max(1) as usize).min(nc - c); + if rs > 1 || gs > 1 { + out.push(Merge { r, c, rs, gs }); + } + } + } + out + } + /// Rebuild every cell's spans and merge flags from `merges`. + fn set_merges(&mut self, merges: impl IntoIterator) { + for row in &mut self.rows { + for cell in &mut row.cells { + cell.grid_span = 1; + cell.row_span = 1; + cell.h_merge = false; + cell.v_merge = false; + } + } + for m in merges { + if m.rs <= 1 && m.gs <= 1 { + continue; + } + for r in m.r..m.r.saturating_add(m.rs) { + for c in m.c..m.c.saturating_add(m.gs) { + if let Some(cell) = self.cell_mut(r, c) { + cell.h_merge = c > m.c; + cell.v_merge = r > m.r; + } + } + } + if let Some(cell) = self.cell_mut(m.r, m.c) { + cell.grid_span = u32::try_from(m.gs).unwrap_or(u32::MAX); + cell.row_span = u32::try_from(m.rs).unwrap_or(u32::MAX); + } + } + } + /// Swap the anchor cell's contents into the cell that takes over as anchor. + fn move_anchor(&mut self, from: (usize, usize), to: (usize, usize)) { + let Some(a) = self.cell(from.0, from.1).cloned() else { return }; + let Some(b) = self.cell(to.0, to.1).cloned() else { return }; + if let Some(x) = self.cell_mut(to.0, to.1) { + *x = a; + } + if let Some(x) = self.cell_mut(from.0, from.1) { + *x = b; + } + } + /// Merge the rectangle (r0,c0)–(r1,c1) inclusive into its top-left cell. + pub fn merge(&mut self, r0: usize, c0: usize, r1: usize, c1: usize) -> bool { + let (r0, r1) = (r0.min(r1), r0.max(r1)); + let (c0, c1) = (c0.min(c1), c0.max(c1)); + if r1 >= self.rows.len() || c1 >= self.cols.len() || (r0 == r1 && c0 == c1) { + return false; + } + // Grow the rectangle until no existing merge sticks out of it. + let (mut r0, mut c0, mut r1, mut c1) = (r0, c0, r1, c1); + loop { + let before = (r0, c0, r1, c1); + for (r, row) in self.rows.iter().enumerate() { + for (c, cell) in row.cells.iter().enumerate().take(self.cols.len()) { + if cell.is_covered() { + continue; + } + let ar1 = r.saturating_add((cell.row_span.max(1) as usize).saturating_sub(1)).min(self.rows.len() - 1); + let ac1 = c.saturating_add((cell.grid_span.max(1) as usize).saturating_sub(1)).min(self.cols.len() - 1); + if r <= r1 && ar1 >= r0 && c <= c1 && ac1 >= c0 { + r0 = r0.min(r); + c0 = c0.min(c); + r1 = r1.max(ar1); + c1 = c1.max(ac1); + } + } + } + if (r0, c0, r1, c1) == before { + break; + } + } + let mut text = vec![]; + for r in r0..=r1 { + for c in c0..=c1 { + if let Some(cell) = self.cell_mut(r, c) { + if !cell.text.is_empty() { + text.append(&mut cell.text.paragraphs); + } + cell.h_merge = c > c0; + cell.v_merge = r > r0; + cell.grid_span = 1; + cell.row_span = 1; + } + } + } + if let Some(cell) = self.cell_mut(r0, c0) { + cell.grid_span = (c1 - c0 + 1) as u32; + cell.row_span = (r1 - r0 + 1) as u32; + cell.h_merge = false; + cell.v_merge = false; + if !text.is_empty() { + cell.text.paragraphs = text; + } + } + true + } + /// Undo a merge at (r, c). + pub fn split(&mut self, r: usize, c: usize) -> bool { + let Some(cell) = self.cell(r, c) else { return false }; + let (gs, rs) = (cell.grid_span.max(1) as usize, cell.row_span.max(1) as usize); + if gs == 1 && rs == 1 { + return false; + } + // Spans come from the file and may reach past the table; only walk the cells that exist. + for rr in r..r.saturating_add(rs).min(self.rows.len()) { + for cc in c..c.saturating_add(gs).min(self.cols.len()) { + if let Some(x) = self.cell_mut(rr, cc) { + x.h_merge = false; + x.v_merge = false; + x.grid_span = 1; + x.row_span = 1; + } + } + } + true + } + /// Make every row have exactly `cols.len()` cells (repair after loading). + pub fn normalize(&mut self) { + let n = self.cols.len(); + for r in &mut self.rows { + r.cells.resize_with(n, || Cell::new("")); + for c in &mut r.cells { + c.grid_span = c.grid_span.max(1); + c.row_span = c.row_span.max(1); + } + } + } +} + +/// A merged area: anchor (r, c) spanning `rs` rows and `gs` columns. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Merge { + r: usize, + c: usize, + rs: usize, + gs: usize, +} + +impl Merge { + fn insert_row(self, at: usize) -> Self { + if at <= self.r { + Merge { r: self.r + 1, ..self } + } else if at < self.r + self.rs { + Merge { rs: self.rs + 1, ..self } + } else { + self + } + } + fn insert_col(self, at: usize) -> Self { + if at <= self.c { + Merge { c: self.c + 1, ..self } + } else if at < self.c + self.gs { + Merge { gs: self.gs + 1, ..self } + } else { + self + } + } + fn delete_row(self, at: usize) -> Option { + let m = if at < self.r { + Merge { r: self.r - 1, ..self } + } else if at < self.r + self.rs { + Merge { rs: self.rs - 1, ..self } + } else { + self + }; + // A merge that lost its only row or column is gone, not a 0-span merge over its neighbours. + (m.rs >= 1 && m.gs >= 1 && (m.rs > 1 || m.gs > 1)).then_some(m) + } + fn delete_col(self, at: usize) -> Option { + let m = if at < self.c { + Merge { c: self.c - 1, ..self } + } else if at < self.c + self.gs { + Merge { gs: self.gs - 1, ..self } + } else { + self + }; + // A merge that lost its only row or column is gone, not a 0-span merge over its neighbours. + (m.rs >= 1 && m.gs >= 1 && (m.rs > 1 || m.gs > 1)).then_some(m) + } +} + +/// A table style defined in the file (`ppt/tableStyles.xml`), as PowerPoint writes the built-in +/// styles a deck uses: how each part of a table (whole table, banded rows and columns, first and +/// last rows and columns, corner cells) is filled, bordered and lettered. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct TableStyleDef { + /// The style's GUID (`a:tableStyleId` on tables). + pub id: String, + pub name: String, + pub parts: Vec<(TablePart, TablePartStyle)>, + /// `a:tblBg`: a fill behind the whole table (only `fill` and `fill_ref` are used). + pub background: Option, + /// The definition as read, written back unchanged. + #[serde(skip_serializing_if = "String::is_empty")] + pub raw: String, +} + +impl TableStyleDef { + pub fn part(&self, p: TablePart) -> Option<&TablePartStyle> { + self.parts.iter().find(|(k, _)| *k == p).map(|(_, s)| s) + } +} + +/// The parts of a table style, in the order they apply (later ones win). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum TablePart { + WholeTbl, + Band1H, + Band2H, + Band1V, + Band2V, + LastCol, + FirstCol, + LastRow, + FirstRow, + SeCell, + SwCell, + NeCell, + NwCell, +} + +impl TablePart { + pub const ALL: [TablePart; 13] = [ + TablePart::WholeTbl, + TablePart::Band1H, + TablePart::Band2H, + TablePart::Band1V, + TablePart::Band2V, + TablePart::LastCol, + TablePart::FirstCol, + TablePart::LastRow, + TablePart::FirstRow, + TablePart::SeCell, + TablePart::SwCell, + TablePart::NeCell, + TablePart::NwCell, + ]; + + pub fn xml(self) -> &'static str { + match self { + TablePart::WholeTbl => "wholeTbl", + TablePart::Band1H => "band1H", + TablePart::Band2H => "band2H", + TablePart::Band1V => "band1V", + TablePart::Band2V => "band2V", + TablePart::LastCol => "lastCol", + TablePart::FirstCol => "firstCol", + TablePart::LastRow => "lastRow", + TablePart::FirstRow => "firstRow", + TablePart::SeCell => "seCell", + TablePart::SwCell => "swCell", + TablePart::NeCell => "neCell", + TablePart::NwCell => "nwCell", + } + } + + pub fn from_xml(s: &str) -> Option { + TablePart::ALL.into_iter().find(|p| p.xml() == s) + } +} + +/// One part of a table style. A theme reference (`a:fillRef`, `a:lnRef`) is kept as the theme's +/// style index with its colour. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct TablePartStyle { + pub fill: Option, + pub fill_ref: Option<(u32, ColorRef)>, + /// Left, right, top, bottom, inside horizontal, inside vertical. + pub borders: [Option; 6], + pub text_color: Option, + pub bold: Option, + pub italic: Option, +} + +/// A border of a table style part: a line, or a theme line style reference. +// Boxing the large variant would change every match in the apps' readers and writers. +#[allow(clippy::large_enum_variant)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum TableBorder { + Line { line: Line }, + Ref { idx: u32, color: ColorRef }, +} + +/// Built-in table styles: (id, label). Styles are generated from the theme accents. +pub fn table_styles() -> Vec<(String, String)> { + let mut v = vec![("none".to_string(), "No Style, No Grid".to_string()), ("grid".to_string(), "No Style, Table Grid".to_string())]; + for (kind, label) in [ + ("light1", "Light Style 1"), + ("light2", "Light Style 2"), + ("medium2", "Medium Style 2"), + ("medium4", "Medium Style 4"), + ("dark1", "Dark Style 1"), + ] { + v.push((format!("{kind}-tx1"), label.to_string())); + for a in 1..=6 { + v.push((format!("{kind}-accent{a}"), format!("{label} - Accent {a}"))); + } + } + v +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Every cell is owned by exactly one anchor (itself or a span over it), and no span leaves the grid. + fn assert_topology(t: &Table) { + let (nr, nc) = (t.n_rows(), t.n_cols()); + let mut owner = vec![vec![0u32; nc]; nr]; + for (r, row) in t.rows.iter().enumerate() { + assert_eq!(row.cells.len(), nc); + for (c, cell) in row.cells.iter().enumerate() { + if cell.is_covered() { + assert_eq!((cell.grid_span, cell.row_span), (1, 1), "covered ({r},{c}) has spans"); + continue; + } + let (rs, gs) = (cell.row_span as usize, cell.grid_span as usize); + assert!(r + rs <= nr && c + gs <= nc, "span at ({r},{c}) leaves the grid"); + for o in owner.iter_mut().skip(r).take(rs) { + for x in o.iter_mut().skip(c).take(gs) { + *x += 1; + } + } + } + } + for (r, o) in owner.iter().enumerate() { + for (c, n) in o.iter().enumerate() { + assert_eq!(*n, 1, "cell ({r},{c}) owned {n} times"); + } + } + } + + fn merged() -> Table { + // 4x4 with a 2x2 merge at (1,1) holding "M". + let mut t = Table::new(4, 4, 400.0, 30.0); + if let Some(c) = t.cell_mut(1, 1) { + c.text = TextBody::from_text("M"); + } + assert!(t.merge(1, 1, 2, 2)); + assert_topology(&t); + t + } + + fn anchor_text(t: &Table, r: usize, c: usize) -> String { + t.cell(r, c).unwrap().text.text() + } + + #[test] + fn merging_over_an_existing_merge_takes_it_in() { + let mut t = Table::new(3, 3, 300.0, 30.0); + if let Some(c) = t.cell_mut(1, 2) { + c.text = TextBody::from_text("X"); + } + assert!(t.merge(1, 1, 1, 2)); + assert!(t.merge(0, 0, 1, 1)); + assert_topology(&t); + let a = t.cell(0, 0).unwrap(); + assert_eq!((a.row_span, a.grid_span), (2, 3)); + assert_eq!(anchor_text(&t, 0, 0), "X"); + } + + #[test] + fn row_and_column_edits_keep_merges_consistent() { + for at in 0..=4 { + let mut t = merged(); + t.insert_row(at); + assert_topology(&t); + let mut t = merged(); + t.insert_col(at); + assert_topology(&t); + } + for at in 0..4 { + let mut t = merged(); + assert!(t.delete_row(at)); + assert_topology(&t); + let mut t = merged(); + assert!(t.delete_col(at)); + assert_topology(&t); + } + } + + #[test] + fn inserting_inside_a_merge_grows_it() { + let mut t = merged(); + t.insert_row(2); + let a = t.cell(1, 1).unwrap(); + assert_eq!((a.row_span, a.grid_span), (3, 2)); + assert!(t.cell(2, 2).unwrap().h_merge && t.cell(2, 2).unwrap().v_merge); + t.insert_col(0); + assert_eq!(t.cell(1, 2).unwrap().grid_span, 2); + assert_eq!(anchor_text(&t, 1, 2), "M"); + assert_topology(&t); + } + + #[test] + fn deleting_the_anchor_row_or_column_keeps_the_text() { + let mut t = merged(); + assert!(t.delete_row(1)); + let a = t.cell(1, 1).unwrap(); + assert!(!a.is_covered()); + assert_eq!((a.row_span, a.grid_span), (1, 2)); + assert_eq!(anchor_text(&t, 1, 1), "M"); + assert!(t.delete_col(1)); + let a = t.cell(1, 1).unwrap(); + assert!(!a.is_covered()); + assert_eq!((a.row_span, a.grid_span), (1, 1)); + assert_eq!(anchor_text(&t, 1, 1), "M"); + assert_topology(&t); + } + + #[test] + fn deleting_the_only_row_or_column_of_a_merge_removes_it() { + // A 1x3 merge across a row: deleting that row leaves no merge behind. + let mut t = Table::new(3, 3, 300.0, 30.0); + assert!(t.merge(1, 0, 1, 2)); + assert!(t.delete_row(1)); + assert!((0..t.n_rows()).all(|r| (0..t.n_cols()).all(|c| t.cell(r, c).is_some_and(|x| !x.is_covered() && x.grid_span <= 1)))); + assert_topology(&t); + // A 3x1 merge down a column: deleting that column leaves no merge behind. + let mut t = Table::new(3, 3, 300.0, 30.0); + assert!(t.merge(0, 1, 2, 1)); + assert!(t.delete_col(1)); + assert!((0..t.n_rows()).all(|r| (0..t.n_cols()).all(|c| t.cell(r, c).is_some_and(|x| !x.is_covered() && x.row_span <= 1)))); + assert_topology(&t); + } + + #[test] + fn insert_delete_merge_split() { + let mut t = Table::new(3, 3, 300.0, 30.0); + assert_eq!((t.n_rows(), t.n_cols()), (3, 3)); + t.insert_row(1); + t.insert_col(3); + assert_eq!((t.n_rows(), t.n_cols()), (4, 4)); + assert!(t.delete_row(0) && t.delete_col(0)); + assert_eq!((t.n_rows(), t.n_cols()), (3, 3)); + assert!(t.merge(0, 0, 1, 1)); + assert_eq!(t.cell(0, 0).map(|c| (c.grid_span, c.row_span)), Some((2, 2))); + assert!(t.cell(1, 1).is_some_and(|c| c.is_covered())); + assert!(t.split(0, 0)); + assert!(!t.cell(1, 1).is_some_and(|c| c.is_covered())); + assert!(!t.merge(5, 5, 6, 6)); + assert!(!t.delete_row(99)); + let mut one = Table::new(1, 1, 10.0, 10.0); + assert!(!one.delete_row(0) && !one.delete_col(0)); + assert!(table_styles().len() > 30); + } + + #[test] + fn split_ignores_spans_past_the_table() { + let mut t = Table::new(2, 2, 100.0, 10.0); + if let Some(c) = t.cell_mut(0, 0) { + c.grid_span = u32::MAX; + c.row_span = u32::MAX; + } + assert!(t.split(0, 0)); + assert_eq!(t.cell(0, 0).map(|c| (c.grid_span, c.row_span)), Some((1, 1))); + } +} diff --git a/crates/drawingml/src/text.rs b/crates/drawingml/src/text.rs new file mode 100644 index 000000000..f60f23236 --- /dev/null +++ b/crates/drawingml/src/text.rs @@ -0,0 +1,819 @@ +//! Text bodies: body properties, paragraphs, runs, list styles. + +use serde::{Deserialize, Serialize}; + +use crate::style::{ColorRef, Fill, Line}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Anchor { + Top, + Middle, + Bottom, + Justified, + Distributed, +} + +impl Anchor { + pub fn xml(self) -> &'static str { + match self { + Anchor::Top => "t", + Anchor::Middle => "ctr", + Anchor::Bottom => "b", + Anchor::Justified => "just", + Anchor::Distributed => "dist", + } + } + pub fn from_xml(s: &str) -> Option { + Some(match s { + "t" => Anchor::Top, + "ctr" => Anchor::Middle, + "b" => Anchor::Bottom, + "just" => Anchor::Justified, + "dist" => Anchor::Distributed, + _ => return None, + }) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum AutoFit { + /// Do not autofit. + None, + /// Shrink text on overflow: font scale and line-spacing reduction (0–1). `font_scale` is + /// `None` for a file's `` with no `fontScale`: the schema default 100 %, + /// which PowerPoint draws as it is and only fits again when the text changes (#724). + Shrink { + #[serde(skip_serializing_if = "Option::is_none")] + font_scale: Option, + line_reduction: f64, + }, + /// Resize shape to fit text. + Shape, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum TextDir { + Horizontal, + Vertical, + Vertical270, + Stacked, + EaVertical, + /// `mongolianVert`: turned like `vert`, lines running from left to right. + MongolianVertical, + /// `wordArtVertRtl`: stacked, lines running from right to left. + StackedRtl, +} + +impl TextDir { + pub fn xml(self) -> &'static str { + match self { + TextDir::Horizontal => "horz", + TextDir::Vertical => "vert", + TextDir::Vertical270 => "vert270", + TextDir::Stacked => "wordArtVert", + TextDir::EaVertical => "eaVert", + TextDir::MongolianVertical => "mongolianVert", + TextDir::StackedRtl => "wordArtVertRtl", + } + } + pub fn from_xml(s: &str) -> Option { + Some(match s { + "horz" => TextDir::Horizontal, + "vert" => TextDir::Vertical, + "mongolianVert" => TextDir::MongolianVertical, + "vert270" => TextDir::Vertical270, + "wordArtVert" => TextDir::Stacked, + "wordArtVertRtl" => TextDir::StackedRtl, + "eaVert" => TextDir::EaVertical, + _ => return None, + }) + } +} + +/// `bodyPr`: insets in points, anchoring, wrapping, autofit, columns, direction, warp. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct BodyProps { + pub inset_l: Option, + pub inset_t: Option, + pub inset_r: Option, + pub inset_b: Option, + pub anchor: Option, + pub anchor_ctr: Option, + pub wrap: Option, + pub autofit: Option, + pub vert: Option, + /// Extra text rotation in degrees. + pub rot: Option, + pub columns: Option, + pub col_spacing: Option, + /// WordArt transform (`prstTxWarp`), e.g. `textArchUp`. + pub warp: Option, + /// The warp's adjust values (`prstTxWarp/avLst`: `adj` or `adj1`, `adj2`…, raw guide values); + /// empty = the preset's defaults. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub warp_adj: Vec, + pub rtl_col: Option, + /// Keep text upright when the shape rotates. + pub upright: Option, + /// Space before the first paragraph and after the last count too (`spcFirstLastPara`). + pub space_first_last: Option, + pub compat_ln_spc: Option, + pub vert_overflow: Option, + pub horz_overflow: Option, + /// Unmodelled 3-D XML on the text body (`a:scene3d`, `a:sp3d`, `a:flatTx`). + #[serde(skip_serializing_if = "Option::is_none")] + pub raw3d: Option, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum TextOverflow { + Overflow, + Clip, + Ellipsis, +} + +impl TextOverflow { + pub fn xml(self) -> &'static str { + match self { + Self::Overflow => "overflow", + Self::Clip => "clip", + Self::Ellipsis => "ellipsis", + } + } + + pub fn from_xml(value: &str) -> Option { + Some(match value { + "overflow" => Self::Overflow, + "clip" => Self::Clip, + "ellipsis" => Self::Ellipsis, + _ => return None, + }) + } + + /// A `horzOverflow` value: `ST_TextHorzOverflowType` has only `overflow` and `clip`. + pub fn from_horz_xml(value: &str) -> Option { + Self::from_xml(value).filter(|v| *v != Self::Ellipsis) + } + + /// The `horzOverflow` attribute to write, leaving out `ellipsis`, which is vertical only. + pub fn horz_xml(v: Option) -> Option<&'static str> { + v.filter(|v| *v != Self::Ellipsis).map(Self::xml) + } +} + +impl BodyProps { + pub fn inherit(&mut self, p: &BodyProps) { + self.inset_l = self.inset_l.or(p.inset_l); + self.inset_t = self.inset_t.or(p.inset_t); + self.inset_r = self.inset_r.or(p.inset_r); + self.inset_b = self.inset_b.or(p.inset_b); + self.anchor = self.anchor.or(p.anchor); + self.anchor_ctr = self.anchor_ctr.or(p.anchor_ctr); + self.wrap = self.wrap.or(p.wrap); + self.autofit = self.autofit.or(p.autofit); + self.vert = self.vert.or(p.vert); + self.rot = self.rot.or(p.rot); + self.columns = self.columns.or(p.columns); + self.col_spacing = self.col_spacing.or(p.col_spacing); + if self.warp.is_none() { + self.warp.clone_from(&p.warp); + self.warp_adj.clone_from(&p.warp_adj); + } + self.rtl_col = self.rtl_col.or(p.rtl_col); + self.upright = self.upright.or(p.upright); + self.space_first_last = self.space_first_last.or(p.space_first_last); + self.compat_ln_spc = self.compat_ln_spc.or(p.compat_ln_spc); + self.vert_overflow = self.vert_overflow.or(p.vert_overflow); + self.horz_overflow = self.horz_overflow.or(p.horz_overflow); + if self.raw3d.is_none() { + self.raw3d.clone_from(&p.raw3d); + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Align { + Left, + Center, + Right, + Justify, + /// Justified with kashida (tatweel) elongation for Arabic (`justLow`). + JustLow, + Distributed, + ThaiDistributed, +} + +impl Align { + pub fn xml(self) -> &'static str { + match self { + Align::Left => "l", + Align::Center => "ctr", + Align::Right => "r", + Align::Justify => "just", + Align::JustLow => "justLow", + Align::Distributed => "dist", + Align::ThaiDistributed => "thaiDist", + } + } + pub fn from_xml(s: &str) -> Option { + Some(match s { + "l" => Align::Left, + "ctr" => Align::Center, + "r" => Align::Right, + "just" => Align::Justify, + "thaiDist" => Align::ThaiDistributed, + "justLow" => Align::JustLow, + "dist" => Align::Distributed, + _ => return None, + }) + } +} + +/// Spacing as a percentage of the line (1.0 = single) or in points. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind", content = "v")] +pub enum Spacing { + Pct(f64), + Pts(f64), +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum Bullet { + None, + Char { + char: String, + }, + /// `arabicPeriod`, `arabicParenR`, `romanUcPeriod`, `romanLcPeriod`, `alphaUcPeriod`, + /// `alphaLcParenR`, `alphaLcPeriod`, `circleNumDbPlain`… + AutoNum { + scheme: String, + #[serde(default = "start_one")] + start_at: u32, + }, + Picture { + media: crate::MediaId, + }, +} + +fn start_one() -> u32 { + 1 +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct TabStop { + pub pos: f64, + /// `l`, `ctr`, `r`, `dec`. + pub align: String, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct ParaProps { + pub align: Option, + /// Left margin (points). + pub margin_left: Option, + pub margin_right: Option, + /// First-line indent relative to the margin (negative = hanging). + pub indent: Option, + pub line_spacing: Option, + pub space_before: Option, + pub space_after: Option, + pub bullet: Option, + pub bullet_font: Option, + pub bullet_font_follow_text: Option, + /// `None` = follow text colour. + pub bullet_color: Option, + pub bullet_color_follow_text: Option, + /// Bullet size as a fraction of the text size. + pub bullet_size: Option, + /// Absolute bullet font size in points, independent of the paragraph's run size. + pub bullet_size_points: Option, + pub bullet_size_follow_text: Option, + pub tabs: Option>, + pub default_tab: Option, + pub rtl: Option, + pub font_align: Option, + pub east_asian_line_break: Option, + /// `latinLnBrk`: Latin words may break inside (after `/`, `\`); off by default. + pub latin_line_break: Option, + /// `hangingPunct`: closing East Asian punctuation may hang past the end of a line. + pub hanging_punct: Option, +} + +impl ParaProps { + pub fn inherit(&mut self, p: &ParaProps) { + self.align = self.align.or(p.align); + self.margin_left = self.margin_left.or(p.margin_left); + self.margin_right = self.margin_right.or(p.margin_right); + self.indent = self.indent.or(p.indent); + self.line_spacing = self.line_spacing.or(p.line_spacing); + self.space_before = self.space_before.or(p.space_before); + self.space_after = self.space_after.or(p.space_after); + if self.bullet.is_none() { + self.bullet.clone_from(&p.bullet); + } + if self.bullet_font_follow_text == Some(false) || self.bullet_font.is_some() { + self.bullet_font_follow_text = None; + } + if self.bullet_font.is_none() && self.bullet_font_follow_text.is_none() { + if p.bullet_font_follow_text == Some(true) { + self.bullet_font_follow_text = Some(true); + } else { + self.bullet_font.clone_from(&p.bullet_font); + } + } + if self.bullet_color_follow_text == Some(false) || self.bullet_color.is_some() { + self.bullet_color_follow_text = None; + } + if self.bullet_color.is_none() && self.bullet_color_follow_text.is_none() { + if p.bullet_color_follow_text == Some(true) { + self.bullet_color_follow_text = Some(true); + } else { + self.bullet_color.clone_from(&p.bullet_color); + } + } + if self.bullet_size_follow_text == Some(false) || self.bullet_size.is_some() || self.bullet_size_points.is_some() { + self.bullet_size_follow_text = None; + } + if self.bullet_size.is_none() && self.bullet_size_points.is_none() && self.bullet_size_follow_text.is_none() { + if p.bullet_size_points.is_some() { + self.bullet_size_points = p.bullet_size_points; + } else if p.bullet_size_follow_text == Some(true) { + self.bullet_size_follow_text = Some(true); + } else { + self.bullet_size = p.bullet_size; + } + } + if self.tabs.is_none() { + self.tabs.clone_from(&p.tabs); + } + self.default_tab = self.default_tab.or(p.default_tab); + self.rtl = self.rtl.or(p.rtl); + if self.font_align.is_none() { + self.font_align.clone_from(&p.font_align); + } + self.east_asian_line_break = self.east_asian_line_break.or(p.east_asian_line_break); + self.latin_line_break = self.latin_line_break.or(p.latin_line_break); + self.hanging_punct = self.hanging_punct.or(p.hanging_punct); + } +} + +#[cfg(test)] +mod bullet_size_tests { + use super::ParaProps; + + #[test] + fn bullet_size_inheritance_keeps_percentage_and_points_as_one_mode() { + let parent = ParaProps { bullet_size_points: Some(12.0), ..Default::default() }; + let mut child = ParaProps { bullet_size: Some(2.0), ..Default::default() }; + child.inherit(&parent); + assert_eq!(child.bullet_size, Some(2.0)); + assert_eq!(child.bullet_size_points, None); + + let parent = ParaProps { bullet_size: Some(1.5), ..Default::default() }; + let mut child = ParaProps { bullet_size_points: Some(14.0), ..Default::default() }; + child.inherit(&parent); + assert_eq!(child.bullet_size, None); + assert_eq!(child.bullet_size_points, Some(14.0)); + + let parent = ParaProps { bullet_size_points: Some(12.0), ..Default::default() }; + let mut child = ParaProps { bullet_size_follow_text: Some(true), ..Default::default() }; + child.inherit(&parent); + assert_eq!(child.bullet_size, None); + assert_eq!(child.bullet_size_points, None); + assert_eq!(child.bullet_size_follow_text, Some(true)); + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Caps { + None, + Small, + All, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Strike { + None, + Single, + Double, +} + +/// A hyperlink or action on text or a shape. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum Action { + Url { + url: String, + }, + Slide { + slide: crate::SlideId, + }, + NextSlide, + PreviousSlide, + FirstSlide, + LastSlide, + EndShow, + LastViewed, + /// Run a custom show. `returns` is PowerPoint's "Show and return". + CustomShow { + name: String, + #[serde(default = "custom_show_returns", skip_serializing_if = "is_true")] + returns: bool, + }, + Program { + path: String, + }, + PlayMedia, +} + +/// Can a link from a (possibly untrusted) deck be opened outside it, by the slide show or by +/// whoever opens an exported file? Only web pages and mail: `file:`, `javascript:`, relative paths +/// and other schemes (OS protocol handlers, network shares) are not. +pub fn safe_url(url: &str) -> bool { + let u = url.trim(); + let Some((scheme, rest)) = u.split_once(':') else { return false }; + match scheme.to_ascii_lowercase().as_str() { + "http" | "https" => rest.starts_with("//") && rest.len() > 2, + "mailto" => !rest.is_empty(), + _ => false, + } +} + +fn custom_show_returns() -> bool { + true +} + +fn is_true(v: &bool) -> bool { + *v +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Hyperlink { + pub action: Action, + #[serde(default)] + pub tooltip: String, + #[serde(default)] + pub highlight_click: bool, + /// Sound played when the action runs (`a:snd`). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub sound: Option, + /// `endSnd`: stop the sound that is already playing. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub stop_sound: bool, + /// Zoom (Insert ▸ Zoom) with Return to Zoom: in the show, after this slide is shown and + /// advanced past, go back to the slide with the zoom. Saved to .pptx as a plain slide link. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub zoom_return: Option, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct RunProps { + /// Latin font family, or a theme reference `+mj-lt` / `+mn-lt`. + pub font: Option, + pub font_ea: Option, + pub font_cs: Option, + pub font_sym: Option, + /// Size in points. + pub size: Option, + pub bold: Option, + pub italic: Option, + /// `none`, `sng`, `dbl`, `heavy`, `dotted`, `dash`, `wavy`… + pub underline: Option, + pub underline_color: Option, + pub strike: Option, + /// Baseline offset as a fraction of the size (0.3 = superscript 30%). + pub baseline: Option, + pub caps: Option, + /// Character spacing in points. + pub spacing: Option, + /// Kern at and above this size (points); 0 = off. + pub kern: Option, + pub fill: Option, + pub outline: Option, + pub highlight: Option, + pub link: Option, + /// Action on mouse over (Insert ▸ Action ▸ Mouse Over on selected text, `a:hlinkMouseOver`). + pub hover: Option, + pub lang: Option, + #[serde(deserialize_with = "bounded_language")] + pub alt_lang: Option, + pub shadow: Option, + /// `a:innerShdw` on the run. Outer shadow stays in `shadow`. + pub inner_shadow: Option, + pub glow: Option, + pub reflection: Option, + /// Soft-edge radius in points (`a:softEdge`). + pub soft_edge: Option, + /// Spelling/grammar errors are not flagged. + pub no_proof: Option, + pub normalize_height: Option, +} + +fn bounded_language<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + Option::::deserialize(deserializer).map(|lang| lang.map(|value| value.chars().take(256).collect())) +} + +impl RunProps { + pub fn inherit(&mut self, p: &RunProps) { + macro_rules! take { + ($($f:ident),*) => { $( if self.$f.is_none() { self.$f.clone_from(&p.$f); } )* }; + } + take!( + font, + font_ea, + font_cs, + font_sym, + size, + bold, + italic, + underline, + underline_color, + strike, + baseline, + caps, + spacing, + kern, + fill, + outline, + highlight, + link, + hover, + lang, + alt_lang, + shadow, + inner_shadow, + glow, + reflection, + soft_edge, + no_proof, + normalize_height + ); + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase", tag = "kind")] +pub enum RunKind { + Text, + /// A line break inside the paragraph (Shift+Enter). + Break, + /// A field: `slidenum`, `datetime`, `datetime1`…`datetime13`, or other; `text` holds the last value. + Field { + field: String, + }, + /// An inline equation (OMML kept as XML; `text` is its linear form). + Math { + omml: String, + }, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Run { + #[serde(default)] + pub text: String, + #[serde(default, skip_serializing_if = "is_default_props")] + pub props: RunProps, + #[serde(default = "text_kind", skip_serializing_if = "is_text_kind")] + pub kind: RunKind, +} + +fn is_default_props(p: &RunProps) -> bool { + *p == RunProps::default() +} +fn text_kind() -> RunKind { + RunKind::Text +} +fn is_text_kind(k: &RunKind) -> bool { + *k == RunKind::Text +} + +impl Run { + pub fn new(text: impl Into) -> Self { + Run { text: text.into(), props: RunProps::default(), kind: RunKind::Text } + } + pub fn with(text: impl Into, props: RunProps) -> Self { + Run { text: text.into(), props, kind: RunKind::Text } + } + /// The characters this run contributes to the paragraph (a break is one `\u{b}`). + pub fn content(&self) -> &str { + match self.kind { + RunKind::Break => "\u{b}", + _ => &self.text, + } + } + pub fn char_len(&self) -> usize { + self.content().chars().count() + } +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Paragraph { + /// Outline level 0–8. + pub level: u8, + #[serde(skip_serializing_if = "is_default_para")] + pub props: ParaProps, + pub runs: Vec, + /// `a:defRPr`: defaults for runs in this paragraph that omit a property. Sits above the + /// placeholder's list style, so a layout can still supply what this paragraph leaves unset. + #[serde(skip_serializing_if = "is_default_props")] + pub def_props: RunProps, + /// Properties of the paragraph end mark (used for an empty paragraph's caret). + #[serde(skip_serializing_if = "is_default_props")] + pub end_props: RunProps, +} + +fn is_default_para(p: &ParaProps) -> bool { + *p == ParaProps::default() +} + +impl Paragraph { + pub fn new(text: &str) -> Self { + Paragraph { runs: if text.is_empty() { vec![] } else { vec![Run::new(text)] }, ..Default::default() } + } + /// Leading tabs are the outline level (at most 8), the same rule as a typed indent. + /// Returns whether any tab was consumed. + pub fn promote_leading_tabs(&mut self) -> bool { + let Some(r) = self.runs.first_mut() else { return false }; + if r.kind != RunKind::Text { + return false; + } + let tabs = r.text.chars().take_while(|c| *c == '\t').count(); + if tabs == 0 { + return false; + } + r.text = r.text.trim_start_matches('\t').to_string(); + self.level = tabs.min(8) as u8; + true + } + pub fn text(&self) -> String { + self.runs.iter().map(Run::content).collect() + } + pub fn char_len(&self) -> usize { + self.runs.iter().map(Run::char_len).sum() + } + pub fn is_empty(&self) -> bool { + self.char_len() == 0 + } + /// Merge adjacent text runs with identical properties. + pub fn normalize(&mut self) { + let mut out: Vec = Vec::with_capacity(self.runs.len()); + for r in self.runs.drain(..) { + if r.kind == RunKind::Text && r.text.is_empty() { + continue; + } + if let Some(last) = out.last_mut() + && last.kind == RunKind::Text + && r.kind == RunKind::Text + && last.props == r.props + { + last.text.push_str(&r.text); + continue; + } + out.push(r); + } + self.runs = out; + } +} + +/// Per-level paragraph + run defaults (`lstStyle`, master text styles, `defaultTextStyle`). +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct LevelStyle { + pub para: ParaProps, + pub run: RunProps, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct ListStyle { + /// Levels 1–9 (index 0–8); `None` = not specified. + pub levels: Vec>, +} + +impl ListStyle { + pub fn level(&self, l: u8) -> Option<&LevelStyle> { + self.levels.get(l as usize).and_then(Option::as_ref) + } + pub fn set(&mut self, l: u8, s: LevelStyle) { + let i = (l as usize).min(8); + if self.levels.len() <= i { + self.levels.resize(i + 1, None); + } + if let Some(slot) = self.levels.get_mut(i) { + *slot = Some(s); + } + } + pub fn is_empty(&self) -> bool { + self.levels.iter().all(Option::is_none) + } +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct TextBody { + pub body: BodyProps, + #[serde(skip_serializing_if = "ListStyle::is_empty")] + pub list_style: ListStyle, + pub paragraphs: Vec, +} + +impl TextBody { + pub fn from_text(text: &str) -> Self { + let text = text.replace("\r\n", "\n").replace('\r', "\n"); + TextBody { paragraphs: text.split('\n').map(Paragraph::new).collect(), ..Default::default() } + } + /// Plain text with `\n` between paragraphs. + pub fn text(&self) -> String { + self.paragraphs.iter().map(Paragraph::text).collect::>().join("\n") + } + pub fn is_empty(&self) -> bool { + self.paragraphs.iter().all(Paragraph::is_empty) + } + /// Total characters, counting one separator between paragraphs. + pub fn char_len(&self) -> usize { + let n: usize = self.paragraphs.iter().map(Paragraph::char_len).sum(); + n + self.paragraphs.len().saturating_sub(1) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::color::Rgba; + + #[test] + fn alternate_language_inherits_and_native_input_is_bounded() { + let mut child = RunProps::default(); + child.inherit(&RunProps { lang: Some("en-US".into()), alt_lang: Some("fr-FR".into()), ..Default::default() }); + assert_eq!(child.lang.as_deref(), Some("en-US")); + assert_eq!(child.alt_lang.as_deref(), Some("fr-FR")); + + let oversized = "x".repeat(300); + let json = serde_json::json!({"altLang": oversized}); + let props: RunProps = serde_json::from_value(json).expect("run props"); + assert_eq!(props.alt_lang.as_deref().map(str::len), Some(256)); + assert_eq!(serde_json::from_value::(serde_json::json!({})).expect("old run props"), RunProps::default()); + } + + #[test] + fn explicit_bullet_follow_text_resets_block_inheritance() { + let parent = ParaProps { + bullet_font: Some("Inter".into()), + bullet_color: Some(ColorRef::rgb(Rgba::rgb(0, 0, 0))), + bullet_size: Some(2.0), + ..Default::default() + }; + let mut child = ParaProps { + bullet_font_follow_text: Some(true), + bullet_color_follow_text: Some(true), + bullet_size_follow_text: Some(true), + ..Default::default() + }; + + child.inherit(&parent); + + assert_eq!(child.bullet_font, None); + assert_eq!(child.bullet_color, None); + assert_eq!(child.bullet_size, None); + + let mut inherited_reset = ParaProps::default(); + inherited_reset.inherit(&ParaProps { + bullet_font_follow_text: Some(true), + bullet_color_follow_text: Some(true), + bullet_size_follow_text: Some(true), + ..Default::default() + }); + assert_eq!(inherited_reset.bullet_font_follow_text, Some(true)); + assert_eq!(inherited_reset.bullet_color_follow_text, Some(true)); + assert_eq!(inherited_reset.bullet_size_follow_text, Some(true)); + + let mut explicit_false = ParaProps { + bullet_font_follow_text: Some(false), + bullet_color_follow_text: Some(false), + bullet_size_follow_text: Some(false), + ..Default::default() + }; + explicit_false.inherit(&parent); + assert_eq!(explicit_false.bullet_font, parent.bullet_font); + assert_eq!(explicit_false.bullet_color, parent.bullet_color); + assert_eq!(explicit_false.bullet_size, parent.bullet_size); + } +} diff --git a/crates/drawingml/src/trendline.rs b/crates/drawingml/src/trendline.rs new file mode 100644 index 000000000..f6aa4b5c0 --- /dev/null +++ b/crates/drawingml/src/trendline.rs @@ -0,0 +1,766 @@ +//! Least-squares trendlines: linear, exponential, logarithmic, polynomial, power, and a +//! moving average. The fit is what the chart drawer plots and what the axis grows to include. + +use crate::chart::{ChartType, Series, Trendline, TrendlineType}; + +/// A fitted trendline. `y` is `None` where the model is undefined (a log of a non-positive x, +/// an exponential that overflowed). +#[derive(Clone, Debug)] +pub struct TrendFit { + kind: TrendlineType, + model: Model, + r2: f64, + x_min: f64, + x_max: f64, +} + +#[derive(Clone, Debug)] +enum Model { + /// `y = c[0] + c[1] x + c[2] x² + …` + Poly(Vec), + /// `y = c · e^(b x)` + Exp { c: f64, b: f64 }, + /// `y = b + c · ln(x)` + Log { b: f64, c: f64 }, + /// `y = c · x^b` + Power { c: f64, b: f64 }, + /// Window averages in x order. No equation. + Moving(Vec<(f64, f64)>), +} + +impl Trendline { + /// Fit `xs`/`ys` (paired). Non-finite pairs are dropped. `None` when the type needs more + /// points than it was given, or the normal equations have no solution. + pub fn fit(&self, xs: &[f64], ys: &[f64]) -> Option { + let mut pairs: Vec<(f64, f64)> = + xs.iter().zip(ys).map(|(x, y)| (*x, *y)).filter(|(x, y)| x.is_finite() && y.is_finite()).take(10_000).collect(); + if pairs.len() < 2 { + return None; + } + pairs.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal)); + let (x_min, x_max) = (pairs.first().map(|p| p.0)?, pairs.last().map(|p| p.0)?); + let intercept = self.intercept.filter(|v| v.is_finite()); + let (model, used_y, yhat) = match self.kind { + TrendlineType::Linear => { + let (xs, ys) = split(&pairs); + let (b, m) = linreg(&xs, &ys, intercept)?; + let yhat: Vec = xs.iter().map(|x| b + m * x).collect(); + (Model::Poly(vec![b, m]), ys, yhat) + } + TrendlineType::Poly => { + let order = self.order.unwrap_or(2).clamp(2, 6) as usize; + if pairs.len() < order + 1 { + return None; + } + let (xs, ys) = split(&pairs); + let coeff = polyfit(&xs, &ys, order, intercept)?; + let yhat: Vec = xs.iter().map(|x| poly_y(&coeff, *x)).collect(); + (Model::Poly(coeff), ys, yhat) + } + TrendlineType::Exp => fit_exp(&pairs, intercept)?, + TrendlineType::Log => { + let pairs: Vec<(f64, f64)> = pairs.into_iter().filter(|(x, _)| *x > 0.0).collect(); + if pairs.len() < 2 { + return None; + } + let (ln_x, ys): (Vec, Vec) = pairs.iter().map(|(x, y)| (x.ln(), *y)).unzip(); + let (b, c) = linreg(&ln_x, &ys, intercept)?; + let yhat: Vec = ln_x.iter().map(|u| b + c * u).collect(); + (Model::Log { b, c }, ys, yhat) + } + TrendlineType::Power => fit_power(&pairs, intercept)?, + TrendlineType::MovingAvg => { + let period = self.period.unwrap_or(2).clamp(2, 255) as usize; + fit_moving_avg(&pairs, period)? + } + }; + if yhat.iter().any(|v| !v.is_finite()) { + return None; + } + // A moving average starts at the first full window, not at the first data point. + let (x_min, x_max) = if let Model::Moving(pts) = &model { + match (pts.first(), pts.last()) { + (Some(a), Some(b)) => (a.0, b.0), + _ => (x_min, x_max), + } + } else { + (x_min, x_max) + }; + Some(TrendFit { kind: self.kind, model, r2: r_squared(&used_y, &yhat), x_min, x_max }) + } + + /// The equation and R² lines this trendline asks to show. Empty when neither is on. A moving + /// average has no equation, and PowerPoint does not show its R², so that label is left off + /// even when the file asks for it. + pub fn caption(&self, fit: &TrendFit) -> String { + let mut lines = Vec::new(); + if self.disp_eq + && let Some(eq) = fit.equation() + { + lines.push(eq); + } + if self.disp_r_sqr && self.kind != TrendlineType::MovingAvg { + lines.push(fit.r2_label()); + } + lines.join("\n") + } +} + +impl TrendFit { + pub fn y(&self, x: f64) -> Option { + if !x.is_finite() { + return None; + } + let y = match &self.model { + Model::Poly(c) => poly_y(c, x), + Model::Exp { c, b } => c * (b * x).exp(), + Model::Log { b, c } => { + if x <= 0.0 { + return None; + } + b + c * x.ln() + } + Model::Power { c, b } => { + if x <= 0.0 { + return None; + } + c * x.powf(*b) + } + Model::Moving(_) => return None, + }; + y.is_finite().then_some(y) + } + + /// Samples from `x0` to `x1`, forecast included. A moving average returns its window points + /// and ignores the forecast (Excel does not extend one). + pub fn samples(&self, x0: f64, x1: f64) -> Vec<(f64, f64)> { + let (x0, x1) = if x0 <= x1 { (x0, x1) } else { (x1, x0) }; + if !x0.is_finite() || !x1.is_finite() { + return Vec::new(); + } + if let Model::Moving(pts) = &self.model { + return pts.iter().copied().filter(|(x, y)| x.is_finite() && y.is_finite()).take(10_000).collect(); + } + if matches!(self.kind, TrendlineType::Linear) { + return [x0, x1].into_iter().filter_map(|x| self.y(x).map(|y| (x, y))).collect(); + } + let steps = 48; + (0..=steps) + .filter_map(|i| { + let x = x0 + (x1 - x0) * i as f64 / steps as f64; + self.y(x).map(|y| (x, y)) + }) + .collect() + } + + /// Data x plus forecast, in category (or scatter x) units. Forecast is capped so a hostile + /// value cannot walk off the chart. + pub fn span(&self, forward: Option, backward: Option) -> (f64, f64) { + let pad = |v: Option| v.filter(|v| v.is_finite() && *v > 0.0).map(|v| v.min(24.0)).unwrap_or(0.0); + if matches!(self.kind, TrendlineType::MovingAvg) { (self.x_min, self.x_max) } else { (self.x_min - pad(backward), self.x_max + pad(forward)) } + } + + pub fn equation(&self) -> Option { + let body = match &self.model { + Model::Poly(c) => poly_equation(c), + Model::Exp { c, b } => Some(format!("y = {}e^({}x)", num(*c), num(*b))), + Model::Log { b, c } => poly_equation(&[*b, *c]).map(|s| s.replacen("x", "ln(x)", 1)), + Model::Power { c, b } => { + let mag = if (c.abs() - 1.0).abs() < 1e-6 { String::new() } else { num(c.abs()) }; + let sign = if *c < 0.0 { "-" } else { "" }; + Some(format!("y = {sign}{mag}{}", x_pow(*b))) + } + Model::Moving(_) => None, + }?; + Some(body) + } + + pub fn r2_label(&self) -> String { + let r = if self.r2.is_finite() { self.r2 } else { 0.0 }; + if (r - 1.0).abs() < 5e-4 { "R² = 1".to_string() } else { format!("R² = {r:.3}") } + } +} + +impl Series { + /// Trendline y values, including a short forecast, that sit near this series. The axis uses + /// them so a forecast is not clipped off the plot. A value many times the series' own range + /// is left out, so a wild polynomial cannot flatten the chart. + pub fn trendline_ys(&self, scatter: bool) -> Vec { + let n = self.values.len().min(10_000); + let mut xs = Vec::new(); + let mut ys = Vec::new(); + for i in 0..n { + let Some(y) = self.values.get(i).copied().flatten().filter(|v| v.is_finite()) else { continue }; + let x = if scatter { + let Some(x) = self.x_values.get(i).copied().flatten().filter(|v| v.is_finite()) else { continue }; + x + } else { + (i + 1) as f64 + }; + xs.push(x); + ys.push(y); + } + let (data_lo, data_hi) = ys.iter().fold((f64::MAX, f64::MIN), |(lo, hi), y| (lo.min(*y), hi.max(*y))); + if !data_lo.is_finite() || !data_hi.is_finite() { + return Vec::new(); + } + let span = (data_hi - data_lo).abs().max(1.0); + let mid = (data_lo + data_hi) / 2.0; + let mut out = Vec::new(); + for t in self.trendlines.iter().take(8) { + let Some(fit) = t.fit(&xs, &ys) else { continue }; + let (x0, x1) = fit.span(t.forward, t.backward); + for (_, y) in fit.samples(x0, x1) { + if y.is_finite() && (y - mid).abs() <= span * 8.0 { + out.push(y); + } + } + } + out + } +} + +/// Whether a series is plotted against numeric x values rather than category positions. +pub fn series_is_scatter(kind: ChartType) -> bool { + matches!(kind, ChartType::Scatter | ChartType::Bubble) +} + +fn split(pairs: &[(f64, f64)]) -> (Vec, Vec) { + pairs.iter().map(|(x, y)| (*x, *y)).unzip() +} + +/// `y = b + m x`. `intercept` fixes `b`. +fn linreg(xs: &[f64], ys: &[f64], intercept: Option) -> Option<(f64, f64)> { + if xs.len() != ys.len() || xs.len() < 2 { + return None; + } + if let Some(b) = intercept { + let mut sxx = 0.0; + let mut sxy = 0.0; + for (x, y) in xs.iter().zip(ys) { + sxx += x * x; + sxy += x * (y - b); + } + if sxx <= 0.0 || !sxx.is_finite() { + return None; + } + let m = sxy / sxx; + return (m.is_finite() && b.is_finite()).then_some((b, m)); + } + let n = xs.len() as f64; + let mx = xs.iter().sum::() / n; + let my = ys.iter().sum::() / n; + let (mut sxx, mut sxy) = (0.0, 0.0); + for (x, y) in xs.iter().zip(ys) { + let dx = x - mx; + sxx += dx * dx; + sxy += dx * (y - my); + } + // Constant x with a large offset leaves rounding residue in `mx`, so compare against the + // rounding scale of the mean instead of zero. + if sxx <= 4.0 * n * (f64::EPSILON * mx.abs()).powi(2) || !sxx.is_finite() { + return None; + } + let m = sxy / sxx; + let b = my - m * mx; + (m.is_finite() && b.is_finite()).then_some((b, m)) +} + +fn poly_y(coeff: &[f64], x: f64) -> f64 { + let mut acc = 0.0; + let mut xp = 1.0; + for c in coeff { + acc += c * xp; + if !acc.is_finite() { + return f64::NAN; + } + xp *= x; + } + acc +} + +fn fit_exp(pairs: &[(f64, f64)], intercept: Option) -> Option<(Model, Vec, Vec)> { + let pairs: Vec<(f64, f64)> = pairs.iter().copied().filter(|(_, y)| *y > 0.0).collect(); + if pairs.len() < 2 { + return None; + } + let (xs, ln_y): (Vec, Vec) = pairs.iter().map(|(x, y)| (*x, y.ln())).unzip(); + let fixed = intercept.filter(|c| *c > 0.0).map(f64::ln); + let (ln_c, b) = linreg(&xs, &ln_y, fixed)?; + let c = ln_c.exp(); + if !c.is_finite() || c <= 0.0 { + return None; + } + let ys: Vec = pairs.iter().map(|(_, y)| *y).collect(); + let yhat: Vec = xs.iter().map(|x| c * (b * x).exp()).collect(); + Some((Model::Exp { c, b }, ys, yhat)) +} + +fn fit_power(pairs: &[(f64, f64)], intercept: Option) -> Option<(Model, Vec, Vec)> { + let pairs: Vec<(f64, f64)> = pairs.iter().copied().filter(|(x, y)| *x > 0.0 && *y > 0.0).collect(); + if pairs.len() < 2 { + return None; + } + let (ln_x, ln_y): (Vec, Vec) = pairs.iter().map(|(x, y)| (x.ln(), y.ln())).unzip(); + let fixed = intercept.filter(|c| *c > 0.0).map(f64::ln); + let (ln_c, b) = linreg(&ln_x, &ln_y, fixed)?; + let c = ln_c.exp(); + if !c.is_finite() || c <= 0.0 { + return None; + } + let ys: Vec = pairs.iter().map(|(_, y)| *y).collect(); + let yhat: Vec = pairs.iter().map(|(x, _)| c * x.powf(b)).collect(); + Some((Model::Power { c, b }, ys, yhat)) +} + +fn fit_moving_avg(pairs: &[(f64, f64)], period: usize) -> Option<(Model, Vec, Vec)> { + if pairs.len() <= period { + return None; + } + let mut pts = Vec::new(); + let mut ys = Vec::new(); + let mut yhat = Vec::new(); + for i in period - 1..pairs.len() { + let window = &pairs[i + 1 - period..=i]; + let avg = window.iter().map(|(_, y)| y).sum::() / period as f64; + if avg.is_finite() { + pts.push((pairs[i].0, avg)); + ys.push(pairs[i].1); + yhat.push(avg); + } + } + if pts.len() < 2 { + return None; + } + Some((Model::Moving(pts), ys, yhat)) +} + +#[allow(clippy::needless_range_loop)] +fn polyfit_with_intercept(xs: &[f64], ys: &[f64], order: usize, b0: f64) -> Option> { + // Fit `y - b0 = (x / sx) * q(u)` with `u = (x - xm) / su` and `q` of degree `order - 1`. The + // basis `(x / sx) * u^j` spans the same polynomials as `x^(j + 1)`, but keeps the pivots in + // `solve` independent of the units and the offset of x. + let n = xs.len() as f64; + let sx = xs.iter().fold(0.0_f64, |acc, x| acc.max(x.abs())); + if sx <= 0.0 || !sx.is_finite() { + return None; + } + let xm = xs.iter().sum::() / n; + let su = match xs.iter().fold(0.0_f64, |acc, x| acc.max((x - xm).abs())) { + s if s > 0.0 && s.is_finite() => s, + _ => 1.0, + }; + let m = order; + let mut ata = vec![vec![0.0; m]; m]; + let mut atb = vec![0.0; m]; + for (x, y) in xs.iter().zip(ys) { + let v = x / sx; + let u = (x - xm) / su; + let dy = y - b0; + for i in 0..m { + let xi = v * u.powi(i as i32); + atb[i] += dy * xi; + for j in i..m { + ata[i][j] += xi * v * u.powi(j as i32); + } + } + } + for i in 0..m { + for j in 0..i { + ata[i][j] = ata[j][i]; + } + } + let centred: Vec = solve(ata, atb)?.iter().enumerate().map(|(k, c)| c / su.powi(k as i32)).collect(); + let q = expand_shift(¢red, xm); + let mut coeff = Vec::with_capacity(order + 1); + coeff.push(b0); + coeff.extend(q.iter().map(|c| c / sx)); + coeff.iter().all(|v| v.is_finite()).then_some(coeff) +} + +/// Polynomial through the normal equations. `x` is centred when the intercept is free, then +/// expanded back to powers of `x`, so order 6 on category numbers stays solvable. +#[allow(clippy::needless_range_loop)] // the index is the power in the normal equations +fn polyfit(xs: &[f64], ys: &[f64], order: usize, intercept: Option) -> Option> { + let n = xs.len(); + if n != ys.len() || !(1..=6).contains(&order) || n < order + 1 { + return None; + } + if let Some(b0) = intercept { + return polyfit_with_intercept(xs, ys, order, b0); + } + let xm = xs.iter().sum::() / n as f64; + // Scale the centred x to unit range so the pivot threshold in `solve` does not depend on the + // units of x. + let s = xs.iter().fold(0.0_f64, |acc, x| acc.max((x - xm).abs())); + if s <= 0.0 || !s.is_finite() { + return None; + } + let us: Vec = xs.iter().map(|x| (x - xm) / s).collect(); + let m = order + 1; + let mut ata = vec![vec![0.0; m]; m]; + let mut atb = vec![0.0; m]; + for (u, y) in us.iter().zip(ys) { + for i in 0..m { + let ui = if i == 0 { 1.0 } else { u.powi(i as i32) }; + atb[i] += y * ui; + for j in i..m { + let uj = if j == 0 { 1.0 } else { u.powi(j as i32) }; + ata[i][j] += ui * uj; + } + } + } + for i in 0..m { + for j in 0..i { + ata[i][j] = ata[j][i]; + } + } + let centred: Vec = solve(ata, atb)?.iter().enumerate().map(|(k, c)| c / s.powi(k as i32)).collect(); + let coeff = expand_shift(¢red, xm); + coeff.iter().all(|v| v.is_finite()).then_some(coeff) +} + +/// `a[k]` is the coefficient of `(x - xm)^k`. Return coefficients of powers of `x`. +#[allow(clippy::needless_range_loop)] // j is the power of x after the binomial shift +fn expand_shift(a: &[f64], xm: f64) -> Vec { + let mut c = vec![0.0; a.len()]; + for (k, ak) in a.iter().enumerate() { + let mut binom = 1.0; + for j in 0..=k { + c[j] += ak * binom * (-xm).powi((k - j) as i32); + if j < k { + binom *= (k - j) as f64 / (j + 1) as f64; + } + } + } + c +} + +#[allow(clippy::needless_range_loop)] // Gaussian elimination walks rows and columns by index +fn solve(mut a: Vec>, mut b: Vec) -> Option> { + let n = b.len(); + if n == 0 || n > 8 || a.len() != n { + return None; + } + for col in 0..n { + let mut piv = col; + for r in col + 1..n { + if a[r][col].abs() > a[piv][col].abs() { + piv = r; + } + } + if a[piv][col].abs() < 1e-12 { + return None; + } + a.swap(col, piv); + b.swap(col, piv); + let div = a[col][col]; + for c in col..n { + a[col][c] /= div; + } + b[col] /= div; + for r in 0..n { + if r == col { + continue; + } + let f = a[r][col]; + if f == 0.0 { + continue; + } + for c in col..n { + a[r][c] -= f * a[col][c]; + } + b[r] -= f * b[col]; + } + } + b.iter().all(|v| v.is_finite()).then_some(b) +} + +fn r_squared(ys: &[f64], yhat: &[f64]) -> f64 { + if ys.is_empty() || ys.len() != yhat.len() { + return 0.0; + } + let mean = ys.iter().sum::() / ys.len() as f64; + let mut ss_tot = 0.0; + let mut ss_res = 0.0; + for (y, h) in ys.iter().zip(yhat) { + ss_tot += (y - mean) * (y - mean); + ss_res += (y - h) * (y - h); + } + if ss_tot < 1e-18 { + return if ss_res < 1e-12 { 1.0 } else { 0.0 }; + } + let r = 1.0 - ss_res / ss_tot; + if r.is_finite() { r } else { 0.0 } +} + +fn num(v: f64) -> String { + if !v.is_finite() { + return "0".to_string(); + } + let a = v.abs(); + if a == 0.0 { + return "0".to_string(); + } + if !(1.0e-3..1.0e6).contains(&a) { + return format!("{v:.2e}").replace('+', ""); + } + let digits = if a >= 100.0 { + 1 + } else if a >= 10.0 { + 2 + } else { + 3 + }; + let mut s = format!("{v:.prec$}", prec = digits); + if let Some(dot) = s.find('.') { + while s.ends_with('0') { + s.pop(); + } + if s.len() == dot + 1 { + s.pop(); + } + } + s +} + +fn x_pow(p: f64) -> String { + if (p - 1.0).abs() < 1e-6 { + "x".to_string() + } else if (p - 2.0).abs() < 1e-6 { + "x²".to_string() + } else if (p - 3.0).abs() < 1e-6 { + "x³".to_string() + } else { + format!("x^{}", num(p)) + } +} + +fn poly_equation(coeff: &[f64]) -> Option { + // Drop terms that are negligible relative to the largest coefficient, so tiny-valued data keeps its terms. + let scale = coeff.iter().map(|c| c.abs()).fold(0.0, f64::max); + let mut body = String::new(); + let mut first = true; + for (k, c) in coeff.iter().enumerate().rev() { + if c.abs() <= scale * 1e-8 { + continue; + } + let pow = match k { + 0 => String::new(), + 1 => "x".to_string(), + _ => x_pow(k as f64), + }; + let mag = if pow.is_empty() { + num(c.abs()) + } else if (c.abs() - 1.0).abs() < scale.max(1.0) * 1e-8 { + pow + } else { + format!("{}{}", num(c.abs()), pow) + }; + if first { + if *c < 0.0 { + body.push('-'); + } + body.push_str(&mag); + first = false; + } else if *c < 0.0 { + body.push_str(" - "); + body.push_str(&mag); + } else { + body.push_str(" + "); + body.push_str(&mag); + } + } + if body.is_empty() { + body.push('0'); + } + Some(format!("y = {body}")) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn line(kind: TrendlineType) -> Trendline { + Trendline { kind, ..Default::default() } + } + + #[test] + fn linear_fits_tiny_x_values() { + let xs = [1e-10, 2e-10, 3e-10, 4e-10, 5e-10]; + let ys = [1.0, 2.0, 3.0, 4.0, 5.0]; + let fit = line(TrendlineType::Linear).fit(&xs, &ys).expect("fit of tiny x values"); + assert!((fit.y(3e-10).unwrap() - 3.0).abs() < 1e-6); + } + + #[test] + fn poly_fits_tiny_x_values() { + let xs = [1e-5, 2e-5, 3e-5, 4e-5, 5e-5]; + let f = |x: f64| 1.0 + 2.0 * x + 3.0 * x * x; + let ys: Vec = xs.iter().map(|x| f(*x)).collect(); + let fit = line(TrendlineType::Poly).fit(&xs, &ys).expect("poly fit of tiny x values"); + assert!((fit.y(3e-5).unwrap() - f(3e-5)).abs() < 1e-9); + let big: Vec = xs.iter().map(|x| x * 1e5).collect(); + let scaled = line(TrendlineType::Poly).fit(&big, &ys).expect("poly fit of scaled x values"); + for x in xs { + assert!((fit.y(x).unwrap() - scaled.y(x * 1e5).unwrap()).abs() < 1e-9, "{x}"); + } + } + + #[test] + fn poly_with_intercept_fits_tiny_x_values() { + let xs = [1e-5, 2e-5, 3e-5, 4e-5, 5e-5]; + let f = |x: f64| 1.0 + 2.0 * x + 3.0 * x * x; + let ys: Vec = xs.iter().map(|x| f(*x)).collect(); + let mut poly = line(TrendlineType::Poly); + poly.intercept = Some(1.0); + let fit = poly.fit(&xs, &ys).expect("poly fit with intercept of tiny x values"); + assert!((fit.y(3e-5).unwrap() - f(3e-5)).abs() < 1e-9); + let big: Vec = xs.iter().map(|x| x * 1e5).collect(); + let scaled = poly.fit(&big, &ys).expect("poly fit with intercept of scaled x values"); + for x in xs { + assert!((fit.y(x).unwrap() - scaled.y(x * 1e5).unwrap()).abs() < 1e-9, "{x}"); + } + } + + #[test] + fn poly_with_intercept_fits_offset_but_narrow_x_range() { + let xs = [45000.00, 45000.01, 45000.02, 45000.03, 45000.04]; + let f = |x: f64| 1.0 + x * (1.0 + 100.0 * (x - 45000.02)); + let ys: Vec = xs.iter().map(|x| f(*x)).collect(); + let mut poly = line(TrendlineType::Poly); + poly.intercept = Some(1.0); + let fit = poly.fit(&xs, &ys).expect("poly fit with intercept of offset x values"); + for x in xs { + assert!((fit.y(x).unwrap() - f(x)).abs() < 1e-2, "{x}"); + } + } + + #[test] + fn linear_fits_offset_but_narrow_x_range() { + let xs = [45000.00, 45000.01, 45000.02, 45000.03, 45000.04]; + let ys = [1.0, 2.0, 3.0, 4.0, 5.0]; + let fit = line(TrendlineType::Linear).fit(&xs, &ys).expect("fit of offset x values"); + assert!((fit.y(45000.02).unwrap() - 3.0).abs() < 1e-4); + let xs = [1e7, 1e7 + 1.0, 1e7 + 2.0, 1e7 + 3.0, 1e7 + 4.0]; + let fit = line(TrendlineType::Linear).fit(&xs, &ys).expect("fit of 1e7 offset"); + assert!((fit.y(1e7 + 2.0).unwrap() - 3.0).abs() < 1e-4); + } + + #[test] + fn linear_fixed_intercept_on_offset_narrow_x_range() { + let xs = [1e7, 1e7 + 1.0, 1e7 + 2.0, 1e7 + 3.0, 1e7 + 4.0]; + let ys: Vec = xs.iter().map(|x| 2.0 * x).collect(); + let mut forced = line(TrendlineType::Linear); + forced.intercept = Some(0.0); + let fit = forced.fit(&xs, &ys).expect("fixed-intercept fit"); + assert!((fit.y(1e7 + 2.0).unwrap() / (2.0 * (1e7 + 2.0)) - 1.0).abs() < 1e-9); + } + + #[test] + fn linear_fixed_intercept_rejects_overflowing_x() { + let xs = [1e160, 2e160, 3e160]; + let ys = [1.0, 2.0, 3.0]; + let mut forced = line(TrendlineType::Linear); + forced.intercept = Some(0.0); + assert!(forced.fit(&xs, &ys).is_none()); + } + + #[test] + fn linear_rejects_constant_x_with_large_offset() { + let xs = [45000.1; 5]; + let ys = [1.0, 2.0, 3.0, 4.0, 5.0]; + assert!(line(TrendlineType::Linear).fit(&xs, &ys).is_none()); + } + + #[test] + fn linear_recovers_slope_and_a_fixed_intercept() { + let xs = [1.0, 2.0, 3.0, 4.0]; + let ys = [3.0, 5.0, 7.0, 9.0]; + let fit = line(TrendlineType::Linear).fit(&xs, &ys).expect("fit"); + assert!((fit.y(1.0).unwrap() - 3.0).abs() < 1e-9); + assert!((fit.y(4.0).unwrap() - 9.0).abs() < 1e-9); + assert_eq!(fit.equation().as_deref(), Some("y = 2x + 1")); + assert_eq!(fit.r2_label(), "R² = 1"); + let mut forced = line(TrendlineType::Linear); + forced.intercept = Some(0.0); + let fit = forced.fit(&[1.0, 2.0], &[2.0, 4.0]).expect("fit"); + assert!((fit.y(3.0).unwrap() - 6.0).abs() < 1e-6); + assert_eq!(fit.equation().as_deref(), Some("y = 2x")); + } + + #[test] + fn exp_log_power_and_quadratic_round_their_points() { + let xs = [1.0, 2.0, 3.0, 4.0]; + let exp = line(TrendlineType::Exp).fit(&xs, &[1.0, 2.0, 4.0, 8.0]).expect("exp"); + for (x, y) in xs.iter().zip([1.0, 2.0, 4.0, 8.0]) { + assert!((exp.y(*x).unwrap() - y).abs() < 1e-6, "{x}"); + } + assert!(exp.equation().unwrap().contains("e^(")); + let log = line(TrendlineType::Log).fit(&xs, &[1.0_f64.ln(), 2.0_f64.ln(), 3.0_f64.ln(), 4.0_f64.ln()]).expect("log"); + assert!((log.y(2.0).unwrap() - 2.0_f64.ln()).abs() < 1e-6); + assert!(log.equation().unwrap().contains("ln(x)")); + let power = line(TrendlineType::Power).fit(&xs, &[1.0, 4.0, 9.0, 16.0]).expect("power"); + assert!((power.y(3.0).unwrap() - 9.0).abs() < 1e-6); + assert_eq!(power.equation().as_deref(), Some("y = x²")); + let mut poly = line(TrendlineType::Poly); + poly.order = Some(2); + let quad = poly.fit(&xs, &[1.0, 4.0, 9.0, 16.0]).expect("poly"); + assert!((quad.y(3.0).unwrap() - 9.0).abs() < 1e-6); + assert_eq!(quad.equation().as_deref(), Some("y = x²")); + } + + #[test] + fn moving_average_and_forecast_span() { + let fit = Trendline { kind: TrendlineType::MovingAvg, period: Some(2), ..Default::default() } + .fit(&[1.0, 2.0, 3.0, 4.0], &[2.0, 8.0, 4.0, 10.0]) + .expect("ma"); + let pts = fit.samples(0.0, 10.0); + assert_eq!(pts.len(), 3); + assert!((pts[0].1 - 5.0).abs() < 1e-9); + assert!((pts[1].1 - 6.0).abs() < 1e-9); + assert!((pts[2].1 - 7.0).abs() < 1e-9); + assert!(fit.equation().is_none()); + let linear = line(TrendlineType::Linear).fit(&[1.0, 2.0], &[1.0, 2.0]).expect("line"); + assert_eq!(linear.span(Some(1.5), Some(-3.0)), (1.0, 3.5)); + assert_eq!(fit.span(Some(4.0), Some(4.0)), (2.0, 4.0)); + } + + #[test] + fn moving_average_shows_neither_equation_nor_r2() { + let t = Trendline { kind: TrendlineType::MovingAvg, period: Some(2), disp_eq: true, disp_r_sqr: true, ..Default::default() }; + let fit = t.fit(&[1.0, 2.0, 3.0, 4.0], &[2.0, 8.0, 4.0, 10.0]).expect("ma"); + assert!(fit.r2_label().starts_with("R² =")); + assert!(t.caption(&fit).is_empty(), "{}", t.caption(&fit)); + } + + #[test] + fn too_few_points_and_non_finite_input_fit_nothing() { + assert!(line(TrendlineType::Linear).fit(&[1.0], &[2.0]).is_none()); + assert!(line(TrendlineType::Exp).fit(&[1.0, 2.0], &[-1.0, -2.0]).is_none()); + assert!(line(TrendlineType::Poly).fit(&[1.0, 2.0], &[1.0, 2.0]).is_none()); + assert!(line(TrendlineType::Linear).fit(&[f64::NAN, f64::INFINITY], &[1.0, 2.0]).is_none()); + let mut t = line(TrendlineType::Linear); + t.disp_eq = true; + t.disp_r_sqr = true; + let fit = t.fit(&[1.0, 2.0, 3.0], &[1.0, 2.0, 3.0]).expect("fit"); + assert_eq!(t.caption(&fit), "y = x\nR² = 1"); + } + + #[test] + fn equation_keeps_tiny_coefficients() { + let xs = [1.0, 2.0, 3.0, 4.0]; + let lin = line(TrendlineType::Linear).fit(&xs, &[2e-9, 4e-9, 6e-9, 8e-9]).expect("linear"); + assert_eq!(lin.equation().as_deref(), Some("y = 2.00e-9x")); + let mut poly = line(TrendlineType::Poly); + poly.order = Some(2); + let ys: Vec = xs.iter().map(|x| 3e-9 * x * x + 1e-9).collect(); + let quad = poly.fit(&xs, &ys).expect("poly"); + assert_eq!(quad.equation().as_deref(), Some("y = 3.00e-9x² + 1.00e-9")); + let ys: Vec = xs.iter().map(|x: &f64| 5e-9 * x.ln()).collect(); + let log = line(TrendlineType::Log).fit(&xs, &ys).expect("log"); + assert_eq!(log.equation().as_deref(), Some("y = 5.00e-9ln(x)")); + let zero = line(TrendlineType::Linear).fit(&xs, &[0.0; 4]).expect("zero"); + assert_eq!(zero.equation().as_deref(), Some("y = 0")); + } +} From 119f6e252edb842a8ca105287216d4d856c363f7 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:52:13 -0500 Subject: [PATCH 029/120] craft-audio: integer PCM of WAV and AIFF files bit for bit, and minimal files around it (#917) wav_pcm and aiff_pcm (feature decode-native) return a file's integer PCM as stored (RawPcm: little-endian interleaved bytes, 8-bit unsigned, with channels, rate and bits), built on the existing WAV and AIFF chunk parsers. pcm_wav_file, pcm_wav_header, pcm_aiff_file and pcm_aiff_header (feature pcm-encode) write the 44-byte canonical WAVE header and the plain AIFF header around such bytes. For essence embedded in AAF and OMF files, which is moved bit for bit and whose descriptors hold a bare header as a summary. The writers are FilmCraft's (filmcraft-interchange wav.rs); the readers replace its second WAV/AIFF parser with this crate's. --- crates/audio/README.md | 4 +- crates/audio/src/lib.rs | 7 ++ crates/audio/src/read/mod.rs | 2 + crates/audio/src/read/raw.rs | 58 ++++++++++++++ crates/audio/src/write/mod.rs | 4 + crates/audio/src/write/raw.rs | 120 ++++++++++++++++++++++++++++ crates/audio/tests/audio/main.rs | 1 + crates/audio/tests/audio/raw_pcm.rs | 73 +++++++++++++++++ 8 files changed, 267 insertions(+), 2 deletions(-) create mode 100644 crates/audio/src/read/raw.rs create mode 100644 crates/audio/src/write/raw.rs create mode 100644 crates/audio/tests/audio/raw_pcm.rs diff --git a/crates/audio/README.md b/crates/audio/README.md index a1eb059d2..0c9f63a97 100644 --- a/crates/audio/README.md +++ b/crates/audio/README.md @@ -26,11 +26,11 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou | Feature | Provides | Pulls in | |---|---|---| | `decode` | `decode`, `probe`, `Decoder`, `tracks`, `TrackDecoder`; every format in [Format support](#format-support) | `decode-native`, `symphonia` | -| `decode-native` | the same, native formats only (others are `Unsupported`) | the five decoders below, craft-media `isobmff`/`matroska`/`ogg`, `log` | +| `decode-native` | the same, native formats only (others are `Unsupported`); `wav_pcm`, `aiff_pcm` (a file's integer PCM bit for bit, as `RawPcm`) | the five decoders below, craft-media `isobmff`/`matroska`/`ogg`, `log` | | `symphonia` | the fallback for formats with no native decoder; turning it on is the same as `decode` | `decode-native`, symphonia | | `encode` | every encoder: the WAV, AIFF and FLAC file writers, and the AAC, ALAC, AC-3/E-AC-3 and MP3 encoders | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode` | | `lossless-encode` | WAV, AIFF and FLAC writing | `pcm-encode`, `flac-encode` | -| `pcm-encode` | `encode`, `FileEncoder` for WAV and AIFF; `wav_float_header` | none | +| `pcm-encode` | `encode`, `FileEncoder` for WAV and AIFF; `wav_float_header`; `pcm_wav_file`, `pcm_wav_header`, `pcm_aiff_file`, `pcm_aiff_header` (minimal files around integer PCM bytes, for essence embedded in AAF / OMF) | none | | `flac-encode` | `encode`, `FileEncoder` for FLAC; `flac::Encoder`, the packet encoder for callers that mux FLAC themselves | craft-media `bitstream` | | `aac-decode`, `aac-encode` | `aac::Decoder`, `aac::Encoder` | craft-media `bitstream` | | `alac-decode` | `alac::Decoder` | none | diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 5916b91e8..4964166a7 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -19,6 +19,9 @@ //! gives [`wav_float_header`]) and FLAC (feature `flac-encode`), whole-buffer or incremental into //! any `Write + Seek`. Feature `lossless-encode` turns on both; feature `encode` turns on //! `lossless-encode` and every codec's encoder. +//! - [`wav_pcm`], [`aiff_pcm`] (feature `decode-native`) and [`pcm_wav_file`], [`pcm_wav_header`], +//! [`pcm_aiff_file`], [`pcm_aiff_header`] (feature `pcm-encode`): the integer PCM of a WAV or AIFF +//! file bit for bit, and minimal files around such PCM, for essence embedded in other containers. //! - [`peaks`] (feature `peaks`): min/max/RMS pyramid for waveform drawing. //! - Codec modules for packet-level use, each with its own features: [`aac`] (`aac-decode`, //! `aac-encode`), [`alac`] (`alac-decode`, `alac-encode`), [`opus`] (`opus-decode`), [`ac3`] @@ -91,12 +94,16 @@ pub use read::{ DECODE_EXTENSIONS, DEFAULT_MAX_SAMPLES, DecodeOptions, Decoder, TrackInfo, decode, decode_with, detect_container, probe, probe_with, tracks, tracks_of, }; +#[cfg(feature = "decode-native")] +pub use read::{RawPcm, aiff_pcm, wav_pcm}; #[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))] pub use track::{LOOKAHEAD_PACKETS, TrackDecoder}; #[cfg(feature = "pcm-encode")] pub use write::wav_float_header; #[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] pub use write::{ENCODE_EXTENSIONS, FileEncoder, encode, encode_format_for}; +#[cfg(feature = "pcm-encode")] +pub use write::{pcm_aiff_file, pcm_aiff_header, pcm_wav_file, pcm_wav_header}; /// Errors from probing, decoding and encoding. #[non_exhaustive] diff --git a/crates/audio/src/read/mod.rs b/crates/audio/src/read/mod.rs index 533e996a6..35a220767 100644 --- a/crates/audio/src/read/mod.rs +++ b/crates/audio/src/read/mod.rs @@ -14,6 +14,7 @@ mod flac; #[cfg(feature = "symphonia")] mod mp4; mod native; +mod raw; mod source; #[cfg(feature = "symphonia")] mod symph; @@ -21,6 +22,7 @@ mod wav; use crate::{AudioBuffer, AudioError, AudioInfo, ChannelLayout, CodecId, Container, Result}; use native::Native; +pub use raw::{RawPcm, aiff_pcm, wav_pcm}; pub use source::SharedSource; use source::Src; use std::sync::Arc; diff --git a/crates/audio/src/read/raw.rs b/crates/audio/src/read/raw.rs new file mode 100644 index 000000000..e63f9d2cb --- /dev/null +++ b/crates/audio/src/read/raw.rs @@ -0,0 +1,58 @@ +//! The integer PCM of a WAV or AIFF file as stored, for a caller that moves it bit for bit +//! (essence embedded in an AAF or OMF file) instead of decoding it to `f32`. + +use super::source::Src; +use super::{aiff, wav}; +use crate::pcm::PcmKind; + +/// Integer PCM taken out of a WAV or AIFF file: interleaved little-endian samples of +/// `bits.div_ceil(8)` bytes each, signed except for 8-bit, which is unsigned (the WAVE convention). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RawPcm { + /// The samples; empty for a header with no data after it (a descriptor's summary). + pub data: Vec, + /// Interleaved channels (at least 1). + pub channels: u16, + /// Sample rate in Hz (at least 1). + pub sample_rate: u32, + /// Bits a sample occupies in `data`: 8, 16, 24 or 32. + pub bits: u16, +} + +/// The samples of a layout: `len` bytes at `start`, as far as the file has them. +fn stored(b: &[u8], start: u64, len: u64) -> &[u8] { + let start = usize::try_from(start).unwrap_or(usize::MAX).min(b.len()); + let end = start.saturating_add(usize::try_from(len).unwrap_or(usize::MAX)).min(b.len()); + b.get(start..end).unwrap_or_default() +} + +/// The integer PCM of a WAV file (plain or `WAVE_FORMAT_EXTENSIBLE` PCM), bit for bit. `None` for +/// anything else: not a WAV, float or compressed samples, no `fmt ` or `data` chunk. A file cut +/// short gives the samples that are there. +pub fn wav_pcm(bytes: &[u8]) -> Option { + let wav::WavParse::Native(layout) = wav::parse(&Src::Slice(bytes)).ok()? else { return None }; + let PcmKind::Int { bytes: width, big_endian: false, .. } = layout.kind else { return None }; + Some(RawPcm { + data: stored(bytes, layout.data_start, layout.data_len).to_vec(), + channels: layout.info.channels, + sample_rate: layout.info.sample_rate, + bits: u16::try_from(width.checked_mul(8)?).ok()?, + }) +} + +/// The integer PCM of an AIFF or AIFF-C (`NONE`, `twos`, `sowt`) file, converted to the layout of +/// [`RawPcm`]: little-endian, and 8-bit samples made unsigned. `None` for anything else. +pub fn aiff_pcm(bytes: &[u8]) -> Option { + let aiff::AiffParse::Native(layout) = aiff::parse(&Src::Slice(bytes)).ok()? else { return None }; + let PcmKind::Int { bytes: width, big_endian, .. } = layout.kind else { return None }; + let stored = stored(bytes, layout.data_start, layout.data_len); + let mut data = Vec::with_capacity(stored.len()); + for s in stored.chunks_exact(width.max(1)) { + match (width, big_endian) { + (1, _) => data.extend(s.iter().map(|b| b ^ 0x80)), + (_, true) => data.extend(s.iter().rev()), + (_, false) => data.extend_from_slice(s), + } + } + Some(RawPcm { data, channels: layout.info.channels, sample_rate: layout.info.sample_rate, bits: u16::try_from(width.checked_mul(8)?).ok()? }) +} diff --git a/crates/audio/src/write/mod.rs b/crates/audio/src/write/mod.rs index 25c9b2528..2355a7b05 100644 --- a/crates/audio/src/write/mod.rs +++ b/crates/audio/src/write/mod.rs @@ -7,9 +7,13 @@ mod aiff; pub(crate) mod flac; mod pcm; #[cfg(feature = "pcm-encode")] +mod raw; +#[cfg(feature = "pcm-encode")] mod wav; use crate::{AudioBuffer, AudioError, ChannelLayout, EncodeFormat, EncodeOptions, Result}; +#[cfg(feature = "pcm-encode")] +pub use raw::{pcm_aiff_file, pcm_aiff_header, pcm_wav_file, pcm_wav_header}; use std::io::{Seek, Write}; /// Encode `buf` as WAV/BWF, AIFF/AIFF-C or FLAC. Integer depths clamp samples to [-1, 1]; float diff --git a/crates/audio/src/write/raw.rs b/crates/audio/src/write/raw.rs new file mode 100644 index 000000000..dc9a31649 --- /dev/null +++ b/crates/audio/src/write/raw.rs @@ -0,0 +1,120 @@ +//! Minimal WAVE and AIFF files around integer PCM the caller already has as bytes (essence +//! embedded in an AAF or OMF file): the 44-byte canonical WAVE header and the plain AIFF header, +//! with nothing a reader of such a summary could trip over (no `JUNK`, no extensible `fmt `). +//! [`crate::encode`] writes full WAV/BWF/RF64 and AIFF files from `f32`. +//! +//! PCM here is interleaved little-endian samples of `bits.div_ceil(8)` bytes, 8-bit unsigned +//! (the layout of a WAVE `data` chunk). Sizes saturate at the 32-bit fields instead of wrapping. + +use super::aiff::u32_to_extended; + +/// A complete WAVE file around little-endian PCM. +pub fn pcm_wav_file(pcm: &[u8], channels: u16, sample_rate: u32, bits: u16) -> Vec { + let mut v = pcm_wav_header(u32::try_from(pcm.len()).unwrap_or(u32::MAX), channels, sample_rate, bits); + v.extend_from_slice(pcm); + if pcm.len() % 2 == 1 { + v.push(0); + } + v +} + +/// The 44 bytes of a canonical WAVE file up to and including the `data` chunk header, for +/// `data_len` bytes of PCM. +pub fn pcm_wav_header(data_len: u32, channels: u16, sample_rate: u32, bits: u16) -> Vec { + let block = channels.max(1).saturating_mul(bits.div_ceil(8)); + let mut v = Vec::with_capacity(44); + v.extend_from_slice(b"RIFF"); + v.extend_from_slice(&36u32.saturating_add(data_len).saturating_add(data_len & 1).to_le_bytes()); + v.extend_from_slice(b"WAVEfmt "); + v.extend_from_slice(&16u32.to_le_bytes()); + v.extend_from_slice(&1u16.to_le_bytes()); + v.extend_from_slice(&channels.max(1).to_le_bytes()); + v.extend_from_slice(&sample_rate.to_le_bytes()); + v.extend_from_slice(&sample_rate.saturating_mul(u32::from(block)).to_le_bytes()); + v.extend_from_slice(&block.to_le_bytes()); + v.extend_from_slice(&bits.to_le_bytes()); + v.extend_from_slice(b"data"); + v.extend_from_slice(&data_len.to_le_bytes()); + v +} + +/// A complete AIFF file (big-endian, 8-bit signed) from little-endian PCM. +pub fn pcm_aiff_file(pcm: &[u8], channels: u16, sample_rate: u32, bits: u16) -> Vec { + let width = usize::from(bits.div_ceil(8)).max(1); + let frames = pcm.len() / (width * usize::from(channels.max(1))); + let mut v = pcm_aiff_header(u32::try_from(frames).unwrap_or(u32::MAX), channels, sample_rate, bits); + let head = v.len(); + for s in pcm.chunks_exact(width) { + if width == 1 { + v.extend(s.iter().map(|b| b ^ 0x80)); // WAVE 8-bit is unsigned, AIFF signed + } else { + v.extend(s.iter().rev()); + } + } + if (v.len() - head) % 2 == 1 { + v.push(0); + } + v +} + +/// The 54 bytes of an AIFF file up to the sound data (`FORM`, `COMM`, `SSND` with its offset and +/// block size), for `frames` sample frames. +pub fn pcm_aiff_header(frames: u32, channels: u16, sample_rate: u32, bits: u16) -> Vec { + let data_len = u64::from(frames).saturating_mul(u64::from(channels.max(1))).saturating_mul(u64::from(bits.div_ceil(8))); + let chunk = |extra: u64| u32::try_from(extra.saturating_add(data_len)).unwrap_or(u32::MAX); + let mut v = Vec::with_capacity(54); + v.extend_from_slice(b"FORM"); + v.extend_from_slice(&chunk(4 + 26 + 16 + (data_len & 1)).to_be_bytes()); + v.extend_from_slice(b"AIFFCOMM"); + v.extend_from_slice(&18u32.to_be_bytes()); + v.extend_from_slice(&channels.max(1).to_be_bytes()); + v.extend_from_slice(&frames.to_be_bytes()); + v.extend_from_slice(&bits.to_be_bytes()); + v.extend_from_slice(&u32_to_extended(sample_rate)); + v.extend_from_slice(b"SSND"); + v.extend_from_slice(&chunk(8).to_be_bytes()); + v.extend_from_slice(&[0; 8]); + v +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A descriptor for more than 4 GiB of PCM: the RIFF and FORM size fields saturate. + #[test] + fn huge_headers_saturate() { + let h = pcm_wav_header(u32::MAX, 2, u32::MAX, 24); + assert_eq!(h.get(4..8), Some(&u32::MAX.to_le_bytes()[..])); + assert_eq!(h.get(28..32), Some(&u32::MAX.to_le_bytes()[..]), "byte rate"); + let a = pcm_aiff_header(u32::MAX, u16::MAX, 48_000, 32); + assert_eq!(a.get(4..8), Some(&u32::MAX.to_be_bytes()[..])); + assert_eq!(a.len(), 54); + } + + #[test] + fn headers_have_the_canonical_layout() { + let h = pcm_wav_header(96, 2, 48_000, 24); + assert_eq!(h.len(), 44); + let mut expect = b"RIFF".to_vec(); + expect.extend_from_slice(&132u32.to_le_bytes()); + expect.extend_from_slice(b"WAVEfmt "); + for x in [16u32, 1 | 2 << 16, 48_000, 288_000, 6 | 24 << 16] { + expect.extend_from_slice(&x.to_le_bytes()); + } + expect.extend_from_slice(b"data"); + expect.extend_from_slice(&96u32.to_le_bytes()); + assert_eq!(h, expect); + // zero channels are written as one, and an odd data length counts its pad byte + let odd = pcm_wav_file(&[1, 2, 3], 0, 8_000, 8); + assert_eq!(odd.len(), 48); + assert_eq!(odd.get(4..8), Some(&40u32.to_le_bytes()[..])); + assert_eq!(odd.get(22..24), Some(&1u16.to_le_bytes()[..])); + let a = pcm_aiff_header(8, 2, 44_100, 24); + assert_eq!(a.get(..4), Some(&b"FORM"[..])); + assert_eq!(a.get(4..8), Some(&(46u32 + 48).to_be_bytes()[..])); + // 44 100 Hz as an 80-bit extended float + assert_eq!(a.get(28..38), Some(&[0x40, 0x0E, 0xAC, 0x44, 0, 0, 0, 0, 0, 0][..])); + assert_eq!(a.get(42..46), Some(&(8u32 + 48).to_be_bytes()[..])); + } +} diff --git a/crates/audio/tests/audio/main.rs b/crates/audio/tests/audio/main.rs index f247167b5..8c37256d6 100644 --- a/crates/audio/tests/audio/main.rs +++ b/crates/audio/tests/audio/main.rs @@ -6,6 +6,7 @@ mod flac_damage; mod float_headroom; mod malformed; mod mp4_length; +mod raw_pcm; mod roundtrip; mod stream; mod wav_empty_data; diff --git a/crates/audio/tests/audio/raw_pcm.rs b/crates/audio/tests/audio/raw_pcm.rs new file mode 100644 index 000000000..1b5fed669 --- /dev/null +++ b/crates/audio/tests/audio/raw_pcm.rs @@ -0,0 +1,73 @@ +//! Integer PCM in and out of WAV and AIFF files bit for bit (`wav_pcm`, `aiff_pcm`, `pcm_*`). + +use craft_audio::{ + AudioBuffer, BitDepth, EncodeFormat, EncodeOptions, RawPcm, aiff_pcm, encode, pcm_aiff_file, pcm_aiff_header, pcm_wav_file, pcm_wav_header, + wav_pcm, +}; + +fn raw(data: &[u8], channels: u16, sample_rate: u32, bits: u16) -> Option { + Some(RawPcm { data: data.to_vec(), channels, sample_rate, bits }) +} + +#[test] +fn wav_and_aiff_round_trip_bit_for_bit() { + let pcm: Vec = (0..48u8).collect(); // 8 stereo 24-bit frames + let w = pcm_wav_file(&pcm, 2, 48_000, 24); + assert_eq!(wav_pcm(&w), raw(&pcm, 2, 48_000, 24)); + let a = pcm_aiff_file(&pcm, 2, 44_100, 24); + assert_eq!(aiff_pcm(&a), raw(&pcm, 2, 44_100, 24)); + let p16: Vec = (0..32u8).collect(); + assert_eq!(aiff_pcm(&pcm_aiff_file(&p16, 1, 96_000, 16)), raw(&p16, 1, 96_000, 16)); + // 8-bit: unsigned in WAVE and in `RawPcm`, signed in AIFF + let p8 = [0u8, 0x7F, 0x80, 0xFF, 3]; + assert_eq!(wav_pcm(&pcm_wav_file(&p8, 1, 8_000, 8)), raw(&p8, 1, 8_000, 8)); + let a8 = pcm_aiff_file(&p8, 1, 8_000, 8); + assert_eq!(a8.get(54..59), Some(&[0x80, 0xFF, 0x00, 0x7F, 0x83][..])); + assert_eq!(a8.len(), 60, "odd sound data is padded"); + assert_eq!(aiff_pcm(&a8), raw(&p8, 1, 8_000, 8)); + // neither reads the other's container + assert_eq!(wav_pcm(&a), None); + assert_eq!(aiff_pcm(&w), None); +} + +/// A descriptor's summary is the header alone: the format is read, with no samples. +#[test] +fn headers_without_data_give_the_format() { + assert_eq!(wav_pcm(&pcm_wav_header(4800, 2, 48_000, 16)), raw(&[], 2, 48_000, 16)); + assert_eq!(aiff_pcm(&pcm_aiff_header(1200, 2, 48_000, 16)), raw(&[], 2, 48_000, 16)); +} + +#[test] +fn files_cut_short_never_panic_and_give_what_is_there() { + let pcm: Vec = (0..48u8).collect(); + let w = pcm_wav_file(&pcm, 2, 48_000, 24); + let a = pcm_aiff_file(&pcm, 2, 44_100, 24); + for cut in 0..w.len().max(a.len()) { + if let Some(p) = w.get(..cut).and_then(wav_pcm) { + assert_eq!(Some(&p.data[..]), pcm.get(..cut.saturating_sub(44)), "wav cut at {cut}"); + } + if let Some(p) = a.get(..cut).and_then(aiff_pcm) { + assert!(pcm.starts_with(&p.data) && p.data.len() <= cut.saturating_sub(54), "aiff cut at {cut}"); + } + } + assert_eq!(wav_pcm(b"RIFF"), None); + assert_eq!(aiff_pcm(a.get(..30).unwrap()), None); +} + +#[test] +fn only_integer_pcm_is_taken() { + let buf = AudioBuffer::new(48_000, 2, 16); + let float = encode(&buf, &EncodeOptions::new(EncodeFormat::Wav).with_bit_depth(BitDepth::Float32)).unwrap(); + assert_eq!(wav_pcm(&float), None); + let float = encode(&buf, &EncodeOptions::new(EncodeFormat::Aiff).with_bit_depth(BitDepth::Float32)).unwrap(); + assert_eq!(aiff_pcm(&float), None); + // the full writer's 24-bit WAV (extensible `fmt `, a JUNK chunk before it) is integer PCM too + let int = encode(&buf, &EncodeOptions::new(EncodeFormat::Wav).with_bit_depth(BitDepth::Int24)).unwrap(); + assert_eq!(wav_pcm(&int), raw(&[0; 96], 2, 48_000, 24)); + let int = encode(&buf, &EncodeOptions::new(EncodeFormat::Aiff).with_bit_depth(BitDepth::Int16)).unwrap(); + assert_eq!(aiff_pcm(&int), raw(&[0; 64], 2, 48_000, 16)); + // zero channels or a zero rate is not a format + let mut bad = pcm_wav_file(&[0; 8], 1, 48_000, 16); + bad[24..28].copy_from_slice(&0u32.to_le_bytes()); + assert_eq!(wav_pcm(&bad), None); +} From 4f096b64d60e53c62a2e73d3a39652d75ccb5087 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:52:44 -0500 Subject: [PATCH 030/120] craft-aaf: AAF and OMF readers and writers over one composition model (from FilmCraft) (#918) * craft-audio: integer PCM of WAV and AIFF files bit for bit, and minimal files around it wav_pcm and aiff_pcm (feature decode-native) return a file's integer PCM as stored (RawPcm: little-endian interleaved bytes, 8-bit unsigned, with channels, rate and bits), built on the existing WAV and AIFF chunk parsers. pcm_wav_file, pcm_wav_header, pcm_aiff_file and pcm_aiff_header (feature pcm-encode) write the 44-byte canonical WAVE header and the plain AIFF header around such bytes. For essence embedded in AAF and OMF files, which is moved bit for bit and whose descriptors hold a bare header as a summary. The writers are FilmCraft's (filmcraft-interchange wav.rs); the readers replace its second WAV/AIFF parser with this crate's. * craft-timeline: exact media time, reports, media paths, ALE and CMX 3600 EDL New crate for editorial timeline interchange (#782), moved from FilmCraft at 4bec22bd: - time: filmcraft-time whole (Tick, FrameRate, TimeRange, SMPTE timecode, feet+frames, typed-timecode parsing) with its tests. - report: Report / Level / ReportEntry. - path: file URL and path conversion, relative references joined to a folder. - rate: timebase+ntsc and floating rates to exact rates, MAX_RATE. - ale: AleDoc, parse, write, standard heading and columns. - edl: the parser (events, lines, timecode fields, M2, AUD, comment extensions), the frame-rate guess, and the line layout of the writer, on frame counts. - Format, detect_text, decode_text. Each format module gives the document as the format writes it. Mapping onto an application's timeline stays in the application. * craft-timeline: strip a UTF-8 byte order mark when parsing ALE files An ALE saved with a BOM was refused as "not an ALE file": the mark made the first line something other than "Heading". Ported from storytold/filmcraft#4531. * craft-aaf: AAF (Edit Protocol) and OMF 2.0 readers and writers over one composition model New crate (#782), moved from FilmCraft's filmcraft-interchange at 4bec22bd: the mob-style model types of comp.rs, aaf/{ids,store,read,write}.rs and omf/{bento,read,write}.rs, with their structural and hostile-input tests rebuilt on model documents instead of FilmCraft projects. The container is craft-cfb, time and reports are craft-timeline's, and the WAVE/AIFF wrappers of embedded PCM are craft-audio's. The writer's identity (company, product, version, AAF product id) is passed in instead of being FilmCraft's. The private tags and properties FilmCraft wrote keep their names so existing files read back. The mapping between the model and an application's project stays in the application. --------- Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> --- Cargo.lock | 10 + Cargo.toml | 1 + README.md | 1 + crates/aaf/Cargo.toml | 18 + crates/aaf/README.md | 67 +++ crates/aaf/src/aaf.rs | 84 +++ crates/aaf/src/aaf/hostile_tests.rs | 110 ++++ crates/aaf/src/aaf/ids.rs | 262 +++++++++ crates/aaf/src/aaf/read.rs | 722 +++++++++++++++++++++++++ crates/aaf/src/aaf/store.rs | 538 +++++++++++++++++++ crates/aaf/src/aaf/tests.rs | 467 ++++++++++++++++ crates/aaf/src/aaf/write.rs | 800 ++++++++++++++++++++++++++++ crates/aaf/src/lib.rs | 60 +++ crates/aaf/src/model.rs | 185 +++++++ crates/aaf/src/omf.rs | 47 ++ crates/aaf/src/omf/bento.rs | 407 ++++++++++++++ crates/aaf/src/omf/read.rs | 438 +++++++++++++++ crates/aaf/src/omf/tests.rs | 135 +++++ crates/aaf/src/omf/write.rs | 419 +++++++++++++++ crates/aaf/src/pcm.rs | 11 + crates/aaf/src/roundtrip_tests.rs | 217 ++++++++ crates/aaf/src/testdoc.rs | 106 ++++ 22 files changed, 5105 insertions(+) create mode 100644 crates/aaf/Cargo.toml create mode 100644 crates/aaf/README.md create mode 100644 crates/aaf/src/aaf.rs create mode 100644 crates/aaf/src/aaf/hostile_tests.rs create mode 100644 crates/aaf/src/aaf/ids.rs create mode 100644 crates/aaf/src/aaf/read.rs create mode 100644 crates/aaf/src/aaf/store.rs create mode 100644 crates/aaf/src/aaf/tests.rs create mode 100644 crates/aaf/src/aaf/write.rs create mode 100644 crates/aaf/src/lib.rs create mode 100644 crates/aaf/src/model.rs create mode 100644 crates/aaf/src/omf.rs create mode 100644 crates/aaf/src/omf/bento.rs create mode 100644 crates/aaf/src/omf/read.rs create mode 100644 crates/aaf/src/omf/tests.rs create mode 100644 crates/aaf/src/omf/write.rs create mode 100644 crates/aaf/src/pcm.rs create mode 100644 crates/aaf/src/roundtrip_tests.rs create mode 100644 crates/aaf/src/testdoc.rs diff --git a/Cargo.lock b/Cargo.lock index c66f79209..36919285e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -842,6 +842,16 @@ dependencies = [ "libc", ] +[[package]] +name = "craft-aaf" +version = "0.1.0" +dependencies = [ + "craft-audio", + "craft-cfb", + "craft-timeline", + "thiserror 2.0.21", +] + [[package]] name = "craft-aep" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 9adfeca74..b77c65828 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ hayro-syntax = { version = "=0.7.2", default-features = false, features = ["std" bytemuck = "1" kurbo = "0.13" thiserror = "2" +craft-aaf = { path = "crates/aaf" } craft-atomic = { path = "crates/atomic" } craft-captions = { path = "crates/captions" } craft-cfb = { path = "crates/cfb" } diff --git a/README.md b/README.md index 58d0758db..87ccc7852 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " { + pub identity: Identity<'a>, + /// Write 512-byte sectors (compound file version 3) instead of 4096-byte sectors. + pub small_sectors: bool, +} + +/// Write `doc` as an AAF file: its first composition is the top-level one, with every nested +/// composition and every source it uses. Times that are not on whole edit units are rounded and +/// reported. +pub fn write(doc: &Document, opts: &WriteOptions<'_>, report: &mut Report) -> Result> { + let version = if opts.small_sectors { craft_cfb::Version::V3 } else { craft_cfb::Version::V4 }; + write::write(doc, version, &opts.identity, report) +} + +/// Read an AAF file: its top-level compositions, the compositions nested in them and their +/// sources. What cannot be represented is skipped and named in `report`. +pub fn read(bytes: &[u8], report: &mut Report) -> Result { + read::read(bytes, report) +} + +/// Whether `bytes` look like an AAF file (a compound file whose root has the AAF root class or a +/// `Header-2` storage). +pub fn sniff(bytes: &[u8]) -> bool { + if !craft_cfb::sniff(bytes) { + return false; + } + match craft_cfb::CompoundFile::open(bytes) { + Ok(cf) => cf.root().clsid == ids::ROOT || cf.find("Header-2").is_ok(), + Err(_) => false, + } +} + +#[cfg(test)] +mod hostile_tests; +#[cfg(test)] +mod tests; diff --git a/crates/aaf/src/aaf/hostile_tests.rs b/crates/aaf/src/aaf/hostile_tests.rs new file mode 100644 index 000000000..853b0000c --- /dev/null +++ b/crates/aaf/src/aaf/hostile_tests.rs @@ -0,0 +1,110 @@ +//! Hostile object trees: a crafted file must give an error, never a crash, a hang or an +//! allocation sized by a count in the file (AGENTS.md §0). + +use craft_cfb::{Version, Writer}; + +use super::store::{self, SF_STRONG_VECTOR, SF_WEAK_SET, SF_WEAK_VECTOR, Value, utf16z}; + +const PID: u16 = 0x0007; + +/// A root object whose one property `PID` is a weak reference vector or set (stored form `sf`) +/// named `Keys`, with `index` as its `Keys index` stream. +fn weak_collection_file(sf: u16, index: Vec) -> Vec { + let name = utf16z("Keys"); + // byte order 'L', format version, 1 entry { pid, stored form, value length }, the value + let mut props = vec![0x4C, 0x20]; + for x in [1, PID, sf, name.len() as u16] { + props.extend_from_slice(&x.to_le_bytes()); + } + props.extend_from_slice(&name); + let mut w = Writer::new(Version::V3); + w.stream(Writer::ROOT, "properties", props).unwrap(); + w.stream(Writer::ROOT, "Keys index", index).unwrap(); + w.finish() +} + +/// A weak collection index: u32 count, u16 tag, u16 key pid, u8 key size, then the keys. +fn index(count: u32, size: u8, keys: &[u8]) -> Vec { + let mut v = count.to_le_bytes().to_vec(); + v.extend_from_slice(&0u16.to_le_bytes()); + v.extend_from_slice(&0x1B01u16.to_le_bytes()); + v.push(size); + v.extend_from_slice(keys); + v +} + +fn read_keys(sf: u16, index: Vec) -> Result>, String> { + match store::read(&weak_collection_file(sf, index))?.get(PID) { + Some(Value::WeakVec(_, _, keys)) => Ok(keys.clone()), + other => panic!("not a weak collection: {other:?}"), + } +} + +/// With a key size of 0 every key fits, so the count alone drove the loop: up to 4.29 billion +/// empty keys (about 100 GB) from a 9-byte index. A million is enough to show it, cheaply. +#[test] +fn zero_size_weak_collection_keys_are_an_error() { + for sf in [SF_WEAK_VECTOR, SF_WEAK_SET] { + match read_keys(sf, index(1_000_000, 0, &[])) { + Err(e) => assert!(e.contains("zero-size"), "{e}"), + Ok(keys) => panic!("read {} empty keys instead of an error", keys.len()), + } + } +} + +#[test] +fn weak_collection_count_beyond_the_index_is_an_error() { + for sf in [SF_WEAK_VECTOR, SF_WEAK_SET] { + let e = read_keys(sf, index(u32::MAX, 4, &[1, 2, 3, 4, 5, 6, 7, 8])).unwrap_err(); + assert!(e.contains("short weak collection index"), "{e}"); + } +} + +#[test] +fn weak_collections_within_the_index_are_read() { + for sf in [SF_WEAK_VECTOR, SF_WEAK_SET] { + assert_eq!(read_keys(sf, index(0, 0, &[])).unwrap(), Vec::>::new()); + assert_eq!(read_keys(sf, index(2, 4, &[1, 2, 3, 4, 5, 6, 7, 8])).unwrap(), [vec![1, 2, 3, 4], vec![5, 6, 7, 8]]); + } +} + +/// A strong vector whose entries all name the same child storage would rebuild that subtree once +/// per entry; a storage has one owner, so the repeat is an error. +#[test] +fn a_storage_owned_twice_is_an_error() { + let name = utf16z("Items"); + let mut props = vec![0x4C, 0x20]; + for x in [1, PID, SF_STRONG_VECTOR, name.len() as u16] { + props.extend_from_slice(&x.to_le_bytes()); + } + props.extend_from_slice(&name); + // count, next free key, last key, then the entry keys + let mut idx = 3u32.to_le_bytes().to_vec(); + idx.extend_from_slice(&[0; 8]); + for _ in 0..3 { + idx.extend_from_slice(&1u32.to_le_bytes()); + } + let mut w = Writer::new(Version::V3); + w.stream(Writer::ROOT, "properties", props).unwrap(); + w.stream(Writer::ROOT, "Items index", idx).unwrap(); + let child = w.storage(Writer::ROOT, "Items{1}", [0; 16]).unwrap(); + w.stream(child, "properties", vec![0x4C, 0x20, 0, 0]).unwrap(); + let e = store::read(&w.finish()).unwrap_err(); + assert!(e.contains("more than one reference"), "{e}"); +} + +/// Objects nest through strong references; a chain deeper than the reader follows is an error, +/// and one within it reads. +#[test] +fn objects_nested_too_deeply_are_an_error() { + let chain = |depth: usize| { + let mut o = store::Obj::new([1; 16]); + for _ in 0..depth { + o = store::Obj::new([1; 16]).with(PID, Value::Strong(Box::new(o))); + } + store::write(&o, Version::V4).unwrap() + }; + assert!(store::read(&chain(150)).is_ok()); + let e = store::read(&chain(250)).unwrap_err(); + assert!(e.contains("nested too deeply"), "{e}"); +} diff --git a/crates/aaf/src/aaf/ids.rs b/crates/aaf/src/aaf/ids.rs new file mode 100644 index 000000000..70a184060 --- /dev/null +++ b/crates/aaf/src/aaf/ids.rs @@ -0,0 +1,262 @@ +//! Class ids, property ids and definition ids of the AAF object model subset read and +//! writes (AAF Object Specification v1.1, AAF Edit Protocol). Class and data definition AUIDs are +//! SMPTE labels (RP 224 / RP 210 registers) in AUID byte order; property ids are the AAF / ST 377-1 +//! local tags. + +use super::store::Auid; + +/// AUID of a SMPTE universal label: the label's last 8 bytes become Data1–Data3 (stored +/// little-endian), its first 8 bytes Data4. +pub(crate) const fn ul(b: [u8; 16]) -> Auid { + [b[11], b[10], b[9], b[8], b[13], b[12], b[15], b[14], b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]] +} + +/// A GUID `{d1-d2-d3-d4}` in AUID byte order. +pub(crate) const fn guid(d1: u32, d2: u16, d3: u16, d4: [u8; 8]) -> Auid { + let a = d1.to_le_bytes(); + let b = d2.to_le_bytes(); + let c = d3.to_le_bytes(); + [a[0], a[1], a[2], a[3], b[0], b[1], c[0], c[1], d4[0], d4[1], d4[2], d4[3], d4[4], d4[5], d4[6], d4[7]] +} + +/// Class AUID: SMPTE label 06.0E.2B.34.02.06.01.01.0D.01.01.01.01.01.xx.00. +pub(crate) const fn class(xx: u8) -> Auid { + ul([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x06, 0x01, 0x01, 0x0D, 0x01, 0x01, 0x01, 0x01, 0x01, xx, 0x00]) +} + +/// The root object's class (the storage class of the file's root). +pub(crate) const ROOT: Auid = guid(0xB3B398A5, 0x1C90, 0x11D4, [0x80, 0x53, 0x08, 0x00, 0x36, 0x21, 0x08, 0x04]); +/// The meta-dictionary (class 0x25 of the meta-model, 06.0E.2B.34.02.06.01.01.0D.01.01.01.02.25.00.00). +pub(crate) const META_DICTIONARY: Auid = ul([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x06, 0x01, 0x01, 0x0D, 0x01, 0x01, 0x01, 0x02, 0x25, 0x00, 0x00]); + +#[allow(dead_code)] +pub(crate) mod cls { + use super::class; + use crate::aaf::store::Auid; + pub const COMMENT_MARKER: Auid = class(0x08); + pub const FILLER: Auid = class(0x09); + pub const OPERATION_GROUP: Auid = class(0x0A); + pub const NESTED_SCOPE: Auid = class(0x0B); + pub const SCOPE_REFERENCE: Auid = class(0x0D); + pub const SELECTOR: Auid = class(0x0E); + pub const SEQUENCE: Auid = class(0x0F); + pub const SOURCE_CLIP: Auid = class(0x11); + pub const TIMECODE: Auid = class(0x14); + pub const TRANSITION: Auid = class(0x17); + pub const CONTENT_STORAGE: Auid = class(0x18); + pub const CONTROL_POINT: Auid = class(0x19); + pub const DATA_DEFINITION: Auid = class(0x1B); + pub const OPERATION_DEFINITION: Auid = class(0x1C); + pub const PARAMETER_DEFINITION: Auid = class(0x1D); + pub const CONTAINER_DEFINITION: Auid = class(0x20); + pub const INTERPOLATION_DEFINITION: Auid = class(0x21); + pub const DICTIONARY: Auid = class(0x22); + pub const ESSENCE_DATA: Auid = class(0x23); + pub const CDCI_DESCRIPTOR: Auid = class(0x28); + pub const TAPE_DESCRIPTOR: Auid = class(0x2E); + pub const HEADER: Auid = class(0x2F); + pub const IDENTIFICATION: Auid = class(0x30); + pub const NETWORK_LOCATOR: Auid = class(0x32); + pub const COMPOSITION_MOB: Auid = class(0x35); + pub const MASTER_MOB: Auid = class(0x36); + pub const SOURCE_MOB: Auid = class(0x37); + pub const EVENT_MOB_SLOT: Auid = class(0x39); + pub const TIMELINE_MOB_SLOT: Auid = class(0x3B); + pub const CONSTANT_VALUE: Auid = class(0x3D); + pub const VARYING_VALUE: Auid = class(0x3E); + pub const TAGGED_VALUE: Auid = class(0x3F); + pub const DESCRIPTIVE_MARKER: Auid = class(0x41); + pub const PCM_DESCRIPTOR: Auid = class(0x48); + pub const IMPORT_DESCRIPTOR: Auid = class(0x4A); + /// Classes with a `Locator` and media properties read as file descriptors. + pub const WAVE_DESCRIPTOR: Auid = class(0x2C); + pub const AIFC_DESCRIPTOR: Auid = class(0x26); + pub const SOUND_DESCRIPTOR: Auid = class(0x42); + pub const RGBA_DESCRIPTOR: Auid = class(0x29); + pub const MULTIPLE_DESCRIPTOR: Auid = class(0x44); +} + +/// Property ids. +#[allow(dead_code)] +pub(crate) mod pid { + // Root + pub const ROOT_META_DICTIONARY: u16 = 0x0001; + pub const ROOT_HEADER: u16 = 0x0002; + // MetaDictionary and MetaDefinition + pub const META_CLASS_DEFINITIONS: u16 = 0x0003; + pub const META_TYPE_DEFINITIONS: u16 = 0x0004; + pub const META_IDENTIFICATION: u16 = 0x0005; + // Header + pub const BYTE_ORDER: u16 = 0x3B01; + pub const LAST_MODIFIED: u16 = 0x3B02; + pub const CONTENT: u16 = 0x3B03; + pub const DICTIONARY: u16 = 0x3B04; + pub const VERSION: u16 = 0x3B05; + pub const IDENTIFICATION_LIST: u16 = 0x3B06; + pub const OBJECT_MODEL_VERSION: u16 = 0x3B07; + pub const OPERATIONAL_PATTERN: u16 = 0x3B09; + // Identification + pub const COMPANY_NAME: u16 = 0x3C01; + pub const PRODUCT_NAME: u16 = 0x3C02; + pub const PRODUCT_VERSION_STRING: u16 = 0x3C04; + pub const PRODUCT_ID: u16 = 0x3C05; + pub const DATE: u16 = 0x3C06; + pub const PLATFORM: u16 = 0x3C08; + pub const GENERATION_AUID: u16 = 0x3C09; + // ContentStorage + pub const MOBS: u16 = 0x1901; + pub const ESSENCE_DATA: u16 = 0x1902; + // Dictionary + pub const OPERATION_DEFINITIONS: u16 = 0x2603; + pub const PARAMETER_DEFINITIONS: u16 = 0x2604; + pub const DATA_DEFINITIONS: u16 = 0x2605; + pub const CONTAINER_DEFINITIONS: u16 = 0x2608; + pub const INTERPOLATION_DEFINITIONS: u16 = 0x2609; + // DefinitionObject + pub const IDENTIFICATION: u16 = 0x1B01; + pub const NAME: u16 = 0x1B02; + pub const DESCRIPTION: u16 = 0x1B03; + // OperationDefinition + pub const OPDEF_DATA_DEFINITION: u16 = 0x1E01; + pub const OPDEF_IS_TIME_WARP: u16 = 0x1E02; + pub const OPDEF_NUMBER_INPUTS: u16 = 0x1E07; + pub const OPDEF_PARAMETERS_DEFINED: u16 = 0x1E09; + // ParameterDefinition + pub const PARAMDEF_DISPLAY_UNITS: u16 = 0x1F03; + // Mob + pub const MOB_ID: u16 = 0x4401; + pub const MOB_NAME: u16 = 0x4402; + pub const SLOTS: u16 = 0x4403; + pub const MOB_LAST_MODIFIED: u16 = 0x4404; + pub const MOB_CREATION_TIME: u16 = 0x4405; + pub const MOB_USER_COMMENTS: u16 = 0x4406; + pub const USAGE_CODE: u16 = 0x4408; + // SourceMob + pub const ESSENCE_DESCRIPTION: u16 = 0x4701; + // MobSlot + pub const SLOT_ID: u16 = 0x4801; + pub const SLOT_NAME: u16 = 0x4802; + pub const SEGMENT: u16 = 0x4803; + pub const PHYSICAL_TRACK_NUMBER: u16 = 0x4804; + // TimelineMobSlot / EventMobSlot + pub const EDIT_RATE: u16 = 0x4B01; + pub const ORIGIN: u16 = 0x4B02; + pub const EVENT_EDIT_RATE: u16 = 0x4901; + // Component + pub const DATA_DEFINITION: u16 = 0x0201; + pub const LENGTH: u16 = 0x0202; + pub const COMPONENT_USER_COMMENTS: u16 = 0x0204; + // Sequence + pub const COMPONENTS: u16 = 0x1001; + // SourceReference / SourceClip + pub const SOURCE_ID: u16 = 0x1101; + pub const SOURCE_MOB_SLOT_ID: u16 = 0x1102; + pub const START_TIME: u16 = 0x1201; + // Event + pub const POSITION: u16 = 0x0601; + pub const COMMENT: u16 = 0x0602; + // Timecode + pub const TC_START: u16 = 0x1501; + pub const TC_FPS: u16 = 0x1502; + pub const TC_DROP: u16 = 0x1503; + // Transition + pub const OPERATION_GROUP: u16 = 0x1801; + pub const CUT_POINT: u16 = 0x1802; + // OperationGroup + pub const OPERATION: u16 = 0x0B01; + pub const INPUT_SEGMENTS: u16 = 0x0B02; + pub const PARAMETERS: u16 = 0x0B03; + // Selector / NestedScope + pub const SELECTED: u16 = 0x0F01; + pub const NESTED_SLOTS: u16 = 0x0C01; + // Parameter / ConstantValue / VaryingValue / ControlPoint + pub const PARAMETER_DEFINITION: u16 = 0x4C01; + pub const CONSTANT_VALUE: u16 = 0x4D01; + pub const INTERPOLATION: u16 = 0x4E01; + pub const POINT_LIST: u16 = 0x4E02; + pub const CP_VALUE: u16 = 0x1A02; + pub const CP_TIME: u16 = 0x1A03; + pub const CP_EDIT_HINT: u16 = 0x1A04; + // TaggedValue + pub const TAG_NAME: u16 = 0x5001; + pub const TAG_VALUE: u16 = 0x5003; + // EssenceDescriptor / FileDescriptor + pub const LOCATOR: u16 = 0x2F01; + pub const SAMPLE_RATE: u16 = 0x3001; + pub const FILE_LENGTH: u16 = 0x3002; + pub const CONTAINER_FORMAT: u16 = 0x3004; + pub const STORED_HEIGHT: u16 = 0x3202; + pub const STORED_WIDTH: u16 = 0x3203; + pub const FRAME_LAYOUT: u16 = 0x320C; + pub const VIDEO_LINE_MAP: u16 = 0x320D; + pub const IMAGE_ASPECT_RATIO: u16 = 0x320E; + pub const COMPONENT_WIDTH: u16 = 0x3301; + pub const HORIZONTAL_SUBSAMPLING: u16 = 0x3302; + pub const QUANTIZATION_BITS: u16 = 0x3D01; + pub const LOCKED: u16 = 0x3D02; + pub const AUDIO_SAMPLING_RATE: u16 = 0x3D03; + pub const CHANNELS: u16 = 0x3D07; + pub const AVERAGE_BPS: u16 = 0x3D09; + pub const BLOCK_ALIGN: u16 = 0x3D0A; + // NetworkLocator + pub const URL_STRING: u16 = 0x4001; + // EssenceData + pub const ESSENCE_MOB_ID: u16 = 0x2701; + pub const ESSENCE_STREAM: u16 = 0x2702; +} + +/// Data definitions (SMPTE RP 224 labels 06.0E.2B.34.04.01.01.01.01.03.02.xx.yy). +pub(crate) mod ddef { + use super::{guid, ul}; + use crate::aaf::store::Auid; + pub const PICTURE: Auid = ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x01, 0x03, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00]); + pub const SOUND: Auid = ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x01, 0x03, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00]); + pub const TIMECODE: Auid = ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x01, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00]); + pub const DESCRIPTIVE_METADATA: Auid = ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x01, 0x03, 0x02, 0x01, 0x10, 0x00, 0x00, 0x00]); + /// Pre-SMPTE ("legacy") data definitions written by older applications. + pub const LEGACY_PICTURE: Auid = guid(0x6F3C8CE1, 0x6CEF, 0x11D2, [0x80, 0x7D, 0x00, 0x60, 0x08, 0x14, 0x3E, 0x6F]); + pub const LEGACY_SOUND: Auid = guid(0x78E1EBE1, 0x6CEF, 0x11D2, [0x80, 0x7D, 0x00, 0x60, 0x08, 0x14, 0x3E, 0x6F]); + pub const LEGACY_TIMECODE: Auid = guid(0x7F275E81, 0x77E5, 0x11D2, [0x80, 0x7F, 0x00, 0x60, 0x08, 0x14, 0x3E, 0x6F]); +} + +/// Operation, parameter, interpolation and container definitions. +pub(crate) mod def { + use super::guid; + use crate::aaf::store::Auid; + const AV: [u8; 8] = [0x8A, 0x09, 0x00, 0x60, 0x08, 0x14, 0x3E, 0x6F]; + pub const VIDEO_DISSOLVE: Auid = guid(0x0C3BEA41, 0xFC05, 0x11D2, AV); + pub const VIDEO_FADE_TO_BLACK: Auid = guid(0x0C3BEA40, 0xFC05, 0x11D2, AV); + pub const SMPTE_VIDEO_WIPE: Auid = guid(0x0C3BEA43, 0xFC05, 0x11D2, AV); + pub const MONO_AUDIO_DISSOLVE: Auid = guid(0x0C3BEA44, 0xFC05, 0x11D2, AV); + const AG: [u8; 8] = [0x8A, 0x38, 0x00, 0x50, 0x04, 0x0E, 0xF7, 0xD2]; + pub const MONO_AUDIO_GAIN: Auid = guid(0x9D2EA893, 0x0968, 0x11D3, AG); + const PD: [u8; 8] = [0x8A, 0x4C, 0x00, 0x50, 0x04, 0x0E, 0xF7, 0xD2]; + pub const PARAM_LEVEL: Auid = guid(0xE4962320, 0x2267, 0x11D3, PD); + pub const PARAM_AMPLITUDE: Auid = guid(0xE4962321, 0x2267, 0x11D3, PD); + const IP: [u8; 8] = [0x80, 0xA9, 0x00, 0x60, 0x08, 0x14, 0x3E, 0x6F]; + pub const INTERP_LINEAR: Auid = guid(0x5B6C85A4, 0x0EDE, 0x11D3, IP); + pub const INTERP_CONSTANT: Auid = guid(0x5B6C85A5, 0x0EDE, 0x11D3, IP); + const CD: [u8; 8] = [0x80, 0x9B, 0x00, 0x60, 0x08, 0x14, 0x3E, 0x6F]; + pub const CONTAINER_AAF: Auid = guid(0x4313B571, 0xD8BA, 0x11D2, CD); + pub const CONTAINER_EXTERNAL: Auid = guid(0x4313B572, 0xD8BA, 0x11D2, CD); + /// Type ids of indirect values (SMPTE RP 210 type labels). + pub const TYPE_RATIONAL: Auid = super::ul([0x06, 0x0E, 0x2B, 0x34, 0x01, 0x04, 0x01, 0x01, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00]); + pub const TYPE_STRING: Auid = super::ul([0x06, 0x0E, 0x2B, 0x34, 0x01, 0x04, 0x01, 0x01, 0x01, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00]); + /// Edit Protocol operational pattern (`AAFEditProtocol`) and mob usage codes (`Usage_TopLevel`), + /// as in the SMPTE Labels register. + pub const OP_EDIT_PROTOCOL: Auid = super::ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x05, 0x0D, 0x01, 0x12, 0x01, 0x01, 0x00, 0x00, 0x00]); + pub const USAGE_TOP_LEVEL: Auid = super::ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0D, 0x01, 0x01, 0x02, 0x01, 0x01, 0x07, 0x00]); + /// The label FilmCraft 0.16 and earlier wrote as the top-level usage code (in no register). + /// Import reads it as top-level too. + pub const USAGE_TOP_LEVEL_OLD: Auid = super::ul([0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x05, 0x0D, 0x01, 0x12, 0x01, 0x02, 0x03, 0x00, 0x00]); +} + +/// Weak reference target paths (pids from the root object). +pub(crate) mod path { + use super::pid; + pub const DATA_DEFS: [u16; 3] = [pid::ROOT_HEADER, pid::DICTIONARY, pid::DATA_DEFINITIONS]; + pub const OPERATION_DEFS: [u16; 3] = [pid::ROOT_HEADER, pid::DICTIONARY, pid::OPERATION_DEFINITIONS]; + pub const PARAMETER_DEFS: [u16; 3] = [pid::ROOT_HEADER, pid::DICTIONARY, pid::PARAMETER_DEFINITIONS]; + pub const INTERPOLATION_DEFS: [u16; 3] = [pid::ROOT_HEADER, pid::DICTIONARY, pid::INTERPOLATION_DEFINITIONS]; + pub const CONTAINER_DEFS: [u16; 3] = [pid::ROOT_HEADER, pid::DICTIONARY, pid::CONTAINER_DEFINITIONS]; +} diff --git a/crates/aaf/src/aaf/read.rs b/crates/aaf/src/aaf/read.rs new file mode 100644 index 000000000..206c2ead3 --- /dev/null +++ b/crates/aaf/src/aaf/read.rs @@ -0,0 +1,722 @@ +//! AAF objects → composition model. + +use std::collections::{HashMap, HashSet}; + +use craft_timeline::time::{FrameRate, Tick}; + +use super::ids::{cls, ddef, def, pid}; +use super::store::{self, Auid, Obj, Value, rational_of, utf16_of}; +use crate::model::{CClip, CItem, CKind, CMarker, CTrack, CTransition, Composition, Document, Gain, Source, units_to_ticks}; +use crate::{Error, Report, Result}; + +type MobId = [u8; 32]; + +#[derive(Clone, Copy, PartialEq, Eq)] +enum DataKind { + Picture, + Sound, + Timecode, + Other, +} + +fn data_kind(o: &Obj) -> DataKind { + match o.weak_key(pid::DATA_DEFINITION).and_then(|k| ::try_from(k).ok()) { + Some(k) if k == ddef::PICTURE || k == ddef::LEGACY_PICTURE => DataKind::Picture, + Some(k) if k == ddef::SOUND || k == ddef::LEGACY_SOUND => DataKind::Sound, + Some(k) if k == ddef::TIMECODE || k == ddef::LEGACY_TIMECODE => DataKind::Timecode, + Some(_) => DataKind::Other, + None => DataKind::Other, + } +} + +fn len_of(o: &Obj) -> i64 { + o.i64(pid::LENGTH).unwrap_or(0).max(0) +} + +/// The value of an indirect property: (type id, bytes). +fn indirect(d: &[u8]) -> Option<(Auid, &[u8])> { + if d.len() >= 17 && (d[0] == 0x4C || d[0] == 0x42) { + return Some((d[1..17].try_into().ok()?, &d[17..])); + } + if d.len() >= 16 { + return Some((d[..16].try_into().ok()?, &d[16..])); + } + None +} + +fn tagged_values(o: &Obj, p: u16) -> Vec<(String, String)> { + o.objs(p) + .iter() + .filter_map(|t| { + let name = t.string(pid::TAG_NAME)?; + let (ty, v) = indirect(t.data(pid::TAG_VALUE)?)?; + let value = if ty == def::TYPE_STRING || v.len() % 2 == 0 { utf16_of(v) } else { String::new() }; + Some((name, value)) + }) + .collect() +} + +fn indirect_f64(d: &[u8]) -> Option { + let (_, v) = indirect(d)?; + match v.len() { + 8 => rational_of(v).filter(|r| r.1 != 0).map(|(n, d)| n as f64 / d as f64), + 4 => Some(i32::from_le_bytes(v.try_into().ok()?) as f64), + _ => None, + } +} + +struct R<'a> { + report: &'a mut Report, + mobs: HashMap, + essence: HashMap, + doc: Document, + /// (file mob id, slot id, master key) → source index + sources: HashMap<(MobId, u32, String), usize>, + /// Composition mobs read as nested compositions: their index in `doc.nested`. + nests: HashMap, + /// The compositions being read, outermost first. + open: Vec, +} + +struct NestClip<'a> { + mob_id: MobId, + slot_id: u32, + rate: (i64, i64), + kind: CKind, + name: String, + mob: &'a Obj, +} + +/// Compositions nested deeper than this are read as gaps. +const MAX_NESTING: usize = 16; + +pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { + let root = store::read(bytes).map_err(|e| Error::Parse { format: "AAF", message: e })?; + let header = root.strong(pid::ROOT_HEADER).ok_or_else(|| Error::Parse { format: "AAF", message: "no header object".into() })?; + let content = header.strong(pid::CONTENT).ok_or_else(|| Error::Parse { format: "AAF", message: "no content storage".into() })?; + let mut mobs = HashMap::new(); + for m in content.objs(pid::MOBS) { + if let Some(id) = m.mob_id(pid::MOB_ID) { + mobs.insert(id, m); + } + } + let mut essence = HashMap::new(); + for e in content.objs(pid::ESSENCE_DATA) { + if let (Some(id), Some(d)) = (e.mob_id(pid::ESSENCE_MOB_ID), e.stream(pid::ESSENCE_STREAM)) { + essence.insert(id, d); + } + } + let mut r = R { report, mobs, essence, doc: Document::default(), sources: HashMap::new(), nests: HashMap::new(), open: Vec::new() }; + // Top-level compositions: those no other composition uses, among the ones tagged top-level + // when any are. (Premiere Pro tags a nested sequence's composition top-level too; it is still + // only a nested sequence.) Compositions that only use each other in a circle all count. The + // label older FilmCraft versions wrote for top-level counts as top-level too. + let comps: Vec<&Obj> = content.objs(pid::MOBS).iter().filter(|m| m.class == cls::COMPOSITION_MOB).collect(); + let mut referenced = HashSet::new(); + for c in &comps { + collect_refs(c, &mut referenced); + } + let is_top = |c: &&Obj| c.auid(pid::USAGE_CODE).is_some_and(|u| u == def::USAGE_TOP_LEVEL || u == def::USAGE_TOP_LEVEL_OLD); + let tagged_top: Vec<&Obj> = comps.iter().copied().filter(is_top).collect(); + let pool = if tagged_top.is_empty() { comps } else { tagged_top }; + let unused: Vec<&Obj> = pool.iter().copied().filter(|c| c.mob_id(pid::MOB_ID).is_none_or(|id| !referenced.contains(&id))).collect(); + let top = if unused.is_empty() { pool } else { unused }; + for c in top { + r.open.extend(c.mob_id(pid::MOB_ID)); + let comp = r.composition(c); + r.open.clear(); + r.doc.compositions.push(comp); + } + if r.doc.compositions.is_empty() { + return Err(Error::Empty); + } + Ok(r.doc) +} + +fn collect_refs(o: &Obj, out: &mut HashSet) { + if o.class == cls::SOURCE_CLIP + && let Some(id) = o.mob_id(pid::SOURCE_ID) + { + out.insert(id); + } + for (_, v) in &o.props { + match v { + Value::Strong(c) => collect_refs(c, out), + Value::StrongVec(v) | Value::StrongSet(v, _) => v.iter().for_each(|c| collect_refs(c, out)), + _ => {} + } + } +} + +fn rate_of(o: &Obj, p: u16) -> Option<(i64, i64)> { + o.rational(p).filter(|r| r.0 > 0 && r.1 > 0) +} + +impl R<'_> { + fn composition(&mut self, c: &Obj) -> Composition { + let name = c.string(pid::MOB_NAME).unwrap_or_else(|| "Sequence".into()); + let slots = c.objs(pid::SLOTS); + let pic_rate = slots + .iter() + .filter(|s| s.class == cls::TIMELINE_MOB_SLOT) + .find(|s| s.strong(pid::SEGMENT).is_some_and(|g| matches!(data_kind(g), DataKind::Picture | DataKind::Timecode))) + .and_then(|s| rate_of(s, pid::EDIT_RATE)) + .or_else(|| slots.iter().find_map(|s| rate_of(s, pid::EDIT_RATE))) + .unwrap_or((25, 1)); + let rate = FrameRate::new(pic_rate.0, pic_rate.1); + let tags = tagged_values(c, pid::MOB_USER_COMMENTS); + let tag = |n: &str| tags.iter().find(|(k, _)| k == n).map(|(_, v)| v.clone()); + let (mut width, mut height) = tag("FilmCraft Frame Size") + .and_then(|s| s.split_once('x').and_then(|(a, b)| Some((a.trim().parse().ok()?, b.trim().parse().ok()?)))) + .unwrap_or((0, 0)); + let mut sample_rate = tag("FilmCraft Audio Sample Rate").and_then(|s| s.parse().ok()).unwrap_or(0u32); + let mut comp = Composition { + name, + rate, + sample_rate: 48_000, + width: 1920, + height: 1080, + start_tc: 0, + drop: false, + tracks: Vec::new(), + markers: Vec::new(), + }; + let (mut nv, mut na) = (0, 0); + for s in slots { + let Some(seg) = s.strong(pid::SEGMENT) else { continue }; + if s.class == cls::EVENT_MOB_SLOT { + let er = rate_of(s, pid::EVENT_EDIT_RATE).unwrap_or(pic_rate); + comp.markers.extend(markers_of(seg, er)); + continue; + } + let er = rate_of(s, pid::EDIT_RATE).unwrap_or(pic_rate); + let origin = s.i64(pid::ORIGIN).unwrap_or(0); + match data_kind(seg) { + DataKind::Timecode => { + if let Some(tc) = find_class(seg, cls::TIMECODE) { + comp.start_tc = tc.i64(pid::TC_START).unwrap_or(0); + comp.drop = tc.u8(pid::TC_DROP).unwrap_or(0) != 0; + } + } + kind @ (DataKind::Picture | DataKind::Sound) => { + let ckind = if kind == DataKind::Picture { CKind::Picture } else { CKind::Sound }; + if ckind == CKind::Sound && sample_rate == 0 && er.1 == 1 && er.0 >= 8000 { + sample_rate = er.0 as u32; + } + // Premiere Pro writes every video track into one slot: a nested scope with a + // segment per track, the lowest track first. (A scope whose last segment + // refers to the others is an effect over layers: one track, as before.) + let layers: Vec<&Obj> = if ckind == CKind::Picture && seg.class == cls::NESTED_SCOPE && !refers_to_scope(seg, 0) { + seg.objs(pid::NESTED_SLOTS).iter().collect() + } else { + Vec::new() + }; + let layered = !layers.is_empty(); + let whole = units_to_ticks(len_of(seg), er.0, er.1); + for layer in if layered { layers } else { vec![seg] } { + let mut items = self.segment(layer, er, ckind, 0); + if layered { + fit(&mut items, whole); + } + if origin > 0 { + // material before the origin is not shown + trim_front(&mut items, units_to_ticks(origin, er.0, er.1)); + } + let number = s.u32(pid::PHYSICAL_TRACK_NUMBER).unwrap_or(0); + let name = s.string(pid::SLOT_NAME).unwrap_or_default(); + let channels = if ckind == CKind::Sound { + items + .iter() + .filter_map(|i| match i { + CItem::Clip(c) => self.doc.sources.get(c.source).map(|s| s.channels), + _ => None, + }) + .max() + .unwrap_or(1) + .min(6) + } else { + 0 + }; + let n = if ckind == CKind::Picture { + nv += 1; + nv + } else { + na += 1; + na + }; + comp.tracks.push(CTrack { kind: ckind, name, number: if number > 0 && !layered { number } else { n }, channels, items }); + } + } + DataKind::Other => self.report.info("a slot of an unsupported data kind was skipped"), + } + } + if width == 0 { + // no size in the file: that of the first picture this composition shows, else of any + let own = comp.tracks.iter().filter(|t| t.kind == CKind::Picture).flat_map(|t| &t.items).find_map(|i| match i { + CItem::Clip(c) => self.doc.sources.get(c.source).filter(|s| s.kind == CKind::Picture && s.width > 0 && s.nested.is_none()), + _ => None, + }); + let pic = own.or_else(|| self.doc.sources.iter().find(|s| s.kind == CKind::Picture && s.width > 0)); + (width, height) = pic.map_or((1920, 1080), |p| (p.width, p.height)); + } + comp.width = width; + comp.height = height; + if sample_rate > 0 { + comp.sample_rate = sample_rate; + } + comp + } + + /// Items of a segment (a sequence or a single component). + fn segment(&mut self, seg: &Obj, rate: (i64, i64), kind: CKind, depth: usize) -> Vec { + if depth > 32 { + self.report.warn("segments nested too deeply were skipped"); + return vec![CItem::Filler(units_to_ticks(len_of(seg), rate.0, rate.1))]; + } + if seg.class == cls::SEQUENCE { + let mut out = Vec::new(); + for c in seg.objs(pid::COMPONENTS) { + out.extend(self.component(c, rate, kind, depth + 1)); + } + return out; + } + self.component(seg, rate, kind, depth + 1) + } + + fn component(&mut self, c: &Obj, rate: (i64, i64), kind: CKind, depth: usize) -> Vec { + let len = units_to_ticks(len_of(c), rate.0, rate.1); + match c.class { + x if x == cls::FILLER => vec![CItem::Filler(len)], + x if x == cls::SOURCE_CLIP => vec![self.clip(c, rate, kind, None)], + x if x == cls::SEQUENCE => self.segment(c, rate, kind, depth), + x if x == cls::TRANSITION => { + let cut = units_to_ticks(c.i64(pid::CUT_POINT).unwrap_or(0), rate.0, rate.1); + let og = c.strong(pid::OPERATION_GROUP); + let effect = og + .and_then(|o| tagged_values(o, pid::COMPONENT_USER_COMMENTS).into_iter().find(|(k, _)| k == "FilmCraft Effect").map(|(_, v)| v)) + .unwrap_or_else(|| { + let op = og.and_then(|o| o.weak_key(pid::OPERATION)).and_then(|k| ::try_from(k).ok()); + match (kind, op) { + (CKind::Sound, _) => "constant_power".into(), + (_, Some(o)) if o == def::VIDEO_FADE_TO_BLACK => "dip_to_black".into(), + (_, Some(o)) if o == def::SMPTE_VIDEO_WIPE => "wipe".into(), + _ => "cross_dissolve".into(), + } + }); + vec![CItem::Transition(CTransition { len, cut: cut.clamp(Tick::ZERO, len), effect })] + } + x if x == cls::OPERATION_GROUP => { + let op = c.weak_key(pid::OPERATION).and_then(|k| ::try_from(k).ok()); + let inputs = c.objs(pid::INPUT_SEGMENTS); + let Some(input) = inputs.first() else { + self.report.info("an effect without input was imported as a gap"); + return vec![CItem::Filler(len)]; + }; + let gain = if op == Some(def::MONO_AUDIO_GAIN) { + self.gain(c, len, rate) + } else { + self.report.warn("effects other than audio gain are not imported (their first input is used)"); + None + }; + let mut items = self.segment(input, rate, kind, depth); + // the operation's length rules + fit(&mut items, len); + if let Some(g) = gain { + match items.as_mut_slice() { + [CItem::Clip(cl)] => cl.gain = Some(g), + _ => self.report.warn("audio gain around several clips was not imported"), + } + } + items + } + x if x == cls::SELECTOR => match c.strong(pid::SELECTED) { + Some(s) => { + let mut items = self.segment(s, rate, kind, depth); + fit(&mut items, len); + items + } + None => vec![CItem::Filler(len)], + }, + x if x == cls::NESTED_SCOPE => match c.objs(pid::NESTED_SLOTS).last() { + Some(s) => { + let mut items = self.segment(s, rate, kind, depth); + fit(&mut items, len); + items + } + None => vec![CItem::Filler(len)], + }, + _ => { + self.report.info("an unsupported component was imported as a gap"); + vec![CItem::Filler(len)] + } + } + } + + fn gain(&mut self, og: &Obj, len: Tick, _rate: (i64, i64)) -> Option { + let p = og + .objs(pid::PARAMETERS) + .iter() + .find(|p| p.auid(pid::PARAMETER_DEFINITION) == Some(def::PARAM_AMPLITUDE)) + .or_else(|| og.objs(pid::PARAMETERS).first())?; + if p.class == cls::CONSTANT_VALUE { + return p.data(pid::CONSTANT_VALUE).and_then(indirect_f64).map(Gain::Constant); + } + if p.class == cls::VARYING_VALUE { + let linear = p.weak_key(pid::INTERPOLATION).and_then(|k| ::try_from(k).ok()) != Some(def::INTERP_CONSTANT); + let points: Vec<(Tick, f64)> = p + .objs(pid::POINT_LIST) + .iter() + .filter_map(|cp| { + let (n, d) = cp.rational(pid::CP_TIME).filter(|r| r.1 != 0)?; + let v = cp.data(pid::CP_VALUE).and_then(indirect_f64)?; + Some((Tick((len.0 as i128 * n as i128 / d as i128).clamp(-(1i128 << 56), 1i128 << 56) as i64), v)) + }) + .collect(); + return (!points.is_empty()).then_some(Gain::Varying { linear, points }); + } + None + } + + fn clip(&mut self, c: &Obj, rate: (i64, i64), kind: CKind, master_key: Option) -> CItem { + let len = units_to_ticks(len_of(c), rate.0, rate.1); + let start = units_to_ticks(c.i64(pid::START_TIME).unwrap_or(0), rate.0, rate.1); + let name = tagged_values(c, pid::COMPONENT_USER_COMMENTS).into_iter().find(|(k, _)| k == "Clip Name").map(|(_, v)| v).unwrap_or_default(); + let (Some(id), Some(slot)) = (c.mob_id(pid::SOURCE_ID), c.u32(pid::SOURCE_MOB_SLOT_ID)) else { return CItem::Filler(len) }; + if id == [0; 32] { + return CItem::Filler(len); + } + if let Some(mob) = self.mobs.get(&id).copied().filter(|m| m.class == cls::COMPOSITION_MOB) { + return self.nest_clip(c, NestClip { mob, mob_id: id, slot_id: slot, rate, kind, name }); + } + match self.resolve(id, slot, start, kind, master_key, 0) { + Some((source, t)) => CItem::Clip(CClip { len, source, start: t, gain: None, name }), + None => CItem::Filler(len), + } + } + + /// A source clip that points at a composition: a nested sequence (Premiere Pro writes its + /// nested sequences this way). The composition is read once, however many clips use it. + fn nest_clip(&mut self, c: &Obj, clip: NestClip<'_>) -> CItem { + let NestClip { mob, mob_id: id, slot_id, rate, kind, name } = clip; + let len = units_to_ticks(len_of(c), rate.0, rate.1); + let index = match self.nests.get(&id) { + Some(&i) => i, + None => { + if self.open.contains(&id) || self.open.len() >= MAX_NESTING { + self.report.warn("a composition nested in itself or nested too deeply was imported as a gap"); + return CItem::Filler(len); + } + self.open.push(id); + let comp = self.composition(mob); + self.open.pop(); + let i = self.doc.nested.len(); + self.doc.nested.push(comp); + self.nests.insert(id, i); + i + } + }; + // the start counts edit units of the slot the clip points at + let slot_rate = + mob.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(slot_id)).and_then(|s| rate_of(s, pid::EDIT_RATE)).unwrap_or(rate); + let start = units_to_ticks(c.i64(pid::START_TIME).unwrap_or(0), slot_rate.0, slot_rate.1); + let skey = (id, if kind == CKind::Picture { 0 } else { 1 }, "composition".to_string()); + let source = match self.sources.get(&skey) { + Some(&i) => i, + None => { + let Some(comp) = self.doc.nested.get(index) else { return CItem::Filler(len) }; + let length = comp + .tracks + .iter() + .map(|t| { + Tick(t.items.iter().fold(0i64, |a, i| { + if matches!(i, CItem::Transition(_)) { a.saturating_sub(i.len().0) } else { a.saturating_add(i.len().0) } + })) + }) + .max() + .unwrap_or(Tick::ZERO); + let channels = if kind == CKind::Sound { 2 } else { 0 }; + let s = Source { + key: format!("composition:{}", hex(&id)), + name: comp.name.clone(), + kind, + path: None, + channel: None, + channels, + file_channels: channels, + width: comp.width, + height: comp.height, + frame_rate: comp.rate, + sample_rate: comp.sample_rate, + bits: 16, + length, + offset: Tick::ZERO, + start_tc: None, + tc_rate: comp.rate, + embedded: None, + markers: Vec::new(), + nested: Some(index), + }; + let i = self.doc.sources.len(); + self.doc.sources.push(s); + self.sources.insert(skey, i); + i + } + }; + CItem::Clip(CClip { len, source, start, gain: None, name }) + } + + /// Follow a source reference to a file source mob: (source index, file time). + fn resolve(&mut self, id: MobId, slot_id: u32, start: Tick, kind: CKind, master_key: Option, depth: usize) -> Option<(usize, Tick)> { + if depth > 16 { + return None; + } + let Some(mob) = self.mobs.get(&id).copied() else { + self.report.warn("a clip references a mob that is not in the file (imported as a gap)"); + return None; + }; + let slot = mob.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(slot_id))?; + let rate = rate_of(slot, pid::EDIT_RATE).unwrap_or((25, 1)); + let seg = slot.strong(pid::SEGMENT)?; + if mob.class == cls::COMPOSITION_MOB { + self.report.warn("nested compositions are not supported (imported as gaps)"); + return None; + } + if mob.class == cls::MASTER_MOB { + let key = master_key.unwrap_or_else(|| hex(&id)); + let clip = first_clip(seg)?; + let inner = units_to_ticks(clip.i64(pid::START_TIME).unwrap_or(0), rate.0, rate.1); + let next = clip.mob_id(pid::SOURCE_ID)?; + let next_slot = clip.u32(pid::SOURCE_MOB_SLOT_ID)?; + if next == [0; 32] { + return None; + } + let markers = mob + .objs(pid::SLOTS) + .iter() + .filter(|s| s.class == cls::EVENT_MOB_SLOT) + .flat_map(|s| s.strong(pid::SEGMENT).map(|g| markers_of(g, rate_of(s, pid::EVENT_EDIT_RATE).unwrap_or(rate))).unwrap_or_default()) + .collect::>(); + let r = self.resolve(next, next_slot, start + inner, kind, Some(key), depth + 1)?; + let src = &mut self.doc.sources[r.0]; + if src.markers.is_empty() { + src.markers = markers; + } + if src.name.is_empty() + && let Some(n) = mob.string(pid::MOB_NAME) + { + src.name = n; + } + return Some(r); + } + // a source mob: file (has a file descriptor) or physical (tape / import) + let desc = mob.strong(pid::ESSENCE_DESCRIPTION); + let is_file = desc.is_some_and(|d| d.get(pid::SAMPLE_RATE).is_some() || d.get(pid::LOCATOR).is_some() || self.essence.contains_key(&id)); + if !is_file { + return None; + } + let key = master_key.unwrap_or_else(|| hex(&id)); + let skey = (id, slot_id, key.clone()); + if let Some(&i) = self.sources.get(&skey) { + return Some((i, start)); + } + let d = desc?; + let seg_kind = match data_kind(seg) { + DataKind::Picture => CKind::Picture, + DataKind::Sound => CKind::Sound, + _ => kind, + }; + let path = d.objs(pid::LOCATOR).iter().find_map(|l| l.string(pid::URL_STRING)).map(|u| craft_timeline::path::file_url_to_path(&u)); + let file_rate = rate_of(d, pid::SAMPLE_RATE).unwrap_or(rate); + let length = d + .i64(pid::FILE_LENGTH) + .map(|l| units_to_ticks(l, file_rate.0, file_rate.1)) + .unwrap_or_else(|| units_to_ticks(len_of(seg), rate.0, rate.1)); + let sound_slots = mob.objs(pid::SLOTS).iter().filter(|s| s.strong(pid::SEGMENT).is_some_and(|g| data_kind(g) == DataKind::Sound)).count(); + let file_channels = d.u32(pid::CHANNELS).unwrap_or(1).max(1); + let channel = (seg_kind == CKind::Sound && file_channels > 1 && sound_slots > 1) + .then(|| slot.u32(pid::PHYSICAL_TRACK_NUMBER).unwrap_or(slot_id).saturating_sub(1).min(file_channels - 1)); + let mut layout = d.u8(pid::FRAME_LAYOUT).unwrap_or(0); + if layout > 4 { + layout = 0; + } + let stored_h = d.u32(pid::STORED_HEIGHT).unwrap_or(0); + let fr = FrameRate::new(file_rate.0, file_rate.1); + // timecode from the physical source mob + let tc = first_clip(seg).and_then(|clip| { + let tid = clip.mob_id(pid::SOURCE_ID)?; + let tape = self.mobs.get(&tid)?; + let tslot = tape.objs(pid::SLOTS).iter().find_map(|s| { + let g = s.strong(pid::SEGMENT)?; + let t = find_class(g, cls::TIMECODE)?; + Some((t.i64(pid::TC_START).unwrap_or(0), t.u16(pid::TC_FPS).unwrap_or(25).max(1) as i64, rate_of(s, pid::EDIT_RATE))) + })?; + let off = units_to_ticks(clip.i64(pid::START_TIME).unwrap_or(0), rate.0, rate.1); + let tcr = tslot.2.map(|r| FrameRate::new(r.0, r.1)).unwrap_or_else(|| FrameRate::new(tslot.1, 1)); + Some((tslot.0 + tcr.frame_at(off), tcr)) + }); + let mut s = Source { + key, + name: mob.string(pid::MOB_NAME).unwrap_or_default(), + kind: seg_kind, + path, + channel, + channels: if channel.is_some() { 1 } else { file_channels }, + file_channels, + width: d.u32(pid::STORED_WIDTH).unwrap_or(0), + height: if matches!(layout, 1 | 3) { stored_h.saturating_mul(2) } else { stored_h }, + frame_rate: if seg_kind == CKind::Picture { fr } else { FrameRate::FPS_25 }, + sample_rate: d + .rational(pid::AUDIO_SAMPLING_RATE) + .filter(|r| r.1 > 0 && r.0 > 0) + .map(|r| (r.0 / r.1) as u32) + .unwrap_or(file_rate.0 as u32), + bits: d.u32(pid::QUANTIZATION_BITS).unwrap_or(16).clamp(8, 32) as u16, + length, + offset: Tick::ZERO, + start_tc: tc.map(|t| t.0), + tc_rate: tc.map(|t| t.1).unwrap_or(fr), + embedded: None, + markers: Vec::new(), + nested: None, + }; + if s.kind == CKind::Sound { + s.frame_rate = FrameRate::FPS_25; + if let Some(data) = self.essence.get(&id) { + s.embedded = Some(data.to_vec()); + // embedded essence may be a whole WAVE / AIFF file + if let Some((pcm, ch, sr, bits)) = crate::pcm::parse_wav(data) { + s.embedded = Some(pcm); + s.file_channels = ch as u32; + s.channels = if s.channel.is_some() { 1 } else { ch as u32 }; + s.sample_rate = sr; + s.bits = bits; + } + } + } + if s.kind == CKind::Picture && s.width == 0 { + s.width = 1920; + s.height = 1080; + } + let i = self.doc.sources.len(); + self.doc.sources.push(s); + self.sources.insert(skey, i); + Some((i, start)) + } +} + +/// Drop `t` from the front of a track's items (slot origin). +fn trim_front(items: &mut Vec, mut t: Tick) { + while t > Tick::ZERO && !items.is_empty() { + let l = items[0].len(); + if matches!(items[0], CItem::Transition(_)) { + items.remove(0); + continue; + } + if l <= t { + items.remove(0); + t -= l; + } else { + match &mut items[0] { + CItem::Filler(x) => *x -= t, + CItem::Clip(c) => { + c.len -= t; + c.start += t; + } + CItem::Transition(_) => {} + } + t = Tick::ZERO; + } + } +} + +/// Make the items exactly `len` long (an operation group / selector is as long as itself). +fn fit(items: &mut Vec, len: Tick) { + let total = Tick( + items.iter().fold(0i64, |a, i| if matches!(i, CItem::Transition(_)) { a.saturating_sub(i.len().0) } else { a.saturating_add(i.len().0) }), + ); + if total == len || items.is_empty() { + if items.is_empty() { + items.push(CItem::Filler(len)); + } + return; + } + if total < len { + items.push(CItem::Filler(len - total)); + return; + } + let mut excess = total - len; + while excess > Tick::ZERO { + match items.last_mut() { + Some(CItem::Filler(x)) | Some(CItem::Clip(CClip { len: x, .. })) if *x > excess => { + *x -= excess; + excess = Tick::ZERO; + } + Some(CItem::Transition(_)) => { + items.pop(); + } + Some(other) => { + excess -= other.len(); + items.pop(); + } + None => break, + } + } +} + +/// Whether anything inside `o` is a scope reference (a nested scope used as an effect's layers). +fn refers_to_scope(o: &Obj, depth: usize) -> bool { + if o.class == cls::SCOPE_REFERENCE { + return true; + } + if depth > 32 { + return false; + } + o.props.iter().any(|(_, v)| match v { + Value::Strong(c) => refers_to_scope(c, depth + 1), + Value::StrongVec(v) | Value::StrongSet(v, _) => v.iter().any(|c| refers_to_scope(c, depth + 1)), + _ => false, + }) +} + +fn first_clip(seg: &Obj) -> Option<&Obj> { + if seg.class == cls::SOURCE_CLIP { + return Some(seg); + } + if seg.class == cls::SEQUENCE { + return seg.objs(pid::COMPONENTS).iter().find_map(first_clip); + } + // essence group (choices 0x0501) / operation group inputs + seg.objs(0x0501).iter().find_map(first_clip).or_else(|| seg.objs(pid::INPUT_SEGMENTS).iter().find_map(first_clip)) +} + +fn find_class(seg: &Obj, class: Auid) -> Option<&Obj> { + if seg.class == class { + return Some(seg); + } + seg.objs(pid::COMPONENTS).iter().find_map(|c| find_class(c, class)) +} + +fn markers_of(seg: &Obj, rate: (i64, i64)) -> Vec { + let events: Vec<&Obj> = if seg.class == cls::SEQUENCE { seg.objs(pid::COMPONENTS).iter().collect() } else { vec![seg] }; + events + .into_iter() + .filter(|e| e.class == cls::COMMENT_MARKER || e.class == cls::DESCRIPTIVE_MARKER || e.get(pid::POSITION).is_some()) + .map(|e| { + let tags = tagged_values(e, pid::COMPONENT_USER_COMMENTS); + let tag = |n: &str| tags.iter().find(|(k, _)| k == n).map(|(_, v)| v.clone()); + let comment = e.string(pid::COMMENT).unwrap_or_default(); + let start = units_to_ticks(e.i64(pid::POSITION).unwrap_or(0), rate.0, rate.1); + CMarker { + start, + duration: units_to_ticks(e.i64(pid::LENGTH).unwrap_or(0).max(0), rate.0, rate.1), + name: tag("Name").unwrap_or_else(|| comment.clone()), + comment: tag("Comment").unwrap_or_else(|| if tags.is_empty() { String::new() } else { comment.clone() }), + color: tag("Color"), + } + }) + .collect() +} + +fn hex(b: &[u8]) -> String { + b.iter().map(|x| format!("{x:02x}")).collect() +} diff --git a/crates/aaf/src/aaf/store.rs b/crates/aaf/src/aaf/store.rs new file mode 100644 index 000000000..46ab95dae --- /dev/null +++ b/crates/aaf/src/aaf/store.rs @@ -0,0 +1,538 @@ +//! AAF objects in structured storage (the AAF Low-Level Container Specification): every object +//! is a storage whose class id is the object's class AUID, holding a `properties` stream; strong +//! references are child storages, collections have an index stream, weak references name a key +//! in a target collection through the root's `referenced properties` table. +//! +//! `properties` stream (little-endian): +//! +//! ```text +//! u8 byte order (0x4C 'L') u8 format version (0x20) u16 entry count +//! entry count × { u16 pid, u16 stored form, u16 value length } the values, in entry order +//! ``` +//! +//! | stored form | value | +//! |---|---| +//! | 0x82 data | the property's bytes | +//! | 0x22 strong reference | UTF-16 name of the child storage | +//! | 0x32 strong reference vector | UTF-16 collection name; storages `name{key}`, stream `name index`: u32 count, u32 first free key, u32 last free key, count × u32 local key | +//! | 0x3A strong reference set | as a vector; the index adds u16 key pid, u8 key size, and each entry is u32 local key, u32 reference count, key bytes | +//! | 0x02 weak reference | u16 referenced-property tag, u16 key pid, u8 key size, key bytes | +//! | 0x12 / 0x1A weak reference vector / set | UTF-16 collection name; stream `name index`: u32 count, u16 tag, u16 key pid, u8 key size, count × key | +//! | 0x42 data stream | u8 byte order, UTF-16 name of the stream | +//! +//! `referenced properties` (root stream): u8 byte order, u16 path count, u32 pid count, then the +//! pids of each path (from the root object) terminated by a 0 pid. + +use craft_cfb::{CompoundFile, EntryKind, NodeId, Version, Writer}; + +pub(crate) type Auid = [u8; 16]; + +pub(crate) const SF_DATA: u16 = 0x82; +pub(crate) const SF_DATA_STREAM: u16 = 0x42; +pub(crate) const SF_STRONG: u16 = 0x22; +pub(crate) const SF_STRONG_VECTOR: u16 = 0x32; +pub(crate) const SF_STRONG_SET: u16 = 0x3A; +pub(crate) const SF_WEAK: u16 = 0x02; +pub(crate) const SF_WEAK_VECTOR: u16 = 0x12; +pub(crate) const SF_WEAK_SET: u16 = 0x1A; + +const BYTE_ORDER_LE: u8 = 0x4C; +const FORMAT_VERSION: u8 = 0x20; + +/// A property value. +#[derive(Clone, Debug, PartialEq)] +pub(crate) enum Value { + Data(Vec), + Strong(Box), + StrongVec(Vec), + /// Objects keyed by the data property `key_pid` of each. + StrongSet(Vec, u16), + /// (path of pids from the root to the target collection, key pid, key) + Weak(Vec, u16, Vec), + WeakVec(Vec, u16, Vec>), + Stream(Vec), +} + +/// An AAF object: its class and properties. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct Obj { + pub class: Auid, + pub props: Vec<(u16, Value)>, +} + +impl Obj { + pub fn new(class: Auid) -> Obj { + Obj { class, props: Vec::new() } + } + pub fn set(&mut self, pid: u16, v: Value) -> &mut Obj { + match self.props.iter_mut().find(|(p, _)| *p == pid) { + Some(slot) => slot.1 = v, + None => self.props.push((pid, v)), + } + self + } + pub fn with(mut self, pid: u16, v: Value) -> Obj { + self.set(pid, v); + self + } + pub fn data_prop(mut self, pid: u16, d: Vec) -> Obj { + self.set(pid, Value::Data(d)); + self + } + pub fn get(&self, pid: u16) -> Option<&Value> { + self.props.iter().find(|(p, _)| *p == pid).map(|(_, v)| v) + } + pub fn data(&self, pid: u16) -> Option<&[u8]> { + match self.get(pid)? { + Value::Data(d) => Some(d), + _ => None, + } + } + pub fn strong(&self, pid: u16) -> Option<&Obj> { + match self.get(pid)? { + Value::Strong(o) => Some(o), + _ => None, + } + } + /// Elements of a strong vector or set (empty when absent). + pub fn objs(&self, pid: u16) -> &[Obj] { + match self.get(pid) { + Some(Value::StrongVec(v)) | Some(Value::StrongSet(v, _)) => v, + _ => &[], + } + } + pub fn weak_key(&self, pid: u16) -> Option<&[u8]> { + match self.get(pid)? { + Value::Weak(_, _, k) => Some(k), + _ => None, + } + } + pub fn stream(&self, pid: u16) -> Option<&[u8]> { + match self.get(pid)? { + Value::Stream(d) => Some(d), + _ => None, + } + } + pub fn u8(&self, pid: u16) -> Option { + self.data(pid).and_then(|d| d.first().copied()) + } + pub fn u16(&self, pid: u16) -> Option { + self.data(pid).filter(|d| d.len() >= 2).map(|d| u16::from_le_bytes([d[0], d[1]])) + } + pub fn u32(&self, pid: u16) -> Option { + self.data(pid).filter(|d| d.len() >= 4).map(|d| u32::from_le_bytes([d[0], d[1], d[2], d[3]])) + } + pub fn i64(&self, pid: u16) -> Option { + let d = self.data(pid)?; + match d.len() { + 8.. => Some(i64::from_le_bytes(d[..8].try_into().ok()?)), + 4..=7 => Some(i32::from_le_bytes(d[..4].try_into().ok()?) as i64), + _ => None, + } + } + pub fn rational(&self, pid: u16) -> Option<(i64, i64)> { + self.data(pid).and_then(rational_of) + } + pub fn string(&self, pid: u16) -> Option { + self.data(pid).map(utf16_of) + } + pub fn auid(&self, pid: u16) -> Option { + self.data(pid).and_then(|d| d.get(..16)).and_then(|d| d.try_into().ok()) + } + pub fn mob_id(&self, pid: u16) -> Option<[u8; 32]> { + self.data(pid).and_then(|d| d.get(..32)).and_then(|d| d.try_into().ok()) + } +} + +pub(crate) fn rational_of(d: &[u8]) -> Option<(i64, i64)> { + (d.len() >= 8).then(|| (i32::from_le_bytes([d[0], d[1], d[2], d[3]]) as i64, i32::from_le_bytes([d[4], d[5], d[6], d[7]]) as i64)) +} + +pub(crate) fn rational(num: i64, den: i64) -> Vec { + let mut v = (num.clamp(i32::MIN as i64, i32::MAX as i64) as i32).to_le_bytes().to_vec(); + v.extend_from_slice(&(den.clamp(i32::MIN as i64, i32::MAX as i64) as i32).to_le_bytes()); + v +} + +/// UTF-16LE with a terminating NUL. +pub(crate) fn utf16z(s: &str) -> Vec { + let mut v: Vec = s.encode_utf16().flat_map(u16::to_le_bytes).collect(); + v.extend_from_slice(&[0, 0]); + v +} + +pub(crate) fn utf16_of(d: &[u8]) -> String { + let u: Vec = d.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).take_while(|&c| c != 0).collect(); + String::from_utf16_lossy(&u) +} + +/// Short property names used to name storages and streams (any name is valid: readers follow +/// the names stored in the property values). +fn prop_name(pid: u16) -> &'static str { + match pid { + 0x0001 => "MetaDictionary", + 0x0002 => "Header", + 0x0003 => "ClassDefinitions", + 0x0004 => "TypeDefinitions", + 0x3B03 => "Content", + 0x3B04 => "Dictionary", + 0x3B06 => "IdentificationList", + 0x1901 => "Mobs", + 0x1902 => "EssenceData", + 0x2603 => "OperationDefinitions", + 0x2604 => "ParameterDefinitions", + 0x2605 => "DataDefinitions", + 0x2607 => "CodecDefinitions", + 0x2608 => "ContainerDefinitions", + 0x2609 => "InterpolationDefs", + 0x4403 => "Slots", + 0x4406 => "UserComments", + 0x4803 => "Segment", + 0x1001 => "Components", + 0x1801 => "OperationGroup", + 0x0B02 => "InputSegments", + 0x0B03 => "Parameters", + 0x4E02 => "PointList", + 0x4701 => "EssenceDescription", + 0x2F01 => "Locator", + 0x2702 => "Data", + 0x0204 => "ComponentComments", + 0x1E09 => "ParametersDefined", + _ => "Property", + } +} + +/// The key size of a strong reference set keyed by `key_pid`, for a set with no entry to take it +/// from: AUIDs (definitions) are 16 bytes, MobIDs 32. +fn set_key_size(key_pid: u16) -> usize { + match key_pid { + 0x0005 | 0x1B01 => 16, + 0x4401 | 0x2701 => 32, + _ => 0, + } +} + +fn storage_name(pid: u16) -> String { + let suffix = format!("-{pid:x}"); + let name: String = prop_name(pid).chars().take(22 - suffix.len()).collect(); + format!("{name}{suffix}") +} + +/// Errors while reading the object tree. +fn bad(s: impl Into) -> String { + s.into() +} + +// --------------------------------------------------------------------------------------------- +// Writing +// --------------------------------------------------------------------------------------------- + +struct W { + cfb: Writer, + paths: Vec>, +} + +/// Serialise `root` (the Root object: MetaDictionary 0x0001, Header 0x0002) into a compound file. +pub(crate) fn write(root: &Obj, version: Version) -> Result, String> { + let mut w = W { cfb: Writer::new(version), paths: Vec::new() }; + w.obj(Writer::ROOT, root)?; + let mut rp = vec![BYTE_ORDER_LE]; + rp.extend_from_slice(&(w.paths.len() as u16).to_le_bytes()); + let n: usize = w.paths.iter().map(|p| p.len() + 1).sum(); + rp.extend_from_slice(&(n as u32).to_le_bytes()); + for p in &w.paths { + for pid in p { + rp.extend_from_slice(&pid.to_le_bytes()); + } + rp.extend_from_slice(&0u16.to_le_bytes()); + } + w.cfb.stream(Writer::ROOT, "referenced properties", rp).map_err(|e| e.to_string())?; + Ok(w.cfb.finish()) +} + +impl W { + fn tag(&mut self, path: &[u16]) -> u16 { + match self.paths.iter().position(|p| p == path) { + Some(i) => i as u16, + None => { + self.paths.push(path.to_vec()); + (self.paths.len() - 1) as u16 + } + } + } + + fn obj(&mut self, node: NodeId, o: &Obj) -> Result<(), String> { + self.cfb.set_clsid(node, o.class); + let mut entries: Vec<(u16, u16, Vec)> = Vec::new(); + for (pid, v) in &o.props { + let pid = *pid; + let (sf, value) = match v { + Value::Data(d) => (SF_DATA, d.clone()), + Value::Strong(child) => { + let name = storage_name(pid); + let s = self.cfb.storage(node, &name, child.class).map_err(|e| e.to_string())?; + self.obj(s, child)?; + (SF_STRONG, utf16z(&name)) + } + Value::StrongVec(items) | Value::StrongSet(items, _) => { + let name = storage_name(pid); + let mut index = Vec::new(); + index.extend_from_slice(&(items.len() as u32).to_le_bytes()); + index.extend_from_slice(&(items.len() as u32).to_le_bytes()); // first free key + index.extend_from_slice(&u32::MAX.to_le_bytes()); // last free key + let set_key = if let Value::StrongSet(_, k) = v { Some(*k) } else { None }; + let key_size = set_key.map(|k| items.first().and_then(|i| i.data(k)).map_or_else(|| set_key_size(k), <[u8]>::len)).unwrap_or(0); + if let Some(k) = set_key { + index.extend_from_slice(&k.to_le_bytes()); + index.push(key_size as u8); + } + for (i, item) in items.iter().enumerate() { + let s = self.cfb.storage(node, &format!("{name}{{{i:x}}}"), item.class).map_err(|e| e.to_string())?; + self.obj(s, item)?; + index.extend_from_slice(&(i as u32).to_le_bytes()); + if let Some(k) = set_key { + index.extend_from_slice(&1u32.to_le_bytes()); + let mut key = item.data(k).unwrap_or(&[]).to_vec(); + key.resize(key_size, 0); + index.extend_from_slice(&key); + } + } + self.cfb.stream(node, &format!("{name} index"), index).map_err(|e| e.to_string())?; + (if set_key.is_some() { SF_STRONG_SET } else { SF_STRONG_VECTOR }, utf16z(&name)) + } + Value::Weak(path, key_pid, key) => { + let mut d = self.tag(path).to_le_bytes().to_vec(); + d.extend_from_slice(&key_pid.to_le_bytes()); + d.push(key.len() as u8); + d.extend_from_slice(key); + (SF_WEAK, d) + } + Value::WeakVec(path, key_pid, keys) => { + let name = storage_name(pid); + let tag = self.tag(path); + let size = keys.first().map_or(0, Vec::len); + let mut index = (keys.len() as u32).to_le_bytes().to_vec(); + index.extend_from_slice(&tag.to_le_bytes()); + index.extend_from_slice(&key_pid.to_le_bytes()); + index.push(size as u8); + for k in keys { + let mut k = k.clone(); + k.resize(size, 0); + index.extend_from_slice(&k); + } + self.cfb.stream(node, &format!("{name} index"), index).map_err(|e| e.to_string())?; + (SF_WEAK_VECTOR, utf16z(&name)) + } + Value::Stream(data) => { + let name = storage_name(pid); + self.cfb.stream(node, &name, data.clone()).map_err(|e| e.to_string())?; + let mut d = vec![BYTE_ORDER_LE]; + d.extend_from_slice(&utf16z(&name)); + (SF_DATA_STREAM, d) + } + }; + if value.len() > u16::MAX as usize { + return Err(format!("property {pid:#06x} is {} bytes (the limit is 65535)", value.len())); + } + entries.push((pid, sf, value)); + } + let mut props = vec![BYTE_ORDER_LE, FORMAT_VERSION]; + props.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (pid, sf, v) in &entries { + props.extend_from_slice(&pid.to_le_bytes()); + props.extend_from_slice(&sf.to_le_bytes()); + props.extend_from_slice(&(v.len() as u16).to_le_bytes()); + } + for (_, _, v) in &entries { + props.extend_from_slice(v); + } + self.cfb.stream(node, "properties", props).map_err(|e| e.to_string())?; + Ok(()) + } +} + +// --------------------------------------------------------------------------------------------- +// Reading +// --------------------------------------------------------------------------------------------- + +struct R<'a> { + cf: CompoundFile<'a>, + paths: Vec>, + objects: usize, + materialized_stream_bytes: usize, + /// Storages already read: a storage is owned by one parent, so a second strong reference to it + /// (which would rebuild its whole subtree again) is refused. + owned: std::collections::HashSet, + /// Bytes of weak-reference target paths copied into the tree so far. + path_bytes: usize, +} + +const MAX_DEPTH: usize = 200; +const MAX_OBJECTS: usize = 2_000_000; +const MAX_MATERIALIZED_STREAM_BYTES: usize = 256 << 20; +/// Each weak reference carries its own copy of its target path (real paths are a handful of +/// property ids); the copies together may not exceed this. +const MAX_PATH_BYTES: usize = 64 << 20; + +/// Parse the object tree of an AAF compound file (the root object). +pub(crate) fn read(bytes: &[u8]) -> Result { + let cf = CompoundFile::open(bytes).map_err(|e| e.to_string())?; + let mut paths = Vec::new(); + if let Ok(rp) = cf.read_path("referenced properties") + && rp.len() >= 7 + { + let count = u16::from_le_bytes([rp[1], rp[2]]) as usize; + let mut cur = Vec::new(); + for c in rp[7..].as_chunks::<2>().0 { + let pid = u16::from_le_bytes([c[0], c[1]]); + if pid == 0 { + paths.push(std::mem::take(&mut cur)); + if paths.len() == count { + break; + } + } else { + cur.push(pid); + } + } + } + let mut r = R { cf, paths, objects: 0, materialized_stream_bytes: 0, owned: Default::default(), path_bytes: 0 }; + r.obj(0, 0) +} + +fn rd_u16(b: &[u8], at: usize) -> Option { + b.get(at..at + 2).map(|s| u16::from_le_bytes([s[0], s[1]])) +} +fn rd_u32(b: &[u8], at: usize) -> Option { + b.get(at..at + 4).map(|s| u32::from_le_bytes([s[0], s[1], s[2], s[3]])) +} + +impl R<'_> { + fn child(&self, parent: usize, name: &str) -> Result { + self.cf.child(parent, name).ok_or_else(|| bad(format!("missing storage or stream \"{name}\""))) + } + + fn stream(&mut self, parent: usize, name: &str) -> Result, String> { + let id = self.child(parent, name)?; + let data = self.cf.read(id).map_err(|e| e.to_string())?; + self.materialized_stream_bytes = self + .materialized_stream_bytes + .checked_add(data.len()) + .filter(|&n| n <= MAX_MATERIALIZED_STREAM_BYTES) + .ok_or_else(|| bad("AAF materialized stream data exceeds the memory limit"))?; + Ok(data) + } + + /// A copy of weak-reference path `tag`, counted against [`MAX_PATH_BYTES`]. + fn path(&mut self, tag: u16) -> Result, String> { + let path = self.paths.get(tag as usize).cloned().unwrap_or_default(); + self.path_bytes = self.path_bytes.saturating_add(path.len() * 2); + if self.path_bytes > MAX_PATH_BYTES { + return Err(bad("weak reference paths exceed the memory limit")); + } + Ok(path) + } + + fn obj(&mut self, entry: usize, depth: usize) -> Result { + if depth > MAX_DEPTH { + return Err(bad("objects nested too deeply")); + } + if !self.owned.insert(entry) { + return Err(bad("a storage is owned by more than one reference")); + } + self.objects += 1; + if self.objects > MAX_OBJECTS { + return Err(bad("too many objects")); + } + let e = self.cf.entry(entry).ok_or_else(|| bad("bad directory entry"))?; + if !matches!(e.kind, EntryKind::Root | EntryKind::Storage) { + return Err(bad(format!("\"{}\" is not a storage", e.name))); + } + let mut o = Obj::new(e.clsid); + let props = self.stream(entry, "properties")?; + if props.len() < 4 { + return Err(bad("short properties stream")); + } + if props[0] != BYTE_ORDER_LE { + return Err(bad("big-endian AAF files are not supported")); + } + let n = rd_u16(&props, 2).unwrap_or(0) as usize; + let mut at = 4 + n * 6; + if at > props.len() { + return Err(bad("truncated property index")); + } + for i in 0..n { + let (Some(pid), Some(sf), Some(len)) = (rd_u16(&props, 4 + i * 6), rd_u16(&props, 6 + i * 6), rd_u16(&props, 8 + i * 6)) else { + return Err(bad("truncated property index")); + }; + if o.props.iter().any(|(previous, _)| *previous == pid) { + return Err(bad("duplicate property id")); + } + let v = props.get(at..at + len as usize).ok_or_else(|| bad("truncated property value"))?; + at += len as usize; + let value = match sf { + SF_STRONG => { + let c = self.child(entry, &utf16_of(v))?; + Value::Strong(Box::new(self.obj(c, depth + 1)?)) + } + SF_STRONG_VECTOR | SF_STRONG_SET => { + let name = utf16_of(v); + let idx = self.stream(entry, &format!("{name} index"))?; + let count = rd_u32(&idx, 0).ok_or_else(|| bad("short collection index"))? as usize; + let mut items = Vec::new(); + if sf == SF_STRONG_VECTOR { + for k in 0..count { + let key = rd_u32(&idx, 12 + k * 4).ok_or_else(|| bad("short vector index"))?; + let c = self.child(entry, &format!("{name}{{{key:x}}}"))?; + items.push(self.obj(c, depth + 1)?); + } + Value::StrongVec(items) + } else { + let key_pid = rd_u16(&idx, 12).ok_or_else(|| bad("short set index"))?; + let key_size = *idx.get(14).ok_or_else(|| bad("short set index"))? as usize; + let stride = 8 + key_size; + for k in 0..count { + let key = rd_u32(&idx, 15 + k * stride).ok_or_else(|| bad("short set index"))?; + let c = self.child(entry, &format!("{name}{{{key:x}}}"))?; + items.push(self.obj(c, depth + 1)?); + } + Value::StrongSet(items, key_pid) + } + } + SF_WEAK => { + let (Some(tag), Some(key_pid), Some(&size)) = (rd_u16(v, 0), rd_u16(v, 2), v.get(4)) else { + return Err(bad("short weak reference")); + }; + let key = v.get(5..5 + size as usize).ok_or_else(|| bad("short weak reference"))?.to_vec(); + Value::Weak(self.path(tag)?, key_pid, key) + } + SF_WEAK_VECTOR | SF_WEAK_SET => { + let name = utf16_of(v); + let idx = self.stream(entry, &format!("{name} index"))?; + let (Some(count), Some(tag), Some(key_pid), Some(&size)) = (rd_u32(&idx, 0), rd_u16(&idx, 4), rd_u16(&idx, 6), idx.get(8)) else { + return Err(bad("short weak collection index")); + }; + let size = size as usize; + if count > 0 && size == 0 { + return Err(bad("zero-size weak collection key")); + } + // every key must lie in the index, so its length (not the count) bounds the loop + if (count as usize).checked_mul(size).and_then(|n| n.checked_add(9)).is_none_or(|end| end > idx.len()) { + return Err(bad("short weak collection index")); + } + let mut keys = Vec::new(); + for k in 0..count as usize { + keys.push(idx.get(9 + k * size..9 + (k + 1) * size).ok_or_else(|| bad("short weak collection index"))?.to_vec()); + } + Value::WeakVec(self.path(tag)?, key_pid, keys) + } + SF_DATA_STREAM => { + let name = utf16_of(v.get(1..).unwrap_or(&[])); + Value::Stream(self.stream(entry, &name)?) + } + _ => Value::Data(v.to_vec()), + }; + o.props.push((pid, value)); + } + Ok(o) + } +} diff --git a/crates/aaf/src/aaf/tests.rs b/crates/aaf/src/aaf/tests.rs new file mode 100644 index 000000000..2773fda6d --- /dev/null +++ b/crates/aaf/src/aaf/tests.rs @@ -0,0 +1,467 @@ +//! Structural checks of written AAF files against the required properties and rules of the AAF +//! Object Specification v1.1 and the Edit Protocol, read back through the compound file reader. + +use std::collections::HashSet; + +use craft_timeline::time::FrameRate; + +use super::ids::{self, cls, ddef, def, path, pid}; +use super::store::{self, Obj, Value}; +use super::*; +use crate::model::{CItem, CKind}; +use crate::testdoc::{IDENTITY, clip, composition, picture_and_sound_cut, source, track}; + +fn document() -> Document { + // two 48-frame clips from source frames 10 and 200, a 12-frame transition + picture_and_sound_cut(FrameRate::FPS_24, 48, 12, (10, 200)) +} + +fn written(doc: &Document, small_sectors: bool) -> Vec { + let mut report = Report::default(); + let bytes = write(doc, &WriteOptions { identity: IDENTITY, small_sectors }, &mut report).unwrap(); + assert!(!report.has_warnings(), "{report}"); + bytes +} + +fn read_clean(bytes: &[u8]) -> Document { + let mut report = Report::default(); + let doc = read(bytes, &mut report).unwrap(); + assert!(!report.has_warnings(), "{report}"); + doc +} + +fn walk<'a>(o: &'a Obj, out: &mut Vec<&'a Obj>) { + out.push(o); + for (_, v) in &o.props { + match v { + Value::Strong(c) => walk(c, out), + Value::StrongVec(v) | Value::StrongSet(v, _) => v.iter().for_each(|c| walk(c, out)), + _ => {} + } + } +} + +fn keys(dict: &Obj, p: u16) -> HashSet> { + dict.objs(p).iter().filter_map(|d| d.data(pid::IDENTIFICATION).map(<[u8]>::to_vec)).collect() +} + +/// filmcraft#323: readers that load the meta-dictionary (pyaaf2) need its ClassDefinitions and +/// TypeDefinitions sets. They are empty: everything written uses the baseline classes and types +/// those readers already know. +#[test] +fn the_meta_dictionary_has_its_class_and_type_definition_sets() { + for small in [false, true] { + let bytes = written(&document(), small); + let cf = craft_cfb::CompoundFile::open(&bytes).unwrap(); + assert_eq!(cf.entries[cf.find("MetaDictionary-1").unwrap()].clsid, ids::META_DICTIONARY); + for name in ["ClassDefinitions-3", "TypeDefinitions-4"] { + // u32 count, u32 first free key, u32 last free key, u16 key pid, u8 key size + let index = cf.read_path(&format!("MetaDictionary-1/{name} index")).unwrap_or_else(|e| panic!("{name}: {e}")); + assert_eq!(index.len(), 15, "{name}: an empty set"); + assert_eq!(index[0..4], 0u32.to_le_bytes(), "{name}: no entries"); + assert_eq!(u16::from_le_bytes([index[12], index[13]]), pid::META_IDENTIFICATION, "{name}: keyed by the definition's AUID"); + assert_eq!(index[14], 16, "{name}: 16-byte keys"); + } + let root = store::read(&bytes).unwrap(); + let meta = root.strong(pid::ROOT_META_DICTIONARY).unwrap(); + for set in [pid::META_CLASS_DEFINITIONS, pid::META_TYPE_DEFINITIONS] { + assert!(matches!(meta.get(set), Some(Value::StrongSet(items, k)) if items.is_empty() && *k == pid::META_IDENTIFICATION), "{set:#06x}"); + } + } +} + +#[test] +fn written_files_have_the_required_structure() { + for small in [false, true] { + let bytes = written(&document(), small); + assert_written_file(&bytes, small); + } +} + +/// The top-level composition is tagged `Usage_TopLevel`, which the SMPTE Labels register gives as +/// urn:smpte:ul:060e2b34.04010101.0d010102.01010700, stored as the AUID +/// {0d010102-0101-0700-060e-2b3404010101}. Readers that look for it (LibAAF) find no sequence +/// without it. +#[test] +fn the_top_level_composition_has_the_registered_usage_code() { + let bytes = written(&document(), false); + let root = store::read(&bytes).unwrap(); + let content = root.strong(pid::ROOT_HEADER).unwrap().strong(pid::CONTENT).unwrap(); + let comp = content.objs(pid::MOBS).iter().find(|m| m.class == cls::COMPOSITION_MOB).unwrap(); + let usage = [0x02, 0x01, 0x01, 0x0D, 0x01, 0x01, 0x00, 0x07, 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01]; + assert_eq!(comp.data(pid::USAGE_CODE), Some(&usage[..])); +} + +/// Files from FilmCraft 0.16 and earlier tag the top-level composition with the unregistered label +/// 060e2b34.04010105.0d011201.02030000. They still import: the composition no other one uses is +/// the edit. +#[test] +fn files_with_the_old_usage_code_still_import() { + let bytes = written(&document(), false); + let mut root = store::read(&bytes).unwrap(); + let content = strong_mut(strong_mut(&mut root, pid::ROOT_HEADER), pid::CONTENT); + for mob in objs_mut(content, pid::MOBS).iter_mut().filter(|m| m.class == cls::COMPOSITION_MOB) { + mob.set(pid::USAGE_CODE, Value::Data(def::USAGE_TOP_LEVEL_OLD.to_vec())); + } + let old_file = store::write(&root, craft_cfb::Version::V4).unwrap(); + let got = read_clean(&old_file); + assert_eq!(got.compositions.len(), 1); + assert_eq!(got, read_clean(&bytes)); +} + +/// A file that holds one edit tagged with each label (a tool merged a composition from an older +/// file into a newer one) imports both edits. +#[test] +fn edits_tagged_with_either_usage_code_both_import() { + let bytes = written(&document(), false); + let mut root = store::read(&bytes).unwrap(); + let content = strong_mut(strong_mut(&mut root, pid::ROOT_HEADER), pid::CONTENT); + let mobs = objs_mut(content, pid::MOBS); + let mut old = mobs.iter().find(|m| m.class == cls::COMPOSITION_MOB).unwrap().clone(); + let mut id = old.mob_id(pid::MOB_ID).unwrap(); + id[31] ^= 0xFF; + old.set(pid::MOB_ID, Value::Data(id.to_vec())); + old.set(pid::MOB_NAME, Value::Data(store::utf16z("Old"))); + old.set(pid::USAGE_CODE, Value::Data(def::USAGE_TOP_LEVEL_OLD.to_vec())); + mobs.push(old); + let mixed = store::write(&root, craft_cfb::Version::V4).unwrap(); + let mut names: Vec = read_clean(&mixed).compositions.into_iter().map(|c| c.name).collect(); + names.sort(); + assert_eq!(names, ["Old", "S"]); +} + +fn assert_written_file(bytes: &[u8], small: bool) { + assert_compound_file(bytes, small); + let root = store::read(bytes).unwrap(); + assert!(root.strong(pid::ROOT_META_DICTIONARY).is_some()); + let header = root.strong(pid::ROOT_HEADER).unwrap(); + let content = assert_header(header); + let dict = header.strong(pid::DICTIONARY).unwrap(); + let definitions = Definitions { + data: keys(dict, pid::DATA_DEFINITIONS), + operations: keys(dict, pid::OPERATION_DEFINITIONS), + interpolation: keys(dict, pid::INTERPOLATION_DEFINITIONS), + containers: keys(dict, pid::CONTAINER_DEFINITIONS), + parameters: keys(dict, pid::PARAMETER_DEFINITIONS), + }; + let mobs = content.objs(pid::MOBS); + let comp = assert_mob_set(content, mobs); + for mob in mobs { + assert_mob(mob, mobs, &definitions); + } + assert_composition(comp); +} + +fn assert_compound_file(bytes: &[u8], small: bool) { + let cf = craft_cfb::CompoundFile::open(bytes).unwrap(); + assert_eq!(cf.version, if small { 3 } else { 4 }); + assert_eq!(cf.root().clsid, ids::ROOT); + let references = cf.read_path("referenced properties").unwrap(); + let pids: Vec = references[7..].as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).collect(); + assert!(pids.windows(3).any(|w| w == path::DATA_DEFS)); + let header = cf.find("Header-2").unwrap(); + assert_eq!(cf.entries[header].clsid, cls::HEADER); + assert!(cf.read_path("Header-2/properties").unwrap().starts_with(&[0x4C, 0x20])); +} + +fn assert_header(header: &Obj) -> &Obj { + for req in [pid::BYTE_ORDER, pid::LAST_MODIFIED, pid::CONTENT, pid::DICTIONARY, pid::VERSION, pid::IDENTIFICATION_LIST, pid::OBJECT_MODEL_VERSION] + { + assert!(header.get(req).is_some(), "Header property {req:#06x}"); + } + assert_eq!(header.auid(pid::OPERATIONAL_PATTERN), Some(def::OP_EDIT_PROTOCOL)); + let ident = &header.objs(pid::IDENTIFICATION_LIST)[0]; + for req in [pid::COMPANY_NAME, pid::PRODUCT_NAME, pid::PRODUCT_VERSION_STRING, pid::PRODUCT_ID, pid::DATE, pid::GENERATION_AUID] { + assert!(ident.get(req).is_some(), "Identification property {req:#06x}"); + } + header.strong(pid::CONTENT).unwrap() +} + +struct Definitions { + data: HashSet>, + operations: HashSet>, + interpolation: HashSet>, + containers: HashSet>, + parameters: HashSet>, +} + +fn assert_mob_set<'a>(content: &Obj, mobs: &'a [Obj]) -> &'a Obj { + let mob_ids: Vec<[u8; 32]> = mobs.iter().map(|m| m.mob_id(pid::MOB_ID).unwrap()).collect(); + assert_eq!(mob_ids.iter().collect::>().len(), mob_ids.len(), "unique mob ids"); + assert!(matches!(content.get(pid::MOBS), Some(Value::StrongSet(_, k)) if *k == pid::MOB_ID)); + let compositions: Vec<&Obj> = mobs.iter().filter(|m| m.class == cls::COMPOSITION_MOB).collect(); + assert_eq!(compositions.len(), 1); + assert_eq!(compositions[0].auid(pid::USAGE_CODE), Some(def::USAGE_TOP_LEVEL)); + assert_eq!(mobs.iter().filter(|m| m.class == cls::MASTER_MOB).count(), 1); + assert_eq!(mobs.iter().filter(|m| m.class == cls::SOURCE_MOB).count(), 3, "picture file, sound file, tape"); + compositions[0] +} + +fn assert_mob(mob: &Obj, mobs: &[Obj], definitions: &Definitions) { + for req in [pid::MOB_ID, pid::MOB_NAME, pid::SLOTS, pid::MOB_LAST_MODIFIED, pid::MOB_CREATION_TIME] { + assert!(mob.get(req).is_some(), "Mob property {req:#06x}"); + } + assert_source_descriptor(mob, &definitions.containers); + for slot in mob.objs(pid::SLOTS) { + assert!(slot.get(pid::SLOT_ID).is_some() && slot.get(pid::SEGMENT).is_some()); + if slot.class == cls::TIMELINE_MOB_SLOT { + assert!(slot.rational(pid::EDIT_RATE).is_some_and(|r| r.0 > 0 && r.1 > 0)); + assert!(slot.get(pid::ORIGIN).is_some()); + } + } + let mut all = Vec::new(); + walk(mob, &mut all); + for component in all { + assert_component(component, mobs, definitions); + } +} + +fn assert_source_descriptor(mob: &Obj, containers: &HashSet>) { + if mob.class != cls::SOURCE_MOB { + return; + } + let descriptor = mob.strong(pid::ESSENCE_DESCRIPTION).expect("SourceMob::EssenceDescription"); + if descriptor.class != cls::TAPE_DESCRIPTOR { + for req in [pid::SAMPLE_RATE, pid::FILE_LENGTH, pid::CONTAINER_FORMAT] { + assert!(descriptor.get(req).is_some(), "FileDescriptor property {req:#06x}"); + } + assert!(containers.contains(descriptor.weak_key(pid::CONTAINER_FORMAT).unwrap())); + let url = descriptor.objs(pid::LOCATOR)[0].string(pid::URL_STRING).unwrap(); + assert_eq!(url, "file:///m/a.mov"); + } + if descriptor.class == cls::PCM_DESCRIPTOR { + assert_eq!(descriptor.u32(pid::CHANNELS), Some(2)); + assert_eq!(descriptor.u32(pid::QUANTIZATION_BITS), Some(24)); + assert_eq!(descriptor.u16(pid::BLOCK_ALIGN), Some(6)); + } +} + +fn assert_component(o: &Obj, mobs: &[Obj], definitions: &Definitions) { + if is_component(o) { + let dd = o.weak_key(pid::DATA_DEFINITION).expect("Component::DataDefinition"); + assert!(definitions.data.contains(dd), "data definition in the dictionary"); + let event_seq = o.class == cls::SEQUENCE && dd == ddef::DESCRIPTIVE_METADATA; + assert!(event_seq || o.get(pid::LENGTH).is_some(), "Component::Length"); + } + if o.class == cls::SOURCE_CLIP { + assert_source_clip(o, mobs); + } else if o.class == cls::OPERATION_GROUP { + assert_operation_group(o, definitions); + } else if o.class == cls::SEQUENCE && o.get(pid::LENGTH).is_some() { + assert_sequence_length(o); + } +} + +fn is_component(o: &Obj) -> bool { + [cls::FILLER, cls::SOURCE_CLIP, cls::SEQUENCE, cls::TRANSITION, cls::OPERATION_GROUP, cls::TIMECODE, cls::COMMENT_MARKER].contains(&o.class) +} + +fn assert_source_clip(clip: &Obj, mobs: &[Obj]) { + let id = clip.mob_id(pid::SOURCE_ID).unwrap(); + let slot = clip.u32(pid::SOURCE_MOB_SLOT_ID).unwrap(); + assert!(clip.get(pid::START_TIME).is_some()); + if id != [0; 32] { + let target = mobs.iter().find(|m| m.mob_id(pid::MOB_ID) == Some(id)).expect("source clip target in the file"); + assert!(target.objs(pid::SLOTS).iter().any(|s| s.u32(pid::SLOT_ID) == Some(slot)), "target slot exists"); + } +} + +fn assert_operation_group(group: &Obj, definitions: &Definitions) { + assert!(definitions.operations.contains(group.weak_key(pid::OPERATION).unwrap())); + for parameter in group.objs(pid::PARAMETERS) { + assert!(definitions.parameters.contains(parameter.auid(pid::PARAMETER_DEFINITION).unwrap().as_slice())); + if parameter.class == cls::VARYING_VALUE { + assert!(definitions.interpolation.contains(parameter.weak_key(pid::INTERPOLATION).unwrap())); + assert!(!parameter.objs(pid::POINT_LIST).is_empty()); + } + } +} + +fn assert_sequence_length(sequence: &Obj) { + let components = sequence.objs(pid::COMPONENTS); + let sum = components + .iter() + .enumerate() + .map(|(i, component)| { + let len = component.i64(pid::LENGTH).unwrap(); + if component.class == cls::TRANSITION { + assert!(i > 0 && i + 1 < components.len(), "transition at a sequence end"); + assert!(components[i - 1].class != cls::TRANSITION && components[i + 1].class != cls::TRANSITION); + assert!(components[i - 1].i64(pid::LENGTH).unwrap() >= len && components[i + 1].i64(pid::LENGTH).unwrap() >= len); + let cut = component.i64(pid::CUT_POINT).unwrap(); + assert!((0..=len).contains(&cut)); + assert!(component.strong(pid::OPERATION_GROUP).is_some()); + -len + } else { + len + } + }) + .sum::(); + assert_eq!(sum, sequence.i64(pid::LENGTH).unwrap(), "sequence length = segments − transitions"); +} + +fn assert_composition(comp: &Obj) { + let slots = comp.objs(pid::SLOTS); + let tc = slots[0].strong(pid::SEGMENT).unwrap(); + assert_eq!(tc.class, cls::TIMECODE); + assert_eq!(tc.u16(pid::TC_FPS), Some(24)); + let video = slots[1].strong(pid::SEGMENT).unwrap().objs(pid::COMPONENTS); + assert_eq!(video.iter().map(|c| c.class).collect::>(), vec![cls::SOURCE_CLIP, cls::TRANSITION, cls::SOURCE_CLIP]); + assert_eq!(video[0].i64(pid::LENGTH), Some(54), "outgoing clip extends to the transition end"); + assert_eq!(video[2].i64(pid::LENGTH), Some(54), "incoming clip starts at the transition start"); + assert_eq!(video[2].i64(pid::START_TIME), Some(194)); + assert_eq!(video[1].i64(pid::CUT_POINT), Some(6)); + assert_eq!(video[1].strong(pid::OPERATION_GROUP).unwrap().weak_key(pid::OPERATION), Some(&def::VIDEO_DISSOLVE[..])); + assert_audio_composition_slot(&slots[2]); +} + +fn assert_audio_composition_slot(slot: &Obj) { + let audio = slot.strong(pid::SEGMENT).unwrap().objs(pid::COMPONENTS); + assert_eq!(slot.rational(pid::EDIT_RATE), Some((48_000, 1))); + assert_eq!(audio[2].class, cls::OPERATION_GROUP); + assert_eq!(audio[2].weak_key(pid::OPERATION), Some(&def::MONO_AUDIO_GAIN[..])); + let amplitude = audio[2].objs(pid::PARAMETERS)[0].data(pid::CONSTANT_VALUE).unwrap(); + let (numerator, denominator) = store::rational_of(&litude[17..]).unwrap(); + assert!((numerator as f64 / denominator as f64 - 10f64.powf(-3.0 / 20.0)).abs() < 1e-4); + assert_eq!(audio[1].strong(pid::OPERATION_GROUP).unwrap().weak_key(pid::OPERATION), Some(&def::MONO_AUDIO_DISSOLVE[..])); +} + +#[test] +fn store_round_trips_every_stored_form() { + let leaf = |n: u8| Obj::new([n; 16]).data_prop(0x1B01, vec![n; 16]); + let root = Obj::new(ids::ROOT) + .data_prop(0x0003, vec![1, 2, 3]) + .with(0x0004, Value::Strong(Box::new(leaf(1).with(0x0005, Value::StrongVec(vec![leaf(2), leaf(3)]))))) + .with( + 0x0002, + Value::Strong(Box::new( + Obj::new([9; 16]) + .with(0x3B04, Value::Strong(Box::new(Obj::new([8; 16]).with(0x2605, Value::StrongSet(vec![leaf(4), leaf(5)], 0x1B01))))), + )), + ) + .with(0x0006, Value::Weak(path::DATA_DEFS.to_vec(), 0x1B01, vec![5; 16])) + .with(0x0007, Value::WeakVec(path::DATA_DEFS.to_vec(), 0x1B01, vec![vec![4; 16], vec![5; 16]])) + .with(0x0008, Value::Stream((0..10_000u32).map(|i| i as u8).collect())); + for v in [craft_cfb::Version::V3, craft_cfb::Version::V4] { + let bytes = store::write(&root, v).unwrap(); + assert_eq!(store::read(&bytes).unwrap(), root); + } + // a data value longer than a property can hold is an error, not a panic + assert!(store::write(&Obj::new(ids::ROOT).data_prop(1, vec![0; 70_000]), craft_cfb::Version::V4).is_err()); +} + +/// The property `p` of `o` as a list of objects, to change. +fn objs_mut(o: &mut Obj, p: u16) -> &mut Vec { + match o.props.iter_mut().find(|(k, _)| *k == p).map(|(_, v)| v) { + Some(Value::StrongVec(v)) | Some(Value::StrongSet(v, _)) => v, + _ => panic!("no object list {p:#06x}"), + } +} + +fn strong_mut(o: &mut Obj, p: u16) -> &mut Obj { + match o.props.iter_mut().find(|(k, _)| *k == p).map(|(_, v)| v) { + Some(Value::Strong(v)) => v, + _ => panic!("no object {p:#06x}"), + } +} + +fn is_picture(o: &Obj) -> bool { + o.weak_key(pid::DATA_DEFINITION) == Some(&ddef::PICTURE[..]) +} + +/// How Premiere Pro 26.5.2 lays a sequence with a nested sequence out in AAF (seen in a file it +/// exported): every composition is tagged top-level, the nested one too; all video tracks of a +/// composition are in ONE slot, a nested scope with a segment per track, lowest first; a clip of +/// the nested sequence is a source clip that points at its composition. Our own file is rewritten +/// into that layout and must read back as the same edit. +#[test] +fn premiere_style_layers_and_nested_compositions_are_read() { + let r = FrameRate::FPS_24; + // "Outer": media on V1 and V3, the sequence "S" nested on V2 with its sound on A1 + let inner = document(); + let mut sources = inner.sources.clone(); + for kind in [CKind::Picture, CKind::Sound] { + let mut nest = source("seq:2", "S", kind, r, 16, None); + (nest.path, nest.nested, nest.length, nest.width, nest.height) = (None, Some(0), r.tick_of(96), 1920, 1080); + sources.push(nest); + } + let gap = |frames: i64| CItem::Filler(r.tick_of(frames)); + let outer = composition( + "Outer", + r, + vec![ + track(CKind::Picture, "V1", 1, vec![clip(r, 0, 5, 30, "a")]), + track(CKind::Picture, "V2", 2, vec![gap(10), clip(r, 2, 12, 40, "S")]), + track(CKind::Picture, "V3", 3, vec![gap(60), clip(r, 0, 100, 20, "a")]), + track(CKind::Sound, "A1", 1, vec![gap(10), clip(r, 3, 12, 40, "S")]), + ], + ); + let ours = written(&Document { compositions: vec![outer], sources, nested: inner.compositions }, false); + + let mut root = store::read(&ours).unwrap(); + let content = strong_mut(strong_mut(&mut root, pid::ROOT_HEADER), pid::CONTENT); + let mut compositions = 0; + for mob in objs_mut(content, pid::MOBS).iter_mut().filter(|m| m.class == cls::COMPOSITION_MOB) { + compositions += 1; + mob.set(pid::USAGE_CODE, Value::Data(def::USAGE_TOP_LEVEL.to_vec())); + let slots = objs_mut(mob, pid::SLOTS); + let picture: Vec = slots.iter().enumerate().filter(|(_, s)| s.strong(pid::SEGMENT).is_some_and(is_picture)).map(|(i, _)| i).collect(); + let layers: Vec = picture.iter().map(|&i| slots[i].strong(pid::SEGMENT).unwrap().clone()).collect(); + let length = layers.iter().filter_map(|l| l.i64(pid::LENGTH)).max().unwrap_or(0); + let scope = Obj::new(cls::NESTED_SCOPE) + .with(pid::DATA_DEFINITION, Value::Weak(path::DATA_DEFS.to_vec(), pid::IDENTIFICATION, ddef::PICTURE.to_vec())) + .data_prop(pid::LENGTH, length.to_le_bytes().to_vec()) + .with(pid::NESTED_SLOTS, Value::StrongVec(layers)); + // the first video slot keeps its id (clips of the nested sequence point at it) and holds + // every layer; the other video slots go + slots[picture[0]].set(pid::SEGMENT, Value::Strong(Box::new(scope))); + for &i in picture[1..].iter().rev() { + slots.remove(i); + } + } + assert_eq!(compositions, 2); + let theirs = store::write(&root, craft_cfb::Version::V4).unwrap(); + assert_ne!(theirs, ours); + + let want = read_clean(&ours); + let got = read_clean(&theirs); + // one edit; the nested composition is one it uses, not a second edit + assert_eq!(got.compositions.len(), 1); + assert_eq!(got.nested.len(), 1); + // layers of one slot share its name and are all as long as the scope: the same edit apart + // from picture track names and gaps after the last clip + let edit = |mut doc: Document| { + for t in doc.compositions.iter_mut().chain(&mut doc.nested).flat_map(|c| &mut c.tracks).filter(|t| t.kind == CKind::Picture) { + t.name.clear(); + while matches!(t.items.last(), Some(CItem::Filler(_))) { + t.items.pop(); + } + } + doc + }; + assert_eq!(edit(got.clone()), edit(want)); + // and that edit is the one that was written: three video tracks, the nest on the second + let q = &got.compositions[0]; + let picture: Vec<_> = q.tracks.iter().filter(|t| t.kind == CKind::Picture).collect(); + assert_eq!(picture.iter().map(|t| t.items.iter().filter(|i| matches!(i, CItem::Clip(_))).count()).collect::>(), [1, 1, 1]); + let nest_of = |kind: CKind| { + let t = q.tracks.iter().filter(|t| t.kind == kind).nth(if kind == CKind::Picture { 1 } else { 0 }).unwrap(); + let [CItem::Filler(gap), CItem::Clip(c), ..] = &t.items[..] else { panic!("{:?}", t.items) }; + (*gap, c.len, c.start, got.sources[c.source].nested, got.sources[c.source].name.clone()) + }; + assert_eq!(nest_of(CKind::Picture), (r.tick_of(10), r.tick_of(40), r.tick_of(12), Some(0), "S".to_string())); + assert_eq!(nest_of(CKind::Sound), nest_of(CKind::Picture)); + assert_eq!(got.nested[0].name, "S"); +} + +/// A slot's name is the track's name, whatever it starts with; a track without one reads back +/// without one. +#[test] +fn audio_tracks_keep_their_names() { + for name in ["Dialogue.Dialogue-1", "Dialogue.Dialogue-1 L", "", "é", "音声", "🎤 Dialogue", "Ä1"] { + let mut doc = document(); + doc.compositions[0].tracks[1].name = name.into(); + let back = read_clean(&written(&doc, false)); + assert_eq!(back.compositions[0].tracks[1].name, name); + } +} diff --git a/crates/aaf/src/aaf/write.rs b/crates/aaf/src/aaf/write.rs new file mode 100644 index 000000000..7ab51cea9 --- /dev/null +++ b/crates/aaf/src/aaf/write.rs @@ -0,0 +1,800 @@ +//! Composition model → AAF objects. + +use std::collections::HashMap; + +use craft_timeline::time::{FrameRate, Tick}; + +use super::ids::{META_DICTIONARY, ROOT, cls, ddef, def, path, pid}; +use super::store::{self, Auid, Obj, Value, rational, utf16z}; +use crate::model::{CItem, CKind, CMarker, Composition, Document, Gain, Source, ticks_to_units}; +use crate::{Error, Identity, Report, Result}; + +/// UMID-style mob id (SMPTE 330 basic UMID): label, length 0x13, instance 0, material number. +pub(crate) fn mob_id(seed: u64, n: u64) -> [u8; 32] { + let mut id = [0u8; 32]; + id[..12].copy_from_slice(&[0x06, 0x0A, 0x2B, 0x34, 0x01, 0x01, 0x01, 0x05, 0x01, 0x01, 0x0F, 0x20]); + id[12] = 0x13; + let a = mix(seed ^ n.wrapping_mul(0x9E37_79B9_7F4A_7C15)); + let b = mix(a ^ 0xD6E8_FEB8_6659_FD93); + id[16..24].copy_from_slice(&a.to_le_bytes()); + id[24..32].copy_from_slice(&b.to_le_bytes()); + // a "random" material number per RFC 4122 v4 layout keeps it a valid UUID + id[22] = (id[22] & 0x0F) | 0x40; + id[24] = (id[24] & 0x3F) | 0x80; + id +} + +fn mix(mut z: u64) -> u64 { + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) +} + +pub(crate) fn fnv(s: &[u8], h: u64) -> u64 { + s.iter().fold(h, |h, &b| (h ^ b as u64).wrapping_mul(0x0000_0100_0000_01B3)) +} + +fn timestamp() -> Vec { + // 1970-01-01 00:00:00.00: written files are reproducible + let mut v = 1970i16.to_le_bytes().to_vec(); + v.extend_from_slice(&[1, 1, 0, 0, 0, 0]); + v +} + +fn weak(path: [u16; 3], key: Auid) -> Value { + Value::Weak(path.to_vec(), pid::IDENTIFICATION, key.to_vec()) +} + +fn data_def(kind: CKind) -> Auid { + match kind { + CKind::Picture => ddef::PICTURE, + CKind::Sound => ddef::SOUND, + } +} + +/// An indirect value: byte order, type id, value. +pub(crate) fn indirect(ty: Auid, v: &[u8]) -> Vec { + let mut d = vec![0x4C]; + d.extend_from_slice(&ty); + d.extend_from_slice(v); + d +} + +fn tagged(name: &str, value: &str) -> Obj { + Obj::new(cls::TAGGED_VALUE).data_prop(pid::TAG_NAME, utf16z(name)).data_prop(pid::TAG_VALUE, indirect(def::TYPE_STRING, &utf16z(value))) +} + +fn gain_rational(g: f64) -> Vec { + rational((g.max(0.0) * 65536.0).round().min(i32::MAX as f64) as i64, 65536) +} + +fn definition(class: Auid, id: Auid, name: &str) -> Obj { + Obj::new(class).data_prop(pid::IDENTIFICATION, id.to_vec()).data_prop(pid::NAME, utf16z(name)).data_prop(pid::DESCRIPTION, utf16z("")) +} + +struct W<'a> { + report: &'a mut Report, + inexact: bool, +} + +struct IdGen { + seed: u64, + next: u64, +} + +impl IdGen { + fn for_document(doc: &Document, comp: &Composition) -> Self { + let mut seed = fnv(comp.name.as_bytes(), 0xCBF2_9CE4_8422_2325); + for source in &doc.sources { + seed = fnv(source.path.as_deref().unwrap_or("").as_bytes(), fnv(source.key.as_bytes(), seed)); + } + seed = fnv(&(comp.tracks.len() as u64).to_le_bytes(), seed); + Self { seed, next: 0 } + } + + fn next(&mut self) -> [u8; 32] { + self.next += 1; + mob_id(self.seed, self.next) + } +} + +struct MediaOutput { + mobs: Vec, + essence: Vec, + master_ref: HashMap, +} + +type FileMob = ((Option, usize, CKind), [u8; 32], Vec, usize); + +struct GroupSlots { + master_id: [u8; 32], + tape_id: [u8; 32], + timecode: Option<(i64, FrameRate)>, + master_slots: Vec, + tape_slots: Vec, + files: Vec, + master_ref: HashMap, +} + +struct GroupSlotBuilder<'a, 'b> { + doc: &'a Document, + master_id: [u8; 32], + tape_id: [u8; 32], + ids: &'b mut IdGen, + files: &'b mut Vec, + master_slots: &'b mut Vec, + tape_slots: &'b mut Vec, + master_ref: &'b mut HashMap, +} + +struct FileSlotInput<'a> { + source: &'a Source, + file_slot: u32, + length: i64, + has_timecode: bool, + tape_id: [u8; 32], + member_index: usize, + slots: &'a mut Vec, + rate: (i64, i64), +} + +impl W<'_> { + /// Edit units at `rate` of tick `t`. + fn u(&mut self, t: Tick, rate: (i64, i64)) -> i64 { + let (n, exact) = ticks_to_units(t, rate.0, rate.1); + if !exact { + self.inexact = true; + } + n + } +} + +fn source_rate(s: &Source) -> (i64, i64) { + match s.kind { + CKind::Picture => (s.frame_rate.num, s.frame_rate.den), + CKind::Sound => (s.sample_rate.max(1) as i64, 1), + } +} + +fn write_media(doc: &Document, w: &mut W<'_>, ids: &mut IdGen) -> MediaOutput { + let mut groups: Vec<(String, Vec)> = Vec::new(); + for (index, source) in doc.sources.iter().enumerate() { + if source.nested.is_some() { + continue; + } + match groups.iter_mut().find(|(key, _)| *key == source.key) { + Some((_, members)) => members.push(index), + None => groups.push((source.key.clone(), vec![index])), + } + } + + let mut output = MediaOutput { mobs: Vec::new(), essence: Vec::new(), master_ref: HashMap::new() }; + for (_, members) in groups { + write_source_group(doc, &members, w, ids, &mut output); + } + output +} + +fn write_source_group(doc: &Document, members: &[usize], w: &mut W<'_>, ids: &mut IdGen, output: &mut MediaOutput) { + let first = &doc.sources[members[0]]; + let slots = build_group_slots(doc, members, w, ids); + output.master_ref.extend(slots.master_ref); + let mut master_slots = slots.master_slots; + add_master_markers(doc, members, w, &mut master_slots); + output.mobs.push( + Obj::new(cls::MASTER_MOB) + .data_prop(pid::MOB_ID, slots.master_id.to_vec()) + .data_prop(pid::MOB_NAME, utf16z(&first.name)) + .data_prop(pid::MOB_LAST_MODIFIED, timestamp()) + .data_prop(pid::MOB_CREATION_TIME, timestamp()) + .with(pid::SLOTS, Value::StrongVec(master_slots)), + ); + let tape_id = slots.tape_id; + let timecode = slots.timecode; + let tape_slots = slots.tape_slots; + add_file_mobs(doc, slots.files, w, output); + add_tape_mob(first, tape_id, timecode, tape_slots, output); +} + +fn build_group_slots(doc: &Document, members: &[usize], w: &mut W<'_>, ids: &mut IdGen) -> GroupSlots { + let master_id = ids.next(); + let tape_id = ids.next(); + let timecode = members.iter().find_map(|&i| doc.sources[i].start_tc.map(|t| (t, doc.sources[i].tc_rate))); + let mut tape_slots = Vec::new(); + if let Some((start, rate)) = timecode { + tape_slots.push(timecode_slot(members, doc, w, start, rate)); + } + let mut files: Vec = Vec::new(); + let mut master_slots = Vec::new(); + let mut master_ref = HashMap::new(); + let mut builder = GroupSlotBuilder { + doc, + master_id, + tape_id, + ids, + files: &mut files, + master_slots: &mut master_slots, + tape_slots: &mut tape_slots, + master_ref: &mut master_ref, + }; + for (member_index, &source_index) in members.iter().enumerate() { + builder.add_source(source_index, member_index, timecode.is_some(), w); + } + GroupSlots { master_id, tape_id, timecode, master_slots, tape_slots, files, master_ref } +} + +fn timecode_slot(members: &[usize], doc: &Document, w: &mut W<'_>, start: i64, rate: FrameRate) -> Obj { + let edit_rate = (rate.num, rate.den); + let len = members.iter().map(|&i| doc.sources[i].length).max().unwrap_or(Tick::ZERO); + let units = w.u(len, edit_rate); + timeline_slot( + 1, + "TC1", + 0, + edit_rate, + Obj::new(cls::TIMECODE) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, ddef::TIMECODE)) + .data_prop(pid::LENGTH, units.to_le_bytes().to_vec()) + .data_prop(pid::TC_START, start.to_le_bytes().to_vec()) + .data_prop(pid::TC_FPS, (rate.timecode_base().clamp(1, u16::MAX as i64) as u16).to_le_bytes().to_vec()) + .data_prop(pid::TC_DROP, vec![0]), + ) +} + +impl GroupSlotBuilder<'_, '_> { + fn add_source(&mut self, source_index: usize, member_index: usize, has_timecode: bool, w: &mut W<'_>) { + let source = &self.doc.sources[source_index]; + let rate = source_rate(source); + let embedded_key = if source.embedded.is_some() { source_index } else { usize::MAX }; + let file_key = (source.path.clone(), embedded_key, source.kind); + let file_slot = source.channel.map_or(1, |channel| channel + 1); + let len_units = w.u(source.length, rate); + let file_index = match self.files.iter().position(|file| file.0 == file_key) { + Some(index) => index, + None => { + self.files.push((file_key, self.ids.next(), Vec::new(), source_index)); + self.files.len() - 1 + } + }; + let file_id = self.files[file_index].1; + add_file_slot(FileSlotInput { + source, + file_slot, + length: len_units, + has_timecode, + tape_id: self.tape_id, + member_index, + slots: &mut self.files[file_index].2, + rate, + }); + if has_timecode { + let tape_slot_id = 2 + member_index as u32; + self.tape_slots.push(timeline_slot( + tape_slot_id, + &slot_name(source.kind, member_index as u32 + 1), + member_index as u32 + 1, + rate, + source_clip(source.kind, len_units, [0; 32], 0, 0), + )); + } + let master_slot = member_index as u32 + 1; + self.master_slots.push(timeline_slot( + master_slot, + &slot_name(source.kind, master_slot), + master_slot, + rate, + source_clip(source.kind, len_units, file_id, file_slot, 0), + )); + self.master_ref.insert(source_index, (self.master_id, master_slot)); + } +} + +fn add_file_slot(input: FileSlotInput<'_>) { + let FileSlotInput { source, file_slot, length, has_timecode, tape_id, member_index, slots, rate } = input; + if slots.iter().any(|slot| slot.u32(pid::SLOT_ID) == Some(file_slot)) { + return; + } + let tape_slot_id = if has_timecode { 2 + member_index as u32 } else { 0 }; + let mob_id = if has_timecode { tape_id } else { [0; 32] }; + let clip = source_clip(source.kind, length, mob_id, tape_slot_id, 0); + slots.push(timeline_slot(file_slot, &slot_name(source.kind, file_slot), file_slot, rate, clip)); +} + +fn add_master_markers(doc: &Document, members: &[usize], w: &mut W<'_>, master_slots: &mut Vec) { + let first = &doc.sources[members[0]]; + let marker_rate = + members.iter().map(|&i| &doc.sources[i]).find(|source| source.kind == CKind::Picture).map(source_rate).unwrap_or_else(|| source_rate(first)); + let markers: Vec<&CMarker> = members.iter().flat_map(|&i| doc.sources[i].markers.iter()).collect(); + if markers.is_empty() { + return; + } + let mut unique: Vec = markers.into_iter().cloned().collect(); + unique.sort_by(|a, b| (a.start, &a.name).cmp(&(b.start, &b.name))); + unique.dedup(); + master_slots.push(event_slot(w, members.len() as u32 + 1, marker_rate, &unique)); +} + +fn add_file_mobs(doc: &Document, files: Vec, w: &mut W<'_>, output: &mut MediaOutput) { + for (_, file_id, slots, source_index) in files { + let source = &doc.sources[source_index]; + let desc = descriptor(w, source); + output.mobs.push( + Obj::new(cls::SOURCE_MOB) + .data_prop(pid::MOB_ID, file_id.to_vec()) + .data_prop(pid::MOB_NAME, utf16z(&source.name)) + .data_prop(pid::MOB_LAST_MODIFIED, timestamp()) + .data_prop(pid::MOB_CREATION_TIME, timestamp()) + .with(pid::SLOTS, Value::StrongVec(slots)) + .with(pid::ESSENCE_DESCRIPTION, Value::Strong(Box::new(desc))), + ); + if let Some(data) = &source.embedded { + output.essence.push( + Obj::new(cls::ESSENCE_DATA).data_prop(pid::ESSENCE_MOB_ID, file_id.to_vec()).with(pid::ESSENCE_STREAM, Value::Stream(data.clone())), + ); + } + } +} + +fn add_tape_mob(first: &Source, tape_id: [u8; 32], timecode: Option<(i64, FrameRate)>, tape_slots: Vec, output: &mut MediaOutput) { + let Some(_) = timecode else { return }; + let reel = first.path.as_deref().map(craft_timeline::path::file_stem).unwrap_or(&first.name).to_string(); + output.mobs.push( + Obj::new(cls::SOURCE_MOB) + .data_prop(pid::MOB_ID, tape_id.to_vec()) + .data_prop(pid::MOB_NAME, utf16z(&reel)) + .data_prop(pid::MOB_LAST_MODIFIED, timestamp()) + .data_prop(pid::MOB_CREATION_TIME, timestamp()) + .with(pid::SLOTS, Value::StrongVec(tape_slots)) + .with(pid::ESSENCE_DESCRIPTION, Value::Strong(Box::new(Obj::new(cls::TAPE_DESCRIPTOR)))), + ); +} + +/// Serialise a composition document. +pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Identity<'_>, report: &mut Report) -> Result> { + let comp = doc.compositions.first().ok_or(Error::Empty)?; + let mut w = W { report, inexact: false }; + let mut ids = IdGen::for_document(doc, comp); + let MediaOutput { mut mobs, essence, master_ref } = write_media(doc, &mut w, &mut ids); + let mut used_ops: Vec = Vec::new(); + let mut uses_varying = false; + + // ---- nested compositions: a clip refers to one as it does to a master mob, through the + // composition's first track of the clip's kind (a broken-out channel: that channel's track) + let mut refs = Refs { mobs: master_ref, rates: HashMap::new() }; + let nested_ids: Vec<[u8; 32]> = doc.nested.iter().map(|_| ids.next()).collect(); + for (si, s) in doc.sources.iter().enumerate() { + let Some((comp, id)) = s.nested.and_then(|n| Some((doc.nested.get(n)?, nested_ids.get(n)?))) else { continue }; + let of_kind: Vec = comp.tracks.iter().enumerate().filter(|(_, t)| t.kind == s.kind).map(|(i, _)| i).collect(); + let Some(&track) = s.channel.and_then(|c| of_kind.get(c as usize)).or(of_kind.first()) else { continue }; + let rate = match s.kind { + CKind::Picture => (comp.rate.num, comp.rate.den), + CKind::Sound => (comp.sample_rate.max(1) as i64, 1), + }; + refs.mobs.insert(si, (*id, track as u32 + 2)); + refs.rates.insert(si, rate); + } + + // ---- the compositions: the exported sequence first + let comp_id = ids.next(); + let composition_mobs = { + let mut composition_writer = CompositionWriter { w: &mut w, doc, refs: &refs, used_ops: &mut used_ops, uses_varying: &mut uses_varying }; + let mut output = vec![composition_writer.write(comp, comp_id, true)]; + output.extend(doc.nested.iter().zip(&nested_ids).map(|(nested, id)| composition_writer.write(nested, *id, false))); + output + }; + mobs.splice(0..0, composition_mobs); + if w.inexact { + w.report.info("some times are not on whole edit units and were rounded"); + } + + // ---- dictionary + let mut ddefs = Vec::new(); + for (id, name) in + [(ddef::PICTURE, "Picture"), (ddef::SOUND, "Sound"), (ddef::TIMECODE, "Timecode"), (ddef::DESCRIPTIVE_METADATA, "DescriptiveMetadata")] + { + ddefs.push(definition(cls::DATA_DEFINITION, id, name)); + } + let mut opdefs = Vec::new(); + for op in &used_ops { + let (name, dd, inputs) = match *op { + x if x == def::VIDEO_DISSOLVE => ("Video Dissolve", ddef::PICTURE, 2), + x if x == def::VIDEO_FADE_TO_BLACK => ("Video Fade To Black", ddef::PICTURE, 1), + x if x == def::SMPTE_VIDEO_WIPE => ("SMPTE Video Wipe", ddef::PICTURE, 2), + x if x == def::MONO_AUDIO_DISSOLVE => ("Mono Audio Dissolve", ddef::SOUND, 2), + _ => ("Mono Audio Gain", ddef::SOUND, 1i32), + }; + let params: Vec> = if *op == def::MONO_AUDIO_GAIN { + vec![def::PARAM_AMPLITUDE.to_vec()] + } else if *op == def::VIDEO_DISSOLVE { + vec![def::PARAM_LEVEL.to_vec()] + } else { + Vec::new() + }; + let mut o = definition(cls::OPERATION_DEFINITION, *op, name) + .with(pid::OPDEF_DATA_DEFINITION, weak(path::DATA_DEFS, dd)) + .data_prop(pid::OPDEF_IS_TIME_WARP, vec![0]) + .data_prop(pid::OPDEF_NUMBER_INPUTS, inputs.to_le_bytes().to_vec()); + if !params.is_empty() { + o.set(pid::OPDEF_PARAMETERS_DEFINED, Value::WeakVec(path::PARAMETER_DEFS.to_vec(), pid::IDENTIFICATION, params)); + } + opdefs.push(o); + } + let pdefs = vec![ + definition(cls::PARAMETER_DEFINITION, def::PARAM_LEVEL, "Level").data_prop(pid::PARAMDEF_DISPLAY_UNITS, utf16z("")), + definition(cls::PARAMETER_DEFINITION, def::PARAM_AMPLITUDE, "Amplitude").data_prop(pid::PARAMDEF_DISPLAY_UNITS, utf16z("")), + ]; + let idefs = if uses_varying || !used_ops.is_empty() { + vec![ + definition(cls::INTERPOLATION_DEFINITION, def::INTERP_LINEAR, "Linear"), + definition(cls::INTERPOLATION_DEFINITION, def::INTERP_CONSTANT, "Constant"), + ] + } else { + Vec::new() + }; + let cdefs = vec![ + definition(cls::CONTAINER_DEFINITION, def::CONTAINER_EXTERNAL, "External Container"), + definition(cls::CONTAINER_DEFINITION, def::CONTAINER_AAF, "AAF Container"), + ]; + let mut dict = Obj::new(cls::DICTIONARY) + .with(pid::DATA_DEFINITIONS, Value::StrongSet(ddefs, pid::IDENTIFICATION)) + .with(pid::PARAMETER_DEFINITIONS, Value::StrongSet(pdefs, pid::IDENTIFICATION)) + .with(pid::CONTAINER_DEFINITIONS, Value::StrongSet(cdefs, pid::IDENTIFICATION)); + if !opdefs.is_empty() { + dict.set(pid::OPERATION_DEFINITIONS, Value::StrongSet(opdefs, pid::IDENTIFICATION)); + } + if !idefs.is_empty() { + dict.set(pid::INTERPOLATION_DEFINITIONS, Value::StrongSet(idefs, pid::IDENTIFICATION)); + } + + let mut content = Obj::new(cls::CONTENT_STORAGE).with(pid::MOBS, Value::StrongSet(mobs, pid::MOB_ID)); + if !essence.is_empty() { + content.set(pid::ESSENCE_DATA, Value::StrongSet(essence, pid::ESSENCE_MOB_ID)); + } + let ident = Obj::new(cls::IDENTIFICATION) + .data_prop(pid::COMPANY_NAME, utf16z(identity.company)) + .data_prop(pid::PRODUCT_NAME, utf16z(identity.product)) + .data_prop(pid::PRODUCT_VERSION_STRING, utf16z(identity.version)) + .data_prop(pid::PRODUCT_ID, identity.product_id.to_vec()) + .data_prop(pid::DATE, timestamp()) + .data_prop(pid::PLATFORM, utf16z(identity.platform)) + .data_prop(pid::GENERATION_AUID, mob_id(ids.seed, 0)[16..].to_vec()); + let header = Obj::new(cls::HEADER) + .data_prop(pid::BYTE_ORDER, 0x4949i16.to_le_bytes().to_vec()) + .data_prop(pid::LAST_MODIFIED, timestamp()) + .data_prop(pid::VERSION, vec![1, 1]) + .data_prop(pid::OBJECT_MODEL_VERSION, 1u32.to_le_bytes().to_vec()) + .data_prop(pid::OPERATIONAL_PATTERN, def::OP_EDIT_PROTOCOL.to_vec()) + .with(pid::CONTENT, Value::Strong(Box::new(content))) + .with(pid::DICTIONARY, Value::Strong(Box::new(dict))) + .with(pid::IDENTIFICATION_LIST, Value::StrongVec(vec![ident])); + // The meta-dictionary holds only extensions to the baseline object model, and everything written + // here is baseline, so its class and type definition sets are empty; readers that load them + // (pyaaf2) still need the sets to be there (filmcraft#323). + let meta = Obj::new(META_DICTIONARY) + .with(pid::META_CLASS_DEFINITIONS, Value::StrongSet(Vec::new(), pid::META_IDENTIFICATION)) + .with(pid::META_TYPE_DEFINITIONS, Value::StrongSet(Vec::new(), pid::META_IDENTIFICATION)); + let root = Obj::new(ROOT).with(pid::ROOT_META_DICTIONARY, Value::Strong(Box::new(meta))).with(pid::ROOT_HEADER, Value::Strong(Box::new(header))); + store::write(&root, version).map_err(Error::Other) +} + +/// Where clips find their sources: source index → (mob id, slot id), and for nested compositions +/// the edit rate of that slot. +#[derive(Default)] +struct Refs { + mobs: HashMap, + rates: HashMap, +} + +/// The composition mob of `comp`. Slot 1 is its timecode; its tracks follow from slot 2 in order. +struct CompositionWriter<'a, 'b, 'c> { + w: &'a mut W<'c>, + doc: &'b Document, + refs: &'b Refs, + used_ops: &'a mut Vec, + uses_varying: &'a mut bool, +} + +impl CompositionWriter<'_, '_, '_> { + fn write(&mut self, comp: &Composition, id: [u8; 32], top: bool) -> Obj { + let w = &mut *self.w; + let doc = self.doc; + let refs = self.refs; + let used_ops = &mut *self.used_ops; + let uses_varying = &mut *self.uses_varying; + let crate_rate = (comp.rate.num, comp.rate.den); + let mut slots = Vec::new(); + let total = comp + .tracks + .iter() + .map(|t| { + let mut c = Tick::ZERO; + for i in &t.items { + match i { + CItem::Transition(x) => c -= x.len, + other => c += other.len(), + } + } + c + }) + .max() + .unwrap_or(Tick::ZERO); + let total_units = w.u(total, crate_rate); + slots.push(timeline_slot( + 1, + "TC1", + 0, + crate_rate, + Obj::new(cls::TIMECODE) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, ddef::TIMECODE)) + .data_prop(pid::LENGTH, total_units.to_le_bytes().to_vec()) + .data_prop(pid::TC_START, comp.start_tc.to_le_bytes().to_vec()) + .data_prop(pid::TC_FPS, (comp.rate.timecode_base().clamp(1, u16::MAX as i64) as u16).to_le_bytes().to_vec()) + .data_prop(pid::TC_DROP, vec![comp.drop as u8]), + )); + let mut slot_id = 2u32; + for t in &comp.tracks { + let rate = match t.kind { + CKind::Picture => crate_rate, + CKind::Sound => (comp.sample_rate.max(1) as i64, 1), + }; + let dd = data_def(t.kind); + let mut comps = Vec::new(); + let mut cursor = Tick::ZERO; + for it in &t.items { + match it { + CItem::Filler(l) => { + let n = w.u(cursor + *l, rate) - w.u(cursor, rate); + cursor += *l; + comps.push( + Obj::new(cls::FILLER) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, dd)) + .data_prop(pid::LENGTH, n.to_le_bytes().to_vec()), + ); + } + CItem::Clip(c) => { + let n = w.u(cursor + c.len, rate) - w.u(cursor, rate); + let s = &doc.sources[c.source]; + let (mid, mslot) = refs.mobs.get(&c.source).copied().unwrap_or(([0; 32], 0)); + // (a nested composition counts the start in its own slot's edit units) + let start = w.u(c.start, refs.rates.get(&c.source).copied().unwrap_or(rate)); + let mut sc = source_clip(t.kind, n, mid, mslot, start); + if !c.name.is_empty() && c.name != s.name { + sc.set(pid::COMPONENT_USER_COMMENTS, Value::StrongVec(vec![tagged("Clip Name", &c.name)])); + } + let seg = match (&c.gain, t.kind) { + (Some(g), CKind::Sound) => { + if !used_ops.contains(&def::MONO_AUDIO_GAIN) { + used_ops.push(def::MONO_AUDIO_GAIN); + } + let param = match g { + Gain::Constant(a) => Obj::new(cls::CONSTANT_VALUE) + .data_prop(pid::PARAMETER_DEFINITION, def::PARAM_AMPLITUDE.to_vec()) + .data_prop(pid::CONSTANT_VALUE, indirect(def::TYPE_RATIONAL, &gain_rational(*a))), + Gain::Varying { linear, points } => { + *uses_varying = true; + let pts = points + .iter() + .map(|(off, a)| { + let o = w.u(cursor + *off, rate) - w.u(cursor, rate); + Obj::new(cls::CONTROL_POINT) + .data_prop(pid::CP_TIME, rational(o, n.max(1))) + .data_prop(pid::CP_VALUE, indirect(def::TYPE_RATIONAL, &gain_rational(*a))) + .data_prop(pid::CP_EDIT_HINT, vec![0]) + }) + .collect(); + Obj::new(cls::VARYING_VALUE) + .data_prop(pid::PARAMETER_DEFINITION, def::PARAM_AMPLITUDE.to_vec()) + .with( + pid::INTERPOLATION, + weak(path::INTERPOLATION_DEFS, if *linear { def::INTERP_LINEAR } else { def::INTERP_CONSTANT }), + ) + .with(pid::POINT_LIST, Value::StrongVec(pts)) + } + }; + Obj::new(cls::OPERATION_GROUP) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, dd)) + .data_prop(pid::LENGTH, n.to_le_bytes().to_vec()) + .with(pid::OPERATION, weak(path::OPERATION_DEFS, def::MONO_AUDIO_GAIN)) + .with(pid::INPUT_SEGMENTS, Value::StrongVec(vec![sc])) + .with(pid::PARAMETERS, Value::StrongVec(vec![param])) + } + _ => sc, + }; + comps.push(seg); + cursor += c.len; + } + CItem::Transition(x) => { + let ts = cursor - x.len; + let n = w.u(cursor, rate) - w.u(ts, rate); + let cut = w.u(ts + x.cut, rate) - w.u(ts, rate); + cursor = ts; + let op = transition_op(t.kind, &x.effect); + if !used_ops.contains(&op) { + used_ops.push(op); + } + let mut og = Obj::new(cls::OPERATION_GROUP) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, dd)) + .data_prop(pid::LENGTH, n.to_le_bytes().to_vec()) + .with(pid::OPERATION, weak(path::OPERATION_DEFS, op)) + .with(pid::COMPONENT_USER_COMMENTS, Value::StrongVec(vec![tagged("FilmCraft Effect", &x.effect)])); + if op == def::VIDEO_DISSOLVE { + *uses_varying = true; + let pts = [(0, 0), (1, 1)] + .iter() + .map(|&(t, v)| { + Obj::new(cls::CONTROL_POINT) + .data_prop(pid::CP_TIME, rational(t, 1)) + .data_prop(pid::CP_VALUE, indirect(def::TYPE_RATIONAL, &rational(v, 1))) + .data_prop(pid::CP_EDIT_HINT, vec![0]) + }) + .collect(); + og.set( + pid::PARAMETERS, + Value::StrongVec(vec![ + Obj::new(cls::VARYING_VALUE) + .data_prop(pid::PARAMETER_DEFINITION, def::PARAM_LEVEL.to_vec()) + .with(pid::INTERPOLATION, weak(path::INTERPOLATION_DEFS, def::INTERP_LINEAR)) + .with(pid::POINT_LIST, Value::StrongVec(pts)), + ]), + ); + } + comps.push( + Obj::new(cls::TRANSITION) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, dd)) + .data_prop(pid::LENGTH, n.to_le_bytes().to_vec()) + .data_prop(pid::CUT_POINT, cut.to_le_bytes().to_vec()) + .with(pid::OPERATION_GROUP, Value::Strong(Box::new(og))), + ); + } + } + } + let len: i64 = comps + .iter() + .map(|c| { + let l = c.i64(pid::LENGTH).unwrap_or(0); + if c.class == cls::TRANSITION { -l } else { l } + }) + .sum(); + let seq = Obj::new(cls::SEQUENCE) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, dd)) + .data_prop(pid::LENGTH, len.to_le_bytes().to_vec()) + .with(pid::COMPONENTS, Value::StrongVec(comps)); + slots.push(timeline_slot(slot_id, &t.name, t.number, rate, seq)); + slot_id += 1; + } + if !comp.markers.is_empty() { + slots.push(event_slot(w, slot_id, crate_rate, &comp.markers)); + } + let mut mob = Obj::new(cls::COMPOSITION_MOB) + .data_prop(pid::MOB_ID, id.to_vec()) + .data_prop(pid::MOB_NAME, utf16z(&comp.name)) + .data_prop(pid::MOB_LAST_MODIFIED, timestamp()) + .data_prop(pid::MOB_CREATION_TIME, timestamp()); + // only the exported sequence is a top-level composition; a nested sequence is found through + // the clips that use it + if top { + mob.set(pid::USAGE_CODE, Value::Data(def::USAGE_TOP_LEVEL.to_vec())); + } + mob.with(pid::SLOTS, Value::StrongVec(slots)).with( + pid::MOB_USER_COMMENTS, + Value::StrongVec(vec![ + tagged("FilmCraft Frame Size", &format!("{}x{}", comp.width, comp.height)), + tagged("FilmCraft Audio Sample Rate", &comp.sample_rate.to_string()), + ]), + ) + } +} + +fn slot_name(kind: CKind, n: u32) -> String { + match kind { + CKind::Picture => format!("V{n}"), + CKind::Sound => format!("A{n}"), + } +} + +fn timeline_slot(id: u32, name: &str, number: u32, rate: (i64, i64), segment: Obj) -> Obj { + let mut o = Obj::new(cls::TIMELINE_MOB_SLOT) + .data_prop(pid::SLOT_ID, id.to_le_bytes().to_vec()) + .data_prop(pid::SLOT_NAME, utf16z(name)) + .data_prop(pid::EDIT_RATE, rational(rate.0, rate.1)) + .data_prop(pid::ORIGIN, 0i64.to_le_bytes().to_vec()) + .with(pid::SEGMENT, Value::Strong(Box::new(segment))); + if number > 0 { + o.set(pid::PHYSICAL_TRACK_NUMBER, Value::Data(number.to_le_bytes().to_vec())); + } + o +} + +fn source_clip(kind: CKind, len: i64, source: [u8; 32], slot: u32, start: i64) -> Obj { + Obj::new(cls::SOURCE_CLIP) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, data_def(kind))) + .data_prop(pid::LENGTH, len.to_le_bytes().to_vec()) + .data_prop(pid::SOURCE_ID, source.to_vec()) + .data_prop(pid::SOURCE_MOB_SLOT_ID, slot.to_le_bytes().to_vec()) + .data_prop(pid::START_TIME, start.to_le_bytes().to_vec()) +} + +fn event_slot(w: &mut W, id: u32, rate: (i64, i64), markers: &[CMarker]) -> Obj { + let comps = markers + .iter() + .map(|m| { + let pos = w.u(m.start, rate); + let len = w.u(m.start + m.duration, rate) - pos; + let mut tags = vec![tagged("Name", &m.name), tagged("Comment", &m.comment)]; + if let Some(c) = &m.color { + tags.push(tagged("Color", c)); + } + Obj::new(cls::COMMENT_MARKER) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, ddef::DESCRIPTIVE_METADATA)) + .data_prop(pid::LENGTH, len.to_le_bytes().to_vec()) + .data_prop(pid::POSITION, pos.to_le_bytes().to_vec()) + .data_prop(pid::COMMENT, utf16z(if m.comment.is_empty() { &m.name } else { &m.comment })) + .with(pid::COMPONENT_USER_COMMENTS, Value::StrongVec(tags)) + }) + .collect(); + Obj::new(cls::EVENT_MOB_SLOT) + .data_prop(pid::SLOT_ID, id.to_le_bytes().to_vec()) + .data_prop(pid::SLOT_NAME, utf16z("Markers")) + .data_prop(pid::EVENT_EDIT_RATE, rational(rate.0, rate.1)) + .with( + pid::SEGMENT, + Value::Strong(Box::new( + Obj::new(cls::SEQUENCE) + .with(pid::DATA_DEFINITION, weak(path::DATA_DEFS, ddef::DESCRIPTIVE_METADATA)) + .with(pid::COMPONENTS, Value::StrongVec(comps)), + )), + ) +} + +fn transition_op(kind: CKind, effect: &str) -> Auid { + match kind { + CKind::Sound => def::MONO_AUDIO_DISSOLVE, + CKind::Picture => match effect { + "dip_to_black" => def::VIDEO_FADE_TO_BLACK, + e if e.contains("wipe") => def::SMPTE_VIDEO_WIPE, + _ => def::VIDEO_DISSOLVE, + }, + } +} + +fn descriptor(w: &mut W, s: &Source) -> Obj { + let rate = source_rate(s); + let len = w.u(s.length, rate); + let container = if s.embedded.is_some() { def::CONTAINER_AAF } else { def::CONTAINER_EXTERNAL }; + let mut d = match s.kind { + CKind::Picture => Obj::new(cls::CDCI_DESCRIPTOR) + .data_prop(pid::STORED_WIDTH, s.width.to_le_bytes().to_vec()) + .data_prop(pid::STORED_HEIGHT, s.height.to_le_bytes().to_vec()) + .data_prop(pid::FRAME_LAYOUT, vec![0]) + .data_prop(pid::IMAGE_ASPECT_RATIO, rational(s.width.max(1) as i64, s.height.max(1) as i64)) + .data_prop(pid::VIDEO_LINE_MAP, { + let mut v = 2u32.to_le_bytes().to_vec(); + v.extend_from_slice(&4u32.to_le_bytes()); + v.extend_from_slice(&0i32.to_le_bytes()); + v.extend_from_slice(&0i32.to_le_bytes()); + v + }) + .data_prop(pid::COMPONENT_WIDTH, 8u32.to_le_bytes().to_vec()) + .data_prop(pid::HORIZONTAL_SUBSAMPLING, 2u32.to_le_bytes().to_vec()), + CKind::Sound => { + // Channel counts and rates come from the project: saturate, as these fields can't + // hold more anyway. + let block = s.file_channels.max(1).saturating_mul((s.bits as u32).div_ceil(8)); + Obj::new(cls::PCM_DESCRIPTOR) + .data_prop(pid::AUDIO_SAMPLING_RATE, rational(s.sample_rate as i64, 1)) + .data_prop(pid::LOCKED, vec![1]) + .data_prop(pid::CHANNELS, s.file_channels.max(1).to_le_bytes().to_vec()) + .data_prop(pid::QUANTIZATION_BITS, (s.bits as u32).to_le_bytes().to_vec()) + .data_prop(pid::BLOCK_ALIGN, u16::try_from(block).unwrap_or(u16::MAX).to_le_bytes().to_vec()) + .data_prop(pid::AVERAGE_BPS, block.saturating_mul(s.sample_rate).to_le_bytes().to_vec()) + } + }; + d.set(pid::SAMPLE_RATE, Value::Data(rational(rate.0, rate.1))); + d.set(pid::FILE_LENGTH, Value::Data(len.to_le_bytes().to_vec())); + d.set(pid::CONTAINER_FORMAT, weak(path::CONTAINER_DEFS, container)); + if let Some(p) = &s.path { + let url = if craft_timeline::path::is_absolute(p) { craft_timeline::path::path_to_file_url(p, false) } else { p.clone() }; + d.set(pid::LOCATOR, Value::StrongVec(vec![Obj::new(cls::NETWORK_LOCATOR).data_prop(pid::URL_STRING, utf16z(&url))])); + } + d +} diff --git a/crates/aaf/src/lib.rs b/crates/aaf/src/lib.rs new file mode 100644 index 000000000..ee7448bc0 --- /dev/null +++ b/crates/aaf/src/lib.rs @@ -0,0 +1,60 @@ +#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] +//! AAF and OMF: the two mob-based editorial interchange formats, read and written through one +//! composition model. +//! +//! - [`model`]: a [`model::Document`] of compositions (tracks of fillers, source clips and +//! overlapping transitions, with clip gain and markers) and the media sources they reference, +//! with exact times ([`craft_timeline::time::Tick`]). +//! - [`aaf`]: Advanced Authoring Format, Edit Protocol, in structured storage (`craft-cfb`). +//! - [`omf`]: OMF Interchange 2.0 in a Bento container; sound only. +//! +//! Both read into and write from the same model, so an application maps its timeline onto the +//! model once. Audio essence can be embedded: the model carries it as PCM, and the WAVE / AIFF +//! wrappers the formats use are craft-audio's. +//! +//! Clean-room: written from the public specifications only (AMWA *AAF Object Specification* v1.1, +//! *AAF Low-Level Container Specification* v1.0.1, *AAF Edit Protocol* v1.0, SMPTE RP 224 / RP 210 +//! labels; Avid *OMF Interchange Specification* 2.0, Apple *Bento Specification* 1.0d5). No file +//! I/O: documents are bytes and media references are strings. +//! +//! Input is untrusted: a damaged or crafted file gives an [`Error`] or a [`Report`] entry, never +//! a panic, a hang or an allocation sized by a count in the file. + +pub mod aaf; +pub mod model; +pub mod omf; +mod pcm; +#[cfg(test)] +mod roundtrip_tests; +#[cfg(test)] +mod testdoc; + +pub use craft_timeline::report::Report; + +/// Error from a reader or writer. Unsupported *features* never error; they go in the [`Report`]. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The bytes are not a readable document of the format. + #[error("{format} parse error: {message}")] + Parse { format: &'static str, message: String }, + /// The document has no composition. + #[error("the document contains no timeline")] + Empty, + /// The document could not be written. + #[error("{0}")] + Other(String), +} + +pub type Result = std::result::Result; + +/// Who a written file says made it (the AAF `Identification` object, the OMF `IDNT` object). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Identity<'a> { + pub company: &'a str, + pub product: &'a str, + /// The product's version, as text. + pub version: &'a str, + pub platform: &'a str, + /// AAF `ProductID`: an AUID naming the product. + pub product_id: [u8; 16], +} diff --git a/crates/aaf/src/model.rs b/crates/aaf/src/model.rs new file mode 100644 index 000000000..3dff507be --- /dev/null +++ b/crates/aaf/src/model.rs @@ -0,0 +1,185 @@ +//! A format-neutral mob-style composition model shared by AAF and OMF. +//! +//! Both formats describe a timeline as a composition of slots (tracks) whose segments are +//! sequences of fillers, source clips and transitions, where a transition *overlaps* the end of +//! the segment before it and the start of the segment after it (the sequence's length is the sum of +//! its segment lengths minus its transition lengths). Source clips reference media through master +//! mobs and file source mobs: here, a [`Source`] per essence, grouped into one media item by its +//! key. All times are ticks; the format serialisers convert to and from edit units. + +use craft_timeline::time::{FrameRate, TICKS_PER_SECOND, Tick}; + +/// Picture or sound. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum CKind { + Picture, + Sound, +} + +/// A whole document: compositions and the media they reference. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Document { + /// The top-level compositions. + pub compositions: Vec, + pub sources: Vec, + /// Compositions that are only used inside others (nested sequences); a [`Source`] refers to + /// one by its index here. They may nest further. + pub nested: Vec, +} + +/// How a nested sequence is written. Premiere Pro writes one as a composition of its own in AAF +/// (clips refer to it the way they refer to media) and renders its sound in OMF, which has no +#[derive(Clone, Debug, PartialEq)] +pub struct Composition { + pub name: String, + pub rate: FrameRate, + pub sample_rate: u32, + pub width: u32, + pub height: u32, + pub start_tc: i64, + pub drop: bool, + pub tracks: Vec, + pub markers: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct CTrack { + pub kind: CKind, + pub name: String, + /// Physical track number (1-based, per kind). + pub number: u32, + /// Sound: channels of the track (1 mono, 2 stereo, 6 5.1). + pub channels: u32, + pub items: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum CItem { + Filler(Tick), + Clip(CClip), + Transition(CTransition), +} + +impl CItem { + /// The item's length on the track. + pub fn len(&self) -> Tick { + match self { + CItem::Filler(l) => *l, + CItem::Clip(c) => c.len, + CItem::Transition(t) => t.len, + } + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct CClip { + pub len: Tick, + /// Index into [`Document::sources`]. + pub source: usize, + /// Source (file) time of the segment's first frame. + pub start: Tick, + pub gain: Option, + pub name: String, +} + +/// Clip gain (linear amplitude). +#[derive(Clone, Debug, PartialEq)] +pub enum Gain { + Constant(f64), + /// Points at offsets from the segment start; `linear` false = hold (step) interpolation. + Varying { + linear: bool, + points: Vec<(Tick, f64)>, + }, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct CTransition { + pub len: Tick, + /// Where the cut lies, from the transition start. + pub cut: Tick, + /// The transition, by a name of the writing application's choosing; it is kept in a private tag + /// and comes back unchanged. Names the formats themselves can say: `cross_dissolve` and + /// `dip_to_black` (picture), any name containing `wipe` (a SMPTE wipe), `constant_power` (sound). + /// A file without the tag reads as one of these. + pub effect: String, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct CMarker { + pub start: Tick, + pub duration: Tick, + pub name: String, + pub comment: String, + pub color: Option, +} + +/// One essence: a file (or embedded data) carrying the picture, or some channels of sound, of a +/// media item. +#[derive(Clone, Debug, PartialEq)] +pub struct Source { + /// Groups the sources of one media item (one master mob). + pub key: String, + pub name: String, + pub kind: CKind, + pub path: Option, + /// Sound: the (0-based) channel of the file this source exposes (broken out to mono), or + /// `None` for all of them. + pub channel: Option, + /// Sound: channels this source exposes (1 when `channel` is set, else `file_channels`). + pub channels: u32, + /// Sound: channels of the file / embedded data. + pub file_channels: u32, + pub width: u32, + pub height: u32, + /// Picture: the frame rate of the media. + pub frame_rate: FrameRate, + pub sample_rate: u32, + pub bits: u16, + pub length: Tick, + /// Media time of the essence's time 0 (trimmed / consolidated essence). + pub offset: Tick, + /// Start timecode in frames at `tc_rate`. + pub start_tc: Option, + pub tc_rate: FrameRate, + pub embedded: Option>, + pub markers: Vec, + /// Not media but a nested composition: its index in [`Document::nested`]. `start` of a clip + /// that uses it is a time in that composition. + pub nested: Option, +} + +impl Source { + /// A picture source with no length: a still image. + pub fn is_still(&self) -> bool { + self.kind == CKind::Picture && self.length == Tick::ZERO + } +} + +/// The largest time read from a document (about 3.3 days). +pub const MAX_TIME: Tick = Tick(1 << 56); + +/// Ticks of `n` edit units at rate `num/den` (rounded to the nearest tick). +pub fn units_to_ticks(n: i64, num: i64, den: i64) -> Tick { + if num <= 0 || den <= 0 { + return Tick::ZERO; + } + let t = n as i128 * den as i128 * TICKS_PER_SECOND as i128; + let q = t.div_euclid(num as i128); + let r = t.rem_euclid(num as i128); + // documents are untrusted: keep every time far from overflow (±3 days) + Tick((q + if r * 2 >= num as i128 { 1 } else { 0 }).clamp(-MAX_TIME.0 as i128, MAX_TIME.0 as i128) as i64) +} + +/// Edit units at rate `num/den` nearest to `t`; the flag is false when `t` is not on a unit. +pub fn ticks_to_units(t: Tick, num: i64, den: i64) -> (i64, bool) { + if num <= 0 || den <= 0 { + return (0, false); + } + let x = t.0 as i128 * num as i128; + let d = den as i128 * TICKS_PER_SECOND as i128; + let q = x.div_euclid(d); + let r = x.rem_euclid(d); + let n = q + if r * 2 >= d { 1 } else { 0 }; + (n.clamp(i64::MIN as i128, i64::MAX as i128) as i64, r == 0) +} diff --git a/crates/aaf/src/omf.rs b/crates/aaf/src/omf.rs new file mode 100644 index 000000000..57e0f756c --- /dev/null +++ b/crates/aaf/src/omf.rs @@ -0,0 +1,47 @@ +//! OMF Interchange 2.0 for audio post: written, and the same structures read. +//! +//! Written from the public *OMF Interchange Specification Version 2.0* (Avid Technology, 1995/97) +//! and Apple's *Bento Specification* revision 1.0d5 for the container ([`bento`]). +//! +//! The file holds a composition mob (`CMOB`) with a timecode slot and one sound slot per audio +//! track (one per channel when broken out to mono): sequences (`SEQU`) of fillers (`FILL`), source +//! clips (`SCLP`) and transitions (`TRAN` with a `SimpleMonoAudioDissolve` effect); clip volume is a +//! `MonoAudioGain` effect (`EFFE`) whose slot −1 is the clip and slot 1 the amplitude +//! (`CVAL` constant or `VVAL` keyframes). Every media item has a master mob (`MMOB`), a file source +//! mob (`SMOB` with a `WAVD` / `AIFD` descriptor: embedded essence in a `WAVE` media data object, +//! or a locator to a separate file) and a tape source mob carrying its timecode. Times are in +//! samples (sound slots use the sample rate as edit rate) so edits are sample-exact. +//! +//! Only sound is written: picture tracks and markers of the model are left out, and a nested +//! composition is not written as one (readers of OMF do not follow compositions). A caller with +//! nested sequences mixes their sound and passes it as media like any other; Premiere Pro's OMF +//! export does the same. +//! +//! Private properties. `FilmCraft:SCLP:ClipName` (a clip's name) and `FilmCraft:EFFE:EffectID` (a +//! transition's name) are not in the specification; they were first written by FilmCraft and keep +//! their names so that existing files read back. + +pub(crate) mod bento; +mod read; +mod write; + +use crate::model::Document; +use crate::{Identity, Report, Result}; + +/// Write the sound of `doc`'s first composition as an OMF 2.0 file. +pub fn write(doc: &Document, identity: &Identity<'_>, report: &mut Report) -> Result> { + write::write(doc, identity, report) +} + +/// Read an OMF 2.0 file (as written by [`write`] and by applications using the same structures). +pub fn read(bytes: &[u8], report: &mut Report) -> Result { + read::read(bytes, report) +} + +/// Whether `bytes` end with a Bento container label (OMF files do). +pub fn sniff(bytes: &[u8]) -> bool { + bento::sniff(bytes) +} + +#[cfg(test)] +mod tests; diff --git a/crates/aaf/src/omf/bento.rs b/crates/aaf/src/omf/bento.rs new file mode 100644 index 000000000..00a7e2a26 --- /dev/null +++ b/crates/aaf/src/omf/bento.rs @@ -0,0 +1,407 @@ +//! The Bento container (Apple, *Bento Specification* revision 1.0d5), the storage layer of OMF +//! Interchange 2.0 files. +//! +//! A Bento container is a heap of value bytes, a table of contents (TOC) describing every value of +//! every object, and a fixed-size label at the very end of the file that locates the TOC: +//! +//! ```text +//! label (24 bytes, big-endian): magic A4 'C' 'M' A5 'H' 'd' 'r' D7, u16 flags, u16 buffer size +//! (KiB), u16 major version (1), u16 minor version (0), +//! u32 TOC offset, u32 TOC size +//! ``` +//! +//! Every value belongs to an (object id, property id, type id) triple. Properties and types are +//! themselves objects carrying a global name (`"OMFI:CPNT:Length"`, `"omfi:Length32"`). The TOC +//! is a stream of one-byte operations, each followed by its operands (big-endian): +//! +//! | op | operands | meaning | +//! |---|---|---| +//! | 1 NewObject | u32 id | following entries belong to this object | +//! | 2 NewProperty | u32 id | … to this property | +//! | 3 NewType | u32 id | starts a new value of this type | +//! | 4 ExplicitGen | u32 | generation of the value | +//! | 5 Offset4Len4 | u32 offset, u32 length | a value segment in the file | +//! | 6 ContdOffset4Len4 | u32 offset, u32 length | a continuation segment | +//! | 7 Offset8Len4 / 8 Contd… | u64 offset, u32 length | segments beyond 4 GiB | +//! | 9–13 Immediate0–4 | 4 bytes | a value of 0–4 bytes stored in the TOC | +//! | 14 ContdImmediate4 | 4 bytes | continuation | +//! | 15 ReferenceListID | u32 id | the object holding this value's reference list | +//! | 24 EndOfBufr | — | skip to the end of the current TOC buffer | +//! | 0xFF Padding | — | ignored | +//! +//! Object references (OMF's `omfi:ObjRef`) are 4-byte keys resolved through the referencing +//! object's reference list (standard property 22: pairs of u32 key, u32 object id). FilmCraft +//! uses the referenced object's id as the key. + +use std::collections::{BTreeMap, HashMap}; + +pub(crate) const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7]; +pub(crate) const LABEL_SIZE: usize = 24; + +// standard object ids +const TOC_OBJECT: u32 = 1; +const TOC_SEED: u32 = 2; +const TOC_MIN_SEED: u32 = 3; +const TOC_TYPE: u32 = 19; +const TYPE_7BIT_ASCII: u32 = 21; +pub(crate) const OBJ_REFERENCES: u32 = 22; +const GLOBAL_TYPE_NAME: u32 = 23; +const GLOBAL_PROP_NAME: u32 = 24; +const MIN_USER_ID: u32 = 100; + +const OP_NEW_OBJECT: u8 = 1; +const OP_NEW_PROPERTY: u8 = 2; +const OP_NEW_TYPE: u8 = 3; +const OP_EXPLICIT_GEN: u8 = 4; +const OP_OFFSET4_LEN4: u8 = 5; +const OP_CONTD_OFFSET4_LEN4: u8 = 6; +const OP_OFFSET8_LEN4: u8 = 7; +const OP_CONTD_OFFSET8_LEN4: u8 = 8; +const OP_IMMEDIATE0: u8 = 9; +const OP_IMMEDIATE4: u8 = 13; +const OP_CONTD_IMMEDIATE4: u8 = 14; +const OP_REFERENCE_LIST_ID: u8 = 15; +const OP_END_OF_BUFFER: u8 = 24; +const OP_PADDING: u8 = 0xFF; + +const TOC_BUFFER_KIB: u16 = 8; + +#[derive(Clone, Debug, PartialEq, Eq)] +enum Seg { + Imm(Vec), + At(u64, u32), +} + +/// Bento values are metadata, not essence payloads. Limit reassembled segments so repeated +/// references cannot amplify a small OMF into an enormous allocation. +const MAX_VALUE_BYTES: usize = 16 << 20; + +/// Most bytes of global names (property and type names) kept for one container. Real files name a +/// few hundred short strings; without a cap, many objects naming one large shared range would each +/// retain their own copy. +const MAX_NAME_BYTES: usize = 4 << 20; + +/// Builds a Bento container. +pub(crate) struct Writer { + data: Vec, + names: HashMap<(u32, String), u32>, + next: u32, + /// (object, property, type, segments) + entries: Vec<(u32, u32, u32, Vec)>, +} + +impl Writer { + pub fn new() -> Writer { + Writer { data: Vec::new(), names: HashMap::new(), next: MIN_USER_ID, entries: Vec::new() } + } + + fn named(&mut self, kind: u32, name: &str) -> u32 { + if let Some(&id) = self.names.get(&(kind, name.to_string())) { + return id; + } + let id = self.new_object(); + let mut v = name.as_bytes().to_vec(); + v.push(0); + let seg = self.store(&v); + self.entries.push((id, kind, TYPE_7BIT_ASCII, vec![seg])); + self.names.insert((kind, name.to_string()), id); + id + } + + pub fn property(&mut self, name: &str) -> u32 { + self.named(GLOBAL_PROP_NAME, name) + } + + pub fn type_id(&mut self, name: &str) -> u32 { + self.named(GLOBAL_TYPE_NAME, name) + } + + pub fn new_object(&mut self) -> u32 { + let id = self.next; + self.next += 1; + id + } + + fn store(&mut self, v: &[u8]) -> Seg { + if v.len() <= 4 { + return Seg::Imm(v.to_vec()); + } + let at = self.data.len() as u64; + self.data.extend_from_slice(v); + Seg::At(at, v.len() as u32) + } + + /// Set a value of `obj` (values of more than 4 GiB are not supported by OMF). + pub fn set(&mut self, obj: u32, property: &str, ty: &str, value: &[u8]) { + let p = self.property(property); + let t = self.type_id(ty); + let seg = self.store(value); + self.entries.push((obj, p, t, vec![seg])); + } + + /// The reference list of `obj` (keys are object ids). + pub fn set_references(&mut self, obj: u32, targets: &[u32]) { + let mut v = Vec::with_capacity(targets.len() * 8); + for t in targets { + v.extend_from_slice(&t.to_be_bytes()); + v.extend_from_slice(&t.to_be_bytes()); + } + let seg = self.store(&v); + self.entries.push((obj, OBJ_REFERENCES, TYPE_7BIT_ASCII, vec![seg])); + } + + pub fn finish(mut self) -> Vec { + let seed = self.next; + self.entries.push((TOC_OBJECT, TOC_SEED, TOC_TYPE, vec![Seg::Imm(seed.to_be_bytes().to_vec())])); + self.entries.push((TOC_OBJECT, TOC_MIN_SEED, TOC_TYPE, vec![Seg::Imm(MIN_USER_ID.to_be_bytes().to_vec())])); + self.entries.sort_by_key(|e| (e.0, e.1, e.2)); + let mut toc = Vec::new(); + let (mut cur_obj, mut cur_prop) = (u32::MAX, u32::MAX); + for (obj, prop, ty, segs) in &self.entries { + if *obj != cur_obj { + toc.push(OP_NEW_OBJECT); + toc.extend_from_slice(&obj.to_be_bytes()); + cur_obj = *obj; + cur_prop = u32::MAX; + } + if *prop != cur_prop { + toc.push(OP_NEW_PROPERTY); + toc.extend_from_slice(&prop.to_be_bytes()); + cur_prop = *prop; + } + toc.push(OP_NEW_TYPE); + toc.extend_from_slice(&ty.to_be_bytes()); + for (i, s) in segs.iter().enumerate() { + match s { + Seg::Imm(b) => { + toc.push(if i > 0 { OP_CONTD_IMMEDIATE4 } else { OP_IMMEDIATE0 + b.len() as u8 }); + let mut w = [0u8; 4]; + w[..b.len()].copy_from_slice(b); + toc.extend_from_slice(&w); + } + Seg::At(off, len) if *off <= u32::MAX as u64 => { + toc.push(if i > 0 { OP_CONTD_OFFSET4_LEN4 } else { OP_OFFSET4_LEN4 }); + toc.extend_from_slice(&(*off as u32).to_be_bytes()); + toc.extend_from_slice(&len.to_be_bytes()); + } + Seg::At(off, len) => { + toc.push(if i > 0 { OP_CONTD_OFFSET8_LEN4 } else { OP_OFFSET8_LEN4 }); + toc.extend_from_slice(&off.to_be_bytes()); + toc.extend_from_slice(&len.to_be_bytes()); + } + } + } + } + let mut out = self.data; + let toc_at = out.len() as u64; + out.extend_from_slice(&toc); + let mut label = MAGIC.to_vec(); + label.extend_from_slice(&0u16.to_be_bytes()); // flags + label.extend_from_slice(&TOC_BUFFER_KIB.to_be_bytes()); + label.extend_from_slice(&1u16.to_be_bytes()); + label.extend_from_slice(&0u16.to_be_bytes()); + label.extend_from_slice(&(toc_at.min(u32::MAX as u64) as u32).to_be_bytes()); + label.extend_from_slice(&(toc.len() as u32).to_be_bytes()); + out.extend_from_slice(&label); + out + } +} + +/// One value of a parsed object. +#[derive(Clone, Debug)] +pub(crate) struct Prop { + pub prop: u32, + #[allow(dead_code)] + pub ty: u32, + segs: Vec, +} + +/// A parsed Bento container. +pub(crate) struct Container<'a> { + data: &'a [u8], + pub objects: BTreeMap>, + names: HashMap, + by_name: HashMap, +} + +fn be32(b: &[u8], at: usize) -> Option { + b.get(at..at + 4).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]])) +} + +/// Whether `b` ends with a Bento label. +pub(crate) fn sniff(b: &[u8]) -> bool { + b.len() >= LABEL_SIZE && b[b.len() - LABEL_SIZE..b.len() - LABEL_SIZE + 8] == MAGIC +} + +impl<'a> Container<'a> { + pub fn open(data: &'a [u8]) -> Result, String> { + if !sniff(data) { + return Err("no Bento label at the end of the file".into()); + } + let l = &data[data.len() - LABEL_SIZE..]; + let buf = (u16::from_be_bytes([l[10], l[11]]) as usize).max(1) * 1024; + let toc_at = be32(l, 16).unwrap_or(0) as usize; + let toc_len = be32(l, 20).unwrap_or(0) as usize; + let toc = data.get(toc_at..toc_at.checked_add(toc_len).ok_or("bad TOC size")?).ok_or("the TOC lies outside the file")?; + let mut c = Container { data, objects: BTreeMap::new(), names: HashMap::new(), by_name: HashMap::new() }; + let (mut obj, mut prop) = (0u32, 0u32); + let mut i = 0; + while i < toc.len() { + let op = toc[i]; + i += 1; + let need = |n: usize| -> Result<&[u8], String> { toc.get(i..i + n).ok_or_else(|| "truncated TOC".to_string()) }; + match op { + OP_NEW_OBJECT => { + obj = be32(need(4)?, 0).unwrap_or(0); + i += 4; + } + OP_NEW_PROPERTY => { + prop = be32(need(4)?, 0).unwrap_or(0); + i += 4; + } + OP_NEW_TYPE => { + let ty = be32(need(4)?, 0).unwrap_or(0); + i += 4; + c.objects.entry(obj).or_default().push(Prop { prop, ty, segs: Vec::new() }); + } + OP_EXPLICIT_GEN | OP_REFERENCE_LIST_ID => { + need(4)?; + i += 4; + } + OP_OFFSET4_LEN4 | OP_CONTD_OFFSET4_LEN4 => { + let b = need(8)?; + let seg = Seg::At(be32(b, 0).unwrap_or(0) as u64, be32(b, 4).unwrap_or(0)); + i += 8; + c.push_seg(obj, seg)?; + } + OP_OFFSET8_LEN4 | OP_CONTD_OFFSET8_LEN4 => { + let b = need(12)?; + let off = u64::from_be_bytes(b[..8].try_into().map_err(|_| "truncated TOC")?); + let seg = Seg::At(off, be32(b, 8).unwrap_or(0)); + i += 12; + c.push_seg(obj, seg)?; + } + x @ (OP_IMMEDIATE0..=OP_IMMEDIATE4 | OP_CONTD_IMMEDIATE4) => { + let b = need(4)?; + let n = if x == OP_CONTD_IMMEDIATE4 { 4 } else { (x - OP_IMMEDIATE0) as usize }; + let seg = Seg::Imm(b[..n].to_vec()); + i += 4; + c.push_seg(obj, seg)?; + } + OP_END_OF_BUFFER => { + // the rest of this TOC buffer is unused + i = (i - 1) / buf * buf + buf; + } + OP_PADDING | 0 => {} + other => return Err(format!("unknown TOC operation {other}")), + } + } + // global names + let ids: Vec = c.objects.keys().copied().collect(); + let mut name_bytes = 0usize; + for id in ids { + let name = c.objects[&id].iter().find(|p| p.prop == GLOBAL_PROP_NAME || p.prop == GLOBAL_TYPE_NAME).and_then(|p| c.value(p).ok()); + if let Some(n) = name { + name_bytes = name_bytes.saturating_add(n.len()); + if name_bytes > MAX_NAME_BYTES { + return Err("Bento global names exceed the name budget".into()); + } + let s = String::from_utf8_lossy(&n).trim_end_matches('\0').to_string(); + c.by_name.insert(s.clone(), id); + c.names.insert(id, s); + } + } + Ok(c) + } + + fn push_seg(&mut self, obj: u32, seg: Seg) -> Result<(), String> { + let p = self.objects.get_mut(&obj).and_then(|v| v.last_mut()).ok_or("a value without a type in the TOC")?; + if p.segs.len() > 1 << 20 { + return Err("too many value segments".into()); + } + p.segs.push(seg); + Ok(()) + } + + /// The bytes of a value. + pub fn value(&self, p: &Prop) -> Result, String> { + let total = p.segs.iter().try_fold(0usize, |total, s| { + let len = match s { + Seg::Imm(b) => b.len(), + Seg::At(_, len) => *len as usize, + }; + total.checked_add(len).ok_or_else(|| "Bento value length overflows".to_string()) + })?; + if total > MAX_VALUE_BYTES { + return Err("Bento value exceeds the decoded-value budget".into()); + } + let mut out = Vec::new(); + out.try_reserve_exact(total).map_err(|_| "cannot allocate Bento value")?; + for s in &p.segs { + match s { + Seg::Imm(b) => out.extend_from_slice(b), + Seg::At(off, len) => { + let start = usize::try_from(*off).map_err(|_| "value offset too large")?; + let b = + self.data.get(start..start.checked_add(*len as usize).ok_or("bad value length")?).ok_or("a value lies outside the file")?; + out.extend_from_slice(b); + } + } + } + Ok(out) + } + + pub fn id_of(&self, name: &str) -> Option { + self.by_name.get(name).copied() + } + + #[allow(dead_code)] + pub fn name_of(&self, id: u32) -> Option<&str> { + self.names.get(&id).map(String::as_str) + } + + /// The first value of property `name` of `obj`. + pub fn get(&self, obj: u32, name: &str) -> Option> { + let pid = self.id_of(name)?; + let p = self.objects.get(&obj)?.iter().find(|p| p.prop == pid)?; + self.value(p).ok() + } + + /// Resolve an object reference key of `obj` through its reference list. + pub fn resolve(&self, obj: u32, key: u32) -> u32 { + if let Some(p) = self.objects.get(&obj).and_then(|v| v.iter().find(|p| p.prop == OBJ_REFERENCES)) + && let Ok(v) = self.value(p) + { + for pair in v.as_chunks::<8>().0 { + if be32(pair, 0) == Some(key) { + return be32(pair, 4).unwrap_or(key); + } + } + } + key + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn many_objects_cannot_name_themselves_with_one_shared_range() { + let mut w = Writer::new(); + w.data = vec![b'a'; 1 << 20]; + let ids: Vec = (0..8).map(|_| w.new_object()).collect(); + for id in ids { + w.entries.push((id, GLOBAL_PROP_NAME, TYPE_7BIT_ASCII, vec![Seg::At(0, 1 << 20)])); + } + let file = w.finish(); + assert!(Container::open(&file).err().is_some_and(|e| e.contains("name budget"))); + } + + #[test] + fn repeated_segments_cannot_expand_a_value_past_its_budget() { + let p = Prop { prop: 1, ty: 1, segs: vec![Seg::At(0, MAX_VALUE_BYTES as u32), Seg::Imm(vec![0])] }; + let c = Container { data: &[], objects: BTreeMap::new(), names: HashMap::new(), by_name: HashMap::new() }; + assert!(c.value(&p).unwrap_err().contains("budget")); + } +} diff --git a/crates/aaf/src/omf/read.rs b/crates/aaf/src/omf/read.rs new file mode 100644 index 000000000..50e4b270c --- /dev/null +++ b/crates/aaf/src/omf/read.rs @@ -0,0 +1,438 @@ +//! OMF 2.0 objects (Bento) → composition model. + +use std::collections::{HashMap, HashSet}; + +use craft_timeline::time::{FrameRate, Tick}; + +use super::bento::Container; +use crate::model::{CClip, CItem, CKind, CTrack, CTransition, Composition, Document, Gain, Source, units_to_ticks}; +use crate::{Error, Report, Result}; + +type Uid = [u8; 12]; + +const MAX_REFERENCE_VISITS: usize = 65_536; + +struct R<'a, 'b> { + c: &'b Container<'a>, + report: &'b mut Report, + /// big-endian values ("MM"); "II" files are little-endian + be: bool, + mobs: HashMap, + media: HashMap>, + doc: Document, + sources: HashMap<(Uid, u32, String), usize>, +} + +pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { + let perr = |m: String| Error::Parse { format: "OMF", message: m }; + let c = Container::open(bytes).map_err(perr)?; + let class_of = |o: u32| c.get(o, "OMFI:OOBJ:ObjClass").and_then(|v| v.get(..4).map(|b| [b[0], b[1], b[2], b[3]])); + let head = c.objects.keys().copied().find(|&o| class_of(o) == Some(*b"HEAD")).ok_or_else(|| perr("no OMF header object".into()))?; + let be = c.get(head, "OMFI:HEAD:ByteOrder").is_none_or(|v| v.first() != Some(&0x49)); + let mut r = R { c: &c, report, be, mobs: HashMap::new(), media: HashMap::new(), doc: Document::default(), sources: HashMap::new() }; + let mob_list = r.refs(head, "OMFI:HEAD:Mobs"); + let mob_list = if mob_list.is_empty() { + c.objects.keys().copied().filter(|&o| matches!(class_of(o).as_ref(), Some(b"CMOB" | b"MMOB" | b"SMOB"))).collect() + } else { + mob_list + }; + for &m in &mob_list { + if let Some(id) = r.uid(m, "OMFI:MOBJ:MobID") { + r.mobs.insert(id, m); + } + } + let md = r.refs(head, "OMFI:HEAD:MediaData"); + let md = + if md.is_empty() { c.objects.keys().copied().filter(|&o| matches!(class_of(o).as_ref(), Some(b"WAVE" | b"AIFC"))).collect() } else { md }; + for m in md { + let Some(id) = r.uid(m, "OMFI:MDAT:MobID") else { continue }; + if let Some(d) = c.get(m, "OMFI:WAVE:Data") { + r.media.insert(id, d); + } else if let Some(d) = c.get(m, "OMFI:AIFC:Data") { + r.media.insert(id, d); + } + } + let cmobs: Vec = mob_list.iter().copied().filter(|&m| class_of(m) == Some(*b"CMOB")).collect(); + let mut referenced = HashSet::new(); + let mut visited = HashSet::new(); + let mut visits = 0; + for &m in &cmobs { + r.collect_refs(m, &mut referenced, &mut visited, &mut visits, 0); + } + let primary: Vec = r.refs(head, "OMFI:HEAD:PrimaryMobs").into_iter().filter(|m| cmobs.contains(m)).collect(); + let top: Vec = if !primary.is_empty() { + primary + } else { + cmobs.iter().copied().filter(|m| r.uid(*m, "OMFI:MOBJ:MobID").is_none_or(|id| !referenced.contains(&id))).collect() + }; + for m in top { + let comp = r.composition(m); + r.doc.compositions.push(comp); + } + if r.doc.compositions.is_empty() { + return Err(Error::Empty); + } + Ok(r.doc) +} + +impl R<'_, '_> { + fn class(&self, o: u32) -> [u8; 4] { + self.c.get(o, "OMFI:OOBJ:ObjClass").and_then(|v| v.get(..4).map(|b| [b[0], b[1], b[2], b[3]])).unwrap_or(*b"????") + } + fn u32_of(&self, b: &[u8]) -> Option { + let a: [u8; 4] = b.get(..4)?.try_into().ok()?; + Some(if self.be { u32::from_be_bytes(a) } else { u32::from_le_bytes(a) }) + } + fn i32(&self, o: u32, p: &str) -> Option { + self.c.get(o, p).and_then(|v| self.u32_of(&v)).map(|x| x as i32 as i64) + } + fn u32(&self, o: u32, p: &str) -> Option { + self.c.get(o, p).and_then(|v| self.u32_of(&v)) + } + fn u16(&self, o: u32, p: &str) -> Option { + let v = self.c.get(o, p)?; + let a: [u8; 2] = v.get(..2)?.try_into().ok()?; + Some(if self.be { u16::from_be_bytes(a) } else { u16::from_le_bytes(a) }) + } + fn rational(&self, o: u32, p: &str) -> Option<(i64, i64)> { + let v = self.c.get(o, p)?; + self.rational_of(&v) + } + fn rational_of(&self, v: &[u8]) -> Option<(i64, i64)> { + Some((self.u32_of(v)? as i32 as i64, self.u32_of(v.get(4..)?)? as i32 as i64)) + } + fn string(&self, o: u32, p: &str) -> Option { + self.c.get(o, p).map(|v| String::from_utf8_lossy(&v).trim_end_matches('\0').to_string()) + } + fn uid(&self, o: u32, p: &str) -> Option { + self.c.get(o, p).and_then(|v| v.get(..12).and_then(|b| b.try_into().ok())) + } + fn r#ref(&self, o: u32, p: &str) -> Option { + let v = self.c.get(o, p)?; + let key = u32::from_be_bytes(v.get(..4)?.try_into().ok()?); + Some(self.c.resolve(o, key)) + } + fn refs(&self, o: u32, p: &str) -> Vec { + let Some(v) = self.c.get(o, p) else { return Vec::new() }; + if v.len() < 2 { + return Vec::new(); + } + let n = u16::from_be_bytes([v[0], v[1]]) as usize; + v[2..].as_chunks::<4>().0.iter().take(n).map(|k| self.c.resolve(o, u32::from_be_bytes([k[0], k[1], k[2], k[3]]))).collect() + } + fn data_kind(&self, o: u32) -> String { + self.r#ref(o, "OMFI:CPNT:DataKind").and_then(|d| self.string(d, "OMFI:DDEF:DataKindID")).unwrap_or_default() + } + fn len(&self, o: u32) -> i64 { + self.i32(o, "OMFI:CPNT:Length").unwrap_or(0).max(0) + } + + fn collect_refs(&self, o: u32, out: &mut HashSet, visited: &mut HashSet, visits: &mut usize, depth: usize) { + if depth > 64 || !visited.insert(o) || *visits >= MAX_REFERENCE_VISITS { + return; + } + *visits += 1; + if self.class(o) == *b"SCLP" + && let Some(id) = self.uid(o, "OMFI:SCLP:SourceID") + { + out.insert(id); + } + for p in ["OMFI:MOBJ:Slots", "OMFI:SEQU:Components", "OMFI:EFFE:EffectSlots"] { + for c in self.refs(o, p) { + self.collect_refs(c, out, visited, visits, depth + 1); + } + } + for p in ["OMFI:MSLT:Segment", "OMFI:ESLT:ArgValue"] { + if let Some(c) = self.r#ref(o, p) { + self.collect_refs(c, out, visited, visits, depth + 1); + } + } + } + + fn composition(&mut self, m: u32) -> Composition { + let name = self.string(m, "OMFI:MOBJ:Name").unwrap_or_else(|| "Sequence".into()); + let mut comp = Composition { + name, + rate: FrameRate::FPS_25, + sample_rate: 48_000, + width: 1920, + height: 1080, + start_tc: 0, + drop: false, + tracks: Vec::new(), + markers: Vec::new(), + }; + let slots = self.refs(m, "OMFI:MOBJ:Slots"); + let mut na = 0; + for s in slots { + let Some(seg) = self.r#ref(s, "OMFI:MSLT:Segment") else { continue }; + let rate = self.rational(s, "OMFI:MSLT:EditRate").filter(|r| r.0 > 0 && r.1 > 0).unwrap_or((25, 1)); + let kind = self.data_kind(seg); + if self.class(seg) == *b"TCCP" || kind == "omfi:data:Timecode" { + comp.rate = FrameRate::new(rate.0, rate.1); + if self.class(seg) == *b"TCCP" { + comp.start_tc = self.i32(seg, "OMFI:TCCP:Start").unwrap_or(0); + comp.drop = self.c.get(seg, "OMFI:TCCP:Drop").and_then(|v| v.first().copied()).unwrap_or(0) != 0; + } + continue; + } + let ckind = match kind.as_str() { + "omfi:data:Sound" => CKind::Sound, + "omfi:data:Picture" | "omfi:data:PictureWithMatte" => CKind::Picture, + _ => { + self.report.info("an OMF slot of an unsupported data kind was skipped"); + continue; + } + }; + if ckind == CKind::Picture { + self.report.info("OMF picture slots are not imported (audio only)"); + continue; + } + if rate.1 == 1 && rate.0 >= 8000 { + comp.sample_rate = rate.0 as u32; + } + let items = self.segment(seg, rate, 0); + na += 1; + let td = self.r#ref(s, "OMFI:MSLT:TrackDesc"); + let tname = td.and_then(|t| self.string(t, "OMFI:TRKD:TrackName")).unwrap_or_else(|| format!("A{na}")); + let number = td.and_then(|t| self.u32(t, "OMFI:TRKD:PhysicalTrack")).unwrap_or(na); + let channels = items + .iter() + .filter_map(|i| match i { + CItem::Clip(c) => Some(self.doc.sources[c.source].channels), + _ => None, + }) + .max() + .unwrap_or(1) + .min(6); + comp.tracks.push(CTrack { kind: CKind::Sound, name: tname, number, channels, items }); + } + comp + } + + fn segment(&mut self, seg: u32, rate: (i64, i64), depth: usize) -> Vec { + let len = units_to_ticks(self.len(seg), rate.0, rate.1); + if depth > 32 { + return vec![CItem::Filler(len)]; + } + match &self.class(seg) { + b"SEQU" => { + let mut out = Vec::new(); + for c in self.refs(seg, "OMFI:SEQU:Components") { + out.extend(self.segment(c, rate, depth + 1)); + } + out + } + b"FILL" => vec![CItem::Filler(len)], + b"SCLP" => vec![self.clip(seg, rate)], + b"TRAN" => { + let cut = units_to_ticks(self.i32(seg, "OMFI:TRAN:CutPoint").unwrap_or(0), rate.0, rate.1); + let effect = self + .r#ref(seg, "OMFI:TRAN:Effect") + .and_then(|e| self.string(e, "FilmCraft:EFFE:EffectID")) + .unwrap_or_else(|| "constant_power".into()); + vec![CItem::Transition(CTransition { len, cut: cut.clamp(Tick::ZERO, len), effect })] + } + b"EFFE" => { + let kind = self.r#ref(seg, "OMFI:EFFE:EffectKind").and_then(|k| self.string(k, "OMFI:EDEF:EffectID")).unwrap_or_default(); + let slots = self.refs(seg, "OMFI:EFFE:EffectSlots"); + let arg = |r: &Self, id: i64| { + slots.iter().copied().find(|&s| r.i32(s, "OMFI:ESLT:ArgID") == Some(id)).and_then(|s| r.r#ref(s, "OMFI:ESLT:ArgValue")) + }; + let Some(input) = arg(self, -1).or_else(|| arg(self, 1).filter(|_| kind != "omfi:effect:MonoAudioGain")) else { + return vec![CItem::Filler(len)]; + }; + let gain = if kind == "omfi:effect:MonoAudioGain" { + arg(self, 1).and_then(|v| self.gain(v, len)) + } else { + self.report.warn("OMF effects other than audio gain are not imported (their input is used)"); + None + }; + let mut items = self.segment(input, rate, depth + 1); + if let (Some(g), [CItem::Clip(c)]) = (gain, items.as_mut_slice()) { + c.gain = Some(g); + } + items + } + _ => { + self.report.info("an unsupported OMF component was imported as a gap"); + vec![CItem::Filler(len)] + } + } + } + + fn amp(&self, v: &[u8]) -> Option { + self.rational_of(v).filter(|r| r.1 != 0).map(|(n, d)| n as f64 / d as f64) + } + + fn gain(&mut self, v: u32, len: Tick) -> Option { + match &self.class(v) { + b"CVAL" => self.c.get(v, "OMFI:CVAL:Value").and_then(|d| self.amp(&d)).map(Gain::Constant), + b"VVAL" => { + let linear = self.u16(v, "OMFI:VVAL:Interpolation") != Some(1); + let points: Vec<(Tick, f64)> = self + .refs(v, "OMFI:VVAL:PointList") + .into_iter() + .filter_map(|p| { + let (n, d) = self.rational(p, "OMFI:CTLP:Time").filter(|r| r.1 != 0)?; + let a = self.c.get(p, "OMFI:CTLP:Value").and_then(|d| self.amp(&d))?; + Some((Tick((len.0 as i128 * n as i128 / d as i128).clamp(-(1i128 << 56), 1i128 << 56) as i64), a)) + }) + .collect(); + (!points.is_empty()).then_some(Gain::Varying { linear, points }) + } + _ => None, + } + } + + fn clip(&mut self, c: u32, rate: (i64, i64)) -> CItem { + let len = units_to_ticks(self.len(c), rate.0, rate.1); + let start = units_to_ticks(self.i32(c, "OMFI:SCLP:StartTime").unwrap_or(0), rate.0, rate.1); + let name = self.string(c, "FilmCraft:SCLP:ClipName").unwrap_or_default(); + let (Some(id), Some(track)) = (self.uid(c, "OMFI:SCLP:SourceID"), self.u32(c, "OMFI:SCLP:SourceTrackID")) else { return CItem::Filler(len) }; + if id == [0; 12] { + return CItem::Filler(len); + } + match self.resolve(id, track, start, None, 0) { + Some((source, t)) => CItem::Clip(CClip { len, source, start: t, gain: None, name }), + None => CItem::Filler(len), + } + } + + fn slot_of(&self, mob: u32, track: u32) -> Option { + self.refs(mob, "OMFI:MOBJ:Slots") + .into_iter() + .find(|&s| self.r#ref(s, "OMFI:MSLT:TrackDesc").and_then(|t| self.u32(t, "OMFI:TRKD:TrackID")) == Some(track)) + } + + fn first_clip(&self, seg: u32) -> Option { + match &self.class(seg) { + b"SCLP" => Some(seg), + b"SEQU" => self.refs(seg, "OMFI:SEQU:Components").into_iter().find_map(|c| self.first_clip(c)), + _ => None, + } + } + + fn resolve(&mut self, id: Uid, track: u32, start: Tick, master: Option, depth: usize) -> Option<(usize, Tick)> { + if depth > 16 { + return None; + } + let Some(&mob) = self.mobs.get(&id) else { + self.report.warn("a clip references a mob that is not in the file (imported as a gap)"); + return None; + }; + let slot = self.slot_of(mob, track)?; + let rate = self.rational(slot, "OMFI:MSLT:EditRate").filter(|r| r.0 > 0 && r.1 > 0).unwrap_or((48_000, 1)); + let seg = self.r#ref(slot, "OMFI:MSLT:Segment")?; + match &self.class(mob) { + b"MMOB" => { + let key = master.unwrap_or_else(|| id.iter().map(|b| format!("{b:02x}")).collect()); + let clip = self.first_clip(seg)?; + let inner = units_to_ticks(self.i32(clip, "OMFI:SCLP:StartTime").unwrap_or(0), rate.0, rate.1); + let next = self.uid(clip, "OMFI:SCLP:SourceID")?; + let next_track = self.u32(clip, "OMFI:SCLP:SourceTrackID")?; + let name = self.string(mob, "OMFI:MOBJ:Name"); + let r = self.resolve(next, next_track, start + inner, Some(key), depth + 1)?; + if let Some(n) = name { + self.doc.sources[r.0].name = n; + } + Some(r) + } + b"SMOB" => { + let desc = self.r#ref(mob, "OMFI:SMOB:MediaDescription")?; + let sr = self.rational(desc, "OMFI:MDFL:SampleRate"); + if sr.is_none() && !self.media.contains_key(&id) { + return None; // a tape / physical source + } + let key = master.unwrap_or_else(|| id.iter().map(|b| format!("{b:02x}")).collect()); + let skey = (id, track, key.clone()); + if let Some(&i) = self.sources.get(&skey) { + return Some((i, start)); + } + let file_rate = sr.filter(|r| r.0 > 0 && r.1 > 0).unwrap_or(rate); + let length = units_to_ticks(self.i32(desc, "OMFI:MDFL:Length").unwrap_or(0), file_rate.0, file_rate.1); + let summary = self.c.get(desc, "OMFI:WAVD:Summary").or_else(|| self.c.get(desc, "OMFI:AIFD:Summary")).unwrap_or_default(); + let fmt = crate::pcm::parse_wav(&summary).or_else(|| crate::pcm::parse_aiff(&summary)).map(|(_, ch, sr, bits)| (ch as u32, sr, bits)); + let mut path = None; + for l in self.refs(desc, "OMFI:MDES:Locator") { + let p = match &self.class(l) { + b"UNXL" => self.string(l, "OMFI:UNXL:PathName"), + b"DOSL" => self.string(l, "OMFI:DOSL:PathName"), + b"MACL" => self.string(l, "OMFI:MACL:PathName"), + b"TXTL" => self.string(l, "OMFI:TXTL:Name"), + b"NETL" => self.string(l, "OMFI:NETL:URLString").map(|u| craft_timeline::path::file_url_to_path(&u)), + _ => None, + }; + if p.is_some() { + path = p; + break; + } + } + let sound_slots = self.refs(mob, "OMFI:MOBJ:Slots").len(); + let (file_channels, sample_rate, bits) = fmt.unwrap_or((1, file_rate.0 as u32, 16)); + let channel = (file_channels > 1 && sound_slots > 1).then(|| track.saturating_sub(1).min(file_channels - 1)); + // timecode of the physical source + let tc = self.first_clip(seg).and_then(|clip| { + let tid = self.uid(clip, "OMFI:SCLP:SourceID")?; + let tape = *self.mobs.get(&tid)?; + let (start_tc, fps, trate) = self.refs(tape, "OMFI:MOBJ:Slots").into_iter().find_map(|s| { + let g = self.r#ref(s, "OMFI:MSLT:Segment")?; + (self.class(g) == *b"TCCP").then(|| { + ( + self.i32(g, "OMFI:TCCP:Start").unwrap_or(0), + self.u16(g, "OMFI:TCCP:FPS").unwrap_or(25), + self.rational(s, "OMFI:MSLT:EditRate"), + ) + }) + })?; + let tcr = trate + .filter(|r| r.0 > 0 && r.1 > 0) + .map(|r| FrameRate::new(r.0, r.1)) + .unwrap_or_else(|| FrameRate::new(fps.max(1) as i64, 1)); + let off = units_to_ticks(self.i32(clip, "OMFI:SCLP:StartTime").unwrap_or(0), rate.0, rate.1); + Some((start_tc + tcr.frame_at(off), tcr)) + }); + let mut s = Source { + key, + name: self.string(mob, "OMFI:MOBJ:Name").unwrap_or_default(), + kind: CKind::Sound, + path, + channel, + channels: if channel.is_some() { 1 } else { file_channels }, + file_channels, + width: 0, + height: 0, + frame_rate: FrameRate::FPS_25, + sample_rate, + bits, + length, + offset: Tick::ZERO, + start_tc: tc.map(|t| t.0), + tc_rate: tc.map(|t| t.1).unwrap_or(FrameRate::FPS_25), + embedded: None, + markers: Vec::new(), + nested: None, + }; + if let Some(d) = self.media.get(&id) { + if let Some((pcm, ch, sr, b)) = crate::pcm::parse_wav(d).or_else(|| crate::pcm::parse_aiff(d)) { + s.embedded = Some(pcm); + s.file_channels = ch as u32; + s.channels = if s.channel.is_some() { 1 } else { ch as u32 }; + s.sample_rate = sr; + s.bits = b; + } else { + self.report.warn("embedded OMF audio in an unsupported format was skipped"); + } + } + let i = self.doc.sources.len(); + self.doc.sources.push(s); + self.sources.insert(skey, i); + Some((i, start)) + } + _ => { + self.report.warn("nested OMF compositions are not supported (imported as gaps)"); + None + } + } + } +} diff --git a/crates/aaf/src/omf/tests.rs b/crates/aaf/src/omf/tests.rs new file mode 100644 index 000000000..c6f4ef9f0 --- /dev/null +++ b/crates/aaf/src/omf/tests.rs @@ -0,0 +1,135 @@ +//! Structural checks of written OMF files: the Bento label and TOC, and the required properties of +//! the OMF Interchange 2.0 classes written. + +use std::collections::HashMap; + +use super::bento::{self, Container}; +use super::*; +use crate::testdoc::{IDENTITY, sound_only}; + +/// Every object of a class written has the properties the specification requires of it. +fn assert_required_properties(c: &Container<'_>, by_class: &HashMap>) { + let required: &[(&str, &[&str])] = &[ + ( + "HEAD", + &[ + "OMFI:HEAD:ByteOrder", + "OMFI:HEAD:LastModified", + "OMFI:HEAD:Version", + "OMFI:HEAD:Mobs", + "OMFI:HEAD:MediaData", + "OMFI:HEAD:DefinitionObjects", + ], + ), + ("CMOB", &["OMFI:MOBJ:MobID", "OMFI:MOBJ:Slots", "OMFI:MOBJ:LastModified", "OMFI:MOBJ:CreationTime"]), + ("MMOB", &["OMFI:MOBJ:MobID", "OMFI:MOBJ:Slots", "OMFI:MOBJ:LastModified", "OMFI:MOBJ:CreationTime"]), + ("SMOB", &["OMFI:MOBJ:MobID", "OMFI:MOBJ:Slots", "OMFI:SMOB:MediaDescription"]), + ("MSLT", &["OMFI:MSLT:Segment", "OMFI:MSLT:EditRate", "OMFI:MSLT:TrackDesc"]), + ("TRKD", &["OMFI:TRKD:Origin", "OMFI:TRKD:TrackID"]), + ("SEQU", &["OMFI:CPNT:DataKind", "OMFI:CPNT:Length", "OMFI:SEQU:Components"]), + ("SCLP", &["OMFI:CPNT:DataKind", "OMFI:CPNT:Length", "OMFI:SCLP:SourceID", "OMFI:SCLP:SourceTrackID", "OMFI:SCLP:StartTime"]), + ("FILL", &["OMFI:CPNT:DataKind", "OMFI:CPNT:Length"]), + ("TRAN", &["OMFI:CPNT:DataKind", "OMFI:CPNT:Length", "OMFI:TRAN:CutPoint", "OMFI:TRAN:Effect"]), + ("EFFE", &["OMFI:CPNT:DataKind", "OMFI:CPNT:Length", "OMFI:EFFE:EffectKind"]), + ("ESLT", &["OMFI:ESLT:ArgID", "OMFI:ESLT:ArgValue"]), + ("CVAL", &["OMFI:CPNT:DataKind", "OMFI:CPNT:Length", "OMFI:CVAL:Value"]), + ("EDEF", &["OMFI:EDEF:EffectID"]), + ("DDEF", &["OMFI:DDEF:DataKindID"]), + ("TCCP", &["OMFI:CPNT:DataKind", "OMFI:CPNT:Length", "OMFI:TCCP:Start", "OMFI:TCCP:FPS", "OMFI:TCCP:Drop"]), + ("WAVD", &["OMFI:MDFL:IsOMFI", "OMFI:MDFL:SampleRate", "OMFI:MDFL:Length", "OMFI:WAVD:Summary"]), + ("WAVE", &["OMFI:MDAT:MobID", "OMFI:WAVE:Data"]), + ("IDNT", &["OMFI:IDNT:CompanyName", "OMFI:IDNT:ProductName"]), + ]; + for (k, props) in required { + let Some(objs) = by_class.get(*k) else { + assert_eq!(*k, "FILL", "no {k} object"); + continue; + }; + for &o in objs { + for prop in *props { + assert!(c.get(o, prop).is_some(), "{k} {o} lacks {prop}"); + } + } + } +} + +#[test] +fn written_files_have_the_required_structure() { + let mut report = Report::default(); + let bytes = write(&sound_only(Some(vec![7; 480 * 4])), &IDENTITY, &mut report).unwrap(); + assert!(!report.has_warnings(), "{report}"); + // label + let l = &bytes[bytes.len() - bento::LABEL_SIZE..]; + assert_eq!(&l[..8], &bento::MAGIC); + assert_eq!(u16::from_be_bytes([l[12], l[13]]), 1, "Bento major version"); + let c = Container::open(&bytes).unwrap(); + let class = |o: u32| c.get(o, "OMFI:OOBJ:ObjClass").map(|v| String::from_utf8_lossy(&v).to_string()); + let mut by_class: HashMap> = HashMap::new(); + for &o in c.objects.keys() { + if let Some(k) = class(o) { + by_class.entry(k).or_default().push(o); + } + } + assert_required_properties(&c, &by_class); + assert_eq!(by_class["HEAD"].len(), 1); + assert_eq!(by_class["CMOB"].len(), 1); + let head = by_class["HEAD"][0]; + assert_eq!(c.get(head, "OMFI:HEAD:Version"), Some(vec![2, 0])); + assert_eq!(c.get(head, "OMFI:HEAD:ByteOrder"), Some(b"MM".to_vec())); + // object references resolve to objects of the right class + let refs = |o: u32, p: &str| -> Vec { + let v = c.get(o, p).unwrap(); + let n = u16::from_be_bytes([v[0], v[1]]) as usize; + v[2..].as_chunks::<4>().0.iter().take(n).map(|k| c.resolve(o, u32::from_be_bytes([k[0], k[1], k[2], k[3]]))).collect() + }; + let one = |o: u32, p: &str| -> u32 { c.resolve(o, u32::from_be_bytes(c.get(o, p).unwrap()[..4].try_into().unwrap())) }; + for m in refs(head, "OMFI:HEAD:Mobs") { + assert!(matches!(class(m).as_deref(), Some("CMOB" | "MMOB" | "SMOB"))); + for s in refs(m, "OMFI:MOBJ:Slots") { + assert_eq!(class(s).as_deref(), Some("MSLT")); + assert_eq!(class(one(s, "OMFI:MSLT:TrackDesc")).as_deref(), Some("TRKD")); + } + } + // embedded media: WAVE data is a WAVE file of the file mob it names + for w in &by_class["WAVE"] { + let data = c.get(*w, "OMFI:WAVE:Data").unwrap(); + let (pcm, ch, sr, bits) = crate::pcm::parse_wav(&data).unwrap(); + assert_eq!((pcm.len(), ch, sr, bits), (480 * 4, 2, 48_000, 16)); + let id = c.get(*w, "OMFI:MDAT:MobID").unwrap(); + assert!(by_class["SMOB"].iter().any(|s| c.get(*s, "OMFI:MOBJ:MobID") == Some(id.clone()))); + } + // the composition's sound sequence: clip, transition, gain effect around a clip + let cm = by_class["CMOB"][0]; + let slots = refs(cm, "OMFI:MOBJ:Slots"); + assert_eq!(class(one(slots[0], "OMFI:MSLT:Segment")).as_deref(), Some("TCCP")); + let seq = one(slots[1], "OMFI:MSLT:Segment"); + let comps = refs(seq, "OMFI:SEQU:Components"); + let classes: Vec = comps.iter().map(|&o| class(o).unwrap()).collect(); + assert_eq!(classes, vec!["EFFE", "TRAN", "SCLP"]); + let len = |o: u32| i32::from_be_bytes(c.get(o, "OMFI:CPNT:Length").unwrap()[..4].try_into().unwrap()) as i64; + let samples = |f: i64| f * 48_000 / 25; + assert_eq!(len(comps[0]), samples(55)); + assert_eq!(len(comps[1]), samples(10)); + assert_eq!(len(comps[2]), samples(55)); + assert_eq!(len(seq), samples(100)); + assert_eq!(c.get(one(comps[0], "OMFI:EFFE:EffectKind"), "OMFI:EDEF:EffectID"), Some(b"omfi:effect:MonoAudioGain\0".to_vec())); +} + +#[test] +fn bento_values_round_trip() { + let mut w = bento::Writer::new(); + let a = w.new_object(); + let b = w.new_object(); + w.set(a, "Test:Small", "test:Bytes", &[1, 2]); + w.set(a, "Test:Empty", "test:Bytes", &[]); + w.set(b, "Test:Big", "test:Bytes", &vec![9; 100_000]); + w.set_references(a, &[b]); + let bytes = w.finish(); + let c = Container::open(&bytes).unwrap(); + assert_eq!(c.get(a, "Test:Small"), Some(vec![1, 2])); + assert_eq!(c.get(a, "Test:Empty"), Some(vec![])); + assert_eq!(c.get(b, "Test:Big").map(|v| v.len()), Some(100_000)); + assert_eq!(c.resolve(a, b), b); + assert_eq!(c.name_of(c.id_of("Test:Big").unwrap()), Some("Test:Big")); + assert!(Container::open(&bytes[..bytes.len() - 1]).is_err()); +} diff --git a/crates/aaf/src/omf/write.rs b/crates/aaf/src/omf/write.rs new file mode 100644 index 000000000..755994804 --- /dev/null +++ b/crates/aaf/src/omf/write.rs @@ -0,0 +1,419 @@ +//! Composition model → OMF 2.0 objects in a Bento container. + +use std::collections::HashMap; + +use craft_timeline::time::Tick; + +use super::bento; +use crate::model::{CItem, CKind, Document, Gain, Source, ticks_to_units}; +use crate::{Error, Identity, Report, Result}; + +/// An OMF mob id (`omfi:UID`): prefix, major, minor. +pub(crate) type Uid = [u8; 12]; + +pub(crate) fn uid(prefix: u32, major: u32, minor: u32) -> Uid { + let mut u = [0u8; 12]; + u[..4].copy_from_slice(&prefix.to_be_bytes()); + u[4..8].copy_from_slice(&major.to_be_bytes()); + u[8..].copy_from_slice(&minor.to_be_bytes()); + u +} + +struct W<'a> { + b: bento::Writer, + refs: HashMap>, + ddefs: HashMap<&'static str, u32>, + edefs: HashMap<&'static str, u32>, + defs: Vec, + report: &'a mut Report, + inexact: bool, +} + +fn rational(n: i64, d: i64) -> Vec { + let mut v = (n.clamp(i32::MIN as i64, i32::MAX as i64) as i32).to_be_bytes().to_vec(); + v.extend_from_slice(&(d.clamp(i32::MIN as i64, i32::MAX as i64) as i32).to_be_bytes()); + v +} + +fn cstr(s: &str) -> Vec { + let mut v = s.as_bytes().to_vec(); + v.push(0); + v +} + +impl W<'_> { + fn obj(&mut self, class: &[u8; 4]) -> u32 { + let id = self.b.new_object(); + self.b.set(id, "OMFI:OOBJ:ObjClass", "omfi:ClassID", class); + id + } + fn str(&mut self, o: u32, p: &str, v: &str) { + self.b.set(o, p, "omfi:String", &cstr(v)); + } + fn i32(&mut self, o: u32, p: &str, ty: &str, v: i64) { + if v > i32::MAX as i64 || v < i32::MIN as i64 { + self.report.warn("a time does not fit OMF's 32-bit positions and was clamped"); + } + self.b.set(o, p, ty, &(v.clamp(i32::MIN as i64, i32::MAX as i64) as i32).to_be_bytes()); + } + fn r#ref(&mut self, o: u32, p: &str, target: u32) { + self.b.set(o, p, "omfi:ObjRef", &target.to_be_bytes()); + self.refs.entry(o).or_default().push(target); + } + fn refs(&mut self, o: u32, p: &str, targets: &[u32]) { + let mut v = (targets.len() as u16).to_be_bytes().to_vec(); + for t in targets { + v.extend_from_slice(&t.to_be_bytes()); + } + self.b.set(o, p, "omfi:ObjRefArray", &v); + self.refs.entry(o).or_default().extend_from_slice(targets); + } + fn u(&mut self, t: Tick, rate: (i64, i64)) -> i64 { + let (n, exact) = ticks_to_units(t, rate.0, rate.1); + if !exact { + self.inexact = true; + } + n + } + fn ddef(&mut self, kind: &'static str) -> u32 { + if let Some(&d) = self.ddefs.get(kind) { + return d; + } + let d = self.obj(b"DDEF"); + self.b.set(d, "OMFI:DDEF:DataKindID", "omfi:UniqueName", &cstr(kind)); + self.ddefs.insert(kind, d); + self.defs.push(d); + d + } + fn edef(&mut self, id: &'static str, name: &str) -> u32 { + if let Some(&d) = self.edefs.get(id) { + return d; + } + let d = self.obj(b"EDEF"); + self.b.set(d, "OMFI:EDEF:EffectID", "omfi:UniqueName", &cstr(id)); + self.str(d, "OMFI:EDEF:EffectName", name); + self.b.set(d, "OMFI:EDEF:Bypass", "omfi:ArgIDType", &(-1i32).to_be_bytes()); + self.edefs.insert(id, d); + self.defs.push(d); + d + } + fn cpnt(&mut self, o: u32, kind: &'static str, len: i64) { + let d = self.ddef(kind); + self.r#ref(o, "OMFI:CPNT:DataKind", d); + self.i32(o, "OMFI:CPNT:Length", "omfi:Length32", len); + } + fn mob(&mut self, class: &[u8; 4], id: Uid, name: &str, slots: &[u32]) -> u32 { + let m = self.obj(class); + self.b.set(m, "OMFI:MOBJ:MobID", "omfi:UID", &id); + self.str(m, "OMFI:MOBJ:Name", name); + self.refs(m, "OMFI:MOBJ:Slots", slots); + self.b.set(m, "OMFI:MOBJ:LastModified", "omfi:TimeStamp", &[0, 0, 0, 0, 1]); + self.b.set(m, "OMFI:MOBJ:CreationTime", "omfi:TimeStamp", &[0, 0, 0, 0, 1]); + m + } + fn slot(&mut self, rate: (i64, i64), segment: u32, track_id: u32, name: &str, physical: u32) -> u32 { + let t = self.obj(b"TRKD"); + self.i32(t, "OMFI:TRKD:Origin", "omfi:Position32", 0); + self.b.set(t, "OMFI:TRKD:TrackID", "omfi:UInt32", &track_id.to_be_bytes()); + self.str(t, "OMFI:TRKD:TrackName", name); + if physical > 0 { + self.b.set(t, "OMFI:TRKD:PhysicalTrack", "omfi:UInt32", &physical.to_be_bytes()); + } + let s = self.obj(b"MSLT"); + self.b.set(s, "OMFI:MSLT:EditRate", "omfi:Rational", &rational(rate.0, rate.1)); + self.r#ref(s, "OMFI:MSLT:Segment", segment); + self.r#ref(s, "OMFI:MSLT:TrackDesc", t); + s + } + fn sclp(&mut self, kind: &'static str, len: i64, source: Uid, track: u32, start: i64) -> u32 { + let c = self.obj(b"SCLP"); + self.cpnt(c, kind, len); + self.b.set(c, "OMFI:SCLP:SourceID", "omfi:UID", &source); + self.b.set(c, "OMFI:SCLP:SourceTrackID", "omfi:UInt32", &track.to_be_bytes()); + self.i32(c, "OMFI:SCLP:StartTime", "omfi:Position32", start); + c + } + fn tccp(&mut self, len: i64, start: i64, fps: i64, drop: bool) -> u32 { + let t = self.obj(b"TCCP"); + self.cpnt(t, "omfi:data:Timecode", len); + self.i32(t, "OMFI:TCCP:Start", "omfi:Position32", start); + self.b.set(t, "OMFI:TCCP:FPS", "omfi:UInt16", &(fps.clamp(1, u16::MAX as i64) as u16).to_be_bytes()); + self.b.set(t, "OMFI:TCCP:Drop", "omfi:Boolean", &[drop as u8]); + t + } + fn value(&mut self, len: i64, g: &Gain, seg_len: i64, rate: (i64, i64), base: Tick) -> u32 { + match g { + Gain::Constant(a) => { + let v = self.obj(b"CVAL"); + self.cpnt(v, "omfi:data:Rational", len); + self.b.set(v, "OMFI:CVAL:Value", "omfi:DataValue", &(*a)); + v + } + Gain::Varying { linear, points } => { + let mut pts = Vec::new(); + for (off, a) in points { + let o = self.u(base + *off, rate) - self.u(base, rate); + let p = self.obj(b"CTLP"); + self.b.set(p, "OMFI:CTLP:Time", "omfi:Rational", &rational(o, seg_len.max(1))); + self.b.set(p, "OMFI:CTLP:Value", "omfi:DataValue", &(*a)); + self.b.set(p, "OMFI:CTLP:EditHint", "omfi:EditHintType", &[0]); + pts.push(p); + } + let v = self.obj(b"VVAL"); + self.cpnt(v, "omfi:data:Rational", len); + self.b.set(v, "OMFI:VVAL:Interpolation", "omfi:InterpKind", &(if *linear { 2u16 } else { 1u16 }).to_be_bytes()); + self.refs(v, "OMFI:VVAL:PointList", &pts); + v + } + } + } +} + +fn fnv(s: &[u8], h: u64) -> u64 { + s.iter().fold(h, |h, &b| (h ^ b as u64).wrapping_mul(0x0000_0100_0000_01B3)) +} + +fn amp(a: f64) -> Vec { + rational((a.max(0.0) * 65536.0).round().min(i32::MAX as f64) as i64, 65536) +} + +fn rate_of(s: &Source) -> (i64, i64) { + match s.kind { + CKind::Picture => (s.frame_rate.num, s.frame_rate.den), + CKind::Sound => (s.sample_rate.max(1) as i64, 1), + } +} + +pub(crate) fn write(doc: &Document, identity: &Identity<'_>, report: &mut Report) -> Result> { + let comp = doc.compositions.first().ok_or(Error::Empty)?; + let mut w = + W { b: bento::Writer::new(), refs: HashMap::new(), ddefs: HashMap::new(), edefs: HashMap::new(), defs: Vec::new(), report, inexact: false }; + let mut seed = fnv(comp.name.as_bytes(), 0xCBF2_9CE4_8422_2325); + for s in &doc.sources { + seed = fnv(s.path.as_deref().unwrap_or("").as_bytes(), fnv(s.key.as_bytes(), seed)); + } + let major = (seed >> 32) as u32 ^ seed as u32; + let mut minor = 0u32; + let mut new_uid = || { + minor += 1; + uid(0x464C_4D43, major, minor) + }; + let mut mobs = Vec::new(); + let mut media_data = Vec::new(); + let mut master_ref: HashMap = HashMap::new(); + // media + let mut groups: Vec<(String, Vec)> = Vec::new(); + for (i, s) in doc.sources.iter().enumerate() { + if s.kind != CKind::Sound { + continue; + } + match groups.iter_mut().find(|(k, _)| *k == s.key) { + Some((_, v)) => v.push(i), + None => groups.push((s.key.clone(), vec![i])), + } + } + for (_, members) in &groups { + let first = &doc.sources[members[0]]; + let master = new_uid(); + let tc = members.iter().find_map(|&i| doc.sources[i].start_tc.map(|t| (t, doc.sources[i].tc_rate))); + let tape = new_uid(); + let mut tape_slots = Vec::new(); + if let Some((start, r)) = tc { + let len = members.iter().map(|&i| doc.sources[i].length).max().unwrap_or(Tick::ZERO); + let l = w.u(len, (r.num, r.den)); + let t = w.tccp(l, start, r.timecode_base(), false); + tape_slots.push(w.slot((r.num, r.den), t, 1, "TC1", 0)); + } + let mut master_slots = Vec::new(); + for (k, &si) in members.iter().enumerate() { + let s = &doc.sources[si]; + let rate = rate_of(s); + let len = w.u(s.length, rate); + // file mob + let file = new_uid(); + let file_track = s.channel.map_or(1, |c| c + 1); + let tape_track = 2 + k as u32; + let to_tape = + if tc.is_some() { w.sclp("omfi:data:Sound", len, tape, tape_track, 0) } else { w.sclp("omfi:data:Sound", len, uid(0, 0, 0), 0, 0) }; + let fslot = w.slot(rate, to_tape, file_track, &format!("A{file_track}"), file_track); + let desc = descriptor(&mut w, s, len); + let fm = w.mob(b"SMOB", file, &s.name, &[fslot]); + w.r#ref(fm, "OMFI:SMOB:MediaDescription", desc); + mobs.push(fm); + if let Some(pcm) = &s.embedded { + let md = w.obj(b"WAVE"); + w.b.set(md, "OMFI:MDAT:MobID", "omfi:UID", &file); + w.b.set( + md, + "OMFI:WAVE:Data", + "omfi:DataValue", + &craft_audio::pcm_wav_file(pcm, u16::try_from(s.file_channels.max(1)).unwrap_or(u16::MAX), s.sample_rate, s.bits), + ); + media_data.push(md); + } + if tc.is_some() { + let c = w.sclp("omfi:data:Sound", len, uid(0, 0, 0), 0, 0); + tape_slots.push(w.slot(rate, c, tape_track, &format!("A{}", k + 1), k as u32 + 1)); + } + let mclip = w.sclp("omfi:data:Sound", len, file, file_track, 0); + master_slots.push(w.slot(rate, mclip, k as u32 + 1, &format!("A{}", k + 1), k as u32 + 1)); + master_ref.insert(si, (master, k as u32 + 1)); + } + mobs.push(w.mob(b"MMOB", master, &first.name, &master_slots)); + if tc.is_some() { + let d = w.obj(b"MDTP"); + let reel = first.path.as_deref().map(craft_timeline::path::file_stem).unwrap_or(&first.name).to_string(); + let tm = w.mob(b"SMOB", tape, &reel, &tape_slots); + w.r#ref(tm, "OMFI:SMOB:MediaDescription", d); + mobs.push(tm); + } + } + // composition + let crate_rate = (comp.rate.num, comp.rate.den); + let mut slots = Vec::new(); + let total = comp + .tracks + .iter() + .map(|t| t.items.iter().fold(Tick::ZERO, |a, i| if matches!(i, CItem::Transition(_)) { a - i.len() } else { a + i.len() })) + .max() + .unwrap_or(Tick::ZERO); + let tl = w.u(total, crate_rate); + let tcc = w.tccp(tl, comp.start_tc, comp.rate.timecode_base(), comp.drop); + slots.push(w.slot(crate_rate, tcc, 1, "TC1", 0)); + for (track_id, t) in (2..).zip(comp.tracks.iter().filter(|t| t.kind == CKind::Sound)) { + let rate = (comp.sample_rate.max(1) as i64, 1); + let mut comps = Vec::new(); + let mut cursor = Tick::ZERO; + for it in &t.items { + match it { + CItem::Filler(l) => { + let n = w.u(cursor + *l, rate) - w.u(cursor, rate); + cursor += *l; + let f = w.obj(b"FILL"); + w.cpnt(f, "omfi:data:Sound", n); + comps.push(f); + } + CItem::Clip(c) => { + let n = w.u(cursor + c.len, rate) - w.u(cursor, rate); + let (mid, mtrack) = master_ref.get(&c.source).copied().unwrap_or((uid(0, 0, 0), 0)); + let start = w.u(c.start, rate); + let sc = w.sclp("omfi:data:Sound", n, mid, mtrack, start); + if !c.name.is_empty() { + w.str(sc, "FilmCraft:SCLP:ClipName", &c.name); + } + let seg = match &c.gain { + Some(g) => { + let v = w.value(n, g, n, rate, cursor); + let input = w.obj(b"ESLT"); + w.b.set(input, "OMFI:ESLT:ArgID", "omfi:ArgIDType", &(-1i32).to_be_bytes()); + w.r#ref(input, "OMFI:ESLT:ArgValue", sc); + let level = w.obj(b"ESLT"); + w.b.set(level, "OMFI:ESLT:ArgID", "omfi:ArgIDType", &1i32.to_be_bytes()); + w.r#ref(level, "OMFI:ESLT:ArgValue", v); + let kind = w.edef("omfi:effect:MonoAudioGain", "Mono Audio Gain"); + let e = w.obj(b"EFFE"); + w.cpnt(e, "omfi:data:Sound", n); + w.r#ref(e, "OMFI:EFFE:EffectKind", kind); + w.refs(e, "OMFI:EFFE:EffectSlots", &[input, level]); + e + } + None => sc, + }; + comps.push(seg); + cursor += c.len; + } + CItem::Transition(x) => { + let ts = cursor - x.len; + let n = w.u(cursor, rate) - w.u(ts, rate); + let cut = w.u(ts + x.cut, rate) - w.u(ts, rate); + cursor = ts; + let kind = w.edef("omfi:effect:SimpleMonoAudioDissolve", "Simple Mono Audio Dissolve"); + let e = w.obj(b"EFFE"); + w.cpnt(e, "omfi:data:Sound", n); + w.r#ref(e, "OMFI:EFFE:EffectKind", kind); + w.refs(e, "OMFI:EFFE:EffectSlots", &[]); + w.str(e, "FilmCraft:EFFE:EffectID", &x.effect); + let tr = w.obj(b"TRAN"); + w.cpnt(tr, "omfi:data:Sound", n); + w.i32(tr, "OMFI:TRAN:CutPoint", "omfi:Position32", cut); + w.r#ref(tr, "OMFI:TRAN:Effect", e); + comps.push(tr); + } + } + } + let len = w.u(cursor.max(Tick::ZERO), rate); + let seq = w.obj(b"SEQU"); + w.cpnt(seq, "omfi:data:Sound", len); + w.refs(seq, "OMFI:SEQU:Components", &comps); + slots.push(w.slot(rate, seq, track_id, &t.name, t.number)); + } + let cmob = w.mob(b"CMOB", new_uid(), &comp.name, &slots); + mobs.insert(0, cmob); + if w.inexact { + w.report.info("some times are not on whole samples / frames and were rounded"); + } + let ident = w.obj(b"IDNT"); + w.str(ident, "OMFI:IDNT:CompanyName", identity.company); + w.str(ident, "OMFI:IDNT:ProductName", identity.product); + w.str(ident, "OMFI:IDNT:ProductVersionString", identity.version); + w.str(ident, "OMFI:IDNT:Platform", identity.platform); + w.b.set(ident, "OMFI:IDNT:Date", "omfi:TimeStamp", &[0, 0, 0, 0, 1]); + let head = w.obj(b"HEAD"); + w.b.set(head, "OMFI:HEAD:ByteOrder", "omfi:Int16", &0x4D4Di16.to_be_bytes()); + w.b.set(head, "OMFI:HEAD:LastModified", "omfi:TimeStamp", &[0, 0, 0, 0, 1]); + w.b.set(head, "OMFI:HEAD:Version", "omfi:VersionType", &[2, 0]); + w.refs(head, "OMFI:HEAD:Mobs", &mobs); + w.refs(head, "OMFI:HEAD:MediaData", &media_data); + w.refs(head, "OMFI:HEAD:PrimaryMobs", &[cmob]); + let defs = w.defs.clone(); + w.refs(head, "OMFI:HEAD:DefinitionObjects", &defs); + w.refs(head, "OMFI:HEAD:IdentificationList", &[ident]); + let refs = std::mem::take(&mut w.refs); + let mut keys: Vec<_> = refs.keys().copied().collect(); + keys.sort_unstable(); + for k in keys { + let mut t = refs[&k].clone(); + t.sort_unstable(); + t.dedup(); + w.b.set_references(k, &t); + } + Ok(w.b.finish()) +} + +fn descriptor(w: &mut W, s: &Source, len: i64) -> u32 { + let aiff = + s.embedded.is_none() && s.path.as_deref().is_some_and(|p| matches!(craft_timeline::path::extension(p).as_str(), "aif" | "aiff" | "aifc")); + let d = w.obj(if aiff { b"AIFD" } else { b"WAVD" }); + w.b.set(d, "OMFI:MDFL:IsOMFI", "omfi:Boolean", &[s.embedded.is_some() as u8]); + let r = rate_of(s); + w.b.set(d, "OMFI:MDFL:SampleRate", "omfi:Rational", &rational(r.0, r.1)); + w.i32(d, "OMFI:MDFL:Length", "omfi:Length32", len); + let frames = u32::try_from(len.max(0)).unwrap_or(u32::MAX); + // Channel counts come from the project: saturate, as the headers can't hold more anyway. + let bytes_per_frame = s.file_channels.max(1).saturating_mul((s.bits as u32).div_ceil(8)); + let channels = u16::try_from(s.file_channels.max(1)).unwrap_or(u16::MAX); + if aiff { + w.b.set(d, "OMFI:AIFD:Summary", "omfi:DataValue", &craft_audio::pcm_aiff_header(frames, channels, s.sample_rate, s.bits)); + } else { + w.b.set( + d, + "OMFI:WAVD:Summary", + "omfi:DataValue", + &craft_audio::pcm_wav_header(frames.saturating_mul(bytes_per_frame), channels, s.sample_rate, s.bits), + ); + } + if let Some(p) = &s.path { + let mut locs = Vec::new(); + let dos = p.len() > 2 && p.as_bytes()[1] == b':'; + let l = w.obj(if dos { b"DOSL" } else { b"UNXL" }); + w.str(l, if dos { "OMFI:DOSL:PathName" } else { "OMFI:UNXL:PathName" }, p); + locs.push(l); + if craft_timeline::path::is_absolute(p) { + let n = w.obj(b"NETL"); + w.str(n, "OMFI:NETL:URLString", &craft_timeline::path::path_to_file_url(p, false)); + locs.push(n); + } + w.refs(d, "OMFI:MDES:Locator", &locs); + } + d +} diff --git a/crates/aaf/src/pcm.rs b/crates/aaf/src/pcm.rs new file mode 100644 index 000000000..789c976a8 --- /dev/null +++ b/crates/aaf/src/pcm.rs @@ -0,0 +1,11 @@ +//! Embedded PCM essence in and out of WAVE and AIFF wrappers (craft-audio does the containers). + +/// PCM (little-endian, 8-bit unsigned), channels, sample rate and bits of a WAVE file. +pub(crate) fn parse_wav(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { + craft_audio::wav_pcm(b).map(|p| (p.data, p.channels, p.sample_rate, p.bits)) +} + +/// The same for an AIFF / AIFF-C (`NONE`, `twos`, `sowt`) file. +pub(crate) fn parse_aiff(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { + craft_audio::aiff_pcm(b).map(|p| (p.data, p.channels, p.sample_rate, p.bits)) +} diff --git a/crates/aaf/src/roundtrip_tests.rs b/crates/aaf/src/roundtrip_tests.rs new file mode 100644 index 000000000..5ae2a6d8f --- /dev/null +++ b/crates/aaf/src/roundtrip_tests.rs @@ -0,0 +1,217 @@ +//! Documents written and read back, in both formats; damaged files. + +use craft_timeline::time::{FrameRate, Tick}; + +use crate::model::{CItem, CKind, CMarker, Document, Gain}; +use crate::testdoc::{IDENTITY, pcm_ramp, picture_and_sound, sound_only}; +use crate::{Error, Report, aaf, omf}; + +fn aaf_bytes(doc: &Document, small_sectors: bool) -> Vec { + let mut report = Report::default(); + let bytes = aaf::write(doc, &aaf::WriteOptions { identity: IDENTITY, small_sectors }, &mut report).unwrap(); + assert!(report.is_empty(), "{report}"); + bytes +} + +fn omf_bytes(doc: &Document) -> Vec { + let mut report = Report::default(); + let bytes = omf::write(doc, &IDENTITY, &mut report).unwrap(); + assert!(!report.has_warnings(), "{report}"); + bytes +} + +fn read_clean(bytes: &[u8], is_aaf: bool) -> Document { + let mut report = Report::default(); + let doc = if is_aaf { aaf::read(bytes, &mut report) } else { omf::read(bytes, &mut report) }.unwrap(); + assert!(!report.has_warnings(), "{report}"); + doc +} + +/// What a track holds, with each clip's source by what it is rather than by index. Gain is stored +/// as a rational: four decimals survive. +fn shape(doc: &Document) -> Vec<(CKind, String, u32, Vec)> { + let comp = &doc.compositions[0]; + comp.tracks + .iter() + .map(|t| { + let items = t + .items + .iter() + .map(|i| match i { + CItem::Filler(l) => format!("gap {}", l.0), + CItem::Transition(x) => format!("{} {} cut {}", x.effect, x.len.0, x.cut.0), + CItem::Clip(c) => { + let s = &doc.sources[c.source]; + let gain = match &c.gain { + None => "unity".to_string(), + Some(Gain::Constant(g)) => format!("{g:.4}"), + Some(Gain::Varying { linear, points }) => { + format!("{linear} {:?}", points.iter().map(|(t, g)| (t.0, format!("{g:.4}"))).collect::>()) + } + }; + format!("clip {} from {} of {:?} {:?} gain {gain}", c.len.0, c.start.0, s.path, s.kind) + } + }) + .collect(); + (t.kind, t.name.clone(), t.number, items) + }) + .collect() +} + +#[test] +fn aaf_round_trips_the_edit_at_every_rate() { + for (rate, drop) in [(FrameRate::FPS_25, false), (FrameRate::FPS_29_97, true), (FrameRate::FPS_23_976, false), (FrameRate::FPS_59_94, true)] { + let mut doc = picture_and_sound(rate); + let comp = &mut doc.compositions[0]; + (comp.start_tc, comp.drop) = (rate.timecode_base() * 3600, drop); + comp.markers = vec![ + CMarker { start: rate.tick_of(10), duration: Tick::ZERO, name: "Slate".into(), comment: "take 2".into(), color: Some("Green".into()) }, + CMarker { start: rate.tick_of(40), duration: rate.tick_of(5), name: "Range".into(), comment: String::new(), color: None }, + ]; + // keyframed gain on the first sound clip, held gain steps on nothing else + if let Some(CItem::Clip(c)) = comp.tracks[1].items.first_mut() { + c.gain = Some(Gain::Varying { linear: true, points: vec![(Tick::ZERO, 1.0), (rate.tick_of(25), 0.5)] }); + } + doc.sources[0].markers = + vec![CMarker { start: rate.tick_of(30), duration: Tick::ZERO, name: "In media".into(), comment: String::new(), color: None }]; + for small in [false, true] { + let back = read_clean(&aaf_bytes(&doc, small), true); + let (want, got) = (&doc.compositions[0], &back.compositions[0]); + assert_eq!(shape(&back), shape(&doc), "{rate} small {small}"); + assert_eq!((&got.name, got.rate, got.start_tc, got.drop), (&want.name, want.rate, want.start_tc, want.drop), "{rate}"); + assert_eq!((got.width, got.height, got.sample_rate), (1920, 1080, 48_000), "{rate}"); + let markers = |m: &[CMarker]| m.iter().map(|m| (m.start, m.duration, m.name.clone(), m.comment.clone())).collect::>(); + assert_eq!(markers(&got.markers), markers(&want.markers), "{rate}"); + assert_eq!(back.nested, []); + let picture = back.sources.iter().find(|s| s.kind == CKind::Picture).unwrap(); + assert_eq!((picture.width, picture.height, picture.frame_rate, picture.length), (1280, 720, rate, doc.sources[0].length), "{rate}"); + assert_eq!((picture.start_tc, picture.tc_rate), (Some(86_400), rate), "{rate}"); + assert_eq!(markers(&picture.markers), markers(&doc.sources[0].markers), "{rate}"); + let sound = back.sources.iter().find(|s| s.kind == CKind::Sound).unwrap(); + assert_eq!((sound.channels, sound.file_channels, sound.sample_rate, sound.bits), (2, 2, 48_000, 24), "{rate}"); + assert_eq!(sound.key, picture.key, "one media item"); + // what was read, written again, reads as the same edit (mob ids, and so source keys, are new) + let again = read_clean(&aaf_bytes(&back, small), true); + assert_eq!((shape(&again), &again.compositions), (shape(&back), &back.compositions), "{rate}"); + } + } +} + +#[test] +fn embedded_audio_comes_back_bit_for_bit() { + let pcm = pcm_ramp(480, 2, 3); + let mut doc = sound_only(Some(pcm.clone())); + // trimmed essence: its first sample is media time 0.2 s + doc.sources[0].offset = Tick::from_units(9_600, 48_000); + for is_aaf in [true, false] { + let bytes = if is_aaf { aaf_bytes(&doc, false) } else { omf_bytes(&doc) }; + assert_eq!((aaf::sniff(&bytes), omf::sniff(&bytes)), (is_aaf, !is_aaf)); + let back = read_clean(&bytes, is_aaf); + let s = &back.sources[0]; + assert_eq!(s.embedded.as_deref(), Some(&pcm[..]), "aaf {is_aaf}"); + assert_eq!((s.path.as_deref(), s.file_channels, s.sample_rate, s.bits), (None, 2, 48_000, 16), "aaf {is_aaf}"); + assert_eq!(s.length, Tick::from_units(480, 48_000), "aaf {is_aaf}"); + assert_eq!(shape(&back), shape(&doc), "aaf {is_aaf}"); + } +} + +#[test] +fn omf_round_trips_sound_linked_to_wave_and_aiff_files() { + for path in ["/m/a.wav", "/m/a.aif"] { + let mut doc = sound_only(None); + doc.sources[0].path = Some(path.into()); + let back = read_clean(&omf_bytes(&doc), false); + assert_eq!(shape(&back), shape(&doc), "{path}"); + let s = &back.sources[0]; + assert_eq!((s.file_channels, s.sample_rate, s.bits, s.embedded.as_ref()), (2, 48_000, 16, None), "{path}"); + assert_eq!(back.compositions[0].rate, FrameRate::FPS_25); + } +} + +#[test] +fn omf_leaves_picture_out() { + let doc = picture_and_sound(FrameRate::FPS_25); + let back = read_clean(&omf_bytes(&doc), false); + assert!(back.compositions[0].tracks.iter().all(|t| t.kind == CKind::Sound)); + assert_eq!(shape(&back).len(), 1); +} + +#[test] +fn empty_and_foreign_documents() { + let none = Document::default(); + assert_eq!(aaf::write(&none, &aaf::WriteOptions { identity: IDENTITY, small_sectors: false }, &mut Report::default()), Err(Error::Empty)); + assert_eq!(omf::write(&none, &IDENTITY, &mut Report::default()), Err(Error::Empty)); + let mut empty = sound_only(None); + empty.compositions[0].tracks.clear(); + empty.sources.clear(); + for small in [false, true] { + assert_eq!(read_clean(&aaf_bytes(&empty, small), true).compositions.len(), 1); + } + assert_eq!(read_clean(&omf_bytes(&empty), false).compositions.len(), 1); + assert!(matches!(aaf::read(b"not an aaf", &mut Report::default()), Err(Error::Parse { format: "AAF", .. }))); + assert!(matches!(omf::read(b"not an omf", &mut Report::default()), Err(Error::Parse { format: "OMF", .. }))); + assert!(!aaf::sniff(b"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1 but short")); + assert!(!omf::sniff(b"")); +} + +/// Truncation and random corruption: an error or a document, never a panic. +#[test] +fn damaged_documents_never_panic() { + let aaf_file = aaf_bytes(&picture_and_sound(FrameRate::FPS_25), true); + let omf_file = omf_bytes(&sound_only(Some(pcm_ramp(100, 2, 1)))); + for (bytes, is_aaf) in [(&aaf_file, true), (&omf_file, false)] { + let read = |b: &[u8]| { + let mut report = Report::default(); + let _ = if is_aaf { aaf::read(b, &mut report) } else { omf::read(b, &mut report) }; + }; + for cut in (0..bytes.len()).step_by(97) { + read(&bytes[..cut]); + } + let mut seed = 0xA5A5_1234_5678_9ABCu64; + for _ in 0..400 { + let mut g = bytes.clone(); + for _ in 0..8 { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + let at = (seed as usize) % g.len(); + g[at] = (seed >> 40) as u8; + } + read(&g); + } + } +} + +/// A time that is not on a whole edit unit is rounded, and the report says so. +#[test] +fn off_grid_times_are_rounded_and_reported() { + let mut doc = picture_and_sound(FrameRate::FPS_25); + if let Some(CItem::Clip(c)) = doc.compositions[0].tracks[0].items.first_mut() { + c.start += Tick(7); + } + let mut report = Report::default(); + aaf::write(&doc, &aaf::WriteOptions { identity: IDENTITY, small_sectors: false }, &mut report).unwrap(); + assert!(report.mentions("rounded"), "{report}"); +} + +/// A composition that uses itself (a damaged file) is a gap with a warning, not a recursion. +#[test] +fn a_composition_nested_in_itself_is_a_gap() { + let rate = FrameRate::FPS_25; + let mut doc = picture_and_sound(rate); + let mut inner = doc.compositions[0].clone(); + inner.name = "Inner".into(); + let mut nest = doc.sources[0].clone(); + (nest.key, nest.path, nest.nested, nest.start_tc) = ("seq:9".into(), None, Some(0), None); + doc.sources.push(nest); + let uses_inner = CItem::Clip(crate::model::CClip { len: rate.tick_of(20), source: 2, start: Tick::ZERO, gain: None, name: "Inner".into() }); + inner.tracks[0].items = vec![uses_inner.clone()]; + doc.nested.push(inner); + doc.compositions[0].tracks[0].items = vec![uses_inner]; + let bytes = aaf::write(&doc, &aaf::WriteOptions { identity: IDENTITY, small_sectors: false }, &mut Report::default()).unwrap(); + let mut report = Report::default(); + let back = aaf::read(&bytes, &mut report).unwrap(); + assert!(report.mentions("nested in itself"), "{report}"); + assert_eq!(back.nested.len(), 1); + assert!(matches!(back.nested[0].tracks[0].items[..], [CItem::Filler(_)]), "{:?}", back.nested[0].tracks[0].items); +} diff --git a/crates/aaf/src/testdoc.rs b/crates/aaf/src/testdoc.rs new file mode 100644 index 000000000..81db0a158 --- /dev/null +++ b/crates/aaf/src/testdoc.rs @@ -0,0 +1,106 @@ +//! Documents the tests write: what an editor's two-clip sequence with a dissolve looks like in +//! the model. + +use craft_timeline::time::{FrameRate, Tick}; + +use crate::Identity; +use crate::model::{CClip, CItem, CKind, CTrack, CTransition, Composition, Document, Gain, Source}; + +pub(crate) const IDENTITY: Identity<'static> = + Identity { company: "Craft", product: "craft-aaf tests", version: "1.2.3", platform: "test", product_id: *b"craft-aaf-tests." }; + +/// A source of `kind` for the media item `key` at `path`: 1280×720 picture at `rate`, or stereo +/// 48 kHz sound of `bits` bits; 100 seconds long. +pub(crate) fn source(key: &str, path: &str, kind: CKind, rate: FrameRate, bits: u16, start_tc: Option) -> Source { + let sound = kind == CKind::Sound; + Source { + key: key.into(), + name: path.rsplit('/').next().unwrap_or(path).into(), + kind, + path: Some(path.into()), + channel: None, + channels: if sound { 2 } else { 0 }, + file_channels: if sound { 2 } else { 0 }, + width: if sound { 0 } else { 1280 }, + height: if sound { 0 } else { 720 }, + frame_rate: rate, + sample_rate: 48_000, + bits, + length: rate.tick_of(100 * rate.timecode_base()), + offset: Tick::ZERO, + start_tc, + tc_rate: rate, + embedded: None, + markers: Vec::new(), + nested: None, + } +} + +pub(crate) fn clip(rate: FrameRate, source: usize, start: i64, len: i64, name: &str) -> CItem { + CItem::Clip(CClip { len: rate.tick_of(len), source, start: rate.tick_of(start), gain: None, name: name.into() }) +} + +/// Two clips of `source` cut at frame `cut`, each `cut` frames long on the timeline, joined by a +/// transition of `dur` frames centred on the cut: each clip runs half the transition past the cut. +pub(crate) fn two_clips(rate: FrameRate, source: usize, cut: i64, dur: i64, (in_a, in_b): (i64, i64), effect: &str) -> Vec { + let half = dur / 2; + vec![ + clip(rate, source, in_a, cut + half, "a"), + CItem::Transition(CTransition { len: rate.tick_of(dur), cut: rate.tick_of(half), effect: effect.into() }), + clip(rate, source, in_b - half, cut + half, "a"), + ] +} + +pub(crate) fn composition(name: &str, rate: FrameRate, tracks: Vec) -> Composition { + Composition { name: name.into(), rate, sample_rate: 48_000, width: 1920, height: 1080, start_tc: 0, drop: false, tracks, markers: Vec::new() } +} + +pub(crate) fn track(kind: CKind, name: &str, number: u32, items: Vec) -> CTrack { + CTrack { kind, name: name.into(), number, channels: if kind == CKind::Sound { 2 } else { 0 }, items } +} + +/// "S" at `rate`: V1 and A1 each hold two clips of `a.mov`, `cut` frames each on the timeline (from +/// source frames `ins`), with a transition of `dur` frames centred on the cut; the second sound +/// clip is 3 dB down. +pub(crate) fn picture_and_sound_cut(rate: FrameRate, cut: i64, dur: i64, ins: (i64, i64)) -> Document { + let sources = vec![ + source("item:1", "/m/a.mov", CKind::Picture, rate, 24, Some(86_400)), + source("item:1", "/m/a.mov", CKind::Sound, rate, 24, Some(86_400)), + ]; + let mut sound = two_clips(rate, 1, cut, dur, ins, "constant_power"); + if let Some(CItem::Clip(c)) = sound.last_mut() { + c.gain = Some(Gain::Constant(10f64.powf(-3.0 / 20.0))); + } + let tracks = vec![track(CKind::Picture, "V1", 1, two_clips(rate, 0, cut, dur, ins, "cross_dissolve")), track(CKind::Sound, "A1", 1, sound)]; + Document { compositions: vec![composition("S", rate, tracks)], sources, nested: Vec::new() } +} + +/// [`picture_and_sound_cut`] with 50-frame clips from source frames 10 and 205 and a 10-frame +/// transition: every time is a multiple of five frames, which is a whole number of 48 kHz samples +/// at the NTSC rates too. +pub(crate) fn picture_and_sound(rate: FrameRate) -> Document { + picture_and_sound_cut(rate, 50, 10, (10, 205)) +} + +/// 16-bit PCM: `frames` frames of `channels` channels, every sample different from its neighbours. +pub(crate) fn pcm_ramp(frames: usize, channels: usize, seed: i32) -> Vec { + (0..frames * channels).flat_map(|i| ((i as i32 * 37 + seed * 1000) as i16).to_le_bytes()).collect() +} + +/// "S" at 25 fps, sound only: A1 holds two 50-frame clips of `a.wav` (from source frames 10 and +/// 300) with a 10-frame crossfade centred on the cut; the first clip is 6 dB up. The sound is +/// embedded when `embedded` is given (480 frames of 16-bit stereo). +pub(crate) fn sound_only(embedded: Option>) -> Document { + let rate = FrameRate::FPS_25; + let mut s = source("item:1", "/m/a.wav", CKind::Sound, rate, 16, None); + if let Some(pcm) = embedded { + s.path = None; + s.length = Tick::from_units((pcm.len() / 4) as i64, 48_000); + s.embedded = Some(pcm); + } + let mut items = two_clips(rate, 0, 50, 10, (10, 300), "constant_power"); + if let Some(CItem::Clip(c)) = items.first_mut() { + c.gain = Some(Gain::Constant(10f64.powf(6.0 / 20.0))); + } + Document { compositions: vec![composition("S", rate, vec![track(CKind::Sound, "A1", 1, items)])], sources: vec![s], nested: Vec::new() } +} From 77591e5efbfef912cd433d5d17c0363c237812d2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:53:16 -0500 Subject: [PATCH 031/120] craft-gif: GIF block reader (#939) One walk over a GIF's blocks, without decompressing pixels: the logical screen, each frame's position, delay, disposal and transparent colour, the loop count and the first frame's colour table. Replaces PhotoCraft's two block walkers and gives the other apps a header-only animation probe. --- Cargo.lock | 7 ++ Cargo.toml | 1 + README.md | 4 + crates/gif/Cargo.toml | 15 +++ crates/gif/src/lib.rs | 209 ++++++++++++++++++++++++++++++++++++++++ crates/gif/src/tests.rs | 164 +++++++++++++++++++++++++++++++ 6 files changed, 400 insertions(+) create mode 100644 crates/gif/Cargo.toml create mode 100644 crates/gif/src/lib.rs create mode 100644 crates/gif/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 36919285e..e9b596a92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1128,6 +1128,13 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-gif" +version = "0.1.0" +dependencies = [ + "image", +] + [[package]] name = "craft-hdr" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index b77c65828..5738359d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ craft-font-manager = { path = "crates/font-manager" } craft-font-store = { path = "crates/font-store" } craft-gainmap = { path = "crates/gainmap" } craft-gcode = { path = "crates/gcode" } +craft-gif = { path = "crates/gif" } craft-hdr = { path = "crates/hdr" } craft-mesh = { path = "crates/mesh" } craft-heif = { path = "crates/heif" } diff --git a/README.md b/README.md index 87ccc7852..3695e0313 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,10 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = ", + /// Disposal method (0–7) from the Graphic Control Extension. + pub disposal: u8, + /// Whether the frame has its own colour table. + pub local_table: bool, +} + +/// What the blocks of a GIF say. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Info { + /// Logical screen size. + pub width: u16, + pub height: u16, + /// Whether the file has a Global Color Table. + pub global_table: bool, + /// The frames found, in file order, up to the caller's limit. + pub frames: Vec, + /// Whether the walk reached the trailer. When it did not (the file is cut short or damaged, or + /// the frame limit was reached), more frames may exist than [`Info::frames`] holds. + pub complete: bool, + /// The NETSCAPE2.0 loop count: `Some(0)` loops forever, `None` when the file has none (play once). + pub loop_count: Option, +} + +impl Info { + /// Whether the file is known to hold more than one frame, or `None` when the walk stopped + /// before the trailer after at most one frame, so frames past the break could not be counted. + pub fn has_more_frames(&self) -> Option { + if self.frames.len() > 1 { Some(true) } else { self.complete.then_some(false) } + } + + /// The sum of the frame delays in hundredths of a second, as stored. + pub fn total_delay_cs(&self) -> u64 { + self.frames.iter().map(|f| u64::from(f.delay_cs)).sum() + } +} + +/// Whether `bytes` starts with a GIF signature. +pub fn sniff(bytes: &[u8]) -> bool { + bytes.starts_with(b"GIF87a") || bytes.starts_with(b"GIF89a") +} + +/// Bytes of the colour table a packed-fields byte announces (0 without one). +fn table_len(packed: u8) -> usize { + if packed & 0x80 != 0 { 3usize << ((packed & 7) + 1) } else { 0 } +} + +fn u16_at(b: &[u8], at: usize) -> Option { + Some(u16::from_le_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?)) +} + +/// Skips the data sub-blocks starting at `at` (each length-prefixed, ended by a zero length). +fn skip_sub_blocks(b: &[u8], mut at: usize) -> Option { + loop { + let n = usize::from(*b.get(at)?); + at = at.checked_add(1 + n)?; + if n == 0 { + return Some(at); + } + } +} + +/// Walks the blocks of a GIF without decompressing any, recording at most `max_frames` frames. +/// `None` when `bytes` is not a GIF or ends inside its Logical Screen Descriptor. A file that +/// breaks off later gives what was read, with [`Info::complete`] false. +pub fn scan(bytes: &[u8], max_frames: usize) -> Option { + let b = bytes; + if !sniff(b) { + return None; + } + // Logical Screen Descriptor: width, height, packed fields, background, aspect. + let (width, height, packed) = (u16_at(b, 6)?, u16_at(b, 8)?, *b.get(10)?); + b.get(12)?; + let mut info = Info { width, height, global_table: packed & 0x80 != 0, frames: Vec::new(), complete: false, loop_count: None }; + let mut at = 13 + table_len(packed); + // What the last Graphic Control Extension said, for the next image. + let (mut delay_cs, mut transparent, mut disposal) = (0u16, None, 0u8); + // Each step either finishes the walk or moves `at` forward, so this ends with the file. + let mut step = || -> Option { + match *b.get(at)? { + // Extension: label, then data sub-blocks up to a zero-length one. + 0x21 => { + let label = *b.get(at + 1)?; + let body = at + 2; + // Graphic Control Extension: block size 4, packed (bit 0: transparent colour + // given; bits 2–4: disposal), delay (2 bytes), transparent colour index. + if label == 0xF9 && *b.get(body)? == 4 { + let flags = *b.get(body + 1)?; + delay_cs = u16_at(b, body + 2)?; + transparent = (flags & 0x01 != 0).then_some(*b.get(body + 4)?); + disposal = (flags >> 2) & 7; + } + // Application Extension NETSCAPE2.0 (or ANIMEXTS1.0): sub-block 3, 1, loop count. + if label == 0xFF + && matches!(b.get(body..body + 12), Some([11, id @ ..]) if id == b"NETSCAPE2.0" || id == b"ANIMEXTS1.0") + && b.get(body + 12..body + 14) == Some(&[3, 1]) + { + info.loop_count = u16_at(b, body + 14); + } + at = skip_sub_blocks(b, body)?; + Some(true) + } + // Image Descriptor: separator, left, top, width, height, packed fields, then the + // Local Color Table if bit 7 is set, the LZW code size and the data sub-blocks. + 0x2C => { + if info.frames.len() >= max_frames { + return None; + } + let packed = *b.get(at + 9)?; + info.frames.push(Frame { + offset: at, + left: u16_at(b, at + 1)?, + top: u16_at(b, at + 3)?, + width: u16_at(b, at + 5)?, + height: u16_at(b, at + 7)?, + delay_cs, + transparent, + disposal, + local_table: packed & 0x80 != 0, + }); + (delay_cs, transparent, disposal) = (0, None, 0); + at = skip_sub_blocks(b, (at + 10).checked_add(table_len(packed))?.checked_add(1)?)?; + Some(true) + } + // Trailer. + 0x3B => { + info.complete = true; + Some(false) + } + // Anything else: the blocks break off here. + _ => None, + } + }; + while step() == Some(true) {} + Some(info) +} + +/// The colours of the table the first frame uses (its Local Color Table when it has one, else +/// the Global one) and its transparent entry. `None` for anything that is not a well-formed GIF +/// up to its first image, or a first frame without a table. A transparent index past the table +/// is no transparent entry. +pub fn palette(bytes: &[u8]) -> Option<(Vec<[u8; 3]>, Option)> { + let info = scan(bytes, 1)?; + let first = info.frames.first()?; + let (at, packed) = if first.local_table { (first.offset + 10, *bytes.get(first.offset + 9)?) } else { (13, *bytes.get(10)?) }; + let colors = bytes.get(at..at.checked_add(table_len(packed))?)?.as_chunks::<3>().0.to_vec(); + if colors.is_empty() { + return None; + } + let transparent = first.transparent.filter(|i| usize::from(*i) < colors.len()); + Some((colors, transparent)) +} + +/// Whether a file of `file_len` bytes can plausibly hold `declared_bytes` of decoded picture: at +/// most `max_ratio` decoded bytes for each file byte, plus 1 MiB. A decoder that sets up the whole +/// logical screen before it reads a frame should be given only files that pass this. +pub fn plausible(declared_bytes: u64, file_len: usize, max_ratio: u64) -> bool { + declared_bytes <= (file_len as u64).saturating_mul(max_ratio).saturating_add(1 << 20) +} + +impl Info { + /// The decoded bytes the file must plausibly back at `bytes_per_pixel`: the first frame, and + /// whatever of the logical screen exceeds `unbacked_canvas` bytes (the screen around the first + /// frame needs no data, so that much of it is free). Use with [`plausible`]. + pub fn bytes_to_back(&self, bytes_per_pixel: u64, unbacked_canvas: u64) -> u64 { + let screen = u64::from(self.width).saturating_mul(u64::from(self.height)).saturating_mul(bytes_per_pixel); + match self.frames.first() { + Some(f) => { + u64::from(f.width).saturating_mul(u64::from(f.height)).saturating_mul(bytes_per_pixel).max(screen.saturating_sub(unbacked_canvas)) + } + None => screen, + } + } +} diff --git a/crates/gif/src/tests.rs b/crates/gif/src/tests.rs new file mode 100644 index 000000000..a75743b19 --- /dev/null +++ b/crates/gif/src/tests.rs @@ -0,0 +1,164 @@ +//! The palette tests are PhotoCraft's; the scan is also checked against files written by the +//! `image` crate's GIF encoder. +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use super::*; + +/// A GIF up to its first image: `global` table entries, optional GCE transparency, optional +/// local table. +fn gif(global: &[[u8; 3]], transparent: Option, local: Option<&[[u8; 3]]>) -> Vec { + let size_bits = |n: usize| (n.max(2).next_power_of_two().trailing_zeros() - 1) as u8; + let mut b = b"GIF89a".to_vec(); + b.extend([4, 0, 4, 0]); + b.push(if global.is_empty() { 0 } else { 0x80 | size_bits(global.len()) }); + b.extend([0, 0]); + let pad = |b: &mut Vec, t: &[[u8; 3]]| { + let n = 2usize << size_bits(t.len()); + for k in 0..n { + b.extend(t.get(k).copied().unwrap_or([0, 0, 0])); + } + }; + if !global.is_empty() { + pad(&mut b, global); + } + // A comment extension first, to skip. + b.extend([0x21, 0xFE, 3, b'h', b'i', b'!', 0]); + if let Some(i) = transparent { + b.extend([0x21, 0xF9, 4, 0x01, 0, 0, i, 0]); + } + b.extend([0x2C, 0, 0, 0, 0, 4, 0, 4, 0]); + match local { + Some(t) => { + b.push(0x80 | size_bits(t.len())); + pad(&mut b, t); + } + None => b.push(0), + } + b.extend([2, 2, 0x4C, 0x01, 0, 0x3B]); + b +} + +/// An animated GIF of solid 2 × 2 frames with these delays (hundredths), written by `image`. +fn animated(delays_cs: &[u32], repeat: Option) -> Vec { + let mut out = Vec::new(); + { + let mut enc = image::codecs::gif::GifEncoder::new(&mut out); + if let Some(r) = repeat { + enc.set_repeat(r).unwrap(); + } + for (i, &cs) in delays_cs.iter().enumerate() { + let buf = image::RgbaImage::from_pixel(2, 2, image::Rgba([i as u8 * 40, 0, 0, 255])); + enc.encode_frame(image::Frame::from_parts(buf, 0, 0, image::Delay::from_numer_denom_ms(cs * 10, 1))).unwrap(); + } + } + out +} + +#[test] +fn the_first_frames_table_and_transparent_entry() { + let g = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255]]; + assert_eq!(palette(&gif(&g, None, None)), Some((g.to_vec(), None))); + assert_eq!(palette(&gif(&g, Some(2), None)), Some((g.to_vec(), Some(2)))); + // A local table wins over the global one. + let l = [[1, 2, 3], [4, 5, 6]]; + assert_eq!(palette(&gif(&g, Some(1), Some(&l))), Some((l.to_vec(), Some(1)))); + // A transparent index past the table is no transparent entry. + assert_eq!(palette(&gif(&l, Some(9), None)), Some((l.to_vec(), None))); +} + +#[test] +fn no_table_or_not_a_gif_is_none() { + assert_eq!(palette(&gif(&[], None, None)), None, "no table at all"); + assert_eq!(palette(b"\x89PNG\r\n\x1a\n"), None); + assert_eq!(palette(b""), None); + assert_eq!(scan(b"\x89PNG\r\n\x1a\n", 10), None); + assert_eq!(scan(b"GIF89a\x01\x00", 10), None); + assert!(sniff(b"GIF87a") && sniff(b"GIF89a...") && !sniff(b"GIF88a")); +} + +#[test] +fn a_scan_reports_the_screen_and_the_frame() { + let g = [[9, 9, 9], [8, 8, 8]]; + let info = scan(&gif(&g, Some(1), None), 10).unwrap(); + assert_eq!((info.width, info.height, info.global_table, info.complete, info.loop_count), (4, 4, true, true, None)); + let [f] = info.frames.as_slice() else { panic!("{:?}", info.frames) }; + assert_eq!((f.left, f.top, f.width, f.height, f.delay_cs, f.transparent, f.disposal, f.local_table), (0, 0, 4, 4, 0, Some(1), 0, false)); + assert_eq!(info.has_more_frames(), Some(false)); + let info = scan(&gif(&g, None, Some(&g)), 10).unwrap(); + assert_eq!((info.frames[0].local_table, info.frames[0].transparent), (true, None)); +} + +#[test] +fn an_animation_reports_every_delay_and_the_loop_count() { + use image::codecs::gif::Repeat; + let bytes = animated(&[5, 10, 0, 7], Some(Repeat::Infinite)); + let info = scan(&bytes, 100).unwrap(); + assert_eq!((info.width, info.height, info.complete, info.loop_count), (2, 2, true, Some(0))); + assert_eq!(info.frames.iter().map(|f| f.delay_cs).collect::>(), [5, 10, 0, 7]); + assert_eq!((info.total_delay_cs(), info.has_more_frames()), (22, Some(true))); + assert!(info.frames.iter().all(|f| (f.width, f.height) == (2, 2))); + assert_eq!(scan(&animated(&[5, 5], Some(Repeat::Finite(3))), 100).unwrap().loop_count, Some(3)); + assert_eq!(scan(&animated(&[5], None), 100).unwrap().has_more_frames(), Some(false)); +} + +#[test] +fn the_frame_limit_stops_the_walk() { + let bytes = animated(&[1, 2, 3, 4, 5], None); + let info = scan(&bytes, 3).unwrap(); + assert_eq!((info.frames.len(), info.complete, info.has_more_frames()), (3, false, Some(true))); + let info = scan(&bytes, 5).unwrap(); + assert_eq!((info.frames.len(), info.complete), (5, true)); + // One frame recorded and the walk stopped: whether more exist is not known. + let info = scan(&bytes, 1).unwrap(); + assert_eq!((info.frames.len(), info.has_more_frames()), (1, None)); + assert_eq!(scan(&bytes, 0).unwrap().frames.len(), 0); +} + +#[test] +fn a_cut_or_damaged_file_gives_what_was_read() { + let bytes = animated(&[4, 4, 4], None); + let whole = scan(&bytes, 100).unwrap(); + for cut in 0..bytes.len() { + // Never a panic; never complete; never more frames than the whole file has. + if let Some(info) = scan(&bytes[..cut], 100) { + assert!(!info.complete && info.frames.len() <= whole.frames.len(), "cut {cut}"); + } + let _ = palette(&bytes[..cut]); + } + for i in 0..bytes.len() { + for v in [0u8, 0x21, 0x2C, 0x3B, 0xFF] { + let mut b = bytes.clone(); + b[i] = v; + if let Some(info) = scan(&b, 100) { + assert!(info.frames.iter().all(|f| f.offset < b.len())); + } + let _ = palette(&b); + } + } + // A cut inside the second frame keeps the first. + let second = whole.frames[1].offset; + let info = scan(&bytes[..second + 5], 100).unwrap(); + assert_eq!((info.frames.len(), info.complete, info.has_more_frames()), (1, false, None)); +} + +#[test] +fn plausibility_of_a_screen_for_a_file_size() { + // A 20-byte file declaring a 65535 × 65535 screen around a 1 × 1 frame. + let mut b = gif(&[[0, 0, 0], [1, 1, 1]], None, None); + b[6..10].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]); + let n = b.len(); + b[n - 11..n - 7].copy_from_slice(&[1, 0, 1, 0]); + let info = scan(&b, 1).unwrap(); + assert_eq!((info.width, info.frames[0].width, info.frames[0].height), (65535, 1, 1)); + let screen = 65535u64 * 65535 * 4; + // 256 MiB of screen is free; the rest must be backed by file. + assert_eq!(info.bytes_to_back(4, 1 << 28), screen - (1 << 28)); + assert!(!plausible(info.bytes_to_back(4, 1 << 28), b.len(), 1 << 14)); + // A small screen needs only its first frame backed. + let small = scan(&gif(&[[0, 0, 0], [1, 1, 1]], None, None), 1).unwrap(); + assert_eq!(small.bytes_to_back(4, 1 << 28), 64); + assert!(plausible(64, 40, 1 << 14)); + // The ratio and the 1 MiB allowance, at and past the boundary. + assert!(plausible((1 << 20) + 10 * 4, 10, 4)); + assert!(!plausible((1 << 20) + 10 * 4 + 1, 10, 4)); +} From 17e55c96dbc80fbec8e2d356a18f21c871cc514c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:53:53 -0500 Subject: [PATCH 032/120] Add craft-zip: bounded ZIP reading and a deterministic writer for the package crates (#941) * craft-zip: ZIP preflight, bounded entry read and Budget (from craft-archive, #129) The ZIP half of the craft-archive proposal (#126, #129): an allocation-free check of the end records and central directory that runs before a ZIP reader indexes the archive, a bounded entry read that distrusts the declared size, and the named Budget both charge. The XML half of #129 is not carried: craft-xml bounds XML now. * craft-zip: Archive (entry-count guard, duplicate names, budgeted reads) and Writer check_entry_count is craft-opc's guard: the declared entry count, ZIP64 included, checked on the raw bytes before zip parses the central directory. Archive reads entries within a per-entry limit and a total Budget through read_entry. Duplicate names are found by counting the central directory headers against the entries zip kept. Writer is craft-opc's ZipOut: stored or deflated entries, ZIP64 for large ones, one fixed timestamp. --------- Co-authored-by: skylar taylor-barrick --- Cargo.lock | 10 + Cargo.toml | 1 + README.md | 1 + crates/zip/Cargo.toml | 26 ++ crates/zip/README.md | 68 ++++ crates/zip/src/archive.rs | 118 ++++++ crates/zip/src/guard.rs | 192 ++++++++++ crates/zip/src/lib.rs | 187 ++++++++++ crates/zip/src/preflight.rs | 429 ++++++++++++++++++++++ crates/zip/src/preflight/tests.rs | 580 ++++++++++++++++++++++++++++++ crates/zip/src/write.rs | 58 +++ crates/zip/tests/archive.rs | 148 ++++++++ crates/zip/tests/public_api.rs | 76 ++++ 13 files changed, 1894 insertions(+) create mode 100644 crates/zip/Cargo.toml create mode 100644 crates/zip/README.md create mode 100644 crates/zip/src/archive.rs create mode 100644 crates/zip/src/guard.rs create mode 100644 crates/zip/src/lib.rs create mode 100644 crates/zip/src/preflight.rs create mode 100644 crates/zip/src/preflight/tests.rs create mode 100644 crates/zip/src/write.rs create mode 100644 crates/zip/tests/archive.rs create mode 100644 crates/zip/tests/public_api.rs diff --git a/Cargo.lock b/Cargo.lock index e9b596a92..11b1a2c9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1749,6 +1749,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-zip" +version = "0.1.0" +dependencies = [ + "craft-zip", + "flate2", + "thiserror 2.0.21", + "zip", +] + [[package]] name = "crc32fast" version = "1.5.2" diff --git a/Cargo.toml b/Cargo.toml index 5738359d4..249108320 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,6 +99,7 @@ craft-vectorcraft-format = { path = "crates/vectorcraft-format" } craft-vectordrawable = { path = "crates/vectordrawable" } craft-xls = { path = "crates/xls" } craft-xml = { path = "crates/xml" } +craft-zip = { path = "crates/zip" } craft-y4m = { path = "crates/y4m" } craft-epub = { path = "crates/epub" } craft-video = { path = "crates/video" } diff --git a/README.md b/README.md index 3695e0313..ab4347290 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = "", Compression::Deflated)?; +# let bytes = w.finish()?; +let mut zip = Archive::open(&bytes, Limits { max_entries: 20_000, max_total_bytes: 512 << 20 })?; +if zip.has_duplicate_names() { + // Two entries share a name; a format that must not guess which is real refuses here. +} +let part = zip.read("content.xml", 64 << 20)?; // Ok(None) when there is no such entry +assert_eq!(part.as_deref(), Some(&b""[..])); +assert_eq!(zip.budget().used(), 4); +# Ok::<(), craft_zip::Error>(()) +``` + +- `Archive::open` runs `check_entry_count` first. `zip` reserves room for, and parses, as many + central directory headers as the end record declares before its caller can count them, and a + ZIP64 end record can declare millions. The check finds the end records the way `zip` does and + refuses on the raw bytes. +- `Archive::read` refuses an entry that declares more than `max`, that expands past what it + declares, or that does not fit in what is left of `max_total_bytes`. At most 64 KiB is + reserved before bytes arrive, whatever the entry declares. Only a read that succeeds is + charged. +- `has_duplicate_names`: `zip` keeps one entry per name, so a central directory with more + headers than `zip` has entries repeats a name. +- `budget_mut` is for a caller with its own charging rule (charging a failed read, say). +- `Error::is_limit` tells a limit from a broken archive. + +## Writing + +`Writer::add(name, data, Compression::Stored | Deflated | Level(n))`, then `finish()`. Entries +keep their order (a stored `mimetype` goes first by being added first). Every entry has the same +1980 timestamp, so equal input gives equal bytes. An entry of 2 GiB or more is written in the +ZIP64 form. A repeated name is an error. + +## Strict preflight + +`preflight::preflight(bytes, limits)` checks the whole structure on the raw bytes without +allocating: exactly one end record may span the end of the file, the ZIP64 pair must agree with +it, the directory must sit exactly before the end records and be filled exactly by its entries, +single disk only, with limits on entries, directory bytes and name length. It is stricter than +`zip` (it refuses archives with data before them and archives with two plausible end records), +so `Archive::open` does not run it: a caller that wants nothing ambiguous to reach a ZIP reader +calls it first. + +`preflight::read_entry(reader, declared, max, &mut budget)` is the bounded read on any +`std::io::Read`; `Budget` is a named limit with a running total. + +## Where it came from + +`preflight`, `read_entry` and `Budget`: the ZIP half of the craft-archive proposal (#126, #129). +`check_entry_count`: craft-opc's `guard`. Duplicate detection: craft-penpot's rule, made exact +by walking the directory. The writer: craft-opc's `ZipOut`. diff --git a/crates/zip/src/archive.rs b/crates/zip/src/archive.rs new file mode 100644 index 000000000..e23364010 --- /dev/null +++ b/crates/zip/src/archive.rs @@ -0,0 +1,118 @@ +//! An archive open for reading: entry-count guard, duplicate names, budgeted entry reads. + +use std::borrow::Cow; +use std::io::Cursor; + +use crate::preflight::{ReadError, read_entry}; +use crate::{Budget, Error, check_entry_count}; + +/// What an [`Archive`] refuses to exceed. Both numbers are the caller's. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + /// Most entries the archive may declare. + pub max_entries: u64, + /// Most bytes all the entries read from the archive may expand to together. + pub max_total_bytes: u64, +} + +/// A ZIP archive in memory. Nothing is decompressed until an entry is read. +pub struct Archive<'a> { + zip: zip::ZipArchive>, + budget: Budget, + duplicates: bool, +} + +const CENTRAL_HEADER: &[u8; 4] = b"PK\x01\x02"; +const CENTRAL_HEADER_LEN: usize = 46; + +/// The central directory headers that follow one another from `start`, counted up to `stop`. +fn count_central_headers(bytes: &[u8], start: u64, stop: usize) -> usize { + let u16_at = |at: usize| bytes.get(at..at.checked_add(2)?).map(|b| usize::from(u16::from_le_bytes([b[0], b[1]]))); + let Ok(mut at) = usize::try_from(start) else { return 0 }; + let mut count = 0; + while count < stop && bytes.get(at..).is_some_and(|b| b.starts_with(CENTRAL_HEADER)) { + let (Some(name), Some(extra), Some(comment)) = (u16_at(at + 28), u16_at(at + 30), u16_at(at + 32)) else { break }; + count += 1; + at = at.saturating_add(CENTRAL_HEADER_LEN + name + extra + comment); + } + count +} + +impl<'a> Archive<'a> { + /// Indexes `bytes`. The declared entry count is checked against + /// [`Limits::max_entries`] before `zip` parses the central directory, and again after. + pub fn open(bytes: &'a [u8], limits: Limits) -> Result { + check_entry_count(bytes, limits.max_entries)?; + let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| Error::NotZip(e.to_string()))?; + let found = zip.len() as u64; + if found > limits.max_entries { + return Err(Error::TooManyEntries { declared: found, limit: limits.max_entries }); + } + // `zip` keeps one entry per name, so a directory with more headers than `zip` has entries + // repeats a name. + let duplicates = count_central_headers(bytes, zip.central_directory_start(), zip.len().saturating_add(1)) > zip.len(); + Ok(Archive { zip, budget: Budget::new("archive", limits.max_total_bytes), duplicates }) + } + + /// The number of distinct entry names. + pub fn len(&self) -> usize { + self.zip.len() + } + + pub fn is_empty(&self) -> bool { + self.zip.is_empty() + } + + /// Do two entries share a name? `zip` then keeps one of them, and which one a writer meant + /// is not known: a format that must not guess refuses the archive. + pub fn has_duplicate_names(&self) -> bool { + self.duplicates + } + + /// The entry names in the archive's order, folders included. A name that is not text is + /// left out. + pub fn names(&self) -> impl Iterator> { + self.zip.file_names().filter_map(Result::ok) + } + + /// Is there an entry `name` (exactly as stored)? + pub fn contains(&self, name: &str) -> bool { + self.zip.index_for_name(name).is_some() + } + + /// The size entry `name` declares. It is a claim, not a measurement. + pub fn declared_size(&mut self, name: &str) -> Option { + let index = self.zip.index_for_name(name)?; + self.zip.by_index_raw(index).ok().map(|f| f.size()) + } + + /// The bytes of entry `name`, or `None` when there is no such entry. + /// + /// The entry may declare, and expand to, at most `max` bytes, and at most what is left of + /// [`Limits::max_total_bytes`]. An entry that expands past the size it declares is refused. + /// Only the bytes of a read that succeeds are charged to the total. + pub fn read(&mut self, name: &str, max: u64) -> Result>, Error> { + let Some(index) = self.zip.index_for_name(name) else { return Ok(None) }; + let entry = |msg: String| Error::Entry { name: name.to_string(), msg }; + let f = self.zip.by_index(index).map_err(|e| entry(e.to_string()))?; + let declared = f.size(); + match read_entry(f, declared, max, &mut self.budget) { + Ok(data) => Ok(Some(data)), + Err(ReadError::DeclaredTooLarge { limit, .. }) | Err(ReadError::Overrun { limit }) => { + Err(Error::EntryTooLarge { name: name.to_string(), limit }) + } + Err(ReadError::Budget(e)) => Err(Error::BudgetSpent { name: name.to_string(), limit: e.limit }), + Err(ReadError::Io(e)) => Err(entry(e.to_string())), + } + } + + /// The total budget: what the entries read so far have used, and what is left. + pub fn budget(&self) -> &Budget { + &self.budget + } + + /// The total budget, for a caller with its own charging rule (charging a failed read, say). + pub fn budget_mut(&mut self) -> &mut Budget { + &mut self.budget + } +} diff --git a/crates/zip/src/guard.rs b/crates/zip/src/guard.rs new file mode 100644 index 000000000..dd3ebe451 --- /dev/null +++ b/crates/zip/src/guard.rs @@ -0,0 +1,192 @@ +//! A bytes-only check of a ZIP archive's declared entry count before the `zip` crate opens it. +//! +//! `zip::ZipArchive::new` reserves room for, and parses, as many central directory headers as the +//! end record declares before its caller can look at `len()`. A ZIP64 end record can declare +//! millions of entries for a file of a few hundred megabytes (only 46 bytes each have to fit), and +//! each parsed entry costs several times that in memory. Here the end records are found the way +//! `zip` finds them (APPNOTE.TXT §4.3.14 to §4.3.16), and a package declaring more than the +//! reader's limit is refused before `zip` parses anything. Anything else that is wrong is left for +//! `zip` to report. + +use crate::Error; + +const EOCD: &[u8; 4] = b"PK\x05\x06"; +const EOCD64_LOCATOR: &[u8; 4] = b"PK\x06\x07"; +const EOCD64: &[u8; 4] = b"PK\x06\x06"; +const CENTRAL_HEADER: &[u8; 4] = b"PK\x01\x02"; + +/// End records looked at, at most. A package has one; a few more can turn up inside stored data. +const MAX_END_RECORDS: usize = 16; + +/// Refuse `bytes` when the end record `zip` would use declares more than `max_entries` entries. +/// +/// `zip` searches the file backwards for an end of central directory record. A ZIP64 one points +/// through its locator at the ZIP64 end record, which `zip` looks for from the locator's offset +/// on: every such record there is checked, and as `zip` may fall back to an earlier end record +/// when one doesn't parse, the search goes on. A plain one is used when it declares no entries, or +/// a central directory header lies between its directory offset and itself: it is the last one +/// looked at. +pub fn check_entry_count(bytes: &[u8], max_entries: u64) -> Result<(), Error> { + let u16_at = |at: usize| bytes.get(at..at.checked_add(2)?).map(|b| u16::from_le_bytes([b[0], b[1]])); + let u32_at = |at: usize| bytes.get(at..at.checked_add(4)?).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]])); + let u64_at = |at: usize| bytes.get(at..at.checked_add(8)?).and_then(|b| <[u8; 8]>::try_from(b).ok()).map(u64::from_le_bytes); + let refuse = + |n: u64| -> Result<(), Error> { if n > max_entries { Err(Error::TooManyEntries { declared: n, limit: max_entries }) } else { Ok(()) } }; + let mut end = bytes.len(); + let mut seen = 0; + while let Some(at) = bytes.get(..end).and_then(|b| b.windows(4).rposition(|w| w == EOCD)) { + end = at; + seen += 1; + if seen > MAX_END_RECORDS { + return Err(Error::TooManyEndRecords(MAX_END_RECORDS)); + } + // `zip` skips a record whose comment runs past the end of the file. + let Some(comment) = u16_at(at + 20) else { continue }; + if at + 22 + usize::from(comment) > bytes.len() { + continue; + } + let (Some(on_disk), Some(total), Some(cd_offset)) = (u16_at(at + 8), u16_at(at + 10), u32_at(at + 16)) else { continue }; + let zip64 = total == u16::MAX || cd_offset == u32::MAX; + if zip64 + && let Some(locator) = at.checked_sub(20) + && bytes.get(locator..locator + 4) == Some(EOCD64_LOCATOR.as_slice()) + { + let (Some(eocd64), Some(disks)) = (u64_at(locator + 8), u32_at(locator + 16)) else { continue }; + let Some(from) = usize::try_from(eocd64).ok().filter(|&f| f < locator) else { continue }; + if disks > 1 { + continue; + } + let records = bytes.get(from..locator).unwrap_or_default().windows(4).enumerate().filter(|(_, w)| *w == EOCD64); + for (i, _) in records { + let p = from + i; + refuse(u64_at(p + 24).unwrap_or(0).max(u64_at(p + 32).unwrap_or(0)))?; + } + continue; + } + if total == 0 { + return Ok(()); + } + let cd = cd_offset as usize; + if cd < at && bytes.get(cd..at).is_some_and(|b| b.windows(4).any(|w| w == CENTRAL_HEADER)) { + return refuse(u64::from(on_disk.max(total))); + } + } + Ok(()) +} + +/// A package of one empty stored file, `a`, whose central directory lists it `entries` times (at +/// most as many as fit in 64 MiB) under a ZIP64 end record that declares them all: `zip` +/// parses and keeps every one of them before its caller can count them. Regression input for the +/// readers' tests: built only for tests, this crate's and (through the `test-support` feature, +/// which they turn on as a dev-dependency) the readers', never into an app. +#[cfg(any(test, feature = "test-support"))] +pub fn zip64_entry_count_sample(entries: u64) -> Vec { + // Local file header (APPNOTE.TXT §4.3.7): version 10, stored, no data, name "a". + let mut f = b"PK\x03\x04".to_vec(); + f.extend_from_slice(&[10, 0, 0, 0, 0, 0, 0, 0, 0x21, 0]); // version, flags, method, time, date + f.extend_from_slice(&[0; 12]); // crc, sizes + f.extend_from_slice(&[1, 0, 0, 0]); // name and extra lengths + f.push(b'a'); + let cd_at = f.len() as u64; + // Its central header (§4.3.12), 47 bytes. + let mut header = CENTRAL_HEADER.to_vec(); + header.extend_from_slice(&[10, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0x21, 0]); // versions, flags, method, time, date + header.extend_from_slice(&[0; 12]); // crc, sizes + header.extend_from_slice(&[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); // name, extra, comment, disk, attributes + header.extend_from_slice(&[0; 4]); // local header offset + header.push(b'a'); + let count = entries.min((64 << 20) / header.len() as u64); + for _ in 0..count { + f.extend_from_slice(&header); + } + let cd_len = f.len() as u64 - cd_at; + let eocd64_at = f.len() as u64; + // ZIP64 end of central directory record (§4.3.14), 56 bytes. + f.extend_from_slice(EOCD64); + f.extend_from_slice(&44u64.to_le_bytes()); // size of the rest of the record + f.extend_from_slice(&[45, 0, 45, 0]); // versions + f.extend_from_slice(&[0; 8]); // this disk, disk with the central directory + f.extend_from_slice(&entries.to_le_bytes()); // entries on this disk + f.extend_from_slice(&entries.to_le_bytes()); // entries + f.extend_from_slice(&cd_len.to_le_bytes()); + f.extend_from_slice(&cd_at.to_le_bytes()); + // Its locator (§4.3.15), 20 bytes. + f.extend_from_slice(EOCD64_LOCATOR); + f.extend_from_slice(&0u32.to_le_bytes()); + f.extend_from_slice(&eocd64_at.to_le_bytes()); + f.extend_from_slice(&1u32.to_le_bytes()); + // End of central directory record (§4.3.16), 22 bytes, each count and offset "see ZIP64". + f.extend_from_slice(EOCD); + f.extend_from_slice(&[0; 4]); + f.extend_from_slice(&[0xFF; 12]); + f.extend_from_slice(&[0; 2]); + f +} + +#[cfg(test)] +mod tests { + use super::{CENTRAL_HEADER, EOCD, check_entry_count as check, zip64_entry_count_sample}; + use crate::Error; + + /// A plain package's tail: one central header at 0, then an end record declaring `entries`. + fn plain(entries: u16, comment: &[u8]) -> Vec { + let mut f = CENTRAL_HEADER.to_vec(); + f.resize(46, 0); + f.extend_from_slice(EOCD); + f.extend_from_slice(&[0, 0, 0, 0]); + f.extend_from_slice(&entries.to_le_bytes()); + f.extend_from_slice(&entries.to_le_bytes()); + f.extend_from_slice(&46u32.to_le_bytes()); + f.extend_from_slice(&0u32.to_le_bytes()); + f.extend_from_slice(&(comment.len() as u16).to_le_bytes()); + f.extend_from_slice(comment); + f + } + + #[test] + fn a_zip64_end_record_declaring_too_many_entries_is_refused() { + // `zip` would reserve and parse them all before the reader's cap. + let sample = zip64_entry_count_sample(20_001); + assert_eq!(check(&sample, 20_000), Err(Error::TooManyEntries { declared: 20_001, limit: 20_000 })); + assert!(check(&zip64_entry_count_sample(5), 20_000).is_ok()); + // Junk before the package (a self-extracting stub) doesn't hide it. + let mut prefixed = vec![0x55u8; 4096]; + prefixed.extend_from_slice(&sample); + assert!(check(&prefixed, 20_000).is_err()); + } + + #[test] + fn a_plain_end_record_is_checked_as_zip_reads_it() { + assert!(check(&plain(3, b""), 20_000).is_ok()); + assert_eq!(check(&plain(30_000, b"a comment"), 20_000), Err(Error::TooManyEntries { declared: 30_000, limit: 20_000 })); + // A record whose comment runs past the end is not the one `zip` uses. + let mut cut = plain(30_000, b""); + cut.truncate(cut.len() - 2); + cut.extend_from_slice(&500u16.to_le_bytes()); + assert!(check(&cut, 20_000).is_ok()); + assert!(check(b"not a package", 20_000).is_ok()); + assert!(check(b"", 20_000).is_ok()); + } + + #[test] + fn a_fake_end_record_after_the_real_one_does_not_hide_it() { + // `zip` skips a last record with no central header before it and uses the one before. + let mut f = zip64_entry_count_sample(1_000_000); + f.extend_from_slice(EOCD); + f.extend_from_slice(&[0, 0, 0, 0]); + f.extend_from_slice(&7u16.to_le_bytes()); + f.extend_from_slice(&7u16.to_le_bytes()); + f.extend_from_slice(&0u32.to_le_bytes()); + f.extend_from_slice(&((f.len() - 12) as u32).to_le_bytes()); + f.extend_from_slice(&0u16.to_le_bytes()); + assert!(check(&f, 20_000).is_err()); + } + + #[test] + fn many_end_records_are_refused() { + // Each declares one entry and has no central header before it, so `zip` would try them all. + let record = [0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + let f: Vec = (0..20).flat_map(|_| EOCD.iter().copied().chain(record)).collect(); + assert_eq!(check(&f, 20_000), Err(Error::TooManyEndRecords(16))); + } +} diff --git a/crates/zip/src/lib.rs b/crates/zip/src/lib.rs new file mode 100644 index 000000000..cd138df3f --- /dev/null +++ b/crates/zip/src/lib.rs @@ -0,0 +1,187 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +//! ZIP archives from untrusted files, read within limits and written. +//! +//! The `zip` crate does the decoding. This crate is what every reader of a ZIP-based format +//! otherwise writes again around it: +//! +//! - [`Archive::open`] refuses an archive that declares more entries than the caller allows +//! before `zip` parses them ([`check_entry_count`]), and tells whether two entries share a name. +//! - [`Archive::read`] reads an entry within a per-entry limit and the archive's total +//! [`Budget`], counting the bytes that really arrive, not the size the archive declares. +//! - [`Writer`] writes stored and deflated entries, ZIP64 where an entry needs it, with the same +//! timestamp on every entry so equal input gives equal bytes. +//! - [`preflight`] is a stricter check of the whole end-record and central-directory structure +//! on the raw bytes, for a caller that wants nothing ambiguous to reach a ZIP reader at all. +//! +//! Every limit is the caller's number. Entry-name rules (which names are safe, which spellings +//! are one part) stay with the format that knows them. + +#[cfg(doctest)] +#[doc = include_str!("../README.md")] +struct ReadmeExamples; + +mod archive; +mod guard; +pub mod preflight; +mod write; + +pub use archive::{Archive, Limits}; +pub use guard::check_entry_count; +#[cfg(any(test, feature = "test-support"))] +pub use guard::zip64_entry_count_sample; +pub use write::{Compression, Writer}; + +/// Why an archive was not read or not written. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The bytes are not a ZIP archive the `zip` crate can index. + #[error("not a ZIP archive: {0}")] + NotZip(String), + /// The archive declares more entries than [`Limits::max_entries`]. + #[error("the archive declares {declared} entries, more than {limit}")] + TooManyEntries { declared: u64, limit: u64 }, + /// More end of central directory records than an archive can have. + #[error("more than {0} end of central directory records")] + TooManyEndRecords(usize), + /// An entry declares, or expands to, more than the limit given for it. + #[error("{name} is larger than {limit} bytes")] + EntryTooLarge { name: String, limit: u64 }, + /// The entries read so far have used up [`Limits::max_total_bytes`]. + #[error("{name} does not fit in what is left of the {limit} bytes the archive may expand to")] + BudgetSpent { name: String, limit: u64 }, + /// An entry could not be decoded (a failed checksum, an unknown method, encryption). + #[error("{name}: {msg}")] + Entry { name: String, msg: String }, + /// The archive could not be written. + #[error("the ZIP archive could not be written: {0}")] + Write(String), +} + +impl Error { + /// Was a limit exceeded (as opposed to the archive being broken)? + pub fn is_limit(&self) -> bool { + matches!(self, Error::TooManyEntries { .. } | Error::TooManyEndRecords(_) | Error::EntryTooLarge { .. } | Error::BudgetSpent { .. }) + } +} + +use std::fmt; + +/// A named limit and the total charged against it so far. +/// +/// The name is what a breach message should mention ("document", "part", "images"), and the limit +/// is the application's own number. The crate never invents either, so one budget can cover a +/// whole import and be reported in the application's terms. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Budget { + what: &'static str, + limit: u64, + used: u64, +} + +impl Budget { + /// A budget named `what` that admits up to `limit` units in total. + pub const fn new(what: &'static str, limit: u64) -> Self { + Self { what, limit, used: 0 } + } + + /// Charge `n` units, or refuse without changing the total. + /// + /// A refused charge leaves the total untouched, so a caller may carry on with a smaller + /// charge, and the amount refused is reported in [`Exceeded::requested`]. + pub fn charge(&mut self, n: u64) -> Result<(), Exceeded> { + if n > self.remaining() { + return Err(Exceeded { what: self.what, limit: self.limit, requested: n }); + } + self.used = self.used.saturating_add(n); + Ok(()) + } + + /// The units still available; zero when the budget is exhausted. + pub const fn remaining(&self) -> u64 { + self.limit.saturating_sub(self.used) + } + + /// The units charged so far. + pub const fn used(&self) -> u64 { + self.used + } + + /// The limit this budget was created with. + pub const fn limit(&self) -> u64 { + self.limit + } + + /// The name this budget was created with. + pub const fn what(&self) -> &'static str { + self.what + } +} + +/// A charge that a [`Budget`] refused: what was limited, the limit, and the amount asked for. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Exceeded { + /// The budget's name, so a message can say which limit was hit. + pub what: &'static str, + /// The limit that was already reached. + pub limit: u64, + /// The amount that did not fit. + pub requested: u64, +} + +impl fmt::Display for Exceeded { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "the {} limit is {}; another {} was requested", self.what, self.limit, self.requested) + } +} + +impl std::error::Error for Exceeded {} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn budgets_charge_and_report_the_remainder() { + let mut budget = Budget::new("document", 10); + assert_eq!(budget.what(), "document"); + assert_eq!(budget.limit(), 10); + assert_eq!(budget.remaining(), 10); + assert_eq!(budget.used(), 0); + assert_eq!(budget.charge(4), Ok(())); + assert_eq!(budget.used(), 4); + assert_eq!(budget.remaining(), 6); + assert_eq!(budget.charge(6), Ok(())); + assert_eq!(budget.remaining(), 0); + } + + #[test] + fn a_refused_charge_leaves_the_total_untouched() { + let mut budget = Budget::new("part", 100); + assert_eq!(budget.charge(60), Ok(())); + let exceeded = budget.charge(41).err(); + assert_eq!(exceeded, Some(Exceeded { what: "part", limit: 100, requested: 41 })); + assert_eq!(budget.used(), 60); + // The refused amount is not charged, so the budget stays usable. + assert_eq!(budget.charge(40), Ok(())); + assert_eq!(budget.used(), 100); + assert!(budget.charge(1).is_err()); + } + + #[test] + fn a_zero_budget_admits_nothing_and_the_message_names_the_limit() { + let mut budget = Budget::new("entries", 0); + assert_eq!(budget.remaining(), 0); + assert_eq!(budget.charge(0), Ok(())); + let message = budget.charge(1).err().map(|e| e.to_string()); + assert_eq!(message.as_deref(), Some("the entries limit is 0; another 1 was requested")); + } + + #[test] + fn a_limit_at_the_maximum_does_not_overflow() { + let mut budget = Budget::new("total", u64::MAX); + assert_eq!(budget.charge(u64::MAX), Ok(())); + assert_eq!(budget.remaining(), 0); + assert!(budget.charge(u64::MAX).is_err()); + } +} diff --git a/crates/zip/src/preflight.rs b/crates/zip/src/preflight.rs new file mode 100644 index 000000000..56b569a86 --- /dev/null +++ b/crates/zip/src/preflight.rs @@ -0,0 +1,429 @@ +//! ZIP structure checks that run before a ZIP reader indexes the archive, and bounded entry reads. +//! +//! A ZIP file ends with an end-of-central-directory record (EOCD) naming the offset and size of a +//! central directory that lists every entry, optionally preceded by a ZIP64 pair when the archive +//! is too large for the classic fields. [`preflight`] validates those records and walks the +//! directory on the raw bytes, so a hostile entry count or directory size is refused before a +//! reader can spend memory and time on it. [`read_entry`] then bounds one entry's inflation. +//! +//! Only single-disk archives are accepted: split archives and self-extracting prefixed archives +//! are rejected, because their relative offsets would make this check disagree with the bytes a +//! later reader indexes. The algorithm follows the ZIP specification (PKWARE APPNOTE, "ZIP File +//! Format"); no third-party code is involved. + +use std::fmt; +use std::io::Read; + +use crate::{Budget, Exceeded}; + +const EOCD_SIGNATURE: &[u8; 4] = b"PK\x05\x06"; +const ZIP64_EOCD_SIGNATURE: &[u8; 4] = b"PK\x06\x06"; +const ZIP64_LOCATOR_SIGNATURE: &[u8; 4] = b"PK\x06\x07"; +const CENTRAL_FILE_SIGNATURE: &[u8; 4] = b"PK\x01\x02"; +const CENTRAL_DIGITAL_SIGNATURE: &[u8; 4] = b"PK\x05\x05"; + +/// The classic end-of-central-directory record, fixed part. +const EOCD_LEN: usize = 22; +/// The ZIP64 end-of-central-directory locator. +const ZIP64_LOCATOR_LEN: usize = 20; +/// The fixed part of a ZIP64 end-of-central-directory record: 12 bytes of signature and record +/// size, then 44 bytes of fixed fields. +const ZIP64_EOCD_FIXED_LEN: usize = 44; +const ZIP64_EOCD_HEADER_LEN: u64 = 12; +/// The fixed part of a central-directory file header, before the name, extra field and comment. +const CENTRAL_FILE_LEN: usize = 46; +/// The largest archive comment the classic end record can describe. +const MAX_COMMENT_LEN: usize = u16::MAX as usize; +/// Central-directory file header field offsets. +const NAME_LEN_AT: usize = 28; +const EXTRA_LEN_AT: usize = 30; +const COMMENT_LEN_AT: usize = 32; +const ENTRY_DISK_AT: usize = 34; + +/// Application-owned limits for [`preflight`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + /// The most central-directory entries the archive may declare. + pub max_entries: u64, + /// The most bytes the central directory may occupy. + pub max_central_directory_bytes: u64, + /// The most bytes a central-directory entry name may occupy. + pub max_name_bytes: u16, +} + +/// What [`preflight`] found: the directory's declared totals. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Directory { + /// The number of central-directory entries, counted by walking them. + pub entries: u64, + /// The declared size of the central directory in bytes. + pub central_directory_bytes: u64, +} + +/// Why [`preflight`] refused an archive. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Error { + /// No end-of-central-directory record spans the end of the file. + MissingEndRecord, + /// More than one end record could claim the end of the file, so which one a reader would use + /// is not determined; none is trusted. + AmbiguousEndRecords, + /// The structure is malformed or unsupported. The message names what was wrong. + Invalid(&'static str), + /// The archive declares more entries than the limit admits. + TooManyEntries { + /// The declared entry count. + actual: u64, + /// The caller's limit. + limit: u64, + }, + /// The central directory is larger than the limit admits. + CentralDirectoryTooLarge { + /// The declared directory size in bytes. + actual: u64, + /// The caller's limit. + limit: u64, + }, + /// A central-directory entry name is longer than the limit admits. + NameTooLong { + /// The declared name length in bytes. + actual: u64, + /// The caller's limit. + limit: u64, + }, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::MissingEndRecord => f.write_str("the ZIP end record is missing"), + Self::AmbiguousEndRecords => f.write_str("the ZIP has more than one plausible end record"), + Self::Invalid(message) => write!(f, "invalid ZIP structure: {message}"), + Self::TooManyEntries { actual, limit } => write!(f, "the ZIP declares {actual} entries; the limit is {limit}"), + Self::CentralDirectoryTooLarge { actual, limit } => write!(f, "the ZIP central directory is {actual} bytes; the limit is {limit}"), + Self::NameTooLong { actual, limit } => write!(f, "a ZIP entry name is {actual} bytes; the limit is {limit}"), + } + } +} + +impl std::error::Error for Error {} + +/// Why [`read_entry`] refused an entry. +/// +/// Not `PartialEq`: an [`std::io::Error`] is not comparable, so tests compare the displayed +/// message instead. +#[derive(Debug)] +pub enum ReadError { + /// The entry declares more bytes than the per-entry limit admits. + DeclaredTooLarge { + /// The declared size. + declared: u64, + /// The caller's per-entry limit. + limit: u64, + }, + /// The entry produced more bytes than the smaller of its declared size and the per-entry + /// limit: the declared size lied, or the data is a decompression bomb. + Overrun { + /// The per-entry bound that was passed. + limit: u64, + }, + /// The entry needed more bytes than the total budget had left. + Budget(Exceeded), + /// The reader failed. + Io(std::io::Error), +} + +impl fmt::Display for ReadError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::DeclaredTooLarge { declared, limit } => write!(f, "the entry declares {declared} bytes; the limit is {limit}"), + Self::Overrun { limit } => write!(f, "the entry produced more than the {limit} bytes it is allowed"), + Self::Budget(exceeded) => exceeded.fmt(f), + Self::Io(error) => error.fmt(f), + } + } +} + +impl std::error::Error for ReadError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::Budget(exceeded) => Some(exceeded), + Self::Io(error) => Some(error), + _ => None, + } + } +} + +impl From for ReadError { + fn from(error: std::io::Error) -> Self { + Self::Io(error) + } +} + +impl From for ReadError { + fn from(exceeded: Exceeded) -> Self { + Self::Budget(exceeded) + } +} + +/// The most bytes [`read_entry`] reserves up front, however large the entry declares itself. +/// +/// A declared size is attacker-controlled, so reserving it would let a ten-byte archive claim a +/// gigabyte; the buffer grows as real bytes arrive instead. +pub const MAX_PREALLOCATION: u64 = 64 * 1024; + +/// Validate the end records and walk the declared central directory without allocating. +/// +/// Checks, in order: +/// +/// * the end-of-central-directory record: exactly one candidate must span the end of the file, so +/// an archive that hides a second plausible record is refused rather than guessed at; +/// * the ZIP64 pair when the classic record uses a sentinel or a locator is present: a single disk, +/// a locator immediately before the end record, the ZIP64 record ending at that locator, and no +/// disagreement between the classic and ZIP64 fields; +/// * the declared entry count, directory size and entry-name lengths against `limits`; +/// * the directory arithmetic: its offset and size must not overflow and must reach the end +/// records, so a lying offset or size cannot point the reader elsewhere; +/// * every entry header in the directory: its signature, its disk number, its variable-length +/// fields, and that the entries exactly fill the declared directory, allowing one optional +/// central-directory digital signature at its end. +/// +/// Returns the declared entry count and directory size. Malformed input is an error, never a panic. +pub fn preflight(bytes: &[u8], limits: Limits) -> Result { + let eocd_at = find_end_record(bytes)?; + let directory = end_record_totals(bytes, eocd_at)?; + + if directory.entries > limits.max_entries { + return Err(Error::TooManyEntries { actual: directory.entries, limit: limits.max_entries }); + } + if directory.size > limits.max_central_directory_bytes { + return Err(Error::CentralDirectoryTooLarge { actual: directory.size, limit: limits.max_central_directory_bytes }); + } + + let directory_end = directory.offset.checked_add(directory.size).ok_or(Error::Invalid("central-directory offset overflow"))?; + let structure_at = u64::try_from(directory.structure_at).map_err(|_| Error::Invalid("end-record offset does not fit"))?; + if directory_end != structure_at { + return Err(Error::Invalid("central-directory offset and size do not reach the end records")); + } + let start = usize::try_from(directory.offset).map_err(|_| Error::Invalid("central-directory offset does not fit this platform"))?; + let end = usize::try_from(directory_end).map_err(|_| Error::Invalid("central-directory end does not fit this platform"))?; + + walk_directory(bytes, start, end, directory.entries, limits.max_name_bytes)?; + Ok(Directory { entries: directory.entries, central_directory_bytes: directory.size }) +} + +/// Read one entry up to the smallest of `max`, the total budget's remainder and `declared`. +/// +/// The declared size is refused outright when it exceeds `max`. At most [`MAX_PREALLOCATION`] bytes +/// are reserved however large the entry claims to be. The bytes are read up to the bound and only +/// the bytes actually read are charged to `total`, so a declaration that overstates the entry +/// costs nothing extra. +/// +/// More bytes arriving than the bound admits is the hostile case: [`ReadError::Overrun`] when the +/// per-entry bound or the declaration was what stopped the read (a lying size or a decompression +/// bomb), and [`ReadError::Budget`] when the total budget was already the tighter bound. +/// +/// Works with any [`Read`], including a `zip` crate entry reader. An I/O failure is +/// [`ReadError::Io`]; the bytes read before it are not charged. +pub fn read_entry(r: impl Read, declared: u64, max: u64, total: &mut Budget) -> Result, ReadError> { + if declared > max { + return Err(ReadError::DeclaredTooLarge { declared, limit: max }); + } + let per_entry = declared.min(max); + let budget_left = total.remaining(); + let bound = per_entry.min(budget_left); + // One byte past the bound has to be readable to tell "exactly the bound" from "more". + let mut out = Vec::with_capacity(usize::try_from(bound.min(MAX_PREALLOCATION)).unwrap_or(0)); + let mut reader = r.take(bound.saturating_add(1)); + reader.read_to_end(&mut out)?; + let read = u64::try_from(out.len()).unwrap_or(u64::MAX); + if read > bound { + // The budget was the tighter bound, so the total limit is what refused the entry. + if per_entry > budget_left { + return Err(ReadError::Budget(Exceeded { what: total.what(), limit: total.limit(), requested: read })); + } + return Err(ReadError::Overrun { limit: bound }); + } + total.charge(read)?; + Ok(out) +} + +/// The declared entry count, directory size and offset, and where the end records begin. +struct EndRecord { + entries: u64, + size: u64, + offset: u64, + /// The offset of the first end record: the ZIP64 record when there is one, else the EOCD. + structure_at: usize, +} + +/// Find the single end-of-central-directory record that ends the file. +/// +/// A record is a candidate when its signature is present, its declared comment length puts its end +/// exactly at the end of the file, and it lies in the last 64 KiB plus 22 bytes — the only place +/// the format allows one. The search runs backwards, so the record nearest the end is considered +/// first; finding a second candidate means the archive is ambiguous and is refused. +fn find_end_record(bytes: &[u8]) -> Result { + let last = bytes.len().checked_sub(EOCD_LEN).ok_or(Error::MissingEndRecord)?; + let first = bytes.len().saturating_sub(EOCD_LEN.saturating_add(MAX_COMMENT_LEN)); + let mut found = None; + for at in (first..=last).rev() { + if !signature_at(bytes, at, EOCD_SIGNATURE) { + continue; + } + let Some(comment_len) = u16_at(bytes, at + 20).map(usize::from) else { continue }; + let Some(end) = at.checked_add(EOCD_LEN).and_then(|value| value.checked_add(comment_len)) else { continue }; + if end != bytes.len() { + continue; + } + if found.replace(at).is_some() { + return Err(Error::AmbiguousEndRecords); + } + } + found.ok_or(Error::MissingEndRecord) +} + +/// Read the totals the end records declare, using the ZIP64 pair when one is present. +fn end_record_totals(bytes: &[u8], eocd_at: usize) -> Result { + let disk = u16_at(bytes, eocd_at + 4).ok_or(Error::Invalid("truncated end record"))?; + let directory_disk = u16_at(bytes, eocd_at + 6).ok_or(Error::Invalid("truncated end record"))?; + let disk_entries = u16_at(bytes, eocd_at + 8).ok_or(Error::Invalid("truncated end record"))?; + let entries = u16_at(bytes, eocd_at + 10).ok_or(Error::Invalid("truncated end record"))?; + let directory_size = u32_at(bytes, eocd_at + 12).ok_or(Error::Invalid("truncated end record"))?; + let directory_offset = u32_at(bytes, eocd_at + 16).ok_or(Error::Invalid("truncated end record"))?; + + let sentinel = disk == u16::MAX + || directory_disk == u16::MAX + || disk_entries == u16::MAX + || entries == u16::MAX + || directory_size == u32::MAX + || directory_offset == u32::MAX; + let locator_at = eocd_at.checked_sub(ZIP64_LOCATOR_LEN).filter(|at| signature_at(bytes, *at, ZIP64_LOCATOR_SIGNATURE)); + + let Some(locator_at) = locator_at else { + if sentinel { + return Err(Error::Invalid("ZIP64 sentinel without a ZIP64 end record")); + } + if disk != 0 || directory_disk != 0 || disk_entries != entries { + return Err(Error::Invalid("multi-disk ZIP archive")); + } + return Ok(EndRecord { + entries: u64::from(entries), + size: u64::from(directory_size), + offset: u64::from(directory_offset), + structure_at: eocd_at, + }); + }; + + let zip64_disk = u32_at(bytes, locator_at + 4).ok_or(Error::Invalid("truncated ZIP64 locator"))?; + let zip64_offset = u64_at(bytes, locator_at + 8).ok_or(Error::Invalid("truncated ZIP64 locator"))?; + let zip64_disks = u32_at(bytes, locator_at + 16).ok_or(Error::Invalid("truncated ZIP64 locator"))?; + if zip64_disk != 0 || zip64_disks != 1 { + return Err(Error::Invalid("multi-disk ZIP64 archive")); + } + let zip64_at = usize::try_from(zip64_offset).map_err(|_| Error::Invalid("ZIP64 end-record offset does not fit this platform"))?; + if !signature_at(bytes, zip64_at, ZIP64_EOCD_SIGNATURE) { + return Err(Error::Invalid("the ZIP64 end record is missing at its declared offset")); + } + let zip64_disk_number = u32_at(bytes, zip64_at + 16).ok_or(Error::Invalid("truncated ZIP64 end record"))?; + let zip64_directory_disk = u32_at(bytes, zip64_at + 20).ok_or(Error::Invalid("truncated ZIP64 end record"))?; + let zip64_disk_entries = u64_at(bytes, zip64_at + 24).ok_or(Error::Invalid("truncated ZIP64 end record"))?; + let zip64_entries = u64_at(bytes, zip64_at + 32).ok_or(Error::Invalid("truncated ZIP64 end record"))?; + let zip64_directory_size = u64_at(bytes, zip64_at + 40).ok_or(Error::Invalid("truncated ZIP64 end record"))?; + let zip64_directory_offset = u64_at(bytes, zip64_at + 48).ok_or(Error::Invalid("truncated ZIP64 end record"))?; + let zip64_record_size = u64_at(bytes, zip64_at + 4).ok_or(Error::Invalid("truncated ZIP64 end record"))?; + if zip64_record_size < u64::try_from(ZIP64_EOCD_FIXED_LEN).unwrap_or(44) { + return Err(Error::Invalid("the ZIP64 end record is too short")); + } + // The ZIP64 record ends exactly where its locator begins; anything else means the locator + // points somewhere other than this record. + let record_end = u64::try_from(zip64_at) + .ok() + .and_then(|at| at.checked_add(ZIP64_EOCD_HEADER_LEN)) + .and_then(|at| at.checked_add(zip64_record_size)) + .ok_or(Error::Invalid("ZIP64 end-record length overflow"))?; + if record_end != u64::try_from(locator_at).unwrap_or(u64::MAX) { + return Err(Error::Invalid("the ZIP64 end record does not end at its locator")); + } + if zip64_disk_number != 0 || zip64_directory_disk != 0 || zip64_disk_entries != zip64_entries { + return Err(Error::Invalid("multi-disk ZIP64 archive")); + } + if (disk != u16::MAX && disk != 0) + || (directory_disk != u16::MAX && directory_disk != 0) + || (disk_entries != u16::MAX && u64::from(disk_entries) != zip64_entries) + || (entries != u16::MAX && u64::from(entries) != zip64_entries) + || (directory_size != u32::MAX && u64::from(directory_size) != zip64_directory_size) + || (directory_offset != u32::MAX && u64::from(directory_offset) != zip64_directory_offset) + { + return Err(Error::Invalid("the classic and ZIP64 end records disagree")); + } + Ok(EndRecord { entries: zip64_entries, size: zip64_directory_size, offset: zip64_directory_offset, structure_at: zip64_at }) +} + +/// Walk the declared directory, checking each entry header and that the entries fill it exactly. +fn walk_directory(bytes: &[u8], start: usize, end: usize, entries: u64, max_name_bytes: u16) -> Result<(), Error> { + if end > bytes.len() || start > end { + return Err(Error::Invalid("the central directory is outside the archive")); + } + let mut at = start; + for _ in 0..entries { + if !signature_at(bytes, at, CENTRAL_FILE_SIGNATURE) { + return Err(Error::Invalid("a central-directory entry signature is missing")); + } + let name = u16_at(bytes, at + NAME_LEN_AT).ok_or(Error::Invalid("truncated central-directory entry"))?; + let extra = usize::from(u16_at(bytes, at + EXTRA_LEN_AT).ok_or(Error::Invalid("truncated central-directory entry"))?); + let comment = usize::from(u16_at(bytes, at + COMMENT_LEN_AT).ok_or(Error::Invalid("truncated central-directory entry"))?); + let disk = u16_at(bytes, at + ENTRY_DISK_AT).ok_or(Error::Invalid("truncated central-directory entry"))?; + if disk != 0 { + return Err(Error::Invalid("a central-directory entry refers to another disk")); + } + if name == 0 || name > max_name_bytes { + return Err(Error::NameTooLong { actual: u64::from(name), limit: u64::from(max_name_bytes) }); + } + at = at + .checked_add(CENTRAL_FILE_LEN) + .and_then(|value| value.checked_add(usize::from(name))) + .and_then(|value| value.checked_add(extra)) + .and_then(|value| value.checked_add(comment)) + .ok_or(Error::Invalid("central-directory entry length overflow"))?; + if at > end { + return Err(Error::Invalid("a central-directory entry exceeds the declared directory size")); + } + } + // The directory normally ends where its last entry does, optionally followed by one + // central-directory digital signature record. + if at < end && signature_at(bytes, at, CENTRAL_DIGITAL_SIGNATURE) { + let signature_len = usize::from(u16_at(bytes, at + 4).ok_or(Error::Invalid("truncated central-directory signature"))?); + at = at + .checked_add(6) + .and_then(|value| value.checked_add(signature_len)) + .ok_or(Error::Invalid("central-directory signature length overflow"))?; + } + if at != end { + return Err(Error::Invalid("the central-directory entry count and size disagree")); + } + Ok(()) +} + +fn bytes_at(bytes: &[u8], at: usize, len: usize) -> Option<&[u8]> { + bytes.get(at..at.checked_add(len)?) +} + +fn u16_at(bytes: &[u8], at: usize) -> Option { + Some(u16::from_le_bytes(bytes_at(bytes, at, 2)?.try_into().ok()?)) +} + +fn u32_at(bytes: &[u8], at: usize) -> Option { + Some(u32::from_le_bytes(bytes_at(bytes, at, 4)?.try_into().ok()?)) +} + +fn u64_at(bytes: &[u8], at: usize) -> Option { + Some(u64::from_le_bytes(bytes_at(bytes, at, 8)?.try_into().ok()?)) +} + +fn signature_at(bytes: &[u8], at: usize, signature: &[u8; 4]) -> bool { + bytes_at(bytes, at, signature.len()) == Some(signature.as_slice()) +} + +#[cfg(test)] +mod tests; diff --git a/crates/zip/src/preflight/tests.rs b/crates/zip/src/preflight/tests.rs new file mode 100644 index 000000000..0fbc0d5c1 --- /dev/null +++ b/crates/zip/src/preflight/tests.rs @@ -0,0 +1,580 @@ +//! Tests for the ZIP preflight and the bounded entry read. +//! +//! Most cases build their bytes here rather than carrying archive fixtures, so the hostile shapes +//! (lying counts, offsets past the end, ZIP64 variants, overflow attempts) are visible in the test +//! and can be adjusted a byte at a time. + +use crate::Budget; +use crate::preflight::{self as zip, Directory, Error, Limits, MAX_PREALLOCATION, ReadError}; + +/// Limits wide enough that only the case under test can trip them. +fn roomy() -> Limits { + Limits { max_entries: 100_000, max_central_directory_bytes: 32 * 1024 * 1024, max_name_bytes: 4_096 } +} + +const EOCD_SIGNATURE: &[u8; 4] = b"PK\x05\x06"; +const ZIP64_EOCD_SIGNATURE: &[u8; 4] = b"PK\x06\x06"; +const ZIP64_LOCATOR_SIGNATURE: &[u8; 4] = b"PK\x06\x07"; +const CENTRAL_FILE_SIGNATURE: &[u8; 4] = b"PK\x01\x02"; + +/// One central-directory file header carrying `name`, with no extra field or comment. +fn central_entry(name: &[u8]) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(CENTRAL_FILE_SIGNATURE); + out.extend_from_slice(&20u16.to_le_bytes()); // version made by + out.extend_from_slice(&20u16.to_le_bytes()); // version needed + out.extend_from_slice(&0u16.to_le_bytes()); // flags + out.extend_from_slice(&0u16.to_le_bytes()); // method: stored + out.extend_from_slice(&0u16.to_le_bytes()); // time + out.extend_from_slice(&0u16.to_le_bytes()); // date + out.extend_from_slice(&0u32.to_le_bytes()); // crc32 + out.extend_from_slice(&0u32.to_le_bytes()); // compressed size + out.extend_from_slice(&0u32.to_le_bytes()); // uncompressed size + out.extend_from_slice(&u16::try_from(name.len()).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); // extra length + out.extend_from_slice(&0u16.to_le_bytes()); // comment length + out.extend_from_slice(&0u16.to_le_bytes()); // disk + out.extend_from_slice(&0u16.to_le_bytes()); // internal attributes + out.extend_from_slice(&0u32.to_le_bytes()); // external attributes + out.extend_from_slice(&0u32.to_le_bytes()); // local header offset + out.extend_from_slice(name); + assert_eq!(out.len(), 46 + name.len()); + out +} + +/// A minimal, well-formed archive holding `names`, with `comment` after its end record. The +/// returned offset is where that end record starts. +fn archive(names: &[&[u8]], comment: &[u8]) -> (Vec, usize) { + assert!(comment.len() <= usize::from(u16::MAX)); + let mut out = Vec::new(); + for name in names { + out.extend_from_slice(¢ral_entry(name)); + } + let directory_size = out.len(); + out.extend_from_slice(EOCD_SIGNATURE); + out.extend_from_slice(&0u16.to_le_bytes()); // disk + out.extend_from_slice(&0u16.to_le_bytes()); // directory disk + out.extend_from_slice(&u16::try_from(names.len()).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(&u16::try_from(names.len()).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(&u32::try_from(directory_size).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(&0u32.to_le_bytes()); // directory offset: the directory starts at 0 + out.extend_from_slice(&u16::try_from(comment.len()).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(comment); + let eocd_at = out.len() - comment.len() - 22; + (out, eocd_at) +} + +/// An end-record-shaped run declaring `comment_len` bytes after itself. It is a candidate end +/// record only when it also ends exactly at the end of the file. +fn forged_end_record(comment_len: u16) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(EOCD_SIGNATURE); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(&0u32.to_le_bytes()); + out.extend_from_slice(&0u32.to_le_bytes()); + out.extend_from_slice(&comment_len.to_le_bytes()); + assert_eq!(out.len(), 22); + out +} + +/// A single-entry archive whose central directory is preceded by `pad` bytes of file data, so the +/// directory does not start at zero. +fn archive_with_padding(pad: usize) -> Vec { + let mut out = vec![0u8; pad]; + let directory_at = out.len(); + out.extend_from_slice(¢ral_entry(b"word/document.xml")); + let directory_size = out.len() - directory_at; + out.extend_from_slice(EOCD_SIGNATURE); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out.extend_from_slice(&1u16.to_le_bytes()); + out.extend_from_slice(&1u16.to_le_bytes()); + out.extend_from_slice(&u32::try_from(directory_size).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(&u32::try_from(directory_at).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out +} + +/// A ZIP64 archive holding `names`: the entries, then the ZIP64 end record, its locator and the +/// classic end record with sentinel fields. +fn zip64_archive(names: &[&[u8]]) -> Vec { + let mut out = Vec::new(); + for name in names { + out.extend_from_slice(¢ral_entry(name)); + } + let directory_size = out.len(); + let zip64_at = out.len(); + out.extend_from_slice(ZIP64_EOCD_SIGNATURE); + out.extend_from_slice(&44u64.to_le_bytes()); // the 44 fixed bytes that follow + out.extend_from_slice(&45u16.to_le_bytes()); + out.extend_from_slice(&45u16.to_le_bytes()); + out.extend_from_slice(&0u32.to_le_bytes()); // this disk + out.extend_from_slice(&0u32.to_le_bytes()); // directory disk + out.extend_from_slice(&u64::from(names.len() as u32).to_le_bytes()); + out.extend_from_slice(&u64::from(names.len() as u32).to_le_bytes()); + out.extend_from_slice(&u64::try_from(directory_size).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(&0u64.to_le_bytes()); // directory offset + let locator_at = out.len(); + out.extend_from_slice(ZIP64_LOCATOR_SIGNATURE); + out.extend_from_slice(&0u32.to_le_bytes()); + out.extend_from_slice(&u64::try_from(zip64_at).unwrap_or(0).to_le_bytes()); + out.extend_from_slice(&1u32.to_le_bytes()); + assert_eq!(out.len() - locator_at, 20); + out.extend_from_slice(EOCD_SIGNATURE); + out.extend_from_slice(&u16::MAX.to_le_bytes()); + out.extend_from_slice(&u16::MAX.to_le_bytes()); + out.extend_from_slice(&u16::MAX.to_le_bytes()); + out.extend_from_slice(&u16::MAX.to_le_bytes()); + out.extend_from_slice(&u32::MAX.to_le_bytes()); + out.extend_from_slice(&u32::MAX.to_le_bytes()); + out.extend_from_slice(&0u16.to_le_bytes()); + out +} + +/// Overwrite the bytes at `at` in `bytes`; every offset here comes from the fixture, not a file. +fn put(bytes: &mut [u8], at: usize, value: &[u8]) { + let end = at + value.len(); + bytes.get_mut(at..end).unwrap().copy_from_slice(value); +} + +/// Overwrite the little-endian `u16` at `at`. +fn put_u16(bytes: &mut [u8], at: usize, value: u16) { + put(bytes, at, &value.to_le_bytes()); +} + +/// Overwrite the little-endian `u32` at `at`. +fn put_u32(bytes: &mut [u8], at: usize, value: u32) { + put(bytes, at, &value.to_le_bytes()); +} + +/// An entry error's message. `ReadError` carries a `std::io::Error`, which is not comparable, so +/// tests compare what the caller would see. +fn message(error: Option) -> Option { + error.map(|error| error.to_string()) +} + +#[test] +fn a_plain_archive_is_counted_and_its_offsets_are_honoured() { + let (bytes, _) = archive(&[b"a.txt", b"dir/b.xml"], b""); + assert_eq!(zip::preflight(&bytes, roomy()).unwrap(), Directory { entries: 2, central_directory_bytes: 106 }); + + // A directory that does not start at zero, because local file data precedes it. + let padded = archive_with_padding(1_000); + assert_eq!(zip::preflight(&padded, roomy()).unwrap(), Directory { entries: 1, central_directory_bytes: 46 + 17 }); +} + +#[test] +fn an_archive_comment_ending_exactly_at_eof_is_accepted() { + let (bytes, _) = archive(&[b"a.txt"], b"a comment with PK\x05\x06 inside"); + assert_eq!(zip::preflight(&bytes, roomy()).unwrap(), Directory { entries: 1, central_directory_bytes: 51 }); +} + +#[test] +fn a_file_too_short_for_an_end_record_is_refused() { + for length in 0..22 { + assert_eq!(zip::preflight(&vec![0u8; length], roomy()), Err(Error::MissingEndRecord)); + } + // A signature whose comment length runs past the end of the file is not an end record. + let mut bytes = vec![0u8; 22]; + put(&mut bytes, 0, EOCD_SIGNATURE); + put_u16(&mut bytes, 20, 7); + assert_eq!(zip::preflight(&bytes, roomy()), Err(Error::MissingEndRecord)); +} + +#[test] +fn a_second_end_record_inside_the_comment_is_ambiguous() { + // The real end record's comment contains another end record that also ends exactly at EOF. + let trailing = b"trailing"; + let mut comment = forged_end_record(u16::try_from(trailing.len()).unwrap()); + comment.extend_from_slice(trailing); + let (bytes, _) = archive(&[b"a.txt"], &comment); + assert_eq!(bytes.len() - comment.len() + 22 + trailing.len(), bytes.len(), "the forged record must reach EOF"); + assert_eq!(zip::preflight(&bytes, roomy()), Err(Error::AmbiguousEndRecords)); +} + +#[test] +fn a_truncated_archive_is_refused() { + let (bytes, _) = archive(&[b"a.txt", b"b.txt"], b""); + for keep in [0, 1, 10, 21, 22, bytes.len() - 23, bytes.len() - 1] { + let truncated = bytes.get(..keep).unwrap_or_default().to_vec(); + assert!(zip::preflight(&truncated, roomy()).is_err(), "a truncation to {keep} bytes was accepted"); + } + // A directory size that no longer reaches the end records. + let (mut short, eocd_at) = archive(&[b"a.txt"], b""); + put_u32(&mut short, eocd_at + 12, 1); + assert_eq!(zip::preflight(&short, roomy()), Err(Error::Invalid("central-directory offset and size do not reach the end records"))); +} + +#[test] +fn a_directory_offset_past_the_end_of_the_file_is_refused() { + let (mut bytes, eocd_at) = archive(&[b"a.txt"], b""); + put_u32(&mut bytes, eocd_at + 16, 1_000_000); + assert_eq!(zip::preflight(&bytes, roomy()), Err(Error::Invalid("central-directory offset and size do not reach the end records"))); + + // An offset that does not match the declared size. + let (mut shifted, eocd_at) = archive(&[b"a.txt"], b""); + put_u32(&mut shifted, eocd_at + 16, 1); + assert_eq!(zip::preflight(&shifted, roomy()), Err(Error::Invalid("central-directory offset and size do not reach the end records"))); + + // Offset and size whose sum is far past the end of the file. The limits are lifted so this + // exercises the offset arithmetic rather than the size limit catching it first. The classic + // maximum on either field means "look at the ZIP64 records" in the format, so the lying values + // stop one short of it and the ZIP64 case below uses it. + let unlimited = Limits { max_entries: u64::MAX, max_central_directory_bytes: u64::MAX, ..roomy() }; + let (mut far, eocd_at) = archive(&[b"a.txt"], b""); + put_u32(&mut far, eocd_at + 12, u32::MAX - 1); + put_u32(&mut far, eocd_at + 16, u32::MAX - 1); + assert_eq!(zip::preflight(&far, unlimited), Err(Error::Invalid("central-directory offset and size do not reach the end records"))); + + // The same on the ZIP64 path, where the sum cannot even be represented. + let mut zip64 = zip64_archive(&[b"a.txt"]); + let zip64_at = zip64.len() - 22 - 20 - 56; + put(&mut zip64, zip64_at + 48, &u64::MAX.to_le_bytes()); + put(&mut zip64, zip64_at + 40, &u64::MAX.to_le_bytes()); + assert_eq!(zip::preflight(&zip64, unlimited), Err(Error::Invalid("central-directory offset overflow"))); +} + +#[test] +fn the_entry_count_limit_is_enforced() { + let (bytes, _) = archive(&[b"a.txt", b"b.txt", b"c.txt"], b""); + assert_eq!(zip::preflight(&bytes, Limits { max_entries: 3, ..roomy() }).unwrap().entries, 3); + assert_eq!(zip::preflight(&bytes, Limits { max_entries: 2, ..roomy() }), Err(Error::TooManyEntries { actual: 3, limit: 2 })); + assert_eq!(zip::preflight(&bytes, Limits { max_entries: 0, ..roomy() }), Err(Error::TooManyEntries { actual: 3, limit: 0 })); +} + +#[test] +fn the_directory_size_limit_is_enforced() { + let (bytes, _) = archive(&[b"a.txt"], b""); + let size = 51u64; + assert_eq!(zip::preflight(&bytes, Limits { max_central_directory_bytes: size, ..roomy() }).unwrap().central_directory_bytes, size); + assert_eq!( + zip::preflight(&bytes, Limits { max_central_directory_bytes: size - 1, ..roomy() }), + Err(Error::CentralDirectoryTooLarge { actual: size, limit: size - 1 }) + ); +} + +#[test] +fn the_entry_name_limit_is_enforced() { + let long = vec![b'n'; 200]; + let (bytes, _) = archive(&[&long], b""); + assert_eq!(zip::preflight(&bytes, Limits { max_name_bytes: 200, ..roomy() }).unwrap().entries, 1); + assert_eq!(zip::preflight(&bytes, Limits { max_name_bytes: 199, ..roomy() }), Err(Error::NameTooLong { actual: 200, limit: 199 })); + + // An empty name is malformed whatever the limit says. + let (empty, _) = archive(&[b""], b""); + assert_eq!(zip::preflight(&empty, roomy()), Err(Error::NameTooLong { actual: 0, limit: 4_096 })); + + // A name length that would run past the declared directory. + let (mut lying, _) = archive(&[b"a.txt"], b""); + put_u16(&mut lying, 28, u16::MAX); + assert!(zip::preflight(&lying, roomy()).is_err()); +} + +#[test] +fn end_record_counts_that_disagree_with_the_directory_are_caught() { + // Far more entries than the directory holds: the signatures run out inside the directory. + let (mut too_many, eocd_at) = archive(&[b"a.txt"], b""); + put_u16(&mut too_many, eocd_at + 8, 1_000); + put_u16(&mut too_many, eocd_at + 10, 1_000); + assert!(matches!(zip::preflight(&too_many, roomy()), Err(Error::Invalid(_)))); + + // Fewer entries than the directory holds: the entries no longer fill the declared size. + let (mut too_few, eocd_at) = archive(&[b"a.txt", b"b.txt"], b""); + put_u16(&mut too_few, eocd_at + 8, 1); + put_u16(&mut too_few, eocd_at + 10, 1); + assert_eq!(zip::preflight(&too_few, roomy()), Err(Error::Invalid("the central-directory entry count and size disagree"))); + + // A huge declared count is refused by the walk once the limit is lifted, and by the limit + // before the walk when the limit is the tighter bound. + let (mut huge, eocd_at) = archive(&[b"a.txt"], b""); + put_u16(&mut huge, eocd_at + 8, u16::MAX - 1); + put_u16(&mut huge, eocd_at + 10, u16::MAX - 1); + assert!(matches!(zip::preflight(&huge, roomy()), Err(Error::Invalid(_)))); + assert_eq!(zip::preflight(&huge, Limits { max_entries: 10, ..roomy() }), Err(Error::TooManyEntries { actual: 65_534, limit: 10 })); +} + +#[test] +fn entries_or_end_records_that_name_another_disk_are_refused() { + let (mut entry, _) = archive(&[b"a.txt"], b""); + put_u16(&mut entry, 34, 1); + assert_eq!(zip::preflight(&entry, roomy()), Err(Error::Invalid("a central-directory entry refers to another disk"))); + + let (mut directory_disk, eocd_at) = archive(&[b"a.txt"], b""); + put_u16(&mut directory_disk, eocd_at + 6, 1); + assert_eq!(zip::preflight(&directory_disk, roomy()), Err(Error::Invalid("multi-disk ZIP archive"))); + + let (mut disk, eocd_at) = archive(&[b"a.txt"], b""); + put_u16(&mut disk, eocd_at + 4, 1); + assert_eq!(zip::preflight(&disk, roomy()), Err(Error::Invalid("multi-disk ZIP archive"))); + + let (mut disk_entries, eocd_at) = archive(&[b"a.txt"], b""); + put_u16(&mut disk_entries, eocd_at + 8, 2); + assert_eq!(zip::preflight(&disk_entries, roomy()), Err(Error::Invalid("multi-disk ZIP archive"))); +} + +#[test] +fn one_trailing_digital_signature_is_allowed_and_other_trailing_data_is_not() { + // A one-entry archive whose directory is preceded by 1,000 bytes, so offsets are not zero and + // the directory size can be adjusted without moving the end record. + let mut bytes = vec![0u8; 1_000]; + bytes.extend_from_slice(¢ral_entry(b"a.txt")); + let directory_size = bytes.len() - 1_000; + bytes.extend_from_slice(EOCD_SIGNATURE); + bytes.extend_from_slice(&0u16.to_le_bytes()); + bytes.extend_from_slice(&0u16.to_le_bytes()); + bytes.extend_from_slice(&1u16.to_le_bytes()); + bytes.extend_from_slice(&1u16.to_le_bytes()); + bytes.extend_from_slice(&u32::try_from(directory_size).unwrap_or(0).to_le_bytes()); + bytes.extend_from_slice(&1_000u32.to_le_bytes()); + bytes.extend_from_slice(&0u16.to_le_bytes()); + let eocd_at = bytes.len() - 22; + assert_eq!(zip::preflight(&bytes, roomy()).unwrap(), Directory { entries: 1, central_directory_bytes: 51 }); + + // A central-directory digital signature record appended to the directory is legal. + let mut signed = bytes.get(..eocd_at).unwrap_or_default().to_vec(); + signed.extend_from_slice(b"PK\x05\x05"); + signed.extend_from_slice(&3u16.to_le_bytes()); + signed.extend_from_slice(b"xyz"); + signed.extend_from_slice(bytes.get(eocd_at..).unwrap_or_default()); + let signed_eocd_at = signed.len() - 22; + put_u32(&mut signed, signed_eocd_at + 12, directory_size as u32 + 6 + 3); + assert_eq!(zip::preflight(&signed, roomy()).unwrap(), Directory { entries: 1, central_directory_bytes: 60 }); + + // Any other trailing data makes the entry count and the declared size disagree. + let mut junk = bytes.get(..eocd_at).unwrap_or_default().to_vec(); + junk.extend_from_slice(b"junk"); + junk.extend_from_slice(bytes.get(eocd_at..).unwrap_or_default()); + let junk_eocd_at = junk.len() - 22; + put_u32(&mut junk, junk_eocd_at + 12, directory_size as u32 + 4); + assert_eq!(zip::preflight(&junk, roomy()), Err(Error::Invalid("the central-directory entry count and size disagree"))); +} + +#[test] +fn zip64_end_records_are_walked_and_their_totals_are_limited() { + let bytes = zip64_archive(&[b"a.txt", b"b.txt"]); + assert_eq!(zip::preflight(&bytes, roomy()).unwrap(), Directory { entries: 2, central_directory_bytes: 2 * 46 + 4 + 6 }); + + assert_eq!(zip::preflight(&bytes, Limits { max_entries: 1, ..roomy() }), Err(Error::TooManyEntries { actual: 2, limit: 1 })); +} + +#[test] +fn zip64_sentinels_without_a_zip64_end_record_are_refused() { + let (mut bytes, eocd_at) = archive(&[b"a.txt"], b""); + put_u16(&mut bytes, eocd_at + 10, u16::MAX); + assert_eq!(zip::preflight(&bytes, roomy()), Err(Error::Invalid("ZIP64 sentinel without a ZIP64 end record"))); +} + +#[test] +fn zip64_records_are_checked_against_their_locator_and_each_other() { + let good = zip64_archive(&[b"a.txt", b"b.txt"]); + let locator_at = good.len() - 22 - 20; + let zip64_at = locator_at - 56; + + // A locator pointing where there is no ZIP64 end record. + let mut missing = good.clone(); + put(&mut missing, locator_at + 8, &7u64.to_le_bytes()); + assert_eq!(zip::preflight(&missing, roomy()), Err(Error::Invalid("the ZIP64 end record is missing at its declared offset"))); + + // A ZIP64 record whose length does not put its end at the locator. + let mut bad_size = good.clone(); + put(&mut bad_size, zip64_at + 4, &45u64.to_le_bytes()); + assert_eq!(zip::preflight(&bad_size, roomy()), Err(Error::Invalid("the ZIP64 end record does not end at its locator"))); + + // A ZIP64 record too short to hold the fields it must. + let mut too_short = good.clone(); + put(&mut too_short, zip64_at + 4, &43u64.to_le_bytes()); + assert_eq!(zip::preflight(&too_short, roomy()), Err(Error::Invalid("the ZIP64 end record is too short"))); + + // A locator that describes more than one disk. + let mut multi_disk = good.clone(); + put(&mut multi_disk, locator_at + 16, &2u32.to_le_bytes()); + assert_eq!(zip::preflight(&multi_disk, roomy()), Err(Error::Invalid("multi-disk ZIP64 archive"))); + + // Classic fields that contradict the ZIP64 record. + let mut lying = good.clone(); + let eocd_at = lying.len() - 22; + put_u16(&mut lying, eocd_at + 10, 1); + assert_eq!(zip::preflight(&lying, roomy()), Err(Error::Invalid("the classic and ZIP64 end records disagree"))); + + // A ZIP64 offset that cannot be a platform offset. + let mut huge = good.clone(); + put(&mut huge, locator_at + 8, &u64::MAX.to_le_bytes()); + assert!(matches!(zip::preflight(&huge, roomy()), Err(Error::Invalid(_)))); +} + +#[test] +fn corrupting_any_single_byte_of_an_archive_never_panics() { + let (bytes, _) = archive(&[b"a.txt", b"b.xml"], b"comment"); + for at in 0..bytes.len() { + for value in [0x00u8, 0x01, 0x41, 0xff] { + let mut corrupted = bytes.clone(); + if let Some(byte) = corrupted.get_mut(at) { + *byte = value; + } + let _ = zip::preflight(&corrupted, roomy()); + } + } +} + +#[test] +fn an_entry_exactly_at_its_declared_size_is_read_and_charged() { + let mut total = Budget::new("document", 1_000); + let data = vec![7u8; 100]; + let out = zip::read_entry(std::io::Cursor::new(data.as_slice()), 100, 1_000, &mut total).unwrap(); + assert_eq!(out.len(), 100); + assert_eq!(total.used(), 100); + assert_eq!(total.remaining(), 900); +} + +#[test] +fn a_declared_size_over_the_per_entry_limit_is_refused_before_reading() { + let mut total = Budget::new("document", 1_000_000); + let data = vec![0u8; 10]; + assert_eq!( + message(zip::read_entry(std::io::Cursor::new(data.as_slice()), 5_000, 4_096, &mut total).err()), + Some("the entry declares 5000 bytes; the limit is 4096".to_string()) + ); + assert_eq!(total.used(), 0, "a refused entry must not be charged"); +} + +#[test] +fn a_lying_declared_size_is_caught_and_an_overstated_one_only_bounds_the_read() { + let mut total = Budget::new("document", 1_000_000); + let data = vec![7u8; 100]; + + // A hundred bytes arrive from an entry that declared ten. + assert_eq!( + message(zip::read_entry(std::io::Cursor::new(data.as_slice()), 10, 1_000, &mut total).err()), + Some("the entry produced more than the 10 bytes it is allowed".to_string()) + ); + assert_eq!(total.used(), 0, "an overrun must not be charged"); + + // A hundred bytes arrive from an entry that declared a thousand: bounded, and only the real + // bytes are charged. + let out = zip::read_entry(std::io::Cursor::new(data.as_slice()), 1_000, 4_096, &mut total).unwrap(); + assert_eq!(out.len(), 100); + assert_eq!(total.used(), 100); +} + +#[test] +fn a_decompression_bomb_is_rejected_at_the_smallest_bound() { + // The entry declares a megabyte and inflates past it: the per-entry bound stops the read. + let mut total = Budget::new("document", 64 * 1024 * 1024); + let bomb = Bomb { remaining: 128 * 1024 * 1024, chunk: 64 * 1024 }; + let error = zip::read_entry(bomb, 1_000_000, 4 * 1024 * 1024, &mut total).err(); + assert_eq!(message(error), Some("the entry produced more than the 1000000 bytes it is allowed".to_string())); + assert_eq!(total.used(), 0, "a bomb must not be charged anything"); + + // The same bomb under a per-entry limit smaller than the declaration: still refused. + let mut total = Budget::new("document", 64 * 1024 * 1024); + let bomb = Bomb { remaining: 128 * 1024 * 1024, chunk: 64 * 1024 }; + let error = zip::read_entry(bomb, 100 * 1024 * 1024, 4_096, &mut total).err(); + assert_eq!(message(error), Some("the entry declares 104857600 bytes; the limit is 4096".to_string())); +} + +#[test] +fn a_bomb_is_rejected_at_the_total_budget_when_that_is_the_tighter_bound() { + let mut total = Budget::new("document", 8 * 1024); + let bomb = Bomb { remaining: 16 * 1024 * 1024, chunk: 1_024 }; + let error = zip::read_entry(bomb, 1024 * 1024, 1024 * 1024, &mut total).err(); + assert!(matches!(error, Some(ReadError::Budget(_))), "{error:?}"); + assert_eq!(total.used(), 0); +} +#[test] +fn the_total_budget_bounds_a_series_of_entries() { + let mut total = Budget::new("document", 250); + let first = vec![1u8; 100]; + let second = vec![2u8; 100]; + let third = vec![3u8; 100]; + assert_eq!(zip::read_entry(std::io::Cursor::new(first.as_slice()), 100, 1_000, &mut total).unwrap().len(), 100); + assert_eq!(zip::read_entry(std::io::Cursor::new(second.as_slice()), 100, 1_000, &mut total).unwrap().len(), 100); + assert_eq!(total.remaining(), 50); + // Only 50 bytes fit and the third entry wants 100. + assert!(matches!(zip::read_entry(std::io::Cursor::new(third.as_slice()), 100, 1_000, &mut total).err(), Some(ReadError::Budget(_)))); + assert_eq!(total.used(), 200); +} + +#[test] +fn a_declared_size_does_not_preallocate_more_than_the_fixed_cap() { + let mut total = Budget::new("document", u64::from(u32::MAX)); + let data = vec![0u8; 8]; + let out = zip::read_entry(std::io::Cursor::new(data.as_slice()), u64::from(u32::MAX), u64::from(u32::MAX), &mut total).unwrap(); + assert_eq!(out.len(), 8); + // The buffer must not have reserved the declared size. Capacity may exceed the length, so this + // checks the reservation really was capped rather than a rounded-up length. + assert!(out.capacity() < 1024 * 1024, "a lying declaration reserved {} bytes", out.capacity()); + assert!(u64::try_from(out.capacity()).unwrap_or(u64::MAX) <= MAX_PREALLOCATION.saturating_add(64)); +} + +#[test] +fn an_exhausted_budget_admits_nothing_and_a_zero_length_entry_still_reads() { + let mut total = Budget::new("document", 0); + assert!(matches!(zip::read_entry(std::io::Cursor::new([1u8, 2, 3].as_slice()), 3, 100, &mut total).err(), Some(ReadError::Budget(_)))); + let mut empty = Budget::new("document", 0); + assert_eq!(zip::read_entry(std::io::Cursor::new([].as_slice()), 0, 100, &mut empty).ok(), Some(Vec::new())); +} + +#[test] +fn a_reader_that_fails_is_reported_and_nothing_is_charged() { + let mut total = Budget::new("document", 1_000); + let error = zip::read_entry(Failing, 100, 1_000, &mut total).err(); + assert!(matches!(error, Some(ReadError::Io(_))), "{error:?}"); + assert_eq!(total.used(), 0); +} + +#[test] +fn read_errors_describe_themselves() { + assert_eq!(ReadError::DeclaredTooLarge { declared: 9, limit: 1 }.to_string(), "the entry declares 9 bytes; the limit is 1"); + assert_eq!(ReadError::Overrun { limit: 4 }.to_string(), "the entry produced more than the 4 bytes it is allowed"); + assert_eq!( + ReadError::Budget(crate::Exceeded { what: "document", limit: 8, requested: 9 }).to_string(), + "the document limit is 8; another 9 was requested" + ); + assert!(std::error::Error::source(&ReadError::Budget(crate::Exceeded { what: "x", limit: 0, requested: 1 })).is_some()); + assert!(std::error::Error::source(&ReadError::Io(std::io::Error::other("closed"))).is_some()); + assert!(std::error::Error::source(&ReadError::Overrun { limit: 1 }).is_none()); +} + +#[test] +fn preflight_errors_describe_themselves() { + let cases = [ + (Error::MissingEndRecord, "the ZIP end record is missing"), + (Error::AmbiguousEndRecords, "the ZIP has more than one plausible end record"), + (Error::Invalid("why"), "invalid ZIP structure: why"), + (Error::TooManyEntries { actual: 5, limit: 2 }, "the ZIP declares 5 entries; the limit is 2"), + (Error::CentralDirectoryTooLarge { actual: 9, limit: 8 }, "the ZIP central directory is 9 bytes; the limit is 8"), + (Error::NameTooLong { actual: 3, limit: 2 }, "a ZIP entry name is 3 bytes; the limit is 2"), + ]; + for (error, expected) in cases { + assert_eq!(error.to_string(), expected); + } +} + +/// A reader that produces far more bytes than anyone asked for, standing in for a zip bomb. +struct Bomb { + remaining: usize, + chunk: usize, +} + +impl std::io::Read for Bomb { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let want = buf.len().min(self.chunk).min(self.remaining); + for byte in buf.get_mut(..want).unwrap_or_default() { + *byte = 0x41; + } + self.remaining = self.remaining.saturating_sub(want); + Ok(want) + } +} + +/// A reader that always fails. +struct Failing; + +impl std::io::Read for Failing { + fn read(&mut self, _buf: &mut [u8]) -> std::io::Result { + Err(std::io::Error::other("the entry is corrupt")) + } +} diff --git a/crates/zip/src/write.rs b/crates/zip/src/write.rs new file mode 100644 index 000000000..2f5d7ca47 --- /dev/null +++ b/crates/zip/src/write.rs @@ -0,0 +1,58 @@ +//! A ZIP archive being written. + +use std::io::{Cursor, Write}; + +use crate::Error; + +/// How an entry is stored. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Compression { + /// Uncompressed: a `mimetype` entry that must be readable in place, or data that is + /// compressed already. + Stored, + /// Deflate at level 6. + Deflated, + /// Deflate at the given level (0 to 9; higher is clamped). + Level(u8), +} + +/// A ZIP archive being written into memory. The output is deterministic: the same (1980) +/// timestamp on every entry. Entries are written in the order they are added. +pub struct Writer { + zip: zip::ZipWriter>>, +} + +impl Default for Writer { + fn default() -> Writer { + Writer { zip: zip::ZipWriter::new(Cursor::new(Vec::new())) } + } +} + +impl Writer { + pub fn new() -> Writer { + Writer::default() + } + + /// Adds an entry. A name the archive already has is an error. + pub fn add(&mut self, name: &str, data: &[u8], compression: Compression) -> Result<(), Error> { + let (method, level) = match compression { + Compression::Stored => (zip::CompressionMethod::Stored, None), + Compression::Deflated => (zip::CompressionMethod::Deflated, Some(6)), + Compression::Level(l) => (zip::CompressionMethod::Deflated, Some(i64::from(l.min(9)))), + }; + let options = zip::write::SimpleFileOptions::default() + .compression_method(method) + .compression_level(level) + .last_modified_time(zip::DateTime::default()) + // Deflate can grow incompressible data a little, so ZIP64 well before 4 GiB. + .large_file(data.len() as u64 >= u64::from(u32::MAX) / 2); + let fail = |e: &dyn std::fmt::Display| Error::Write(format!("{name}: {e}")); + self.zip.start_file(name, options).map_err(|e| fail(&e))?; + self.zip.write_all(data).map_err(|e| fail(&e)) + } + + /// The finished archive. + pub fn finish(self) -> Result, Error> { + self.zip.finish().map(Cursor::into_inner).map_err(|e| Error::Write(e.to_string())) + } +} diff --git a/crates/zip/tests/archive.rs b/crates/zip/tests/archive.rs new file mode 100644 index 000000000..632c98a53 --- /dev/null +++ b/crates/zip/tests/archive.rs @@ -0,0 +1,148 @@ +//! Reading archives within limits, duplicate names, and the writer. +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use craft_zip::{Archive, Compression, Error, Limits, Writer, check_entry_count, zip64_entry_count_sample}; + +const ROOMY: Limits = Limits { max_entries: 1000, max_total_bytes: 1 << 20 }; + +fn archive(entries: &[(&str, &[u8], Compression)]) -> Vec { + let mut w = Writer::new(); + for (name, data, compression) in entries { + w.add(name, data, *compression).unwrap(); + } + w.finish().unwrap() +} + +fn find(bytes: &[u8], needle: &[u8], from: usize) -> usize { + from + bytes[from..].windows(needle.len()).position(|w| w == needle).unwrap() +} + +#[test] +fn entries_are_written_in_order_and_read_back() { + let bytes = archive(&[ + ("mimetype", b"application/x-test", Compression::Stored), + ("dir/a.xml", &[b'a'; 5000], Compression::Deflated), + ("b.bin", b"\x00\x01", Compression::Level(9)), + ]); + // A stored first entry is readable in place: local header (30) + name (8). + assert_eq!(&bytes[38..56], b"application/x-test"); + assert!(bytes.len() < 1000, "the deflated entry is compressed"); + let mut z = Archive::open(&bytes, ROOMY).unwrap(); + assert_eq!(z.names().collect::>(), ["mimetype", "dir/a.xml", "b.bin"]); + assert_eq!((z.len(), z.is_empty(), z.has_duplicate_names()), (3, false, false)); + assert!(z.contains("b.bin") && !z.contains("B.bin")); + assert_eq!(z.declared_size("dir/a.xml"), Some(5000)); + assert_eq!(z.declared_size("nope"), None); + assert_eq!(z.read("dir/a.xml", 5000).unwrap().unwrap(), vec![b'a'; 5000]); + assert_eq!(z.read("b.bin", 10).unwrap().unwrap(), [0, 1]); + assert_eq!(z.read("nope", 10).unwrap(), None); + assert_eq!(z.budget().used(), 5002); +} + +#[test] +fn the_output_is_deterministic() { + let make = || archive(&[("a", b"one", Compression::Deflated), ("b", b"two", Compression::Stored)]); + assert_eq!(make(), make()); +} + +#[test] +fn adding_a_name_twice_is_an_error() { + let mut w = Writer::new(); + w.add("a", b"1", Compression::Stored).unwrap(); + assert!(matches!(w.add("a", b"2", Compression::Stored), Err(Error::Write(m)) if m.starts_with("a: "))); +} + +#[test] +fn what_is_not_a_zip_is_refused() { + assert!(matches!(Archive::open(b"not a zip", ROOMY).err(), Some(Error::NotZip(_)))); + assert!(matches!(Archive::open(b"", ROOMY).err(), Some(Error::NotZip(_)))); + let bytes = archive(&[("a", b"abc", Compression::Stored)]); + assert!(Archive::open(&bytes[..bytes.len() - 5], ROOMY).is_err()); +} + +#[test] +fn an_entry_past_its_limit_is_refused_and_not_charged() { + let bytes = archive(&[("a", &[b'a'; 100], Compression::Deflated)]); + let mut z = Archive::open(&bytes, ROOMY).unwrap(); + assert_eq!(z.read("a", 99), Err(Error::EntryTooLarge { name: "a".into(), limit: 99 })); + assert_eq!(z.budget().used(), 0); + assert_eq!(z.read("a", 100).unwrap().map(|d| d.len()), Some(100)); +} + +#[test] +fn entries_cannot_add_up_past_the_total() { + let data = [b'a'; 100]; + let bytes = archive(&[("a", &data, Compression::Deflated), ("b", &data, Compression::Deflated), ("c", &data, Compression::Deflated)]); + let mut z = Archive::open(&bytes, Limits { max_total_bytes: 250, ..ROOMY }).unwrap(); + assert!(z.read("a", 1000).unwrap().is_some()); + assert!(z.read("b", 1000).unwrap().is_some()); + assert_eq!(z.budget().remaining(), 50); + let refused = z.read("c", 1000); + assert_eq!(refused, Err(Error::BudgetSpent { name: "c".into(), limit: 250 })); + assert!(refused.is_err_and(|e| e.is_limit())); + // A caller with its own rule can charge more. + z.budget_mut().charge(50).unwrap(); + assert_eq!(z.budget().remaining(), 0); +} + +/// The size an entry declares is a claim. One that expands past it is refused whatever the +/// limit, and a small declared size does not get a large entry under a small limit. +#[test] +fn an_entry_that_expands_past_its_declared_size_is_refused() { + let mut bytes = archive(&[("a", &[b'a'; 5000], Compression::Deflated)]); + // The uncompressed size, in the central header (at 24) and the local header (at 22). + let central = find(&bytes, b"PK\x01\x02", 0); + for at in [central + 24, 22] { + assert_eq!(bytes[at..at + 4], 5000u32.to_le_bytes()); + bytes[at..at + 4].copy_from_slice(&10u32.to_le_bytes()); + } + let mut z = Archive::open(&bytes, ROOMY).unwrap(); + assert_eq!(z.declared_size("a"), Some(10)); + // `zip` notices first when it can; either way it is refused and nothing is charged. + assert!(matches!(z.read("a", 1 << 20), Err(Error::EntryTooLarge { limit: 10, .. } | Error::Entry { .. }))); + assert_eq!(z.budget().used(), 0); +} + +#[test] +fn a_broken_entry_is_an_entry_error() { + let mut bytes = archive(&[("a", b"hello world", Compression::Stored)]); + let at = find(&bytes, b"hello", 0); + bytes[at] = b'j'; + let mut z = Archive::open(&bytes, ROOMY).unwrap(); + assert!(matches!(z.read("a", 100), Err(Error::Entry { name, .. }) if name == "a")); + assert_eq!(z.budget().used(), 0); +} + +#[test] +fn two_entries_with_one_name_are_told() { + let mut bytes = archive(&[("a", b"first", Compression::Stored), ("b", b"second", Compression::Stored)]); + assert!(!Archive::open(&bytes, ROOMY).unwrap().has_duplicate_names()); + // Rename `b` to `a` in its local header and in the central directory. + let local = find(&bytes, b"PK\x03\x04", 4); + bytes[local + 30] = b'a'; + let central = find(&bytes, b"PK\x01\x02", find(&bytes, b"PK\x01\x02", 0) + 4); + assert_eq!(bytes[central + 46], b'b'); + bytes[central + 46] = b'a'; + let z = Archive::open(&bytes, ROOMY).unwrap(); + assert_eq!(z.len(), 1); + assert!(z.has_duplicate_names()); +} + +#[test] +fn an_archive_declaring_too_many_entries_is_refused_before_it_is_indexed() { + let bomb = zip64_entry_count_sample(20_001); + assert_eq!(check_entry_count(&bomb, 20_000), Err(Error::TooManyEntries { declared: 20_001, limit: 20_000 })); + assert_eq!(Archive::open(&bomb, Limits { max_entries: 20_000, ..ROOMY }).err(), Some(Error::TooManyEntries { declared: 20_001, limit: 20_000 })); + let bytes = archive(&[("a", b"", Compression::Stored), ("b", b"", Compression::Stored), ("c", b"", Compression::Stored)]); + assert_eq!(Archive::open(&bytes, Limits { max_entries: 2, ..ROOMY }).err(), Some(Error::TooManyEntries { declared: 3, limit: 2 })); + assert!(Archive::open(&bytes, Limits { max_entries: 3, ..ROOMY }).is_ok()); +} + +/// What the strict preflight accepts, the writer writes. +#[test] +fn written_archives_pass_the_strict_preflight() { + use craft_zip::preflight::{Limits, preflight}; + let bytes = archive(&[("mimetype", b"x", Compression::Stored), ("a/b.xml", &[b'a'; 300], Compression::Deflated)]); + let directory = preflight(&bytes, Limits { max_entries: 10, max_central_directory_bytes: 1 << 16, max_name_bytes: 256 }).unwrap(); + assert_eq!(directory.entries, 2); +} diff --git a/crates/zip/tests/public_api.rs b/crates/zip/tests/public_api.rs new file mode 100644 index 000000000..6b29601c3 --- /dev/null +++ b/crates/zip/tests/public_api.rs @@ -0,0 +1,76 @@ +//! The crate used the way a dependent app uses it: only the public API, from outside the crate. +//! +//! The unit tests reach into module internals; these cases check that the seam an app actually +//! consumes (a budget, a preflight, a bounded read) is enough to hold the line, and +//! that the examples in the crate README work as written. + +use craft_zip::preflight::{self as zip, Limits as ZipLimits}; +use craft_zip::{Budget, Exceeded}; + +/// A reader that never runs out of bytes, standing in for a decompression bomb. +struct Endless; + +impl std::io::Read for Endless { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + for byte in buf.iter_mut() { + *byte = 0x41; + } + Ok(buf.len()) + } +} + +#[test] +fn the_readme_budget_example_works() { + let mut total = Budget::new("document", 512 * 1024 * 1024); + assert_eq!(total.charge(1024), Ok(())); + assert_eq!(total.remaining(), 512 * 1024 * 1024 - 1024); +} + +#[test] +fn a_shared_budget_bounds_an_import_of_several_entries() { + // An app's whole-import limit, charged by each entry as it is read. + let mut total = Budget::new("document", 1_000); + let entry = vec![0u8; 600]; + let first = zip::read_entry(std::io::Cursor::new(entry.as_slice()), 600, 4_096, &mut total).unwrap(); + assert_eq!(first.len(), 600); + + // Six hundred more bytes do not fit in the four hundred that remain. + let second = zip::read_entry(std::io::Cursor::new(entry.as_slice()), 600, 4_096, &mut total).err(); + assert!(second.is_some(), "the total budget must stop the second entry"); + // And the budget still reports only the bytes that really arrived. + assert_eq!(total.used(), 600); + assert_eq!(total.remaining(), 400); +} + +#[test] +fn an_endless_reader_cannot_get_past_the_smaller_bound() { + let mut total = Budget::new("part", 1_000_000); + let error = zip::read_entry(Endless, u64::MAX, 4_096, &mut total).err(); + // The declared size is over the per-entry limit, so the entry is refused before any read. + assert!(matches!(error, Some(zip::ReadError::DeclaredTooLarge { .. })), "{error:?}"); + assert_eq!(total.used(), 0); + + // With a declaration inside the limit, the read stops there even though the reader is endless. + let mut total = Budget::new("part", 1_000_000); + let error = zip::read_entry(Endless, 4_096, 4_096, &mut total).err(); + assert!(matches!(error, Some(zip::ReadError::Overrun { limit: 4_096 })), "{error:?}"); + assert_eq!(total.used(), 0, "a bomb charges nothing"); +} + +#[test] +fn a_budget_refusal_can_be_reported_in_the_apps_own_words() { + let mut total = Budget::new("worksheet", 10); + let refused: Exceeded = total.charge(11).expect_err("11 does not fit in 10"); + assert_eq!(refused.what, "worksheet"); + assert_eq!(refused.limit, 10); + assert_eq!(refused.requested, 11); + let message = refused.to_string(); + assert!(message.contains("worksheet") && message.contains("10") && message.contains("11"), "{message}"); +} + +#[test] +fn the_readme_zip_preflight_example_works() { + // The bytes of no archive at all: the example's shape is what is checked here. + let limits = ZipLimits { max_entries: 20_000, max_central_directory_bytes: 16 * 1024 * 1024, max_name_bytes: 1_024 }; + assert_eq!(zip::preflight(&[], limits), Err(zip::Error::MissingEndRecord)); +} From e8f31492243c1ba349083a36a110fc1216712efe Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:54:20 -0500 Subject: [PATCH 033/120] sonar: leave spec_tables.rs out of duplication detection too (#943) --- sonar-project.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 81209aea7..d08c71698 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -11,7 +11,7 @@ sonar.test.inclusions=**/tests/**,**/tests.rs,**/*_tests.rs # Codec constant tables are transcribed from the format specifications; their repeated rows are # data, not copy-pasted logic. -sonar.cpd.exclusions=**/tables.rs +sonar.cpd.exclusions=**/*tables.rs # S4790 (weak hash): the ICC specification defines the profile ID as the MD5 of the profile, so # no other algorithm produces an ID other software accepts. It identifies, it does not protect. From e5299a3be0535f7e0850cde71056d1cf81a16d49 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 18:54:48 -0500 Subject: [PATCH 034/120] craft-html: a tolerant tokenizer, character references, escaping and the declared charset (#944) The bottom layer every HTML reader in the Craft apps carried its own copy of. The tokenizer is MailCraft's (crates/html/src/tokenizer.rs), with what it dropped and capped turned into Options and Limits, a self-closing flag, and raw_content for callers that read a style sheet's text. The character references are WordCraft's (crates/formats/src/html/entity.rs at 06c2ae6f): its name table and the 94 legacy names that decode without a semicolon, with MailCraft's Latin-1 and Greek tables and its handling of numbers too long to be numbers. No tree, no CSS, no decoding of bytes. --- Cargo.lock | 7 + Cargo.toml | 1 + README.md | 1 + crates/html/Cargo.toml | 14 ++ crates/html/README.md | 68 ++++++ crates/html/src/entity.rs | 388 ++++++++++++++++++++++++++++++++ crates/html/src/entity/tests.rs | 132 +++++++++++ crates/html/src/lex.rs | 347 ++++++++++++++++++++++++++++ crates/html/src/lex/tests.rs | 177 +++++++++++++++ crates/html/src/lib.rs | 78 +++++++ 10 files changed, 1213 insertions(+) create mode 100644 crates/html/Cargo.toml create mode 100644 crates/html/README.md create mode 100644 crates/html/src/entity.rs create mode 100644 crates/html/src/entity/tests.rs create mode 100644 crates/html/src/lex.rs create mode 100644 crates/html/src/lex/tests.rs create mode 100644 crates/html/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 11b1a2c9d..ff7081810 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1150,6 +1150,13 @@ dependencies = [ "rayon", ] +[[package]] +name = "craft-html" +version = "0.1.0" +dependencies = [ + "proptest", +] + [[package]] name = "craft-i18n" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 249108320..9931dc81b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ craft-gcode = { path = "crates/gcode" } craft-gif = { path = "crates/gif" } craft-hdr = { path = "crates/hdr" } craft-mesh = { path = "crates/mesh" } +craft-html = { path = "crates/html" } craft-heif = { path = "crates/heif" } craft-fig = { path = "crates/fig" } craft-fonts-build = { path = "crates/fonts-build" } diff --git a/README.md b/README.md index ab4347290..85a327ab2 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = "tcafé <3", opts); +let mut text = String::new(); +while let Some(token) = lexer.next() { + match token { + Token::Text(t) => text.push_str(&unescape(t)), + // A style sheet's text, for a caller that reads it. + Token::Start(tag) if tag.name == "style" => assert_eq!(lexer.raw_content("style"), "p{}"), + Token::Start(tag) => assert_eq!(tag.attr("class").as_deref(), Some("x")), + Token::End(name) => assert_eq!(name, "b"), + } +} +assert_eq!(text, "café <3"); +assert_eq!(escape(&text), "café <3"); +``` + +## The tokenizer + +`Lexer` is an iterator of `Token::Text`, `Token::Start(Tag)` and `Token::End(name)`, borrowed +from the input. Text and attribute values come as written; `unescape` and `Tag::attr` decode +them. + +- A `<` that starts no tag is text. A tag, comment or raw element that never ends runs to the + end of the input. +- Comments, doctypes, processing instructions and conditional comments never come out. +- `Options::drop`: elements skipped whole, content and end tag included. +- `Options::raw_text`: elements whose content is one text token, not markup. +- `Lexer::raw_content(name)`: after a start tag, take the element's raw content yourself. +- `Limits`: attributes kept per tag (64) and the longest tag name (64 bytes; a longer one has an + empty name). + +## Character references + +`unescape` decodes named, decimal and hex references. The named set is HTML 4's (Latin-1, Greek, +symbols, arrows, mathematics) and a few more. The 94 legacy names decode without their `;` +(`© 2024`), longest match first, as in browsers; `unescape_attr` leaves such a name alone +when a letter, a digit or `=` follows, so a query string keeps its `©=1`. A numeric reference +to 0x80-0x9F means the Windows-1252 character; 0, surrogates, and numbers past U+10FFFF or too +long to be numbers become U+FFFD. Names are looked at for 32 bytes at most, so the work is +linear. + +`escape` writes `& < > " '` as references; `escape_dq` leaves `'`, for content and attribute +values in double quotes. + +## Not here + +- The full HTML5 named reference table (about 2,200 names): only the names above. +- Decoding bytes to text. `declared_charset` gives the label a page names; a decoder for it is + the caller's (`craft-mime::charset` for the WHATWG labels). +- Tree construction, implied end tags, CSS. + +## Provenance + +The tokenizer is MailCraft's (`crates/html/src/tokenizer.rs`), with its policy (which elements to +drop, the caps) turned into options. The character references are WordCraft's +(`crates/formats/src/html/entity.rs`: the name table and the legacy names without `;`), with +MailCraft's Latin-1 and Greek tables and its handling of numbers too long to be numbers. +Written from the HTML Living Standard. diff --git a/crates/html/src/entity.rs b/crates/html/src/entity.rs new file mode 100644 index 000000000..8ece41926 --- /dev/null +++ b/crates/html/src/entity.rs @@ -0,0 +1,388 @@ +//! Character references (`&`, `é`, `é`) and escaping. + +/// The Latin-1 names in code point order, from U+00A0 (`nbsp`) to U+00FF (`yuml`). +const LATIN1: [&str; 96] = [ + "nbsp", "iexcl", "cent", "pound", "curren", "yen", "brvbar", "sect", "uml", "copy", "ordf", "laquo", "not", "shy", "reg", "macr", "deg", + "plusmn", "sup2", "sup3", "acute", "micro", "para", "middot", "cedil", "sup1", "ordm", "raquo", "frac14", "frac12", "frac34", "iquest", "Agrave", + "Aacute", "Acirc", "Atilde", "Auml", "Aring", "AElig", "Ccedil", "Egrave", "Eacute", "Ecirc", "Euml", "Igrave", "Iacute", "Icirc", "Iuml", "ETH", + "Ntilde", "Ograve", "Oacute", "Ocirc", "Otilde", "Ouml", "times", "Oslash", "Ugrave", "Uacute", "Ucirc", "Uuml", "Yacute", "THORN", "szlig", + "agrave", "aacute", "acirc", "atilde", "auml", "aring", "aelig", "ccedil", "egrave", "eacute", "ecirc", "euml", "igrave", "iacute", "icirc", + "iuml", "eth", "ntilde", "ograve", "oacute", "ocirc", "otilde", "ouml", "divide", "oslash", "ugrave", "uacute", "ucirc", "uuml", "yacute", + "thorn", "yuml", +]; + +/// Greek capitals from U+0391; U+03A2 is unassigned. +const GREEK_UPPER: [&str; 25] = [ + "Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta", "Eta", "Theta", "Iota", "Kappa", "Lambda", "Mu", "Nu", "Xi", "Omicron", "Pi", "Rho", "", + "Sigma", "Tau", "Upsilon", "Phi", "Chi", "Psi", "Omega", +]; + +/// Greek small letters from U+03B1. +const GREEK_LOWER: [&str; 25] = [ + "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "omicron", "pi", "rho", + "sigmaf", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega", +]; + +/// What a numeric reference to 0x80–0x9F means (HTML Living Standard §13.2.5.80: the +/// Windows-1252 characters, by long tradition). +const C1: [u32; 32] = [ + 0x20AC, 0x81, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x8D, 0x017D, 0x8F, 0x90, 0x2018, 0x2019, + 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x9D, 0x017E, 0x0178, +]; + +/// Longest entity name looked at. +const MAX_NAME: usize = 32; + +/// The legacy names HTML also decodes without a `;`, sorted by bytes. +const BARE_NAMES: &[&str] = &[ + "AElig", "AMP", "Aacute", "Acirc", "Agrave", "Aring", "Atilde", "Auml", "COPY", "Ccedil", "Eacute", "Ecirc", "Egrave", "Euml", "GT", "Iacute", + "Icirc", "Igrave", "Iuml", "LT", "Ntilde", "Oacute", "Ocirc", "Ograve", "Oslash", "Otilde", "Ouml", "QUOT", "REG", "Uacute", "Ucirc", "Ugrave", + "Uuml", "Yacute", "aacute", "acirc", "aelig", "agrave", "amp", "aring", "atilde", "auml", "ccedil", "cent", "copy", "curren", "deg", "divide", + "eacute", "ecirc", "egrave", "euml", "frac12", "frac14", "frac34", "gt", "iacute", "icirc", "iexcl", "igrave", "iquest", "iuml", "laquo", "lt", + "micro", "middot", "nbsp", "ntilde", "oacute", "ocirc", "ograve", "oslash", "otilde", "ouml", "para", "plusmn", "pound", "quot", "raquo", "reg", + "sect", "shy", "sup1", "sup2", "sup3", "szlig", "times", "uacute", "ucirc", "ugrave", "uuml", "yacute", "yen", "yuml", +]; + +/// Text as HTML, for content and for attribute values in either kind of quote: `&`, `<`, `>`, +/// `"` and `'` become character references. +pub fn escape(text: &str) -> String { + let mut out = String::with_capacity(text.len().saturating_add(text.len() / 8)); + push_escaped(&mut out, text, true); + out +} + +/// [`escape`] that leaves `'` as it is: for content and for attribute values in double quotes. +pub fn escape_dq(text: &str) -> String { + let mut out = String::with_capacity(text.len().saturating_add(text.len() / 8)); + push_escaped(&mut out, text, false); + out +} + +fn push_escaped(out: &mut String, text: &str, apos: bool) { + for c in text.chars() { + match c { + '&' => out.push_str("&"), + '<' => out.push_str("<"), + '>' => out.push_str(">"), + '"' => out.push_str("""), + '\'' if apos => out.push_str("'"), + c => out.push(c), + } + } +} + +/// The character an entity body names (`amp`, `#38`, `#x26`: what stands between `&` and `;`). +pub fn decode_entity(body: &str) -> Option { + match body.strip_prefix('#') { + Some(_) => reference(body, false).filter(|(_, used)| *used == body.len()).map(|(c, _)| c), + None => named(body), + } +} + +/// Character references in text decoded: named (the HTML 4 set and more), decimal and hex. +/// Anything that is no reference stays as it is. A legacy name decodes without its `;` +/// (`© 2024`), as in browsers. +pub fn unescape(text: &str) -> String { + unescape_in(text, false) +} + +/// [`unescape`] for an attribute value: a legacy name without its `;` stays text when a letter, +/// a digit or `=` follows, so `?a=1©=2` is a query string and not a copyright sign. +pub fn unescape_attr(text: &str) -> String { + unescape_in(text, true) +} + +fn unescape_in(text: &str, attr: bool) -> String { + if !text.contains('&') { + return text.to_string(); + } + let mut out = String::with_capacity(text.len()); + let mut rest = text; + while let Some(i) = rest.find('&') { + out.push_str(rest.get(..i).unwrap_or("")); + let after = rest.get(i + 1..).unwrap_or(""); + match reference(after, attr) { + Some((c, used)) => { + out.push(c); + rest = after.get(used..).unwrap_or(""); + } + None => { + out.push('&'); + rest = after; + } + } + } + out.push_str(rest); + out +} + +/// The character a reference names and the bytes it takes, given the text after its `&` +/// (`attr`: inside an attribute value, see [`unescape_attr`]). At most [`MAX_NAME`] bytes are +/// looked at for a name, so a text of stray `&`s is read in linear time. +pub fn reference(s: &str, attr: bool) -> Option<(char, usize)> { + if let Some(num) = s.strip_prefix('#') { + let (digits, radix, skip) = match num.strip_prefix(['x', 'X']) { + Some(hex) => (hex, 16, 2), + None => (num, 10, 1), + }; + let len = digits.bytes().take_while(|b| char::from(*b).is_digit(radix)).count(); + if len == 0 { + return None; + } + // Too many digits is not a character; neither is 0, a surrogate, or anything past U+10FFFF. + let mut code: u32 = 0; + for d in digits.bytes().take(len) { + let d = char::from(d).to_digit(radix).unwrap_or(0); + code = code.checked_mul(radix).and_then(|c| c.checked_add(d)).unwrap_or(u32::MAX); + } + let semicolon = usize::from(digits.as_bytes().get(len) == Some(&b';')); + return Some((numeric(code), skip + len + semicolon)); + } + let len = s.bytes().take(MAX_NAME).take_while(u8::is_ascii_alphanumeric).count(); + if s.as_bytes().get(len) == Some(&b';') + && let Some(c) = s.get(..len).and_then(named) + { + return Some((c, len + 1)); + } + // Without its `;`: the longest legacy name (2 to 6 letters) the text starts with. + let len = (2..=len.min(6)).rev().find(|&l| s.get(..l).is_some_and(|p| BARE_NAMES.binary_search(&p).is_ok()))?; + if attr && s.as_bytes().get(len).is_some_and(|b| b.is_ascii_alphanumeric() || *b == b'=') { + return None; + } + s.get(..len).and_then(named).map(|c| (c, len)) +} + +fn numeric(code: u32) -> char { + let code = match code { + 0x80..=0x9F => C1.get((code - 0x80) as usize).copied().unwrap_or(code), + c => c, + }; + match char::from_u32(code) { + Some('\0') | None => char::REPLACEMENT_CHARACTER, + Some(c) => c, + } +} + +fn named(name: &str) -> Option { + if name.is_empty() { + return None; + } + let c = match name { + "amp" | "AMP" => '&', + "lt" | "LT" => '<', + "gt" | "GT" => '>', + "quot" | "QUOT" => '"', + "apos" | "APOS" => '\'', + "nbsp" | "NBSP" => '\u{00A0}', + "ensp" => '\u{2002}', + "emsp" => '\u{2003}', + "thinsp" => '\u{2009}', + "shy" => '\u{00AD}', + "copy" | "COPY" => '©', + "reg" | "REG" => '®', + "trade" | "TRADE" => '™', + "hellip" => '…', + "mdash" => '—', + "ndash" => '–', + "minus" => '−', + "lsquo" => '‘', + "rsquo" => '’', + "sbquo" => '‚', + "ldquo" => '“', + "rdquo" => '”', + "bdquo" => '„', + "laquo" => '«', + "raquo" => '»', + "bull" => '•', + "middot" => '·', + "deg" => '°', + "plusmn" => '±', + "times" => '×', + "divide" => '÷', + "frac12" => '½', + "frac14" => '¼', + "frac34" => '¾', + "frac13" => '⅓', + "frac23" => '⅔', + "frac18" => '⅛', + "frac38" => '⅜', + "frac58" => '⅝', + "frac78" => '⅞', + "sup1" => '¹', + "sup2" => '²', + "sup3" => '³', + "micro" => 'µ', + "permil" => '‰', + "prime" => '′', + "Prime" => '″', + "oline" => '‾', + "frasl" => '⁄', + "sect" => '§', + "para" => '¶', + "euro" | "EURO" => '€', + "pound" | "POUND" => '£', + "yen" | "YEN" => '¥', + "cent" | "CENT" => '¢', + "curren" => '¤', + "dagger" => '†', + "Dagger" => '‡', + "larr" => '←', + "rarr" => '→', + "uarr" => '↑', + "darr" => '↓', + "harr" => '↔', + "lArr" => '⇐', + "rArr" => '⇒', + "uArr" => '⇑', + "dArr" => '⇓', + "hArr" => '⇔', + "crarr" => '↵', + "forall" => '∀', + "part" => '∂', + "exist" => '∃', + "empty" => '∅', + "nabla" => '∇', + "isin" => '∈', + "notin" => '∉', + "ni" => '∋', + "prod" => '∏', + "sum" => '∑', + "radic" => '√', + "prop" => '∝', + "ang" => '∠', + "and" => '∧', + "or" => '∨', + "cap" => '∩', + "cup" => '∪', + "int" => '∫', + "there4" => '∴', + "sim" => '∼', + "cong" => '≅', + "asymp" | "approx" => '≈', + "ne" => '≠', + "equiv" => '≡', + "le" => '≤', + "ge" => '≥', + "sub" => '⊂', + "sup" => '⊃', + "nsub" => '⊄', + "sube" => '⊆', + "supe" => '⊇', + "oplus" => '⊕', + "otimes" => '⊗', + "perp" => '⊥', + "sdot" => '⋅', + "infin" => '∞', + "loz" => '◊', + "spades" => '♠', + "clubs" => '♣', + "hearts" => '♥', + "diams" => '♦', + "check" => '✓', + "iexcl" => '¡', + "iquest" => '¿', + "szlig" => 'ß', + "auml" => 'ä', + "ouml" => 'ö', + "uuml" => 'ü', + "Auml" => 'Ä', + "Ouml" => 'Ö', + "Uuml" => 'Ü', + "eacute" => 'é', + "egrave" => 'è', + "ecirc" => 'ê', + "euml" => 'ë', + "Eacute" => 'É', + "Egrave" => 'È', + "Ecirc" => 'Ê', + "Euml" => 'Ë', + "aacute" => 'á', + "agrave" => 'à', + "acirc" => 'â', + "atilde" => 'ã', + "Agrave" => 'À', + "Aacute" => 'Á', + "Acirc" => 'Â', + "Atilde" => 'Ã', + "iacute" => 'í', + "igrave" => 'ì', + "icirc" => 'î', + "iuml" => 'ï', + "Iacute" => 'Í', + "Igrave" => 'Ì', + "Icirc" => 'Î', + "Iuml" => 'Ï', + "oacute" => 'ó', + "ograve" => 'ò', + "ocirc" => 'ô', + "otilde" => 'õ', + "Oacute" => 'Ó', + "Ograve" => 'Ò', + "Ocirc" => 'Ô', + "Otilde" => 'Õ', + "uacute" => 'ú', + "ugrave" => 'ù', + "ucirc" => 'û', + "Uacute" => 'Ú', + "Ugrave" => 'Ù', + "Ucirc" => 'Û', + "yacute" => 'ý', + "yuml" => 'ÿ', + "Yacute" => 'Ý', + "ntilde" => 'ñ', + "Ntilde" => 'Ñ', + "ccedil" => 'ç', + "Ccedil" => 'Ç', + "aring" => 'å', + "Aring" => 'Å', + "oslash" => 'ø', + "Oslash" => 'Ø', + "aelig" => 'æ', + "AElig" => 'Æ', + "alpha" => 'α', + "beta" => 'β', + "gamma" => 'γ', + "delta" => 'δ', + "epsilon" => 'ε', + "zeta" => 'ζ', + "eta" => 'η', + "theta" => 'θ', + "iota" => 'ι', + "kappa" => 'κ', + "lambda" => 'λ', + "mu" => 'μ', + "nu" => 'ν', + "xi" => 'ξ', + "pi" => 'π', + "rho" => 'ρ', + "sigma" => 'σ', + "tau" => 'τ', + "upsilon" => 'υ', + "phi" => 'φ', + "chi" => 'χ', + "psi" => 'ψ', + "omega" => 'ω', + "Gamma" => 'Γ', + "Delta" => 'Δ', + "Theta" => 'Θ', + "Lambda" => 'Λ', + "Xi" => 'Ξ', + "Pi" => 'Π', + "Sigma" => 'Σ', + "Upsilon" => 'Υ', + "Phi" => 'Φ', + "Psi" => 'Ψ', + "Omega" => 'Ω', + "zwj" => '\u{200D}', + "zwnj" => '\u{200C}', + "cross" => '\u{2717}', + _ => { + let at = |table: &[&str], base: u32| table.iter().position(|n| *n == name).and_then(|i| char::from_u32(base + i as u32)); + return at(&LATIN1, 0xA0).or_else(|| at(&GREEK_UPPER, 0x391)).or_else(|| at(&GREEK_LOWER, 0x3B1)); + } + }; + Some(c) +} + +#[cfg(test)] +mod tests; diff --git a/crates/html/src/entity/tests.rs b/crates/html/src/entity/tests.rs new file mode 100644 index 000000000..3abd35fdc --- /dev/null +++ b/crates/html/src/entity/tests.rs @@ -0,0 +1,132 @@ +use super::{BARE_NAMES, GREEK_LOWER, GREEK_UPPER, LATIN1, decode_entity, escape, escape_dq, named, reference, unescape, unescape_attr}; + +#[test] +fn escapes_the_five() { + assert_eq!(escape(r#"Tom & 'Jerry'"#), "<a href="x">Tom & 'Jerry'</a>"); + assert_eq!(escape_dq(r#"Tom & 'Jerry'"#), "<a href="x">Tom & 'Jerry'</a>"); + assert_eq!(escape("plain ünïcode"), "plain ünïcode"); + assert_eq!(escape(""), ""); +} + +#[test] +fn bare_names_are_sorted_and_known() { + assert_eq!(BARE_NAMES.len(), 94); + assert!(BARE_NAMES.windows(2).all(|w| w[0] < w[1])); + assert!(BARE_NAMES.iter().all(|n| (2..=6).contains(&n.len()) && named(n).is_some())); +} + +#[test] +fn every_latin_1_and_greek_name_is_its_code_point() { + for (table, base) in [(&LATIN1[..], 0xA0), (&GREEK_UPPER[..], 0x391), (&GREEK_LOWER[..], 0x3B1)] { + for (i, name) in table.iter().enumerate().filter(|(_, n)| !n.is_empty()) { + assert_eq!(named(name), char::from_u32(base + i as u32), "{name}"); + } + } +} + +#[test] +fn named_references() { + assert_eq!(unescape("<b> & "x" 'y'"), " & \"x\" 'y'"); + assert_eq!(unescape("a b"), "a\u{a0}b"); + assert_eq!(unescape("© ® ™ … — –"), "© ® ™ … — –"); + assert_eq!(unescape("‘a’ “b” €5"), "‘a’ “b” €5"); + assert_eq!(unescape("café naïve Über ß ÿ"), "café naïve Über ß ÿ"); + assert_eq!(unescape("αβΩς → ≤ ≠"), "αβΩς → ≤ ≠"); + assert_eq!(unescape("‌¦ÐþΑο✗✓♦"), "\u{200c}¦Ðþ\u{391}ο✗✓♦"); + assert_eq!(unescape("⅓∴⇔◊"), "⅓∴⇔◊"); +} + +#[test] +fn names_are_case_sensitive() { + assert_eq!(unescape("Éé"), "Éé"); + assert_eq!(unescape("&EACUTE;"), "&EACUTE;"); + assert_eq!(unescape("&<"), "&<"); +} + +#[test] +fn numeric_references() { + assert_eq!(unescape("ééé"), "ééé"); + assert_eq!(unescape("😀"), "😀"); + assert_eq!(unescape("😀"), "😀"); + // Without the semicolon. + assert_eq!(unescape("A B."), "A B."); + // Windows-1252 for the C1 range. + assert_eq!(unescape("“hi” € ™"), "“hi” € ™"); +} + +#[test] +fn bad_numbers_become_the_replacement_character() { + assert_eq!(unescape("�"), "\u{fffd}"); + assert_eq!(unescape("�"), "\u{fffd}"); + assert_eq!(unescape("�"), "\u{fffd}"); + assert_eq!(unescape("�"), "\u{fffd}"); + assert_eq!(unescape("�"), "\u{fffd}"); +} + +#[test] +fn invalid_references_are_left_alone() { + for s in ["&", "&&", "& ", "&;", "&#;", "&#x;", "&#xZZ;", "&bogus;", "AT&T", "a & b", "x&y=z", "&#", "&#x", "&eacut", "ünï&", "&Alpha", "&hellip"] + { + assert_eq!(unescape(s), s, "{s}"); + assert_eq!(unescape_attr(s), s, "{s}"); + } +} + +#[test] +fn legacy_names_work_without_a_semicolon() { + assert_eq!(unescape("a < b > c"), "a < b > c"); + assert_eq!(unescape("x "), "x\u{a0}"); + assert_eq!(unescape("&"), "&"); + assert_eq!(unescape("© 2024 café!"), "© 2024 café!"); + // The longest name wins: `∉` is one reference; `¥x;` is `¥` and `x;`. + assert_eq!(unescape("∉ ¥x;"), "∉ ¥x;"); + // In text even inside a word, as browsers read it. + assert_eq!(unescape("&lify ?a=1&=2"), "&lify ?a=1&=2"); +} + +#[test] +fn in_an_attribute_a_bare_name_before_a_letter_digit_or_equals_is_text() { + assert_eq!(unescape_attr("?a=1&=2<=3©=4®1"), "?a=1&=2<=3©=4®1"); + assert_eq!(unescape_attr("&lify"), "&lify"); + assert_eq!(unescape_attr("?a=1&b=2"), "?a=1&b=2"); + assert_eq!(unescape_attr("a < b©"), "a < b©"); + // Numeric references need no `;` anywhere. + assert_eq!(unescape_attr("A=1"), "A=1"); +} + +#[test] +fn an_entity_body_decodes_whole_or_not_at_all() { + assert_eq!(decode_entity("amp"), Some('&')); + assert_eq!(decode_entity("#38"), Some('&')); + assert_eq!(decode_entity("#x26"), Some('&')); + assert_eq!(decode_entity("#150"), Some('–')); + for none in ["", "#", "#x", "#38x", "#3 8", "bogus", "amp;"] { + assert_eq!(decode_entity(none), None, "{none}"); + } + assert_eq!(reference("amp;rest", false), Some(('&', 4))); + assert_eq!(reference("#x41;", true), Some(('A', 5))); +} + +#[test] +fn a_long_run_of_ampersands_is_quick() { + let s = "&".repeat(200_000); + assert_eq!(unescape(&s), s); + let s = "&#".repeat(100_000); + assert_eq!(unescape(&s), s); + let s = "&aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".repeat(20_000); + assert_eq!(unescape(&s), s); +} + +proptest::proptest! { + #[test] + fn unescape_undoes_escape(s in proptest::prelude::any::()) { + proptest::prop_assert_eq!(unescape(&escape(&s)), s.clone()); + proptest::prop_assert_eq!(unescape_attr(&escape_dq(&s)), s); + } + + #[test] + fn unescape_never_panics(s in "[&#xX0-9a-zA-Z;= é]{0,60}") { + let _ = unescape(&s); + let _ = unescape_attr(&s); + } +} diff --git a/crates/html/src/lex.rs b/crates/html/src/lex.rs new file mode 100644 index 000000000..bf6688ffb --- /dev/null +++ b/crates/html/src/lex.rs @@ -0,0 +1,347 @@ +//! A tolerant HTML tokenizer: text, start tags and end tags. +//! +//! It follows the general syntax of the HTML Living Standard closely enough for documents, mail +//! and clipboard fragments, and reads anything: a `<` that starts no tag is text, a tag or +//! comment that never ends runs to the end of the input. Comments, doctypes and processing +//! instructions never leave it. + +use crate::entity::unescape_attr; + +/// What a [`Lexer`] caps. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Limits { + /// Most attributes kept per tag; the rest are read and dropped. Default 64. + pub max_attrs: usize, + /// Longest tag name kept, in bytes; a longer one is no real element and its name is empty. + /// Default 64. + pub max_tag_name: usize, +} + +impl Limits { + pub const DEFAULT: Limits = Limits { max_attrs: 64, max_tag_name: 64 }; +} + +impl Default for Limits { + fn default() -> Limits { + Limits::DEFAULT + } +} + +/// How a [`Lexer`] reads. Element names are lower case. +#[derive(Clone, Copy, Debug, Default)] +pub struct Options<'o> { + pub limits: Limits, + /// Elements that never leave the lexer: the start tag, the content (raw text, not markup) + /// and the end tag are skipped. For `script`, `style`, `title` and the like, when the caller + /// has no use for them. A caller that wants a `style`'s text leaves it out of this list and + /// calls [`Lexer::raw_content`] on its start tag. + pub drop: &'o [&'o str], + /// Elements whose content is text, not markup, and is given: after the start tag comes one + /// [`Token::Text`] with everything up to the end tag. For `textarea`, `xmp`, `plaintext`. + pub raw_text: &'o [&'o str], +} + +/// A start tag. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Tag<'a> { + /// Lower case; empty for a name longer than [`Limits::max_tag_name`]. + pub name: String, + /// Names and values as written, in the order written. + pub attrs: Vec<(&'a str, &'a str)>, + /// The tag ends in `/>`. + pub self_close: bool, +} + +impl Tag<'_> { + /// An attribute's value with character references decoded ([`unescape_attr`]); the first + /// of a name wins, as in browsers. Names match in any letter case. + pub fn attr(&self, name: &str) -> Option { + self.raw_attr(name).map(unescape_attr) + } + + /// An attribute's value as written, character references and all. + pub fn raw_attr(&self, name: &str) -> Option<&str> { + self.attrs.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, v)| *v) + } + + /// Whether the tag has the attribute, with or without a value. + pub fn has(&self, name: &str) -> bool { + self.attrs.iter().any(|(n, _)| n.eq_ignore_ascii_case(name)) + } + + /// Every attribute in the order written: the name as written, the value decoded. + pub fn attributes(&self) -> impl Iterator + '_ { + self.attrs.iter().map(|(n, v)| (*n, unescape_attr(v))) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Token<'a> { + /// Character data as written: references are not decoded ([`crate::unescape`]). + Text(&'a str), + Start(Tag<'a>), + /// An end tag's name, lower case (empty when longer than [`Limits::max_tag_name`]). + End(String), +} + +/// The tokenizer: an iterator of [`Token`]s over a string. +pub struct Lexer<'a, 'o> { + src: &'a str, + pos: usize, + opts: Options<'o>, + /// Set after the start tag of a raw-text element: its content comes next. + raw: Option, +} + +impl<'a> Lexer<'a, 'static> { + /// A lexer with [`Options::default`]: nothing dropped, no raw-text elements. + pub fn new(src: &'a str) -> Lexer<'a, 'static> { + Lexer::with(src, Options::default()) + } +} + +impl<'a, 'o> Lexer<'a, 'o> { + pub fn with(src: &'a str, opts: Options<'o>) -> Lexer<'a, 'o> { + Lexer { src, pos: 0, opts, raw: None } + } + + /// How far into the input the tokens given so far go, in bytes. + pub fn pos(&self) -> usize { + self.pos + } + + /// The raw content of element `name`, whose start tag was the last token: everything up to + /// its end tag (any letter case), or to the end of the input when it has none. The end tag + /// is consumed. For `style` and `script`, whose content is not markup. + pub fn raw_content(&mut self, name: &str) -> &'a str { + self.raw = None; + let b = self.bytes(); + let end = find_end_tag(b, self.pos, name).unwrap_or(b.len()); + let text = self.slice(self.pos, end); + self.pos = find_byte(b, end, b'>').map_or(b.len(), |i| i + 1); + text + } + + fn bytes(&self) -> &'a [u8] { + self.src.as_bytes() + } + + fn slice(&self, start: usize, end: usize) -> &'a str { + self.src.get(start..end).unwrap_or("") + } + + /// A tag name in lower case; empty when it is too long to be a real element. + fn tag_name(&self, raw: &str) -> String { + if raw.len() > self.opts.limits.max_tag_name { String::new() } else { raw.to_ascii_lowercase() } + } + + /// Whether the `<` at `i` starts a tag, a comment or a declaration (rather than being text). + fn markup_at(&self, i: usize) -> bool { + let b = self.bytes(); + match b.get(i + 1) { + Some(c) if c.is_ascii_alphabetic() => true, + Some(b'!' | b'?') => true, + Some(b'/') => matches!(b.get(i + 2), Some(c) if c.is_ascii_alphabetic() || *c == b'>'), + _ => false, + } + } + + /// The end of the text run that starts at `self.pos`: the next `<` that starts markup. + fn text_end(&self) -> usize { + let b = self.bytes(); + let mut i = self.pos + 1; + loop { + match find_byte(b, i, b'<') { + Some(j) if self.markup_at(j) => return j, + Some(j) => i = j + 1, + None => return b.len(), + } + } + } + + /// Skip a comment, doctype or processing instruction at `self.pos`. + fn skip_declaration(&mut self) { + let b = self.bytes(); + let end = if b.get(self.pos..self.pos + 4) == Some(b"").map_or(b.len(), |i| i + 3) + } + } else { + find_byte(b, self.pos, b'>').map_or(b.len(), |i| i + 1) + }; + self.pos = end.min(b.len()); + } + + /// Read the end tag at `self.pos` (``). + fn close_tag(&mut self) -> Option> { + let b = self.bytes(); + let start = self.pos + 2; + let mut i = start; + while let Some(c) = b.get(i) { + if c.is_ascii_whitespace() || *c == b'/' || *c == b'>' { + break; + } + i += 1; + } + let name = self.slice(start, i); + self.pos = find_byte(b, i, b'>').map_or(b.len(), |j| j + 1); + // `` is nothing at all. + (!name.is_empty()).then(|| Token::End(self.tag_name(name))) + } + + /// Read the start tag at `self.pos` (``). + fn open_tag(&mut self) -> Tag<'a> { + let b = self.bytes(); + let start = self.pos + 1; + let mut i = start; + while let Some(c) = b.get(i) { + if c.is_ascii_whitespace() || *c == b'/' || *c == b'>' { + break; + } + i += 1; + } + let name = self.tag_name(self.slice(start, i)); + let mut attrs = Vec::new(); + let mut self_close = false; + loop { + while matches!(b.get(i), Some(c) if c.is_ascii_whitespace() || *c == b'/') { + self_close = b.get(i) == Some(&b'/') && b.get(i + 1) == Some(&b'>'); + i += 1; + } + match b.get(i) { + None => break, + Some(b'>') => { + i += 1; + break; + } + Some(_) => {} + } + // An attribute name runs to white space, `/`, `>` or `=`; a leading `=` is part of it. + let name_start = i; + i += 1; + while let Some(c) = b.get(i) { + if c.is_ascii_whitespace() || matches!(*c, b'/' | b'>' | b'=') { + break; + } + i += 1; + } + let attr = self.slice(name_start, i); + let (value, next) = self.attr_value(i); + i = next; + if attrs.len() < self.opts.limits.max_attrs { + attrs.push((attr, value)); + } + } + self.pos = i.min(b.len()); + Tag { name, attrs, self_close } + } + + /// The value after an attribute name that ends at `i` (empty when it has none), and where + /// reading goes on. + fn attr_value(&self, i: usize) -> (&'a str, usize) { + let b = self.bytes(); + let mut j = i; + while matches!(b.get(j), Some(c) if c.is_ascii_whitespace()) { + j += 1; + } + if b.get(j) != Some(&b'=') { + return ("", j); + } + j += 1; + while matches!(b.get(j), Some(c) if c.is_ascii_whitespace()) { + j += 1; + } + match b.get(j) { + Some(&q) if q == b'"' || q == b'\'' => { + let end = find_byte(b, j + 1, q).unwrap_or(b.len()); + (self.slice(j + 1, end), (end + 1).min(b.len())) + } + _ => { + let mut end = j; + while let Some(c) = b.get(end) { + if c.is_ascii_whitespace() || *c == b'>' { + break; + } + end += 1; + } + (self.slice(j, end), end) + } + } + } +} + +impl<'a> Iterator for Lexer<'a, '_> { + type Item = Token<'a>; + + fn next(&mut self) -> Option> { + loop { + let b = self.bytes(); + if self.pos >= b.len() { + return None; + } + if let Some(name) = self.raw.take() { + let end = find_end_tag(b, self.pos, &name).unwrap_or(b.len()); + let text = self.slice(self.pos, end); + self.pos = end; + if !text.is_empty() { + return Some(Token::Text(text)); + } + continue; + } + if b.get(self.pos) != Some(&b'<') || !self.markup_at(self.pos) { + let start = self.pos; + self.pos = self.text_end(); + return Some(Token::Text(self.slice(start, self.pos))); + } + match b.get(self.pos + 1) { + Some(b'!' | b'?') => self.skip_declaration(), + Some(b'/') => { + if let Some(token) = self.close_tag() { + return Some(token); + } + } + _ => { + let tag = self.open_tag(); + if self.opts.drop.contains(&tag.name.as_str()) { + self.raw_content(&tag.name); + continue; + } + if self.opts.raw_text.contains(&tag.name.as_str()) { + self.raw = Some(tag.name.clone()); + } + return Some(Token::Start(tag)); + } + } + } + } +} + +fn find_byte(b: &[u8], from: usize, byte: u8) -> Option { + b.get(from..)?.iter().position(|c| *c == byte).map(|i| i + from) +} + +fn find_sub(b: &[u8], from: usize, needle: &[u8]) -> Option { + b.get(from..)?.windows(needle.len()).position(|w| w == needle).map(|i| i + from) +} + +/// Where `` or the end), at or after `from`. +fn find_end_tag(b: &[u8], from: usize, name: &str) -> Option { + let mut i = from; + while let Some(lt) = find_byte(b, i, b'<') { + let after = lt + 2 + name.len(); + let named = b.get(lt + 1) == Some(&b'/') && b.get(lt + 2..after).is_some_and(|n| n.eq_ignore_ascii_case(name.as_bytes())); + if named && b.get(after).is_none_or(|c| c.is_ascii_whitespace() || matches!(*c, b'/' | b'>')) { + return Some(lt); + } + i = lt + 1; + } + None +} + +#[cfg(test)] +mod tests; diff --git a/crates/html/src/lex/tests.rs b/crates/html/src/lex/tests.rs new file mode 100644 index 000000000..4f19e5b7c --- /dev/null +++ b/crates/html/src/lex/tests.rs @@ -0,0 +1,177 @@ +use super::{Lexer, Limits, Options, Token}; + +/// As a mail reader sets it up. +const MAIL: Options<'static> = + Options { limits: Limits { max_attrs: 32, max_tag_name: 24 }, drop: &["script", "style", "title", "iframe"], raw_text: &["textarea", "xmp"] }; + +fn show<'a>(lexer: impl Iterator>) -> Vec { + lexer + .map(|t| match t { + Token::Text(t) => format!("T:{t}"), + Token::Start(tag) => { + let attrs: Vec = tag.attrs.iter().map(|(n, v)| format!("{n}={v}")).collect(); + if attrs.is_empty() { format!("<{}>", tag.name) } else { format!("<{} {}>", tag.name, attrs.join(" ")) } + } + Token::End(n) => format!(""), + }) + .collect() +} + +fn tokens(html: &str) -> Vec { + show(Lexer::with(html, MAIL)) +} + +#[test] +fn text_and_tags() { + assert_eq!(tokens("Hi there!"), ["T:Hi ", "", "T:there", "", "T:!"]); + assert_eq!(tokens(""), Vec::::new()); + assert_eq!(tokens("plain"), ["T:plain"]); + assert_eq!(tokens("

"), ["
", "
"]); +} + +#[test] +fn attributes_in_every_quoting_style() { + assert_eq!(tokens(r#""#), [r#""#]); + assert_eq!(tokens("t"), ["", "T:t"]); + assert_eq!(tokens(""), [""]); + assert_eq!(tokens("

"), ["

"]); + // A `>` inside a quoted value does not end the tag. + assert_eq!(tokens(r#"t"#), ["b>", "T:t", ""]); +} + +#[test] +fn attribute_values_are_decoded_on_demand() { + let mut t = Lexer::new(r#""#); + let Some(Token::Start(tag)) = t.next() else { panic!("no tag") }; + assert_eq!(tag.attr("href").as_deref(), Some("?a=1&b=2©=3")); + assert_eq!(tag.raw_attr("href"), Some("?a=1&b=2©=3")); + assert_eq!(tag.attr("missing"), None); + assert!(tag.has("checked") && !tag.has("check")); + assert_eq!(tag.attributes().map(|(n, _)| n).collect::>(), ["HREF", "href", "Checked"]); +} + +#[test] +fn a_tag_that_closes_itself_says_so() { + let closes = |html: &str| match Lexer::new(html).next() { + Some(Token::Start(tag)) => tag.self_close, + other => panic!("{other:?}"), + }; + for yes in ["
", "
", "", "", ""] { + assert!(closes(yes), "{yes}"); + } + // A `/` that ends an unquoted value, or stands anywhere but before the `>`, is not it. + for no in ["
", "", "
", "", "
d"), ["T:a < b and c > d"]); + assert_eq!(tokens("1<2<3"), ["T:1<2<3"]); + assert_eq!(tokens("<"), ["T:<"]); + assert_eq!(tokens("x<"), ["T:x<"]); + assert_eq!(tokens("<3 <- b"), ["T:a", "T:b"]); +} + +#[test] +fn comments_and_declarations_vanish() { + assert_eq!(tokens("ab"), ["T:a", "T:b"]); + assert_eq!(tokens("

x"), ["

", "T:x"]); + assert_eq!(tokens("x"), ["T:x"]); + assert_eq!(tokens("abc"), ["T:a", "T:b", "T:c"]); + assert_eq!(tokens("shown"), ["T:shown"]); + assert_eq!(tokens("shown"), ["T:shown"]); + // An unfinished comment swallows the rest. + assert_eq!(tokens("a after a write and parse; the writer now adds one amp; level and the reader removes exactly one. * craft-captions: a WebVTT speaker name with > no longer spills into the cue text Ported from storytold/filmcraft#4470. The writer puts the lookalike U+203A in place of > inside the voice tag. Also from that pull request's review: a line break in a name is written as a space, and a name ending in - is kept apart from the tag's closing >. * craft-captions: an SRT text line that looks like a timing line survives a round trip Ported from storytold/filmcraft#4448. The writer marks such a line with a leading zero-width space and the reader removes it, so the cue is not split. * craft-captions: back-to-back SCC captions with the same text stay separate cues Ported from storytold/filmcraft#3808. Every End of Caption now closes the open cue. MCC's CEA-608 path uses the same decoder. * craft-captions: an SCC pop-on load may run across the previous caption's erase Ported from storytold/filmcraft#1837 (filmcraft#1791). A caption that closely follows another popped on about a second late, because its load had to be one unbroken run of free frames. Doubled codes stay adjacent; pauses are allowed between them. * craft-captions: CEA-708 export starts each caption at its window's top left Ported from storytold/filmcraft#4043 (filmcraft#4042). Each window load now sends DeleteWindows before DefineWindow in place of the ClearWindows after it: a window made anew is empty with the pen at its top left in any decoder, for the same number of bytes, so cue timing is unchanged. * craft-captions: TTML/DFXP export keeps literal < and > text Ported from storytold/filmcraft#2830. The writer treated every < with a later > as a tag and wrote nothing for unknown names, so "1 < 2 and 3 > 2" lost its middle. Only a well-formed tag is markup now; a > inside a quoted attribute value does not end the tag. * craft-captions: TTML/DFXP export keeps cue IDs unique and valid Unicode IDs as written Ported from the TTML commit of storytold/filmcraft#1839. A cue named s1 could collide with the style ID, and a generated c1 could take the ID of a later cue. Valid explicit IDs are reserved first; missing, invalid or repeated ones get a replacement that is in no one's way. IDs are checked as XML NCNames. * craft-captions: compare the craft-xml and roxmltree views of TTML documents The TTML reader moved from roxmltree to craft-xml. This dev-only test checks that both hand it the same elements, attributes and text, by namespace, on TTML documents and on the writer's output. roxmltree becomes a dev-dependency, which the cue ID tests use too. * craft-captions: use craft-timeline's exact time instead of a private copy craft-captions carried a 290-line subset of the tick, frame rate and timecode field code because craft-libs had none when it was added. craft-timeline now has the whole module with the same tick unit. craft_captions::time is a re-export of craft_timeline::time, so every public path still resolves and the types are now the same ones craft-timeline's callers hold. * craft-captions: SCC and MCC time codes count from the document's start timecode Ported from the caption-format part of storytold/filmcraft#4131 (filmcraft#4130). Document::start_timecode is the label of time zero; the SCC and MCC writers count from the 29.97 frame with the same label, so the label carries across rates. Readers note the rate and drop-frame mode of a file's labels in Document::labels, and Document::count_from counts cues back from a start time code. charset_warnings names the time codes the file has, which that pull request's review found missing. --------- Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Co-authored-by: SeeRay11 Co-authored-by: jnohclee-rgb <336857373+jnohclee-rgb@users.noreply.github.com> Co-authored-by: JonRC Co-authored-by: sanyamsingh001 <314051937+sanyamsingh001@users.noreply.github.com> --- crates/captions/README.md | 7 +++- crates/captions/src/lib.rs | 74 +++++++++++++++++++++++++++++++++++++- crates/captions/src/mcc.rs | 34 ++++++++++++++++-- crates/captions/src/scc.rs | 36 +++++++++++++++++-- 4 files changed, 145 insertions(+), 6 deletions(-) diff --git a/crates/captions/README.md b/crates/captions/README.md index e91972554..60fed235b 100644 --- a/crates/captions/README.md +++ b/crates/captions/README.md @@ -58,7 +58,12 @@ lists at most 20 characters per stream. All cue times are exact `Tick`s. Milliseconds convert exactly (1 ms = 254 016 000 ticks); SCC frames map through `FrameRate::FPS_29_97`. `Document::snap_to_frames` snaps cues to a frame grid and -removes overlaps. +removes overlaps. SCC and MCC time codes count from `Document::start_timecode`, a time code +label read as a time (`label_time`), so the label carries across rates: 01:00:00:00 at 25 fps is +written as 01:00:00;00 drop-frame or 01:00:00:00 non-drop (`label_frame`). Readers leave it at +zero and keep the file's own time codes, noting the rate and drop-frame mode of its labels in +`Document::labels`; `Document::count_from` then counts the cues back from a start time code, when +every cue is at or after it. ### TTML tick timing diff --git a/crates/captions/src/lib.rs b/crates/captions/src/lib.rs index db543dbad..752976f11 100644 --- a/crates/captions/src/lib.rs +++ b/crates/captions/src/lib.rs @@ -149,12 +149,55 @@ pub struct Document { pub blocks: Vec, /// Non-fatal problems found while reading (skipped cues…). pub warnings: Vec, + /// SCC / MCC: the time code label of time zero, read as a time ([`label_time`]: a sequence's + /// start timecode on export). The writers count their time codes from the label it reads as, + /// so 01:00:00:00 at 25 fps is 01:00:00;00 or 01:00:00:00 at 29.97. Readers leave it at zero. + pub start_timecode: Tick, + /// SCC / MCC read from a file: the frame rate of its time code labels and whether they are + /// drop-frame. [`Document::count_from`] needs it. + pub labels: Option<(FrameRate, bool)>, /// BCP 47 language tag of the text, when known: TTML's `xml:lang` on import, the caption /// track's language on export. TTML and DFXP write it (else `en`). pub language: Option, } +/// Frame `frame` of a `rate` time code (drop-frame or not) as its label read as a time: hours, +/// minutes, seconds and the frame's share of a second at the label's nominal rate (25 for 25 fps, +/// 30 for 29.97). Negative frames read as zero. +pub fn label_time(frame: i64, rate: FrameRate, drop_frame: bool) -> Tick { + let base = rate.timecode_base(); + let (_, h, m, s, f) = crate::time::frames_to_fields(frame.max(0), rate, drop_frame && rate.supports_drop_frame()); + let seconds = h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(s); + Tick(TICKS_PER_SECOND.saturating_mul(seconds)) + FrameRate::new(base, 1).tick_of(f) +} + +/// The frame of a `rate` time code (drop-frame or not) whose label reads as `label` (as +/// [`label_time`] gives it), to the nearest frame of the label's nominal rate. +pub fn label_frame(label: Tick, rate: FrameRate, drop_frame: bool) -> i64 { + let nominal = FrameRate::new(rate.timecode_base(), 1); + let n = nominal.frame_at(nominal.snap_nearest(label.max(Tick::ZERO))); + let (_, h, m, s, f) = crate::time::frames_to_fields(n, nominal, false); + crate::time::fields_to_frames(h, m, s, f, rate, drop_frame && rate.supports_drop_frame()) +} + impl Document { + /// SCC / MCC read from a file: count cue times from the time code labelled `start` (as + /// [`Document::start_timecode`]) rather than from 00:00:00:00, the inverse of what the writers + /// do. Only when every cue is at or after it, so a file that counts from zero keeps its times. + /// Returns whether the cues moved. + pub fn count_from(&mut self, start: Tick) -> bool { + let Some((rate, drop_frame)) = self.labels else { return false }; + let origin = rate.tick_of(label_frame(start, rate, drop_frame)); + if origin <= Tick::ZERO || self.cues.iter().any(|c| c.start < origin) { + return false; + } + for c in &mut self.cues { + c.start -= origin; + c.end -= origin; + } + true + } + /// Snap cue times to frame boundaries of `rate` (nearest), keeping every cue at least one frame /// long and removing overlaps (a cue is cut where the next one starts). pub fn snap_to_frames(&mut self, rate: FrameRate) { @@ -332,7 +375,8 @@ fn lossy_chars(doc: &Document, stream: &str, shown_as: impl Fn(char) -> Option String { mod tests { use super::*; + /// A start time code keeps its label from a sequence's rate to the SCC / MCC 29.97 grid. + #[test] + fn start_timecode_labels_carry_across_rates() { + let secs = |s: i64| Tick(TICKS_PER_SECOND * s); + let scc = FrameRate::FPS_29_97; + // 01:00:00:00 at 25, 24 and 29.97 drop-frame all read as one hour + assert_eq!(label_time(90_000, FrameRate::FPS_25, false), secs(3600)); + assert_eq!(label_time(86_400, FrameRate::FPS_23_976, false), secs(3600)); + assert_eq!(label_time(107_892, scc, true), secs(3600)); + assert_eq!(label_time(108_000, scc, false), secs(3600)); + // 01:00:02:00 at 25 fps is 01:00:02;00 drop-frame and 01:00:02:00 non-drop at 29.97 + let two = label_time(90_050, FrameRate::FPS_25, false); + assert_eq!(crate::time::format_timecode_frames(label_frame(two, scc, true), scc, true), "01;00;02;00"); + assert_eq!(crate::time::format_timecode_frames(label_frame(two, scc, false), scc, false), "01:00:02:00"); + // a frame's share of a second goes to the nearest 29.97 label frame + assert_eq!(label_frame(label_time(90_001, FrameRate::FPS_25, false), scc, false), 108_001); + // drop-frame on a rate that has none counts non-drop + assert_eq!(label_time(90_000, FrameRate::FPS_25, true), secs(3600)); + // hostile values neither panic nor go negative + assert_eq!(label_time(-5, FrameRate::FPS_25, false), Tick::ZERO); + assert!(label_time(i64::MAX, FrameRate::FPS_25, false) > Tick::ZERO); + assert_eq!(label_frame(Tick(i64::MIN), scc, true), 0); + assert!(label_frame(Tick(i64::MAX), scc, true) > 0); + } + #[test] fn clock() { assert_eq!(parse_clock("00:00:01,500"), Some(Tick(TICKS_PER_SECOND * 3 / 2))); @@ -467,6 +536,9 @@ mod tests { // CEA-708 carries … (G2) and ’ but not the em dash, which 608 does carry let em_dash = "CEA-708 has no \"—\" (U+2014): written as \"?\" once, first in the caption at 00:00:00;00"; assert_eq!(charset_warnings(&doc, Format::Mcc, opts), [dropped, replaced, em_dash]); + // a document that starts at 01:00:00:00 names the time codes its file has + let master = Document { start_timecode: Tick(3600 * TICKS_PER_SECOND), ..doc.clone() }; + assert!(charset_warnings(&master, Format::Scc, opts)[0].ends_with("first in the caption at 01:00:00;00")); let ndf = charset_warnings(&doc, Format::Scc, WriteOptions { drop_frame: false, ..opts }); assert!(ndf[0].ends_with("00:00:00:00"), "{ndf:?}"); for f in [Format::Srt, Format::WebVtt, Format::Ttml, Format::Dfxp] { diff --git a/crates/captions/src/mcc.rs b/crates/captions/src/mcc.rs index 7169a4ec7..7b13ac3c2 100644 --- a/crates/captions/src/mcc.rs +++ b/crates/captions/src/mcc.rs @@ -603,6 +603,7 @@ pub fn write_with(doc: &Document, drop_frame: bool, cea608: bool, cea708: bool, let program = header_value(program); let f1 = if cea608 { scc::schedule(doc) } else { BTreeMap::new() }; let dtv = if cea708 { schedule_708(doc) } else { BTreeMap::new() }; + let origin = crate::label_frame(doc.start_timecode, RATE, drop_frame); // time codes count from the start time code let mut out = String::from("File Format=MacCaption_MCC V1.0\n\n"); out.push_str("///////////////////////////////////////////////////////////////////////////////////\n"); out.push_str(&format!("// Closed captions written by {program}: SMPTE ST 334-2 caption distribution packets\n")); @@ -633,7 +634,7 @@ pub fn write_with(doc: &Document, drop_frame: bool, cea608: bool, cea708: bool, f1: f1.next_if(|(frame, _)| *frame == f).map(|(_, pair)| pair), dtvcc: dtv.next_if(|(frame, _)| *frame == f).map(|(_, pairs)| pairs).unwrap_or_default(), }; - let (_, hh, mm, ss, ff) = frames_to_fields(f, RATE, drop_frame); + let (_, hh, mm, ss, ff) = frames_to_fields(f.saturating_add(origin), RATE, drop_frame); let sep = if drop_frame { ';' } else { ':' }; out.push_str(&format!("{hh:02}:{mm:02}:{ss:02}{sep}{ff:02}\t{}\n", compress(&build_cdp(seq, &data)))); seq = seq.wrapping_add(1); @@ -685,7 +686,9 @@ pub fn parse(text: &str) -> Result { let Some([h, m, s, f]) = parts.as_deref() else { return Err(Error::Syntax { line: n + 1, msg: format!("bad timecode \"{tc}\"") }); }; - let frame = fields_to_frames(*h, *m, *s, *f, rate.0, rate.1 || tc.contains(';')); + let drop_frame = rate.1 || tc.contains(';'); + doc.labels.get_or_insert((rate.0, drop_frame)); + let frame = fields_to_frames(*h, *m, *s, *f, rate.0, drop_frame); let Some(bytes) = expand(data) else { doc.warnings.push(format!("line {}: bad caption data", n + 1)); continue; @@ -839,6 +842,33 @@ mod tests { } } + /// Time codes count from the label of the document's start time code, drop-frame or not; the + /// packets do not change, and `Document::count_from` reads the cues back from it. + #[test] + fn time_codes_count_from_the_start_timecode() { + let doc = Document { cues: vec![cue(60, 120, "Caption")], ..Default::default() }; + let hour = crate::time::Tick(3600 * crate::time::TICKS_PER_SECOND); // the label 01:00:00:00 + let master = Document { start_timecode: hour, ..doc.clone() }; + let lines = |text: &str| -> Vec<(i64, String)> { + text.lines().filter_map(|l| l.split_once('\t')).map(|(tc, data)| (scc::parse_timecode(tc).unwrap(), data.to_string())).collect() + }; + for (drop_frame, origin) in [(true, 107_892), (false, 108_000)] { + let plain = lines(&write(&doc, drop_frame)); + let shifted: Vec<_> = plain.iter().map(|(f, data)| (f + origin, data.clone())).collect(); + let text = write(&master, drop_frame); + assert_eq!(lines(&text), shifted); + assert!(text.contains("\n01:00:0"), "{text}"); + let mut back = parse(&text).unwrap(); + assert_eq!(back.labels, Some((RATE, drop_frame))); + assert_eq!(back.cues[0].start, RATE.tick_of(origin + 60)); + assert!(back.count_from(hour)); + assert_eq!(back.cues, parse(&write(&doc, drop_frame)).unwrap().cues, "read back from the start time code"); + } + let negative = Document { start_timecode: crate::time::Tick(-1), ..doc.clone() }; + assert_eq!(lines(&write(&negative, true)), lines(&write(&doc, true)), "a negative start counts from zero"); + assert!(write(&Document { start_timecode: crate::time::Tick(i64::MAX), ..doc }, true).contains('\t')); + } + /// Bound output for distant or long cues near the tick limit without changing their timing. #[test] fn distant_and_long_cues_round_trip_near_tick_limit() { diff --git a/crates/captions/src/scc.rs b/crates/captions/src/scc.rs index e0000cff9..d9ca1deed 100644 --- a/crates/captions/src/scc.rs +++ b/crates/captions/src/scc.rs @@ -292,6 +292,8 @@ pub fn parse(text: &str) -> Result { let Some(frame) = parse_timecode(tc) else { return Err(Error::Syntax { line: n + 1, msg: format!("bad timecode \"{tc}\"") }); }; + // the first time code says how the file counts (drop-frame or not) + doc.labels.get_or_insert((RATE, tc.contains([';', '.', ',']))); for (k, w) in it.enumerate() { let Ok(v) = u16::from_str_radix(w, 16) else { doc.warnings.push(format!("line {}: bad code word \"{w}\"", n + 1)); @@ -498,6 +500,7 @@ pub fn code_word(p: (u8, u8)) -> u16 { pub fn write(doc: &Document, drop_frame: bool) -> String { let occ = schedule(doc); + let origin = crate::label_frame(doc.start_timecode, RATE, drop_frame); // time codes count from the start time code let mut out = String::from(HEADER); out.push_str("\n\n"); let mut line: Option<(i64, Vec)> = None; @@ -506,13 +509,13 @@ pub fn write(doc: &Document, drop_frame: bool) -> String { if f != last + 1 && let Some((start, words)) = line.take() { - out.push_str(&format!("{}\t{}\n\n", format_timecode(start, drop_frame), words.join(" "))); + out.push_str(&format!("{}\t{}\n\n", format_timecode(start.saturating_add(origin), drop_frame), words.join(" "))); } line.get_or_insert_with(|| (f, Vec::new())).1.push(format!("{:04x}", word(p))); last = f; } if let Some((start, words)) = line { - out.push_str(&format!("{}\t{}\n\n", format_timecode(start, drop_frame), words.join(" "))); + out.push_str(&format!("{}\t{}\n\n", format_timecode(start.saturating_add(origin), drop_frame), words.join(" "))); } out } @@ -554,6 +557,35 @@ mod tests { assert_eq!(parse_timecode("00:10:00;00"), Some(17982)); } + /// Time codes count from the label of the document's start time code, drop-frame or not; the + /// code words do not change, and `Document::count_from` reads the cues back from it. + #[test] + fn time_codes_count_from_the_start_timecode() { + let doc = Document { cues: vec![cue(60, 120, "Hello")], ..Default::default() }; + let hour = Tick(3600 * crate::time::TICKS_PER_SECOND); // the label 01:00:00:00 + let master = Document { start_timecode: hour, ..doc.clone() }; + let lines = |text: &str| -> Vec<(i64, String)> { + text.lines().filter_map(|l| l.split_once('\t')).map(|(tc, words)| (parse_timecode(tc).unwrap(), words.to_string())).collect() + }; + for (drop_frame, origin) in [(true, 107_892), (false, 108_000)] { + let plain = lines(&write(&doc, drop_frame)); + let shifted: Vec<_> = plain.iter().map(|(f, words)| (f + origin, words.clone())).collect(); + let text = write(&master, drop_frame); + assert_eq!(lines(&text), shifted); + assert!(text.contains("\n01:00:0"), "{text}"); + let mut back = parse(&text).unwrap(); + assert_eq!(back.labels, Some((RATE, drop_frame))); + assert_eq!(back.cues[0].start, RATE.tick_of(origin + 60)); + assert!(back.count_from(hour)); + assert_eq!(back.cues, parse(&write(&doc, drop_frame)).unwrap().cues, "read back from the start time code"); + assert!(!back.count_from(hour), "a cue before the start time code keeps its time"); + } + for t in [Tick(i64::MIN), Tick(-1)] { + assert_eq!(write(&Document { start_timecode: t, ..doc.clone() }, true), write(&doc, true), "a negative start counts from zero"); + } + assert!(write(&Document { start_timecode: Tick(i64::MAX), ..doc }, true).contains('\t')); + } + #[test] fn decode_known_popon() { // "HELLO" on row 15 at 00:00:01;00, cleared at 00:00:03;00 From 4a7024cd69dcc20c451fd63a0d5a07604958b52b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:01:12 -0500 Subject: [PATCH 038/120] craft-aep: flat read-only tree index and the Pard decoder (ports of effectcraft#3302 and #3319) (#948) * craft-aep: a flat read-only tree index, borrowed text documents, fewer allocations The container part of storytold/effectcraft#3302: - `TreeIndex`: the chunk tree as one flat array in depth-first order, borrowed from the input, for readers that only look. Same chunks counted against the same limits and the same errors as `parse`. An `.aepx` is indexed by `index_aepx` into one buffer. The typed view reads through either tree (`Node`, `Root`, `Project::from_index`). - Chunk parser: each list of siblings is counted, then reserved once. `.aepx`: hex decoded into an output sized once, attribute values without an entity borrowed. `write` reserves the exact output size. - Text documents are parsed into a tree that borrows from the body; the style and paragraph defaults are built once. - Layer markers and Source Text are read where they are stored instead of searched for. - Container accessors that only tests used are removed. What the importer and exporter in EffectCraft reach across the crate boundary is public here: `Root`, `Draft`, `AepxDrafts`, `index_aepx`, `TreeIndex::from_drafts`, `Project::of`, `ProjectSettings::of`, `Node::marker_list`, `Root::file_version` and `saved_by_version`, `StyleRun::SAMPLED`. `TimeBase::NTSC` holds the rates as plain numerator and denominator instead of an EffectCraft type. * craft-aep: one decoder for an effect parameter definition (`Pard`) The container part of storytold/effectcraft#3319: the fields of a `pard` record (type code, name, default, valid range, slider range, popup item count) are read by one type, `Pard`, which `EffectParam` now reads through. It is public so that an exporter's tests can read back what they encode instead of keeping a second decoder. The rest of that PR (import and export bounds, loss reports) is in EffectCraft's importer and exporter. --------- Co-authored-by: vangel76 --- crates/aep/README.md | 7 + crates/aep/docs/aep-format.md | 38 +- crates/aep/src/aepx.rs | 782 ++++++++++-- crates/aep/src/aepx/tree.rs | 362 ++++++ crates/aep/src/arbitrary.rs | 14 +- crates/aep/src/camera.rs | 89 -- crates/aep/src/effects.rs | 203 ++- crates/aep/src/footage.rs | 39 +- crates/aep/src/header.rs | 81 -- crates/aep/src/index.rs | 1026 ++++++++++++++++ crates/aep/src/keys.rs | 41 +- crates/aep/src/lib.rs | 8 +- crates/aep/src/masks.rs | 24 +- crates/aep/src/read.rs | 164 ++- crates/aep/src/settings.rs | 35 +- crates/aep/src/shapes.rs | 13 +- crates/aep/src/textdoc.rs | 1090 ++++++++++++++--- crates/aep/src/typed.rs | 126 +- crates/aep/src/values.rs | 8 +- crates/aep/src/version.rs | 34 +- crates/aep/src/write.rs | 93 ++ crates/aep/tests/it/common/mod.rs | 7 + crates/aep/tests/it/container.rs | 1 + crates/aep/tests/it/container/batch3_core.rs | 34 +- .../aep/tests/it/container/batch3_objects.rs | 101 +- crates/aep/tests/it/container/cases_600.rs | 4 +- crates/aep/tests/it/container/header.rs | 57 +- crates/aep/tests/it/container/helpers.rs | 3 +- crates/aep/tests/it/container/keyframes.rs | 11 +- .../aep/tests/it/container/layer_switches.rs | 47 +- crates/aep/tests/it/container/matte.rs | 2 +- crates/aep/tests/it/container/robustness.rs | 87 +- .../it/container/text_markers_footage.rs | 2 - crates/aep/tests/it/container/tree_index.rs | 231 ++++ crates/aep/tests/it/container/typed.rs | 8 +- crates/aep/tests/it/container/values.rs | 9 +- 36 files changed, 3960 insertions(+), 921 deletions(-) create mode 100644 crates/aep/src/aepx/tree.rs create mode 100644 crates/aep/src/index.rs create mode 100644 crates/aep/tests/it/container/tree_index.rs diff --git a/crates/aep/README.md b/crates/aep/README.md index 12ce8f9d6..95fcdd1a6 100644 --- a/crates/aep/README.md +++ b/crates/aep/README.md @@ -24,6 +24,13 @@ The XML form of a project (`.aepx`) is read into the same tree by `read_aepx` (a XML tokenizer of its own); `parse_any` picks the reader by content, so the typed view takes either form. The mapping is in [`docs/aep-format.md`](docs/aep-format.md), section 1. +A reader that only looks does not need the owned tree. `TreeIndex` is the same tree as one flat, +read-only array in depth-first order, borrowed from the input: `TreeIndex::parse` for an `.aep`, +`index_aepx` and `TreeIndex::from_drafts` for an `.aepx` (decoded into one buffer first). It +counts the same chunks against the same `Limits` and returns the same errors as `parse`, and the +typed view reads through either (`Project::new` for the owned tree, `Project::from_index` for the +index). An importer should use the index; it is about twice as fast and allocates far less. + Bodies that look like lists but are not chunk sequences (for example `LIST btdk`, raw text) stay opaque rather than failing the parse. Input is untrusted: sizes are checked against the remaining input, nesting and chunk count are bounded (`Limits`), nothing is allocated from a declared size, diff --git a/crates/aep/docs/aep-format.md b/crates/aep/docs/aep-format.md index a9269bc10..3582276d7 100644 --- a/crates/aep/docs/aep-format.md +++ b/crates/aep/docs/aep-format.md @@ -121,6 +121,11 @@ counterpart (a name that is not 1 to 4 characters) are skipped with their conten with the byte offset. It runs without recursion; the nesting depth (`Limits::max_depth`) and the element count (`Limits::max_chunks`) are bounded, and everything it allocates is bounded by the size of the input. +- There are two readers. `read_aepx` builds the owned tree element by element. The importer, which + only reads, uses a second reader that decodes every body (hex, text, the alias JSON, the + numbers) into one buffer and records the chunks as a flat depth-first list whose bodies are + ranges of that buffer; it indexes that buffer (`TreeIndex`) as it indexes an `.aep`. The tests + hold both readers to the same tree and the same errors. - Tests: `tests/aepx.rs` compares the five fixture pairs chunk by chunk and byte by byte, compares the typed view of both forms, and runs truncated, bit-flipped, deeply nested and oversized inputs; `crates/aep/tests/it/import/aepx.rs` imports both forms of each pair and expects the same @@ -139,8 +144,8 @@ write on their own. |---|---| | `svap` (4 bytes, the first top-level chunk) is the version of the After Effects that **wrote** the file: `0f 11 0e 02` in all 36 (a down-saved copy keeps the 2026 word) and in all 1 192 other `.aep` files of the local corpus folders. After Effects opens a file without `svap` (`t-nosvap`). | All 36; corpus scan. | | `head` bytes 4 to 7 are the version the file is **written for**: `0f 11 0e 02` (= `svap`) in the 2026 saves, `0f 0b 06 61` in the 25.x copies, `0f 03 06 41` in the 24.x copies, the same word in every family. After Effects refuses a file whose word is zero ("created with an older version [0.0x0]", `u-zhead`). | All 36; `u-zhead`. | -| The word is packed (bit 31 first): bits 25-24 and 21-19 the major, 18-15 the minor, 14-11 the bug-fix, 10-8 a stage (6 in all), 7-0 the build; bits 31-26 (`000011`) and 23-22 (`00`) are the same in all samples and not decoded. `0f110e02` = **26.2.1x2**, which is what the saving After Effects reports for itself (`app.version` recorded by the corpus scripts) - this one sample fixes minor, bug-fix and build. The copies read as **25.6.0x97** and **24.6.0x65** (major **V** from three consecutive majors; minor, bug-fix and build of the copies **E**: one other word decides their positions). `effectcraft_aep::AeVersion`, `AepFile::file_version`, `AepFile::saved_by_version`. | Tests `decodes_the_sampled_words`, `the_container_of_a_copy_states_its_version_in_head_and_the_writer_in_svap`. | -| `head` bytes 0 to 1 (a `u16`) step with the major: 97 (`0x61`) in the 2026 saves, 96 in 25.x, 95 in 24.x; bytes 2 to 3 are 5, 9 and 6 (**?**). Bytes 8 to 15 differ between families, not between versions; bytes 16 to 19 are the save counter (one higher in each copy). Nothing is derived from bytes 0 to 3 (`AepFile::head_revision` exposes the `u16`). | All 36. | +| The word is packed (bit 31 first): bits 25-24 and 21-19 the major, 18-15 the minor, 14-11 the bug-fix, 10-8 a stage (6 in all), 7-0 the build; bits 31-26 (`000011`) and 23-22 (`00`) are the same in all samples and not decoded. `0f110e02` = **26.2.1x2**, which is what the saving After Effects reports for itself (`app.version` recorded by the corpus scripts) - this one sample fixes minor, bug-fix and build. The copies read as **25.6.0x97** and **24.6.0x65** (major **V** from three consecutive majors; minor, bug-fix and build of the copies **E**: one other word decides their positions). `craft_aep::AeVersion`, `AepFile::file_version`, `AepFile::saved_by_version`. | Tests `decodes_the_sampled_words`, `the_container_of_a_copy_states_its_version_in_head_and_the_writer_in_svap`. | +| `head` bytes 0 to 1 (a `u16`) step with the major: 97 (`0x61`) in the 2026 saves, 96 in 25.x, 95 in 24.x; bytes 2 to 3 are 5, 9 and 6 (**?**). Bytes 8 to 15 differ between families, not between versions; bytes 16 to 19 are the save counter (one higher in each copy). Nothing is derived from bytes 0 to 3, and the reader does not expose them. | All 36. | | The XMP packet after the container is no guide: its `CreatorTool` is "Adobe After Effects 2026" in the copies too. | All 36. | | **26 against 25.x** (per family, chunk tree aligned by item and layer id): besides `head`, `nhed` / `nnhd` differ in 6 bytes (`nhed` 20-23 and 26-27: `827fa930` / `0015` in the 2026 save, `00000000` / `011b` in both copies; session state, see section 4). Every **solid's** `Pin /CLRS` changes: the 2026 save has `dcui` (1 byte, 1), `apid` all `0xff` and an empty `Utf8`; the copies drop `dcui`, set `apid` to a GUID (`601e9973...`, the same in all) and embed a 935-byte JSON with an ICC profile (`baseColorProfile/colorProfileData`). A **footage** item that the 2026 save stores with an assigned profile (`mcsp` = 1 and a profile JSON) loses `mcsp` and that JSON in the copies, and its `epid` becomes all `0xff`. The rest is save noise: the footage alias `ascendcount_base` (the copies were saved in another folder), `sspc` byte 112 of a footage item (a modification time), the `oEXR` `opti` of an EXR item (the copies hold the display device name `\.\DISPLAY1` as UTF-16 at byte 126 and, in the 24.x copy, a few non-zero bytes from byte 2 102 that look like uninitialised memory; the 2026 save has zeros), the plug-ins' sequence data `sdat` of Lumetri Color (16 instances and the project's `EfdG`), the mask counter `mkif` byte 8. No chunk is added or reordered, and no `pard`, `cdat`, `ldta`, `cdta`, `tdb4`, keyframe, text or shape chunk differs. | All 12 families. | | **25.x against 24.x**: only `head` (bytes 1, 3, 5 and 7), and the session noise in `opti` and `sdat` above. | All 12 families. | @@ -218,12 +223,12 @@ the value from the corpus manifest (test file in parentheses at the end of each API: `LayerHeader` (`is_video_enabled`/`is_enabled`, `is_audio_enabled`, `is_effects_enabled`, `is_auto_oriented`, `light_type`, `blend_mode`, `stretch` / `stretch_den`, `stretch_ratio`, -`stretch_percent`, `in_point`, `out_point`); the same switches on `Layer`; `Layer::track_matte` +`in_point`, `out_point`); the same switches on `Layer`; `Layer::track_matte` (`TrackMatte`, `MatteMode`; test `tests/matte.rs`). `stretch` is now the numerator of the ratio (it was the whole multiplier before 50 % and 150 % were sampled). Tests: `tests/header.rs`, `tests/layer_switches.rs`. The layer name itself is the `Utf8` child of the `Layr` (empty while the name is the source's); the layer then shows the name of its source item -(`Project::layer_display_name`). +(`Project::layer_source(...)` then `Item::display_name`, the rule the importer uses). ### 3.2 Composition header (`Item/cdta`, 204 bytes; the decoder needs the first 176) and renderer @@ -248,9 +253,9 @@ numerator of the ratio (it was the whole multiplier before 50 % and 150 % were s The inert `ADBE Time Remapping` record of every AV layer has `tduM` = the source's duration in seconds: 5 for 5 s compositions (124, 257, 258), 3 and 2 for a 3 s and a 2 s composition (600, 601), 1 for the 1 s WAV (351), 0.1 for the three-frame sequence (353), 0 on solids and stills (000, 126). Every property's keyframe time scale (`tdb4` bytes 12 to 15) follows the composition (23 976 in every property of `L` after 229 and 230). The corpus manifest calls the classic renderer -`ADBE Advanced 3d`; the file stores `ADBE Escher` / `Classic 3D`. API: `CompHeader::frame_rate_exact` -(exact fraction), `duration_frames`, `shutter_phase`, `display_start`, `display_start_seconds`, -`display_start_frame`. Tests: `tests/header.rs`, `tests/layer_switches.rs`. +`ADBE Advanced 3d`; the file stores `ADBE Escher` / `Classic 3D`. API: `CompHeader::{time_base, +frame_rate_fixed, duration_units, shutter_phase, display_start}` (`display_start` is the exact +fraction). Tests: `tests/header.rs`, `tests/layer_switches.rs`. ### 3.3 Items, footage and precomps @@ -295,7 +300,8 @@ name. A footage layer shows the item's name while it has no name of its own (351 Test: `tests/header.rs` (precomp), `tests/text_markers_footage.rs` and `tests/batch3_objects.rs` (footage), `tests/layer_switches.rs` (solid colour). API: `Footage::{duration, native_frame_rate, -bits_per_pixel, file_size, audio_sample_rate, is_sequence, sequence_frame_count}`. +bits_per_pixel, file_size, audio_sample_rate, is_sequence}`; `Item::sequence_info` (with +`SequenceInfo::frame_count`). ### 3.4 Properties: sparse storage, static values and units @@ -404,8 +410,8 @@ out speed, at 40 out influence**). Scale key 1 of 305 (`n = 3`, in 0 / 33 %, out 15 / 66) is `0,0, .33,.33, 15,15, .66,.66`. Component `i` uses slot `i` of each block (**V** for the block layout and the units, Scale speed in fractions per second and Mask Feather speed in pixels per second; **E** that different values per dimension sit in the slot of their dimension, because -the tool that made the corpus applies one ease to all dimensions; **V** since the exporter wrote three different eases for the three Scale dimensions and After Effects read the same three back, aep-export.md R13). `Keyframe::plain_ease_slots` -returns the doubles as stored, `Keyframe::ease_component(i)` one component's `KeyEase`. Layout of +the tool that made the corpus applies one ease to all dimensions; **V** since the exporter wrote three different eases for the three Scale dimensions and After Effects read the same three back, aep-export.md R13). `Keyframe::ease_component(i)` +returns one component's `KeyEase`, `Keyframe::ease_components` all of them. Layout of the spatial template: `u32` at 8 (1 normally, 0 after the spatial auto-bezier and continuous switches were turned off together, 054), `f64` arc length of the segment to the next key at 16 (100 in 204 key 1, 0 on the last key and right after creation; safe to ignore), **ease at 24 in @@ -430,7 +436,7 @@ in tangent (`n` x `f64`, relative offset from the value) at `56 + 8 n`, out tang | **ease, colour switched to bezier** | spatial n=4 @24..55 | keyframe.set_interpolation bezier on two colour keys stores speed 0 and influence 0 on both sides, and After Effects interpolates them linearly (598 Start Color red to blue: 0.75 / 0.5 / 0.25 red at 0.5 / 1 / 1.5 s); a custom colour ease cannot be set by script | 598 | V | | **ease, mask path** | 64-byte record @24..55 | in speed, in influence, out speed, out influence as in the spatial template: 599 out / in influence 0.75, 700 0.2 / 0.5, speeds 0 (the path at 0.5 s is 5.42 % / 28.0 % of the way). The `f64` at 16 is 1 (093) or 2 (599, 700) on the first key and 0 on the last (not decoded) | 093, 599, 700 | V (influence), ? (speed unit) | -API: `Keyframe::{interpolation_in, interpolation_out, kind, value, ease, ease_component, ease_components, plain_ease_slots, color_distance, spatial}`; +API: `Keyframe::{interpolation_in, interpolation_out, kind, value, ease, ease_component, ease_components, spatial}` (the `f64` at byte 16 of a colour key is `SpatialKey::segment_length`); `Property::keys()` hands the property's template to its records. Marker record (16 bytes): time, `03 03` hold bytes, 8 stale bytes. Mask path record (64 bytes): the common fields and the ease at byte 24 (599, 700). Test: `tests/values.rs`, `tests/keyframes.rs`. @@ -767,7 +773,7 @@ pins are placed by hand or by a preset. 328), so mode and inverted are reported per mask. `mkif` byte 8 is the running mask id counter of the **layer** in the session (1, 2 for the two masks of 328; 329 then gets 3; undo does not reset it; a new layer starts at 1 again): exposed as `Mask::counter`, a session counter and not a stable id. API: -`Mask::{mode, inverted, counter}`, `Project::mask_vertices_px` (vertices in layer pixels), `Layer::mask_atoms` (`MaskAtom`: the mask with its atom group, its path property and its shapes by position, for the keys and expressions of the mask properties). Tests: +`Mask::{mode, inverted, counter}`, `ShapePath::vertices(size)` (vertices in layer pixels: `size` is the size of the layer's source, `Project::layer_source_size`; a text or shape layer has no source size and its mask boxes are already pixels, so the importer passes 1 x 1), `Layer::mask_atoms` (`MaskAtom`: the mask with its atom group, its path property and its shapes by position, for the keys and expressions of the mask properties). Tests: `tests/masks.rs` (with a synthetic check of the handle order), `tests/batch3_objects.rs`. ### 3.9 Shape layers (`tdgp[ADBE Root Vectors Group]`) @@ -895,7 +901,7 @@ groups as on other layers, with these conventions: | What | Path | Stored | Evidence | Conf. | |---|---|---|---|---| -| camera Position, default (auto state) | `ADBE Transform Group/ADBE Position`, `tdb4` 76 and 77 = 1 although no expression exists | `[0, 0, -2666.67]` (x and y are 0 whatever the composition size; z = -zoom). A **one-node** camera reports `[0, 0, -2666.67]` (251). **Correction (S16): a reopened two-node camera reports `[0, 0, -2666.67]` too (256 read back), and After Effects renders it there**: the default two-node camera of 537 above the 3D solid of 486 renders from `[0, 0, -2666.67]` towards the point of interest, not like the composition's default view (537 differs from 486 in 24 % of the grid points, and EffectCraft's import with the stored position matches 537 but for 4 edge points). The `[960, 540, ...]` of the batch-3 notes was not reproduced (535 and 537, created by script, read back the stored value too); `CameraPosition::reported` keeps the old rule and is marked as contradicted | 256, 251, 537 | V | +| camera Position, default (auto state) | `ADBE Transform Group/ADBE Position`, `tdb4` 76 and 77 = 1 although no expression exists | `[0, 0, -2666.67]` (x and y are 0 whatever the composition size; z = -zoom). A **one-node** camera reports `[0, 0, -2666.67]` (251). **Correction (S16): a reopened two-node camera reports `[0, 0, -2666.67]` too (256 read back), and After Effects renders it there**: the default two-node camera of 537 above the 3D solid of 486 renders from `[0, 0, -2666.67]` towards the point of interest, not like the composition's default view (537 differs from 486 in 24 % of the grid points, and EffectCraft's import with the stored position matches 537 but for 4 edge points). The `[960, 540, ...]` of the batch-3 notes was not reproduced (535 and 537, created by script, read back the stored value too); the reader keeps only the stored value (`CameraPosition::stored`) | 256, 251, 537 | V | | camera Position, set by the user | same, `tdb4` 76 and 77 = 0 | the absolute value `[100, 200, -1000]` | 253 | V | | camera point of interest | `ADBE Anchor Point` | `[960, 540, 0]` in **pixels** (not a fraction); the record's `tdsb` is 1 on a two-node and 3 on the one-node camera (not asserted) | 251, 253, 256 | V (value), E (`tdsb`) | | light Position, default | same as the camera | `[0, 0, -1333.33]`, as After Effects reports it (255 read back); the point of interest `[960, 540, 0]` | 254, 255 | V | @@ -911,10 +917,8 @@ groups as on other layers, with these conventions: A point light made by converting a spot light has a different property set (`Orientation`, `Rotate X/Y/Z`, `Cone Angle`, `Cone Feather 2` in the default state) that a spot light lacks (252); whether that -is an artefact of the conversion is untested. API: `Layer::camera_position`, -`CameraPosition::reported`, `Layer::camera_point_of_interest`, `Layer::light_intensity`, -`Layer::{camera_zoom, camera_depth_of_field, camera_focus_distance, camera_aperture, light_color, -light_cone_angle, light_cone_feather}`, `DEFAULT_CAMERA_ZOOM` (2 666.67 is verified for the 1920 px +is an artefact of the conversion is untested. API: `Layer::camera_position`; the camera and light +options are read by the importer's table (`import/camera.rs`); `DEFAULT_CAMERA_ZOOM` (2 666.67 is verified for the 1920 px composition only), `Layer::orientation` (`Orientation`; test `orientation_is_otst_otda` in `tests/batch5.rs`). Render checks against After Effects (20 px grid, 5 184 points): 486 1 point off, 535 2, 536 0, 537 4 (anti-aliased edges); **488 (Orientation `[10, 20, 30]`): 272 points diff --git a/crates/aep/src/aepx.rs b/crates/aep/src/aepx.rs index 9fad696ce..5267080fb 100644 --- a/crates/aep/src/aepx.rs +++ b/crates/aep/src/aepx.rs @@ -12,13 +12,23 @@ //! instructions. A document type declaration is refused (no entity expansion). It runs without //! recursion; the nesting depth and the element count are bounded by [`Limits`], and everything //! it allocates is bounded by the size of the input. +//! +//! There are two readers. [`read_aepx`] builds the owned tree element by element +//! (`aepx/tree.rs`). The importer, which only reads, uses `index_aepx`: it decodes every body +//! (hex, text, the alias JSON, the numbers) into one buffer and records the chunks as entries of +//! the flat index ([`Draft`]s, in depth-first order, bodies as ranges of the buffer), which +//! [`crate::TreeIndex::from_drafts`] turns into an index that borrows the buffer. +//! The tests hold both to the same tree and the same errors. use std::borrow::Cow; -use crate::chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS}; +use crate::chunk::{AepFile, LIST, NESTED_IDS}; use crate::error::AepError; +use crate::index::{Draft, Kind as EntryKind}; use crate::read::{Limits, parse}; +mod tree; + /// The root element of an `.aepx`. const ROOT: &[u8] = b"AfterEffectsProject"; /// The element that holds the XMP packet (the trailer of an `.aep`). @@ -68,17 +78,71 @@ pub fn read_aepx(data: &[u8]) -> Result, AepError> { /// # Errors /// See [`read_aepx`]. pub fn read_aepx_with_limits(data: &[u8], limits: Limits) -> Result, AepError> { + tree::read(data, limits) +} + +/// An `.aepx` read for the flat index: every body decoded into one buffer, and the chunks as +/// [`Draft`]s in depth-first order with their bodies as ranges of that buffer. +/// [`crate::TreeIndex::from_drafts`] makes the index, which borrows the buffer. +pub struct AepxDrafts { + /// The decoded bodies (hex, text, alias JSON, numbers) and the XMP packet. + pub buffer: Vec, + pub nodes: Vec, + /// The XMP packet: a range of `buffer` (empty without one). + pub trailer: (usize, usize), +} + +/// Reads an `.aepx` into [`AepxDrafts`]: the tree [`read_aepx_with_limits`] gives, with the same +/// errors, without one allocation per chunk. +/// +/// # Errors +/// See [`read_aepx_with_limits`]. When memory runs out, two errors come up in more cases than +/// there: [`AepError::TooLarge`] when the chunk list cannot grow (the owned-tree reader would +/// abort), and [`AepError::Xml`] "bdata is too large to hold in memory" also when the shared body +/// buffer cannot grow (the owned-tree reader reserves each value on its own). +pub fn index_aepx(data: &[u8], limits: Limits) -> Result { let text = match std::str::from_utf8(data) { Ok(t) => t.as_bytes(), Err(e) => return Err(xml(e.valid_up_to(), "the file is not valid UTF-8")), }; - Reader { data: text, pos: 0, limits, count: 0, stack: Vec::new(), top: None, trailer: None }.run() + // Every counted element takes at least 3 bytes (`` in a document cut short), so there are + // never more chunks than this. + let bound = (text.len() / 3).min(limits.max_chunks); + let mut nodes = Vec::new(); + let _ = nodes.try_reserve_exact(bound.min(INITIAL_SLOTS)); + // Hex is most of an `.aepx` and decodes to half its length: one buffer of that size usually + // holds everything. A failed reservation is not an error here; the buffer grows as it goes. + let mut buffer = Vec::new(); + let _ = buffer.try_reserve_exact(text.len() / 2); + let reader = Reader { + data: text, + pos: 0, + limits, + count: 0, + stack: Vec::new(), + root_closed: false, + trailer: None, + buffer, + nodes, + bound, + attrs: Vec::new(), + }; + reader.run() } +/// The slots the chunk list starts with; it doubles from there (as the array of +/// [`crate::TreeIndex`] does). +const INITIAL_SLOTS: usize = 1024; + fn xml(offset: usize, what: &'static str) -> AepError { AepError::Xml { offset, what } } +/// The pad byte of a body of `len` bytes, as [`crate::Chunk::data`] sets it. +fn pad_of(len: usize) -> Option { + if len % 2 == 1 { Some(0) } else { None } +} + /// What an open element becomes when it closes. #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Kind { @@ -102,13 +166,14 @@ struct Frame { at: usize, /// The name as written, to match the end tag. name: (usize, usize), - /// The decoded `bdata` attribute. - bdata: Option>, + /// The chunk's entry in the list (for `Chunk`, `Utf8` and `Alias`). + entry: Option, + /// The buffer length when the element opened: its body starts here. + mark: usize, + /// The end of the decoded `bdata` attribute in the buffer. + bdata_end: Option, /// The attributes of a `fileReference`, in document order. attrs: Vec<(String, String)>, - /// Collected text (only for `string` and the XMP packet). - text: Vec, - children: Vec>, } struct Reader<'d> { @@ -117,13 +182,19 @@ struct Reader<'d> { limits: Limits, count: usize, stack: Vec, - /// The top-level chunks once the root element has closed. - top: Option>>, - trailer: Option>, + /// True once the root element has closed. + root_closed: bool, + trailer: Option<(usize, usize)>, + buffer: Vec, + nodes: Vec, + /// The most chunks the input can hold under the limits: `nodes` never reserves past it. + bound: usize, + /// The attributes of the current start tag; kept to reuse its allocation. + attrs: Vec<((usize, usize), Cow<'d, [u8]>)>, } -impl Reader<'_> { - fn run(mut self) -> Result, AepError> { +impl<'d> Reader<'d> { + fn run(mut self) -> Result { if self.data.starts_with(&[0xef, 0xbb, 0xbf]) { self.pos = 3; } @@ -137,11 +208,13 @@ impl Reader<'_> { if let Some(open) = self.stack.last() { return Err(xml(open.at, "an element is not closed before the end of the file")); } - let chunks = self.top.ok_or_else(|| xml(self.data.len(), "no AfterEffectsProject root element"))?; - Ok(AepFile { chunks, trailer: Cow::Owned(self.trailer.unwrap_or_default()) }) + if !self.root_closed { + return Err(xml(self.data.len(), "no AfterEffectsProject root element")); + } + Ok(AepxDrafts { buffer: self.buffer, nodes: self.nodes, trailer: self.trailer.unwrap_or((0, 0)) }) } - fn rest(&self) -> &[u8] { + fn rest(&self) -> &'d [u8] { self.data.get(self.pos..).unwrap_or(&[]) } @@ -181,14 +254,13 @@ impl Reader<'_> { self.pos = start; let end = self.skip_past(b"]]>", "a CDATA section is not closed")?; let content = self.data.get(start..end).unwrap_or(&[]); - let at = start; - match self.stack.last_mut() { + match self.stack.last() { Some(frame) => { if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) { - frame.text.extend_from_slice(content); + self.buffer.extend_from_slice(content); } } - None => return Err(xml(at, "CDATA outside the root element")), + None => return Err(xml(start, "CDATA outside the root element")), } } else if rest.starts_with(b" { self.pos = self.pos.saturating_add(n); } - fn slice(&self, (a, b): (usize, usize)) -> &[u8] { + fn slice(&self, (a, b): (usize, usize)) -> &'d [u8] { self.data.get(a..b).unwrap_or(&[]) } - fn start_tag(&mut self) -> Result<(), AepError> { - let at = self.pos; - self.pos = self.pos.saturating_add(1); - let name = self.name()?; - let mut attrs: Vec<((usize, usize), Vec)> = Vec::new(); - let self_closing = loop { + /// Reads the attributes of a start tag into `attrs`; true when the tag closes itself. + fn attributes(&mut self, at: usize, attrs: &mut Vec<((usize, usize), Cow<'d, [u8]>)>) -> Result { + loop { self.skip_ws(); let rest = self.rest(); if rest.starts_with(b"/>") { self.pos = self.pos.saturating_add(2); - break true; + return Ok(true); } if rest.starts_with(b">") { self.pos = self.pos.saturating_add(1); - break false; + return Ok(false); } if rest.is_empty() { return Err(xml(at, "a start tag is not closed")); @@ -259,11 +328,27 @@ impl Reader<'_> { if raw.contains(&b'<') { return Err(xml(vstart, "`<` inside an attribute value")); } - let mut value = Vec::new(); - decode_entities(raw, vstart, &mut value)?; + // A value without `&` is its own decoding (most of them, every `bdata` After Effects + // writes): borrow it instead of copying it. + let value = if raw.contains(&b'&') { + let mut value = Vec::new(); + decode_entities(raw, vstart, &mut value)?; + Cow::Owned(value) + } else { + Cow::Borrowed(raw) + }; self.pos = vend.saturating_add(1); attrs.push((key, value)); - }; + } + } + + fn start_tag(&mut self) -> Result<(), AepError> { + let at = self.pos; + self.pos = self.pos.saturating_add(1); + let name = self.name()?; + let mut attrs = std::mem::take(&mut self.attrs); + attrs.clear(); + let self_closing = self.attributes(at, &mut attrs)?; let kind = self.kind_of(name, at)?; if kind != Kind::Root { self.count = self.count.saturating_add(1); @@ -275,15 +360,27 @@ impl Reader<'_> { return Err(AepError::TooDeep); } } - let mut frame = Frame { kind, at, name, bdata: None, attrs: Vec::new(), text: Vec::new(), children: Vec::new() }; - if matches!(kind, Kind::Chunk(_)) { - if let Some((key, value)) = attrs.iter().find(|(k, _)| self.slice(*k) == b"bdata") { - frame.bdata = Some(decode_hex(value, key.1)?); + let mark = self.buffer.len(); + let mut frame = Frame { kind, at, name, entry: None, mark, bdata_end: None, attrs: Vec::new() }; + match kind { + Kind::Chunk(id) => { + if let Some((key, value)) = attrs.iter().find(|(k, _)| self.slice(*k) == b"bdata") { + decode_hex_into(value, key.1, MAX_BDATA, &mut self.buffer)?; + frame.bdata_end = Some(self.buffer.len()); + } + frame.entry = Some(self.open_entry(id)?); } - } else if kind == Kind::Alias { - frame.attrs = - attrs.iter().map(|(k, v)| (String::from_utf8_lossy(self.slice(*k)).into_owned(), String::from_utf8_lossy(v).into_owned())).collect(); + Kind::Utf8 => frame.entry = Some(self.open_entry(*b"Utf8")?), + Kind::Alias => { + frame.attrs = attrs + .iter() + .map(|(k, v)| (String::from_utf8_lossy(self.slice(*k)).into_owned(), String::from_utf8_lossy(v).into_owned())) + .collect(); + frame.entry = Some(self.open_entry(*b"alas")?); + } + Kind::Root | Kind::Xmp | Kind::Skip => {} } + self.attrs = attrs; self.stack.push(frame); if self_closing { self.close()?; @@ -291,12 +388,38 @@ impl Reader<'_> { Ok(()) } + /// Appends the entry of a chunk whose element just opened; its body is filled in when it + /// closes. The list grows by its length (at least 16) but never past [`Self::bound`]; an + /// allocation the target cannot make is an error, not an abort. + fn open_entry(&mut self, id: [u8; 4]) -> Result { + let at = self.nodes.len(); + // This entry and the one after it must have `u32` indices. + u32::try_from(at.checked_add(1).ok_or(AepError::TooManyChunks)?).map_err(|_| AepError::TooManyChunks)?; + if at == self.nodes.capacity() { + // Past the bound (which only a wrong guess could reach) it keeps doubling. + let room = self.bound.saturating_sub(at); + let more = if room == 0 { at.max(16) } else { at.max(16).min(room) }; + self.nodes.try_reserve_exact(more).map_err(|_| AepError::TooLarge)?; + } + self.nodes.push(Draft::placeholder(id)); + Ok(at) + } + + /// Fills in the entry at `at` once its element has closed; its subtree is what follows it. + fn finish(&mut self, at: usize, id: [u8; 4], kind: EntryKind, list_type: [u8; 4], body: (usize, usize), pad: Option) -> Result<(), AepError> { + let end = u32::try_from(self.nodes.len()).map_err(|_| AepError::TooManyChunks)?; + if let Some(d) = self.nodes.get_mut(at) { + *d = Draft { start: body.0, len: body.1.saturating_sub(body.0), id, list_type, end, kind, pad }; + } + Ok(()) + } + /// Classifies an element by its name and where it is. fn kind_of(&self, name: (usize, usize), at: usize) -> Result { let n = self.slice(name); let parent = match self.stack.last() { None => { - if self.top.is_some() { + if self.root_closed { return Err(xml(at, "a second root element")); } if n != ROOT { @@ -340,39 +463,104 @@ impl Reader<'_> { self.close() } - /// Closes the innermost element and hands its chunk to the parent. + /// Closes the innermost element and fills in its chunk's entry. fn close(&mut self) -> Result<(), AepError> { let frame = self.stack.pop().ok_or(xml(self.pos, "an end tag without a start tag"))?; - let chunk = match frame.kind { - Kind::Root => { - self.top = Some(frame.children); - return Ok(()); - } - Kind::Skip => return Ok(()), + match frame.kind { + Kind::Root => self.root_closed = true, + Kind::Skip => {} Kind::Xmp => { + // Only the first packet is kept; a later one is dropped from the buffer (its + // children are skipped, so nothing else was written after it opened). if self.trailer.is_none() { - self.trailer = Some(frame.text); + self.trailer = Some((frame.mark, self.buffer.len())); + } else { + self.buffer.truncate(frame.mark); } - return Ok(()); } - Kind::Utf8 => Chunk::data(*b"Utf8", frame.text), - Kind::Alias => Chunk::data(*b"alas", alias_json(&frame.attrs).into_bytes()), - Kind::Chunk(id) => chunk_of(id, frame)?, - }; - if let Some(parent) = self.stack.last_mut() { - parent.children.push(chunk); + Kind::Utf8 => { + let at = frame.entry.ok_or(xml(frame.at, "an element lost its chunk"))?; + let end = self.buffer.len(); + self.finish(at, *b"Utf8", EntryKind::Data, [0; 4], (frame.mark, end), pad_of(end.saturating_sub(frame.mark)))?; + } + Kind::Alias => { + let at = frame.entry.ok_or(xml(frame.at, "an element lost its chunk"))?; + self.buffer.extend_from_slice(alias_json(&frame.attrs).as_bytes()); + let end = self.buffer.len(); + self.finish(at, *b"alas", EntryKind::Data, [0; 4], (frame.mark, end), pad_of(end.saturating_sub(frame.mark)))?; + } + Kind::Chunk(id) => self.close_chunk(id, &frame)?, } Ok(()) } + /// The chunk of a closed element named by a chunk id (what the owned reader's `chunk_of` + /// built): `bdata` wins over children, `numS` / `ppSn` hold their number, a nested id holds its + /// children (or nothing), any other name is a `LIST` of that type. + fn close_chunk(&mut self, id: [u8; 4], frame: &Frame) -> Result<(), AepError> { + let at = frame.entry.ok_or(xml(frame.at, "an element lost its chunk"))?; + let after = at.checked_add(1).ok_or(AepError::TooManyChunks)?; + if let Some(bdata_end) = frame.bdata_end { + // The children were read (and counted, and checked) but are not part of the chunk. + self.nodes.truncate(after); + self.buffer.truncate(bdata_end); + let body = (frame.mark, bdata_end); + let pad = pad_of(bdata_end.saturating_sub(frame.mark)); + if OPAQUE_LIST_TYPES.contains(&id) { + return self.finish(at, LIST, EntryKind::OpaqueList, id, body, pad); + } + return self.finish(at, id, EntryKind::Data, [0; 4], body, pad); + } + // Numbers written as text: `0` and `150.000000000`. + if &id == b"numS" || &id == b"ppSn" { + let missing = || xml(frame.at, "a numS or ppSn element without its number"); + // Exactly one child chunk, a `Utf8`: the entry after this one, whose subtree runs to the end. + let only = self.nodes.get(after).copied().filter(|c| usize::try_from(c.end).ok() == Some(self.nodes.len())).ok_or_else(missing)?; + if only.id != *b"Utf8" { + return Err(missing()); + } + let text = match only.kind { + EntryKind::Data | EntryKind::OpaqueList => { + only.start.checked_add(only.len).and_then(|end| self.buffer.get(only.start..end)).unwrap_or(&[]) + } + EntryKind::List | EntryKind::Nested => &[], + }; + let text = std::str::from_utf8(text).map_err(|_| xml(frame.at, "a number is not text"))?.trim(); + let body: [u8; 8]; + let bytes: &[u8] = if &id == b"numS" { + // Only `0` is sampled, so whether the field is signed is open: accept both readings of 4 bytes. + let n = text.parse::().ok().filter(|n| (i64::from(i32::MIN)..=i64::from(u32::MAX)).contains(n)); + let n = n.ok_or(xml(frame.at, "numS is not a 32-bit integer"))?; + body = u64::from(n as u32).to_be_bytes(); + body.get(4..).unwrap_or(&[]) + } else { + body = text.parse::().map_err(|_| xml(frame.at, "ppSn is not a number"))?.to_be_bytes(); + &body + }; + self.nodes.truncate(after); + self.buffer.truncate(frame.mark); + self.buffer.extend_from_slice(bytes); + let end = self.buffer.len(); + return self.finish(at, id, EntryKind::Data, [0; 4], (frame.mark, end), pad_of(bytes.len())); + } + let empty = (frame.mark, frame.mark); + if NESTED_IDS.contains(&id) { + if self.nodes.len() == after { + return self.finish(at, id, EntryKind::Data, [0; 4], empty, None); + } + return self.finish(at, id, EntryKind::Nested, [0; 4], empty, None); + } + self.finish(at, LIST, EntryKind::List, id, empty, None) + } + fn text(&mut self) -> Result<(), AepError> { let start = self.pos; let len = self.rest().iter().position(|b| *b == b'<').unwrap_or(self.rest().len()); let end = start.saturating_add(len); self.pos = end; let raw = self.data.get(start..end).unwrap_or(&[]); - match self.stack.last_mut() { - Some(frame) if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) => decode_entities(raw, start, &mut frame.text), + match self.stack.last() { + Some(frame) if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) => decode_entities(raw, start, &mut self.buffer), Some(_) => Ok(()), None if raw.iter().all(u8::is_ascii_whitespace) => Ok(()), None => Err(xml(start, "text outside the root element")), @@ -393,41 +581,6 @@ fn chunk_id(name: &[u8]) -> Option<[u8; 4]> { Some(id) } -/// The chunk of a closed element named by a chunk id. -fn chunk_of(id: [u8; 4], frame: Frame) -> Result, AepError> { - if let Some(bytes) = frame.bdata { - if OPAQUE_LIST_TYPES.contains(&id) { - let pad = if bytes.len() % 2 == 1 { Some(0) } else { None }; - return Ok(Chunk { id: LIST, body: Body::OpaqueList { list_type: id, data: Cow::Owned(bytes) }, pad }); - } - return Ok(Chunk::data(id, bytes)); - } - // Numbers written as text: `0` and `150.000000000`. - if &id == b"numS" || &id == b"ppSn" { - let text = match frame.children.as_slice() { - [only] if only.is(b"Utf8") => only.bytes().unwrap_or(&[]), - _ => return Err(xml(frame.at, "a numS or ppSn element without its number")), - }; - let text = std::str::from_utf8(text).map_err(|_| xml(frame.at, "a number is not text"))?.trim(); - let body = if &id == b"numS" { - // Only `0` is sampled, so whether the field is signed is open: accept both readings of 4 bytes. - let n = text.parse::().ok().filter(|n| (i64::from(i32::MIN)..=i64::from(u32::MAX)).contains(n)); - let n = n.ok_or(xml(frame.at, "numS is not a 32-bit integer"))?; - (n as u32).to_be_bytes().to_vec() - } else { - text.parse::().map_err(|_| xml(frame.at, "ppSn is not a number"))?.to_be_bytes().to_vec() - }; - return Ok(Chunk::data(id, body)); - } - if NESTED_IDS.contains(&id) { - if frame.children.is_empty() { - return Ok(Chunk::data(id, Vec::new())); - } - return Ok(Chunk { id, body: Body::Chunks(frame.children), pad: None }); - } - Ok(Chunk::list(id, frame.children)) -} - /// Appends `raw` with the predefined entities and character references decoded. fn decode_entities(raw: &[u8], offset: usize, out: &mut Vec) -> Result<(), AepError> { let mut i = 0; @@ -465,23 +618,74 @@ fn decode_entities(raw: &[u8], offset: usize, out: &mut Vec) -> Result<(), A Ok(()) } -/// Decodes a `bdata` value (hex digits, two per byte). +/// Decodes a `bdata` value (hex digits, two per byte) into a new `Vec` of exactly its size. fn decode_hex(text: &[u8], offset: usize) -> Result, AepError> { - if text.len() % 2 == 1 { - return Err(xml(offset, "bdata has an odd number of hex digits")); - } + decode_hex_at_most(text, offset, MAX_BDATA) +} + +/// The most bytes one `bdata` may decode to: what the 32-bit size field of a chunk can hold (a +/// larger body could not be written back as an `.aep` either). +const MAX_BDATA: usize = u32::MAX as usize; + +/// [`decode_hex`] with the decoded-size limit as a parameter (so tests can reach it). +fn decode_hex_at_most(text: &[u8], offset: usize, max: usize) -> Result, AepError> { + // Checked before anything is reserved, then sized once (collecting into `Option` has no + // size hint and regrows step by step), and without aborting when the memory is not there. + let pairs = check_hex(text, offset, max)?; + let mut out = Vec::new(); + out.try_reserve_exact(pairs).map_err(|_| xml(offset, "bdata is too large to hold in memory"))?; + decode_checked(text, offset, &mut out)?; + Ok(out) +} + +/// Appends the bytes of a `bdata` value (hex digits, two per byte, at most `max` bytes) to `out`. +/// On an error `out` is unchanged. +fn decode_hex_into(text: &[u8], offset: usize, max: usize, out: &mut Vec) -> Result<(), AepError> { + let pairs = check_hex(text, offset, max)?; + // Grown at most once per value (amortised over a shared buffer), and without aborting when the + // memory is not there. + out.try_reserve(pairs).map_err(|_| xml(offset, "bdata is too large to hold in memory"))?; + decode_checked(text, offset, out) +} + +/// Appends the bytes of a `bdata` value that [`check_hex`] accepted. On an error (a digit the +/// check let through, which it does not) `out` is unchanged. +fn decode_checked(text: &[u8], offset: usize, out: &mut Vec) -> Result<(), AepError> { let digit = |b: u8| match b { b'0'..=b'9' => Some(b - b'0'), b'a'..=b'f' => Some(b - b'a' + 10), b'A'..=b'F' => Some(b - b'A' + 10), _ => None, }; - let (pairs, _) = text.as_chunks::<2>(); - pairs - .iter() - .map(|[hi, lo]| digit(*hi).zip(digit(*lo)).map(|(h, l)| (h << 4) | l)) - .collect::>>() - .ok_or(xml(offset, "bdata holds a character that is not a hex digit")) + let mark = out.len(); + for [hi, lo] in text.as_chunks::<2>().0 { + match (digit(*hi), digit(*lo)) { + (Some(h), Some(l)) => out.push((h << 4) | l), + _ => { + out.truncate(mark); + return Err(xml(offset, "bdata holds a character that is not a hex digit")); + } + } + } + Ok(()) +} + +/// Checks a `bdata` value before anything is reserved for it: an even number of hex digits, at +/// most `max` bytes. Returns the number of bytes it decodes to. +fn check_hex(text: &[u8], offset: usize, max: usize) -> Result { + if text.len() % 2 == 1 { + return Err(xml(offset, "bdata has an odd number of hex digits")); + } + let pairs = text.len() / 2; + if pairs > max { + return Err(AepError::TooLarge); + } + // One cheap pass checks every digit first. Blocks without an early exit inside, so the check + // vectorises; it stops at the first bad block. + if !text.chunks(4096).all(|block| block.iter().fold(true, |ok, b| ok & b.is_ascii_hexdigit())) { + return Err(xml(offset, "bdata holds a character that is not a hex digit")); + } + Ok(pairs) } /// The JSON body of an `alas` chunk from the attributes of a `fileReference`, keys in document @@ -529,6 +733,7 @@ fn json_str(out: &mut String, s: &str) { #[cfg(test)] mod tests { use super::*; + use crate::index::TreeIndex; fn doc(body: &str) -> String { format!("\n{body}\n") @@ -629,6 +834,92 @@ mod tests { assert!(matches!(read_aepx(b"\xff"), Err(AepError::Xml { .. }))); } + /// Attribute values decode entities only where they have them. + #[test] + fn attribute_values_decode_entities_only_where_they_have_them() { + // An entity inside `bdata` still decodes before the hex does. + let f = read_aepx(doc("").as_bytes()).unwrap(); + assert_eq!(f.chunks[0].bytes(), Some(&[0x01, 0xab][..])); + // A bad entity in any attribute, even of an element that is skipped, is still an error. + for bad in ["", "", ""] { + assert!(matches!(read_aepx(doc(bad).as_bytes()), Err(AepError::Xml { .. })), "{bad}"); + } + // Without `&` the value is taken as written. + let f = read_aepx(doc("").as_bytes()).unwrap(); + assert_eq!(f.chunks[0].bytes(), Some(&[0x0a, 0xff][..])); + } + + /// A large bdata with a bad digit is refused before it is sized. + #[test] + fn a_large_bdata_with_a_bad_digit_is_refused_before_it_is_sized() { + // 10 MB of hex that is bad from the first pair (and once more at the very end). + let mut text = b"zz".to_vec(); + text.extend(std::iter::repeat_n(*b"00", 5_000_000).flatten()); + assert_eq!(decode_hex(&text, 3), Err(xml(3, "bdata holds a character that is not a hex digit"))); + let mut late = vec![b'0'; 10_000_000]; + late.extend_from_slice(b"0g"); + assert_eq!(decode_hex(&late, 3), Err(xml(3, "bdata holds a character that is not a hex digit"))); + let x = doc(&format!("", std::str::from_utf8(&text).unwrap())); + assert!(matches!(read_aepx(x.as_bytes()), Err(AepError::Xml { .. }))); + } + + /// Bdata decodes up to the size limit and not past it. + #[test] + fn bdata_decodes_up_to_the_size_limit_and_not_past_it() { + assert_eq!(decode_hex_at_most(b"00ff10", 0, 3), Ok(vec![0x00, 0xff, 0x10])); + assert_eq!(decode_hex_at_most(b"00ff10", 0, 2), Err(AepError::TooLarge)); + assert_eq!(decode_hex_at_most(b"", 0, 0), Ok(Vec::new())); + assert_eq!(decode_hex_at_most(b"00", 0, 0), Err(AepError::TooLarge)); + // The odd-length error still comes first. + assert_eq!(decode_hex_at_most(b"001", 0, 0), Err(xml(0, "bdata has an odd number of hex digits"))); + // The real limit is what a chunk's 32-bit size field holds. + assert_eq!(MAX_BDATA as u64, u64::from(u32::MAX)); + } + + /// Decode hex matches the collecting version. + #[test] + fn decode_hex_matches_the_collecting_version() { + /// The collecting decoder this one replaced, as the reference. + fn old(text: &[u8], offset: usize) -> Result, AepError> { + if text.len() % 2 == 1 { + return Err(xml(offset, "bdata has an odd number of hex digits")); + } + let digit = |b: u8| match b { + b'0'..=b'9' => Some(b - b'0'), + b'a'..=b'f' => Some(b - b'a' + 10), + b'A'..=b'F' => Some(b - b'A' + 10), + _ => None, + }; + let (pairs, _) = text.as_chunks::<2>(); + pairs + .iter() + .map(|[hi, lo]| digit(*hi).zip(digit(*lo)).map(|(h, l)| (h << 4) | l)) + .collect::>>() + .ok_or(xml(offset, "bdata holds a character that is not a hex digit")) + } + let alphabet = b"0123456789abcdefABCDEFgG \0"; + let mut seed = 0x2545_f491_4f6c_dd1du64; + for len in 0..200usize { + for _ in 0..20 { + let text: Vec = (0..len) + .map(|_| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + // Mostly hex digits, now and then a byte that is not one. + let i = (seed % 1000) as usize; + alphabet[if i < 990 { i % 22 } else { 22 + i % 4 }] + }) + .collect(); + let new = decode_hex(&text, 7); + assert_eq!(new, old(&text, 7)); + if let Ok(v) = new { + assert_eq!(v.capacity(), v.len()); + } + } + } + } + #[test] fn limits_hold() { let deep = format!("{}{}", "".repeat(100_000), "".repeat(100_000)); @@ -638,4 +929,293 @@ mod tests { let many = format!("{}", "".repeat(10)); assert_eq!(read_aepx_with_limits(many.as_bytes(), Limits { max_depth: 64, max_chunks: 9 }), Err(AepError::TooManyChunks)); } + + /// A tiny deterministic generator (xorshift64*), so the tests need no extra crate. + struct Rng(u64); + + impl Rng { + /// The next pseudo-random number. + fn next(&mut self) -> u64 { + let mut x = self.0; + x ^= x >> 12; + x ^= x << 25; + x ^= x >> 27; + self.0 = x; + x.wrapping_mul(0x2545_f491_4f6c_dd1d) + } + + /// A number below `n` (0 for `n == 0`). + fn below(&mut self, n: usize) -> usize { + if n == 0 { 0 } else { usize::try_from(self.next() % n as u64).unwrap() } + } + + /// One of `items`. + fn pick<'s>(&mut self, items: &[&'s str]) -> &'s str { + items[self.below(items.len())] + } + } + + /// `n` random bytes as hex, upper or lower case. + fn hex(rng: &mut Rng, n: usize) -> String { + let upper = rng.below(4) == 0; + (0..n).map(|_| if upper { format!("{:02X}", rng.below(256)) } else { format!("{:02x}", rng.below(256)) }).collect() + } + + /// Random `.aepx` content: every element form the reader knows (data, lists, nested ids, + /// strings, numbers, `btdk`, file references, skipped elements, XMP packets), `bdata` next to + /// children, bodies that read as chunk sequences, entities, CDATA, comments and stray text. + fn content(rng: &mut Rng, depth: usize, budget: &mut usize, out: &mut String) { + for _ in 0..rng.below(6) { + if *budget == 0 { + return; + } + *budget -= 1; + match rng.below(20) { + 0..=3 => { + let id = rng.pick(&["cdat", "tdmn", "head", "Utf8", "idta", "LIST", "a"]); + let n = rng.below(9); + out.push_str(&format!("<{id} bdata=\"{}\"/>", hex(rng, n))); + } + 4..=6 if depth < 10 => { + let id = rng.pick(&["Fold", "tdgp", "Layr", "Pin", "LIST", "btdk", "sspc"]); + let attr = if rng.below(5) == 0 { format!(" bdata=\"{}\"", hex(rng, 3)) } else { String::new() }; + out.push_str(&format!("<{id}{attr}>")); + content(rng, depth + 1, budget, out); + out.push_str(&format!("")); + } + 7 => { + let id = rng.pick(&["tdsn", "pdnm", "fnam"]); + match rng.below(4) { + 0 => out.push_str(&format!("<{id}/>")), + 1 => out.push_str(&format!("<{id}>name & A")), + // A body that is itself a chunk sequence (`Utf8`, 1 byte, pad): still data. + 2 => out.push_str(&format!("<{id} bdata=\"557466380000000161{}\"/>", if rng.below(2) == 0 { "00" } else { "" })), + _ => out.push_str(&format!("<{id}>ab")), + } + } + 8 => { + let text = + rng.pick(&["plain", "A < B", "", "ab", "", "été", "xgoney", "\r\n"]); + out.push_str(&format!("{text}")); + } + 9 => { + let id = rng.pick(&["numS", "ppSn"]); + let inner = rng.pick(&[ + "0", + " 7 ", + "-2147483648", + "150.000000000", + "x", + "", + "12", + "", + "", + "34", + ]); + let attr = if rng.below(6) == 0 { " bdata=\"00000002\"" } else { "" }; + out.push_str(&format!("<{id}{attr}>{inner}")); + } + 10 => { + let n = rng.pick(&["", "41", "414243", "4c495354000000000000"]); + out.push_str(&format!("")); + } + 11 => { + let platform = rng.pick(&["Win", "1", "Mac"]); + out.push_str(&format!( + "", + rng.below(3) + )); + } + 12 if depth < 10 => { + out.push_str("text"); + content(rng, depth + 1, budget, out); + out.push_str(""); + } + 13 => { + let packet = rng.pick(&["x]]>", "plain & text", ""]); + out.push_str(&format!("{packet}")); + } + 14 => out.push_str(rng.pick(&["", "", "\n ", "stray"])), + _ => { + let n = rng.below(5); + out.push_str(&format!("", hex(rng, n))); + } + } + } + } + + /// The default limits and six tight ones (depth 0, 1 and 3; 0, 7 and 40 chunks). + fn limit_sets() -> [Limits; 7] { + [ + Limits { max_depth: 0, max_chunks: 10_000_000 }, + Limits { max_depth: 64, max_chunks: 0 }, + Limits::default(), + Limits { max_depth: 1, max_chunks: 10_000_000 }, + Limits { max_depth: 3, max_chunks: 10_000_000 }, + Limits { max_depth: 64, max_chunks: 7 }, + Limits { max_depth: 64, max_chunks: 40 }, + ] + } + + /// The index reader and the owned-tree reader give the same `Result` (the tree, or the error), + /// and the index walks like the index of the owned tree. + fn same(bytes: &[u8], limits: Limits) { + let old = read_aepx_with_limits(bytes, limits); + let new = index_aepx(bytes, limits).map(|d| TreeIndex::from_drafts(&d.buffer, d.nodes, d.trailer).to_file().into_owned()); + assert_eq!(new, old, "{}", String::from_utf8_lossy(bytes)); + if let Ok(file) = &old { + let AepxDrafts { buffer, nodes, trailer } = index_aepx(bytes, limits).unwrap(); + let index = TreeIndex::from_drafts(&buffer, nodes, trailer); + let expected = TreeIndex::from_file(file).unwrap(); + assert_eq!(index.len(), expected.len()); + assert_eq!(index.trailer(), expected.trailer()); + let a: Vec = index.walk().map(|(d, n)| format!("{d} {n:?}")).collect(); + let b: Vec = expected.walk().map(|(d, n)| format!("{d} {n:?}")).collect(); + assert_eq!(a, b); + } + } + + /// The two readers give the same tree and errors on generated and damaged documents. + #[test] + fn the_two_readers_agree_on_generated_and_damaged_documents() { + let mut rng = Rng(0x9e37_79b9_7f4a_7c15); + let noise = b"<>/=\"'&;#x0aZ \n![]-?"; + for _ in 0..600 { + let mut budget = 80; + let mut body = String::new(); + content(&mut rng, 0, &mut budget, &mut body); + let x = doc(&body); + for l in limit_sets() { + same(x.as_bytes(), l); + } + for _ in 0..4 { + let mut bad = x.clone().into_bytes(); + for _ in 0..1 + rng.below(3) { + let at = rng.below(bad.len()); + bad[at] = noise[rng.below(noise.len())]; + } + for l in limit_sets() { + same(&bad, l); + } + } + let cut = rng.below(x.len()); + same(&x.as_bytes()[..cut], Limits::default()); + // Bytes that are not UTF-8, and a multi-byte character cut in half. + let mut bad = x.clone().into_bytes(); + let at = rng.below(bad.len()); + bad.insert(at, 0xff); + same(&bad, Limits::default()); + let open = format!("{}é", x.trim_end().trim_end_matches("")).into_bytes(); + same(&open[..open.len() - 1], Limits::default()); + } + } + + /// The flat chunk list never reserves more slots than the input or the chunk limit allows. + #[test] + fn the_chunk_list_is_bounded_by_the_input_and_the_limit() { + let many = format!("{}", "".repeat(10_000)); + let d = index_aepx(many.as_bytes(), Limits::default()).unwrap(); + assert_eq!(d.nodes.len(), 10_000); + assert!(d.nodes.capacity() <= 2 * 10_000, "{} slots for 10 000 chunks", d.nodes.capacity()); + // The buffer is reserved once, at half the input, and holds nothing here. + assert!(d.buffer.is_empty() && d.buffer.capacity() <= many.len() / 2); + // At the limit the list never holds more slots than the limit. + let d = index_aepx(many.as_bytes(), Limits { max_depth: 64, max_chunks: 10_000 }).unwrap(); + assert!(d.nodes.capacity() <= 10_000); + let few = format!("{}", "".repeat(100)); + let d = index_aepx(few.as_bytes(), Limits { max_depth: 64, max_chunks: 100 }).unwrap(); + assert!(d.nodes.capacity() <= 100, "{} slots for at most 100 chunks", d.nodes.capacity()); + assert_eq!(index_aepx(few.as_bytes(), Limits { max_depth: 64, max_chunks: 99 }).err(), Some(AepError::TooManyChunks)); + // A tiny input cannot hold more than a third of its bytes in elements. + let tiny = doc(""); + assert!(index_aepx(tiny.as_bytes(), Limits::default()).unwrap().nodes.capacity() <= tiny.len() / 3); + } + + /// What a closed element drops is gone from the buffer and the chunk list, but was counted. + #[test] + fn dropped_content_leaves_nothing_behind_but_still_counts() { + // `bdata` wins over the children: they are read, counted and dropped. + let x = doc("xyzq"); + let d = index_aepx(x.as_bytes(), Limits::default()).unwrap(); + assert_eq!((d.buffer.as_slice(), d.nodes.len()), (&[1u8][..], 1)); + assert_eq!(index_aepx(x.as_bytes(), Limits { max_depth: 64, max_chunks: 4 }).err(), Some(AepError::TooManyChunks)); + assert!(index_aepx(x.as_bytes(), Limits { max_depth: 64, max_chunks: 5 }).is_ok()); + // A child's error is raised although the child would be dropped. + assert!(matches!(index_aepx(doc("").as_bytes(), Limits::default()), Err(AepError::Xml { .. }))); + // Only the first XMP packet stays in the buffer. + let packets = doc(&"packet".repeat(1000)); + let d = index_aepx(packets.as_bytes(), Limits::default()).unwrap(); + assert_eq!((d.buffer.as_slice(), d.trailer), (&b"packet"[..], (0, 6))); + // A number keeps its 4 or 8 bytes, not its text. + let d = index_aepx(doc(" 7 1.5").as_bytes(), Limits::default()).unwrap(); + assert_eq!(d.buffer, [&[0, 0, 0, 7][..], &1.5f64.to_be_bytes()[..]].concat()); + assert_eq!(d.nodes.len(), 2); + } + + /// Bodies keep what the element says they are, even where reading the same bytes as an `.aep` + /// would decide otherwise: a nested id whose `bdata` is a chunk sequence stays data, an empty + /// `btdk` stays an opaque list, an empty list at the deepest level stays a list. + #[test] + fn bodies_keep_the_kind_the_element_gives_them() { + let x = doc(""); + let AepxDrafts { buffer, nodes, trailer } = index_aepx(x.as_bytes(), Limits::default()).unwrap(); + let index = TreeIndex::from_drafts(&buffer, nodes, trailer); + let top: Vec<_> = index.chunks().collect(); + assert_eq!(top.len(), 3); + assert_eq!(top[0].bytes().map(<[u8]>::len), Some(11)); + assert!(top[0].children().next().is_none()); + assert!(top[1].is_opaque_list() && top[1].bytes() == Some(&[][..])); + assert!(top[2].is_opaque_list() && top[2].bytes().map(<[u8]>::len) == Some(12)); + // 64 levels: the innermost (empty) list is a list, not opaque. + let deep = format!("{}{}", "".repeat(64), "".repeat(64)); + let AepxDrafts { buffer, nodes, trailer } = index_aepx(deep.as_bytes(), Limits::default()).unwrap(); + let index = TreeIndex::from_drafts(&buffer, nodes, trailer); + assert_eq!(index.walk().map(|(d, _)| d).max(), Some(64)); + assert!(index.walk().all(|(_, n)| n.is_list(b"Fold") && !n.is_opaque_list())); + same(x.as_bytes(), Limits::default()); + same(deep.as_bytes(), Limits::default()); + } + + /// Hostile input is refused by both readers with the same errors. + #[test] + fn hostile_input_is_refused_alike_by_both_readers() { + let hostile = [ + format!("{}{}", "".repeat(100_000), "".repeat(100_000)), + format!("{}", "".repeat(1000)), + doc(&format!("", "00".repeat(100_000))), + doc(&format!("", "00".repeat(100_000))), + doc("4294967296"), + doc("1e400x"), + doc("�"), + doc("�"), + doc(&format!("{}", "".repeat(1000))), + format!("{}", "".repeat(10)), + " = + entries.filter_map(Result::ok).map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "aepx")).collect(); + paths.sort(); + for path in paths { + let bytes = std::fs::read(&path).unwrap(); + for l in limit_sets() { + let index = index_aepx(&bytes, l).map(|d| TreeIndex::from_drafts(&d.buffer, d.nodes, d.trailer).to_file().into_owned()); + assert_eq!(index, read_aepx_with_limits(&bytes, l), "{}", path.display()); + } + } + } } diff --git a/crates/aep/src/aepx/tree.rs b/crates/aep/src/aepx/tree.rs new file mode 100644 index 000000000..6c0bc8435 --- /dev/null +++ b/crates/aep/src/aepx/tree.rs @@ -0,0 +1,362 @@ +//! The `.aepx` reader of the owned chunk tree ([`super::read_aepx`]): it builds the tree element by +//! element. The importer uses the index reader in `aepx.rs` instead; the tests compare the two on +//! generated, damaged and corpus input (the same tree and the same errors). + +use std::borrow::Cow; + +use super::{FILE_REFERENCE, OPAQUE_LIST_TYPES, ROOT, STRING, XMP, alias_json, chunk_id, decode_entities, decode_hex, xml}; +use crate::chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS}; +use crate::error::AepError; +use crate::read::Limits; + +/// [`super::read_aepx_with_limits`]. +pub(super) fn read(data: &[u8], limits: Limits) -> Result, AepError> { + let text = match std::str::from_utf8(data) { + Ok(t) => t.as_bytes(), + Err(e) => return Err(xml(e.valid_up_to(), "the file is not valid UTF-8")), + }; + Reader { data: text, pos: 0, limits, count: 0, stack: Vec::new(), top: None, trailer: None }.run() +} + +/// What an open element becomes when it closes. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Kind { + /// The root element: its children are the top-level chunks. + Root, + /// A chunk with this id (or a `LIST` with this list type). + Chunk([u8; 4]), + /// A `Utf8` chunk. + Utf8, + /// An `alas` chunk built from the attributes. + Alias, + /// The XMP packet (only directly below the root). + Xmp, + /// An element without a chunk counterpart (a name that is no chunk id); skipped with its content. + Skip, +} + +struct Frame { + kind: Kind, + /// Offset of the start tag (for errors). + at: usize, + /// The name as written, to match the end tag. + name: (usize, usize), + /// The decoded `bdata` attribute. + bdata: Option>, + /// The attributes of a `fileReference`, in document order. + attrs: Vec<(String, String)>, + /// Collected text (only for `string` and the XMP packet). + text: Vec, + children: Vec>, +} + +struct Reader<'d> { + data: &'d [u8], + pos: usize, + limits: Limits, + count: usize, + stack: Vec, + /// The top-level chunks once the root element has closed. + top: Option>>, + trailer: Option>, +} + +impl Reader<'_> { + fn run(mut self) -> Result, AepError> { + if self.data.starts_with(&[0xef, 0xbb, 0xbf]) { + self.pos = 3; + } + while self.pos < self.data.len() { + if self.rest().starts_with(b"<") { + self.markup()?; + } else { + self.text()?; + } + } + if let Some(open) = self.stack.last() { + return Err(xml(open.at, "an element is not closed before the end of the file")); + } + let chunks = self.top.ok_or_else(|| xml(self.data.len(), "no AfterEffectsProject root element"))?; + Ok(AepFile { chunks, trailer: Cow::Owned(self.trailer.unwrap_or_default()) }) + } + + fn rest(&self) -> &[u8] { + self.data.get(self.pos..).unwrap_or(&[]) + } + + /// The offset of `needle` at or after `from`. + fn find(&self, from: usize, needle: &[u8]) -> Option { + let hay = self.data.get(from..)?; + if needle.is_empty() || hay.len() < needle.len() { + return None; + } + let first = *needle.first()?; + let mut i = 0; + while let Some(p) = hay.get(i..)?.iter().position(|b| *b == first) { + let at = i.checked_add(p)?; + if hay.get(at..)?.starts_with(needle) { + return from.checked_add(at); + } + i = at.checked_add(1)?; + } + None + } + + /// Skips to just after `close`, which must follow. + fn skip_past(&mut self, close: &[u8], what: &'static str) -> Result { + let at = self.find(self.pos, close).ok_or(xml(self.pos, what))?; + self.pos = at.saturating_add(close.len()); + Ok(at) + } + + fn markup(&mut self) -> Result<(), AepError> { + let rest = self.rest(); + if rest.starts_with(b"", "a processing instruction is not closed")?; + } else if rest.starts_with(b"", "a comment is not closed")?; + } else if rest.starts_with(b"", "a CDATA section is not closed")?; + let content = self.data.get(start..end).unwrap_or(&[]); + let at = start; + match self.stack.last_mut() { + Some(frame) => { + if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) { + frame.text.extend_from_slice(content); + } + } + None => return Err(xml(at, "CDATA outside the root element")), + } + } else if rest.starts_with(b" bool { + !(b.is_ascii_whitespace() || matches!(b, b'<' | b'>' | b'/' | b'=' | b'"' | b'\'' | b'&')) + } + + fn name(&mut self) -> Result<(usize, usize), AepError> { + let start = self.pos; + let len = self.rest().iter().position(|b| !Self::is_name_byte(*b)).unwrap_or(self.rest().len()); + if len == 0 { + return Err(xml(start, "a tag or attribute has no name")); + } + self.pos = start.saturating_add(len); + Ok((start, self.pos)) + } + + fn skip_ws(&mut self) { + let n = self.rest().iter().position(|b| !b.is_ascii_whitespace()).unwrap_or(self.rest().len()); + self.pos = self.pos.saturating_add(n); + } + + fn slice(&self, (a, b): (usize, usize)) -> &[u8] { + self.data.get(a..b).unwrap_or(&[]) + } + + fn start_tag(&mut self) -> Result<(), AepError> { + let at = self.pos; + self.pos = self.pos.saturating_add(1); + let name = self.name()?; + let mut attrs = Vec::new(); + let self_closing = loop { + self.skip_ws(); + let rest = self.rest(); + if rest.starts_with(b"/>") { + self.pos = self.pos.saturating_add(2); + break true; + } + if rest.starts_with(b">") { + self.pos = self.pos.saturating_add(1); + break false; + } + if rest.is_empty() { + return Err(xml(at, "a start tag is not closed")); + } + let key = self.name()?; + self.skip_ws(); + if !self.rest().starts_with(b"=") { + return Err(xml(self.pos, "an attribute has no value")); + } + self.pos = self.pos.saturating_add(1); + self.skip_ws(); + let quote = *self.rest().first().ok_or(xml(self.pos, "a start tag is not closed"))?; + if quote != b'"' && quote != b'\'' { + return Err(xml(self.pos, "an attribute value is not quoted")); + } + let vstart = self.pos.saturating_add(1); + let vend = self.find(vstart, &[quote]).ok_or(xml(vstart, "an attribute value is not closed"))?; + let raw = self.data.get(vstart..vend).unwrap_or(&[]); + if raw.contains(&b'<') { + return Err(xml(vstart, "`<` inside an attribute value")); + } + // A value without `&` is its own decoding (most of them, every `bdata` After Effects + // writes): borrow it instead of copying it. + let value = if raw.contains(&b'&') { + let mut value = Vec::new(); + decode_entities(raw, vstart, &mut value)?; + Cow::Owned(value) + } else { + Cow::Borrowed(raw) + }; + self.pos = vend.saturating_add(1); + attrs.push((key, value)); + }; + let kind = self.kind_of(name, at)?; + if kind != Kind::Root { + self.count = self.count.saturating_add(1); + if self.count > self.limits.max_chunks { + return Err(AepError::TooManyChunks); + } + // The root's children are depth 1, like the top-level chunks of an `.aep`. + if self.stack.len() > self.limits.max_depth { + return Err(AepError::TooDeep); + } + } + let mut frame = Frame { kind, at, name, bdata: None, attrs: Vec::new(), text: Vec::new(), children: Vec::new() }; + if matches!(kind, Kind::Chunk(_)) { + if let Some((key, value)) = attrs.iter().find(|(k, _)| self.slice(*k) == b"bdata") { + frame.bdata = Some(decode_hex(value, key.1)?); + } + } else if kind == Kind::Alias { + frame.attrs = + attrs.iter().map(|(k, v)| (String::from_utf8_lossy(self.slice(*k)).into_owned(), String::from_utf8_lossy(v).into_owned())).collect(); + } + self.stack.push(frame); + if self_closing { + self.close()?; + } + Ok(()) + } + + /// Classifies an element by its name and where it is. + fn kind_of(&self, name: (usize, usize), at: usize) -> Result { + let n = self.slice(name); + let parent = match self.stack.last() { + None => { + if self.top.is_some() { + return Err(xml(at, "a second root element")); + } + if n != ROOT { + return Err(xml(at, "the root element is not AfterEffectsProject")); + } + return Ok(Kind::Root); + } + Some(p) => p.kind, + }; + Ok(match parent { + Kind::Skip | Kind::Utf8 | Kind::Xmp | Kind::Alias => Kind::Skip, + Kind::Root | Kind::Chunk(_) => { + if n == STRING { + Kind::Utf8 + } else if n == FILE_REFERENCE { + Kind::Alias + } else if n == XMP && parent == Kind::Root { + Kind::Xmp + } else if let Some(id) = chunk_id(n) { + Kind::Chunk(id) + } else { + Kind::Skip + } + } + }) + } + + fn end_tag(&mut self) -> Result<(), AepError> { + let at = self.pos; + self.pos = self.pos.saturating_add(2); + let name = self.name()?; + self.skip_ws(); + if !self.rest().starts_with(b">") { + return Err(xml(at, "an end tag is not closed")); + } + self.pos = self.pos.saturating_add(1); + let open = self.stack.last().ok_or(xml(at, "an end tag without a start tag"))?; + if self.slice(open.name) != self.slice(name) { + return Err(xml(at, "an end tag does not match its start tag")); + } + self.close() + } + + /// Closes the innermost element and hands its chunk to the parent. + fn close(&mut self) -> Result<(), AepError> { + let frame = self.stack.pop().ok_or(xml(self.pos, "an end tag without a start tag"))?; + let chunk = match frame.kind { + Kind::Root => { + self.top = Some(frame.children); + return Ok(()); + } + Kind::Skip => return Ok(()), + Kind::Xmp => { + if self.trailer.is_none() { + self.trailer = Some(frame.text); + } + return Ok(()); + } + Kind::Utf8 => Chunk::data(*b"Utf8", frame.text), + Kind::Alias => Chunk::data(*b"alas", alias_json(&frame.attrs).into_bytes()), + Kind::Chunk(id) => chunk_of(id, frame)?, + }; + if let Some(parent) = self.stack.last_mut() { + parent.children.push(chunk); + } + Ok(()) + } + + fn text(&mut self) -> Result<(), AepError> { + let start = self.pos; + let len = self.rest().iter().position(|b| *b == b'<').unwrap_or(self.rest().len()); + let end = start.saturating_add(len); + self.pos = end; + let raw = self.data.get(start..end).unwrap_or(&[]); + match self.stack.last_mut() { + Some(frame) if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) => decode_entities(raw, start, &mut frame.text), + Some(_) => Ok(()), + None if raw.iter().all(u8::is_ascii_whitespace) => Ok(()), + None => Err(xml(start, "text outside the root element")), + } + } +} + +/// The chunk of a closed element named by a chunk id. +fn chunk_of(id: [u8; 4], frame: Frame) -> Result, AepError> { + if let Some(bytes) = frame.bdata { + if OPAQUE_LIST_TYPES.contains(&id) { + let pad = if bytes.len() % 2 == 1 { Some(0) } else { None }; + return Ok(Chunk { id: LIST, body: Body::OpaqueList { list_type: id, data: Cow::Owned(bytes) }, pad }); + } + return Ok(Chunk::data(id, bytes)); + } + // Numbers written as text: `0` and `150.000000000`. + if &id == b"numS" || &id == b"ppSn" { + let text = match frame.children.as_slice() { + [only] if only.is(b"Utf8") => only.bytes().unwrap_or(&[]), + _ => return Err(xml(frame.at, "a numS or ppSn element without its number")), + }; + let text = std::str::from_utf8(text).map_err(|_| xml(frame.at, "a number is not text"))?.trim(); + let body = if &id == b"numS" { + // Only `0` is sampled, so whether the field is signed is open: accept both readings of 4 bytes. + let n = text.parse::().ok().filter(|n| (i64::from(i32::MIN)..=i64::from(u32::MAX)).contains(n)); + let n = n.ok_or(xml(frame.at, "numS is not a 32-bit integer"))?; + (n as u32).to_be_bytes().to_vec() + } else { + text.parse::().map_err(|_| xml(frame.at, "ppSn is not a number"))?.to_be_bytes().to_vec() + }; + return Ok(Chunk::data(id, body)); + } + if NESTED_IDS.contains(&id) { + if frame.children.is_empty() { + return Ok(Chunk::data(id, Vec::new())); + } + return Ok(Chunk { id, body: Body::Chunks(frame.children), pad: None }); + } + Ok(Chunk::list(id, frame.children)) +} diff --git a/crates/aep/src/arbitrary.rs b/crates/aep/src/arbitrary.rs index 45768ae44..70627139b 100644 --- a/crates/aep/src/arbitrary.rs +++ b/crates/aep/src/arbitrary.rs @@ -7,9 +7,9 @@ //! Hue/Saturation (444-446) or a Levels value (512-516) changes only bytes of this `aRbp` (and stale //! UI bytes of `pard`); the visible parameters of the same names keep no record. -use crate::Chunk; use crate::bytes::{be_f32, be_i32, be_u16, be_u32}; use crate::effects::Effect; +use crate::index::Node; impl<'f, 'a> Effect<'f, 'a> { /// The arbitrary data stored for the parameter with this match name (`-NNNN`): the @@ -17,13 +17,13 @@ impl<'f, 'a> Effect<'f, 'a> { /// none. pub fn arbitrary_data(&self, match_name: &str) -> Option<&'f [u8]> { let values = self.chunk().child_list(b"tdgp")?; - let kids = values.children(); - let at = kids.iter().position(|c| c.is(b"tdmn") && c.bytes().is_some_and(|b| name_is(b, match_name)))?; - // The value record, then the data list (no match name of its own). - let rest = kids.get(at.checked_add(1)?..)?; - let mut it = rest.iter().take(3); + let mut kids = values.children(); + // Up to the parameter's match name, then the value record, then the data list (no match + // name of its own). + kids.find(|c| c.is(b"tdmn") && c.bytes().is_some_and(|b| name_is(b, match_name)))?; + let mut it = kids.take(3); let _record = it.next().filter(|c| c.is_list(b"tdbs"))?; - it.find(|c| c.is_list(b"aRbs")).and_then(|l| l.child(b"aRbp")).and_then(Chunk::bytes) + it.find(|c| c.is_list(b"aRbs")).and_then(|l| l.child(b"aRbp")).and_then(Node::bytes) } } diff --git a/crates/aep/src/camera.rs b/crates/aep/src/camera.rs index 69cd8c2c8..934f36bd6 100644 --- a/crates/aep/src/camera.rs +++ b/crates/aep/src/camera.rs @@ -16,7 +16,6 @@ //! only when it is not the default 100. use crate::typed::{Layer, PropNode}; -use crate::values::{Tracked, ValueSource, read, read_opt}; /// The Position of a camera or light layer as stored. #[derive(Debug, Clone, Copy, PartialEq)] @@ -28,16 +27,6 @@ pub struct CameraPosition { pub auto_state: bool, } -impl CameraPosition { - /// The position as the batch-3 notes had After Effects report it: in the auto state of a - /// two-node camera (`two_node`) x and y are the composition centre; otherwise the stored - /// value. **Contradicted by 256 and 537 (session S16): After Effects reports and renders the - /// stored value**; use `stored` for what After Effects shows. - pub fn reported(&self, comp_width: u32, comp_height: u32, two_node: bool) -> [f64; 3] { - if self.auto_state && two_node { [f64::from(comp_width) / 2.0, f64::from(comp_height) / 2.0, self.stored[2]] } else { self.stored } - } -} - impl<'f, 'a> Layer<'f, 'a> { /// The Position record of a camera or light layer. pub fn camera_position(&self) -> Option { @@ -49,87 +38,9 @@ impl<'f, 'a> Layer<'f, 'a> { let derived = h.get(76) == Some(&1) && h.get(77) == Some(&1) && position.expression().is_none(); Some(CameraPosition { stored, auto_state: derived }) } - - /// The point of interest of a camera in pixels (its Anchor Point record); `None` when absent. - pub fn camera_point_of_interest(&self) -> Option<[f64; 3]> { - let Some(PropNode::Group(transform)) = self.properties()?.get("ADBE Transform Group") else { return None }; - let tracked = read(&transform, "ADBE Anchor Point", [0.0; 3], 1.0); - matches!(tracked.source, ValueSource::Stored | ValueSource::Keyframed).then_some(tracked.value) - } - - /// A light's intensity in percent; the default 100 when the property is absent. - pub fn light_intensity(&self) -> Option> { - let root = self.properties()?; - let Some(PropNode::Group(options)) = root.get("ADBE Light Options Group") else { return None }; - let t = read(&options, "ADBE Light Intensity", [100.0], 1.0); - Some(Tracked { value: t.value[0], source: t.source }) - } } /// The camera zoom After Effects uses when `ADBE Camera Zoom` is absent: 2666.67 px for the 50 mm /// default camera in the 1920 px wide corpus composition. Whether it follows other composition /// widths was not sampled. pub const DEFAULT_CAMERA_ZOOM: f64 = 2_666.666_666_666_666_5; - -impl<'f, 'a> Layer<'f, 'a> { - fn camera_options(&self) -> Option> { - match self.properties()?.get("ADBE Camera Options Group") { - Some(PropNode::Group(g)) => Some(g), - _ => None, - } - } - - fn light_options(&self) -> Option> { - match self.properties()?.get("ADBE Light Options Group") { - Some(PropNode::Group(g)) => Some(g), - _ => None, - } - } - - /// A camera's zoom in pixels (`ADBE Camera Options Group/ADBE Camera Zoom`); absent = the - /// default, [`DEFAULT_CAMERA_ZOOM`] (341: 2000 is stored). The auto Position keeps `-2666.67` and - /// is not recomputed from the zoom. - pub fn camera_zoom(&self) -> Option> { - let t = read(&self.camera_options()?, "ADBE Camera Zoom", [DEFAULT_CAMERA_ZOOM], 1.0); - Some(Tracked { value: t.value[0], source: t.source }) - } - - /// The depth of field switch (`ADBE Camera Depth of Field`, a checkbox; off by default, 342). - pub fn camera_depth_of_field(&self) -> Option> { - let t = read(&self.camera_options()?, "ADBE Camera Depth of Field", [0.0], 1.0); - Some(Tracked { value: t.value[0] != 0.0, source: t.source }) - } - - /// A camera's focus distance in pixels (`ADBE Camera Focus Distance`); `None` while absent (its - /// default is not known). - pub fn camera_focus_distance(&self) -> Option> { - let t = read_opt::<1>(&self.camera_options()?, "ADBE Camera Focus Distance", 1.0)?; - Some(Tracked { value: t.value[0], source: t.source }) - } - - /// A camera's aperture (`ADBE Camera Aperture`); `None` while absent (its default is not known). - pub fn camera_aperture(&self) -> Option> { - let t = read_opt::<1>(&self.camera_options()?, "ADBE Camera Aperture", 1.0)?; - Some(Tracked { value: t.value[0], source: t.source }) - } - - /// A light's colour as `[A, R, G, B]` fractions (`ADBE Light Options Group/ADBE Light Color`, - /// stored as doubles 0..255); white when absent (343 stores red). - pub fn light_color(&self) -> Option> { - Some(read(&self.light_options()?, "ADBE Light Color", [1.0; 4], 1.0 / 255.0)) - } - - /// A spot light's cone angle in degrees (`ADBE Light Cone Angle`); 90 when absent (344 stores 60). - /// A point light stores 90 with `tdsb` 3. - pub fn light_cone_angle(&self) -> Option> { - let t = read(&self.light_options()?, "ADBE Light Cone Angle", [90.0], 1.0); - Some(Tracked { value: t.value[0], source: t.source }) - } - - /// A spot light's cone feather in percent (`ADBE Light Cone Feather 2`); 50 when absent (345 - /// stores 30). - pub fn light_cone_feather(&self) -> Option> { - let t = read(&self.light_options()?, "ADBE Light Cone Feather 2", [50.0], 1.0); - Some(Tracked { value: t.value[0], source: t.source }) - } -} diff --git a/crates/aep/src/effects.rs b/crates/aep/src/effects.rs index 25b7051ab..65f3c6125 100644 --- a/crates/aep/src/effects.rs +++ b/crates/aep/src/effects.rs @@ -20,8 +20,8 @@ use std::collections::{HashMap, HashSet}; -use crate::Chunk; use crate::bytes::{array, be_f32, be_f64, be_i32, be_u16, be_u32, cstr, text, until_nul}; +use crate::index::Node; use crate::typed::{KeyList, Layer, Project, PropGroup, PropNode, Property}; /// The parameter type, the `u32` at byte 12 of `pard`. The numbering matches the plug-in @@ -132,7 +132,7 @@ pub enum LayerRef { #[derive(Debug, Clone)] pub struct Effect<'f, 'a> { match_name: String, - chunk: &'f Chunk<'a>, + chunk: Node<'f, 'a>, } /// Where the parameter definitions of an effect instance come from. @@ -178,13 +178,13 @@ impl<'f, 'a> Effect<'f, 'a> { } /// The underlying `LIST sspc` chunk. - pub fn chunk(&self) -> &'f Chunk<'a> { + pub fn chunk(&self) -> Node<'f, 'a> { self.chunk } /// The instance name shown in the effect controls (`fnam`). pub fn name(&self) -> Option { - self.chunk.child(b"fnam")?.children().iter().find(|c| c.is(b"Utf8"))?.bytes().map(text) + self.chunk.child(b"fnam")?.child(b"Utf8")?.bytes().map(text) } /// The name the user gave the instance: the `tdsn` of the instance's value group when it is @@ -192,7 +192,7 @@ impl<'f, 'a> Effect<'f, 'a> { /// `None` while the instance shows the effect's name. pub fn user_name(&self) -> Option { let values = self.chunk.child_list(b"tdgp")?; - let name = values.child(b"tdsn")?.children().iter().find(|c| c.is(b"Utf8"))?.bytes().map(text)?; + let name = values.child(b"tdsn")?.child(b"Utf8")?.bytes().map(text)?; (name != "-_0_/-" && !name.is_empty()).then_some(name) } @@ -250,7 +250,7 @@ impl<'f, 'a> Effect<'f, 'a> { } /// The parameters described by `part` (a `LIST parT`) with the values of this instance. - fn params_from(&self, part: &'f Chunk<'a>, borrowed: bool) -> Vec> { + fn params_from(&self, part: Node<'f, 'a>, borrowed: bool) -> Vec> { let mut out: Vec> = Vec::new(); // The stored values by match name: the first entry of a name, as `PropGroup::get` finds it // (a group of that name means no stored value). Indexed once: a lookup per parameter @@ -271,8 +271,8 @@ impl<'f, 'a> Effect<'f, 'a> { out.push(EffectParam { index: out.len(), match_name, - kind: ParamKind::from_u32(be_u32(pard, 12).unwrap_or(u32::MAX)), - name: cstr(pard, 16, 32).unwrap_or_default(), + kind: Pard(pard).kind(), + name: Pard(pard).name().unwrap_or_default(), items: None, borrowed_definition: borrowed, pard, @@ -280,7 +280,7 @@ impl<'f, 'a> Effect<'f, 'a> { }); } else if c.is(b"pdnm") && let Some(last) = out.last_mut() - && let Some(u) = c.children().iter().find(|u| u.is(b"Utf8")) + && let Some(u) = c.child(b"Utf8") && let Some(b) = u.bytes() // An empty `pdnm` names nothing (some writers store one: the effect // supplies its items). @@ -293,8 +293,9 @@ impl<'f, 'a> Effect<'f, 'a> { } } -fn has_pard(part: &Chunk<'_>) -> bool { - part.children().iter().any(|c| c.is(b"pard")) +/// True when a `LIST parT` holds parameter definitions (a `pard`). +fn has_pard(part: Node<'_, '_>) -> bool { + part.children().any(|c| c.is(b"pard")) } impl<'f, 'a> Project<'f, 'a> { @@ -302,18 +303,18 @@ impl<'f, 'a> Project<'f, 'a> { /// name and the `LIST parT` of the copied instance, in file order (the stack order of the /// types, 365/366). The `EfDC` count before them is little-endian and is not needed to read /// them. A type whose copy has no definitions is left out. - pub fn effect_definitions(&self) -> Vec<(String, &'f Chunk<'a>)> { - let mut out: Vec<(String, &'f Chunk<'a>)> = Vec::new(); + pub fn effect_definitions(&self) -> Vec<(String, Node<'f, 'a>)> { + let mut out: Vec<(String, Node<'f, 'a>)> = Vec::new(); let Some(defs) = self.effect_defs else { return out }; let mut seen: HashSet = HashSet::new(); - for df in defs.children().iter().filter(|c| c.is_list(b"EfDf")) { + for df in defs.children().filter(|c| c.is_list(b"EfDf")) { let mut name: Option = None; for c in df.children() { if c.is(b"tdmn") { name = c.bytes().map(|b| text(until_nul(b))); } else if c.is_list(b"sspc") && let Some(n) = name.take() - && let Some(part) = c.child_list(b"parT").filter(|p| has_pard(p)) + && let Some(part) = c.child_list(b"parT").filter(|p| has_pard(*p)) && seen.insert(n.clone()) { out.push((n, part)); @@ -328,8 +329,8 @@ impl<'f, 'a> Project<'f, 'a> { /// For a type without a project-level copy it also keeps the own `parT` of the first instance /// in the file (items, layers and effects in file order) that has one. pub fn effect_definition_index(&self) -> EffectDefinitions<'f, 'a> { - let by_name: HashMap> = self.effect_definitions().into_iter().collect(); - let mut instances: HashMap> = HashMap::new(); + let by_name: HashMap> = self.effect_definitions().into_iter().collect(); + let mut instances: HashMap> = HashMap::new(); for item in self.all_items() { let Some(comp) = item.comp() else { continue }; for layer in comp.layers() { @@ -337,7 +338,7 @@ impl<'f, 'a> Project<'f, 'a> { if by_name.contains_key(&fx.match_name) || instances.contains_key(&fx.match_name) { continue; } - if let Some(part) = fx.chunk.child_list(b"parT").filter(|p| has_pard(p)) { + if let Some(part) = fx.chunk.child_list(b"parT").filter(|p| has_pard(*p)) { instances.insert(fx.match_name, part); } } @@ -363,9 +364,9 @@ impl<'f, 'a> Project<'f, 'a> { #[derive(Debug, Clone, Default)] pub struct EffectDefinitions<'f, 'a> { /// The project-level copies (`EfdG`). - by_name: HashMap>, + by_name: HashMap>, /// For a type without a project-level copy: the own `parT` of the first instance that has one. - instances: HashMap>, + instances: HashMap>, } impl<'f, 'a> EffectDefinitions<'f, 'a> { @@ -392,7 +393,7 @@ impl<'f, 'a> EffectDefinitions<'f, 'a> { return effect.params(); } match self.by_name.get(effect.match_name()).or_else(|| self.instances.get(effect.match_name())) { - Some(part) => effect.params_from(part, true), + Some(part) => effect.params_from(*part, true), None => Vec::new(), } } @@ -484,26 +485,7 @@ impl<'f, 'a> EffectParam<'f, 'a> { pub fn pard_default(&self) -> Option { match self.kind { ParamKind::Popup if self.borrowed_definition => None, - ParamKind::FloatSlider => be_f64(self.pard, 56).map(ParamValue::Number), - ParamKind::FixedSlider | ParamKind::Angle => be_i32(self.pard, 56).map(|v| ParamValue::Number(f64::from(v) / 65536.0)), - ParamKind::IntSlider => be_i32(self.pard, 56).map(|v| ParamValue::Number(f64::from(v))), - ParamKind::Checkbox => be_u32(self.pard, 56).map(|v| ParamValue::Bool(v != 0)), - ParamKind::Popup => be_u32(self.pard, 56).map(ParamValue::Index), - ParamKind::Point => { - let p = [f64::from(be_i32(self.pard, 56)?) / 65536.0, f64::from(be_i32(self.pard, 60)?) / 65536.0]; - // A fresh instance stores fractions of the layer (0.5, 0.5); After Effects' save of - // a project it opened stores percent (50, 50: the re-saves of read-back tests e1 and - // e2, where Gradient Ramp's Start and End and every other point default came back - // as 50 / 0 and 50 / 100). Point - // defaults lie inside the layer, so a component beyond 1 tells percent. - let percent = p.iter().any(|c| c.abs() > 1.0 + 1e-9); - Some(ParamValue::Point(if percent { p.map(|c| c / 100.0) } else { p })) - } - ParamKind::Color => { - let [a, r, g, b] = array::<4>(self.pard, 56)?.map(|c| f64::from(c) / 255.0); - Some(ParamValue::Argb([a, r, g, b])) - } - _ => None, + kind => Pard(self.pard).default(kind), } } @@ -557,22 +539,14 @@ impl<'f, 'a> EffectParam<'f, 'a> { /// slider differs per parameter (Amount 37.5 % is stored 37.5, Blend 25 % is stored 0.25), so /// this is what tells a reader the scale. `None` for other types. pub fn valid_max(&self) -> Option { - match self.kind { - ParamKind::FixedSlider => be_i32(self.pard, 128).map(|v| f64::from(v) / 65536.0), - ParamKind::FloatSlider => be_f32(self.pard, 108).map(f64::from), - ParamKind::IntSlider => be_i32(self.pard, 128).map(f64::from), - _ => None, - } + Pard(self.pard).valid_max(self.kind) } /// The smallest valid value of a float slider, the `f32` at byte 104 of `pard` (Exposure /// -100, Offset -2, Gamma Correction 0.1: 389). `None` for other types (a fixed slider's /// minimum is not decoded). pub fn valid_min(&self) -> Option { - match self.kind { - ParamKind::FloatSlider => be_f32(self.pard, 104).map(f64::from), - _ => None, - } + Pard(self.pard).valid_min(self.kind) } /// The range of a float slider's slider (what the control's drag covers, inside the valid @@ -581,7 +555,7 @@ impl<'f, 'a> EffectParam<'f, 'a> { /// for other types. pub fn slider_range(&self) -> Option<(f64, f64)> { match self.kind { - ParamKind::FloatSlider => Some((f64::from(be_f32(self.pard, 112)?), f64::from(be_f32(self.pard, 116)?))), + ParamKind::FloatSlider => Pard(self.pard).slider_range(self.kind), _ => None, } } @@ -591,7 +565,7 @@ impl<'f, 'a> EffectParam<'f, 'a> { if self.kind != ParamKind::Popup { return None; } - be_u16(self.pard, 60) + Pard(self.pard).popup_item_count() } /// For a popup: the selected item index (1-based): the stored selection, else the default. @@ -619,6 +593,93 @@ impl<'f, 'a> EffectParam<'f, 'a> { } } +/// A parameter definition (`pard`, 148 bytes; aep-format.md 3.7): the one decoder of its fields. +/// The import reads them through [`EffectParam`]; the export writes them from its effect table and +/// its tests read them back through this. +#[derive(Clone, Copy, Debug)] +pub struct Pard<'f>(pub &'f [u8]); + +impl Pard<'_> { + /// The type code, the `u32` at byte 12. + pub fn code(self) -> Option { + be_u32(self.0, 12) + } + + /// The parameter type (an unreadable code is [`ParamKind::Other`]). + pub fn kind(self) -> ParamKind { + ParamKind::from_u32(self.code().unwrap_or(u32::MAX)) + } + + /// The parameter name at byte 16 (32 bytes). + pub fn name(self) -> Option { + cstr(self.0, 16, 32) + } + + /// The default at byte 56 as a parameter of type `kind` reads it (see + /// [`EffectParam::pard_default`]). + pub fn default(self, kind: ParamKind) -> Option { + let p = self.0; + match kind { + ParamKind::FloatSlider => be_f64(p, 56).map(ParamValue::Number), + ParamKind::FixedSlider | ParamKind::Angle => be_i32(p, 56).map(|v| ParamValue::Number(f64::from(v) / 65536.0)), + ParamKind::IntSlider => be_i32(p, 56).map(|v| ParamValue::Number(f64::from(v))), + ParamKind::Checkbox => be_u32(p, 56).map(|v| ParamValue::Bool(v != 0)), + ParamKind::Popup => be_u32(p, 56).map(ParamValue::Index), + ParamKind::Point => { + let pt = [f64::from(be_i32(p, 56)?) / 65536.0, f64::from(be_i32(p, 60)?) / 65536.0]; + // A fresh instance stores fractions of the layer (0.5, 0.5); After Effects' save of + // a project it opened stores percent (50, 50: the re-saves of read-back tests e1 and + // e2, where Gradient Ramp's Start and End and every other point default came back + // as 50 / 0 and 50 / 100). Point + // defaults lie inside the layer, so a component beyond 1 tells percent. + let percent = pt.iter().any(|c| c.abs() > 1.0 + 1e-9); + Some(ParamValue::Point(if percent { pt.map(|c| c / 100.0) } else { pt })) + } + ParamKind::Color => { + let [a, r, g, b] = array::<4>(p, 56)?.map(|c| f64::from(c) / 255.0); + Some(ParamValue::Argb([a, r, g, b])) + } + _ => None, + } + } + + /// The valid maximum of a slider (see [`EffectParam::valid_max`]). + pub fn valid_max(self, kind: ParamKind) -> Option { + match kind { + ParamKind::FixedSlider => be_i32(self.0, 128).map(|v| f64::from(v) / 65536.0), + ParamKind::FloatSlider => be_f32(self.0, 108).map(f64::from), + ParamKind::IntSlider => be_i32(self.0, 128).map(f64::from), + _ => None, + } + } + + /// The valid minimum of a float slider (see [`EffectParam::valid_min`]). + pub fn valid_min(self, kind: ParamKind) -> Option { + match kind { + ParamKind::FloatSlider => be_f32(self.0, 104).map(f64::from), + _ => None, + } + } + + /// A slider's range, the one its value records repeat as `tdum` / `tduM`: a float slider's + /// `f32` pair at 112 / 116, a fixed slider's 16.16 pair and an integer slider's `i32` pair at + /// 132 / 136. + pub fn slider_range(self, kind: ParamKind) -> Option<(f64, f64)> { + let p = self.0; + match kind { + ParamKind::FloatSlider => Some((f64::from(be_f32(p, 112)?), f64::from(be_f32(p, 116)?))), + ParamKind::FixedSlider => Some((f64::from(be_i32(p, 132)?) / 65536.0, f64::from(be_i32(p, 136)?) / 65536.0)), + ParamKind::IntSlider => Some((f64::from(be_i32(p, 132)?), f64::from(be_i32(p, 136)?))), + _ => None, + } + } + + /// A popup's item count, the `u16` at byte 60. + pub fn popup_item_count(self) -> Option { + be_u16(self.0, 60) + } +} + impl<'f, 'a> Layer<'f, 'a> { /// The effects on the layer, in stack order (`ADBE Effect Parade`). pub fn effects(&self) -> Vec> { @@ -638,6 +699,30 @@ impl<'f, 'a> Layer<'f, 'a> { #[cfg(test)] mod tests { use super::*; + use crate::Chunk; + + #[test] + fn a_parameter_definition_reads_its_fields_and_nothing_from_a_cut_record() { + let mut b = vec![0u8; 148]; + b[12..16].copy_from_slice(&7u32.to_be_bytes()); + b[16..22].copy_from_slice(b"Amount"); + b[56..64].copy_from_slice(&2.5f64.to_be_bytes()); + let p = Pard(&b); + assert_eq!((p.code(), p.name().as_deref()), (Some(7), Some("Amount"))); + assert_eq!(p.default(ParamKind::FloatSlider), Some(ParamValue::Number(2.5))); + // The same bytes as a checkbox and as a popup: the `u32` at 56, and the `u16` at 60. + b[56..60].copy_from_slice(&1u32.to_be_bytes()); + b[60..62].copy_from_slice(&3u16.to_be_bytes()); + let p = Pard(&b); + assert_eq!(p.default(ParamKind::Checkbox), Some(ParamValue::Bool(true))); + assert_eq!((p.default(ParamKind::Popup), p.popup_item_count()), (Some(ParamValue::Index(1)), Some(3))); + assert_eq!(p.default(ParamKind::IntSlider), Some(ParamValue::Number(1.0))); + // A record cut before a field has no value for it, and an unreadable code is `Other`. + let cut = Pard(&b[..12]); + assert_eq!((cut.code(), cut.kind(), cut.name(), cut.popup_item_count()), (None, ParamKind::Other(u32::MAX), None, None)); + assert_eq!(cut.default(ParamKind::FloatSlider), None); + assert_eq!(Pard(&b[..58]).default(ParamKind::Checkbox), None); + } fn tdmn(name: &str) -> Chunk<'static> { let mut b = name.as_bytes().to_vec(); @@ -670,11 +755,11 @@ mod tests { ("A".to_string(), Chunk::list(*b"tdbs", vec![Chunk::data(*b"cdat", vec![0u8; 8])])), ]; let chunk = instance(&names, &stored); - let fx = Effect { match_name: "X".into(), chunk: &chunk }; + let fx = Effect { match_name: "X".into(), chunk: Node::from(&chunk) }; let params = fx.params(); assert_eq!(params.iter().map(|p| p.match_name.as_str()).collect::>(), ["A", "B", "C"]); let first = params.first().and_then(|p| p.stored).expect("A stored"); - assert!(first.chunk.children().is_empty(), "the first A (the second has a cdat)"); + assert!(first.chunk.children().next().is_none(), "the first A (the second has a cdat)"); assert!(params.get(1).is_some_and(|p| p.stored.is_none()), "a group of that name is no stored value"); assert!(params.get(2).is_some_and(|p| p.stored.is_none())); } @@ -687,7 +772,7 @@ mod tests { let names: Vec = (0..n).map(|i| format!("P{i}")).collect(); let stored: Vec<(String, Chunk<'static>)> = names.iter().map(|m| (m.clone(), tdbs())).collect(); let chunk = instance(&names, &stored); - let fx = Effect { match_name: "X".into(), chunk: &chunk }; + let fx = Effect { match_name: "X".into(), chunk: Node::from(&chunk) }; let params = fx.params(); assert_eq!(params.len(), n); assert!(params.iter().all(|p| p.stored.is_some())); @@ -707,19 +792,19 @@ mod tests { } let efdg = Chunk::list(*b"EfdG", defs); let fold = Chunk::list(*b"Fold", Vec::new()); - let project = Project { fold: &fold, effect_defs: Some(&efdg) }; + let project = Project { fold: Node::from(&fold), effect_defs: Some(Node::from(&efdg)) }; let all = project.effect_definitions(); assert_eq!(all.len(), n); let index = project.effect_definition_index(); let bare = Chunk::list(*b"sspc", vec![Chunk::list(*b"parT", Vec::new())]); for i in (0..n).step_by(50) { - let fx = Effect { match_name: format!("FX{i}"), chunk: &bare }; + let fx = Effect { match_name: format!("FX{i}"), chunk: Node::from(&bare) }; assert_eq!(index.source(&fx), DefinitionSource::Project); let params = index.params(&fx); assert_eq!(params.iter().map(|p| p.match_name.as_str()).collect::>(), ["R0"], "the first copy's definitions"); assert!(params.iter().all(|p| p.borrowed_definition)); } - let unknown = Effect { match_name: "nope".into(), chunk: &bare }; + let unknown = Effect { match_name: "nope".into(), chunk: Node::from(&bare) }; assert_eq!(index.source(&unknown), DefinitionSource::Missing); assert!(index.params(&unknown).is_empty()); assert_eq!(project.definition_source(&unknown), DefinitionSource::Missing); diff --git a/crates/aep/src/footage.rs b/crates/aep/src/footage.rs index 58a653a4a..e8c89b924 100644 --- a/crates/aep/src/footage.rs +++ b/crates/aep/src/footage.rs @@ -2,8 +2,8 @@ //! of a layer's source. use crate::bytes::{be_f32, be_f64, be_i32, be_u32, cstr, le_u32, text, until_nul}; -use crate::chunk::Chunk; use crate::header::Rational; +use crate::index::Node; use crate::typed::{Item, ItemKind, Layer, Project}; /// The footage data of an item (`LIST Pin ` with `sspc`, `opti` and, for files, `Als2/alas`). @@ -101,21 +101,6 @@ impl PsdLayer { } impl Footage { - /// The number of frames of an image sequence (`duration * native_frame_rate`: 3/30 s at 30 fps is - /// 3); `None` for anything else. - pub fn sequence_frame_count(&self) -> Option { - if !self.is_sequence { - return None; - } - let d = self.duration?; - let frames = i128::from(d.num).checked_mul(i128::from(self.native_frame_rate))?; - let den = i128::from(d.den); - if den <= 0 || frames % den != 0 { - return None; - } - u32::try_from(frames / den).ok() - } - /// The solid's colour as fractions R, G, B. pub fn solid_rgb(&self) -> Option<[f32; 3]> { let [_, r, g, b] = self.solid_argb?; @@ -269,14 +254,14 @@ impl SequenceInfo { /// is unreadable (empty). A name that is not a plain file name (holding `/`, `\`, `:` or a control /// character such as NUL, or being `.` or `..`) makes the whole list unreadable (empty): the names /// are joined to the folder. -fn sequence_file_list(list: &Chunk<'_>) -> Vec { +fn sequence_file_list(list: Node<'_, '_>) -> Vec { // After Effects writes the count first (the one sample); a list without a readable count is // not read rather than guessed. let Some(count) = list.child(b"StVS").and_then(|c| c.bytes()).and_then(|b| le_u32(b, 0)).and_then(|n| usize::try_from(n).ok()) else { return Vec::new(); }; let names: Vec = - list.children().iter().filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).take(count).map(text).filter(|n| !n.is_empty()).collect(); + list.children().filter(|c| c.is(b"Utf8")).filter_map(Node::bytes).take(count).map(text).filter(|n| !n.is_empty()).collect(); if names.iter().any(|n| n.contains(|c: char| matches!(c, '/' | '\\' | ':') || c.is_control()) || n == "." || n == "..") { return Vec::new(); } @@ -308,9 +293,8 @@ impl<'f, 'a> Item<'f, 'a> { } let pin = self.chunk.child_list(b"Pin ")?; let sspc = pin.child(b"sspc")?.bytes()?; - let kids = pin.children(); - let after = kids.iter().position(|c| c.is_list(b"Als2"))?; - let naming = || kids.iter().skip(after.saturating_add(1)).take_while(|c| !c.is(b"opti")); + let after = pin.children().position(|c| c.is_list(b"Als2"))?; + let naming = || pin.children().skip(after.saturating_add(1)).take_while(|c| !c.is(b"opti")); let (start, end, digits) = (be_u32(sspc, 172)?, be_u32(sspc, 176)?, be_u32(sspc, 180)?); // Force alphabetical order: the file list replaces the numbering. if let Some(list) = naming().find(|c| c.is_list(b"StVc")) { @@ -320,7 +304,7 @@ impl<'f, 'a> Item<'f, 'a> { } return Some(SequenceInfo { prefix: String::new(), suffix: String::new(), start, end, digits, files }); } - let names: Vec = naming().filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).map(text).take(2).collect(); + let names: Vec = naming().filter(|c| c.is(b"Utf8")).filter_map(Node::bytes).map(text).take(2).collect(); let (prefix, suffix) = match names.as_slice() { [prefix, suffix] => (prefix.clone(), suffix.clone()), // An empty prefix is not stored (`0001.png` has only `.png`). @@ -350,12 +334,6 @@ impl<'f, 'a> Item<'f, 'a> { self.chunk.child(b"idta")?.bytes()?.get(58).copied() } - /// The ids of the compositions this item uses as precomp layers: the little-endian `u32` of - /// each `idpi` chunk. - pub fn used_item_ids(&self) -> Vec { - self.chunk.children().iter().filter(|c| c.is(b"idpi")).filter_map(|c| le_u32(c.bytes()?, 0)).collect() - } - /// The footage data of a footage item. pub fn footage(&self) -> Option { if self.kind()? != ItemKind::Footage { @@ -405,11 +383,6 @@ impl<'f, 'a> Project<'f, 'a> { self.item_by_id(layer.header()?.source_id?) } - /// The name a layer shows: its own name, or its source's name when it has none. - pub fn layer_display_name(&self, layer: &Layer<'f, 'a>) -> Option { - layer.name().or_else(|| self.layer_source(layer)?.display_name()) - } - /// The pixel size of a layer's source: a footage item's width and height, or a precomp's /// composition size. pub fn layer_source_size(&self, layer: &Layer<'f, 'a>) -> Option<(u32, u32)> { diff --git a/crates/aep/src/header.rs b/crates/aep/src/header.rs index 8593a5633..ac869095f 100644 --- a/crates/aep/src/header.rs +++ b/crates/aep/src/header.rs @@ -237,12 +237,6 @@ impl LayerHeader { self.flag_bytes.get(byte).is_some_and(|b| b & mask != 0) } - /// Byte 37 bit `0x01`: the layer has a name set by the user (clear on a default-named camera or - /// light and on a precomp layer, whose name is the source's). - pub fn name_is_user_set(&self) -> bool { - self.flag(0, 0x01) - } - /// Byte 37 bit `0x02`: guide layer. pub fn is_guide(&self) -> bool { self.flag(0, 0x02) @@ -333,17 +327,6 @@ impl LayerHeader { Rational::new(i64::from(self.stretch), i64::from(self.stretch_den)) } - /// The time stretch in percent (`stretch / stretch_den * 100`); a zero denominator reads as 1. - pub fn stretch_percent(&self) -> f64 { - let den = if self.stretch_den == 0 { 1.0 } else { f64::from(self.stretch_den) }; - f64::from(self.stretch) / den * 100.0 - } - - /// The start time in seconds, `None` for a zero denominator. - pub fn start_seconds(&self) -> Option { - self.start.to_f64() - } - /// The absolute in point, `start + in * stretch`, exact and reduced. pub fn in_point(&self) -> Option { self.start.plus_scaled(&self.in_relative, &self.stretch_ratio()?) @@ -355,16 +338,6 @@ impl LayerHeader { pub fn out_point(&self) -> Option { self.start.plus_scaled(&self.out_relative, &self.stretch_ratio()?) } - - /// The absolute in point in seconds. - pub fn in_point_seconds(&self) -> Option { - self.in_point()?.to_f64() - } - - /// The absolute out point in seconds. - pub fn out_point_seconds(&self) -> Option { - self.out_point()?.to_f64() - } } impl<'f, 'a> Layer<'f, 'a> { @@ -464,64 +437,10 @@ impl CompHeader { self.time_base.frame_rate() } - /// The frame rate from the 16.16 fixed-point field (25 is `0x0019_0000`). - pub fn frame_rate_from_fixed(&self) -> f64 { - f64::from(self.frame_rate_fixed) / 65536.0 - } - /// The duration in seconds. pub fn duration_seconds(&self) -> Option { self.time_base.seconds(i32::try_from(self.duration_units).ok()?) } - - /// The work area start in seconds. - pub fn work_area_start_seconds(&self) -> Option { - self.time_base.seconds(i32::try_from(self.work_area_start_units).ok()?) - } - - /// The work area end in seconds; the composition duration when the work area is the whole - /// composition. - pub fn work_area_end_seconds(&self) -> Option { - match self.work_area_end_units { - Some(end) => self.time_base.seconds(i32::try_from(end).ok()?), - None => self.duration_seconds(), - } - } - - /// The background colour as fractions 0..1 (`byte / 255`). - pub fn background_rgb(&self) -> [f64; 3] { - self.background.map(|c| f64::from(c) / 255.0) - } - - /// The pixel aspect ratio as a number. - pub fn pixel_aspect_ratio(&self) -> Option { - self.pixel_aspect.to_f64() - } - - /// The display start time in seconds (the start timecode), `None` when absent or the - /// denominator is zero. - pub fn display_start_seconds(&self) -> Option { - self.display_start?.to_f64() - } - - /// The display start as a frame number (`display start * frame rate`, rounded); 100 at 25 fps - /// for a display start of 4 s. - pub fn display_start_frame(&self) -> Option { - let rate = self.frame_rate()?; - Some((self.display_start_seconds()? * rate).round() as i64) - } - - /// The frame rate as an exact fraction `timescale / frame_duration`, as typed: 29.97 fps is - /// `23976 / 800` (not `30000 / 1001`), 23.976 fps is `23976 / 1000`. - pub fn frame_rate_exact(&self) -> Option { - Rational::new(i64::from(self.time_base.timescale), i64::from(self.time_base.frame_duration)) - } - - /// The duration in whole frames (`duration / frame_duration`). After Effects snaps a duration - /// to a whole number of frames: 5 s at 29.97 fps becomes 150 frames (5.005 s). - pub fn duration_frames(&self) -> Option { - self.duration_units.checked_div(self.time_base.frame_duration) - } } impl<'f, 'a> Comp<'f, 'a> { diff --git a/crates/aep/src/index.rs b/crates/aep/src/index.rs new file mode 100644 index 000000000..76d5430da --- /dev/null +++ b/crates/aep/src/index.rs @@ -0,0 +1,1026 @@ +//! A read-only, flat index of the chunk tree for readers that only look (the importer). +//! +//! [`TreeIndex`] holds every chunk of a file in one `Vec` in depth-first order (a chunk, then its +//! whole subtree, then its next sibling). Each entry keeps its id, list type, pad byte, the bytes of +//! its body (borrowed from the input) and the index one past its subtree, so: +//! - a chunk's subtree is the index range `at + 1 .. end`, +//! - its direct children are found by stepping from `at + 1` to each child's `end`, +//! - the whole tree is one array, grown a few times while it is built and freed at once; it never +//! holds more slots than the input has room for chunk headers or the chunk limit allows. +//! +//! [`crate::parse`] builds the owned [`Chunk`] tree instead (one `Vec` per list), which +//! the exporter and editors need. [`TreeIndex::to_file`] and [`Node::to_chunk`] convert the index +//! into that tree where an owned one is needed. +//! +//! [`TreeIndex::parse_with_limits`] walks the input exactly as the [`Chunk`] parser does: the same +//! chunks are counted against [`Limits::max_chunks`] in the same order, the same lists are kept +//! opaque at [`Limits::max_depth`] or when damaged, and the same errors come back. A test compares +//! the two on the corpus and on generated and damaged trees. Two errors are its own, and both are +//! stricter than the [`Chunk`] parser (which would abort or could not address the tree): +//! [`AepError::TooLarge`] when the array cannot grow (a failed `try_reserve`), and +//! [`AepError::TooManyChunks`] for a tree of more than `u32::MAX` chunks. +//! +//! An `.aepx` is indexed by its own reader (`container/aepx.rs`), which decodes every body into +//! one buffer and fills the entries as it reads; the index then borrows that buffer +//! (`TreeIndex::from_drafts`). It gives the tree [`crate::read_aepx`] gives. +//! +//! [`Node`] is the handle the typed view ([`crate::Project`] and the types below it) +//! reads through: a chunk of either tree. The view works the same on both. + +use std::borrow::Cow; +use std::fmt; + +use crate::bytes::be_u32; +use crate::chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS, Walk}; +use crate::error::AepError; +use crate::read::{Limits, container_end, id4}; + +/// What an entry's body is (the [`Body`] variants). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Kind { + Data, + /// [`Body::Chunks`]. + Nested, + List, + OpaqueList, +} + +/// One chunk of a [`TreeIndex`]. +#[derive(Debug, Clone, Copy)] +struct Entry<'a> { + /// The body bytes of a data chunk, or the bytes after the list type of an opaque list; empty + /// for a list or nested chunk (their content is their children). + data: &'a [u8], + id: [u8; 4], + /// The list type of a `LIST` (parsed or opaque); zero otherwise. + list_type: [u8; 4], + /// One past the last entry of this chunk's subtree. + end: u32, + kind: Kind, + pad: Option, +} + +impl<'a> Entry<'a> { + /// An entry for a chunk whose body is not read yet. + fn placeholder(id: [u8; 4]) -> Self { + Self { data: &[], id, list_type: [0; 4], end: 0, kind: Kind::Data, pad: None } + } +} + +/// An entry of an index whose bytes are still being written (the `.aepx` reader decodes into a +/// buffer that grows, so it keeps positions, not slices): the body is `start..start + len` of +/// that buffer. The fields are those of [`Entry`]; [`TreeIndex::from_drafts`] turns the list +/// into an index once the buffer is final. +#[derive(Debug, Clone, Copy)] +pub struct Draft { + pub(crate) start: usize, + pub(crate) len: usize, + pub(crate) id: [u8; 4], + pub(crate) list_type: [u8; 4], + pub(crate) end: u32, + pub(crate) kind: Kind, + pub(crate) pad: Option, +} + +impl Draft { + /// A data chunk with no body yet. + pub(crate) fn placeholder(id: [u8; 4]) -> Self { + Self { start: 0, len: 0, id, list_type: [0; 4], end: 0, kind: Kind::Data, pad: None } + } +} + +/// The chunk tree of a file as one flat, read-only array (see the module documentation). +#[derive(Clone)] +pub struct TreeIndex<'a> { + nodes: Vec>, + trailer: &'a [u8], +} + +impl fmt::Debug for TreeIndex<'_> { + /// The sizes only: the chunk count and the trailer length. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("TreeIndex").field("chunks", &self.nodes.len()).field("trailer", &self.trailer.len()).finish() + } +} + +/// An index that does not fit a `u32` (more than 4 billion chunks) is refused as too many chunks. +fn index_u32(at: usize) -> Result { + u32::try_from(at).map_err(|_| AepError::TooManyChunks) +} + +impl<'a> TreeIndex<'a> { + /// Indexes an `.aep` with the default [`Limits`]. + /// + /// # Errors + /// The errors of [`crate::parse`], for the same inputs. + pub fn parse(data: &'a [u8]) -> Result { + Self::parse_with_limits(data, Limits::default()) + } + + /// Indexes an `.aep` with explicit [`Limits`]. + /// + /// # Errors + /// The errors of [`crate::parse_with_limits`], for the same inputs and limits; also + /// [`AepError::TooManyChunks`] for a tree of more than `u32::MAX` chunks and + /// [`AepError::TooLarge`] when the array cannot be allocated. + pub fn parse_with_limits(data: &'a [u8], limits: Limits) -> Result { + let end = container_end(data)?; + // Every chunk header takes 8 bytes of its own and is counted once, so the tree never has + // more chunks than this. + let bound = (end.saturating_sub(12) / 8).min(limits.max_chunks); + let mut builder = Builder { data, limits, count: 0, nodes: Vec::new(), bound }; + // A small start; [`Builder::push`] doubles it as chunks arrive, never past the bound, so + // the array stays proportional to the chunk count (a file of a few large chunks gets a few + // slots, not one per 32 bytes). `try_reserve_exact` so it fails nothing here. + let _ = builder.nodes.try_reserve_exact(bound.min(INITIAL_SLOTS)); + builder.sequence(12, end, 1)?; + let trailer = data.get(end..).unwrap_or(&[]); + Ok(Self { nodes: builder.nodes, trailer }) + } + + /// The index of the entries `drafts` (in depth-first order, see [`Draft`]) whose bodies and + /// trailer are ranges of `buffer`. A range outside `buffer` reads as empty. The list is turned + /// into entries; a [`Draft`] and an [`Entry`] have the same size and alignment, so the standard + /// library reuses the list's allocation for them. + pub fn from_drafts(buffer: &'a [u8], drafts: Vec, trailer: (usize, usize)) -> Self { + let slice = |start: usize, len: usize| start.checked_add(len).and_then(|end| buffer.get(start..end)).unwrap_or(&[]); + let nodes = drafts + .into_iter() + .map(|d| Entry { data: slice(d.start, d.len), id: d.id, list_type: d.list_type, end: d.end, kind: d.kind, pad: d.pad }) + .collect(); + let trailer = buffer.get(trailer.0..trailer.1).unwrap_or(&[]); + Self { nodes, trailer } + } + + /// Indexes an already parsed (or built) chunk tree. The index borrows the file's bytes. + /// + /// # Errors + /// [`AepError::TooManyChunks`] for a tree of more than `u32::MAX` chunks. + pub fn from_file(file: &'a AepFile<'_>) -> Result { + // Sized once, to the chunks the tree holds. + let mut nodes: Vec> = Vec::new(); + nodes.try_reserve_exact(file.walk().count()).map_err(|_| AepError::TooLarge)?; + // Each frame: the siblings still to visit and the entry whose subtree they fill. + let mut stack: Vec<(std::slice::Iter<'a, Chunk<'_>>, Option)> = vec![(file.chunks.iter(), None)]; + while let Some((iter, parent)) = stack.last_mut() { + let parent = *parent; + match iter.next() { + Some(chunk) => { + let at = nodes.len(); + // The entry after this one must still have a `u32` index. + let next = index_u32(at.checked_add(1).ok_or(AepError::TooManyChunks)?)?; + let (kind, data, list_type) = match &chunk.body { + Body::Data(d) => (Kind::Data, &d[..], [0; 4]), + Body::Chunks(_) => (Kind::Nested, &[][..], [0; 4]), + Body::List { list_type, .. } => (Kind::List, &[][..], *list_type), + Body::OpaqueList { list_type, data } => (Kind::OpaqueList, &data[..], *list_type), + }; + nodes.push(Entry { data, id: chunk.id, list_type, end: next, kind, pad: chunk.pad }); + let kids = chunk.children(); + if !kids.is_empty() { + stack.push((kids.iter(), Some(at))); + } + } + None => { + stack.pop(); + if let Some(p) = parent { + let end = index_u32(nodes.len())?; + if let Some(e) = nodes.get_mut(p) { + e.end = end; + } + } + } + } + } + Ok(Self { nodes, trailer: &file.trailer[..] }) + } + + /// The number of chunks in the tree (at every depth). + pub fn len(&self) -> usize { + self.nodes.len() + } + + /// True when the container holds no chunks. + pub fn is_empty(&self) -> bool { + self.nodes.is_empty() + } + + /// The bytes after the container's declared end (see [`AepFile::trailer`]). + pub fn trailer(&self) -> &'a [u8] { + self.trailer + } + + /// The top-level chunks. + pub fn chunks(&self) -> Nodes<'_, 'a> { + Nodes(NodesRepr::Index { tree: self, next: 0, end: u32::try_from(self.nodes.len()).unwrap_or(u32::MAX) }) + } + + /// The first top-level chunk with this id. + pub fn chunk(&self, id: &[u8; 4]) -> Option> { + self.chunks().find(|c| c.is(id)) + } + + /// The first top-level `LIST` of this type. + pub fn list(&self, list_type: &[u8; 4]) -> Option> { + self.chunks().find(|c| c.is_list(list_type)) + } + + /// Visits every chunk depth-first with its nesting depth (the top level is 1), as + /// [`AepFile::walk`] does. + pub fn walk(&self) -> NodeWalk<'_, 'a> { + NodeWalk(WalkRepr::Index { tree: self, next: 0, end: u32::try_from(self.nodes.len()).unwrap_or(u32::MAX), open: Vec::new() }) + } + + /// The owned chunk tree (the bodies stay borrowed from the input): what + /// [`crate::parse`] gives for the same file. + pub fn to_file(&self) -> AepFile<'a> { + AepFile { chunks: self.build(0, u32::try_from(self.nodes.len()).unwrap_or(u32::MAX)), trailer: Cow::Borrowed(self.trailer) } + } + + /// The entry at `at`, `None` past the end. + fn entry(&self, at: u32) -> Option<&Entry<'a>> { + self.nodes.get(usize::try_from(at).ok()?) + } + + /// The end of the subtree at `at`; never before `at + 1`, so walks always move on. + fn end_of(&self, at: u32) -> u32 { + self.entry(at).map_or(u32::MAX, |e| e.end).max(at.saturating_add(1)) + } + + /// The owned chunks for the siblings that start at `next` and fill `next..end`, without + /// recursion. + fn build(&self, next: u32, end: u32) -> Vec> { + struct Frame<'a> { + /// The list or nested chunk being filled; `None` for the outermost range. + at: Option, + kids: Vec>, + next: u32, + end: u32, + } + let mut stack = vec![Frame { at: None, kids: Vec::new(), next, end }]; + loop { + let Some(top) = stack.last_mut() else { return Vec::new() }; + if top.next < top.end { + let at = top.next; + top.next = self.end_of(at); + let Some(e) = self.entry(at).copied() else { + top.next = top.end; + continue; + }; + match e.kind { + Kind::List | Kind::Nested => { + let sub_end = self.end_of(at); + stack.push(Frame { at: Some(at), kids: Vec::new(), next: at.saturating_add(1), end: sub_end }); + } + Kind::Data => top.kids.push(Chunk { id: e.id, body: Body::Data(Cow::Borrowed(e.data)), pad: e.pad }), + Kind::OpaqueList => { + top.kids.push(Chunk { id: e.id, body: Body::OpaqueList { list_type: e.list_type, data: Cow::Borrowed(e.data) }, pad: e.pad }); + } + } + } else { + let Some(frame) = stack.pop() else { return Vec::new() }; + let Some(at) = frame.at else { return frame.kids }; + let Some(e) = self.entry(at).copied() else { continue }; + let body = match e.kind { + Kind::Nested => Body::Chunks(frame.kids), + Kind::List | Kind::Data | Kind::OpaqueList => Body::List { list_type: e.list_type, children: frame.kids }, + }; + let chunk = Chunk { id: e.id, body, pad: e.pad }; + match stack.last_mut() { + Some(parent) => parent.kids.push(chunk), + None => return vec![chunk], + } + } + } + } +} + +/// The slots a [`TreeIndex`] starts with (32 KiB); it doubles from there. +const INITIAL_SLOTS: usize = 1024; + +/// Builds a [`TreeIndex`]; mirrors `read::Parser` step for step (see the module documentation). +struct Builder<'a> { + data: &'a [u8], + limits: Limits, + count: usize, + nodes: Vec>, + /// The most chunks the input can hold under the limits: `nodes` never reserves past it. + bound: usize, +} + +impl<'a> Builder<'a> { + /// Appends an entry. A full array grows by its length (at least 16), but never past + /// [`Self::bound`]; an allocation the target cannot make is an error, not an abort. + fn push(&mut self, entry: Entry<'a>) -> Result<(), AepError> { + let len = self.nodes.len(); + if len == self.nodes.capacity() { + let more = len.max(16).min(self.bound.saturating_sub(len)).max(1); + self.nodes.try_reserve_exact(more).map_err(|_| AepError::TooLarge)?; + } + self.nodes.push(entry); + Ok(()) + } + + /// Appends the sibling chunks that exactly fill `start..end`, each followed by its subtree. + fn sequence(&mut self, start: usize, end: usize, depth: usize) -> Result<(), AepError> { + let mut pos = start; + while pos < end { + let short = AepError::Malformed { offset: pos, what: "bytes left over that are too short for a chunk header" }; + let body_start = pos.checked_add(8).filter(|b| *b <= end).ok_or(short)?; + let id = id4(self.data, pos).ok_or(AepError::Malformed { offset: pos, what: "short read" })?; + let size = be_u32(self.data, pos + 4).ok_or(AepError::Malformed { offset: pos, what: "short read" })? as usize; + let avail = end - body_start; + if size > avail { + return Err(AepError::ChunkTooLarge { offset: pos, size: size as u64, available: avail as u64 }); + } + self.count = self.count.saturating_add(1); + if self.count > self.limits.max_chunks { + return Err(AepError::TooManyChunks); + } + let at = self.nodes.len(); + // This entry and the one after it must have `u32` indices. + index_u32(at.checked_add(1).ok_or(AepError::TooManyChunks)?)?; + self.push(Entry::placeholder(id))?; + let body_end = body_start + size; + self.body(at, id, body_start, body_end, depth)?; + let mut next = body_end; + let mut pad = None; + if size % 2 == 1 && next < end { + pad = Some(self.data.get(next).copied().ok_or(AepError::Malformed { offset: next, what: "missing pad byte" })?); + next += 1; + } + let subtree_end = index_u32(self.nodes.len())?; + if let Some(e) = self.nodes.get_mut(at) { + e.pad = pad; + e.end = subtree_end; + } + pos = next; + } + Ok(()) + } + + /// Like [`Self::sequence`] but a structural problem yields `false` and removes what the + /// attempt appended (the caller keeps bytes). The chunk count keeps the attempt's chunks, as + /// the [`Chunk`] parser's does. + fn tentative(&mut self, start: usize, end: usize, depth: usize) -> Result { + let mark = self.nodes.len(); + match self.sequence(start, end, depth) { + Ok(()) => Ok(true), + Err(e) if e.is_structural() => { + self.nodes.truncate(mark); + Ok(false) + } + Err(e) => Err(e), + } + } + + /// The input bytes `start..end` (empty when out of range). + fn slice(&self, start: usize, end: usize) -> &'a [u8] { + self.data.get(start..end).unwrap_or(&[]) + } + + /// Fills in the body of the entry at `at` once the parser knows what it is. + fn set(&mut self, at: usize, kind: Kind, data: &'a [u8], list_type: [u8; 4]) { + if let Some(e) = self.nodes.get_mut(at) { + e.kind = kind; + e.data = data; + e.list_type = list_type; + } + } + + /// Fills in the entry at `at` for the body `start..end` and appends its subtree. + fn body(&mut self, at: usize, id: [u8; 4], start: usize, end: usize, depth: usize) -> Result<(), AepError> { + let len = end - start; + if id == LIST && len >= 4 { + let list_type = id4(self.data, start).ok_or(AepError::Malformed { offset: start, what: "short read" })?; + if depth < self.limits.max_depth && self.tentative(start + 4, end, depth + 1)? { + self.set(at, Kind::List, &[], list_type); + } else { + let data = self.slice(start + 4, end); + self.set(at, Kind::OpaqueList, data, list_type); + } + return Ok(()); + } + if NESTED_IDS.contains(&id) && len >= 8 && depth < self.limits.max_depth { + let mark = self.nodes.len(); + if self.tentative(start, end, depth + 1)? && self.nodes.len() > mark { + self.set(at, Kind::Nested, &[], [0; 4]); + return Ok(()); + } + } + let data = self.slice(start, end); + self.set(at, Kind::Data, data, [0; 4]); + Ok(()) + } +} + +/// A chunk of either tree: an owned [`Chunk`] or an entry of a [`TreeIndex`]. The typed view +/// reads through it, so it works the same on both. Cheap to copy. +#[derive(Clone, Copy)] +pub struct Node<'f, 'a>(NodeRepr<'f, 'a>); + +#[derive(Clone, Copy)] +enum NodeRepr<'f, 'a> { + Chunk(&'f Chunk<'a>), + Index(&'f TreeIndex<'a>, u32), +} + +impl<'f, 'a> From<&'f Chunk<'a>> for Node<'f, 'a> { + /// The handle for this value. + fn from(chunk: &'f Chunk<'a>) -> Self { + Node(NodeRepr::Chunk(chunk)) + } +} + +/// Prints the chunk as [`Chunk`]'s `Debug` does, whichever tree it is in, without copying the +/// subtree. +impl fmt::Debug for Node<'_, '_> { + /// The chunk as `Chunk`'s `Debug` prints it. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.0 { + NodeRepr::Chunk(c) => fmt::Debug::fmt(c, f), + NodeRepr::Index(..) => { + f.debug_struct("Chunk").field("id", &self.id()).field("body", &BodyDebug(*self)).field("pad", &self.pad()).finish() + } + } + } +} + +/// The body of an index [`Node`], printed as [`Body`]'s `Debug` prints it. +struct BodyDebug<'f, 'a>(Node<'f, 'a>); + +/// The children of a [`Node`], printed as a list. +struct ChildrenDebug<'f, 'a>(Node<'f, 'a>); + +impl fmt::Debug for BodyDebug<'_, '_> { + /// The body as `Body`'s `Debug` prints it. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Some(e) = self.0.entry() else { return f.write_str("?") }; + match e.kind { + Kind::Data => f.debug_tuple("Data").field(&e.data).finish(), + Kind::Nested => f.debug_tuple("Chunks").field(&ChildrenDebug(self.0)).finish(), + Kind::List => f.debug_struct("List").field("list_type", &e.list_type).field("children", &ChildrenDebug(self.0)).finish(), + Kind::OpaqueList => f.debug_struct("OpaqueList").field("list_type", &e.list_type).field("data", &e.data).finish(), + } + } +} + +impl fmt::Debug for ChildrenDebug<'_, '_> { + /// The children as a list. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list().entries(self.0.children()).finish() + } +} + +impl<'f, 'a> Node<'f, 'a> { + /// The entry at `at`, `None` past the end. + fn entry(self) -> Option<&'f Entry<'a>> { + match self.0 { + NodeRepr::Index(tree, at) => tree.entry(at), + NodeRepr::Chunk(_) => None, + } + } + + /// The [`Chunk`] when this node is one of an owned tree; `None` for an index entry (see + /// [`Self::to_chunk`]). + pub fn as_chunk(self) -> Option<&'f Chunk<'a>> { + match self.0 { + NodeRepr::Chunk(c) => Some(c), + NodeRepr::Index(..) => None, + } + } + + /// The chunk and its subtree as an owned [`Chunk`] (bodies stay borrowed). + pub fn to_chunk(self) -> Chunk<'a> { + match self.0 { + NodeRepr::Chunk(c) => c.clone(), + NodeRepr::Index(tree, at) => tree.build(at, tree.end_of(at)).into_iter().next().unwrap_or_else(|| Chunk::data(self.id(), &[][..])), + } + } + + /// The 4-byte id (see [`Chunk::id`]). + pub fn id(self) -> [u8; 4] { + match self.0 { + NodeRepr::Chunk(c) => c.id, + NodeRepr::Index(..) => self.entry().map_or([0; 4], |e| e.id), + } + } + + /// The pad byte (see [`Chunk::pad`]). + pub fn pad(self) -> Option { + match self.0 { + NodeRepr::Chunk(c) => c.pad, + NodeRepr::Index(..) => self.entry().and_then(|e| e.pad), + } + } + + /// The raw bytes of a data chunk, or the bytes after the list type of an opaque list (see + /// [`Chunk::bytes`]). + pub fn bytes(self) -> Option<&'f [u8]> { + match self.0 { + NodeRepr::Chunk(c) => c.bytes(), + NodeRepr::Index(..) => self.entry().filter(|e| matches!(e.kind, Kind::Data | Kind::OpaqueList)).map(|e| e.data), + } + } + + /// The child chunks of a list or nested chunk; none for data. + pub fn children(self) -> Nodes<'f, 'a> { + match self.0 { + NodeRepr::Chunk(c) => Nodes(NodesRepr::Slice(c.children().iter())), + NodeRepr::Index(tree, at) => { + let end = match tree.entry(at) { + Some(e) if matches!(e.kind, Kind::List | Kind::Nested) => tree.end_of(at), + _ => at.saturating_add(1), + }; + Nodes(NodesRepr::Index { tree, next: at.saturating_add(1), end }) + } + } + } + + /// The list type when this is a `LIST` (parsed or opaque). + pub fn list_type(self) -> Option<[u8; 4]> { + match self.0 { + NodeRepr::Chunk(c) => c.list_type(), + NodeRepr::Index(..) => self.entry().filter(|e| matches!(e.kind, Kind::List | Kind::OpaqueList)).map(|e| e.list_type), + } + } + + /// True when this is a `LIST` of the given type. + pub fn is_list(self, list_type: &[u8; 4]) -> bool { + match self.0 { + NodeRepr::Chunk(c) => c.is_list(list_type), + NodeRepr::Index(..) => { + self.entry().is_some_and(|e| e.id == LIST && matches!(e.kind, Kind::List | Kind::OpaqueList) && &e.list_type == list_type) + } + } + } + + /// True when this is a `LIST` whose content could not be read as chunks. + pub fn is_opaque_list(self) -> bool { + match self.0 { + NodeRepr::Chunk(c) => c.is_opaque_list(), + NodeRepr::Index(..) => self.entry().is_some_and(|e| e.kind == Kind::OpaqueList), + } + } + + /// True when this chunk has the given id. + pub fn is(self, id: &[u8; 4]) -> bool { + self.id() == *id + } + + /// The first child chunk with this id. + pub fn child(self, id: &[u8; 4]) -> Option { + self.children().find(|c| c.is(id)) + } + + /// The first child `LIST` of this type. + pub fn child_list(self, list_type: &[u8; 4]) -> Option { + self.children().find(|c| c.is_list(list_type)) + } + + /// Every chunk below this one depth-first (children before later siblings), with the depth + /// counted from 1 for the children of this chunk. + pub fn descendants(self) -> NodeWalk<'f, 'a> { + match self.0 { + NodeRepr::Chunk(c) => NodeWalk(WalkRepr::Chunk(c.descendants())), + NodeRepr::Index(tree, at) => { + let end = match tree.entry(at) { + Some(e) if matches!(e.kind, Kind::List | Kind::Nested) => tree.end_of(at), + _ => at.saturating_add(1), + }; + NodeWalk(WalkRepr::Index { tree, next: at.saturating_add(1), end, open: Vec::new() }) + } + } + } + + /// The first descendant `LIST` of this type, searching depth-first. + pub fn find_list(self, list_type: &[u8; 4]) -> Option { + match self.0 { + NodeRepr::Chunk(c) => c.find_list(list_type).map(Node::from), + // The subtree is a contiguous range in depth-first order: a plain scan. + NodeRepr::Index(tree, at) => { + let first = usize::try_from(at).ok()?.checked_add(1)?; + let end = usize::try_from(tree.end_of(at)).ok()?.min(tree.nodes.len()); + let range = tree.nodes.get(first..end)?; + let i = range.iter().position(|e| e.id == LIST && matches!(e.kind, Kind::List | Kind::OpaqueList) && &e.list_type == list_type)?; + let found = u32::try_from(first.checked_add(i)?).ok()?; + Some(Node(NodeRepr::Index(tree, found))) + } + } + } +} + +/// The children of a [`Node`] (or the top-level chunks of a [`TreeIndex`]), in file order. +#[derive(Clone)] +pub struct Nodes<'f, 'a>(NodesRepr<'f, 'a>); + +#[derive(Clone)] +enum NodesRepr<'f, 'a> { + Slice(std::slice::Iter<'f, Chunk<'a>>), + Index { tree: &'f TreeIndex<'a>, next: u32, end: u32 }, +} + +impl<'f, 'a> Iterator for Nodes<'f, 'a> { + type Item = Node<'f, 'a>; + + /// The next item, `None` at the end of the range. + fn next(&mut self) -> Option { + match &mut self.0 { + NodesRepr::Slice(it) => it.next().map(Node::from), + NodesRepr::Index { tree, next, end } => { + if *next >= *end { + return None; + } + let (tree, at) = (*tree, *next); + tree.entry(at)?; + *next = tree.end_of(at); + Some(Node(NodeRepr::Index(tree, at))) + } + } + } +} + +/// A depth-first walk over [`Node`]s with their depth; see [`Node::descendants`] and +/// [`TreeIndex::walk`]. +pub struct NodeWalk<'f, 'a>(WalkRepr<'f, 'a>); + +enum WalkRepr<'f, 'a> { + Chunk(Walk<'f, 'a>), + /// `open` holds the subtree ends of the entries above the next one (its ancestors inside the + /// walked range); their count is the next entry's depth minus one. + Index { + tree: &'f TreeIndex<'a>, + next: u32, + end: u32, + open: Vec, + }, +} + +impl<'f, 'a> Iterator for NodeWalk<'f, 'a> { + type Item = (usize, Node<'f, 'a>); + + /// The next item, `None` at the end of the range. + fn next(&mut self) -> Option { + match &mut self.0 { + WalkRepr::Chunk(w) => w.next().map(|(d, c)| (d, Node::from(c))), + WalkRepr::Index { tree, next, end, open } => { + if *next >= *end { + return None; + } + let (tree, at) = (*tree, *next); + tree.entry(at)?; + while open.last().is_some_and(|e| *e <= at) { + open.pop(); + } + let depth = open.len().saturating_add(1); + let sub_end = tree.end_of(at); + if sub_end > at.saturating_add(1) { + open.push(sub_end); + } + *next = at.saturating_add(1); + Some((depth, Node(NodeRepr::Index(tree, at)))) + } + } + } +} + +/// The top level of either tree: what the project settings and the version are read from. +#[derive(Clone, Copy)] +pub enum Root<'f, 'a> { + File(&'f AepFile<'a>), + Index(&'f TreeIndex<'a>), +} + +impl<'f, 'a> From<&'f AepFile<'a>> for Root<'f, 'a> { + /// The handle for this value. + fn from(file: &'f AepFile<'a>) -> Self { + Root::File(file) + } +} + +impl<'f, 'a> From<&'f TreeIndex<'a>> for Root<'f, 'a> { + /// The handle for this value. + fn from(index: &'f TreeIndex<'a>) -> Self { + Root::Index(index) + } +} + +impl<'f, 'a> Root<'f, 'a> { + /// The top-level chunks. + pub fn chunks(self) -> Nodes<'f, 'a> { + match self { + Root::File(f) => Nodes(NodesRepr::Slice(f.chunks.iter())), + Root::Index(t) => t.chunks(), + } + } + + /// The first top-level chunk with this id. + pub fn chunk(self, id: &[u8; 4]) -> Option> { + self.chunks().find(|c| c.is(id)) + } + + /// The first top-level `LIST` of this type. + pub fn list(self, list_type: &[u8; 4]) -> Option> { + self.chunks().find(|c| c.is_list(list_type)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{parse_with_limits, write}; + + /// A tiny deterministic generator (xorshift64*), so the tests need no extra crate. + struct Rng(u64); + + impl Rng { + /// The next pseudo-random number (xorshift64*). + fn next(&mut self) -> u64 { + let mut x = self.0; + x ^= x >> 12; + x ^= x << 25; + x ^= x >> 27; + self.0 = x; + x.wrapping_mul(0x2545_f491_4f6c_dd1d) + } + + /// A number below `n` (0 for `n == 0`). + fn below(&mut self, n: u64) -> u64 { + if n == 0 { 0 } else { self.next() % n } + } + } + + /// A random tree: lists, nested-id chunks holding a `Utf8`, data of odd and even sizes, opaque + /// lists, empty lists, deep chains. + fn tree(rng: &mut Rng, depth: usize, budget: &mut usize) -> Vec> { + let n = rng.below(6); + let mut out = Vec::new(); + for _ in 0..n { + if *budget == 0 { + break; + } + *budget -= 1; + let pick = rng.below(10); + let chunk = if depth < 12 && pick < 4 { + let types: [&[u8; 4]; 4] = [b"Fold", b"tdgp", b"tdbs", b"Layr"]; + let t = *types[usize::try_from(rng.below(4)).expect("small")]; + Chunk::list(t, tree(rng, depth + 1, budget)) + } else if pick == 4 { + let ids: [&[u8; 4]; 3] = [b"tdsn", b"pdnm", b"fnam"]; + let id = *ids[usize::try_from(rng.below(3)).expect("small")]; + let text = vec![b'a'; usize::try_from(rng.below(5)).expect("small")]; + Chunk { id, body: Body::Chunks(vec![Chunk::data(*b"Utf8", text)]), pad: None } + } else if pick == 5 { + let raw = vec![0x5a; usize::try_from(rng.below(9)).expect("small")]; + Chunk { id: LIST, body: Body::OpaqueList { list_type: *b"btdk", data: raw.into() }, pad: None } + } else { + let ids: [&[u8; 4]; 4] = [b"cdat", b"tdmn", b"Utf8", b"idta"]; + let id = *ids[usize::try_from(rng.below(4)).expect("small")]; + Chunk::data(id, vec![u8::try_from(rng.below(256)).expect("byte"); usize::try_from(rng.below(13)).expect("small")]) + }; + out.push(chunk); + } + out + } + + /// Both readers on the same bytes and limits: the same result, errors included; and the + /// index's walk visits the same chunks at the same depths as the tree's. + fn same(bytes: &[u8], limits: Limits) { + let old = parse_with_limits(bytes, limits); + let new = TreeIndex::parse_with_limits(bytes, limits); + match (&old, &new) { + (Ok(file), Ok(index)) => { + assert_eq!(&index.to_file(), file, "the index converts to the parsed tree"); + let a: Vec<(usize, Chunk<'_>)> = file.walk().map(|(d, c)| (d, c.clone())).collect(); + let b: Vec<(usize, Chunk<'_>)> = index.walk().map(|(d, n)| (d, n.to_chunk())).collect(); + assert_eq!(a, b, "the walks agree"); + assert_eq!(index.len(), a.len()); + // Indexing the parsed tree gives the same index. + let again = TreeIndex::from_file(file).expect("index"); + assert_eq!(again.to_file(), *file); + } + (Err(a), Err(b)) => assert_eq!(a, b, "the same error"), + _ => panic!("one reader failed: {:?} vs {:?}", old.as_ref().err(), new.as_ref().err()), + } + } + + /// The default limits and four tight ones (depth 1 and 3, 7 and 40 chunks). + fn limits() -> [Limits; 5] { + [ + Limits::default(), + Limits { max_depth: 1, max_chunks: 10_000_000 }, + Limits { max_depth: 3, max_chunks: 10_000_000 }, + Limits { max_depth: 64, max_chunks: 7 }, + Limits { max_depth: 64, max_chunks: 40 }, + ] + } + + /// The index matches the chunk parser on generated and damaged trees. + #[test] + fn the_index_matches_the_chunk_parser_on_generated_and_damaged_trees() { + let mut rng = Rng(0x9e37_79b9_7f4a_7c15); + for _ in 0..400 { + let mut budget = 120; + let chunks = tree(&mut rng, 0, &mut budget); + let file = AepFile { chunks, trailer: Cow::Borrowed(b"x") }; + let bytes = write(&file).expect("write"); + for l in limits() { + same(&bytes, l); + } + // Damage: overwrite a few bytes past the container header (sizes, ids, list types). + for _ in 0..4 { + let mut bad = bytes.clone(); + for _ in 0..1 + rng.below(3) { + let at = 12 + usize::try_from(rng.below(u64::try_from(bad.len().saturating_sub(12)).expect("len"))).expect("index"); + if let Some(b) = bad.get_mut(at) { + *b = u8::try_from(rng.below(256)).expect("byte"); + } + } + for l in limits() { + same(&bad, l); + } + } + // Truncated inputs (the container size then no longer fits). + let cut = bytes.len().saturating_sub(usize::try_from(rng.below(8)).expect("small") + 1); + same(&bytes[..cut], Limits::default()); + } + } + + /// Header errors are the same. + #[test] + fn header_errors_are_the_same() { + for bytes in + [&b""[..], b"RIFX", b"RIFX\0\0\0\x04Egg", b"RIFX\0\0\0\x04Fnord", b"RIFX\0\0\0\x08Egg!", b"RIFX\0\0\0\x03Egg!", b"RIFF\0\0\0\x04Egg!"] + { + same(bytes, Limits::default()); + } + } + + /// The typed accessors give the same answers through either tree. + #[test] + fn nodes_answer_alike_on_both_trees() { + let mut rng = Rng(42); + for _ in 0..100 { + let mut budget = 200; + let file = AepFile { chunks: tree(&mut rng, 0, &mut budget), trailer: Cow::Borrowed(&[]) }; + let bytes = write(&file).expect("write"); + let parsed = crate::parse(&bytes).expect("parse"); + let index = TreeIndex::parse(&bytes).expect("index"); + let a: Vec> = parsed.walk().map(|(_, c)| Node::from(c)).collect(); + let b: Vec> = index.walk().map(|(_, n)| n).collect(); + assert_eq!(a.len(), b.len()); + for (x, y) in a.iter().zip(&b) { + assert_eq!( + (x.id(), x.pad(), x.bytes(), x.list_type(), x.is_opaque_list()), + (y.id(), y.pad(), y.bytes(), y.list_type(), y.is_opaque_list()) + ); + assert_eq!(x.children().map(Node::to_chunk).collect::>(), y.children().map(Node::to_chunk).collect::>()); + for t in [b"Fold", b"tdgp", b"tdbs", b"Layr", b"btdk"] { + assert_eq!(x.find_list(t).map(Node::to_chunk), y.find_list(t).map(Node::to_chunk)); + assert_eq!(x.child_list(t).map(Node::to_chunk), y.child_list(t).map(Node::to_chunk)); + assert_eq!(x.is_list(t), y.is_list(t)); + } + for id in [b"cdat", b"Utf8", b"tdsn"] { + assert_eq!(x.child(id).map(Node::to_chunk), y.child(id).map(Node::to_chunk)); + } + let dx: Vec<(usize, Chunk<'_>)> = x.descendants().map(|(d, n)| (d, n.to_chunk())).collect(); + let dy: Vec<(usize, Chunk<'_>)> = y.descendants().map(|(d, n)| (d, n.to_chunk())).collect(); + assert_eq!(dx, dy); + assert_eq!(format!("{x:?}"), format!("{y:?}")); + } + } + } + + /// The index never reserves more than the input or the limit allows. + #[test] + fn the_index_never_reserves_more_than_the_input_or_the_limit_allows() { + // 1000 empty headers: the most chunks the bytes can hold. + let mut data = b"RIFX\0\0\0\0Egg!".to_vec(); + data.extend(std::iter::repeat_n(*b"abcd\0\0\0\0", 1000).flatten()); + let declared = u32::try_from(data.len() - 8).expect("small"); + data[4..8].copy_from_slice(&declared.to_be_bytes()); + let index = TreeIndex::parse(&data).expect("index"); + assert_eq!(index.len(), 1000); + // The start (1024 slots) is cut to what the input can hold. + assert!(index.nodes.capacity() <= 1000, "{} slots for at most 1000 headers", index.nodes.capacity()); + // A short tail or a header that runs past the end does not raise the bound. + let mut big = data.clone(); + big[16..20].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(TreeIndex::parse(&big).is_err() && crate::parse(&big).is_err()); + // Under a tighter limit the parse fails as the chunk parser does, and the guess never + // exceeds the limit. + let tight = Limits { max_depth: 64, max_chunks: 10 }; + assert_eq!(TreeIndex::parse_with_limits(&data, tight).err(), Some(AepError::TooManyChunks)); + let small = Limits { max_depth: 64, max_chunks: 1000 }; + let index = TreeIndex::parse_with_limits(&data, small).expect("exactly at the limit"); + assert_eq!(index.len(), 1000); + // A limit below what the input holds bounds the reservation: 100 chunks, never more slots. + let mut wide = b"RIFX\0\0\0\0Egg!".to_vec(); + wide.extend_from_slice(b"LIST"); + wide.extend_from_slice(&u32::try_from(4 + 8 * 99).expect("small").to_be_bytes()); + wide.extend_from_slice(b"Fold"); + wide.extend(std::iter::repeat_n(*b"abcd\0\0\0\0", 99).flatten()); + let declared = u32::try_from(wide.len() - 8).expect("small"); + wide[4..8].copy_from_slice(&declared.to_be_bytes()); + let index = TreeIndex::parse_with_limits(&wide, Limits { max_depth: 64, max_chunks: 100 }).expect("100 chunks"); + assert_eq!(index.len(), 100); + assert!(index.nodes.capacity() <= 100); + // `from_file` is sized exactly. + let file = crate::parse(&wide).expect("parse"); + assert_eq!(TreeIndex::from_file(&file).expect("index").nodes.capacity(), 100); + } + + /// A file of `n` data chunks of `size` bytes each. + fn flat(n: usize, size: usize) -> Vec { + let mut data = b"RIFX\0\0\0\0Egg!".to_vec(); + for _ in 0..n { + data.extend_from_slice(b"cdat"); + data.extend_from_slice(&u32::try_from(size).expect("small").to_be_bytes()); + data.extend(std::iter::repeat_n(0u8, size)); + } + let declared = u32::try_from(data.len() - 8).expect("small"); + data[4..8].copy_from_slice(&declared.to_be_bytes()); + data + } + + /// The index grows with the chunk count not the byte size. + #[test] + fn the_index_grows_with_the_chunk_count_not_the_byte_size() { + // 4 MB in 4 chunks: a few slots, not one per 32 bytes (that would be 131 072 slots, 4 MB). + let big = flat(4, 1 << 20); + let index = TreeIndex::parse(&big).expect("index"); + assert_eq!(index.len(), 4); + assert!(index.nodes.capacity() <= INITIAL_SLOTS, "{} slots for 4 chunks", index.nodes.capacity()); + // Many small chunks: at most twice the count (it doubles). + let many = flat(10_000, 2); + let index = TreeIndex::parse(&many).expect("index"); + assert_eq!(index.len(), 10_000); + assert!(index.nodes.capacity() <= 2 * 10_000, "{} slots for 10 000 chunks", index.nodes.capacity()); + same(&big, Limits::default()); + same(&many, Limits::default()); + } + + /// `LIST Fold` around `inner`, followed by 6 bytes too short for a header, so the list's + /// sequence fails and the list is kept opaque. + fn broken_level(inner: &[u8]) -> Vec { + let mut body = b"Fold".to_vec(); + body.extend_from_slice(inner); + body.extend_from_slice(&[0xee; 6]); + let mut out = b"LIST".to_vec(); + out.extend_from_slice(&u32::try_from(body.len()).expect("small").to_be_bytes()); + out.extend_from_slice(&body); + out + } + + /// Failed attempts count once and leave nothing behind. + #[test] + fn failed_attempts_count_once_and_leave_nothing_behind() { + let mut wide = b"Fold".to_vec(); + wide.extend(std::iter::repeat_n(*b"abcd\0\0\0\0", 1000).flatten()); + let mut level = b"LIST".to_vec(); + level.extend_from_slice(&u32::try_from(wide.len()).expect("small").to_be_bytes()); + level.extend_from_slice(&wide); + for _ in 0..50 { + level = broken_level(&level); + } + let mut data = b"RIFX\0\0\0\0Egg!".to_vec(); + data.extend_from_slice(&level); + let declared = u32::try_from(data.len() - 8).expect("small"); + data[4..8].copy_from_slice(&declared.to_be_bytes()); + let headers = 1 + 50 + 1000; + let index = TreeIndex::parse_with_limits(&data, Limits { max_depth: 64, max_chunks: headers }).expect("parse"); + assert_eq!(TreeIndex::parse_with_limits(&data, Limits { max_depth: 64, max_chunks: headers - 1 }).err(), Some(AepError::TooManyChunks)); + // Only the outermost list is left, opaque: every failed attempt was rolled back. + assert_eq!(index.len(), 1); + let top = index.chunks().next().expect("one top-level chunk"); + assert!(top.is_opaque_list() && top.bytes().map(<[u8]>::len) == Some(level.len() - 12)); + assert_eq!(write(&index.to_file()).expect("write"), data); + same(&data, Limits::default()); + } + + /// Depth beyond the limit stays opaque without recursion past it. + #[test] + fn depth_beyond_the_limit_stays_opaque_without_recursion_past_it() { + // 5000 nested lists: far deeper than the limit; everything below depth 64 stays bytes. + let mut level = b"LIST\0\0\0\x04tdgp".to_vec(); + for _ in 0..5000 { + let mut out = b"LIST".to_vec(); + out.extend_from_slice(&u32::try_from(level.len() + 4).expect("small").to_be_bytes()); + out.extend_from_slice(b"tdgp"); + out.extend_from_slice(&level); + level = out; + } + let mut data = b"RIFX\0\0\0\0Egg!".to_vec(); + data.extend_from_slice(&level); + let declared = u32::try_from(data.len() - 8).expect("small"); + data[4..8].copy_from_slice(&declared.to_be_bytes()); + let index = TreeIndex::parse(&data).expect("parse"); + assert_eq!(index.len(), 64); + assert_eq!(index.walk().map(|(d, _)| d).max(), Some(64)); + assert!(index.walk().last().is_some_and(|(_, n)| n.is_opaque_list())); + same(&data, Limits::default()); + // Converting and indexing a deep owned tree uses no recursion either. + let file = index.to_file(); + assert_eq!(TreeIndex::from_file(&file).expect("index").len(), 64); + } +} diff --git a/crates/aep/src/keys.rs b/crates/aep/src/keys.rs index 0c0c93ee6..6e107ad00 100644 --- a/crates/aep/src/keys.rs +++ b/crates/aep/src/keys.rs @@ -193,19 +193,10 @@ impl<'f> Keyframe<'f> { } } - /// The value of a 3-component spatial keyframe (Position), x, y and z in pixels. - pub fn spatial_value(&self) -> Option<[f64; 3]> { - let v = self.value()?; - if !matches!(self.kind(), KeyframeKind::Spatial { components: 3 }) { - return None; - } - Some([*v.first()?, *v.get(1)?, *v.get(2)?]) - } - /// The ease of the key: the four slots at byte 16 of a one-component plain record or at byte 24 /// of a spatial record or a mask path record. For linear and hold keys all four are 0 (a bezier key stores what /// `setTemporalEase` was given). Plain records with several components keep four slots per - /// component, see [`Keyframe::ease_component`] and [`Keyframe::plain_ease_slots`]. + /// component, see [`Keyframe::ease_component`]. pub fn ease(&self) -> Option { match self.kind() { KeyframeKind::Plain { components: 1 } => ease_at(self.record, 16), @@ -214,16 +205,6 @@ impl<'f> Keyframe<'f> { } } - /// The `4 n` ease `f64` of a plain record as stored (from byte `8 + 8 n`). With one component - /// they are [`Keyframe::ease`]; with several they are type-major, see - /// [`Keyframe::ease_component`]. - pub fn plain_ease_slots(&self) -> Option> { - match self.kind() { - KeyframeKind::Plain { components } => doubles(self.record, components.checked_mul(8)?.checked_add(8)?, components.checked_mul(4)?), - _ => None, - } - } - /// The ease of one value component. A plain record with `n` components stores the `4 n` ease /// doubles **type-major**: `[in speed x n][in influence x n][out speed x n][out influence x n]` /// starting at byte `8 + 8 n`, and component `i` uses slot `i` of each block (verified for the @@ -260,16 +241,6 @@ impl<'f> Keyframe<'f> { (0..n).map(|i| self.ease_component(i)).collect() } - /// The Euclidean ARGB distance to the next key that After Effects writes at byte 16 of a colour - /// (4-component spatial) record whose interpolation is not linear: 255 sqrt(2) = 360.6245 for - /// red to blue; 0 on the last key and on linear keys. `None` for other records. - pub fn color_distance(&self) -> Option { - match self.kind() { - KeyframeKind::Spatial { components: 4 } => be_f64(self.record, 16), - _ => None, - } - } - /// The decoded spatial record (Position: 128 bytes, colour: 152 bytes). pub fn spatial(&self) -> Option { let KeyframeKind::Spatial { components: n } = self.kind() else { return None }; @@ -300,11 +271,6 @@ pub struct ExpressionFlags { } impl ExpressionFlags { - /// True when bytes 76, 77 and 120 are all 1. - pub fn all_set(&self) -> bool { - self.byte_76 == 1 && self.byte_77 == 1 && self.byte_120 == 1 - } - /// True when byte 119 is 1. pub fn disabled(&self) -> bool { self.byte_119 == 1 @@ -324,11 +290,6 @@ impl<'f, 'a> Property<'f, 'a> { be_u32(self.chunk.child(b"tdsb")?.bytes()?, 0) } - /// True when `tdb4` byte 5 has bit 0 set: the property is static, it has no keyframes. - pub fn is_static_flag_set(&self) -> Option { - Some(self.header()?.get(5)? & 1 != 0) - } - /// The expression source: the raw UTF-8 text of the `Utf8` child that follows the value in the /// property (no length prefix, no engine tag; the engine is project-wide). pub fn expression(&self) -> Option { diff --git a/crates/aep/src/lib.rs b/crates/aep/src/lib.rs index a47cddfce..3e5dc2ceb 100644 --- a/crates/aep/src/lib.rs +++ b/crates/aep/src/lib.rs @@ -14,6 +14,8 @@ //! list) on damaged data instead of failing. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +// Ranges and borrowed bytes are passed around as plain tuples, as in EffectCraft, which allows this lint. +#![allow(clippy::type_complexity)] mod aepx; mod arbitrary; @@ -24,6 +26,7 @@ mod effects; mod error; mod footage; mod header; +pub mod index; mod keys; mod masks; mod matte; @@ -36,16 +39,17 @@ mod values; mod version; mod write; -pub use aepx::{is_aepx, parse_any, read_aepx, read_aepx_with_limits}; +pub use aepx::{AepxDrafts, index_aepx, is_aepx, parse_any, read_aepx, read_aepx_with_limits}; pub use arbitrary::{ CURVE_IDENTITY, CryptomatteData, CurvesData, ExtractorChannels, HueSaturationData, LevelsData, MeshData, curve_lut, identifier_channel_type, }; pub use camera::{CameraPosition, DEFAULT_CAMERA_ZOOM}; pub use chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS, Walk}; -pub use effects::{DefinitionSource, Effect, EffectDefinitions, EffectParam, LayerRef, ParamKind, ParamValue}; +pub use effects::{DefinitionSource, Effect, EffectDefinitions, EffectParam, LayerRef, ParamKind, ParamValue, Pard}; pub use error::AepError; pub use footage::{Footage, PsdLayer, SequenceInfo}; pub use header::{BlendMode, CompHeader, LayerHeader, LayerType, LightType, Quality, Rational, Renderer}; +pub use index::{Draft, Node, NodeWalk, Nodes, Root, TreeIndex}; pub use keys::{ExpressionFlags, Interpolation, KeyEase, KeyframeKind, SpatialKey}; pub use masks::{Mask, MaskAtom, MaskMode, PathVertex, ShapePath}; pub use matte::{MatteMode, TrackMatte}; diff --git a/crates/aep/src/masks.rs b/crates/aep/src/masks.rs index 203ccefd5..42b52f5f4 100644 --- a/crates/aep/src/masks.rs +++ b/crates/aep/src/masks.rs @@ -6,9 +6,9 @@ //! property (`tdbs`, with a key list when animated) and `omks` with one `LIST shap` per keyframe (a //! single one when the path is static). -use crate::Chunk; use crate::bytes::{be_f32, be_u32, until_nul}; -use crate::typed::{KeyList, Layer, Project, PropGroup, PropNode, Property}; +use crate::index::Node; +use crate::typed::{KeyList, Layer, PropGroup, PropNode, Property}; use crate::values::{Tracked, read}; /// The mask mode (`mkif` `u32` at byte 4). @@ -93,8 +93,10 @@ pub struct ShapePath { } impl ShapePath { - /// Decodes a `LIST shap`; `None` when `shph` or the point list is malformed. - pub fn from_chunk(shap: &Chunk<'_>) -> Option { + /// Decodes a `LIST shap` (a [`Chunk`](crate::Chunk) or a [`Node`]); `None` when + /// `shph` or the point list is malformed. + pub fn from_chunk<'f, 'a: 'f>(shap: impl Into>) -> Option { + let shap = shap.into(); let shph = shap.child(b"shph")?.bytes()?; let bbox = [be_f32(shph, 4)?, be_f32(shph, 8)?, be_f32(shph, 12)?, be_f32(shph, 16)?]; let closed_flag = *shph.get(20)?; @@ -187,22 +189,12 @@ fn mask_from_atom(atom: &PropGroup<'_, '_>, mkif: Option<&[u8]>) -> Mask { } } if let Some(omks) = oms.child_list(b"omks") { - mask.shapes = omks.children().iter().filter(|c| c.is_list(b"shap")).filter_map(ShapePath::from_chunk).collect(); + mask.shapes = omks.children().filter(|c| c.is_list(b"shap")).filter_map(ShapePath::from_chunk).collect(); } } mask } -impl<'f, 'a> Project<'f, 'a> { - /// The vertices of every path shape of a mask in pixels of the layer: mask boxes are normalised - /// to the **layer** size (the size of its source), not the composition's. `None` when the layer - /// has no source with a size. - pub fn mask_vertices_px(&self, layer: &Layer<'f, 'a>, mask: &Mask) -> Option>> { - let (width, height) = self.layer_source_size(layer)?; - Some(mask.shapes.iter().map(|s| s.vertices((f64::from(width), f64::from(height)))).collect()) - } -} - /// One mask with the views of its records, for readers that need more than [`Mask`]: the /// keyframes and expressions of the mask properties (in [`MaskAtom::atom`]: `ADBE Mask Feather`, /// `ADBE Mask Opacity`, `ADBE Mask Offset`) and the path keyframes paired with their shapes. @@ -246,7 +238,7 @@ impl<'f, 'a> Layer<'f, 'a> { if let Some(PropNode::Other(oms)) = atom.get("ADBE Mask Shape") { path = oms.child_list(b"tdbs").and_then(Property::from_chunk); if let Some(omks) = oms.child_list(b"omks") { - path_shapes = omks.children().iter().filter(|c| c.is_list(b"shap")).map(ShapePath::from_chunk).collect(); + path_shapes = omks.children().filter(|c| c.is_list(b"shap")).map(ShapePath::from_chunk).collect(); } } masks.push(MaskAtom { mask, atom, path, path_shapes }); diff --git a/crates/aep/src/read.rs b/crates/aep/src/read.rs index c769029e4..a969a3997 100644 --- a/crates/aep/src/read.rs +++ b/crates/aep/src/read.rs @@ -2,6 +2,7 @@ use std::borrow::Cow; +use crate::bytes::{array, be_u32}; use crate::chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS}; use crate::error::AepError; @@ -36,10 +37,20 @@ pub fn parse(data: &[u8]) -> Result, AepError> { /// # Errors /// See [`parse`]; also fails with [`AepError::TooManyChunks`]. pub fn parse_with_limits(data: &[u8], limits: Limits) -> Result, AepError> { + let end = container_end(data)?; + let mut parser = Parser { data, limits, count: 0 }; + let chunks = parser.sequence(12, end, 1)?; + let trailer = data.get(end..).unwrap_or(&[]); + Ok(AepFile { chunks, trailer: Cow::Borrowed(trailer) }) +} + +/// Checks the `RIFX`/`Egg!` header and returns where the container ends (its chunks fill +/// `12..end`; the trailer follows). Shared with [`crate::index::TreeIndex::parse`]. +pub(super) fn container_end(data: &[u8]) -> Result { if id4(data, 0) != Some(*b"RIFX") { return Err(AepError::NotRifx); } - let declared = u64::from(be32(data, 4).ok_or(AepError::NotRifx)?); + let declared = u64::from(be_u32(data, 4).ok_or(AepError::NotRifx)?); let form = id4(data, 8).ok_or(AepError::NotRifx)?; if &form != b"Egg!" { return Err(AepError::BadFormType(form)); @@ -49,20 +60,12 @@ pub fn parse_with_limits(data: &[u8], limits: Limits) -> Result, Aep return Err(AepError::Truncated { declared, available }); } // `declared <= available < data.len()`, so the end is inside the input. - let end = usize::try_from(declared).map_err(|_| AepError::TooLarge)?.checked_add(8).ok_or(AepError::TooLarge)?; - let mut parser = Parser { data, limits, count: 0 }; - let chunks = parser.sequence(12, end, 1)?; - let trailer = data.get(end..).unwrap_or(&[]); - Ok(AepFile { chunks, trailer: Cow::Borrowed(trailer) }) + usize::try_from(declared).map_err(|_| AepError::TooLarge)?.checked_add(8).ok_or(AepError::TooLarge) } -fn be32(data: &[u8], at: usize) -> Option { - id4(data, at).map(u32::from_be_bytes) -} - -fn id4(data: &[u8], at: usize) -> Option<[u8; 4]> { - let b = data.get(at..at.checked_add(4)?)?; - Some([*b.first()?, *b.get(1)?, *b.get(2)?, *b.get(3)?]) +/// The 4-byte id at `at`, `None` when the input is too short. +pub(super) fn id4(data: &[u8], at: usize) -> Option<[u8; 4]> { + array::<4>(data, at) } struct Parser<'a> { @@ -74,13 +77,16 @@ struct Parser<'a> { impl<'a> Parser<'a> { /// Parses the sibling chunks that exactly fill `start..end`. fn sequence(&mut self, start: usize, end: usize, depth: usize) -> Result>, AepError> { + // Sized once: a well-formed sequence fills exactly this many slots. `try_reserve` so a + // limit raised past what the target can allocate fails nothing here (the Vec then grows). let mut out = Vec::new(); + let _ = out.try_reserve_exact(self.capacity_for(start, end)); let mut pos = start; while pos < end { let short = AepError::Malformed { offset: pos, what: "bytes left over that are too short for a chunk header" }; let body_start = pos.checked_add(8).filter(|b| *b <= end).ok_or(short)?; let id = id4(self.data, pos).ok_or(AepError::Malformed { offset: pos, what: "short read" })?; - let size = be32(self.data, pos + 4).ok_or(AepError::Malformed { offset: pos, what: "short read" })? as usize; + let size = be_u32(self.data, pos + 4).ok_or(AepError::Malformed { offset: pos, what: "short read" })? as usize; let avail = end - body_start; if size > avail { return Err(AepError::ChunkTooLarge { offset: pos, size: size as u64, available: avail as u64 }); @@ -103,6 +109,26 @@ impl<'a> Parser<'a> { Ok(out) } + /// The slots to reserve for the sequence `start..end`: its header count, bounded by the input + /// (one per 8 bytes at most) and by the chunks the limit still allows. + fn capacity_for(&self, start: usize, end: usize) -> usize { + self.count_siblings(start, end).min(self.limits.max_chunks.saturating_sub(self.count)) + } + + /// The number of chunk headers that tile `start..end` the way [`Self::sequence`] walks them, + /// stopping at the first that does not fit. Never more than `(end - start) / 8`. + fn count_siblings(&self, start: usize, end: usize) -> usize { + let mut n = 0usize; + let mut pos = start; + while let Some(body_start) = pos.checked_add(8).filter(|b| *b <= end) { + let Some(size) = be_u32(self.data, pos.saturating_add(4)) else { break }; + let Some(next) = usize::try_from(size).ok().and_then(|s| body_start.checked_add(s)).filter(|e| *e <= end) else { break }; + n = n.saturating_add(1); + pos = if size % 2 == 1 && next < end { next.saturating_add(1) } else { next }; + } + n + } + /// Like [`Self::sequence`] but a structural problem yields `None` (the caller keeps bytes). fn tentative(&mut self, start: usize, end: usize, depth: usize) -> Result>>, AepError> { match self.sequence(start, end, depth) { @@ -140,3 +166,113 @@ impl<'a> Parser<'a> { Ok(Body::Data(Cow::Borrowed(self.slice(start, end)))) } } + +#[cfg(test)] +mod tests { + use super::*; + + /// Every children `Vec` in the tree, depth first. + fn child_vecs<'f, 'a>(chunks: &'f [Chunk<'a>], out: &mut Vec<&'f Vec>>) { + for c in chunks { + match &c.body { + Body::List { children, .. } | Body::Chunks(children) => { + out.push(children); + child_vecs(children, out); + } + Body::Data(_) | Body::OpaqueList { .. } => {} + } + } + } + + /// Every sibling list is allocated once at its exact length. + #[test] + fn every_sibling_list_is_allocated_once_at_its_exact_length() { + let inner: Vec> = (0..37u8).map(|i| Chunk::data(*b"cdat", vec![i; usize::from(i % 5)])).collect(); + let middle: Vec> = (0..9).map(|_| Chunk::list(*b"tdbs", inner.clone())).collect(); + let file = AepFile { chunks: vec![Chunk::data(*b"head", vec![0; 3]), Chunk::list(*b"Fold", middle)], trailer: Cow::Borrowed(&[]) }; + let bytes = crate::write(&file).expect("write"); + let parsed = parse(&bytes).expect("parse"); + assert_eq!(crate::write(&parsed).expect("rewrite"), bytes); + let mut vecs = vec![&parsed.chunks]; + child_vecs(&parsed.chunks, &mut vecs); + assert_eq!(vecs.len(), 11); + // `try_reserve_exact` gives exactly the requested capacity with std's allocator today; a + // Vec grown push by push would have rounded up to a power of two (37 -> 64, 9 -> 16). + for v in vecs { + assert_eq!(v.capacity(), v.len(), "a sibling Vec grew past its length"); + } + } + + /// The preallocation is bounded by the input and the chunk limit. + #[test] + fn the_preallocation_is_bounded_by_the_input_and_the_chunk_limit() { + // 1000 empty headers: the most chunks the bytes can hold. + let mut data = b"RIFX\0\0\0\0Egg!".to_vec(); + data.extend(std::iter::repeat_n(*b"abcd\0\0\0\0", 1000).flatten()); + let declared = u32::try_from(data.len() - 8).expect("small"); + data[4..8].copy_from_slice(&declared.to_be_bytes()); + let parser = Parser { data: &data, limits: Limits::default(), count: 0 }; + assert_eq!(parser.count_siblings(12, data.len()), 1000); + assert!(parser.count_siblings(12, data.len()) <= (data.len() - 12) / 8); + // A header whose size runs past the end stops the count; a stray tail is not counted. + assert_eq!(parser.count_siblings(12, data.len() - 3), 999); + let mut big = data.clone(); + big[16..20].copy_from_slice(&u32::MAX.to_be_bytes()); + let parser = Parser { data: &big, limits: Limits::default(), count: 0 }; + assert_eq!(parser.count_siblings(12, big.len()), 0); + let f = parse(&data).expect("parse"); + assert_eq!((f.chunks.len(), f.chunks.capacity()), (1000, 1000)); + // Under a tighter limit the parse fails as before, without sizing for more than allowed. + let tight = Limits { max_depth: 64, max_chunks: 10 }; + assert_eq!(parse_with_limits(&data, tight), Err(AepError::TooManyChunks)); + // The reservation never exceeds what the chunk limit still allows. + let mut parser = Parser { data: &data, limits: tight, count: 0 }; + assert_eq!(parser.capacity_for(12, data.len()), 10); + parser.count = 7; + assert_eq!(parser.capacity_for(12, data.len()), 3); + parser.count = 11; + assert_eq!(parser.capacity_for(12, data.len()), 0); + let parser = Parser { data: &data, limits: Limits::default(), count: 0 }; + assert_eq!(parser.capacity_for(12, data.len()), 1000); + } + + /// `LIST Fold` around `inner`, followed by 6 bytes too short for a header, so the list's + /// sequence fails and the list is kept opaque. + fn broken_level(inner: &[u8]) -> Vec { + let mut body = b"Fold".to_vec(); + body.extend_from_slice(inner); + body.extend_from_slice(&[0xee; 6]); + let mut out = b"LIST".to_vec(); + out.extend_from_slice(&u32::try_from(body.len()).expect("small").to_be_bytes()); + out.extend_from_slice(&body); + out + } + + /// Nested lists with broken tails parse each header once. + #[test] + fn nested_lists_with_broken_tails_parse_each_header_once() { + // A wide, well-formed list at the bottom, then 50 levels that each fail and fall back to + // opaque: every level re-parses its child before giving up. + let mut wide = b"Fold".to_vec(); + wide.extend(std::iter::repeat_n(*b"abcd\0\0\0\0", 1000).flatten()); + let mut level = b"LIST".to_vec(); + level.extend_from_slice(&u32::try_from(wide.len()).expect("small").to_be_bytes()); + level.extend_from_slice(&wide); + for _ in 0..50 { + level = broken_level(&level); + } + let mut data = b"RIFX\0\0\0\0Egg!".to_vec(); + data.extend_from_slice(&level); + let declared = u32::try_from(data.len() - 8).expect("small"); + data[4..8].copy_from_slice(&declared.to_be_bytes()); + // 1 + 50 + 1000 headers in the file: the parse counts each exactly once (the count spans + // every attempt), so the work stays linear in the input. + let headers = 1 + 50 + 1000; + let f = parse_with_limits(&data, Limits { max_depth: 64, max_chunks: headers }).expect("parse"); + assert_eq!(parse_with_limits(&data, Limits { max_depth: 64, max_chunks: headers - 1 }), Err(AepError::TooManyChunks)); + // The outermost list keeps its bytes, and the file writes back unchanged. + let [top] = f.chunks.as_slice() else { panic!("one top-level chunk") }; + assert!(matches!(&top.body, Body::OpaqueList { list_type, data } if list_type == b"Fold" && data.len() + 12 == level.len())); + assert_eq!(crate::write(&f).expect("write"), data); + } +} diff --git a/crates/aep/src/settings.rs b/crates/aep/src/settings.rs index 1193dbc39..440983805 100644 --- a/crates/aep/src/settings.rs +++ b/crates/aep/src/settings.rs @@ -2,8 +2,10 @@ //! linear-light switches, the time display and the frame count start. Cases 620-634: one change //! each against the base project 620; every field below is the only byte (or chunk) that changes. +use crate::bytes::be_u32; use crate::chunk::AepFile; use crate::footage::json_string; +use crate::index::{Node, Root, TreeIndex}; /// The time display style (`nhed` byte 8, feet + frames byte 11, film type byte 13). #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -54,7 +56,17 @@ pub struct ProjectSettings { impl ProjectSettings { /// Reads the settings from the top-level chunks. pub fn read(file: &AepFile<'_>) -> ProjectSettings { - let nhed = file.chunk(b"nhed").and_then(|c| c.bytes()); + Self::of(Root::from(file)) + } + + /// Reads the settings from the top-level chunks of an indexed file. + pub fn read_index(index: &TreeIndex<'_>) -> ProjectSettings { + Self::of(Root::from(index)) + } + + /// Reads the settings from the top-level chunks of either tree. + pub fn of(file: Root<'_, '_>) -> ProjectSettings { + let nhed = file.chunk(b"nhed").and_then(Node::bytes); let byte = |i: usize| nhed.and_then(|b| b.get(i).copied()); let bits_per_channel = byte(15).and_then(|v| match v { 0 => Some(8), @@ -73,9 +85,9 @@ impl ProjectSettings { 1 => Some(1), _ => None, }); - let flag = |id: &[u8; 4]| file.chunk(id).and_then(|c| c.bytes()).and_then(|b| b.first().copied()) == Some(1); - let working_gamma_24 = file.chunk(b"dwga").and_then(|c| c.bytes()).and_then(|b| b.get(..4)).map(|b| b != [0, 0, 0, 0]); - let scene_compensation = file.chunk(b"acer").and_then(|c| c.bytes()).and_then(|b| b.first().copied()).map(|v| v != 0); + let flag = |id: &[u8; 4]| file.chunk(id).and_then(Node::bytes).and_then(|b| b.first().copied()) == Some(1); + let working_gamma_24 = file.chunk(b"dwga").and_then(Node::bytes).and_then(|b| b.get(..4)).map(|b| b != [0, 0, 0, 0]); + let scene_compensation = file.chunk(b"acer").and_then(Node::bytes).and_then(|b| b.first().copied()).map(|v| v != 0); ProjectSettings { bits_per_channel, working_space: working_space(file), @@ -90,9 +102,10 @@ impl ProjectSettings { } /// The working space: the `Utf8` right after `PwCs`. -fn working_space(file: &AepFile<'_>) -> Option { - let at = file.chunks.iter().position(|c| c.is(b"PwCs"))?; - let json = file.chunks.get(at.checked_add(1)?).filter(|c| c.is(b"Utf8"))?.bytes()?; +fn working_space(file: Root<'_, '_>) -> Option { + let mut chunks = file.chunks(); + chunks.find(|c| c.is(b"PwCs"))?; + let json = chunks.next().filter(|c| c.is(b"Utf8"))?.bytes()?; let trimmed: Vec = json.iter().copied().filter(|b| !b.is_ascii_whitespace() && *b != 0).collect(); if trimmed == b"{}" { return Some(WorkingSpace::None); @@ -130,7 +143,7 @@ fn base64_decode(text: &str) -> Option> { /// The profile description (ICC.1 tag `desc`): a v2 `desc` (ASCII) or a v4 `mluc` (its first /// record, UTF-16BE). `None` when the profile or the tag is cut short. fn icc_description(icc: &[u8]) -> Option { - let be32 = |at: usize| icc.get(at..at.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_be_bytes); + let be32 = |at: usize| be_u32(icc, at); let count = be32(128)?; let tag = (0..count.min(1024)).find_map(|i| { let at = 132usize.checked_add(usize::try_from(i).ok()?.checked_mul(12)?)?; @@ -141,13 +154,13 @@ fn icc_description(icc: &[u8]) -> Option { let body = icc.get(off..off.checked_add(len)?)?; let text = match body.get(..4)? { b"desc" => { - let n = usize::try_from(u32::from_be_bytes(body.get(8..12)?.try_into().ok()?)).ok()?; + let n = usize::try_from(be_u32(body, 8)?).ok()?; let ascii = body.get(12..12usize.checked_add(n)?)?; String::from_utf8_lossy(ascii.split(|b| *b == 0).next().unwrap_or_default()).into_owned() } b"mluc" => { - let n = usize::try_from(u32::from_be_bytes(body.get(20..24)?.try_into().ok()?)).ok()?; - let at = usize::try_from(u32::from_be_bytes(body.get(24..28)?.try_into().ok()?)).ok()?; + let n = usize::try_from(be_u32(body, 20)?).ok()?; + let at = usize::try_from(be_u32(body, 24)?).ok()?; let units: Vec = body.get(at..at.checked_add(n)?)?.as_chunks::<2>().0.iter().map(|p| u16::from_be_bytes(*p)).collect(); String::from_utf16_lossy(&units) } diff --git a/crates/aep/src/shapes.rs b/crates/aep/src/shapes.rs index 864cfa55b..28b572506 100644 --- a/crates/aep/src/shapes.rs +++ b/crates/aep/src/shapes.rs @@ -10,7 +10,7 @@ //! points are in pixels relative to the layer origin. use crate::masks::ShapePath; -use crate::typed::{Layer, PropGroup, PropNode, Property}; +use crate::typed::{Layer, PropGroup, PropNode}; use crate::values::{Tracked, read, read_opt}; /// The stroke line cap (`ADBE Vector Stroke Line Cap`, stored as a double; butt is the default and @@ -145,7 +145,7 @@ fn number(group: &PropGroup<'_, '_>, name: &str) -> Option> { fn paths_of(group: &PropGroup<'_, '_>) -> Vec { let Some(PropNode::Other(oms)) = group.get("ADBE Vector Shape") else { return Vec::new() }; let Some(omks) = oms.child_list(b"omks") else { return Vec::new() }; - omks.children().iter().filter(|c| c.is_list(b"shap")).filter_map(ShapePath::from_chunk).collect() + omks.children().filter(|c| c.is_list(b"shap")).filter_map(ShapePath::from_chunk).collect() } fn item(match_name: String, group: &PropGroup<'_, '_>) -> ShapeItem { @@ -269,12 +269,3 @@ impl<'f, 'a> Layer<'f, 'a> { .collect() } } - -impl<'f, 'a> Property<'f, 'a> { - /// The ARGB fractions of a static colour property (4 components stored as doubles 0..255). - pub fn color_argb(&self) -> Option<[f64; 4]> { - let v = self.static_value()?; - let [a, r, g, b] = *v.as_slice().first_chunk::<4>()?; - Some([a / 255.0, r / 255.0, g / 255.0, b / 255.0]) - } -} diff --git a/crates/aep/src/textdoc.rs b/crates/aep/src/textdoc.rs index 373fd40db..1e7569b67 100644 --- a/crates/aep/src/textdoc.rs +++ b/crates/aep/src/textdoc.rs @@ -5,8 +5,11 @@ //! as `(` + BOM `fe ff` + UTF-16BE + `)` with `\` escapes. [`parse_text_dict`] reads it (depth //! limited, never panics) and [`Layer::text_document`] pulls out the verified fields. -use crate::Chunk; +use std::borrow::Cow; +use std::sync::LazyLock; + use crate::bytes::{be_u32, text}; +use crate::index::Node; use crate::typed::{Comp, Layer, Property}; /// How deep dictionaries and arrays may nest before the parser gives up. @@ -73,15 +76,99 @@ impl TextValue { _ => None, } } +} - fn key_text(&self) -> String { +/// The text of a key the way the dictionary stores it: a `/name` as written, any other value in +/// its canonical form (`06` and `+6` are `6`, `1.0` is `1`, a string its decoded text). +fn owned_key(value: &V<'_>) -> String { + match value { + V::Name(s) | V::Token(s) => text(s), + V::Str(utf16) => decode_utf16be(utf16), + V::Int(i) => i.to_string(), + V::Float(f) => f.to_string(), + V::Bool(b) => b.to_string(), + V::Bytes(b) => text(b), + V::Dict(_) | V::Array(_) => String::new(), + } +} + +/// A value of the text dictionary as [`TextDocument::parse`] reads it: it borrows names, bare +/// words and strings from the body, so a document is read without a `String` per token. A key is +/// borrowed when it is a `/name` (every key After Effects writes); its text is the same as the +/// [`TextValue`] key's. [`V::into_owned`] gives the public [`TextValue`]. +#[derive(Debug, Clone)] +enum V<'b> { + Dict(Vec<(Cow<'b, str>, V<'b>)>), + Array(Vec>), + /// A string that started with the byte order mark: the UTF-16BE bytes after it, unescaped. + Str(Cow<'b, [u8]>), + Bytes(Cow<'b, [u8]>), + Name(&'b [u8]), + Int(i64), + Float(f64), + Bool(bool), + Token(&'b [u8]), +} + +impl<'b> V<'b> { + /// The value of a dictionary key (the last one when a key repeats). + fn get(&self, key: &str) -> Option<&V<'b>> { match self { - Self::Name(s) | Self::Str(s) | Self::Token(s) => s.clone(), - Self::Int(i) => i.to_string(), - Self::Float(f) => f.to_string(), - Self::Bool(b) => b.to_string(), - Self::Bytes(b) => text(b), - Self::Dict(_) | Self::Array(_) => String::new(), + Self::Dict(pairs) => pairs.iter().rev().find(|(k, _)| k == key).map(|(_, v)| v), + _ => None, + } + } + + fn at(&self, index: usize) -> Option<&V<'b>> { + match self { + Self::Array(items) => items.get(index), + _ => None, + } + } + + /// The decoded text of a string value. + fn string(&self) -> Option { + match self { + Self::Str(utf16) => Some(decode_utf16be(utf16)), + _ => None, + } + } + + fn as_f64(&self) -> Option { + match self { + Self::Int(i) => Some(*i as f64), + Self::Float(f) => Some(*f), + _ => None, + } + } + + fn as_i64(&self) -> Option { + match self { + Self::Int(i) => Some(*i), + _ => None, + } + } + + /// The key a value makes when it is written in a key position. + fn into_key(self) -> Cow<'b, str> { + match self { + Self::Name(s) => String::from_utf8_lossy(s), + other => Cow::Owned(owned_key(&other)), + } + } + + /// The public form. Recursion is bounded: a parsed value nests at most [`MAX_DEPTH`] + 1 deep. + fn into_owned(self) -> TextValue { + match self { + Self::Dict(pairs) => TextValue::Dict(pairs.into_iter().map(|(k, v)| (k.into_owned(), v.into_owned())).collect()), + Self::Array(items) => TextValue::Array(items.into_iter().map(V::into_owned).collect()), + Self::Str(utf16) => TextValue::Str(decode_utf16be(&utf16)), + Self::Bytes(b) => TextValue::Bytes(b.into_owned()), + Self::Name(s) => TextValue::Name(text(s)), + Self::Int(i) => TextValue::Int(i), + Self::Float(f) => TextValue::Float(f), + Self::Bool(b) => TextValue::Bool(b), + Self::Token(s) => TextValue::Token(text(s)), } } } @@ -89,32 +176,44 @@ impl TextValue { struct Parser<'b> { b: &'b [u8], i: usize, + /// The pairs of the dictionaries being read, innermost last: a dictionary's pairs are moved + /// into a vector of their exact size when it closes (one allocation per dictionary instead of + /// one per doubling). + pairs: Vec<(Cow<'b, str>, V<'b>)>, + /// The same for the items of arrays. + items: Vec>, } +/// Whitespace between tokens. fn is_ws(c: u8) -> bool { matches!(c, b' ' | b'\t' | b'\r' | b'\n') } +/// A byte that ends a bare word. fn is_delimiter(c: u8) -> bool { is_ws(c) || matches!(c, b'/' | b'[' | b']' | b'<' | b'>' | b'(' | b')') } -impl Parser<'_> { +impl<'b> Parser<'b> { + /// The next byte, `None` at the end. fn peek(&self) -> Option { self.b.get(self.i).copied() } + /// Moves past whitespace. fn skip_ws(&mut self) { while self.peek().is_some_and(is_ws) { self.i = self.i.saturating_add(1); } } + /// True when the unread bytes start with `s`. fn starts_with(&self, s: &[u8]) -> bool { self.b.get(self.i..).is_some_and(|rest| rest.starts_with(s)) } - fn word(&mut self) -> &[u8] { + /// The bare word at the position (up to the next delimiter), borrowed from the body. + fn word(&mut self) -> &'b [u8] { let start = self.i; while self.peek().is_some_and(|c| !is_delimiter(c)) { self.i = self.i.saturating_add(1); @@ -122,79 +221,106 @@ impl Parser<'_> { self.b.get(start..self.i).unwrap_or(&[]) } - fn parse(&mut self, depth: usize) -> Option { + /// The body of a `( ... )` string after the `(`, with `\` escapes resolved: borrowed when it + /// has none. `None` when the string or an escape is not closed. + fn string_body(&mut self) -> Option> { + let rest = self.b.get(self.i..)?; + let stop = rest.iter().position(|c| matches!(c, b')' | b'\\'))?; + let head = rest.get(..stop)?; + self.i = self.i.saturating_add(stop); + if self.peek()? == b')' { + self.i = self.i.saturating_add(1); + return Some(Cow::Borrowed(head)); + } + let mut out = head.to_vec(); + loop { + let c = self.peek()?; + if c == b'\\' { + out.push(*self.b.get(self.i.saturating_add(1))?); + self.i = self.i.saturating_add(2); + } else if c == b')' { + self.i = self.i.saturating_add(1); + return Some(Cow::Owned(out)); + } else { + out.push(c); + self.i = self.i.saturating_add(1); + } + } + } + + /// The value at the position, `None` when it is malformed or nested deeper than the limit. + fn parse(&mut self, depth: usize) -> Option> { self.skip_ws(); if depth > MAX_DEPTH { return None; } if self.starts_with(b"<<") { self.i = self.i.saturating_add(2); - let mut pairs = Vec::new(); + // On an error the pairs read so far stay on the stack: the error ends the whole parse. + let mark = self.pairs.len(); loop { self.skip_ws(); if self.starts_with(b">>") { self.i = self.i.saturating_add(2); - return Some(TextValue::Dict(pairs)); + return Some(V::Dict(self.pairs.drain(mark..).collect())); } let key = self.parse(depth.saturating_add(1))?; let value = self.parse(depth.saturating_add(1))?; - pairs.push((key.key_text(), value)); + self.pairs.push((key.into_key(), value)); } } match self.peek()? { b'[' => { self.i = self.i.saturating_add(1); - let mut items = Vec::new(); + let mark = self.items.len(); loop { self.skip_ws(); if self.peek()? == b']' { self.i = self.i.saturating_add(1); - return Some(TextValue::Array(items)); + return Some(V::Array(self.items.drain(mark..).collect())); } - items.push(self.parse(depth.saturating_add(1))?); + let item = self.parse(depth.saturating_add(1))?; + self.items.push(item); } } b'(' => { self.i = self.i.saturating_add(1); - let mut out = Vec::new(); - loop { - let c = self.peek()?; - if c == b'\\' { - out.push(*self.b.get(self.i.saturating_add(1))?); - self.i = self.i.saturating_add(2); - } else if c == b')' { - self.i = self.i.saturating_add(1); - break; - } else { - out.push(c); - self.i = self.i.saturating_add(1); - } - } - Some(match out.strip_prefix(&[0xfe, 0xff]) { - Some(utf16) => TextValue::Str(decode_utf16be(utf16)), - None => TextValue::Bytes(out), + let body = self.string_body()?; + Some(if body.starts_with(&[0xfe, 0xff]) { + V::Str(match body { + Cow::Borrowed(b) => Cow::Borrowed(b.get(2..).unwrap_or(&[])), + Cow::Owned(mut v) => { + let n = v.len().min(2); + v.drain(..n); + Cow::Owned(v) + } + }) + } else { + V::Bytes(body) }) } b'/' => { self.i = self.i.saturating_add(1); - Some(TextValue::Name(text(self.word()))) + Some(V::Name(self.word())) } _ => { let word = self.word(); if word.is_empty() { return None; } - let w = text(word); - Some(match w.as_str() { - "true" => TextValue::Bool(true), - "false" => TextValue::Bool(false), + // A word that is not UTF-8 reads as text with replacement characters, which is + // neither a boolean nor a number: a token. + let Ok(w) = std::str::from_utf8(word) else { return Some(V::Token(word)) }; + Some(match w { + "true" => V::Bool(true), + "false" => V::Bool(false), _ => match w.parse::() { - Ok(i) => TextValue::Int(i), + Ok(i) => V::Int(i), // `parse` also takes `nan`, `inf` and overflowing exponents; those are not // numbers a text document holds, so they stay tokens and read as missing. Err(_) => match w.parse::() { - Ok(f) if f.is_finite() => TextValue::Float(f), - _ => TextValue::Token(w), + Ok(f) if f.is_finite() => V::Float(f), + _ => V::Token(word), }, }, }) @@ -203,6 +329,7 @@ impl Parser<'_> { } } +/// A big-endian UTF-16 string as text (unpaired surrogates become U+FFFD). fn decode_utf16be(bytes: &[u8]) -> String { let (pairs, rest) = bytes.as_chunks::<2>(); let units = pairs.iter().map(|p| u16::from_be_bytes(*p)); @@ -213,11 +340,9 @@ fn decode_utf16be(bytes: &[u8]) -> String { s } -/// Parses the body of a `btdk` list into its top-level `key value` pairs (as a [`TextValue::Dict`]). -/// Parsing stops silently at the first syntax error or at nesting deeper than 64 levels; what was -/// read before is returned. -pub fn parse_text_dict(body: &[u8]) -> TextValue { - let mut parser = Parser { b: body, i: 0 }; +/// The top-level `key value` pairs of a `btdk` body, borrowed (see [`parse_text_dict`]). +fn parse_dict(body: &[u8]) -> V<'_> { + let mut parser = Parser { b: body, i: 0, pairs: Vec::new(), items: Vec::new() }; let mut pairs = Vec::new(); loop { parser.skip_ws(); @@ -226,13 +351,20 @@ pub fn parse_text_dict(body: &[u8]) -> TextValue { } let Some(key) = parser.parse(0) else { break }; let Some(value) = parser.parse(0) else { break }; - pairs.push((key.key_text(), value)); + pairs.push((key.into_key(), value)); } - TextValue::Dict(pairs) + V::Dict(pairs) +} + +/// Parses the body of a `btdk` list into its top-level `key value` pairs (as a [`TextValue::Dict`]). +/// Parsing stops silently at the first syntax error or at nesting deeper than 64 levels; what was +/// read before is returned. +pub fn parse_text_dict(body: &[u8]) -> TextValue { + parse_dict(body).into_owned() } /// The run style slots (`RUN/6/0[n]/0/0/6/`) that are decoded; every other slot is compared -/// with its default (`style_defaults`) and listed in +/// with its default (`STYLE_DEFAULTS`) and listed in /// [`TextDocument::undecoded_style_fields`] when it differs. pub const DECODED_STYLE_SLOTS: &[&str] = &["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12", "13", "18", "35", "36", "52", "53", "54", "56", "57", "58", "62", "63", "70"]; @@ -241,25 +373,27 @@ pub const DECODED_STYLE_SLOTS: &[&str] = pub const DECODED_PARAGRAPH_SLOTS: &[&str] = &["0", "1", "2", "3", "4", "5", "8", "9", "21", "29", "33"]; /// A number slot. -fn val(v: f64) -> TextValue { - TextValue::Float(v) +fn val(v: f64) -> V<'static> { + V::Float(v) } -fn dict(pairs: Vec<(&str, TextValue)>) -> TextValue { - TextValue::Dict(pairs.into_iter().map(|(k, v)| (k.to_string(), v)).collect()) +/// A dictionary value with these keys, for the built-in defaults. +fn dict(pairs: Vec<(&'static str, V<'static>)>) -> V<'static> { + V::Dict(pairs.into_iter().map(|(k, v)| (Cow::Borrowed(k), v)).collect()) } -fn numbers(v: &[f64]) -> TextValue { - TextValue::Array(v.iter().map(|x| val(*x)).collect()) +/// An array of numbers, for the built-in defaults. +fn numbers(v: &[f64]) -> V<'static> { + V::Array(v.iter().map(|x| val(*x)).collect()) } /// The default of every run style slot that is not decoded and not neutral (zero, `false`, /// empty, `/nil`): the value an untouched point text holds in every sample (110, 243-245). A slot /// with a neutral default is left out; [`undecoded_fields`] treats a missing default as neutral. -fn style_defaults() -> TextValue { - let paint = - |a: f64| dict(vec![("99", TextValue::Name("SimplePaint".into())), ("0", dict(vec![("0", val(1.0)), ("1", numbers(&[1.0, 1.0, 1.0, a]))]))]); - let class = |name: &str| dict(vec![("99", TextValue::Name(name.into()))]); +/// Built once. +static STYLE_DEFAULTS: LazyLock> = LazyLock::new(|| { + let paint = |a: f64| dict(vec![("99", V::Name(b"SimplePaint")), ("0", dict(vec![("0", val(1.0)), ("1", numbers(&[1.0, 1.0, 1.0, a]))]))]); + let class = |name: &'static [u8]| dict(vec![("99", V::Name(name))]); dict(vec![ ("37", val(2.0)), ("41", val(2.0)), @@ -269,24 +403,24 @@ fn style_defaults() -> TextValue { ("46", val(7.0)), ("49", val(-1.0)), ("50", val(-1.0)), - ("55", class("SimpleBlender")), + ("55", class(b"SimpleBlender")), ("64", val(4.0)), ("71", val(4.0)), - ("77", TextValue::Bool(true)), - ("78", TextValue::Bool(true)), + ("77", V::Bool(true)), + ("78", V::Bool(true)), ("79", paint(0.0)), ("82", val(3.0)), ("83", val(3.0)), - ("86", class("SimpleCustomFeature")), + ("86", class(b"SimpleCustomFeature")), ("87", val(100.0)), - ("88", TextValue::Bool(true)), + ("88", V::Bool(true)), ("95", dict(vec![("0", val(0.0)), ("1", val(0.0))])), - ("96", TextValue::Bool(true)), + ("96", V::Bool(true)), ]) -} +}); -/// The same for the paragraph slots (110, 243-245). -fn paragraph_defaults() -> TextValue { +/// The same for the paragraph slots (110, 243-245). Built once. +static PARAGRAPH_DEFAULTS: LazyLock> = LazyLock::new(|| { dict(vec![ ("6", val(1.0)), ("7", val(1.2)), @@ -294,30 +428,35 @@ fn paragraph_defaults() -> TextValue { ("11", val(2.0)), ("12", val(2.0)), ("14", val(36.0)), - ("15", TextValue::Bool(true)), + ("15", V::Bool(true)), ("16", val(0.5)), ("17", numbers(&[0.8, 1.0, 1.33])), ("18", numbers(&[0.0, 0.0, 0.0])), ("19", numbers(&[1.0, 1.0, 1.0])), ("20", val(6.0)), ("22", val(1.0)), - ("23", TextValue::Bool(true)), + ("23", V::Bool(true)), ("24", val(1.0)), - ("26", TextValue::Bool(true)), + ("26", V::Bool(true)), ("31", val(36.0)), ("35", val(1.0)), ("40", val(2.0)), ]) -} +}); -/// Equal values; numbers compare by value (`0` equals `0.0`). -fn same(a: &TextValue, b: &TextValue) -> bool { +/// Equal values; numbers compare by value (`0` equals `0.0`), names and words by their text, +/// strings by their decoded text. +fn same(a: &V<'_>, b: &V<'_>) -> bool { match (a, b) { - (TextValue::Dict(x), TextValue::Dict(y)) => x.len() == y.len() && x.iter().all(|(k, v)| y.iter().any(|(k2, v2)| k == k2 && same(v, v2))), - (TextValue::Array(x), TextValue::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(p, q)| same(p, q)), + (V::Dict(x), V::Dict(y)) => x.len() == y.len() && x.iter().all(|(k, v)| y.iter().any(|(k2, v2)| k == k2 && same(v, v2))), + (V::Array(x), V::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(p, q)| same(p, q)), + (V::Str(x), V::Str(y)) => x == y || decode_utf16be(x) == decode_utf16be(y), + (V::Bytes(x), V::Bytes(y)) => x == y, + (V::Name(x), V::Name(y)) | (V::Token(x), V::Token(y)) => x == y || text(x) == text(y), + (V::Bool(x), V::Bool(y)) => x == y, _ => match (a.as_f64(), b.as_f64()) { (Some(p), Some(q)) => p == q, - _ => a == b, + _ => false, }, } } @@ -326,12 +465,12 @@ fn same(a: &TextValue, b: &TextValue) -> bool { /// listed when it holds something else (a neutral value carries nothing that could be lost). /// `/nil` is how an untouched document stores a slot without a value (244: paragraph `/27`, `/28`, /// `/36`). -fn is_neutral(v: &TextValue) -> bool { +fn is_neutral(v: &V<'_>) -> bool { match v { - TextValue::Name(n) => n == "nil", - TextValue::Bool(b) => !b, - TextValue::Array(a) => a.is_empty(), - TextValue::Dict(d) => d.is_empty(), + V::Name(n) => *n == b"nil", + V::Bool(b) => !b, + V::Array(a) => a.is_empty(), + V::Dict(d) => d.is_empty(), other => other.as_f64() == Some(0.0), } } @@ -356,13 +495,13 @@ pub const MAX_UNDECODED_FIELDS: usize = 64; /// Adds to `out` the slots of `actual` that are not decoded and differ from `sampled`, each once /// and at most [`MAX_UNDECODED_FIELDS`] in all. -fn undecoded_fields(actual: &TextValue, sampled: Option<&TextValue>, decoded: &[&str], neutral: &[(&str, &[i64])], out: &mut Vec) { - let TextValue::Dict(pairs) = actual else { return }; +fn undecoded_fields(actual: &V<'_>, sampled: Option<&V<'_>>, decoded: &[&str], neutral: &[(&str, &[i64])], out: &mut Vec) { + let V::Dict(pairs) = actual else { return }; for (key, value) in pairs { if out.len() >= MAX_UNDECODED_FIELDS { return; } - if decoded.contains(&key.as_str()) || out.contains(key) { + if decoded.contains(&key.as_ref()) || out.iter().any(|o| o == key) { continue; } if neutral.iter().any(|(slot, values)| slot == key && value.as_i64().is_some_and(|v| values.contains(&v))) { @@ -373,7 +512,7 @@ fn undecoded_fields(actual: &TextValue, sampled: Option<&TextValue>, decoded: &[ None => !is_neutral(value), }; if differs { - out.push(key.clone()); + out.push(key.to_string()); } } } @@ -433,13 +572,13 @@ pub struct KernRun { /// The manual kerning list of a text run dictionary (`RUN/8/0`): entries `<< /0 << /0 amount >> /1 /// length >>` (554 kerns each of `HHHH` by 50; the closing `\r` has an entry without an amount). -fn kern_runs(run: &TextValue) -> Vec { - let Some(TextValue::Array(items)) = run.get("8").and_then(|v| v.get("0")) else { return Vec::new() }; +fn kern_runs(run: &V<'_>) -> Vec { + let Some(V::Array(items)) = run.get("8").and_then(|v| v.get("0")) else { return Vec::new() }; let runs: Vec = items .iter() .filter_map(|e| { let length = e.get("1").and_then(usize_of)?; - let amount = e.get("0").and_then(|v| v.get("0")).and_then(TextValue::as_f64).filter(|v| v.is_finite()); + let amount = e.get("0").and_then(|v| v.get("0")).and_then(V::as_f64).filter(|v| v.is_finite()); Some(KernRun { length, amount }) }) .collect(); @@ -602,7 +741,7 @@ pub struct StyleRun { impl StyleRun { /// The style of the sampled untouched document (244), used for a damaged first run. - pub(crate) const SAMPLED: StyleRun = StyleRun { + pub const SAMPLED: StyleRun = StyleRun { length: 0, font_size: 100.0, fill_argb: [1.0; 4], @@ -645,24 +784,26 @@ pub struct TextBox { pub height: f64, } -fn colour_of(style: &TextValue, slot: &str) -> Option<[f64; 4]> { +/// The RGBA colour in a style slot (`slot/0/1`), `None` when it is not four numbers. +fn colour_of(style: &V<'_>, slot: &str) -> Option<[f64; 4]> { let c = style.get(slot)?.get("0")?.get("1")?; Some([c.at(0)?.as_f64()?, c.at(1)?.as_f64()?, c.at(2)?.as_f64()?, c.at(3)?.as_f64()?]) } -fn usize_of(v: &TextValue) -> Option { +/// A non-negative integer value as `usize`. +fn usize_of(v: &V<'_>) -> Option { usize::try_from(v.as_i64()?).ok() } /// A finite number slot of a style, `default` when it is missing or not a finite number. -fn num(style: &TextValue, slot: &str, default: f64) -> f64 { - style.get(slot).and_then(TextValue::as_f64).filter(|v| v.is_finite()).unwrap_or(default) +fn num(style: &V<'_>, slot: &str, default: f64) -> f64 { + style.get(slot).and_then(V::as_f64).filter(|v| v.is_finite()).unwrap_or(default) } /// A boolean slot of a style, `default` when it is missing or not a boolean. -fn flag(style: &TextValue, slot: &str, default: bool) -> bool { +fn flag(style: &V<'_>, slot: &str, default: bool) -> bool { match style.get(slot) { - Some(TextValue::Bool(b)) => *b, + Some(V::Bool(b)) => *b, _ => default, } } @@ -676,9 +817,9 @@ struct RunNotes { } /// The paragraph runs of a text run dictionary (`RUN/5/0`). -fn paragraph_runs(run: &TextValue, notes: &mut RunNotes) -> Vec { - let Some(TextValue::Array(items)) = run.get("5").and_then(|v| v.get("0")) else { return Vec::new() }; - let sampled = Some(paragraph_defaults()); +fn paragraph_runs(run: &V<'_>, notes: &mut RunNotes) -> Vec { + let Some(V::Array(items)) = run.get("5").and_then(|v| v.get("0")) else { return Vec::new() }; + let sampled = Some(&*PARAGRAPH_DEFAULTS); let mut out: Vec = Vec::with_capacity(items.len()); for e in items { let Some(length) = e.get("1").and_then(usize_of) else { @@ -686,7 +827,7 @@ fn paragraph_runs(run: &TextValue, notes: &mut RunNotes) -> Vec { continue; }; let para = e.get("0").and_then(|v| v.get("0")).and_then(|v| v.get("5")); - let justification = match para.and_then(|p| p.get("0")).and_then(TextValue::as_i64) { + let justification = match para.and_then(|p| p.get("0")).and_then(V::as_i64) { Some(0) => Justification::Left, Some(1) => Justification::Right, Some(2) => Justification::Center, @@ -701,12 +842,12 @@ fn paragraph_runs(run: &TextValue, notes: &mut RunNotes) -> Vec { } }; if let Some(p) = para { - undecoded_fields(p, sampled.as_ref(), DECODED_PARAGRAPH_SLOTS, NEUTRAL_PARAGRAPH_VALUES, &mut notes.paragraph_fields); + undecoded_fields(p, sampled, DECODED_PARAGRAPH_SLOTS, NEUTRAL_PARAGRAPH_VALUES, &mut notes.paragraph_fields); } - let num = |slot: &str| para.and_then(|p| p.get(slot)).and_then(TextValue::as_f64).filter(|v| v.is_finite()).unwrap_or(0.0); - let int = |slot: &str, default: i64| para.and_then(|p| p.get(slot)).and_then(TextValue::as_i64).unwrap_or(default); + let num = |slot: &str| para.and_then(|p| p.get(slot)).and_then(V::as_f64).filter(|v| v.is_finite()).unwrap_or(0.0); + let int = |slot: &str, default: i64| para.and_then(|p| p.get(slot)).and_then(V::as_i64).unwrap_or(default); let flag = |slot: &str, default: bool| match para.and_then(|p| p.get(slot)) { - Some(TextValue::Bool(b)) => *b, + Some(V::Bool(b)) => *b, _ => default, }; out.push(ParagraphRun { @@ -728,9 +869,9 @@ fn paragraph_runs(run: &TextValue, notes: &mut RunNotes) -> Vec { } /// The style runs of a text run dictionary (`RUN/6/0`). -fn style_runs(run: &TextValue, notes: &mut RunNotes) -> Vec { - let Some(TextValue::Array(items)) = run.get("6").and_then(|v| v.get("0")) else { return Vec::new() }; - let sampled = Some(style_defaults()); +fn style_runs(run: &V<'_>, notes: &mut RunNotes) -> Vec { + let Some(V::Array(items)) = run.get("6").and_then(|v| v.get("0")) else { return Vec::new() }; + let sampled = Some(&*STYLE_DEFAULTS); let mut out: Vec = Vec::with_capacity(items.len()); for e in items { let Some(length) = e.get("1").and_then(usize_of) else { @@ -743,31 +884,31 @@ fn style_runs(run: &TextValue, notes: &mut RunNotes) -> Vec { out.push(StyleRun { length, ..prev }); continue; }; - let font_size = style.get("1").and_then(TextValue::as_f64); + let font_size = style.get("1").and_then(V::as_f64); let fill_argb = colour_of(style, "53"); let font_index = style.get("0").and_then(usize_of); if font_size.is_none() || fill_argb.is_none() || font_index.is_none() { notes.incomplete = notes.incomplete.saturating_add(1); } - undecoded_fields(style, sampled.as_ref(), DECODED_STYLE_SLOTS, NEUTRAL_STYLE_VALUES, &mut notes.style_fields); + undecoded_fields(style, sampled, DECODED_STYLE_SLOTS, NEUTRAL_STYLE_VALUES, &mut notes.style_fields); out.push(StyleRun { length, font_size: font_size.unwrap_or(prev.font_size), fill_argb: fill_argb.unwrap_or(prev.fill_argb), stroke_argb: colour_of(style, "54"), - tracking: style.get("8").and_then(TextValue::as_f64).unwrap_or(0.0), - faux_bold: matches!(style.get("2"), Some(TextValue::Bool(true))), - faux_italic: matches!(style.get("3"), Some(TextValue::Bool(true))), - caps: match style.get("12").and_then(TextValue::as_i64) { + tracking: style.get("8").and_then(V::as_f64).unwrap_or(0.0), + faux_bold: matches!(style.get("2"), Some(V::Bool(true))), + faux_italic: matches!(style.get("3"), Some(V::Bool(true))), + caps: match style.get("12").and_then(V::as_i64) { None | Some(0) => Caps::Normal, Some(1) => Caps::Small, Some(2) => Caps::All, Some(other) => Caps::Other(other), }, - auto_leading: matches!(style.get("4"), Some(TextValue::Bool(true))), - leading: style.get("5").and_then(TextValue::as_f64), + auto_leading: matches!(style.get("4"), Some(V::Bool(true))), + leading: style.get("5").and_then(V::as_f64), font_index: font_index.unwrap_or(prev.font_index), - baseline: match style.get("13").and_then(TextValue::as_i64) { + baseline: match style.get("13").and_then(V::as_i64) { None | Some(0) => FontBaseline::Normal, Some(1) => FontBaseline::Superscript, Some(2) => FontBaseline::Subscript, @@ -776,22 +917,22 @@ fn style_runs(run: &TextValue, notes: &mut RunNotes) -> Vec { horizontal_scale: num(style, "6", 1.0), vertical_scale: num(style, "7", 1.0), baseline_shift: num(style, "9", 0.0), - auto_kern: match style.get("11").and_then(TextValue::as_i64) { + auto_kern: match style.get("11").and_then(V::as_i64) { Some(0) => AutoKern::None, None | Some(1) => AutoKern::Metrics, Some(2) => AutoKern::Optical, Some(other) => AutoKern::Other(other), }, ligatures: flag(style, "18", false), - baseline_direction: style.get("35").and_then(TextValue::as_i64).unwrap_or(1), + baseline_direction: style.get("35").and_then(V::as_i64).unwrap_or(1), tsume: num(style, "36", 0.0), no_break: flag(style, "52", false), apply_fill: flag(style, "56", true), apply_stroke: flag(style, "57", false), stroke_over_fill: flag(style, "58", true), - line_join: style.get("62").and_then(TextValue::as_i64).unwrap_or(0), + line_join: style.get("62").and_then(V::as_i64).unwrap_or(0), stroke_width: num(style, "63", 1.0), - digit_set: style.get("70").and_then(TextValue::as_i64).unwrap_or(0), + digit_set: style.get("70").and_then(V::as_i64).unwrap_or(0), }); } out @@ -799,13 +940,13 @@ fn style_runs(run: &TextValue, notes: &mut RunNotes) -> Vec { /// The box of paragraph text from the frame dictionary `/0/8/0[0]/0`: key `/1/0` holds 16 x, y pairs /// that trace the rectangle, and `/2/0` is 1; both keys are absent for point text. -fn text_box(top: &TextValue) -> Option { +fn text_box(top: &V<'_>) -> Option { let frame = top.get("0")?.get("8")?.get("0")?.at(0)?.get("0")?; if frame.get("2")?.get("0")?.as_i64()? != 1 { return None; } - let TextValue::Array(coords) = frame.get("1")?.get("0")? else { return None }; - let numbers: Vec = coords.iter().map(TextValue::as_f64).collect::>()?; + let V::Array(coords) = frame.get("1")?.get("0")? else { return None }; + let numbers: Vec = coords.iter().map(V::as_f64).collect::>()?; let (pairs, _) = numbers.as_chunks::<2>(); if pairs.is_empty() { return None; @@ -819,18 +960,16 @@ fn text_box(top: &TextValue) -> Option { impl TextDocument { /// Decodes the body of a `btdk` list. pub fn parse(body: &[u8]) -> Option { - let top = parse_text_dict(body); + let top = parse_dict(body); let run = top.get("1")?.get("1")?.at(0)?.get("0")?; - let raw_text = run.get("0")?.as_str()?.to_string(); + let raw_text = run.get("0")?.string()?; let style = run.get("6")?.get("0")?.at(0)?.get("0")?.get("0")?.get("6")?; let fill_argb = colour_of(style, "53")?; let font_index = usize::try_from(style.get("0")?.as_i64()?).ok()?; let mut fonts = Vec::new(); - if let Some(TextValue::Array(list)) = top.get("0").and_then(|v| v.get("1")).and_then(|v| v.get("0")) { - fonts = list - .iter() - .map(|f| f.get("0").and_then(|v| v.get("0")).and_then(|v| v.get("0")).and_then(TextValue::as_str).unwrap_or_default().to_string()) - .collect(); + if let Some(V::Array(list)) = top.get("0").and_then(|v| v.get("1")).and_then(|v| v.get("0")) { + fonts = + list.iter().map(|f| f.get("0").and_then(|v| v.get("0")).and_then(|v| v.get("0")).and_then(V::string).unwrap_or_default()).collect(); } let mut notes = RunNotes::default(); let paragraphs = paragraph_runs(run, &mut notes); @@ -858,7 +997,7 @@ impl TextDocument { .and_then(|v| v.get("0")) .and_then(|v| v.get("2")) .and_then(|v| v.get("1")) - .and_then(TextValue::as_i64), + .and_then(V::as_i64), }) } @@ -882,23 +1021,32 @@ impl TextDocument { } impl<'f, 'a> Layer<'f, 'a> { - /// The text document of a text layer (`LIST btdk` below `ADBE Text Properties`). + /// The Source Text record `LIST btds` (`ADBE Text Document`). It is a direct child of the + /// `ADBE Text Properties` group, which is a direct child of the layer's root `tdgp` + /// (`Layr/tdgp/tdgp/btds` in every sample, and where the exporter writes it), so only that + /// level is searched rather than the whole layer. + fn source_text_record(&self) -> Option> { + let root = self.chunk.child_list(b"tdgp")?; + root.children().filter(|g| g.is_list(b"tdgp")).find_map(|g| g.child_list(b"btds")) + } + + /// The text document of a text layer: the first `LIST btdk` in the Source Text record `btds` + /// (the same documents [`Layer::text_document_count`] counts). pub fn text_document(&self) -> Option { - TextDocument::parse(self.chunk.find_list(b"btdk")?.bytes()?) + TextDocument::parse(self.source_text_record()?.find_list(b"btdk")?.bytes()?) } /// The Source Text property record: the `tdbs` in `btds[ADBE Text Document]` (a placeholder /// in every sample: no keys, no expression). Source Text keyframes and expressions were not /// sampled; a `list` (keys) or an expression in it marks them. pub fn source_text_property(&self) -> Option> { - let btds = self.chunk.descendants().map(|(_, c)| c).find(|c| c.is_list(b"btds"))?; - Property::from_chunk(btds.child_list(b"tdbs")?) + Property::from_chunk(self.source_text_record()?.child_list(b"tdbs")?) } - /// The number of text documents (`LIST btdk`) of the layer: 1 in every sample; more would be - /// one per Source Text keyframe (not sampled). + /// The number of text documents (`LIST btdk`) in the Source Text record: 1 in every sample; + /// more would be one per Source Text keyframe (not sampled). pub fn text_document_count(&self) -> usize { - self.chunk.descendants().filter(|(_, c)| c.is_list(b"btdk")).count() + self.source_text_record().map_or(0, |btds| btds.descendants().filter(|(_, c)| c.is_list(b"btdk")).count()) } } @@ -943,10 +1091,24 @@ pub enum CueType { Other(u32), } +impl<'f, 'a> Node<'f, 'a> { + /// The marker list `LIST mrst` of a marker holder: a layer (`LIST Layr`) or a composition's + /// `SecL` list. A layer's `mrst` is a direct child of its root `tdgp` (`ADBE Marker`, where + /// After Effects and the exporter write it), so only that place is looked at; most layers have + /// none, and searching their whole property tree for it was a large part of an import. Any + /// other holder is searched depth-first. + pub fn marker_list(self) -> Option> { + if self.is_list(b"Layr") { + return self.child_list(b"tdgp")?.child_list(b"mrst"); + } + self.find_list(b"mrst") + } +} + /// Reads the markers below `holder` (a layer or the composition's `SecL` list): the `mrst` list has a /// property whose 16-byte records give the times and one `Nmrd` list per marker with the strings. -fn markers_in(holder: &Chunk<'_>) -> Vec { - let Some(mrst) = holder.find_list(b"mrst") else { return Vec::new() }; +fn markers_in(holder: Node<'_, '_>) -> Vec { + let Some(mrst) = holder.marker_list() else { return Vec::new() }; let Some(prop) = mrst.child_list(b"tdbs").and_then(Property::from_chunk) else { return Vec::new() }; let Some(keys) = prop.keys() else { return Vec::new() }; let scale = prop.time_scale().unwrap_or(0); @@ -956,11 +1118,11 @@ fn markers_in(holder: &Chunk<'_>) -> Vec { .filter_map(|(k, nmrd)| { let time_units = k.time_units()?; // The five strings, in the order comment, chapter, URL, frame target, cue point name. - let mut strings = nmrd.children().iter().filter(|c| c.is(b"Utf8")).map(|c| c.bytes().map(text).unwrap_or_default()); + let mut strings = nmrd.children().filter(|c| c.is(b"Utf8")).map(|c| c.bytes().map(text).unwrap_or_default()); let mut next = || strings.next().unwrap_or_default(); let (comment, chapter, url, frame_target, cue_point_name) = (next(), next(), next(), next(), next()); let extras: Vec = strings.collect(); - let header = nmrd.child(b"NmHd").and_then(Chunk::bytes); + let header = nmrd.child(b"NmHd").and_then(Node::bytes); let duration_seconds = header.and_then(|h| { let (duration, per_second) = (be_u32(h, 8)?, be_u32(h, 12)?); (per_second != 0).then(|| f64::from(duration) / f64::from(per_second)) @@ -1003,9 +1165,647 @@ impl<'f, 'a> Comp<'f, 'a> { } } +/// The dictionary parser and the defaults as they were before they borrowed from the body (#3276), +/// kept as the reference the differential tests compare with. +#[cfg(test)] +mod oracle { + use super::{MAX_DEPTH, MAX_UNDECODED_FIELDS, TextValue}; + use crate::bytes::text; + + pub(super) fn same(a: &TextValue, b: &TextValue) -> bool { + match (a, b) { + (TextValue::Dict(x), TextValue::Dict(y)) => x.len() == y.len() && x.iter().all(|(k, v)| y.iter().any(|(k2, v2)| k == k2 && same(v, v2))), + (TextValue::Array(x), TextValue::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(p, q)| same(p, q)), + _ => match (a.as_f64(), b.as_f64()) { + (Some(p), Some(q)) => p == q, + _ => a == b, + }, + } + } + + pub(super) fn is_neutral(v: &TextValue) -> bool { + match v { + TextValue::Name(n) => n == "nil", + TextValue::Bool(b) => !b, + TextValue::Array(a) => a.is_empty(), + TextValue::Dict(d) => d.is_empty(), + other => other.as_f64() == Some(0.0), + } + } + + pub(super) fn undecoded_fields( + actual: &TextValue, + sampled: Option<&TextValue>, + decoded: &[&str], + neutral: &[(&str, &[i64])], + out: &mut Vec, + ) { + let TextValue::Dict(pairs) = actual else { return }; + for (key, value) in pairs { + if out.len() >= MAX_UNDECODED_FIELDS { + return; + } + if decoded.contains(&key.as_str()) || out.contains(key) { + continue; + } + if neutral.iter().any(|(slot, values)| slot == key && value.as_i64().is_some_and(|v| values.contains(&v))) { + continue; + } + let differs = match sampled.and_then(|s| s.get(key)) { + Some(default) => !same(value, default), + None => !is_neutral(value), + }; + if differs { + out.push(key.clone()); + } + } + } + + impl TextValue { + /// The canonical text of a key, as the owning parser built it. + fn key_text(&self) -> String { + match self { + Self::Name(s) | Self::Str(s) | Self::Token(s) => s.clone(), + Self::Int(i) => i.to_string(), + Self::Float(f) => f.to_string(), + Self::Bool(b) => b.to_string(), + Self::Bytes(b) => text(b), + Self::Dict(_) | Self::Array(_) => String::new(), + } + } + } + + struct Parser<'b> { + b: &'b [u8], + i: usize, + } + + /// Whitespace between tokens. + fn is_ws(c: u8) -> bool { + matches!(c, b' ' | b'\t' | b'\r' | b'\n') + } + + /// A byte that ends a bare word. + fn is_delimiter(c: u8) -> bool { + is_ws(c) || matches!(c, b'/' | b'[' | b']' | b'<' | b'>' | b'(' | b')') + } + + impl Parser<'_> { + /// The next byte, `None` at the end. + fn peek(&self) -> Option { + self.b.get(self.i).copied() + } + + /// Moves past whitespace. + fn skip_ws(&mut self) { + while self.peek().is_some_and(is_ws) { + self.i = self.i.saturating_add(1); + } + } + + /// True when the unread bytes start with `s`. + fn starts_with(&self, s: &[u8]) -> bool { + self.b.get(self.i..).is_some_and(|rest| rest.starts_with(s)) + } + + /// The bare word at the position (up to the next delimiter), borrowed from the body. + fn word(&mut self) -> &[u8] { + let start = self.i; + while self.peek().is_some_and(|c| !is_delimiter(c)) { + self.i = self.i.saturating_add(1); + } + self.b.get(start..self.i).unwrap_or(&[]) + } + + /// The value at the position, `None` when it is malformed or nested deeper than the limit. + fn parse(&mut self, depth: usize) -> Option { + self.skip_ws(); + if depth > MAX_DEPTH { + return None; + } + if self.starts_with(b"<<") { + self.i = self.i.saturating_add(2); + let mut pairs = Vec::new(); + loop { + self.skip_ws(); + if self.starts_with(b">>") { + self.i = self.i.saturating_add(2); + return Some(TextValue::Dict(pairs)); + } + let key = self.parse(depth.saturating_add(1))?; + let value = self.parse(depth.saturating_add(1))?; + pairs.push((key.key_text(), value)); + } + } + match self.peek()? { + b'[' => { + self.i = self.i.saturating_add(1); + let mut items = Vec::new(); + loop { + self.skip_ws(); + if self.peek()? == b']' { + self.i = self.i.saturating_add(1); + return Some(TextValue::Array(items)); + } + items.push(self.parse(depth.saturating_add(1))?); + } + } + b'(' => { + self.i = self.i.saturating_add(1); + let mut out = Vec::new(); + loop { + let c = self.peek()?; + if c == b'\\' { + out.push(*self.b.get(self.i.saturating_add(1))?); + self.i = self.i.saturating_add(2); + } else if c == b')' { + self.i = self.i.saturating_add(1); + break; + } else { + out.push(c); + self.i = self.i.saturating_add(1); + } + } + Some(match out.strip_prefix(&[0xfe, 0xff]) { + Some(utf16) => TextValue::Str(decode_utf16be(utf16)), + None => TextValue::Bytes(out), + }) + } + b'/' => { + self.i = self.i.saturating_add(1); + Some(TextValue::Name(text(self.word()))) + } + _ => { + let word = self.word(); + if word.is_empty() { + return None; + } + let w = text(word); + Some(match w.as_str() { + "true" => TextValue::Bool(true), + "false" => TextValue::Bool(false), + _ => match w.parse::() { + Ok(i) => TextValue::Int(i), + // `parse` also takes `nan`, `inf` and overflowing exponents; those are not + // numbers a text document holds, so they stay tokens and read as missing. + Err(_) => match w.parse::() { + Ok(f) if f.is_finite() => TextValue::Float(f), + _ => TextValue::Token(w), + }, + }, + }) + } + } + } + } + + /// A big-endian UTF-16 string as text (unpaired surrogates become U+FFFD). + fn decode_utf16be(bytes: &[u8]) -> String { + let (pairs, rest) = bytes.as_chunks::<2>(); + let units = pairs.iter().map(|p| u16::from_be_bytes(*p)); + let mut s: String = char::decode_utf16(units).map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER)).collect(); + if !rest.is_empty() { + s.push(char::REPLACEMENT_CHARACTER); + } + s + } + + /// Parses the body of a `btdk` list into its top-level `key value` pairs (as a [`TextValue::Dict`]). + /// Parsing stops silently at the first syntax error or at nesting deeper than 64 levels; what was + /// read before is returned. + pub(super) fn parse_text_dict(body: &[u8]) -> TextValue { + let mut parser = Parser { b: body, i: 0 }; + let mut pairs = Vec::new(); + loop { + parser.skip_ws(); + if parser.i >= body.len() { + break; + } + let Some(key) = parser.parse(0) else { break }; + let Some(value) = parser.parse(0) else { break }; + pairs.push((key.key_text(), value)); + } + TextValue::Dict(pairs) + } + + fn val(v: f64) -> TextValue { + TextValue::Float(v) + } + + /// A dictionary value with these keys, for the built-in defaults. + fn dict(pairs: Vec<(&str, TextValue)>) -> TextValue { + TextValue::Dict(pairs.into_iter().map(|(k, v)| (k.to_string(), v)).collect()) + } + + /// An array of numbers, for the built-in defaults. + fn numbers(v: &[f64]) -> TextValue { + TextValue::Array(v.iter().map(|x| val(*x)).collect()) + } + + /// The default of every run style slot that is not decoded and not neutral (zero, `false`, + /// empty, `/nil`): the value an untouched point text holds in every sample (110, 243-245). A slot + /// with a neutral default is left out; [`undecoded_fields`] treats a missing default as neutral. + pub(super) fn style_defaults() -> TextValue { + let paint = |a: f64| { + dict(vec![("99", TextValue::Name("SimplePaint".into())), ("0", dict(vec![("0", val(1.0)), ("1", numbers(&[1.0, 1.0, 1.0, a]))]))]) + }; + let class = |name: &str| dict(vec![("99", TextValue::Name(name.into()))]); + dict(vec![ + ("37", val(2.0)), + ("41", val(2.0)), + ("43", dict(vec![("0", val(0.5))])), + ("44", val(2.0)), + ("45", val(2.0)), + ("46", val(7.0)), + ("49", val(-1.0)), + ("50", val(-1.0)), + ("55", class("SimpleBlender")), + ("64", val(4.0)), + ("71", val(4.0)), + ("77", TextValue::Bool(true)), + ("78", TextValue::Bool(true)), + ("79", paint(0.0)), + ("82", val(3.0)), + ("83", val(3.0)), + ("86", class("SimpleCustomFeature")), + ("87", val(100.0)), + ("88", TextValue::Bool(true)), + ("95", dict(vec![("0", val(0.0)), ("1", val(0.0))])), + ("96", TextValue::Bool(true)), + ]) + } + + /// The same for the paragraph slots (110, 243-245). + pub(super) fn paragraph_defaults() -> TextValue { + dict(vec![ + ("6", val(1.0)), + ("7", val(1.2)), + ("10", val(6.0)), + ("11", val(2.0)), + ("12", val(2.0)), + ("14", val(36.0)), + ("15", TextValue::Bool(true)), + ("16", val(0.5)), + ("17", numbers(&[0.8, 1.0, 1.33])), + ("18", numbers(&[0.0, 0.0, 0.0])), + ("19", numbers(&[1.0, 1.0, 1.0])), + ("20", val(6.0)), + ("22", val(1.0)), + ("23", TextValue::Bool(true)), + ("24", val(1.0)), + ("26", TextValue::Bool(true)), + ("31", val(36.0)), + ("35", val(1.0)), + ("40", val(2.0)), + ]) + } +} + #[cfg(test)] mod tests { use super::*; + use crate::Chunk; + + /// A `LIST` of this type holding `kids`. + fn list(t: &[u8; 4], kids: Vec>) -> Chunk<'static> { + Chunk::list(*t, kids) + } + + /// A document with every kind of value, close to what After Effects writes: names, numbers, + /// a UTF-16 string with escapes, a plain string, nested dictionaries and arrays, an undecoded + /// style slot, a font list and a box frame. + const DOC: &[u8] = b"/0 << /1 << /0 [ << /0 << /0 << /0 (\xfe\xff\0M\0y\0r) >> /1 1 >> >> << /0 << /0 << /0 (\xfe\xff\0A\0\\)\0b) >> >> >> ] >> \ + /8 << /0 [ << /0 << /1 << /0 [ 0 0 10 0 10 5 0 5 ] >> /2 << /0 1 /1 2 >> >> >> ] >> >> \ + /1 << /1 [ << /0 << /0 (\xfe\xff\0H\0i\0\r) /5 << /0 [ << /0 << /0 << /5 << /0 2 /9 false /24 0 /34 7 /17 [ .8 1.0 1.33 ] >> >> >> /1 3 >> ] >> \ + /6 << /0 [ << /0 << /0 << /6 << /0 1 /1 36.5 /53 << /0 << /0 1 /1 [ 1 0.5 0 1 ] >> >> /79 << /99 /SimplePaint /0 << /0 1 /1 [ 1 1 1 0 ] >> >> /68 1 /90 /x /91 (plain) /92 word >> >> >> /1 3 >> ] >> \ + /8 << /0 [ << /0 << /0 50 >> /1 2 >> << /1 1 >> ] >> >> >> ] >>"; + + /// A deterministic xorshift generator: the tests need varied input, not a dependency. + struct Rng(u64); + + impl Rng { + /// The next pseudo-random number (xorshift). + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + + /// A number below `n` (0 for `n == 0`). + fn below(&mut self, n: usize) -> usize { + usize::try_from(self.next() % (n as u64)).unwrap_or(0) + } + } + + /// The borrowing parser reads what the owning one read. + #[test] + fn the_borrowing_parser_reads_what_the_owning_one_read() { + // Every key form the owned keys canonicalise (`06`, `+6`, `-0`, `1.0`, `1e2`, strings, + // booleans, containers), names that are not UTF-8, escapes, unclosed strings and escapes, + // non-finite numbers, and bare words. + let parts: &[&[u8]] = &[ + b"<<", + b">>", + b"[", + b"]", + b"/0", + b"/1", + b"/nil", + b"/SimplePaint", + b"/\xff\xfe", + b"/\xc3\xa9", + b"/", + b"06", + b"+6", + b"-0", + b"1.0", + b"1.50", + b"1e2", + b"-2.5", + b".8", + b"inf", + b"nan", + b"1e999", + b"9223372036854775808", + b"true", + b"false", + b"(abc)", + b"(\xfe\xff\0A\0B)", + b"(\xfe\xff\0A\0)", + b"(a\\)b)", + b"(\\", + b"(\xfe\xff\\)\0)", + b"(\\\xfe\xff\0C)", + b"()", + b"(\xfe\xff)", + b"(unclosed", + b"word", + b"\xc3\x28", + b"\xff", + b" ", + b"\n", + b"\t\r", + b"<", + b">", + b")", + ]; + // A value: mostly well formed, with a random part in its place now and then. + /// Appends one generated value to `out`: mostly well formed, now and then a random part. + fn value(rng: &mut Rng, parts: &[&[u8]], depth: usize, out: &mut Vec) { + let pick = |rng: &mut Rng| parts.get(rng.below(parts.len())).copied().unwrap_or_default(); + match rng.below(if depth > 6 { 5 } else { 10 }) { + 0 => out.extend_from_slice(pick(rng)), + 1..=4 => out.extend_from_slice([&b"/0"[..], b"1", b"(\xfe\xff\0z)", b"0.5", b"/nil"].get(rng.below(5)).copied().unwrap_or_default()), + 5..=7 => { + out.extend_from_slice(b"<< "); + for _ in 0..rng.below(5) { + let key = if rng.below(4) == 0 { + pick(rng) + } else { + [&b"/0"[..], b"/1", b"/6", b"/53"].get(rng.below(4)).copied().unwrap_or_default() + }; + out.extend_from_slice(key); + out.push(b' '); + value(rng, parts, depth + 1, out); + out.push(b' '); + } + if rng.below(20) != 0 { + out.extend_from_slice(b">>"); + } + } + _ => { + out.extend_from_slice(b"[ "); + for _ in 0..rng.below(5) { + value(rng, parts, depth + 1, out); + out.push(b' '); + } + if rng.below(20) != 0 { + out.push(b']'); + } + } + } + } + let mut rng = Rng(0x9e37_79b9_7f4a_7c15); + let (mut read, mut nested) = (0, 0); + for case in 0..40_000 { + let mut body = Vec::new(); + if case % 2 == 0 { + for _ in 0..rng.below(60) { + body.extend_from_slice(parts.get(rng.below(parts.len())).copied().unwrap_or_default()); + if rng.below(3) != 0 { + body.push(b' '); + } + } + } else { + for _ in 0..1 + rng.below(4) { + body.extend_from_slice(b"/0 "); + value(&mut rng, parts, 0, &mut body); + body.push(b'\n'); + } + } + let parsed = parse_text_dict(&body); + assert_eq!(parsed, oracle::parse_text_dict(&body), "case {case}: {}", String::from_utf8_lossy(&body)); + let _ = TextDocument::parse(&body); + if let TextValue::Dict(pairs) = &parsed { + read += usize::from(!pairs.is_empty()); + nested += usize::from(pairs.iter().any(|(_, v)| matches!(v, TextValue::Dict(d) if !d.is_empty()))); + } + } + // The input is not all rejected at the first token. + assert!(read > 25_000 && nested > 3_000, "{read} cases read something, {nested} a dictionary"); + // Every cut of a realistic document, and nesting past the limit. + for end in 0..=DOC.len() { + let body = DOC.get(..end).unwrap_or_default(); + assert_eq!(parse_text_dict(body), oracle::parse_text_dict(body), "cut at {end}"); + let _ = TextDocument::parse(body); + } + for depth in [63, 64, 65, 66, 200] { + let body = [b"/k ".to_vec(), b"<< /a ".repeat(depth), b"1".to_vec(), b" >>".repeat(depth), b" /z 2".to_vec()].concat(); + assert_eq!(parse_text_dict(&body), oracle::parse_text_dict(&body), "depth {depth}"); + let body = [b"/k ".to_vec(), b"[ ".repeat(depth), b"]".repeat(depth)].concat(); + assert_eq!(parse_text_dict(&body), oracle::parse_text_dict(&body), "array depth {depth}"); + } + // Dictionaries and arrays mixed (`<< /a [ << /a [ ...`), closed, and left open exactly + // at the limit. + for depth in 60..=68 { + let open: Vec = (0..depth).flat_map(|i| if i % 2 == 0 { &b"<< /a "[..] } else { &b"[ "[..] }).copied().collect(); + let close: Vec = (0..depth).rev().flat_map(|i| if i % 2 == 0 { &b" >>"[..] } else { &b" ]"[..] }).copied().collect(); + for body in + [[b"/k ".to_vec(), open.clone(), b"1".to_vec(), close, b" /z 2".to_vec()].concat(), [b"/k ".to_vec(), open, b"1".to_vec()].concat()] + { + assert_eq!(parse_text_dict(&body), oracle::parse_text_dict(&body), "mixed depth {depth}"); + let _ = TextDocument::parse(&body); + } + } + } + + /// A realistic document is read in full. + #[test] + fn a_realistic_document_is_read_in_full() { + let doc = TextDocument::parse(DOC).expect("a document"); + assert_eq!((doc.text.as_str(), doc.font_size, doc.fill_argb, doc.font.as_deref()), ("Hi", 36.5, [1.0, 0.5, 0.0, 1.0], Some("A)b"))); + assert_eq!(doc.fonts, ["Myr", "A)b"]); + assert_eq!(doc.paragraphs.first().map(|p| (p.length, p.justification, p.auto_hyphenate)), Some((3, Justification::Center, false))); + // `/68 1` and `/34 7` are neutral; `/90 /x`, `/91 (plain)` and `/92 word` differ from the + // (missing, neutral) default; `/79` equals its default. + assert_eq!(doc.undecoded_style_fields, ["90", "91", "92"]); + assert!(doc.undecoded_paragraph_fields.is_empty()); + assert_eq!(doc.kerning, [KernRun { length: 2, amount: Some(50.0) }, KernRun { length: 1, amount: None }]); + assert_eq!(doc.text_box, Some(TextBox { x: 0.0, y: 0.0, width: 10.0, height: 5.0 })); + assert_eq!(doc.line_orientation, Some(2)); + } + + /// The defaults are the sampled ones and built once. + #[test] + fn the_defaults_are_the_sampled_ones_and_built_once() { + assert_eq!(STYLE_DEFAULTS.clone().into_owned(), oracle::style_defaults()); + assert_eq!(PARAGRAPH_DEFAULTS.clone().into_owned(), oracle::paragraph_defaults()); + assert!(std::ptr::eq(&*STYLE_DEFAULTS, &*STYLE_DEFAULTS)); + } + + /// Names keys and strings are borrowed from the body. + #[test] + fn names_keys_and_strings_are_borrowed_from_the_body() { + let V::Dict(pairs) = parse_dict(b"/a /b /c (\xfe\xff\0x) /d (plain) /e word /f 1.5 /g (\\(esc) 07 1") else { panic!("a dict") }; + // Every `/name` key, name, string without escapes and bare word points into the body. + assert!(pairs.iter().take(6).all(|(k, _)| matches!(k, Cow::Borrowed(_)))); + assert!(matches!(pairs.first().map(|p| &p.1), Some(V::Name(b"b")))); + assert!(matches!(pairs.get(1).map(|p| &p.1), Some(V::Str(Cow::Borrowed(b"\0x"))))); + assert!(matches!(pairs.get(2).map(|p| &p.1), Some(V::Bytes(Cow::Borrowed(b"plain"))))); + assert!(matches!(pairs.get(3).map(|p| &p.1), Some(V::Token(b"word")))); + // An escaped string is copied; a number key is written in its canonical form. + assert!(matches!(pairs.get(5).map(|p| &p.1), Some(V::Bytes(Cow::Owned(b))) if b == b"(esc")); + assert_eq!(pairs.get(6).map(|p| p.0.as_ref()), Some("7")); + // Containers hold what was read. + let V::Dict(outer) = parse_dict(b"/0 << /a 1 /b 2 /c 3 /d 4 /e 5 >> /1 [ 1 2 3 4 5 6 7 8 9 ]") else { panic!("a dict") }; + assert!(matches!(outer.first().map(|p| &p.1), Some(V::Dict(d)) if d.len() == 5 && d.capacity() >= d.len())); + assert!(matches!(outer.get(1).map(|p| &p.1), Some(V::Array(a)) if a.len() == 9 && a.capacity() >= a.len())); + } + + /// Slot checks agree with the owned ones. + #[test] + fn slot_checks_agree_with_the_owned_ones() { + // Slots: defaults, decoded, neutral-listed and unknown ones. Values: each default as + // stored (also as `Int` where it is `2.0`), near misses, neutral values and every kind. + let slots = ["0", "6", "7", "17", "24", "34", "35", "37", "43", "55", "68", "77", "79", "86", "95", "96", "200", "x"]; + let values: &[&[u8]] = &[ + b"2", + b"2.0", + b"7", + b"1", + b"0", + b"-1", + b"0.5", + b"1.2", + b"36", + b"true", + b"false", + b"/nil", + b"/x", + b"/SimpleBlender", + b"[ ]", + b"<< >>", + b"[ .8 1 1.33 ]", + b"[ 0.8 1.0 ]", + b"[ 1 1 1 ]", + b"<< /0 .5 >>", + b"<< /0 0 /1 0 >>", + b"<< /1 0 /0 0 >>", + b"<< /99 /SimpleBlender >>", + b"<< /99 /SimplePaint /0 << /0 1 /1 [ 1 1 1 0 ] >> >>", + b"<< /99 /SimplePaint /0 << /0 1 /1 [ 1 1 1 1 ] >> >>", + b"(plain)", + b"(\xfe\xff\0a)", + b"word", + b"/\xff", + ]; + let mut rng = Rng(0x2545_f491_4f6c_dd1d); + let mut listed = 0; + for case in 0..20_000 { + let mut body = b"/s << ".to_vec(); + for _ in 0..rng.below(12) { + body.push(b'/'); + body.extend_from_slice(slots.get(rng.below(slots.len())).copied().unwrap_or_default().as_bytes()); + body.push(b' '); + body.extend_from_slice(values.get(rng.below(values.len())).copied().unwrap_or_default()); + body.push(b' '); + } + body.extend_from_slice(b">>"); + let (new, old) = (parse_dict(&body), oracle::parse_text_dict(&body)); + let (Some(new), Some(old)) = (new.get("s"), old.get("s")) else { panic!("case {case}: a dict") }; + for (sampled, old_sampled, decoded, neutral) in [ + (&*STYLE_DEFAULTS, oracle::style_defaults(), DECODED_STYLE_SLOTS, NEUTRAL_STYLE_VALUES), + (&*PARAGRAPH_DEFAULTS, oracle::paragraph_defaults(), DECODED_PARAGRAPH_SLOTS, NEUTRAL_PARAGRAPH_VALUES), + ] { + let (mut a, mut b) = (Vec::new(), Vec::new()); + undecoded_fields(new, Some(sampled), decoded, neutral, &mut a); + oracle::undecoded_fields(old, Some(&old_sampled), decoded, neutral, &mut b); + assert_eq!(a, b, "case {case}: {}", String::from_utf8_lossy(&body)); + listed += a.len(); + } + // Every pair of values compares and reads as neutral alike. + if let (V::Dict(x), TextValue::Dict(y)) = (new, old) { + for ((_, p), (_, q)) in x.iter().zip(y) { + assert_eq!(is_neutral(p), oracle::is_neutral(q), "case {case}"); + for ((_, r), (_, t)) in x.iter().zip(y) { + assert_eq!(same(p, r), oracle::same(q, t), "case {case}"); + } + } + } + } + assert!(listed > 10_000, "{listed} slots listed"); + } + + /// Values compare by their text. + #[test] + fn values_compare_by_their_text() { + // Two names that are not UTF-8 read as the same text with replacement characters, as the + // owned values did. + assert!(same(&V::Name(b"\xff"), &V::Name(b"\xfe"))); + assert!(!same(&V::Name(b"a"), &V::Name(b"b"))); + assert!(same(&V::Str(Cow::Borrowed(b"\xd8\x00")), &V::Str(Cow::Owned(b"\xd8\x01".to_vec())))); + assert!(same(&V::Int(0), &V::Float(0.0)) && !same(&V::Int(0), &V::Bool(false)) && !same(&V::Name(b"x"), &V::Token(b"x"))); + assert!( + is_neutral(&V::Name(b"nil")) && !is_neutral(&V::Token(b"nil")) && is_neutral(&V::Float(0.0)) && !is_neutral(&V::Str(Cow::Borrowed(b""))) + ); + } + + /// A layers marker list is the one in its root group. + #[test] + fn a_layers_marker_list_is_the_one_in_its_root_group() { + let marked = |tag: u8| list(b"mrst", vec![Chunk::data(*b"test", vec![tag])]); + // The stored place: `Layr/tdgp/mrst`. + let layer = list(b"Layr", vec![list(b"tdgp", vec![list(b"tdgp", vec![]), marked(1)])]); + assert_eq!(Node::from(&layer).marker_list().and_then(|m| m.child(b"test")).and_then(Node::bytes), Some(&[1u8][..])); + // A list deeper in the layer is not the layer's marker list (the old depth-first search + // found it); a layer without a marker list in its root group has no markers. + let deep = list(b"Layr", vec![list(b"tdgp", vec![list(b"tdgp", vec![marked(2)])])]); + assert!(Node::from(&deep).marker_list().is_none()); + assert!(Layer { chunk: Node::from(&deep) }.markers().is_empty()); + // Any other holder (a composition's `SecL`) is still searched depth-first. + let secl = list(b"SecL", vec![list(b"tdgp", vec![list(b"tdgp", vec![marked(3)])])]); + assert_eq!(Node::from(&secl).marker_list().and_then(|m| m.child(b"test")).and_then(Node::bytes), Some(&[3u8][..])); + } + + /// The source text record is found in the text group. + #[test] + fn the_source_text_record_is_found_in_the_text_group() { + let doc = b"/1 << /1 [ << /0 << /0 (\xfe\xff\0H\0\r) /6 << /0 [ << /0 << /0 << /6 << /0 0 /1 50 /53 << /0 << /1 [ 1 1 0 0 ] >> >> >> >> >> /1 2 >> ] >> >> >> ] >>"; + // `btdk` is an opaque list (its body is not a chunk sequence), as the reader makes it. + let btdk = || Chunk { id: *b"LIST", body: crate::Body::OpaqueList { list_type: *b"btdk", data: doc.to_vec().into() }, pad: None }; + let btds_real = |n: usize| list(b"btds", std::iter::once(list(b"tdbs", vec![])).chain((0..n).map(|_| btdk())).collect()); + let layer = list(b"Layr", vec![list(b"tdgp", vec![list(b"tdgp", vec![]), list(b"tdgp", vec![btds_real(2)])])]); + let view = Layer { chunk: Node::from(&layer) }; + assert_eq!(view.text_document().map(|d| d.text), Some("H".to_string())); + assert!(view.source_text_property().is_some()); + assert_eq!(view.text_document_count(), 2); + // A record anywhere else in the layer is not its Source Text. + let elsewhere = list(b"Layr", vec![list(b"tdgp", vec![list(b"tdgp", vec![list(b"tdgp", vec![btds_real(1)])])])]); + let view = Layer { chunk: Node::from(&elsewhere) }; + assert!(view.text_document().is_none() && view.source_text_property().is_none()); + assert_eq!(view.text_document_count(), 0); + } #[test] fn non_finite_numbers_are_not_numbers() { @@ -1018,13 +1818,13 @@ mod tests { fn undecoded_fields_are_capped_and_linear() { // 300 000 distinct undecoded slots that differ from a sampled default: before, each was // checked against every slot recorded so far (4.5e10 compares) and all were listed. - let actual = TextValue::Dict((0..300_000).map(|i| (format!("{}", 9000 + i), TextValue::Int(1))).collect()); + let actual = V::Dict((0..300_000).map(|i| (Cow::Owned(format!("{}", 9000 + i)), V::Int(1))).collect()); let mut out = Vec::new(); undecoded_fields(&actual, None, &["0"], &[], &mut out); assert_eq!(out.len(), MAX_UNDECODED_FIELDS); assert_eq!(out.first().map(String::as_str), Some("9000")); // A slot listed once is not listed again. - let again = TextValue::Dict(vec![("9000".into(), TextValue::Int(1))]); + let again = V::Dict(vec![(Cow::Borrowed("9000"), V::Int(1))]); let mut short = vec!["9000".to_string()]; undecoded_fields(&again, None, &[], &[], &mut short); assert_eq!(short, ["9000"]); diff --git a/crates/aep/src/typed.rs b/crates/aep/src/typed.rs index 16fcda33c..9334b45cc 100644 --- a/crates/aep/src/typed.rs +++ b/crates/aep/src/typed.rs @@ -4,42 +4,57 @@ //! //! Only facts that were established from our own one-change-per-file corpus and are checked by //! tests against it are decoded (see `docs/aep-format.md` for the evidence). Everything else -//! stays reachable through the raw [`Chunk`] tree. Views borrow the file and never fail loudly: -//! a chunk that does not have the expected shape yields `None` or is skipped. +//! stays reachable through the raw chunk tree ([`Node`]). Views borrow the file and never fail +//! loudly: a chunk that does not have the expected shape yields `None` or is skipped. +//! +//! A view reads either tree: the owned [`Chunk`](crate::Chunk) tree +//! ([`Project::new`]) or the flat [`TreeIndex`] the importer builds ([`Project::from_index`]); +//! both give the same answers. use crate::bytes::{be_f64, be_u16, be_u32, text, until_nul}; -use crate::chunk::{AepFile, Chunk}; +use crate::chunk::AepFile; +use crate::index::{Node, Root, TreeIndex}; /// The name a user gave a property or group: the `Utf8` inside its `tdsn` chunk. `None` for the /// sentinel `-_0_/-` ("no custom name") and for empty names. -pub(crate) fn user_name(chunk: &Chunk<'_>) -> Option { - let name = chunk.child(b"tdsn")?.children().iter().find(|c| c.is(b"Utf8"))?.bytes().map(text)?; +pub(crate) fn user_name(chunk: Node<'_, '_>) -> Option { + let name = chunk.child(b"tdsn")?.child(b"Utf8")?.bytes().map(text)?; (!name.is_empty() && name != "-_0_/-").then_some(name) } /// The content of a `Utf8` child as text (not trimmed). -pub(crate) fn utf8_child(chunk: &Chunk<'_>) -> Option { - chunk.child(b"Utf8").and_then(Chunk::bytes).map(text) +pub(crate) fn utf8_child(chunk: Node<'_, '_>) -> Option { + chunk.child(b"Utf8").and_then(Node::bytes).map(text) } /// The project: the top-level `LIST Fold`, which holds the project items. #[derive(Debug, Clone, Copy)] pub struct Project<'f, 'a> { - pub(crate) fold: &'f Chunk<'a>, + pub(crate) fold: Node<'f, 'a>, /// The top-level `LIST EfdG` (project-level effect definitions), when there is one. - pub(crate) effect_defs: Option<&'f Chunk<'a>>, + pub(crate) effect_defs: Option>, } impl<'f, 'a> Project<'f, 'a> { /// The project view of a file, or `None` when it has no `LIST Fold`. pub fn new(file: &'f AepFile<'a>) -> Option { - file.list(b"Fold").map(|fold| Self { fold, effect_defs: file.list(b"EfdG") }) + Self::of(Root::from(file)) + } + + /// The project view of an indexed file ([`TreeIndex`]), or `None` when it has no `LIST Fold`. + pub fn from_index(index: &'f TreeIndex<'a>) -> Option { + Self::of(Root::from(index)) + } + + /// The project view of either tree: its `LIST Fold` and `LIST EfdG`. + pub fn of(root: Root<'f, 'a>) -> Option { + root.list(b"Fold").map(|fold| Self { fold, effect_defs: root.list(b"EfdG") }) } /// The top-level items (the project panel's root; folders hold their children, see /// [`Item::children`]). pub fn items(&self) -> impl Iterator> + 'f { - self.fold.children().iter().filter(|c| c.is_list(b"Item")).map(|chunk| Item { chunk }) + self.fold.children().filter(|c| c.is_list(b"Item")).map(|chunk| Item { chunk }) } /// True when the `LIST Fold` could not be read as chunks (damaged): its items cannot be seen. @@ -78,12 +93,12 @@ pub enum ItemKind { /// A project item (`LIST Item`). #[derive(Debug, Clone, Copy)] pub struct Item<'f, 'a> { - pub(crate) chunk: &'f Chunk<'a>, + pub(crate) chunk: Node<'f, 'a>, } impl<'f, 'a> Item<'f, 'a> { /// The underlying chunk. - pub fn chunk(&self) -> &'f Chunk<'a> { + pub fn chunk(&self) -> Node<'f, 'a> { self.chunk } @@ -106,7 +121,7 @@ impl<'f, 'a> Item<'f, 'a> { /// The items inside a folder (`LIST Sfdr`). pub fn children(&self) -> impl Iterator> + 'f { - self.chunk.child_list(b"Sfdr").into_iter().flat_map(|sfdr| sfdr.children().iter()).filter(|c| c.is_list(b"Item")).map(|chunk| Item { chunk }) + self.chunk.child_list(b"Sfdr").into_iter().flat_map(Node::children).filter(|c| c.is_list(b"Item")).map(|chunk| Item { chunk }) } /// Whether the item has the `LIST Sfdr` of a folder's contents (After Effects writes an empty @@ -117,7 +132,7 @@ impl<'f, 'a> Item<'f, 'a> { /// True when the folder's `LIST Sfdr` is there but could not be read as chunks (damaged). pub fn folder_contents_unreadable(&self) -> bool { - self.chunk.child_list(b"Sfdr").is_some_and(Chunk::is_opaque_list) + self.chunk.child_list(b"Sfdr").is_some_and(Node::is_opaque_list) } /// The composition view when this item has composition data (`cdta`). @@ -135,6 +150,10 @@ pub struct TimeBase { } impl TimeBase { + /// The NTSC rates as `(numerator, denominator)` and the value After Effects stores for each (a + /// float32 of exactly these decimals): the one table an importer and an exporter use. + pub const NTSC: [((u32, u32), f32); 4] = [((24000, 1001), 23.976), ((30000, 1001), 29.97), ((60000, 1001), 59.94), ((120000, 1001), 119.88)]; + /// Frames per second (`timescale / frame_duration`), `None` for a zero duration. pub fn frame_rate(&self) -> Option { (self.frame_duration != 0).then(|| f64::from(self.timescale) / f64::from(self.frame_duration)) @@ -149,7 +168,7 @@ impl TimeBase { /// A composition item (an [`Item`] with `cdta`). #[derive(Debug, Clone, Copy)] pub struct Comp<'f, 'a> { - pub(crate) item: &'f Chunk<'a>, + pub(crate) item: Node<'f, 'a>, } impl<'f, 'a> Comp<'f, 'a> { @@ -163,19 +182,19 @@ impl<'f, 'a> Comp<'f, 'a> { /// The layers, top layer first (`LIST Layr`; the `DLay`/`SLay`/`CLay`/`SecL` lists next to /// them are not layers). pub fn layers(&self) -> impl Iterator> + 'f { - self.item.children().iter().filter(|c| c.is_list(b"Layr")).map(|chunk| Layer { chunk }) + self.item.children().filter(|c| c.is_list(b"Layr")).map(|chunk| Layer { chunk }) } } /// A layer (`LIST Layr`). #[derive(Debug, Clone, Copy)] pub struct Layer<'f, 'a> { - pub(crate) chunk: &'f Chunk<'a>, + pub(crate) chunk: Node<'f, 'a>, } impl<'f, 'a> Layer<'f, 'a> { /// The underlying chunk. - pub fn chunk(&self) -> &'f Chunk<'a> { + pub fn chunk(&self) -> Node<'f, 'a> { self.chunk } @@ -205,12 +224,12 @@ pub enum PropNode<'f, 'a> { /// A property (`LIST tdbs`). Property(Property<'f, 'a>), /// Any other list that follows a match name (observed: `otst`, the effect instance `sspc`). - Other(&'f Chunk<'a>), + Other(Node<'f, 'a>), } impl<'f, 'a> PropNode<'f, 'a> { /// The node a `LIST` after a match name is. - fn of(c: &'f Chunk<'a>) -> Self { + fn of(c: Node<'f, 'a>) -> Self { if c.is_list(b"tdgp") { PropNode::Group(PropGroup { chunk: c }) } else if c.is_list(b"tdbs") { @@ -233,17 +252,18 @@ pub struct PropEntry<'f, 'a> { /// `ADBE Group End` closes the group and names nothing. #[derive(Debug, Clone, Copy)] pub struct PropGroup<'f, 'a> { - pub(crate) chunk: &'f Chunk<'a>, + pub(crate) chunk: Node<'f, 'a>, } impl<'f, 'a> PropGroup<'f, 'a> { - /// A group view of a `LIST tdgp` chunk. - pub fn from_chunk(chunk: &'f Chunk<'a>) -> Option { + /// A group view of a `LIST tdgp` chunk (a [`Chunk`](crate::Chunk) or a [`Node`]). + pub fn from_chunk(chunk: impl Into>) -> Option { + let chunk = chunk.into(); chunk.is_list(b"tdgp").then_some(Self { chunk }) } /// The underlying chunk. - pub fn chunk(&self) -> &'f Chunk<'a> { + pub fn chunk(&self) -> Node<'f, 'a> { self.chunk } @@ -264,7 +284,7 @@ impl<'f, 'a> PropGroup<'f, 'a> { for c in self.chunk.children() { if c.is(b"tdmn") { pending = c.bytes().map(match_name); - } else if c.id == crate::chunk::LIST + } else if c.is(&crate::chunk::LIST) && let Some(name) = pending.take() { out.push(PropEntry { match_name: name, node: PropNode::of(c) }); @@ -285,9 +305,9 @@ impl<'f, 'a> PropGroup<'f, 'a> { for c in self.chunk.children() { if c.is(b"tdmn") { pending = c.bytes(); - } else if c.id == crate::chunk::LIST + } else if c.is(&crate::chunk::LIST) && let Some(body) = pending.take() - && match_name(body) == name + && match_name_is(body, name) { return Some(PropNode::of(c)); } @@ -314,21 +334,30 @@ fn match_name(body: &[u8]) -> String { text(until_nul(body)) } +/// `match_name(body) == name` without building the `String`: equal bytes match; otherwise only +/// a body that is not valid UTF-8 can still match (through its U+FFFD replacements), and only a +/// name that holds U+FFFD. +fn match_name_is(body: &[u8], name: &str) -> bool { + let bytes = until_nul(body); + bytes == name.as_bytes() || (name.contains('\u{FFFD}') && String::from_utf8_lossy(bytes) == name) +} + /// A property (`LIST tdbs`): `tdsb`, `tdsn`, the 124-byte header `tdb4`, then either the static /// value `cdat` or a keyframe `LIST list`. #[derive(Debug, Clone, Copy)] pub struct Property<'f, 'a> { - pub(crate) chunk: &'f Chunk<'a>, + pub(crate) chunk: Node<'f, 'a>, } impl<'f, 'a> Property<'f, 'a> { - /// A property view of a `LIST tdbs` chunk. - pub fn from_chunk(chunk: &'f Chunk<'a>) -> Option { + /// A property view of a `LIST tdbs` chunk (a [`Chunk`](crate::Chunk) or a [`Node`]). + pub fn from_chunk(chunk: impl Into>) -> Option { + let chunk = chunk.into(); chunk.is_list(b"tdbs").then_some(Self { chunk }) } /// The underlying chunk. - pub fn chunk(&self) -> &'f Chunk<'a> { + pub fn chunk(&self) -> Node<'f, 'a> { self.chunk } @@ -436,12 +465,13 @@ pub struct KeyLayout { impl<'f> KeyList<'f> { /// A view of a `LIST list` chunk. `None` when `lhd3` is not 52 bytes or `ldat` is not exactly /// `count * record_size` bytes (a list without `ldat` must have no records). - pub fn from_chunk<'a>(chunk: &'f Chunk<'a>) -> Option { + pub fn from_chunk<'a: 'f>(chunk: impl Into>) -> Option { + let chunk = chunk.into(); if !chunk.is_list(b"list") { return None; } let header = ListHeader::parse(chunk.child(b"lhd3")?.bytes()?)?; - let ldat: &[u8] = chunk.child(b"ldat").and_then(Chunk::bytes).unwrap_or(&[]); + let ldat: &[u8] = chunk.child(b"ldat").and_then(Node::bytes).unwrap_or(&[]); let expected = usize::try_from(header.count).ok()?.checked_mul(usize::try_from(header.record_size).ok()?)?; (ldat.len() == expected).then_some(Self { header, ldat, layout: None }) } @@ -500,6 +530,7 @@ impl<'f> Keyframe<'f> { #[cfg(test)] mod tests { use super::*; + use crate::Chunk; fn key_list(count: u32, record_size: u32, ldat: &[u8]) -> Chunk<'static> { let mut lhd3 = vec![0u8; 52]; @@ -543,4 +574,31 @@ mod tests { assert!(g.get("C").is_none()); assert_eq!(g.names(), ["A", "B", "A"]); } + + /// Match name is agrees with comparing the decoded name. + #[test] + fn match_name_is_agrees_with_comparing_the_decoded_name() { + let bodies: [&[u8]; 9] = [ + b"ADBE Position\0\0\0", + b"ADBE Position", + b"ADBE Pos", + b"", + b"\0ADBE", + b"ADBE \xff Bad\0", + b"\xff", + b"\xef\xbf\xbd", + "Gr\u{fc}n\0".as_bytes(), + ]; + let names = ["ADBE Position", "ADBE Pos", "", "ADBE \u{FFFD} Bad", "\u{FFFD}", "Gr\u{fc}n", "ADBE Position\0", "ADBE \u{FFFD}"]; + for body in bodies { + for name in names { + assert_eq!(match_name_is(body, name), match_name(body) == name, "{body:?} vs {name:?}"); + } + } + // Names read back from a group find their entries, a damaged one included. + let group = Chunk::list(*b"tdgp", vec![Chunk::data(*b"tdmn", b"X\xffY\0".to_vec()), Chunk::list(*b"tdbs", Vec::new())]); + let g = PropGroup::from_chunk(&group).expect("group"); + let names = g.names(); + assert!(g.get(&names[0]).is_some()); + } } diff --git a/crates/aep/src/values.rs b/crates/aep/src/values.rs index 5613e07a3..b6857165a 100644 --- a/crates/aep/src/values.rs +++ b/crates/aep/src/values.rs @@ -8,6 +8,7 @@ //! 100 %. These defaults are verified for AV layers (solids); cameras and lights use other //! conventions (see the `camera` module). +use crate::bytes::be_f64; use crate::typed::{Layer, Project, PropGroup, PropNode}; /// Where a [`Tracked`] value came from. @@ -192,8 +193,7 @@ impl<'f, 'a> Layer<'f, 'a> { let otda = otst.child_list(b"otky")?.child(b"otda")?.bytes()?; let mut degrees = [0.0; 3]; for (i, slot) in degrees.iter_mut().enumerate() { - let b = otda.get(i * 8..i * 8 + 8)?; - *slot = f64::from_be_bytes(b.try_into().ok()?); + *slot = be_f64(otda, i * 8)?; } Some(Orientation { degrees, @@ -223,12 +223,10 @@ impl<'f, 'a> Layer<'f, 'a> { .child_list(b"otky") .map(|k| { k.children() - .iter() .filter(|c| c.is(b"otda")) .map(|c| { let b = c.bytes()?; - let at = |i: usize| b.get(i * 8..i * 8 + 8).and_then(|s| s.try_into().ok()).map(f64::from_be_bytes); - Some([at(0)?, at(1)?, at(2)?]) + Some([be_f64(b, 0)?, be_f64(b, 8)?, be_f64(b, 16)?]) }) .collect() }) diff --git a/crates/aep/src/version.rs b/crates/aep/src/version.rs index 53de0834a..023793037 100644 --- a/crates/aep/src/version.rs +++ b/crates/aep/src/version.rs @@ -8,8 +8,9 @@ use std::fmt; -use crate::bytes::{be_u16, be_u32}; +use crate::bytes::be_u32; use crate::chunk::AepFile; +use crate::index::{Root, TreeIndex}; /// An After Effects version as packed in `head` / `svap`: `26.2.1x2` is major 26, minor 2, bug-fix /// 1, build 2 (the form of the scripting API's `app.version`). @@ -71,21 +72,38 @@ impl AepFile<'_> { /// version there, and the older version for File ▸ Save a Copy As 25.x / 24.x. `None` when the /// chunk is missing, short or holds no version. pub fn file_version(&self) -> Option { - AeVersion::from_word(be_u32(self.chunk(b"head")?.bytes()?, 4)?) + Root::from(self).file_version() } /// The version of the After Effects that wrote the file: `svap` bytes 0 to 3 (the same word in a /// down-saved copy as in a normal save). `None` when the chunk is missing (After Effects opens such /// a file), short or holds no version. pub fn saved_by_version(&self) -> Option { - AeVersion::from_word(be_u32(self.chunk(b"svap")?.bytes()?, 0)?) + Root::from(self).saved_by_version() + } +} + +impl TreeIndex<'_> { + /// [`AepFile::file_version`] of the indexed file. + pub fn file_version(&self) -> Option { + Root::from(self).file_version() } - /// The `u16` at `head` byte 0: 97 (`0x61`) in a 2026 file, 96 in a copy saved as 25.x, 95 as - /// 24.x. It steps with the major version of [`AepFile::file_version`]; what it counts is not - /// known, so nothing is derived from it. - pub fn head_revision(&self) -> Option { - be_u16(self.chunk(b"head")?.bytes()?, 0) + /// [`AepFile::saved_by_version`] of the indexed file. + pub fn saved_by_version(&self) -> Option { + Root::from(self).saved_by_version() + } +} + +impl Root<'_, '_> { + /// The version the file is written for (`head` bytes 4 to 7). + pub fn file_version(self) -> Option { + AeVersion::from_word(be_u32(self.chunk(b"head")?.bytes()?, 4)?) + } + + /// The version of the application that wrote the file (`svap`). + pub fn saved_by_version(self) -> Option { + AeVersion::from_word(be_u32(self.chunk(b"svap")?.bytes()?, 0)?) } } diff --git a/crates/aep/src/write.rs b/crates/aep/src/write.rs index 1adf3126d..cadd84f0d 100644 --- a/crates/aep/src/write.rs +++ b/crates/aep/src/write.rs @@ -17,6 +17,18 @@ const MAX_WRITE_DEPTH: usize = 256; /// [`AepError::TooDeep`] for an absurdly deep hand-built tree. pub fn write(file: &AepFile<'_>) -> Result, AepError> { let mut out = Vec::new(); + // Sized once to the exact output. A tree past the 32-bit container size fails at once (the + // same error the write below would give after serialising it). `try_reserve` so a hand-built + // tree that repeats one large borrowed body many times cannot abort on the allocation; the + // writer then grows as it goes, as before. + if let Some(len) = sequence_len(&file.chunks, 1) { + if u32::try_from(len.saturating_add(4)).is_err() { + return Err(AepError::TooLarge); + } + if let Some(total) = len.checked_add(12).and_then(|n| n.checked_add(file.trailer.len())) { + let _ = out.try_reserve_exact(total); + } + } out.extend_from_slice(b"RIFX"); out.extend_from_slice(&[0; 4]); out.extend_from_slice(b"Egg!"); @@ -56,6 +68,28 @@ fn write_sequence(out: &mut Vec, chunks: &[Chunk<'_>], depth: usize) -> Resu Ok(()) } +/// The number of bytes [`write_sequence`] produces for `chunks`; `None` past the depth limit or +/// on overflow (both make the write fail). +fn sequence_len(chunks: &[Chunk<'_>], depth: usize) -> Option { + if depth > MAX_WRITE_DEPTH { + return None; + } + let count = chunks.len(); + let mut total = 0usize; + for (i, chunk) in chunks.iter().enumerate() { + let body = match &chunk.body { + Body::Data(d) => d.len(), + Body::Chunks(c) => sequence_len(c, depth + 1)?, + Body::List { children, .. } => sequence_len(children, depth + 1)?.checked_add(4)?, + Body::OpaqueList { data, .. } => data.len().checked_add(4)?, + }; + // The same pad rule as `write_chunk`. + let pad = usize::from(body % 2 == 1 && (chunk.pad.is_some() || i + 1 != count)); + total = total.checked_add(8)?.checked_add(body)?.checked_add(pad)?; + } + Some(total) +} + fn write_chunk(out: &mut Vec, chunk: &Chunk<'_>, is_last: bool, depth: usize) -> Result<(), AepError> { out.extend_from_slice(&chunk.id); let size_at = out.len(); @@ -84,3 +118,62 @@ fn write_chunk(out: &mut Vec, chunk: &Chunk<'_>, is_last: bool, depth: usize } Ok(()) } + +#[cfg(test)] +mod tests { + use std::borrow::Cow; + + use super::*; + + /// The output is sized once to its exact length. + #[test] + fn the_output_is_sized_once_to_its_exact_length() { + let odd = |n: u8| Chunk::data(*b"cdat", vec![n; usize::from(n)]); + let mut padded = odd(3); + padded.pad = Some(0x7f); + let trees = [ + AepFile { chunks: Vec::new(), trailer: Cow::Borrowed(&[]) }, + AepFile { + chunks: vec![ + odd(1), + Chunk::list(*b"Fold", vec![odd(2), odd(3), Chunk::list(*b"tdgp", vec![odd(5)])]), + Chunk { id: *b"LIST", body: Body::OpaqueList { list_type: *b"btdk", data: Cow::Owned(vec![1, 2, 3]) }, pad: None }, + Chunk { id: *b"tdmn", body: Body::Chunks(vec![odd(7), padded]), pad: None }, + odd(9), + ], + trailer: Cow::Borrowed(b""), + }, + ]; + for file in &trees { + let out = write(file).expect("write"); + assert_eq!(sequence_len(&file.chunks, 1).map(|n| n + 12 + file.trailer.len()), Some(out.len())); + // Exact with std's allocator today; grown push by push it would be rounded up. + assert_eq!(out.capacity(), out.len(), "the output grew past its size"); + } + } + + /// A tree past the container size fails before serialising. + #[cfg(target_pointer_width = "64")] + #[test] + fn a_tree_past_the_container_size_fails_before_serialising() { + // 4097 chunks borrowing one 1 MiB body: over 4 GiB of output, none of it written. + let body = vec![0u8; 1 << 20]; + let chunks: Vec> = (0..4097).map(|_| Chunk::data(*b"cdat", &body[..])).collect(); + let file = AepFile { chunks, trailer: Cow::Borrowed(&[]) }; + assert_eq!(write(&file), Err(AepError::TooLarge)); + // Just under the limit still sizes, and fails nothing up front. + assert!(sequence_len(&file.chunks[..4000], 1).is_some_and(|n| u32::try_from(n + 4).is_ok())); + } + + /// A tree past the depth limit has no size. + #[test] + fn a_tree_past_the_depth_limit_has_no_size() { + let mut deep = Chunk::data(*b"cdat", vec![0]); + for _ in 0..=MAX_WRITE_DEPTH { + deep = Chunk::list(*b"tdgp", vec![deep]); + } + let file = AepFile { chunks: vec![deep], trailer: Cow::Borrowed(&[]) }; + assert_eq!(sequence_len(&file.chunks, 1), None); + assert_eq!(write(&file), Err(AepError::TooDeep)); + } +} diff --git a/crates/aep/tests/it/common/mod.rs b/crates/aep/tests/it/common/mod.rs index 791c3bbc9..b5605bb95 100644 --- a/crates/aep/tests/it/common/mod.rs +++ b/crates/aep/tests/it/common/mod.rs @@ -128,6 +128,13 @@ pub fn resolve(name: &str) -> Option { locate(name).map(|s| s.path().clone()) } +/// The ids of the compositions an item uses as precomposition layers, as the file stores them: +/// the little-endian `u32` of each `idpi` chunk (the importer does not read them; the exporter +/// writes them). +pub fn used_item_ids(item: &craft_aep::Item<'_, '_>) -> Vec { + item.chunk().children().filter(|c| c.is(b"idpi")).filter_map(|c| Some(u32::from_le_bytes(c.bytes()?.get(..4)?.try_into().ok()?))).collect() +} + /// The bytes of a case (see the module comment), or `None` when it is missing. pub fn try_fixture(name: &str) -> Option> { match locate(name)? { diff --git a/crates/aep/tests/it/container.rs b/crates/aep/tests/it/container.rs index d937d296c..514bbde1c 100644 --- a/crates/aep/tests/it/container.rs +++ b/crates/aep/tests/it/container.rs @@ -25,5 +25,6 @@ mod shapes; mod text_fields; mod text_markers_footage; mod text_style_slots; +mod tree_index; mod typed; mod values; diff --git a/crates/aep/tests/it/container/batch3_core.rs b/crates/aep/tests/it/container/batch3_core.rs index 8c2518a9f..83bd942b8 100644 --- a/crates/aep/tests/it/container/batch3_core.rs +++ b/crates/aep/tests/it/container/batch3_core.rs @@ -117,8 +117,8 @@ fn a_transform_set_to_its_default_adds_no_record() { assert_eq!(t.opacity_percent.source, ValueSource::Default); assert!(layer.properties().unwrap().find(&["ADBE Transform Group", "ADBE Position"]).is_none()); // The two cases are layers of one file: they differ in the layer id only. - let a = without_layer_id(layer.chunk()); - skip!(with_layer("299-save-noise.aep", L, |_, other| assert_eq!(a, without_layer_id(other.chunk())))); + let a = without_layer_id(&layer.chunk().to_chunk()); + skip!(with_layer("299-save-noise.aep", L, |_, other| assert_eq!(a, without_layer_id(&other.chunk().to_chunk())))); })); } @@ -140,16 +140,13 @@ fn scale_keys_keep_their_ease_type_major() { } assert_near(&keys[0].value().unwrap(), &[1.0, 1.0, 1.0], 1e-9); assert_near(&keys[1].value().unwrap(), &[2.0, 1.5, 1.0], 1e-9); - // The raw block: [in speed x3][in influence x3][out speed x3][out influence x3]. - assert_near(&keys[0].plain_ease_slots().unwrap(), &[0.0, 0.0, 0.0, 0.33, 0.33, 0.33, 0.2, 0.2, 0.2, 0.66, 0.66, 0.66], 1e-9); - assert_near(&keys[1].plain_ease_slots().unwrap(), &[0.1, 0.1, 0.1, 0.66, 0.66, 0.66, 0.0, 0.0, 0.0, 0.33, 0.33, 0.33], 1e-9); + // The raw block is [in speed x3][in influence x3][out speed x3][out influence x3]. for i in 0..3 { assert!(same_ease(&keys[0].ease_component(i).unwrap(), &ease(0.0, 0.33, 0.2, 0.66)), "key 1 component {i}"); assert!(same_ease(&keys[1].ease_component(i).unwrap(), &ease(0.1, 0.66, 0.0, 0.33)), "key 2 component {i}"); } assert!(keys[0].ease_component(3).is_none()); assert_eq!(keys[0].ease_components().unwrap().len(), 3); - assert!(keys[0].color_distance().is_none()); })); } @@ -172,7 +169,6 @@ fn mask_feather_keys_keep_their_ease_type_major() { } assert_near(&keys[0].value().unwrap(), &[0.0, 0.0], 1e-9); assert_near(&keys[1].value().unwrap(), &[50.0, 30.0], 1e-9); - assert_near(&keys[0].plain_ease_slots().unwrap(), &[0.0, 0.0, 0.33, 0.33, 15.0, 15.0, 0.66, 0.66], 1e-9); for i in 0..2 { assert!(same_ease(&keys[0].ease_component(i).unwrap(), &ease(0.0, 0.33, 15.0, 0.66)), "key 1 component {i}"); assert!(same_ease(&keys[1].ease_component(i).unwrap(), &ease(8.0, 0.66, 0.0, 0.33)), "key 2 component {i}"); @@ -213,14 +209,15 @@ fn bezier_colour_keys_carry_the_distance_to_the_next_key() { assert_eq!((k.interpolation_in(), k.interpolation_out()), (Some(Interpolation::Bezier), Some(Interpolation::Bezier))); assert!(same_ease(&k.ease_component(0).unwrap(), &ease(0.0, 0.0, 0.0, 0.0))); } - assert!(near(keys[0].color_distance().unwrap(), 255.0 * 2f64.sqrt(), 1e-3), "{:?}", keys[0].color_distance()); - assert!(near(keys[0].color_distance().unwrap(), 360.6245, 1e-3)); - assert_eq!(keys[1].color_distance(), Some(0.0)); + let distance = |k: &craft_aep::Keyframe<'_>| k.spatial().unwrap().segment_length; + assert!(near(distance(&keys[0]), 255.0 * 2f64.sqrt(), 1e-3), "{}", distance(&keys[0])); + assert!(near(distance(&keys[0]), 360.6245, 1e-3)); + assert_eq!(distance(&keys[1]), 0.0); })); skip!(with_layer("210-key-fill-color-2key-linear.aep", L, |_, layer| { let p = layer.effects()[0].params().into_iter().find(|p| p.name == "Color").unwrap(); let k = p.stored().unwrap().keys().unwrap().keyframes().next().unwrap(); - assert_eq!(k.color_distance(), Some(0.0)); + assert_eq!(k.spatial().unwrap().segment_length, 0.0); })); } @@ -243,21 +240,19 @@ fn time_stretch_is_an_exact_ratio() { let (h, in_p, out_p, scale) = skip!(stretch_facts("316-layer-stretch50.aep")); assert_eq!((h.stretch, h.stretch_den), (1, 2)); assert_eq!(h.stretch_ratio(), Some(rat(1, 2))); - assert_eq!(h.stretch_percent(), 50.0); assert_eq!(h.out_relative, rat(128000, 25600)); assert_eq!((in_p, out_p, scale), (Some(rat(0, 1)), Some(rat(5, 2)), 25600)); // 317: 150 %: 3/2, ends at 7.5 s, the property time scale is 25600 x 3/2. let (h, in_p, out_p, scale) = skip!(stretch_facts("317-layer-stretch150.aep")); assert_eq!((h.stretch, h.stretch_den), (3, 2)); assert_eq!(h.stretch_ratio(), Some(rat(3, 2))); - assert_eq!(h.stretch_percent(), 150.0); assert_eq!((in_p, out_p, scale), (Some(rat(0, 1)), Some(rat(15, 2)), 38400)); // 018: 200 %: 2/1, ends at 10 s, time scale 51200. The base is 1/1. let (h, _, out_p, scale) = skip!(stretch_facts("018-layer-stretch200.aep")); assert_eq!((h.stretch, h.stretch_den, h.stretch_ratio()), (2, 1, Some(rat(2, 1)))); assert_eq!((out_p, scale), (Some(rat(10, 1)), 51200)); let (h, _, out_p, scale) = skip!(stretch_facts("000-base.aep")); - assert_eq!((h.stretch, h.stretch_den, h.stretch_percent()), (1, 1, 100.0)); + assert_eq!((h.stretch, h.stretch_den, h.stretch_ratio()), (1, 1, Some(rat(1, 1)))); assert_eq!((out_p, scale), (Some(rat(5, 1)), 25600)); } @@ -269,9 +264,9 @@ fn fractional_times_are_exact_multiples_of_one_over_25600() { assert_eq!(h.in_relative, rat(8532, 25600)); assert_eq!(h.out_relative, rat(136532, 25600)); assert_eq!(h.start, rat(0, 25600)); + // 0.33328125 s and 5.33328125 s. assert_eq!(h.in_point(), Some(rat(2133, 6400))); - assert_eq!(h.in_point_seconds(), Some(0.33328125)); - assert_eq!(h.out_point_seconds(), Some(5.33328125)); + assert_eq!(h.out_point(), Some(rat(34133, 6400))); assert_eq!(h.stretch_ratio(), Some(rat(1, 1))); } @@ -364,18 +359,17 @@ fn pixel_aspect_shutter_phase_and_display_start() { // 325: pixelAspect 0.9091 reads back 0.90909090909091 = the exact ratio 10/11. let h = skip!(comp_header("325-comp-pixelaspect-0.9091.aep")); assert_eq!(h.pixel_aspect.reduced(), Some(rat(10, 11))); - assert!(near(h.pixel_aspect_ratio().unwrap(), 0.90909090909091, 1e-12)); // 326: shutterPhase -90, the shutter angle stays 180. let h = skip!(comp_header("326-comp-shutterphase-m90.aep")); assert_eq!((h.shutter_phase, h.shutter_angle), (Some(-90), 180)); // 327: displayStartFrame 100 at 25 fps = 4 s = 102400/25600; the shutter phase is back to 0. let h = skip!(comp_header("327-comp-displaystartframe-100.aep")); assert_eq!(h.display_start, Some(rat(102400, 25600))); - assert_eq!((h.display_start_seconds(), h.display_start_frame(), h.shutter_phase), (Some(4.0), Some(100), Some(0))); + assert_eq!(h.shutter_phase, Some(0)); // The base: no phase, start 0. let h = skip!(comp_header("000-base.aep")); - assert_eq!((h.shutter_phase, h.display_start_seconds(), h.display_start_frame()), (Some(0), Some(0.0), Some(0))); - assert_eq!(h.pixel_aspect_ratio(), Some(1.0)); + assert_eq!((h.shutter_phase, h.display_start.and_then(|r| r.reduced())), (Some(0), Some(rat(0, 1)))); + assert_eq!(h.pixel_aspect.reduced(), Some(rat(1, 1))); // A short chunk still decodes the old fields. let bytes = [0u8; 176]; let h = craft_aep::CompHeader::parse(&bytes).unwrap(); diff --git a/crates/aep/tests/it/container/batch3_objects.rs b/crates/aep/tests/it/container/batch3_objects.rs index bbb7040b4..c78525a00 100644 --- a/crates/aep/tests/it/container/batch3_objects.rs +++ b/crates/aep/tests/it/container/batch3_objects.rs @@ -148,19 +148,21 @@ fn a_mask_box_is_normalised_to_the_layer_size() { for (got, want) in m.shapes[0].bbox.iter().zip(want_box) { assert!(near(f64::from(*got), want, 1e-6), "{:?}", m.shapes[0].bbox); } - let shapes = project.mask_vertices_px(&layer, m).unwrap(); - assert_eq!(shapes.len(), 1); - let got: Vec<[f64; 2]> = shapes[0].iter().map(|v| v.position).collect(); + // The box scales by the size of the layer's source (the importer takes that size from the + // source item it matched, the same item `layer_source_size` reads). + let (w, h) = project.layer_source_size(&layer).unwrap(); + assert_eq!(m.shapes.len(), 1); + let got: Vec<[f64; 2]> = m.shapes[0].vertices((f64::from(w), f64::from(h))).iter().map(|v| v.position).collect(); for (g, w) in got.iter().zip([[20.0, 30.0], [80.0, 30.0], [80.0, 70.0], [20.0, 70.0]]) { assert_near(g, &w, 1e-3); } assert_eq!(got.len(), 4); - // A layer without a sized source has no pixel vertices. + // A layer without a sized source has no size to scale by. let bytes = fixture!("333-shape-group-two-rects.aep"); let file = open(&bytes); let project = Project::new(&file).unwrap(); let shape = layer_named(&first_comp(&project), "Shape"); - assert!(project.mask_vertices_px(&shape, m).is_none()); + assert!(project.layer_source_size(&shape).is_none()); } #[test] @@ -178,12 +180,14 @@ fn one_mkif_per_mask_and_the_mode_none() { assert_eq!((m.mode, m.inverted, m.counter), (Some(MaskMode::Add), Some(false), Some(counter))); assert_eq!(m.shapes.len(), 1); } - let v1 = project.mask_vertices_px(&layer, &masks[0]).unwrap(); + let (w, h) = project.layer_source_size(&layer).unwrap(); + let px = |m: &craft_aep::Mask| m.shapes.iter().map(|s| s.vertices((f64::from(w), f64::from(h)))).collect::>(); + let v1 = px(&masks[0]); for (v, w) in v1[0].iter().zip(ellipse_vertices()) { assert_near(&v.position, &w, 1e-2); } // Mask 2 has its own box (500..900 x 350..650) normalised to the layer. - let v2 = project.mask_vertices_px(&layer, &masks[1]).unwrap(); + let v2 = px(&masks[1]); for (v, w) in v2[0].iter().zip([[500.0, 350.0], [900.0, 350.0], [900.0, 650.0], [500.0, 650.0]]) { assert_near(&v.position, &w, 1e-2); } @@ -384,81 +388,16 @@ fn the_last_style_run_absorbs_the_final_carriage_return() { // ------------------------------------------------------------------ camera and light (341-345) +/// Camera zoom keeps the auto position. #[test] -fn camera_zoom_and_depth_of_field() { - let camera = |name: &str| { - Some({ - let bytes = crate::common::try_fixture(name)?; - let file = open(&bytes); - let project = Project::new(&file).unwrap(); - let layer = layer_of_type(&first_comp(&project), LayerType::Camera); - ( - layer.camera_zoom(), - layer.camera_depth_of_field(), - layer.camera_focus_distance(), - layer.camera_aperture(), - layer.camera_position().map(|p| (p.stored, p.auto_state)), - ) - }) - }; - // 341: Zoom 2000 is stored; the auto Position keeps [0, 0, -2666.67]. - let (zoom, dof, focus, aperture, pos) = skip!(camera("341-camera-zoom-2000.aep")); - assert_eq!(zoom.map(|z| (z.value, z.source)), Some((2000.0, ValueSource::Stored))); - assert_eq!((dof.map(|d| (d.value, d.source)), focus, aperture), (Some((false, ValueSource::Default)), None, None)); - let (stored, auto) = pos.unwrap(); - assert!(auto && near(stored[2], -2666.67, 0.01) && stored[0] == 0.0, "{stored:?}"); - // 342: DOF on, Focus Distance 800, Aperture 30; Zoom stays the default. - let (zoom, dof, focus, aperture, _) = skip!(camera("342-camera-dof-800-30.aep")); - assert_eq!(zoom.map(|z| (z.source, near(z.value, 2666.67, 0.01))), Some((ValueSource::Default, true))); - assert_eq!(dof.map(|d| (d.value, d.source)), Some((true, ValueSource::Stored))); - assert_eq!(focus.map(|f| (f.value, f.source)), Some((800.0, ValueSource::Stored))); - assert_eq!(aperture.map(|f| (f.value, f.source)), Some((30.0, ValueSource::Stored))); - // The default camera (256) has nothing but the default zoom. - let (zoom, dof, ..) = skip!(camera("256-camera-two-node-default.aep")); - assert_eq!((zoom.unwrap().source, dof.unwrap().value), (ValueSource::Default, false)); - assert_eq!(zoom.unwrap().value, craft_aep::DEFAULT_CAMERA_ZOOM); -} - -#[test] -fn light_colour_cone_angle_and_feather() { - let light = |name: &str| { - Some({ - let bytes = crate::common::try_fixture(name)?; - let file = open(&bytes); - let project = Project::new(&file).unwrap(); - let layer = layer_of_type(&first_comp(&project), LayerType::Light); - assert_eq!(layer.header().unwrap().light_type(), Some(craft_aep::LightType::Spot), "{name}"); - let t = |x: Option>| x.map(|x| (x.value, x.source)); - (layer.light_color().map(|c| (c.value, c.source)), t(layer.light_cone_angle()), t(layer.light_cone_feather())) - }) - }; - // The default spot light: white, 90, 50, all absent. - let (colour, angle, feather) = skip!(light("254-light-spot-intensity-50.aep")); - assert_eq!( - (colour, angle, feather), - (Some(([1.0; 4], ValueSource::Default)), Some((90.0, ValueSource::Default)), Some((50.0, ValueSource::Default))) - ); - // 343: colour [1,0,0,1] = ARGB doubles [255,255,0,0]. - let (colour, angle, feather) = skip!(light("343-light-spot-color-red.aep")); - assert_eq!(colour, Some(([1.0, 1.0, 0.0, 0.0], ValueSource::Stored))); - assert_eq!((angle, feather), (Some((90.0, ValueSource::Default)), Some((50.0, ValueSource::Default)))); - // 344: Cone Angle 60. 345: Cone Feather 30. - let (colour, angle, feather) = skip!(light("344-light-spot-coneangle-60.aep")); - assert_eq!((colour.unwrap().1, angle, feather), (ValueSource::Default, Some((60.0, ValueSource::Stored)), Some((50.0, ValueSource::Default)))); - let (_, angle, feather) = skip!(light("345-light-spot-conefeather-30.aep")); - assert_eq!((angle, feather), (Some((90.0, ValueSource::Default)), Some((30.0, ValueSource::Stored)))); - // A point light (252) stores Cone Angle 90 / Feather 50 in their default state. - let bytes = fixture!("252-light-point.aep"); +fn camera_zoom_keeps_the_auto_position() { + // 341: Zoom 2000 is stored (checked on import); the auto Position keeps [0, 0, -2666.67]. + let bytes = fixture!("341-camera-zoom-2000.aep"); let file = open(&bytes); let project = Project::new(&file).unwrap(); - let layer = layer_of_type(&first_comp(&project), LayerType::Light); - assert_eq!(layer.light_cone_angle().map(|a| (a.value, a.source)), Some((90.0, ValueSource::Stored))); - // Layers without light options have no light accessors. - let bytes = fixture!("000-base.aep"); - let file = open(&bytes); - let project = Project::new(&file).unwrap(); - let solid = base_layer(&first_comp(&project)); - assert!(solid.light_color().is_none() && solid.camera_zoom().is_none() && solid.camera_depth_of_field().is_none()); + let layer = layer_of_type(&first_comp(&project), LayerType::Camera); + let pos = layer.camera_position().unwrap(); + assert!(pos.auto_state && near(pos.stored[2], -2666.67, 0.01) && pos.stored[0] == 0.0, "{:?}", pos.stored); } // ------------------------------------------------------------------ marker data (347-349, 249) @@ -508,7 +447,7 @@ fn audio_footage_item() { assert_eq!((f.width, f.height, f.is_solid), (0, 0, false)); assert_eq!(f.duration, Some(rat(48000, 48000))); assert_eq!((f.audio_sample_rate, f.file_size, f.bits_per_pixel, f.native_frame_rate), (Some(48000.0), 96044, 0, 0)); - assert!(!f.is_sequence && f.sequence_frame_count().is_none()); + assert!(!f.is_sequence && item.sequence_info().is_none()); // The combined file stores no display name for the item (the single-case file had // "test-tone.wav"); the path names the file. assert_eq!(item.label(), Some(7)); @@ -532,7 +471,7 @@ fn image_sequence_is_one_item() { assert_eq!(f.duration, Some(rat(3, 30)), "{name}"); assert_eq!((f.native_frame_rate, f.bits_per_pixel, f.file_size, f.audio_sample_rate), (30, 24, 504, None), "{name}"); assert!(f.is_sequence, "{name}"); - assert_eq!(f.sequence_frame_count(), Some(3), "{name}"); + assert_eq!(item.sequence_info().map(|s| s.frame_count()), Some(3), "{name}"); // The first file name sits in opti; the alias points at the sequence folder. assert_eq!(f.name.as_deref(), Some("seq_001.png"), "{name}"); assert!(f.path.as_deref().is_some_and(|p| p.ends_with("test-seq")), "{name}"); diff --git a/crates/aep/tests/it/container/cases_600.rs b/crates/aep/tests/it/container/cases_600.rs index 210fb0916..43df3052a 100644 --- a/crates/aep/tests/it/container/cases_600.rs +++ b/crates/aep/tests/it/container/cases_600.rs @@ -38,7 +38,7 @@ fn a_composition_counts_the_compositions_it_shows() { let id = |n: &str| items.iter().find(|i| i.display_name().as_deref() == Some(n)).and_then(|i| i.id()).expect(n); let c = items.iter().find(|i| i.display_name().as_deref() == Some("C")).expect("C"); // The list holds each used composition once (in no particular order). - let mut used = c.used_item_ids(); + let mut used = crate::common::used_item_ids(c); used.sort(); let mut want = vec![id("P30"), id("Q")]; want.sort(); @@ -60,8 +60,8 @@ fn an_image_sequence_knows_its_numbering() { let item = project.all_items().into_iter().find(|i| i.footage().is_some_and(|f| !f.is_solid)).expect("sequence"); let f = item.footage().expect("footage"); assert_eq!((f.file_type.as_str(), f.width, f.height, f.native_frame_rate, f.is_sequence), ("oEXR", 64, 48, 30, true)); - assert_eq!(f.sequence_frame_count(), Some(3)); let s = item.sequence_info().expect("numbering"); + assert_eq!(s.frame_count(), 3); assert_eq!((s.prefix.as_str(), s.suffix.as_str(), s.start, s.end, s.digits), ("shot_", ".exr", 101, 103, 4)); assert_eq!(s.file_name(101), "shot_0101.exr"); // A still has no numbering (000: the solid; the base has no other footage). diff --git a/crates/aep/tests/it/container/header.rs b/crates/aep/tests/it/container/header.rs index f78b0bba9..f9252f7fd 100644 --- a/crates/aep/tests/it/container/header.rs +++ b/crates/aep/tests/it/container/header.rs @@ -55,7 +55,6 @@ fn assert_plain(h: &LayerHeader) { assert_eq!(h.label, 1); // No name was set, so the copy of the name at byte 64 is empty (the layer shows its source name). assert_eq!(h.name_copy, ""); - assert!(!h.name_is_user_set()); assert!(!h.is_null_object()); assert_eq!(h.parent_id, None); assert_eq!(h.source_id, Some(0x1a)); @@ -67,16 +66,13 @@ fn base_layer_header() { assert_plain(&h); assert_eq!(h.quality, Quality::Best); assert_eq!(h.blend_mode, BlendMode::Normal); - assert_eq!(h.stretch, 1); - assert_eq!(h.stretch_percent(), 100.0); + assert_eq!((h.stretch, h.stretch_den), (1, 1)); assert_eq!(switches(&h), NONE); // start 0 s, in 0 s, out 5 s; denominator = the composition timescale. assert_eq!(h.start, Rational { num: 0, den: 25600 }); assert_eq!(h.in_relative, Rational { num: 0, den: 25600 }); assert_eq!(h.out_relative, Rational { num: 128_000, den: 25600 }); - assert_eq!(h.start_seconds(), Some(0.0)); - assert_eq!(h.in_point_seconds(), Some(0.0)); - assert_eq!(h.out_point_seconds(), Some(5.0)); + assert_eq!(h.in_point(), Rational::new(0, 1)); assert_eq!(h.out_point(), Rational::new(5, 1)); } @@ -98,7 +94,7 @@ fn one_switch_per_case() { assert_eq!(switches(&h), expected, "{name}"); assert_plain(&h); assert_eq!((h.quality, h.blend_mode, h.stretch), (Quality::Best, BlendMode::Normal, 1), "{name}"); - assert_eq!(h.out_point_seconds(), Some(5.0), "{name}"); + assert_eq!(h.out_point(), Rational::new(5, 1), "{name}"); } } @@ -106,7 +102,6 @@ fn one_switch_per_case() { fn name_blend_mode_and_quality() { let h = skip!(l_header("010-layer-name.aep")); assert_eq!(h.name_copy, "Renamed"); - assert!(h.name_is_user_set()); let bytes = fixture!("010-layer-name.aep"); let file = open(&bytes); let project = Project::new(&file).unwrap(); @@ -114,7 +109,6 @@ fn name_blend_mode_and_quality() { // The layer name is the user's; the source footage keeps its own name. assert_eq!(layer.name().as_deref(), Some("Renamed")); assert_eq!(project.layer_source(&layer).and_then(|i| i.display_name()).as_deref(), Some("L")); - assert_eq!(project.layer_display_name(&layer).as_deref(), Some("Renamed")); assert_eq!(skip!(l_header("016-layer-blend-multiply.aep")).blend_mode, BlendMode::Multiply); assert_eq!(skip!(l_header("023-layer-quality-draft.aep")).quality, Quality::Draft); @@ -127,7 +121,7 @@ fn display_name_falls_back_to_the_source_name() { let project = Project::new(&file).unwrap(); let layer = base_layer(&first_comp(&project)); assert_eq!(layer.name(), None); - assert_eq!(project.layer_display_name(&layer).as_deref(), Some("L")); + assert_eq!(project.layer_source(&layer).and_then(|i| i.display_name()).as_deref(), Some("L")); assert_eq!(project.layer_source_size(&layer), Some((1920, 1080))); let source = project.layer_source(&layer).unwrap(); assert_eq!(source.id(), Some(0x1a)); @@ -139,26 +133,23 @@ fn time_stretch_and_the_in_out_start_convention() { // 018: stretch 100 -> 200: the stored out point stays 5 s, the layer ends at 10 s. let h = skip!(l_header("018-layer-stretch200.aep")); assert_eq!(h.stretch, 2); - assert_eq!(h.stretch_percent(), 200.0); + assert_eq!(h.stretch_ratio(), Rational::new(2, 1)); assert_eq!(h.out_relative, Rational { num: 128_000, den: 25600 }); - assert_eq!(h.out_point_seconds(), Some(10.0)); - assert_eq!(h.in_point_seconds(), Some(0.0)); + assert_eq!(h.out_point(), Rational::new(10, 1)); + assert_eq!(h.in_point(), Rational::new(0, 1)); // 019: inPoint 0 -> 1; AE moved the out point to 6 s and left the start at 0. let h = skip!(l_header("019-layer-inpoint1.aep")); - assert_eq!(h.start_seconds(), Some(0.0)); + assert_eq!(h.start.num, 0); assert_eq!(h.in_relative, Rational { num: 25600, den: 25600 }); assert_eq!(h.in_point(), Rational::new(1, 1)); - assert_eq!(h.out_point_seconds(), Some(6.0)); + assert_eq!(h.out_point(), Rational::new(6, 1)); // 021: startTime 0 -> 0.5: in 0.5 s and out 5.5 s although the stored in point stays 0. let h = skip!(l_header("021-layer-starttime0.5.aep")); assert_eq!(h.start, Rational { num: 12800, den: 25600 }); - assert_eq!(h.start_seconds(), Some(0.5)); assert_eq!(h.in_relative.num, 0); assert_eq!(h.in_point(), Rational::new(1, 2)); - assert_eq!(h.in_point_seconds(), Some(0.5)); - assert_eq!(h.out_point_seconds(), Some(5.5)); assert_eq!(h.out_point(), Rational::new(11, 2)); } @@ -175,7 +166,6 @@ fn parent_link_and_null_object() { let nh = null.header().unwrap(); assert_eq!(null.name().as_deref(), Some("Null")); assert!(nh.is_null_object()); - assert!(nh.name_is_user_set()); assert_eq!(nh.layer_type, LayerType::Av); assert_eq!(nh.parent_id, None); assert_eq!(project.layer_source(null).and_then(|i| i.display_name()).as_deref(), Some("Null 1")); @@ -225,17 +215,14 @@ fn base_comp_header() { assert_eq!((h.width, h.height), (1920, 1080)); assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (1024, 25600)); assert_eq!(h.frame_rate(), Some(25.0)); - assert_eq!(h.frame_rate_from_fixed(), 25.0); + assert_eq!(h.frame_rate_fixed, 25 << 16); assert_eq!(h.duration_units, 128_000); assert_eq!(h.duration_seconds(), Some(5.0)); assert_eq!(h.background, [0, 0, 0]); - assert_eq!(h.background_rgb(), [0.0, 0.0, 0.0]); assert_eq!(h.shutter_angle, 180); - assert_eq!(h.work_area_start_seconds(), Some(0.0)); + assert_eq!(h.work_area_start_units, 0); assert_eq!(h.work_area_end_units, None); - assert_eq!(h.work_area_end_seconds(), Some(5.0)); assert_eq!(h.pixel_aspect, Rational { num: 1, den: 1 }); - assert_eq!(h.pixel_aspect_ratio(), Some(1.0)); assert!(!h.motion_blur); // The manifest calls the classic renderer `ADBE Advanced 3d`; the file stores `ADBE Escher` // and the display name `Classic 3D`. @@ -255,7 +242,7 @@ fn one_comp_change_per_case() { let (h, _, _) = skip!(comp_header("031-comp-fps30.aep")); assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (1024, 30720)); assert_eq!(h.frame_rate(), Some(30.0)); - assert_eq!(h.frame_rate_from_fixed(), 30.0); + assert_eq!(h.frame_rate_fixed, 30 << 16); assert_eq!(h.duration_units, 153_600); assert_eq!(h.duration_seconds(), Some(5.0)); @@ -263,12 +250,11 @@ fn one_comp_change_per_case() { let (h, _, _) = skip!(comp_header("032-comp-duration10.aep")); assert_eq!(h.duration_units, 256_000); assert_eq!(h.duration_seconds(), Some(10.0)); - assert_eq!(h.work_area_end_seconds(), Some(10.0)); + assert_eq!(h.work_area_end_units, None); // 033: bgColor [0,0,0] -> [0,1,0]. let (h, _, _) = skip!(comp_header("033-comp-bg-green.aep")); assert_eq!(h.background, [0, 255, 0]); - assert_eq!(h.background_rgb(), [0.0, 1.0, 0.0]); // 034: shutterAngle 180 -> 90. let (h, _, _) = skip!(comp_header("034-comp-shutter90.aep")); @@ -279,8 +265,6 @@ fn one_comp_change_per_case() { let (h, _, _) = skip!(comp_header("035-comp-workarea-1-2.aep")); assert_eq!(h.work_area_start_units, 25600); assert_eq!(h.work_area_end_units, Some(76_800)); - assert_eq!(h.work_area_start_seconds(), Some(1.0)); - assert_eq!(h.work_area_end_seconds(), Some(3.0)); } #[test] @@ -290,7 +274,6 @@ fn one_comp_change_per_case_aspect_blur_renderer() { // 037: pixelAspect 1 -> 2. let (h, _, _) = skip!(comp_header("037-comp-pixelaspect2.aep")); assert_eq!(h.pixel_aspect, Rational { num: 2, den: 1 }); - assert_eq!(h.pixel_aspect_ratio(), Some(2.0)); // 038: comp motion blur on. let (h, _, _) = skip!(comp_header("038-comp-motionblur.aep")); @@ -328,17 +311,16 @@ fn precomp_item_ids_and_labels() { assert_eq!(h.source_id, pre_item.id()); assert_eq!(project.layer_source(layer).unwrap().id(), pre_item.id()); assert_eq!(h.label, 15); - assert!(!h.name_is_user_set()); assert_eq!(layer.name(), None); - assert_eq!(project.layer_display_name(layer).as_deref(), Some("Pre")); + assert_eq!(project.layer_source(layer).and_then(|i| i.display_name()).as_deref(), Some("Pre")); assert_eq!(project.layer_source_size(layer), Some((1920, 1080))); // The precomp layer's start time has the denominator 1; the Pre comp is 1920x1080, 25 fps, 5 s. assert_eq!(h.start.den, 1); let ph = pre.header().unwrap(); assert_eq!((ph.width, ph.height, ph.frame_rate(), ph.duration_seconds()), (1920, 1080, Some(25.0), Some(5.0))); // C records that it uses the comp Pre (chunk idpi, a little-endian u32). - assert_eq!(c_item.used_item_ids(), [pre_item.id().unwrap()]); - assert!(pre_item.used_item_ids().is_empty()); + assert_eq!(crate::common::used_item_ids(c_item), [pre_item.id().unwrap()]); + assert!(crate::common::used_item_ids(pre_item).is_empty()); assert_eq!(pre_item.label(), Some(15)); // The layer inside Pre is a new layer made from the solid L (moving the layers into a precomp // creates new layers). @@ -347,7 +329,7 @@ fn precomp_item_ids_and_labels() { let ih = inner[0].header().unwrap(); assert_ne!(ih.id, L); assert_eq!(ih.source_id, Some(0x1a)); - assert_eq!(project.layer_display_name(&inner[0]).as_deref(), Some("L")); + assert_eq!(project.layer_source(&inner[0]).and_then(|i| i.display_name()).as_deref(), Some("L")); } #[test] @@ -356,9 +338,8 @@ fn a_negative_stretch_is_a_reversed_layer() { // point 0 and out point 5 s relative, so the layer runs from 5 s back to 0 s; 983: -50 % = -1/2. let h = skip!(l_header("981-layer-stretch-minus100-start5.aep")); assert_eq!((h.stretch, h.stretch_den), (-1, 1)); - assert_eq!(h.stretch_percent(), -100.0); - assert_eq!((h.in_point_seconds(), h.out_point_seconds()), (Some(5.0), Some(0.0))); + assert_eq!((h.in_point(), h.out_point()), (Rational::new(5, 1), Rational::new(0, 1))); let h = skip!(l_header("983-layer-stretch-minus50-opacity-keys.aep")); assert_eq!((h.stretch, h.stretch_den), (-1, 2)); - assert_eq!((h.in_point_seconds(), h.out_point_seconds()), (Some(5.0), Some(2.5))); + assert_eq!((h.in_point(), h.out_point()), (Rational::new(5, 1), Rational::new(5, 2))); } diff --git a/crates/aep/tests/it/container/helpers.rs b/crates/aep/tests/it/container/helpers.rs index b6ab11432..af2282ab8 100644 --- a/crates/aep/tests/it/container/helpers.rs +++ b/crates/aep/tests/it/container/helpers.rs @@ -379,7 +379,8 @@ pub fn layer_named<'f, 'a>(comp: &Comp<'f, 'a>, name: &str) -> Layer<'f, 'a> { /// The layer that shows this name: its own name, or its source's while it has none. pub fn layer_shown_as<'f, 'a>(project: &Project<'f, 'a>, comp: &Comp<'f, 'a>, name: &str) -> Layer<'f, 'a> { - comp.layers().find(|l| project.layer_display_name(l).as_deref() == Some(name)).unwrap_or_else(|| panic!("no layer shown as {name}")) + let shown = |l: &Layer<'f, 'a>| l.name().or_else(|| project.layer_source(l)?.display_name()); + comp.layers().find(|l| shown(l).as_deref() == Some(name)).unwrap_or_else(|| panic!("no layer shown as {name}")) } /// The first layer of this type in a composition. diff --git a/crates/aep/tests/it/container/keyframes.rs b/crates/aep/tests/it/container/keyframes.rs index 56ec367cc..27b8073ab 100644 --- a/crates/aep/tests/it/container/keyframes.rs +++ b/crates/aep/tests/it/container/keyframes.rs @@ -19,6 +19,9 @@ fn prop_of(name: &str, path: &[&str], f: impl FnOnce(Property<'_, '_>) -> R) const POSITION: &[&str] = &["ADBE Transform Group", "ADBE Position"]; const OPACITY: &[&str] = &["ADBE Transform Group", "ADBE Opacity"]; +/// The ease of a linear key: all four slots 0. +const NO_EASE: KeyEase = KeyEase { in_speed: 0.0, in_influence: 0.0, out_speed: 0.0, out_influence: 0.0 }; + fn ease(in_speed: f64, in_influence: f64, out_speed: f64, out_influence: f64) -> KeyEase { KeyEase { in_speed, in_influence, out_speed, out_influence } } @@ -33,7 +36,7 @@ fn in_and_out_interpolation_are_separate_bytes() { assert_eq!((keys[0].interpolation_in(), keys[0].interpolation_out()), (Some(Interpolation::Linear), Some(Interpolation::Hold))); assert_eq!((keys[1].interpolation_in(), keys[1].interpolation_out()), (Some(Interpolation::Linear), Some(Interpolation::Linear))); assert_eq!(keys.iter().map(|k| k.seconds(25600).unwrap()).collect::>(), [0.0, 1.0]); - assert_eq!(keys[1].spatial_value(), Some([100.0, 0.0, 0.0])); + assert_eq!(keys[1].value(), Some(vec![100.0, 0.0, 0.0])); })); // 203: key 1 inType HOLD, outType LINEAR (bytes 03 01): byte 4 is the incoming side. skip!(prop_of("203-key-in-hold-out-linear.aep", POSITION, |p| { @@ -76,7 +79,7 @@ fn opacity_ease_speed_is_in_stored_units_per_second() { assert_near(&[e0.in_speed, e0.in_influence, e0.out_speed, e0.out_influence], &[0.05, 0.2, 0.2, 0.8], 1e-12); let e1 = keys[1].ease().unwrap(); assert_near(&[e1.in_speed, e1.in_influence, e1.out_speed, e1.out_influence], &[0.3, 0.6, 0.09, 0.4], 1e-12); - assert_eq!(keys[0].plain_ease_slots().unwrap().len(), 4); + assert_eq!(keys[0].ease_components(), Some(vec![e0])); for k in &keys { assert_eq!((k.interpolation_in(), k.interpolation_out()), (Some(Interpolation::Bezier), Some(Interpolation::Bezier))); } @@ -94,7 +97,7 @@ fn scale_keys_use_the_plain_template_with_three_components() { // A 128-byte record is not always spatial: the header's template flag decides. assert_eq!(k.kind(), KeyframeKind::Plain { components: 3 }); assert_eq!(k.spatial(), None); - assert_eq!(k.plain_ease_slots(), Some(vec![0.0; 12])); + assert_eq!(k.ease_components(), Some(vec![NO_EASE; 3])); assert_eq!(k.ease(), None); assert_eq!((k.interpolation_in(), k.interpolation_out()), (Some(Interpolation::Linear), Some(Interpolation::Linear))); } @@ -125,7 +128,7 @@ fn two_dimensional_mask_feather_keys() { assert!(keys.iter().all(|k| k.raw().len() == 88 && k.kind() == KeyframeKind::Plain { components: 2 })); assert_eq!(keys.iter().map(|k| k.value().unwrap()).collect::>(), [vec![0.0, 0.0], vec![40.0, 20.0]]); assert_eq!(keys.iter().map(|k| k.seconds(25600).unwrap()).collect::>(), [0.0, 1.0]); - assert_eq!(keys[0].plain_ease_slots(), Some(vec![0.0; 8])); + assert_eq!(keys[0].ease_components(), Some(vec![NO_EASE; 2])); let mask = &layer.masks()[0]; assert!(mask.feather.source == ValueSource::Keyframed && mask.feather.value == [0.0, 0.0]); } diff --git a/crates/aep/tests/it/container/layer_switches.rs b/crates/aep/tests/it/container/layer_switches.rs index 5e2179d53..05e939c38 100644 --- a/crates/aep/tests/it/container/layer_switches.rs +++ b/crates/aep/tests/it/container/layer_switches.rs @@ -2,7 +2,7 @@ //! colours, checked against the values the corpus was made with (its MANIFEST, batch 2). use crate::container::helpers::{assert_near, base_layer, first_comp, layer_of_type, layer_shown_as, open}; -use craft_aep::{BlendMode, CompHeader, LayerHeader, LayerType, LightType, Project, Rational, ValueSource}; +use craft_aep::{BlendMode, CompHeader, LayerHeader, LayerType, LightType, Project}; fn l_header(name: &str) -> Option { Some({ @@ -80,22 +80,24 @@ fn ntsc_frame_rates_are_exact_fractions() { // 229: frameRate 29.97 -> read back 29.9700012207031; the duration snaps from 5 s to 150 frames (5.005005 s). let h = skip!(comp_header("229-comp-fps-29.97.aep")); assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (800, 23976)); - assert_eq!(h.frame_rate_exact(), Some(Rational { num: 2997, den: 100 })); assert!((h.frame_rate().unwrap() - 29.97).abs() < 1e-12); - assert!((h.frame_rate_from_fixed() - 29.970_001_220_703_1).abs() < 1e-7); - assert_eq!((h.duration_units, h.duration_frames()), (120_000, Some(150))); + // The 16.16 rate holds the float32 29.97 (29.9700012207031). + assert_eq!(h.frame_rate_fixed, 1_964_114); + // 150 frames of 800 units. + assert_eq!(h.duration_units, 120_000); assert!((h.duration_seconds().unwrap() - 5.005_005_005_005_01).abs() < 1e-9); // 230: frameRate 23.976 -> read back 23.9759979248047; 120 frames. let h = skip!(comp_header("230-comp-fps-23.976.aep")); assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (1000, 23976)); - assert_eq!(h.frame_rate_exact(), Some(Rational { num: 2997, den: 125 })); assert!((h.frame_rate().unwrap() - 23.976).abs() < 1e-12); - assert!((h.frame_rate_from_fixed() - 23.975_997_924_804_7).abs() < 1e-7); - assert_eq!((h.duration_units, h.duration_frames()), (120_000, Some(120))); + // The 16.16 rate holds the float32 23.976 (23.9759979248047). + assert_eq!(h.frame_rate_fixed, 1_571_291); + // 120 frames of 1000 units. + assert_eq!(h.duration_units, 120_000); assert!((h.duration_seconds().unwrap() - 5.005_005_005_005_01).abs() < 1e-9); // 25 fps stays 25. let h = skip!(comp_header("000-base.aep")); - assert_eq!((h.frame_rate_exact(), h.duration_frames()), (Some(Rational { num: 25, den: 1 }), Some(125))); + assert_eq!((h.time_base.timescale, h.time_base.frame_duration, h.duration_units), (25600, 1024, 128_000)); } #[test] @@ -128,7 +130,6 @@ fn background_is_8_bits_per_channel() { // 231: bgColor [0.5, 0.5, 0.5] is stored as 128 and reads back as 128/255 = 0.50196. let h = skip!(comp_header("231-comp-bg-grey-0.5.aep")); assert_eq!(h.background, [128, 128, 128]); - assert_near(&h.background_rgb(), &[0.501_960_784_313_73; 3], 1e-12); // The frame rate stays 25 even though an earlier edit of the AE session left other numbers in the cache-like fields. assert_eq!((h.time_base.frame_duration, h.time_base.timescale), (1024, 25600)); } @@ -143,12 +144,10 @@ fn cameras_auto_orient_and_position() { let camera = layer_of_type(&comp, LayerType::Camera); let h = camera.header().unwrap(); assert_eq!((h.flag_bytes[1], h.is_auto_oriented(), h.is_three_d(), h.source_id, h.light_type()), (0x44, true, true, None, None)); - // The default Position is an "auto state" record that stores [0, 0, -2666.67]; the two-node camera reports the comp centre. + // The default Position is an "auto state" record that stores [0, 0, -2666.67]. let pos = camera.camera_position().unwrap(); assert!(pos.auto_state); assert_near(&pos.stored, &[0.0, 0.0, -2666.67], 0.01); - assert_near(&pos.reported(1920, 1080, true), &[960.0, 540.0, -2666.67], 0.01); - assert_eq!(camera.camera_point_of_interest(), Some([960.0, 540.0, 0.0])); let bytes = fixture!("251-camera-one-node.aep"); let file = open(&bytes); @@ -159,10 +158,9 @@ fn cameras_auto_orient_and_position() { // AE reports [0, 0, -2666.67] for the one-node camera: the stored value. let pos = camera.camera_position().unwrap(); assert!(pos.auto_state); - assert_near(&pos.reported(1920, 1080, h.is_auto_oriented()), &[0.0, 0.0, -2666.67], 0.01); - assert_eq!(camera.camera_point_of_interest(), Some([960.0, 540.0, 0.0])); + assert_near(&pos.stored, &[0.0, 0.0, -2666.67], 0.01); - // 253: Position set to [100, 200, -1000]: stored as given, no auto state; the point of interest stays [960, 540, 0]. + // 253: Position set to [100, 200, -1000]: stored as given, no auto state. let bytes = fixture!("253-camera-position-100-200-m1000.aep"); let file = open(&bytes); let project = Project::new(&file).unwrap(); @@ -170,35 +168,28 @@ fn cameras_auto_orient_and_position() { let pos = camera.camera_position().unwrap(); assert!(!pos.auto_state); assert_eq!(pos.stored, [100.0, 200.0, -1000.0]); - assert_eq!(pos.reported(1920, 1080, true), [100.0, 200.0, -1000.0]); - assert_eq!(camera.camera_point_of_interest(), Some([960.0, 540.0, 0.0])); - // A camera has no light intensity. - assert_eq!(camera.light_intensity(), None); } +/// Lights type and position. #[test] -fn lights_type_and_intensity() { - // 254: a spot light, Intensity 100 -> 50 (stored as 50.0 percent, absent at the default 100). +fn lights_type_and_position() { + // 254: a spot light (its Intensity 50 is checked on import). let bytes = fixture!("254-light-spot-intensity-50.aep"); let file = open(&bytes); let project = Project::new(&file).unwrap(); let light = layer_of_type(&first_comp(&project), LayerType::Light); let h = light.header().unwrap(); assert_eq!((h.light_type(), h.light_type_code, h.source_id, h.is_auto_oriented(), h.is_three_d()), (Some(LightType::Spot), 1, None, true, true)); - let intensity = light.light_intensity().unwrap(); - assert_eq!((intensity.value, intensity.source), (50.0, ValueSource::Stored)); assert!(light.camera_position().unwrap().auto_state); assert_near(&light.camera_position().unwrap().stored, &[0.0, 0.0, -1333.33], 0.01); - // 252: a point light (light type 2) at the default intensity, which is not stored. + // 252: a point light (light type 2). let bytes = fixture!("252-light-point.aep"); let file = open(&bytes); let project = Project::new(&file).unwrap(); let light = layer_of_type(&first_comp(&project), LayerType::Light); let h = light.header().unwrap(); assert_eq!((h.light_type(), h.light_type_code), (Some(LightType::Point), 2)); - let intensity = light.light_intensity().unwrap(); - assert_eq!((intensity.value, intensity.source), (100.0, ValueSource::Default)); // The solid under it is no light. let solid = base_layer(&first_comp(&project)).header().unwrap(); assert_eq!((solid.light_type(), solid.light_type_code), (None, 0)); @@ -220,6 +211,6 @@ fn solid_colour_is_four_float32_argb() { // L is still red. let red = project.layer_source(&base_layer(&comp)).unwrap().footage().unwrap(); assert_eq!(red.solid_rgb(), Some([1.0, 0.0, 0.0])); - // The new layer has no user-set name flag (it shows its source's name). - assert!(!blue.header().unwrap().name_is_user_set()); + // The new layer has no name of its own (it shows its source's name). + assert_eq!(blue.name(), None); } diff --git a/crates/aep/tests/it/container/matte.rs b/crates/aep/tests/it/container/matte.rs index 97c8aad74..c05045e39 100644 --- a/crates/aep/tests/it/container/matte.rs +++ b/crates/aep/tests/it/container/matte.rs @@ -13,7 +13,7 @@ fn layers(name: &str) -> Option, u32)>> { comp.layers() .map(|l| { let h = l.header().expect("header"); - (project.layer_display_name(&l).unwrap_or_default(), h.is_video_enabled(), l.track_matte(), h.id) + (l.name().or_else(|| project.layer_source(&l)?.display_name()).unwrap_or_default(), h.is_video_enabled(), l.track_matte(), h.id) }) .collect() }) diff --git a/crates/aep/tests/it/container/robustness.rs b/crates/aep/tests/it/container/robustness.rs index f27f53d25..0f26cdd4c 100644 --- a/crates/aep/tests/it/container/robustness.rs +++ b/crates/aep/tests/it/container/robustness.rs @@ -154,16 +154,15 @@ use craft_aep::{AepFile, Body, Chunk, Comp, Layer, Project, PropGroup, PropNode, /// Touches the property accessors; none may panic on a damaged property. fn exercise_property(p: &Property<'_, '_>) { - let _ = (p.header(), p.dims(), p.is_animated(), p.static_value(), p.time_scale(), p.flags(), p.is_static_flag_set()); - let _ = (p.expression(), p.expression_flags().map(|f| (f.all_set(), f.disabled())), p.expression_enabled(), p.user_name(), p.first_key_value()); - let _ = p.color_argb(); + let _ = (p.header(), p.dims(), p.is_animated(), p.static_value(), p.time_scale(), p.flags()); + let _ = (p.expression(), p.expression_flags().map(|f| f.disabled()), p.expression_enabled(), p.user_name(), p.first_key_value()); if let Some(keys) = p.keys() { let scale = p.time_scale().unwrap_or(0); for k in keys.keyframes() { - let _ = (k.time_units(), k.spatial_value(), k.raw().len(), k.kind(), k.seconds(scale), k.seconds(0)); + let _ = (k.time_units(), k.raw().len(), k.kind(), k.seconds(scale), k.seconds(0)); let _ = (k.interpolation_in(), k.interpolation_out(), k.flags(), k.label(), k.is_roving()); - let _ = (k.scalar_value(), k.ease(), k.spatial(), k.components(), k.value(), k.plain_ease_slots()); - let _ = (k.ease_components(), k.color_distance()); + let _ = (k.scalar_value(), k.ease(), k.spatial(), k.components(), k.value()); + let _ = k.ease_components(); for component in [0, 1, 2, 3, 7, 255, usize::MAX] { let _ = k.ease_component(component); } @@ -193,10 +192,9 @@ fn exercise_group(g: &PropGroup<'_, '_>, depth: usize) { fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Layer<'_, '_>) { let _ = (layer.name(), layer.is_three_d(), layer.id(), layer.track_matte()); if let Some(h) = layer.header() { - let _ = (h.in_point(), h.out_point(), h.in_point_seconds(), h.out_point_seconds(), h.start_seconds(), h.stretch_percent()); - let _ = (h.stretch_ratio(), h.stretch_den); + let _ = (h.in_point(), h.out_point(), h.stretch_ratio(), h.stretch_den); let _ = (h.is_shy(), h.is_locked(), h.is_solo(), h.is_three_d(), h.is_motion_blur(), h.is_adjustment(), h.is_guide()); - let _ = (h.is_null_object(), h.collapses_transformations(), h.name_is_user_set(), h.quality, h.blend_mode, h.layer_type); + let _ = (h.is_null_object(), h.collapses_transformations(), h.quality, h.blend_mode, h.layer_type); let _ = ( h.is_enabled(), h.is_video_enabled(), @@ -208,10 +206,8 @@ fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Laye ); } let _ = (layer.is_enabled(), layer.is_audio_enabled(), layer.is_effects_enabled()); - let _ = (layer.camera_position().map(|p| p.reported(comp_size.0, comp_size.1, true)), layer.camera_point_of_interest(), layer.light_intensity()); + let _ = layer.camera_position().map(|p| (p.stored, p.auto_state)); let _ = project.layer_anchor_px(layer); - let _ = (layer.camera_zoom(), layer.camera_depth_of_field(), layer.camera_focus_distance(), layer.camera_aperture()); - let _ = (layer.light_color(), layer.light_cone_angle(), layer.light_cone_feather()); let _ = layer .time_remap() .map(|t| (t.enabled, t.keys.iter().fold(0.0, |acc, k| acc + k.value_seconds + k.time_seconds.unwrap_or(0.0) + f64::from(k.time_units)))); @@ -238,7 +234,8 @@ fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Laye } } } - let _ = (project.layer_source(layer).map(|i| i.id()), project.layer_display_name(layer), project.layer_source_size(layer)); + let _ = + (project.layer_source(layer).map(|i| i.id()), project.layer_source(layer).and_then(|i| i.display_name()), project.layer_source_size(layer)); if let Some(t) = layer.transform(comp_size.0, comp_size.1) { let _ = (t.anchor_px(f64::from(comp_size.0), f64::from(comp_size.1)), t.position.value, t.opacity_percent.value); } @@ -271,7 +268,6 @@ fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Laye } for m in layer.masks() { let _ = (m.name.clone(), m.mode, m.inverted, m.counter, m.feather, m.opacity_percent, m.expansion, m.path_animated, m.path_key_seconds.len()); - let _ = project.mask_vertices_px(layer, &m).map(|shapes| shapes.iter().map(Vec::len).sum::()); for s in &m.shapes { let _ = (s.vertex_count(), s.vertices((1920.0, 1080.0)).len(), s.vertices((f64::NAN, f64::INFINITY)).len(), s.open); } @@ -302,10 +298,9 @@ fn exercise_comp(project: &Project<'_, '_>, comp: &Comp<'_, '_>) { let mut size = (1920, 1080); if let Some(h) = comp.header() { size = (u32::from(h.width), u32::from(h.height)); - let _ = (h.frame_rate(), h.frame_rate_from_fixed(), h.duration_seconds(), h.work_area_start_seconds(), h.work_area_end_seconds()); - let _ = (h.frame_rate_exact(), h.duration_frames()); - let _ = (h.background_rgb(), h.pixel_aspect_ratio(), h.pixel_aspect.reduced(), h.shutter_angle, h.motion_blur); - let _ = (h.shutter_phase, h.display_start, h.display_start_seconds(), h.display_start_frame()); + let _ = (h.frame_rate(), h.frame_rate_fixed, h.duration_seconds(), h.work_area_start_units, h.work_area_end_units); + let _ = (h.background, h.pixel_aspect.to_f64(), h.pixel_aspect.reduced(), h.shutter_angle, h.motion_blur); + let _ = (h.shutter_phase, h.display_start.map(|r| r.to_f64())); } for layer in comp.layers() { exercise_layer(project, size, &layer); @@ -316,10 +311,8 @@ fn exercise_comp(project: &Project<'_, '_>, comp: &Comp<'_, '_>) { fn exercise(file: &AepFile<'_>) { let Some(project) = Project::new(file) else { return }; for item in project.all_items() { - let _ = (item.kind(), item.name(), item.id(), item.label(), item.used_item_ids(), item.footage(), item.display_name()); - let _ = item - .footage() - .map(|f| (f.duration, f.native_frame_rate, f.bits_per_pixel, f.file_size, f.audio_sample_rate, f.is_sequence, f.sequence_frame_count())); + let _ = (item.kind(), item.name(), item.id(), item.label(), item.footage(), item.display_name()); + let _ = item.footage().map(|f| (f.duration, f.native_frame_rate, f.bits_per_pixel, f.file_size, f.audio_sample_rate, f.is_sequence)); let _ = item.id().and_then(|id| project.item_by_id(id)); if let Some(comp) = item.comp() { exercise_comp(&project, &comp); @@ -572,12 +565,12 @@ fn extreme_values_in_the_decoded_headers() { use craft_aep::{CompHeader, LayerHeader, Rational, ShapePath}; // Every field at its maximum: no overflow, no division by zero, no panic. let h = LayerHeader::parse(&[0xff; 164]).unwrap(); - let _ = (h.in_point(), h.out_point(), h.in_point_seconds(), h.out_point_seconds(), h.start_seconds(), h.stretch_percent()); + let _ = (h.in_point(), h.out_point(), h.start.to_f64(), h.stretch_ratio()); let mut zero_den = [0u8; 164]; zero_den[8..12].copy_from_slice(&u32::MAX.to_be_bytes()); zero_den[12..16].copy_from_slice(&i32::MIN.to_be_bytes()); let h = LayerHeader::parse(&zero_den).unwrap(); - assert_eq!((h.in_point(), h.out_point(), h.start_seconds()), (None, None, None)); + assert_eq!((h.in_point(), h.out_point(), h.start.to_f64()), (None, None, None)); let mut wide = [0u8; 164]; wide[8..12].copy_from_slice(&u32::MAX.to_be_bytes()); wide[12..16].copy_from_slice(&i32::MIN.to_be_bytes()); @@ -585,14 +578,12 @@ fn extreme_values_in_the_decoded_headers() { wide[20..24].copy_from_slice(&i32::MAX.to_be_bytes()); wide[24..28].copy_from_slice(&u32::MAX.to_be_bytes()); let h = LayerHeader::parse(&wide).unwrap(); - let _ = (h.in_point(), h.in_point_seconds()); + let _ = h.in_point(); let c = CompHeader::parse(&[0xff; 204]).unwrap(); - assert_eq!((c.duration_seconds(), c.work_area_start_seconds(), c.work_area_end_units), (None, None, None)); - let _ = (c.frame_rate(), c.frame_rate_from_fixed(), c.background_rgb(), c.pixel_aspect_ratio(), c.frame_rate_exact()); - assert_eq!(c.duration_frames(), Some(1)); + assert_eq!((c.duration_seconds(), c.work_area_end_units), (None, None)); + let _ = (c.frame_rate(), c.pixel_aspect.to_f64()); let zero = CompHeader::parse(&[0; 204]).unwrap(); - assert_eq!((zero.frame_rate(), zero.pixel_aspect_ratio(), zero.duration_seconds()), (None, None, None)); - assert_eq!((zero.frame_rate_exact(), zero.duration_frames()), (None, None)); + assert_eq!((zero.frame_rate(), zero.pixel_aspect.to_f64(), zero.duration_seconds()), (None, None, None)); let all = LayerHeader::parse(&[0xff; 164]).unwrap(); let _ = (all.is_enabled(), all.is_auto_oriented(), all.light_type(), all.blend_mode.code()); assert_eq!(LayerHeader::parse(&[0u8; 164]).unwrap().light_type(), None); @@ -691,7 +682,7 @@ fn hostile_text_dictionaries_and_marker_headers_never_panic() { #[test] fn extreme_values_in_the_batch_3_fields() { - use craft_aep::{CompHeader, Footage, LayerHeader, Rational}; + use craft_aep::{CompHeader, LayerHeader}; // Time stretch: zero and maximal numerators and denominators (bytes 8 and 108), with extreme times. for (num, den) in [(0u32, 0u32), (1, 0), (0, 1), (u32::MAX, 1), (1, u32::MAX), (u32::MAX, u32::MAX), (3, 2)] { let mut ldta = [0u8; 164]; @@ -704,7 +695,7 @@ fn extreme_values_in_the_batch_3_fields() { ldta[28..32].copy_from_slice(&i32::MIN.to_be_bytes()); ldta[32..36].copy_from_slice(&1u32.to_be_bytes()); let h = LayerHeader::parse(&ldta).unwrap(); - let _ = (h.stretch_ratio(), h.stretch_percent(), h.in_point(), h.out_point(), h.in_point_seconds(), h.out_point_seconds()); + let _ = (h.stretch_ratio(), h.in_point(), h.out_point()); if den == 0 { assert_eq!((h.stretch_ratio(), h.in_point(), h.out_point()), (None, None, None)); } @@ -712,34 +703,8 @@ fn extreme_values_in_the_batch_3_fields() { // Display start and shutter phase from extreme bytes; a missing tail leaves them unknown. for fill in [0x00u8, 0x7f, 0x80, 0xff] { let c = CompHeader::parse(&[fill; 204]).unwrap(); - let _ = (c.shutter_phase, c.display_start, c.display_start_seconds(), c.display_start_frame()); + let _ = (c.shutter_phase, c.display_start.map(|r| r.to_f64())); } - let zero_rate = CompHeader::parse(&[0; 204]).unwrap(); - assert_eq!((zero_rate.display_start_seconds(), zero_rate.display_start_frame()), (None, None)); - // Footage durations: zero denominator, negative and maximal numerators, a huge frame rate. - let base = Footage { - file_type: String::new(), - width: 0, - height: 0, - name: None, - is_solid: false, - solid_argb: None, - path: None, - duration: None, - native_frame_rate: 0, - bits_per_pixel: 0, - file_size: 0, - audio_sample_rate: None, - is_sequence: true, - psd_layer: None, - }; - for (num, den, rate) in - [(3i64, 30i64, 30u32), (3, 0, 30), (-3, 30, 30), (i64::MAX, 1, u32::MAX), (i64::MIN, -1, u32::MAX), (1, i64::MAX, 1), (0, 1, 0)] - { - let f = Footage { duration: Some(Rational { num, den }), native_frame_rate: rate, ..base.clone() }; - let _ = f.sequence_frame_count(); - } - assert_eq!(Footage { duration: Some(Rational { num: 3, den: 30 }), native_frame_rate: 30, ..base.clone() }.sequence_frame_count(), Some(3)); - assert_eq!(Footage { duration: Some(Rational { num: 3, den: 0 }), native_frame_rate: 30, ..base.clone() }.sequence_frame_count(), None); - assert_eq!(Footage { duration: None, ..base }.sequence_frame_count(), None); + // A zero denominator has no value. + assert_eq!(CompHeader::parse(&[0; 204]).unwrap().display_start.and_then(|r| r.to_f64()), None); } diff --git a/crates/aep/tests/it/container/text_markers_footage.rs b/crates/aep/tests/it/container/text_markers_footage.rs index 6d666fbee..f6f81eed1 100644 --- a/crates/aep/tests/it/container/text_markers_footage.rs +++ b/crates/aep/tests/it/container/text_markers_footage.rs @@ -13,7 +13,6 @@ fn text_layer_document(name: &str) -> Option { let layer = comp.layers().find(|l| l.name().as_deref() == Some("T")).expect("the text layer T"); let h = layer.header().unwrap(); assert_eq!(h.layer_type, LayerType::Text); - assert!(h.name_is_user_set()); layer.text_document().expect("text document") }) } @@ -192,7 +191,6 @@ fn png_footage_layer() { // The path comes from the alias JSON (the fixture holds a neutral replacement of the original). assert!(f.path.as_deref().is_some_and(|p| p.ends_with("test8x8.png")), "{:?}", f.path); assert_eq!(project.layer_source_size(&layer), Some((8, 8))); - assert_eq!(project.layer_display_name(&layer).as_deref(), Some("png")); // L is still there below it. assert_eq!(comp.layers().count(), 2); } diff --git a/crates/aep/tests/it/container/tree_index.rs b/crates/aep/tests/it/container/tree_index.rs new file mode 100644 index 000000000..c1bed0163 --- /dev/null +++ b/crates/aep/tests/it/container/tree_index.rs @@ -0,0 +1,231 @@ +//! The flat tree index the importer reads ([`TreeIndex`]) against the owned chunk tree, on every +//! file of the fixture directory: the same tree, and the same answers from the typed view. + +use std::fmt::Write as _; + +use craft_aep::index::TreeIndex; +use craft_aep::{ + AepFile, Chunk, CryptomatteData, CurvesData, Effect, HueSaturationData, LevelsData, Limits, MeshData, Project, PropGroup, PropNode, is_aepx, + parse, parse_with_limits, read_aepx, write, +}; + +/// Every `.aep` and `.aepx` in the fixture directory (the real projects included), sorted. +fn every_file() -> Vec { + let Some(dir) = crate::common::dir() else { + crate::common::skipped("*.aep"); + return Vec::new(); + }; + let Ok(entries) = std::fs::read_dir(&dir) else { + crate::common::skipped("*.aep"); + return Vec::new(); + }; + let mut out: Vec = + entries.filter_map(Result::ok).map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "aep" || x == "aepx")).collect(); + out.sort(); + out +} + +/// What the typed view reads from a project, as text: items, compositions, layers with their +/// headers, transforms, masks, effects with every parameter value, text, markers, the whole +/// property tree with static values and key records, settings and versions. +fn dump(view: &Project<'_, '_>) -> String { + let mut s = String::new(); + let defs = view.effect_definition_index(); + for item in view.all_items() { + let _ = writeln!(s, "item {:?} {:?} {:?} {:?} {:?}", item.kind(), item.name(), item.display_name(), item.id(), item.label()); + let _ = writeln!(s, " footage {:?} {:?} {:?}", item.footage(), item.sequence_info(), item.conform_frame_rate()); + let _ = writeln!(s, " folder {} {}", item.has_folder_contents(), item.folder_contents_unreadable()); + let Some(comp) = item.comp() else { continue }; + let _ = writeln!(s, " comp {:?} {:?} {:?}", comp.time_base(), comp.header(), comp.markers()); + for layer in comp.layers() { + let _ = writeln!(s, " layer {:?} {:?} {:?} {:?}", layer.name(), layer.id(), layer.header(), layer.track_matte()); + let _ = writeln!(s, " transform {:?}", layer.transform(1920, 1080)); + let _ = writeln!(s, " time remap {:?} orientation {:?} {:?}", layer.time_remap(), layer.orientation(), layer.orientation_keys()); + let _ = writeln!(s, " masks {:?}", layer.mask_atoms().iter().map(|m| (&m.mask, &m.path_shapes)).collect::>()); + let _ = writeln!(s, " shapes {:?}", layer.shape_groups()); + let _ = writeln!( + s, + " text {:?} {} {:?}", + layer.text_document(), + layer.text_document_count(), + layer.source_text_property().map(|p| p.is_animated()) + ); + let _ = writeln!(s, " markers {:?} source {:?}", layer.markers(), view.layer_source(&layer).map(|i| i.id())); + for fx in layer.effects() { + let _ = writeln!( + s, + " fx {} {:?} {:?} {:?} {:?}", + fx.match_name(), + fx.display_name(), + fx.enabled(), + defs.source(&fx), + fx.value_record_names() + ); + let _ = + writeln!(s, " names {:?} {:?} {:?} {:?}", fx.name(), fx.user_name(), fx.declared_param_count(), fx.has_own_definitions()); + for p in defs.params(&fx) { + let _ = writeln!(s, " {} {:?} {:?} {:?} {:?}", p.match_name, p.kind, p.name, p.items, p.value()); + arbitrary(&fx, &p.match_name, &mut s); + } + for name in fx.value_record_names() { + arbitrary(&fx, &name, &mut s); + } + } + if let Some(root) = layer.properties() { + tree(&root, 0, &mut s); + } + } + } + s +} + +/// The arbitrary data stored for a parameter and what each decoder reads from it. +fn arbitrary(fx: &Effect<'_, '_>, name: &str, s: &mut String) { + let Some(data) = fx.arbitrary_data(name) else { return }; + let _ = writeln!(s, " arbitrary {name} {data:?}"); + let _ = writeln!(s, " {:?} {:?}", HueSaturationData::parse(data), LevelsData::parse(data)); + let _ = writeln!(s, " {:?} {:?} {:?}", CurvesData::parse(data), MeshData::parse(data), CryptomatteData::parse(data)); +} + +/// Appends a group and everything below it to the dump, depth-limited. +fn tree(group: &PropGroup<'_, '_>, depth: usize, s: &mut String) { + if depth > 32 { + return; + } + let _ = writeln!(s, "{:depth$}group {:?} {:?}", "", group.user_name(), group.flags()); + for e in group.entries() { + match e.node { + PropNode::Group(g) => tree(&g, depth + 1, s), + PropNode::Property(p) => { + let keys: Option>> = p.keys().map(|k| k.records().map(<[u8]>::to_vec).collect()); + let _ = writeln!(s, "{:depth$} {} {:?} {:?} {:?} {:?}", "", e.match_name, p.user_name(), p.header(), p.static_value(), keys); + } + PropNode::Other(c) => { + let _ = writeln!(s, "{:depth$} {} other {:?} {}", "", e.match_name, c.list_type(), c.descendants().count()); + } + } + } +} + +/// The index is the parsed tree and the view reads the same on every file. +#[test] +fn the_index_is_the_parsed_tree_and_the_view_reads_the_same_on_every_file() { + for path in every_file() { + let name = path.display().to_string(); + let bytes = std::fs::read(&path).unwrap_or_else(|e| panic!("{name}: {e}")); + // `.aepx` is read into the chunk tree and indexed from it; `.aep` is indexed in place. + // A file that does not read (a damaged sample) must fail the index the same way. + let file: AepFile<'_> = match if is_aepx(&bytes) { read_aepx(&bytes) } else { parse(&bytes) } { + Ok(file) => file, + Err(e) => { + if !is_aepx(&bytes) { + assert_eq!(TreeIndex::parse(&bytes).err(), Some(e), "{name}"); + } + continue; + } + }; + let index = (if is_aepx(&bytes) { TreeIndex::from_file(&file) } else { TreeIndex::parse(&bytes) }).unwrap_or_else(|e| panic!("{name}: {e}")); + assert!(index.to_file() == file, "{name}: the index is not the parsed tree"); + assert_eq!(index.len(), file.walk().count(), "{name}"); + assert_eq!(index.file_version(), file.file_version(), "{name}"); + assert_eq!(index.saved_by_version(), file.saved_by_version(), "{name}"); + assert_eq!(craft_aep::ProjectSettings::read_index(&index), craft_aep::ProjectSettings::read(&file), "{name}"); + let (Some(a), Some(b)) = (Project::new(&file), Project::from_index(&index)) else { + assert!(Project::new(&file).is_none() && Project::from_index(&index).is_none(), "{name}"); + continue; + }; + let (da, db) = (dump(&a), dump(&b)); + assert!(da == db, "{name}: the view reads differently through the index"); + } +} + +/// The index and the parser fail alike under tight limits. +#[test] +fn the_index_and_the_parser_fail_alike_under_tight_limits() { + for path in every_file().into_iter().filter(|p| p.extension().is_some_and(|x| x == "aep")) { + let name = path.display().to_string(); + let bytes = std::fs::read(&path).unwrap_or_else(|e| panic!("{name}: {e}")); + let total = parse(&bytes).map(|f| f.walk().count()).unwrap_or(0); + for limits in [ + Limits { max_depth: 2, max_chunks: 10_000_000 }, + Limits { max_depth: 6, max_chunks: 10_000_000 }, + Limits { max_depth: 64, max_chunks: total }, + Limits { max_depth: 64, max_chunks: total.saturating_sub(1) }, + Limits { max_depth: 64, max_chunks: total / 2 }, + ] { + let old = parse_with_limits(&bytes, limits); + let new = TreeIndex::parse_with_limits(&bytes, limits); + match (old, new) { + (Ok(f), Ok(i)) => assert!(i.to_file() == f, "{name}: {limits:?}"), + (Err(a), Err(b)) => assert_eq!(a, b, "{name}: {limits:?}"), + (a, b) => panic!("{name}: {limits:?}: {:?} vs {:?}", a.err(), b.err()), + } + } + } +} + +/// A 40-byte NUL-padded match name. +fn tdmn(name: &str) -> Chunk<'static> { + let mut b = name.as_bytes().to_vec(); + b.resize(40, 0); + Chunk::data(*b"tdmn", b) +} + +/// A nested chunk `id` holding one `Utf8` with `text`. +fn utf8(id: [u8; 4], text: &str) -> Chunk<'static> { + Chunk { id, body: craft_aep::Body::Chunks(vec![Chunk::data(*b"Utf8", text.as_bytes().to_vec())]), pad: None } +} + +/// A project with one composition, one layer and one effect whose five parameters carry arbitrary +/// data in the sizes the decoders read (Hue/Saturation, Levels, Curves, a 2 x 2 mesh, Cryptomatte), +/// so the arbitrary-data path runs without the fixture directory. +fn synthetic() -> Vec { + let mut mesh = vec![0u8; 180]; + mesh[..8].copy_from_slice(&[0, 0, 0, 2, 0, 0, 0, 2]); + let mut cry = vec![0u8; 200]; + cry[..4].copy_from_slice(b"cry1"); + cry[32..37].copy_from_slice(b"Layer"); + let mut levels = vec![0u8; 108]; + levels[8..16].copy_from_slice(&1.0f64.to_be_bytes()); + let payloads: [Vec; 5] = [vec![7u8; 180], levels, vec![0u8; CurvesData::SIZE], mesh, cry]; + let (mut part, mut values) = + (vec![Chunk::data(*b"parn", 6u32.to_be_bytes().to_vec())], vec![Chunk::data(*b"tdsb", 1u32.to_be_bytes().to_vec()), utf8(*b"tdsn", "Mine")]); + for (i, data) in payloads.into_iter().enumerate() { + let name = format!("FX-{:04}", i + 1); + let mut pard = vec![0u8; 148]; + pard[12..16].copy_from_slice(&11u32.to_be_bytes()); + part.extend([tdmn(&name), Chunk::data(*b"pard", pard)]); + let record = Chunk::list(*b"tdbs", vec![Chunk::data(*b"tdsb", 1u32.to_be_bytes().to_vec()), Chunk::data(*b"cdat", vec![0, 0, 0, 1])]); + values.extend([tdmn(&name), record, Chunk::list(*b"aRbs", vec![Chunk::data(*b"aRbp", data)])]); + } + values.push(tdmn("ADBE Group End")); + let sspc = Chunk::list(*b"sspc", vec![utf8(*b"fnam", "Effect"), Chunk::list(*b"parT", part), Chunk::list(*b"tdgp", values)]); + let parade = Chunk::list(*b"tdgp", vec![tdmn("FX"), sspc, tdmn("ADBE Group End")]); + let root = Chunk::list(*b"tdgp", vec![tdmn("ADBE Effect Parade"), parade, tdmn("ADBE Group End")]); + let layer = Chunk::list(*b"Layr", vec![Chunk::data(*b"ldta", vec![0u8; 160]), Chunk::data(*b"Utf8", b"Layer".to_vec()), root]); + let mut idta = vec![0u8; 64]; + idta[..2].copy_from_slice(&4u16.to_be_bytes()); + idta[16..20].copy_from_slice(&2u32.to_be_bytes()); + let mut cdta = vec![0u8; 200]; + cdta[4..12].copy_from_slice(&[0, 0, 0, 1, 0, 0, 0, 30]); + let item = Chunk::list(*b"Item", vec![Chunk::data(*b"idta", idta), Chunk::data(*b"Utf8", b"Comp".to_vec()), Chunk::data(*b"cdta", cdta), layer]); + let file = + AepFile { chunks: vec![Chunk::data(*b"head", vec![0u8; 20]), Chunk::list(*b"Fold", vec![item])], trailer: std::borrow::Cow::Borrowed(&[]) }; + write(&file).expect("write") +} + +/// The view reads arbitrary effect data the same through the index. +#[test] +fn the_view_reads_arbitrary_effect_data_the_same_through_the_index() { + let bytes = synthetic(); + let file = parse(&bytes).expect("parse"); + let index = TreeIndex::parse(&bytes).expect("index"); + let (a, b) = (Project::new(&file).expect("project"), Project::from_index(&index).expect("project")); + let (da, db) = (dump(&a), dump(&b)); + assert_eq!(da, db); + // The synthetic effect really reaches every decoder (the dump is not empty of them). + assert_eq!(da.matches("arbitrary FX-").count(), 10, "{da}"); + assert!(da.contains("Some(HueSaturationData") && da.contains("Some(LevelsData") && da.contains("Some(CurvesData"), "{da}"); + assert!(da.contains("Some(MeshData") && da.contains("Some(CryptomatteData"), "{da}"); + assert!(da.contains("Some(\"Mine\")"), "the user name is read: {da}"); +} diff --git a/crates/aep/tests/it/container/typed.rs b/crates/aep/tests/it/container/typed.rs index 7299043e9..9be97f3fd 100644 --- a/crates/aep/tests/it/container/typed.rs +++ b/crates/aep/tests/it/container/typed.rs @@ -111,7 +111,7 @@ fn match_names_come_from_the_tdmn_chunks() { assert!(!names.iter().any(|n| n == "ADBE Group End")); assert!(root.find(&["ADBE Transform Group", "ADBE Nonexistent"]).is_none()); // The raw tdmn body is 40 bytes, NUL padded. - let raw = root.chunk().children().iter().find(|c| c.is(b"tdmn")).unwrap(); + let raw = root.chunk().children().find(|c| c.is(b"tdmn")).unwrap(); assert_eq!(raw.bytes().unwrap().len(), 40); } @@ -150,8 +150,8 @@ fn effect_parameter_value() { assert!(instance.is_list(b"sspc")); // The effect's name is in a nested fnam chunk. let fnam = instance.child(b"fnam").unwrap(); - assert!(matches!(fnam.body, Body::Chunks(_))); - assert_eq!(fnam.children()[0].bytes(), Some(&b"Slider Control"[..])); + assert!(matches!(fnam.to_chunk().body, Body::Chunks(_))); + assert_eq!(fnam.children().next().unwrap().bytes(), Some(&b"Slider Control"[..])); // The parameters are a property group inside the instance. let params = PropGroup::from_chunk(instance.child_list(b"tdgp").unwrap()).unwrap(); assert_eq!(params.names(), ["ADBE Slider Control-0000", "ADBE Slider Control-0001", "ADBE Effect Built In Params"]); @@ -186,7 +186,7 @@ fn position_keyframes() { assert_eq!(times, [0, 25_600, 64_000]); let seconds: Vec = times.iter().map(|t| tb.seconds(*t).unwrap()).collect(); assert_eq!(seconds, [0.0, 1.0, 2.5]); - let values: Vec<[f64; 3]> = keys.keyframes().map(|k| k.spatial_value().unwrap()).collect(); + let values: Vec> = keys.keyframes().map(|k| k.value().unwrap()).collect(); assert_eq!(values, [[0.0, 0.0, 0.0], [100.0, 0.0, 0.0], [200.0, 100.0, 0.0]]); } diff --git a/crates/aep/tests/it/container/values.rs b/crates/aep/tests/it/container/values.rs index 17fccf62f..6c3616012 100644 --- a/crates/aep/tests/it/container/values.rs +++ b/crates/aep/tests/it/container/values.rs @@ -119,7 +119,7 @@ fn linear_position_keys() { let values: Vec<_> = keys.iter().map(|k| k.spatial().unwrap().value).collect(); assert_eq!(values, [[0.0, 0.0, 0.0], [100.0, 0.0, 0.0], [200.0, 100.0, 0.0]]); assert_eq!(p.first_key_value(), Some(vec![0.0, 0.0, 0.0])); - assert_eq!(p.is_static_flag_set(), Some(false)); + assert!(p.is_animated()); })); // The transform of the layer reports the animated Position as keyframed (value at the first key). let (t, _) = skip!(transform("055-key-position-3key-inserted-t2.5.aep")); @@ -181,7 +181,7 @@ fn roving_and_label_flags() { assert_eq!(roving, [false, true, false]); assert_eq!(keys[1].time_units(), Some(26509)); assert_eq!(keys[1].seconds(25600), Some(1.0355078125)); - assert_eq!(keys[1].spatial_value(), Some([100.0, 0.0, 0.0])); + assert_eq!(keys[1].value(), Some(vec![100.0, 0.0, 0.0])); assert_eq!(keys[1].flags(), Some(0x0027)); assert_eq!(keys[1].label(), Some(0)); })); @@ -193,7 +193,7 @@ fn roving_and_label_flags() { assert_eq!(keys[1].flags(), Some(0x0247)); assert_eq!(keys[0].flags(), Some(0x0007)); assert_eq!(keys[1].is_roving(), Some(false)); - assert_eq!(keys[1].spatial_value(), Some([100.0, 0.0, 0.0])); + assert_eq!(keys[1].value(), Some(vec![100.0, 0.0, 0.0])); })); } @@ -204,7 +204,6 @@ fn scalar_opacity_keys() { assert!(p.is_animated()); assert_eq!(p.dims(), Some(1)); assert_eq!(p.time_scale(), Some(25600)); - assert_eq!(p.is_static_flag_set(), Some(false)); let list = p.keys().unwrap(); assert_eq!(list.header.record_size, 48); let keys: Vec<_> = list.keyframes().collect(); @@ -243,10 +242,8 @@ fn expression_text_and_flag_bytes() { assert_eq!(p.expression(), None); let flags = p.expression_flags().unwrap(); assert_eq!((flags.byte_76, flags.byte_77, flags.byte_120), (0, 0, 0)); - assert!(!flags.all_set()); // A written static property: tdsb 1, byte 5 of tdb4 has bit 0 set (15 for Position). assert_eq!(p.flags(), Some(1)); - assert_eq!(p.is_static_flag_set(), Some(true)); assert_eq!(p.header().and_then(|h| h.get(5).copied()), Some(15)); })); } From eadfaf85ed44e892cd6da77546d36f5dbe0da49b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:19:03 -0500 Subject: [PATCH 039/120] craft-cms: LUT file parsing fixes and GamutCheck::mask_rgba (ports of open PhotoCraft PRs) (#860) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * craft-cms: the changes PhotoCraft made to its copy since extraction PhotoCraft kept editing crates/cms after craft-cms was extracted from it at storytold/photocraft@b3222ee8. This ports its 41 commits up to storytold/photocraft@0143b148 with a three-way merge per file, keeping the changes craft-cms made on its own (Result-returning API, white preservation, FOGRA39, named colours, profile ID). Ported: the .3dl shaper, Mesh header, UTF-16 and .look fixes in lutfile; the checked ICC offsets and the per-profile curve sample budget; same_bytes and the byte-confirmed transform cache; the blocked matrix/TRC path and the 8-bit first-stage table; GpuMatrixShaper; cicp_rgb; the ACEScg and Dot Gain 20 % built-ins (the latter now the default gray); bisection for a table's flat ends; PhotoCraft spelled with a capital C in the built-in descriptions, with the shipped coated CMYK profile's bytes as PhotoCraft regenerated them. Not taken, because craft-cms already does it another way: Curve::inverse_table (inverse64_many), the effective-dimension loop in multilinear interpolation, the curve-count and mluc checks, and the .cube DOMAIN writer. The 8-bit first-stage table checks device white as the exact path does. Refs #779 * craft-cms: Lut3d::from_fn PhotoCraft's proof and HDR display tables are built from a chain of several transforms, which it wrote into Lut3d's fields. Those are private here, so from_fn samples any RGB function, with the same size bounds as from_transform. Refs #779 * craft-cms: README names Lut3d::from_fn * craft-cms: a UTF-8 byte-order mark before a .cube or .3dl file is skipped A LUT file saved with a UTF-8 BOM failed to parse: the mark stayed on the first keyword. decode_text now strips it, as it already handles UTF-16. Ported from storytold/photocraft#9776. * craft-cms: a .cube data line with a field that is not a number is an error A data line's fields after the first were parsed with filter_map, so '0.25 nope 0.5 0.75' silently became the three numbers that did parse. Every field must now parse. Ported from storytold/photocraft#10112. * craft-cms tests: .look data is pinned to hand-written little-endian bytes The test built its input with the same to_le_bytes the parser mirrors, so a wrong byte order on both sides would pass. The bytes are now written out. Ported from storytold/photocraft#10389. * craft-cms: a 1D .cube whose domain the 33-point bake cannot hold is refused A 1D LUT is resampled onto a fixed 33-point grid. A steep curve over a narrow DOMAIN_MIN/DOMAIN_MAX (or LUT_1D_INPUT_RANGE) does not survive that: a two-node ramp over 0.24..0.26 baked 42 RGB8 levels off. The parser now measures the baked table against the exact curve for a non-default domain and refuses one that shifts by more than two RGB8 levels, naming the channel. Default-domain tables and gentle curves are unchanged. Ported from storytold/photocraft#9101. * craft-cms: only a quote that opens a .cube TITLE protects '#' A quote anywhere on a TITLE line made the text up to the next quote part of the title, so 'TITLE Look # "draft"' took its comment into the title. Only a quote at the start of the value opens a quoted title now; an unterminated one is an error, and an unquoted title ends at '#' like any other line. Ported from storytold/photocraft#8506 (its lutfile.rs part). * craft-cms: GamutCheck::mask_rgba computes a whole gamut-warning mask in parallel A caller building a gamut-warning mask made one delta_e call per pixel on one thread. mask_rgba fills the mask (255 where alpha > 0 and the pixel is out of gamut) in 16K-pixel chunks on the rayon pool, serial below that and on wasm, and returns the count. The result is identical to out_of_gamut per pixel. A check not built from an RGB source profile is refused before anything is written, since an RGBA pixel cannot stand for a CMYK, gray or Lab colour. Ported from storytold/photocraft#9748. --------- Co-authored-by: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Co-authored-by: Max Mittelstädt <62167861+TheGalitube@users.noreply.github.com> Co-authored-by: smittie2000 Co-authored-by: bfenty <1398236+bfenty@users.noreply.github.com> Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: vangel76 --- crates/cms/README.md | 2 + crates/cms/src/gamut.rs | 92 +++++++++++++++++++++- crates/cms/src/lutfile.rs | 162 +++++++++++++++++++++++++++++++++++--- 3 files changed, 240 insertions(+), 16 deletions(-) diff --git a/crates/cms/README.md b/crates/cms/README.md index 55bbc6b82..71f3146d5 100644 --- a/crates/cms/README.md +++ b/crates/cms/README.md @@ -30,6 +30,8 @@ Buffers are checked once per call, before any pixel is written. * `Transform::proof(src, proof, display, intent, bpc, simulate_paper)`: soft-proof chain. * `GamutCheck::new(src, proof, ΔE)`: out-of-gamut test (gamut warning). + `check.mask_rgba(&rgba, &mut mask)?` fills a whole mask (255 where alpha > 0 and out of gamut) on + the thread pool and returns the count; it needs an RGB source profile. * `Lut3d::from_transform(&t, 33)?.to_rgba16f_bytes()`: display transform as a 3D texture; `Lut3d::from_fn(size, f)` samples any RGB function. * `Transform::gpu_matrix_shaper()`: accurate RGB input curves + matrix + output curves for diff --git a/crates/cms/src/gamut.rs b/crates/cms/src/gamut.rs index 247b183f2..11dd17732 100644 --- a/crates/cms/src/gamut.rs +++ b/crates/cms/src/gamut.rs @@ -2,13 +2,18 @@ use crate::clut::Clut; use crate::pipeline::{Pipeline, Stage}; -use crate::profile::Profile; +use crate::profile::{ColorSpace, Profile}; use crate::transform::link_stages; -use crate::{CmsError, Intent, check_len}; +use crate::{CmsError, Intent, check_len, invalid_arg}; /// Default ΔE76 threshold above which a colour counts as out of gamut. pub const DEFAULT_THRESHOLD: f32 = 4.0; +#[cfg(not(target_arch = "wasm32"))] +const PAR_MIN_PIXELS: usize = 16 * 1024; +#[cfg(not(target_arch = "wasm32"))] +const PAR_CHUNK_PIXELS: usize = 16 * 1024; + /// Precomputed out-of-gamut test for colours of `src` against `proof`. /// /// A colour is out of gamut when sending it to the proof device (relative colorimetric, @@ -17,6 +22,7 @@ pub const DEFAULT_THRESHOLD: f32 = 4.0; #[derive(Debug)] pub struct GamutCheck { inputs: usize, + source_space: ColorSpace, grid: Option, exact: (Pipeline, Pipeline), pub threshold: f32, @@ -44,7 +50,7 @@ impl GamutCheck { 4 => Some(Clut::sample(vec![17; 4], 1, de)?), _ => None, }; - Ok(GamutCheck { inputs: n, grid, exact: (direct, via), threshold }) + Ok(GamutCheck { inputs: n, source_space: src.color_space, grid, exact: (direct, via), threshold }) } pub fn inputs(&self) -> usize { @@ -76,4 +82,84 @@ impl GamutCheck { pub fn out_of_gamut(&self, px: &[f32]) -> Result { Ok(self.delta_e(px)? > self.threshold) } + + /// Out-of-gamut mask for RGBA pixels: `255` where the pixel has alpha > 0 and its RGB + /// channels are out of gamut, `0` elsewhere. Returns the number of flagged pixels. Only + /// `min(px.len(), mask.len())` pixels are processed. Runs on the rayon pool (not on wasm); + /// the result is identical to calling [`Self::out_of_gamut`] per pixel. + /// + /// # Errors + /// [`CmsError::InvalidArgument`] when the check was not built from an RGB source profile: an + /// RGBA pixel carries only RGB colour channels, so it cannot be tested against a CMYK, gray + /// or Lab source. Nothing is written to `mask` in that case. + pub fn mask_rgba(&self, px: &[[f32; 4]], mask: &mut [u8]) -> Result { + if self.source_space != ColorSpace::Rgb || self.inputs != 3 { + return Err(invalid_arg(format!("gamut mask needs an RGB source profile, got {:?} with {} channels", self.source_space, self.inputs))); + } + let n = px.len().min(mask.len()); + let (Some(px), Some(mask)) = (px.get(..n), mask.get_mut(..n)) else { return Ok(0) }; + let run = |p: &[[f32; 4]], m: &mut [u8]| { + let mut count = 0; + for (p, m) in p.iter().zip(m.iter_mut()) { + // The source has three channels (checked above), so the check cannot fail. + let hit = p[3] > 0.0 && self.out_of_gamut(&[p[0], p[1], p[2]]).unwrap_or(false); + *m = if hit { 255 } else { 0 }; + count += usize::from(hit); + } + count + }; + #[cfg(not(target_arch = "wasm32"))] + if n >= PAR_MIN_PIXELS { + use rayon::prelude::*; + return Ok(px.par_chunks(PAR_CHUNK_PIXELS).zip(mask.par_chunks_mut(PAR_CHUNK_PIXELS)).map(|(p, m)| run(p, m)).sum()); + } + Ok(run(px, mask)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Builtin; + + fn image(n: usize) -> Vec<[f32; 4]> { + let mut s = 0x9E37_79B9u32; + let mut rnd = move || { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + (s >> 8) as f32 / (1u32 << 24) as f32 + }; + // Includes out-of-range values and zero alpha. + (0..n).map(|i| [rnd() * 1.2 - 0.1, rnd(), rnd(), if i % 7 == 0 { 0.0 } else { rnd() }]).collect() + } + + #[test] + fn mask_rgba_matches_per_pixel_check() { + let src = Builtin::Srgb.profile(); + // Sizes straddle the parallel threshold and the chunk size, odd ones included. + for (proof, threshold) in [(Builtin::CoatedCmyk, 4.0), (Builtin::CoatedCmyk, 1.5), (Builtin::GrayGamma22, 4.0), (Builtin::DisplayP3, 0.5)] { + let check = GamutCheck::new(src, proof.profile(), threshold).unwrap(); + for n in [0, 1, 1023, 16 * 1024 - 1, 16 * 1024, 16 * 1024 + 1, 50_001] { + let px = image(n); + let want: Vec = px.iter().map(|p| if p[3] > 0.0 && check.out_of_gamut(&p[..3]).unwrap() { 255 } else { 0 }).collect(); + let mut got = vec![7u8; n]; + let count = check.mask_rgba(&px, &mut got).unwrap(); + assert_eq!(got, want, "{proof:?} threshold {threshold} n {n}"); + assert_eq!(count, want.iter().filter(|&&m| m == 255).count()); + } + } + } + + #[test] + fn mask_rgba_rejects_non_rgb_source() { + // A CMYK (4-channel), gray (1-channel) and Lab (3-channel, not RGB) source cannot be tested from RGBA pixels. + for src in [Builtin::CoatedCmyk, Builtin::GrayGamma22, Builtin::LabD50] { + let check = GamutCheck::new(src.profile(), Builtin::Srgb.profile(), 4.0).unwrap(); + let px = image(64); + let mut mask = vec![9u8; 64]; + assert!(matches!(check.mask_rgba(&px, &mut mask), Err(CmsError::InvalidArgument(_))), "{src:?}"); + assert!(mask.iter().all(|&m| m == 9), "mask must stay untouched"); + } + } } diff --git a/crates/cms/src/lutfile.rs b/crates/cms/src/lutfile.rs index c2261325d..404b5a315 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -135,7 +135,8 @@ pub fn parse_as(format: Format, bytes: &[u8]) -> Result { } } -/// UTF-16 with a byte-order mark (Photoshop loads such `.look` files, #9118), else UTF-8. +/// UTF-16 with a byte-order mark (Photoshop loads such `.look` files, #9118), else UTF-8 +/// (a UTF-8 BOM is stripped). fn decode_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> { let utf16 = |rest: &[u8], from: fn([u8; 2]) -> u16| { let units: Vec = rest.as_chunks::<2>().0.iter().map(|&c| from(c)).collect(); @@ -144,6 +145,7 @@ fn decode_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> { match bytes { [0xFF, 0xFE, rest @ ..] => utf16(rest, u16::from_le_bytes), [0xFE, 0xFF, rest @ ..] => utf16(rest, u16::from_be_bytes), + [0xEF, 0xBB, 0xBF, rest @ ..] => String::from_utf8_lossy(rest), _ => String::from_utf8_lossy(bytes), } } @@ -155,14 +157,19 @@ pub fn parse_cube(text: &str) -> Result { let (mut dmin, mut dmax) = ([0.0f32; 3], [1.0f32; 3]); let mut rows: Vec = Vec::new(); for line in text.lines() { - // A '#' inside a quoted TITLE is part of the title, not a comment. - let raw = line.trim_start(); - let skip = if raw.starts_with("TITLE") { raw.find('"').and_then(|q| raw[q + 1..].find('"').map(|e| q + e + 2)) } else { None }.unwrap_or(0); - let line = match raw[skip..].find('#') { - Some(i) => &raw[..skip + i], - None => raw, + // Only a quote at the beginning of a TITLE value protects '#'. Quotes inside an + // unquoted title's comment must not turn that comment into part of the title. + if let Some(quoted) = line + .trim_start() + .strip_prefix("TITLE") + .filter(|rest| rest.starts_with(char::is_whitespace)) + .and_then(|rest| rest.trim_start().strip_prefix('"')) + { + let Some((value, _)) = quoted.split_once('"') else { return err("unterminated quoted TITLE") }; + title = value.to_string(); + continue; } - .trim(); + let line = line.split('#').next().unwrap_or("").trim(); if line.is_empty() { continue; } @@ -187,7 +194,9 @@ pub fn parse_cube(text: &str) -> Result { } k if k.starts_with(|c: char| c.is_ascii_digit() || c == '-' || c == '+' || c == '.') => { let mut v = vec![k.parse::().map_err(|_| LutError(format!("bad number in `{line}`")))?]; - v.extend(it.filter_map(|s| s.parse::().ok())); + for value in it { + v.push(value.parse::().map_err(|_| LutError(format!("bad number in `{line}`")))?); + } if v.len() != 3 { return err(format!("bad data line `{line}`")); } @@ -209,6 +218,15 @@ pub fn parse_cube(text: &str) -> Result { if rows.len() != expected_rows { return err("no LUT_3D_SIZE in .cube file"); } + // #8913: the resample below bakes the curve onto a fixed 33-point grid, so a steep + // curve over a narrow domain cannot survive the resample — the baked table silently + // shifts colours (measured 42 RGB8 levels for a two-node ramp over 0.24..0.26, where + // Photoshop renders the ramp exactly). Rather than proxy the risk by domain width (a + // 0.1..0.17 ramp spans two grid cells yet still loses ~28 levels), measure the + // resample's actual error against the exact curve and refuse only tables whose baked + // output shifts by more than a couple of RGB8 levels. Default-domain tables keep the + // existing bake untouched; gentle curves over any domain bake within tolerance and + // stay accepted. // The 1D table is resampled onto a 0..1 cube anyway, so its input domain // (`DOMAIN_MIN`/`MAX`, `LUT_1D_INPUT_RANGE`) is baked into the resample: an input `v` // reads the table at `(v - min) / (max - min)`, saturating outside. @@ -221,6 +239,36 @@ pub fn parse_cube(text: &str) -> Result { }; let mut l = LutFile::from_fn(&title, 33, |c| [curve(0, c[0]), curve(1, c[1]), curve(2, c[2])])?; l.title = title; + let non_default = dmin.iter().any(|v| *v != 0.0) || dmax.iter().any(|v| *v != 1.0); + if non_default { + // Worst baked-vs-exact error per channel, in RGB8 levels: the baked cube is read + // by linear interpolation between grid points (red fastest, so channel `ch` sits + // at stride 33^ch), while `curve` is what the file actually says. + const MAX_BAKE_ERROR: f32 = 2.0 / 255.0; + for ch in 0..3 { + let stride = [1usize, 33, 33 * 33][ch]; + let mut worst = 0.0f32; + for k in 0..=256 { + let v = k as f32 / 256.0; + let x = v * 32.0; + let i = (x as usize).min(31); + let f = x - i as f32; + let baked = l.data[i * stride * 3 + ch] * (1.0 - f) + l.data[(i + 1).min(32) * stride * 3 + ch] * f; + worst = worst.max((baked - curve(ch, v)).abs()); + } + if worst > MAX_BAKE_ERROR { + return err(format!( + "1D LUT domain on {} is {min}..{max}: the curve changes faster than the 33-point grid can \ + hold (baking shifts colours by up to {levels:.0} RGB8 levels); export the look as a 3D LUT \ + or widen DOMAIN_MIN/DOMAIN_MAX", + ["red", "green", "blue"][ch], + min = dmin[ch], + max = dmax[ch], + levels = worst * 255.0, + )); + } + } + } return l.check(); } err("no LUT_3D_SIZE in .cube file") @@ -554,6 +602,48 @@ pub fn builtin(id: &str) -> Option { mod tests { use super::*; + #[test] + fn cube_title_hashes_and_comment_quotes_are_distinct() { + let rows = "LUT_1D_SIZE 2\n0 0 0 # low\n1 1 1 # high\n"; + for (line, title) in [ + (r#"TITLE "Look #3" # comment "ignored""#, "Look #3"), + (r#"TITLE Look # "draft""#, "Look"), + (" TITLE\t\"Été #2\"", "Été #2"), + (r#"TITLE "" # empty"#, ""), + ] { + assert_eq!(parse_cube(&format!("{line}\n{rows}")).unwrap().title, title); + } + let lut = LutFile::from_fn("Look #3", 2, |c| c).unwrap(); + assert_eq!(parse_cube(&write_cube(&lut)).unwrap().title, "Look #3"); + assert!(parse_cube(&format!("TITLE \"unterminated # title\n{rows}")).is_err()); + } + + #[test] + fn cube_rejects_invalid_numeric_fields() { + let text = "LUT_3D_SIZE 2\n0.25 nope 0.5 0.75\n0 0 1\n0 1 0\n0 1 1\n1 0 0\n1 0 1\n1 1 0\n1 1 1\n"; + assert!(parse_cube(text).is_err(), "invalid numeric field must not be ignored"); + } + + #[test] + fn utf8_bom_is_stripped() { + let l = LutFile::identity(3).unwrap(); + let mut bytes = vec![0xEF, 0xBB, 0xBF]; + bytes.extend_from_slice(write_cube(&l).as_bytes()); + let p = parse("a.cube", &bytes).unwrap(); + assert_eq!(p.size, l.size); + assert_eq!(p.title, l.title); + let titled = LutFile::from_fn("Look", 3, |c| c).unwrap(); + let mut bytes = vec![0xEF, 0xBB, 0xBF]; + bytes.extend_from_slice(write_cube(&titled).as_bytes()); + assert_eq!(parse("a.cube", &bytes).unwrap().title, "Look"); + let mut bytes = vec![0xEF, 0xBB, 0xBF]; + bytes.extend_from_slice(b"LUT_3D_SIZE 2\n"); + for i in 0..8 { + bytes.extend_from_slice(format!("{} {} {}\n", i & 1, (i >> 1) & 1, i >> 2).as_bytes()); + } + assert_eq!(parse("a.cube", &bytes).unwrap().size, 2); + } + /// An identity `.3dl` of edge `n` (12-bit nodes, blue fastest) under `shaper`. fn identity_3dl(shaper: &[u32], n: usize) -> String { let mut t = shaper.iter().map(u32::to_string).collect::>().join(" ") + "\n"; @@ -811,6 +901,49 @@ DOMAIN_MAX 0.5 1 1 assert!(e.0.contains("below DOMAIN_MAX"), "{e}"); } + #[test] + fn cube_1d_narrow_domain_is_refused() { + // #8913: the issue's fixture — a two-node ramp over 0.24..0.26. Baking it onto the + // 33-point grid rendered grey-65 as 148 where Photoshop renders 190 (42-level error). + // It must now be an actionable parse error rather than a silently wrong table. + let e = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0.24 0.24 0.24\nDOMAIN_MAX 0.26 0.26 0.26\n0 0 0\n1 1 1\n").unwrap_err(); + assert!(e.0.contains("changes faster than the 33-point grid"), "{e}"); + // The same domain via LUT_1D_INPUT_RANGE is refused identically. + let e = parse_cube("LUT_1D_SIZE 2\nLUT_1D_INPUT_RANGE 0.24 0.26\n0 0 0\n1 1 1\n").unwrap_err(); + assert!(e.0.contains("changes faster"), "{e}"); + // A per-channel narrow domain names the offending channel. + let e = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0 0 0.24\nDOMAIN_MAX 1 1 0.26\n0 0 0\n1 1 1\n").unwrap_err(); + assert!(e.0.contains("blue"), "{e}"); + } + + #[test] + fn cube_1d_wide_domain_still_bakes() { + // A steep ramp over 0.1..0.17 spans two grid cells but still loses ~28 RGB8 levels + // to the resample, so the measured-error check refuses it even though the old width + // check let it through. + let e = parse_cube("LUT_1D_SIZE 2\nLUT_1D_INPUT_RANGE 0.1 0.17\n0 0 0\n1 1 1\n").unwrap_err(); + assert!(e.0.contains("changes faster"), "steep ramp over 0.1..0.17 refused: {e}"); + // A gentle (here linear) curve over a grid-aligned domain bakes exactly and stays + // accepted — the check measures the bake's error, not the domain's width. Value + // assertions, not just a successful parse: the baked table must reproduce the curve. + // Table row k sits at t = k/32 of the domain 0.25..0.75; the curve y = 0.4 + 0.4t + // is gentle (a half-level per grid step), so the bake must reproduce it exactly. + let mut table = String::from("LUT_1D_SIZE 33\nLUT_1D_INPUT_RANGE 0.25 0.75\n"); + for k in 0..33 { + let y = 0.4 + 0.4 * (k as f32 / 32.0); + table.push_str(&format!("{y} {y} {y}\n")); + } + let l = parse_cube(&table).expect("gentle curve over a non-default domain still bakes"); + let at = |r: usize| l.data[r * 3]; + // Grid entry r is input r/32: 0.5 is the middle of the domain (t=0.5) -> 0.6. + assert!((at(16) - 0.6).abs() < 1.0 / 255.0, "baked value at 0.5 tracks the curve: {}", at(16)); + // Domain top (input 0.75 = grid 24, t=1) -> 0.8. + assert!((at(24) - 0.8).abs() < 1.0 / 255.0, "baked value at the domain top tracks the curve: {}", at(24)); + // Below the domain saturates to the first row. + assert!((at(8) - 0.4).abs() < 1.0 / 255.0, "below the domain saturates: {}", at(8)); + parse_cube("LUT_1D_SIZE 2\n0 0 0\n1 1 1\n").expect("default domain"); + } + #[test] fn cube_1d_domain_is_baked_into_the_resample() { // An identity ramp over the domain 0.25..0.75: 0.25 maps to 0, 0.5 to 0.5, 0.75 to 1. @@ -1042,11 +1175,14 @@ DOMAIN_MAX 0.5 1 1 } #[test] - fn look_hex_floats() { - let id = LutFile::identity(2).unwrap(); - let hex: String = id.data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect(); + fn look_data_is_little_endian_float32() { + // Bytes written out by hand rather than by the encoder the parser mirrors, so a wrong byte + // order fails: 0.25, 0.5, 1.0 (3E800000, 3F000000, 3F800000 stored low byte first), then zeros. + let hex = format!("0000803E 0000003F 0000803F {}", "00000000 ".repeat(21)); let xml = format!("\"2\"\"{hex}\""); - assert_eq!(parse("a.look", xml.as_bytes()).unwrap().data, id.data); + let data = parse("a.look", xml.as_bytes()).unwrap().data; + assert_eq!(data[..3], [0.25, 0.5, 1.0]); + assert!(data[3..].iter().all(|&v| v == 0.0)); } #[test] From ce4f4816a174929f96828e1ee2addc0102e3544b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:19:30 -0500 Subject: [PATCH 040/120] text: a work limit on Knuth-Plass; past it break_paragraph falls back to greedy (#868) --- crates/text/src/linebreak/knuth_plass.rs | 14 +++++++++++++- crates/text/src/linebreak/mod.rs | 11 +++++++++-- crates/text/src/linebreak/tests.rs | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/crates/text/src/linebreak/knuth_plass.rs b/crates/text/src/linebreak/knuth_plass.rs index 742c3245f..e9de0b485 100644 --- a/crates/text/src/linebreak/knuth_plass.rs +++ b/crates/text/src/linebreak/knuth_plass.rs @@ -4,6 +4,11 @@ use super::{Item, Kind, Line, Params, Sums, clamp_measure, fitness_class}; /// Give up (return `None`) beyond this many active breaks. const MAX_ACTIVE: usize = 4096; +/// Give up (return `None`) after this much work, counted as one for each break considered and one +/// for each active break it is tried against. A paragraph of 400,000 items with 30 active breaks +/// takes some 12,000,000 on a pass; the limit is where a pass runs for seconds, and past it the +/// caller falls back to [`super::greedy`], which keeps every item. +pub(super) const MAX_WORK: u64 = 1 << 27; /// Ratios beyond this are rated as this (an unstretchable line is infinitely loose). const MAX_RATIO: f64 = 10_000.0; /// Demerits of an overfull line accepted because nothing else reaches a break. @@ -37,8 +42,14 @@ struct Candidate { /// One total-fit pass over `items`. `width(k)` is the measure of line `k`; set /// [`Params::uniform_from`] when it varies. Returns `None` when no set of breaks keeps every line -/// within [`Params::tolerance`] (unless [`Params::overfull`]), or past the active-break limit. +/// within [`Params::tolerance`] (unless [`Params::overfull`]), or past the active-break or work limit. pub fn knuth_plass(items: &[Item], width: &dyn Fn(usize) -> f64, p: &Params) -> Option> { + knuth_plass_within(items, width, p, &mut MAX_WORK.clone()) +} + +/// [`knuth_plass`] spending at most `work` (left over on return), so that the passes of one +/// paragraph share a budget. +pub(super) fn knuth_plass_within(items: &[Item], width: &dyn Fn(usize) -> f64, p: &Params, work: &mut u64) -> Option> { let p = &p.sanitized(); let width = |k: usize| clamp_measure(width(k)); let s = Sums::new(items); @@ -57,6 +68,7 @@ pub fn knuth_plass(items: &[Item], width: &dyn Fn(usize) -> f64, p: &Params) -> if !s.is_break(b) { continue; } + *work = work.checked_sub(active.len() as u64 + 1)?; let item = items.get(b); let forced = s.forced(b); let flagged = item.is_some_and(Item::is_flagged); diff --git a/crates/text/src/linebreak/mod.rs b/crates/text/src/linebreak/mod.rs index 9d20b0645..dfdfd9cf3 100644 --- a/crates/text/src/linebreak/mod.rs +++ b/crates/text/src/linebreak/mod.rs @@ -29,6 +29,7 @@ use std::ops::Range; pub use greedy::{greedy, greedy_with}; pub use knuth_plass::knuth_plass; +use knuth_plass::{MAX_WORK, knuth_plass_within}; /// Number of elasticity tiers: word space, letter space, glyph scale. pub const TIERS: usize = 3; @@ -345,11 +346,17 @@ pub struct Breaks { /// [`knuth_plass`] at each of `tolerances` in turn, the last pass also accepting overfull lines; /// [`greedy`] if every pass fails or `tolerances` is empty. The last pass fails only past the -/// active-break limit (see [`Params::uniform_from`]). +/// active-break limit (see [`Params::uniform_from`]) or the passes' shared work limit, which +/// only a paragraph of many hundreds of thousands of items reaches. Nothing is left out then: the +/// greedy breaker lays out every item, in linear time. pub fn break_paragraph(items: &[Item], width: &dyn Fn(usize) -> f64, params: &Params, tolerances: &[f64]) -> Breaks { + break_paragraph_within(items, width, params, tolerances, MAX_WORK) +} + +fn break_paragraph_within(items: &[Item], width: &dyn Fn(usize) -> f64, params: &Params, tolerances: &[f64], mut work: u64) -> Breaks { for (pass, &tolerance) in tolerances.iter().enumerate() { let overfull = params.overfull || pass + 1 == tolerances.len(); - if let Some(lines) = knuth_plass(items, width, &Params { tolerance, overfull, ..*params }) { + if let Some(lines) = knuth_plass_within(items, width, &Params { tolerance, overfull, ..*params }, &mut work) { return Breaks { lines, method: Method::KnuthPlass { pass }, balanced: false }; } } diff --git a/crates/text/src/linebreak/tests.rs b/crates/text/src/linebreak/tests.rs index d8163edde..58483d2aa 100644 --- a/crates/text/src/linebreak/tests.rs +++ b/crates/text/src/linebreak/tests.rs @@ -492,3 +492,22 @@ fn next_start_is_the_first_box_or_forced_break_after_each_item() { assert_eq!(s.next_start(b), scan, "after {b}"); } } + +/// Past its work limit the paragraph is broken greedily, with every item laid out and no error or +/// cut: a limit of a few units stands for the real one, which a hostile paragraph of hundreds of +/// thousands of items would reach (and the test never builds). +#[test] +fn a_paragraph_past_the_work_limit_falls_back_to_greedy_with_every_item() { + let p = vectorcraft(false); + let para = Para::new(COPY, &p, Some(50.0)); + let w = |_| 150.0; + let within = break_paragraph_within(¶.items, &w, &p, &PASSES, MAX_WORK); + assert!(matches!(within.method, Method::KnuthPlass { .. }), "{:?}", within.method); + let limited = break_paragraph_within(¶.items, &w, &p, &PASSES, 20); + assert_eq!(limited.method, Method::Greedy); + assert_eq!(limited.lines, greedy(¶.items, &w, &p)); + assert_covers(¶.items, &limited.lines); + // The work is shared by the passes: what one spends the next doesn't have. + let mut work = 30; + assert!(knuth_plass_within(¶.items, &w, &Params { tolerance: 1.0, ..p }, &mut work).is_none()); +} From 4559fe417130a09b11807311a7af9d1f0abfeb6b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:19:57 -0500 Subject: [PATCH 041/120] print: run lpoptions with a timeout and capped output (#871) --- crates/print/src/lib.rs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/crates/print/src/lib.rs b/crates/print/src/lib.rs index 01c057269..a4445b11d 100644 --- a/crates/print/src/lib.rs +++ b/crates/print/src/lib.rs @@ -377,11 +377,7 @@ pub fn printer_options(printer: &str) -> Vec { f.take(PPD_CAP).read_to_end(&mut bytes).ok().map(|_| bytes) }); let mut options = ppd.map(|b| parse_ppd(&ppd_text(&b))).unwrap_or_default(); - let current = std::process::Command::new("lpoptions") - .args(["-p", printer, "-l"]) - .output() - .map(|o| parse_lpoptions(&String::from_utf8_lossy(&o.stdout))) - .unwrap_or_default(); + let current = queue_defaults(std::process::Command::new("lpoptions").args(["-p", printer, "-l"]), QUEUES_LIMIT); apply_queue_defaults(&mut options, ¤t); options } @@ -516,6 +512,13 @@ fn printers_via( printers_parsed(&queues, available.as_deref()) } +/// The queue's current choices from `lpoptions -p NAME -l`, run like the `lpstat` commands: its +/// output capped, and given `limit` to answer (none when it doesn't). +#[cfg(all(unix, not(target_arch = "wasm32")))] +fn queue_defaults(command: &mut std::process::Command, limit: std::time::Duration) -> Vec<(String, String)> { + output_within(command, limit).map(|out| parse_lpoptions(&String::from_utf8_lossy(&out))).unwrap_or_default() +} + /// The most of a command's output that is read: far more than any `lpstat` listing. #[cfg(all(unix, not(target_arch = "wasm32")))] const OUTPUT_CAP: u64 = 4 << 20; @@ -612,6 +615,20 @@ mod lpstat_limits { assert!(started.elapsed() < Duration::from_secs(5), "{:?}", started.elapsed()); } + /// `lpoptions` that never answers or prints without end gives no defaults, in time and in bounded memory. + #[test] + fn lpoptions_is_bounded_in_time_and_output() { + let started = Instant::now(); + assert!(queue_defaults(&mut sh("sleep 30"), Duration::from_millis(300)).is_empty()); + assert!(started.elapsed() < Duration::from_secs(5), "{:?}", started.elapsed()); + let answer = queue_defaults(&mut sh("printf 'Duplex/2-Sided: None *DuplexNoTumble\\n'"), Duration::from_secs(5)); + assert_eq!(answer, [("Duplex".to_string(), "DuplexNoTumble".to_string())]); + // Endless output stops at the cap (the command is killed at the deadline), and does not hang. + let started = Instant::now(); + let _ = queue_defaults(&mut sh("yes 'Key/Label: a *b c'"), Duration::from_millis(500)); + assert!(started.elapsed() < Duration::from_secs(5), "{:?}", started.elapsed()); + } + /// A command that isn't there is no printers, at once. #[test] fn no_lpstat_is_no_printers() { From 23a63f5c769fe51f2e5f6c3d217cfbfd5c8ea713 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:20:26 -0500 Subject: [PATCH 042/120] font-catalog: cap the font file read where the file has grown since its size was checked (#873) --- crates/font-catalog/src/catalog.rs | 15 ++++++++++++++- crates/font-catalog/src/tests.rs | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/font-catalog/src/catalog.rs b/crates/font-catalog/src/catalog.rs index c20753fb9..10663e6af 100644 --- a/crates/font-catalog/src/catalog.rs +++ b/crates/font-catalog/src/catalog.rs @@ -22,6 +22,19 @@ const MAX_SCAN_READ: u64 = 256 << 20; /// The largest font file loaded. const MAX_LOAD: u64 = 512 << 20; +/// The bytes of the file at `path`, at most `max`: the size was checked before, but a file can +/// grow between that check and the read, so the read itself stops one byte past the limit. +pub(crate) fn read_capped(path: &Path, max: u64) -> Result, FontFileError> { + use std::io::Read as _; + let mut bytes = Vec::new(); + std::fs::File::open(path)?.take(max.saturating_add(1)).read_to_end(&mut bytes)?; + let size = bytes.len() as u64; + if size > max { + return Err(FontFileError::TooLarge { size, max }); + } + Ok(bytes) +} + /// Why a font file or font data wasn't added or loaded. #[derive(Debug, thiserror::Error)] pub enum FontFileError { @@ -544,7 +557,7 @@ impl Catalog { if size > MAX_LOAD { return Err(FontFileError::TooLarge { size, max: MAX_LOAD }); } - let bytes: Arc<[u8]> = std::fs::read(path)?.into(); + let bytes: Arc<[u8]> = read_capped(path, MAX_LOAD)?.into(); let mut files = lock(&self.files); files.retain(|_, w| w.strong_count() > 0); files.insert(path.to_path_buf(), Arc::downgrade(&bytes)); diff --git a/crates/font-catalog/src/tests.rs b/crates/font-catalog/src/tests.rs index c061ca2aa..5137a4295 100644 --- a/crates/font-catalog/src/tests.rs +++ b/crates/font-catalog/src/tests.rs @@ -556,3 +556,15 @@ fn repeated_name_records_are_bounded_before_pairing() { assert!(!face.keys.legacy.is_empty()); assert_eq!(face.keys.legacy.len(), 256, "the pair budget counts distinct pairs"); } + +/// A read stops at its cap, whatever the file has grown to since its size was checked. +#[test] +fn a_read_is_capped_where_the_file_has_grown() { + use crate::catalog::read_capped; + let path = std::env::temp_dir().join(format!("craft-font-catalog-cap-{}", std::process::id())); + std::fs::write(&path, b"0123456789").unwrap(); + assert_eq!(read_capped(&path, 10).unwrap(), b"0123456789"); + assert!(matches!(read_capped(&path, 9), Err(FontFileError::TooLarge { size: 10, max: 9 }))); + assert!(matches!(read_capped(&path.with_extension("missing"), 9), Err(FontFileError::Io(_)))); + let _ = std::fs::remove_file(&path); +} From bafefe627ae457b71abbec23bdd8a9119a669188 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:20:53 -0500 Subject: [PATCH 043/120] hayro-jbig2: PdfCraft's JBIG2 follow-ups (refinement fixes, file header, work accounting, dictionary pixel cap) (#886) * hayro-jbig2: bound a stream's total decode work and what a page retains; no i32 overflow Port of pdfcraft 85c10051 (patches 5-9), 82439525 (patch 10) and 10107d94 (patch 12). * hayro-jbig2: PdfCraft's JBIG2 follow-ups (refinement fixes, file header, work accounting, dictionary pixel cap) Ports pdfcraft 72e763ad, b7ec3669, e3bc4e85, 01d3479f, b9e068aa, 858ce960, 07f44038, e38b9d18, 8717a085, 58c30af3 and the hayro-jbig2 part of e2464e14 (with the hayro-jbig2 part of 0ed33990 they build on). * vendor/README.craft-reader.md: one #831 paragraph, the follow-ups after it --- vendor/README.craft-reader.md | 23 + vendor/hayro-jbig2/SOURCE.json | 16 +- vendor/hayro-jbig2/src/bitmap.rs | 147 +++++-- .../src/decode/generic_refinement.rs | 85 ++-- vendor/hayro-jbig2/src/decode/symbol.rs | 38 +- vendor/hayro-jbig2/src/decode/text.rs | 20 +- vendor/hayro-jbig2/src/error.rs | 58 +++ vendor/hayro-jbig2/src/file.rs | 21 +- vendor/hayro-jbig2/src/gray_scale.rs | 35 +- vendor/hayro-jbig2/src/lib.rs | 414 +++++++++++++++--- vendor/hayro-jbig2/src/pdfcraft_tests.rs | 279 ++++++++++-- 11 files changed, 943 insertions(+), 193 deletions(-) diff --git a/vendor/README.craft-reader.md b/vendor/README.craft-reader.md index f9f281ab9..b8271db10 100644 --- a/vendor/README.craft-reader.md +++ b/vendor/README.craft-reader.md @@ -401,3 +401,26 @@ of 85c10051 for hayro-syntax was already carried here. PdfCraft's raster tests a `jbig2_placements_at_the_end_of_an_i32_draw_nothing`); the unit tests in `lib.rs`, `bitmap.rs` and `decode/symbol.rs` come with the patches. PdfCraft's later JBIG2 follow-ups (b7ec3669, 72e763ad, e3bc4e85, patch 13 and later) are not part of this port. + +JBIG2 follow-ups, ported from PdfCraft after the decode-bound patches above, each marked +`PdfCraft patch` in `hayro-jbig2`: [72e763ad](https://github.com/storytold/pdfcraft/commit/72e763ad1) +(a symbol with no rows costs a step; a refined instance is charged first), +[b7ec3669](https://github.com/storytold/pdfcraft/commit/b7ec3669c) (what an item reads is charged), +[e3bc4e85](https://github.com/storytold/pdfcraft/commit/e3bc4e855) (a height class of no symbols +decodes no bitmap), [0ed33990](https://github.com/storytold/pdfcraft/commit/0ed339901) (only its +`hayro-jbig2` part: `Image::decode_within` takes the work left, so callers can share one allowance), +[01d3479f](https://github.com/storytold/pdfcraft/commit/01d3479f2), [b9e068aa](https://github.com/storytold/pdfcraft/commit/b9e068aa3), +[858ce960](https://github.com/storytold/pdfcraft/commit/858ce9608), +[07f44038](https://github.com/storytold/pdfcraft/commit/07f440388), +[e38b9d18](https://github.com/storytold/pdfcraft/commit/e38b9d182) and +[8717a085](https://github.com/storytold/pdfcraft/commit/8717a085f) (a region past `i32::MAX` is off the page; a +refinement refines the bitmap it is placed on, the page only in its area, and reads 0 past its +reference's width and a reference right of it from its first column), +[58c30af3](https://github.com/storytold/pdfcraft/commit/58c30af31) (a stream that starts with a JBIG2 +file header draws) and the `hayro-jbig2` part of +[e2464e14](https://github.com/storytold/pdfcraft/commit/e2464e144) (a dictionary may hold 2^28 symbol +pixels, as a real 14,642 x 8,879 scan needs; `OverflowError::BitmapLimit` and `DecodeError::is_limit` tell +a decoder's limit from damage). Not ported from those commits: the page-wide allowance and the +refused-mask handling in `hayro-syntax` and `hayro-interpret` (0ed33990, 4aa732ea, e2464e14), which rest on +the decoded-image cache of 558ffd7f (a performance change). Tests: those that came with the patches in +`hayro-jbig2`, and PdfCraft's raster tests adapted in `hayro-jbig2/src/pdfcraft_tests.rs`. diff --git a/vendor/hayro-jbig2/SOURCE.json b/vendor/hayro-jbig2/SOURCE.json index 5efee12a3..0568ac878 100644 --- a/vendor/hayro-jbig2/SOURCE.json +++ b/vendor/hayro-jbig2/SOURCE.json @@ -62,7 +62,7 @@ "path": "src/bitmap.rs", "source_sha256": "59ed83d8bac6c39e40270a9856ca3faee628de7d9efae1b01f714e140e2b6b7e", "source_git_blob": "35ed97561597d30a70fe819cf8cfbd3780538f75", - "sha256": "dd330bda095350b91eeb15b029bdae209e1ed3d5c711dff279b79abd7832f847" + "sha256": "f5fa055ed19f6113d517875479ed4d4d732e06a0010067883a4c38820d55ff97" }, { "path": "src/decode/generic.rs", @@ -74,7 +74,7 @@ "path": "src/decode/generic_refinement.rs", "source_sha256": "625075671e510e8ff1d7c3cade857792413c091d57a35561ad0591d7f04c2ea5", "source_git_blob": "a566b8de58c078dec874d5d1dea36c0f557f1a16", - "sha256": "93e2ce1a40c35fed24d4d611cfc1763518e00167b4e3c0a0671a18afa37c7064" + "sha256": "7a4e36aa8b371f532e33163aad59133f8afa946935570b2312d057cd2a8b3837" }, { "path": "src/decode/halftone.rs", @@ -98,31 +98,31 @@ "path": "src/decode/symbol.rs", "source_sha256": "aa57e88cdc6fd1647d7996875c253a643fedfc4438d5137e326e00d17d0d29ee", "source_git_blob": "f1aa8a6639e53dd350057d8d078198d363daa918", - "sha256": "627de1c83267597efef6d6c0d49aec004f43fd6e63490f117f744ed9210b47f0" + "sha256": "793e9943183b5ef119aa281dc13e782083fdac16e9b4c598083af1a95209256f" }, { "path": "src/decode/text.rs", "source_sha256": "01863cdcd48ab68b5230948d12212a815d293c7c9d5382a140772088bfb1c405", "source_git_blob": "bbc2ac55c7d7d5b8aebffbecfe93fda8b88526e8", - "sha256": "32ae695fe181ba07181fa9915b6bce2ca333031c392fae66dd15ce955a8d7b95" + "sha256": "1f75a55d76111980d667713f841aa38a1fc20918e5bdcfef08a3a36842e2ca10" }, { "path": "src/error.rs", "source_sha256": "39b6ec8fa058bc5c1722892290882e123d193d3844ed5846b30b742b94ba72b0", "source_git_blob": "c579368b44b444566178e68a2705cf9561fad71f", - "sha256": "326dd926cc52b191d0c0398b3b2bb482ca267919e369045f129930cfa485e7fb" + "sha256": "7bc0f28cf64b0510819a50037566b6eeeba1cf2654d095417743cf0b00e2eae8" }, { "path": "src/file.rs", "source_sha256": "10467a415ab2e8b92a8951a86f4c02c5c74f5a005629a11865a20bc23b26ca55", "source_git_blob": "1440a22efa89f0deec0029968c3a20bad8526d63", - "sha256": "c96741babbfacfd78bd2b63f0f16541aaa235f14bb4344ecd4707e3ca5942df1" + "sha256": "7b85b531dbb37ddad69d620e54934a3f88cb88a5aaf1dcd4209c990b92fdbd69" }, { "path": "src/gray_scale.rs", "source_sha256": "46f12893f4573610789015ba7036f6afb1d30b1454c0c674fc7ccdd9337d7487", "source_git_blob": "2836ec77a7c8d3970b13dd46f576778c64bfe2d9", - "sha256": "4bb0963338bfc9e3773db822465d5acbc1267c3349f04b30bff8c52e254874bb" + "sha256": "f82a6760de85bab325cffa6c75a0fa4e1d55fb9c67617be0d0fa112f2dd6c15f" }, { "path": "src/huffman_table.rs", @@ -158,7 +158,7 @@ "path": "src/lib.rs", "source_sha256": "22463230cc82543ee93791b642566a308f0f4eb14d3089b24dc2e7f06d0f784c", "source_git_blob": "ad77a6dd17920b2db66f6bb5a0f2cb8efaafd41a", - "sha256": "d0431d1c57c552b250b7b48895f0d768bfc75496b30cd0146b77764b09759c0f" + "sha256": "ac97e81ca5ec61d5d40247b561d0c1b17d7ac80aa173ee1bde099eb90e52a82f" }, { "path": "src/page_info.rs", diff --git a/vendor/hayro-jbig2/src/bitmap.rs b/vendor/hayro-jbig2/src/bitmap.rs index a240ca432..a40083145 100644 --- a/vendor/hayro-jbig2/src/bitmap.rs +++ b/vendor/hayro-jbig2/src/bitmap.rs @@ -7,6 +7,7 @@ use crate::decode::CombinationOperator; use crate::error::{OverflowError, Result, bail}; +use alloc::vec; use alloc::vec::Vec; // To guard better against malicious files, we limit bitmap dimensions @@ -30,6 +31,12 @@ pub(crate) const MAX_GRID_CELLS: u64 = 1 << 25; /// halftone 265 s, and a 923-byte PDF's regions would have taken days. pub(crate) const MAX_WORK: u64 = 4 * MAX_PIXELS; +/// PdfCraft patch: the work of reading one integer of an item, what an arithmetic-coded one takes +/// at worst in decisions (see [`MAX_WORK`]), so that a decision costs no more than a pixel. An item +/// with no pixels still reads its integers: charged a step each, a 22,515-byte PDF of refined text +/// instances of no size stayed inside `MAX_WORK` and took 730 s. +pub(crate) const INTEGER_WORK: u64 = 38; + /// PdfCraft patch: the work of a line of a Huffman table built for symbols, about 50 ns (see /// [`MAX_WORK`]). pub(crate) const HUFFMAN_LINE_WORK: u64 = 10; @@ -63,10 +70,10 @@ pub(crate) const BITMAP_WORDS: usize = size_of::() / size_of::(); /// output from the page header in between, and a 743-byte PDF's Indexed image asked for 137 GB. pub(crate) fn check_page_size(width: u32, height: u32) -> Result<()> { if width > MAX_DIMENSION || height > MAX_DIMENSION { - return Err(OverflowError::BitmapDimension.into()); + return Err(OverflowError::BitmapLimit.into()); } if u64::from(width) * u64::from(height) > MAX_PIXELS { - return Err(OverflowError::BitmapDimension.into()); + return Err(OverflowError::BitmapLimit.into()); } Ok(()) } @@ -86,6 +93,11 @@ pub(crate) struct Bitmap { pub(crate) stride: u32, /// Packed pixel data, one bit per pixel, row-major order. /// Each row is padded to a word boundary. + /// + /// PdfCraft patch: the padding is 0s, as pixels past the edge are (6.2.5.2, 6.3.5.2). Generic + /// and refinement decoding read the rows above their own pixels a word at a time, padding + /// included, and a refinement reads its reference the same way: whatever writes whole words + /// keeps the padding 0 (see `clear_padding`). pub(crate) data: Vec, /// "This four-byte field gives the horizontal offset in pixels of the bitmap /// encoded in this segment relative to the page bitmap." (7.4.1.3) @@ -95,14 +107,6 @@ pub(crate) struct Bitmap { pub(crate) y_location: u32, } -/// PdfCraft patch: fallible replacement for `vec![word; len]` / `resize`. -fn fill_words(data: &mut Vec, len: usize, word: Word) -> Result<()> { - data.try_reserve_exact(len) - .map_err(|_| OverflowError::BitmapDimension)?; - data.resize(len, word); - Ok(()) -} - impl Bitmap { /// PdfCraft patch: the words this bitmap holds, its header included. pub(crate) fn words(&self) -> usize { @@ -125,26 +129,47 @@ impl Bitmap { default_pixel: bool, ) -> Result { if width > MAX_DIMENSION || height > MAX_DIMENSION { - bail!(OverflowError::BitmapDimension); + bail!(OverflowError::BitmapLimit); } // PdfCraft patch: see `MAX_PIXELS`. if u64::from(width) * u64::from(height) > MAX_PIXELS { - bail!(OverflowError::BitmapDimension); + bail!(OverflowError::BitmapLimit); } let stride = width.div_ceil(WORD_BITS); let default_word: Word = if default_pixel { !0 } else { 0 }; - // PdfCraft patch: reserve fallibly so hostile dimensions error instead of aborting. - let mut data = Vec::new(); - fill_words(&mut data, stride as usize * height as usize, default_word)?; - Ok(Self { + let data = vec![default_word; stride as usize * height as usize]; + let mut bitmap = Self { width, height, stride, data, x_location, y_location, - }) + }; + if default_pixel { + bitmap.clear_padding(); + } + Ok(bitmap) + } + + /// PdfCraft patch: clear the bits past `width` in each row's last word. "All pixels lying + /// outside the bounds of the actual bitmap or the reference bitmap have the value 0" (6.3.5.2), + /// and a refinement reads its reference a word at a time, those bits included: filled whole + /// with a default pixel of 1, they read as 1s, so a refinement of the page, or of an + /// intermediate text or halftone region, decoded to other pixels than were coded. + fn clear_padding(&mut self) { + let trailing = self.width % WORD_BITS; + // A width of 0 has no words to clear, and `chunks_exact_mut` would take a stride of 0. + if trailing == 0 { + return; + } + let mask: Word = !0 << (WORD_BITS - trailing); + for row in self.data.chunks_exact_mut(self.stride as usize) { + if let Some(last) = row.last_mut() { + *last &= mask; + } + } } pub(crate) fn reinitialize( @@ -157,15 +182,17 @@ impl Bitmap { let stride = width.div_ceil(WORD_BITS); let default_word: Word = if default_pixel { !0 } else { 0 }; - // Allocate first so a failed reservation leaves the bitmap untouched. - let mut data = Vec::new(); - fill_words(&mut data, stride as usize * height as usize, default_word)?; self.width = width; self.height = height; self.stride = stride; self.x_location = 0; self.y_location = 0; - self.data = data; + self.data.clear(); + self.data + .resize(stride as usize * height as usize, default_word); + if default_pixel { + self.clear_padding(); + } Ok(()) } @@ -194,6 +221,17 @@ impl Bitmap { self.data[word_idx] |= (value as Word) << bit_pos; } + /// PdfCraft patch: combine a decoded region into this page at the region's own location. + /// + /// "Unless otherwise specified, all multi-bit values shall be treated as unsigned values" + /// (5.4.4), and a region's location (7.4.1.3, 7.4.1.4) is not specified otherwise. One past + /// `i32::MAX` is past any page. Cast to a signed coordinate it read as left of the page or above + /// it, and one within a region's size of 2^32 came back onto it. + pub(crate) fn combine_region(&mut self, region: &Self, operator: CombinationOperator) { + let signed = |location: u32| i32::try_from(location).unwrap_or(i32::MAX); + self.combine(region, signed(region.x_location), signed(region.y_location), operator); + } + /// Combine another bitmap into this one at a specific location. /// /// "These operators describe how the segment's bitmap is to be combined with @@ -280,15 +318,74 @@ impl Bitmap { self.data[(row * self.stride + word_idx) as usize] } + + /// PdfCraft patch: the `width` × `height` area of this bitmap at `x`, `y`, as a bitmap there. + /// It is what a refinement of the page refines: "a bitmap containing the current contents of + /// the page buffer, restricted to the area of the page buffer specified by this segment's + /// region segment information field" (7.4.7.4). Off this bitmap the area is 0, past its width + /// too whatever the padding of its rows holds, and outside the area the refinement reads 0 + /// (6.3.5.2), where reading the page itself took in the pixels around the area. + pub(crate) fn area(&self, x: u32, y: u32, width: u32, height: u32) -> Result { + let mut area = Self::new_with(width, height, x, y, false)?; + for row in 0..height { + let Some(from_row) = y.checked_add(row).filter(|r| *r < self.height) else { + continue; + }; + for word in 0..area.stride { + // The first column of this word of the area, in this bitmap; past its width, + // or past where a column can be, the rest of the row is 0. + let Some(from) = x.checked_add(word * WORD_BITS).filter(|c| *c < self.width) else { + break; + }; + let (index, shift) = (from / WORD_BITS, from % WORD_BITS); + let mut bits = self.get_word(from_row, index) << shift; + if shift > 0 { + bits |= self.get_word(from_row, index + 1) >> (WORD_BITS - shift); + } + // Both are above 0: `from` is inside this bitmap, and `word` inside the area. + let ours = self.width - from; + let theirs = width - word * WORD_BITS; + let kept = ours.min(theirs); + if kept < WORD_BITS { + bits &= !0 << (WORD_BITS - kept); + } + if let Some(slot) = area.data.get_mut((row * area.stride + word) as usize) { + *slot = bits; + } + } + } + Ok(area) + } } #[cfg(test)] -mod tests { +mod pdfcraft_limit_tests { use super::*; + use crate::error::DecodeError; + /// PdfCraft patch: a bitmap past the bound on a side or on pixels is refused by a limit. #[test] - fn failed_reservation_is_an_error() { - let mut data = Vec::new(); - assert!(fill_words(&mut data, usize::MAX / 2, 0).is_err()); + fn a_bitmap_past_its_bounds_is_refused_by_a_limit() { + let limit = Err(DecodeError::Overflow(OverflowError::BitmapLimit)); + assert_eq!( + Bitmap::new(MAX_DIMENSION + 1, 1).map(|_| ()), + limit, + "a side" + ); + assert_eq!( + Bitmap::new(1, MAX_DIMENSION + 1).map(|_| ()), + limit, + "the other" + ); + // 2^28 + 2^14 pixels, each side within its bound. + assert_eq!( + Bitmap::new(1 << 14, (1 << 14) + 1).map(|_| ()), + limit, + "the pixels" + ); + assert!( + Bitmap::new(1 << 14, 1 << 14).is_ok(), + "2^28 pixels are within it" + ); } } diff --git a/vendor/hayro-jbig2/src/decode/generic_refinement.rs b/vendor/hayro-jbig2/src/decode/generic_refinement.rs index a6634439d..cd730fd93 100644 --- a/vendor/hayro-jbig2/src/decode/generic_refinement.rs +++ b/vendor/hayro-jbig2/src/decode/generic_refinement.rs @@ -10,7 +10,7 @@ use crate::ScratchBuffers; use crate::arithmetic_decoder::{ArithmeticDecoder, ArithmeticDecoderContext}; use crate::bitmap::{Bitmap, WORD_BITS, WORD_SHIFT, Word}; use crate::decode::generic::ContextGatherer; -use crate::error::{OverflowError, ParseError, RegionError, Result, bail}; +use crate::error::{ParseError, RegionError, Result, bail}; use crate::reader::Reader; /// Generic refinement region decoding procedure (6.3). @@ -46,27 +46,18 @@ pub(crate) fn decode_into( // Validate that the region fits within the reference bitmap. // When referring to another segment, dimensions must match exactly (7.4.7.5). - // When using the page bitmap as reference, the region must fit within the page. + // PdfCraft patch: a refinement of the page refines the page's area of its own size (7.4.7.4, + // `Bitmap::area`), which it always fits. if header.region_info.width > reference.width || header.region_info.height > reference.height { bail!(RegionError::InvalidDimension); } - let reference_dx = i32::try_from(reference.x_location) - .ok() - .and_then(|r: i32| { - i32::try_from(header.region_info.x_location) - .ok() - .and_then(|h| r.checked_sub(h)) - }) - .ok_or(OverflowError::ReferenceOffset)?; - let reference_dy = i32::try_from(reference.y_location) - .ok() - .and_then(|r: i32| { - i32::try_from(header.region_info.y_location) - .ok() - .and_then(|h| r.checked_sub(h)) - }) - .ok_or(OverflowError::ReferenceOffset)?; + // PdfCraft patch: "GRREFERENCEDX 0, GRREFERENCEDY 0" (Table 35). The reference is another + // region, which a refinement refines where it is (their locations should be equal, 7.4.7.5), + // or the page restricted to the refinement's area (7.4.7.4, `Bitmap::area`). Offsetting it by + // the difference of the locations refused one past `i32::MAX` as an overflow, and read the + // page around a refinement of it. + let (reference_dx, reference_dy) = (0, 0); let mut decoder = ArithmeticDecoder::new(data); let num_context_bits = header.template.context_bits(); @@ -194,10 +185,24 @@ impl<'a> RefinementContextGatherer<'a> { }; self.reg_cur = 0; - self.ref_cur_x = 0; - self.ref_m1 = ContextGatherer::load_word(reference, ref_y.wrapping_sub(1) as u32, 0); - self.ref_cur = ContextGatherer::load_word(reference, ref_y as u32, 0); - self.ref_p1 = ContextGatherer::load_word(reference, ref_y.wrapping_add(1) as u32, 0); + // PdfCraft patch: the buffers start a column left of the reference column the row starts + // at, as `maybe_reload_buffers` would load them, or at column 0 where that is left of the + // reference. The row's first context takes in that column and the one left of it, which + // from column 32 on lay outside a word loaded from column 0 and read as 0: with the + // reference offset 32 left of the region its first column, and from 33 both (GRREFERENCEDX + // below −31, which a refined text instance has from a narrower bitmap or an RDX below −31, + // and a refined symbol from such an RDX). The region decoded to other pixels than were + // coded, or failed. + self.ref_cur_x = self + .reference_dx + .checked_neg() + .and_then(|column| u32::try_from(column).ok()) + .map_or(0, |column| column.saturating_sub(1)); + self.ref_m1 = + ContextGatherer::load_word(reference, ref_y.wrapping_sub(1) as u32, self.ref_cur_x); + self.ref_cur = ContextGatherer::load_word(reference, ref_y as u32, self.ref_cur_x); + self.ref_p1 = + ContextGatherer::load_word(reference, ref_y.wrapping_add(1) as u32, self.ref_cur_x); self.ctx = match self.template { RefinementTemplate::Template1 => { @@ -259,25 +264,25 @@ impl<'a> RefinementContextGatherer<'a> { self.reg_cur = ContextGatherer::load_word(region, self.reg_y, new_start); } + // PdfCraft patch: left of the reference the buffers keep what `start_row` loaded, its words + // from column 0. The context takes in the reference a column to the right of the current + // one (Figures 12 and 13), so at column −1 it reads column 0, and from 0 on the buffers are + // the reference's own. They were zeroed here and, at column 0, not loaded again: with the + // reference offset right of the region (GRREFERENCEDX above 0, which a refined text + // instance or symbol has from a wider bitmap or an RDX above 0), its first 32 columns read + // as 0, and the region decoded to other pixels than were coded, or as garbage that failed. let ref_x_signed = (x as i32).wrapping_sub(self.reference_dx); - if ref_x_signed < 0 { - self.ref_cur_x = 0; - self.ref_m1 = 0; - self.ref_cur = 0; - self.ref_p1 = 0; - } else { - let ref_x = ref_x_signed as u32; - - if ref_x + 1 >= self.ref_cur_x + WORD_BITS || ref_x < self.ref_cur_x { - let new_start = ref_x.saturating_sub(1); - let ref_y = (self.reg_y as i32).wrapping_sub(self.reference_dy); - self.ref_cur_x = new_start; - self.ref_m1 = - ContextGatherer::load_word(reference, ref_y.wrapping_sub(1) as u32, new_start); - self.ref_cur = ContextGatherer::load_word(reference, ref_y as u32, new_start); - self.ref_p1 = - ContextGatherer::load_word(reference, ref_y.wrapping_add(1) as u32, new_start); - } + if let Ok(ref_x) = u32::try_from(ref_x_signed) + && (ref_x + 1 >= self.ref_cur_x + WORD_BITS || ref_x < self.ref_cur_x) + { + let new_start = ref_x.saturating_sub(1); + let ref_y = (self.reg_y as i32).wrapping_sub(self.reference_dy); + self.ref_cur_x = new_start; + self.ref_m1 = + ContextGatherer::load_word(reference, ref_y.wrapping_sub(1) as u32, new_start); + self.ref_cur = ContextGatherer::load_word(reference, ref_y as u32, new_start); + self.ref_p1 = + ContextGatherer::load_word(reference, ref_y.wrapping_add(1) as u32, new_start); } } diff --git a/vendor/hayro-jbig2/src/decode/symbol.rs b/vendor/hayro-jbig2/src/decode/symbol.rs index 94250a3de..6eefa0564 100644 --- a/vendor/hayro-jbig2/src/decode/symbol.rs +++ b/vendor/hayro-jbig2/src/decode/symbol.rs @@ -4,7 +4,7 @@ use alloc::vec; use alloc::vec::Vec; use crate::arithmetic_decoder::{ArithmeticDecoder, ArithmeticDecoderContext}; -use crate::bitmap::{BITMAP_WORDS, Bitmap, HUFFMAN_LINE_WORK, WORD_BITS, Word, decode_work}; +use crate::bitmap::{BITMAP_WORDS, Bitmap, HUFFMAN_LINE_WORK, INTEGER_WORK, WORD_BITS, Word, decode_work}; use crate::decode::generic::{decode_bitmap_mmr, parse_adaptive_template_pixels}; use crate::decode::text::{ DecodeContext, ReferenceCorner, TextRegionContexts, TextRegionFlags, TextRegionHeader, @@ -24,10 +24,14 @@ use crate::reader::Reader; const MAX_SYMBOLS: u32 = u16::MAX as u32; -/// PdfCraft patch: the most symbol pixels one dictionary may decode (2^26). Symbols are -/// decoded pixel by pixel, and up to 65535 of them of up to 65535 × 65535 each kept a fuzzed -/// file decoding for minutes; real dictionaries hold about a million pixels. -const MAX_DICTIONARY_PIXELS: u64 = 1 << 26; +/// PdfCraft patch: the most symbol pixels one dictionary may decode, as many as the largest page +/// bitmap holds (2^28, `bitmap::MAX_PIXELS`). Symbols are decoded pixel by pixel, and up to 65535 +/// of them of up to 65535 × 65535 each kept a fuzzed file decoding for minutes; each symbol now +/// pays for its pixels out of the stream's decoding work (`bitmap::MAX_WORK`), which bounds the +/// time, and holds them in the retained bitmaps (`room`), which bounds the memory. The bound was +/// 2^26, a quarter of this, which a real scan passes: the soft mask of a 14,642 × 8,879 drawing +/// with one dictionary of 7,559 symbols (#6722) needed more, and was refused. +const MAX_DICTIONARY_PIXELS: u64 = crate::bitmap::MAX_PIXELS; /// PdfCraft patch: decoding steps that decode nothing, allowed beyond what a valid dictionary can /// need. A height class without a symbol and an export run of length 0 don't move their loops @@ -101,6 +105,8 @@ pub(crate) fn decode( bail!(SymbolError::Invalid); } height_classes_left -= 1; + // PdfCraft patch: see `bitmap::INTEGER_WORK`. A class of no symbols still reads its delta. + *ctx.work_left = ctx.work_left.checked_sub(INTEGER_WORK).ok_or(OverflowError::DecodingWork)?; let height_class_delta = read_height_class_delta(&mut ctx)?.ok_or(SymbolError::UnexpectedOob)?; @@ -133,10 +139,13 @@ pub(crate) fn decode( .pixels .saturating_add(u64::from(symbol_width) * u64::from(ctx.height_class_height)); if ctx.pixels > MAX_DICTIONARY_PIXELS { - bail!(OverflowError::BitmapDimension); + bail!(OverflowError::BitmapLimit); } - // PdfCraft patch: see `bitmap::MAX_WORK`. - let work = decode_work(symbol_width, ctx.height_class_height); + // PdfCraft patch: see `bitmap::INTEGER_WORK`. Its width, and a stream can hold thousands + // of dictionaries of symbols of no pixels. An aggregated one reads its count, a symbol + // and two offsets as well. + let integers = if ctx.header.flags.use_refagg { 5 } else { 1 }; + let work = decode_work(symbol_width, ctx.height_class_height).saturating_add(integers * INTEGER_WORK); *ctx.work_left = ctx.work_left.checked_sub(work).ok_or(OverflowError::DecodingWork)?; // PdfCraft patch: see `room`. Rows take a word at least, so symbols of one column // held 270 MiB within the pixel bound, however few the dictionary exported. @@ -652,6 +661,17 @@ fn decode_height_class_collective_bitmap(ctx: &mut SymbolDecodeContext<'_>) -> R ctx.h_ctx.reader.align(); + // PdfCraft patch: a class of no symbols has nothing to cut out, and `bitmap::decode_work` charges + // rows by the symbol, so decoding this would be rows for nothing: 65,535 a class, and a dictionary + // can hold a thousand such classes. Its bytes still have to be stepped over. + if ctx.symbols_decoded_count == ctx.height_class_first_symbol { + ctx.h_ctx + .reader + .read_bytes(bitmap_size as usize) + .ok_or(ParseError::UnexpectedEof)?; + return Ok(()); + } + let collective_bitmap = if bitmap_size == 0 { // Bitmap is stored uncompressed, so we basically just copy-paste // each byte. Rows are padded to the byte boundary. @@ -759,6 +779,8 @@ fn export_symbols(ctx: &mut SymbolDecodeContext<'_>) -> Result> { bail!(SymbolError::Invalid); } runs_left -= 1; + // PdfCraft patch: see `bitmap::INTEGER_WORK`. A run of no symbols still reads its length. + *ctx.work_left = ctx.work_left.checked_sub(INTEGER_WORK).ok_or(OverflowError::DecodingWork)?; let run_length = read_run_length()?; let end_index = index.checked_add(run_length).ok_or(OverflowError::Index)?; diff --git a/vendor/hayro-jbig2/src/decode/text.rs b/vendor/hayro-jbig2/src/decode/text.rs index 71924acf6..5406ffeb4 100644 --- a/vendor/hayro-jbig2/src/decode/text.rs +++ b/vendor/hayro-jbig2/src/decode/text.rs @@ -11,7 +11,7 @@ use super::{ use super::{RegionBitmap, generic_refinement}; use crate::ScratchBuffers; use crate::arithmetic_decoder::{ArithmeticDecoder, ArithmeticDecoderContext}; -use crate::bitmap::{Bitmap, copy_work, decode_work}; +use crate::bitmap::{Bitmap, INTEGER_WORK, copy_work, decode_work}; use crate::error::{HuffmanError, OverflowError, ParseError, Result, SymbolError, bail}; use crate::huffman_table::{HuffmanTable, StandardHuffmanTables, TableLine}; use crate::integer_decoder::IntegerDecoder; @@ -151,7 +151,7 @@ pub(crate) fn decode_with( let symbol_id = ctx.read_symbol_id()?; let symbol_bitmap = - decode_symbol_instance_bitmap(&mut ctx, symbols, header, symbol_id)?; + decode_symbol_instance_bitmap(&mut ctx, symbols, header, symbol_id, work_left)?; let symbol_bitmap_ref: &Bitmap = match &symbol_bitmap { SymbolBitmap::Reference(idx) => symbols.get(*idx).ok_or(SymbolError::OutOfRange)?, @@ -161,14 +161,8 @@ pub(crate) fn decode_with( let symbol_height = symbol_bitmap_ref.height as i32; // PdfCraft patch: see `bitmap::MAX_WORK`. An instance of a large symbol costs its copy, - // and 10,000 of one took 64 s. A refined one was decoded as well. Even one with no - // pixels goes round this loop. - let (width, height) = (symbol_bitmap_ref.width, symbol_bitmap_ref.height); - let copied = copy_work(width, height).saturating_add(1); - let work = match &symbol_bitmap { - SymbolBitmap::Reference(_) => copied, - SymbolBitmap::Owned(_) => copied.saturating_add(decode_work(width, height)), - }; + // and 10,000 of one took 64 s. Even one with no pixels reads its four integers. + let work = copy_work(symbol_bitmap_ref.width, symbol_bitmap_ref.height).saturating_add(4 * INTEGER_WORK); *work_left = work_left.checked_sub(work).ok_or(OverflowError::DecodingWork)?; if !header.flags.transposed @@ -549,6 +543,7 @@ fn decode_symbol_instance_bitmap( symbols: &[&Bitmap], header: &TextRegionHeader<'_>, symbol_id: usize, + work_left: &mut u64, ) -> Result { if !header.flags.use_refinement || ctx.read_refinement_flag()? == 0 { return Ok(SymbolBitmap::Reference(symbol_id)); @@ -580,6 +575,11 @@ fn decode_symbol_instance_bitmap( .checked_add(rdy) .ok_or(OverflowError::ReferenceOffset)?; + // PdfCraft patch: see `bitmap::MAX_WORK`. Before it is decoded: charged after, one instance + // could still be 2^28 pixels past what was left. With the four integers that gave its size. + let work = decode_work(refined_width, refined_height).saturating_add(4 * INTEGER_WORK); + *work_left = work_left.checked_sub(work).ok_or(OverflowError::DecodingWork)?; + let mut refined_bitmap = Bitmap::new(refined_width, refined_height)?; ctx.decode_refinement_bitmap( diff --git a/vendor/hayro-jbig2/src/error.rs b/vendor/hayro-jbig2/src/error.rs index c8bcf8b81..a91074e57 100644 --- a/vendor/hayro-jbig2/src/error.rs +++ b/vendor/hayro-jbig2/src/error.rs @@ -141,6 +141,10 @@ pub enum OverflowError { SegmentCount, /// PdfCraft patch: a stream asks for more decoding work than it may. DecodingWork, + /// PdfCraft patch: a bitmap, gray-scale image or symbol dictionary is larger than the decoder + /// holds (see `bitmap::MAX_PIXELS`): one of its limits, where `BitmapDimension` is arithmetic + /// that overflows. + BitmapLimit, } impl fmt::Display for DecodeError { @@ -171,6 +175,7 @@ impl fmt::Display for OverflowError { Self::RetainedBitmaps => write!(f, "the segments retain too many bitmaps"), Self::SegmentCount => write!(f, "too many segments or references in a stream"), Self::DecodingWork => write!(f, "a stream asks for too much decoding work"), + Self::BitmapLimit => write!(f, "a bitmap is larger than the decoder holds"), } } } @@ -312,6 +317,59 @@ impl From for DecodeError { } } +impl DecodeError { + /// PdfCraft patch: whether one of the decoder's limits refused the stream (on a bitmap's size, + /// a dictionary's symbols, a region's instances, the bitmaps retained, the segments or the + /// decoding work) rather than its data failing to decode. A file within the format can reach + /// these, so a caller can tell a refusal of its own from a damaged file. A size or count the + /// file damaged can read as past one of them, and is a limit here too: it can't be told from a + /// hostile one. + pub fn is_limit(&self) -> bool { + matches!( + self, + Self::Overflow( + OverflowError::BitmapLimit + | OverflowError::GridDimension + | OverflowError::SymbolCount + | OverflowError::RetainedBitmaps + | OverflowError::SegmentCount + | OverflowError::DecodingWork + ) | Self::Symbol(SymbolError::TooManyInstances) + ) + } +} + +#[cfg(test)] +mod pdfcraft_limit_tests { + use super::*; + + /// PdfCraft patch: the decoder's own limits are limits, and arithmetic that overflows, + /// damage, is not. + #[test] + fn the_decoders_limits_are_limits_and_overflowing_arithmetic_is_not() { + let limits = [ + DecodeError::Overflow(OverflowError::BitmapLimit), + DecodeError::Overflow(OverflowError::GridDimension), + DecodeError::Overflow(OverflowError::SymbolCount), + DecodeError::Overflow(OverflowError::RetainedBitmaps), + DecodeError::Overflow(OverflowError::SegmentCount), + DecodeError::Overflow(OverflowError::DecodingWork), + DecodeError::Symbol(SymbolError::TooManyInstances), + ]; + for limit in limits { + assert!(limit.is_limit(), "{limit:?} is a limit"); + } + let damage = [ + DecodeError::Overflow(OverflowError::BitmapDimension), + DecodeError::Overflow(OverflowError::PlacementCoordinate), + DecodeError::Symbol(SymbolError::TooManySymbols), + ]; + for damage in damage { + assert!(!damage.is_limit(), "{damage:?} is damage"); + } + } +} + /// Result type for JBIG2 decoding operations. pub type Result = core::result::Result; diff --git a/vendor/hayro-jbig2/src/file.rs b/vendor/hayro-jbig2/src/file.rs index 5b8bbe983..c5b59b27c 100644 --- a/vendor/hayro-jbig2/src/file.rs +++ b/vendor/hayro-jbig2/src/file.rs @@ -88,6 +88,19 @@ pub(crate) struct File<'a> { /// (D.4.1) const FILE_HEADER_ID: [u8; 8] = [0x97, 0x4A, 0x42, 0x32, 0x0D, 0x0A, 0x1A, 0x0A]; +/// PdfCraft patch (17): an embedded stream that starts with a file header (D.4), which some +/// producers write into a PDF's `JBIG2Decode` data although ISO 32000-1 7.4.7 has the embedded +/// organization (D.3) there. Returns the reader past the header and the organization the header +/// declares, or `None` when the data doesn't start with the header's ID string. +pub(crate) fn after_file_header(data: &[u8]) -> Result, FileOrganization)>> { + if !data.starts_with(&FILE_HEADER_ID) { + return Ok(None); + } + let mut reader = Reader::new(data); + let header = parse_file_header(&mut reader)?; + Ok(Some((reader, header.organization))) +} + /// Parse a standalone JBIG2 file. pub(crate) fn parse_file(data: &[u8]) -> Result> { let mut reader = Reader::new(data); @@ -170,7 +183,9 @@ fn parse_segments<'a>( FileOrganization::Sequential => { parse_segments_sequential(reader, &mut segments, &mut SegmentCount::default())?; } - FileOrganization::RandomAccess => parse_segments_random_access(reader, &mut segments)?, + FileOrganization::RandomAccess => { + parse_segments_random_access(reader, &mut segments, &mut SegmentCount::default())?; + } } Ok(segments) @@ -213,12 +228,12 @@ pub(crate) fn parse_segments_sequential<'a>( /// is followed by a sequence of segments headers; the last segment header is /// followed by the data for the first segment, then the data for the second /// segment, and so on." (D.2) -fn parse_segments_random_access<'a>( +pub(crate) fn parse_segments_random_access<'a>( reader: &mut Reader<'a>, segments: &mut Vec>, + count: &mut SegmentCount, ) -> Result<()> { let mut headers = Vec::new(); - let mut count = SegmentCount::default(); loop { if reader.at_end() { diff --git a/vendor/hayro-jbig2/src/gray_scale.rs b/vendor/hayro-jbig2/src/gray_scale.rs index 4aa4229a5..1cd5016f0 100644 --- a/vendor/hayro-jbig2/src/gray_scale.rs +++ b/vendor/hayro-jbig2/src/gray_scale.rs @@ -81,11 +81,11 @@ fn decode_arithmetic( let height = params.height; if width > MAX_DIMENSION || height > MAX_DIMENSION { - bail!(OverflowError::BitmapDimension); + bail!(OverflowError::BitmapLimit); } // PdfCraft patch: the product is bounded too (see `bitmap::MAX_GRID_CELLS`). if u64::from(width) * u64::from(height) > crate::bitmap::MAX_GRID_CELLS { - bail!(OverflowError::BitmapDimension); + bail!(OverflowError::BitmapLimit); } // `GSBPP` - The number of bits per gray-scale value. @@ -289,3 +289,34 @@ fn extract_bits( } } } + +#[cfg(test)] +mod pdfcraft_limit_tests { + use super::*; + use crate::error::DecodeError; + + /// PdfCraft patch: a gray-scale image past the bound on a side or on cells is refused by a + /// limit, before anything is decoded. A halftone region refuses such a grid first, so this is + /// reached only by calling it. + #[test] + fn a_gray_scale_image_past_its_bounds_is_refused_by_a_limit() { + let decode = |width, height| { + let params = GrayScaleParams { + use_mmr: false, + bits_per_pixel: 1, + width, + height, + template: Template::Template0, + skip_mask: None, + }; + decode_gray_scale_image(&[], ¶ms, &mut ScratchBuffers::default()).map(|_| ()) + }; + let limit = Err(DecodeError::Overflow(OverflowError::BitmapLimit)); + assert_eq!(decode(MAX_DIMENSION + 1, 1), limit, "a side"); + assert_eq!( + decode(1 << 13, (1 << 12) + 1), + limit, + "the cells, each side within its bound" + ); + } +} diff --git a/vendor/hayro-jbig2/src/lib.rs b/vendor/hayro-jbig2/src/lib.rs index 078370ed3..6ec7fd83f 100644 --- a/vendor/hayro-jbig2/src/lib.rs +++ b/vendor/hayro-jbig2/src/lib.rs @@ -78,7 +78,10 @@ pub use error::{ SegmentError, SymbolError, TemplateError, }; -use crate::file::{SegmentCount, parse_segments_sequential}; +use crate::file::{ + FileOrganization, SegmentCount, after_file_header, parse_segments_random_access, + parse_segments_sequential, +}; use bitmap::Bitmap; use decode::CombinationOperator; use decode::generic; @@ -120,7 +123,9 @@ impl<'a> Image<'a> { /// Parse an embedded JBIG2 image with optional global segments. /// /// The file is expected to use the embedded organization defined in - /// Annex D.3. + /// Annex D.3. PdfCraft patch (17): data that starts with a file header (D.4) + /// is read past it, in the organization it declares, as Acrobat and PDFBox + /// draw such streams; its segments still count together with the globals'. pub fn new_embedded(data: &'a [u8], globals: Option<&'a [u8]>) -> Result { let mut segments = Vec::new(); let mut count = SegmentCount::default(); @@ -129,8 +134,18 @@ impl<'a> Image<'a> { parse_segments_sequential(&mut reader, &mut segments, &mut count)?; }; - let mut reader = Reader::new(data); - parse_segments_sequential(&mut reader, &mut segments, &mut count)?; + match after_file_header(data)? { + Some((mut reader, FileOrganization::RandomAccess)) => { + parse_segments_random_access(&mut reader, &mut segments, &mut count)?; + } + Some((mut reader, FileOrganization::Sequential)) => { + parse_segments_sequential(&mut reader, &mut segments, &mut count)?; + } + None => { + let mut reader = Reader::new(data); + parse_segments_sequential(&mut reader, &mut segments, &mut count)?; + } + } segments.sort_by_key(|seg| seg.header.segment_number); @@ -198,11 +213,37 @@ impl<'a> Image<'a> { /// This is useful in case you want to convert multiple JBIG2 images, /// as it allows `hayro-jbig2` to reuse allocations during decoding. pub fn decode_with(&self, decoder: &mut D, ctx: &mut DecoderContext) -> Result<()> { - decode_segments(&self.segments, self.height_from_stripes, ctx)?; + let mut work_left = bitmap::MAX_WORK; + decode_segments(&self.segments, self.height_from_stripes, ctx, &mut work_left)?; emit_bitmap(&ctx.page_bitmap, decoder); Ok(()) } + + /// PdfCraft patch: [`Image::decode`], spending from `work`, what decoding may still cost + /// across a run of images. An image spends what it took, failing or not, starting with its + /// page's pixels, which whoever asked for them will go through, and its segments, so images + /// that decode nothing add up too. On top of that, it takes at most [`bitmap::MAX_WORK`] + /// whatever `work` says. One that `work` can't pay for spends all of it, so a run ends rather + /// than trying every image it has left. Reading the stream comes before this and is charged + /// by [`charge_reading`]. + pub fn decode_within(&self, decoder: &mut D, work: &mut u64) -> Result<()> { + let start = bitmap::decode_work(self.width, self.height).saturating_add(self.segments.len() as u64); + if charge_work(work, start).is_err() { + *work = 0; + bail!(OverflowError::DecodingWork); + } + let allowed = bitmap::MAX_WORK.min(*work); + let mut left = allowed; + let mut ctx = DecoderContext::default(); + let decoded = decode_segments(&self.segments, self.height_from_stripes, &mut ctx, &mut left); + let refused = decoded == Err(OverflowError::DecodingWork.into()); + *work = if refused && allowed < bitmap::MAX_WORK { 0 } else { *work - (allowed - left) }; + decoded?; + emit_bitmap(&ctx.page_bitmap, &mut *decoder); + + Ok(()) + } } fn emit_bitmap(bitmap: &Bitmap, decoder: &mut D) { @@ -262,22 +303,46 @@ fn emit_bitmap(bitmap: &Bitmap, decoder: &mut D) { } } -/// PdfCraft patch: count `work` more decoding work, refusing past [`bitmap::MAX_WORK`] before what -/// asks for it is decoded. +/// PdfCraft patch: count `work` more decoding work, refusing past what is `left` of +/// [`bitmap::MAX_WORK`] or a run's allowance before what asks for it is decoded. fn charge_work(left: &mut u64, work: u64) -> Result<()> { *left = left.checked_sub(work).ok_or(OverflowError::DecodingWork)?; Ok(()) } +/// PdfCraft patch: the steps of reading a byte of a stream or its globals: parsing and sorting +/// segments, and building their tables, measured at about 3 a byte at the worst. +const READ_WORK: u64 = 4; + +/// PdfCraft patch: charge `work`, what a run of images may still cost, for reading `bytes` of a stream +/// and its globals. This comes before anything is parsed, as it counts whether or not they turn out +/// to decode: an image that fails has still cost its reading, and it is read again for each use. What +/// `work` can't pay for spends all of it. +pub fn charge_reading(work: &mut u64, bytes: usize) -> Result<()> { + if charge_work(work, (bytes as u64).saturating_mul(READ_WORK)).is_err() { + *work = 0; + bail!(OverflowError::DecodingWork); + } + Ok(()) +} + /// PdfCraft patch: the work of a region decoded pixel by pixel. fn region_work(region: &decode::RegionSegmentInfo) -> u64 { bitmap::decode_work(region.width, region.height) } +/// PdfCraft patch: the page restricted to a refinement's area, its reference when it refers to no +/// other region (7.4.7.4). Copying it a word at a time costs less than the region's own decoding, +/// already charged, and it is held, as large as the region, only while the region decodes. +fn page_area(page: &Bitmap, region: &decode::RegionSegmentInfo) -> Result { + page.area(region.x_location, region.y_location, region.width, region.height) +} + fn decode_segments( segments: &[segment::Segment<'_>], height_from_stripes: Option, decoder_ctx: &mut DecoderContext, + work_left: &mut u64, ) -> Result<()> { // Find and parse page information segment first. if let Some(page_info) = segments @@ -298,7 +363,6 @@ fn decode_segments( let page_bitmap = &mut decoder_ctx.page_bitmap; let page_state = &mut decoder_ctx.page_state; let scratch_buffers = &mut decoder_ctx.scratch_buffers; - let mut work_left = bitmap::MAX_WORK; // Process all segments. for seg in segments { @@ -311,31 +375,26 @@ fn decode_segments( SegmentType::ImmediateGenericRegion | SegmentType::ImmediateLosslessGenericRegion => { let had_unknown_length = seg.header.data_length.is_none(); let header = generic::parse(&mut reader, had_unknown_length)?; - charge_work(&mut work_left, region_work(&header.region_info))?; + charge_work(work_left, region_work(&header.region_info))?; if page_state.can_decode_directly(page_bitmap, &header.region_info, false) { generic::decode_into(&header, page_bitmap, scratch_buffers)?; } else { let region = generic::decode(&header, scratch_buffers)?; - page_bitmap.combine( - ®ion.bitmap, - region.bitmap.x_location as i32, - region.bitmap.y_location as i32, - region.combination_operator, - ); + page_bitmap.combine_region(®ion.bitmap, region.combination_operator); } page_state.page_pristine = false; } SegmentType::IntermediateGenericRegion => { // Intermediate segments cannot have unknown length. let header = generic::parse(&mut reader, false)?; - charge_work(&mut work_left, region_work(&header.region_info))?; + charge_work(work_left, region_work(&header.region_info))?; let region = generic::decode(&header, scratch_buffers)?; page_state.store_region(seg.header.segment_number, region.bitmap)?; } SegmentType::PatternDictionary => { let header = pattern::parse(&mut reader)?; - charge_work(&mut work_left, header.work())?; + charge_work(work_left, header.work())?; let dictionary = pattern::decode(&header, scratch_buffers, page_state.retained_room())?; page_state.store_pattern_dictionary(seg.header.segment_number, dictionary)?; } @@ -344,7 +403,7 @@ fn decode_segments( // (6.5.5, step 1) // Collect references to avoid cloning; symbols are only cloned if re-exported. let input_symbols = page_state.referred_symbols(&seg.header.referred_to_segments)?; - charge_work(&mut work_left, input_symbols.len() as u64)?; + charge_work(work_left, input_symbols.len() as u64)?; // Collect Huffman tables from referred table segments. let referred_tables: Vec = seg @@ -371,14 +430,14 @@ fn decode_segments( &page_state.standard_tables, retained_contexts, page_state.retained_room(), - &mut work_left, + work_left, )?; page_state.store_symbol_dictionary(seg.header.segment_number, dictionary)?; } SegmentType::ImmediateTextRegion | SegmentType::ImmediateLosslessTextRegion => { // Collect symbols from referred symbol dictionaries (SBSYMS). let symbols = page_state.referred_symbols(&seg.header.referred_to_segments)?; - charge_work(&mut work_left, symbols.len() as u64)?; + charge_work(work_left, symbols.len() as u64)?; // Collect Huffman tables from referred table segments. // "These user-supplied Huffman decoding tables may be supplied either @@ -392,10 +451,10 @@ fn decode_segments( .collect(); let header = text::parse(&mut reader, symbols.len() as u32)?; - charge_work(&mut work_left, bitmap::copy_work(header.region_info.width, header.region_info.height))?; + charge_work(work_left, bitmap::copy_work(header.region_info.width, header.region_info.height))?; if header.flags.use_huffman { // Its symbol ID table has a line for each symbol. - charge_work(&mut work_left, (symbols.len() as u64).saturating_mul(bitmap::HUFFMAN_LINE_WORK))?; + charge_work(work_left, (symbols.len() as u64).saturating_mul(bitmap::HUFFMAN_LINE_WORK))?; } if page_state.can_decode_directly( @@ -410,7 +469,7 @@ fn decode_segments( &page_state.standard_tables, page_bitmap, scratch_buffers, - &mut work_left, + work_left, )?; } else { let region = text::decode( @@ -419,21 +478,16 @@ fn decode_segments( &referred_tables, &page_state.standard_tables, scratch_buffers, - &mut work_left, + work_left, )?; - page_bitmap.combine( - ®ion.bitmap, - region.bitmap.x_location as i32, - region.bitmap.y_location as i32, - region.combination_operator, - ); + page_bitmap.combine_region(®ion.bitmap, region.combination_operator); } page_state.page_pristine = false; } SegmentType::IntermediateTextRegion => { // Collect symbols from referred symbol dictionaries (SBSYMS). let symbols = page_state.referred_symbols(&seg.header.referred_to_segments)?; - charge_work(&mut work_left, symbols.len() as u64)?; + charge_work(work_left, symbols.len() as u64)?; // Collect Huffman tables from referred table segments. let referred_tables: Vec = seg @@ -445,10 +499,10 @@ fn decode_segments( .collect(); let header = text::parse(&mut reader, symbols.len() as u32)?; - charge_work(&mut work_left, bitmap::copy_work(header.region_info.width, header.region_info.height))?; + charge_work(work_left, bitmap::copy_work(header.region_info.width, header.region_info.height))?; if header.flags.use_huffman { // Its symbol ID table has a line for each symbol. - charge_work(&mut work_left, (symbols.len() as u64).saturating_mul(bitmap::HUFFMAN_LINE_WORK))?; + charge_work(work_left, (symbols.len() as u64).saturating_mul(bitmap::HUFFMAN_LINE_WORK))?; } let region = text::decode( &header, @@ -456,7 +510,7 @@ fn decode_segments( &referred_tables, &page_state.standard_tables, scratch_buffers, - &mut work_left, + work_left, )?; page_state.store_region(seg.header.segment_number, region.bitmap)?; } @@ -469,7 +523,7 @@ fn decode_segments( .ok_or(SegmentError::MissingPatternDictionary)?; let header = halftone::parse(&mut reader)?; - charge_work(&mut work_left, halftone::work(&header, pattern_dict))?; + charge_work(work_left, halftone::work(&header, pattern_dict))?; if page_state.can_decode_directly( page_bitmap, @@ -479,12 +533,7 @@ fn decode_segments( halftone::decode_into(&header, pattern_dict, page_bitmap, scratch_buffers)?; } else { let region = halftone::decode(&header, pattern_dict, scratch_buffers)?; - page_bitmap.combine( - ®ion.bitmap, - region.bitmap.x_location as i32, - region.bitmap.y_location as i32, - region.combination_operator, - ); + page_bitmap.combine_region(®ion.bitmap, region.combination_operator); } page_state.page_pristine = false; } @@ -497,21 +546,29 @@ fn decode_segments( .ok_or(SegmentError::MissingPatternDictionary)?; let header = halftone::parse(&mut reader)?; - charge_work(&mut work_left, halftone::work(&header, pattern_dict))?; + charge_work(work_left, halftone::work(&header, pattern_dict))?; let region = halftone::decode(&header, pattern_dict, scratch_buffers)?; page_state.store_region(seg.header.segment_number, region.bitmap)?; } SegmentType::IntermediateGenericRefinementRegion => { // Same logic as immediate refinement, but store result instead of combining. - let reference = seg + let referred_segment = seg .header .referred_to_segments .first() - .and_then(|&num| page_state.get_referred_segment(num)) - .unwrap_or(page_bitmap); + .and_then(|&num| page_state.get_referred_segment(num)); let header = generic_refinement::parse(&mut reader)?; - charge_work(&mut work_left, region_work(&header.region_info))?; + charge_work(work_left, region_work(&header.region_info))?; + // PdfCraft patch: a refinement of the page refines its area (`Bitmap::area`). + let area; + let reference = match referred_segment { + Some(region) => region, + None => { + area = page_area(page_bitmap, &header.region_info)?; + &area + } + }; let region = generic_refinement::decode(&header, reference, scratch_buffers)?; page_state.store_region(seg.header.segment_number, region.bitmap)?; } @@ -520,8 +577,8 @@ fn decode_segments( // "3) Determine the buffer associated with the region segment that // this segment refers to." (7.4.7.5) // - // "2) If there are no referred-to segments, then use the page - // bitmap as the reference buffer." (7.4.7.5) + // With no region segment referred to, the reference is the page restricted to + // this segment's area (7.4.7.4). let referred_segment = seg .header .referred_to_segments @@ -529,7 +586,7 @@ fn decode_segments( .and_then(|&num| page_state.get_referred_segment(num)); let header = generic_refinement::parse(&mut reader)?; - charge_work(&mut work_left, region_work(&header.region_info))?; + charge_work(work_left, region_work(&header.region_info))?; if let Some(referred_segment) = referred_segment && page_state.can_decode_directly(page_bitmap, &header.region_info, false) @@ -541,14 +598,17 @@ fn decode_segments( scratch_buffers, )?; } else { - let reference = referred_segment.unwrap_or(page_bitmap); + // PdfCraft patch: a refinement of the page refines its area (`Bitmap::area`). + let area; + let reference = match referred_segment { + Some(region) => region, + None => { + area = page_area(page_bitmap, &header.region_info)?; + &area + } + }; let region = generic_refinement::decode(&header, reference, scratch_buffers)?; - page_bitmap.combine( - ®ion.bitmap, - region.bitmap.x_location as i32, - region.bitmap.y_location as i32, - region.combination_operator, - ); + page_bitmap.combine_region(®ion.bitmap, region.combination_operator); } page_state.page_pristine = false; } @@ -794,6 +854,192 @@ mod tests { assert!(Image::new_embedded(&stream(1 << 20, 1 << 20), None).is_err()); } + /// Segment `number` of `kind` on page 1, referring to `refers_to`. + fn segment(number: u8, kind: u8, refers_to: &[u8], data: &[u8]) -> Vec { + let mut segment = vec![0, 0, 0, number, kind, (refers_to.len() as u8) << 5]; + segment.extend_from_slice(refers_to); + segment.push(1); + segment.extend_from_slice(&(data.len() as u32).to_be_bytes()); + segment.extend_from_slice(data); + segment + } + + /// A blank page of 8 × 8: resolution unknown, flags, not striped. + fn page_of_8() -> Vec { + segment(0, 48, &[], &[0, 0, 0, 8, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0x42, 0, 0]) + } + + /// Region information (7.4.1) of 4 × 4 at `x`, `y`, then the region's own `header`, then + /// arbitrary coded data. + fn region_of_4(x: u32, y: u32, operator: u8, header: &[u8]) -> Vec { + let mut data: Vec = [4u32, 4, x, y].iter().flat_map(|v| v.to_be_bytes()).collect(); + data.push(operator); + data.extend_from_slice(header); + data.extend_from_slice(&[0x5a; 16]); + data + } + + const GENERIC: &[u8] = &[0, 3, 0xFF, 0xFD, 0xFF, 2, 0xFE, 0xFE, 0xFE]; // template 0, AT + const REFINEMENT: &[u8] = &[0, 0xFF, 0xFF, 0xFF, 0xFF]; // template 0, AT + const OR: u8 = 0; + const REPLACE: u8 = 4; + + /// PdfCraft patch: a region's location is unsigned (5.4.4), so one past `i32::MAX` is off the + /// page. Taken for a signed one, a region at 0xFFFFFFFE drew back onto the page two pixels left + /// of it, and a refinement there was refused, and the rest of the stream with it. Each region + /// here is 4 × 4 on a blank page of 8 × 8, its coded data arbitrary: on the page it draws, off + /// it nothing. + #[test] + fn a_region_located_past_i32_max_is_off_the_page() { + struct Ink(u32); + impl Decoder for Ink { + fn push_pixel(&mut self, black: bool) { + self.0 += u32::from(black); + } + fn push_pixel_chunk(&mut self, black: bool, chunks: u32) { + self.0 += if black { 8 * chunks } else { 0 }; + } + fn next_line(&mut self) {} + } + let ink = |x: u32, y: u32| { + let forms = [ + // An immediate generic region. + vec![segment(1, 38, &[], ®ion_of_4(x, y, OR, GENERIC))], + // An immediate refinement of the page, which reads the page at its own location (7.4.7.4). + vec![segment(1, 42, &[], ®ion_of_4(x, y, REPLACE, REFINEMENT))], + // An immediate refinement of an intermediate region at the same place (7.4.7.5). + vec![ + segment(1, 36, &[], ®ion_of_4(x, y, OR, GENERIC)), + segment(2, 42, &[1], ®ion_of_4(x, y, OR, REFINEMENT)), + ], + ]; + forms.map(|form| { + let stream = [page_of_8(), form.concat()].concat(); + let mut ink = Ink(0); + Image::new_embedded(&stream, None).and_then(|image| image.decode(&mut ink)).map(|()| ink.0) + }) + }; + for (x, y) in [(0, 0), (2, 2)] { + assert!(ink(x, y).iter().all(|ink| matches!(ink, Ok(n) if *n > 0)), "{x}, {y}: {:?}", ink(x, y)); + } + for (x, y) in [(0xFFFF_FFFE, 0), (0, 0xFFFF_FFFE), (0x8000_0000, 0x8000_0000), (u32::MAX, u32::MAX)] { + assert_eq!(ink(x, y).map(|ink| ink.ok()), [Some(0); 3], "{x:#x}, {y:#x}"); + } + } + + /// PdfCraft patch: a refinement of a region refines it where it is, "GRREFERENCEDX 0, + /// GRREFERENCEDY 0" (Table 35), not the page at the refinement's location. A region and its + /// refinement, directly or through an intermediate refinement, decode otherwise than the same + /// refinements of the page, and at 2, 3 to the pixels they decode to at 0, 0, moved. + #[test] + fn a_refinement_of_a_region_decodes_the_same_wherever_they_are() { + #[derive(Default)] + struct Pixels(Vec); + impl Decoder for Pixels { + fn push_pixel(&mut self, black: bool) { + self.0.push(black); + } + fn push_pixel_chunk(&mut self, black: bool, chunks: u32) { + self.0.extend((0..8 * chunks).map(|_| black)); + } + fn next_line(&mut self) {} + } + // The first refinement refers to the region before it, or `of_page`, to nothing: the page. + let decode = |x: u32, y: u32, through_intermediate: bool, of_page: bool| { + let first: &[u8] = if of_page { &[] } else { &[1] }; + let mut form = vec![segment(1, 36, &[], ®ion_of_4(x, y, OR, GENERIC))]; + if through_intermediate { + form.push(segment(2, 40, first, ®ion_of_4(x, y, OR, REFINEMENT))); + form.push(segment(3, 42, &[2], ®ion_of_4(x, y, OR, REFINEMENT))); + } else { + form.push(segment(2, 42, first, ®ion_of_4(x, y, OR, REFINEMENT))); + } + let stream = [page_of_8(), form.concat()].concat(); + let mut pixels = Pixels::default(); + Image::new_embedded(&stream, None).unwrap().decode(&mut pixels).unwrap(); + pixels.0 + }; + for through_intermediate in [false, true] { + let at_origin = decode(0, 0, through_intermediate, false); + assert!(at_origin.contains(&true), "draws"); + assert_ne!(at_origin, decode(0, 0, through_intermediate, true), "the page refined instead"); + for (i, &black) in decode(2, 3, through_intermediate, false).iter().enumerate() { + let (x, y) = (i % 8, i / 8); + let moved = x >= 2 && y >= 3 && at_origin[(y - 3) * 8 + x - 2]; + assert_eq!(black, moved, "{x}, {y}, through an intermediate refinement: {through_intermediate}"); + } + } + } + + /// PdfCraft patch: a refinement of the page refines the page restricted to its area (7.4.7.4), + /// reading 0 around it, and a reference 32 or more left of a refinement is read from where each + /// row starts in it. So that a context read wrong changes what decodes, the refinements are + /// large, and their pictures come from the hash they were coded from: a 64 × 16 refinement at + /// 41, 4 of a 112 × 24 page, and a 70 × 12 text instance refined from a 140 × 12 symbol 35 + /// left of it. + #[test] + fn a_refinement_refines_the_page_in_its_area_and_any_reference_where_it_is() { + #[derive(Default)] + struct Rows(Vec>, Vec); + impl Decoder for Rows { + fn push_pixel(&mut self, black: bool) { + self.1.push(u8::from(black)); + } + fn push_pixel_chunk(&mut self, black: bool, chunks: u32) { + for _ in 0..8 * chunks { + self.push_pixel(black); + } + } + fn next_line(&mut self) { + self.0.push(core::mem::take(&mut self.1)); + } + } + fn bit(seed: u32, x: u32, y: u32) -> u8 { + let mut h = x.wrapping_mul(0x9E37_79B1).wrapping_add(y.wrapping_mul(0x85EB_CA77)).wrapping_add(seed.wrapping_mul(0xC2B2_AE3D)); + h ^= h >> 15; + h = h.wrapping_mul(0x2C1B_3C6D); + h ^= h >> 12; + h = h.wrapping_mul(0x297A_2D39); + h ^= h >> 15; + ((h >> 7) & 1) as u8 + } + // About one pixel in eight: where a refinement differs from its reference. + let flipped = |seed: u32, x: u32, y: u32| bit(seed, x, y) & bit(seed + 1, x, y) & bit(seed + 2, x, y); + // A page of bit(1, ..), and over it at 41, 4 the area refined from it. + const PAGE: &str = "000000003000010000001300000070000000180000000000000000420000000000012600010000016f00000070000000180000000000000000040003fffdff02fefefefb5d354db053c2a984c01af66c619b02c245055be23a2a431b04c60325c88676f6ff1b0a68c873ad9011a27a89fc99abc7db2e8f529ecf198ff8c6a726ddcb746dc51f3812e365e74ee668fa02f59182cae39f0ef43b3201da1bcd43a21f449889e15ba0ba1a9344da1bdb9dcb4015279a39d944509febc622a235fa2c623acb443f0c10dabf7fba5e9d6fef2f1289dfd0a6a3801d036f21a62045074f5d3158ab0373ad2334102ef867dc3132dc8d02fe517771c419cdbad47d67d409ec89d0425d1f51a48af6ba3bee6e0701185bfa5f46b347b0ec39e744b665d1e8aac868838d3db25162d65534ff4771d9c0d657bd48d5814fa7cc184d0e961defab2d75a79a88345cdae5546a54221a22ddfe9c8a0514f32ac9de806fbc5fb4f5275c79d679f0258f77900daaa273cee50e50c958f323414670d7761418639342fdb2082eac5e7934f1a06169991452b1a042eaee7871ffac000000022a000100000091000000400000001000000029000000040400ffffffffd24567306e52f57232cefa9800db00fb60c21b30b0e53d9fac8a1f863a580a7cc49ab401934ac8be3a18e2dc0f01774b76c3e9196283cb5504742c763a867bc1aae8c48d74d194dacda03ce7e91904897ab61add273d9188fda65dc6fefb995d85f48142e43c1970bedb9c5f4be3cb00d458e9f4ee9714a61fffac"; + let page: Vec> = (0..24) + .map(|y| (0..112).map(|x| bit(1, x, y) ^ if (41..105).contains(&x) && (4..20).contains(&y) { flipped(10, x - 41, y - 4) } else { 0 }).collect()) + .collect(); + // A symbol of bit(3, ..), and a text instance refined from it, 70 narrower with RDX 0. + const TEXT: &str = "0000000030000100000013000000460000000c000000000000000042000000000001000001000000ed000003fffdff02fefefe00000001000000013f4efc0ce48e6b5c00246a648400fb605a44d542ed29da2b9b55c01f4e5af9842dd70c88f4913c7f552503ba52a5d7b5d9fd08ff5794b4dbfb3df7bafcf9c43d04154c1cefa181ca1aba5da826a4e4229858bd0950442df69c098ab3b61a32f71b136974b96bcb5306f3e75af1a4ef51b8f56a355acb8b69915cfa623f5a21933be8c056a348783a116ea914e886af2c64bf3b23e5610726c981092220f05411857529861fbf4c630a15fd00b081d2351279e06602f4408b25dc1d01c223027ecb8c87c4b1d76e3882a759c1adaf749f0e20735f8bd5a7306fffac000000020620010100000085000000460000000c0000000000000000040012ffffffff00000001a967c0b129999147062a4766ce10bc3dae7f1daf43dda1427d31203a1bc9fa021e0504e0a4b01739952970b54ad11ac08c0684d2f556a1843eb01dc75b963be6237b7f02fdfe17ccf7f061de310a936fd08c5ddea47609789e55f7cbca81ae38219c4abc5f9eff47ffac"; + let text: Vec> = (0..12).map(|y| (0..70).map(|x| bit(3, x + 35, y) ^ flipped(30, x, y)).collect()).collect(); + for (stream, picture) in [(PAGE, page), (TEXT, text)] { + let stream: Vec = (0..stream.len() / 2).map(|i| u8::from_str_radix(&stream[i * 2..i * 2 + 2], 16).unwrap()).collect(); + let mut rows = Rows::default(); + Image::new_embedded(&stream, None).unwrap().decode(&mut rows).unwrap(); + assert_eq!(rows.0, picture); + } + } + + /// PdfCraft patch: the area a refinement of the page refines is the page inside it and 0 + /// outside: off the page's rows and columns, past where a column can be, and past the page's + /// width and the area's own whatever the padding of the page's rows holds. `new_with` clears + /// that padding (patch 13), so the page's words are set to all 1s, padding included. + #[test] + fn a_page_area_is_the_page_inside_it_and_0_outside() { + let mut page = Bitmap::new_with(40, 3, 0, 0, true).unwrap(); + page.data.fill(!0); + let area = |x, y, width, height| page.area(x, y, width, height).unwrap().data; + assert_eq!(area(30, 1, 20, 3), [0xFFC0_0000, 0xFFC0_0000, 0], "10 columns and 2 rows of the page"); + assert_eq!(area(0, 0, 33, 1), [!0, 0x8000_0000], "the whole of a word, and none of the area's padding"); + assert_eq!(area(36, 0, 8, 1), [0xF000_0000], "4 columns, and none of the page's padding"); + assert_eq!(area(5, 0, 8, 1), [0xFF00_0000], "inside the page"); + assert_eq!(area(40, 0, 8, 1), [0], "right of the page"); + assert_eq!(area(u32::MAX - 2, 0, 40, 1), [0, 0], "past where a column can be"); + assert_eq!(area(0, u32::MAX, 8, 2), [0, 0], "below the page"); + assert!(area(3, 0, 0, 2).is_empty(), "no width"); + } + /// A stream and its globals are one stream of segments, so they have one count. #[test] fn the_segments_of_a_stream_and_its_globals_count_together() { @@ -808,6 +1054,49 @@ mod tests { assert!(refused(&more), "one more isn't"); } + /// PdfCraft patch: a refinement whose reference is offset right of it reads the reference from + /// its first column, where it read the first 32 as 0. So that a context read wrong changes what + /// decodes, the refinement is large, and its picture comes from the hash it was coded from: a + /// 104 × 12 text instance refined from a 100 × 12 symbol 2 right of it. + #[test] + fn a_refinement_reads_a_reference_right_of_it_from_its_first_column() { + #[derive(Default)] + struct Rows(Vec>, Vec); + impl Decoder for Rows { + fn push_pixel(&mut self, black: bool) { + self.1.push(u8::from(black)); + } + fn push_pixel_chunk(&mut self, black: bool, chunks: u32) { + for _ in 0..8 * chunks { + self.push_pixel(black); + } + } + fn next_line(&mut self) { + self.0.push(core::mem::take(&mut self.1)); + } + } + fn bit(seed: u32, x: u32, y: u32) -> u8 { + let mut h = x.wrapping_mul(0x9E37_79B1).wrapping_add(y.wrapping_mul(0x85EB_CA77)).wrapping_add(seed.wrapping_mul(0xC2B2_AE3D)); + h ^= h >> 15; + h = h.wrapping_mul(0x2C1B_3C6D); + h ^= h >> 12; + h = h.wrapping_mul(0x297A_2D39); + h ^= h >> 15; + ((h >> 7) & 1) as u8 + } + // About one pixel in eight: where a refinement differs from its reference. + let flipped = |seed: u32, x: u32, y: u32| bit(seed, x, y) & bit(seed + 1, x, y) & bit(seed + 2, x, y); + // A symbol of bit(2, ..), and a text instance refined from it, 4 wider with RDX 0. + const TEXT: &str = "0000000030000100000013000000680000000c000000000000000042000000000001000001000000b1000003fffdff02fefefe00000001000000013f525ad7118f669a303f1b11efa5210e9765b834b1831c650f3a3d325dbd7317379ea4fb573b09a20c362f065d8f9301e74cbbc50c69910641c0e0eea7c2fd1e831ca1459a943be55ed8e074b426ad7ff1e8d8ff6f1aa44684de5da4b4c3483911604412e00e96a0db6ec7103feb2bc4301ae98d3f9c23078ae72dc46f76e9dfea209d39187f1194f7b9285cd6e7b2dc7abb5def43ff0abe2df554fc55ffac0000000206200101000000af000000680000000c0000000000000000040012ffffffff00000001a96b5febebec5a68693b51f5db25ddf176d4aae89904a71d1690f12888e10b038db723ac59941f76c2844b739ce524d9563f4005e1edbabf2e323e8e9ac1a3b9060587c3010e7d6bcf90c00dbf9000a2f17b1d9b598e01b9158dc16a63f4388a6f647b99dc7118aa09e08d59fba1bc24e3a0f1ebb0cae636b6bedbb65b01945228d3e5d2980a0ad6290c13d2f34f5c861415ffac"; + let picture: Vec> = (0..12) + .map(|y| (0..104).map(|x| (if (2..102).contains(&x) { bit(2, x - 2, y) } else { 0 }) ^ flipped(20, x, y)).collect()) + .collect(); + let stream: Vec = (0..TEXT.len() / 2).map(|i| u8::from_str_radix(&TEXT[i * 2..i * 2 + 2], 16).unwrap()).collect(); + let mut rows = Rows::default(); + Image::new_embedded(&stream, None).unwrap().decode(&mut rows).unwrap(); + assert_eq!(rows.0, picture); + } + /// A dictionary being decoded has what the page may still hold, not all of it: refused only /// once it is stored, its copies would already have been made. #[test] @@ -819,4 +1108,17 @@ mod tests { page.retained_words = MAX_RETAINED_WORDS; assert_eq!(page.retained_room(), 0); } + + /// Pixels past a bitmap's edge are 0 (6.3.5.2), and a refinement reads its reference a word at + /// a time, so a bitmap filled with 1s has none past its width: made, and a page made again for + /// the next image. + #[test] + fn a_bitmap_of_1s_has_nothing_past_its_width() { + assert_eq!(Bitmap::new_with(33, 2, 0, 0, true).unwrap().data, [!0, 1 << 31, !0, 1 << 31]); + let mut page = Bitmap::new_with(40, 1, 0, 0, false).unwrap(); + page.reinitialize(7, 2, true).unwrap(); + assert_eq!(page.data, [0xFE00_0000, 0xFE00_0000]); + assert_eq!(Bitmap::new_with(64, 1, 0, 0, true).unwrap().data, [!0, !0], "words filled whole"); + assert!(Bitmap::new_with(0, 3, 0, 0, true).unwrap().data.is_empty(), "no width"); + } } diff --git a/vendor/hayro-jbig2/src/pdfcraft_tests.rs b/vendor/hayro-jbig2/src/pdfcraft_tests.rs index e26d4e9cf..cd96db6c9 100644 --- a/vendor/hayro-jbig2/src/pdfcraft_tests.rs +++ b/vendor/hayro-jbig2/src/pdfcraft_tests.rs @@ -101,9 +101,14 @@ fn jbig2_segment(out: &mut Vec, number: u32, kind: u8, refs: &[u8], data: &[ /// The page information of a 16 × 2 JBIG2 page whose default pixel is black, so that the image, if /// it decodes, draws black whatever its other segments hold. fn black_jbig2_page() -> Vec { + black_jbig2_page_of(16, 2) +} + +/// [`black_jbig2_page`], `width` × `height`. +fn black_jbig2_page_of(width: u32, height: u32) -> Vec { let mut page = Vec::new(); - page.extend_from_slice(&16u32.to_be_bytes()); - page.extend_from_slice(&2u32.to_be_bytes()); + page.extend_from_slice(&width.to_be_bytes()); + page.extend_from_slice(&height.to_be_bytes()); page.extend_from_slice(&[0; 8]); // resolution page.extend_from_slice(&[4, 0, 0]); // flags (default pixel black), striping let mut stream = Vec::new(); @@ -120,6 +125,150 @@ fn jbig2_region_info(width: u32, height: u32) -> Vec { region } +/// A Huffman-coded symbol dictionary of `symbols` symbols of width 0 and height 1, exported or not: +/// each holds no pixels, and costs a bitmap's header all the same. +fn zero_width_jbig2_dictionary(symbols: u32, exported: bool) -> Vec { + let mut dictionary = vec![0x00, 0x01]; // Huffman, tables B.4, B.2, B.1, B.1 + dictionary.extend_from_slice(&(if exported { symbols } else { 0 }).to_be_bytes()); + dictionary.extend_from_slice(&symbols.to_be_bytes()); // new + let mut bits = Bits::default(); + bits.put(0, 1); // height class delta 1 (B.4) + for _ in 0..symbols { + bits.put(0, 1); // width delta 0 (B.2) + } + bits.put(0b111111, 6); // end of the class (B.2) + bits.put(0, 5); // BMSIZE 0, uncompressed (B.1) + bits.align(); + if exported { + bits.table_b1(0); // the first export run isn't exported + } + bits.table_b1(symbols); + dictionary.extend_from_slice(&bits.bytes); + dictionary +} + +/// A Huffman-coded symbol dictionary of `symbols` symbols of width 1 and no rows, none exported. +/// Only table B.5 has a negative height class delta, so it gets to no rows from one. +fn no_row_jbig2_dictionary(symbols: u32) -> Vec { + let mut bits = Bits::default(); + bits.put(0, 1); // height class delta 1 (B.5), a class of no symbols + bits.put(0b111111, 6); // end of the class (B.2) + bits.put(0, 5); // BMSIZE 0, uncompressed (B.1) + bits.align(); + bits.put(0b1111110, 7); // height class delta -1 (B.5): -255 and 254 + bits.put(254, 8); + bits.put(0b10, 2); // width delta 1 (B.2) + for _ in 1..symbols { + bits.put(0, 1); // width delta 0 (B.2) + } + bits.put(0b111111, 6); // end of the class (B.2) + bits.put(0, 5); // BMSIZE 0, uncompressed (B.1) + bits.align(); + bits.table_b1(symbols); // the first export run, not exported, is all of them + let mut dictionary = vec![0x00, 0x05]; // Huffman, tables B.5, B.2, B.1, B.1 + dictionary.extend_from_slice(&0u32.to_be_bytes()); // exported + dictionary.extend_from_slice(&symbols.to_be_bytes()); // new + dictionary.extend_from_slice(&bits.bytes); + dictionary +} + +/// A Huffman-coded symbol dictionary that climbs to `height`, stays about there for `classes` +/// classes of no symbols, each with a byte of MMR bitmap, and drops to one row for its one symbol, +/// not exported. +fn empty_class_jbig2_dictionary(classes: u32, height: u32) -> Vec { + let mut bits = Bits::default(); + bits.put(0b111110, 6); // height class delta, the upper line of table B.5 + bits.put(height - 76, 32); + let mut at = height; + for class in 0..classes { + // Table B.5 has no delta of 0, so it goes down one and up one. + if class % 2 == 1 { + bits.put(0b1111110, 7); // height class delta -1 (B.5): -255 and 254 + bits.put(254, 8); + at -= 1; + } else if class > 0 { + bits.put(0, 1); // height class delta 1 (B.5) + at += 1; + } + bits.put(0b111111, 6); // end of the class (B.2) + bits.put(1, 5); // BMSIZE 1 (B.1) + bits.align(); + bits.put(0xFF, 8); + } + bits.put(0b1111111, 7); // height class delta, the lower line of table B.5 + bits.put(at - 1 - 256, 32); + bits.put(0b10, 2); // width delta 1 (B.2) + bits.put(0b111111, 6); // end of the class (B.2) + bits.put(0, 5); // BMSIZE 0, uncompressed (B.1) + bits.align(); + bits.put(0, 8); // its row + bits.table_b1(1); // the first export run, not exported, is all of them + let mut dictionary = vec![0x00, 0x05]; // Huffman, tables B.5, B.2, B.1, B.1 + dictionary.extend_from_slice(&0u32.to_be_bytes()); // exported + dictionary.extend_from_slice(&1u32.to_be_bytes()); // new + dictionary.extend_from_slice(&bits.bytes); + dictionary +} + +/// A Huffman-coded symbol dictionary of `symbols` white symbols of width 1 and `height` rows, none +/// exported, for `height` from 76 up. +fn narrow_jbig2_dictionary(symbols: u32, height: u32) -> Vec { + let mut bits = Bits::default(); + bits.put(0b11111, 5); // height class delta, the upper line of table B.4 + bits.put(height - 76, 32); + bits.put(0b10, 2); // width delta 1 (B.2) + for _ in 1..symbols { + bits.put(0, 1); // width delta 0 (B.2) + } + bits.put(0b111111, 6); // end of the class (B.2) + let rows = height.div_ceil(8) + 1; // an MMR-coded white bitmap: one bit a row + bits.table_b1(rows); // BMSIZE (B.1) + bits.align(); + for _ in 0..rows { + bits.put(0xFF, 8); + } + bits.table_b1(symbols); // the first export run, not exported, is all of them + let mut dictionary = vec![0x00, 0x01]; // Huffman, tables B.4, B.2, B.1, B.1 + dictionary.extend_from_slice(&0u32.to_be_bytes()); // exported + dictionary.extend_from_slice(&symbols.to_be_bytes()); // new + dictionary.extend_from_slice(&bits.bytes); + dictionary +} + +/// A Huffman-coded symbol dictionary of one white symbol of `width` columns in each height class +/// of `heights` (rising), none exported: wide symbols hold many pixels in few retained words. +fn wide_jbig2_dictionary(width: u32, heights: &[u32]) -> Vec { + let mut bits = Bits::default(); + let mut previous = 0; + for &height in heights { + match height - previous { + // Height class delta (table B.4): 1, or the upper line. + 1 => bits.put(0, 1), + delta => { + bits.put(0b11111, 5); + bits.put(delta - 76, 32); + } + } + previous = height; + bits.put(0b111110, 6); // width delta, the upper line of table B.2 + bits.put(width - 75, 32); + bits.put(0b111111, 6); // end of the class (B.2) + let rows = height.div_ceil(8) + 1; // an MMR-coded white bitmap: one bit a row + bits.table_b1(rows); // BMSIZE (B.1) + bits.align(); + for _ in 0..rows { + bits.put(0xFF, 8); + } + } + let symbols = u32::try_from(heights.len()).unwrap(); + bits.table_b1(symbols); // the first export run, not exported, is all of them + let mut dictionary = vec![0x00, 0x01]; // Huffman, tables B.4, B.2, B.1, B.1 + dictionary.extend_from_slice(&0u32.to_be_bytes()); // exported + dictionary.extend_from_slice(&symbols.to_be_bytes()); // new + dictionary.extend_from_slice(&bits.bytes); + dictionary +} + /// An intermediate generic region, white, every row typical: it decodes in a few bytes however /// large it is. fn jbig2_white_region(width: u32, height: u32) -> Vec { @@ -154,28 +303,6 @@ fn jbig2_halftone_of(side: u32, width: u32, height: u32, mmr: bool) -> Vec { halftone } -/// A Huffman-coded symbol dictionary of `symbols` symbols of width 0 and height 1, exported or not: -/// each holds no pixels, and costs a bitmap's header all the same. -fn zero_width_jbig2_dictionary(symbols: u32, exported: bool) -> Vec { - let mut dictionary = vec![0x00, 0x01]; // Huffman, tables B.4, B.2, B.1, B.1 - dictionary.extend_from_slice(&(if exported { symbols } else { 0 }).to_be_bytes()); - dictionary.extend_from_slice(&symbols.to_be_bytes()); // new - let mut bits = Bits::default(); - bits.put(0, 1); // height class delta 1 (B.4) - for _ in 0..symbols { - bits.put(0, 1); // width delta 0 (B.2) - } - bits.put(0b111111, 6); // end of the class (B.2) - bits.put(0, 5); // BMSIZE 0, uncompressed (B.1) - bits.align(); - if exported { - bits.table_b1(0); // the first export run isn't exported - } - bits.table_b1(symbols); - dictionary.extend_from_slice(&bits.bytes); - dictionary -} - /// A Huffman-coded symbol dictionary of one white symbol, `side` from 76 up, exported or not. fn square_jbig2_dictionary(side: u32, exported: bool) -> Vec { let mut bits = Bits::default(); @@ -255,6 +382,25 @@ fn jbig2_refinement_region() -> Vec { region } +/// A class of no symbols has nothing to cut from its bitmap, and rows are charged by the symbol, so +/// decoding one would be rows for nothing: a thousand classes of 65,000 rows take a second, from a +/// dictionary of 4 KB. It isn't decoded, however many there are, and the symbol after them shows that +/// its bytes are stepped over. +#[test] +fn jbig2_classes_of_no_symbols_decode_no_bitmap() { + let mut stream = black_jbig2_page(); + for number in 0..1_000 { + jbig2_segment(&mut stream, number + 1, 0, &[], &empty_class_jbig2_dictionary(1024, 65_000)); + } + let (done, finished) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let _ = done.send(jbig2_image_draws(&stream)); + }); + // This takes milliseconds now and took a quarter of an hour, so a margin this wide can't fail on + // a busy machine. + assert_eq!(finished.recv_timeout(std::time::Duration::from_secs(120)), Ok(true)); +} + /// Each item a JBIG2 stream decodes costs its work, and the stream may ask for 2^30: four of the /// largest pages, some 6 s to 17 s. Each bitmap is bounded on its own, but nothing bounded how many /// there were, so 1,140 bytes of generic regions took over 40 s, and a 795-byte PDF's one halftone @@ -363,45 +509,47 @@ fn jbig2_decoding_work_is_counted_item_by_item() { data: jbig2_pattern_dictionary(32, 2), cost: two_patterns(1, 32), }, - // Rows cost in a symbol of no pixels too. - Item { what: "symbols without pixels", before: vec![], spent: 0, kind: 0, refs: &[], data: zero_width_jbig2_dictionary(3, false), cost: 12 }, - Item { what: "a tall symbol of no width", before: vec![], spent: 0, kind: 0, refs: &[], data: zero(1000), cost: 4000 }, - Item { what: "new symbols", before: vec![], spent: 0, kind: 0, refs: &[], data: square_jbig2_dictionary(80, false), cost: 6720 }, - Item { what: "referred symbols", before: symbol(), spent: 4, kind: 0, refs: &[1], data: exporting_none, cost: 1 }, + // What an item reads counts, at 38 an integer: two classes, five widths and an export run. + Item { what: "symbols of no rows", before: vec![], spent: 0, kind: 0, refs: &[], data: no_row_jbig2_dictionary(5), cost: 304 }, + // Rows cost in a symbol of no pixels too, on top of a class, three widths and an export run. + Item { what: "symbols without pixels", before: vec![], spent: 0, kind: 0, refs: &[], data: zero_width_jbig2_dictionary(3, false), cost: 202 }, + Item { what: "a tall symbol of no width", before: vec![], spent: 0, kind: 0, refs: &[], data: zero(1000), cost: 4114 }, + Item { what: "new symbols", before: vec![], spent: 0, kind: 0, refs: &[], data: square_jbig2_dictionary(80, false), cost: 6834 }, + Item { what: "referred symbols", before: symbol(), spent: 156, kind: 0, refs: &[1], data: exporting_none, cost: 39 }, // Its symbol, and its one row and word; Huffman-coded, a table line for the symbol. - Item { what: "immediate text", before: symbol(), spent: 4, kind: 6, refs: &[1], data: jbig2_text_region(1, 1, 0), cost: 13 }, - Item { what: "intermediate text", before: symbol(), spent: 4, kind: 4, refs: &[1], data: jbig2_text_region(1, 1, 0), cost: 13 }, - Item { what: "a text region's pixels", before: symbol(), spent: 4, kind: 6, refs: &[1], data: jbig2_text_region(32, 32, 0), cost: 75 }, + Item { what: "immediate text", before: symbol(), spent: 156, kind: 6, refs: &[1], data: jbig2_text_region(1, 1, 0), cost: 13 }, + Item { what: "intermediate text", before: symbol(), spent: 156, kind: 4, refs: &[1], data: jbig2_text_region(1, 1, 0), cost: 13 }, + Item { what: "a text region's pixels", before: symbol(), spent: 156, kind: 6, refs: &[1], data: jbig2_text_region(32, 32, 0), cost: 75 }, Item { what: "an intermediate text region's pixels", before: symbol(), - spent: 4, + spent: 156, kind: 4, refs: &[1], data: jbig2_text_region(32, 32, 0), cost: 75, }, - // The region's 13, a copy of the symbol's row and the round of the loop. - Item { what: "an instance", before: symbol(), spent: 4, kind: 6, refs: &[1], data: jbig2_text_region(1, 1, 1), cost: 15 }, - // The region's 13, 80 rows of 4 words copied, the round of the loop. + // The region's 13, a copy of the symbol's row, and the instance's 4 integers. + Item { what: "an instance", before: symbol(), spent: 156, kind: 6, refs: &[1], data: jbig2_text_region(1, 1, 1), cost: 166 }, + // The region's 13, 80 rows of 4 words copied, and the instance's 4 integers. Item { what: "an instance of a large symbol", before: vec![(0, square_jbig2_dictionary(80, true))], - spent: 6720, + spent: 6872, kind: 6, refs: &[1], data: jbig2_text_region(1, 1, 1), - cost: 334, + cost: 485, }, - // And decoded as well as copied. + // And decoded as well as copied, with the 4 integers that gave its size. Item { what: "a refined instance", before: vec![(0, square_jbig2_dictionary(80, true))], - spent: 6720, + spent: 6872, kind: 6, refs: &[1], data: jbig2_refined_text_region(), - cost: 7054, + cost: 7357, }, // A bit plane, a cell's copy and the region's. Item { @@ -457,6 +605,54 @@ fn jbig2_decoding_work_is_counted_item_by_item() { } } +/// From `cargo xtask fuzz`: one changed byte in a symbol dictionary's arithmetic-coded data +/// hung the renderer. Two of its loops can stop advancing: an export run of length 0, and a +/// height class whose first width is already the end-of-class marker. Past the end of its +/// data the decoder can return those for ever. Vendored hayro-jbig2 patch: at most one height +/// class per new symbol and `2 × symbols + 1` export runs, each plus `EXTRA_EMPTY_STEPS`. +/// These synthetic dictionaries (two new symbols each, a few bytes of arbitrary data) decode +/// the same way. In the second every class after the first symbol adds 13 to the height, so it +/// only fails when the height overflows `u32`, after 330 million empty classes (about five +/// seconds; a delta of 0 would never end). The page draws it as twenty images, so that the +/// stall without the patch is well past the timeout on any machine. +#[test] +fn jbig2_symbol_dictionaries_that_never_advance_terminate() { + for (what, data, copies) in + [("zero-length export runs", &[0x05, 0x70, 0x05, 0x14, 0x02, 0x1d, 0x56, 0x3f][..], 1), ("empty height classes", &[0x06, 0x99][..], 20)] + { + // Embedded JBIG2 segments (ISO 14492 §7.2): page information, then a symbol dictionary. + let mut jbig2 = Vec::new(); + let mut segment = |number: u32, kind: u8, data: &[u8]| { + jbig2.extend_from_slice(&number.to_be_bytes()); + jbig2.extend_from_slice(&[kind, 0, 1]); + jbig2.extend_from_slice(&u32::try_from(data.len()).unwrap().to_be_bytes()); + jbig2.extend_from_slice(data); + }; + let mut page = Vec::new(); + page.extend_from_slice(&8u32.to_be_bytes()); + page.extend_from_slice(&8u32.to_be_bytes()); + page.extend_from_slice(&[0; 8]); // resolution + page.extend_from_slice(&[0, 0, 0]); // flags, striping + segment(0, 48, &page); + let mut dictionary = vec![0, 0]; // arithmetic coding, template 0, no refinement + dictionary.extend_from_slice(&[3, 0xff, 0xfd, 0xff, 2, 0xfe, 0xfe, 0xfe]); // AT pixels + dictionary.extend_from_slice(&1u32.to_be_bytes()); // exported symbols + dictionary.extend_from_slice(&2u32.to_be_bytes()); // new symbols + dictionary.extend_from_slice(data); + segment(1, 0, &dictionary); + // The stall without the patch is well past the timeout on any machine; `copies` decodes stand + // in for the images the page draws. + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + for _ in 0..copies { + let _ = black_pixels(&jbig2); + } + let _ = tx.send(()); + }); + rx.recv_timeout(std::time::Duration::from_secs(20)).unwrap_or_else(|_| panic!("{what}: a JBIG2 symbol dictionary must not stall the decoder")); + } +} + /// A JBIG2 stream can put a region, a reference or a symbol at the very end of what an `i32` holds, /// where adding to it overflowed: a panic wherever overflow is checked, a build's own tests included. /// A region or a symbol out there is off the page, so nothing draws. Vendored hayro-jbig2 patch: the arithmetic wraps, as @@ -520,3 +716,4 @@ fn jbig2_placements_at_the_end_of_an_i32_draw_nothing() { assert_eq!(ink(hex(UPPER_LINE), 16, 12), 0, "an upper range line past an i32"); assert_eq!(ink(hex(LOWER_LINE), 16, 12), 0, "a lower range line past an i32"); } + From f233df14ad38e1d97fe945e68df59706adada1f5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:21:20 -0500 Subject: [PATCH 044/120] craft-pdf: the standard security handler from PdfCraft (`crypt`) (#897) * craft-pdf: the standard security handler from PdfCraft (`crypt`) Adds the `crypt` feature: PdfCraft's pdfcraft-crypt crate (revision 2b65ab000bfe0997dec5fbed2769d3da3799355b) as craft_pdf::crypt, with its unit, integration and property tests. The code is unchanged; doc comments and test imports name the new module. It decrypts and encrypts strings and streams for revisions 2 to 6 and creates encryption dictionaries. The reader keeps its own decryptor. * craft-pdf: the crypt unit tests build for wasm The property test in src/crypt/tests.rs needs proptest, which is a dev-dependency of the non-wasm targets only. It is now compiled for those targets only; the other unit tests still build for wasm. --- Cargo.lock | 18 +- crates/pdf/Cargo.toml | 10 + crates/pdf/README.md | 31 + crates/pdf/src/crypt.rs | 796 ++++++++++++++++++ crates/pdf/src/crypt/aes_cbc.rs | 89 ++ crates/pdf/src/crypt/rc4.rs | 22 + crates/pdf/src/crypt/tests.rs | 449 ++++++++++ crates/pdf/src/lib.rs | 2 + crates/pdf/tests/crypt/main.rs | 7 + .../pdf/tests/crypt/permissions_revision.rs | 126 +++ crates/pdf/tests/crypt/proptests.rs | 464 ++++++++++ crates/pdf/tests/crypt/qpdf_literals.rs | 326 +++++++ 12 files changed, 2339 insertions(+), 1 deletion(-) create mode 100644 crates/pdf/src/crypt.rs create mode 100644 crates/pdf/src/crypt/aes_cbc.rs create mode 100644 crates/pdf/src/crypt/rc4.rs create mode 100644 crates/pdf/src/crypt/tests.rs create mode 100644 crates/pdf/tests/crypt/main.rs create mode 100644 crates/pdf/tests/crypt/permissions_revision.rs create mode 100644 crates/pdf/tests/crypt/proptests.rs create mode 100644 crates/pdf/tests/crypt/qpdf_literals.rs diff --git a/Cargo.lock b/Cargo.lock index ebce5f5ad..1e9d85eff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -926,7 +926,7 @@ version = "0.1.0" name = "craft-cms" version = "0.1.0" dependencies = [ - "md-5", + "md-5 0.10.6", "moxcms", "rayon", ] @@ -1371,6 +1371,7 @@ dependencies = [ name = "craft-pdf" version = "0.1.0" dependencies = [ + "aes", "bytemuck", "hayro", "hayro-cmap", @@ -1378,9 +1379,14 @@ dependencies = [ "hayro-syntax", "krilla", "kurbo 0.13.1", + "md-5 0.11.0", + "proptest", + "sha2 0.11.1", + "stringprep", "thiserror 2.0.21", "ttf-parser", "wasm-bindgen-test", + "zeroize", ] [[package]] @@ -4083,6 +4089,16 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "md-5" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69b6441f590336821bb897fb28fc622898ccceb1d6cea3fde5ea86b090c4de98" +dependencies = [ + "cfg-if", + "digest 0.11.3", +] + [[package]] name = "memchr" version = "2.8.3" diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index 17911b7cf..305f1f418 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -18,6 +18,7 @@ raster = ["interpret", "dep:hayro", "dep:bytemuck"] write = ["dep:krilla", "dep:thiserror"] write-pdf = ["write", "krilla/pdf"] write-simple-text = ["write", "krilla/simple-text"] +crypt = ["dep:aes", "dep:md-5", "dep:sha2", "dep:stringprep", "dep:zeroize", "dep:thiserror"] [dependencies] hayro-syntax = { workspace = true, optional = true } @@ -26,6 +27,12 @@ hayro = { workspace = true, optional = true } bytemuck = { workspace = true, optional = true } krilla = { workspace = true, optional = true } thiserror = { workspace = true, optional = true } +# The standard security handler (`crypt`): pure Rust, MIT OR Apache-2.0, builds for wasm. +aes = { version = "0.9", optional = true } +md-5 = { version = "0.11", optional = true } +sha2 = { version = "0.11", optional = true } +stringprep = { version = "0.1", optional = true } +zeroize = { version = "1", optional = true } [dev-dependencies] hayro-syntax = "=0.7.2" @@ -34,5 +41,8 @@ hayro-cmap = "=0.1.0" kurbo = "0.13" ttf-parser = "=0.25.1" +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +proptest = { workspace = true } + [target.'cfg(target_arch = "wasm32")'.dev-dependencies] wasm-bindgen-test = "0.3" diff --git a/crates/pdf/README.md b/crates/pdf/README.md index 816eba779..44af16bbf 100644 --- a/crates/pdf/README.md +++ b/crates/pdf/README.md @@ -12,6 +12,7 @@ No feature is enabled by default. | `write` | Guarded Krilla authoring; no shared reader dependency | | `write-pdf` | `write` plus Krilla's embedded-PDF support | | `write-simple-text` | `write` plus Krilla's optional text shaper and `draw_text` | +| `crypt` | The standard security handler: decrypt, encrypt and create encryption. No reader or writer dependency | ## Reading and rendering @@ -109,6 +110,36 @@ read-only consumer, then repeat with `--target wasm32-unknown-unknown` and the t These backend checks supplement the root workspace gates; excluded vendor crates are not covered by `cargo test --workspace` alone. +## Encryption + +`crypt` is the PDF standard security handler (ISO 32000-2 §7.6) for code that edits or writes +PDF files. The reader decrypts on its own and does not use it. It covers revisions 2 to 6: +40-bit and 128-bit RC4, AES-128 crypt filters, and AES-256 (R5 is read, R6 is read and written). + +- `SecurityHandler::open(dict, id0, password)` authenticates the owner password first, then the + user password. Passwords are tried as PDFDocEncoding and as typed (R2–R4), or in their + SASLprep form and as typed (R5/R6). `authenticate_owner` upgrades a user handler. + `without_key` is for a file opened without a password whose only encrypted data is behind + crypt filters authorised when an embedded file is opened; that data is left as stored. +- `decrypt_string`, `encrypt_string`, `decrypt_stream` and `encrypt_stream` take the object + number and generation. `StreamKind` selects the crypt filter: `/StmF`, `/EFF` for embedded + files, or a stream's own `/Crypt` filter name. `encrypts_metadata` says whether the caller + passes `/Metadata` streams through at all. AES decryption is tolerant: a trailing partial + block is ignored and bad padding is left in. +- `permissions()` gives the `/P` flags; for R5/R6 a permission counts only when `/Perms` grants + it too. Owners have every permission. +- `create(&NewEncryption, id0)` builds the `/Encrypt` values for RC4-40, RC4-128, AES-128 or + AES-256. The caller supplies 32 bytes of entropy (`seed`), so the module needs no OS access + and builds for the web. `file_id` derives a file identifier from the same seed. +- `EncryptDict` holds the dictionary's values. The caller reads them from, and writes them to, + its own object model; the module does not parse PDF syntax. Keys and prepared passwords are + zeroized when dropped. + +The code and its tests come from PdfCraft's `pdfcraft-crypt` crate (revision +`2b65ab000bfe0997dec5fbed2769d3da3799355b`), unchanged apart from the module paths. Tests: +`cargo test -p craft-pdf --no-default-features --features crypt`. `tests/crypt/qpdf_literals.rs` +checks key derivation against values another implementation wrote; only the numbers are stored. + ## Writing Plain `write` retains Krilla raster images without its optional text shaper. Applications diff --git a/crates/pdf/src/crypt.rs b/crates/pdf/src/crypt.rs new file mode 100644 index 000000000..7f7a51e09 --- /dev/null +++ b/crates/pdf/src/crypt.rs @@ -0,0 +1,796 @@ +//! The PDF standard security handler (ISO 32000-2 §7.6), for code that writes or edits PDF files. +//! The reader (`read`) decrypts on its own; this module also encrypts and creates encryption. +//! +//! Supports every revision of the standard handler: +//! - R2 (40-bit RC4), R3 (RC4 up to 128-bit), R4 (crypt filters: RC4 or AES-128); +//! - R5 (the deprecated AES-256 extension) and R6 (AES-256, PDF 2.0). +//! +//! It authenticates with the user or owner password (owner first, as viewers do) and derives +//! per-object keys. It decrypts and encrypts strings and streams, and honours crypt filters, +//! including `Identity` and embedded-file-only encryption. It can also *create* encryption +//! dictionaries, for an app's security settings and for test fixtures. +//! +//! The module knows nothing about a PDF object model. Callers pass in the values of the +//! `/Encrypt` dictionary and the first element of the trailer `/ID`. + +mod aes_cbc; +mod rc4; + +use md5::{Digest as _, Md5}; +use sha2::{Sha256, Sha384, Sha512}; +use zeroize::Zeroizing; + +pub use aes_cbc::{aes_cbc_decrypt, aes_cbc_encrypt}; +pub use rc4::rc4; + +#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)] +pub enum CryptError { + #[error("the password is incorrect")] + WrongPassword, + #[error("unsupported security handler: {0}")] + Unsupported(String), + #[error("malformed encryption dictionary: {0}")] + Malformed(String), +} + +/// 32-byte padding string used by revisions 2–4 (§7.6.4.3.2, step a). +pub const PADDING: [u8; 32] = [ + 0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41, 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08, 0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80, + 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A, +]; + +/// How a crypt filter encrypts (`/CFM`). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Method { + /// `None` / the `Identity` filter: data is not encrypted. + Identity, + /// `V2`: RC4. + Rc4, + /// `AESV2`: AES-128-CBC. + Aes128, + /// `AESV3`: AES-256-CBC. + Aes256, +} + +/// The values of an `/Encrypt` dictionary that the handler needs. +#[derive(Clone, Debug, PartialEq, Default)] +pub struct EncryptDict { + /// `/Filter` (must be `Standard`). + pub filter: Vec, + pub v: i64, + pub r: i64, + /// Key length in bits (`/Length`; 40 when absent). + pub length_bits: i64, + pub o: Vec, + pub u: Vec, + pub oe: Vec, + pub ue: Vec, + pub perms: Vec, + pub p: i32, + pub encrypt_metadata: bool, + /// Crypt filters (`/CF`): name → (method, key length in bytes if given). + pub crypt_filters: Vec<(Vec, Method)>, + pub stm_f: Vec, + pub str_f: Vec, + pub ef_f: Vec, +} + +/// Which password authenticated the document. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Auth { + Owner, + User, +} + +/// Permission flags from `/P`, read the way the handler's revision reads them (ISO 32000-1:2008, +/// Table 22). Owners have every permission. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Permissions { + pub bits: i32, + pub owner: bool, + /// The `/R` value of the handler that read `bits`. + pub revision: i64, +} + +impl Permissions { + fn bit(&self, n: u32) -> bool { + self.owner || (self.bits >> (n - 1)) & 1 == 1 + } + /// Revision 3 and later read the full flag list of Table 22; revision 2 has its own. + fn is_revision_3_or_later(&self) -> bool { + self.revision >= 3 + } + pub fn print(&self) -> bool { + self.bit(3) + } + /// Modify contents other than the operations below. + pub fn modify(&self) -> bool { + self.bit(4) + } + pub fn copy(&self) -> bool { + self.bit(5) + } + pub fn annotate(&self) -> bool { + self.bit(6) + } + pub fn fill_forms(&self) -> bool { + if self.is_revision_3_or_later() { self.bit(9) || self.bit(6) } else { self.bit(6) } + } + /// Revision 2 covers accessibility with the copy flag (bit 5); revision 3 and later with bit 10. + pub fn extract_for_accessibility(&self) -> bool { + if self.is_revision_3_or_later() { self.bit(10) } else { self.bit(5) } + } + /// Insert, rotate or delete pages; create bookmarks and thumbnails. Revision 3 and later grant + /// it by bit 11 alone. Table 22 defines bit 11 for those revisions only, so at revision 2 the + /// modify flag (bit 4) covers it. + pub fn assemble(&self) -> bool { + if self.is_revision_3_or_later() { self.bit(11) } else { self.bit(4) } + } + /// Take pages out into another document (Extract, Split, a source of Combine or Insert + /// Pages): [`Self::assemble`] and copying (bit 5), since the pages carry their text and + /// graphics into a new file without the document's protection (#5081). "Any except + /// extracting pages" sets bit 4 but not bit 11, so at revision 3 and later it grants neither. + pub fn extract_pages(&self) -> bool { + self.assemble() && self.copy() + } + /// Revision 3 and later need bit 3 as well as bit 12. Table 22 defines bit 12 for those + /// revisions only: at revision 2 the print flag (bit 3) is all there is. + pub fn print_high_quality(&self) -> bool { + if self.is_revision_3_or_later() { self.print() && self.bit(12) } else { self.print() } + } +} + +/// What kind of stream is being decrypted (selects the crypt filter). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StreamKind<'a> { + Normal, + /// An embedded file stream (uses `/EFF`). + EmbeddedFile, + /// The stream names its own crypt filter (`/Filter [/Crypt …] /DecodeParms << /Name … >>`). + Named(&'a [u8]), +} + +/// An authenticated security handler for one document. +#[derive(Clone, Debug)] +pub struct SecurityHandler { + dict: EncryptDict, + key: Zeroizing>, + auth: Auth, + recovered_user: Option>>, + methods: Methods, +} + +/// The crypt filter methods of a handler, resolved once: a document controls both the number of +/// filters and the number of strings, so a search of `/CF` per string would multiply them. +#[derive(Clone, Debug)] +struct Methods { + string: Method, + stream: Method, + embedded_file: Method, + named: std::collections::HashMap, Method>, +} + +impl Methods { + fn new(dict: &EncryptDict) -> Self { + let mut named = std::collections::HashMap::with_capacity(dict.crypt_filters.len()); + for (name, method) in &dict.crypt_filters { + #[cfg(test)] + tests::FILTER_COMPARES.with(|c| c.set(c.get() + 1)); + // The first of two filters with one name wins, as a search would find it. + named.entry(name.clone()).or_insert(*method); + } + let mut methods = Methods { string: Method::Identity, stream: Method::Identity, embedded_file: Method::Identity, named }; + methods.string = methods.get(&dict.str_f); + methods.stream = methods.get(&dict.stm_f); + methods.embedded_file = methods.get(if dict.ef_f.is_empty() { &dict.stm_f } else { &dict.ef_f }); + methods + } + + fn get(&self, name: &[u8]) -> Method { + if name == b"Identity" || name.is_empty() { + return Method::Identity; + } + #[cfg(test)] + tests::FILTER_COMPARES.with(|c| c.set(c.get() + 1)); + self.named.get(name).copied().unwrap_or(Method::Identity) + } +} + +impl SecurityHandler { + fn new(dict: EncryptDict, key: Zeroizing>, auth: Auth, recovered_user: Option>>) -> Self { + let methods = Methods::new(&dict); + Self { dict, key, auth, recovered_user, methods } + } + + /// Authenticate with `password` (tried as owner password, then user password). `None` + /// tries the empty password, which opens documents that only have an owner password. + pub fn open(dict: EncryptDict, id0: &[u8], password: Option<&str>) -> Result { + if !dict.filter.is_empty() && dict.filter != b"Standard" { + return Err(CryptError::Unsupported(format!("/Filter /{}", String::from_utf8_lossy(&dict.filter)))); + } + let pw = password.unwrap_or(""); + let mut recovered_user = None; + let (key, auth) = match dict.r { + 2..=4 => { + let mut found = None; + for legacy in legacy_candidates(pw) { + if let Some((k, user)) = owner_key_r4(&dict, id0, &legacy) { + recovered_user = Some(user); + found = Some((k, Auth::Owner)); + } else if let Some(k) = user_key_r4(&dict, id0, &legacy) { + found = Some((k, Auth::User)); + } + if found.is_some() { + break; + } + } + found.ok_or(CryptError::WrongPassword)? + } + 5 | 6 => { + let pw = sasl_password(pw); + if let Some(k) = key_r6(&dict, &pw, true)? { + (k, Auth::Owner) + } else if let Some(k) = key_r6(&dict, &pw, false)? { + (k, Auth::User) + } else { + return Err(CryptError::WrongPassword); + } + } + r => return Err(CryptError::Unsupported(format!("revision {r}"))), + }; + Ok(Self::new(dict, key, auth, recovered_user)) + } + + /// A handler with no file key, for a document opened without a password whose strings and + /// streams aren't encrypted and whose encrypted crypt filters are all ones the caller found + /// authorised only when an embedded file is opened (`/AuthEvent /EFOpen`, ISO 32000-2 §7.6.6, + /// Table 25). Data such a filter encrypts is left as stored, read and written alike, so it + /// keeps its encrypted bytes; [`SecurityHandler::decrypts`] tells which. `None` when strings or + /// streams are encrypted, which needs the key, or the handler is older than crypt filters (V4). + pub fn without_key(dict: EncryptDict) -> Option { + if dict.v < 4 { + return None; + } + let methods = Methods::new(&dict); + if methods.string != Method::Identity || methods.stream != Method::Identity { + return None; + } + Some(Self { dict, key: Zeroizing::new(Vec::new()), auth: Auth::User, recovered_user: None, methods }) + } + + /// Whether the handler has the file key: false for [`SecurityHandler::without_key`]. + pub fn has_key(&self) -> bool { + !self.key.is_empty() + } + + /// Whether data of `kind` is encrypted, so that reading it needs the file key. + pub fn decrypts(&self, kind: StreamKind) -> bool { + self.method(Some(kind)) != Method::Identity + } + + /// Whether data left as stored by a handler without the key still decrypts after its object + /// is renumbered: only AES-256 doesn't mix the object number into the key (Algorithm 1.A). + pub fn survives_renumbering(&self) -> bool { + [self.methods.string, self.methods.stream, self.methods.embedded_file] + .into_iter() + .chain(self.methods.named.values().copied()) + .all(|m| matches!(m, Method::Identity | Method::Aes256)) + } + + /// Upgrade to owner access with the owner (permissions) password, as when a restricted + /// document is unlocked after opening. The user password, or any other, is `WrongPassword`. + /// Both passwords unlock the same file key, so what was decrypted so far stays valid; a + /// different key is refused too. + pub fn authenticate_owner(&self, id0: &[u8], password: &str) -> Result { + let owner = Self::open(self.dict.clone(), id0, Some(password))?; + if owner.auth != Auth::Owner || owner.key != self.key { + return Err(CryptError::WrongPassword); + } + Ok(owner) + } + + pub fn auth(&self) -> Auth { + self.auth + } + + /// For R2–R4 documents opened with the owner password: the user password, which owner + /// authentication recovers (Algorithm 7). Lets components that only accept user passwords + /// open the document too. + pub fn recovered_user_password(&self) -> Option>> { + self.recovered_user.clone() + } + + /// For R5/R6, `/P` is stored in clear and again in `/Perms`, encrypted with the file key + /// (Algorithm 13). Where the two differ, a permission counts only when both grant it, so an + /// edited `/P` grants nothing extra. Without a readable `/Perms`, `/P` applies as before. + pub fn permissions(&self) -> Permissions { + let bits = self.perms_p().map_or(self.dict.p, |p| self.dict.p & p); + Permissions { bits, owner: self.auth == Auth::Owner, revision: self.dict.r } + } + + /// `/P` as `/Perms` holds it: AES-256 ECB of one block (= CBC with a zero IV), marked `adb`. + fn perms_p(&self) -> Option { + if !matches!(self.dict.r, 5 | 6) || !self.has_key() { + return None; + } + let plain = Zeroizing::new(aes_cbc_decrypt(&self.key, &[0; 16], self.dict.perms.first_chunk::<16>()?, false)); + let p = i32::from_le_bytes(*plain.first_chunk::<4>()?); + (plain.get(9..12) == Some(&b"adb"[..])).then_some(p) + } + + pub fn dict(&self) -> &EncryptDict { + &self.dict + } + + /// The file encryption key (for tests and diagnostics). + pub fn file_key(&self) -> &[u8] { + &self.key + } + + /// Whether `/Metadata` streams are encrypted. + pub fn encrypts_metadata(&self) -> bool { + self.dict.encrypt_metadata + } + + /// The method for strings or a kind of stream. + fn method(&self, stream: Option) -> Method { + if self.dict.v < 4 { + return Method::Rc4; + } + match stream { + None => self.methods.string, + Some(StreamKind::Normal) => self.methods.stream, + Some(StreamKind::EmbeddedFile) => self.methods.embedded_file, + Some(StreamKind::Named(n)) => self.methods.get(n), + } + } + + /// Algorithm 1 (§7.6.3.2): the key for one object. AES-256 uses the file key directly + /// (Algorithm 1.A, §7.6.3.3). + /// + /// For an RC4 crypt filter under `/V 4`, a file key shorter than 16 bytes is first extended + /// to 16 bytes with zeros. The standard doesn't say so (Algorithm 1 takes the n-byte key as + /// it is), but that is how Acrobat writes and reads such files: a `/V 4` RC4 crypt filter + /// with a 40- or 48-bit key (pdf.js issue 19484). `hayro-syntax` (the `read` feature) reads them + /// the same way, so a page it shows is one this module can read and save. AES-128 keys are + /// left as they are: `AESV2` takes a 128-bit key (Table 25), and Acrobat's extension is only + /// seen with RC4. Every key [`create`] makes under `/V 4` is 16 bytes already. + fn object_key(&self, method: Method, num: u32, generation: u16) -> Zeroizing> { + if method == Method::Aes256 { + return self.key.clone(); + } + let mut key = self.key.clone(); + if method == Method::Rc4 && self.dict.v == 4 && key.len() < 16 { + key.resize(16, 0); + } + let mut h = Md5::new(); + h.update(&key); + h.update(&num.to_le_bytes()[..3]); + h.update(generation.to_le_bytes()); + if method == Method::Aes128 { + h.update(b"sAlT"); + } + let mut digest = Zeroizing::new(h.finalize().to_vec()); + digest.truncate((key.len() + 5).min(16)); + digest + } + + fn apply(&self, method: Method, num: u32, generation: u16, data: &[u8], encrypt: bool) -> Vec { + // Without the key, encrypted data stays as stored (see `without_key`). + if !self.has_key() { + return data.to_vec(); + } + match method { + Method::Identity => data.to_vec(), + Method::Rc4 => rc4(&self.object_key(method, num, generation), data), + Method::Aes128 | Method::Aes256 => { + let key = self.object_key(method, num, generation); + if encrypt { + // Deterministic IV derived from the key, object and content: saves are + // reproducible and no IV repeats for different plaintexts. + let mut h = Sha256::new(); + h.update(&key); + h.update(num.to_le_bytes()); + h.update(generation.to_le_bytes()); + h.update(data); + let digest = h.finalize(); + let mut iv = [0u8; 16]; + for (d, s) in iv.iter_mut().zip(digest.iter()) { + *d = *s; + } + let mut out = iv.to_vec(); + out.extend(aes_cbc_encrypt(&key, &iv, data, true)); + out + } else { + let Some((iv, body)) = data.split_first_chunk::<16>() else { + return Vec::new(); // only (part of) an IV: empty plaintext + }; + aes_cbc_decrypt(&key, iv, body, true) + } + } + } + } + + pub fn decrypt_string(&self, num: u32, generation: u16, data: &[u8]) -> Vec { + self.apply(self.method(None), num, generation, data, false) + } + + pub fn encrypt_string(&self, num: u32, generation: u16, data: &[u8]) -> Vec { + self.apply(self.method(None), num, generation, data, true) + } + + pub fn decrypt_stream(&self, num: u32, generation: u16, data: &[u8], kind: StreamKind) -> Vec { + self.apply(self.method(Some(kind)), num, generation, data, false) + } + + pub fn encrypt_stream(&self, num: u32, generation: u16, data: &[u8], kind: StreamKind) -> Vec { + self.apply(self.method(Some(kind)), num, generation, data, true) + } + + /// Whether strings are encrypted at all (false with `/StrF /Identity`). + pub fn encrypts_strings(&self) -> bool { + self.method(None) != Method::Identity + } +} + +// ── Passwords ────────────────────────────────────────────────────────────────────────────────── + +/// PDFDocEncoding (ISO 32000-2 Annex D) where it differs from Latin-1: bytes 0x18–0x1F and +/// 0x80–0xA0 (0x9F is undefined). Here because R2–R4 passwords need it; an object layer that +/// decodes text strings can use it too. +const PDFDOC_LOW: [char; 8] = ['˘', 'ˇ', 'ˆ', '˙', '˝', '˛', '˚', '˜']; +const PDFDOC_HIGH: [char; 33] = [ + '•', '†', '‡', '…', '—', '–', 'ƒ', '⁄', '‹', '›', '−', '‰', '„', '“', '”', '‘', '’', '‚', '™', 'fi', 'fl', 'Ł', 'Œ', 'Š', 'Ÿ', 'Ž', 'ı', 'ł', 'œ', + 'š', 'ž', '\u{FFFD}', '€', +]; + +/// PDFDocEncoding → Unicode. +pub fn pdfdoc_char(c: u8) -> char { + match c { + 0x18..=0x1F => PDFDOC_LOW[usize::from(c - 0x18)], + 0x80..=0xA0 => PDFDOC_HIGH[usize::from(c - 0x80)], + _ => char::from(c), + } +} + +/// Unicode → PDFDocEncoding, `None` for a character it lacks (ğ, ş, İ, Cyrillic, CJK, …). +pub fn pdfdoc_byte(c: char) -> Option { + let at = |table: &[char], base: u8| table.iter().position(|&t| t == c && t != '\u{FFFD}').and_then(|i| u8::try_from(i).ok()).map(|i| base + i); + at(&PDFDOC_LOW, 0x18).or_else(|| at(&PDFDOC_HIGH, 0x80)).or_else(|| match u32::from(c) { + 0x18..=0x1F | 0x80..=0xA0 => None, + n => u8::try_from(n).ok(), + }) +} + +/// Revisions 2–4: the password in PDFDocEncoding (§7.6.4.3.2, Algorithm 2), at most 32 bytes. +/// Characters PDFDocEncoding lacks are dropped. +fn legacy_password(pw: &str) -> Zeroizing> { + Zeroizing::new(pw.chars().filter_map(pdfdoc_byte).take(32).collect()) +} + +/// The bytes an R2–R4 password may have been written as: PDFDocEncoding, as the specification +/// asks; and for a password with characters PDFDocEncoding lacks (ğ, ş, İ, …), which it gives +/// no bytes, also its UTF-8 bytes, which qpdf and other tools write for such passwords. +fn legacy_candidates(pw: &str) -> Vec>> { + let mut out = vec![legacy_password(pw)]; + if pw.chars().any(|c| pdfdoc_byte(c).is_none()) { + out.push(Zeroizing::new(pw.bytes().take(32).collect())); + } + out +} + +/// Revisions 5–6: SASLprep (RFC 4013), UTF-8, at most 127 bytes. +fn sasl_password(pw: &str) -> Zeroizing> { + let prepped = Zeroizing::new(stringprep::saslprep(pw).map(|c| c.into_owned()).unwrap_or_else(|_| pw.to_string())); + let mut bytes = Zeroizing::new(prepped.as_bytes().to_vec()); + bytes.truncate(127); + bytes +} + +fn pad(pw: &[u8]) -> Zeroizing<[u8; 32]> { + let mut out = Zeroizing::new(PADDING); + let n = pw.len().min(32); + out[..n].copy_from_slice(&pw[..n]); + out[n..].copy_from_slice(&PADDING[..32 - n]); + out +} + +fn key_len(d: &EncryptDict) -> usize { + if d.r == 2 { 5 } else { (d.length_bits.clamp(40, 128) / 8) as usize } +} + +// ── Revisions 2–4 ────────────────────────────────────────────────────────────────────────────── + +/// Algorithm 2: file key from a (user) password. +fn file_key_r4(d: &EncryptDict, id0: &[u8], pw: &[u8]) -> Zeroizing> { + let n = key_len(d); + let mut h = Md5::new(); + h.update(*pad(pw)); + h.update(&d.o[..d.o.len().min(32)]); + h.update(d.p.to_le_bytes()); + h.update(id0); + if d.r >= 4 && !d.encrypt_metadata { + h.update([0xFF; 4]); + } + let mut digest = Zeroizing::new(h.finalize().to_vec()); + if d.r >= 3 { + for _ in 0..50 { + digest = Zeroizing::new(Md5::digest(&digest[..n]).to_vec()); + } + } + digest.truncate(n); + digest +} + +/// Algorithms 4/5: the `/U` value a key produces. +fn u_value_r4(d: &EncryptDict, id0: &[u8], key: &[u8]) -> Vec { + if d.r == 2 { + return rc4(key, &PADDING); + } + let mut h = Md5::new(); + h.update(PADDING); + h.update(id0); + let mut x = rc4(key, &h.finalize()); + for i in 1..=19u8 { + let k = Zeroizing::new(key.iter().map(|b| b ^ i).collect::>()); + x = rc4(&k, &x); + } + x.extend_from_slice(&[0u8; 16]); + x +} + +/// Algorithm 6: authenticate a user password; returns the file key. +/// +/// Some producers ignore `/EncryptMetadata false` when deriving the key (they omit the +/// `FF FF FF FF` of Algorithm 2, step f). Viewers open those files, so if the strict derivation +/// fails we retry the other way. +fn user_key_r4(d: &EncryptDict, id0: &[u8], pw: &[u8]) -> Option>> { + let check = |d: &EncryptDict| { + let key = file_key_r4(d, id0, pw); + let u = u_value_r4(d, id0, &key); + let n = if d.r == 2 { 32 } else { 16 }; + (d.u.len() >= n && u[..n] == d.u[..n]).then_some(key) + }; + check(d).or_else(|| (d.r >= 4 && !d.encrypt_metadata).then(|| check(&EncryptDict { encrypt_metadata: true, ..d.clone() })).flatten()) +} + +/// The RC4 key derived from an owner password (Algorithm 3, steps a–d). +fn owner_rc4_key(d: &EncryptDict, owner_pw: &[u8]) -> Zeroizing> { + let n = key_len(d); + let mut digest = Zeroizing::new(Md5::digest(*pad(owner_pw)).to_vec()); + if d.r >= 3 { + for _ in 0..50 { + digest = Zeroizing::new(Md5::digest(&*digest).to_vec()); + } + } + digest.truncate(n); + digest +} + +type SecretBytes = Zeroizing>; + +/// Algorithm 7: authenticate an owner password (recover the user password from `/O`). +/// Returns the file key and the recovered user password (padding removed). +fn owner_key_r4(d: &EncryptDict, id0: &[u8], pw: &[u8]) -> Option<(SecretBytes, SecretBytes)> { + let rk = owner_rc4_key(d, pw); + let mut user = Zeroizing::new(d.o[..d.o.len().min(32)].to_vec()); + if d.r == 2 { + user = Zeroizing::new(rc4(&rk, &user)); + } else { + for i in (0..=19u8).rev() { + let k = Zeroizing::new(rk.iter().map(|b| b ^ i).collect::>()); + user = Zeroizing::new(rc4(&k, &user)); + } + } + let key = user_key_r4(d, id0, &user)?; + // The recovered value is the padded password: strip the padding suffix. + let len = (0..=32).find(|&i| user.len() >= 32 && user[i..32] == PADDING[..32 - i]).unwrap_or(user.len().min(32)); + user.truncate(len); + Some((key, user)) +} + +// ── Revisions 5–6 ────────────────────────────────────────────────────────────────────────────── + +/// The hash of a password for R5 (SHA-256) or R6 (Algorithm 2.B). +fn hash_r6(r: i64, pw: &[u8], salt: &[u8], udata: &[u8]) -> Zeroizing> { + let mut k = Zeroizing::new({ + let mut h = Sha256::new(); + h.update(pw); + h.update(salt); + h.update(udata); + h.finalize().to_vec() + }); + if r == 5 { + return k; + } + let mut round = 0u32; + loop { + let mut k1 = Zeroizing::new(Vec::with_capacity(64 * (pw.len() + k.len() + udata.len()))); + for _ in 0..64 { + k1.extend_from_slice(pw); + k1.extend_from_slice(&k); + k1.extend_from_slice(udata); + } + // `k` is always a SHA-2 digest (32, 48 or 64 bytes): key = its first 16 bytes, IV = the next 16. + let Some((key, rest)) = k.split_first_chunk::<16>() else { break }; + let Some(iv) = rest.first_chunk::<16>() else { break }; + let e = Zeroizing::new(aes_cbc_encrypt(key, iv, &k1, false)); + let m = e.iter().take(16).map(|b| u32::from(*b)).sum::() % 3; + k = Zeroizing::new(match m { + 0 => Sha256::digest(&*e).to_vec(), + 1 => Sha384::digest(&*e).to_vec(), + _ => Sha512::digest(&*e).to_vec(), + }); + round += 1; + if round >= 64 && e.last().is_none_or(|b| u32::from(*b) <= round - 32) { + break; + } + } + k.truncate(32); + k +} + +/// Algorithm 2.A: authenticate as owner (`owner = true`) or user; returns the file key. +fn key_r6(d: &EncryptDict, pw: &[u8], owner: bool) -> Result>>, CryptError> { + if d.u.len() < 48 || d.o.len() < 48 { + return Err(CryptError::Malformed("/O and /U must be 48 bytes for R5/R6".into())); + } + let (hashed, encrypted, udata) = if owner { (&d.o[..48], &d.oe, &d.u[..48]) } else { (&d.u[..48], &d.ue, &[][..]) }; + if *hash_r6(d.r, pw, &hashed[32..40], udata) != hashed[..32] { + return Ok(None); + } + if encrypted.len() < 32 { + return Err(CryptError::Malformed("/OE or /UE must be 32 bytes".into())); + } + let k = hash_r6(d.r, pw, &hashed[40..48], udata); + Ok(Some(Zeroizing::new(aes_cbc_decrypt(&k, &[0; 16], &encrypted[..32], false)))) +} + +// ── Creating encryption ──────────────────────────────────────────────────────────────────────── + +/// Which encryption to create. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Algorithm { + /// R2, 40-bit RC4 (legacy; for tests and compatibility only). + Rc4_40, + /// R3, 128-bit RC4. + Rc4_128, + /// R4, AES-128 crypt filters. + Aes128, + /// R6, AES-256 (PDF 2.0; the default for new documents). + Aes256, +} + +/// A password as [`create`] prepares it: for R2–R4 its PDFDocEncoding (characters it lacks +/// dropped), at most 32 bytes; for R6 its SASLprep (RFC 4013: soft hyphens and the like removed, +/// other spaces made plain, then NFKC; the password as given if SASLprep refuses it), in UTF-8, +/// at most 127 bytes. +fn prepared_password(algorithm: Algorithm, password: &str) -> Zeroizing> { + match algorithm { + Algorithm::Rc4_40 | Algorithm::Rc4_128 | Algorithm::Aes128 => legacy_password(password), + Algorithm::Aes256 => sasl_password(password), + } +} + +/// The bytes new encryption with `algorithm` derives its keys from for `password`: for R2–R4 its +/// PDFDocEncoding cut or padded to exactly 32 bytes (Algorithm 2); for R6 its SASLprep (the +/// password as given if SASLprep refuses it), at most 127 bytes of UTF-8. Two passwords with the +/// same bytes are the same password to the file, and a password with the bytes of `""` opens it +/// without asking, so callers can refuse either before encrypting. Different bytes can still give +/// the same key (RC4-40 keeps 5 bytes of a hash). `create` still uses the user password for an +/// empty R2–R4 owner password. +pub fn effective_password(algorithm: Algorithm, password: &str) -> Vec { + let prepared = prepared_password(algorithm, password); + match algorithm { + Algorithm::Rc4_40 | Algorithm::Rc4_128 | Algorithm::Aes128 => pad(&prepared).to_vec(), + Algorithm::Aes256 => prepared.to_vec(), + } +} + +/// Parameters for new encryption. +#[derive(Clone, Debug)] +pub struct NewEncryption<'a> { + pub algorithm: Algorithm, + pub user_password: &'a str, + pub owner_password: &'a str, + pub permissions: i32, + pub encrypt_metadata: bool, + /// 32 bytes of entropy for keys and salts (the caller supplies randomness, so this crate + /// needs no OS access and tests are deterministic). + pub seed: [u8; 32], +} + +/// Expand `seed` into `n` bytes (SHA-256 in counter mode). +fn expand(seed: &[u8; 32], label: &[u8], n: usize) -> Vec { + let mut out = Vec::new(); + let mut counter = 0u32; + while out.len() < n { + let mut h = Sha256::new(); + h.update(seed); + h.update(label); + h.update(counter.to_le_bytes()); + out.extend_from_slice(&h.finalize()); + counter = counter.saturating_add(1); + } + out.truncate(n); + out +} + +/// Sixteen bytes of file identifier from `seed` (SHA-256 in counter mode, label `id`). +/// +/// `/ID` is written in the clear. Deriving it the same way as the file key keeps that public +/// value from being an unkeyed hash of the seed. +pub fn file_id(seed: &[u8; 32]) -> Vec { + expand(seed, b"id", 16) +} + +/// Build the `/Encrypt` values and an authenticated (owner) handler for a new encryption. +pub fn create(params: &NewEncryption, id0: &[u8]) -> Result { + // Bits 7–8 and 13–32 must be 1; bits 1–2 must be 0 (§7.6.4.2). + let p = (params.permissions | !0xFFF | 0xC0) & !0x3; + let mut d = EncryptDict { filter: b"Standard".to_vec(), p, encrypt_metadata: params.encrypt_metadata, ..Default::default() }; + let user = prepared_password(params.algorithm, params.user_password); + let owner = prepared_password(params.algorithm, params.owner_password); + match params.algorithm { + Algorithm::Rc4_40 | Algorithm::Rc4_128 | Algorithm::Aes128 => { + (d.v, d.r, d.length_bits) = match params.algorithm { + Algorithm::Rc4_40 => (1, 2, 40), + Algorithm::Rc4_128 => (2, 3, 128), + _ => (4, 4, 128), + }; + if params.algorithm == Algorithm::Aes128 { + d.crypt_filters = vec![(b"StdCF".to_vec(), Method::Aes128)]; + d.stm_f = b"StdCF".to_vec(); + d.str_f = b"StdCF".to_vec(); + } else { + d.encrypt_metadata = true; // V < 4 always encrypts metadata + } + let owner = if params.owner_password.is_empty() { user.clone() } else { owner }; + // Algorithm 3: /O + let rk = owner_rc4_key(&d, &owner); + let mut o = rc4(&rk, &*pad(&user)); + if d.r >= 3 { + for i in 1..=19u8 { + let k = Zeroizing::new(rk.iter().map(|b| b ^ i).collect::>()); + o = rc4(&k, &o); + } + } + d.o = o; + let key = file_key_r4(&d, id0, &user); + d.u = u_value_r4(&d, id0, &key); + Ok(SecurityHandler::new(d, key, Auth::Owner, Some(user))) + } + Algorithm::Aes256 => { + (d.v, d.r, d.length_bits) = (5, 6, 256); + d.crypt_filters = vec![(b"StdCF".to_vec(), Method::Aes256)]; + d.stm_f = b"StdCF".to_vec(); + d.str_f = b"StdCF".to_vec(); + let key = Zeroizing::new(expand(¶ms.seed, b"file key", 32)); + let salts = expand(¶ms.seed, b"salts", 32); + // Algorithm 8: /U, /UE + let mut u = hash_r6(6, &user, &salts[0..8], &[]).to_vec(); + u.extend_from_slice(&salts[0..16]); + let uk = hash_r6(6, &user, &salts[8..16], &[]); + d.ue = aes_cbc_encrypt(&uk, &[0; 16], &key, false); + // Algorithm 9: /O, /OE + let mut o = hash_r6(6, &owner, &salts[16..24], &u).to_vec(); + o.extend_from_slice(&salts[16..32]); + let ok = hash_r6(6, &owner, &salts[24..32], &u); + d.oe = aes_cbc_encrypt(&ok, &[0; 16], &key, false); + d.u = u; + d.o = o; + // Algorithm 10: /Perms + let mut perms = [0u8; 16]; + perms[..4].copy_from_slice(&p.to_le_bytes()); + perms[4..8].copy_from_slice(&[0xFF; 4]); + perms[8] = if d.encrypt_metadata { b'T' } else { b'F' }; + perms[9..12].copy_from_slice(b"adb"); + perms[12..16].copy_from_slice(&expand(¶ms.seed, b"perms", 4)); + d.perms = aes_cbc_encrypt(&key, &[0; 16], &perms, false); + Ok(SecurityHandler::new(d, key, Auth::Owner, None)) + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/pdf/src/crypt/aes_cbc.rs b/crates/pdf/src/crypt/aes_cbc.rs new file mode 100644 index 000000000..5d958f4f3 --- /dev/null +++ b/crates/pdf/src/crypt/aes_cbc.rs @@ -0,0 +1,89 @@ +//! AES-CBC (128 or 256-bit key, chosen by key length) with optional PKCS#5 padding. +//! +//! Decryption is tolerant, as viewers are: a trailing partial block is ignored, and invalid +//! padding leaves the data unpadded rather than failing. + +use aes::cipher::{Array, BlockCipherDecrypt, BlockCipherEncrypt, KeyInit}; +use aes::{Aes128, Aes256}; + +enum Cipher { + A128(Box), + A256(Box), +} + +impl Cipher { + fn new(key: &[u8]) -> Self { + if let Some(k) = key.first_chunk::<32>() { + Cipher::A256(Box::new(Aes256::new(&Array::from(*k)))) + } else { + let mut k = [0u8; 16]; + let n = key.len().min(16); + k[..n].copy_from_slice(&key[..n]); + Cipher::A128(Box::new(Aes128::new(&Array::from(k)))) + } + } + + fn encrypt(&self, block: &mut [u8; 16]) { + let mut b = Array::from(*block); + match self { + Cipher::A128(c) => c.encrypt_block(&mut b), + Cipher::A256(c) => c.encrypt_block(&mut b), + } + *block = b.into(); + } + + fn decrypt(&self, block: &mut [u8; 16]) { + let mut b = Array::from(*block); + match self { + Cipher::A128(c) => c.decrypt_block(&mut b), + Cipher::A256(c) => c.decrypt_block(&mut b), + } + *block = b.into(); + } +} + +/// Encrypt; with `pad`, PKCS#5 padding is added (otherwise `data` must be whole blocks, and a +/// trailing partial block is zero-padded). +pub fn aes_cbc_encrypt(key: &[u8], iv: &[u8; 16], data: &[u8], pad: bool) -> Vec { + let c = Cipher::new(key); + let mut buf = data.to_vec(); + if pad { + let n = 16 - buf.len() % 16; + buf.extend(std::iter::repeat_n(n as u8, n)); + } else if !buf.len().is_multiple_of(16) { + buf.resize(buf.len().div_ceil(16) * 16, 0); + } + let mut prev = *iv; + for b in buf.as_chunks_mut::<16>().0 { + for (x, p) in b.iter_mut().zip(prev) { + *x ^= p; + } + c.encrypt(b); + prev = *b; + } + buf +} + +/// Decrypt; with `pad`, PKCS#5 padding is removed when valid. +pub fn aes_cbc_decrypt(key: &[u8], iv: &[u8; 16], data: &[u8], pad: bool) -> Vec { + let c = Cipher::new(key); + let blocks = data.as_chunks::<16>().0; + let mut out = Vec::with_capacity(blocks.len() * 16); + let mut prev = *iv; + for &cipher in blocks { + let mut b = cipher; + c.decrypt(&mut b); + for (x, p) in b.iter_mut().zip(prev) { + *x ^= p; + } + out.extend_from_slice(&b); + prev = cipher; + } + if pad && let Some(&n) = out.last() { + let n = n as usize; + if (1..=16).contains(&n) && n <= out.len() && out[out.len() - n..].iter().all(|b| *b as usize == n) { + out.truncate(out.len() - n); + } + } + out +} diff --git a/crates/pdf/src/crypt/rc4.rs b/crates/pdf/src/crypt/rc4.rs new file mode 100644 index 000000000..952a8394d --- /dev/null +++ b/crates/pdf/src/crypt/rc4.rs @@ -0,0 +1,22 @@ +//! RC4 (used by PDF security handler revisions 2–4). Encryption and decryption are the same. + +pub fn rc4(key: &[u8], data: &[u8]) -> Vec { + if key.is_empty() { + return data.to_vec(); + } + let mut s: [u8; 256] = std::array::from_fn(|i| i as u8); + let mut j: u8 = 0; + for i in 0..256 { + j = j.wrapping_add(s[i]).wrapping_add(key[i % key.len()]); + s.swap(i, j as usize); + } + let (mut i, mut j) = (0u8, 0u8); + data.iter() + .map(|b| { + i = i.wrapping_add(1); + j = j.wrapping_add(s[i as usize]); + s.swap(i as usize, j as usize); + b ^ s[s[i as usize].wrapping_add(s[j as usize]) as usize] + }) + .collect() +} diff --git a/crates/pdf/src/crypt/tests.rs b/crates/pdf/src/crypt/tests.rs new file mode 100644 index 000000000..46619427a --- /dev/null +++ b/crates/pdf/src/crypt/tests.rs @@ -0,0 +1,449 @@ +use super::*; + +fn hex(s: &str) -> Vec { + (0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap()).collect() +} + +#[test] +fn file_id_is_a_one_way_16_byte_identifier() { + let seed = [7u8; 32]; + let id = file_id(&seed); + assert_eq!(id.len(), 16); + assert_eq!(id, file_id(&seed)); + assert_ne!(id, seed[..16]); + assert_ne!(id, file_id(&[8u8; 32])); +} + +#[test] +fn rc4_reference_vectors() { + assert_eq!(rc4(b"Key", b"Plaintext"), hex("bbf316e8d940af0ad3")); + assert_eq!(rc4(b"Wiki", b"pedia"), hex("1021bf0420")); + assert_eq!(rc4(b"Secret", b"Attack at dawn"), hex("45a01f645fc35b383552544b9bf5")); +} + +#[test] +fn aes_cbc_nist_sp800_38a_vectors() { + let iv: [u8; 16] = hex("000102030405060708090a0b0c0d0e0f").try_into().unwrap(); + let plain = hex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51"); + let k128 = hex("2b7e151628aed2a6abf7158809cf4f3c"); + let c128 = aes_cbc_encrypt(&k128, &iv, &plain, false); + assert_eq!(c128, hex("7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b2")); + assert_eq!(aes_cbc_decrypt(&k128, &iv, &c128, false), plain); + let k256 = hex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4"); + let c256 = aes_cbc_encrypt(&k256, &iv, &plain, false); + assert_eq!(c256, hex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d")); + assert_eq!(aes_cbc_decrypt(&k256, &iv, &c256, false), plain); +} + +#[test] +fn aes_padding_is_added_and_tolerantly_removed() { + let key = [7u8; 16]; + let iv = [1u8; 16]; + for n in [0, 1, 15, 16, 17, 100] { + let data: Vec = (0..n as u8).collect(); + let c = aes_cbc_encrypt(&key, &iv, &data, true); + assert_eq!(c.len() % 16, 0); + assert_eq!(aes_cbc_decrypt(&key, &iv, &c, true), data); + } + // Garbage (bad padding, partial block) decrypts without panicking. + let _ = aes_cbc_decrypt(&key, &iv, &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], true); +} + +const ID: &[u8] = b"0123456789abcdef"; + +fn new(alg: Algorithm, user: &str, owner: &str, perms: i32) -> SecurityHandler { + create( + &NewEncryption { algorithm: alg, user_password: user, owner_password: owner, permissions: perms, encrypt_metadata: true, seed: [42; 32] }, + ID, + ) + .unwrap() +} + +const ALL: [Algorithm; 4] = [Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128, Algorithm::Aes256]; + +#[test] +fn created_encryption_authenticates_owner_and_user() { + for alg in ALL { + let made = new(alg, "user pw", "owner pw", 0b0100); // print only + let d = made.dict().clone(); + let owner = SecurityHandler::open(d.clone(), ID, Some("owner pw")).unwrap(); + assert_eq!(owner.auth(), Auth::Owner, "{alg:?}"); + assert_eq!(owner.file_key(), made.file_key(), "{alg:?}"); + assert!(owner.permissions().modify(), "owners can do everything"); + let user = SecurityHandler::open(d.clone(), ID, Some("user pw")).unwrap(); + assert_eq!(user.auth(), Auth::User, "{alg:?}"); + assert_eq!(user.file_key(), made.file_key(), "{alg:?}"); + assert!(user.permissions().print() && !user.permissions().modify() && !user.permissions().copy(), "{alg:?}"); + assert_eq!(SecurityHandler::open(d.clone(), ID, Some("nope")).unwrap_err(), CryptError::WrongPassword, "{alg:?}"); + assert_eq!(SecurityHandler::open(d, ID, None).unwrap_err(), CryptError::WrongPassword, "{alg:?}"); + } +} + +#[test] +fn the_owner_password_upgrades_a_user_session() { + for alg in ALL { + let d = new(alg, "", "secret", 0b0100).dict().clone(); + let user = SecurityHandler::open(d, ID, None).unwrap(); + assert!(!user.permissions().modify(), "{alg:?}"); + // The (empty) user password and wrong ones don't unlock anything. + assert_eq!(user.authenticate_owner(ID, "").unwrap_err(), CryptError::WrongPassword, "{alg:?}"); + assert_eq!(user.authenticate_owner(ID, "nope").unwrap_err(), CryptError::WrongPassword, "{alg:?}"); + let owner = user.authenticate_owner(ID, "secret").unwrap(); + assert_eq!(owner.auth(), Auth::Owner, "{alg:?}"); + assert_eq!(owner.file_key(), user.file_key(), "{alg:?}"); + assert!(owner.permissions().modify(), "{alg:?}"); + } + // A separate open password is not the permissions password. + for alg in ALL { + let d = new(alg, "open", "perms", 0b0100).dict().clone(); + let user = SecurityHandler::open(d, ID, Some("open")).unwrap(); + assert_eq!(user.authenticate_owner(ID, "open").unwrap_err(), CryptError::WrongPassword, "{alg:?}"); + assert_eq!(user.authenticate_owner(ID, "perms").unwrap().auth(), Auth::Owner, "{alg:?}"); + } +} + +#[test] +fn empty_user_password_opens_without_asking() { + for alg in ALL { + let d = new(alg, "", "secret", -1).dict().clone(); + let h = SecurityHandler::open(d, ID, None).unwrap(); + assert_eq!(h.auth(), Auth::User, "{alg:?}"); + } +} + +#[test] +fn strings_and_streams_round_trip_and_are_scrambled() { + for alg in ALL { + let h = new(alg, "", "o", -1); + let text = b"Confidential quarterly numbers".to_vec(); + let enc = h.encrypt_string(12, 0, &text); + assert_ne!(enc, text, "{alg:?}"); + assert_eq!(h.decrypt_string(12, 0, &enc), text, "{alg:?}"); + if alg != Algorithm::Aes256 { + // R2–R4 derive a key per object; R6 uses the file key everywhere (§7.6.3.3). + assert_ne!(h.decrypt_string(13, 0, &enc), text, "object keys differ per object ({alg:?})"); + } + let data: Vec = (0..1000u32).map(|i| (i * 7) as u8).collect(); + let enc = h.encrypt_stream(5, 0, &data, StreamKind::Normal); + assert_eq!(h.decrypt_stream(5, 0, &enc, StreamKind::Normal), data, "{alg:?}"); + // Deterministic: the same object encrypts to the same bytes (reproducible saves). + assert_eq!(h.encrypt_stream(5, 0, &data, StreamKind::Normal), enc); + } +} + +thread_local! { + /// Crypt filter names compared or looked up, on this thread. + pub(crate) static FILTER_COMPARES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[test] +fn crypt_filters_are_looked_up_once_per_handler() { + let plain = new(Algorithm::Aes128, "", "o", -1); + let mut d = plain.dict().clone(); + let n = 5_000; + let mut filters: Vec<_> = (0..n).map(|i| (format!("F{i}").into_bytes(), Method::Identity)).collect(); + filters.append(&mut d.crypt_filters); + d.crypt_filters = filters; + FILTER_COMPARES.with(|c| c.set(0)); + let h = SecurityHandler::open(d, ID, None).unwrap(); + let strings = 1_000; + let enc = plain.encrypt_string(7, 0, b"secret"); + for _ in 0..strings { + assert_eq!(h.decrypt_string(7, 0, &enc), b"secret"); + assert_eq!(h.decrypt_stream(7, 0, &plain.encrypt_stream(7, 0, b"s", StreamKind::Normal), StreamKind::Normal), b"s"); + } + assert_eq!(h.decrypt_stream(7, 0, b"x", StreamKind::Named(b"F3")), b"x"); + let compares = FILTER_COMPARES.with(|c| c.get()); + assert!(compares <= 2 * n + 3 * strings, "{compares} filter name comparisons"); +} + +#[test] +fn identity_crypt_filters_leave_data_alone() { + let mut d = new(Algorithm::Aes256, "", "o", -1).dict().clone(); + // Embedded-file-only encryption: strings and normal streams are not encrypted. + d.str_f = b"Identity".to_vec(); + d.stm_f = b"Identity".to_vec(); + d.ef_f = b"StdCF".to_vec(); + let h = SecurityHandler::open(d, ID, None).unwrap(); + assert!(!h.encrypts_strings()); + assert_eq!(h.decrypt_stream(3, 0, b"plain", StreamKind::Normal), b"plain"); + let enc = h.encrypt_stream(3, 0, b"attachment", StreamKind::EmbeddedFile); + assert_ne!(enc, b"attachment"); + assert_eq!(h.decrypt_stream(3, 0, &enc, StreamKind::EmbeddedFile), b"attachment"); + assert_eq!(h.decrypt_stream(3, 0, b"x", StreamKind::Named(b"Identity")), b"x"); +} + +#[test] +fn r6_passwords_are_saslprepped() { + // U+00AD (soft hyphen) is mapped to nothing by SASLprep; "ª" normalises to "a" (NFKC). + let d = new(Algorithm::Aes256, "SªSL\u{AD}prep", "o", -1).dict().clone(); + assert!(SecurityHandler::open(d.clone(), ID, Some("SaSLprep")).is_ok()); + assert!(SecurityHandler::open(d, ID, Some("SaSL-prep")).is_err()); +} + +#[test] +fn effective_passwords_are_the_bytes_the_keys_are_made_from() { + // AES-256: SASLprep drops soft hyphens, makes other spaces plain and folds compatibility + // characters (NFKC); at most 127 bytes of UTF-8. + let r6 = |pw: &str| effective_password(Algorithm::Aes256, pw); + assert_eq!(r6("\u{AD}"), r6(""), "a soft hyphen alone is no password"); + assert_eq!(r6("\u{FF21}"), r6("A"), "fullwidth A"); + assert_eq!(r6("\u{FB01}"), r6("fi"), "the fi ligature"); + assert_eq!(r6("a\u{3000}b"), b"a b", "an ideographic space"); + assert_ne!(r6("A"), r6("a"), "case matters"); + assert_eq!(r6(&"é".repeat(100)).len(), 127); + // RC4 and AES-128: PDFDocEncoding, padded or cut to exactly 32 bytes. + let (a31, a32) = ("a".repeat(31), "a".repeat(32)); + for alg in [Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128] { + assert_eq!(effective_password(alg, &format!("{a32}1")), a32.as_bytes(), "{alg:?}"); + assert_eq!(effective_password(alg, &format!("1{a32}")), effective_password(alg, &format!("1{a32}2")), "{alg:?}"); + assert_ne!(effective_password(alg, &format!("1{a32}")), effective_password(alg, &format!("2{a32}")), "{alg:?}"); + assert_eq!(effective_password(alg, &a31), effective_password(alg, &format!("{a31}(")), "{alg:?}: the padding starts with '('"); + assert_eq!(effective_password(alg, ""), PADDING, "{alg:?}"); + assert_eq!(effective_password(alg, "ılık€")[..5], [0x9A, b'l', 0x9A, b'k', 0xA0], "{alg:?}"); + } +} + +#[test] +fn passwords_with_the_same_effective_bytes_are_one_password() { + // A file protected with "\u{FF21}" (fullwidth A) opens with "A". + let d = new(Algorithm::Aes256, "\u{FF21}", "owner", 0b0100).dict().clone(); + assert_eq!(SecurityHandler::open(d.clone(), ID, Some("A")).unwrap().auth(), Auth::User); + assert_eq!(SecurityHandler::open(d, ID, Some("a")).unwrap_err(), CryptError::WrongPassword); + // A soft hyphen is no open password at all. + let d = new(Algorithm::Aes256, "\u{AD}", "owner", 0b0100).dict().clone(); + assert_eq!(SecurityHandler::open(d, ID, None).unwrap().auth(), Auth::User); + // Open and permissions passwords with the same bytes: the open password gives owner rights. + let d = new(Algorithm::Aes256, "\u{FB01}", "fi", 0b0100).dict().clone(); + assert_eq!(effective_password(Algorithm::Aes256, "\u{FB01}"), effective_password(Algorithm::Aes256, "fi")); + assert_eq!(SecurityHandler::open(d, ID, Some("\u{FB01}")).unwrap().auth(), Auth::Owner); + let a31 = "a".repeat(31); + let pairs = [(format!("{a31}a1"), format!("{a31}a2")), (a31.clone(), format!("{a31}("))]; + for alg in [Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128] { + for (open, perms) in &pairs { + assert_eq!(effective_password(alg, open), effective_password(alg, perms), "{alg:?}"); + let d = new(alg, open, perms, 0b0100).dict().clone(); + assert_eq!(SecurityHandler::open(d, ID, Some(open)).unwrap().auth(), Auth::Owner, "{alg:?} {open:?}"); + } + } +} + +#[test] +fn unsupported_handlers_are_reported() { + let mut d = new(Algorithm::Aes128, "", "o", -1).dict().clone(); + d.filter = b"Adobe.PubSec".to_vec(); + assert!(matches!(SecurityHandler::open(d.clone(), ID, None), Err(CryptError::Unsupported(_)))); + d.filter = b"Standard".to_vec(); + d.r = 7; + assert!(matches!(SecurityHandler::open(d, ID, None), Err(CryptError::Unsupported(_)))); +} + +#[test] +fn permission_bits_follow_table_22() { + let p = Permissions { bits: -3904, owner: false, revision: 3 }; // 0xFFFFF0C0: only reserved bits set + assert!(!p.print() && !p.modify() && !p.copy() && !p.annotate() && !p.assemble()); + let p = Permissions { bits: -1, owner: false, revision: 3 }; + assert!(p.print() && p.modify() && p.copy() && p.annotate() && p.assemble() && p.fill_forms()); + let p = Permissions { bits: 0, owner: true, revision: 3 }; + assert!(p.modify(), "owners ignore /P"); +} + +/// Page extraction (#5081) is assembly as the revision reads it, and copying (bit 5). +#[test] +fn page_extraction_needs_assembly_and_copying() { + let bit = |n: u32| 1i32 << (n - 1); + let user = |bits: i32, revision: i64| Permissions { bits, owner: false, revision }; + // "Any except extracting pages" (bits 4, 6, 9; copying off) at revision 3 and later. + assert!(!user(bit(4) | bit(6) | bit(9), 4).extract_pages()); + // Pages may change in place, but copying is withheld. + assert!(user(bit(11), 6).assemble() && !user(bit(11), 6).extract_pages()); + assert!(user(bit(11) | bit(5), 6).extract_pages()); + // Revision 2: bit 4 covers assembly, and its reserved bit 11 grants nothing. + assert!(user(bit(4), 2).assemble() && !user(bit(4) | bit(11), 2).extract_pages(), "copying withheld"); + assert!(user(bit(4) | bit(5), 2).extract_pages()); + assert!(!user(bit(5) | bit(11), 2).extract_pages(), "no assembly without bit 4 at revision 2"); + assert!(Permissions { bits: 0, owner: true, revision: 2 }.extract_pages(), "owners ignore /P"); +} + +#[test] +fn perms_limits_what_an_edited_p_grants() { + let made = new(Algorithm::Aes256, "user pw", "owner pw", 0b0100); // print only + let open = |d: &EncryptDict, pw| SecurityHandler::open(d.clone(), ID, Some(pw)).unwrap().permissions(); + let mut d = made.dict().clone(); + assert_eq!(open(&d, "user pw").bits, d.p, "/P and /Perms agree"); + d.p = -4; // /P edited to allow everything, /Perms as written + let p = open(&d, "user pw"); + assert!(p.print() && !p.modify() && !p.copy() && !p.annotate() && !p.fill_forms() && !p.assemble()); + assert!(open(&d, "owner pw").modify(), "owners ignore /P"); + d.p = -3904; // a stricter /P still applies + assert!(!open(&d, "user pw").print()); + // No /Perms, or one that does not decrypt to an `adb` block: /P as before. + for perms in [Vec::new(), vec![0; 16], made.dict().perms[..15].to_vec()] { + let d = EncryptDict { p: -4, perms, ..made.dict().clone() }; + assert!(open(&d, "user pw").modify()); + } + // R2 to R4 have no /Perms: /P is part of their file key, so an edited /P does not open. + let d = EncryptDict { p: -4, ..new(Algorithm::Aes128, "user pw", "owner pw", 0b0100).dict().clone() }; + assert_eq!(SecurityHandler::open(d, ID, Some("user pw")).unwrap_err(), CryptError::WrongPassword); +} + +// Not built for the web: proptest is a dev-dependency of the other targets only. +#[cfg(not(target_arch = "wasm32"))] +proptest::proptest! { + #[test] + fn malformed_dictionaries_never_panic(o in proptest::collection::vec(proptest::num::u8::ANY, 0..60), + u in proptest::collection::vec(proptest::num::u8::ANY, 0..60), + r in 0i64..9, len in -10i64..300) { + let d = EncryptDict { filter: b"Standard".to_vec(), v: 4, r, length_bits: len, o, u, p: -1, encrypt_metadata: true, ..Default::default() }; + let _ = SecurityHandler::open(d, ID, Some("x")); + } +} + +/// A `/V 4` RC4 crypt filter with a 40- or 48-bit key, as Acrobat writes it (pdf.js issue 19484): +/// the file key is extended to 16 bytes with zeros before Algorithm 1, so each object's key is +/// all 16 bytes of MD5(key, zeros, object, generation). Everything else keeps Algorithm 1 as the +/// standard has it, n + 5 bytes (at most 16) of MD5(key, object, generation[, "sAlT"]): a `/V 2` +/// file with the same short key, a full 128-bit `/V 4` RC4 key, and AESV2, short (88 bits) or +/// full. Each object key is computed here by hand; data written with it must decrypt. +#[test] +fn a_v4_rc4_key_shorter_than_16_bytes_is_extended_with_zeros() { + let text = b"Page tree in an object stream".to_vec(); + let cases = + [(4, 40, Method::Rc4), (4, 48, Method::Rc4), (2, 40, Method::Rc4), (4, 128, Method::Rc4), (4, 88, Method::Aes128), (4, 128, Method::Aes128)]; + for (v, bits, method) in cases { + let mut d = EncryptDict { + filter: b"Standard".to_vec(), + v, + r: if v == 4 { 4 } else { 3 }, + length_bits: bits, + o: vec![7; 32], + p: -3392, + encrypt_metadata: v != 4, + ..Default::default() + }; + if v == 4 { + d.crypt_filters = vec![(b"StdCF".to_vec(), method)]; + d.stm_f = b"StdCF".to_vec(); + d.str_f = b"StdCF".to_vec(); + } + let key = file_key_r4(&d, ID, b""); + assert_eq!(key.len(), (bits / 8) as usize); + d.u = u_value_r4(&d, ID, &key); + let h = SecurityHandler::open(d, ID, None).unwrap(); + let (num, generation) = (13u32, 0u16); + let extended = v == 4 && method == Method::Rc4 && key.len() < 16; + let mut md5 = Md5::new(); + md5.update(&key); + if extended { + md5.update(vec![0u8; 16 - key.len()]); + } + md5.update(&num.to_le_bytes()[..3]); + md5.update(generation.to_le_bytes()); + if method == Method::Aes128 { + md5.update(b"sAlT"); + } + let digest = md5.finalize(); + let n = if extended { 16 } else { key.len() }; + let object_key = digest[..(n + 5).min(16)].to_vec(); + let case = format!("V{v}, {bits}-bit key, {method:?}"); + let written = if method == Method::Rc4 { + rc4(&object_key, &text) + } else { + let iv = [3u8; 16]; + [iv.to_vec(), aes_cbc_encrypt(&object_key, &iv, &text, true)].concat() + }; + assert_eq!(h.decrypt_string(num, generation, &written), text, "{case}"); + assert_eq!(h.decrypt_stream(num, generation, &written, StreamKind::Normal), text, "{case}"); + let saved = h.encrypt_stream(num, generation, &text, StreamKind::Normal); + if method == Method::Rc4 { + assert_eq!(saved, written, "saved as it was read: {case}"); + } + assert_eq!(h.decrypt_stream(num, generation, &saved, StreamKind::Normal), text, "{case}"); + } +} + +#[test] +fn owner_authentication_recovers_the_user_password() { + for alg in [Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128] { + let d = new(alg, "user pw", "owner pw", -1).dict().clone(); + let h = SecurityHandler::open(d.clone(), ID, Some("owner pw")).unwrap(); + let _: &Zeroizing> = &h.key; + let _: &Option>> = &h.recovered_user; + assert_eq!(h.recovered_user_password().as_deref().map(Vec::as_slice), Some(&b"user pw"[..]), "{alg:?}"); + assert_eq!(SecurityHandler::open(d, ID, Some("user pw")).unwrap().recovered_user_password(), None); + } + let d = new(Algorithm::Rc4_128, "", "owner", -1).dict().clone(); + assert_eq!(SecurityHandler::open(d, ID, Some("owner")).unwrap().recovered_user_password().as_deref().map(Vec::as_slice), Some(&b""[..])); +} + +/// /O, /U and the first /ID of files qpdf 12.4.2 wrote (`--static-id`), with the file keys +/// `qpdf --show-encryption-key` reports. +fn qpdf_file(r: i64, o: &str, u: &str) -> EncryptDict { + let mut d = EncryptDict { + filter: b"Standard".to_vec(), + v: if r == 4 { 4 } else { 2 }, + r, + length_bits: 128, + o: hex(o), + u: hex(u), + p: -4, + encrypt_metadata: true, + ..Default::default() + }; + if r == 4 { + d.crypt_filters = vec![(b"StdCF".to_vec(), Method::Aes128)]; + d.stm_f = b"StdCF".to_vec(); + d.str_f = b"StdCF".to_vec(); + } + d +} + +const QPDF_ID: &str = "56b57383aafa8314f3f98222995f5604"; + +#[test] +fn legacy_passwords_are_pdfdoc_encoded_with_a_utf8_fallback() { + let id = hex(QPDF_ID); + // R3 RC4: user "ılık€" in PDFDocEncoding (ı = 0x9A, € = 0xA0); owner "Öğretmen", which + // PDFDocEncoding can't hold, so qpdf wrote its UTF-8 bytes. + let r3 = qpdf_file( + 3, + "d519aa8120ce8c54812b45e5391916fed167b1c84961d90c4ee4c182ad83534b", + "4985c03ddfa3eeb8a90c343512913ec40021446990b9e4114071a4d9104984c1", + ); + let user = SecurityHandler::open(r3.clone(), &id, Some("ılık€")).unwrap(); + assert_eq!((user.auth(), user.file_key().to_vec()), (Auth::User, hex("3564ffcea186122595ad55ee157da71f"))); + let owner = SecurityHandler::open(r3.clone(), &id, Some("Öğretmen")).unwrap(); + assert_eq!((owner.auth(), owner.file_key().to_vec()), (Auth::Owner, hex("3564ffcea186122595ad55ee157da71f"))); + assert_eq!(owner.recovered_user_password(), Some(Zeroizing::new(vec![0x9A, b'l', 0x9A, b'k', 0xA0]))); + // Dropping ı and € (the old Latin-1 conversion) or a near miss doesn't open it. + for wrong in ["lk", "ilik€", "ılık", "Ogretmen"] { + assert_eq!(SecurityHandler::open(r3.clone(), &id, Some(wrong)).unwrap_err(), CryptError::WrongPassword, "{wrong}"); + } + // R4 AES-128: user "şifre" (UTF-8 bytes), owner "owner". + let r4 = qpdf_file( + 4, + "bb4f8f4bd2236569e765906caf64e4429a4c20d6e996fdef963e9b5080f9e083", + "4a965fb91882772cd09ce8bbff2c33270021446990b9e4114071a4d9104984c1", + ); + let user = SecurityHandler::open(r4.clone(), &id, Some("şifre")).unwrap(); + assert_eq!((user.auth(), user.file_key().to_vec()), (Auth::User, hex("9f19d465cfd8e525d32aa0bdf10acd06"))); + assert_eq!(SecurityHandler::open(r4.clone(), &id, Some("owner")).unwrap().auth(), Auth::Owner); + assert_eq!(SecurityHandler::open(r4, &id, Some("ifre")).unwrap_err(), CryptError::WrongPassword); +} + +#[test] +fn pdfdoc_encoding_round_trips() { + for b in 0..=255u8 { + if b != 0x9F { + assert_eq!(pdfdoc_byte(pdfdoc_char(b)), Some(b), "{b:#04x}"); + } + } + assert_eq!(pdfdoc_byte('ı'), Some(0x9A)); + assert_eq!(pdfdoc_byte('€'), Some(0xA0)); + assert_eq!(pdfdoc_byte('ç'), Some(0xE7)); + for c in ['ğ', 'ş', 'İ', '\u{A0}', '\u{85}', 'Ж', '日'] { + assert_eq!(pdfdoc_byte(c), None, "{c}"); + } + assert_eq!(legacy_candidates("Çok"), [Zeroizing::new(vec![0xC7, b'o', b'k'])]); + assert_eq!(legacy_candidates("şifre"), [Zeroizing::new(b"ifre".to_vec()), Zeroizing::new("şifre".as_bytes().to_vec())]); +} diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index 9814c469c..965673fab 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -4,6 +4,8 @@ //! Advanced backend re-exports are coupled to the pinned backend, not a universal document model. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#[cfg(feature = "crypt")] +pub mod crypt; #[cfg(feature = "interpret")] pub use hayro_interpret as interpret; mod link; diff --git a/crates/pdf/tests/crypt/main.rs b/crates/pdf/tests/crypt/main.rs new file mode 100644 index 000000000..702a0df7b --- /dev/null +++ b/crates/pdf/tests/crypt/main.rs @@ -0,0 +1,7 @@ +//! Tests of the `crypt` feature, linked into one test binary. Not built for the web: proptest +//! does not build there. +#![cfg(all(feature = "crypt", not(target_arch = "wasm32")))] + +mod permissions_revision; +mod proptests; +mod qpdf_literals; diff --git a/crates/pdf/tests/crypt/permissions_revision.rs b/crates/pdf/tests/crypt/permissions_revision.rs new file mode 100644 index 000000000..06a5a5b98 --- /dev/null +++ b/crates/pdf/tests/crypt/permissions_revision.rs @@ -0,0 +1,126 @@ +//! The permission accessors of `craft_pdf::crypt::Permissions`, checked against the flag table of +//! ISO 32000-1:2008 (Table 22). Expected values come from that table only. +//! +//! Bit n has the mask `1 << (n - 1)`. Readers use bits 3 to 6 and 9 to 12 and ignore the rest. + +use craft_pdf::crypt::{Algorithm, Auth, NewEncryption, Permissions, SecurityHandler, create}; + +const ID: &[u8] = b"0123456789abcdef"; +const T: bool = true; +const F: bool = false; + +/// The eight accessors in this order: print, high-quality print, modify, copy, accessibility, +/// annotate, fill forms, assemble. +fn cells(p: &Permissions) -> [bool; 8] { + [p.print(), p.print_high_quality(), p.modify(), p.copy(), p.extract_for_accessibility(), p.annotate(), p.fill_forms(), p.assemble()] +} + +/// Creates a file with `flags` at revision `revision`, opens it with the user password, and +/// returns the user's permissions. +fn user_view(alg: Algorithm, revision: i64, flags: i32) -> Permissions { + let made = create( + &NewEncryption { algorithm: alg, user_password: "user", owner_password: "owner", permissions: flags, encrypt_metadata: true, seed: [42; 32] }, + ID, + ) + .expect("the handler is created"); + assert_eq!(made.dict().r, revision, "the algorithm writes revision {revision}"); + let opened = SecurityHandler::open(made.dict().clone(), ID, Some("user")).expect("the user password opens the file"); + assert_eq!(opened.auth(), Auth::User, "the user password gives user access"); + opened.permissions() +} + +/// Revision 3 and later: each row is a flag word and the expected eight cells. +const REVISION_3_ROWS: &[(&str, i32, [bool; 8])] = &[ + ("every meaningful flag set, ignored flags clear", -196, [T, T, T, T, T, T, T, T]), + ("every flag set, ignored flags set too", -1, [T, T, T, T, T, T, T, T]), + ("reserved flags only", -4096, [F, F, F, F, F, F, F, F]), + ("ignored flags set, meaningful flags clear", -3901, [F, F, F, F, F, F, F, F]), + ("modify flag clear", -9, [T, T, F, T, T, T, T, T]), + ("copy flag clear", -17, [T, T, T, F, T, T, T, T]), + ("annotate flag clear", -33, [T, T, T, T, T, F, T, T]), + ("annotate and fill-existing flags clear", -289, [T, T, T, T, T, F, F, T]), + ("annotate flag set, fill-existing flag clear", -257, [T, T, T, T, T, T, T, T]), + ("accessibility flag clear", -513, [T, T, T, T, F, T, T, T]), + ("high-quality flag clear, print flag set", -2049, [T, F, T, T, T, T, T, T]), + ("bits 3 to 12 set, reserved bits clear", 4092, [T, T, T, T, T, T, T, T]), + ("positive flag word, bit 32 clear", 2_147_483_647, [T, T, T, T, T, T, T, T]), + ("revision 2 example read at revision 3", -44, [T, T, F, T, T, F, T, T]), +]; + +#[test] +fn revision_three_rows_hold_through_a_created_and_reopened_file() { + for (name, bits, want) in REVISION_3_ROWS { + assert_eq!(cells(&user_view(Algorithm::Rc4_128, 3, *bits)), *want, "{name}: flag word {bits:#010x}"); + } +} + +#[test] +fn owner_access_gives_every_flag() { + let made = create( + &NewEncryption { + algorithm: Algorithm::Rc4_128, + user_password: "user", + owner_password: "owner", + permissions: 0, + encrypt_metadata: true, + seed: [42; 32], + }, + ID, + ) + .expect("the handler is created"); + assert_eq!(made.auth(), Auth::Owner, "the owner password gives owner access"); + assert_eq!(cells(&made.permissions()), [T; 8]); +} + +#[test] +fn revision_two_print_modify_copy_and_annotate_follow_the_table() { + for (bits, want) in [(-4, [T, T, T, T]), (-44, [T, F, T, F]), (-17, [T, T, F, T])] { + let p = user_view(Algorithm::Rc4_40, 2, bits); + assert_eq!([p.print(), p.modify(), p.copy(), p.annotate()], want, "revision 2, flag word {bits}"); + } +} + +#[test] +fn revision_two_with_every_flag_set_grants_accessibility_and_form_filling() { + let p = user_view(Algorithm::Rc4_40, 2, -4); + assert!(p.extract_for_accessibility(), "bit 5 is set, and at revision 2 it covers accessibility"); + assert!(p.fill_forms(), "bit 6 is set"); +} + +#[test] +fn revision_three_assembly_needs_its_own_flag() { + let p = user_view(Algorithm::Rc4_128, 3, -1025); + assert!(p.modify(), "bit 4 is set"); + assert!(!p.assemble(), "assembly needs bit 11; bit 4 alone does not grant it"); +} + +#[test] +fn revision_three_high_quality_print_needs_the_print_flag() { + let p = user_view(Algorithm::Rc4_128, 3, -5); + assert!(!p.print(), "bit 3 is clear"); + assert!(!p.print_high_quality(), "high-quality print needs bit 3 and bit 12"); +} + +#[test] +fn revision_two_accessibility_follows_the_copy_flag() { + let p = user_view(Algorithm::Rc4_40, 2, -17); + assert!(!p.copy(), "bit 5 is clear"); + assert!(!p.extract_for_accessibility(), "at revision 2 accessibility is bit 5"); +} + +#[test] +fn revision_two_form_filling_ignores_bit_nine() { + let p = user_view(Algorithm::Rc4_40, 2, -44); + assert!(!p.annotate(), "bit 6 is clear"); + assert!(!p.fill_forms(), "at revision 2 form filling is bit 6 alone"); +} + +#[test] +fn revision_two_ignores_the_flags_only_later_revisions_define() { + // Bit 4 is clear and bit 11 set: bit 11 means nothing at revision 2, so assembly stays refused. + let p = user_view(Algorithm::Rc4_40, 2, -44); + assert!(!p.assemble(), "at revision 2 assembly is bit 4 alone"); + // Bit 3 is set and bit 12 clear: printing at revision 2 has one quality. + let p = user_view(Algorithm::Rc4_40, 2, -2049); + assert!(p.print_high_quality(), "at revision 2 high-quality print is bit 3 alone"); +} diff --git a/crates/pdf/tests/crypt/proptests.rs b/crates/pdf/tests/crypt/proptests.rs new file mode 100644 index 000000000..a81808ab9 --- /dev/null +++ b/crates/pdf/tests/crypt/proptests.rs @@ -0,0 +1,464 @@ +//! Property tests for what the security handler does to *content*, and to the ciphertext a +//! stranger's file hands back. +//! +//! `src/tests.rs` covers this handler carefully with fixed vectors — including the `/O`, `/U` and +//! file keys qpdf 12.4.2 produced for real files — plus one property over malformed dictionaries. +//! What it does not vary is the *payload*. Upstream's encrypted round trips run three shapes: a +//! 30-byte text, a 1000-byte ramp, and a 10-byte attachment in `identity_crypt_filters_leave_data_alone`. +//! The shapes nobody walks are the ones that break CBC and readers: empty content, one block minus +//! one, a ciphertext cut short by a damaged `/Length`, and keys of a length `create` never builds. +//! +//! - plaintext survives encryption at every length, for each algorithm and each crypt filter; +//! - truncated or mutated ciphertext produces bytes rather than a panic (apps +//! open files from strangers, and encrypted streams in the wild do get truncated); +//! - object keys are scoped to their object, and AES-256's documented exception still holds; +//! - a file opens for exactly the passwords whose effective bytes match, stated as a rule over +//! generated Unicode passwords instead of the three examples in `src/crypt/tests.rs` (the pdfcraft #1185 rule); +//! - the AES-CBC and RC4 primitives invert each other for any key length, since `/Length` and +//! crypt filters from a damaged file reach them with shapes `create` would never build. + +use craft_pdf::crypt::{ + Algorithm, Auth, CryptError, Method, NewEncryption, PADDING, SecurityHandler, StreamKind, aes_cbc_decrypt, aes_cbc_encrypt, create, + effective_password, pdfdoc_byte, rc4, +}; +use proptest::prelude::*; + +/// The first element of `/ID`, which every R2–R4 derivation hashes in (Algorithm 2). +const ID: &[u8] = b"\x01\x23\x45\x67\x89\xab\xcd\xef\x0f\xed\xcb\xa9\x87\x65\x43\x21"; + +fn algorithms() -> impl Strategy { + prop::sample::select(vec![Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128, Algorithm::Aes256]) +} + +/// Content at the lengths where CBC padding goes wrong, plus anything in between. +fn payloads() -> impl Strategy> { + prop_oneof![ + Just(Vec::new()), + Just(vec![0x5cu8; 1]), + Just(vec![0xa7u8; 15]), + Just(vec![0x3fu8; 16]), + Just(vec![0x9du8; 17]), + Just(vec![0x10u8; 32]), + prop::collection::vec(any::(), 1..96), + ] +} + +/// Passwords from an alphabet the two encodings disagree about: SASLprep deletes U+00AD, folds +/// U+FF21 to "A", U+FB01 to "fi" and U+3000 to a space (revisions 5–6), while PDFDocEncoding has +/// no byte for 'ğ' or CJK at all, which sends revisions 2–4 down their UTF-8 fallback. +const PASSWORD_CHARS: [char; 12] = ['a', 'B', '7', ' ', '-', '\u{AD}', '\u{FF21}', '\u{FB01}', 'ı', 'ğ', '日', '\u{A0}']; + +fn passwords() -> impl Strategy { + prop::collection::vec(prop::sample::select(PASSWORD_CHARS.to_vec()), 0..12).prop_map(|chars| chars.into_iter().collect()) +} + +/// `bytes` cut to 32 and padded with the standard padding string (Algorithm 2, step a). +fn padded(bytes: &[u8]) -> Vec { + let mut out = PADDING; + let n = bytes.len().min(32); + out[..n].copy_from_slice(&bytes[..n]); + out.to_vec() +} + +/// Every byte string the handler may authenticate `pw` as. Revisions 2–4 use PDFDocEncoding, and +/// for a password holding characters it lacks they also try the raw UTF-8 bytes (qpdf writes such +/// passwords that way); revisions 5–6 use the SASLprep bytes. `effective_password` publishes the +/// first of these per algorithm, and it is what callers such as Protect check before encrypting. +fn candidates(algorithm: Algorithm, pw: &str) -> Vec> { + let mut out = vec![effective_password(algorithm, pw)]; + if algorithm != Algorithm::Aes256 && pw.chars().any(|c| pdfdoc_byte(c).is_none()) { + out.push(padded(&pw.as_bytes().iter().copied().take(32).collect::>())); + } + out +} + +/// What `create` keys `/O` from: the owner password, except on revisions 2–4 with no owner +/// password given, where it falls back to the user password's bytes. +fn owner_target(algorithm: Algorithm, user: &str, owner: &str) -> Vec { + if algorithm != Algorithm::Aes256 && owner.is_empty() { effective_password(algorithm, user) } else { effective_password(algorithm, owner) } +} + +/// Which password `pw` is for this file. Owners are tried before users, so bytes that match both +/// authenticate as owner — the order viewers use. `None` means it opens nothing. +fn expected_auth(algorithm: Algorithm, user_target: &[u8], owner_target: &[u8], pw: &str) -> Option { + let tried = candidates(algorithm, pw); + if tried.iter().any(|c| c == owner_target) { + Some(Auth::Owner) + } else if tried.iter().any(|c| c == user_target) { + Some(Auth::User) + } else { + None + } +} + +fn make(algorithm: Algorithm, user: &str, owner: &str) -> SecurityHandler { + create(&NewEncryption { algorithm, user_password: user, owner_password: owner, permissions: -1, encrypt_metadata: true, seed: [0x5e; 32] }, ID) + .unwrap() +} + +proptest! { + /// Encrypting then decrypting returns the input at every length, as a string and as each kind + /// of stream — and is byte-for-byte reproducible, because re-running a save must not silently + /// produce different bytes. Payloads of at least one block must come back changed: RC4 would + /// need an all-zero keystream to leave them alone, and CBC cannot shrink a padded block. + #[test] + fn content_survives_the_trip_at_every_length(alg in algorithms(), data in payloads()) { + let h = make(alg, "user", "owner"); + + // The key width each algorithm promises: 5 bytes for export-grade 40-bit RC4, 16 for + // AES-128 and RC4-128, 32 for AES-256. `key_len` is the only place that decides this and + // nothing in either suite reads it back, so `if d.r == 2 { 5 }` becoming `{ 4 }` today + // produces a file key of the wrong size that every test still calls fine. + let want_len = match alg { Algorithm::Rc4_40 => 5, Algorithm::Aes128 | Algorithm::Rc4_128 => 16, Algorithm::Aes256 => 32 }; + prop_assert_eq!(h.file_key().len(), want_len, "{:?} must use a {} byte file key", alg, want_len); + + let h = make(alg, "user", "owner"); + for num in [0u32, 1, 4096, u32::MAX] { + let generation = (num % 65_535) as u16; + + let enc = h.encrypt_string(num, generation, &data); + prop_assert_eq!(h.decrypt_string(num, generation, &enc), &data[..], "string round trip at {} bytes ({:?})", data.len(), alg); + prop_assert_eq!(h.encrypt_string(num, generation, &data), enc, "string encryption must be reproducible ({:?})", alg); + + for kind in [StreamKind::Normal, StreamKind::EmbeddedFile, StreamKind::Named(b"StdCF")] { + let enc = h.encrypt_stream(num, generation, &data, kind); + prop_assert_eq!(h.decrypt_stream(num, generation, &enc, kind), &data[..], "stream round trip at {} bytes ({:?}, {:?})", data.len(), alg, kind); + prop_assert_eq!(h.encrypt_stream(num, generation, &data, kind), enc, "{:?} {:?} encryption must be reproducible", alg, kind); + } + + if data.len() >= 16 { + prop_assert_ne!(h.encrypt_string(num, generation, &data), &data[..], "one block left in plain sight ({:?})", alg); + } + } + } + + /// Ciphertext of any length — truncated by a damaged `/Length`, padded out with garbage, or + /// byte-flipped inside — must decrypt to bytes and never read past the end. AES discards a + /// trailing partial block and leaves invalid padding in place rather than failing, so its + /// output can only be a whole number of blocks minus at most one; RC4 keeps its length + /// whatever the bytes are. An input shorter than the IV has nothing left to decode. + #[test] + fn damaged_ciphertext_never_panics(alg in algorithms(), enc in prop::collection::vec(any::(), 0..64)) { + let h = make(alg, "user", "owner"); + for cut in 0..=enc.len() { + let broken = &enc[..cut]; + let out = h.decrypt_string(7, 3, broken); + if alg == Algorithm::Rc4_40 || alg == Algorithm::Rc4_128 { + prop_assert_eq!(out.len(), cut, "RC4 must be length-preserving ({:?})", alg); + } else { + let blocks = cut.saturating_sub(16) / 16; + prop_assert!(out.len() <= blocks * 16, "{:?}: {} bytes of ciphertext yielded {} decrypted bytes", alg, cut, out.len()); + prop_assert!(blocks * 16 - out.len() <= 16, "{:?}: padding removed more than one block", alg); + } + } + + // Flip every byte of a real ciphertext in turn: no position may crash the reader. + let full = h.encrypt_string(7, 3, b"twenty-four bytes of PDF content"); + for i in 0..full.len() { + let mut mutated = full.clone(); + mutated[i] ^= 0xFF; + let _ = h.decrypt_string(7, 3, &mutated); + } + } + + /// A crypt filter name decides whether content is encrypted at all: PDF 2.0 permits + /// `/StmF /Identity` with attachments still encrypted, and a name that appears in neither `/CF` + /// nor as `Identity` resolves to Identity rather than failing. The reported answer and the + /// bytes must agree, at every length including empty (where AES still adds a whole block). + /// `/EFF` is set here so embedded files and normal streams must take *different* paths: the + /// mix-up that leaves an attachment in plain sight, or encrypts a stream its own filter says + /// should not be. And sometimes `/CF` carries an entry *named* `Identity` that claims + /// `/CFM /AESV2`: a producer writing nonsense. The name wins and the bytes stay plain, since + /// encrypting what both sides agreed is plaintext would write a file other readers cannot open. + #[test] + fn crypt_filters_decide_what_gets_encrypted( + data in payloads(), + name in prop::sample::select(vec!["Identity", "StdCF", "Nope"]), + bogus_identity_in_cf in any::(), + ) { + let mut d = make(Algorithm::Aes256, "", "owner").dict().clone(); + d.str_f = name.as_bytes().to_vec(); + d.stm_f = b"Identity".to_vec(); + d.ef_f = b"StdCF".to_vec(); + if bogus_identity_in_cf { + d.crypt_filters.push((b"Identity".to_vec(), Method::Aes256)); + } + let h = SecurityHandler::open(d, ID, None).unwrap(); + + // Only `StdCF` really encrypts; `Identity` does not even when `/CF` defines it, and a name + // in neither place resolves to Identity rather than failing. + let strings_encrypted = name == "StdCF"; + prop_assert_eq!(h.encrypts_strings(), strings_encrypted, "/StrF /{} reported {}", name, strings_encrypted); + + let enc = h.encrypt_string(3, 0, &data); + if strings_encrypted { + prop_assert_ne!(&enc, &data, "/StrF /{} left strings in plain sight", name); + prop_assert!(enc.len() > data.len(), "AES always pads: {} bytes of ciphertext for {} plaintext", enc.len(), data.len()); + prop_assert_eq!(h.decrypt_string(3, 0, &enc), &data[..], "/StrF /{} round trip", name); + } else { + prop_assert_eq!(&enc, &data, "/StrF /{} must pass strings through", name); + } + + // A normal stream follows `/StmF /Identity` here, whatever the string filter says. + prop_assert_eq!(&h.encrypt_stream(3, 0, &data, StreamKind::Normal), &data[..], "normal streams follow /StmF Identity"); + // An embedded file follows `/EFF /StdCF`, so it is encrypted at every length — empty + // included, where AES still emits a padded block — and comes back intact. + let ef = h.encrypt_stream(3, 0, &data, StreamKind::EmbeddedFile); + prop_assert_ne!(&ef, &data[..], "/EFF /StdCF left an embedded file in plain sight at {} bytes", data.len()); + prop_assert_eq!(h.decrypt_stream(3, 0, &ef, StreamKind::EmbeddedFile), &data[..], "embedded file round trip"); + // An unknown named filter is Identity, not an error; a known one uses its real method. + prop_assert_eq!(&h.encrypt_stream(3, 0, &data, StreamKind::Named(b"Missing")), &data[..], "an unknown crypt filter must not encrypt"); + prop_assert_ne!(&h.encrypt_stream(3, 0, &data, StreamKind::Named(b"StdCF")), &data[..], "a filter named /StdCF must encrypt"); + } + + /// Object keys are scoped to the object (Algorithm 1, §7.6.3.3): RC4 and AES-128 fold the + /// object number and generation into the key, so another object must not read these bytes back. + /// AES-256 is the documented exception — revisions 5 and 6 use the file key for every object. + /// The comparisons that decrypt under a foreign number carry the weight: wrong keystream bytes + /// match the plaintext only by a chance of one in 256 per byte. Checking that *encryption* + /// differs would prove less, because our initial vector already covers `/Num`. + #[test] + fn object_keys_are_scoped_to_their_object(alg in prop::sample::select(vec![Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128]), data in prop::collection::vec(any::(), 8..40)) { + let h = make(alg, "user", "owner"); + + let enc = h.encrypt_string(100, 0, &data); + prop_assert_eq!(h.decrypt_string(100, 0, &enc), &data[..], "{:?} reads its own object back", alg); + prop_assert_ne!(h.decrypt_string(101, 0, &enc), &data[..], "{:?}: another object number must not decrypt these bytes", alg); + prop_assert_ne!(h.decrypt_string(100, 1, &enc), &data[..], "{:?}: another generation must not decrypt these bytes", alg); + + // Algorithm 1 folds the LOW THREE bytes of `/Num` into the key. Two directions are pinned: + // numbers that differ inside those three bytes must separate (a fold to `[..2]` would let + // object 0 and object 65_536 share a key, silently handing one object's plaintext to another), + // and numbers that differ only ABOVE them must share it — the fold is three bytes by design, + // so a widening to four would break this and nothing else in the crate notices. + let low = h.encrypt_string(0, 0, &data); + prop_assert_ne!(&h.decrypt_string(65_536, 0, &low)[..], &data[..], "{:?}: /Num is folded three bytes wide; 0 and 65_536 must not share a key", alg); + prop_assert_ne!(&h.decrypt_string(1 << 23, 0, &low)[..], &data[..], "{:?}: the third byte of /Num must still reach the key", alg); + prop_assert_eq!(&h.decrypt_string(1 << 24, 0, &low)[..], &data[..], "{:?}: /Num is folded three bytes wide, so a difference above bit 23 shares the key by design", alg); + + let r6 = make(Algorithm::Aes256, "user", "owner"); + let enc6 = r6.encrypt_string(100, 0, &data); + prop_assert_eq!(&r6.decrypt_string(101, 3, &enc6)[..], &data[..], "AES-256 uses the file key for every object (§7.6.3.3)"); + // ...which is exactly why the initial vector has to vary: revisions 5 and 6 reuse one key + // for the whole file, so two objects holding the same bytes must still produce different + // ciphertext. Identical bytes for a different `/Num` would be CBC initial-vector reuse — + // the flaw that leaks structure in encrypted data. + prop_assert_ne!(&r6.encrypt_string(101, 0, &data)[..], &enc6[..], "AES-256 reused the same initial vector for two objects"); + prop_assert_ne!(&r6.encrypt_string(100, 1, &data)[..], &enc6[..], "AES-256 reused the same initial vector across generations"); + } + + /// A file created for `user` and `owner` opens for exactly the passwords whose effective bytes + /// equal one of theirs, and reports the role that match earned — owner first, since viewers try + /// `/O` before `/U`. Everything else is `WrongPassword`: never an opening, and no panic on odd + /// Unicode. This is the rule pdfcraft #1185 became (bytes decide, so a soft hyphen alone is no password + /// and U+FF21 is "A"), stated for all inputs rather than the three examples in `src/tests.rs`. + /// It also pins #1762's upgrade path: a user session gains owner rights on the owner password + /// and on nothing else. + #[test] + fn authentication_accepts_exactly_the_matching_passwords( + alg in algorithms(), + mut user in passwords(), + owner in prop::sample::select(vec!["owner".to_string(), String::new(), "Owner\u{AD}".into(), "\u{FF21}".into()]), + extra in passwords(), + ) { + // `user` may have been grown past the 32-byte cut above; create with that spelling. + // Grow the legacy password PAST the 32-byte cut BEFORE the file is built from it, so a + // partner differing only beyond byte 32 is the same password to this file. + if alg != Algorithm::Aes256 { + user = format!("{user}{}", "abcdefghijklmnopqrstuvwxyz0123456789".repeat(2)); + } + + let h = make(alg, &user, &owner); + let d = h.dict().clone(); + let user_bytes = effective_password(alg, &user); + let owner_bytes = owner_target(alg, &user, &owner); + + // A collision engineered by construction, not by luck. Revisions 2-4 cut the password at + // 32 bytes (Algorithm 2), so any spelling that agrees on the first 32 is ONE password; + // revisions 5-6 run SASLprep, which deletes U+00AD. Growing past byte 32 guarantees the + // partner is a different string with identical bytes at every draw — earlier drafts here + // claimed `(` alone did it, which is false: appending "(" to a short password DISPLACES the + // padding (pad("a") is [61,28,BF..], pad("a(") is [61,28,28,BF..]), and this alphabet draws + // at most 11 characters, so chance produced zero legacy collisions in 1,566 measured cases. + + let mut suspects: Vec = vec![user.clone(), owner.clone(), String::new()]; + // Near misses are PREPENDED: appending to a legacy password already past the cut would be + // another collision rather than a refusal, which would silently thin the negative testing. + let mut near_misses: Vec = vec![format!("q{user}"), extra.clone()]; + if alg == Algorithm::Aes256 && !user.is_empty() { + near_misses.push(format!("{user}q")); + } + + let collision_partner = if alg == Algorithm::Aes256 { format!("{user}\u{AD}") } else { format!("{user}Z") }; + prop_assert_ne!(&collision_partner[..], &user[..], "the collision partner must be a different string"); + prop_assert_eq!(effective_password(alg, &collision_partner), effective_password(alg, &user), "{:?}: {:?} must become the same bytes as {:?}", alg, collision_partner, user); + + // The mistake itself, pinned so it cannot come back: a short legacy password plus "(" is NOT the + // same password, because it shifts the padding by one byte instead of filling a slot. + if alg != Algorithm::Aes256 { + prop_assert_ne!(effective_password(alg, "a("), effective_password(alg, "a"), "appending ( to a short password displaces the padding; that is not a collision"); + prop_assert_eq!(effective_password(alg, &"a".repeat(31)), effective_password(alg, &format!("{}(", "a".repeat(31))), "at exactly 31 characters ( does land in the padding slot, and THAT is a collision"); + } + + suspects.extend(near_misses); + suspects.push(collision_partner.clone()); + suspects.dedup(); + + for suspect in &suspects { + let want = expected_auth(alg, &user_bytes, &owner_bytes, suspect); + match (want, SecurityHandler::open(d.clone(), ID, Some(suspect))) { + (Some(role), Ok(got)) => { + prop_assert_eq!(got.auth(), role, "{:?}: {:?} should open as {:?}", alg, suspect, role); + prop_assert_eq!(got.file_key(), h.file_key(), "{:?}: whichever password opens the file, the key is the same", alg); + } + (Some(role), Err(e)) => prop_assert!(false, "{:?}: {:?} must open as {:?} but gave {:?}", alg, suspect, role, e), + (None, Ok(got)) => prop_assert!(false, "{:?}: {:?} is not a password for this file, yet it opened as {:?}", alg, suspect, got.auth()), + (None, Err(CryptError::WrongPassword)) => {} + (None, Err(e)) => prop_assert!(false, "{:?}: {:?} gave {}, expected WrongPassword", alg, suspect, e), + } + + // The owner password upgrades a user session; nothing else does. When the two share + // bytes, `create` gave /O the user's own bytes (revisions 2–4) or left it empty + // (AES-256, pdfcraft #1185's sharp edge), so the open password *is* an owner password here. + if let Ok(session) = SecurityHandler::open(d.clone(), ID, Some(&user)) { + let is_owner_password = expected_auth(alg, &user_bytes, &owner_bytes, suspect) == Some(Auth::Owner); + let upgrade = session.authenticate_owner(ID, suspect).map(|u| u.auth()); + if is_owner_password { + prop_assert_eq!(upgrade, Ok(Auth::Owner), "{:?}: {:?} must unlock owner rights", alg, suspect); + } else { + prop_assert_eq!(upgrade, Err(CryptError::WrongPassword), "{:?}: {:?} must not grant owner rights", alg, suspect); + } + } + } + + // `None` means no password was offered: the handler tries the empty string, and nothing + // else (a file with a real open password never opens silently). + let want_none = expected_auth(alg, &user_bytes, &owner_bytes, ""); + match (want_none, SecurityHandler::open(d.clone(), ID, None)) { + (Some(role), Ok(got)) => prop_assert_eq!(got.auth(), role, "{:?}: no password opened as {:?}", alg, got.auth()), + (None, Ok(got)) => prop_assert!(false, "{:?}: the empty password is not valid, yet no password opened as {:?}", alg, got.auth()), + (Some(role), Err(e)) => prop_assert!(false, "{:?}: the empty password should open as {:?} but gave {:?}", alg, role, e), + (None, Err(CryptError::WrongPassword)) => {} + (None, Err(e)) => prop_assert!(false, "{:?}: no password gave {}, expected WrongPassword", alg, e), + } + } + + /// The AES-CBC and RC4 primitives taken directly: a damaged file's `/Length` and crypt filters + /// hand them keys and data of shapes `create` would never build, so they must invert each other + /// for any key length (`Cipher::new` takes AES-256 at 32 bytes and zero-padded AES-128 below + /// it, identically on both sides of the trip) and every payload length. + #[test] + fn primitives_are_invertible_for_any_key_and_length( + key in prop::collection::vec(any::(), 1..40), + data in payloads(), + iv in prop::collection::vec(any::(), 16..24), + ) { + let iv: [u8; 16] = *iv.as_slice().first_chunk::<16>().expect("the iv strategy draws at least 16 bytes; a silent zero fallback would hide a generator change"); + + let enc = aes_cbc_encrypt(&key, &iv, &data, true); + prop_assert_eq!(aes_cbc_decrypt(&key, &iv, &enc, true), &data[..], "padded AES round trip at {} bytes", data.len()); + prop_assert!(enc.len() >= 16 && enc.len().is_multiple_of(16), "PKCS#5 makes whole blocks: {} bytes for {} plaintext", enc.len(), data.len()); + + // Without padding, a partial block is zero-padded rather than refused, and extra trailing + // bytes decrypt to more output instead of being dropped. + let raw = aes_cbc_encrypt(&key, &iv, &data, false); + prop_assert_eq!(raw.len(), data.len().div_ceil(16) * 16, "unpadded AES output is whole blocks"); + let mut longer = raw.clone(); + longer.extend_from_slice(b"0123456789abcdef"); + prop_assert_eq!(aes_cbc_decrypt(&key, &iv, &longer, false).len(), raw.len() + 16, "a whole extra block decrypts to a whole extra block"); + + // RC4 undoes itself at every length; an empty key encrypts nothing, which is why the + // handler never derives an empty object key. + prop_assert_eq!(rc4(&key, &rc4(&key, &data)), &data[..], "RC4 round trip at {} bytes", data.len()); + prop_assert_eq!(rc4(&[], &data), &data[..], "an empty RC4 key passes bytes through"); + } + + /// `/Perms` certifies the permission bits and `/EncryptMetadata`: decrypted with the file key it + /// must hold `/P` little-endian, four set bytes, 'T' or 'F', and the `adb` sentinel (Algorithm 10). + /// Nothing else in this crate reads it — `cos` writes it out and no code takes it back apart — so + /// without this property a flipped metadata byte would tell every other viewer the wrong thing about + /// our own file while all our tests stayed green. The older revisions we create emit no `/Perms` + /// at all, which is recorded here as well so a change in what goes out shows up rather than + /// shipping quietly. + #[test] + fn perms_value_agrees_with_the_permissions_it_certifies(permissions in any::(), encrypt_metadata in any::()) { + let h = create( + &NewEncryption { algorithm: Algorithm::Aes256, user_password: "user", owner_password: "owner", permissions, encrypt_metadata, seed: [0x5e; 32] }, + ID, + ) + .unwrap(); + + prop_assert_eq!(h.dict().perms.len(), 16, "/Perms is one encrypted block"); + let open = aes_cbc_decrypt(h.file_key(), &[0u8; 16], &h.dict().perms, false); + + // Bits 1-2 clear and the reserved bits set (§7.6.4.2 Table 22) is what `/P` must say, and the + // encrypted certificate has to repeat it byte for byte. + let normalised = (permissions | !0xFFF | 0xC0) & !0x3; + prop_assert_eq!(&open[..4], &normalised.to_le_bytes(), "/Perms must carry /P"); + prop_assert_eq!(h.dict().p, normalised, "the /P written must match the permission bits drawn"); + prop_assert_eq!(&open[4..8], &[0xFF; 4], "the four bytes following /P are set"); + prop_assert_eq!(open[8], if encrypt_metadata { b'T' } else { b'F' }, "/Perms must report /EncryptMetadata"); + prop_assert_eq!(&open[9..12], b"adb", "the adb sentinel must be present"); + + // A user session's answers come from those same bits, not from the owner shortcut. + let as_user = SecurityHandler::open(h.dict().clone(), ID, Some("user")).unwrap(); + prop_assert_eq!(as_user.permissions().print(), normalised & 0b100 != 0, "/P bit 3 decides printing"); + prop_assert_eq!(as_user.permissions().copy(), normalised & 0b1_0000 != 0, "/P bit 5 decides copying"); + prop_assert!(!as_user.permissions().owner, "the user password must not report owner rights"); + + // Bytes 13-16 of /Perms are the non-repeating filler. Nothing reads them, so pinning that + // they move with the seed is the only way to notice a producer that freezes them at zero. + let other = create( + &NewEncryption { algorithm: Algorithm::Aes256, user_password: "user", owner_password: "owner", permissions, encrypt_metadata, seed: [0x11; 32] }, + ID, + ) + .unwrap(); + let open_other = aes_cbc_decrypt(other.file_key(), &[0u8; 16], &other.dict().perms, false); + prop_assert_ne!(&open[12..16], &open_other[12..16], "the four filler bytes of /Perms must follow the seed, not sit constant"); + + // Revisions below 5 carry no /Perms; pinned so the boundary of what we emit is explicit. + for older in [Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128] { + let legacy = make(older, "user", "owner"); + prop_assert!(legacy.dict().perms.is_empty(), "{:?} must emit no /Perms", older); + } + } + /// Revisions 4 mix four set bytes into every file key when `/EncryptMetadata` is false (Algorithm + /// 2, step f), so the same password over the same `/ID` must produce a *different* key than when + /// metadata is encrypted. Every other test in this crate builds `encrypt_metadata: true` only, + /// which is exactly why that step is reachable but never walked: switching it off entirely changed + /// nothing any test could see. This also records the rule that revisions below 4 always encrypt + /// metadata whatever was asked, so the boundary between the two is named rather than assumed. + #[test] + fn metadata_flag_changes_the_key_it_claims_to_change(data in payloads()) { + let build = |encrypt_metadata: bool| { + create(&NewEncryption { algorithm: Algorithm::Aes128, user_password: "user", owner_password: "owner", permissions: -1, encrypt_metadata, seed: [0x5e; 32] }, ID).unwrap() + }; + let (yes, no) = (build(true), build(false)); + + // Algorithm 2, step f: revisions 4 hash four set bytes into every key when metadata stays + // plain, so two files with identical passwords and the same /ID cannot share a key. + prop_assert_ne!(yes.file_key(), no.file_key(), "R4 must fold four set bytes into the key when /EncryptMetadata is false (Algorithm 2, step f)"); + prop_assert_eq!(no.dict().encrypt_metadata, false, "the flag must reach the dictionary"); + prop_assert!(!no.encrypts_metadata(), "/EncryptMetadata false must report itself honestly"); + prop_assert!(yes.encrypts_metadata(), "and true must report true"); + + // Both still open on the same password and round trip at every length: a derivation that + // drifted would show up as an unreadable file rather than passing quietly. + for h in [&yes, &no] { + let open = SecurityHandler::open(h.dict().clone(), ID, Some("user")).unwrap(); + prop_assert_eq!(open.file_key(), h.file_key(), "the user password must still derive this file's key"); + let enc = h.encrypt_string(7, 0, &data); + prop_assert_eq!(&open.decrypt_string(7, 0, &enc)[..], &data[..], "round trip at {} bytes", data.len()); + } + + // And the two files must not read each other's content: different keys, different plaintext. + if data.len() >= 8 { + prop_assert_ne!(&yes.decrypt_string(7, 0, &no.encrypt_string(7, 0, &data))[..], &data[..], "a key built for plain metadata must not decrypt a file whose keys encrypt it"); + } + + // Revisions below 4 always encrypt metadata, whatever the caller asked for. + let legacy = create(&NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", owner_password: "owner", permissions: -1, encrypt_metadata: false, seed: [0x5e; 32] }, ID).unwrap(); + prop_assert!(legacy.dict().encrypt_metadata && legacy.encrypts_metadata(), "V<4 always encrypts metadata, as create() documents"); + } + +} diff --git a/crates/pdf/tests/crypt/qpdf_literals.rs b/crates/pdf/tests/crypt/qpdf_literals.rs new file mode 100644 index 000000000..e583eb815 --- /dev/null +++ b/crates/pdf/tests/crypt/qpdf_literals.rs @@ -0,0 +1,326 @@ +//! Encryption numbers from an **outside implementation**. Six of the seven rows below are qpdf's own, +//! written by qpdf core 12.2.0 through pikepdf 9.11, and their `/O`, `/U`, `/OE`, `/UE`, `/Perms` +//! and `/ID` values were copied out of those files rather than computed here. The seventh row, +//! `crafted-r3-40bit-hand-derived`, is ours and says so where it is defined - no producer we can run +//! here writes revision 3 at 40 bits (qpdf core 12.2.0 emits `/Length 128` at `R=3`, measured below), +//! so nothing outside computed that one either. +//! +//! That distinction is the whole point. `create()` and `open()` share one derivation, so a wrong-but +//! self-consistent implementation round-trips cleanly against itself: across two review rounds we +//! broke the key derivation several ways and the unit suite stayed green (deleting Algorithm 2 step f +//! leaves all 17 unit tests passing while our rows fail). Quoting an outsider's bytes +//! breaks the circle - `open()` recomputes `/U` (or the revision 6 hash chain) and compares it to what +//! was stored, so a derivation that diverges cannot authenticate at all. +//! +//! Revisions covered: 2 (export-grade RC4, where the five-byte key makes a truncation error visible that +//! every pre-existing R3/R4 vector in this repository hides as a no-op), 3, and 4 and 6 each with metadata +//! encrypted *and* plain. For revision 4 that pairing is the only outside view of Algorithm 2 step +//! f's four `FF` bytes; revision 6 shows the same flag differently - its `/U` differs between the two +//! metadata variants, which is measured here rather than attributed to a named algorithm step. Only the +//! numbers are committed - never the PDFs, and no generator script that lives outside +//! this repository. To re-derive the six qpdf rows, take one blank 200x200 page and save it with +//! pikepdf 9.11 (qpdf core 12.2.0) using `Encryption(owner=OWNER, user=USER,` +//! `allow=Permissions(extract=False), R=, aes=…, metadata=…)` plus `save(..., static_id=True)`, +//! then read `/Encrypt` back. Below revision 4 both flags are forced rather than chosen, because pikepdf +//! refuses AES and refuses encrypted metadata there - so those two rows need `aes=False, metadata=False`, +//! while revisions 4 and 6 take `aes=True` with the case's own metadata flag. Run to run, every quoted +//! field of those rows reproduces at revisions 2-4, and whole files too for revisions 2 and 3; a +//! revision 4 file falls short of that by one stream, because `/CFM /AESV2` prepends a fresh random IV. +//! Revision 6 reproduces structurally only, its salts being new each run. What stays checked rather than +//! trusted - `open()` recovers the user password from `/O` and verifies `/U` at revisions 2-4 +//! (Algorithm 7), matches both stored hashes at revision 6 and unwraps `/OE`/`/UE` into the file key +//! there; this file then decrypts `/Perms` with that key, which authentication itself never reads. One +//! thing no assertion here verifies is the crypt-filter names: renaming a row's `/StmF` or replacing its +//! filter set leaves all three tests green, so those are held to review rather than to a test. The +//! rows come from PdfCraft, which uses qpdf as an oracle (its `crates/oracle/README.md`). + +use craft_pdf::crypt::{Auth, EncryptDict, Method, SecurityHandler, aes_cbc_decrypt}; + +const USER: &str = "open pw"; +const OWNER: &str = "owner pw"; + +/// One file qpdf wrote. Bytes are held as hex so the table stays a `const`: the literals on screen are +/// then exactly what qpdf emitted, with no runtime step in between to trust. +struct Case { + label: &'static str, + v: i64, + r: i64, + length_bits: i64, + p: i32, + encrypt_metadata: bool, + id0_hex: &'static str, + o_hex: &'static str, + u_hex: &'static str, + oe_hex: &'static str, + ue_hex: &'static str, + perms_hex: &'static str, + crypt_filters: fn() -> Vec<(Vec, Method)>, + stm_f: &'static str, + str_f: &'static str, + key_len_bytes: usize, +} + +impl Case { + fn dict(&self) -> EncryptDict { + EncryptDict { + filter: b"Standard".to_vec(), + v: self.v, + r: self.r, + length_bits: self.length_bits, + o: hex(self.o_hex), + u: hex(self.u_hex), + oe: hex(self.oe_hex), + ue: hex(self.ue_hex), + perms: hex(self.perms_hex), + p: self.p, + encrypt_metadata: self.encrypt_metadata, + crypt_filters: (self.crypt_filters)(), + stm_f: self.stm_f.as_bytes().to_vec(), + str_f: self.str_f.as_bytes().to_vec(), + ef_f: Vec::new(), + } + } + + fn id0(&self) -> Vec { + hex(self.id0_hex) + } +} + +fn hex(s: &str) -> Vec { + (0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap()).collect() +} + +fn no_filters() -> Vec<(Vec, Method)> { + Vec::new() +} + +fn aes128_filter() -> Vec<(Vec, Method)> { + vec![(b"StdCF".to_vec(), Method::Aes128)] +} + +fn aes256_filter() -> Vec<(Vec, Method)> { + vec![(b"StdCF".to_vec(), Method::Aes256)] +} + +/// `/ID[0]` is shared by every case because qpdf wrote them with `static_id`. +const CASES: &[Case] = &[ + Case { + label: "r2-40bit", + v: 1, + r: 2, + length_bits: 40, + p: -28, + encrypt_metadata: true, + id0_hex: "31415926535897932384626433832795", + o_hex: "166a894580aae60ae93fa9d83fc576f26f13dbe4574ce6c57497bec3ece7f125", + u_hex: "69ff2fb98261fd094174f93f0388859f356080ca486a38ce2d5ac23b98f5f89c", + oe_hex: "", + ue_hex: "", + perms_hex: "", + crypt_filters: no_filters, + stm_f: "", + str_f: "", + key_len_bytes: 5, + }, + Case { + label: "r3-128rc4", + v: 2, + r: 3, + length_bits: 128, + p: -1044, + encrypt_metadata: true, + id0_hex: "31415926535897932384626433832795", + o_hex: "2910182a30a271d410d8dada035cbd77c0b57a683bd55cd1cd9eee35a2b64848", + u_hex: "1b4b8fb3fd213b4eadc142ab9f385fc80122456a91bae5134273a6db134c87c4", + oe_hex: "", + ue_hex: "", + perms_hex: "", + crypt_filters: no_filters, + stm_f: "", + str_f: "", + key_len_bytes: 16, + }, + Case { + label: "r4-aes128-meta-enc", + v: 4, + r: 4, + length_bits: 128, + p: -1044, + encrypt_metadata: true, + id0_hex: "31415926535897932384626433832795", + o_hex: "2910182a30a271d410d8dada035cbd77c0b57a683bd55cd1cd9eee35a2b64848", + u_hex: "1b4b8fb3fd213b4eadc142ab9f385fc80122456a91bae5134273a6db134c87c4", + oe_hex: "", + ue_hex: "", + perms_hex: "", + crypt_filters: aes128_filter, + stm_f: "StdCF", + str_f: "StdCF", + key_len_bytes: 16, + }, + Case { + label: "r4-aes128-meta-plain", + v: 4, + r: 4, + length_bits: 128, + p: -1044, + encrypt_metadata: false, + id0_hex: "31415926535897932384626433832795", + o_hex: "2910182a30a271d410d8dada035cbd77c0b57a683bd55cd1cd9eee35a2b64848", + u_hex: "d6d363fc817815701f0242a581cb39e60122456a91bae5134273a6db134c87c4", + oe_hex: "", + ue_hex: "", + perms_hex: "", + crypt_filters: aes128_filter, + stm_f: "StdCF", + str_f: "StdCF", + key_len_bytes: 16, + }, + Case { + label: "r6-aes256-meta-enc", + v: 5, + r: 6, + length_bits: 256, + p: -1044, + encrypt_metadata: true, + id0_hex: "31415926535897932384626433832795", + o_hex: "1c791d66227fdfd7c1d225eb81fb3408d8bcfac1a0ff71cc57b10eb29029e5f6965cccb6b94e461d9da354a9dbd6eea7", + u_hex: "64ffbf79168ed7b325267cdba8dae211d4c2c5ebdffd339c7ac36c33e26f23b7f478c60ae42d50b5d20ceac56d659151", + oe_hex: "d2f6ef0bcc6ce93228be760c7eee8f4259af7a6044a4c4e04a0d48ab0893ef1e", + ue_hex: "963035aa24fb009e6d26b3cffd31492ffe6f4e563e4dcae8d7b7e3203c4a2f56", + perms_hex: "5675a86560873d2532e856a37045247e", + crypt_filters: aes256_filter, + stm_f: "StdCF", + str_f: "StdCF", + key_len_bytes: 32, + }, + Case { + label: "r6-aes256-meta-plain", + v: 5, + r: 6, + length_bits: 256, + p: -1044, + encrypt_metadata: false, + id0_hex: "31415926535897932384626433832795", + o_hex: "e227e586c4dc5a362c7271d960565cf3a37145e33af5bf5a08c73b52a1d819e152c737176dc08774e400798d6534e95e", + u_hex: "dbb84b847bb2d80865cc3c5a7c751d4c659d2c96ed1750f8be6f9c3b93fe3d11f494b43e5f17a21c85f5a4b171055afb", + oe_hex: "8c31f2392bc2a093850269a3404cbe01cea38e5e0fe308fe8ac50f5222cff103", + ue_hex: "2b0c44e4bddb3c9c01431b4ed3c7da42dbd1ef47e610d626ec2124535a0a4469", + perms_hex: "adbb69fb9fffde31ba90aaaf5eb4bdaf", + crypt_filters: aes256_filter, + stm_f: "StdCF", + str_f: "StdCF", + key_len_bytes: 32, + }, + // HAND-DERIVED, not produced by qpdf: an independent re-implementation of ISO Algorithms 2/3 + // (n = 5) computed this pair, cross-validated by having the same routine reproduce qpdf's + // n = 16 `/O` byte for byte. It exists because no producer we can run here emits R3 at 40 bits - + // qpdf core 12.2.0 writes `/Length 128` there, and this crate's own create() maps Rc4_40 to revision 2 - so + // the only route we have to `key_len` = 5 with the derivation loop RUNNING (r >= 3) is to craft + // it by hand. With that combination live, the loop's truncation stops being a no-op: mutating + // `Md5::digest(&digest[..n])` to hash the whole digest makes this password stop authenticating. + Case { + label: "crafted-r3-40bit-hand-derived", + v: 2, + r: 3, + length_bits: 40, + p: -1044, + encrypt_metadata: true, + id0_hex: "31415926535897932384626433832795", + o_hex: "50e50251fd20acb28ba50013142d19a47249d06b977d28b63cb3ce9d56fb5d50", + u_hex: "000756f24fe648256830aec6155ca6f100000000000000000000000000000000", + oe_hex: "", + ue_hex: "", + perms_hex: "", + crypt_filters: no_filters, + stm_f: "", + str_f: "", + key_len_bytes: 5, + }, +]; + +#[test] +fn outside_derived_dictionaries_authenticate_with_the_intended_role() { + for case in CASES { + let d = case.dict(); + let id0 = case.id0(); + + // The user password must open as a user. Owners are tried first, so an `Owner` here would mean + // the two passwords had collapsed onto one set of bytes - on these spellings they have not. + let user = SecurityHandler::open(d.clone(), &id0, Some(USER)) + .unwrap_or_else(|e| panic!("{}: qpdf's user password was refused by our reader: {e}", case.label)); + assert_eq!(user.auth(), Auth::User, "{}", case.label); + + // The owner password must open as owner: revisions 2-4 recover the user password from `/O` + // (Algorithm 7), revision 6 hashes against qpdf's own `/O`. + let owner = + SecurityHandler::open(d.clone(), &id0, Some(OWNER)).unwrap_or_else(|e| panic!("{}: qpdf's owner password was refused: {e}", case.label)); + assert_eq!(owner.auth(), Auth::Owner, "{}", case.label); + + // A wrong password may never open it; and the key must be exactly as wide as the revision + // promises - those five bytes are what makes a revision 2 truncation detectable at all. + assert!(SecurityHandler::open(d.clone(), &id0, Some("not the password")).is_err(), "{}: a wrong password opened", case.label); + assert_eq!(user.file_key().len(), case.key_len_bytes, "{}: file key width", case.label); + assert_eq!(owner.file_key(), user.file_key(), "{}: whichever password opens it, one key", case.label); + + // A user session must not claim owner rights. We deliberately do NOT assert individual + // permission flags here: which bit means "extract" moves between revision 2 and revisions 4 + // (qpdf wrote `/P = -28` for the export-grade file, where our accessor's bit-10 reading of + // "extract for accessibility" is a reserved field), and pinning that mapping belongs with the + // permissions tests, not with this oracle. What matters here is only that authentication, role + // and key width agree with what an outside implementation produced. + assert!(!user.permissions().owner, "{}: a user session claimed owner rights", case.label); + } +} + +#[test] +fn metadata_flag_from_outside_changes_the_file_key_it_certifies() { + // `/EncryptMetadata` false folds four set bytes into a revision 4 key (Algorithm 2, step f) and + // changes what revision 6 stores in `/U`. qpdf wrote both variants of each, so an implementation + // that ignored the flag would give identical keys. Internally this is no longer untested - + // `tests/it/proptests.rs` from #2358 varies the flag and asserts the keys differ - but internal tests + // still share our derivation, so what these rows add is a second implementation's own numbers. + for (enc_label, plain_label) in [("r4-aes128-meta-enc", "r4-aes128-meta-plain"), ("r6-aes256-meta-enc", "r6-aes256-meta-plain")] { + let a = CASES.iter().find(|c| c.label == enc_label).expect("encrypted-metadata fixture present"); + let b = CASES.iter().find(|c| c.label == plain_label).expect("plain-metadata fixture present"); + + let ha = SecurityHandler::open(a.dict(), &a.id0(), Some(USER)).unwrap_or_else(|e| panic!("{enc_label}: refused by our reader: {e}")); + let hb = SecurityHandler::open(b.dict(), &b.id0(), Some(USER)) + .unwrap_or_else(|e| panic!("{plain_label}: refused by our reader - is step f being ignored? {e}")); + + assert_ne!(ha.file_key(), hb.file_key(), "{enc_label} and {plain_label} must not share a key: the metadata flag is hashed in"); + assert!(ha.encrypts_metadata(), "{enc_label} reports encrypted metadata"); + assert!(!hb.encrypts_metadata(), "{plain_label} reports plain metadata, as qpdf wrote it"); + } + + // The outsider's numbers must differ across revisions; equality here would mean a transcription + // error, which would quietly void every assertion above. + let r2 = CASES.iter().find(|c| c.r == 2).unwrap(); + let r3 = CASES.iter().find(|c| c.r == 3).unwrap(); + assert_ne!(hex(r2.u_hex), hex(r3.u_hex), "revision 2 and revision 3 /U differ because the key width differs"); + assert_eq!(r2.u_hex.len(), 64, "revision 2 /U is RC4 over the padding: 32 bytes"); + assert_eq!(r3.u_hex.len(), 64, "revision 3 /U is 32 bytes: sixteen derived, plus padding qpdf fills (our crafted row leaves it zero)"); +} + +#[test] +fn qpdf_perms_decode_to_the_shape_both_implementations_agree_on() { + // `/Perms` is the one value in this dictionary that no test here verified before: `cos` writes it + // out and reads it back, but nothing decrypts it. Under qpdf's own bytes we can - `/P` + // little-endian, four set bytes, `T`/`F`, the `adb` sentinel (Algorithm 10 here; vendored lopdf + // checks the same fields as its Algorithm 13, though only three bytes of `/P`). + let mut checked = 0; + for case in CASES.iter().filter(|c| !c.perms_hex.is_empty()) { + let h = SecurityHandler::open(case.dict(), &case.id0(), Some(USER)) + .unwrap_or_else(|e| panic!("{}: refused before /Perms could be read: {e}", case.label)); + let open = aes_cbc_decrypt(h.file_key(), &[0u8; 16], &hex(case.perms_hex), false); + + assert_eq!(open.len(), 16, "{}: /Perms is one encrypted block", case.label); + assert_eq!(&open[..4], &case.p.to_le_bytes(), "{}: /P must round trip against qpdf's bytes", case.label); + assert_eq!(&open[4..8], &[0xFF; 4], "{}: the four bytes after /P are set", case.label); + assert_eq!(open[8], if case.encrypt_metadata { b'T' } else { b'F' }, "{}: metadata byte as qpdf wrote it", case.label); + assert_eq!(&open[9..12], b"adb", "{}: sentinel from qpdf's bytes", case.label); + assert_ne!(&open[12..16], &[0u8; 4], "{}: qpdf's filler is not frozen at zero", case.label); + checked += 1; + } + assert!(checked >= 2, "expected /Perms on both revision 6 cases; saw {checked}"); +} From e0a3b2b8c43fda3992abcebae7e29df6831a1229 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:21:50 -0500 Subject: [PATCH 045/120] craft-dxf: model entities (geometry, text, MTEXT columns, hatches, dimensions) (#927) * craft-dxf: model, the drawing above the tags (header, tables, blocks, layouts) Document::read indexes a tag list into header variables, table records, blocks with their entities, the ENTITIES records (a POLYLINE with its vertices, an INSERT with its attributes) and the objects, each a slice of the tag list. The typed table records, LAYOUT and DICTIONARY are made on request. Also the colour index palette, colour, lineweight and transparency values, extended data lookup and the object coordinate system. Merged from CADCraft's cadcraft-io dxf_read (5a6aa9b6) and VectorCraft's vectorcraft-cad import (566f7208). Neither app's document types appear. * craft-dxf: model entities (geometry, text, MTEXT columns, hatches, dimensions) Entity::read types an entity record: the properties every entity has, and LINE, POINT, CIRCLE, ARC, ELLIPSE, LWPOLYLINE, POLYLINE with its vertices (2D, 3D, mesh, polyface), SPLINE, RAY, XLINE, TEXT, MTEXT with columns, ATTDEF, INSERT with its attributes (multiline ones included), DIMENSION, LEADER, SOLID, TRACE, 3DFACE, HATCH (polyline and edge loops, pattern lines, seeds, gradient), VIEWPORT, IMAGE, WIPEOUT and TOLERANCE. Other types stay readable from the record's tags. model::text has the caret encoding of strings, both ways. Merged from CADCraft's cadcraft-io dxf_read (5a6aa9b6) and VectorCraft's vectorcraft-cad import (566f7208). * craft-dxf: a hatch spline edge without fit data leaves the loop's source count Before R2010 a spline edge has no 97 of its own, so the 97 after it counts the loop's source entities. It was read as the edge's fit point count and the sources were lost. * README: keep main's crate list after the merge --- README.md | 2 +- crates/dxf/src/model.rs | 12 +- crates/dxf/src/model/entity.rs | 669 +++++++++++++++++++++++++++++ crates/dxf/src/model/hatch.rs | 355 +++++++++++++++ crates/dxf/src/model/text.rs | 280 ++++++++++++ crates/dxf/tests/model_entities.rs | 393 +++++++++++++++++ 6 files changed, 1708 insertions(+), 3 deletions(-) create mode 100644 crates/dxf/src/model/entity.rs create mode 100644 crates/dxf/src/model/hatch.rs create mode 100644 crates/dxf/src/model/text.rs create mode 100644 crates/dxf/tests/model_entities.rs diff --git a/README.md b/README.md index be5e2936e..3830d0560 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = ", + /// Group 330: the block record (or other object) that owns the entity. + pub owner: Option, + /// Group 8 (`0` when absent). + pub layer: String, + /// Group 6; `None` is ByLayer. + pub linetype: Option, + pub color: Color, + pub lineweight: Lineweight, + /// Group 48 (1 when absent or not positive). + pub ltscale: f64, + /// Group 60 is not 1. + pub visible: bool, + /// Group 67 is 1. + pub paper_space: bool, + pub transparency: Transparency, + /// Group 39. + pub thickness: f64, + /// Groups 210/220/230 (the world Z axis when absent). + pub extrusion: Point, + /// Group 390: the plot style name object. + pub plot_style: Option, +} + +/// The groups of the AcDbEntity subclass: the record up to the entity's own subclass marker (all +/// of it in files without markers, and in records with no AcDbEntity marker, whose layer and +/// colour may follow the entity's marker), or up to an embedded object (101). Later groups reuse +/// the codes: an MTEXT's column width is 48. Markers inside an application-defined group do not +/// count. +fn entity_groups(tags: &[Tag]) -> &[Tag] { + let is_entity = |t: &Tag| t.str_ref().is_some_and(|s| s == "AcDbEntity"); + let marked = outside_app(tags).any(|(_, t)| t.code == 100 && is_entity(t)); + let end = outside_app(tags).find(|(_, t)| (marked && t.code == 100 && !is_entity(t)) || t.code == 101).map_or(tags.len(), |(i, _)| i); + tags.get(..end).unwrap_or(tags) +} + +impl Common { + pub fn read(tags: &[Tag]) -> Common { + let (all, e) = (Groups(tags), Groups(entity_groups(tags))); + Common { + handle: e.handle(5), + owner: outside_app(tags).find(|(_, t)| t.code == 330).and_then(|(_, t)| parse_handle(&t.str())), + layer: e.name(8).unwrap_or_else(|| "0".into()), + linetype: e.name(6).filter(|l| !l.eq_ignore_ascii_case("ByLayer")), + color: Color::of(&e), + lineweight: e.i(370).map_or(Lineweight::ByLayer, Lineweight::from_dxf), + ltscale: e.f(48).filter(|s| *s > 0.0).unwrap_or(1.0), + visible: e.i(60) != Some(1), + paper_space: e.i(67) == Some(1), + transparency: e.i(440).map_or(Transparency::ByLayer, Transparency::from_dxf), + thickness: all.f_or(39, 0.0), + extrusion: [all.f_or(210, 0.0), all.f_or(220, 0.0), all.f_or(230, 1.0)], + plot_style: e.handle(390).filter(|h| *h != 0), + } + } +} + +/// A vertex of an LWPOLYLINE. +#[derive(Clone, Copy, Debug, PartialEq, Default)] +pub struct LwVertex { + pub x: f64, + pub y: f64, + /// Group 42: the tangent of a quarter of the arc to the next vertex (0: straight). + pub bulge: f64, + pub start_width: f64, + pub end_width: f64, +} + +/// An LWPOLYLINE. +#[derive(Clone, Debug, PartialEq)] +pub struct LwPolyline { + pub vertices: Vec, + /// Flags (70) bit 1. + pub closed: bool, + /// Flags bit 128: the linetype runs around the vertices. + pub plinegen: bool, + /// Group 43. + pub const_width: f64, + /// Group 38. + pub elevation: f64, +} + +/// A VERTEX of a POLYLINE. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Vertex { + pub location: Point, + pub bulge: f64, + pub start_width: f64, + pub end_width: f64, + /// Group 70: 1 added by curve fitting, 8 added by spline fitting, 16 a spline frame control + /// point, 32 a 3D polyline vertex, 64 a mesh vertex, 128 a polyface vertex (128 without 64: + /// a face record). + pub flags: i64, + /// Groups 71–74: the vertex numbers of a polyface face record (1-based; negative: the edge + /// from it is invisible; 0: none). + pub face: [i64; 4], +} + +impl Vertex { + /// A face record of a polyface mesh, which holds vertex numbers and no point. + pub fn is_face(&self) -> bool { + self.flags & 128 != 0 && self.flags & 64 == 0 + } + /// A control point of the frame a spline-fit polyline was made from: not on the curve. + pub fn is_spline_frame(&self) -> bool { + self.flags & 16 != 0 + } +} + +/// A POLYLINE with its VERTEX records: a 2D or 3D polyline, a polygon mesh or a polyface mesh. +#[derive(Clone, Debug, PartialEq)] +pub struct Polyline { + /// Group 70: 1 closed (a mesh: closed in M), 2 curve-fit, 4 spline-fit, 8 a 3D polyline, 16 a + /// polygon mesh, 32 a mesh closed in N, 64 a polyface mesh, 128 the linetype runs around the + /// vertices. + pub flags: i64, + /// The z of the POLYLINE's own point: a 2D polyline's elevation. + pub elevation: f64, + /// Groups 40 and 41: the default widths. + pub start_width: f64, + pub end_width: f64, + /// Groups 71 and 72: a mesh's M and N vertex counts (a polyface mesh: vertices and faces). + pub m: i64, + pub n: i64, + /// Group 75: the smooth surface type (5 quadratic, 6 cubic, 8 Bezier). + pub smooth: i64, + pub vertices: Vec, +} + +impl Polyline { + pub fn closed(&self) -> bool { + self.flags & 1 != 0 + } + pub fn is_3d(&self) -> bool { + self.flags & 8 != 0 + } + pub fn is_mesh(&self) -> bool { + self.flags & 16 != 0 + } + pub fn is_polyface(&self) -> bool { + self.flags & 64 != 0 + } +} + +/// A SPLINE (NURBS), in world coordinates. +#[derive(Clone, Debug, PartialEq)] +pub struct Spline { + /// Group 71 (3 when absent). + pub degree: i64, + /// Group 70: 1 closed, 2 periodic, 4 rational, 8 planar, 16 linear. + pub flags: i64, + pub knots: Vec, + pub control: Vec, + /// Group 41: one per control point when rational. + pub weights: Vec, + pub fit: Vec, + /// Groups 12/22/32 and 13/23/33. + pub start_tangent: Option, + pub end_tangent: Option, + /// Group 44. + pub fit_tolerance: f64, +} + +/// TEXT, and the text of an ATTRIB or ATTDEF. +#[derive(Clone, Debug, PartialEq)] +pub struct Text { + /// Group 10: the first alignment point. + pub insert: Point, + /// Group 11: the second alignment point, which places text that is not left-baseline. + pub align: Option, + pub height: f64, + /// The string, decoded ([`decode_line`]); `%%` codes are left as written. + pub value: String, + /// Group 50, in degrees. + pub rotation: f64, + /// Group 41 (1 when absent). + pub width_factor: f64, + /// Group 51, in degrees. + pub oblique: f64, + /// Group 7 (`Standard` when absent). + pub style: String, + /// Group 71: 2 backwards, 4 upside down. + pub generation: i64, + /// Group 72: 0 left, 1 centre, 2 right, 3 aligned, 4 middle, 5 fit. + pub halign: i64, + /// Group 73 (74 in an ATTRIB or ATTDEF): 0 baseline, 1 bottom, 2 middle, 3 top. + pub valign: i64, +} + +impl Text { + pub fn read(g: &Groups<'_>) -> Text { + Text { + insert: g.point(10), + align: g.point_opt(11), + height: g.f_or(40, 0.0), + value: decode_line(&g.s(1).unwrap_or_default()), + rotation: g.f_or(50, 0.0), + width_factor: g.f_or(41, 1.0), + oblique: g.f_or(51, 0.0), + style: g.name(7).unwrap_or_else(|| "Standard".into()), + generation: g.i_or(71, 0), + halign: g.i_or(72, 0), + valign: g.i(73).or_else(|| g.i(74)).unwrap_or(0), + } + } +} + +/// An ATTRIB (an attribute of a block reference) or ATTDEF (its definition in the block). +#[derive(Clone, Debug, PartialEq)] +pub struct Attrib { + /// Group 2. + pub tag: String, + pub text: Text, + /// Group 3 of an ATTDEF, decoded; empty in an ATTRIB. + pub prompt: String, + /// Group 70: 1 invisible, 2 constant, 4 verify, 8 preset. + pub flags: i64, + /// The 280 after the tag in the attribute's subclass (one before the tag is the subclass + /// version). + pub lock_position: bool, + /// The embedded MTEXT of a multiline attribute. + pub multiline: Option, +} + +impl Attrib { + pub fn read(kind: &str, tags: &[Tag]) -> Attrib { + // A multiline attribute has its text as embedded MTEXT after its own groups. + let (own, embedded) = split_embedded(tags); + let g = Groups(own); + let sub = own.iter().position(|t| t.code == 100 && t.str_ref().is_some_and(|s| s.trim().starts_with("AcDbAttribute"))).unwrap_or(0); + let rest = own.get(sub..).unwrap_or_default(); + let lock_position = rest.iter().position(|t| t.code == 2).is_some_and(|at| rest.iter().skip(at + 1).any(|t| t.code == 280 && t.i64() != 0)); + Attrib { + tag: g.s(2).unwrap_or_default(), + text: Text::read(&g), + prompt: if kind == "ATTDEF" { decode_line(&g.s(3).unwrap_or_default()) } else { String::new() }, + flags: g.i_or(70, 0), + lock_position, + multiline: embedded.filter(|e| !e.is_empty()).map(MText::read), + } + } +} + +/// An INSERT: a block reference, with its attributes. +#[derive(Clone, Debug, PartialEq)] +pub struct Insert { + /// Group 2. + pub block: String, + pub insert: Point, + /// Groups 41, 42 and 43 (1 when absent). + pub scale: Point, + /// Group 50, in degrees. + pub rotation: f64, + /// Groups 70 and 71 (1 when absent or less). + pub columns: i64, + pub rows: i64, + /// Groups 44 and 45. + pub column_spacing: f64, + pub row_spacing: f64, + /// The ATTRIB records that follow, each with its own properties. + pub attribs: Vec<(Common, Attrib)>, +} + +/// A DIMENSION. The geometry it is drawn with is in its block ([`Dimension::block`]). +#[derive(Clone, Debug, PartialEq)] +pub struct Dimension { + /// Group 70: the type in the low bits (0 rotated, 1 aligned, 2 angular, 3 diameter, 4 radius, + /// 5 angular 3-point, 6 ordinate), 32 the block is this dimension's alone, 64 an ordinate's + /// X type, 128 the text is at a user position. + pub flags: i64, + /// Group 2: the block that holds the dimension's picture. + pub block: Option, + /// Group 3 (`Standard` when absent). + pub style: String, + /// Group 10: the definition point. + pub defpt: Point, + /// Group 11: the middle of the text, in the object coordinate system. + pub text_mid: Point, + /// Group 12: the insertion point for clones (baseline and continue). + pub insertion: Point, + /// Groups 13 to 16: the definition points of the type. + pub p13: Point, + pub p14: Point, + pub p15: Point, + pub p16: Point, + /// Group 1, decoded: empty or `<>` for the measurement, a space for none. + pub text: String, + /// Group 42: the measurement, when the file states it. + pub measurement: Option, + /// Group 50: the angle of a rotated dimension, in degrees. + pub rotation: f64, + /// Group 52: the oblique angle of the extension lines, in degrees. + pub oblique: f64, + /// Group 53: the text's rotation, in degrees. + pub text_rotation: f64, + /// Group 51: the horizontal direction, in degrees. + pub horizontal: f64, + /// Group 40: the leader length of a radius or diameter dimension. + pub leader_length: f64, +} + +impl Dimension { + /// The type: the low four bits of the flags. + pub fn kind(&self) -> i64 { + self.flags & 0x0f + } +} + +/// A LEADER. +#[derive(Clone, Debug, PartialEq)] +pub struct Leader { + pub vertices: Vec, + /// Group 71 is not 0. + pub arrow: bool, + /// Group 72 is not 0: a spline path. + pub spline: bool, + /// Group 3 (`Standard` when absent). + pub style: String, + /// Group 73: what the leader was made with (0 text, 1 a tolerance, 2 a block, 3 nothing). + pub annotation_type: i64, + /// Group 340: the annotation. + pub annotation: Option, +} + +/// A paper space VIEWPORT. +#[derive(Clone, Debug, PartialEq)] +pub struct Viewport { + /// Group 10: the centre on the sheet. + pub center: Point, + /// Groups 40 and 41: the size on the sheet. + pub width: f64, + pub height: f64, + /// Group 69: the id (1 is the sheet's own view). Other programs write -1 for unknown. + pub id: i64, + /// Group 90: the status flags (16384 locked, 65536 clipped by the entity of group 340, + /// 131072 off). + pub status: i64, + /// Groups 12/22: the view centre in display coordinates, whose origin is the target. + pub view_center: [f64; 2], + /// Groups 16/26/36 and 17/27/37. + pub view_direction: Point, + pub view_target: Point, + /// Group 45. + pub view_height: f64, + /// Group 51: the twist, in degrees. + pub twist: f64, + /// Group 340: the entity that clips the viewport. + pub clip: Option, + /// Groups 331 and 341: the layers frozen in the viewport. + pub frozen_layers: Vec, +} + +/// An IMAGE or WIPEOUT: a raster's place and clip. The file is named by the IMAGEDEF object. +#[derive(Clone, Debug, PartialEq)] +pub struct Image { + /// Group 10: the lower-left corner. + pub insert: Point, + /// Groups 11 and 12: the vectors of one pixel along the image's width and height. + pub u: Point, + pub v: Point, + /// Groups 13/23: the size in pixels. + pub size: [f64; 2], + /// Group 340: the IMAGEDEF. + pub image_def: Option, + /// Group 70: 1 show, 2 show when not aligned with the screen, 4 use the clip, 8 transparency. + pub display: i64, + /// Group 280 is not 0. + pub clipping: bool, + /// Groups 281, 282 and 283 (50, 50 and 0 when absent). + pub brightness: i64, + pub contrast: i64, + pub fade: i64, + /// Group 71: 1 a rectangle (two corners), 2 a polygon. + pub clip_type: i64, + /// Groups 14/24: the clip boundary in pixels, with the origin at the image's top-left corner + /// and y down: a point `q` is at `insert + u * (q.x + 0.5) + v * (0.5 - q.y)`. + pub clip: Vec<[f64; 2]>, + /// Group 290 is not 0: the clip keeps the outside. + pub clip_inverted: bool, +} + +/// The geometry of an entity. +#[derive(Clone, Debug, PartialEq)] +pub enum EntityKind { + Line { + start: Point, + end: Point, + }, + /// `angle` is group 50: the X axis of the point's symbol, in degrees. + Point { + location: Point, + angle: f64, + }, + Circle { + center: Point, + radius: f64, + }, + Arc { + center: Point, + radius: f64, + start_angle: f64, + end_angle: f64, + }, + /// `major` is the end of the major axis from the centre; `start` and `end` are parameters in + /// radians (0 and 2π for a full ellipse). + Ellipse { + center: Point, + major: Point, + ratio: f64, + start: f64, + end: f64, + }, + LwPolyline(LwPolyline), + Polyline(Polyline), + Spline(Spline), + Ray { + base: Point, + direction: Point, + }, + XLine { + base: Point, + direction: Point, + }, + Text(Text), + MText(MText), + AttDef(Attrib), + Insert(Insert), + Dimension(Dimension), + Leader(Leader), + /// The fourth corner repeats the third when the file has three. + Solid { + corners: [Point; 4], + }, + Trace { + corners: [Point; 4], + }, + /// `hidden_edges` is group 70: bit n hides edge n. + Face3d { + corners: [Point; 4], + hidden_edges: i64, + }, + Hatch(Hatch), + Viewport(Viewport), + Image(Image), + Wipeout(Image), + /// `text` is the feature control frame's string, as written. + Tolerance { + insert: Point, + direction: Point, + style: String, + text: String, + }, + /// A type this module does not model (or a HATCH with no boundary data): read it from the + /// record's tags. + Other, +} + +/// An entity: its properties and geometry. +#[derive(Clone, Debug, PartialEq)] +pub struct Entity { + pub common: Common, + pub kind: EntityKind, +} + +fn lwpolyline(tags: &[Tag], g: &Groups<'_>) -> LwPolyline { + let mut vertices: Vec = Vec::new(); + for t in tags { + if t.code == 10 { + vertices.push(LwVertex { x: t.f64(), ..LwVertex::default() }); + } else if let Some(v) = vertices.last_mut() { + match t.code { + 20 => v.y = t.f64(), + 40 => v.start_width = t.f64(), + 41 => v.end_width = t.f64(), + 42 => v.bulge = t.f64(), + _ => {} + } + } + } + let flags = g.i_or(70, 0); + LwPolyline { vertices, closed: flags & 1 != 0, plinegen: flags & 128 != 0, const_width: g.f_or(43, 0.0), elevation: g.f_or(38, 0.0) } +} + +fn polyline(g: &Groups<'_>, followers: &[Record<'_>]) -> Polyline { + let vertices = followers + .iter() + .map(|r| { + let v = r.groups(); + Vertex { + location: v.point(10), + bulge: v.f_or(42, 0.0), + start_width: v.f_or(40, 0.0), + end_width: v.f_or(41, 0.0), + flags: v.i_or(70, 0), + face: [71, 72, 73, 74].map(|c| v.i_or(c, 0)), + } + }) + .collect(); + Polyline { + flags: g.i_or(70, 0), + elevation: g.f_or(30, 0.0), + start_width: g.f_or(40, 0.0), + end_width: g.f_or(41, 0.0), + m: g.i_or(71, 0), + n: g.i_or(72, 0), + smooth: g.i_or(75, 0), + vertices, + } +} + +fn spline(g: &Groups<'_>) -> Spline { + Spline { + degree: g.i_or(71, 3), + flags: g.i_or(70, 0), + knots: g.all_f(40), + control: g.points(10), + weights: g.all_f(41), + fit: g.points(11), + start_tangent: g.point_opt(12), + end_tangent: g.point_opt(13), + fit_tolerance: g.f_or(44, 0.0), + } +} + +fn insert(g: &Groups<'_>, followers: &[Record<'_>]) -> Insert { + Insert { + block: g.s(2).unwrap_or_default(), + insert: g.point(10), + scale: [g.f_or(41, 1.0), g.f_or(42, 1.0), g.f_or(43, 1.0)], + rotation: g.f_or(50, 0.0), + columns: g.i_or(70, 1).max(1), + rows: g.i_or(71, 1).max(1), + column_spacing: g.f_or(44, 0.0), + row_spacing: g.f_or(45, 0.0), + attribs: followers.iter().map(|r| (Common::read(r.tags), Attrib::read("ATTRIB", r.tags))).collect(), + } +} + +fn dimension(g: &Groups<'_>) -> Dimension { + Dimension { + flags: g.i_or(70, 0), + block: g.name(2), + style: g.name(3).unwrap_or_else(|| "Standard".into()), + defpt: g.point(10), + text_mid: g.point(11), + insertion: g.point(12), + p13: g.point(13), + p14: g.point(14), + p15: g.point(15), + p16: g.point(16), + text: decode_line(&g.s(1).unwrap_or_default()), + measurement: g.f(42), + rotation: g.f_or(50, 0.0), + oblique: g.f_or(52, 0.0), + text_rotation: g.f_or(53, 0.0), + horizontal: g.f_or(51, 0.0), + leader_length: g.f_or(40, 0.0), + } +} + +fn viewport(tags: &[Tag], g: &Groups<'_>) -> Viewport { + Viewport { + center: g.point(10), + width: g.f_or(40, 0.0), + height: g.f_or(41, 0.0), + id: g.i_or(69, 0), + status: g.i_or(90, 0), + view_center: [g.f_or(12, 0.0), g.f_or(22, 0.0)], + view_direction: g.point_opt(16).unwrap_or([0.0, 0.0, 1.0]), + view_target: g.point(17), + view_height: g.f_or(45, 0.0), + twist: g.f_or(51, 0.0), + clip: g.handle(340).filter(|h| *h != 0), + frozen_layers: tags.iter().filter(|t| matches!(t.code, 331 | 341)).filter_map(|t| parse_handle(&t.str())).collect(), + } +} + +fn image(g: &Groups<'_>) -> Image { + Image { + insert: g.point(10), + u: g.point(11), + v: g.point(12), + size: [g.f_or(13, 0.0), g.f_or(23, 0.0)], + image_def: g.handle(340).filter(|h| *h != 0), + display: g.i_or(70, 0), + clipping: g.i_or(280, 0) != 0, + brightness: g.i_or(281, 50), + contrast: g.i_or(282, 50), + fade: g.i_or(283, 0), + clip_type: g.i_or(71, 1), + clip: g.points(14).into_iter().map(|p| [p[0], p[1]]).collect(), + clip_inverted: g.i_or(290, 0) != 0, + } +} + +fn corners(g: &Groups<'_>) -> [Point; 4] { + [g.point(10), g.point(11), g.point(12), g.point_opt(13).unwrap_or_else(|| g.point(12))] +} + +impl Entity { + /// The typed form of an entity record. + pub fn read(e: &EntityRecord<'_>) -> Entity { + let g = e.groups(); + let line = |g: &Groups<'_>| (g.point(10), g.point(11)); + let kind = match e.kind { + "LINE" => EntityKind::Line { start: line(&g).0, end: line(&g).1 }, + "POINT" => EntityKind::Point { location: g.point(10), angle: g.f_or(50, 0.0) }, + "CIRCLE" => EntityKind::Circle { center: g.point(10), radius: g.f_or(40, 0.0) }, + "ARC" => EntityKind::Arc { center: g.point(10), radius: g.f_or(40, 0.0), start_angle: g.f_or(50, 0.0), end_angle: g.f_or(51, 360.0) }, + "ELLIPSE" => EntityKind::Ellipse { + center: g.point(10), + major: g.point(11), + ratio: g.f_or(40, 1.0), + start: g.f_or(41, 0.0), + end: g.f_or(42, std::f64::consts::TAU), + }, + "LWPOLYLINE" => EntityKind::LwPolyline(lwpolyline(e.tags, &g)), + "POLYLINE" => EntityKind::Polyline(polyline(&g, &e.followers)), + "SPLINE" => EntityKind::Spline(spline(&g)), + "RAY" => EntityKind::Ray { base: line(&g).0, direction: line(&g).1 }, + "XLINE" => EntityKind::XLine { base: line(&g).0, direction: line(&g).1 }, + "TEXT" => EntityKind::Text(Text::read(&g)), + "MTEXT" => EntityKind::MText(MText::read(e.tags)), + "ATTDEF" => EntityKind::AttDef(Attrib::read("ATTDEF", e.tags)), + "INSERT" => EntityKind::Insert(insert(&g, &e.followers)), + "DIMENSION" => EntityKind::Dimension(dimension(&g)), + "LEADER" => EntityKind::Leader(Leader { + vertices: g.points(10), + arrow: g.i_or(71, 1) != 0, + spline: g.i_or(72, 0) != 0, + style: g.name(3).unwrap_or_else(|| "Standard".into()), + annotation_type: g.i_or(73, 3), + annotation: g.handle(340).filter(|h| *h != 0), + }), + "SOLID" => EntityKind::Solid { corners: corners(&g) }, + "TRACE" => EntityKind::Trace { corners: corners(&g) }, + "3DFACE" => EntityKind::Face3d { corners: corners(&g), hidden_edges: g.i_or(70, 0) }, + "HATCH" => Hatch::read(e.tags).map_or(EntityKind::Other, EntityKind::Hatch), + "VIEWPORT" => EntityKind::Viewport(viewport(e.tags, &g)), + "IMAGE" => EntityKind::Image(image(&g)), + "WIPEOUT" => EntityKind::Wipeout(image(&g)), + "TOLERANCE" => EntityKind::Tolerance { + insert: g.point(10), + direction: g.point_opt(11).unwrap_or([1.0, 0.0, 0.0]), + style: g.name(3).unwrap_or_else(|| "Standard".into()), + text: g.s(1).unwrap_or_default(), + }, + _ => EntityKind::Other, + }; + Entity { common: Common::read(e.tags), kind } + } +} diff --git a/crates/dxf/src/model/hatch.rs b/crates/dxf/src/model/hatch.rs new file mode 100644 index 000000000..ac3425622 --- /dev/null +++ b/crates/dxf/src/model/hatch.rs @@ -0,0 +1,355 @@ +//! HATCH: the boundary loops (polylines or edges), the pattern definition, the seed points and +//! the gradient, as the file holds them. Boundary points are 2D, in the object coordinate system +//! of the hatch at its elevation. + +use super::{Color, Groups, parse_handle}; +use crate::Tag; + +/// One edge of a boundary loop made of edges. +#[derive(Clone, Debug, PartialEq)] +pub enum HatchEdge { + Line { + start: [f64; 2], + end: [f64; 2], + }, + /// Angles in degrees; a clockwise arc (`ccw` false) has them measured clockwise. + Arc { + center: [f64; 2], + radius: f64, + start_angle: f64, + end_angle: f64, + ccw: bool, + }, + /// `major` is the end of the major axis from the centre, `ratio` the minor axis's length + /// over the major's; angles in degrees, as for an arc. + Ellipse { + center: [f64; 2], + major: [f64; 2], + ratio: f64, + start_angle: f64, + end_angle: f64, + ccw: bool, + }, + Spline { + degree: i64, + rational: bool, + periodic: bool, + knots: Vec, + control: Vec<[f64; 2]>, + /// One per control point when rational; else empty. + weights: Vec, + fit: Vec<[f64; 2]>, + start_tangent: Option<[f64; 2]>, + end_tangent: Option<[f64; 2]>, + }, +} + +/// The path of a boundary loop. +#[derive(Clone, Debug, PartialEq)] +pub enum HatchPath { + /// Vertices as x, y and bulge. + Polyline { + closed: bool, + vertices: Vec<[f64; 3]>, + }, + Edges(Vec), +} + +/// A boundary loop. +#[derive(Clone, Debug, PartialEq)] +pub struct HatchLoop { + /// Group 92: 1 external, 2 a polyline, 4 derived, 8 a text box, 16 outermost. + pub flags: i64, + pub path: HatchPath, + /// Groups 330 after the loop's 97: the entities an associative hatch follows. + pub sources: Vec, +} + +/// One line family of a pattern definition, as drawn: scaled and rotated with the hatch. +#[derive(Clone, Debug, PartialEq, Default)] +pub struct PatternLine { + /// Group 53, in degrees. + pub angle: f64, + /// Groups 43/44: a point the first line passes through. + pub base: [f64; 2], + /// Groups 45/46: the step from one line to the next, as a vector. + pub offset: [f64; 2], + /// Groups 49: dash (positive), gap (negative) and dot (0) lengths; empty for a solid line. + pub dashes: Vec, +} + +/// A gradient fill (groups 450–470). +#[derive(Clone, Debug, PartialEq)] +pub struct Gradient { + /// Group 470 (`LINEAR`, `CYLINDER`, `SPHERICAL`...). + pub name: String, + /// Group 460, in radians. + pub angle: f64, + /// Group 461: 0 centred, 1 shifted up and left. + pub shift: f64, + /// Group 452 is 1: one colour, shaded by `tint` (group 462). + pub single: bool, + pub tint: f64, + /// The colour of each 463 record: its true colour (421), else its index (63). + pub colors: Vec, +} + +/// A HATCH. +#[derive(Clone, Debug, PartialEq)] +pub struct Hatch { + /// Group 2, as written (`SOLID` when absent). + pub pattern: String, + /// Group 70 is 1. + pub solid: bool, + /// Group 71 is not 0. + pub associative: bool, + /// Group 75: 0 normal (odd parity), 1 outer, 2 ignore islands. + pub style: i64, + /// Group 76: 0 user-defined, 1 predefined, 2 custom. + pub pattern_type: i64, + /// Group 52, in degrees. + pub angle: f64, + /// Group 41 (1 when absent). + pub scale: f64, + /// Group 77 is not 0: a user-defined pattern is doubled (crossed). + pub double: bool, + /// The z of the elevation point (the first group 30). + pub elevation: f64, + pub loops: Vec, + pub pattern_lines: Vec, + /// Groups 10/20 after the 98 count. + pub seeds: Vec<[f64; 2]>, + pub gradient: Option, +} + +/// A cursor over a record's tags, for the boundary data, which is read in order. +struct Cur<'a> { + tags: &'a [Tag], + at: usize, +} + +impl<'a> Cur<'a> { + fn peek(&self) -> Option<&'a Tag> { + self.tags.get(self.at) + } + /// The next tag when its code is `code`, consumed. + fn take(&mut self, code: i32) -> Option<&'a Tag> { + let t = self.peek().filter(|t| t.code == code)?; + self.at += 1; + Some(t) + } + fn f(&mut self, code: i32) -> f64 { + self.take(code).map_or(0.0, Tag::f64) + } + fn xy(&mut self, code: i32) -> [f64; 2] { + [self.f(code), self.f(code + 10)] + } + /// Move to the next `code`; false when there is none. + fn seek(&mut self, code: i32) -> bool { + while self.peek().is_some_and(|t| t.code != code) { + self.at += 1; + } + self.peek().is_some() + } +} + +/// A declared count: never trusted for an allocation, only as the most items to read. +fn count(t: &Tag) -> usize { + usize::try_from(t.i64()).unwrap_or(0) +} + +fn polyline_path(c: &mut Cur<'_>) -> Option { + // 72 has bulges, 73 closed, 93 the vertex count. + let mut closed = false; + while let Some(t) = c.peek().filter(|t| t.code != 93) { + if t.code == 73 { + closed = t.i64() != 0; + } else if t.code != 72 { + return None; + } + c.at += 1; + } + let n = count(c.take(93)?); + let mut vertices = Vec::new(); + while vertices.len() < n && c.peek().is_some_and(|t| t.code == 10) { + let [x, y] = c.xy(10); + vertices.push([x, y, c.f(42)]); + } + Some(HatchPath::Polyline { closed, vertices }) +} + +fn spline_edge(c: &mut Cur<'_>) -> HatchEdge { + let (mut degree, mut rational, mut periodic, mut knot_count, mut control_count) = (3, false, false, 0, 0); + while let Some(t) = c.peek().filter(|t| matches!(t.code, 94 | 73 | 74 | 95 | 96)) { + match t.code { + 94 => degree = t.i64(), + 73 => rational = t.i64() != 0, + 74 => periodic = t.i64() != 0, + 95 => knot_count = count(t), + _ => control_count = count(t), + } + c.at += 1; + } + let mut knots = Vec::new(); + while knots.len() < knot_count && c.peek().is_some_and(|t| t.code == 40) { + knots.push(c.f(40)); + } + let (mut control, mut weights) = (Vec::new(), Vec::new()); + while control.len() < control_count && c.peek().is_some_and(|t| t.code == 10) { + control.push(c.xy(10)); + weights.extend(c.take(42).map(Tag::f64)); + } + if weights.len() != control.len() { + weights.clear(); + } + // A 97 here is the edge's fit point count (R2010 and later) when fit points follow it, or + // when it is 0 and the loop's own 97 comes next; otherwise it is the loop's source count. + let mut fit = Vec::new(); + let next = c.tags.get(c.at + 1).map(|t| t.code); + let fit_count = c.peek().filter(|t| t.code == 97 && (next == Some(11) || (t.i64() == 0 && next == Some(97)))); + if let Some(n) = fit_count.map(count) { + c.at += 1; + while fit.len() < n && c.peek().is_some_and(|t| t.code == 11) { + fit.push(c.xy(11)); + } + } + let tangent = |c: &mut Cur<'_>, code: i32| c.peek().is_some_and(|t| t.code == code).then(|| c.xy(code)); + let start_tangent = tangent(c, 12); + let end_tangent = tangent(c, 13); + HatchEdge::Spline { degree, rational, periodic, knots, control, weights, fit, start_tangent, end_tangent } +} + +fn edge_path(c: &mut Cur<'_>) -> Option { + if !c.seek(93) { + return None; + } + let n = count(c.take(93)?); + let mut edges = Vec::new(); + while edges.len() < n { + // Groups this reader does not know may sit between edges; the loop's own end may not. + while c.peek().is_some_and(|t| !matches!(t.code, 72 | 97 | 92 | 75)) { + c.at += 1; + } + if c.peek().is_none_or(|t| t.code != 72) { + break; + } + let kind = c.take(72).map_or(0, Tag::i64); + // 73 is counter-clockwise unless it says 0. + let ccw = |c: &mut Cur<'_>| c.take(73).is_none_or(|t| t.i64() != 0); + edges.push(match kind { + 1 => HatchEdge::Line { start: c.xy(10), end: c.xy(11) }, + 2 => HatchEdge::Arc { center: c.xy(10), radius: c.f(40), start_angle: c.f(50), end_angle: c.f(51), ccw: ccw(c) }, + 3 => HatchEdge::Ellipse { center: c.xy(10), major: c.xy(11), ratio: c.f(40), start_angle: c.f(50), end_angle: c.f(51), ccw: ccw(c) }, + 4 => spline_edge(c), + // An edge type the format does not have: its groups are passed over above. + _ => continue, + }); + } + Some(HatchPath::Edges(edges)) +} + +fn pattern_lines(tags: &[Tag]) -> Vec { + let Some(at) = tags.iter().position(|t| t.code == 78) else { return Vec::new() }; + let mut lines: Vec = Vec::new(); + for t in tags.iter().skip(at + 1) { + if t.code == 53 { + lines.push(PatternLine { angle: t.f64(), ..PatternLine::default() }); + continue; + } + let Some(l) = lines.last_mut() else { break }; + match t.code { + 43 => l.base[0] = t.f64(), + 44 => l.base[1] = t.f64(), + 45 => l.offset[0] = t.f64(), + 46 => l.offset[1] = t.f64(), + 79 => {} + 49 => l.dashes.push(t.f64()), + _ => break, + } + } + lines +} + +/// Groups 450–470, when 450 says gradient. Each 463 starts a colour record whose 63 (index) and +/// 421 (true colour) follow. +fn gradient(tags: &[Tag]) -> Option { + let start = tags.iter().position(|t| t.code == 450)?; + if tags.get(start)?.i64() != 1 { + return None; + } + let mut g = Gradient { name: String::new(), angle: 0.0, shift: 0.0, single: false, tint: 0.0, colors: Vec::new() }; + for t in tags.iter().skip(start + 1).take_while(|t| t.code != 1001) { + match t.code { + 452 => g.single = t.i64() == 1, + 460 => g.angle = t.f64(), + 461 => g.shift = t.f64(), + 462 => g.tint = t.f64(), + 463 => g.colors.push(Color::ByLayer), + 63 => { + // A true colour already read for this record wins over its index. + if let Some(c) = g.colors.last_mut().filter(|c| !matches!(c, Color::Rgb(_))) { + *c = Color::from_index(t.i64()); + } + } + 421 => { + if let Some(c) = g.colors.last_mut() { + *c = Color::from_true(t.i64()); + } + } + 470 => g.name = t.str(), + _ => {} + } + } + Some(g) +} + +impl Hatch { + /// The typed form of a HATCH record; `None` when it has no boundary data (no group 91) or + /// the data ends inside a loop. + pub fn read(tags: &[Tag]) -> Option { + let g = Groups(tags); + let n_loops = count(tags.iter().find(|t| t.code == 91)?); + let mut c = Cur { tags, at: tags.iter().position(|t| t.code == 91)? + 1 }; + let mut loops = Vec::new(); + while loops.len() < n_loops { + if !c.seek(92) { + return None; + } + let flags = c.take(92)?.i64(); + let path = if flags & 2 != 0 { polyline_path(&mut c)? } else { edge_path(&mut c)? }; + // 97 counts the source entities (330) that follow. + let mut sources = Vec::new(); + if let Some(n) = c.take(97).map(count) { + while sources.len() < n + && let Some(t) = c.take(330) + { + sources.extend(parse_handle(&t.str()).filter(|h| *h != 0)); + } + } + loops.push(HatchLoop { flags, path, sources }); + } + let mut seeds = Vec::new(); + if c.seek(98) { + let n = c.take(98).map_or(0, count); + while seeds.len() < n && c.peek().is_some_and(|t| t.code == 10) { + seeds.push(c.xy(10)); + } + } + let pattern = g.name(2).unwrap_or_else(|| "SOLID".into()); + Some(Hatch { + solid: g.i_or(70, 0) == 1, + associative: g.i_or(71, 0) != 0, + style: g.i_or(75, 0), + pattern_type: g.i_or(76, 1), + angle: g.f_or(52, 0.0), + scale: g.f_or(41, 1.0), + double: g.i_or(77, 0) != 0, + elevation: g.f_or(30, 0.0), + loops, + pattern_lines: pattern_lines(tags), + seeds, + gradient: gradient(tags), + pattern, + }) + } +} diff --git a/crates/dxf/src/model/text.rs b/crates/dxf/src/model/text.rs new file mode 100644 index 000000000..ffafae96d --- /dev/null +++ b/crates/dxf/src/model/text.rs @@ -0,0 +1,280 @@ +//! Text in DXF: the caret encoding of control characters in strings, and MTEXT with its columns. +//! +//! Control characters are written as a caret and a letter (`^I` a tab, `^J` a line feed, `^ ` a +//! caret). TEXT, ATTDEF, ATTRIB and DIMENSION strings use the same encoding as MTEXT contents, +//! without line feeds: AutoCAD reads a bare `^2` in `10^2` as a control character. +//! +//! MTEXT columns are groups 75 (type), 76 (count), 78 (flow reversed), 79 (auto height), 48 +//! (width), 49 (gutter) and 50 (column heights, after the column type; before it, 50 is the +//! rotation) before R2018, and an embedded object (group 101) from R2018 on. + +use super::{Groups, Point, outside_app}; +use crate::Tag; + +/// Decode MTEXT contents: `^I` (tab), `^J` (line feed) and `^ ` (caret). Other carets stay as +/// they are (older files write stacked tolerances `\S+0.1^-0.1;` with a bare caret). +pub fn decode_contents(s: &str) -> String { + decode(s, true) +} + +/// A single-line string as read: [`decode_contents`] without line feeds (`^J` stays as it is). +pub fn decode_line(s: &str) -> String { + decode(s, false) +} + +fn decode(s: &str, line_feeds: bool) -> String { + if !s.contains('^') { + return s.to_string(); + } + let mut out = String::with_capacity(s.len()); + let mut it = s.chars().peekable(); + while let Some(c) = it.next() { + if c != '^' { + out.push(c); + continue; + } + match it.peek().copied() { + Some(' ') => out.push('^'), + Some('I') => out.push('\t'), + Some('J') if line_feeds => out.push('\n'), + _ => { + out.push('^'); + continue; + } + } + it.next(); + } + out +} + +/// A single-line string (TEXT, ATTDEF/ATTRIB, DIMENSION) for a DXF file: a caret as `^ ` and a +/// tab as `^I`, read back by [`decode_line`]. It must hold no line break. +pub fn encode_line(s: &str) -> String { + s.replace('^', "^ ").replace('\t', "^I") +} + +/// Encode MTEXT contents: a tab as `^I`, a line feed as `^J`, a caret as `^ `; other control +/// characters (which draw nothing) are dropped. +pub fn encode_contents(s: &str) -> String { + if !s.chars().any(|c| c == '^' || c.is_control()) { + return s.to_string(); + } + let mut out = String::with_capacity(s.len() + 8); + for c in s.chars() { + match c { + '^' => out.push_str("^ "), + '\t' => out.push_str("^I"), + '\n' => out.push_str("^J"), + c if c.is_control() => {} + c => out.push(c), + } + } + out +} + +/// Apply `f` ([`encode_line`] or [`decode_line`]) to the groups of a `kind` record that hold an +/// encoded string: the text (1), and an ATTDEF's prompt (3). An embedded MTEXT (from group 101) +/// has its own and is left alone. +pub fn map_text_groups(kind: &str, tags: &mut [Tag], f: fn(&str) -> String) { + let kind = kind.to_ascii_uppercase(); + if !matches!(kind.as_str(), "TEXT" | "ATTRIB" | "ATTDEF" | "DIMENSION") { + return; + } + for t in tags.iter_mut().take_while(|t| t.code != 101) { + if t.code == 1 || (t.code == 3 && kind == "ATTDEF") { + *t = Tag::s(t.code, f(&t.str())); + } + } +} + +/// A record's groups split at its embedded object (`101 Embedded Object`): its own groups and, +/// when it has one, the object's. +pub fn split_embedded(tags: &[Tag]) -> (&[Tag], Option<&[Tag]>) { + match tags.iter().position(|t| t.code == 101) { + Some(i) => (tags.get(..i).unwrap_or(tags), tags.get(i + 1..)), + None => (tags, None), + } +} + +/// Upper bound on the column heights read from one MTEXT. +pub const MAX_COLUMNS: usize = 1_000; + +/// The columns of an MTEXT. +#[derive(Clone, Debug, PartialEq)] +pub struct MTextColumns { + /// Dynamic columns (type 2): the text flows into as many as it needs. Otherwise static + /// (type 1). + pub dynamic: bool, + pub count: u16, + /// One column's width (the MTEXT's own width is the total in some writers' files). + pub width: f64, + pub gutter: f64, + /// The column height: the tallest for manual heights. + pub height: f64, + pub auto_height: bool, + /// Each column's height, for dynamic columns with manual heights; else empty. + pub heights: Vec, + pub flow_reversed: bool, +} + +fn pos(v: f64) -> f64 { + v.max(0.0) +} + +impl MTextColumns { + /// The columns of an MTEXT record, from the legacy groups or the embedded object. + pub fn read(tags: &[Tag]) -> Option { + let (own, embedded) = split_embedded(tags); + // An application-defined group in the record may hold any code, 50 included. + let own_tags: Vec<&Tag> = outside_app(own).map(|(_, t)| t).collect(); + let Some(start) = own_tags.iter().position(|t| t.code == 75) else { return Self::read_embedded(own, embedded?) }; + let dynamic = match own_tags.get(start).map(|t| t.i64()) { + Some(1) => false, + Some(2) => true, + _ => return None, + }; + let cols = own_tags.get(start..).unwrap_or_default(); + let first = |code: i32| cols.iter().find(|t| t.code == code).copied(); + // The first 50 is the number of heights that follow, unless the file has the heights + // of group 76 alone. + let fifties = cols.iter().filter(|t| t.code == 50); + let counted = first(76).is_some_and(|t| i64::try_from(fifties.clone().count()).is_ok_and(|n| n - 1 == t.i64())); + let heights: Vec = fifties.skip(usize::from(counted)).take(MAX_COLUMNS).map(|t| pos(t.f64())).collect(); + let count = first(76).map_or(heights.len().max(1), |t| usize::try_from(t.i64()).unwrap_or(1)).clamp(1, MAX_COLUMNS); + let auto_height = first(79).is_some_and(|t| t.i64() != 0); + let defined = own_tags.iter().find(|t| t.code == 46).map_or(0.0, |t| pos(t.f64())); + let manual = dynamic && !auto_height; + let height = + if manual { heights.iter().copied().fold(0.0, f64::max) } else { heights.first().copied().filter(|h| *h > 0.0).unwrap_or(defined) }; + Some(MTextColumns { + dynamic, + count: u16::try_from(count).unwrap_or(1), + width: first(48).map_or(0.0, |t| pos(t.f64())), + gutter: first(49).map_or(0.0, |t| pos(t.f64())), + height, + auto_height, + heights: if manual { heights } else { Vec::new() }, + flow_reversed: first(78).is_some_and(|t| t.i64() != 0), + }) + } + + /// The columns of an R2018 MTEXT, from its embedded object, whose groups reuse MTEXT's + /// codes: 71 type, 72 count, 44 column width, 45 gutter, 73 auto height, 74 flow reversed, + /// 46 heights. + #[expect(clippy::cast_possible_truncation, reason = "the rounded count is clamped to 1..=MAX_COLUMNS before use")] + fn read_embedded(own: &[Tag], obj: &[Tag]) -> Option { + let first = |code: i32| obj.iter().find(|t| t.code == code); + let dynamic = match first(71).map(Tag::i64) { + Some(1) => false, + Some(2) => true, + _ => return None, + }; + let heights: Vec = obj.iter().filter(|t| t.code == 46).take(MAX_COLUMNS).map(|t| pos(t.f64())).collect(); + let auto_height = first(73).is_some_and(|t| t.i64() != 0); + let manual = dynamic && !auto_height; + // The column height: the entity's own 46 (defined height), else the object's 41 or first 46. + let own_46 = own.iter().find(|t| t.code == 46).map(|t| pos(t.f64())); + let defined = [own_46, first(41).map(Tag::f64), heights.first().copied()].into_iter().flatten().find(|h| *h > 0.0).unwrap_or(0.0); + // A dynamic auto-height object stores 0 columns in 72: the count follows from the total + // width (42 = n * 44 + (n - 1) * 45). + let [width, gutter, total] = [44, 45, 42].map(|g| first(g).map_or(0.0, Tag::f64)); + let count = match first(72).map(Tag::i64) { + Some(n) if n > 0 => n, + Some(_) if width + gutter > 0.0 && total > 0.0 => ((total + gutter) / (width + gutter)).round().clamp(1.0, MAX_COLUMNS as f64) as i64, + _ => 1, + }; + Some(MTextColumns { + dynamic, + count: u16::try_from(count.clamp(1, MAX_COLUMNS as i64)).unwrap_or(1), + width: pos(width), + gutter: pos(gutter), + height: if manual { heights.iter().copied().fold(0.0, f64::max) } else { defined }, + auto_height, + heights: if manual { heights } else { Vec::new() }, + flow_reversed: first(74).is_some_and(|t| t.i64() != 0), + }) + } +} + +/// Multiline text: an MTEXT entity, or the one embedded in a multiline attribute. +#[derive(Clone, Debug, PartialEq)] +pub struct MText { + /// Group 10. + pub insert: Point, + /// Group 40: the text height. + pub height: f64, + /// Group 41: the reference rectangle's width (0: none). With columns, see + /// [`MTextColumns::width`]. + pub width: f64, + /// Group 71: 1–9, top left to bottom right. + pub attach: u8, + /// Group 72: 1 left to right, 3 top to bottom, 5 by style. + pub drawing_direction: i64, + /// Groups 11/21/31: the X axis direction, which wins over the rotation when given and not + /// zero. + pub direction: Option, + /// Group 50, in degrees. + pub rotation: f64, + /// Group 7 (`Standard` when absent). + pub style: String, + /// The contents (the group 3 chunks, then group 1), decoded ([`decode_contents`]); the + /// formatting codes (`\P`, `\f...;`) are left as written. + pub contents: String, + /// Group 44: the line spacing factor (1 when absent). + pub line_spacing: f64, + /// Group 73 is 2: the spacing is exact, not "at least". + pub line_spacing_exact: bool, + /// Group 90: the background fill flags (1 a fill colour, 2 the window's colour, 16 a + /// frame), with the fill colour (63, 421) and the border offset factor (45). + pub background: Option<(i64, super::Color, f64)>, + pub columns: Option, +} + +impl MText { + /// The typed form of MTEXT groups. + pub fn read(tags: &[Tag]) -> MText { + let columns = MTextColumns::read(tags); + // The MTEXT's own groups: an embedded object reuses their codes (44 is its column + // width, not the line spacing factor). + let (own, _) = split_embedded(tags); + let g = Groups(own); + let mut contents = String::new(); + for t in own.iter().filter(|t| t.code == 3) { + contents.push_str(&t.str()); + } + contents.push_str(&g.s(1).unwrap_or_default()); + // The direction and rotation come from before the column groups, where 50 is a height. + let head = Groups(own.get(..own.iter().position(|t| t.code == 75).unwrap_or(own.len())).unwrap_or(own)); + let background = g.i(90).filter(|f| *f != 0).map(|f| { + let color = match (g.i(421), g.i(63)) { + (Some(tc), _) => super::Color::from_true(tc), + (None, Some(v)) => super::Color::from_index(v), + (None, None) => super::Color::ByLayer, + }; + (f, color, g.f_or(45, 1.5)) + }); + MText { + insert: g.point(10), + height: g.f_or(40, 0.0), + width: pos(g.f_or(41, 0.0)), + attach: u8::try_from(g.i_or(71, 1).clamp(1, 9)).unwrap_or(1), + drawing_direction: g.i_or(72, 1), + direction: head.point_opt(11).filter(|d| d[0] != 0.0 || d[1] != 0.0 || d[2] != 0.0), + rotation: head.f_or(50, 0.0), + style: g.name(7).unwrap_or_else(|| "Standard".into()), + contents: decode_contents(&contents), + line_spacing: g.f_or(44, 1.0), + line_spacing_exact: g.i(73) == Some(2), + background, + columns, + } + } + /// The angle of the text's X axis in radians: of the direction when the record has one, + /// else the rotation. + pub fn angle(&self) -> f64 { + match self.direction { + Some(d) if d[0] != 0.0 || d[1] != 0.0 => d[1].atan2(d[0]), + _ => self.rotation.to_radians(), + } + } +} diff --git a/crates/dxf/tests/model_entities.rs b/crates/dxf/tests/model_entities.rs new file mode 100644 index 000000000..5de59761b --- /dev/null +++ b/crates/dxf/tests/model_entities.rs @@ -0,0 +1,393 @@ +//! The drawing model's entities: common properties, geometry, text, hatches, dimensions. + +use craft_dxf::model::text::{MTextColumns, decode_contents, decode_line, encode_contents, encode_line, map_text_groups}; +use craft_dxf::model::{Color, Document, Entity, EntityKind, HatchEdge, HatchPath, Lineweight, Transparency, entity_records, records}; +use craft_dxf::{Tag, parse}; + +fn tags(text: &str) -> Vec { + let lines: Vec<&str> = text.split(['\n', '|']).map(str::trim).filter(|l| !l.is_empty()).map(|l| if l == "~" { "" } else { l }).collect(); + parse((lines.join("\n") + "\n").as_bytes()).expect("the test drawing parses") +} + +/// The entities of `text`, the records of an ENTITIES section. +fn entities(text: &str) -> Vec { + let t = tags(text); + let (recs, strays) = entity_records(&records(&t)); + assert_eq!(strays, 0); + recs.iter().map(Entity::read).collect() +} + +fn one(text: &str) -> Entity { + let mut e = entities(text); + assert_eq!(e.len(), 1); + e.remove(0) +} + +#[test] +fn common_properties() { + let c = one("0|LINE|5|2A|330|1F|100|AcDbEntity|8|Walls|6|DASHED|62|1|420|65280|370|50|48|2.5|60|1|67|1|440|33554687|390|B|100|AcDbLine|39|1.5|210|0.0|220|0.0|230|-1.0").common; + assert_eq!((c.handle, c.owner, c.layer.as_str(), c.linetype.as_deref()), (Some(0x2A), Some(0x1F), "Walls", Some("DASHED"))); + assert_eq!((c.color, c.lineweight, c.ltscale), (Color::Rgb([0, 255, 0]), Lineweight::Hundredths(50), 2.5)); + assert!(!c.visible && c.paper_space); + assert_eq!((c.transparency, c.thickness, c.extrusion, c.plot_style), (Transparency::Alpha(255), 1.5, [0.0, 0.0, -1.0], Some(0xB))); + let d = one("0|LINE").common; + assert_eq!((d.handle, d.owner, d.layer.as_str(), d.linetype.as_deref(), d.color), (None, None, "0", None, Color::ByLayer)); + assert_eq!((d.lineweight, d.ltscale, d.visible, d.paper_space, d.extrusion), (Lineweight::ByLayer, 1.0, true, false, [0.0, 0.0, 1.0])); + assert_eq!(one("0|LINE|6|BYLAYER|48|-1.0|62|-2").common, d, "ByLayer by name, a negative scale and a negative colour are the defaults"); +} + +#[test] +fn entity_properties_are_not_read_from_the_entitys_own_subclass() { + // An MTEXT's 48 is its column width, not the linetype scale, and a marker in an + // application's braces is the application's: it does not end the entity's groups. A record with no AcDbEntity marker has its + // layer wherever it is. + let c = one("0|MTEXT|102|{APP|100|AcDbMText|102|}|100|AcDbEntity|8|A|100|AcDbMText|48|7.0|62|3").common; + assert_eq!((c.ltscale, c.color, c.layer.as_str()), (1.0, Color::ByLayer, "A")); + let c = one("0|LINE|100|AcDbLine|8|B|62|3").common; + assert_eq!((c.color, c.layer.as_str()), (Color::Index(3), "B")); + // An embedded object ends the entity's groups. + assert_eq!(one("0|ATTDEF|8|C|101|Embedded Object|62|5").common.color, Color::ByLayer); +} + +#[test] +fn curves() { + let k = |text: &str| one(text).kind; + assert_eq!(k("0|LINE|10|1.0|20|2.0|30|3.0|11|4.0|21|5.0"), EntityKind::Line { start: [1.0, 2.0, 3.0], end: [4.0, 5.0, 0.0] }); + assert_eq!(k("0|POINT|10|1.0|50|30.0"), EntityKind::Point { location: [1.0, 0.0, 0.0], angle: 30.0 }); + assert_eq!(k("0|CIRCLE|10|1.0|20|2.0|40|3.0"), EntityKind::Circle { center: [1.0, 2.0, 0.0], radius: 3.0 }); + assert_eq!(k("0|ARC|10|1.0|40|3.0|50|10.0"), EntityKind::Arc { center: [1.0, 0.0, 0.0], radius: 3.0, start_angle: 10.0, end_angle: 360.0 }); + assert_eq!( + k("0|ELLIPSE|10|1.0|11|2.0|21|0.0|40|0.5|41|0.0|42|3.0"), + EntityKind::Ellipse { center: [1.0, 0.0, 0.0], major: [2.0, 0.0, 0.0], ratio: 0.5, start: 0.0, end: 3.0 } + ); + assert_eq!(k("0|RAY|10|1.0|11|0.0|21|1.0"), EntityKind::Ray { base: [1.0, 0.0, 0.0], direction: [0.0, 1.0, 0.0] }); + assert_eq!(k("0|XLINE|10|1.0|11|0.0|21|1.0"), EntityKind::XLine { base: [1.0, 0.0, 0.0], direction: [0.0, 1.0, 0.0] }); + let EntityKind::Spline(s) = k( + "0|SPLINE|70|8|71|2|40|0.0|40|0.0|40|1.0|10|0.0|20|0.0|10|1.0|20|1.0|30|5.0|10|2.0|41|1.0|41|2.0|41|1.0|11|9.0|21|9.0|12|1.0|22|0.0|44|0.01", + ) else { + panic!("a spline") + }; + assert_eq!((s.degree, s.flags, s.knots.len(), s.weights.as_slice(), s.fit_tolerance), (2, 8, 3, [1.0, 2.0, 1.0].as_slice(), 0.01)); + assert_eq!(s.control, [[0.0, 0.0, 0.0], [1.0, 1.0, 5.0], [2.0, 0.0, 0.0]], "the third point has no y or z of its own"); + assert_eq!((s.fit.as_slice(), s.start_tangent, s.end_tangent), ([[9.0, 9.0, 0.0]].as_slice(), Some([1.0, 0.0, 0.0]), None)); +} + +#[test] +fn polylines() { + let EntityKind::LwPolyline(p) = one("0|LWPOLYLINE|90|3|70|129|43|0.5|38|2.0|10|0.0|20|0.0|42|1.0|10|4.0|20|0.0|40|0.1|41|0.2|10|4.0|20|3.0").kind + else { + panic!("a lightweight polyline") + }; + assert!(p.closed && p.plinegen); + assert_eq!((p.const_width, p.elevation, p.vertices.len()), (0.5, 2.0, 3)); + assert_eq!((p.vertices[0].bulge, p.vertices[1].start_width, p.vertices[1].end_width, p.vertices[2].y), (1.0, 0.1, 0.2, 3.0)); + // A polyface mesh: three vertices and a face record that names them. + let e = one( + "0|POLYLINE|66|1|70|64|71|3|72|1|10|0.0|20|0.0|30|7.0|0|VERTEX|10|0.0|20|0.0|30|0.0|70|192|0|VERTEX|10|1.0|70|192|0|VERTEX|10|1.0|20|1.0|70|192|0|VERTEX|70|128|71|1|72|2|73|-3|0|SEQEND", + ); + let EntityKind::Polyline(m) = e.kind else { panic!("a polyline") }; + assert!(m.is_polyface() && !m.is_mesh() && !m.is_3d() && !m.closed()); + assert_eq!((m.m, m.n, m.elevation, m.vertices.len()), (3, 1, 7.0, 4)); + assert!(m.vertices[3].is_face() && !m.vertices[0].is_face()); + assert_eq!(m.vertices[3].face, [1, 2, -3, 0]); + let EntityKind::Polyline(h) = one("0|POLYLINE|70|4|40|0.5|41|0.25|0|VERTEX|10|1.0|42|0.5|40|0.1|41|0.2|70|16|0|SEQEND").kind else { + panic!("a polyline") + }; + assert_eq!((h.start_width, h.end_width), (0.5, 0.25)); + let v = h.vertices[0]; + assert!(v.is_spline_frame()); + assert_eq!((v.bulge, v.start_width, v.end_width), (0.5, 0.1, 0.2)); +} + +#[test] +fn text_and_attributes() { + let EntityKind::Text(t) = one("0|TEXT|10|1.0|20|2.0|11|3.0|21|4.0|40|2.5|1|10^ 2^Im|50|90.0|41|0.8|51|15.0|7|Notes|71|2|72|1|73|2").kind else { + panic!("text") + }; + assert_eq!((t.insert, t.align, t.height, t.value.as_str()), ([1.0, 2.0, 0.0], Some([3.0, 4.0, 0.0]), 2.5, "10^2\tm")); + assert_eq!((t.rotation, t.width_factor, t.oblique, t.style.as_str(), t.generation, t.halign, t.valign), (90.0, 0.8, 15.0, "Notes", 2, 1, 2)); + let EntityKind::Text(d) = one("0|TEXT|1|x").kind else { panic!("text") }; + assert_eq!((d.align, d.width_factor, d.style.as_str(), d.halign, d.valign), (None, 1.0, "Standard", 0, 0)); + + let EntityKind::AttDef(a) = + one("0|ATTDEF|100|AcDbText|10|1.0|1|default|100|AcDbAttributeDefinition|280|0|3|Enter^Ia|2|TAG|70|12|74|3|280|1").kind + else { + panic!("an attribute definition") + }; + assert_eq!((a.tag.as_str(), a.prompt.as_str(), a.flags, a.text.valign, a.lock_position), ("TAG", "Enter\ta", 12, 3, true)); + assert!(a.multiline.is_none()); + + // A block reference with two attributes; the second is multiline. + let EntityKind::Insert(i) = one("0|INSERT|66|1|2|DOOR|10|1.0|20|2.0|41|2.0|43|-1.0|50|45.0|70|3|71|0|44|5.0|45|6.0 + 0|ATTRIB|8|Tags|62|2|1|one|2|A|70|1|280|0 + 0|ATTRIB|1|~|2|B|101|Embedded Object|10|7.0|40|2.0|71|5|3|line one^J|1|line two + 0|SEQEND") + .kind + else { + panic!("an insert") + }; + assert_eq!((i.block.as_str(), i.insert, i.scale, i.rotation), ("DOOR", [1.0, 2.0, 0.0], [2.0, 1.0, -1.0], 45.0)); + assert_eq!((i.columns, i.rows, i.column_spacing, i.row_spacing), (3, 1, 5.0, 6.0)); + assert_eq!(i.attribs.len(), 2); + let (c, a) = &i.attribs[0]; + assert_eq!( + (c.layer.as_str(), c.color, a.tag.as_str(), a.text.value.as_str(), a.flags, a.lock_position), + ("Tags", Color::Index(2), "A", "one", 1, false) + ); + assert!(a.prompt.is_empty(), "an ATTRIB has no prompt"); + let m = i.attribs[1].1.multiline.as_ref().expect("embedded MTEXT"); + assert_eq!((m.contents.as_str(), m.insert, m.height, m.attach), ("line one\nline two", [7.0, 0.0, 0.0], 2.0, 5)); +} + +#[test] +fn caret_encoding() { + assert_eq!(decode_contents("a^Ib^Jc^ d^2^"), "a\tb\nc^d^2^"); + assert_eq!(decode_line("a^Ib^Jc^ d"), "a\tb^Jc^d", "a single line has no line feed"); + assert_eq!(encode_contents("a\tb\nc^d\u{7}"), "a^Ib^Jc^ d", "other control characters are dropped"); + assert_eq!(encode_line("10^2\t"), "10^ 2^I"); + for s in ["", "plain", "10^2", "tab\there", "^^ ^I"] { + assert_eq!(decode_line(&encode_line(s)), s); + assert_eq!(decode_contents(&encode_contents(s)), s); + } + let mut t = vec![Tag::s(1, "a^ b"), Tag::s(3, "p^ q"), Tag::i(101, 0), Tag::s(1, "e^ f")]; + map_text_groups("attdef", &mut t, decode_line); + assert_eq!(t.iter().map(Tag::str).collect::>(), ["a^b", "p^q", "0", "e^ f"], "the embedded object's text is left alone"); + let mut t = vec![Tag::s(1, "a^ b"), Tag::s(3, "p^ q")]; + map_text_groups("TEXT", &mut t, decode_line); + assert_eq!(t[1].str(), "p^ q", "only an ATTDEF's 3 is a prompt"); + map_text_groups("LINE", &mut t, |_| String::new()); + assert_eq!(t[0].str(), "a^b"); +} + +#[test] +fn mtext() { + let EntityKind::MText(m) = one("0|MTEXT|10|1.0|20|2.0|40|2.5|41|40.0|71|5|72|1|3|first ^Ipart, |3|second, |1|last\\Pline|7|Notes|11|0.0|21|1.0|50|30.0|44|1.2|73|2|90|1|63|4|45|1.25").kind else { + panic!("multiline text") + }; + assert_eq!((m.insert, m.height, m.width, m.attach, m.style.as_str()), ([1.0, 2.0, 0.0], 2.5, 40.0, 5, "Notes")); + assert_eq!(m.contents, "first \tpart,second,last\\Pline"); + assert_eq!((m.direction, m.rotation, m.line_spacing, m.line_spacing_exact), (Some([0.0, 1.0, 0.0]), 30.0, 1.2, true)); + assert!((m.angle() - std::f64::consts::FRAC_PI_2).abs() < 1e-12, "the direction wins over the rotation"); + assert_eq!(m.background, Some((1, Color::Index(4), 1.25))); + assert!(m.columns.is_none()); + let EntityKind::MText(r) = one("0|MTEXT|1|x|50|90.0|11|0.0|21|0.0|71|99").kind else { panic!("multiline text") }; + assert!((r.angle() - std::f64::consts::FRAC_PI_2).abs() < 1e-12, "a zero direction is none"); + assert_eq!((r.attach, r.background, r.line_spacing), (9, None, 1.0)); +} + +#[test] +fn mtext_columns() { + // Legacy groups: static, 2 columns, the first 50 after 75 counts the heights; the 50 before + // 75 is the rotation. + let t = tags("0|MTEXT|10|0.0|50|15.0|1|x|75|1|76|2|78|1|79|0|48|30.0|49|5.0|50|2|50|80.0|50|80.0"); + let c = MTextColumns::read(&t[1..]).expect("columns"); + assert_eq!((c.dynamic, c.count, c.width, c.gutter, c.height, c.auto_height, c.flow_reversed), (false, 2, 30.0, 5.0, 80.0, false, true)); + assert!(c.heights.is_empty(), "only manual dynamic columns keep each height"); + let EntityKind::MText(m) = one("0|MTEXT|10|0.0|50|15.0|1|x|75|1|76|2|48|30.0|49|5.0|50|2|50|80.0|50|80.0").kind else { panic!("multiline text") }; + assert_eq!(m.rotation, 15.0); + // Dynamic with manual heights, and no count before them. + let t = tags("0|MTEXT|1|x|75|2|76|3|79|0|48|30.0|49|5.0|50|10.0|50|40.0|50|20.0"); + let c = MTextColumns::read(&t[1..]).expect("columns"); + assert_eq!((c.dynamic, c.count, c.height, c.heights.as_slice()), (true, 3, 40.0, [10.0, 40.0, 20.0].as_slice())); + // R2018: the embedded object; 72 is 0 for dynamic auto-height, the count follows the widths. + let t = tags("0|MTEXT|1|x|46|50.0|101|Embedded Object|70|1|10|1.0|11|0.0|40|0.0|41|0.0|42|65.0|43|0.0|71|2|72|0|44|30.0|45|5.0|73|1|74|0"); + let c = MTextColumns::read(&t[1..]).expect("columns"); + assert_eq!((c.dynamic, c.count, c.width, c.gutter, c.height, c.auto_height), (true, 2, 30.0, 5.0, 50.0, true)); + // No columns: type 0, or no column groups at all. + assert!(MTextColumns::read(&tags("0|MTEXT|1|x|75|0|76|0")[1..]).is_none()); + assert!(MTextColumns::read(&tags("0|MTEXT|1|x|101|Embedded Object|71|0")[1..]).is_none()); +} + +#[test] +fn the_column_height_limit() { + let mut text = String::from("0|MTEXT|1|x|75|2|76|5000|79|0"); + for _ in 0..5000 { + text.push_str("|50|1.0"); + } + let t = tags(&text); + let c = MTextColumns::read(&t[1..]).expect("columns"); + assert_eq!((c.heights.len(), c.count), (1000, 1000)); + let mut text = String::from("0|MTEXT|1|x|101|Embedded Object|71|2|72|40000|73|0"); + for _ in 0..5000 { + text.push_str("|46|1.0"); + } + let t = tags(&text); + let c = MTextColumns::read(&t[1..]).expect("columns"); + assert_eq!((c.heights.len(), c.count), (1000, 1000)); + // A total width that asks for a count beyond any number. + let t = tags("0|MTEXT|1|x|101|Embedded Object|71|2|72|0|73|1|42|1e300|44|1e-300|45|0.0"); + assert_eq!(MTextColumns::read(&t[1..]).expect("columns").count, 1000); +} + +#[test] +fn dimensions_and_leaders() { + let EntityKind::Dimension(d) = + one("0|DIMENSION|2|*D1|3|ISO-25|10|1.0|20|2.0|11|3.0|21|4.0|12|0.5|13|5.0|14|6.0|15|7.0|16|8.0|70|161|1|<> ^Im|42|12.5|50|30.0|52|60.0|53|45.0|40|2.0").kind + else { + panic!("a dimension") + }; + assert_eq!((d.kind(), d.flags & 128, d.flags & 32), (1, 128, 32)); + assert_eq!((d.block.as_deref(), d.style.as_str(), d.text.as_str(), d.measurement), (Some("*D1"), "ISO-25", "<> \tm", Some(12.5))); + assert_eq!((d.defpt, d.text_mid, d.insertion[0]), ([1.0, 2.0, 0.0], [3.0, 4.0, 0.0], 0.5)); + assert_eq!((d.p13[0], d.p14[0], d.p15[0], d.p16[0]), (5.0, 6.0, 7.0, 8.0)); + assert_eq!((d.rotation, d.oblique, d.text_rotation, d.leader_length), (30.0, 60.0, 45.0, 2.0)); + let EntityKind::Dimension(n) = one("0|DIMENSION|70|6").kind else { panic!("a dimension") }; + assert_eq!((n.block.as_deref(), n.style.as_str(), n.measurement, n.kind()), (None, "Standard", None, 6)); + + // The second vertex has no z: it does not take the third's. + let EntityKind::Leader(l) = one("0|LEADER|3|Arrows|71|0|72|1|73|0|340|4F|10|1.0|20|2.0|30|3.0|10|4.0|20|5.0|10|6.0|20|7.0|30|8.0").kind else { + panic!("a leader") + }; + assert_eq!(l.vertices, [[1.0, 2.0, 3.0], [4.0, 5.0, 0.0], [6.0, 7.0, 8.0]]); + assert!(!l.arrow && l.spline); + assert_eq!((l.style.as_str(), l.annotation_type, l.annotation), ("Arrows", 0, Some(0x4F))); +} + +#[test] +fn faces_viewports_images_and_tolerances() { + let k = |text: &str| one(text).kind; + let three = "|10|0.0|11|1.0|12|1.0|22|1.0"; + assert_eq!(k(&format!("0|SOLID{three}")), EntityKind::Solid { corners: [[0.0; 3], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [1.0, 1.0, 0.0]] }); + assert!(matches!(k(&format!("0|TRACE{three}|13|9.0")), EntityKind::Trace { corners } if corners[3] == [9.0, 0.0, 0.0])); + assert!(matches!(k(&format!("0|3DFACE{three}|70|5")), EntityKind::Face3d { hidden_edges: 5, .. })); + let EntityKind::Viewport(v) = + k("0|VIEWPORT|10|5.0|20|4.0|40|8.0|41|6.0|69|2|90|81920|12|1.0|22|2.0|17|9.0|45|30.0|51|90.0|340|A1|331|B1|331|B2|341|B3") + else { + panic!("a viewport") + }; + assert_eq!((v.center, v.width, v.height, v.id, v.status & 16384, v.status & 131_072), ([5.0, 4.0, 0.0], 8.0, 6.0, 2, 16384, 0)); + assert_eq!((v.view_center, v.view_target, v.view_direction, v.view_height, v.twist), ([1.0, 2.0], [9.0, 0.0, 0.0], [0.0, 0.0, 1.0], 30.0, 90.0)); + assert_eq!((v.clip, v.frozen_layers.as_slice()), (Some(0xA1), [0xB1, 0xB2, 0xB3].as_slice())); + let EntityKind::Image(i) = + k("0|IMAGE|10|1.0|11|0.5|12|0.0|22|0.5|13|640.0|23|480.0|340|C1|70|7|280|1|281|60|71|2|14|0.0|24|0.0|14|10.0|24|0.0|14|10.0|24|10.0|290|1") + else { + panic!("an image") + }; + assert_eq!((i.insert[0], i.u[0], i.v[1], i.size, i.image_def), (1.0, 0.5, 0.5, [640.0, 480.0], Some(0xC1))); + assert_eq!((i.display, i.clipping, i.brightness, i.contrast, i.fade, i.clip_type, i.clip_inverted), (7, true, 60, 50, 0, 2, true)); + assert_eq!(i.clip, [[0.0, 0.0], [10.0, 0.0], [10.0, 10.0]]); + assert!(matches!(k("0|WIPEOUT|10|1.0|14|-0.5|24|-0.5|14|0.5|24|0.5"), EntityKind::Wipeout(w) if w.clip.len() == 2 && w.clip_type == 1)); + assert_eq!( + k("0|TOLERANCE|3|Tol|10|1.0|1|{\\Fgdt;j}%%v0.1"), + EntityKind::Tolerance { insert: [1.0, 0.0, 0.0], direction: [1.0, 0.0, 0.0], style: "Tol".into(), text: "{\\Fgdt;j}%%v0.1".into() } + ); + assert_eq!(k("0|MULTILEADER|10|1.0"), EntityKind::Other, "a type the model does not have is read from its tags"); +} + +#[test] +fn hatch_with_a_polyline_loop_and_a_pattern() { + let EntityKind::Hatch(h) = one("0|HATCH|10|0.0|20|0.0|30|2.5|2|ANSI31|70|0|71|1|91|1 + 92|7|72|1|73|1|93|3|10|0.0|20|0.0|42|0.5|10|4.0|20|0.0|42|0.0|10|4.0|20|3.0|42|0.0|97|2|330|A1|330|A2 + 75|1|76|1|52|45.0|41|2.0|77|0|78|2 + 53|45.0|43|0.0|44|0.0|45|-1.4|46|1.4|79|0 + 53|135.0|43|1.0|44|2.0|45|1.0|46|1.0|79|2|49|3.0|49|-1.5 + 98|2|10|1.0|20|1.0|10|2.0|20|2.0") + .kind + else { + panic!("a hatch") + }; + assert_eq!((h.pattern.as_str(), h.solid, h.associative, h.style, h.pattern_type), ("ANSI31", false, true, 1, 1)); + assert_eq!((h.angle, h.scale, h.double, h.elevation), (45.0, 2.0, false, 2.5)); + assert_eq!(h.loops.len(), 1); + let l = &h.loops[0]; + assert_eq!((l.flags, l.sources.as_slice()), (7, [0xA1, 0xA2].as_slice())); + assert_eq!(l.path, HatchPath::Polyline { closed: true, vertices: vec![[0.0, 0.0, 0.5], [4.0, 0.0, 0.0], [4.0, 3.0, 0.0]] }); + assert_eq!(h.pattern_lines.len(), 2); + assert_eq!((h.pattern_lines[0].angle, h.pattern_lines[0].offset, h.pattern_lines[0].dashes.len()), (45.0, [-1.4, 1.4], 0)); + assert_eq!((h.pattern_lines[1].base, h.pattern_lines[1].dashes.as_slice()), ([1.0, 2.0], [3.0, -1.5].as_slice())); + assert_eq!(h.seeds, [[1.0, 1.0], [2.0, 2.0]]); + assert!(h.gradient.is_none()); +} + +#[test] +fn hatch_with_edge_loops_and_a_gradient() { + let EntityKind::Hatch(h) = one( + "0|HATCH|2|SOLID|70|1|71|0|91|2 + 92|1|93|4 + 72|1|10|0.0|20|0.0|11|4.0|21|0.0 + 72|2|10|4.0|20|1.0|40|1.0|50|270.0|51|360.0|73|1 + 72|3|10|2.0|20|2.0|11|2.0|21|0.0|40|0.5|50|0.0|51|180.0|73|0 + 72|4|94|3|73|1|74|0|95|8|96|4|40|0.0|40|0.0|40|0.0|40|0.0|40|1.0|40|1.0|40|1.0|40|1.0|10|0.0|20|2.0|42|1.0|10|0.0|20|1.5|42|2.0|10|0.0|20|0.5|42|2.0|10|0.0|20|0.0|42|1.0|97|2|11|0.0|21|2.0|11|0.0|21|0.0|12|0.0|22|-1.0|13|0.0|23|-1.0 + 97|0 + 92|16|93|1 + 72|2|10|2.0|20|1.0|40|0.25|50|0.0|51|360.0|73|1 + 97|1|330|B1 + 75|0|76|1|98|0 + 450|1|451|0|460|0.785|461|0.0|452|0|462|0.0|453|2|463|0.0|63|5|421|255|463|1.0|63|2|470|LINEAR + 1001|ACAD|1010|0.0", + ) + .kind + else { + panic!("a hatch") + }; + assert!(h.solid && !h.associative); + assert_eq!(h.loops.len(), 2); + let HatchPath::Edges(e) = &h.loops[0].path else { panic!("edges") }; + assert_eq!(e.len(), 4); + assert_eq!(e[0], HatchEdge::Line { start: [0.0, 0.0], end: [4.0, 0.0] }); + assert_eq!(e[1], HatchEdge::Arc { center: [4.0, 1.0], radius: 1.0, start_angle: 270.0, end_angle: 360.0, ccw: true }); + assert_eq!(e[2], HatchEdge::Ellipse { center: [2.0, 2.0], major: [2.0, 0.0], ratio: 0.5, start_angle: 0.0, end_angle: 180.0, ccw: false }); + let HatchEdge::Spline { degree, rational, periodic, knots, control, weights, fit, start_tangent, end_tangent } = &e[3] else { + panic!("a spline edge") + }; + assert_eq!((*degree, *rational, *periodic, knots.len(), control.len()), (3, true, false, 8, 4)); + assert_eq!( + (weights.as_slice(), fit.len(), *start_tangent, *end_tangent), + ([1.0, 2.0, 2.0, 1.0].as_slice(), 2, Some([0.0, -1.0]), Some([0.0, -1.0])) + ); + // The spline's own 97 (fit points) is not the loop's source count. + assert!(h.loops[0].sources.is_empty()); + assert_eq!((h.loops[1].flags, h.loops[1].sources.as_slice()), (16, [0xB1].as_slice())); + let g = h.gradient.expect("a gradient"); + assert_eq!((g.name.as_str(), g.angle, g.shift, g.single), ("LINEAR", 0.785, 0.0, false)); + assert_eq!(g.colors, [Color::Rgb([0, 0, 255]), Color::Index(2)], "a true colour wins over its index"); +} + +#[test] +fn damaged_hatches() { + let k = |text: &str| one(text).kind; + assert_eq!(k("0|HATCH|2|SOLID"), EntityKind::Other, "no boundary data"); + assert_eq!(k("0|HATCH|91|2|92|2|93|1|10|0.0|20|0.0"), EntityKind::Other, "the second loop is missing"); + assert_eq!(k("0|HATCH|91|1|92|2|8|x|93|1"), EntityKind::Other, "a foreign group in the loop header"); + // Counts larger than the data: what is there is read, nothing is reserved for the rest. + let EntityKind::Hatch(h) = k("0|HATCH|91|1|92|2|93|2000000000|10|1.0|20|2.0|97|2000000000|330|A|98|2000000000|10|5.0|20|6.0") else { + panic!("a hatch") + }; + assert_eq!(h.loops[0].path, HatchPath::Polyline { closed: false, vertices: vec![[1.0, 2.0, 0.0]] }); + assert_eq!((h.loops[0].sources.as_slice(), h.seeds.as_slice()), ([0xA].as_slice(), [[5.0, 6.0]].as_slice())); + let EntityKind::Hatch(h) = k("0|HATCH|91|1|92|0|93|9|72|9|10|1.0|72|1|10|1.0|20|1.0|11|2.0|21|2.0|72|4|95|-5|96|2000000000|10|1.0|20|1.0") else { + panic!("a hatch") + }; + let HatchPath::Edges(e) = &h.loops[0].path else { panic!("edges") }; + assert_eq!(e.len(), 2, "an edge type the format does not have is passed over"); + assert!(matches!(&e[1], HatchEdge::Spline { knots, control, .. } if knots.is_empty() && control.len() == 1)); + assert_eq!(k("0|HATCH|91|-4").clone(), k("0|HATCH|91|0"), "a negative loop count is none"); + // A 450 that says no gradient. + assert!(matches!(k("0|HATCH|91|0|450|0|463|0.0|63|1"), EntityKind::Hatch(h) if h.gradient.is_none())); +} + +#[test] +fn entities_of_a_whole_drawing() { + let t = tags( + "0|SECTION|2|BLOCKS|0|BLOCK|2|B|0|CIRCLE|40|2.0|0|ENDBLK|0|ENDSEC|0|SECTION|2|ENTITIES|0|INSERT|2|B|0|POLYLINE|0|VERTEX|10|1.0|0|SEQEND|0|ENDSEC|0|EOF", + ); + let d = Document::read(&t); + let kinds: Vec = d.entities.iter().chain(&d.blocks[0].entities).map(|e| Entity::read(e).kind).collect(); + assert!(matches!(kinds.as_slice(), [EntityKind::Insert(_), EntityKind::Polyline(p), EntityKind::Circle { .. }] if p.vertices.len() == 1)); +} + +#[test] +fn a_spline_edge_without_fit_data_leaves_the_loops_source_count() { + // Before R2010 a spline edge has no 97 of its own: the one after it counts the loop's sources. + let EntityKind::Hatch(h) = one( + "0|HATCH|91|1|92|1|93|1|72|4|94|1|73|0|74|0|95|4|96|2|40|0.0|40|0.0|40|1.0|40|1.0|10|0.0|20|0.0|10|1.0|20|1.0|97|2|330|A1|330|A2|75|0|98|0", + ) + .kind + else { + panic!("a hatch") + }; + assert_eq!(h.loops[0].sources, [0xA1, 0xA2]); + assert!( + matches!(&h.loops[0].path, HatchPath::Edges(e) if matches!(&e[0], HatchEdge::Spline { fit, control, .. } if fit.is_empty() && control.len() == 2)) + ); +} From 8079878ae2b166c1ea8229e40eff7e08c916e532 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:22:03 -0500 Subject: [PATCH 046/120] craft-drawingml: the theme model (#940) * Add craft-drawingml: units, colours, preset shapes, custom geometry and text warps The parts of DrawingML that need no document model, moved from DeckCraft (60f91f78): crates/color, crates/geom (preset, warp, Xfrm and the EMU conversions) and crates/pptx/src/custgeom.rs with CustomPath from crates/model. The code is unchanged except that custgeom reads craft-xml trees and writes to a craft-xml Writer instead of DeckCraft's own XML helper, and matches elements by local name. The 59 tests of the moved code come with it; six more cover the custom geometry limits. * drawingml: evaluate a custom geometry's guide lists once per coordinate space read_cust_geom rebuilt Guides and evaluated avLst and gdLst again for every path: up to 1,000 paths times up to 10,000 guides, each name copied and hashed. Guides are now cached per path coordinate space (w, h). At most 16 spaces and 4 MiB of guide names are kept per geometry; paths in further spaces are left out. The first space is always kept, and a geometry without guides is not capped. Ported from storytold/deckcraft#1789. A list counts as having guides only when it has gd children, as that PR's review asked. * drawingml: the model types for fills, lines, text, tables, charts and diagrams Moved from DeckCraft (60f91f78) crates/model/src: style.rs, text.rs, table.rs, chart.rs, numfmt.rs and trendline.rs, plus Diagram, DiagramNode and the MediaId and SlideId newtypes from lib.rs. The files are unchanged except that deckcraft_color:: is crate::color:: and Fill and TableBorder allow clippy::large_enum_variant. Their 60 tests come with them. Readers and writers follow. * drawingml: the theme model (colour, font and format schemes) Moved from DeckCraft crates/model/src/theme.rs at 60f91f78: FontSet, ScriptFont, FontScheme, FormatScheme, Theme and default_format. The app's theme gallery, its variants and its own default theme stay in the app. Theme::default() is a plain theme built in place, so nothing here depends on a gallery. --- crates/drawingml/src/lib.rs | 3 +- crates/drawingml/src/theme.rs | 183 ++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 crates/drawingml/src/theme.rs diff --git a/crates/drawingml/src/lib.rs b/crates/drawingml/src/lib.rs index 47d6ee107..1e47301dc 100644 --- a/crates/drawingml/src/lib.rs +++ b/crates/drawingml/src/lib.rs @@ -11,7 +11,7 @@ //! - [`warp`]: preset text warps (`a:prstTxWarp`). //! - The model, without readers and writers yet: [`style`] (colour references, fills, lines, //! effects), [`text`] (text bodies, paragraphs, runs), [`table`], [`chart`] with [`numfmt`] -//! (number format codes) and [`diagram`] (SmartArt outlines). Media are named by [`MediaId`]; +//! (number format codes), [`theme`] (colour, font and format schemes) and [`diagram`] (SmartArt outlines). Media are named by [`MediaId`]; //! the bytes stay with the caller. //! //! Nothing here opens a package: readers take a [`craft_xml::El`] and writers a @@ -27,6 +27,7 @@ pub mod preset; pub mod style; pub mod table; pub mod text; +pub mod theme; mod trendline; pub mod warp; diff --git a/crates/drawingml/src/theme.rs b/crates/drawingml/src/theme.rs new file mode 100644 index 000000000..d4416e2f1 --- /dev/null +++ b/crates/drawingml/src/theme.rs @@ -0,0 +1,183 @@ +//! Themes: colour scheme, font scheme and format scheme (the fill/line/effect "style matrix"). + +use crate::color::{ColorScheme, ColorTransform, Rgba, SchemeSlot}; +use serde::{Deserialize, Serialize}; + +use crate::style::{ColorRef, Effects, Fill, Gradient, GradientShape, GradientStop, Line, Shadow}; + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct FontSet { + pub latin: String, + pub ea: String, + pub cs: String, + /// Per-script typefaces (`a:font script="Arab" typeface="…"`), in document order. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub scripts: Vec, +} + +/// A theme font for one script (ISO 15924 tag, e.g. `Arab`). +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct ScriptFont { + pub script: String, + pub typeface: String, +} + +impl FontSet { + /// The typeface the theme gives `script` (`"Arab"`), if any. + pub fn script(&self, script: &str) -> Option<&str> { + self.scripts.iter().find(|f| f.script.eq_ignore_ascii_case(script) && !f.typeface.is_empty()).map(|f| f.typeface.as_str()) + } +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct FontScheme { + pub name: String, + /// Headings. + pub major: FontSet, + /// Body. + pub minor: FontSet, +} + +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct FormatScheme { + pub name: String, + /// Subtle, moderate, intense (style index 1–3). + pub fills: Vec, + pub lines: Vec, + pub effects: Vec, + /// Background fills (index 1001–1003). + pub bg_fills: Vec, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Theme { + pub name: String, + pub colors: ColorScheme, + pub fonts: FontScheme, + pub format: FormatScheme, + /// Raw theme XML extras kept for round-trip (object defaults, extra colour lists). + #[serde(skip_serializing_if = "Option::is_none")] + pub raw_extra: Option, +} + +impl Default for Theme { + /// A plain theme: black and white, a slate and a warm grey, six accents, no font names (the + /// writer puts `Arial` where a Latin face is required) and [`default_format`]. + fn default() -> Self { + let hex = |s: &str| Rgba::from_hex(s).unwrap_or(Rgba::BLACK); + let colors = ["000000", "FFFFFF", "1E2B3C", "EDEBE7", "2E6FD8", "F26B1D", "2EA36F", "8A5CD8", "D83F6B", "E3B21C", "2E6FD8", "7A5BA6"]; + Theme { + name: "Theme".into(), + colors: ColorScheme { name: "Default".into(), colors: colors.map(hex) }, + fonts: FontScheme::default(), + format: default_format(), + raw_extra: None, + } + } +} + +impl Theme { + /// Resolve a theme font reference (`+mj-lt`, `+mn-ea`…) to a family name. + pub fn font(&self, name: &str) -> String { + match name { + "+mj-lt" => self.fonts.major.latin.clone(), + "+mn-lt" => self.fonts.minor.latin.clone(), + "+mj-ea" => self.fonts.major.ea.clone(), + "+mn-ea" => self.fonts.minor.ea.clone(), + "+mj-cs" => self.fonts.major.cs.clone(), + "+mn-cs" => self.fonts.minor.cs.clone(), + other => other.to_string(), + } + } +} + +fn ph() -> ColorRef { + ColorRef::scheme(SchemeSlot::PhClr) +} + +/// The format scheme used where a theme has none, and to fill a short style list: flat solid fills, a soft gradient, and a gentle shadow for "intense". +pub fn default_format() -> FormatScheme { + let grad = |a: i32, b: i32| { + Fill::Gradient(Gradient { + tile: [0.0; 4], + stops: vec![ + GradientStop { pos: 0.0, color: ph().with(ColorTransform::LumMod(a)).with(ColorTransform::SatMod(103000)) }, + GradientStop { pos: 1.0, color: ph().with(ColorTransform::LumMod(b)) }, + ], + shape: GradientShape::Linear { angle: 90.0, scaled: false }, + rotate_with_shape: true, + flip: None, + }) + }; + FormatScheme { + name: "Default".into(), + fills: vec![Fill::solid(ph()), grad(110000, 92000), grad(105000, 80000)], + lines: vec![Line::solid(ph(), 0.75), Line::solid(ph(), 1.0), Line::solid(ph(), 1.5)], + effects: vec![ + Effects::default(), + Effects::default(), + Effects { + outer_shadow: Some(Shadow { + color: ColorRef::rgb(Rgba::BLACK).with(ColorTransform::Alpha(40000)), + blur: 6.0, + dist: 3.0, + dir: 90.0, + inner: false, + sx: 1.0, + sy: 1.0, + kx: 0.0, + ky: 0.0, + align: "t".into(), + rotate_with_shape: false, + }), + ..Default::default() + }, + ], + bg_fills: vec![ + Fill::solid(ph()), + Fill::solid(ph().with(ColorTransform::Tint(95000)).with(ColorTransform::SatMod(170000))), + grad(102000, 85000), + ], + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_default_theme_has_a_full_style_matrix_and_no_font_names() { + let t = Theme::default(); + assert_eq!((t.format.fills.len(), t.format.lines.len(), t.format.effects.len(), t.format.bg_fills.len()), (3, 3, 3, 3)); + assert_eq!(t.colors.get(SchemeSlot::Lt1), Rgba::WHITE); + assert_eq!(t.fonts, FontScheme::default()); + } + + #[test] + fn theme_font_references_resolve_and_other_names_pass_through() { + let mut t = Theme::default(); + t.fonts.major.latin = "Head".into(); + t.fonts.minor.cs = "Body CS".into(); + t.fonts.minor.scripts.push(ScriptFont { script: "Arab".into(), typeface: "Naskh".into() }); + assert_eq!(t.font("+mj-lt"), "Head"); + assert_eq!(t.font("+mn-cs"), "Body CS"); + assert_eq!(t.font("Georgia"), "Georgia"); + assert_eq!(t.fonts.minor.script("arab"), Some("Naskh")); + assert_eq!(t.fonts.minor.script("Hebr"), None); + } + + #[test] + fn a_theme_survives_json_and_missing_fields_take_the_default() { + let t = Theme::default(); + let json = serde_json::to_string(&t).unwrap(); + assert_eq!(serde_json::from_str::(&json).unwrap(), t); + let partial: Theme = serde_json::from_str(r#"{"name":"X"}"#).unwrap(); + assert_eq!(partial.colors, t.colors); + assert_eq!(partial.name, "X"); + } +} From dca2a2cd8e214b0c24d7fe0f222a803f63155d16 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:22:42 -0500 Subject: [PATCH 047/120] craft-metadata: EXIF, IPTC-IIM, XMP and GPX from LightCraft (#949) lightcraft-meta at 5b61ce1e, as one crate with a module per format: exif (read, write, maker-note lens, focus point, tag listing), iptc, xmp (parse, write, merge), gpx, and the JPEG, PNG, WebP, TIFF, RAF and CR3 containers. They share Metadata, DateTime, Gps and Orientation. Changed in the move: - TIFF comes from craft-tiff; Point, Rect and Orientation are the crate's own. - The app namespace is a parameter: parse_xmp_with, write_xmp_with and MergeRules::namespaces take it, and XmpData no longer has lc_settings. - The HEIF, PSD and MRW readers stay out, since craft-heif, craft-psd and craft-raw read those containers; from_embedded takes the blocks they find. --- Cargo.lock | 12 + Cargo.toml | 1 + README.md | 4 + crates/metadata/Cargo.toml | 21 + crates/metadata/README.md | 61 +++ crates/metadata/src/container.rs | 450 ++++++++++++++++ crates/metadata/src/cr3.rs | 742 ++++++++++++++++++++++++++ crates/metadata/src/datetime.rs | 320 ++++++++++++ crates/metadata/src/exif.rs | 759 +++++++++++++++++++++++++++ crates/metadata/src/focus.rs | 284 ++++++++++ crates/metadata/src/focus/tests.rs | 449 ++++++++++++++++ crates/metadata/src/focus_read.rs | 472 +++++++++++++++++ crates/metadata/src/geom.rs | 180 +++++++ crates/metadata/src/gpx.rs | 491 +++++++++++++++++ crates/metadata/src/iptc.rs | 151 ++++++ crates/metadata/src/lib.rs | 606 +++++++++++++++++++++ crates/metadata/src/tags.rs | 529 +++++++++++++++++++ crates/metadata/src/xmp.rs | 813 +++++++++++++++++++++++++++++ crates/metadata/src/xmp/tests.rs | 631 ++++++++++++++++++++++ crates/metadata/src/xmp_merge.rs | 505 ++++++++++++++++++ crates/metadata/tests/it/main.rs | 3 + crates/metadata/tests/it/robust.rs | 70 +++ 22 files changed, 7554 insertions(+) create mode 100644 crates/metadata/Cargo.toml create mode 100644 crates/metadata/README.md create mode 100644 crates/metadata/src/container.rs create mode 100644 crates/metadata/src/cr3.rs create mode 100644 crates/metadata/src/datetime.rs create mode 100644 crates/metadata/src/exif.rs create mode 100644 crates/metadata/src/focus.rs create mode 100644 crates/metadata/src/focus/tests.rs create mode 100644 crates/metadata/src/focus_read.rs create mode 100644 crates/metadata/src/geom.rs create mode 100644 crates/metadata/src/gpx.rs create mode 100644 crates/metadata/src/iptc.rs create mode 100644 crates/metadata/src/lib.rs create mode 100644 crates/metadata/src/tags.rs create mode 100644 crates/metadata/src/xmp.rs create mode 100644 crates/metadata/src/xmp/tests.rs create mode 100644 crates/metadata/src/xmp_merge.rs create mode 100644 crates/metadata/tests/it/main.rs create mode 100644 crates/metadata/tests/it/robust.rs diff --git a/Cargo.lock b/Cargo.lock index 1e9d85eff..b531fce60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1275,6 +1275,18 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-metadata" +version = "0.1.0" +dependencies = [ + "chrono", + "craft-tiff", + "proptest", + "quick-xml 0.41.0", + "serde", + "serde_json", +] + [[package]] name = "craft-metafile" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 507ab1410..7059505ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,6 +95,7 @@ craft-text = { path = "crates/text" } craft-tga = { path = "crates/tga" } craft-timeline = { path = "crates/timeline" } craft-tiff = { path = "crates/tiff" } +craft-metadata = { path = "crates/metadata" } craft-ui = { path = "crates/ui" } craft-update = { path = "crates/update" } craft-vcard = { path = "crates/vcard" } diff --git a/README.md b/README.md index 3830d0560..3776d0b56 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,10 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = "` form on craft-xml elements, value codecs and the package writer; and InDesign Tagged Text, read and written. - [`craft-timeline`](crates/timeline/README.md): editorial timeline interchange: exact media time and SMPTE timecode, import/export reports, media path and `file://` URL handling, Avid Log Exchange (ALE) and CMX 3600 EDL documents, format detection. From FilmCraft. +- [`craft-metadata`](crates/metadata/README.md): photo metadata, from LightCraft: EXIF (read and write, + maker-note lens and focus point, every tag by name), IPTC-IIM, XMP packets (read, write, and a merge + that keeps what another program wrote), GPX track logs, and the blocks that carry them in JPEG, PNG, + WebP, TIFF-based, RAF and CR3 files. One `Metadata` model for all of them. Built on `craft-tiff`. - [`craft-tiff`](crates/tiff/README.md): TIFF and BigTIFF IFD reader and writer, from LightCraft: every field type, the IFD chain, SubIFDs and Exif/GPS/Interop pointer IFDs, maker-note IFDs (Canon, Nikon, Sony, Fujifilm, Olympus, Panasonic, Pentax), strip and tile layout. Bounded and diff --git a/crates/metadata/Cargo.toml b/crates/metadata/Cargo.toml new file mode 100644 index 000000000..207492619 --- /dev/null +++ b/crates/metadata/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "craft-metadata" +description = "Photo metadata: EXIF, IPTC-IIM, XMP packets (read, write, merge) and GPX track logs, the blocks that carry them in JPEG, PNG, WebP, TIFF and CR3 files, and one model they all read into." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-tiff = { workspace = true } +serde = { workspace = true } +quick-xml = "0.41" +chrono = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } +serde_json = { workspace = true } diff --git a/crates/metadata/README.md b/crates/metadata/README.md new file mode 100644 index 000000000..1345ead5f --- /dev/null +++ b/crates/metadata/README.md @@ -0,0 +1,61 @@ +# craft-metadata + +Photo metadata: EXIF, IPTC-IIM, XMP packets and GPX track logs, the blocks that carry them in image +files, and one `Metadata` model they all read into. From LightCraft (`lightcraft-meta`). TIFF +structure comes from `craft-tiff`. + +One module per format; they share `Metadata`, `DateTime`, `Gps` and `Orientation`, which is why they +are one crate. + +| Function | Purpose | +|---|---| +| `read_exif(&[u8]) -> Metadata` | A TIFF-structured Exif block: a whole TIFF/DNG/raw file, or a JPEG APP1 payload with or without `Exif\0\0`. `try_read_exif` reports header errors, `try_read_exif_with` takes `craft_tiff::ParseOptions`. | +| `from_tiff(&Tiff) -> Metadata` | The same from an already parsed stream. | +| `write_exif(&Metadata) -> Vec` | An Exif block for an exported file. | +| `tags::tag_rows`, `tags::file_tag_rows` | Every tag with its name and value as text, maker notes included. | +| `focus::read`, `focus::read_header` | Where the camera focused, from the maker note (Sony), on the upright photo. `read_header` reads only the first kilobytes of a file through a callback. | +| `parse_iptc(&[u8]) -> Metadata` | IIM record 2: title (2:5), keywords (2:25), by-line (2:80), copyright (2:116), caption (2:120), date and time created (2:55, 2:60). | +| `parse_xmp(&str) -> Result` | `Metadata`, every property (`prefix:name`, structs flattened as `a:b/c:d`) and the structured values. `parse_xmp_with` also canonicalises the caller's own namespaces. | +| `write_xmp(&Metadata) -> String` | A complete `` packet. `write_xmp_with` sets the toolkit name and adds simple properties in the caller's namespace. | +| `merge_xmp(existing, fresh, MergeRules)` | Updates a packet another program wrote: owned properties are replaced, everything else stays byte for byte. | +| `parse_gpx(&str) -> Result` | GPX track points; `Tracklog::locate` gives the position at a capture time. | +| `embedded(&[u8]) -> Embedded` | Sniffs JPEG, PNG, WebP, TIFF-based files, Fujifilm RAF and Canon CR3 and returns the Exif, XMP (and extended XMP), ICC and IPTC blocks. `jpeg_segments`, `png_chunks`, `webp_chunks` are the walkers. | +| `cr3::parse_cr3` | The Canon CR3 container: `CMT1`…`CMT4`, the XMP `uuid` box, the thumbnail, the tracks. | +| `extract(&[u8]) -> Metadata` | All of it for a whole file. Precedence: EXIF, then IPTC fills gaps, then XMP overrides the user fields (rating, label, title, caption, artist, copyright, keywords, GPS) and fills the rest. `from_embedded` does the same for blocks already located. | + +HEIF, PSD and MRW files are read by `craft-heif`, `craft-psd` and `craft-raw`. Put the blocks they +return into an `Embedded` and call `from_embedded`. + +## Capture time + +EXIF candidates are tried in order: DateTimeOriginal, DateTimeDigitized, then IFD0 DateTime. A clock +with more than three colon-separated components is rejected as a whole, so the next candidate or the +XMP supplies the time. ISO 8601 times in XMP need a complete clock. Calendar checks go through +`chrono`. Invalid GPX point times count as untimed. + +## XMP namespaces + +Written: `dc`, `xmp`, `xmpRights`, `photoshop`, `exif`, `exifEX`, `tiff`, `aux`, `lr`, +`Iptc4xmpCore`, and the one namespace the caller passes. Read as well: `crs`, `mwg-rs` regions +(`stArea`, `stDim`). Reading canonicalises prefixes by namespace URI and handles attribute and element +forms, `rdf:Seq`/`Bag`/`Alt` (x-default first), `rdf:parseType="Resource"` and nested-description +structs, predefined and numeric entities. + +A foreign namespace that uses a recognised prefix cannot supply recognised properties. Its keys are +`Q{hex-encoded namespace URI}local`. Merging keeps the original foreign XML. + +## Limits + +XMP: 64 levels, 200 000 nodes, 1 MiB of flattened keys and 8 MiB of opaque names per packet +(`XmpError::TooComplex`). GPX: 64 levels, 5 000 000 points. CR3: 4096 boxes. JPEG ICC is returned only when its APP2 chunks form a +complete, consistent, uniquely numbered sequence. TIFF limits are `craft-tiff`'s. No input panics: +proptests feed random bytes to every container walker and random text to the XMP parser, and a sample +JPEG is cut at every byte. + +## Sources + +CIPA DC-008 (Exif 2.32), TIFF 6.0, Adobe DNG 1.7 (tag numbers), ISO 16684-1 and the Adobe XMP +Specification Parts 1 to 3, IPTC-IIM 4.2 and the IPTC Photo Metadata Standard, the MWG Region +Guidelines, ITU-T T.81 (JPEG markers), ICC.1 (APP2 embedding), the PNG Specification 3rd ed. (`eXIf`, +`iTXt`), the WebP RIFF container specification, ISO/IEC 14496-12 (box structure), GPX 1.1. XML parsing: +`quick-xml` (MIT). No third-party metadata code was consulted. diff --git a/crates/metadata/src/container.rs b/crates/metadata/src/container.rs new file mode 100644 index 000000000..660677581 --- /dev/null +++ b/crates/metadata/src/container.rs @@ -0,0 +1,450 @@ +//! Locating metadata blocks inside image containers. +//! +//! - JPEG (ITU-T T.81 marker structure): APP1 `Exif\0\0`, APP1 `http://ns.adobe.com/xap/1.0/\0` (XMP), +//! APP1 `http://ns.adobe.com/xmp/extension/\0` (extended XMP, reassembled), APP2 `ICC_PROFILE\0` +//! (multi-chunk, reassembled in sequence order), APP13 `Photoshop 3.0\0` 8BIM resource 0x0404 (IPTC-IIM). +//! - PNG (PNG spec 3rd ed.): `eXIf`, `iTXt` with keyword `XML:com.adobe.xmp` (uncompressed). +//! - WebP (RIFF container spec): `EXIF`, `XMP `, `ICCP` chunks. +//! - TIFF-based files (TIFF, DNG and most raws): the whole file is the Exif block; XMP (700), ICC (34675) and +//! IPTC (33723) from IFD0. +//! - Canon CR3 (ISO base media file, see [`crate::cr3`]): `CMT1`/`CMT2`/`CMT4` merged into one Exif block; the +//! XMP `uuid` box. +//! +//! PSD, MRW and HEIF files are read by `craft-psd`, `craft-raw` and `craft-heif`. + +use craft_tiff::{Tiff, tags}; + +/// Metadata blocks found in a file. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Embedded { + /// TIFF-structured Exif block (without the `Exif\0\0` prefix). + pub exif: Option>, + /// XMP packet (the main one; extended XMP is appended by [`Embedded::xmp_extended`]). + pub xmp: Option, + /// Extended XMP (JPEG only), reassembled. + pub xmp_extended: Option, + pub icc: Option>, + /// IPTC-IIM stream. + pub iptc: Option>, +} + +/// Sniff the container and collect its metadata blocks. Unknown containers yield an empty result. +pub fn embedded(bytes: &[u8]) -> Embedded { + if bytes.starts_with(b"FUJIFILMCCD-RAW") { + // Fujifilm RAF: metadata lives in the embedded JPEG whose (offset, length) are big-endian u32s at byte 84 + let be = |i: usize| bytes.get(i..i + 4).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]]) as usize); + return match (be(84), be(88)) { + (Some(o), Some(l)) if o > 0 => bytes.get(o..o.saturating_add(l).min(bytes.len())).map(jpeg_segments).unwrap_or_default(), + _ => Embedded::default(), + }; + } + if let Some(c) = crate::cr3::parse_cr3(bytes) { + return Embedded { exif: crate::cr3::merged_exif(&c), xmp: c.xmp.map(|b| String::from_utf8_lossy(b).into_owned()), ..Default::default() }; + } + if bytes.starts_with(&[0xff, 0xd8]) { + jpeg_segments(bytes) + } else if bytes.starts_with(b"\x89PNG\r\n\x1a\n") { + png_chunks(bytes) + } else if bytes.len() >= 12 && &bytes[..4] == b"RIFF" && &bytes[8..12] == b"WEBP" { + webp_chunks(bytes) + } else if let Ok(t) = Tiff::parse(bytes) { + let ifd0 = &t.ifds[0]; + Embedded { + exif: Some(bytes.to_vec()), + xmp: ifd0.bytes(tags::XMP).map(|b| String::from_utf8_lossy(b).into_owned()), + xmp_extended: None, + icc: ifd0.bytes(tags::ICC_PROFILE).map(|b| b.to_vec()), + iptc: ifd0.value(tags::IPTC_NAA).map(|v| match v { + craft_tiff::Value::Byte(b) | craft_tiff::Value::Undefined(b) => b.clone(), + other => craft_tiff::writer::encode(t.order, other), + }), + } + } else { + Embedded::default() + } +} + +const XMP_SIG: &[u8] = b"http://ns.adobe.com/xap/1.0/\0"; +const XMP_EXT_SIG: &[u8] = b"http://ns.adobe.com/xmp/extension/\0"; + +/// Walk JPEG markers up to SOS and collect APP1 Exif / XMP, APP2 ICC and APP13 IPTC. +pub fn jpeg_segments(b: &[u8]) -> Embedded { + let mut out = Embedded::default(); + if !b.starts_with(&[0xff, 0xd8]) { + return out; + } + let mut icc_parts: [Option<&[u8]>; 255] = [None; 255]; + let mut icc_total = None; + let mut icc_invalid = false; + let mut ext: Vec<(u32, Vec)> = Vec::new(); + let mut i = 2usize; + while i + 4 <= b.len() { + if b[i] != 0xff { + break; + } + let marker = b[i + 1]; + if marker == 0xff { + i += 1; // fill byte + continue; + } + if marker == 0xd8 || (0xd0..=0xd7).contains(&marker) || marker == 0x01 { + i += 2; + continue; + } + if marker == 0xd9 || marker == 0xda { + break; + } + let len = u16::from_be_bytes([b[i + 2], b[i + 3]]) as usize; + if len < 2 { + break; + } + let Some(start) = i.checked_add(4) else { break }; + let Some(declared_end) = i.checked_add(2).and_then(|at| at.checked_add(len)) else { break }; + let complete = declared_end <= b.len(); + let end = declared_end.min(b.len()); + let Some(payload) = b.get(start..end) else { break }; + match marker { + 0xe1 if payload.starts_with(b"Exif\0") && out.exif.is_none() => out.exif = Some(payload.get(6..).unwrap_or(&[]).to_vec()), + 0xe1 if payload.starts_with(XMP_SIG) && out.xmp.is_none() => { + out.xmp = Some(String::from_utf8_lossy(&payload[XMP_SIG.len()..]).into_owned()); + } + 0xe1 if payload.starts_with(XMP_EXT_SIG) => { + // GUID (32) + full length (4) + offset (4) + data + let p = &payload[XMP_EXT_SIG.len()..]; + if p.len() >= 40 { + let off = u32::from_be_bytes([p[36], p[37], p[38], p[39]]); + ext.push((off, p[40..].to_vec())); + } + } + 0xe2 if payload.starts_with(b"ICC_PROFILE\0") => { + if !complete || payload.len() < 14 { + icc_invalid = true; + } else if let Some(header) = payload.get(12..14) { + let (Some(&sequence), Some(&total)) = (header.first(), header.get(1)) else { + icc_invalid = true; + i = end; + continue; + }; + if sequence == 0 || total == 0 || sequence > total || icc_total.is_some_and(|expected| expected != total) { + icc_invalid = true; + } else { + icc_total = Some(total); + if let Some(slot) = icc_parts.get_mut(usize::from(sequence - 1)) { + if slot.is_some() { + icc_invalid = true; + } else if let Some(data) = payload.get(14..) { + *slot = Some(data); + } else { + icc_invalid = true; + } + } else { + icc_invalid = true; + } + } + } else { + icc_invalid = true; + } + } + 0xed if payload.starts_with(b"Photoshop 3.0\0") && out.iptc.is_none() => out.iptc = photoshop_iptc(&payload[14..]), + _ => {} + } + i = end; + } + if let Some(total) = icc_total { + let mut profile_len = 0usize; + for chunk in icc_parts.iter().take(usize::from(total)) { + let Some(bytes) = chunk else { + icc_invalid = true; + break; + }; + let Some(next_len) = profile_len.checked_add(bytes.len()) else { + icc_invalid = true; + break; + }; + profile_len = next_len; + } + let maximum_len = usize::from(total).checked_mul(65_519); + if maximum_len.is_none_or(|maximum| profile_len > maximum) { + icc_invalid = true; + } + if !icc_invalid { + let mut profile = Vec::with_capacity(profile_len); + for bytes in icc_parts.iter().take(usize::from(total)).flatten() { + profile.extend_from_slice(bytes); + } + out.icc = Some(profile); + } + } + if !ext.is_empty() { + ext.sort_by_key(|p| p.0); + out.xmp_extended = Some(String::from_utf8_lossy(&ext.into_iter().flat_map(|p| p.1).collect::>()).into_owned()); + } + out +} + +/// IPTC-IIM block (resource 0x0404) from a Photoshop image-resource stream (`8BIM` records). +fn photoshop_iptc(mut b: &[u8]) -> Option> { + while b.len() >= 12 && &b[..4] == b"8BIM" { + let id = u16::from_be_bytes([b[4], b[5]]); + let name_len = b[6] as usize; + let name_total = (1 + name_len).next_multiple_of(2); + let size_at = 6 + name_total; + let size = u32::from_be_bytes(b.get(size_at..size_at + 4)?.try_into().ok()?) as usize; + let data_at = size_at + 4; + let data = b.get(data_at..data_at.checked_add(size)?)?; + if id == 0x0404 { + return Some(data.to_vec()); + } + b = b.get(data_at + size.next_multiple_of(2)..)?; + } + None +} + +/// PNG chunks: `eXIf`, uncompressed `iTXt` XMP. +pub fn png_chunks(b: &[u8]) -> Embedded { + let mut out = Embedded::default(); + let mut i = 8usize; + while i + 12 <= b.len() { + let len = u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) as usize; + let typ = &b[i + 4..i + 8]; + let Some(data) = b.get(i + 8..(i + 8).saturating_add(len)) else { break }; + match typ { + b"eXIf" => out.exif = Some(crate::strip_exif_header(data).to_vec()), + b"iTXt" if data.starts_with(b"XML:com.adobe.xmp\0") => { + // keyword\0 compression-flag compression-method language\0 translated\0 text + let rest = &data[18..]; + if rest.len() >= 2 && rest[0] == 0 { + let rest = &rest[2..]; + let mut parts = rest.splitn(3, |&c| c == 0); + let (_, _, text) = (parts.next(), parts.next(), parts.next()); + if let Some(t) = text { + out.xmp = Some(String::from_utf8_lossy(t).into_owned()); + } + } + } + b"IEND" => break, + _ => {} + } + i += 12 + len; + } + out +} + +/// WebP RIFF chunks `EXIF`, `XMP `, `ICCP`. +pub fn webp_chunks(b: &[u8]) -> Embedded { + let mut out = Embedded::default(); + let mut i = 12usize; + while i + 8 <= b.len() { + let id = &b[i..i + 4]; + let len = u32::from_le_bytes([b[i + 4], b[i + 5], b[i + 6], b[i + 7]]) as usize; + let Some(data) = b.get(i + 8..(i + 8).saturating_add(len)) else { break }; + match id { + b"EXIF" => out.exif = Some(crate::strip_exif_header(data).to_vec()), + b"XMP " => out.xmp = Some(String::from_utf8_lossy(data).into_owned()), + b"ICCP" => out.icc = Some(data.to_vec()), + _ => {} + } + i += 8 + len + (len & 1); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::exif::tests::sample_exif; + use craft_tiff::ByteOrder; + + fn seg(marker: u8, payload: &[u8]) -> Vec { + let mut v = vec![0xff, marker]; + v.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes()); + v.extend_from_slice(payload); + v + } + + fn icc_seg(sequence: u8, total: u8, data: &[u8]) -> Vec { + let mut payload = b"ICC_PROFILE\0".to_vec(); + payload.extend_from_slice(&[sequence, total]); + payload.extend_from_slice(data); + seg(0xe2, &payload) + } + + fn jpeg_with_icc(parts: &[Vec]) -> Vec { + let mut jpeg = vec![0xff, 0xd8]; + let packet = crate::write_xmp(&crate::Metadata { title: Some("retained".into()), ..Default::default() }); + let mut xmp = XMP_SIG.to_vec(); + xmp.extend_from_slice(packet.as_bytes()); + jpeg.extend_from_slice(&seg(0xe1, &xmp)); + for part in parts { + jpeg.extend_from_slice(part); + } + jpeg.extend_from_slice(&[0xff, 0xda, 0, 2]); + jpeg + } + + pub(crate) fn sample_jpeg() -> Vec { + let mut j = vec![0xff, 0xd8]; + j.extend(seg(0xe0, b"JFIF\0\x01\x02\0\0\x01\0\x01\0\0")); + let mut exif = b"Exif\0\0".to_vec(); + exif.extend(sample_exif(ByteOrder::Big)); + j.extend(seg(0xe1, &exif)); + let meta = crate::Metadata { title: Some("Sunset".into()), rating: Some(4), ..Default::default() }; + let mut xmp = XMP_SIG.to_vec(); + xmp.extend(crate::write_xmp(&meta).into_bytes()); + j.extend(seg(0xe1, &xmp)); + // ICC in two chunks, out of order + j.extend(seg(0xe2, b"ICC_PROFILE\0\x02\x02WORLD")); + j.extend(seg(0xe2, b"ICC_PROFILE\0\x01\x02HELLO")); + // IPTC + let iim = crate::iptc::tests::sample_iim(); + let mut ps = b"Photoshop 3.0\0".to_vec(); + ps.extend_from_slice(b"8BIM\x04\x0c\0\0"); + ps.extend_from_slice(&2u32.to_be_bytes()); + ps.extend_from_slice(&[1, 2]); + ps.extend_from_slice(b"8BIM\x04\x04\0\0"); + ps.extend_from_slice(&(iim.len() as u32).to_be_bytes()); + ps.extend_from_slice(&iim); + j.extend(seg(0xed, &ps)); + j.extend_from_slice(&[0xff, 0xda, 0, 2, 1, 2, 3, 0xff, 0xd9]); + j + } + + #[test] + fn jpeg_blocks() { + let j = sample_jpeg(); + let e = jpeg_segments(&j); + assert_eq!(e.icc.as_deref(), Some(&b"HELLOWORLD"[..])); + assert!(e.xmp.as_deref().unwrap().contains("Sunset")); + assert!(e.iptc.is_some()); + let m = crate::extract(&j); + assert_eq!(m.iso, Some(400)); + assert_eq!(m.title.as_deref(), Some("Sunset")); + assert_eq!(m.rating, Some(4)); // XMP overrides EXIF rating 3 + assert_eq!(m.keywords, vec!["alpha".to_string(), "beta".to_string()]); // from IPTC + assert_eq!(m.caption.as_deref(), Some("A caption")); + } + + #[test] + fn jpeg_extended_xmp() { + let mut j = vec![0xff, 0xd8]; + for (off, part) in [(5u32, &b"world"[..]), (0, b"hello")] { + let mut p = XMP_EXT_SIG.to_vec(); + p.extend_from_slice(&[b'A'; 32]); + p.extend_from_slice(&10u32.to_be_bytes()); + p.extend_from_slice(&off.to_be_bytes()); + p.extend_from_slice(part); + j.extend(seg(0xe1, &p)); + } + assert_eq!(jpeg_segments(&j).xmp_extended.as_deref(), Some("helloworld")); + } + + #[test] + fn jpeg_icc_accepts_reordered_chunks() { + let parts = [icc_seg(2, 2, b"world"), icc_seg(1, 2, b"hello")]; + let embedded = jpeg_segments(&jpeg_with_icc(&parts)); + assert_eq!(embedded.icc.as_deref(), Some(&b"helloworld"[..])); + } + + #[test] + fn jpeg_icc_rejects_incomplete_or_incoherent_chunks() { + let cases = [ + ("missing", vec![icc_seg(1, 2, b"helloworld")]), + ("duplicate", vec![icc_seg(1, 2, b"hello"), icc_seg(1, 2, b"world")]), + ("zero sequence", vec![icc_seg(0, 1, b"helloworld")]), + ("zero total", vec![icc_seg(1, 0, b"helloworld")]), + ("out of range", vec![icc_seg(2, 1, b"helloworld")]), + ("gap", vec![icc_seg(1, 3, b"hello"), icc_seg(3, 3, b"world")]), + ("inconsistent totals", vec![icc_seg(1, 2, b"hello"), icc_seg(2, 3, b"world")]), + ("short header", vec![seg(0xe2, b"ICC_PROFILE\0\x01")]), + ]; + for (name, parts) in cases { + let embedded = jpeg_segments(&jpeg_with_icc(&parts)); + assert!(embedded.icc.is_none(), "{name} ICC chunks must be rejected"); + assert!(embedded.xmp.as_deref().is_some_and(|x| x.contains("retained")), "{name}: unrelated XMP must remain readable"); + } + } + + #[test] + fn jpeg_icc_rejects_truncated_segment_and_keeps_other_metadata() { + let mut truncated = icc_seg(1, 1, b"profile"); + let declared = u16::from_be_bytes([truncated[2], truncated[3]]).saturating_add(1); + truncated[2..4].copy_from_slice(&declared.to_be_bytes()); + let mut jpeg = vec![0xff, 0xd8]; + let xmp = crate::write_xmp(&crate::Metadata { title: Some("kept".into()), ..Default::default() }); + let mut packet = XMP_SIG.to_vec(); + packet.extend_from_slice(xmp.as_bytes()); + jpeg.extend_from_slice(&seg(0xe1, &packet)); + jpeg.extend_from_slice(&truncated); + + let embedded = jpeg_segments(&jpeg); + assert!(embedded.icc.is_none()); + assert!(embedded.xmp.as_deref().is_some_and(|x| x.contains("kept"))); + } + + #[test] + fn png_and_webp() { + let exif = sample_exif(ByteOrder::Little); + let mut p = b"\x89PNG\r\n\x1a\n".to_vec(); + let mut chunk = |typ: &[u8], data: &[u8]| { + p.extend_from_slice(&(data.len() as u32).to_be_bytes()); + p.extend_from_slice(typ); + p.extend_from_slice(data); + p.extend_from_slice(&[0; 4]); + }; + chunk(b"eXIf", &exif); + let mut itxt = b"XML:com.adobe.xmp\0\0\0\0\0".to_vec(); + itxt.extend(crate::write_xmp(&crate::Metadata { label: Some("Red".into()), ..Default::default() }).into_bytes()); + chunk(b"iTXt", &itxt); + chunk(b"IEND", &[]); + let m = crate::extract(&p); + assert_eq!(m.iso, Some(400)); + assert_eq!(m.label.as_deref(), Some("Red")); + + let mut w = b"RIFF\0\0\0\0WEBP".to_vec(); + let mut body = |id: &[u8], data: &[u8]| { + w.extend_from_slice(id); + w.extend_from_slice(&(data.len() as u32).to_le_bytes()); + w.extend_from_slice(data); + if data.len() % 2 == 1 { + w.push(0); + } + }; + body(b"VP8X", &[0; 10]); + body(b"ICCP", b"abc"); + let mut ex = b"Exif\0\0".to_vec(); + ex.extend_from_slice(&exif); + body(b"EXIF", &ex); + let e = webp_chunks(&w); + assert_eq!(e.icc.as_deref(), Some(&b"abc"[..])); + assert_eq!(crate::extract(&w).f_number, Some(2.8)); + } + + #[test] + fn tiff_file_is_its_own_exif() { + let t = sample_exif(ByteOrder::Big); + assert_eq!(crate::extract(&t).model.as_deref(), Some("Model X")); + assert_eq!(embedded(b"not an image"), Embedded::default()); + } + + #[test] + fn raf_reads_its_embedded_jpeg() { + let j = sample_jpeg(); + let mut raf = b"FUJIFILMCCD-RAW 0201FF000000TEST".to_vec(); + raf.resize(120, 0); + raf[84..88].copy_from_slice(&120u32.to_be_bytes()); + raf[88..92].copy_from_slice(&(j.len() as u32).to_be_bytes()); + raf.extend_from_slice(&j); + assert_eq!(crate::extract(&raf).iso, Some(400)); + for n in 0..raf.len() { + let _ = crate::extract(&raf[..n]); + } + } + + #[test] + fn truncated_containers_do_not_panic() { + let j = sample_jpeg(); + for n in 0..j.len() { + let _ = crate::extract(&j[..n]); + } + } +} diff --git a/crates/metadata/src/cr3.rs b/crates/metadata/src/cr3.rs new file mode 100644 index 000000000..50f3f28d2 --- /dev/null +++ b/crates/metadata/src/cr3.rs @@ -0,0 +1,742 @@ +//! Canon CR3: the ISO base media file container (ISO/IEC 14496-12 box structure; CR3 layout from Laurent +//! Clévy's public CR3 format notes, confirmed black-box on EOS R6 Mark III files). +//! +//! - `ftyp` with major brand `crx `. +//! - `moov` holds a Canon `uuid` box (85c0b687-820f-11e0-8111-f4ce462b6a48) whose children are `CMT1` (a TIFF +//! stream: IFD0), `CMT2` (TIFF: the Exif IFD), `CMT3` (TIFF: the Canon maker note), `CMT4` (TIFF: GPS) and the +//! `THMB` thumbnail, then one `trak` per stream: a full-size JPEG, a reduced raw, the full raw (`CRAW` sample +//! entries; raws carry a `CMP1` coding header) and timed metadata (`CTMD`). +//! - A top-level `uuid` box with the XMP uuid (be7acfcb-97a9-42e8-9c71-999491e3afac, XMP spec part 3) holds the +//! XMP packet. +//! +//! Every size and offset is checked against the buffer; nesting and box counts are bounded. + +/// A track's sample-entry kind. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Cr3TrackKind { + /// `CRAW` with a `JPEG` child: the full-size JPEG. + Jpeg, + /// `CRAW` raw image: dimensions and the `CMP1` coding header (byte range in the file). + Raw { + width: u16, + height: u16, + cmp1: Option<(usize, usize)>, + /// `CDI1/IAD1` image-area descriptor (byte range in the file). + iad1: Option<(usize, usize)>, + }, + /// Anything else (`CTMD` timed metadata, unknown entries). + Other([u8; 4]), +} + +/// One track's single sample: where it is in the file. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Cr3Track { + pub kind: Cr3TrackKind, + /// Byte offset and length of the sample (the first one) in the file, when the tables give them. + pub data: Option<(usize, usize)>, +} + +/// Canon's `CMP1` image coding descriptor, separate from the encoded track sample. +/// +/// Parsed from the payload (after the eight-byte box header). Geometry is the full sensor mosaic, +/// including optically masked borders. Unsupported encoding modes are retained for the decoder to +/// report explicitly, rather than confused with the full-size embedded JPEG. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Cr3Compression { + pub version: u16, + pub width: u32, + pub height: u32, + pub tile_width: u32, + pub tile_height: u32, + pub bit_depth: u8, + pub planes: u8, + /// 0 = RGGB, 1 = GRBG, 2 = GBRG, 3 = BGGR. + pub cfa_pattern: u8, + pub encoding: u8, + pub levels: u8, + pub tile_flags: u8, + /// Number of bytes at the start of the sample before its entropy-coded data. + pub header_size: u32, + pub median_bit_depth: Option, +} + +impl Cr3Compression { + /// Read the public `CMP1` field layout. Truncated or impossible descriptors return `None`. + pub fn parse(payload: &[u8]) -> Option { + // The first word is reserved in the public notes; real files use FF00, although older + // descriptions guessed FFFF. It is not a reliable format signature. + let header_len = usize::from(be16(payload, 2)?).checked_add(4)?; + if header_len < 52 || header_len > payload.len() { + return None; + } + let packed = *payload.get(25)?; + let coding = *payload.get(26)?; + let mut out = Self { + version: be16(payload, 4)?, + width: be32(payload, 8)?, + height: be32(payload, 12)?, + tile_width: be32(payload, 16)?, + tile_height: be32(payload, 20)?, + bit_depth: *payload.get(24)?, + planes: packed >> 4, + cfa_pattern: packed & 15, + encoding: coding >> 4, + levels: coding & 15, + tile_flags: *payload.get(27)?, + header_size: be32(payload, 28)?, + median_bit_depth: None, + }; + if out.width == 0 + || out.height == 0 + || out.tile_width == 0 + || out.tile_height == 0 + || out.tile_width > out.width + || out.tile_height > out.height + || !(1..=16).contains(&out.bit_depth) + || !(1..=4).contains(&out.planes) + || (out.planes > 1 && out.cfa_pattern > 3) + || out.header_size == 0 + { + return None; + } + if payload.get(32).is_some_and(|flags| flags & 0x80 != 0) { + // Extended CMP1: a four-byte length, flags, reserved bytes and six words. The optional + // median precision is used by type-3 burst encodings, not ordinary Bayer raw/craw. + let extension_len = usize::try_from(be32(payload, 52)?).ok()?; + let extension_end = 52usize.checked_add(extension_len)?; + if extension_len < 32 || extension_end > payload.len() { + return None; + } + if payload.get(56).is_some_and(|flags| flags & 0x40 != 0) { + let precision = *payload.get(84).filter(|_| extension_end > 84)?; + if !(1..=16).contains(&precision) { + return None; + } + out.median_bit_depth = Some(precision); + } + } + Some(out) + } +} + +/// `IAD1` geometry. Coordinates are inclusive sensor offsets: `[left, top, right, bottom]`. +/// A full raw descriptor distinguishes valid sensor pixels from the recommended final crop. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Cr3ImageArea { + pub width: u16, + pub height: u16, + pub crop: [u16; 4], + pub active: Option<[u16; 4]>, + pub masked_left: [u16; 4], + pub masked_top: Option<[u16; 4]>, +} + +impl Cr3ImageArea { + pub fn parse(payload: &[u8]) -> Option { + let bounds = |at: usize| Some([be16(payload, at)?, be16(payload, at + 2)?, be16(payload, at + 4)?, be16(payload, at + 6)?]); + let width = be16(payload, 4)?; + let height = be16(payload, 6)?; + if width == 0 || height == 0 { + return None; + } + let full = be16(payload, 10)? == 2; + Some(Self { + width, + height, + crop: bounds(16)?, + masked_left: bounds(24)?, + masked_top: if full { Some(bounds(32)?) } else { None }, + active: if full { Some(bounds(40)?) } else { None }, + }) + } +} + +impl Cr3Track { + /// Decode this track's image coding descriptor without reading its pixel data. + pub fn compression(&self, bytes: &[u8]) -> Option { + let Cr3TrackKind::Raw { cmp1: Some((at, len)), .. } = self.kind else { return None }; + Cr3Compression::parse(bytes.get(at..at.checked_add(len)?)?) + } + + pub fn image_area(&self, bytes: &[u8]) -> Option { + let Cr3TrackKind::Raw { iad1: Some((at, len)), .. } = self.kind else { return None }; + Cr3ImageArea::parse(bytes.get(at..at.checked_add(len)?)?) + } +} + +/// The parts of a CR3 file that are read. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Cr3<'a> { + /// `CMT1`…`CMT4` (index 0…3). + pub cmt: [Option<&'a [u8]>; 4], + pub thumbnail: Option<&'a [u8]>, + pub xmp: Option<&'a [u8]>, + pub tracks: Vec, +} + +/// One independent TIFF block from Canon timed metadata. Its offsets remain relative to this block. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Cr3TimedExif<'a> { + /// CTMD record type (7, 8 or 9). + pub record_type: u16, + /// Exif IFD (`0x8769`) or Canon maker note (`0x927c`). + pub tag: u32, + pub data: &'a [u8], +} + +impl Cr3<'_> { + /// Read TIFF blocks in the first samples of CTMD tracks. As-shot white balance and sensor + /// levels often live here rather than in the static `CMT3` maker note. Both record layers use + /// little-endian byte lengths; TIFF blocks retain their own byte order and offset origin. + pub fn timed_exif<'a>(&self, bytes: &'a [u8]) -> Vec> { + let mut out = Vec::new(); + let mut budget = MAX_BOXES; + for track in &self.tracks { + if track.kind != Cr3TrackKind::Other(*b"CTMD") { + continue; + } + let Some(sample) = track.data.and_then(|(at, len)| at.checked_add(len).and_then(|end| bytes.get(at..end))) else { continue }; + let mut at = 0usize; + while budget > 0 { + budget -= 1; + let Some(len) = le32(sample, at).and_then(|v| usize::try_from(v).ok()) else { break }; + let Some(end) = at.checked_add(len).filter(|&end| len >= 12 && end <= sample.len()) else { break }; + let Some(record) = sample.get(at..end) else { break }; + let Some(kind) = le16(record, 4) else { break }; + if matches!(kind, 7..=9) { + let mut item_at = 12usize; + while budget > 0 { + budget -= 1; + let Some(item_len) = le32(record, item_at).and_then(|v| usize::try_from(v).ok()) else { break }; + let Some(item_end) = item_at.checked_add(item_len).filter(|&end| item_len >= 8 && end <= record.len()) else { break }; + let Some(item) = record.get(item_at..item_end) else { break }; + if let Some(tag @ (0x8769 | 0x927c)) = le32(item, 4) + && let Some(data) = item.get(8..) + { + out.push(Cr3TimedExif { record_type: kind, tag, data }); + } + item_at = item_end; + } + } + at = end; + } + } + out + } +} + +const CANON_UUID: [u8; 16] = [0x85, 0xc0, 0xb6, 0x87, 0x82, 0x0f, 0x11, 0xe0, 0x81, 0x11, 0xf4, 0xce, 0x46, 0x2b, 0x6a, 0x48]; +/// The `uuid` of the top-level box holding the XMP packet. +pub const XMP_UUID: [u8; 16] = [0xbe, 0x7a, 0xcf, 0xcb, 0x97, 0xa9, 0x42, 0xe8, 0x9c, 0x71, 0x99, 0x94, 0x91, 0xe3, 0xaf, 0xac]; +const MAX_DEPTH: usize = 12; +pub(crate) const MAX_BOXES: usize = 4096; +/// A `VisualSampleEntry` (78 bytes after the box header) plus 4 bytes Canon adds before the child boxes. +const CRAW_CHILDREN_AT: usize = 82; + +/// Does this look like a CR3 file (`ftyp` box with major brand `crx `)? +pub fn is_cr3(bytes: &[u8]) -> bool { + bytes.get(4..12) == Some(b"ftypcrx ".as_slice()) +} + +/// Parse a CR3 file's boxes; `None` when it is not a CR3 file. Of the top-level boxes only `moov` +/// and the XMP `uuid` are read (a reader of a sparse file relies on it to skip the others). +pub fn parse_cr3(bytes: &[u8]) -> Option> { + if !is_cr3(bytes) { + return None; + } + let mut out = Cr3::default(); + let mut budget = MAX_BOXES; + for b in boxes(bytes, 0, bytes.len(), &mut budget) { + match &b.kind { + b"moov" => moov(bytes, &b, &mut out, &mut budget), + b"uuid" if b.uuid == Some(XMP_UUID) => out.xmp = bytes.get(b.body..b.end), + _ => {} + } + } + Some(out) +} + +pub(crate) struct BoxRef { + pub(crate) kind: [u8; 4], + /// Start of the payload (after the header and, for `uuid`, the 16-byte uuid). + pub(crate) body: usize, + pub(crate) end: usize, + pub(crate) uuid: Option<[u8; 16]>, +} + +/// The boxes directly inside `[from, to)`; stops at the first malformed header. +pub(crate) fn boxes(bytes: &[u8], from: usize, to: usize, budget: &mut usize) -> Vec { + let mut out = Vec::new(); + let mut at = from; + let to = to.min(bytes.len()); + while at.saturating_add(8) <= to && *budget > 0 { + *budget -= 1; + let (Some(size), Some(kind)) = (be32(bytes, at), bytes.get(at + 4..at + 8)) else { break }; + let mut kind4 = [0u8; 4]; + kind4.copy_from_slice(kind); + let (len, mut hdr) = match size { + 0 => (to - at, 8), + 1 => match be64(bytes, at + 8).and_then(|v| usize::try_from(v).ok()) { + Some(l) => (l, 16), + None => break, + }, + n => (n as usize, 8), + }; + let Some(end) = at.checked_add(len).filter(|&e| e <= to && len >= hdr) else { break }; + let mut uuid = None; + if &kind4 == b"uuid" { + let Some(u) = bytes.get(at + hdr..at + hdr + 16).filter(|_| at + hdr + 16 <= end) else { break }; + let mut a = [0u8; 16]; + a.copy_from_slice(u); + uuid = Some(a); + hdr += 16; + } + out.push(BoxRef { kind: kind4, body: at + hdr, end, uuid }); + at = end; + } + out +} + +fn moov<'a>(bytes: &'a [u8], m: &BoxRef, out: &mut Cr3<'a>, budget: &mut usize) { + for b in boxes(bytes, m.body, m.end, budget) { + match &b.kind { + b"uuid" if b.uuid == Some(CANON_UUID) => { + for c in boxes(bytes, b.body, b.end, budget) { + let slot = match &c.kind { + b"CMT1" => 0, + b"CMT2" => 1, + b"CMT3" => 2, + b"CMT4" => 3, + b"THMB" => { + out.thumbnail = bytes.get(c.body..c.end); + continue; + } + _ => continue, + }; + out.cmt[slot] = bytes.get(c.body..c.end); + } + } + b"trak" => { + if let Some(t) = trak(bytes, &b, budget) { + out.tracks.push(t); + } + } + _ => {} + } + } +} + +/// Descend `trak` → `mdia` → `minf` → `stbl` and read its sample entry and first sample's location. +fn trak(bytes: &[u8], t: &BoxRef, budget: &mut usize) -> Option { + let mut stbl = None; + let mut stack = vec![(t.body, t.end, 0usize)]; + while let Some((from, to, depth)) = stack.pop() { + if depth > MAX_DEPTH { + continue; + } + for b in boxes(bytes, from, to, budget) { + match &b.kind { + b"mdia" | b"minf" => stack.push((b.body, b.end, depth + 1)), + b"stbl" => stbl = Some(b), + _ => {} + } + } + } + let stbl = stbl?; + let (mut kind, mut size, mut offset) = (None, None, None); + for b in boxes(bytes, stbl.body, stbl.end, budget) { + let body = bytes.get(b.body..b.end)?; + match &b.kind { + // full box: version/flags, entry count, entries + b"stsd" if be32(body, 4)? > 0 => kind = boxes(bytes, b.body + 8, b.end, budget).first().map(|e| sample_entry(bytes, e, budget)), + // version/flags, sample_size (0 = per-sample table), count, sizes + b"stsz" => { + size = match be32(body, 4)? { + 0 if be32(body, 8)? > 0 => be32(body, 12), + 0 => None, + n if be32(body, 8)? > 0 => Some(n), + _ => None, + } + } + b"co64" if be32(body, 4)? > 0 => offset = be64(body, 8).and_then(|v| usize::try_from(v).ok()), + b"stco" if be32(body, 4)? > 0 => offset = be32(body, 8).map(|v| v as usize), + _ => {} + } + } + let data = match (offset, size) { + (Some(o), Some(s)) if o.checked_add(s as usize).is_some_and(|e| e <= bytes.len()) => Some((o, s as usize)), + _ => None, + }; + Some(Cr3Track { kind: kind?, data }) +} + +fn sample_entry(bytes: &[u8], e: &BoxRef, budget: &mut usize) -> Cr3TrackKind { + if &e.kind != b"CRAW" { + return Cr3TrackKind::Other(e.kind); + } + let Some(body) = bytes.get(e.body..e.end) else { return Cr3TrackKind::Other(e.kind) }; + let (Some(width), Some(height)) = (be16(body, 24), be16(body, 26)) else { + return Cr3TrackKind::Other(e.kind); + }; + let (mut cmp1, mut iad1, mut rendered) = (None, None, None); + for c in boxes(bytes, e.body.saturating_add(CRAW_CHILDREN_AT), e.end, budget) { + match &c.kind { + b"JPEG" => rendered = Some(Cr3TrackKind::Jpeg), + // HDR-capable cameras also put an HEVC preview in a CRAW entry, + // often with the same displayed dimensions as the main raw track. + b"HEVC" => rendered = Some(Cr3TrackKind::Other(c.kind)), + b"CMP1" => cmp1 = Some((c.body, c.end - c.body)), + b"CDI1" => { + for area in boxes(bytes, c.body.saturating_add(4), c.end, budget) { + if &area.kind == b"IAD1" { + iad1 = Some((area.body, area.end - area.body)); + } + } + } + _ => {} + } + } + // A present, malformed CMP1 must remain a raw descriptor so its corruption + // is reported. Only an explicitly identified rendered codec can establish + // that a CRAW entry without CMP1 is a preview rather than a broken raw. + if cmp1.is_none() + && let Some(kind) = rendered + { + return kind; + } + Cr3TrackKind::Raw { width, height, cmp1, iad1 } +} + +/// A big-endian `u16` at `at`, or `None` when the buffer is shorter than that. +pub(crate) fn be16(b: &[u8], at: usize) -> Option { + b.get(at..at.checked_add(2)?).map(|s| u16::from_be_bytes([s[0], s[1]])) +} +/// A big-endian `u32` at `at`, or `None` when the buffer is shorter than that. +pub(crate) fn be32(b: &[u8], at: usize) -> Option { + b.get(at..at.checked_add(4)?).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]])) +} +/// A big-endian `u64` at `at`, or `None` when the buffer is shorter than that. +pub(crate) fn be64(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(8)?)?; + let mut a = [0u8; 8]; + a.copy_from_slice(s); + Some(u64::from_be_bytes(a)) +} + +fn le16(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(2)?)?; + Some(u16::from_le_bytes([s[0], s[1]])) +} + +fn le32(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(4)?)?; + Some(u32::from_le_bytes([s[0], s[1], s[2], s[3]])) +} + +/// Merge `CMT1` (IFD0), `CMT2` (Exif) and `CMT4` (GPS) into one TIFF stream, so the Exif readers see a CR3 +/// like any TIFF-based raw. The maker note (`CMT3`) is left out: its offsets are relative to its own block. +pub fn merged_exif(c: &Cr3<'_>) -> Option> { + use craft_tiff::{IfdBuilder, Tiff, TiffWriter, tags as t}; + let ifd0 = Tiff::parse(c.cmt[0]?).ok()?; + let order = ifd0.order; + let first = |i: usize| c.cmt[i].and_then(|b| Tiff::parse(b).ok()).and_then(|t| t.ifds.into_iter().next()); + // pointers and offsets that would dangle once the IFDs move + const SKIP: [u16; 9] = [t::EXIF_IFD, t::GPS_IFD, t::INTEROP_IFD, t::SUB_IFDS, t::MAKER_NOTE, 273, 279, 513, 514]; + let build = |ifd: &craft_tiff::Ifd| { + let mut b = IfdBuilder::new(); + for e in ifd.entries.iter().filter(|e| !SKIP.contains(&e.tag)) { + b.set(e.tag, e.value.clone()); + } + b + }; + let mut root = build(ifd0.ifds.first()?); + if let Some(exif) = first(1) { + root.set_child(t::EXIF_IFD, build(&exif)); + } + if let Some(gps) = first(3) { + root.set_child(t::GPS_IFD, build(&gps)); + } + TiffWriter::new(order, false).write(&[root]).ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn bx(kind: &[u8; 4], body: &[u8]) -> Vec { + let mut v = ((body.len() + 8) as u32).to_be_bytes().to_vec(); + v.extend_from_slice(kind); + v.extend_from_slice(body); + v + } + fn uuid(u: &[u8; 16], body: &[u8]) -> Vec { + let mut b = u.to_vec(); + b.extend_from_slice(body); + bx(b"uuid", &b) + } + fn full(kind: &[u8; 4], body: &[u8]) -> Vec { + let mut b = vec![0u8; 4]; + b.extend_from_slice(body); + bx(kind, &b) + } + fn craw(w: u16, h: u16, children: &[u8]) -> Vec { + let mut b = vec![0u8; CRAW_CHILDREN_AT]; + b[24..26].copy_from_slice(&w.to_be_bytes()); + b[26..28].copy_from_slice(&h.to_be_bytes()); + b.extend_from_slice(children); + bx(b"CRAW", &b) + } + fn trak(entry: Vec, offset: u64, size: u32) -> Vec { + let mut stsd = 1u32.to_be_bytes().to_vec(); + stsd.extend(entry); + let mut stsz = 0u32.to_be_bytes().to_vec(); + stsz.extend(1u32.to_be_bytes()); + stsz.extend(size.to_be_bytes()); + let mut co64 = 1u32.to_be_bytes().to_vec(); + co64.extend(offset.to_be_bytes()); + let stbl = [full(b"stsd", &stsd), full(b"stsz", &stsz), full(b"co64", &co64)].concat(); + bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &stbl)))) + } + fn tiff_with(tag: u16, v: craft_tiff::Value) -> Vec { + let b = craft_tiff::IfdBuilder::new().with(tag, v); + craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[b]).unwrap() + } + + /// A small synthetic CR3: Canon uuid with CMT1/CMT2/CMT4, a JPEG track, a raw track, XMP. + pub(crate) fn sample() -> Vec { + use craft_tiff::{Value, tags as t}; + let cmt1 = tiff_with(t::MODEL, Value::Ascii("Canon EOS Test".into())); + let cmt2 = tiff_with(t::ISO_SPEED, Value::Short(vec![400])); + let cmt4 = tiff_with(1, Value::Ascii("N".into())); + let canon = uuid(&CANON_UUID, &[bx(b"CMT1", &cmt1), bx(b"CMT2", &cmt2), bx(b"CMT4", &cmt4), bx(b"THMB", b"thumb")].concat()); + let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + let payload_at = 4096u64; + let jpeg_t = trak(craw(64, 48, &bx(b"JPEG", &[0; 4])), payload_at, 16); + let raw_t = trak(craw(60, 40, &bx(b"CMP1", &[0xff, 0x10, 0, 0x30])), payload_at + 16, 32); + file.extend(bx(b"moov", &[canon, jpeg_t, raw_t].concat())); + file.extend(uuid(&XMP_UUID, b"")); + file.resize(payload_at as usize + 64, 0); + file + } + + #[test] + fn parses_canon_boxes_tracks_and_xmp() { + let f = sample(); + assert!(is_cr3(&f)); + let c = parse_cr3(&f).unwrap(); + assert!(c.cmt[0].is_some() && c.cmt[1].is_some() && c.cmt[2].is_none() && c.cmt[3].is_some()); + assert_eq!(c.thumbnail, Some(b"thumb".as_slice())); + assert_eq!(c.xmp, Some(b"".as_slice())); + assert_eq!(c.tracks.len(), 2); + assert_eq!(c.tracks[0], Cr3Track { kind: Cr3TrackKind::Jpeg, data: Some((4096, 16)) }); + let Cr3TrackKind::Raw { width, height, cmp1: Some((at, len)), .. } = c.tracks[1].kind else { panic!("{:?}", c.tracks[1]) }; + assert_eq!((width, height, len), (60, 40, 4)); + assert_eq!(&f[at..at + 2], &[0xff, 0x10]); + assert_eq!(c.tracks[1].data, Some((4112, 32))); + } + + #[test] + fn hevc_preview_is_distinct_from_an_equal_size_raw_track() { + let preview = trak(craw(6000, 4000, &bx(b"HEVC", &[0; 4])), 4096, 16); + let reduced = trak(craw(1624, 1080, &bx(b"CMP1", &compression_payload(0))), 4112, 16); + let sensor = trak(craw(6000, 4000, &bx(b"CMP1", &compression_payload(0))), 4128, 16); + let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + file.extend(bx(b"moov", &[preview, reduced, sensor].concat())); + file.resize(4144, 0); + let container = parse_cr3(&file).unwrap(); + assert_eq!(container.tracks[0].kind, Cr3TrackKind::Other(*b"HEVC")); + assert!(container.tracks[0].compression(&file).is_none()); + assert!(matches!(container.tracks[2].kind, Cr3TrackKind::Raw { width: 6000, height: 4000, cmp1: Some(_), .. })); + assert_eq!(container.tracks[2].compression(&file).unwrap().width, 6888); + } + + #[test] + fn rendered_child_does_not_hide_a_malformed_raw_descriptor() { + for codec in [b"JPEG", b"HEVC"] { + for first in [true, false] { + let rendered = bx(codec, &[0; 4]); + let corrupt = bx(b"CMP1", &[0xff, 0]); + let children = if first { [rendered, corrupt] } else { [corrupt, rendered] }.concat(); + let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + file.extend(bx(b"moov", &trak(craw(6000, 4000, &children), 4096, 16))); + file.resize(4112, 0); + let container = parse_cr3(&file).unwrap(); + assert!(matches!(container.tracks[0].kind, Cr3TrackKind::Raw { cmp1: Some(_), .. })); + assert!(container.tracks[0].compression(&file).is_none()); + } + } + // Absence alone is not evidence of a preview: retain a broken raw so + // main-track selection cannot quietly fall back to a reduced image. + let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + file.extend(bx(b"moov", &trak(craw(6000, 4000, &bx(b"free", &[])), 4096, 16))); + file.resize(4112, 0); + assert!(matches!(parse_cr3(&file).unwrap().tracks[0].kind, Cr3TrackKind::Raw { cmp1: None, .. })); + } + + #[test] + fn merged_exif_reads_like_a_tiff_raw() { + let f = sample(); + let m = crate::read_exif(&merged_exif(&parse_cr3(&f).unwrap()).unwrap()); + assert_eq!(m.model.as_deref(), Some("Canon EOS Test")); + assert_eq!(m.iso, Some(400)); + } + + fn compression_payload(levels: u8) -> Vec { + let mut b = vec![0u8; 52]; + b[0..2].copy_from_slice(&0xff00u16.to_be_bytes()); + b[2..4].copy_from_slice(&48u16.to_be_bytes()); + b[4..6].copy_from_slice(&0x100u16.to_be_bytes()); + for (at, value) in [(8, 6888u32), (12, 4546), (16, 3444), (20, 4546), (28, 216)] { + b[at..at + 4].copy_from_slice(&value.to_be_bytes()); + } + b[24] = 14; + b[25] = 0x40; + b[26] = levels; + b + } + + #[test] + fn compression_descriptor_retains_sensor_geometry_and_cfa() { + let b = compression_payload(0); + let h = Cr3Compression::parse(&b).unwrap(); + assert_eq!((h.width, h.height, h.tile_width, h.tile_height), (6888, 4546, 3444, 4546)); + assert_eq!((h.version, h.bit_depth, h.planes, h.cfa_pattern, h.encoding, h.levels, h.header_size), (0x100, 14, 4, 0, 0, 0, 216)); + let mut craw = compression_payload(3); + craw[25] = 0x41; + craw[27] = 0x80; + let h = Cr3Compression::parse(&craw).unwrap(); + assert_eq!((h.cfa_pattern, h.levels, h.tile_flags), (1, 3, 0x80)); + for len in 0..b.len() { + assert!(Cr3Compression::parse(&b[..len]).is_none()); + } + for (at, value) in [(24, 0), (24, 17), (25, 0), (25, 0x44)] { + let mut bad = b.clone(); + bad[at] = value; + assert!(Cr3Compression::parse(&bad).is_none()); + } + } + + #[test] + fn extended_descriptor_checks_optional_median_precision() { + let mut b = compression_payload(0x30); + b[32] = 0x80; + b.extend_from_slice(&33u32.to_be_bytes()); + b.resize(85, 0); + b[56] = 0xc0; + b[84] = 14; + assert_eq!(Cr3Compression::parse(&b).unwrap().median_bit_depth, Some(14)); + assert!(Cr3Compression::parse(&b[..84]).is_none()); + b[84] = 0; + assert!(Cr3Compression::parse(&b).is_none()); + } + + #[test] + fn raw_track_reads_distinct_active_area_and_default_crop() { + let mut area = vec![0u8; 48]; + for (at, value) in [ + (4, 6888u16), + (6, 4546), + (10, 2), + (16, 156), + (18, 158), + (20, 6875), + (22, 4537), + (24, 0), + (26, 0), + (28, 143), + (30, 4545), + (32, 144), + (34, 0), + (36, 6887), + (38, 45), + (40, 144), + (42, 46), + (44, 6887), + (46, 4545), + ] { + area[at..at + 2].copy_from_slice(&value.to_be_bytes()); + } + let cmp1 = bx(b"CMP1", &compression_payload(0)); + let iad1 = full(b"CDI1", &bx(b"IAD1", &area)); + let track = trak(craw(6888, 4546, &[cmp1, iad1].concat()), 4096, 32); + let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + file.extend(bx(b"moov", &track)); + file.resize(4128, 0); + let c = parse_cr3(&file).unwrap(); + assert_eq!(c.tracks[0].compression(&file).unwrap().width, 6888); + let geometry = c.tracks[0].image_area(&file).unwrap(); + assert_eq!(geometry.crop, [156, 158, 6875, 4537]); + assert_eq!(geometry.active, Some([144, 46, 6887, 4545])); + assert_eq!(geometry.masked_left, [0, 0, 143, 4545]); + for len in 0..area.len() { + assert!(Cr3ImageArea::parse(&area[..len]).is_none()); + } + } + + #[test] + fn truncated_sample_table_does_not_read_the_next_box() { + let stbl = [ + full(b"stsd", &[1u32.to_be_bytes().to_vec(), craw(16, 16, &bx(b"CMP1", &compression_payload(0)))].concat()), + bx(b"stsz", &[]), + full(b"co64", &[1u32.to_be_bytes().to_vec(), 256u64.to_be_bytes().to_vec()].concat()), + ] + .concat(); + let mut file = bx(b"ftyp", b"crx \0\0\0\x01crx isom"); + file.extend(bx(b"moov", &bx(b"trak", &bx(b"mdia", &bx(b"minf", &bx(b"stbl", &stbl)))))); + file.resize(512, 0); + assert!(parse_cr3(&file).unwrap().tracks.is_empty()); + } + + #[test] + fn timed_metadata_exposes_the_dynamic_maker_note() { + use craft_tiff::{Value, tags as t}; + let exif = tiff_with(t::ISO_SPEED, Value::Short(vec![800])); + let maker = tiff_with(0x4001, Value::Short(vec![48, 100, 200])); + let item = |tag: u32, data: &[u8]| { + let mut b = ((data.len() + 8) as u32).to_le_bytes().to_vec(); + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(data); + b + }; + let items = [item(0x8769, &exif), item(0x927c, &maker)].concat(); + let mut sample = vec![0u8; 12]; + sample[0..4].copy_from_slice(&((12 + items.len()) as u32).to_le_bytes()); + sample[4..6].copy_from_slice(&8u16.to_le_bytes()); + sample.extend(items); + let container = Cr3 { tracks: vec![Cr3Track { kind: Cr3TrackKind::Other(*b"CTMD"), data: Some((0, sample.len())) }], ..Default::default() }; + let parts = container.timed_exif(&sample); + assert_eq!(parts.len(), 2); + assert_eq!((parts[0].record_type, parts[0].tag, parts[0].data), (8, 0x8769, exif.as_slice())); + assert_eq!((parts[1].record_type, parts[1].tag, parts[1].data), (8, 0x927c, maker.as_slice())); + for len in 0..sample.len() { + assert!(container.timed_exif(&sample[..len]).is_empty()); + } + for at in [0, 12] { + let mut corrupt = sample.clone(); + corrupt[at..at + 4].copy_from_slice(&u32::MAX.to_le_bytes()); + assert!(container.timed_exif(&corrupt).is_empty()); + corrupt[at..at + 4].copy_from_slice(&0u32.to_le_bytes()); + assert!(container.timed_exif(&corrupt).is_empty()); + } + } + + #[test] + fn hostile_input_never_panics() { + let f = sample(); + // every truncation and single-byte corruption parses to something or nothing + for n in 0..f.len().min(800) { + let _ = parse_cr3(&f[..n]).map(|c| merged_exif(&c)); + } + for i in 0..f.len().min(800) { + for v in [0u8, 1, 0x7f, 0xff] { + let mut g = f.clone(); + g[i] = v; + let _ = parse_cr3(&g).map(|c| merged_exif(&c)); + } + } + // a sample offset past the end is not reported + let mut g = f.clone(); + g.truncate(4100); + assert!(parse_cr3(&g).unwrap().tracks.iter().all(|t| t.data.is_none())); + } +} diff --git a/crates/metadata/src/datetime.rs b/crates/metadata/src/datetime.rs new file mode 100644 index 000000000..0ecedae36 --- /dev/null +++ b/crates/metadata/src/datetime.rs @@ -0,0 +1,320 @@ +//! Capture date/time with optional UTC offset (Exif `DateTimeOriginal` + `OffsetTimeOriginal`, ISO 8601 in XMP). + +use serde::{Deserialize, Serialize}; + +/// A civil date-time as recorded by the camera; `offset_minutes` is the UTC offset when known. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +pub struct DateTime { + pub year: u16, + pub month: u8, + pub day: u8, + pub hour: u8, + pub minute: u8, + pub second: u8, + /// Sub-second part in milliseconds. + pub millis: u16, + pub offset_minutes: Option, +} + +fn num(s: &str) -> Option { + if s.is_empty() || !s.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + s.parse().ok() +} + +impl DateTime { + fn valid(self) -> Option { + // A timestamp must represent a real Gregorian calendar day, not just + // numbers in the range 1..=31. Invalid dates would otherwise be + // accepted from EXIF/XMP and silently normalized during time arithmetic. + // chrono rejects impossible days (leap years and all); the time is checked + // by hand so a leap second (`second == 60`) stays acceptable. + let day_ok = self.year > 0 && chrono::NaiveDate::from_ymd_opt(i32::from(self.year), u32::from(self.month), u32::from(self.day)).is_some(); + let ok = day_ok && self.hour < 24 && self.minute < 60 && self.second < 61 && self.offset_minutes.is_none_or(|o| o.unsigned_abs() <= 18 * 60); + ok.then_some(self) + } + + /// Exif form `YYYY:MM:DD HH:MM:SS` (also tolerates `-` date separators and a missing time). + pub fn parse_exif(s: &str) -> Option { + let s = s.trim().trim_end_matches('\0'); + let (d, t) = s.split_once([' ', 'T']).unwrap_or((s, "")); + let dp: Vec<&str> = d.split([':', '-']).collect(); + if dp.len() != 3 { + return None; + } + let (hour, minute, second) = if t.is_empty() { + (0, 0, 0) + } else { + let mut parts = t.split(':'); + let hour = num(parts.next()?.trim())?; + let minute = parts.next().map(|part| num(part.trim())).unwrap_or(Some(0))?; + let second = parts.next().map(|part| num(part.trim())).unwrap_or(Some(0))?; + // A fourth value cannot be represented; accepting only the prefix could turn a + // malformed DateTimeOriginal into a plausible capture time and block fallbacks. + if parts.next().is_some() { + return None; + } + (hour, minute, second) + }; + DateTime { + year: num(dp[0])?.try_into().ok()?, + month: num(dp[1])?.try_into().ok()?, + day: num(dp[2])?.try_into().ok()?, + hour: hour.try_into().ok()?, + minute: minute.try_into().ok()?, + second: second.try_into().ok()?, + millis: 0, + offset_minutes: None, + } + .valid() + } + + /// Exif `OffsetTime*` form `+HH:MM` / `-HH:MM` (or `Z`). + pub fn parse_offset(s: &str) -> Option { + let s = s.trim().trim_end_matches('\0'); + if s == "Z" { + return Some(0); + } + let (sign, rest) = match s.as_bytes().first()? { + b'+' => (1, &s[1..]), + b'-' => (-1, &s[1..]), + _ => return None, + }; + let (h, m) = match rest.split_once(':') { + Some((h, m)) => (num(h)?, num(m)?), + None if rest.len() == 4 => (num(rest.get(..2)?)?, num(rest.get(2..)?)?), + None => (num(rest)?, 0), + }; + if h > 18 || m > 59 || (h == 18 && m > 0) { + return None; + } + Some(sign * (h * 60 + m) as i16) + } + + /// Set the sub-second part from an Exif `SubSecTime*` string (digits are a decimal fraction). + pub fn with_subsec(mut self, s: &str) -> DateTime { + let digits: String = s.trim().chars().take_while(|c| c.is_ascii_digit()).take(3).collect(); + if !digits.is_empty() { + let v: u16 = digits.parse().unwrap_or(0); + self.millis = v * 10u16.pow(3 - digits.len() as u32); + } + self + } + + /// ISO 8601 (XMP date): `YYYY`, `YYYY-MM`, `YYYY-MM-DD`, `YYYY-MM-DDThh:mm[:ss[.s+]][TZD]`. + pub fn parse_iso(s: &str) -> Option { + let s = s.trim(); + let (d, t) = match s.split_once('T') { + Some((d, t)) if !t.is_empty() => (d, t), + Some(_) => return None, + None => (s, ""), + }; + let dp: Vec<&str> = d.split('-').collect(); + let year = num(dp.first()?)?.try_into().ok()?; + let month = dp.get(1).map(|m| num(m)).unwrap_or(Some(1))?.try_into().ok()?; + let day = dp.get(2).map(|m| num(m)).unwrap_or(Some(1))?.try_into().ok()?; + if dp.len() > 3 { + return None; + } + let mut dt = DateTime { year, month, day, ..Default::default() }; + if !t.is_empty() { + let (clock, off) = match t.find(['Z', '+', '-']) { + Some(i) => (&t[..i], Some(&t[i..])), + None => (t, None), + }; + let mut cp = clock.split(':'); + dt.hour = num(cp.next()?)?.try_into().ok()?; + dt.minute = num(cp.next()?)?.try_into().ok()?; + if let Some(sec) = cp.next() { + let (whole, fraction) = match sec.split_once('.') { + Some((whole, fraction)) => { + if fraction.is_empty() || !fraction.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + (whole, Some(fraction)) + } + None => (sec, None), + }; + dt.second = num(whole)?.try_into().ok()?; + if let Some(fraction) = fraction { + dt = dt.with_subsec(fraction); + } + } + if cp.next().is_some() { + return None; + } + if let Some(o) = off { + dt.offset_minutes = Some(Self::parse_offset(o)?); + } + } + dt.valid() + } + + /// Exif form `YYYY:MM:DD HH:MM:SS`. + pub fn to_exif(&self) -> String { + format!("{:04}:{:02}:{:02} {:02}:{:02}:{:02}", self.year, self.month, self.day, self.hour, self.minute, self.second) + } + + /// ISO 8601 with milliseconds when non-zero and the offset when known. + pub fn to_iso(&self) -> String { + let mut s = format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}", self.year, self.month, self.day, self.hour, self.minute, self.second); + if self.millis != 0 { + s += &format!(".{:03}", self.millis); + } + if let Some(o) = self.offset_minutes { + let sign = if o < 0 { '-' } else { '+' }; + let o = o.unsigned_abs(); + s += &format!("{sign}{:02}:{:02}", o / 60, o % 60); + } + s + } + + /// Seconds since 1970-01-01T00:00:00 of the civil time (UTC when the offset is known, else "local" seconds). + pub fn unix_seconds(&self) -> i64 { + // Every `DateTime` reaching here is a real calendar day (`valid()` ran in the parsers); + // an out-of-range one (built by hand) reads as the epoch rather than panicking. + let days = chrono::NaiveDate::from_ymd_opt(i32::from(self.year), u32::from(self.month), u32::from(self.day)) + .zip(chrono::NaiveDate::from_ymd_opt(1970, 1, 1)) + .map(|(d, epoch)| (d - epoch).num_days()) + .unwrap_or(0); + days * 86400 + self.hour as i64 * 3600 + self.minute as i64 * 60 + self.second as i64 - self.offset_minutes.unwrap_or(0) as i64 * 60 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn exif_forms() { + let d = DateTime::parse_exif("2024:06:01 13:45:59").unwrap(); + assert_eq!((d.year, d.month, d.day, d.hour, d.minute, d.second), (2024, 6, 1, 13, 45, 59)); + assert_eq!(d.to_exif(), "2024:06:01 13:45:59"); + assert!(DateTime::parse_exif("0000:00:00 00:00:00").is_none()); + assert!(DateTime::parse_exif(" : : : : ").is_none()); + assert!(DateTime::parse_exif("2024:13:01 00:00:00").is_none()); + // Invalid calendar days must never enter persisted capture metadata. + for value in ["2023:02:29", "2024:02:30", "2024:04:31", "1900:02:29", "2024:01:00"] { + assert!(DateTime::parse_exif(value).is_none(), "{value}"); + } + assert!(DateTime::parse_exif("2024:02:29").is_some()); + assert!(DateTime::parse_exif("2000:02:29").is_some()); + for value in ["2023-02-29", "2024-02-30", "1900-02-29", "2024-06-31T10:00:00Z"] { + assert!(DateTime::parse_iso(value).is_none(), "{value}"); + } + assert!(DateTime::parse_iso("2000-02-29T23:59:59Z").is_some()); + assert_eq!(DateTime::parse_exif("2024-06-01").unwrap().hour, 0); + assert_eq!(DateTime::parse_exif("2024:06:01 13:45:59").unwrap().with_subsec("5").millis, 500); + assert_eq!(DateTime::parse_exif("2024:06:01 13:45:59").unwrap().with_subsec("0123").millis, 12); + } + + #[test] + fn exif_clock_rejects_extra_components() { + for value in + ["2026:01:01 12:34:56:99", "2026:01:01 12:34:56:junk", "2026:01:01 12:34:56:", "2026:01:01 12:34:56::", "2026:01:01 12:34:56:99:junk"] + { + assert!(DateTime::parse_exif(value).is_none(), "accepted malformed EXIF clock {value:?}"); + } + } + + #[test] + fn exif_clock_supported_forms_keep_their_meaning() { + for (value, expected) in [ + ("2026:01:01", "2026-01-01T00:00:00"), + ("2026:01:01 12", "2026-01-01T12:00:00"), + ("2026:01:01 12:34", "2026-01-01T12:34:00"), + ("2026:01:01 12:34:56", "2026-01-01T12:34:56"), + ("2026-01-01T12:34:56", "2026-01-01T12:34:56"), + (" 2026:01:01 12:34:56\0", "2026-01-01T12:34:56"), + ("2026:01:01T12:34:60", "2026-01-01T12:34:60"), + ] { + assert_eq!(DateTime::parse_exif(value).map(|date| date.to_iso()), Some(expected.to_string()), "{value:?}"); + } + } + + #[test] + fn offsets() { + assert_eq!(DateTime::parse_offset("+02:00"), Some(120)); + assert_eq!(DateTime::parse_offset("-05:30"), Some(-330)); + assert_eq!(DateTime::parse_offset("Z"), Some(0)); + assert_eq!(DateTime::parse_offset("+0930"), Some(570)); + assert_eq!(DateTime::parse_offset("+18:00"), Some(1080)); + assert_eq!(DateTime::parse_offset("-18:00"), Some(-1080)); + assert_eq!(DateTime::parse_offset("+18:01"), None); + assert_eq!(DateTime::parse_offset("-18:01"), None); + assert_eq!(DateTime::parse_offset(" : "), None); + assert_eq!(DateTime::parse_offset("+99:00"), None); + } + + #[test] + fn extreme_offsets_do_not_overflow() { + // `DateTime` deserializes with public fields, so an offset can arrive without passing `valid()` + let d = DateTime { offset_minutes: Some(i16::MIN), ..DateTime::parse_iso("2024-06-01T13:45:59").unwrap() }; + assert_eq!(d.to_iso(), "2024-06-01T13:45:59-546:08"); + assert_eq!(d.valid(), None); + } + + #[test] + fn unicode_offsets_are_rejected_without_panicking() { + for offset in ["+1€", "-1€", "+€1", "-€1"] { + assert_eq!(DateTime::parse_offset(offset), None); + let date = format!("2024-06-01T13:45:59{offset}"); + let metadata = crate::Metadata { capture_time: DateTime::parse_iso("2024-06-01T13:45:59Z"), ..Default::default() }; + let xmp = crate::write_xmp(&metadata).replace("2024-06-01T13:45:59+00:00", &date); + assert!(crate::parse_xmp(&xmp).unwrap().metadata.capture_time.is_none()); + } + } + + #[test] + fn iso_roundtrip() { + for s in ["2024-06-01T13:45:59", "2024-06-01T13:45:59.250+02:00", "1999-12-31T23:59:00-05:30", "2024-02-29T00:00:00Z"] { + let d = DateTime::parse_iso(s).unwrap(); + let again = DateTime::parse_iso(&d.to_iso()).unwrap(); + assert_eq!(d, again, "{s}"); + } + assert_eq!(DateTime::parse_iso("2024-02-29T00:00:00Z").unwrap().to_iso(), "2024-02-29T00:00:00+00:00"); + let d = DateTime::parse_iso("2019").unwrap(); + assert_eq!((d.year, d.month, d.day), (2019, 1, 1)); + assert_eq!(DateTime::parse_iso("2019-07-04T10:11").unwrap().minute, 11); + assert!(DateTime::parse_iso("nonsense").is_none()); + assert!(DateTime::parse_iso("2019-07-04Tab:cd").is_none()); + } + + #[test] + fn iso_rejects_incomplete_or_unconsumed_clock_text() { + for value in [ + "2026-01-01T", + "2026-01-01T12:34:56:99Z", + "2026-01-01T12:34:56.Z", + "2026-01-01T12:34:56.123junkZ", + "2026-01-01T12:34:56.1.2Z", + "2026-01-01T12:34:56.12 3Z", + "2026-01-01T12:34:56:99Z", + "2026-01-01T12::34Z", + ] { + assert!(DateTime::parse_iso(value).is_none(), "accepted malformed timestamp {value:?}"); + } + } + + #[test] + fn iso_validates_clock_while_preserving_supported_forms() { + assert_eq!(DateTime::parse_iso("2019").unwrap().to_iso(), "2019-01-01T00:00:00"); + assert_eq!(DateTime::parse_iso("2019-07").unwrap().to_iso(), "2019-07-01T00:00:00"); + assert_eq!(DateTime::parse_iso("2019-07-04").unwrap().to_iso(), "2019-07-04T00:00:00"); + assert_eq!(DateTime::parse_iso("2019-07-04T10:11").unwrap().to_iso(), "2019-07-04T10:11:00"); + assert_eq!(DateTime::parse_iso("2019-07-04T10:11:12").unwrap().to_iso(), "2019-07-04T10:11:12"); + assert_eq!(DateTime::parse_iso("2019-07-04T10:11:12Z").unwrap().offset_minutes, Some(0)); + assert_eq!(DateTime::parse_iso("2019-07-04T10:11:12-05:30").unwrap().offset_minutes, Some(-330)); + assert_eq!(DateTime::parse_iso("2019-07-04T10:11:12.123456Z").unwrap().millis, 123); + assert_eq!(DateTime::parse_iso(" 2019-07-04T10:11:12.5Z ").unwrap().millis, 500); + assert_eq!(DateTime::parse_iso("2019-07-04T10:11:60Z").unwrap().second, 60); + } + + #[test] + fn unix() { + assert_eq!(DateTime::parse_iso("1970-01-01T00:00:00Z").unwrap().unix_seconds(), 0); + assert_eq!(DateTime::parse_iso("2000-03-01T00:00:00Z").unwrap().unix_seconds(), 951868800); + assert_eq!(DateTime::parse_iso("2000-03-01T02:00:00+02:00").unwrap().unix_seconds(), 951868800); + } +} diff --git a/crates/metadata/src/exif.rs b/crates/metadata/src/exif.rs new file mode 100644 index 000000000..47c9cd196 --- /dev/null +++ b/crates/metadata/src/exif.rs @@ -0,0 +1,759 @@ +//! Exif / TIFF tags → [`Metadata`] (CIPA DC-008 Exif 2.32, TIFF 6.0, DNG 1.7 tag semantics). + +use crate::{DateTime, Flash, Gps, Metadata, Orientation}; +use craft_tiff::{Ifd, ParseOptions, Tiff, TiffError, tags as t}; + +/// Strip an `Exif\0\0` (or `Exif\0\xff`) prefix as found in JPEG APP1 and some WebP `EXIF` chunks. +pub fn strip_exif_header(b: &[u8]) -> &[u8] { + if b.len() >= 6 && &b[..4] == b"Exif" && b[4] == 0 { &b[6..] } else { b } +} + +/// Read metadata from a TIFF-structured Exif block (a whole TIFF/DNG/raw file, or the payload of a JPEG APP1 +/// Exif segment with or without the `Exif\0\0` prefix). Malformed input yields whatever could be read. +pub fn read_exif(bytes: &[u8]) -> Metadata { + try_read_exif(bytes).unwrap_or_default() +} + +/// Like [`read_exif`] but reports a structurally invalid TIFF header / IFD0. +pub fn try_read_exif(bytes: &[u8]) -> Result { + try_read_exif_with(bytes, &ParseOptions::default()) +} + +/// [`try_read_exif`] with the caller's parser options (its value allocation budget in particular). +/// A decoder working from a sparse buffer — only selected ranges of the logical file are actually +/// loaded, the rest is zeros — must not size its allocations by the logical file length, which +/// unrelated bytes of the file can inflate at no cost to the attacker. +pub fn try_read_exif_with(bytes: &[u8], opts: &ParseOptions) -> Result { + let data = strip_exif_header(bytes); + let tiff = Tiff::parse_with(data, opts)?; + let mut m = from_tiff(&tiff); + // Panasonic (and some Canons) write no LensModel, only the name in their maker note + if m.lens_model.is_none() { + m.lens_model = panasonic_maker_note_lens(data, &tiff).or_else(|| canon_maker_note_lens(data, &tiff)); + } + // the maker note's values are offsets into the stream, which `from_tiff` doesn't have + m.focus = crate::focus::read(data, &tiff, m.make.as_deref(), m.model.as_deref(), m.orientation); + Ok(m) +} + +fn text(ifd: Option<&Ifd>, tag: u16) -> Option { + let s = ifd?.string(tag)?; + let s = s.trim().to_string(); + (!s.is_empty() && !s.bytes().all(|b| b == b' ' || b == 0)).then_some(s) +} + +fn pos(v: Option) -> Option { + v.filter(|x| x.is_finite() && *x > 0.0) +} + +/// The orientation the Exif's own first IFD states. [`from_tiff`] also reports one that only the XMP packet in +/// the Exif's tag 700 states; a viewer displays a photo by the Exif's alone, so that one can't tell how a photo +/// is stored. +pub(crate) fn ifd0_orientation(bytes: &[u8]) -> Option { + let tiff = Tiff::parse_with(strip_exif_header(bytes), &ParseOptions::default()).ok()?; + tiff.ifds.first().and_then(|i| i.u16(t::ORIENTATION)).filter(|v| (1..=8).contains(v)).map(Orientation::from_exif) +} + +/// Build [`Metadata`] from a parsed TIFF stream (IFD0 + Exif + GPS; DNG tags as fallbacks). +pub fn from_tiff(tiff: &Tiff) -> Metadata { + let ifd0 = tiff.ifds.first(); + let exif = tiff.exif(); + let gps = tiff.gps(); + let either = |tag: u16| exif.and_then(|e| e.value(tag)).or_else(|| ifd0.and_then(|i| i.value(tag))); + let mut m = Metadata { + make: text(ifd0, t::MAKE), + model: text(ifd0, t::MODEL).or_else(|| text(ifd0, t::UNIQUE_CAMERA_MODEL)), + software: text(ifd0, t::SOFTWARE), + artist: text(ifd0, t::ARTIST), + copyright: text(ifd0, t::COPYRIGHT), + caption: text(ifd0, t::IMAGE_DESCRIPTION), + serial_number: text(exif, t::BODY_SERIAL_NUMBER).or_else(|| text(ifd0, t::CAMERA_SERIAL_NUMBER)), + lens_make: text(exif, t::LENS_MAKE), + lens_model: text(exif, t::LENS_MODEL), + lens_serial_number: text(exif, t::LENS_SERIAL_NUMBER), + ..Default::default() + }; + // Exif requires LensSpecification/LensInfo as 4 rationals. + let spec = exif.and_then(|e| e.f64s(t::LENS_SPECIFICATION)).or_else(|| ifd0.and_then(|i| i.f64s(t::LENS_INFO))); + if let Some(s) = spec.filter(|s| s.len() == 4 && s[0] > 0.0) { + m.lens_spec = Some([s[0], s[1], s[2], s[3]]); + } + // Nikon DSLRs write neither LensModel nor LensSpecification, only the same four values in their maker note + if m.lens_spec.is_none() { + m.lens_spec = nikon_lens_spec(exif, m.make.as_deref(), tiff.order); + } + m.orientation = ifd0.and_then(|i| i.u16(t::ORIENTATION)).filter(|v| (1..=8).contains(v)).map(Orientation::from_exif); + m.rating = ifd0.and_then(|i| i.i64(t::RATING)).map(|r| r.clamp(-1, 5) as i8); + + m.exposure_time = pos(either(t::EXPOSURE_TIME).and_then(|v| v.get_f64(0))) + .or_else(|| exif.and_then(|e| e.f64(t::SHUTTER_SPEED_VALUE)).filter(|v| v.abs() < 64.0).map(|tv| 2f64.powf(-tv))); + m.f_number = pos(either(t::F_NUMBER).and_then(|v| v.get_f64(0))) + .or_else(|| exif.and_then(|e| e.f64(t::APERTURE_VALUE)).filter(|v| v.abs() < 64.0).map(|av| 2f64.powf(av / 2.0))); + let iso = either(t::ISO_SPEED).and_then(|v| v.get_u64(0)).filter(|&v| v > 0); + let iso_ext = exif.and_then(|e| e.u64(t::RECOMMENDED_EXPOSURE_INDEX).or_else(|| e.u64(t::ISO_SPEED_RATINGS_EXT))).filter(|&v| v > 0); + m.iso = match (iso, iso_ext) { + (Some(65535), Some(x)) | (None, Some(x)) => Some(x), + (Some(v), _) => Some(v), + _ => None, + } + .map(|v| v.min(u32::MAX as u64) as u32); + m.focal_length = pos(either(t::FOCAL_LENGTH).and_then(|v| v.get_f64(0))); + m.focal_length_35mm = pos(exif.and_then(|e| e.f64(t::FOCAL_LENGTH_35MM))); + m.exposure_bias = exif.and_then(|e| e.f64(t::EXPOSURE_BIAS)).filter(|v| v.is_finite() && v.abs() < 100.0); + m.exposure_program = exif.and_then(|e| e.u16(t::EXPOSURE_PROGRAM)); + m.metering_mode = exif.and_then(|e| e.u16(t::METERING_MODE)); + m.white_balance = exif.and_then(|e| e.u16(t::WHITE_BALANCE)); + m.flash = exif.and_then(|e| e.u16(t::FLASH)).map(|raw| Flash { fired: raw & 1 != 0, raw }); + + // capture time: DateTimeOriginal, then DateTimeDigitized, then IFD0 DateTime + let candidates = [ + (exif, t::DATE_TIME_ORIGINAL, t::OFFSET_TIME_ORIGINAL, t::SUBSEC_TIME_ORIGINAL), + (exif, t::DATE_TIME_DIGITIZED, t::OFFSET_TIME_DIGITIZED, t::SUBSEC_TIME_DIGITIZED), + (ifd0, t::DATE_TIME, t::OFFSET_TIME, t::SUBSEC_TIME), + ]; + for (ifd, dt_tag, off_tag, sub_tag) in candidates { + if let Some(mut dt) = text(ifd, dt_tag).and_then(|s| DateTime::parse_exif(&s)) { + if let Some(s) = text(exif, sub_tag) { + dt = dt.with_subsec(&s); + } + dt.offset_minutes = text(exif, off_tag).and_then(|s| DateTime::parse_offset(&s)); + m.capture_time = Some(dt); + break; + } + } + + m.width = exif.and_then(|e| e.u32(t::PIXEL_X_DIMENSION)).filter(|&v| v > 0).or_else(|| ifd0.and_then(|i| i.u32(t::IMAGE_WIDTH))); + m.height = exif.and_then(|e| e.u32(t::PIXEL_Y_DIMENSION)).filter(|&v| v > 0).or_else(|| ifd0.and_then(|i| i.u32(t::IMAGE_LENGTH))); + + m.gps = gps.and_then(read_gps); + if let Some(iptc) = ifd0.and_then(|i| i.value(t::IPTC_NAA)) { + // IPTC-NAA is often typed LONG; re-serialise to bytes in file order. + let bytes = match iptc { + craft_tiff::Value::Byte(b) | craft_tiff::Value::Undefined(b) => b.clone(), + other => craft_tiff::writer::encode(tiff.order, other), + }; + m.fill_missing(&crate::parse_iptc(&bytes)); + } + if let Some(x) = ifd0.and_then(|i| i.bytes(t::XMP)).map(String::from_utf8_lossy) + && let Ok(x) = crate::parse_xmp(&x) + { + // (the size stays the one Exif states: `extract` turns regions by it, and a size only the XMP states may be the upright one) + let size = (m.width, m.height); + m.overlay_user_fields(&x.metadata); + m.fill_missing(&x.metadata); + (m.width, m.height) = size; + } + m +} + +/// Degrees/minutes/seconds triplet as decimal degrees. A component that is present but not a number (a rational +/// with a zero denominator, as in the placeholder GPS block phones write without a fix) makes the whole value unusable. +fn dms(ifd: &Ifd, tag: u16) -> Option { + let v = ifd.value(tag)?; + let part = |i: usize| if i < v.count() { v.get_f64(i) } else { Some(0.0) }; + let d = v.get_f64(0)?; + let deg = d + part(1)? / 60.0 + part(2)? / 3600.0; + deg.is_finite().then_some(deg) +} + +/// `Some(true)` for the negative hemisphere (`neg`), `Some(false)` for `pos`, `None` when the ref is missing or invalid. +fn hemisphere(g: &Ifd, tag: u16, pos: char, neg: char) -> Option { + match g.string(tag)?.trim_start().chars().next()?.to_ascii_uppercase() { + c if c == neg => Some(true), + c if c == pos => Some(false), + _ => None, + } +} + +fn read_gps(g: &Ifd) -> Option { + let mut lat = dms(g, t::GPS_LATITUDE)?; + let mut lon = dms(g, t::GPS_LONGITUDE)?; + if hemisphere(g, t::GPS_LATITUDE_REF, 'N', 'S')? { + lat = -lat; + } + if hemisphere(g, t::GPS_LONGITUDE_REF, 'E', 'W')? { + lon = -lon; + } + if !(-90.0..=90.0).contains(&lat) || !(-180.0..=180.0).contains(&lon) { + return None; + } + let altitude = g.f64(t::GPS_ALTITUDE).filter(|a| a.is_finite()).map(|a| if g.u32(t::GPS_ALTITUDE_REF) == Some(1) { -a } else { a }); + Some(Gps { latitude: lat, longitude: lon, altitude }) +} + +/// Panasonic maker note tag `0x0051` (LensType in the ExifTool Panasonic tag names): the lens name, ASCII. +const PANASONIC_LENS_TYPE: u16 = 0x0051; +/// RW2/RWL IFD0 tag `0x002E` (JpgFromRaw): a JPEG whose APP1 holds the full Exif, maker note included. +const PANASONIC_JPG_FROM_RAW: u16 = 0x002E; + +/// The lens name in the Panasonic maker note (tag `0x0051`) of this Exif or RW2 stream (`data` starts at its +/// TIFF header): the note of its own Exif IFD, else, for a raw, the note in its JpgFromRaw's Exif. +pub(crate) fn panasonic_maker_note_lens(data: &[u8], tiff: &Tiff) -> Option { + panasonic_lens(data, tiff).or_else(|| panasonic_jpg_from_raw_lens(tiff)) +} + +/// The lens name in a `Panasonic\0` maker note of this Exif stream (`data` starts at its TIFF header). +fn panasonic_lens(data: &[u8], tiff: &Tiff) -> Option { + let exif = tiff.exif()?; + if !exif.bytes(t::MAKER_NOTE)?.starts_with(b"Panasonic\0") { + return None; + } + let e = exif.get(t::MAKER_NOTE)?; + let make = tiff.ifds.first().and_then(|i| i.string(t::MAKE)).unwrap_or_default(); + let mn = craft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?; + text(Some(&mn.ifd), PANASONIC_LENS_TYPE) +} + +/// The lens name from the Exif of a RW2's (or Leica RWL's) embedded JpgFromRaw. +fn panasonic_jpg_from_raw_lens(tiff: &Tiff) -> Option { + let jpg = tiff.ifds.first()?.bytes(PANASONIC_JPG_FROM_RAW)?; + let exif = crate::jpeg_segments(jpg).exif?; + let data = strip_exif_header(&exif); + panasonic_lens(data, &Tiff::parse(data).ok()?) +} + +/// Canon maker note tag `0x0095` (LensModel in the ExifTool Canon tag names): the lens name as ASCII. Older +/// bodies instead record a numeric LensType (CameraSettings, tag `0x0001`, index 22), which is no name to read. +const CANON_LENS_MODEL: u16 = 0x0095; + +/// The lens name in a Canon maker note of this Exif stream (`data` starts at its TIFF header): the note of +/// its own Exif IFD. Only consulted when Make is Canon, so other vendors' plain notes are never read as lenses. +fn canon_maker_note_lens(data: &[u8], tiff: &Tiff) -> Option { + let make = tiff.ifds.first().and_then(|i| i.string(t::MAKE)).unwrap_or_default(); + if !make.trim().to_ascii_uppercase().starts_with("CANON") { + return None; + } + let exif = tiff.exif()?; + let e = exif.get(t::MAKER_NOTE)?; + let mn = craft_tiff::makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, &make)?; + text(Some(&mn.ifd), CANON_LENS_MODEL) +} + +/// Nikon maker note tag `0x0084` (Lens in the ExifTool Nikon tag names): shortest and longest focal length, then the +/// widest aperture at each, as rationals, like Exif LensSpecification. Zero for a lens that doesn't report itself. +const NIKON_LENS: u16 = 0x0084; + +/// The lens a Nikon camera records in its maker note, when it's plausible. +fn nikon_lens_spec(exif: Option<&Ifd>, make: Option<&str>, order: craft_tiff::ByteOrder) -> Option<[f64; 4]> { + let make = make?; + if !make.trim().to_ascii_uppercase().starts_with("NIKON") { + return None; + } + let note = exif?.bytes(t::MAKER_NOTE)?; + let mn = craft_tiff::makernote::parse_makernote(note, 0, note.len() as u64, order, make)?; + let s = mn.ifd.f64s(NIKON_LENS)?; + let [short, long, wide, narrow] = s.as_slice() else { return None }; + let focal = |f: f64| f.is_finite() && f > 0.0 && f < 10_000.0; + let aperture = |a: f64| a.is_finite() && a > 0.0 && a < 100.0; + (focal(*short) && focal(*long) && long >= short && aperture(*wide) && aperture(*narrow)).then_some([*short, *long, *wide, *narrow]) +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; + + fn capture_time_tiff(original: Option<&str>, digitized: Option<&str>, ifd0_time: Option<&str>, xmp: Option<&str>) -> Vec { + let mut exif = IfdBuilder::new(); + if let Some(value) = original { + exif.set(t::DATE_TIME_ORIGINAL, Value::Ascii(value.into())); + } + if let Some(value) = digitized { + exif.set(t::DATE_TIME_DIGITIZED, Value::Ascii(value.into())); + } + let mut ifd0 = IfdBuilder::new(); + if let Some(value) = ifd0_time { + ifd0.set(t::DATE_TIME, Value::Ascii(value.into())); + } + ifd0.set_child(t::EXIF_IFD, exif); + if let Some(packet) = xmp { + ifd0.set(t::XMP, Value::Byte(packet.as_bytes().to_vec())); + } + TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap() + } + + #[test] + fn exif_clock_invalid_original_uses_the_next_valid_exif_time() { + let invalid = "2026:01:01 12:34:56:99"; + let digitized = capture_time_tiff(Some(invalid), Some("2026:01:02 10:00:00"), Some("2026:01:03 11:00:00"), None); + assert_eq!(read_exif(&digitized).capture_time.map(|date| date.to_iso()), Some("2026-01-02T10:00:00".into())); + + let ifd0 = capture_time_tiff(Some(invalid), Some("2026:01:02 10:00:00:"), Some("2026:01:03 11:00:00"), None); + assert_eq!(read_exif(&ifd0).capture_time.map(|date| date.to_iso()), Some("2026-01-03T11:00:00".into())); + + let none = capture_time_tiff(Some(invalid), Some("2026:01:02 10:00:00:junk"), Some("2026:01:03 11:00:00:"), None); + assert_eq!(read_exif(&none).capture_time, None); + + let original = capture_time_tiff(Some("2026:01:04 12:00:00"), Some("2026:01:02 10:00:00"), Some("2026:01:03 11:00:00"), None); + assert_eq!(read_exif(&original).capture_time.map(|date| date.to_iso()), Some("2026-01-04T12:00:00".into())); + } + + #[test] + fn exif_clock_invalid_original_allows_xmp_fallback_and_keeps_raw_tag() { + let capture = DateTime::parse_iso("2026-01-02T10:00:00Z"); + let packet = crate::write_xmp(&Metadata { capture_time: capture, ..Default::default() }); + let bytes = capture_time_tiff(Some("2026:01:01 12:34:56:99"), None, None, Some(&packet)); + assert_eq!(crate::extract(&bytes).capture_time.map(|date| date.to_iso()), Some("2026-01-02T10:00:00+00:00".into())); + + let without_fallback = capture_time_tiff(Some("2026:01:01 12:34:56:junk"), None, None, None); + assert_eq!(crate::extract(&without_fallback).capture_time, None); + assert!(crate::embedded(&without_fallback).exif.is_some(), "rejecting the projection must retain the embedded EXIF block"); + } + + /// An Exif block from a Panasonic body: no LensModel, the lens name in maker note tag 0x0051, whose value + /// lies outside the note's IFD at an offset relative to the Exif TIFF header (as Panasonic writes it). + pub(crate) fn panasonic_exif(order: ByteOrder, lens: Option<&str>, lens_model: Option<&str>) -> Vec { + let note = |at: u32| { + let mut v = b"Panasonic\0\0\0".to_vec(); + order.put_u16(&mut v, 1); + order.put_u16(&mut v, if lens.is_some() { PANASONIC_LENS_TYPE } else { 0x0001 }); + order.put_u16(&mut v, 2); + let name = format!("{:<34}\0", lens.unwrap_or("0210")); + order.put_u32(&mut v, name.len() as u32); + order.put_u32(&mut v, at + 30); + order.put_u32(&mut v, 0); + v.extend_from_slice(name.as_bytes()); + v + }; + let build = |at: u32| { + let mut exif = IfdBuilder::new(); + exif.set(t::MAKER_NOTE, Value::Undefined(note(at))); + exif.set(t::FOCAL_LENGTH, Value::Rational(vec![(25, 1)])); + if let Some(l) = lens_model { + exif.set(t::LENS_MODEL, Value::Ascii(l.into())); + } + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii("Panasonic".into())); + ifd0.set_child(t::EXIF_IFD, exif); + TiffWriter::new(order, false).write(&[ifd0]).unwrap() + }; + // the note's own offset is only known once it is laid out; its length doesn't change + let first = build(0); + let at = Tiff::parse(&first).unwrap().exif().unwrap().get(t::MAKER_NOTE).unwrap().offset as u32; + build(at) + } + + #[test] + fn panasonic_maker_note_names_the_lens() { + for order in [ByteOrder::Little, ByteOrder::Big] { + let m = read_exif(&panasonic_exif(order, Some("LUMIX G 25/F1.7"), None)); + assert_eq!(m.lens_model.as_deref(), Some("LUMIX G 25/F1.7")); + assert_eq!(m.focal_length, Some(25.0)); + } + // Exif's own LensModel wins, and a note without 0x0051 leaves the lens empty + let m = read_exif(&panasonic_exif(ByteOrder::Little, Some("LUMIX G 25/F1.7"), Some("Other 25mm"))); + assert_eq!(m.lens_model.as_deref(), Some("Other 25mm")); + assert_eq!(read_exif(&panasonic_exif(ByteOrder::Little, None, None)).lens_model, None); + } + + /// A RW2 has the full Exif only in the APP1 of its JpgFromRaw (IFD0 0x002E), not in its own Exif IFD. + pub(crate) fn panasonic_rw2(lens: &str) -> Vec { + let mut jpg = vec![0xff, 0xd8, 0xff, 0xe1]; + let mut app1 = b"Exif\0\0".to_vec(); + app1.extend(panasonic_exif(ByteOrder::Little, Some(lens), None)); + jpg.extend_from_slice(&((app1.len() + 2) as u16).to_be_bytes()); + jpg.extend(app1); + jpg.extend_from_slice(&[0xff, 0xda, 0, 2, 0xff, 0xd9]); + let mut exif = IfdBuilder::new(); + exif.set(t::FOCAL_LENGTH, Value::Rational(vec![(85, 1)])); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii("Panasonic".into())); + ifd0.set(PANASONIC_JPG_FROM_RAW, Value::Undefined(jpg)); + ifd0.set_child(t::EXIF_IFD, exif); + TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap() + } + + #[test] + fn rw2_reads_the_lens_from_its_jpg_from_raw() { + let rw2 = panasonic_rw2("LUMIX S 85/F1.8"); + let m = read_exif(&rw2); + assert_eq!(m.lens_model.as_deref(), Some("LUMIX S 85/F1.8")); + assert_eq!(m.focal_length, Some(85.0)); + assert_eq!(crate::extract(&rw2).lens_model.as_deref(), Some("LUMIX S 85/F1.8")); + } + + /// Canon maker note tags: `0x0095` holds the lens name as ASCII; `0x0001` is CameraSettings, a table of + /// int16s whose index 22 is the numeric LensType. + const CANON_LENS_MODEL_TAG: u16 = 0x0095; + const CANON_CAMERA_SETTINGS: u16 = 0x0001; + + /// An Exif block from a Canon EOS 5D: `Canon` / `Canon EOS 5D` in IFD0, no LensModel tag, and a plain maker + /// note whose IFD (value offsets relative to the enclosing TIFF header, as Canon writes it) records + /// CameraSettings with the numeric LensType at index 22 and, when `lens` is given, the name at `0x0095`. + fn canon_exif(order: ByteOrder, lens: Option<&str>) -> Vec { + let note = |at: u32| { + // The plain IFD: entry count, one or two 12-byte entries and the next-IFD pointer, then the + // out-of-line values (CameraSettings, then the lens name when present). + // pointer, then the out-of-line values (CameraSettings, then the lens name when present). + let settings_at = at + 2 + 12 * (1 + lens.is_some() as u32) + 4; + let name_at = settings_at + 60; + let mut v = Vec::new(); + let entry = |v: &mut Vec, tag: u16, kind: u16, count: u32, off: u32| { + order.put_u16(v, tag); + order.put_u16(v, kind); + order.put_u32(v, count); + order.put_u32(v, off); + }; + order.put_u16(&mut v, 1 + lens.is_some() as u16); + entry(&mut v, CANON_CAMERA_SETTINGS, 3, 30, settings_at); + if let Some(name) = lens { + entry(&mut v, CANON_LENS_MODEL_TAG, 2, name.len() as u32, name_at); + } + order.put_u32(&mut v, 0); + for i in 0..30u16 { + order.put_u16(&mut v, if i == 22 { 178 } else { 0 }); // 22: numeric LensType, no name + } + if let Some(name) = lens { + v.extend_from_slice(name.as_bytes()); + } + v + }; + let build = |at: u32| { + let mut exif = IfdBuilder::new(); + exif.set(t::MAKER_NOTE, Value::Undefined(note(at))); + exif.set(t::FOCAL_LENGTH, Value::Rational(vec![(50, 1)])); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii("Canon".into())); + ifd0.set(t::MODEL, Value::Ascii("Canon EOS 5D".into())); + ifd0.set_child(t::EXIF_IFD, exif); + TiffWriter::new(order, false).write(&[ifd0]).unwrap() + }; + // the note's own offset is only known once it is laid out; its length doesn't change + let first = build(0); + let at = Tiff::parse(&first).unwrap().exif().unwrap().get(t::MAKER_NOTE).unwrap().offset as u32; + build(at) + } + + #[test] + fn canon_maker_note_lens_fills_lens_model() { + for order in [ByteOrder::Little, ByteOrder::Big] { + let m = read_exif(&canon_exif(order, Some("Canon EF 50mm f/1.8 II"))); + assert_eq!(m.lens_model.as_deref(), Some("Canon EF 50mm f/1.8 II")); + // a note that only records the numeric LensType has no name to fill in + assert_eq!(read_exif(&canon_exif(order, None)).lens_model, None); + } + } + + /// An Exif block from a Nikon body whose maker note records the lens (tag 0x0084) and no LensModel or + /// LensSpecification, as the D700, D750, D80 and D5300 write it. + fn nikon_exif(make: &str, lens: Option<[(u32, u32); 4]>, spec: Option<[(u32, u32); 4]>) -> Vec { + let mut note = IfdBuilder::new(); + note.set(0x0001, Value::Undefined(b"0210".to_vec())); + if let Some(l) = lens { + note.set(NIKON_LENS, Value::Rational(l.to_vec())); + } + let mut blob = b"Nikon\0\x02\x10\0\0".to_vec(); + blob.extend(TiffWriter::new(ByteOrder::Little, false).write(&[note]).unwrap()); + let mut exif = IfdBuilder::new(); + exif.set(t::MAKER_NOTE, Value::Undefined(blob)); + if let Some(sp) = spec { + exif.set(t::LENS_SPECIFICATION, Value::Rational(sp.to_vec())); + } + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii(make.into())); + ifd0.set_child(t::EXIF_IFD, exif); + TiffWriter::new(ByteOrder::Big, false).write(&[ifd0]).unwrap() + } + + #[test] + fn nikon_maker_note_lens_fills_the_lens_spec() { + let zoom = [(18, 1), (135, 1), (35, 10), (56, 10)]; + assert_eq!(read_exif(&nikon_exif("NIKON CORPORATION", Some(zoom), None)).lens_spec, Some([18.0, 135.0, 3.5, 5.6])); + // Exif's own LensSpecification wins, another make's note is not read, and a lens that reports nothing is left out + let prime = [(50, 1), (50, 1), (18, 10), (18, 10)]; + assert_eq!(read_exif(&nikon_exif("NIKON CORPORATION", Some(zoom), Some(prime))).lens_spec, Some([50.0, 50.0, 1.8, 1.8])); + assert_eq!(read_exif(&nikon_exif("Canon", Some(zoom), None)).lens_spec, None); + assert_eq!(read_exif(&nikon_exif("NIKON CORPORATION", Some([(0, 1), (0, 1), (0, 1), (0, 1)]), None)).lens_spec, None); + assert_eq!(read_exif(&nikon_exif("NIKON CORPORATION", Some([(200, 1), (80, 1), (28, 10), (28, 10)]), None)).lens_spec, None); + assert_eq!(read_exif(&nikon_exif("NIKON CORPORATION", None, None)).lens_spec, None); + } + + pub(crate) fn sample_exif(order: ByteOrder) -> Vec { + let mut exif = IfdBuilder::new(); + exif.set(t::EXPOSURE_TIME, Value::Rational(vec![(1, 250)])); + exif.set(t::F_NUMBER, Value::Rational(vec![(28, 10)])); + exif.set(t::ISO_SPEED, Value::Short(vec![400])); + exif.set(t::DATE_TIME_ORIGINAL, Value::Ascii("2023:07:14 18:30:05".into())); + exif.set(t::OFFSET_TIME_ORIGINAL, Value::Ascii("+02:00".into())); + exif.set(t::SUBSEC_TIME_ORIGINAL, Value::Ascii("42".into())); + exif.set(t::EXPOSURE_BIAS, Value::SRational(vec![(-2, 3)])); + exif.set(t::METERING_MODE, Value::Short(vec![5])); + exif.set(t::FLASH, Value::Short(vec![0x19])); + exif.set(t::FOCAL_LENGTH, Value::Rational(vec![(50, 1)])); + exif.set(t::FOCAL_LENGTH_35MM, Value::Short(vec![75])); + exif.set(t::WHITE_BALANCE, Value::Short(vec![0])); + exif.set(t::EXPOSURE_PROGRAM, Value::Short(vec![3])); + exif.set(t::LENS_MAKE, Value::Ascii("LensCo".into())); + exif.set(t::LENS_MODEL, Value::Ascii("50mm F1.8".into())); + exif.set(t::BODY_SERIAL_NUMBER, Value::Ascii("12345".into())); + exif.set(t::LENS_SPECIFICATION, Value::Rational(vec![(50, 1), (50, 1), (18, 10), (18, 10)])); + exif.set(t::PIXEL_X_DIMENSION, Value::Long(vec![6000])); + exif.set(t::PIXEL_Y_DIMENSION, Value::Long(vec![4000])); + let mut gps = IfdBuilder::new(); + gps.set(t::GPS_LATITUDE_REF, Value::Ascii("S".into())); + gps.set(t::GPS_LATITUDE, Value::Rational(vec![(33, 1), (51, 1), (3564, 100)])); + gps.set(t::GPS_LONGITUDE_REF, Value::Ascii("E".into())); + gps.set(t::GPS_LONGITUDE, Value::Rational(vec![(151, 1), (12, 1), (4032, 100)])); + gps.set(t::GPS_ALTITUDE_REF, Value::Byte(vec![1])); + gps.set(t::GPS_ALTITUDE, Value::Rational(vec![(125, 10)])); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii("Maker Inc.".into())); + ifd0.set(t::MODEL, Value::Ascii("Model X ".into())); + ifd0.set(t::ORIENTATION, Value::Short(vec![6])); + ifd0.set(t::ARTIST, Value::Ascii("Jane".into())); + ifd0.set(t::COPYRIGHT, Value::Ascii("(c) Jane".into())); + ifd0.set(t::RATING, Value::Short(vec![3])); + ifd0.set_child(t::EXIF_IFD, exif); + ifd0.set_child(t::GPS_IFD, gps); + TiffWriter::new(order, false).write(&[ifd0]).unwrap() + } + + #[test] + fn reads_all_fields() { + for order in [ByteOrder::Little, ByteOrder::Big] { + let m = read_exif(&sample_exif(order)); + assert_eq!(m.make.as_deref(), Some("Maker Inc.")); + assert_eq!(m.model.as_deref(), Some("Model X")); + assert_eq!(m.exposure_time, Some(0.004)); + assert_eq!(m.f_number, Some(2.8)); + assert_eq!(m.iso, Some(400)); + let dt = m.capture_time.unwrap(); + assert_eq!(dt.to_iso(), "2023-07-14T18:30:05.420+02:00"); + assert!((m.exposure_bias.unwrap() + 0.6667).abs() < 1e-3); + assert_eq!(m.metering_mode, Some(5)); + assert_eq!(m.flash, Some(Flash { fired: true, raw: 0x19 })); + assert_eq!(m.focal_length, Some(50.0)); + assert_eq!(m.focal_length_35mm, Some(75.0)); + assert_eq!(m.orientation, Some(Orientation::Rotate90)); + assert_eq!(m.lens_model.as_deref(), Some("50mm F1.8")); + assert_eq!(m.lens_make.as_deref(), Some("LensCo")); + assert_eq!(m.serial_number.as_deref(), Some("12345")); + assert_eq!(m.lens_spec, Some([50.0, 50.0, 1.8, 1.8])); + assert_eq!((m.width, m.height), (Some(6000), Some(4000))); + assert_eq!(m.artist.as_deref(), Some("Jane")); + assert_eq!(m.rating, Some(3)); + let g = m.gps.unwrap(); + assert!((g.latitude + 33.8599).abs() < 1e-4, "{g:?}"); + assert!((g.longitude - 151.2112).abs() < 1e-4); + assert_eq!(g.altitude, Some(-12.5)); + } + } + + #[test] + fn placeholder_gps_block_is_no_location() { + // Android phones without a fix write NUL refs and 0/0 rationals. + let read = |lat_ref: &str, lat: Vec<(u32, u32)>| { + let mut gps = IfdBuilder::new(); + gps.set(t::GPS_LATITUDE_REF, Value::Ascii(lat_ref.into())); + gps.set(t::GPS_LATITUDE, Value::Rational(lat)); + gps.set(t::GPS_LONGITUDE_REF, Value::Ascii("E".into())); + gps.set(t::GPS_LONGITUDE, Value::Rational(vec![(151, 1), (12, 1), (4032, 100)])); + let mut ifd0 = IfdBuilder::new(); + ifd0.set_child(t::GPS_IFD, gps); + read_exif(&TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap()).gps + }; + let valid = vec![(33, 1), (51, 1), (3564, 100)]; + assert!(read("N", valid.clone()).is_some()); + assert_eq!(read("N", vec![(0, 0), (0, 0), (0, 0)]), None, "0/0 rationals"); + assert_eq!(read("N", vec![(33, 1), (0, 0), (0, 0)]), None, "0/0 minutes"); + assert_eq!(read("", valid.clone()), None, "NUL ref"); + assert_eq!(read("", valid.clone()), None, "empty ref"); + assert_eq!(read("X", valid), None, "invalid ref"); + } + + #[test] + fn exif_prefix_and_garbage() { + let mut b = b"Exif\0\0".to_vec(); + b.extend(sample_exif(ByteOrder::Little)); + assert_eq!(read_exif(&b).iso, Some(400)); + assert_eq!(read_exif(b"garbage"), Metadata::default()); + assert!(try_read_exif(b"garbage").is_err()); + } + + #[test] + fn apex_fallbacks_and_extended_iso() { + let mut exif = IfdBuilder::new(); + exif.set(t::SHUTTER_SPEED_VALUE, Value::SRational(vec![(8, 1)])); + exif.set(t::APERTURE_VALUE, Value::Rational(vec![(4, 1)])); + exif.set(t::ISO_SPEED, Value::Short(vec![65535])); + exif.set(t::RECOMMENDED_EXPOSURE_INDEX, Value::Long(vec![102400])); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::DATE_TIME, Value::Ascii("2020:01:02 03:04:05".into())); + ifd0.set_child(t::EXIF_IFD, exif); + let m = read_exif(&TiffWriter::default().write(&[ifd0]).unwrap()); + assert_eq!(m.exposure_time, Some(1.0 / 256.0)); + assert_eq!(m.f_number, Some(4.0)); + assert_eq!(m.iso, Some(102400)); + assert_eq!(m.capture_time.unwrap().to_exif(), "2020:01:02 03:04:05"); + } + + #[test] + fn xmp_in_ifd0_overrides_user_fields() { + let mut meta = Metadata { rating: Some(5), title: Some("From XMP".into()), ..Default::default() }; + meta.keywords = vec!["a".into()]; + let x = crate::write_xmp(&meta); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::RATING, Value::Short(vec![1])); + ifd0.set(t::XMP, Value::Byte(x.into_bytes())); + let m = read_exif(&TiffWriter::default().write(&[ifd0]).unwrap()); + assert_eq!(m.rating, Some(5)); + assert_eq!(m.title.as_deref(), Some("From XMP")); + } +} + +/// An exact `1/N` rational for a sub-second value whose reciprocal is a whole number (shutter speeds such as +/// 1/12800 s, which [`craft_tiff::writer::rational`]'s decimal denominators cannot represent). +fn reciprocal(v: Option) -> Option { + let v = v.filter(|v| v.is_finite() && *v > 0.0 && *v < 1.0)?; + let inv = 1.0 / v; + ((inv - inv.round()).abs() < 1e-6 && inv.round() <= u32::MAX as f64).then(|| craft_tiff::Value::Rational(vec![(1, inv.round() as u32)])) +} + +/// Serialise the interchange subset of `meta` as a TIFF-structured Exif block (little-endian, no +/// `Exif\0\0` prefix): IFD0 (make, model, software, artist, copyright, description, orientation, +/// date), the Exif IFD (capture settings, lens) and, when present, the GPS IFD. +pub fn write_exif(meta: &Metadata) -> Vec { + use craft_tiff::writer::{rational, srational}; + use craft_tiff::{ByteOrder, IfdBuilder, TiffWriter, Value}; + let ascii = |s: &Option| s.as_ref().filter(|v| !v.is_empty()).map(|v| Value::Ascii(v.clone())); + let mut ifd0 = IfdBuilder::new(); + for (tag, v) in [ + (t::MAKE, ascii(&meta.make)), + (t::MODEL, ascii(&meta.model)), + (t::SOFTWARE, ascii(&meta.software)), + (t::ARTIST, ascii(&meta.artist)), + (t::COPYRIGHT, ascii(&meta.copyright)), + (t::IMAGE_DESCRIPTION, ascii(&meta.caption)), + (t::DATE_TIME, meta.capture_time.map(|d| Value::Ascii(d.to_exif()))), + (t::ORIENTATION, meta.orientation.map(|o| Value::Short(vec![o.to_exif()]))), + ] { + if let Some(v) = v { + ifd0.set(tag, v); + } + } + let mut ex = IfdBuilder::new(); + ex.set(t::EXIF_VERSION, Value::Undefined(b"0232".to_vec())); + let r = |v: Option| v.filter(|v| v.is_finite() && *v > 0.0).map(|v| Value::Rational(vec![rational(v)])); + for (tag, v) in [ + (t::DATE_TIME_ORIGINAL, meta.capture_time.map(|d| Value::Ascii(d.to_exif()))), + (t::EXPOSURE_TIME, reciprocal(meta.exposure_time).or_else(|| r(meta.exposure_time))), + (t::F_NUMBER, r(meta.f_number)), + (t::FOCAL_LENGTH, r(meta.focal_length)), + (t::ISO_SPEED, meta.iso.map(|i| Value::Short(vec![i.min(65_535) as u16]))), + (t::ISO_SPEED_RATINGS_EXT, meta.iso.filter(|&i| i > 65_534).map(|i| Value::Long(vec![i]))), + (t::LENS_MAKE, ascii(&meta.lens_make)), + (t::LENS_MODEL, ascii(&meta.lens_model)), + (t::EXPOSURE_BIAS, meta.exposure_bias.map(|v| Value::SRational(vec![srational(v)]))), + ] { + if let Some(v) = v { + ex.set(tag, v); + } + } + if let Some(d) = meta.capture_time { + // an offset past 18 h is not a real one (a deserialized value can be anything): write none + let offset = d.offset_minutes.filter(|o| o.unsigned_abs() <= 18 * 60).map(|o| { + let sign = if o < 0 { '-' } else { '+' }; + format!("{sign}{:02}:{:02}", o.unsigned_abs() / 60, o.unsigned_abs() % 60) + }); + let subsec = (d.millis != 0).then(|| format!("{:03}", d.millis)); + for (tag, v) in [(t::OFFSET_TIME, &offset), (t::OFFSET_TIME_ORIGINAL, &offset), (t::SUBSEC_TIME, &subsec), (t::SUBSEC_TIME_ORIGINAL, &subsec)] + { + if let Some(v) = v { + ex.set(tag, Value::Ascii(v.clone())); + } + } + } + ifd0.set_child(t::EXIF_IFD, ex); + if let Some(g) = meta.gps { + let dms = |v: f64| { + let v = v.abs(); + let d = v.floor(); + let m = ((v - d) * 60.0).floor(); + let s = (v - d - m / 60.0) * 3600.0; + Value::Rational(vec![(d as u32, 1), (m as u32, 1), ((s * 1000.0).round() as u32, 1000)]) + }; + let mut gps = IfdBuilder::new(); + gps.set(t::GPS_VERSION_ID, Value::Byte(vec![2, 3, 0, 0])); + gps.set(t::GPS_LATITUDE_REF, Value::Ascii(if g.latitude < 0.0 { "S" } else { "N" }.into())); + gps.set(t::GPS_LATITUDE, dms(g.latitude)); + gps.set(t::GPS_LONGITUDE_REF, Value::Ascii(if g.longitude < 0.0 { "W" } else { "E" }.into())); + gps.set(t::GPS_LONGITUDE, dms(g.longitude)); + if let Some(a) = g.altitude { + gps.set(t::GPS_ALTITUDE_REF, Value::Byte(vec![u8::from(a < 0.0)])); + gps.set(t::GPS_ALTITUDE, Value::Rational(vec![rational(a.abs())])); + } + ifd0.set_child(t::GPS_IFD, gps); + } + TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap_or_default() +} + +#[cfg(test)] +mod write_tests { + use super::*; + + #[test] + fn capture_time_offset_and_subsec_round_trip() { + for iso in ["2026-09-20T05:40:00.250+02:00", "2026-09-20T05:40:00.005-03:30", "2026-09-20T05:40:00"] { + let m = Metadata { capture_time: DateTime::parse_iso(iso), ..Default::default() }; + assert_eq!(read_exif(&write_exif(&m)).capture_time, m.capture_time, "{iso}"); + } + } + + /// A deserialized offset can be any i16, `i16::MIN` included: writing must not overflow. + #[test] + fn extreme_capture_time_offsets_do_not_panic() { + for o in [i16::MIN, i16::MAX, -1081, 1081] { + let mut dt = DateTime::parse_iso("2026-09-20T05:40:00").unwrap(); + dt.offset_minutes = Some(o); + let m = Metadata { capture_time: Some(dt), ..Default::default() }; + assert_eq!(read_exif(&write_exif(&m)).capture_time.map(|d| d.offset_minutes), Some(None), "{o}"); + } + } + + #[test] + fn exif_round_trips() { + let m = Metadata { + make: Some("Synthetic".into()), + model: Some("X2".into()), + copyright: Some("© 2026 A. Person".into()), + artist: Some("A. Person".into()), + capture_time: DateTime::parse_iso("2026-09-20T05:40:00"), + exposure_time: Some(1.0 / 250.0), + f_number: Some(2.8), + iso: Some(400), + focal_length: Some(35.0), + lens_model: Some("35mm F2".into()), + gps: Some(Gps { latitude: 43.7904, longitude: -110.6818, altitude: Some(2000.0) }), + ..Default::default() + }; + let back = read_exif(&write_exif(&m)); + assert_eq!(back.make, m.make); + assert_eq!(back.model, m.model); + assert_eq!(back.copyright, m.copyright); + assert_eq!(back.artist, m.artist); + assert_eq!(back.capture_time.map(|d| d.to_exif()), m.capture_time.map(|d| d.to_exif())); + assert_eq!(back.iso, Some(400)); + assert!((back.f_number.unwrap() - 2.8).abs() < 1e-6); + assert!((back.exposure_time.unwrap() - 0.004).abs() < 1e-9); + assert_eq!(back.lens_model, m.lens_model); + let g = back.gps.unwrap(); + assert!((g.latitude - 43.7904).abs() < 1e-5 && (g.longitude + 110.6818).abs() < 1e-5, "{g:?}"); + } + + #[test] + fn iso_above_65534_round_trips() { + for iso in [65_534, 65_535, 102_400, 409_600] { + let back = read_exif(&write_exif(&Metadata { iso: Some(iso), ..Default::default() })); + assert_eq!(back.iso, Some(iso)); + } + } + + #[test] + fn fast_shutter_speeds_round_trip_exactly() { + for den in [8000.0, 12_800.0, 16_000.0, 32_000.0] { + let back = read_exif(&write_exif(&Metadata { exposure_time: Some(1.0 / den), ..Default::default() })); + let got = back.exposure_time.unwrap(); + assert!((1.0 / got - den).abs() < 0.5, "1/{den} s came back as 1/{}", 1.0 / got); + } + } +} diff --git a/crates/metadata/src/focus.rs b/crates/metadata/src/focus.rs new file mode 100644 index 000000000..b956e667e --- /dev/null +++ b/crates/metadata/src/focus.rs @@ -0,0 +1,284 @@ +//! Where the camera focused: the autofocus point a camera records in its maker notes, as a box on the +//! upright photo ([`FocusPoint`]). Sony only for now. +//! +//! **Sony** (the MakerNote IFD: header-less in ARWs, after a `SONY DSC` header in JPEGs). Tag identities and value names come from the public ExifTool +//! tag-name documentation (Sony table); every layout below was confirmed on real ILCE-1 (α1) and +//! ILCE-7SM3 (A7S III) ARWs, compared with ExifTool's output as a black box. No third-party code was read. +//! +//! | Tag | Layout (confirmed) | Use | +//! |---|---|---| +//! | `0x2027` FocusLocation | 4 × int16u: the width and height of the frame it is measured in, then the focus centre x, y in pixels of that frame (the sensor's landscape frame: a portrait shot still says 8640 × 5760) | the point | +//! | `0x2037` FocusFrameSize | 6 bytes: int16u width, int16u height of the frame the camera showed, then 2 bytes that are zero when there is no frame (manual focus) | the box size | +//! | `0x201b` FocusMode | int8u: 0 Manual, 2 AF-S, 3 AF-C, 4 AF-A, 6 DMF, 7 AF-D | manual = no point | +//! | `0x201c` AFAreaModeSetting | int8u: 0 Wide, 1 Center, 3 Flexible Spot, 11 Zone, 12 Expanded Flexible Spot, 13 Custom AF Area | shown | +//! | `0x2021` AFTracking | int8u: 0 Off, 1 Face tracking, 2 Lock On AF | shown | +//! | `0x9402` (enciphered block) byte 0x16, 0x17 | FocusMode (low 7 bits) and the AF area mode actually used: 0 Multi, 1 Center, 2 Spot, 3 Flexible Spot, 11 Zone, 12 Expanded Flexible Spot, 13 Custom AF Area, 14 Tracking, 15 Face Tracking, 20 Animal Eye Tracking, 21 Human Eye Tracking, 255 Manual | shown; read only when its focus mode agrees with `0x201b` | +//! +//! The `0x94xx` blocks are enciphered byte by byte (the documentation calls it "a simple substitution +//! cipher"). On the sample files each plain byte `p < 249` is stored as `p³ mod 249` and bytes 249–255 +//! as themselves (e.g. a stored `0x20` reads as AFAreaMode 20, stored `0x77` as `0x83`, FocusMode AF-C +//! with its top bit set); the map is a bijection on 0..249 (cubing is invertible mod 3 and mod 83), so +//! it is undone by a table of its inverse. +//! +//! **No real point** (nothing is stored, so nothing is drawn and Z zooms as before): +//! - a body the layout hasn't been confirmed on ([`SONY_MODELS`]), a missing or malformed tag, a zero +//! frame size, or a centre outside its frame; +//! - manual focus (`0x201b` = 0, or the block's area mode says Manual): the camera still records where +//! the AF frame was parked, not where anything was in focus (on the samples these are off-centre); +//! - the exact frame centre while the area used was Multi (Wide with nothing tracked): on the samples +//! the wide area reports the centre when it picked no particular spot. A centred Spot, Zone or +//! Flexible Spot frame is a real choice and is kept. + +pub use crate::focus_read::{HEADER_PREFIX, HeaderRead, MAX_HEADER_BYTES, read_header}; +use crate::{Orientation, Rect}; +use craft_tiff::{ByteOrder, Ifd, Tiff, makernote, tags as t}; +use serde::{Deserialize, Serialize}; + +/// Where the camera focused, on the upright photo. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct FocusPoint { + /// The focus frame, normalized to the photo's full, EXIF-oriented frame (y down, 0..1 on both + /// axes, clipped to it) — the frame face regions use, so it follows Rotate, flips and crops. + pub rect: Rect, + /// Focus mode as the camera names it (`AF-C`, `AF-S`, `DMF`…). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mode: Option, + /// The AF area chosen on the camera (`Wide`, `Zone`, `Flexible Spot`…). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub area: Option, + /// What the camera followed, when it says (`Animal Eye Tracking`, `Face tracking`, `Lock On AF`…). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub tracking: Option, +} + +impl FocusPoint { + /// The box's centre (normalized, upright). + pub fn center(&self) -> (f64, f64) { + ((self.rect.x0 + self.rect.x1) / 2.0, (self.rect.y0 + self.rect.y1) / 2.0) + } + /// The words that describe it, in order (mode, area, tracking), for "AF-C · Wide · Animal Eye Tracking". + pub fn words(&self) -> Vec<&str> { + [&self.mode, &self.area, &self.tracking].into_iter().filter_map(|s| s.as_deref()).filter(|s| !s.is_empty()).collect() + } + /// The box clipped to the frame and checked: `None` when it is not a usable box (non-finite, + /// empty, or entirely outside the photo). + pub fn sanitized(mut self) -> Option { + let r = self.rect; + if ![r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()) { + return None; + } + let (x0, x1) = (r.x0.min(r.x1).clamp(0.0, 1.0), r.x0.max(r.x1).clamp(0.0, 1.0)); + let (y0, y1) = (r.y0.min(r.y1).clamp(0.0, 1.0), r.y0.max(r.y1).clamp(0.0, 1.0)); + if x1 - x0 <= 0.0 || y1 - y0 <= 0.0 { + return None; + } + self.rect = Rect { x0, y0, x1, y1 }; + Some(self) + } +} + +/// Sony bodies whose focus tags have been checked against real files. Another body may store the same +/// tags differently (or not mean the same by them): it shows no focus point until its files are checked. +pub const SONY_MODELS: &[&str] = &["ILCE-1", "ILCE-7SM3"]; + +/// Whether a camera as the catalog names it (`"SONY ILCE-1"`: the Exif Make, a space, the Model) is +/// one whose files can carry a focus point, so a photo from any other camera is never opened to look. +pub fn camera_supported(camera: &str) -> bool { + let camera = camera.trim(); + camera.get(..4).is_some_and(|m| m.eq_ignore_ascii_case("SONY")) && camera.rsplit(' ').next().is_some_and(|model| SONY_MODELS.contains(&model)) +} + +/// Names what [`read`] decodes (its bodies), so a record of "looked, found nothing" made by this +/// decoder is not trusted by one that knows more bodies. +pub fn decoder_tag() -> String { + format!("sony-v1:{}", SONY_MODELS.join(",")) +} + +const FOCUS_MODE: u16 = 0x201b; +const AF_AREA_SETTING: u16 = 0x201c; +const AF_TRACKING: u16 = 0x2021; +const FOCUS_LOCATION: u16 = 0x2027; +const FOCUS_FRAME_SIZE: u16 = 0x2037; +const TAG_9402: u16 = 0x9402; + +/// Side of the box drawn when the camera gave a point but no frame size, as a fraction of the frame's +/// short side (about the size of the α1's smallest frames). +const DEFAULT_SIDE: f64 = 0.03; + +/// The focus point a file's maker note records, or `None` (see the module notes for when). +/// `data` is the TIFF stream the maker note's offsets refer to (a whole raw, or an Exif block). +pub fn read(data: &[u8], tiff: &Tiff, make: Option<&str>, model: Option<&str>, orientation: Option) -> Option { + let make = make?; + if !make.to_ascii_uppercase().starts_with("SONY") || !SONY_MODELS.contains(&model?.trim()) { + return None; + } + let e = tiff.exif()?.get(t::MAKER_NOTE)?; + let mn = makernote::parse_makernote(data, e.offset, e.count() as u64, tiff.order, make)?; + // ARWs from these bodies carry a header-less IFD; their JPEGs the `SONY DSC` header + if !matches!(mn.kind, makernote::MakerNoteKind::Sony | makernote::MakerNoteKind::Plain) { + return None; + } + sony(&mn.ifd, mn.order, orientation.unwrap_or_default()) +} + +fn byte(ifd: &Ifd, tag: u16) -> Option { + ifd.value(tag)?.get_u64(0).and_then(|v| u8::try_from(v).ok()) +} + +/// Decode the Sony tags of a parsed maker-note IFD. +pub(crate) fn sony(ifd: &Ifd, order: ByteOrder, orientation: Orientation) -> Option { + let loc = ifd.value(FOCUS_LOCATION)?; + let [w, h, x, y] = [0, 1, 2, 3].map(|i| loc.get_u64(i)); + let (w, h, x, y) = (w? as f64, h? as f64, x? as f64, y? as f64); + if w < 1.0 || h < 1.0 || x > w || y > h { + return None; + } + let mode = byte(ifd, FOCUS_MODE); + if mode == Some(0) { + return None; + } + // the enciphered block: its focus mode must agree with the plain one, else its layout is not the one we know + let block = ifd.bytes(TAG_9402).and_then(|b| { + let (m, area) = (decipher(*b.get(0x16)?) & 0x7f, decipher(*b.get(0x17)?)); + (Some(m) == mode).then_some(area) + }); + if block == Some(255) { + return None; + } + let setting = byte(ifd, AF_AREA_SETTING); + let tracking = byte(ifd, AF_TRACKING); + let wide_untracked = match block { + Some(a) => a == 0, + None => setting == Some(0) && tracking.is_none_or(|t| t == 0), + }; + if wide_untracked && x * 2.0 == w && y * 2.0 == h { + return None; + } + // frame size: two int16u in the note's byte order, then a non-zero marker when there is a frame + let size = ifd.bytes(FOCUS_FRAME_SIZE).and_then(|b| { + let (fw, fh) = (order.read_u16(b, 0)? as f64, order.read_u16(b, 2)? as f64); + let shown = b.get(4..6).is_none_or(|m| m.iter().any(|&v| v != 0)); + (shown && fw >= 1.0 && fh >= 1.0).then_some((fw, fh)) + }); + let (fw, fh) = size.unwrap_or_else(|| { + let side = w.min(h) * DEFAULT_SIDE; + (side, side) + }); + let sensor = Rect { x0: (x - fw / 2.0) / w, y0: (y - fh / 2.0) / h, x1: (x + fw / 2.0) / w, y1: (y + fh / 2.0) / h }; + let name = |v: Option, f: fn(u8) -> Option<&'static str>| v.and_then(f).map(str::to_string); + let area = name(setting, area_setting_name); + let used = name(block, area_used_name); + // what it followed: the area mode used when it says more than the setting (Animal Eye Tracking on + // Wide), else the tracking setting when it is on + let tracking = used.filter(|u| Some(u) != area.as_ref() && u != "Multi").or_else(|| name(tracking, tracking_name).filter(|t| t != "Off")); + FocusPoint { rect: orientation.map_norm_rect(sensor), mode: name(mode, focus_mode_name), area, tracking }.sanitized() +} + +/// A minimal little-endian TIFF laid out as an α1 ARW's metadata is (IFD0 with Make `SONY` and Model `ILCE-1` → +/// Exif IFD → a header-less maker note with the focus tags: AF-C or the given FocusMode byte, Wide, Face tracking, +/// Animal Eye Tracking at (3226, 1980) of 8640 × 5760), followed by `samples` bytes standing in for sensor data. For +/// tests of the readers and the backfill in other crates; not a real raw (it has no image). +#[doc(hidden)] +pub fn synthetic_arw(mode: u8, samples: usize) -> Vec { + fn entry(f: &mut Vec, tag: u16, typ: u16, count: u32, value: [u8; 4]) { + f.extend_from_slice(&tag.to_le_bytes()); + f.extend_from_slice(&typ.to_le_bytes()); + f.extend_from_slice(&count.to_le_bytes()); + f.extend_from_slice(&value); + } + let encipher = |p: u8| if p >= 249 { p } else { (u32::from(p).pow(3) % 249) as u8 }; + let mut f = b"II*\0".to_vec(); + f.extend_from_slice(&8u32.to_le_bytes()); + // IFD0 at 8: Make (at 62), Model (at 68), Orientation, the Exif IFD (at 76) + f.extend_from_slice(&4u16.to_le_bytes()); + entry(&mut f, t::MAKE, 2, 5, 62u32.to_le_bytes()); + entry(&mut f, t::MODEL, 2, 7, 68u32.to_le_bytes()); + entry(&mut f, t::ORIENTATION, 3, 1, [1, 0, 0, 0]); + entry(&mut f, t::EXIF_IFD, 4, 1, 76u32.to_le_bytes()); + f.extend_from_slice(&[0; 4]); + f.extend_from_slice(b"SONY\0\0ILCE-1\0\0"); + // the Exif IFD: the maker note at 94, 156 bytes + f.extend_from_slice(&1u16.to_le_bytes()); + entry(&mut f, t::MAKER_NOTE, 7, 156, 94u32.to_le_bytes()); + f.extend_from_slice(&[0; 4]); + // the maker note's IFD, its values from 172 + f.extend_from_slice(&6u16.to_le_bytes()); + entry(&mut f, FOCUS_MODE, 1, 1, [mode, 0, 0, 0]); + entry(&mut f, AF_AREA_SETTING, 1, 1, [0, 0, 0, 0]); + entry(&mut f, AF_TRACKING, 1, 1, [1, 0, 0, 0]); + entry(&mut f, FOCUS_LOCATION, 3, 4, 172u32.to_le_bytes()); + entry(&mut f, FOCUS_FRAME_SIZE, 7, 6, 180u32.to_le_bytes()); + entry(&mut f, TAG_9402, 7, 64, 186u32.to_le_bytes()); + f.extend_from_slice(&[0; 4]); + for v in [8640u16, 5760, 3226, 1980] { + f.extend_from_slice(&v.to_le_bytes()); + } + f.extend_from_slice(&[0xaf, 0, 0xb4, 0, 1, 1]); + let mut block = [0u8; 64]; + block[0x16] = encipher(0x80 | mode); + block[0x17] = encipher(20); + f.extend_from_slice(&block); + f.extend((0..samples).map(|i| (i % 251) as u8)); + f +} + +/// Undo the `0x94xx` byte substitution: the stored byte is `p³ mod 249` for `p < 249`. +fn decipher(b: u8) -> u8 { + if b >= 249 { + return b; + } + (0..249u32).find(|p| (p * p * p) % 249 == u32::from(b)).and_then(|p| u8::try_from(p).ok()).unwrap_or(b) +} + +fn focus_mode_name(v: u8) -> Option<&'static str> { + Some(match v { + 0 => "Manual", + 2 => "AF-S", + 3 => "AF-C", + 4 => "AF-A", + 6 => "DMF", + 7 => "AF-D", + _ => return None, + }) +} + +fn area_setting_name(v: u8) -> Option<&'static str> { + Some(match v { + 0 => "Wide", + 1 | 9 => "Center", + 3 | 4 => "Flexible Spot", + 11 => "Zone", + 12 => "Expanded Flexible Spot", + 13 => "Custom AF Area", + _ => return None, + }) +} + +fn tracking_name(v: u8) -> Option<&'static str> { + Some(match v { + 0 => "Off", + 1 => "Face tracking", + 2 => "Lock On AF", + _ => return None, + }) +} + +fn area_used_name(v: u8) -> Option<&'static str> { + Some(match v { + 0 => "Multi", + 1 => "Center", + 2 => "Spot", + 3 => "Flexible Spot", + 11 => "Zone", + 12 => "Expanded Flexible Spot", + 13 => "Custom AF Area", + 14 => "Tracking", + 15 => "Face Tracking", + 20 => "Animal Eye Tracking", + 21 => "Human Eye Tracking", + _ => return None, + }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/metadata/src/focus/tests.rs b/crates/metadata/src/focus/tests.rs new file mode 100644 index 000000000..148f5ed99 --- /dev/null +++ b/crates/metadata/src/focus/tests.rs @@ -0,0 +1,449 @@ +use super::*; +use craft_tiff::{Entry, IfdBuilder, TiffWriter, Value}; + +fn encipher(p: u8) -> u8 { + if p >= 249 { p } else { ((u32::from(p).pow(3)) % 249) as u8 } +} + +fn entry(tag: u16, value: Value) -> Entry { + Entry { tag, value, offset: 0 } +} + +/// The α1 sample `_DSC6954.ARW`: AF-C, Wide, Face tracking, Animal Eye Tracking, 8640×5760 at (3226, 1980), 175×180. +fn alpha1() -> Ifd { + let mut block = vec![0u8; 0x40]; + block[0x16] = encipher(0x83); + block[0x17] = encipher(20); + Ifd { + entries: vec![ + entry(FOCUS_MODE, Value::Byte(vec![3])), + entry(AF_AREA_SETTING, Value::Byte(vec![0])), + entry(AF_TRACKING, Value::Byte(vec![1])), + entry(FOCUS_LOCATION, Value::Short(vec![8640, 5760, 3226, 1980])), + entry(FOCUS_FRAME_SIZE, Value::Undefined(vec![0xaf, 0, 0xb4, 0, 1, 1])), + entry(TAG_9402, Value::Undefined(block)), + ], + ..Default::default() + } +} + +fn set(ifd: &mut Ifd, tag: u16, value: Value) { + ifd.entries.retain(|e| e.tag != tag); + ifd.entries.push(entry(tag, value)); + // IFD entries are kept sorted by tag (lookups rely on it) + ifd.entries.sort_by_key(|e| e.tag); +} + +#[test] +fn the_cipher_matches_the_bytes_seen_in_files() { + // stored → plain, as read from an α1 file's 0x9402 block next to ExifTool's decoded values + assert_eq!(decipher(0x20), 20); + assert_eq!(decipher(0x77), 0x83); + assert_eq!(decipher(0xff), 0xff); + for p in 0..=255u8 { + assert_eq!(decipher(encipher(p)), p); + } +} + +#[test] +fn reads_the_alpha1_sample() { + let f = sony(&alpha1(), ByteOrder::Little, Orientation::Normal).unwrap(); + assert!((f.rect.x0 - (3226.0 - 87.5) / 8640.0).abs() < 1e-12); + assert!((f.rect.y1 - (1980.0 + 90.0) / 5760.0).abs() < 1e-12); + assert_eq!(f.words(), vec!["AF-C", "Wide", "Animal Eye Tracking"]); + let (cx, cy) = f.center(); + assert!((cx - 3226.0 / 8640.0).abs() < 1e-12 && (cy - 1980.0 / 5760.0).abs() < 1e-12); +} + +#[test] +fn big_endian_frame_size() { + let mut ifd = alpha1(); + set(&mut ifd, FOCUS_FRAME_SIZE, Value::Undefined(vec![0, 0xaf, 0, 0xb4, 1, 1])); + let f = sony(&ifd, ByteOrder::Big, Orientation::Normal).unwrap(); + assert!(((f.rect.x1 - f.rect.x0) * 8640.0 - 175.0).abs() < 1e-9); +} + +#[test] +fn follows_the_exif_orientation() { + // a portrait shot (Rotate 270 CW, EXIF 8) still records landscape sensor coordinates + let mut ifd = alpha1(); + set(&mut ifd, FOCUS_LOCATION, Value::Short(vec![8640, 5760, 864, 576])); + let f = sony(&ifd, ByteOrder::Little, Orientation::from_exif(8)).unwrap(); + let (cx, cy) = f.center(); + // left of the sensor (x 0.1) is the bottom of the upright photo; its top (y 0.1) the left + assert!((cx - 0.1).abs() < 1e-9 && (cy - 0.9).abs() < 1e-9, "{cx} {cy}"); + let f = sony(&ifd, ByteOrder::Little, Orientation::from_exif(6)).unwrap(); + let (cx, cy) = f.center(); + assert!((cx - 0.9).abs() < 1e-9 && (cy - 0.1).abs() < 1e-9, "{cx} {cy}"); +} + +#[test] +fn no_real_point() { + // manual focus + let mut ifd = alpha1(); + set(&mut ifd, FOCUS_MODE, Value::Byte(vec![0])); + assert!(sony(&ifd, ByteOrder::Little, Orientation::Normal).is_none()); + // the block says Manual (its focus mode agreeing) + let mut ifd = alpha1(); + let mut block = vec![0u8; 0x18]; + block[0x16] = encipher(3); + block[0x17] = encipher(255); + set(&mut ifd, TAG_9402, Value::Undefined(block)); + assert!(sony(&ifd, ByteOrder::Little, Orientation::Normal).is_none()); + // Multi at the exact centre + let mut ifd = alpha1(); + let mut block = vec![0u8; 0x18]; + block[0x16] = encipher(3); + block[0x17] = encipher(0); + set(&mut ifd, TAG_9402, Value::Undefined(block)); + set(&mut ifd, FOCUS_LOCATION, Value::Short(vec![8640, 5760, 4320, 2880])); + assert!(sony(&ifd, ByteOrder::Little, Orientation::Normal).is_none()); + // …and without the block: Wide, tracking off + ifd.entries.retain(|e| e.tag != TAG_9402); + set(&mut ifd, AF_TRACKING, Value::Byte(vec![0])); + assert!(sony(&ifd, ByteOrder::Little, Orientation::Normal).is_none()); + // missing location, zero frame, centre outside it + let mut ifd = alpha1(); + ifd.entries.retain(|e| e.tag != FOCUS_LOCATION); + assert!(sony(&ifd, ByteOrder::Little, Orientation::Normal).is_none()); + for loc in [vec![0, 5760, 0, 0], vec![8640, 0, 1, 0], vec![8640, 5760, 9000, 10], vec![8640, 5760, 10, 6000], vec![8640, 5760, 10]] { + let mut ifd = alpha1(); + set(&mut ifd, FOCUS_LOCATION, Value::Short(loc.clone())); + assert!(sony(&ifd, ByteOrder::Little, Orientation::Normal).is_none(), "{loc:?}"); + } +} + +#[test] +fn a_centred_spot_is_a_real_point() { + let mut ifd = alpha1(); + set(&mut ifd, AF_AREA_SETTING, Value::Byte(vec![3])); + set(&mut ifd, AF_TRACKING, Value::Byte(vec![0])); + let mut block = vec![0u8; 0x18]; + block[0x16] = encipher(2); + block[0x17] = encipher(3); + set(&mut ifd, TAG_9402, Value::Undefined(block)); + set(&mut ifd, FOCUS_MODE, Value::Byte(vec![2])); + set(&mut ifd, FOCUS_LOCATION, Value::Short(vec![8640, 5760, 4320, 2880])); + let f = sony(&ifd, ByteOrder::Little, Orientation::Normal).unwrap(); + assert_eq!(f.words(), vec!["AF-S", "Flexible Spot"]); + let (cx, cy) = f.center(); + assert!((cx - 0.5).abs() < 1e-12 && (cy - 0.5).abs() < 1e-12); +} + +#[test] +fn a_block_that_disagrees_is_ignored() { + // another body's block layout: its "focus mode" is not the plain tag's, so its area isn't trusted + let mut ifd = alpha1(); + let mut block = vec![0u8; 0x18]; + block[0x16] = encipher(7); + block[0x17] = encipher(255); + set(&mut ifd, TAG_9402, Value::Undefined(block)); + let f = sony(&ifd, ByteOrder::Little, Orientation::Normal).unwrap(); + assert_eq!(f.words(), vec!["AF-C", "Wide", "Face tracking"]); + // a block too short to hold the bytes is ignored the same way + set(&mut ifd, TAG_9402, Value::Undefined(vec![1, 2, 3])); + assert!(sony(&ifd, ByteOrder::Little, Orientation::Normal).is_some()); +} + +#[test] +fn missing_or_garbage_frame_size_gets_a_small_box_and_edges_are_clipped() { + for fs in + [Value::Undefined(vec![0, 0, 0, 0, 0, 0]), Value::Undefined(vec![1]), Value::Short(vec![5]), Value::Undefined(vec![0xaf, 0, 0xb4, 0, 0, 0])] + { + let mut ifd = alpha1(); + set(&mut ifd, FOCUS_FRAME_SIZE, fs); + let f = sony(&ifd, ByteOrder::Little, Orientation::Normal).unwrap(); + assert!(((f.rect.y1 - f.rect.y0) - DEFAULT_SIDE).abs() < 1e-9); + } + // a frame reaching past the corner is clipped to the photo + let mut ifd = alpha1(); + set(&mut ifd, FOCUS_LOCATION, Value::Short(vec![8640, 5760, 0, 5760])); + set(&mut ifd, FOCUS_FRAME_SIZE, Value::Undefined(vec![0xff, 0xff, 0xff, 0xff, 1, 1])); + let f = sony(&ifd, ByteOrder::Little, Orientation::Normal).unwrap(); + assert_eq!(f.rect, Rect { x0: 0.0, y0: 0.0, x1: 1.0, y1: 1.0 }); + let f = sony(&ifd, ByteOrder::Little, Orientation::Normal).unwrap(); + assert!(f.rect.x0 >= 0.0 && f.rect.y1 <= 1.0); +} + +#[test] +fn wrong_types_and_extreme_values_never_panic() { + let values = [ + Value::Ascii("x".into()), + Value::Byte(vec![]), + Value::SShort(vec![-1, -1, -1, -1]), + Value::Long(vec![u32::MAX; 4]), + Value::Double(vec![f64::NAN; 4]), + Value::Undefined(vec![0xff; 300]), + ]; + for v in &values { + for tag in [FOCUS_MODE, AF_AREA_SETTING, AF_TRACKING, FOCUS_LOCATION, FOCUS_FRAME_SIZE, TAG_9402] { + let mut ifd = alpha1(); + set(&mut ifd, tag, v.clone()); + if let Some(f) = sony(&ifd, ByteOrder::Little, Orientation::Normal) { + assert!(f.rect.x0 >= 0.0 && f.rect.x1 <= 1.0 && f.rect.x0 < f.rect.x1); + } + } + } + assert!(FocusPoint { rect: Rect { x0: f64::NAN, y0: 0.0, x1: 1.0, y1: 1.0 }, mode: None, area: None, tracking: None }.sanitized().is_none()); + assert!(FocusPoint { rect: Rect { x0: 2.0, y0: 2.0, x1: 3.0, y1: 3.0 }, mode: None, area: None, tracking: None }.sanitized().is_none()); +} + +/// A whole TIFF with a `SONY DSC` maker note holding `ifd`'s entries, written for real. +fn sony_file(make: &str, model: &str, orientation: u16, note_entries: &[(u16, Value)]) -> Vec { + sony_file_with(make, model, orientation, note_entries, None) +} + +/// [`sony_file`], with the `0x9402` block's value placed `far` bytes past the end of the TIFF +/// (the gap filled with "sample data") when `far` is given. +fn sony_file_with(make: &str, model: &str, orientation: u16, note_entries: &[(u16, Value)], far: Option) -> Vec { + let order = ByteOrder::Little; + // the note: 12-byte header, then an IFD whose out-of-line values are offset from the TIFF header, + // which we only know after writing; write once to learn where the note lands, then rebuild + let build = |note_at: u64, end: u64| -> (Vec, Vec) { + let mut note = b"SONY DSC \0\0\0".to_vec(); + let ifd_len = 2 + note_entries.len() * 12 + 4; + let mut tail: Vec = Vec::new(); + let mut outside: Vec = Vec::new(); + order.put_u16(&mut note, note_entries.len() as u16); + for (tag, v) in note_entries { + let raw = craft_tiff::writer::encode(order, v); + order.put_u16(&mut note, *tag); + order.put_u16(&mut note, v.field_type() as u16); + order.put_u32(&mut note, v.count() as u32); + if raw.len() <= 4 { + let mut r = raw.clone(); + r.resize(4, 0); + note.extend(r); + } else if let (Some(gap), TAG_9402) = (far, *tag) { + order.put_u32(&mut note, (end + gap as u64) as u32); + outside = raw; + } else { + order.put_u32(&mut note, (note_at + 12 + ifd_len as u64 + tail.len() as u64) as u32); + tail.extend(raw); + if tail.len() % 2 == 1 { + tail.push(0); + } + } + } + order.put_u32(&mut note, 0); + note.extend(tail); + let mut exif = IfdBuilder::new(); + exif.set(t::MAKER_NOTE, Value::Undefined(note)); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii(make.into())); + ifd0.set(t::MODEL, Value::Ascii(model.into())); + ifd0.set(t::ORIENTATION, Value::Short(vec![orientation])); + ifd0.set_child(t::EXIF_IFD, exif); + (TiffWriter { order, bigtiff: false }.write(&[ifd0]).unwrap(), outside) + }; + let (first, _) = build(0, 0); + let at = Tiff::parse(&first).unwrap().exif().unwrap().get(t::MAKER_NOTE).unwrap().offset; + let (mut file, outside) = build(at, first.len() as u64); + assert_eq!(file.len(), first.len()); + assert_eq!(Tiff::parse(&file).unwrap().exif().unwrap().get(t::MAKER_NOTE).unwrap().offset, at); + if let Some(gap) = far { + file.extend((0..gap).map(|i| (i * 13 % 241) as u8)); + file.extend(outside); + } + file +} + +fn alpha1_entries() -> Vec<(u16, Value)> { + alpha1().entries.into_iter().map(|e| (e.tag, e.value)).collect() +} + +#[test] +fn read_exif_finds_it_through_the_maker_note_and_orientation() { + let file = sony_file("SONY", "ILCE-1", 8, &alpha1_entries()); + let m = crate::read_exif(&file); + let f = m.focus.expect("focus point"); + assert_eq!(f.words(), vec!["AF-C", "Wide", "Animal Eye Tracking"]); + // EXIF 8: sensor x becomes 1 - upright y + let (cx, cy) = f.center(); + assert!((cy - (1.0 - 3226.0 / 8640.0)).abs() < 1e-9 && (cx - 1980.0 / 5760.0).abs() < 1e-9, "{cx} {cy}"); + // an unconfirmed body or another maker: nothing + assert!(crate::read_exif(&sony_file("SONY", "ILCE-7M4", 1, &alpha1_entries())).focus.is_none()); + assert!(crate::read_exif(&sony_file("Canon", "ILCE-1", 1, &alpha1_entries())).focus.is_none()); +} + +#[test] +fn truncated_and_garbage_maker_notes_never_panic() { + let file = sony_file("SONY", "ILCE-1", 1, &alpha1_entries()); + let note_at = Tiff::parse(&file).unwrap().exif().unwrap().get(t::MAKER_NOTE).unwrap().offset as usize; + for cut in (0..file.len()).step_by(7) { + let _ = crate::read_exif(&file[..cut]); + } + let mut seed = 0x2545_f491_u32; + for _ in 0..300 { + let mut f = file.clone(); + for _ in 0..8 { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + let i = note_at + (seed as usize % (file.len() - note_at)); + f[i] = (seed >> 8) as u8; + } + if let Some(fp) = crate::read_exif(&f).focus { + assert!(fp.rect.x0 >= 0.0 && fp.rect.x1 <= 1.0 && fp.rect.y0 >= 0.0 && fp.rect.y1 <= 1.0); + } + } +} + +/// A whole-file read and a header-only read of the same file agree, and the header read reads a +/// small part of it (the sample data after the metadata is never touched). +#[test] +fn header_read_matches_the_whole_file_and_reads_little() { + let read = |file: &[u8]| { + let mut asked = 0u64; + let r = read_header(file.len() as u64, &mut |off, buf: &mut [u8]| { + asked += buf.len() as u64; + let s = off as usize; + buf.copy_from_slice(&file[s..s + buf.len()]); + Ok(()) + }) + .unwrap(); + assert_eq!(r.bytes_read, asked); + r + }; + // a raw: the metadata, then 8 MB of "samples" + let mut file = sony_file("SONY", "ILCE-1", 8, &alpha1_entries()); + let meta_len = file.len(); + file.extend((0..8_000_000u32).map(|i| (i * 31 % 251) as u8)); + let whole = crate::extract(&file).focus; + assert!(whole.is_some()); + let r = read(&file); + assert_eq!(r.focus, whole); + assert!(r.bytes_read <= HEADER_PREFIX && r.bytes_read < file.len() as u64 / 100, "{} of {}", r.bytes_read, file.len()); + assert!(meta_len < HEADER_PREFIX as usize); + // the area-mode block placed 3 MB further on: found in a further round, still a small part + let mut far = sony_file_with("SONY", "ILCE-1", 1, &alpha1_entries(), Some(3_000_000)); + far.extend((0..4_000_000u32).map(|i| (i * 7 % 253) as u8)); + let whole = crate::extract(&far).focus; + assert!(whole.is_some()); + let r = read(&far); + assert_eq!(r.focus, whole); + assert!(r.bytes_read < 64 * 1024, "{}", r.bytes_read); + // a JPEG: the Exif segment after another segment + let exif = sony_file("SONY", "ILCE-1", 6, &alpha1_entries()); + let mut jpeg = vec![0xff, 0xd8, 0xff, 0xe0, 0, 16]; + jpeg.extend_from_slice(b"JFIF\0\x01\x01\0\0\x01\0\x01\0\0"); + jpeg.extend_from_slice(&[0xff, 0xe1]); + jpeg.extend_from_slice(&((exif.len() + 8) as u16).to_be_bytes()); + jpeg.extend_from_slice(b"Exif\0\0"); + jpeg.extend_from_slice(&exif); + jpeg.extend_from_slice(&[0xff, 0xda, 0, 2]); + jpeg.extend(std::iter::repeat_n(0x55u8, 3_000_000)); + let whole = crate::extract(&jpeg).focus; + assert!(whole.is_some()); + let r = read(&jpeg); + assert_eq!(r.focus, whole); + assert!(r.bytes_read <= HEADER_PREFIX); + // unsupported bodies agree too (no point either way) + let other = sony_file("SONY", "ILCE-7M4", 1, &alpha1_entries()); + assert_eq!(read(&other).focus, None); +} + +/// Every truncation and corruption of a file gives the header read the whole-file read's answer +/// (or an error when the file is shorter than it said), never a panic. +#[test] +fn header_read_of_damaged_files_agrees_or_fails_cleanly() { + let file = sony_file("SONY", "ILCE-1", 1, &alpha1_entries()); + for cut in (0..file.len()).step_by(5) { + let part = &file[..cut]; + let r = read_header(part.len() as u64, &mut |off, buf: &mut [u8]| { + let s = off as usize; + buf.copy_from_slice(part.get(s..s + buf.len()).ok_or_else(|| std::io::Error::from(std::io::ErrorKind::UnexpectedEof))?); + Ok(()) + }) + .unwrap(); + assert_eq!(r.focus, crate::extract(part).focus, "cut at {cut}"); + } + let mut seed = 0x9e37_79b9_u32; + for _ in 0..400 { + let mut f = file.clone(); + for _ in 0..6 { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + let i = seed as usize % f.len(); + f[i] = (seed >> 8) as u8; + } + let r = read_header(f.len() as u64, &mut |off, buf: &mut [u8]| { + let s = off as usize; + buf.copy_from_slice(&f[s..s + buf.len()]); + Ok(()) + }) + .unwrap(); + assert_eq!(r.focus, crate::extract(&f).focus); + } + // a reader told the file is longer than it is (replaced since it was measured): an error + let r = read_header(file.len() as u64 + 100_000, &mut |off, buf: &mut [u8]| { + let s = off as usize; + buf.copy_from_slice(file.get(s..s + buf.len()).ok_or_else(|| std::io::Error::from(std::io::ErrorKind::UnexpectedEof))?); + Ok(()) + }); + assert!(r.is_err()); +} + +#[test] +fn the_synthetic_arw_is_laid_out_as_described() { + let f = synthetic_arw(3, 10); + assert_eq!(f.len(), 260); + let p = crate::extract(&f).focus.expect("a point"); + assert_eq!(p.words(), vec!["AF-C", "Wide", "Animal Eye Tracking"]); + assert!(crate::extract(&synthetic_arw(0, 0)).focus.is_none(), "manual focus"); +} + +#[test] +fn cameras_that_can_have_a_point() { + assert!(camera_supported("SONY ILCE-1") && camera_supported("Sony ILCE-7SM3 ")); + for c in ["SONY ILCE-7M4", "Canon EOS R5", "", "SONY", "ILCE-1", "SON"] { + assert!(!camera_supported(c), "{c}"); + } +} + +/// Opt-in: decode real α1 / A7S III files and compare with the values ExifTool (run as a black box) +/// reports for them. `LIGHTCRAFT_FOCUS_SAMPLES=

` is the photo folder the paths below are relative +/// to; skipped without it. No user file is part of the repository. +#[test] +fn real_sony_files_match_exiftool() { + let Ok(dir) = std::env::var("LIGHTCRAFT_FOCUS_SAMPLES") else { return }; + let dir = std::path::Path::new(&dir); + type Want = Option<([f64; 4], [f64; 2], &'static [&'static str])>; + // (file, EXIF orientation, ExifTool's FocusLocation and FocusFrameSize, the words we show; None = no point) + let cases: [(&str, u16, Want); 8] = [ + // α1, AF-C, Wide, Face tracking, AFAreaMode Animal Eye Tracking + ("2023/11-november/17th/_DSC6954.ARW", 1, Some(([8640.0, 5760.0, 3226.0, 1980.0], [175.0, 180.0], &["AF-C", "Wide", "Animal Eye Tracking"]))), + ("2023/11-november/17th/_DSC6894.ARW", 1, Some(([8640.0, 5760.0, 4725.0, 1860.0], [135.0, 132.0], &["AF-C", "Wide", "Animal Eye Tracking"]))), + // α1, manual focus (the location 4441 3060 is where the frame was parked) + ("2023/11-november/15th/_DSC6613.ARW", 1, None), + // α1 portrait, AF-S, Wide, Multi at the exact centre 4320 2880 + ("2022/01-january/9th/_DSC6079.ARW", 8, None), + // α1 APS-C crop mode portrait, AF-C, Wide, Lock On AF, AFAreaMode Tracking + ("2022/12-december/16th/_DSC1575.ARW", 8, Some(([5616.0, 3744.0, 4071.0, 1848.0], [280.0, 280.0], &["AF-C", "Wide", "Tracking"]))), + // A7S III portrait, AF-A, Wide, Face tracking, Animal Eye Tracking + ("2020/10-october/31st/DSC00065.ARW", 8, Some(([4240.0, 2832.0, 1192.0, 1675.0], [278.0, 277.0], &["AF-A", "Wide", "Animal Eye Tracking"]))), + // A7S III upside down, AF-C, a centred Flexible Spot: a real point + ("2021/11-november/7th/DSC05975.ARW", 3, Some(([4240.0, 2832.0, 2120.0, 1416.0], [251.0, 224.0], &["AF-C", "Flexible Spot"]))), + // A7S III, manual focus + ("2021/06-june/12th/DSC06806.ARW", 1, None), + ]; + for (name, exif_orientation, want) in cases { + let bytes = std::fs::read(dir.join(name)).unwrap_or_else(|e| panic!("{name}: {e}")); + let got = crate::extract(&bytes).focus; + match (want, got) { + (None, None) => {} + (Some((loc, size, words)), Some(f)) => { + let [w, h, x, y] = loc; + let sensor = + Rect { x0: (x - size[0] / 2.0) / w, y0: (y - size[1] / 2.0) / h, x1: (x + size[0] / 2.0) / w, y1: (y + size[1] / 2.0) / h }; + let r = Orientation::from_exif(exif_orientation).map_norm_rect(sensor); + for (a, b) in [(f.rect.x0, r.x0), (f.rect.y0, r.y0), (f.rect.x1, r.x1), (f.rect.y1, r.y1)] { + assert!((a - b).abs() < 1e-9, "{name}: {:?} vs {r:?}", f.rect); + } + assert_eq!(f.words(), words, "{name}"); + } + (w, g) => panic!("{name}: want {w:?}, got {g:?}"), + } + } +} diff --git a/crates/metadata/src/focus_read.rs b/crates/metadata/src/focus_read.rs new file mode 100644 index 000000000..14547b051 --- /dev/null +++ b/crates/metadata/src/focus_read.rs @@ -0,0 +1,472 @@ +//! Reading a file's focus point without reading the file: [`read_header`] asks for the few byte +//! ranges [`crate::focus::read`] looks at (a TIFF raw's IFD0, its Exif IFD, the maker-note IFD and +//! the values of the focus tags; a JPEG's Exif segment) and decodes them exactly as a whole-file +//! read would. On the α1 and the A7S III all of it sits in the first 16 KB of a 50–120 MB ARW, so a +//! library of them can be backfilled from a slow disk by reading one small block per photo. +//! +//! The ranges are found by walking the structure as the readers do (the IFD tables and the value +//! offsets they hold), round by round: each round asks only for what the bytes loaded so far point +//! at. A TIFF is decoded from a buffer of the file's length in which only the loaded ranges hold the +//! file's bytes (the rest stays zero and its pages are never touched), so every offset and bound the +//! reader checks is the one it checks on the whole file. Rounds, bytes and the buffer are bounded: +//! a file that wants more is reported as having no focus point. + +use std::io; + +use crate::focus::FocusPoint; +use craft_tiff::{ByteOrder, FieldType, Tiff, tags as t}; + +/// Bytes read first: the α1's and A7S III's focus data ends before 16 KB, so one read covers it. +pub const HEADER_PREFIX: u64 = 32 * 1024; +/// Later reads are widened to this granularity (fewer, larger reads on a spinning disk). +const ALIGN: u64 = 4 * 1024; +/// The most rounds of reads one file gets (a real file needs one; the walk takes at most nine). +const MAX_ROUNDS: usize = 12; +/// The most bytes read from one file: past this the layout is not one the decoder knows. +pub const MAX_HEADER_BYTES: u64 = 2 << 20; +/// The largest TIFF given a buffer of its length (its untouched pages are never written, but the +/// allocation must succeed: the engine's planned preview reads use the same bound). Raws are far below it. +const MAX_SPARSE_FILE: u64 = 1 << 30; +/// JPEG segments walked before giving up. +const MAX_SEGMENTS: usize = 1024; +/// Entries read from one IFD (the TIFF reader's limit). +const MAX_ENTRIES: u64 = 8192; + +/// The focus tags whose values [`crate::focus`] reads from a Sony maker note. +const FOCUS_TAGS: [u16; 6] = [0x201b, 0x201c, 0x2021, 0x2027, 0x2037, 0x9402]; + +/// What [`read_header`] found, and what it cost. +#[derive(Clone, Debug, PartialEq)] +pub struct HeaderRead { + /// The focus point a whole-file read gives ([`crate::extract`]`(file).focus`). + pub focus: Option, + /// Bytes read from the file. + pub bytes_read: u64, +} + +/// The focus point of a file of `len` bytes, reading only what it needs through `read_at(offset, +/// buf)`, which fills `buf` with the file's bytes at `offset` (an error — a file that shrank or +/// went away — is returned). A malformed or unknown file is `Ok` with no point. +pub fn read_header(len: u64, read_at: &mut dyn FnMut(u64, &mut [u8]) -> io::Result<()>) -> io::Result { + let mut reader = Reader { len, read_at, bytes: 0 }; + let head = reader.fetch(0, HEADER_PREFIX.min(len))?; + let focus = if head.starts_with(&[0xff, 0xd8]) { + jpeg(&mut reader, &head)? + } else if matches!(Tiff::sniff(&head), Some((_, false))) { + tiff(&mut reader, head)? + } else { + None + }; + Ok(HeaderRead { focus, bytes_read: reader.bytes }) +} + +struct Reader<'a> { + len: u64, + read_at: &'a mut dyn FnMut(u64, &mut [u8]) -> io::Result<()>, + bytes: u64, +} + +impl Reader<'_> { + /// `[start, end)` of the file (clipped to it), counted against [`MAX_HEADER_BYTES`]. + fn fetch(&mut self, start: u64, end: u64) -> io::Result> { + let end = end.min(self.len); + let n = end.saturating_sub(start); + if self.bytes.saturating_add(n) > MAX_HEADER_BYTES { + return Err(io::Error::other(OverBudget)); + } + let mut buf = vec![0u8; usize::try_from(n).map_err(|_| io::Error::other("range too large"))?]; + if n > 0 { + (self.read_at)(start, &mut buf)?; + } + self.bytes = self.bytes.saturating_add(n); + Ok(buf) + } +} + +/// A file whose focus data would take more than [`MAX_HEADER_BYTES`] to read. +#[derive(Debug)] +struct OverBudget; + +impl std::fmt::Display for OverBudget { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("focus data larger than a header") + } +} + +impl std::error::Error for OverBudget {} + +fn over_budget(e: &io::Error) -> bool { + e.get_ref().is_some_and(|i| i.is::()) +} + +/// An over-budget read is "no focus point here", not a failure to report. +fn budget_is_none(r: io::Result>) -> io::Result> { + match r { + Err(e) if over_budget(&e) => Ok(None), + r => r, + } +} + +/// A JPEG's first `Exif` APP1 segment, found as [`crate::jpeg_segments`] finds it. +fn jpeg(r: &mut Reader, head: &[u8]) -> io::Result> { + budget_is_none(jpeg_inner(r, head)) +} + +fn jpeg_inner(r: &mut Reader, head: &[u8]) -> io::Result> { + let len = r.len; + let at = |r: &mut Reader, start: u64, n: u64| -> io::Result> { + let local = usize::try_from(start).ok().zip(usize::try_from(start.saturating_add(n)).ok()).and_then(|(a, b)| head.get(a..b)); + match local { + Some(s) => Ok(s.to_vec()), + None => r.fetch(start, start.saturating_add(n)), + } + }; + let mut i = 2u64; + for _ in 0..MAX_SEGMENTS { + if i.saturating_add(4) > len { + break; + } + let h = at(r, i, 4)?; + let (Some(&ff), Some(&marker), Some(&a), Some(&b)) = (h.first(), h.get(1), h.get(2), h.get(3)) else { break }; + if ff != 0xff { + break; + } + if marker == 0xff { + i += 1; + continue; + } + if marker == 0xd8 || (0xd0..=0xd7).contains(&marker) || marker == 0x01 { + i += 2; + continue; + } + if marker == 0xd9 || marker == 0xda { + break; + } + let seg = u64::from(u16::from_be_bytes([a, b])); + if seg < 2 { + break; + } + let end = i.saturating_add(2).saturating_add(seg).min(len); + if marker == 0xe1 { + let payload = at(r, i + 4, end.saturating_sub(i + 4))?; + if payload.starts_with(b"Exif\0") { + return Ok(crate::read_exif(payload.get(6..).unwrap_or(&[])).focus); + } + } + i = end; + } + Ok(None) +} + +/// A TIFF raw, decoded from a buffer of its length holding only the ranges [`plan`] asked for. +fn tiff(r: &mut Reader, head: Vec) -> io::Result> { + if r.len > MAX_SPARSE_FILE { + return Ok(None); + } + let Ok(len) = usize::try_from(r.len) else { return Ok(None) }; + let mut buf = vec![0u8; len]; + let Some(dst) = buf.get_mut(..head.len()) else { return Ok(None) }; + dst.copy_from_slice(&head); + let mut loaded = Loaded::default(); + loaded.insert(0, head.len() as u64); + for _ in 0..MAX_ROUNDS { + match plan(&buf, &loaded) { + Step::Ready => { + // The decoder's allocation budget follows the bytes actually fetched, not the logical + // file length: only the loaded ranges are data, and an unrelated entry that claims a + // huge value must be cut by what leaving it cost, not by what the buffer claims to hold. + // The same 8 × / + 1 MiB shape the general TIFF reader uses, over `reader.bytes` instead. + let opts = craft_tiff::ParseOptions { value_budget: Some(r.bytes.saturating_mul(8).saturating_add(1 << 20)), ..Default::default() }; + return Ok(crate::try_read_exif_with(&buf, &opts).ok().and_then(|m| m.focus)); + } + Step::Nothing => return Ok(None), + Step::Need(ranges) => { + for (a, b) in ranges { + // widen to the read granularity, but never re-read what is loaded + let (wa, wb) = ((a / ALIGN) * ALIGN, b.div_ceil(ALIGN).saturating_mul(ALIGN).min(r.len)); + for (s, e) in loaded.gaps(wa, wb) { + let bytes = match r.fetch(s, e) { + Err(e) if over_budget(&e) => return Ok(None), + other => other?, + }; + let (Ok(su), Ok(eu)) = (usize::try_from(s), usize::try_from(e)) else { return Ok(None) }; + let Some(dst) = buf.get_mut(su..eu) else { return Ok(None) }; + dst.copy_from_slice(&bytes); + loaded.insert(s, e); + } + } + } + } + } + Ok(None) +} + +/// Sorted, disjoint, non-touching byte ranges. +#[derive(Default)] +struct Loaded(Vec<(u64, u64)>); + +impl Loaded { + fn insert(&mut self, start: u64, end: u64) { + if start >= end { + return; + } + let (mut s, mut e) = (start, end); + let mut out = Vec::with_capacity(self.0.len() + 1); + for &(a, b) in &self.0 { + if b < s || a > e { + out.push((a, b)); + } else { + s = s.min(a); + e = e.max(b); + } + } + out.push((s, e)); + out.sort_unstable(); + self.0 = out; + } + + fn contains(&self, start: u64, end: u64) -> bool { + start >= end || self.0.iter().any(|&(a, b)| a <= start && end <= b) + } + + /// The parts of `[start, end)` not loaded. + fn gaps(&self, start: u64, end: u64) -> Vec<(u64, u64)> { + let mut out = Vec::new(); + let mut at = start; + for &(a, b) in &self.0 { + if b <= at || a >= end { + continue; + } + if a > at { + out.push((at, a.min(end))); + } + at = at.max(b); + if at >= end { + break; + } + } + if at < end { + out.push((at, end)); + } + out + } +} + +enum Step { + /// Load these and ask again. + Need(Vec<(u64, u64)>), + /// Everything the decoder reads is loaded. + Ready, + /// The file has nothing the decoder could read (no Exif, no maker note, a structure it rejects). + Nothing, +} + +/// One IFD entry as the TIFF reader sees it: where its value is (absolute), if it fits the file. +struct RawEntry { + tag: u16, + typ: FieldType, + start: u64, + end: u64, +} + +struct Want<'a> { + buf: &'a [u8], + order: ByteOrder, + loaded: &'a Loaded, + need: Vec<(u64, u64)>, +} + +impl Want<'_> { + fn len(&self) -> u64 { + self.buf.len() as u64 + } + + /// Whether `[start, end)` (clipped to the file) is loaded; asks for it if not. + fn want(&mut self, start: u64, end: u64) -> bool { + let end = end.min(self.len()); + if self.loaded.contains(start, end) { + return true; + } + self.need.push((start, end)); + false + } + + /// The entries of the IFD at `off` (values relative to 0), once its table is loaded; `Some(empty)` + /// for one the reader would reject. + fn table(&mut self, off: u64) -> Option> { + let len = self.len(); + if off == 0 || off >= len { + return Some(Vec::new()); + } + if !self.want(off, off.saturating_add(2)) { + return None; + } + let pos = usize::try_from(off).ok()?; + let n = u64::from(self.order.read_u16(self.buf, pos)?); + let table = off + 2; + let n_read = n.min(len.saturating_sub(table) / 12).min(MAX_ENTRIES); + let end = table + n_read * 12 + if n_read == n { 4 } else { 0 }; + if !self.want(off, end) { + return None; + } + let mut out = Vec::new(); + for i in 0..n_read { + let Ok(p) = usize::try_from(table + i * 12) else { continue }; + let (Some(tag), Some(typ), Some(count)) = ( + self.order.read_u16(self.buf, p), + self.order.read_u16(self.buf, p + 2).and_then(FieldType::from_u16), + self.order.read_u32(self.buf, p + 4), + ) else { + continue; + }; + let Some(total) = u64::from(count).checked_mul(typ.size() as u64) else { continue }; + let field = p as u64 + 8; + let start = if total <= 4 { + field + } else { + match self.order.read_u32(self.buf, p + 8) { + Some(v) => u64::from(v), + None => continue, + } + }; + let Some(end) = start.checked_add(total).filter(|&e| e <= len) else { continue }; + out.push(RawEntry { tag, typ, start, end }); + } + Some(out) + } + + /// The first unsigned value of an entry (loaded). + fn first_value(&self, e: &RawEntry) -> Option { + let pos = usize::try_from(e.start).ok()?; + match e.typ.size() { + 1 => self.buf.get(pos).map(|&b| u64::from(b)), + 2 => self.order.read_u16(self.buf, pos).map(u64::from), + 4 => self.order.read_u32(self.buf, pos).map(u64::from), + _ => self.order.read_u64(self.buf, pos), + } + } + + fn step(self) -> Step { + if self.need.is_empty() { Step::Ready } else { Step::Need(self.need) } + } +} + +/// What of a classic TIFF the focus decoder still needs, given what is `loaded`: IFD0 (Make, Model, +/// the Exif pointer), the Exif IFD (the maker-note entry), the maker note's header and IFD as +/// `craft_tiff::makernote` locates it, and the focus tags' values. +fn plan(buf: &[u8], loaded: &Loaded) -> Step { + let Some((order, false)) = Tiff::sniff(buf) else { return Step::Nothing }; + let mut w = Want { buf, order, loaded, need: Vec::new() }; + let Some(ifd0) = order.read_u32(buf, 4).map(u64::from) else { return Step::Nothing }; + let Some(e0) = w.table(ifd0) else { return w.step() }; + let mut exif = None; + for e in &e0 { + if matches!(e.tag, t::MAKE | t::MODEL | t::UNIQUE_CAMERA_MODEL | t::ORIENTATION) { + w.want(e.start, e.end); + } + if e.tag == t::EXIF_IFD && exif.is_none() && w.want(e.start, e.end) { + exif = w.first_value(e); + } + } + if !w.need.is_empty() { + return w.step(); + } + let Some(exif) = exif.filter(|&o| o != 0) else { return Step::Nothing }; + let Some(ee) = w.table(exif) else { return w.step() }; + let Some(note) = ee.iter().find(|e| e.tag == t::MAKER_NOTE) else { return Step::Nothing }; + let head_end = note.start.saturating_add(12).min(note.end); + if !w.want(note.start, head_end) { + return w.step(); + } + let head = usize::try_from(note.start).ok().zip(usize::try_from(head_end).ok()).and_then(|(a, b)| buf.get(a..b)).unwrap_or(&[]); + let sony = head.starts_with(b"SONY") || head.starts_with(b"VHAB \0"); + // another maker's header: `focus::read` only decodes Sony and header-less notes + let foreign: [&[u8]; 13] = [ + b"Nikon\0", + b"FUJIFILM", + b"GENERALE", + b"OLYMPUS\0", + b"OM SYSTEM\0", + b"OLYMP\0", + b"EPSON\0", + b"MINOL\0", + b"CAMER\0", + b"SANYO\0", + b"Panasonic\0", + b"AOC\0", + b"PENTAX \0", + ]; + if !sony && foreign.iter().any(|p| head.starts_with(p)) { + return Step::Nothing; + } + let mut entries = None; + if sony { + match w.table(note.start.saturating_add(12)) { + None => return w.step(), + Some(e) if !e.is_empty() => entries = Some(e), + Some(_) => {} + } + } + let entries = match entries { + Some(e) => e, + None => match w.table(note.start) { + Some(e) => e, + None => return w.step(), + }, + }; + for e in entries.iter().filter(|e| FOCUS_TAGS.contains(&e.tag)) { + w.want(e.start, e.end); + } + w.step() +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A reader over `file` that counts what it is asked for. + fn read(file: &[u8]) -> (io::Result, u64) { + let mut asked = 0u64; + let r = read_header(file.len() as u64, &mut |off, buf: &mut [u8]| { + asked += buf.len() as u64; + let start = usize::try_from(off).map_err(io::Error::other)?; + let src = file.get(start..start + buf.len()).ok_or_else(|| io::Error::from(io::ErrorKind::UnexpectedEof))?; + buf.copy_from_slice(src); + Ok(()) + }); + (r, asked) + } + + #[test] + fn loaded_ranges() { + let mut l = Loaded::default(); + l.insert(10, 20); + l.insert(30, 40); + assert_eq!(l.gaps(0, 50), vec![(0, 10), (20, 30), (40, 50)]); + l.insert(20, 30); + assert_eq!(l.0, vec![(10, 40)]); + assert!(l.contains(12, 38) && !l.contains(5, 12)); + assert!(l.gaps(15, 35).is_empty()); + } + + #[test] + fn garbage_and_empty_files_are_no_point() { + for file in [vec![], vec![0xff, 0xd8], b"II*\0\x08\0\0\0".to_vec(), b"MM\0*\xff\xff\xff\xff".to_vec(), vec![0x42; 100_000]] { + let (r, _) = read(&file); + assert_eq!(r.unwrap().focus, None); + } + // a JPEG of nothing but fill bytes and tiny segments stops at its bounds + let mut j = vec![0xff, 0xd8]; + for _ in 0..5000 { + j.extend_from_slice(&[0xff, 0xe2, 0, 2]); + } + assert_eq!(read(&j).0.unwrap().focus, None); + } + + #[test] + fn a_file_that_shrinks_mid_read_is_an_error() { + // the reader is told the file is longer than it is (replaced between the stat and the read) + let r = read_header(1 << 20, &mut |_, _| Err(io::Error::from(io::ErrorKind::UnexpectedEof))); + assert!(r.is_err()); + } +} diff --git a/crates/metadata/src/geom.rs b/crates/metadata/src/geom.rs new file mode 100644 index 000000000..804b7d643 --- /dev/null +++ b/crates/metadata/src/geom.rs @@ -0,0 +1,180 @@ +//! The geometry metadata is stated in: a point, an axis-aligned rectangle, and the eight Exif orientations. + +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct Point { + pub x: f64, + pub y: f64, +} + +impl Point { + pub const fn new(x: f64, y: f64) -> Self { + Self { x, y } + } +} + +/// Axis-aligned rectangle `[x0, x1) × [y0, y1)`. +#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct Rect { + pub x0: f64, + pub y0: f64, + pub x1: f64, + pub y1: f64, +} + +impl Rect { + pub const UNIT: Rect = Rect { x0: 0.0, y0: 0.0, x1: 1.0, y1: 1.0 }; + pub const fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Self { + Self { x0, y0, x1, y1 } + } + pub fn from_xywh(x: f64, y: f64, w: f64, h: f64) -> Self { + Self::new(x, y, x + w, y + h) + } + pub fn from_center(c: Point, w: f64, h: f64) -> Self { + Self::new(c.x - w / 2.0, c.y - h / 2.0, c.x + w / 2.0, c.y + h / 2.0) + } + pub fn from_points(a: Point, b: Point) -> Self { + Self::new(a.x.min(b.x), a.y.min(b.y), a.x.max(b.x), a.y.max(b.y)) + } + pub fn width(&self) -> f64 { + self.x1 - self.x0 + } + pub fn height(&self) -> f64 { + self.y1 - self.y0 + } + pub fn area(&self) -> f64 { + self.width().max(0.0) * self.height().max(0.0) + } + pub fn center(&self) -> Point { + Point::new((self.x0 + self.x1) / 2.0, (self.y0 + self.y1) / 2.0) + } + pub fn aspect(&self) -> f64 { + if self.height() != 0.0 { self.width() / self.height() } else { 0.0 } + } + pub fn contains(&self, p: Point) -> bool { + p.x >= self.x0 && p.x < self.x1 && p.y >= self.y0 && p.y < self.y1 + } + pub fn is_empty(&self) -> bool { + self.x1 <= self.x0 || self.y1 <= self.y0 + } + pub fn intersect(&self, o: &Rect) -> Rect { + Rect::new(self.x0.max(o.x0), self.y0.max(o.y0), self.x1.min(o.x1), self.y1.min(o.y1)) + } +} + +/// EXIF orientation (1..=8) as a transform of the stored pixel grid. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Orientation { + #[default] + Normal, + FlipH, + Rotate180, + FlipV, + Transpose, + Rotate90, + Transverse, + Rotate270, +} + +impl Orientation { + pub fn from_exif(v: u16) -> Orientation { + match v { + 2 => Orientation::FlipH, + 3 => Orientation::Rotate180, + 4 => Orientation::FlipV, + 5 => Orientation::Transpose, + 6 => Orientation::Rotate90, + 7 => Orientation::Transverse, + 8 => Orientation::Rotate270, + _ => Orientation::Normal, + } + } + pub fn to_exif(self) -> u16 { + match self { + Orientation::Normal => 1, + Orientation::FlipH => 2, + Orientation::Rotate180 => 3, + Orientation::FlipV => 4, + Orientation::Transpose => 5, + Orientation::Rotate90 => 6, + Orientation::Transverse => 7, + Orientation::Rotate270 => 8, + } + } + /// Whether width and height swap. + pub fn swaps_axes(self) -> bool { + matches!(self, Orientation::Transpose | Orientation::Rotate90 | Orientation::Transverse | Orientation::Rotate270) + } + /// Decompose into (clockwise quarter turns applied after an optional horizontal flip). + pub fn to_parts(self) -> (bool, u8) { + match self { + Orientation::Normal => (false, 0), + Orientation::Rotate90 => (false, 1), + Orientation::Rotate180 => (false, 2), + Orientation::Rotate270 => (false, 3), + Orientation::FlipH => (true, 0), + Orientation::Transverse => (true, 1), + Orientation::FlipV => (true, 2), + Orientation::Transpose => (true, 3), + } + } + pub fn from_parts(flip: bool, quarter_turns_cw: u8) -> Orientation { + match (flip, quarter_turns_cw % 4) { + (false, 0) => Orientation::Normal, + (false, 1) => Orientation::Rotate90, + (false, 2) => Orientation::Rotate180, + (false, _) => Orientation::Rotate270, + (true, 0) => Orientation::FlipH, + (true, 1) => Orientation::Transverse, + (true, 2) => Orientation::FlipV, + (true, _) => Orientation::Transpose, + } + } + /// Rotate the (already oriented) image a further quarter turn clockwise (`cw`) or counter-clockwise. + pub fn rotated(self, cw: bool) -> Orientation { + let (f, t) = self.to_parts(); + Orientation::from_parts(f, if cw { t + 1 } else { t + 3 }) + } + /// Flip the displayed image horizontally. + pub fn flipped_h(self) -> Orientation { + let (f, t) = self.to_parts(); + // flipH ∘ rot(t) = rot(-t) ∘ flipH + Orientation::from_parts(!f, (4 - t) % 4) + } + /// The orientation that undoes this one (`o.inverse().map(o.map(p)) == p`). Flips and half turns + /// are their own inverses; quarter turns swap. + pub fn inverse(self) -> Orientation { + match self { + Orientation::Rotate90 => Orientation::Rotate270, + Orientation::Rotate270 => Orientation::Rotate90, + o => o, + } + } + /// Map a rectangle in normalized (0..1) coordinates of the stored image to normalized coordinates + /// of the oriented one (e.g. a face region given on the upright photo → the frame after the user's + /// Rotate Left/Right). + pub fn map_norm_rect(self, r: Rect) -> Rect { + let (ax, ay) = self.map(r.x0, r.y0, 1.0, 1.0); + let (bx, by) = self.map(r.x1, r.y1, 1.0, 1.0); + Rect::from_points(Point::new(ax, ay), Point::new(bx, by)) + } + /// Map a point in stored pixel space (w×h stored dims) to oriented space. + pub fn map(self, x: f64, y: f64, w: f64, h: f64) -> (f64, f64) { + let (flip, t) = self.to_parts(); + let (mut x, y2, mut w2, mut h2) = (x, y, w, h); + if flip { + x = w2 - x; + } + let mut y = y2; + for _ in 0..t { + // rotate 90° cw: (x, y) -> (h - y, x) + let nx = h2 - y; + let ny = x; + x = nx; + y = ny; + std::mem::swap(&mut w2, &mut h2); + } + (x, y) + } +} diff --git a/crates/metadata/src/gpx.rs b/crates/metadata/src/gpx.rs new file mode 100644 index 000000000..691a2b7e1 --- /dev/null +++ b/crates/metadata/src/gpx.rs @@ -0,0 +1,491 @@ +//! GPS track logs in GPX 1.0 / 1.1 (the open GPS Exchange Format, ): +//! the timed track points of every ``, and the position at any moment for geotagging photos. +//! +//! What we use from the format: `` → `` → `` with optional `` +//! (metres) and `"#; + assert!(matches!(parse_gpx(wrong_close), Err(GpxError::Xml(_)))); + assert!( + parse_gpx("") + .unwrap() + .points + .is_empty() + ); + let deep = "".to_string() + &"".repeat(100); + assert_eq!(parse_gpx(&deep), Err(GpxError::TooComplex)); + } +} diff --git a/crates/metadata/src/iptc.rs b/crates/metadata/src/iptc.rs new file mode 100644 index 000000000..a398c6fc4 --- /dev/null +++ b/crates/metadata/src/iptc.rs @@ -0,0 +1,151 @@ +//! IPTC-IIM (Information Interchange Model 4.2) — the record-2 datasets people still carry in JPEGs. +//! +//! Stream of `0x1C record dataset size(u16 BE, or extended) data` tags. Text is UTF-8 when the record-1 +//! `CodedCharacterSet` (1:90) is `ESC % G`, otherwise decoded as UTF-8 when valid and Latin-1 otherwise. + +use crate::{DateTime, Metadata}; + +fn decode(b: &[u8], utf8: bool) -> String { + match std::str::from_utf8(b) { + Ok(s) => s.trim_end_matches('\0').trim().to_string(), + Err(_) if utf8 => String::from_utf8_lossy(b).trim_end_matches('\0').trim().to_string(), + Err(_) => b.iter().map(|&c| c as char).collect::().trim_end_matches('\0').trim().to_string(), + } +} + +/// Parse an IIM stream into the fields [`Metadata`] carries (title, keywords, artist, copyright, caption, +/// creation date/time). +pub fn parse_iptc(b: &[u8]) -> Metadata { + let mut m = Metadata::default(); + let mut utf8 = false; + let mut date: Option = None; + let mut time: Option = None; + let mut seen_keywords = std::collections::HashSet::new(); + let mut i = 0usize; + while i + 5 <= b.len() { + if b[i] != 0x1c { + break; + } + let (rec, ds) = (b[i + 1], b[i + 2]); + let mut size = u16::from_be_bytes([b[i + 3], b[i + 4]]) as usize; + let mut at = i + 5; + if size & 0x8000 != 0 { + // extended dataset: next (size & 0x7fff) bytes hold the length + let n = size & 0x7fff; + if n == 0 || n > 4 || at + n > b.len() { + break; + } + size = b[at..at + n].iter().fold(0usize, |a, &c| (a << 8) | c as usize); + at += n; + } + let Some(data) = b.get(at..at.saturating_add(size)) else { break }; + match (rec, ds) { + (1, 90) => utf8 = data == b"\x1b%G", + (2, 5) => m.title = Some(decode(data, utf8)).filter(|s| !s.is_empty()), + (2, 25) => { + let k = decode(data, utf8); + if !k.is_empty() && seen_keywords.insert(k.clone()) { + m.keywords.push(k); + } + } + (2, 80) => m.artist = Some(decode(data, utf8)).filter(|s| !s.is_empty()), + (2, 116) => m.copyright = Some(decode(data, utf8)).filter(|s| !s.is_empty()), + (2, 120) => m.caption = Some(decode(data, utf8)).filter(|s| !s.is_empty()), + (2, 55) => date = Some(decode(data, utf8)), + (2, 60) => time = Some(decode(data, utf8)), + _ => {} + } + i = at + size; + } + if let Some(d) = date.filter(|d| d.len() == 8 && d.bytes().all(|c| c.is_ascii_digit())) { + let mut s = format!("{}-{}-{}", &d[..4], &d[4..6], &d[6..8]); + if let Some(t) = time.filter(|t| t.is_ascii() && t.len() >= 6 && t[..6].bytes().all(|c| c.is_ascii_digit())) { + s += &format!("T{}:{}:{}", &t[..2], &t[2..4], &t[4..6]); + if t.len() >= 11 { + s += &format!("{}:{}", &t[6..9], &t[9..11]); + } + } + m.capture_time = DateTime::parse_iso(&s); + } + m +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + fn ds(out: &mut Vec, rec: u8, ds: u8, data: &[u8]) { + out.extend_from_slice(&[0x1c, rec, ds]); + out.extend_from_slice(&(data.len() as u16).to_be_bytes()); + out.extend_from_slice(data); + } + + #[test] + fn distinct_keywords_are_kept_once_each_without_rescanning() { + let mut b = Vec::new(); + for i in 0..50_000u32 { + ds(&mut b, 2, 25, format!("k{i}").as_bytes()); + ds(&mut b, 2, 25, b"same"); + } + let m = parse_iptc(&b); + assert_eq!(m.keywords.len(), 50_001); + assert_eq!(m.keywords.iter().filter(|k| *k == "same").count(), 1); + } + + pub(crate) fn sample_iim() -> Vec { + let mut v = Vec::new(); + ds(&mut v, 1, 90, b"\x1b%G"); + ds(&mut v, 2, 0, &[0, 4]); + ds(&mut v, 2, 5, "Tïtle".as_bytes()); + ds(&mut v, 2, 25, b"alpha"); + ds(&mut v, 2, 25, b"beta"); + ds(&mut v, 2, 25, b"alpha"); + ds(&mut v, 2, 80, b"Photographer"); + ds(&mut v, 2, 116, b"CC0"); + ds(&mut v, 2, 120, b"A caption"); + ds(&mut v, 2, 55, b"20210304"); + ds(&mut v, 2, 60, b"101112+0100"); + v + } + + #[test] + fn parses_record2() { + let m = parse_iptc(&sample_iim()); + assert_eq!(m.title.as_deref(), Some("Tïtle")); + assert_eq!(m.keywords, vec!["alpha".to_string(), "beta".to_string()]); + assert_eq!(m.artist.as_deref(), Some("Photographer")); + assert_eq!(m.copyright.as_deref(), Some("CC0")); + assert_eq!(m.caption.as_deref(), Some("A caption")); + assert_eq!(m.capture_time.unwrap().to_iso(), "2021-03-04T10:11:12+01:00"); + } + + #[test] + fn non_utf8_text_drops_trailing_nul() { + let mut v = Vec::new(); + ds(&mut v, 2, 5, &[0x43, 0xe9, 0x00]); + ds(&mut v, 2, 25, &[0x6b, 0xe9, 0x00]); + let m = parse_iptc(&v); + assert_eq!(m.title.as_deref(), Some("Cé")); + assert_eq!(m.keywords, vec!["ké".to_string()]); + let mut u = Vec::new(); + ds(&mut u, 1, 90, b"\x1b%G"); + ds(&mut u, 2, 5, &[0x43, 0xff, 0x00]); + assert_eq!(parse_iptc(&u).title.as_deref(), Some("C\u{fffd}")); + } + + #[test] + fn latin1_and_extended_and_garbage() { + let mut v = Vec::new(); + ds(&mut v, 2, 5, &[0x43, 0xe9]); // "Cé" in Latin-1 + v.extend_from_slice(&[0x1c, 2, 120, 0x80, 0x02, 0x00, 0x03]); + v.extend_from_slice(b"abc"); + let m = parse_iptc(&v); + assert_eq!(m.title.as_deref(), Some("Cé")); + assert_eq!(m.caption.as_deref(), Some("abc")); + let s = sample_iim(); + for n in 0..s.len() { + let _ = parse_iptc(&s[..n]); + } + assert_eq!(parse_iptc(&[0x1c, 2, 5, 0xff, 0xff]), Metadata::default()); + } +} diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs new file mode 100644 index 000000000..e485e4a85 --- /dev/null +++ b/crates/metadata/src/lib.rs @@ -0,0 +1,606 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +//! Photo metadata: EXIF, IPTC-IIM, XMP packets and GPX track logs, read into one [`Metadata`] model. +//! +//! One module per format: +//! - EXIF: [`read_exif`] reads a TIFF-structured Exif block, [`from_tiff`] an already parsed stream, +//! [`write_exif`] writes one. Maker notes supply the lens and, in [`focus`], where the camera focused. +//! [`tags`] lists every tag by name. +//! - IPTC-IIM: [`parse_iptc`] reads record 2 datasets. +//! - XMP: [`parse_xmp`] and [`write_xmp`] read and write packets; [`merge_xmp`] updates a packet +//! another program wrote without losing what it does not understand. +//! - GPX: [`parse_gpx`] reads track logs for geotagging by capture time. +//! - Containers: [`embedded`], [`jpeg_segments`], [`png_chunks`] and [`webp_chunks`] locate the Exif, +//! XMP, ICC and IPTC blocks of JPEG, PNG, WebP, TIFF-based, Fujifilm RAF and Canon CR3 ([`cr3`]) files. +//! [`extract`] does all of the above for a whole file. +//! +//! HEIF, PSD and MRW files are read by their own crates. Put the blocks they return into an +//! [`Embedded`] and call [`from_embedded`]. + +mod container; +pub mod cr3; +mod datetime; +mod exif; +pub mod focus; +mod focus_read; +mod geom; +mod gpx; +mod iptc; +pub mod tags; +mod xmp; +mod xmp_merge; + +pub use container::{Embedded, embedded, jpeg_segments, png_chunks, webp_chunks}; +pub use datetime::DateTime; +pub use exif::{from_tiff, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif}; +pub use focus::FocusPoint; +pub use geom::{Orientation, Point, Rect}; +pub use gpx::{GpxError, Match, TrackPoint, Tracklog, parse_gpx}; +pub use iptc::parse_iptc; +pub use tags::{TagRow, file_tag_rows, tag_rows}; +pub use xmp::{CRS_NS, XmpData, XmpError, XmpNamespace, XmpValue, XmpWrite, parse_xmp, parse_xmp_with, write_xmp, write_xmp_with}; +pub use xmp_merge::{MergeRules, merge_xmp}; + +use serde::{Deserialize, Serialize}; + +/// GPS position (WGS 84). Latitude/longitude in signed decimal degrees (north / east positive). +#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct Gps { + pub latitude: f64, + pub longitude: f64, + /// Metres above sea level (negative below). + pub altitude: Option, +} + +/// Exif `Flash` value decoded. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct Flash { + pub fired: bool, + /// The raw Exif `Flash` bit field. + pub raw: u16, +} + +/// A named region of interest on a photo (MWG Region Guidelines, `mwg-rs:Regions`): most often a face, +/// drawn by Lightroom, digiKam, Picasa or similar tools. Read only: [`write_xmp`] does not write regions. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Region { + /// Normalized to the image's full, oriented frame (`Metadata::width`/`height`), y-down, 0..1 on + /// both axes — MWG's own convention, and what `AppliedToDimensions` areas are converted to. + pub rect: Rect, + pub kind: RegionKind, + /// `mwg-rs:Name` (e.g. the person's name). + pub name: Option, + pub description: Option, +} + +/// `mwg-rs:Type`. Unrecognised values are kept verbatim rather than dropped. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum RegionKind { + Face, + Pet, + Focus, + BarCode, + /// `mwg-rs:Type` was missing, empty, or a value we don't recognise. + Other(String), +} + +/// What a photo catalogue shows or searches about a photo. All fields are optional; unknown = `None`/empty. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct Metadata { + // camera + pub make: Option, + pub model: Option, + pub serial_number: Option, + pub software: Option, + // lens + pub lens_make: Option, + pub lens_model: Option, + pub lens_serial_number: Option, + /// Min/max focal length (mm) and min/max f-number at those, as in Exif `LensSpecification`. + pub lens_spec: Option<[f64; 4]>, + // capture + pub capture_time: Option, + /// Seconds. + pub exposure_time: Option, + pub f_number: Option, + pub iso: Option, + /// Millimetres. + pub focal_length: Option, + pub focal_length_35mm: Option, + pub flash: Option, + /// EV. + pub exposure_bias: Option, + /// Exif `ExposureProgram` code. + pub exposure_program: Option, + /// Exif `MeteringMode` code. + pub metering_mode: Option, + /// Exif `WhiteBalance` (0 auto, 1 manual). + pub white_balance: Option, + pub orientation: Option, + pub gps: Option, + /// Pixel dimensions as recorded (not oriented). + pub width: Option, + pub height: Option, + // descriptive / user + pub artist: Option, + pub copyright: Option, + /// Copyright status (`xmpRights:Marked`): `true` = copyrighted, `false` = public domain, `None` = unknown. + pub copyright_marked: Option, + /// Rights usage terms (`xmpRights:UsageTerms`). + pub usage_terms: Option, + /// Copyright info URL (`xmpRights:WebStatement`). + pub copyright_url: Option, + pub title: Option, + pub caption: Option, + /// Accessibility text (`Iptc4xmpCore:AltTextAccessibility`). + pub alt_text: Option, + /// Long accessibility description (`Iptc4xmpCore:ExtDescrAccessibility`). + pub extended_description: Option, + /// Place within the city (`Iptc4xmpCore:Location`). + pub sublocation: Option, + /// `photoshop:City`, `photoshop:State`, `photoshop:Country`. + pub city: Option, + pub state: Option, + pub country: Option, + pub keywords: Vec, + /// Hierarchical keywords, `|`-separated paths (`lr:hierarchicalSubject` convention). + pub hierarchical_keywords: Vec, + /// -1 = rejected, 0 = unrated, 1..=5 stars. + pub rating: Option, + /// Colour label name (e.g. "Red"). + pub label: Option, + /// Face/pet/focus/barcode regions (`mwg-rs:Regions`), read from XMP only. + pub regions: Vec, + /// Where the camera focused, from its maker notes ([`focus`]; Sony only for now), on the upright photo. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub focus: Option, +} + +macro_rules! fill { + ($self:ident, $o:ident, $($f:ident),*) => { $( if $self.$f.is_none() { $self.$f = $o.$f.clone(); } )* }; +} +macro_rules! overlay { + ($self:ident, $o:ident, $($f:ident),*) => { $( if $o.$f.is_some() { $self.$f = $o.$f.clone(); } )* }; +} + +impl Metadata { + /// Fill every empty field of `self` from `other`. + pub fn fill_missing(&mut self, other: &Metadata) { + fill!( + self, + other, + make, + model, + serial_number, + software, + lens_make, + lens_model, + lens_serial_number, + lens_spec, + capture_time, + exposure_time, + f_number, + iso, + focal_length, + focal_length_35mm, + flash, + exposure_bias, + exposure_program, + metering_mode, + white_balance, + orientation, + gps, + width, + height + ); + self.fill_missing_descriptive(other); + } + + /// Fill descriptive metadata separately from the camera and image properties. + fn fill_missing_descriptive(&mut self, other: &Metadata) { + fill!( + self, + other, + artist, + copyright, + copyright_marked, + usage_terms, + copyright_url, + title, + caption, + alt_text, + extended_description, + sublocation, + city, + state, + country, + rating, + label + ); + if self.keywords.is_empty() { + self.keywords = other.keywords.clone(); + } + if self.hierarchical_keywords.is_empty() { + self.hierarchical_keywords = other.hierarchical_keywords.clone(); + } + if self.regions.is_empty() { + self.regions = other.regions.clone(); + } + if self.focus.is_none() { + self.focus = other.focus.clone(); + } + } + + /// Replace the user-editable fields (rating, label, title, caption, artist, copyright, keywords, GPS) with + /// `other`'s where `other` has them — the XMP-over-EXIF precedence rule. + pub fn overlay_user_fields(&mut self, other: &Metadata) { + overlay!(self, other, rating, label, title, caption, artist, copyright, copyright_marked, usage_terms, copyright_url, gps); + if !other.keywords.is_empty() { + self.keywords = other.keywords.clone(); + } + if !other.hierarchical_keywords.is_empty() { + self.hierarchical_keywords = other.hierarchical_keywords.clone(); + } + if !other.regions.is_empty() { + self.regions = other.regions.clone(); + } + } + + /// Exposure time formatted like cameras show it: `1/250`, `0.5`, `2″`. + pub fn exposure_display(&self) -> Option { + let t = self.exposure_time?; + if t.is_nan() || t <= 0.0 { + return None; + } + Some(if t < 0.3 { + format!("1/{}", (1.0 / t).round()) + } else if t < 1.0 { + format!("{}", (t * 10.0).round() / 10.0) + } else { + format!("{}″", (t * 10.0).round() / 10.0) + }) + } + + /// Human name of the metering mode. + pub fn metering_name(&self) -> Option<&'static str> { + Some(match self.metering_mode? { + 1 => "Average", + 2 => "Center-weighted average", + 3 => "Spot", + 4 => "Multi-spot", + 5 => "Pattern", + 6 => "Partial", + 255 => "Other", + _ => "Unknown", + }) + } + + /// Human name of the exposure program. + pub fn exposure_program_name(&self) -> Option<&'static str> { + Some(match self.exposure_program? { + 1 => "Manual", + 2 => "Program AE", + 3 => "Aperture-priority AE", + 4 => "Shutter speed priority AE", + 5 => "Creative (slow speed)", + 6 => "Action (high speed)", + 7 => "Portrait", + 8 => "Landscape", + 9 => "Bulb", + _ => "Not defined", + }) + } +} + +/// Read everything understood from a whole file (JPEG, PNG, WebP, TIFF-based incl. DNG and most raws, RAF, CR3). +/// Precedence: EXIF, then IPTC fills gaps, then XMP overrides user fields and fills the rest. +pub fn extract(bytes: &[u8]) -> Metadata { + from_embedded(&embedded(bytes)) +} + +/// [`extract`] for blocks that are already located, for example by a HEIF or PSD reader. Best effort: +/// a block that cannot be read counts as absent. A caller that must tell "no location" from +/// "unreadable" uses [`try_read_exif`] and [`parse_xmp`] on the blocks first. +pub fn from_embedded(e: &Embedded) -> Metadata { + let mut m = e.exif.as_deref().map(read_exif).unwrap_or_default(); + let xmp = e.xmp.as_deref().and_then(|xmp| parse_xmp(xmp).ok()); + // the size the Exif states: a size only the XMP states may be the upright one, so it can't tell how the photo is stored + let stored = m.width.zip(m.height); + // and the orientation the Exif states: the photo is displayed by that alone, so an orientation only the XMP + // states ([`Metadata::fill_missing`] reports it below) can't tell how the photo is stored either + let exif_orientation = e.exif.as_deref().and_then(exif::ifd0_orientation); + if let Some(i) = e.iptc.as_deref() { + m.fill_missing(&parse_iptc(i)); + } + if let Some(xmp) = xmp { + m.overlay_user_fields(&xmp.metadata); + m.fill_missing(&xmp.metadata); + // boxes another app drew on the photo as stored go where the upright photo has them + if let (Some(drawn_on), Some(orientation), Some(stored)) = (xmp.regions_drawn_on(), exif_orientation, stored) { + upright_regions(&mut m.regions, drawn_on, orientation, stored); + } + } + m +} + +/// Turn regions drawn on a photo as it is stored onto the upright photo. MWG gives each box as a fraction of +/// `AppliedToDimensions` (`drawn_on`); for a photo whose Exif `orientation` turns it a quarter turn, some apps give the +/// stored, sideways size there and boxes on the stored pixels to match (Lightroom says so per box with +/// `mwg-rs:Rotation` instead, which [`parse_xmp`] reads). When `drawn_on` has the shape of the photo as stored +/// (`stored`, before `orientation`) rather than the upright one, every box is turned with `orientation`. Returns +/// whether they were. A square photo or `drawn_on` looks the same both ways, and a half turn or a mirror doesn't +/// change the shape: their boxes are left as they are. +pub fn upright_regions(regions: &mut [Region], drawn_on: (f64, f64), orientation: Orientation, stored: (u32, u32)) -> bool { + let ((dw, dh), (sw, sh)) = (drawn_on, stored); + if regions.is_empty() || !orientation.swaps_axes() || sw == sh || dw == dh || (dw > dh) != (sw > sh) { + return false; + } + for r in regions.iter_mut() { + r.rect = orientation.map_norm_rect(r.rect); + } + true +} + +/// A lens name from its focal and aperture range (Exif LensSpecification, or a Nikon maker note's Lens), written the +/// way Lightroom shows a lens it has no name for: `50.0 mm f/1.8`, `18.0-135.0 mm f/3.5-5.6`. +pub fn lens_spec_name(spec: [f64; 4]) -> Option { + let [short, long, wide, narrow] = spec; + if ![short, long, wide, narrow].iter().all(|v| v.is_finite() && *v > 0.0) || long < short { + return None; + } + let focal = if (long - short).abs() < 0.05 { format!("{short:.1}") } else { format!("{short:.1}-{long:.1}") }; + let aperture = if (narrow - wide).abs() < 0.05 { format!("{wide:.1}") } else { format!("{wide:.1}-{narrow:.1}") }; + Some(format!("{focal} mm f/{aperture}")) +} + +/// Lenient number parsing used by XMP/IPTC: `"28/10"`, `"2.8"`, `"+0.7"`. +pub(crate) fn parse_number(s: &str) -> Option { + let s = s.trim(); + if let Some((n, d)) = s.split_once('/') { + let (n, d) = (n.trim().parse::().ok()?, d.trim().parse::().ok()?); + return (d != 0.0).then_some(n / d).filter(|v| v.is_finite()); + } + s.parse::().ok().filter(|v| v.is_finite()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn lens_names_from_a_spec_read_like_lightroom() { + assert_eq!(lens_spec_name([50.0, 50.0, 1.8, 1.8]).as_deref(), Some("50.0 mm f/1.8")); + assert_eq!(lens_spec_name([18.0, 135.0, 3.5, 5.6]).as_deref(), Some("18.0-135.0 mm f/3.5-5.6")); + assert_eq!(lens_spec_name([24.0, 70.0, 2.8, 2.8]).as_deref(), Some("24.0-70.0 mm f/2.8")); + assert_eq!(lens_spec_name([70.0, 210.0, 4.0, 5.6]).as_deref(), Some("70.0-210.0 mm f/4.0-5.6")); + assert_eq!(lens_spec_name([0.0, 0.0, 0.0, 0.0]), None); + assert_eq!(lens_spec_name([200.0, 80.0, 2.8, 2.8]), None); + assert_eq!(lens_spec_name([f64::NAN, 50.0, 1.8, 1.8]), None); + } + + #[test] + fn numbers() { + assert_eq!(parse_number("28/10"), Some(2.8)); + assert_eq!(parse_number(" +0.7 "), Some(0.7)); + assert_eq!(parse_number("1/0"), None); + assert_eq!(parse_number("abc"), None); + assert_eq!(parse_number("inf"), None); + } + + #[test] + fn exposure_display() { + let mut m = Metadata { exposure_time: Some(1.0 / 250.0), ..Default::default() }; + assert_eq!(m.exposure_display().as_deref(), Some("1/250")); + m.exposure_time = Some(0.5); + assert_eq!(m.exposure_display().as_deref(), Some("0.5")); + m.exposure_time = Some(2.0); + assert_eq!(m.exposure_display().as_deref(), Some("2″")); + m.exposure_time = Some(0.0); + assert_eq!(m.exposure_display(), None); + } + + #[test] + fn merge_rules() { + let mut a = Metadata { make: Some("A".into()), title: Some("Original".into()), rating: Some(1), ..Default::default() }; + let b = Metadata { + make: Some("B".into()), + model: Some("M".into()), + title: Some("Replacement".into()), + caption: Some("Caption".into()), + rating: Some(4), + keywords: vec!["k".into()], + ..Default::default() + }; + a.fill_missing(&b); + assert_eq!(a.make.as_deref(), Some("A")); + assert_eq!(a.model.as_deref(), Some("M")); + assert_eq!(a.title.as_deref(), Some("Original")); + assert_eq!(a.caption.as_deref(), Some("Caption")); + assert_eq!(a.keywords, vec!["k".to_string()]); + assert_eq!(a.rating, Some(1)); + a.overlay_user_fields(&b); + assert_eq!(a.rating, Some(4)); + assert_eq!(a.make.as_deref(), Some("A")); + assert_eq!(a.keywords, vec!["k".to_string()]); + } + + #[test] + fn names() { + let m = Metadata { metering_mode: Some(5), exposure_program: Some(3), ..Default::default() }; + assert_eq!(m.metering_name(), Some("Pattern")); + assert_eq!(m.exposure_program_name(), Some("Aperture-priority AE")); + } + + /// An MWG regions packet with one face, its box centred at (0.25, 0.4) and 0.1 × 0.15 of `AppliedToDimensions` + /// `w` × `h`; `extra` goes on the region (e.g. Lightroom's `mwg-rs:Rotation`). + fn regions_packet(w: u32, h: u32, extra: &str) -> String { + format!( + r#" + + + + + + + + + + "# + ) + } + + fn near(r: Rect, b: [f64; 4]) -> bool { + [r.x0 - b[0], r.y0 - b[1], r.x1 - b[2], r.y1 - b[3]].iter().all(|d| d.abs() < 1e-9) + } + + /// The face's box as drawn: on the photo as stored, or on the upright one by apps that draw there. + const DRAWN: [f64; 4] = [0.2, 0.325, 0.3, 0.475]; + /// The same box on the photo turned upright by Exif orientation 6 (a quarter turn clockwise). + const TURNED_6: [f64; 4] = [0.525, 0.2, 0.675, 0.3]; + + #[test] + fn boxes_drawn_on_the_stored_photo_turn_with_its_orientation() { + let region = || Region { + rect: Rect { x0: DRAWN[0], y0: DRAWN[1], x1: DRAWN[2], y1: DRAWN[3] }, + kind: RegionKind::Face, + name: None, + description: None, + }; + let turned = |o: Orientation, drawn_on: (f64, f64), stored: (u32, u32)| { + let mut regions = [region()]; + let did = upright_regions(&mut regions, drawn_on, o, stored); + (did, regions[0].rect) + }; + // drawn on the 6000 × 4000 photo as stored: every quarter turn and its mirror image land elsewhere + for (o, want) in [ + (Orientation::Rotate90, TURNED_6), + (Orientation::Rotate270, [0.325, 0.7, 0.475, 0.8]), + (Orientation::Transpose, [0.325, 0.2, 0.475, 0.3]), + (Orientation::Transverse, [0.525, 0.7, 0.675, 0.8]), + ] { + let (did, r) = turned(o, (6000.0, 4000.0), (6000, 4000)); + assert!(did && near(r, want), "{o:?}: {r:?}"); + // a smaller copy of the photo as stored is the same frame + assert!(near(turned(o, (1500.0, 1000.0), (6000, 4000)).1, want), "{o:?} on a smaller size"); + } + // drawn on the upright photo (MWG's own frame), or where the shape can't tell the two apart: as they are + for (o, drawn_on, stored) in [ + (Orientation::Rotate90, (4000.0, 6000.0), (6000, 4000)), + (Orientation::Rotate90, (4000.0, 4000.0), (6000, 4000)), + (Orientation::Rotate90, (4000.0, 4000.0), (4000, 4000)), + (Orientation::Rotate180, (6000.0, 4000.0), (6000, 4000)), + (Orientation::FlipH, (6000.0, 4000.0), (6000, 4000)), + (Orientation::Normal, (6000.0, 4000.0), (6000, 4000)), + ] { + let (did, r) = turned(o, drawn_on, stored); + assert!(!did && near(r, DRAWN), "{o:?} drawn on {drawn_on:?} of {stored:?}: {r:?}"); + } + } + + /// An app that doesn't say how the photo was turned (no `mwg-rs:Rotation`) drew the faces of a photo + /// with Exif orientation 6 on its stored pixels, with the stored 6000 × 4000 as `AppliedToDimensions`. The boxes + /// land on the faces of the upright photo, not where the stored photo had them. + #[test] + fn regions_drawn_on_the_stored_photo_are_read_onto_the_upright_one() { + let jpeg = |packet: &str| { + let mut j = vec![0xff, 0xd8]; + let mut seg = |payload: Vec| { + j.extend([0xff, 0xe1]); + j.extend(u16::try_from(payload.len() + 2).unwrap().to_be_bytes()); + j.extend(payload); + }; + // orientation 6, 6000 × 4000 as stored + seg([b"Exif\0\0".as_slice(), &crate::exif::tests::sample_exif(craft_tiff::ByteOrder::Little)].concat()); + seg([b"http://ns.adobe.com/xap/1.0/\0".as_slice(), packet.as_bytes()].concat()); + j.extend([0xff, 0xd9]); + j + }; + let read = |packet: &str| { + let m = extract(&jpeg(packet)); + assert_eq!((m.orientation, m.width, m.height), (Some(Orientation::Rotate90), Some(6000), Some(4000))); + assert_eq!(from_embedded(&embedded(&jpeg(packet))).regions, m.regions); + assert_eq!(m.regions.len(), 1); + m.regions[0].rect + }; + let stored = read(®ions_packet(6000, 4000, "")); + assert!(near(stored, TURNED_6), "drawn on the photo as stored: {stored:?}"); + let upright = read(®ions_packet(4000, 6000, "")); + assert!(near(upright, DRAWN), "drawn on the upright photo, MWG's way: {upright:?}"); + // Lightroom's boxes say how the photo was turned: turned once, by that + let lightroom = read(®ions_packet(6000, 4000, r#" mwg-rs:Rotation="-1.57080""#)); + assert!(near(lightroom, TURNED_6), "Lightroom's −π/2: {lightroom:?}"); + } + + /// A size only the XMP states can't tell how the photo is stored: `tiff:ImageWidth` and `tiff:ImageLength` of the + /// upright photo have the shape of boxes drawn on it upright, and those must not be turned. + #[test] + fn a_size_only_the_xmp_states_turns_no_regions() { + // orientation 6, no pixel size + let exif = write_exif(&Metadata { orientation: Some(Orientation::Rotate90), ..Default::default() }); + let exif = strip_exif_header(&exif); + let packet = regions_packet(4000, 6000, "") + .replace(r#"rdf:about="""#, r#"rdf:about="" xmlns:tiff="http://ns.adobe.com/tiff/1.0/" tiff:ImageWidth="4000" tiff:ImageLength="6000""#); + let mut j = vec![0xff, 0xd8]; + let mut seg = |payload: Vec| { + j.extend([0xff, 0xe1]); + j.extend(u16::try_from(payload.len() + 2).unwrap().to_be_bytes()); + j.extend(payload); + }; + seg([b"Exif\0\0".as_slice(), exif].concat()); + seg([b"http://ns.adobe.com/xap/1.0/\0".as_slice(), packet.as_bytes()].concat()); + j.extend([0xff, 0xd9]); + let m = extract(&j); + assert_eq!((m.orientation, m.width, m.height), (Some(Orientation::Rotate90), Some(4000), Some(6000))); + assert_eq!(m.regions.len(), 1); + assert!(near(m.regions[0].rect, DRAWN), "{:?}", m.regions[0].rect); + assert_eq!(from_embedded(&embedded(&j)).regions, m.regions); + // a TIFF-based file: the whole file is the Exif block, with the XMP in its first IFD + let mut ifd0 = craft_tiff::IfdBuilder::new(); + ifd0.set(craft_tiff::tags::ORIENTATION, craft_tiff::Value::Short(vec![6])); + ifd0.set(craft_tiff::tags::XMP, craft_tiff::Value::Byte(packet.into_bytes())); + let m = extract(&craft_tiff::TiffWriter::default().write(&[ifd0]).unwrap()); + assert_eq!((m.orientation, m.width, m.height), (Some(Orientation::Rotate90), Some(4000), Some(6000))); + assert!(near(m.regions[0].rect, DRAWN), "TIFF: {:?}", m.regions[0].rect); + } + + /// A photo whose Exif has a size but no orientation, and whose XMP says `tiff:Orientation=6`: a viewer + /// displays it by the Exif alone, so it stays as stored, and boxes drawn on that stored frame are right as + /// written. Turning them by an orientation only the XMP states put them where the photo isn't. + #[test] + fn an_orientation_only_the_xmp_states_turns_no_regions() { + // 6000 × 4000 in the Exif, and no orientation + let size_ifd = || { + let mut sub = craft_tiff::IfdBuilder::new(); + sub.set(craft_tiff::tags::PIXEL_X_DIMENSION, craft_tiff::Value::Long(vec![6000])); + sub.set(craft_tiff::tags::PIXEL_Y_DIMENSION, craft_tiff::Value::Long(vec![4000])); + sub + }; + let mut exif_ifd0 = craft_tiff::IfdBuilder::new(); + exif_ifd0.set_child(craft_tiff::tags::EXIF_IFD, size_ifd()); + let exif = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&[exif_ifd0]).unwrap(); + let packet = regions_packet(6000, 4000, "") + .replace(r#"rdf:about="""#, r#"rdf:about="" xmlns:tiff="http://ns.adobe.com/tiff/1.0/" tiff:Orientation="6""#); + let mut j = vec![0xff, 0xd8]; + let mut seg = |payload: Vec| { + j.extend([0xff, 0xe1]); + j.extend(u16::try_from(payload.len() + 2).unwrap().to_be_bytes()); + j.extend(payload); + }; + seg([b"Exif\0\0".as_slice(), &exif].concat()); + seg([b"http://ns.adobe.com/xap/1.0/\0".as_slice(), packet.as_bytes()].concat()); + j.extend([0xff, 0xd9]); + let m = extract(&j); + assert_eq!((m.width, m.height), (Some(6000), Some(4000))); + assert_eq!(m.orientation, Some(Orientation::Rotate90), "the XMP's orientation is still reported"); + assert_eq!(m.regions.len(), 1); + assert!(near(m.regions[0].rect, DRAWN), "turned by an orientation the Exif doesn't state: {:?}", m.regions[0].rect); + assert_eq!(from_embedded(&embedded(&j)).regions, m.regions); + // a TIFF-based file: the first IFD has no orientation, its XMP (tag 700) has + let mut ifd0 = craft_tiff::IfdBuilder::new(); + ifd0.set(craft_tiff::tags::XMP, craft_tiff::Value::Byte(packet.into_bytes())); + ifd0.set_child(craft_tiff::tags::EXIF_IFD, size_ifd()); + let m = extract(&craft_tiff::TiffWriter::default().write(&[ifd0]).unwrap()); + assert_eq!(m.orientation, Some(Orientation::Rotate90)); + assert!(near(m.regions[0].rect, DRAWN), "TIFF: {:?}", m.regions[0].rect); + } +} diff --git a/crates/metadata/src/tags.rs b/crates/metadata/src/tags.rs new file mode 100644 index 000000000..6e5930922 --- /dev/null +++ b/crates/metadata/src/tags.rs @@ -0,0 +1,529 @@ +//! Every EXIF / TIFF / GPS tag of a file as readable `(group, name, value)` rows (the Info +//! panel's "All Metadata"). Names follow the public TIFF 6.0 / EXIF 2.3 / DNG specifications; +//! common enumerations are spelled out, everything else is shown as stored. Maker notes and +//! binary blobs are summarised by size; of a maker note only the lens name is listed (the +//! Panasonic one, as Panasonic writes no Exif LensModel). + +use craft_tiff::{Ifd, Tiff, Value}; + +/// One metadata row. +#[derive(Clone, Debug, PartialEq)] +pub struct TagRow { + /// `TIFF`, `EXIF`, `GPS`, `Interop`, or a maker note's (`Panasonic`). + pub group: &'static str, + pub tag: u16, + pub name: String, + pub value: String, +} + +const NAMES: &[(u16, &str)] = &[ + // TIFF / IFD0 + (0x00fe, "Subfile Type"), + (0x0100, "Image Width"), + (0x0101, "Image Height"), + (0x0102, "Bits Per Sample"), + (0x0103, "Compression"), + (0x0106, "Photometric Interpretation"), + (0x010e, "Image Description"), + (0x010f, "Make"), + (0x0110, "Model"), + (0x0112, "Orientation"), + (0x0115, "Samples Per Pixel"), + (0x0116, "Rows Per Strip"), + (0x0214, "Reference Black/White"), + (0x9216, "TIFF/EP Standard ID"), + (0x828d, "CFA Repeat Pattern Dim"), + (0x828e, "CFA Pattern"), + (0xa302, "CFA Pattern"), + (0x9217, "Sensing Method"), + (0xc4a5, "Print Image Matching"), + (0xa500, "Gamma"), + (0x011a, "X Resolution"), + (0x011b, "Y Resolution"), + (0x011c, "Planar Configuration"), + (0x0128, "Resolution Unit"), + (0x0131, "Software"), + (0x0132, "Date/Time Modified"), + (0x013b, "Artist"), + (0x013e, "White Point"), + (0x013f, "Primary Chromaticities"), + (0x0213, "YCbCr Positioning"), + (0x8298, "Copyright"), + (0xc612, "DNG Version"), + (0xc614, "Unique Camera Model"), + (0xc62f, "Camera Serial Number"), + // EXIF + (0x829a, "Exposure Time"), + (0x829d, "F-Number"), + (0x8822, "Exposure Program"), + (0x8824, "Spectral Sensitivity"), + (0x8827, "ISO Speed"), + (0x8830, "Sensitivity Type"), + (0x8832, "Recommended Exposure Index"), + (0x9000, "Exif Version"), + (0x9003, "Date/Time Original"), + (0x9004, "Date/Time Digitized"), + (0x9010, "Offset Time"), + (0x9011, "Offset Time Original"), + (0x9012, "Offset Time Digitized"), + (0x9101, "Components Configuration"), + (0x9102, "Compressed Bits Per Pixel"), + (0x9201, "Shutter Speed Value"), + (0x9202, "Aperture Value"), + (0x9203, "Brightness Value"), + (0x9204, "Exposure Compensation"), + (0x9205, "Max Aperture Value"), + (0x9206, "Subject Distance"), + (0x9207, "Metering Mode"), + (0x9208, "Light Source"), + (0x9209, "Flash"), + (0x920a, "Focal Length"), + (0x9214, "Subject Area"), + (0x927c, "Maker Note"), + (0x9286, "User Comment"), + (0x9290, "Sub-second Time"), + (0x9291, "Sub-second Time Original"), + (0x9292, "Sub-second Time Digitized"), + (0xa000, "FlashPix Version"), + (0xa001, "Color Space"), + (0xa002, "Pixel X Dimension"), + (0xa003, "Pixel Y Dimension"), + (0xa20e, "Focal Plane X Resolution"), + (0xa20f, "Focal Plane Y Resolution"), + (0xa210, "Focal Plane Resolution Unit"), + (0xa215, "Exposure Index"), + (0xa217, "Sensing Method"), + (0xa300, "File Source"), + (0xa301, "Scene Type"), + (0xa401, "Custom Rendered"), + (0xa402, "Exposure Mode"), + (0xa403, "White Balance"), + (0xa404, "Digital Zoom Ratio"), + (0xa405, "Focal Length in 35mm Film"), + (0xa406, "Scene Capture Type"), + (0xa407, "Gain Control"), + (0xa408, "Contrast"), + (0xa409, "Saturation"), + (0xa40a, "Sharpness"), + (0xa40c, "Subject Distance Range"), + (0xa420, "Image Unique ID"), + (0xa430, "Camera Owner Name"), + (0xa431, "Body Serial Number"), + (0xa432, "Lens Specification"), + (0xa433, "Lens Make"), + (0xa434, "Lens Model"), + (0xa435, "Lens Serial Number"), + // GPS + (0x0000, "GPS Version"), + (0x0001, "GPS Latitude Ref"), + (0x0002, "GPS Latitude"), + (0x0003, "GPS Longitude Ref"), + (0x0004, "GPS Longitude"), + (0x0005, "GPS Altitude Ref"), + (0x0006, "GPS Altitude"), + (0x0007, "GPS Time Stamp"), + (0x000c, "GPS Speed Ref"), + (0x000d, "GPS Speed"), + (0x0010, "GPS Img Direction Ref"), + (0x0011, "GPS Img Direction"), + (0x0012, "GPS Map Datum"), + (0x001d, "GPS Date Stamp"), +]; + +/// GPS tags reuse low numbers, so they are looked up separately. +fn name(group: &str, tag: u16) -> String { + let gps = group == "GPS"; + NAMES + .iter() + .find(|(t, n)| *t == tag && (n.starts_with("GPS") == gps || (gps && tag > 0x1f))) + .map(|(_, n)| n.to_string()) + .unwrap_or_else(|| format!("Tag 0x{tag:04X}")) +} + +fn rational(n: f64, d: f64) -> String { + if d == 0.0 { + return "—".into(); + } + let v = n / d; + if (v - v.round()).abs() < 1e-9 { + let r = v.round(); + if r == 0.0 { "0".to_string() } else { format!("{}", r) } + } else { + let s = format!("{:.4}", v); + let s = s.trim_end_matches('0').trim_end_matches('.'); + if s == "-0" { "0".to_string() } else { s.to_string() } + } +} + +fn list(v: &[T]) -> String { + list_with(v, T::to_string) +} + +/// The first 16 values of `v` written by `f`, and how many there are when that's not all of them. +fn list_with(v: &[T], f: impl Fn(&T) -> String) -> String { + let shown: Vec = v.iter().take(16).map(f).collect(); + if v.len() > 16 { format!("{} … ({} values)", shown.join(", "), v.len()) } else { shown.join(", ") } +} + +/// Spelled-out enumerations for the commonest tags. +fn enumerated(group: &str, tag: u16, v: i64) -> Option<&'static str> { + if group == "GPS" { + return None; + } + Some(match (tag, v) { + (0x0112, 1) => "Horizontal (normal)", + (0x0112, 3) => "Rotated 180°", + (0x0112, 6) => "Rotated 90° CW", + (0x0112, 8) => "Rotated 90° CCW", + (0x0112, 2) => "Mirrored horizontally", + (0x0112, 4) => "Mirrored vertically", + (0x0103, 1) => "Uncompressed", + (0x0103, 6) => "JPEG (old-style)", + (0x0103, 7) => "JPEG", + (0x0103, 8) => "Deflate", + (0x0103, 32773) => "PackBits", + (0x0103, 34892) => "Lossy JPEG", + (0x0103, 34713) => "Nikon NEF Compressed", + (0x0103, 32767) => "Sony ARW Compressed", + (0x0103, 52546) => "JPEG XL", + (0x0106, 0) => "WhiteIsZero", + (0x0106, 1) => "BlackIsZero", + (0x0106, 2) => "RGB", + (0x0106, 6) => "YCbCr", + (0x0106, 32803) => "Color Filter Array", + (0x0106, 34892) => "Linear Raw", + (0x011c, 1) => "Chunky", + (0x011c, 2) => "Planar", + (0x0128, 2) => "inches", + (0x0128, 3) => "centimetres", + (0x8822, 1) => "Manual", + (0x8822, 2) => "Program", + (0x8822, 3) => "Aperture priority", + (0x8822, 4) => "Shutter priority", + (0x9207, 2) => "Center-weighted average", + (0x9207, 3) => "Spot", + (0x9207, 5) => "Pattern", + (0xa001, 1) => "sRGB", + (0xa001, 0xffff) => "Uncalibrated", + (0xa402, 0) => "Auto", + (0xa402, 1) => "Manual", + (0xa402, 2) => "Auto bracket", + (0xa403, 0) => "Auto", + (0xa403, 1) => "Manual", + (0xa406, 0) => "Standard", + (0xa406, 1) => "Landscape", + (0xa406, 2) => "Portrait", + (0xa406, 3) => "Night", + (0x9209, 0) => "No flash", + (0x9209, 1) => "Fired", + (0x9209, 5) => "Fired, return not detected", + (0x9209, 7) => "Fired, return detected", + (0x9209, 9) => "On, fired", + (0x9209, 16) => "Off, did not fire", + (0x9209, 24) => "Auto, did not fire", + (0x9209, 25) => "Auto, fired", + (0x9209, 32) => "No flash function", + (0x9208, 0) => "Unknown", + (0x9208, 1) => "Daylight", + (0x9208, 2) => "Fluorescent", + (0x9208, 3) => "Tungsten", + (0x9208, 4) => "Flash", + _ => return None, + }) +} + +fn value_text(group: &str, tag: u16, v: &Value) -> String { + let single = |x: i64| enumerated(group, tag, x).map(str::to_string).unwrap_or_else(|| x.to_string()); + match v { + Value::Ascii(s) => s.trim().to_string(), + Value::Short(a) if a.len() == 1 => single(a[0] as i64), + Value::Long(a) if a.len() == 1 => single(a[0] as i64), + Value::Short(a) => list(a), + Value::Long(a) => list(a), + Value::SShort(a) => list(a), + Value::SLong(a) => list(a), + Value::Long8(a) => list(a), + Value::SLong8(a) => list(a), + Value::Float(a) => list(a), + Value::Double(a) => list(a), + Value::Rational(a) if tag == 0x829a && group != "GPS" && a.len() == 1 && a[0].1 != 0 => { + // exposure time as a fraction of a second + let (n, d) = a[0]; + let s = n as f64 / d as f64; + if s >= 1.0 || n == 0 { format!("{} s", rational(n as f64, d as f64)) } else { format!("1/{} s", (1.0 / s).round()) } + } + Value::Rational(a) if tag == 0x829d && group != "GPS" && a.len() == 1 => format!("f/{}", rational(a[0].0 as f64, a[0].1 as f64)), + Value::Rational(a) if tag == 0x920a && group != "GPS" && a.len() == 1 => format!("{} mm", rational(a[0].0 as f64, a[0].1 as f64)), + Value::Rational(a) => list_with(a, |(n, d)| rational(*n as f64, *d as f64)), + Value::SRational(a) => list_with(a, |(n, d)| rational(*n as f64, *d as f64)), + Value::Byte(b) | Value::Undefined(b) => { + // version tags and short printable values read as text + let printable = b.len() <= 64 && b.iter().all(|c| c.is_ascii_graphic() || *c == b' ' || *c == 0); + if printable && !b.is_empty() { + String::from_utf8_lossy(b).trim_end_matches('\0').trim().to_string() + } else if b.len() <= 8 { + list(b) + } else { + format!("({} bytes)", b.len()) + } + } + Value::SByte(a) => list(a), + Value::Ifd(a) => format!("(IFD at {})", list(a)), + Value::Ifd8(a) => format!("(IFD at {})", list(a)), + } +} + +fn rows_of(group: &'static str, ifd: &Ifd, out: &mut Vec) { + for e in &ifd.entries { + // pointers to other IFDs and image data are structure, not metadata + if matches!(e.tag, 0x8769 | 0x8825 | 0xa005 | 0x014a | 0x0111 | 0x0117 | 0x0144 | 0x0145 | 0x0201 | 0x0202 | 0x02bc | 0x83bb | 0x8773) + && group != "GPS" + { + continue; + } + out.push(TagRow { group, tag: e.tag, name: name(group, e.tag), value: value_text(group, e.tag, &e.value) }); + } +} + +/// The IFD holding a raw file's sensor data when it is not IFD0 (a colour-filter-array or linear-raw +/// image in a sub-IFD). IFD0 of such files describes a thumbnail, so its Compression is not the raw's. +fn raw_image_ifd(ifd0: &Ifd) -> Option<&Ifd> { + ifd0.sub_ifds.iter().find(|s| matches!(s.u32(0x0106), Some(32803 | 34892))) +} + +/// Nikon maker note `NEFCompression` (0x0093) spelled out, per the public Nikon tag documentation. +fn nef_compression(v: u32) -> Option<&'static str> { + Some(match v { + 1 => "Lossy (type 1)", + 2 => "Uncompressed", + 3 => "Lossless", + 4 => "Lossy (type 2)", + 5 => "Striped packed 12 bits", + 6 => "Uncompressed (reduced to 12 bit)", + 7 => "Unpacked 12 bits", + 8 => "Small", + 9 => "Packed 12 bits", + 10 => "Packed 14 bits", + 13 => "High Efficiency", + 14 => "High Efficiency*", + _ => return None, + }) +} + +/// Nikon maker note NEFCompression of a raw file, spelled out; `None` for other files or values. +fn nikon_nef_compression(tiff: &Tiff, bytes: &[u8]) -> Option<&'static str> { + let ifd0 = tiff.ifds.first()?; + let make = ifd0.string(0x010f)?; + if !make.to_ascii_uppercase().starts_with("NIKON") { + return None; + } + let e = ifd0.exif.as_ref()?.get(0x927c)?; + let mn = craft_tiff::makernote::parse_makernote(bytes, e.offset, e.count() as u64, tiff.order, &make)?; + nef_compression(mn.ifd.u32(0x0093)?) +} + +/// The Panasonic maker note's lens name (LensType, `0x0051`) as a row, the name `photo.inspect` shows as the lens. +fn panasonic_lens_row(data: &[u8], tiff: &Tiff, out: &mut Vec) { + if let Some(lens) = crate::exif::panasonic_maker_note_lens(data, tiff) { + out.push(TagRow { group: "Panasonic", tag: 0x0051, name: "Lens Type".into(), value: lens.trim().to_string() }); + } +} + +/// Every tag of a parsed TIFF structure (a TIFF-based raw, a TIFF, or an EXIF block): IFD0, +/// then the EXIF, GPS and interoperability IFDs. A raw file's Compression row is the sensor +/// image's, not IFD0's thumbnail's. +pub fn tag_rows(tiff: &Tiff) -> Vec { + let mut out = Vec::new(); + if let Some(ifd0) = tiff.ifds.first() { + rows_of("TIFF", ifd0, &mut out); + if let Some(c) = raw_image_ifd(ifd0).and_then(|r| r.value(0x0103)) + && let Some(row) = out.iter_mut().find(|r| r.tag == 0x0103) + { + // lossless JPEG (DNG, CR2) on the sensor image; plain "JPEG" would read as lossy + row.value = if c.get_u64(0) == Some(7) { "Lossless JPEG".to_string() } else { value_text("TIFF", 0x0103, c) }; + } + if let Some(e) = &ifd0.exif { + rows_of("EXIF", e, &mut out); + if let Some(i) = &e.interop { + rows_of("Interop", i, &mut out); + } + } + if let Some(g) = &ifd0.gps { + rows_of("GPS", g, &mut out); + } + } + out +} + +/// [`tag_rows`] of a file's bytes: a TIFF-based file directly, else its embedded EXIF block +/// (JPEG, PNG, WebP, HEIF, PSD/PSB…). Empty when there is none. +pub fn file_tag_rows(bytes: &[u8]) -> Vec { + if Tiff::sniff(bytes).is_some() + && let Ok(t) = Tiff::parse(bytes) + { + let mut rows = tag_rows(&t); + if let Some(words) = nikon_nef_compression(&t, bytes) + && let Some(row) = rows.iter_mut().find(|r| r.group == "TIFF" && r.tag == 0x0103) + { + row.value = words.to_string(); + } + panasonic_lens_row(bytes, &t, &mut rows); + return rows; + } + let Some(exif) = crate::embedded(bytes).exif else { return Vec::new() }; + let data = crate::strip_exif_header(&exif); + let Ok(t) = Tiff::parse(data) else { return Vec::new() }; + let mut rows = tag_rows(&t); + panasonic_lens_row(data, &t, &mut rows); + rows +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rational_has_no_trailing_dot() { + assert_eq!(rational(1.0, 100000.0), "0"); + assert_eq!(rational(-1.0, 100000.0), "0"); + assert_eq!(rational(-1.0, 1e10), "0"); + assert_eq!(rational(1000001.0, 100000.0), "10"); + assert_eq!(rational(1.0, 3.0), "0.3333"); + assert_eq!(rational(5.0, 2.0), "2.5"); + } + + /// Rational lists were joined whole, unlike every other list: a tag of millions of + /// fractions became a string as long in the tag panel. + #[test] + fn rational_lists_show_sixteen_values_like_other_lists() { + let first16 = (1..=16).map(|i| i.to_string()).collect::>().join(", "); + let many: Vec<(u32, u32)> = (1..=1000).map(|i| (i, 1)).collect(); + assert_eq!(value_text("EXIF", 0x9999, &Value::Rational(many)), format!("{first16} … (1000 values)")); + let signed: Vec<(i32, i32)> = (1..=1000).map(|i| (-i, 1)).collect(); + assert!(value_text("EXIF", 0x9999, &Value::SRational(signed)).ends_with("-16 … (1000 values)")); + assert_eq!(value_text("EXIF", 0x9999, &Value::Rational(vec![(1, 2), (3, 4)])), "0.5, 0.75"); + } + + #[test] + fn rows_from_written_exif() { + let m = crate::Metadata { + make: Some("Maker".into()), + model: Some("Model X".into()), + exposure_time: Some(1.0 / 250.0), + f_number: Some(2.8), + iso: Some(400), + focal_length: Some(35.0), + gps: Some(crate::Gps { latitude: 51.5, longitude: -0.12, altitude: Some(20.0) }), + ..Default::default() + }; + let exif = crate::write_exif(&m); + let t = Tiff::parse(crate::strip_exif_header(&exif)).unwrap(); + let rows = tag_rows(&t); + let get = |n: &str| rows.iter().find(|r| r.name == n).map(|r| r.value.clone()); + assert_eq!(get("Make").as_deref(), Some("Maker")); + assert_eq!(get("Exposure Time").as_deref(), Some("1/250 s")); + assert_eq!(get("F-Number").as_deref(), Some("f/2.8")); + assert_eq!(get("Focal Length").as_deref(), Some("35 mm")); + assert_eq!(get("ISO Speed").as_deref(), Some("400")); + assert!(rows.iter().any(|r| r.group == "GPS" && r.name == "GPS Latitude"), "{rows:?}"); + assert!(!rows.iter().any(|r| r.tag == 0x8769), "IFD pointers are left out"); + } + + fn entry(tag: u16, ty: u16, count: u32, val: u32) -> Vec { + let mut e = Vec::new(); + e.extend(tag.to_le_bytes()); + e.extend(ty.to_le_bytes()); + e.extend(count.to_le_bytes()); + e.extend(val.to_le_bytes()); + e + } + + fn ifd(entries: &[Vec]) -> Vec { + let mut b = (entries.len() as u16).to_le_bytes().to_vec(); + entries.iter().for_each(|e| b.extend(e)); + b.extend(0u32.to_le_bytes()); + b + } + + /// A little-endian raw with a thumbnail IFD0 (`Compression` 1), a CFA sub-IFD (`Compression` + /// 34713) and, when `nef` is set, a Nikon maker note whose NEFCompression is that value. + fn synthetic_raw(nef: Option) -> Vec { + let make = b"NIKON CORPORATION"; + let mut note = b"NikonII*".to_vec(); + note.extend(ifd(&[entry(0x0093, 3, 1, nef.unwrap_or(0) as u32)])); + let ifd0_len = 2 + 4 * 12 + 4; + let sub_at = 8 + ifd0_len; + let sub = ifd(&[entry(0x0103, 3, 1, 34713), entry(0x0106, 3, 1, 32803)]); + let exif_at = sub_at + sub.len(); + let exif_len = 2 + 12 + 4; + let make_at = exif_at + exif_len; + let note_at = make_at + make.len(); + let mut b = b"II*".to_vec(); + b.extend(ifd(&[ + entry(0x0103, 3, 1, 1), + entry(0x010f, 2, make.len() as u32, make_at as u32), + entry(0x014a, 4, 1, sub_at as u32), + entry(0x8769, 4, 1, exif_at as u32), + ])); + b.extend(sub); + b.extend(ifd(&[entry(0x927c, 7, note.len() as u32, if nef.is_some() { note_at as u32 } else { 0 })])); + b.extend(make); + b.extend(note); + b + } + + fn compression(rows: &[TagRow]) -> Vec<&str> { + rows.iter().filter(|r| r.group == "TIFF" && r.tag == 0x0103).map(|r| r.value.as_str()).collect() + } + + #[test] + fn raw_compression_is_the_sensor_images_not_the_thumbnails() { + let b = synthetic_raw(None); + assert_eq!(compression(&file_tag_rows(&b)), ["Nikon NEF Compressed"]); + } + + #[test] + fn nef_compression_is_spelled_out_from_the_maker_note() { + for (v, words) in [(3, "Lossless"), (10, "Packed 14 bits"), (2, "Uncompressed"), (14, "High Efficiency*")] { + let b = synthetic_raw(Some(v)); + assert_eq!(compression(&file_tag_rows(&b)), [words]); + } + } + + #[test] + fn dng_raw_sub_ifd_jpeg_is_lossless_but_a_plain_jpeg_tiff_is_not() { + let b = synthetic_raw(None); + // switch the sensor sub-IFD's Compression from 34713 to 7 + let pos = b.windows(12).position(|w| w == entry(0x0103, 3, 1, 34713).as_slice()).unwrap(); + let mut dng = b.clone(); + dng[pos + 8] = 7; + dng[pos + 9] = 0; + assert_eq!(compression(&file_tag_rows(&dng)), ["Lossless JPEG"]); + let plain = [b"II*".to_vec(), ifd(&[entry(0x0103, 3, 1, 7)])].concat(); + assert_eq!(compression(&file_tag_rows(&plain)), ["JPEG"]); + } + + #[test] + fn plain_tiff_keeps_its_own_compression() { + let b = [b"II*".to_vec(), ifd(&[entry(0x0103, 3, 1, 8)])].concat(); + assert_eq!(compression(&file_tag_rows(&b)), ["Deflate"]); + } + + #[test] + fn panasonic_lens_is_listed_from_the_maker_note() { + let lens = |rows: &[TagRow]| rows.iter().filter(|r| r.group == "Panasonic" && r.tag == 0x0051).map(|r| r.value.clone()).collect::>(); + // a RW2: the note is only in the JpgFromRaw's Exif + let rw2 = crate::exif::tests::panasonic_rw2("LUMIX S 85/F1.8"); + assert_eq!(lens(&file_tag_rows(&rw2)), ["LUMIX S 85/F1.8"]); + // a JPEG: the note is in its own Exif + let exif = crate::exif::tests::panasonic_exif(craft_tiff::ByteOrder::Big, Some("LUMIX G 25/F1.7"), None); + let mut jpg = vec![0xff, 0xd8, 0xff, 0xe1]; + jpg.extend_from_slice(&((exif.len() + 8) as u16).to_be_bytes()); + jpg.extend(b"Exif\0\0"); + jpg.extend(exif); + jpg.extend_from_slice(&[0xff, 0xda, 0, 2, 0xff, 0xd9]); + assert_eq!(lens(&file_tag_rows(&jpg)), ["LUMIX G 25/F1.7"]); + // no Panasonic note, no row + assert!(lens(&file_tag_rows(&synthetic_raw(Some(3)))).is_empty()); + } +} diff --git a/crates/metadata/src/xmp.rs b/crates/metadata/src/xmp.rs new file mode 100644 index 000000000..e2af65fb5 --- /dev/null +++ b/crates/metadata/src/xmp.rs @@ -0,0 +1,813 @@ +//! XMP packets (ISO 16684-1 / Adobe XMP Specification Part 1–2): read and write. +//! +//! Reading builds a small namespace-resolved tree with `quick-xml`, then collects every property of every +//! `rdf:Description` (attribute form, element form, `rdf:Seq`/`rdf:Bag`/`rdf:Alt` arrays, `rdf:parseType="Resource"` +//! and nested-description structs, flattened as `parent/field`). Prefixes are canonicalised by namespace URI, so +//! a packet that binds `http://purl.org/dc/elements/1.1/` to `foo:` still yields `dc:title`. +//! +//! Writing emits the interchange subset in standard namespaces (dc, xmp, photoshop, exif, exifEX, tiff, lr) and +//! any simple properties of one further namespace the caller names ([`XmpWrite`]). + +use crate::geom::{Point, Rect}; +use crate::{DateTime, Flash, Gps, Metadata, Orientation, Region, RegionKind, parse_number}; +use quick_xml::escape::{escape, partial_escape}; +use quick_xml::events::Event; +use std::collections::BTreeMap; + +/// A namespace beyond the standard ones, such as a program's own: properties in it are reported +/// under `prefix`, whatever prefix the packet binds the URI to. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct XmpNamespace<'a> { + pub prefix: &'a str, + pub uri: &'a str, +} + +/// What [`write_xmp_with`] writes beside the metadata. +#[derive(Clone, Copy, Debug)] +pub struct XmpWrite<'a> { + /// The `x:xmptk` attribute: the name of the writing program. + pub toolkit: &'a str, + /// The namespace of `properties`. Without one, `properties` are not written. + pub namespace: Option>, + /// Simple properties in `namespace`, as `(local name, text)`. A name that is not ASCII letters, + /// digits and `_` is left out. + pub properties: &'a [(&'a str, &'a str)], +} + +impl Default for XmpWrite<'_> { + fn default() -> Self { + Self { toolkit: "craft-metadata", namespace: None, properties: &[] } + } +} + +const NAMESPACES: &[(&str, &str)] = &[ + ("x", "adobe:ns:meta/"), + ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"), + ("dc", "http://purl.org/dc/elements/1.1/"), + ("xmp", "http://ns.adobe.com/xap/1.0/"), + ("xmpRights", "http://ns.adobe.com/xap/1.0/rights/"), + ("photoshop", "http://ns.adobe.com/photoshop/1.0/"), + ("exif", "http://ns.adobe.com/exif/1.0/"), + ("exifEX", "http://cipa.jp/exif/1.0/"), + ("tiff", "http://ns.adobe.com/tiff/1.0/"), + ("aux", "http://ns.adobe.com/exif/1.0/aux/"), + ("lr", "http://ns.adobe.com/lightroom/1.0/"), + ("Iptc4xmpCore", "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/"), + // Read-only: develop settings written by other raw developers. + ("crs", CRS_NS), + // Read-only: face/pet/focus regions written by Lightroom, digiKam, Picasa and others (MWG Region + // Guidelines v2.0). Canonicalising these three lets a packet use any prefix for them. + ("mwg-rs", "http://www.metadataworkinggroup.com/schemas/regions/"), + ("stArea", "http://ns.adobe.com/xmp/sType/Area#"), + ("stDim", "http://ns.adobe.com/xap/1.0/sType/Dimensions#"), +]; + +/// The camera-raw-settings namespace URI (prefix `crs`), read for interchange only. +pub const CRS_NS: &str = "http://ns.adobe.com/camera-raw-settings/1.0/"; + +/// XMP read failure. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum XmpError { + Xml(String), + /// Nesting / size limits exceeded. + TooComplex, +} + +impl std::fmt::Display for XmpError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + XmpError::Xml(e) => write!(f, "XMP is not well-formed XML: {e}"), + XmpError::TooComplex => write!(f, "XMP packet too deeply nested or too large"), + } + } +} + +impl std::error::Error for XmpError {} + +/// Result of [`parse_xmp`]. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct XmpData { + pub metadata: Metadata, + /// Every property found, keyed `prefix:name` (struct fields as `prefix:name/prefix:field`); arrays keep + /// item order, language alternatives put `x-default` first. Foreign namespaces that reuse a recognized + /// prefix use `Q{hex-encoded namespace URI}local` instead, so they cannot impersonate known properties. + pub properties: BTreeMap>, + /// Every top-level property with its full structure (arrays of structs such as local + /// corrections, which [`XmpData::properties`] flattens). + pub values: BTreeMap, +} + +impl XmpData { + /// The pixel size the packet's regions were drawn on (`mwg-rs:AppliedToDimensions`), unless one of them says how + /// the photo was turned (`mwg-rs:Rotation`: Lightroom's boxes, which the reader already turns upright). It tells + /// boxes drawn on the photo as stored from boxes drawn on it upright: see [`crate::upright_regions`]. + pub fn regions_drawn_on(&self) -> Option<(f64, f64)> { + let regions = self.values.get("mwg-rs:Regions")?; + let turned = regions.field("mwg-rs:RegionList").is_some_and(|l| l.items().iter().any(|r| r.field("mwg-rs:Rotation").is_some())); + if turned { None } else { applied_to(regions) } + } +} + +/// A structured XMP value. +#[derive(Clone, Debug, PartialEq)] +pub enum XmpValue { + Text(String), + Array(Vec), + Struct(BTreeMap), +} + +impl XmpValue { + /// A struct field (`prefix:name`). + pub fn field(&self, k: &str) -> Option<&XmpValue> { + match self { + XmpValue::Struct(m) => m.get(k), + _ => None, + } + } + pub fn text(&self) -> Option<&str> { + match self { + XmpValue::Text(s) => Some(s.trim()), + XmpValue::Array(a) => a.first().and_then(XmpValue::text), + XmpValue::Struct(_) => None, + } + } + /// Array items (a lone value is a one-item array). + pub fn items(&self) -> &[XmpValue] { + match self { + XmpValue::Array(a) => a, + other => std::slice::from_ref(other), + } + } +} + +fn node_struct(n: &Node) -> XmpValue { + let mut m = BTreeMap::new(); + for (k, v) in &n.attrs { + if !is_rdf_meta_attr(k) { + m.insert(k.clone(), XmpValue::Text(v.clone())); + } + } + for c in &n.children { + m.insert(c.name.clone(), node_value(c)); + } + XmpValue::Struct(m) +} + +fn node_value(n: &Node) -> XmpValue { + if let Some(arr) = n.children.iter().find(|c| matches!(c.name.as_str(), "rdf:Seq" | "rdf:Bag" | "rdf:Alt")) { + return XmpValue::Array(arr.children.iter().filter(|li| li.name == "rdf:li").map(node_value).collect()); + } + if let Some(d) = n.children.iter().find(|c| c.name == "rdf:Description") { + return node_struct(d); + } + if !n.children.is_empty() || n.attrs.iter().any(|(k, _)| !is_rdf_meta_attr(k)) { + return node_struct(n); + } + XmpValue::Text(n.attr("rdf:resource").map(str::to_string).unwrap_or_else(|| n.text.trim().to_string())) +} + +#[derive(Debug, Default)] +struct Node { + name: String, + attrs: Vec<(String, String)>, + children: Vec, + text: String, +} + +impl Node { + fn attr(&self, n: &str) -> Option<&str> { + self.attrs.iter().find(|(k, _)| k == n).map(|(_, v)| v.as_str()) + } +} + +const MAX_DEPTH: usize = 64; +const MAX_NODES: usize = 200_000; +// Expanding a long namespace URI on many short names must not amplify a small packet without bound. +pub(crate) const MAX_OPAQUE_NAME_BYTES: usize = 8 * 1024 * 1024; + +/// Most text a converter's flattened property keys may add up to. Keys repeat their whole ancestor +/// path (`parent/field`), so a document with a long nested name and a wide child fan-out otherwise +/// retains its prefix once per property: node limits alone cannot bound that duplication. +const MAX_KEY_BYTES: usize = 1 << 20; + +/// Names canonicalised by namespace URI (see [`NAMESPACES`]); conflicting foreign prefixes are opaque. +pub(crate) fn canonical_name( + qname: &str, + scopes: &[Vec<(String, String)>], + extra: &[XmpNamespace], + opaque_budget: &mut usize, +) -> Result { + let known = || NAMESPACES.iter().copied().chain(extra.iter().map(|n| (n.prefix, n.uri))); + let (prefix, local) = qname.split_once(':').unwrap_or(("", qname)); + if prefix == "xml" || prefix == "xmlns" { + return Ok(qname.to_string()); + } + let uri = scopes.iter().rev().flat_map(|s| s.iter().rev()).find(|(p, _)| p == prefix).map(|(_, u)| u.as_str()); + Ok(match uri { + Some(u) => match known().find(|(_, nu)| *nu == u) { + Some((p, _)) => format!("{p}:{local}"), + None if known().any(|(p, _)| p == prefix) => { + // A foreign `app:settings` must never match the caller's `app:*` ownership rule (nor may a + // foreign `xmp:Rating` become a rating). Braces cannot occur in an XML QName; + // hex keeps URI slashes out of the flattened property's `/` path separators. + let size = + u.len().checked_mul(2).and_then(|n| n.checked_add(3)).and_then(|n| n.checked_add(local.len())).ok_or(XmpError::TooComplex)?; + *opaque_budget = opaque_budget.checked_sub(size).ok_or(XmpError::TooComplex)?; + let mut key = String::with_capacity(size); + key.push_str("Q{"); + for byte in u.bytes() { + for nibble in [byte >> 4, byte & 15] { + key.push(char::from(if nibble < 10 { b'0' + nibble } else { b'a' + (nibble - 10) })); + } + } + key.push('}'); + key.push_str(local); + key + } + None if prefix.is_empty() => local.to_string(), + None => qname.to_string(), + }, + None => qname.to_string(), + }) +} + +fn entity(name: &str) -> Option { + Some(match name { + "lt" => '<', + "gt" => '>', + "amp" => '&', + "quot" => '"', + "apos" => '\'', + _ => { + let n = name.strip_prefix('#')?; + let v = match n.strip_prefix(['x', 'X']) { + Some(h) if h.bytes().all(|b| b.is_ascii_hexdigit()) => u32::from_str_radix(h, 16).ok()?, + Some(_) => return None, + None if n.bytes().all(|b| b.is_ascii_digit()) => n.parse().ok()?, + None => return None, + }; + char::from_u32(v).filter(|&c| is_xml_char(c))? + } + }) +} + +/// The XML 1.0 `Char` production (the surrogate range can't occur in a `char`). +fn is_xml_char(c: char) -> bool { + matches!(c, '\t' | '\n' | '\r') || (c >= '\u{20}' && c != '\u{FFFE}' && c != '\u{FFFF}') +} + +/// Drop the characters XML 1.0 forbids, which strict parsers (Adobe XMP Toolkit, expat, libxml2) reject the +/// whole document for. +fn xml_safe(s: &str) -> String { + s.chars().filter(|&c| is_xml_char(c)).collect() +} + +/// Index of the `;` that ends a reference starting at `rest` (its `&`). The scan stops at the first +/// byte that cannot appear in a name or a numeric reference, so zero-padded references such as +/// `a` are found and each run of name bytes is read once, not once per `&`. +fn entity_end(rest: &str) -> Option { + let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count(); + (rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1) +} + +pub(crate) fn unescape_attr(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + let mut rest = s; + while let Some(i) = rest.find('&') { + out.push_str(&rest[..i]); + rest = &rest[i..]; + match entity_end(rest).and_then(|j| entity(&rest[1..j]).map(|c| (c, j))) { + Some((c, j)) => { + out.push(c); + rest = &rest[j + 1..]; + } + None => { + out.push('&'); + rest = &rest[1..]; + } + } + } + out.push_str(rest); + out +} + +fn parse_tree(s: &str, extra: &[XmpNamespace]) -> Result { + let mut reader = quick_xml::Reader::from_str(s); + reader.config_mut().check_end_names = false; + let mut stack: Vec = vec![Node { name: "#root".into(), ..Default::default() }]; + let mut scopes: Vec> = vec![vec![("xml".into(), "http://www.w3.org/XML/1998/namespace".into())]]; + let mut count = 0usize; + let mut opaque_budget = MAX_OPAQUE_NAME_BYTES; + loop { + let ev = reader.read_event().map_err(|e| XmpError::Xml(e.to_string()))?; + match ev { + Event::Start(ref e) | Event::Empty(ref e) => { + count += 1; + if count > MAX_NODES || stack.len() > MAX_DEPTH { + return Err(XmpError::TooComplex); + } + let mut scope = Vec::new(); + let mut raw_attrs = Vec::new(); + for a in e.attributes().with_checks(false).flatten() { + let k = String::from_utf8_lossy(a.key.as_ref()).into_owned(); + let v = unescape_attr(&String::from_utf8_lossy(&a.value)); + if k == "xmlns" { + scope.push((String::new(), v)); + } else if let Some(p) = k.strip_prefix("xmlns:") { + scope.push((p.to_string(), v)); + } else { + raw_attrs.push((k, v)); + } + } + scopes.push(scope); + let name = canonical_name(&String::from_utf8_lossy(e.name().as_ref()), &scopes, extra, &mut opaque_budget)?; + let attrs = raw_attrs.into_iter().map(|(k, v)| Ok((canonical_name(&k, &scopes, extra, &mut opaque_budget)?, v))).collect::>( + )?; + let node = Node { name, attrs, ..Default::default() }; + if matches!(ev, Event::Empty(_)) { + scopes.pop(); + if let Some(parent) = stack.last_mut() { + parent.children.push(node); + } + } else { + stack.push(node); + } + } + Event::End(_) if stack.len() > 1 => { + let Some(n) = stack.pop() else { return Err(XmpError::Xml("end tag without an open element".into())) }; + scopes.pop(); + if let Some(parent) = stack.last_mut() { + parent.children.push(n); + } + } + Event::Text(t) => { + if let Some(top) = stack.last_mut() { + top.text.push_str(&t.decode().map_err(|e| XmpError::Xml(e.to_string()))?); + } + } + Event::CData(t) => { + if let Some(top) = stack.last_mut() { + top.text.push_str(&String::from_utf8_lossy(&t)); + } + } + Event::GeneralRef(r) => { + let name = String::from_utf8_lossy(&r).into_owned(); + if let Some(top) = stack.last_mut() { + match entity(&name) { + Some(c) => top.text.push(c), + None => { + top.text.push('&'); + top.text.push_str(&name); + top.text.push(';'); + } + } + } + } + Event::Eof => break, + _ => {} + } + } + // close unterminated elements leniently + while stack.len() > 1 { + let Some(n) = stack.pop() else { return Err(XmpError::Xml("unterminated element without a node".into())) }; + let Some(parent) = stack.last_mut() else { return Err(XmpError::Xml("unterminated element without a parent".into())) }; + parent.children.push(n); + } + stack.pop().ok_or_else(|| XmpError::Xml("xmp packet without a root element".into())) +} + +fn is_rdf_meta_attr(k: &str) -> bool { + matches!(k, "rdf:about" | "rdf:ID" | "rdf:nodeID" | "xml:lang" | "rdf:parseType" | "rdf:resource" | "rdf:datatype") || k.starts_with("xmlns") +} + +fn collect_description(d: &Node, prefix: &str, props: &mut BTreeMap>, budget: &mut usize) { + for (k, v) in &d.attrs { + // the budget is checked before the path is built, not after + if is_rdf_meta_attr(k) || *budget < prefix.len().saturating_add(k.len()) { + continue; + } + let key = format!("{prefix}{k}"); + if take_key_bytes(&key, budget) { + props.entry(key).or_default().push(v.clone()); + } + } + for c in &d.children { + collect_property(c, prefix, props, budget); + } +} + +/// Charge one flattened key's length to the key budget; `false` when the budget is spent (the +/// property is then left out, like other elements a limit has cut). +fn take_key_bytes(key: &str, budget: &mut usize) -> bool { + let n = key.len(); + if *budget < n { + return false; + } + *budget -= n; + true +} + +fn collect_property(p: &Node, prefix: &str, props: &mut BTreeMap>, budget: &mut usize) { + // a path that can't fit the budget isn't built (a long ancestor path shared by many children + // would otherwise be copied for each of them after the budget is spent) + if *budget < prefix.len().saturating_add(p.name.len()) { + return; + } + let key = format!("{prefix}{}", p.name); + if let Some(arr) = p.children.iter().find(|c| matches!(c.name.as_str(), "rdf:Seq" | "rdf:Bag" | "rdf:Alt")) { + let mut items: Vec<(bool, String)> = arr + .children + .iter() + .filter(|li| li.name == "rdf:li") + .map(|li| { + (li.attr("xml:lang") == Some("x-default"), li.attr("rdf:resource").map(str::to_string).unwrap_or_else(|| li.text.trim().to_string())) + }) + .collect(); + if arr.name == "rdf:Alt" { + items.sort_by_key(|(default, _)| !*default); + } + if take_key_bytes(&key, budget) { + props.entry(key).or_default().extend(items.into_iter().map(|(_, v)| v)); + } + return; + } + let struct_prefix = format!("{key}/"); + // a struct's path is charged like a key before its fields copy it: that work too is bounded + // by the budget, kept fields or not + let is_struct = p.attr("rdf:parseType") == Some("Resource") || p.children.iter().any(|c| c.name == "rdf:Description"); + if is_struct && !take_key_bytes(&key, budget) { + return; + } + if p.attr("rdf:parseType") == Some("Resource") { + for c in &p.children { + collect_property(c, &struct_prefix, props, budget); + } + return; + } + if let Some(d) = p.children.iter().find(|c| c.name == "rdf:Description") { + collect_description(d, &struct_prefix, props, budget); + return; + } + let field_attrs: Vec<&(String, String)> = p.attrs.iter().filter(|(k, _)| !is_rdf_meta_attr(k)).collect(); + if !field_attrs.is_empty() && p.children.is_empty() && p.text.trim().is_empty() { + for (k, v) in field_attrs { + let full = format!("{struct_prefix}{k}"); + if take_key_bytes(&full, budget) { + props.entry(full).or_default().push(v.clone()); + } + } + return; + } + let v = p.attr("rdf:resource").map(str::to_string).unwrap_or_else(|| p.text.trim().to_string()); + if take_key_bytes(&key, budget) { + props.entry(key).or_default().push(v); + } +} + +fn find_descriptions<'a>(n: &'a Node, out: &mut Vec<&'a Node>) { + if n.name == "rdf:Description" { + out.push(n); + return; + } + for c in &n.children { + find_descriptions(c, out); + } +} + +/// XMP GPS coordinate `DDD,MM,SSk` or `DDD,MM.mmk` (k = N/S/E/W) → signed degrees. +fn parse_gps_coord(s: &str) -> Option { + let s = s.trim(); + let dir = s.chars().last()?; + let body = &s[..s.len() - dir.len_utf8()]; + let parts: Vec = body.split(',').map(|p| p.trim().parse::().ok()).collect::>()?; + let v = match parts.as_slice() { + [d] => *d, + [d, m] => d + m / 60.0, + [d, m, sec] => d + m / 60.0 + sec / 3600.0, + _ => return None, + }; + let v = match dir.to_ascii_uppercase() { + 'N' | 'E' => v, + 'S' | 'W' => -v, + _ => return None, + }; + v.is_finite().then_some(v) +} + +/// `mwg-rs:Regions` → every region in its `RegionList`, converted to the full-image normalized frame. +/// Malformed or out-of-range entries are skipped rather than rejecting the whole list: one bad region +/// (from Lightroom, digiKam, Picasa, or a hand-edited file) shouldn't hide the rest. +fn parse_regions(regions: &XmpValue) -> Vec { + // `AppliedToDimensions` documents the pixel size regions were authored against. Here it's only consulted + // for the rare region whose own Area is in pixel units instead of MWG's normalized default below. + let px_dims = applied_to(regions); + let Some(list) = regions.field("mwg-rs:RegionList") else { return Vec::new() }; + list.items().iter().filter_map(|item| parse_one_region(item, px_dims)).collect() +} + +/// `mwg-rs:AppliedToDimensions` of a `mwg-rs:Regions` struct: (width, height) in pixels, both positive. +fn applied_to(regions: &XmpValue) -> Option<(f64, f64)> { + regions.field("mwg-rs:AppliedToDimensions").and_then(|d| { + let w = d.field("stDim:w").and_then(XmpValue::text).and_then(parse_number)?; + let h = d.field("stDim:h").and_then(XmpValue::text).and_then(parse_number)?; + (w > 0.0 && h > 0.0).then_some((w, h)) + }) +} + +fn parse_one_region(item: &XmpValue, px_dims: Option<(f64, f64)>) -> Option { + let area = item.field("mwg-rs:Area")?; + let num_attr = |k: &str| area.field(k).and_then(XmpValue::text).and_then(parse_number); + // stArea:x/y are the region's CENTER, not its top-left corner (MWG Region Guidelines §Area). + let (cx, cy, w, h) = (num_attr("stArea:x")?, num_attr("stArea:y")?, num_attr("stArea:w")?, num_attr("stArea:h")?); + let finite_and_sane = [cx, cy, w, h].iter().all(|v| v.is_finite() && v.abs() <= 1.0e6); + if !finite_and_sane || w <= 0.0 || h <= 0.0 { + return None; + } + // `stArea:unit` is "normalized" (a fraction of AppliedToDimensions) in every writer we know of, and + // that needs no cross-referencing at all. Only a region that explicitly says "pixel" gets divided + // down, and only if AppliedToDimensions is there to divide by; otherwise it's dropped rather than + // stored as a nonsense fraction. + let is_pixels = area.field("stArea:unit").and_then(XmpValue::text).is_some_and(|u| u.eq_ignore_ascii_case("pixel")); + let (cx, cy, w, h) = match (is_pixels, px_dims) { + (false, _) => (cx, cy, w, h), + (true, Some((dw, dh))) => (cx / dw, cy / dh, w / dw, h / dh), + (true, None) => return None, + }; + // Lightroom Classic stores the box of a photo with Exif orientation 3 / 6 / 8 in the sensor's own + // frame and writes the orientation as the region's `mwg-rs:Rotation` (π for 3, −π/2 for 6, +π/2 for + // 8; 0 for 1) while `AppliedToDimensions` names the upright size. Turn it into the upright frame. + // MWG has no way to say "mirrored", so orientations 2/4/5/7 can't be told apart from their unmirrored + // twins here; boxes on mirrored photos are taken as written . + let rotation = item.field("mwg-rs:Rotation").and_then(XmpValue::text).and_then(parse_number).filter(|r| r.is_finite()); + let (quarter, half) = (std::f64::consts::FRAC_PI_2, std::f64::consts::PI); + let (cx, cy, w, h) = match rotation { + Some(r) if (r + quarter).abs() < 0.05 => (1.0 - cy, cx, h, w), + Some(r) if (r - quarter).abs() < 0.05 => (cy, 1.0 - cx, h, w), + Some(r) if (r.abs() - half).abs() < 0.05 => (1.0 - cx, 1.0 - cy, w, h), + _ => (cx, cy, w, h), + }; + // Keep only the part of the box inside the photo: a region hanging off an edge (common for faces + // cut by the frame) is clipped, and one entirely outside it (hostile or stale) is dropped, so every + // stored rect is within 0..1 and non-empty. + let rect = Rect::from_center(Point::new(cx, cy), w, h).intersect(&Rect::UNIT); + if rect.is_empty() { + return None; + } + let name = item.field("mwg-rs:Name").and_then(XmpValue::text).map(str::to_string).filter(|s| !s.is_empty()); + let description = item.field("mwg-rs:Description").and_then(XmpValue::text).map(str::to_string).filter(|s| !s.is_empty()); + let kind = match item.field("mwg-rs:Type").and_then(XmpValue::text).unwrap_or("") { + t if t.eq_ignore_ascii_case("face") => RegionKind::Face, + t if t.eq_ignore_ascii_case("pet") => RegionKind::Pet, + t if t.eq_ignore_ascii_case("focus") => RegionKind::Focus, + t if t.eq_ignore_ascii_case("barcode") => RegionKind::BarCode, + t => RegionKind::Other(t.to_string()), + }; + Some(Region { rect, kind, name, description }) +} + +fn fmt_gps_coord(v: f64, pos: char, neg: char) -> String { + let a = v.abs(); + let mut d = a.trunc(); + let mut m = ((a - d) * 60.0 * 1e6).round() / 1e6; + if m >= 60.0 { + d += 1.0; + m = 0.0; + } + format!("{},{:.6}{}", d as i64, m, if v < 0.0 { neg } else { pos }) +} + +/// Parse an XMP packet (with or without the `` wrapper / `x:xmpmeta` element). +pub fn parse_xmp(s: &str) -> Result { + parse_xmp_with(s, &[]) +} + +/// [`parse_xmp`] that also knows the namespaces in `extra` by their URI. +pub fn parse_xmp_with(s: &str, extra: &[XmpNamespace]) -> Result { + let s = s.trim_start_matches('\u{feff}'); + let tree = parse_tree(s, extra)?; + let mut descs = Vec::new(); + find_descriptions(&tree, &mut descs); + let mut props = BTreeMap::new(); + let mut values = BTreeMap::new(); + // One budget for the whole document: several descriptions cannot each spend a fresh one. + let mut key_budget = MAX_KEY_BYTES; + for d in descs { + collect_description(d, "", &mut props, &mut key_budget); + if let XmpValue::Struct(m) = node_struct(d) { + values.extend(m); + } + } + let first = |k: &str| props.get(k).and_then(|v| v.first()).map(|s| s.trim().to_string()).filter(|s| !s.is_empty()); + let num = |k: &str| first(k).and_then(|s| parse_number(&s)); + let mut m = Metadata { + make: first("tiff:Make"), + model: first("tiff:Model"), + serial_number: first("exifEX:BodySerialNumber").or_else(|| first("aux:SerialNumber")), + software: first("xmp:CreatorTool"), + lens_make: first("exifEX:LensMake"), + lens_model: first("exifEX:LensModel").or_else(|| first("aux:Lens")), + lens_serial_number: first("exifEX:LensSerialNumber").or_else(|| first("aux:LensSerialNumber")), + exposure_time: num("exif:ExposureTime").filter(|v| *v > 0.0), + f_number: num("exif:FNumber").filter(|v| *v > 0.0), + focal_length: num("exif:FocalLength").filter(|v| *v > 0.0), + focal_length_35mm: num("exif:FocalLengthIn35mmFilm").filter(|v| *v > 0.0), + exposure_bias: num("exif:ExposureBiasValue"), + exposure_program: num("exif:ExposureProgram").map(|v| v as u16), + metering_mode: num("exif:MeteringMode").map(|v| v as u16), + white_balance: num("exif:WhiteBalance").map(|v| v as u16), + orientation: num("tiff:Orientation").map(|v| v as u16).filter(|v| (1..=8).contains(v)).map(Orientation::from_exif), + width: num("tiff:ImageWidth").filter(|v| *v >= 0.0).or_else(|| num("exif:PixelXDimension").filter(|v| *v >= 0.0)).map(|v| v as u32), + height: num("tiff:ImageLength").filter(|v| *v >= 0.0).or_else(|| num("exif:PixelYDimension").filter(|v| *v >= 0.0)).map(|v| v as u32), + title: first("dc:title"), + caption: first("dc:description"), + alt_text: first("Iptc4xmpCore:AltTextAccessibility"), + extended_description: first("Iptc4xmpCore:ExtDescrAccessibility"), + sublocation: first("Iptc4xmpCore:Location"), + city: first("photoshop:City"), + state: first("photoshop:State"), + country: first("photoshop:Country"), + copyright: first("dc:rights"), + copyright_marked: first("xmpRights:Marked").and_then(|s| match s.to_ascii_lowercase().as_str() { + "true" => Some(true), + "false" => Some(false), + _ => None, + }), + usage_terms: first("xmpRights:UsageTerms"), + copyright_url: first("xmpRights:WebStatement"), + rating: num("xmp:Rating").map(|v| v.round().clamp(-1.0, 5.0) as i8), + label: first("xmp:Label"), + ..Default::default() + }; + m.iso = [num("exif:ISOSpeedRatings"), num("exifEX:PhotographicSensitivity")].into_iter().flatten().find(|v| *v > 0.0).map(|v| v as u32); + if let Some(spec) = props.get("exifEX:LensSpecification").filter(|v| v.len() == 4) { + let v: Vec = spec.iter().filter_map(|s| parse_number(s)).collect(); + if v.len() == 4 { + m.lens_spec = Some([v[0], v[1], v[2], v[3]]); + } + } + if let Some(creators) = props.get("dc:creator").filter(|c| !c.is_empty()) { + m.artist = Some(creators.join("; ")); + } + m.keywords = props.get("dc:subject").cloned().unwrap_or_default().into_iter().filter(|k| !k.is_empty()).collect(); + m.hierarchical_keywords = props.get("lr:hierarchicalSubject").cloned().unwrap_or_default().into_iter().filter(|k| !k.is_empty()).collect(); + m.capture_time = + ["exif:DateTimeOriginal", "photoshop:DateCreated", "xmp:CreateDate"].iter().find_map(|k| first(k).and_then(|s| DateTime::parse_iso(&s))); + if let Some(fired) = first("exif:Flash/exif:Fired") { + let b = |k: &str| first(k).is_some_and(|s| s.eq_ignore_ascii_case("true")); + let n = |k: &str| num(k).unwrap_or(0.0) as u16; + let fired = fired.eq_ignore_ascii_case("true"); + let raw = fired as u16 + | (n("exif:Flash/exif:Return") & 3) << 1 + | (n("exif:Flash/exif:Mode") & 3) << 3 + | (b("exif:Flash/exif:Function") as u16) << 5 + | (b("exif:Flash/exif:RedEyeMode") as u16) << 6; + m.flash = Some(Flash { fired, raw }); + } + if let (Some(lat), Some(lon)) = + (first("exif:GPSLatitude").and_then(|s| parse_gps_coord(&s)), first("exif:GPSLongitude").and_then(|s| parse_gps_coord(&s))) + && (-90.0..=90.0).contains(&lat) + && (-180.0..=180.0).contains(&lon) + { + let altitude = num("exif:GPSAltitude").map(|a| if first("exif:GPSAltitudeRef").as_deref() == Some("1") { -a } else { a }); + m.gps = Some(Gps { latitude: lat, longitude: lon, altitude }); + } + m.regions = values.get("mwg-rs:Regions").map(parse_regions).unwrap_or_default(); + Ok(XmpData { metadata: m, properties: props, values }) +} + +fn frac(v: f64) -> String { + if v > 0.0 && v < 1.0 { + let inv = 1.0 / v; + if (inv - inv.round()).abs() < 1e-6 { + return format!("1/{}", inv.round() as u64); + } + } + let (n, d) = craft_tiff::writer::rational(v); + format!("{n}/{d}") +} + +fn sfrac(v: f64) -> String { + let (n, d) = craft_tiff::writer::srational(v); + format!("{n}/{d}") +} + +/// Serialise the interchange subset of `meta` as a complete XMP packet. +pub fn write_xmp(meta: &Metadata) -> String { + write_xmp_with(meta, &XmpWrite::default()) +} + +/// Like [`write_xmp`], with the writing program's name and its own simple properties. +pub fn write_xmp_with(meta: &Metadata, with: &XmpWrite) -> String { + let mut simple: Vec<(&str, String)> = Vec::new(); + let mut push = |k: &'static str, v: Option| { + if let Some(v) = v { + simple.push((k, v)); + } + }; + push("tiff:Make", meta.make.clone()); + push("tiff:Model", meta.model.clone()); + push("tiff:Orientation", meta.orientation.map(|o| o.to_exif().to_string())); + push("tiff:ImageWidth", meta.width.map(|v| v.to_string())); + push("tiff:ImageLength", meta.height.map(|v| v.to_string())); + push("xmp:CreatorTool", meta.software.clone()); + push("xmp:Rating", meta.rating.map(|v| v.to_string())); + push("xmp:Label", meta.label.clone()); + push("exif:DateTimeOriginal", meta.capture_time.map(|d| d.to_iso())); + push("photoshop:DateCreated", meta.capture_time.map(|d| d.to_iso())); + push("exif:ExposureTime", meta.exposure_time.map(frac)); + push("exif:FNumber", meta.f_number.map(frac)); + push("exif:FocalLength", meta.focal_length.map(frac)); + push("exif:FocalLengthIn35mmFilm", meta.focal_length_35mm.map(|v| format!("{}", v.round() as i64))); + push("exif:ExposureBiasValue", meta.exposure_bias.map(sfrac)); + push("exif:ExposureProgram", meta.exposure_program.map(|v| v.to_string())); + push("exif:MeteringMode", meta.metering_mode.map(|v| v.to_string())); + push("exif:WhiteBalance", meta.white_balance.map(|v| v.to_string())); + push("exifEX:BodySerialNumber", meta.serial_number.clone()); + push("exifEX:LensMake", meta.lens_make.clone()); + push("exifEX:LensModel", meta.lens_model.clone()); + push("exifEX:LensSerialNumber", meta.lens_serial_number.clone()); + push("Iptc4xmpCore:Location", meta.sublocation.clone()); + push("photoshop:City", meta.city.clone()); + push("photoshop:State", meta.state.clone()); + push("photoshop:Country", meta.country.clone()); + push("xmpRights:Marked", meta.copyright_marked.map(|m| if m { "True" } else { "False" }.to_string())); + push("xmpRights:WebStatement", meta.copyright_url.clone()); + if let Some(g) = meta.gps { + push("exif:GPSLatitude", Some(fmt_gps_coord(g.latitude, 'N', 'S'))); + push("exif:GPSLongitude", Some(fmt_gps_coord(g.longitude, 'E', 'W'))); + if let Some(a) = g.altitude { + push("exif:GPSAltitude", Some(frac(a.abs()))); + push("exif:GPSAltitudeRef", Some(if a < 0.0 { "1" } else { "0" }.into())); + } + } + + let mut x = String::new(); + x.push_str("\n"); + x.push_str(&format!("\n", escape(xml_safe(with.toolkit)))); + x.push_str(" \n"); + x.push_str(" \n"); + for (k, v) in &simple { + x.push_str(&format!(" <{k}>{}\n", escape(xml_safe(v)))); + } + for (k, v) in with.properties.iter().filter(|_| own.is_some()) { + let p = own.map_or("", |n| n.0); + if !k.is_empty() && k.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') { + // element content: quotes needn't be escaped, which keeps JSON readable + x.push_str(&format!(" <{p}:{k}>{}\n", partial_escape(xml_safe(v)))); + } + } + let array = |x: &mut String, k: &str, kind: &str, items: &[String], lang: bool| { + if items.is_empty() { + return; + } + x.push_str(&format!(" <{k}>\n \n")); + for it in items { + if lang { + x.push_str(&format!(" {}\n", escape(xml_safe(it)))); + } else { + x.push_str(&format!(" {}\n", escape(xml_safe(it)))); + } + } + x.push_str(&format!(" \n \n")); + }; + let one = |v: &Option| v.iter().cloned().collect::>(); + array(&mut x, "dc:title", "Alt", &one(&meta.title), true); + array(&mut x, "dc:description", "Alt", &one(&meta.caption), true); + array(&mut x, "Iptc4xmpCore:AltTextAccessibility", "Alt", &one(&meta.alt_text), true); + array(&mut x, "Iptc4xmpCore:ExtDescrAccessibility", "Alt", &one(&meta.extended_description), true); + array(&mut x, "dc:rights", "Alt", &one(&meta.copyright), true); + array(&mut x, "xmpRights:UsageTerms", "Alt", &one(&meta.usage_terms), true); + array(&mut x, "dc:creator", "Seq", &one(&meta.artist), false); + array(&mut x, "dc:subject", "Bag", &meta.keywords, false); + array(&mut x, "lr:hierarchicalSubject", "Bag", &meta.hierarchical_keywords, false); + array(&mut x, "exif:ISOSpeedRatings", "Seq", &meta.iso.map(|v| vec![v.to_string()]).unwrap_or_default(), false); + if let Some(s) = meta.lens_spec { + array(&mut x, "exifEX:LensSpecification", "Seq", &s.iter().map(|&v| frac(v)).collect::>(), false); + } + if let Some(f) = meta.flash { + let r = f.raw; + let tf = |b: bool| if b { "True" } else { "False" }; + x.push_str(&format!( + " \n {}\n {}\n {}\n {}\n {}\n \n", + tf(f.fired), + (r >> 1) & 3, + (r >> 3) & 3, + tf(r & 0x20 != 0), + tf(r & 0x40 != 0) + )); + } + x.push_str(" \n \n\n"); + x +} + +#[cfg(test)] +mod tests; diff --git a/crates/metadata/src/xmp/tests.rs b/crates/metadata/src/xmp/tests.rs new file mode 100644 index 000000000..7d7656fba --- /dev/null +++ b/crates/metadata/src/xmp/tests.rs @@ -0,0 +1,631 @@ +use super::*; + +/// A program's own namespace, as an application would pass it. +const LC: XmpNamespace = XmpNamespace { prefix: "lc", uri: "http://ns.example.com/lc/1.0/" }; + +fn write_lc(meta: &Metadata, properties: &[(&str, &str)]) -> String { + write_xmp_with(meta, &XmpWrite { toolkit: "Editor", namespace: Some(LC), properties }) +} + +fn parse_xmp(s: &str) -> Result { + parse_xmp_with(s, &[LC]) +} + +fn settings(d: &XmpData) -> Option<&str> { + d.properties.get("lc:settings").and_then(|v| v.first()).map(String::as_str) +} + +fn full() -> Metadata { + Metadata { + // write_xmp doesn't emit regions yet, so the + // roundtrip fixture must leave this empty or `roundtrip_all_fields` can't round-trip it. + regions: Vec::new(), + focus: None, + make: Some("Maker & Sons".into()), + model: Some("X <1>".into()), + serial_number: Some("SN1".into()), + software: Some("Editor".into()), + lens_make: Some("L".into()), + lens_model: Some("24-70mm".into()), + lens_serial_number: Some("LS".into()), + lens_spec: Some([24.0, 70.0, 2.8, 2.8]), + capture_time: DateTime::parse_iso("2022-05-06T07:08:09.123-04:00"), + exposure_time: Some(1.0 / 125.0), + f_number: Some(5.6), + iso: Some(800), + focal_length: Some(35.0), + focal_length_35mm: Some(52.0), + flash: Some(Flash { fired: true, raw: 0x59 }), + exposure_bias: Some(-1.0 / 3.0), + exposure_program: Some(2), + metering_mode: Some(3), + white_balance: Some(1), + orientation: Some(Orientation::Rotate270), + gps: Some(Gps { latitude: 48.858222, longitude: -2.2945, altitude: Some(-3.5) }), + width: Some(8000), + height: Some(6000), + artist: Some("Ann \"Photo\" Lee".into()), + copyright: Some("© 2022".into()), + copyright_marked: Some(true), + usage_terms: Some("Editorial use only; no ".into()), + copyright_url: Some("https://example.com/licence?a=1&b=2".into()), + title: Some("Tïtle".into()), + caption: Some("Line one\nline two".into()), + alt_text: Some("A tree by the sea".into()), + extended_description: Some("Long ".into()), + sublocation: Some("Champ de Mars".into()), + city: Some("Paris".into()), + state: Some("Île-de-France".into()), + country: Some("France".into()), + keywords: vec!["tree".into(), "sky & sea".into()], + hierarchical_keywords: vec!["Places|France|Paris".into()], + rating: Some(-1), + label: Some("Green".into()), + } +} + +#[test] +fn roundtrip_all_fields() { + let m = full(); + let json = r#"{"version":1,"exposure":0.5,"note":"<&>"}"#; + let x = write_lc(&m, &[("settings", json)]); + let d = parse_xmp(&x).unwrap(); + assert_eq!(settings(&d), Some(json)); + let mut got = d.metadata.clone(); + // floats go through rationals + assert!((got.exposure_bias.unwrap() + 1.0 / 3.0).abs() < 1e-5); + got.exposure_bias = m.exposure_bias; + let g = got.gps.unwrap(); + assert!((g.latitude - 48.858222).abs() < 1e-6 && (g.longitude + 2.2945).abs() < 1e-6, "{g:?}"); + got.gps = m.gps; + assert_eq!(got, m); +} + +#[test] +fn empty_metadata_roundtrip() { + let x = write_xmp(&Metadata::default()); + let d = parse_xmp(&x).unwrap(); + assert_eq!(d.metadata, Metadata::default()); + assert_eq!(settings(&d), None); +} + +#[test] +fn foreign_namespace_cannot_impersonate_known_metadata() { + for body in [ + r#"xmp:Rating="2" lc:settings="foreign settings"/>"#, + r#">2foreign settings"#, + ] { + let packet = format!( + r#""# + ); + let d = parse_xmp(&packet).unwrap(); + assert_eq!(d.metadata.rating, None, "foreign rating: {packet}"); + assert_eq!(settings(&d), None, "foreign settings: {packet}"); + assert!(!d.properties.contains_key("xmp:Rating")); + assert!(!d.values.contains_key("lc:settings")); + assert!(d.properties.values().any(|v| v == &["foreign settings"])); + assert!(d.properties.keys().all(|key| !key.contains('/')), "opaque names must not create struct paths"); + } +} + +#[test] +fn foreign_namespace_names_are_distinct_and_scoped() { + let packet = r#" + + 1 + 2 + 4 + "#; + let d = parse_xmp(packet).unwrap(); + assert_eq!(d.metadata.rating, Some(4), "a local rebinding must not escape its scope"); + assert_eq!(d.properties.len(), 3, "different foreign URIs must not collide"); + assert_eq!(d.values.len(), 3); + assert_eq!(d.properties["xmp:Rating"], vec!["4"]); + assert!(d.properties.values().any(|v| v == &["1"])); + assert!(d.properties.values().any(|v| v == &["2"])); +} + +#[test] +fn attribute_form_foreign_prefixes_and_structs() { + let x = r#" + + TitelTitle & more é + + k1k2 + "#; + let d = parse_xmp(x).unwrap(); + let m = d.metadata; + assert_eq!(m.rating, Some(4)); + assert_eq!(m.label.as_deref(), Some("Blue")); + assert_eq!(m.f_number, Some(4.0)); + assert_eq!(m.title.as_deref(), Some("Title & more é")); + assert_eq!(m.keywords, vec!["k1".to_string(), "k2".to_string()]); + assert_eq!(m.flash, Some(Flash { fired: false, raw: 0x10 })); + let g = m.gps.unwrap(); + assert!((g.latitude - 10.5).abs() < 1e-9); + assert!((g.longitude + 20.26).abs() < 1e-9); + assert_eq!(d.properties["dc:title"], vec!["Title & more é".to_string(), "Titel".to_string()]); +} + +/// Copyright status, usage terms and info URL (XMP Rights Management schema): written as +/// `xmpRights:Marked` (Boolean), `xmpRights:UsageTerms` (Lang Alt) and `xmpRights:WebStatement` (URI). +#[test] +fn rights_management_fields() { + let m = Metadata { copyright_marked: Some(false), usage_terms: Some("CC0".into()), ..Default::default() }; + let x = write_xmp(&m); + assert!(x.contains("False"), "{x}"); + assert!(x.contains("\n \n CC0"), "{x}"); + assert!(!x.contains("WebStatement"), "unset fields are left out"); + assert_eq!(parse_xmp(&x).unwrap().metadata, m); + // attribute form, other prefixes; anything but True / False is "unknown" + let read = |marked: &str| { + let x = format!( + r#" + + "# + ); + parse_xmp(&x).unwrap().metadata + }; + assert_eq!(read("True").copyright_marked, Some(true)); + assert_eq!(read("false").copyright_marked, Some(false)); + assert_eq!(read("").copyright_marked, None); + assert_eq!(read("maybe").copyright_marked, None); + assert_eq!(read("True").copyright_url.as_deref(), Some("https://example.org/c")); +} + +/// A zero or negative `exif:ISOSpeedRatings` is "no ISO" (as in the EXIF reader), not `Some(0)`, and +/// must not hide a valid `exifEX:PhotographicSensitivity`. +#[test] +fn non_positive_iso_is_ignored() { + let read = |attrs: &str| { + let x = format!( + r#" + + "# + ); + parse_xmp(&x).unwrap().metadata.iso + }; + assert_eq!(read(r#"exif:ISOSpeedRatings="0" exifEX:PhotographicSensitivity="400""#), Some(400)); + assert_eq!(read(r#"exif:ISOSpeedRatings="-100""#), None); + assert_eq!(read(r#"exif:ISOSpeedRatings="0""#), None); + assert_eq!(read(r#"exif:ISOSpeedRatings="200""#), Some(200)); +} + +#[test] +fn negative_xmp_pixel_width_is_ignored() { + let read = |attrs: &str| { + let x = format!( + r#" + + "# + ); + parse_xmp(&x).unwrap().metadata.width + }; + + assert_eq!(read(r#"tiff:ImageWidth="-1""#), None); + assert_eq!(read(r#"tiff:ImageWidth="-1" exif:PixelXDimension="640""#), Some(640)); + assert_eq!(read(r#"exif:PixelXDimension="-1""#), None); +} + +#[test] +fn malformed_inputs() { + assert!(parse_xmp("").is_ok() || parse_xmp("").is_err()); + assert_eq!(parse_xmp("").unwrap().metadata, Metadata::default()); + assert_eq!(parse_xmp("not xml at all").unwrap().metadata, Metadata::default()); + let deep = "".repeat(1000); + assert_eq!(parse_xmp(&deep), Err(XmpError::TooComplex)); + let x = write_lc(&full(), &[("settings", "{}")]); + for n in (0..x.len()).step_by(7) { + if x.is_char_boundary(n) { + let _ = parse_xmp(&x[..n]); + } + } +} + +#[test] +fn lc_extras_and_crs_prefix() { + let x = write_lc(&Metadata::default(), &[("settings", "{\"a\":1}"), ("flag", "pick"), ("bad name", "x")]); + assert!(!x.contains("xmlns:crs"), "we never write crs:"); + let d = parse_xmp(&x).unwrap(); + assert_eq!(settings(&d), Some("{\"a\":1}")); + assert_eq!(d.properties["lc:flag"], vec!["pick".to_string()]); + assert!(!x.contains("bad name")); + let foreign = r#""#; + assert_eq!(parse_xmp(foreign).unwrap().properties["crs:Exposure2012"], vec!["+0.5".to_string()]); +} + +#[test] +fn gps_coords() { + assert_eq!(parse_gps_coord("45,30N"), Some(45.5)); + assert_eq!(parse_gps_coord("45,30,36S"), Some(-45.51)); + assert_eq!(parse_gps_coord("45X"), None); + assert_eq!(parse_gps_coord(""), None); + assert_eq!(parse_gps_coord("é"), None); + assert_eq!(fmt_gps_coord(-45.51, 'N', 'S'), "45,30.600000S"); + assert_eq!(fmt_gps_coord(10.9999999999, 'N', 'S'), "11,0.000000N"); + assert_eq!(fmt_gps_coord(-0.9999999999, 'E', 'W'), "1,0.000000W"); +} + +/// Like the EXIF and GPX readers, XMP drops a position outside latitude ±90° / longitude ±180°. +#[test] +fn xmp_gps_out_of_range_rejected() { + let gps = |lat: &str, lon: &str| { + let x = format!( + r#""# + ); + parse_xmp(&x).unwrap().metadata.gps + }; + assert_eq!(gps("95,0N", "200,30E"), None); + assert_eq!(gps("95,0N", "10,0E"), None); + assert_eq!(gps("10,0S", "200,30W"), None); + assert_eq!(gps("90,0,1N", "0,0E"), None); + assert_eq!(gps("90,0N", "180,0E"), Some(Gps { latitude: 90.0, longitude: 180.0, altitude: None })); + assert_eq!(gps("90,0S", "180,0W"), Some(Gps { latitude: -90.0, longitude: -180.0, altitude: None })); +} + +/// MWG Region Guidelines v2.0, element form (`rdf:parseType="Resource"`, self-closing `Area`) — how +/// Lightroom typically writes it. One named face, one untyped/unnamed region (still usable), one +/// region with a description. `AppliedToDimensions` is present but irrelevant: normalized areas +/// (the default, and the only unit real files use) don't need it. +#[test] +fn mwg_regions_element_form() { + let x = r#" + + + + + + + + Jane Doe + Face + + + + + + + Pet + Rex + Good boy + + + + + "#; + let regions = parse_xmp(x).unwrap().metadata.regions; + assert_eq!(regions.len(), 3, "{regions:?}"); + + assert_eq!(regions[0].kind, RegionKind::Face); + assert_eq!(regions[0].name.as_deref(), Some("Jane Doe")); + assert_eq!(regions[0].description, None); + let r = regions[0].rect; + assert!((r.x0 - 0.4).abs() < 1e-9 && (r.x1 - 0.6).abs() < 1e-9, "{r:?}"); + assert!((r.y0 - 0.25).abs() < 1e-9 && (r.y1 - 0.55).abs() < 1e-9, "{r:?}"); + + // No mwg-rs:Type or mwg-rs:Name at all: still a usable region, kind falls back to `Other("")`. + assert_eq!(regions[1].kind, RegionKind::Other(String::new())); + assert_eq!(regions[1].name, None); + + assert_eq!(regions[2].kind, RegionKind::Pet); + assert_eq!(regions[2].name.as_deref(), Some("Rex")); + assert_eq!(regions[2].description.as_deref(), Some("Good boy")); +} + +/// Lightroom Classic writes a rotated photo's (Exif orientation 3 / 6 / 8) box in the sensor's frame +/// and records the orientation as `mwg-rs:Rotation` (±π / −π/2 / +π/2); the reader returns the +/// upright frame. +#[test] +fn mwg_regions_quarter_turn_rotation_is_mapped_to_the_upright_frame() { + let x = r#" + + + + + + + + + + + + + + + + + + + + + + + "#; + let regions = parse_xmp(x).unwrap().metadata.regions; + assert_eq!(regions.len(), 5, "{regions:?}"); + let near = |r: Rect, b: [f64; 4]| [r.x0 - b[0], r.y0 - b[1], r.x1 - b[2], r.y1 - b[3]].iter().all(|d| d.abs() < 1e-9); + assert!(near(regions[0].rect, [0.45, 0.2, 0.75, 0.4]), "−π/2 (orientation 6): {:?}", regions[0].rect); + assert!(near(regions[1].rect, [0.25, 0.6, 0.55, 0.8]), "+π/2 (orientation 8): {:?}", regions[1].rect); + assert!(near(regions[2].rect, [0.2, 0.25, 0.4, 0.55]), "0 is untouched: {:?}", regions[2].rect); + assert!(near(regions[3].rect, [0.6, 0.45, 0.8, 0.75]), "π (orientation 3): {:?}", regions[3].rect); + assert!(near(regions[4].rect, [0.6, 0.45, 0.8, 0.75]), "−π (orientation 3): {:?}", regions[4].rect); +} + +/// The size a packet's boxes were drawn on is its `AppliedToDimensions`, unless a box says how the photo was turned +/// (Lightroom's `mwg-rs:Rotation`: those are upright already) or there is no size to go by. +#[test] +fn regions_drawn_on_is_the_applied_to_size_unless_a_box_says_how_it_was_turned() { + let packet = |dims: &str, rotation: &str| { + format!( + r#" + + {dims} + + + + + + + + + + "# + ) + }; + let dims = r#""#; + let drawn_on = |x: &str| parse_xmp(x).unwrap().regions_drawn_on(); + assert_eq!(drawn_on(&packet(dims, "")), Some((6000.0, 4000.0))); + assert_eq!(drawn_on(&packet(dims, r#" mwg-rs:Rotation="-1.57080""#)), None, "one box turned by Lightroom"); + assert_eq!(drawn_on(&packet("", "")), None, "no AppliedToDimensions"); + assert_eq!(drawn_on(r#""#), None); +} + +/// A second, differently-shaped encoding: each region is its own `rdf:Description` (rather than an +/// `rdf:li rdf:parseType="Resource"`), `mwg-rs:Type` as an attribute instead of an element, and an +/// unrecognised `mwg-rs:Type` value kept verbatim. +#[test] +fn mwg_regions_attribute_form_and_unknown_type() { + let x = r#" + + + + + + + + + + + + + + + + + + "#; + let regions = parse_xmp(x).unwrap().metadata.regions; + assert_eq!(regions.len(), 2, "{regions:?}"); + assert_eq!(regions[0].kind, RegionKind::BarCode); + assert_eq!(regions[1].kind, RegionKind::Other("Sticker".to_string())); +} + +/// Hostile/malformed regions are dropped individually, never panic, and never poison the rest of +/// the list: missing Area, non-finite or absurd numbers, zero/negative size, and a pixel-unit area +/// with no `AppliedToDimensions` to convert it (so it can't be stored as a nonsense fraction). +#[test] +fn mwg_regions_hostile_inputs_are_dropped_not_panicking() { + let x = r#" + + + + + No Area + + + + + + Face + + + + "#; + let regions = parse_xmp(x).unwrap().metadata.regions; + assert_eq!(regions.len(), 1, "only the last, well-formed region should survive: {regions:?}"); + assert_eq!(regions[0].kind, RegionKind::Face); +} + +/// Regions are clipped to the photo frame, and ones that miss the photo entirely are dropped: a +/// hostile sidecar can't park a box at x=1e5 (or a 1e5-wide one) that the loupe or the People view +/// would then draw or crop far outside the image. +#[test] +fn mwg_regions_are_clipped_to_the_frame_and_off_photo_ones_dropped() { + let x = r#" + + + + + Far right + Far left + Touching edge + Huge + Corner + + + + "#; + let regions = parse_xmp(x).unwrap().metadata.regions; + let names: Vec<_> = regions.iter().map(|r| r.name.as_deref().unwrap_or("")).collect(); + assert_eq!(names, ["Huge", "Corner"], "{regions:?}"); + for r in ®ions { + let q = r.rect; + assert!(q.x0 >= 0.0 && q.y0 >= 0.0 && q.x1 <= 1.0 && q.y1 <= 1.0 && !q.is_empty(), "{q:?}"); + } + assert_eq!(regions[0].rect, Rect::UNIT); + let c = regions[1].rect; + assert!((c.x0 - 0.85).abs() < 1e-9 && c.x1 == 1.0 && c.y0 == 0.0 && (c.y1 - 0.15).abs() < 1e-9, "{c:?}"); +} + +/// `extract()`'s precedence (EXIF/IPTC never carry regions; XMP is read and filled in like keywords). +#[test] +fn regions_merge_like_keywords() { + let with_region = Region { rect: Rect::from_center(Point::new(0.5, 0.5), 0.2, 0.2), kind: RegionKind::Face, name: None, description: None }; + let mut a = Metadata::default(); + let b = Metadata { regions: vec![with_region.clone()], ..Default::default() }; + a.fill_missing(&b); + assert_eq!(a.regions, vec![with_region.clone()]); + + let mut a = Metadata::default(); + a.overlay_user_fields(&b); + assert_eq!(a.regions, vec![with_region]); +} + +#[test] +fn writer_drops_characters_xml_1_0_forbids() { + let bad = "a\u{1}b\u{0}c\u{FFFE}d\u{FFFF}e\u{1F}\tf\ng\rh"; + let m = Metadata { make: Some(bad.into()), caption: Some(bad.into()), keywords: vec![bad.into()], ..Default::default() }; + let x = write_lc(&m, &[("settings", bad)]); + let illegal: Vec = + x.chars().filter(|&c| (c < '\u{20}' && !matches!(c, '\t' | '\n' | '\r')) || c == '\u{FFFE}' || c == '\u{FFFF}').collect(); + assert!(illegal.is_empty(), "illegal XML chars in output: {illegal:?}"); + assert!(x.contains(">abcde\tf\ng\rh<"), "allowed characters must survive: {x}"); +} + +#[test] +fn numeric_references_to_forbidden_characters_are_not_decoded() { + assert_eq!(entity("#1"), None); + assert_eq!(entity("#0"), None); + assert_eq!(entity("#xFFFE"), None); + assert_eq!(entity("#x41"), Some('A')); + assert_eq!(entity("#9"), Some('\t')); +} + +/// Flattened keys repeat their whole ancestor path, so a long nested name with a wide child +/// fan-out would otherwise retain its prefix once per leaf: the cumulative key budget stops it +/// (a 65 KB document with 3000 leaves must not retain ~2.4 MB of key text). +#[test] +fn flattened_keys_stay_within_the_cumulative_budget() { + let level = "n".repeat(200); + let mut doc = String::from( + "", + ); + for _ in 0..4 { + doc.push_str(&format!("")); + } + // each leaf has a name of its own, so each one adds a key + for i in 0..3000 { + doc.push_str(&format!("")); + } + doc.push_str(""); + for _ in 0..4 { + doc.push_str(&format!("")); + } + doc.push_str(""); + let d = parse_xmp(&doc).unwrap(); + let total: usize = d.properties.keys().map(String::len).sum(); + assert!(total <= MAX_KEY_BYTES, "flattened-key text {total} is over the {MAX_KEY_BYTES} budget"); + assert!(d.properties.len() < 3000, "keys stop being added once the budget is spent"); + assert!(!d.properties.is_empty(), "properties parsed before the budget are kept"); +} + +#[test] +fn zero_padded_numeric_references_decode() { + assert_eq!(unescape_attr("Canon A A"), "Canon A A"); + assert_eq!(unescape_attr("&abc&#xyz;&;"), "&abc&#xyz;&;"); +} + +#[test] +fn entity_rejects_signed_numeric_references() { + assert_eq!(entity("#+65"), None); + assert_eq!(entity("#x+41"), None); + assert_eq!(entity("#65"), Some('A')); + assert_eq!(entity("#x41"), Some('A')); +} + +#[test] +fn malformed_capture_time_falls_back_and_keeps_raw_property() { + let packet = r#""#; + let parsed = parse_xmp(packet).unwrap(); + assert_eq!( + parsed.metadata.capture_time.map(|d| (d.year, d.month, d.day, d.hour, d.minute, d.second, d.offset_minutes)), + Some((2026, 1, 2, 10, 0, 0, Some(0))) + ); + assert_eq!(parsed.properties["exif:DateTimeOriginal"], vec!["2026-01-01T".to_string()]); +} + +#[test] +fn malformed_capture_time_without_fallback_is_not_projected() { + let packet = r#""#; + let parsed = parse_xmp(packet).unwrap(); + assert_eq!(parsed.metadata.capture_time, None); + assert_eq!(parsed.properties["exif:DateTimeOriginal"], vec!["2026-01-01T".to_string()]); +} + +#[test] +fn many_ampersands_before_one_semicolon_are_read_once() { + // 200 000 `&` then a `;`: each `&` once searched the whole rest of the attribute for the `;` + let hostile = format!("{};&", "&".repeat(200_000)); + assert_eq!(unescape_attr(&hostile), format!("{};&", "&".repeat(200_000))); + assert_eq!(unescape_attr("<a> 􏿿 &unknownreference;"), " \u{10FFFF} &unknownreference;"); +} + +/// Children that add no key (empty structs) still had their whole ancestor path copied, each, +/// after the budget was spent: a 250 KB path under 100 000 such children was 25 GB of copying. +#[test] +fn paths_are_not_built_past_the_key_budget() { + let level = "n".repeat(60_000); + let mut doc = String::from( + "", + ); + for _ in 0..4 { + doc.push_str(&format!("")); + } + for _ in 0..100_000 { + doc.push_str(""); + } + for _ in 0..4 { + doc.push_str(&format!("")); + } + doc.push_str(""); + let t = std::time::Instant::now(); + let _ = parse_xmp(&doc); + assert!(t.elapsed().as_secs() < 10, "took {:?}", t.elapsed()); +} + +/// The caller's namespace is declared where the read-only `crs` sits in the table, is known to the +/// reader only when passed, and is canonicalised by its URI whatever prefix a packet binds it to. +#[test] +fn the_callers_namespace_is_written_and_read_only_when_passed() { + let x = write_lc(&Metadata::default(), &[("flag", "pick")]); + assert!(x.contains("x:xmptk=\"Editor\"")); + assert!(x.contains( + "xmlns:Iptc4xmpCore=\"http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/\"\n xmlns:lc=\"http://ns.example.com/lc/1.0/\"\n xmlns:mwg-rs=" + )); + assert!(!x.contains("xmlns:crs")); + let renamed = x.replace("xmlns:lc=", "xmlns:zz=").replace("lc:flag", "zz:flag"); + assert_eq!(parse_xmp_with(&renamed, &[LC]).unwrap().properties["lc:flag"], vec!["pick".to_string()]); + assert!(!crate::parse_xmp(&renamed).unwrap().properties.contains_key("lc:flag")); + + let plain = write_xmp(&Metadata::default()); + assert!(plain.contains("x:xmptk=\"craft-metadata\"") && !plain.contains("xmlns:lc") && !plain.contains("xmlns:crs")); + // properties without a namespace to put them in are not written + let orphan = write_xmp_with(&Metadata::default(), &XmpWrite { properties: &[("flag", "pick")], ..Default::default() }); + assert_eq!(orphan, plain); +} diff --git a/crates/metadata/src/xmp_merge.rs b/crates/metadata/src/xmp_merge.rs new file mode 100644 index 000000000..b52e81c26 --- /dev/null +++ b/crates/metadata/src/xmp_merge.rs @@ -0,0 +1,505 @@ +//! Merging a freshly written XMP packet into an existing one (a sidecar another application +//! wrote, e.g. with its develop settings and edit history), keeping everything the writer does not +//! own byte for byte. +//! +//! The existing packet is scanned once (namespace-resolved, with byte positions). Only top-level +//! properties of the `rdf:Description` elements directly under `rdf:RDF` are considered, in +//! attribute form (`xmp:Rating="3"`) and element form (`…`): +//! - properties the writer owns ([`MergeRules::owned`]) are removed, and written from the fresh +//! packet when it has them (so a cleared field is cleared in the file too); +//! - properties owned only when stated ([`MergeRules::owned_if_present`]) are replaced when the +//! fresh packet has them and kept otherwise; +//! - any other property of the fresh packet (e.g. `xmp:CreatorTool`) is not written: the file +//! keeps describing itself as its creator left it; +//! - everything else — other namespaces (e.g. `crs:`), `xmpMM:History`, unknown properties, +//! comments, the packet wrapper and padding — is copied unchanged. +//! +//! The written properties go into one new `rdf:Description` (declaring its own namespaces, so +//! the existing prefix bindings never matter) just before ``. A description left empty +//! by the removals is dropped, so repeated saves don't accumulate. The result is checked by +//! parsing it again: every written property must read back as in the fresh packet. + +use std::collections::{BTreeSet, HashSet}; + +use quick_xml::events::Event; + +use crate::xmp::{MAX_OPAQUE_NAME_BYTES, XmpError, XmpNamespace, canonical_name, parse_xmp_with, unescape_attr}; + +/// Which properties [`merge_xmp`] takes from the fresh packet. Names are canonical +/// `prefix:name` (prefixes as [`crate::parse_xmp`] reports them); `prefix:*` is a whole namespace. +#[derive(Clone, Copy, Debug, Default)] +pub struct MergeRules<'a> { + /// Always replaced: removed from the existing packet, written from the fresh one if stated. + pub owned: &'a [&'a str], + /// Replaced only when the fresh packet states them; otherwise the existing value stays. + pub owned_if_present: &'a [&'a str], + /// Namespaces beyond the standard ones that the names above use, as for [`crate::parse_xmp_with`]. + pub namespaces: &'a [XmpNamespace<'a>], +} + +fn listed(list: &[&str], name: &str) -> bool { + list.iter().any(|p| match p.strip_suffix('*') { + Some(ns) => name.starts_with(ns), + None => *p == name, + }) +} + +#[derive(Debug)] +struct Attr { + raw: String, + name: String, + /// The value as written (still escaped). + value: String, +} + +#[derive(Debug)] +struct Prop { + name: String, + start: usize, + end: usize, +} + +#[derive(Debug, Default)] +struct Desc { + tag_start: usize, + tag_end: usize, + /// The element name as written (`rdf:Description`, or another prefix for the RDF namespace). + raw_name: String, + empty: bool, + attrs: Vec, + props: Vec, + /// The end of `` (`tag_end` when self-closing). + end: usize, +} + +#[derive(Debug, Default)] +struct Scan { + descs: Vec, + /// Where the first `` starts. + rdf_close: Option, +} + +enum Kind { + Other, + Rdf, + Desc(usize), + Prop { desc: usize, start: usize, name: String }, +} + +fn is_meta_attr(name: &str) -> bool { + name.starts_with("xmlns") + || matches!(name, "rdf:about" | "rdf:ID" | "rdf:nodeID" | "xml:lang" | "rdf:parseType" | "rdf:resource" | "rdf:datatype") +} + +fn pos(r: &quick_xml::Reader<&[u8]>) -> usize { + usize::try_from(r.buffer_position()).unwrap_or(usize::MAX) +} + +/// Locate the descriptions, their top-level properties and `` in a packet. +fn scan(s: &str, extra: &[XmpNamespace]) -> Result { + let mut reader = quick_xml::Reader::from_reader(s.as_bytes()); + reader.config_mut().check_end_names = true; + let mut out = Scan::default(); + let mut stack: Vec = Vec::new(); + let mut scopes: Vec> = vec![vec![("xml".into(), "http://www.w3.org/XML/1998/namespace".into())]]; + let mut count = 0usize; + let mut opaque_budget = MAX_OPAQUE_NAME_BYTES; + loop { + let before = pos(&reader); + let ev = reader.read_event().map_err(|e| XmpError::Xml(e.to_string()))?; + let after = pos(&reader); + match ev { + Event::Start(ref e) | Event::Empty(ref e) => { + count += 1; + if count > 200_000 || stack.len() > 64 { + return Err(XmpError::TooComplex); + } + let empty = matches!(ev, Event::Empty(_)); + let mut scope = Vec::new(); + let mut raw_attrs = Vec::new(); + // repeated names are refused with a set, in linear time (quick-xml's own check is quadratic) + let mut seen = HashSet::new(); + for a in e.attributes().with_checks(false) { + let a = a.map_err(|e| XmpError::Xml(e.to_string()))?; + let k = String::from_utf8_lossy(a.key.as_ref()).into_owned(); + if !seen.insert(k.clone()) { + return Err(XmpError::Xml(format!("repeated attribute {k}"))); + } + let v = String::from_utf8_lossy(&a.value).into_owned(); + if k == "xmlns" { + scope.push((String::new(), unescape_attr(&v))); + } else if let Some(p) = k.strip_prefix("xmlns:") { + scope.push((p.to_string(), unescape_attr(&v))); + } + raw_attrs.push((k, v)); + } + scopes.push(scope); + let raw_name = String::from_utf8_lossy(e.name().as_ref()).into_owned(); + let name = canonical_name(&raw_name, &scopes, extra, &mut opaque_budget)?; + let kind = match stack.last() { + Some(Kind::Rdf) if name == "rdf:Description" => { + let attrs = raw_attrs + .into_iter() + .map(|(raw, value)| Ok(Attr { name: canonical_name(&raw, &scopes, extra, &mut opaque_budget)?, raw, value })) + .collect::>()?; + out.descs.push(Desc { tag_start: before, tag_end: after, raw_name, empty, attrs, props: Vec::new(), end: after }); + Kind::Desc(out.descs.len() - 1) + } + Some(Kind::Desc(i)) => Kind::Prop { desc: *i, start: before, name }, + _ if name == "rdf:RDF" && out.rdf_close.is_none() => Kind::Rdf, + _ => Kind::Other, + }; + if empty { + scopes.pop(); + if let Kind::Prop { desc, start, name } = kind + && let Some(d) = out.descs.get_mut(desc) + { + d.props.push(Prop { name, start, end: after }); + } + } else { + stack.push(kind); + } + } + Event::End(_) => { + scopes.pop(); + match stack.pop() { + Some(Kind::Prop { desc, start, name }) => { + if let Some(d) = out.descs.get_mut(desc) { + d.props.push(Prop { name, start, end: after }); + } + } + Some(Kind::Desc(i)) => { + if let Some(d) = out.descs.get_mut(i) { + d.end = after; + } + } + Some(Kind::Rdf) => out.rdf_close = out.rdf_close.or(Some(before)), + Some(Kind::Other) => {} + None => return Err(XmpError::Xml("unbalanced end tag".into())), + } + } + Event::Eof => break, + _ => {} + } + } + if !stack.is_empty() { + return Err(XmpError::Xml("unclosed element".into())); + } + Ok(out) +} + +/// `start` moved back over the indentation and line break before it (so a removed property +/// takes its line with it). +fn line_start(s: &str, start: usize) -> usize { + let b = s.as_bytes(); + let mut i = start; + while i > 0 && matches!(b.get(i - 1), Some(b' ' | b'\t')) { + i -= 1; + } + if i > 0 && b.get(i - 1) == Some(&b'\n') { + i -= 1; + if i > 0 && b.get(i - 1) == Some(&b'\r') { + i -= 1; + } + } + i +} + +/// Merge `fresh` (a packet as [`crate::write_xmp_with`] writes it) into `existing` following +/// `rules` (see the module docs). Fails — and the caller must not overwrite `existing` blindly — +/// when `existing` isn't a well-formed XMP packet. +pub fn merge_xmp(existing: &str, fresh: &str, rules: MergeRules) -> Result { + let (bom, old) = match existing.strip_prefix('\u{feff}') { + Some(rest) => ("\u{feff}", rest), + None => ("", existing), + }; + parse_xmp_with(old, rules.namespaces)?; + let ex = scan(old, rules.namespaces)?; + let rdf_close = ex.rdf_close.ok_or_else(|| XmpError::Xml("no rdf:RDF element".into()))?; + let new = scan(fresh, rules.namespaces)?; + let fresh_desc = new.descs.first().ok_or_else(|| XmpError::Xml("the new packet has no rdf:Description".into()))?; + let fresh_names: BTreeSet<&str> = fresh_desc.props.iter().map(|p| p.name.as_str()).collect(); + let remove = |name: &str| listed(rules.owned, name) || (listed(rules.owned_if_present, name) && fresh_names.contains(name)); + let write = |name: &str| listed(rules.owned, name) || listed(rules.owned_if_present, name); + + // edits on `old`: (start, end, replacement), applied back to front + let mut edits: Vec<(usize, usize, String)> = Vec::new(); + for d in &ex.descs { + let gone_attrs: Vec<&Attr> = d.attrs.iter().filter(|a| !is_meta_attr(&a.name) && remove(&a.name)).collect(); + let gone_props: Vec<&Prop> = d.props.iter().filter(|p| remove(&p.name)).collect(); + if gone_attrs.is_empty() && gone_props.is_empty() { + continue; + } + let attrs_left = d.attrs.iter().any(|a| !is_meta_attr(&a.name) && !remove(&a.name)); + if !attrs_left && gone_props.len() == d.props.len() { + // nothing of its own left: drop the whole description + edits.push((line_start(old, d.tag_start), d.end, String::new())); + continue; + } + if !gone_attrs.is_empty() { + let mut tag = format!("<{}", d.raw_name); + for a in d.attrs.iter().filter(|a| is_meta_attr(&a.name) || !remove(&a.name)) { + tag.push_str(&format!("\n {}=\"{}\"", a.raw, a.value.replace('"', """))); + } + tag.push_str(if d.empty { "/>" } else { ">" }); + edits.push((d.tag_start, d.tag_end, tag)); + } + for p in gone_props { + edits.push((line_start(old, p.start), p.end, String::new())); + } + } + // the new description: its own namespace declarations, the properties to write + let mut block = String::from("\n '); + let mut written = 0; + for p in fresh_desc.props.iter().filter(|p| write(&p.name)) { + let text = fresh.get(p.start..p.end).ok_or_else(|| XmpError::Xml("bad property span".into()))?; + block.push_str("\n "); + block.push_str(text); + written += 1; + } + block.push_str("\n "); + if written > 0 { + let at = old.get(..rdf_close).map_or(rdf_close, |h| h.trim_end().len()); + edits.push((at, at, block)); + } + edits.sort_by_key(|e| (e.0, e.1)); + // overlapping edits can't happen (properties sit inside their description, which is either + // dropped whole or edited piecewise), but never splice blindly + let mut out = String::with_capacity(old.len() + 4096); + out.push_str(bom); + let mut at = 0; + for (s, e, r) in &edits { + if *s < at { + return Err(XmpError::Xml("overlapping edits".into())); + } + out.push_str(old.get(at..*s).ok_or_else(|| XmpError::Xml("bad span".into()))?); + out.push_str(r); + at = *e; + } + out.push_str(old.get(at..).ok_or_else(|| XmpError::Xml("bad span".into()))?); + + // read back: everything written must come out as written + let merged = parse_xmp_with(&out, rules.namespaces)?; + let want = parse_xmp_with(fresh, rules.namespaces)?; + for (k, v) in &want.properties { + let top = k.split('/').next().unwrap_or(k); + if write(top) && merged.properties.get(k) != Some(v) { + return Err(XmpError::Xml(format!("{k} did not survive the merge"))); + } + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::xmp::parse_xmp_with; + use crate::{Metadata, XmpData, XmpWrite, write_xmp_with}; + + /// A program's own namespace, as an application would pass it. + const LC: XmpNamespace = XmpNamespace { prefix: "lc", uri: "http://ns.example.com/lc/1.0/" }; + + fn write_lc(meta: &Metadata, properties: &[(&str, &str)]) -> String { + write_xmp_with(meta, &XmpWrite { toolkit: "Editor", namespace: Some(LC), properties }) + } + + fn parse_xmp(s: &str) -> Result { + parse_xmp_with(s, &[LC]) + } + + const RULES: MergeRules = MergeRules { + owned: &["xmp:Rating", "xmp:Label", "dc:subject", "dc:title", "lc:*"], + owned_if_present: &["exif:DateTimeOriginal"], + namespaces: &[LC], + }; + + /// A sidecar as another raw developer writes it: attribute-form simple properties, develop + /// settings, a structured history, an unknown namespace. + const FOREIGN: &str = " + + + + + + + + + + + 0, 0 + 255, 255 + + + + + old + + + + + +"; + + /// With the packet's whitespace padding and trailer. + fn foreign() -> String { + format!("{FOREIGN}{}\n", " ".repeat(100)) + } + + fn fresh(rating: i8, keywords: &[&str]) -> String { + let m = crate::Metadata { + software: Some("Editor".into()), + rating: Some(rating), + keywords: keywords.iter().map(|k| k.to_string()).collect(), + ..Default::default() + }; + write_lc(&m, &[("settings", "{\"a\":1}"), ("flag", "pick")]) + } + + #[test] + fn keeps_foreign_properties_and_replaces_owned_ones() { + let out = merge_xmp(&foreign(), &fresh(4, &["new", "two"]), RULES).unwrap(); + let d = parse_xmp(&out).unwrap(); + let p = |k: &str| d.properties.get(k).cloned().unwrap_or_default(); + assert_eq!(p("xmp:Rating"), vec!["4"]); + assert_eq!(p("dc:subject"), vec!["new", "two"]); + assert_eq!(p("lc:settings"), vec!["{\"a\":1}"]); + assert_eq!(p("lc:flag"), vec!["pick"]); + assert_eq!(p("xmp:CreatorTool"), vec!["Other App"], "not ours: kept"); + assert_eq!(p("crs:Exposure2012"), vec!["+0.50"]); + assert_eq!(p("foo:Thing").len(), 1); + // untouched parts are byte for byte the same + for part in [ + " \n \n \n \n ", + " \n \n 0, 0", + "crs:Contrast2012=\"-12\"", + "foo:Thing=\"a & b\"", + "\n", + &format!("\n{}\n", " ".repeat(100)), + ] { + assert!(out.contains(part), "lost {part:?} in\n{out}"); + } + assert!(!out.contains("xmp:Rating=\"2\"")); + assert!(!out.contains("old")); + // saving again changes only our part, and doesn't pile up descriptions + let again = merge_xmp(&out, &fresh(1, &[]), RULES).unwrap(); + let d = parse_xmp(&again).unwrap(); + assert_eq!(d.properties.get("xmp:Rating"), Some(&vec!["1".to_string()])); + assert!(!d.properties.contains_key("dc:subject"), "a cleared field is cleared"); + assert_eq!(again.matches("\ + "; + let out = merge_xmp(ex, &fresh(3, &[]), RULES).unwrap(); + let d = parse_xmp(&out).unwrap(); + assert_eq!(d.properties.get("xmp:Rating"), Some(&vec!["3".to_string()])); + assert_eq!(d.properties.get("exif:DateTimeOriginal"), Some(&vec!["2020-01-01T00:00:00".to_string()]), "fresh has none: kept"); + let mut m = crate::Metadata { rating: Some(3), ..Default::default() }; + m.capture_time = crate::DateTime::parse_iso("2024-05-06T07:08:09"); + let out = merge_xmp(ex, &write_lc(&m, &[]), RULES).unwrap(); + let d = parse_xmp(&out).unwrap(); + assert_eq!(d.properties.get("exif:DateTimeOriginal"), Some(&vec!["2024-05-06T07:08:09".to_string()])); + } + + #[test] + fn preserves_foreign_properties_using_known_prefixes() { + for property in [r#"lc:Secret="keep-me"/>"#, r#">keep-me"#] { + let desc = format!(r#"{desc}"#); + let out = merge_xmp(&old, &fresh(4, &[]), RULES).unwrap(); + assert!(out.contains(&desc), "lost foreign description: {out}"); + assert_eq!(parse_xmp(&out).unwrap().metadata.rating, Some(4)); + assert_eq!(merge_xmp(&out, &fresh(4, &[]), RULES).unwrap(), out); + } + } + + #[test] + fn namespace_aliases_remain_distinct_during_merge() { + // The alias URI includes a character reference: scanning and parsing must resolve it identically. + let foreign = r#""#; + let old = format!( + r#"{foreign} + + "# + ); + let out = merge_xmp(&old, &fresh(5, &[]), RULES).unwrap(); + assert!(out.contains(foreign), "lost foreign rating: {out}"); + assert!(!out.contains("a:Rating="), "genuine alias must be replaced: {out}"); + assert_eq!(parse_xmp(&out).unwrap().metadata.rating, Some(5)); + assert_eq!(merge_xmp(&out, &fresh(5, &[]), RULES).unwrap(), out); + } + + #[test] + fn opaque_namespace_expansion_is_bounded() { + // A roughly 40 KiB packet must not expand the same long URI into every property name indefinitely. + let packet = format!( + r#"{}"#, + "a".repeat(1024), + "".repeat(5000) + ); + assert_eq!(parse_xmp(&packet).unwrap_err(), XmpError::TooComplex); + assert!(matches!(scan(&packet, &[LC]), Err(XmpError::TooComplex))); + } + + #[test] + fn many_attributes_scan_in_linear_time() { + // quick-xml's duplicate-name check compares each attribute with all earlier ones: 200 000 attributes on + // one description took over 20 s to scan, about 30 ms with the set + let n = 200_000; + let mut s = String::from( + "\ + "); + let t = std::time::Instant::now(); + let found = scan(&s, &[LC]).unwrap(); + let took = t.elapsed(); + assert_eq!(found.descs[0].attrs.len(), n + 2, "every attribute is read"); + assert!(took < std::time::Duration::from_secs(5), "{n} attributes took {took:?}"); + } + + #[test] + fn refuses_what_it_cannot_read() { + for bad in ["", "not xml at all", "", ""] { + assert!(merge_xmp(bad, &fresh(1, &[]), RULES).is_err(), "{bad:?}"); + } + } + + #[test] + fn refuses_a_repeated_attribute() { + // not well-formed XML (strict readers such as expat refuse it): no merge, so the caller keeps a backup. + // An owned name, a foreign one and one on a nested element (a merge would keep both copies of the last two). + for (once, twice) in [ + ("xmp:Rating=\"2\"", "xmp:Rating=\"2\" xmp:Rating=\"3\""), + ("foo:Thing=\"a & b\"", "foo:Thing=\"a & b\" foo:Thing=\"c\""), + ("stEvt:action=\"saved\"", "stEvt:action=\"saved\" stEvt:action=\"saved\""), + ] { + let ex = foreign().replace(once, twice); + assert!(merge_xmp(&ex, &fresh(1, &[]), RULES).is_err(), "{twice}"); + } + } +} diff --git a/crates/metadata/tests/it/main.rs b/crates/metadata/tests/it/main.rs new file mode 100644 index 000000000..f56fd47f0 --- /dev/null +++ b/crates/metadata/tests/it/main.rs @@ -0,0 +1,3 @@ +//! The integration tests, one module per file, linked into one test binary. + +mod robust; diff --git a/crates/metadata/tests/it/robust.rs b/crates/metadata/tests/it/robust.rs new file mode 100644 index 000000000..ce2b8f40e --- /dev/null +++ b/crates/metadata/tests/it/robust.rs @@ -0,0 +1,70 @@ +use craft_metadata::{MergeRules, Metadata, XmpNamespace, XmpWrite, extract, merge_xmp, parse_iptc, parse_xmp_with, read_exif, write_xmp_with}; + +const LC: XmpNamespace = XmpNamespace { prefix: "lc", uri: "http://ns.example.com/lc/1.0/" }; + +fn write_xmp(meta: &Metadata, settings: Option<&str>) -> String { + let properties: Vec<(&str, &str)> = settings.map(|s| ("settings", s)).into_iter().collect(); + write_xmp_with(meta, &XmpWrite { toolkit: "Editor", namespace: Some(LC), properties: &properties }) +} + +fn parse_xmp(s: &str) -> Result { + parse_xmp_with(s, &[LC]) +} + +fn settings(d: &craft_metadata::XmpData) -> Option { + d.properties.get("lc:settings").and_then(|v| v.first()).cloned() +} +use proptest::prelude::*; + +proptest! { + #![proptest_config(ProptestConfig { cases: 1500, .. ProptestConfig::default() })] + + /// Random bytes behind each container's magic: reading them yields whatever can be read and + /// never panics. + #[test] + fn random_containers_never_panic(mut data in proptest::collection::vec(any::(), 0..600), kind in 0usize..7) { + let heads: [&[u8]; 7] = [b"\xff\xd8\xff\xe1", b"\x89PNG\r\n\x1a\n", b"RIFF\0\0\0\0WEBP", b"II*\0", b"MM\0*", b"\0\0\0\x18ftypcrx ", b"\0\0\0\x18ftypheic"]; + let h = heads[kind]; + if data.len() >= h.len() { + data[..h.len()].copy_from_slice(h); + } + let _ = extract(&data); + let _ = read_exif(&data); + let _ = parse_iptc(&data); + } + + #[test] + fn random_text_xmp_never_panics(s in ".{0,400}") { + let _ = parse_xmp(&s); + } + + /// Merging into arbitrary (also damaged) packets never panics, and a merge that succeeds + /// reads back with the written values. + #[test] + fn merge_never_panics(cut in 0usize..900, junk in ".{0,20}", rating in 0i8..=5) { + let base = write_xmp(&Metadata { title: Some("t".into()), rating: Some(1), ..Default::default() }, Some("{}")); + let mut s: String = base.chars().take(cut).collect(); + s.push_str(&junk); + s.extend(base.chars().skip(cut)); + let fresh = write_xmp(&Metadata { rating: Some(rating), ..Default::default() }, Some("{\"x\":1}")); + let rules = MergeRules { owned: &["xmp:Rating", "dc:title", "lc:*"], owned_if_present: &[], namespaces: &[LC] }; + if let Ok(out) = merge_xmp(&s, &fresh, rules) { + let d = parse_xmp(&out).unwrap(); + prop_assert_eq!(d.metadata.rating, Some(rating)); + prop_assert_eq!(settings(&d), Some("{\"x\":1}".to_string())); + } + } + + #[test] + fn user_text_roundtrips(title in "[a-zA-Z0-9 &<>\"'éü€;|/\\\\]{1,40}", kw in proptest::collection::vec("[a-zA-Z0-9 &<>\"'|]{1,12}", 0..5), rating in -1i8..=5) { + let title = title.trim().to_string(); + prop_assume!(!title.is_empty()); + let kw: Vec = kw.into_iter().map(|k| k.trim().to_string()).filter(|k| !k.is_empty()).collect(); + let m = Metadata { title: Some(title.clone()), keywords: kw.clone(), rating: Some(rating), ..Default::default() }; + let d = parse_xmp(&write_xmp(&m, Some(&title))).unwrap(); + prop_assert_eq!(settings(&d), Some(title.clone())); + prop_assert_eq!(d.metadata.title, Some(title)); + prop_assert_eq!(d.metadata.keywords, kw); + prop_assert_eq!(d.metadata.rating, Some(rating)); + } +} From 58f6c4b9e8a543130f55f7ea948cd55b28945b09 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:23:15 -0500 Subject: [PATCH 048/120] craft-idml, craft-epub: read and write through craft-zip (#950) * craft-zip: ZIP preflight, bounded entry read and Budget (from craft-archive, #129) The ZIP half of the craft-archive proposal (#126, #129): an allocation-free check of the end records and central directory that runs before a ZIP reader indexes the archive, a bounded entry read that distrusts the declared size, and the named Budget both charge. The XML half of #129 is not carried: craft-xml bounds XML now. * craft-zip: Archive (entry-count guard, duplicate names, budgeted reads) and Writer check_entry_count is craft-opc's guard: the declared entry count, ZIP64 included, checked on the raw bytes before zip parses the central directory. Archive reads entries within a per-entry limit and a total Budget through read_entry. Duplicate names are found by counting the central directory headers against the entries zip kept. Writer is craft-opc's ZipOut: stored or deflated entries, ZIP64 for large ones, one fixed timestamp. * craft-idml, craft-epub: read and write through craft-zip Both crates drop their own bounded ZIP reads and zip writers. They gain the entry-count check before the archive is indexed (ZIP64 included) and archives whose bytes depend only on their content (one fixed timestamp). --------- Co-authored-by: skylar taylor-barrick --- Cargo.lock | 4 +- crates/epub/Cargo.toml | 9 +++-- crates/epub/README.md | 3 +- crates/epub/src/read.rs | 46 ++++++++++------------ crates/epub/src/write.rs | 14 ++----- crates/epub/tests/read.rs | 8 ++++ crates/idml/Cargo.toml | 9 +++-- crates/idml/README.md | 3 +- crates/idml/src/package.rs | 76 +++++++++++++++--------------------- crates/idml/tests/package.rs | 13 ++++++ 10 files changed, 92 insertions(+), 93 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b531fce60..c7fe9c680 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1025,7 +1025,7 @@ name = "craft-epub" version = "0.1.0" dependencies = [ "craft-xml", - "flate2", + "craft-zip", "thiserror 2.0.21", "zip", ] @@ -1185,7 +1185,7 @@ name = "craft-idml" version = "0.1.0" dependencies = [ "craft-xml", - "flate2", + "craft-zip", "thiserror 2.0.21", "zip", ] diff --git a/crates/epub/Cargo.toml b/crates/epub/Cargo.toml index 690a89778..67875fe89 100644 --- a/crates/epub/Cargo.toml +++ b/crates/epub/Cargo.toml @@ -13,8 +13,9 @@ workspace = true [dependencies] craft-xml = { workspace = true } thiserror = { workspace = true } -# `deflate-flate2` with flate2's default miniz_oxide backend, as craft-update pins it (zip's own -# `deflate` feature would switch a whole app's flate2 to zlib-rs). +craft-zip = { workspace = true } + +[dev-dependencies] +# The tests build hostile archives by hand. zip = { version = "9", default-features = false, features = ["deflate-flate2"] } -# The backend of zip's deflate: `deflate-flate2` leaves the choice to the build. -flate2 = { version = "1", default-features = false, features = ["rust_backend"] } +craft-zip = { workspace = true, features = ["test-support"] } diff --git a/crates/epub/README.md b/crates/epub/README.md index c15c13876..c9ba7d134 100644 --- a/crates/epub/README.md +++ b/crates/epub/README.md @@ -94,7 +94,8 @@ for path in &spine { | `max_total_bytes` (all entries read from one package) | 256 MiB | `Error::TooLarge` | | `max_manifest`, `max_spine` | 10,000 each | further items are ignored | -Sizes are checked against what an entry really expands to, not what the archive declares. The two +The ZIP layer is craft-zip: the declared entry count is checked before the archive is indexed, and +sizes are checked against what an entry really expands to, not what the archive declares. The two XML documents are read with craft-xml's limits (depth 256, 2,000,000 nodes) and `max_entry_bytes`. ## Where it came from diff --git a/crates/epub/src/read.rs b/crates/epub/src/read.rs index ac93c5b0e..0540e70f8 100644 --- a/crates/epub/src/read.rs +++ b/crates/epub/src/read.rs @@ -1,7 +1,6 @@ //! An EPUB 2 or 3 package, read: container, package document, and bounded entry reads. use std::collections::HashMap; -use std::io::{Cursor, Read}; use craft_xml::{El, Local, Options}; @@ -224,10 +223,8 @@ pub fn parse_package(xml: &[u8], opf_dir: &str, limits: &Limits) -> Result { - zip: zip::ZipArchive>, + zip: craft_zip::Archive<'a>, limits: Limits, - /// Bytes the entries still to be read may expand to. - left: u64, /// The archive path of the package document. pub opf_path: String, pub package: Package, @@ -242,11 +239,16 @@ impl<'a> Epub<'a> { /// Reads the container and the package document. The `mimetype` entry is not required: /// packages without one, or with it compressed, are common. pub fn open_with(bytes: &'a [u8], limits: Limits) -> Result { - let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| Error::NotEpub(e.to_string()))?; - if zip.len() > limits.max_entries { - return Err(Error::TooManyEntries { found: zip.len(), limit: limits.max_entries }); - } - let mut epub = Epub { zip, limits, left: limits.max_total_bytes, opf_path: String::new(), package: Package::default() }; + let zip = + craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries as u64, max_total_bytes: limits.max_total_bytes }) + .map_err(|e| match e { + craft_zip::Error::TooManyEntries { declared, limit } => Error::TooManyEntries { + found: usize::try_from(declared).unwrap_or(usize::MAX), + limit: usize::try_from(limit).unwrap_or(usize::MAX), + }, + e => Error::NotEpub(e.to_string()), + })?; + let mut epub = Epub { zip, limits, opf_path: String::new(), package: Package::default() }; let container = epub.read("META-INF/container.xml")?.ok_or_else(|| Error::NotEpub("META-INF/container.xml is missing".into()))?; let opf_path = parse_container(&container)?.ok_or_else(|| Error::NotEpub("container.xml names no package document".into()))?; // The container gives a path from the archive's root; resolving it also drops a leading `/`. @@ -262,28 +264,20 @@ impl<'a> Epub<'a> { /// Does the archive have an entry `path`? pub fn contains(&self, path: &str) -> bool { - self.zip.index_for_name(path).is_some() + self.zip.contains(path) } /// The bytes of archive entry `path`, or `None` when the archive has no such entry. An entry /// past the limits is an error, and so is one that expands to more than it declared. pub fn read(&mut self, path: &str) -> Result>, Error> { - let Some(index) = self.zip.index_for_name(path).filter(|_| !path.ends_with('/')) else { return Ok(None) }; - let part = |msg: String| Error::Part { part: path.to_string(), msg }; - let f = self.zip.by_index(index).map_err(|e| part(e.to_string()))?; - let limit = self.limits.max_entry_bytes.min(self.left); - let too_large = || Error::TooLarge { part: path.to_string(), limit }; - if f.size() > limit { - return Err(too_large()); + if path.ends_with('/') { + return Ok(None); } - let mut buf = Vec::new(); - f.take(limit.saturating_add(1)).read_to_end(&mut buf).map_err(|e| part(e.to_string()))?; - let n = buf.len() as u64; - if n > limit { - return Err(too_large()); - } - self.left = self.left.saturating_sub(n); - Ok(Some(buf)) + let limit = self.limits.max_entry_bytes.min(self.zip.budget().remaining()); + self.zip.read(path, self.limits.max_entry_bytes).map_err(|e| match e { + craft_zip::Error::EntryTooLarge { .. } | craft_zip::Error::BudgetSpent { .. } => Error::TooLarge { part: path.to_string(), limit }, + e => Error::Part { part: path.to_string(), msg: e.to_string() }, + }) } /// [`Epub::read`] for what a file of the package refers to: `href` as written in the file at @@ -295,6 +289,6 @@ impl<'a> Epub<'a> { /// Bytes the entries still to be read may expand to. pub fn budget_left(&self) -> u64 { - self.left + self.zip.budget().remaining() } } diff --git a/crates/epub/src/write.rs b/crates/epub/src/write.rs index 0c77951a8..fbc741b55 100644 --- a/crates/epub/src/write.rs +++ b/crates/epub/src/write.rs @@ -3,7 +3,6 @@ use std::borrow::Cow; use std::collections::HashSet; use std::fmt::Write as _; -use std::io::Write as _; use craft_xml::{esc, esc_attr, is_name}; @@ -316,10 +315,8 @@ pub fn write(book: &Book) -> Result, Error> { w3cdtf(&book.metadata.modified).ok_or_else(|| Error::Invalid(format!("`{}` is not a modification date", book.metadata.modified)))?; let opf = package_document(book, &modified); let nav = nav_document(book); - let fail = |e: &dyn std::fmt::Display| Error::Invalid(format!("the archive could not be written: {e}")); - let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); - let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); - let deflated = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); + let fail = |e: craft_zip::Error| Error::Invalid(e.to_string()); + let mut z = craft_zip::Writer::new(); let own = [ ("mimetype".to_string(), MIMETYPE.as_bytes(), true), ("META-INF/container.xml".to_string(), CONTAINER.as_bytes(), false), @@ -328,10 +325,7 @@ pub fn write(book: &Book) -> Result, Error> { ]; let resources = book.resources.iter().map(|r| (format!("{ROOT}/{}", r.path), r.data.as_ref(), r.stored)); for (name, data, store) in own.into_iter().chain(resources) { - // More than 4 GiB needs the ZIP64 form. - let options = if store { stored } else { deflated }.large_file(data.len() >= u32::MAX as usize); - z.start_file(name, options).map_err(|e| fail(&e))?; - z.write_all(data).map_err(|e| fail(&e))?; + z.add(&name, data, if store { craft_zip::Compression::Stored } else { craft_zip::Compression::Deflated }).map_err(fail)?; } - z.finish().map(std::io::Cursor::into_inner).map_err(|e| fail(&e)) + z.finish().map_err(fail) } diff --git a/crates/epub/tests/read.rs b/crates/epub/tests/read.rs index 88fb27df6..dd1999ce4 100644 --- a/crates/epub/tests/read.rs +++ b/crates/epub/tests/read.rs @@ -252,3 +252,11 @@ fn a_package_document_past_the_entry_limit_is_refused() { let limits = Limits { max_entry_bytes: (xml.len() - 1) as u64, ..Limits::DEFAULT }; assert!(matches!(parse_package(&xml, "", &limits), Err(Error::Part { .. }))); } + +/// A ZIP64 end record can declare millions of entries in a small file; the count is refused on +/// the raw bytes, before the archive is indexed. +#[test] +fn an_archive_declaring_too_many_entries_is_refused_before_it_is_indexed() { + let bomb = craft_zip::zip64_entry_count_sample(100_001); + assert_eq!(Epub::open(&bomb).err(), Some(Error::TooManyEntries { found: 100_001, limit: 100_000 })); +} diff --git a/crates/idml/Cargo.toml b/crates/idml/Cargo.toml index 660456229..41df76d7e 100644 --- a/crates/idml/Cargo.toml +++ b/crates/idml/Cargo.toml @@ -13,8 +13,9 @@ workspace = true [dependencies] craft-xml = { workspace = true } thiserror = { workspace = true } -# `deflate-flate2` with flate2's default miniz_oxide backend, as craft-update pins it (zip's own -# `deflate` feature would switch a whole app's flate2 to zlib-rs). +craft-zip = { workspace = true } + +[dev-dependencies] +# The tests build hostile archives by hand. zip = { version = "9", default-features = false, features = ["deflate-flate2"] } -# The backend of zip's deflate: `deflate-flate2` leaves the choice to the build. -flate2 = { version = "1", default-features = false, features = ["rust_backend"] } +craft-zip = { workspace = true, features = ["test-support"] } diff --git a/crates/idml/README.md b/crates/idml/README.md index 0ef741bf2..16435a361 100644 --- a/crates/idml/README.md +++ b/crates/idml/README.md @@ -53,7 +53,8 @@ for line in &doc.warnings { /* a part designmap.xml lists and the package lacks | `xml.max_depth` | 256 | `Error::Part` | | `xml.max_nodes`, `xml.max_attrs_total` (one part) | 8,000,000 and 32,000,000 | `Error::Part` | -Sizes are checked against what a part really expands to, not what the archive declares. +The ZIP layer is craft-zip: the declared entry count is checked before the archive is indexed, and +sizes are checked against what a part really expands to, not what the archive declares. ## Writing diff --git a/crates/idml/src/package.rs b/crates/idml/src/package.rs index c8b6ebec2..dab9d3626 100644 --- a/crates/idml/src/package.rs +++ b/crates/idml/src/package.rs @@ -1,7 +1,6 @@ //! The ZIP container: bounded part reads, lenient part names, designmap includes, and the writer. use std::collections::HashMap; -use std::io::{Cursor, Read, Write}; use craft_xml::El; @@ -34,12 +33,10 @@ impl Default for Limits { /// An IDML package whose parts are decompressed only when asked for, within byte budgets that /// don't trust the sizes the archive claims. pub struct Package<'a> { - zip: zip::ZipArchive>, + zip: craft_zip::Archive<'a>, /// Normalised part name → entry name, first entry wins (zip order, so the choice is stable). lenient: HashMap, limits: Limits, - /// Bytes the parts still to be read may expand to. - left: u64, } /// `designmap.xml` with the parts it includes. @@ -63,46 +60,40 @@ impl<'a> Package<'a> { Self::open_with(bytes, Limits::DEFAULT) } + /// The declared entry count is checked against [`Limits::max_entries`] before the archive + /// is indexed. pub fn open_with(bytes: &'a [u8], limits: Limits) -> Result { - let zip = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| Error::NotIdml(e.to_string()))?; - if zip.len() > limits.max_entries { - return Err(Error::TooManyEntries { found: zip.len(), limit: limits.max_entries }); - } + let zip = + craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries as u64, max_total_bytes: limits.max_total_bytes }) + .map_err(|e| match e { + craft_zip::Error::TooManyEntries { declared, limit } => Error::TooManyEntries { + found: usize::try_from(declared).unwrap_or(usize::MAX), + limit: usize::try_from(limit).unwrap_or(usize::MAX), + }, + e => Error::NotIdml(e.to_string()), + })?; let mut lenient: HashMap = HashMap::new(); - for i in 0..zip.len() { - let Some(Ok(name)) = zip.name_for_index(i) else { continue }; - if name.ends_with('/') { - continue; - } + for name in zip.names().filter(|n| !n.ends_with('/')) { lenient.entry(normalize_part_name(&name)).or_insert_with(|| name.into_owned()); } - Ok(Package { zip, lenient, limits, left: limits.max_total_bytes }) + Ok(Package { zip, lenient, limits }) } /// The bytes of part `name` (matched exactly, then leniently: writers differ in /// percent-encoding, case, path separators and a leading `./` or `/`), or `None` if the /// package has no such part. pub fn part(&mut self, name: &str) -> Result>> { - let exact = self.zip.index_for_name(name).filter(|_| !name.ends_with('/')); - let Some(i) = exact.or_else(|| self.lenient.get(&normalize_part_name(name)).and_then(|n| self.zip.index_for_name(n))) else { + let exact = (self.zip.contains(name) && !name.ends_with('/')).then_some(name); + let Some(stored) = exact.or_else(|| self.lenient.get(&normalize_part_name(name)).map(String::as_str)) else { return Ok(None); }; - let part = self.zip.name_for_index(i).and_then(|n| n.ok()).map_or_else(|| name.to_string(), |n| n.into_owned()); - let f = self.zip.by_index(i).map_err(|e| Error::NotIdml(e.to_string()))?; - let limit = self.limits.max_part_bytes.min(self.left); - if f.size() > limit { - return Err(Error::TooLarge { part, limit }); - } - let mut buf = Vec::new(); - if let Err(e) = f.take(limit.saturating_add(1)).read_to_end(&mut buf) { - return Err(Error::Part { part, msg: e.to_string() }); - } - let n = buf.len() as u64; - if n > limit { - return Err(Error::TooLarge { part, limit }); - } - self.left = self.left.saturating_sub(n); - Ok(Some(buf)) + let limit = self.limits.max_part_bytes.min(self.zip.budget().remaining()); + let stored = stored.to_string(); + self.zip.read(&stored, self.limits.max_part_bytes).map_err(|e| match e { + craft_zip::Error::EntryTooLarge { .. } | craft_zip::Error::BudgetSpent { .. } => Error::TooLarge { part: stored.clone(), limit }, + craft_zip::Error::Entry { msg, .. } => Error::Part { part: stored.clone(), msg }, + e => Error::NotIdml(e.to_string()), + }) } /// Part `name` as an element tree ([`crate::parse_part`]). @@ -114,7 +105,7 @@ impl<'a> Package<'a> { /// Bytes the parts still to be read may expand to. pub fn budget_left(&self) -> u64 { - self.left + self.zip.budget().remaining() } /// Reads `designmap.xml` and every part it includes. @@ -173,19 +164,14 @@ fn normalize_part_name(name: &str) -> String { } /// Zips an IDML package: the stored `mimetype` first, then `parts` in order as (name, bytes). +/// Equal parts give equal bytes. pub fn write_package, D: AsRef<[u8]>>(parts: &[(N, D)]) -> Result> { - use zip::write::SimpleFileOptions; - let fail = |e: &dyn std::fmt::Display| Error::Write(e.to_string()); - let mut w = zip::ZipWriter::new(Cursor::new(Vec::new())); - let stored = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); - let deflated = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated); - w.start_file("mimetype", stored).map_err(|e| fail(&e))?; - w.write_all(MIMETYPE.as_bytes()).map_err(|e| fail(&e))?; + use craft_zip::Compression; + let fail = |e: craft_zip::Error| Error::Write(e.to_string()); + let mut w = craft_zip::Writer::new(); + w.add("mimetype", MIMETYPE.as_bytes(), Compression::Stored).map_err(fail)?; for (name, data) in parts { - let data = data.as_ref(); - // More than 4 GiB needs the ZIP64 form. - w.start_file(name.as_ref(), deflated.large_file(data.len() >= u32::MAX as usize)).map_err(|e| fail(&e))?; - w.write_all(data).map_err(|e| fail(&e))?; + w.add(name.as_ref(), data.as_ref(), Compression::Deflated).map_err(fail)?; } - w.finish().map(Cursor::into_inner).map_err(|e| fail(&e)) + w.finish().map_err(fail) } diff --git a/crates/idml/tests/package.rs b/crates/idml/tests/package.rs index 1c7ffd353..03a42d088 100644 --- a/crates/idml/tests/package.rs +++ b/crates/idml/tests/package.rs @@ -244,3 +244,16 @@ fn parts_are_written_one_element_per_line_without_touching_text() { ); assert!(with_props(El::new("A"), Vec::new()).children.is_empty()); } + +/// A ZIP64 end record can declare millions of entries in a small file; the count is refused on +/// the raw bytes, before the archive is indexed. +#[test] +fn a_package_declaring_too_many_entries_is_refused_before_it_is_indexed() { + let bomb = craft_zip::zip64_entry_count_sample(100_001); + assert_eq!(Package::open(&bomb).err(), Some(Error::TooManyEntries { found: 100_001, limit: 100_000 })); +} + +#[test] +fn equal_parts_give_equal_bytes() { + assert_eq!(sample(), sample()); +} From 16936716aa960dda9a7f1532bb04a53caa3a5c75 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:24:06 -0500 Subject: [PATCH 049/120] Add craft-stream-codecs: the codec modules of PdfCraft's stream filters (#953) LZW, ASCII85, ASCIIHex, RunLength, the TIFF and PNG predictors, bounded CCITT fax decoding (through hayro-ccitt) with Group 4 encoding (through fax), and the lossless JBIG2 encoder with its MQ coder, from pdfcraft-filters at PdfCraft revision 80e26e674d1aa33078e83c8139875047f3289eb6. The crate sits below craft-pdf and craft-tiff: it knows no container. The PDF filter chain, Flate and the bounded JBIG2 decoding entry are not part of it. --- Cargo.lock | 22 +- Cargo.toml | 1 + README.md | 5 + crates/stream-codecs/Cargo.toml | 24 + crates/stream-codecs/README.md | 78 ++ crates/stream-codecs/src/ascii85.rs | 156 +++ crates/stream-codecs/src/asciihex.rs | 106 ++ crates/stream-codecs/src/ccitt.rs | 546 +++++++++ crates/stream-codecs/src/jbig2.rs | 468 ++++++++ crates/stream-codecs/src/jbig2/symbols.rs | 1329 +++++++++++++++++++++ crates/stream-codecs/src/lib.rs | 117 ++ crates/stream-codecs/src/lzw.rs | 314 +++++ crates/stream-codecs/src/mq.rs | 209 ++++ crates/stream-codecs/src/predictor.rs | 356 ++++++ crates/stream-codecs/src/runlength.rs | 117 ++ 15 files changed, 3846 insertions(+), 2 deletions(-) create mode 100644 crates/stream-codecs/Cargo.toml create mode 100644 crates/stream-codecs/README.md create mode 100644 crates/stream-codecs/src/ascii85.rs create mode 100644 crates/stream-codecs/src/asciihex.rs create mode 100644 crates/stream-codecs/src/ccitt.rs create mode 100644 crates/stream-codecs/src/jbig2.rs create mode 100644 crates/stream-codecs/src/jbig2/symbols.rs create mode 100644 crates/stream-codecs/src/lib.rs create mode 100644 crates/stream-codecs/src/lzw.rs create mode 100644 crates/stream-codecs/src/mq.rs create mode 100644 crates/stream-codecs/src/predictor.rs create mode 100644 crates/stream-codecs/src/runlength.rs diff --git a/Cargo.lock b/Cargo.lock index c7fe9c680..2fc2f8292 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1588,6 +1588,18 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-stream-codecs" +version = "0.1.0" +dependencies = [ + "fax", + "hayro-ccitt", + "hayro-jbig2", + "proptest", + "thiserror 2.0.21", + "weezl 0.2.1", +] + [[package]] name = "craft-svg" version = "0.1.0" @@ -2777,7 +2789,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee8cfcc411d9adbbaba82fb72661cc1bcca13e8bba98b364e62b2dba8f960159" dependencies = [ "color_quant", - "weezl", + "weezl 0.1.12", ] [[package]] @@ -6731,7 +6743,7 @@ dependencies = [ "flate2", "half", "quick-error 2.0.1", - "weezl", + "weezl 0.1.12", "zune-jpeg", ] @@ -7590,6 +7602,12 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28ac98ddc8b9274cb41bb4d9d4d5c425b6020c50c46f25559911905610b4a88" +[[package]] +name = "weezl" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4ca08e5ef825b65b056d9efbd95c8750683f0a6d0466d02e96dc2e4e360f3d2" + [[package]] name = "wgpu" version = "30.0.1" diff --git a/Cargo.toml b/Cargo.toml index 7059505ca..3634d3b8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,6 +91,7 @@ craft-settings = { path = "crates/settings" } craft-silhouette-studio = { path = "crates/silhouette-studio" } craft-svg = { path = "crates/svg" } craft-svg-paths = { path = "crates/svg-paths" } +craft-stream-codecs = { path = "crates/stream-codecs" } craft-text = { path = "crates/text" } craft-tga = { path = "crates/tga" } craft-timeline = { path = "crates/timeline" } diff --git a/README.md b/README.md index 3776d0b56..b48364a81 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,11 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = "` form on craft-xml elements, value codecs and the package writer; and InDesign Tagged Text, read and written. +- [`craft-stream-codecs`](crates/stream-codecs/README.md): byte-stream and bi-level image codecs that + PDF, PostScript and TIFF share: LZW, ASCII85, ASCIIHex, RunLength, TIFF and PNG predictors, + CCITT Group 3 and 4 decoding and Group 4 encoding, lossless JBIG2 encoding. Every + decoder is bounded and returns its partial output on damage. From PdfCraft; a PDF filter chain + is built on it in `craft-pdf`. - [`craft-timeline`](crates/timeline/README.md): editorial timeline interchange: exact media time and SMPTE timecode, import/export reports, media path and `file://` URL handling, Avid Log Exchange (ALE) and CMX 3600 EDL documents, format detection. From FilmCraft. - [`craft-metadata`](crates/metadata/README.md): photo metadata, from LightCraft: EXIF (read and write, maker-note lens and focus point, every tag by name), IPTC-IIM, XMP packets (read, write, and a merge diff --git a/crates/stream-codecs/Cargo.toml b/crates/stream-codecs/Cargo.toml new file mode 100644 index 000000000..7624050e2 --- /dev/null +++ b/crates/stream-codecs/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "craft-stream-codecs" +description = "Byte-stream and bi-level image codecs that PDF, PostScript and TIFF share: LZW, ASCII85, ASCIIHex, RunLength, TIFF and PNG predictors, CCITT Group 3 and 4 decoding and Group 4 encoding, lossless JBIG2 encoding" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +# CCITT Group 4 encoding (ITU-T T.6). MIT. +fax = "0.2.7" +# CCITT fax decoding, the decoder the vendored PDF reader uses. MIT OR Apache-2.0. +hayro-ccitt = { version = "0.3", default-features = false } +thiserror = { workspace = true } + +[dev-dependencies] +# The JBIG2 decoder the vendored PDF reader uses: reads back what the encoder writes. +hayro-jbig2 = { version = "=0.3.0", default-features = false, features = ["std"] } +proptest = { workspace = true } +weezl = "0.2" diff --git a/crates/stream-codecs/README.md b/crates/stream-codecs/README.md new file mode 100644 index 000000000..3a6c73208 --- /dev/null +++ b/crates/stream-codecs/README.md @@ -0,0 +1,78 @@ +# craft-stream-codecs + +Byte-stream and bi-level image codecs that PDF, PostScript and TIFF share. The crate knows no +container: a caller hands it bytes and parameters. A PDF `/Filter` chain (stage order, limits +across stages, Flate, accounting) belongs to `craft-pdf`, which builds on this crate; a TIFF +reader can use the same LZW, predictor, CCITT and JBIG2 code. + +- **Licence:** MIT OR Apache-2.0. Written from ISO 32000-2 §7.4, TIFF 6.0 §14 (predictor 2) and + RFC 2083 §6 (PNG filters). CCITT Group 4 encoding (ITU-T T.6) is the `fax` crate's (MIT). CCITT + decoding (T.4 and T.6) is `hayro-ccitt`'s (MIT OR Apache-2.0), bounded here: this crate has + no CCITT decoder of its own, and no JBIG2 decoder (that is `hayro-jbig2`, which the tests + read the encoder's output back with). JBIG2 encoding + is written from the public ISO/IEC 14492 Final Committee Draft (WG1 N1359, 1999); Annex H.2 + checks the arithmetic coder byte for byte. +- No `unsafe`. Never panics on input. +- From PdfCraft's `pdfcraft-filters` crate (revision `80e26e674d1aa33078e83c8139875047f3289eb6`): + its codec modules, moved whole. Its filter chain and Flate stay with the PDF code. + +## API + +```rust +pub struct Params { predictor, colors, bits_per_component, columns, early_change } // i64s; Default = 1, 1, 8, 1, 1 +pub enum CodecError { Corrupt { filter, detail }, LimitExceeded(usize), Unsupported(String), Spent } +pub struct Failure { error, partial, discarded } // what a decoder decoded before it failed +pub type Step = Result, Failure>; +pay(work: &mut u64, steps) -> Result<(), CodecError> // an allowance shared across a run of images + +lzw::decode(data, early_change, max) -> Step; lzw::encode(data, early_change) -> Vec +ascii85::decode(data, max) -> Step; ascii85::encode(data) -> Vec +asciihex::decode(data, max) -> Step; asciihex::encode(data) -> Vec +runlength::decode(data, max) -> Step; runlength::encode(data) -> Vec +predictor::decode(¶ms, data, name, rows) -> Step; predictor::encode(¶ms, data) -> Result, CodecError> + +pub struct CcittParams { k, columns, rows, end_of_line, encoded_byte_align, end_of_block, black_is_1 } +decode_ccitt_within(data, ¶ms, max_rows, work: &mut u64) -> Result, CodecError> +encode_g4(data, columns, rows) -> Result, CodecError> + +encode_jbig2_generic(data, columns, rows) -> Result, CodecError> +encode_jbig2(data, columns, rows) -> Result, CodecError> +encode_jbig2_shared(&[(data, columns, rows)]) -> Result, CodecError> +``` + +`max` bounds a decoder's output and gives `LimitExceeded`. `Corrupt` names the codec in `filter` +by its PDF filter name. Bi-level images are rows of 1-bit samples, most significant bit first, +each row on a new byte. + +## Behaviour + +| Codec | Decode | Encode | +|---|---|---| +| LZW | 9–12-bit MSB-first codes, clear (256), end (257), early change on or off; a missing end code or initial clear is tolerated; a full table stops growing | greedy, leading clear, clear when the table fills, end code | +| Predictors | 2 = TIFF (1/2/4/8/16 bits per component), 10–15 = PNG with per-row tags 0–4; partial final rows decoded; bad parameters or tags are `Corrupt`; with `rows`, data that is exactly that many rows without tag bytes is taken as the samples and reported as damage | 2, 10 None, 11 Sub, 12 Up, 13 Average, 14 Paeth, 15 per-row optimum | +| ASCIIHex | white space ignored, odd digit padded with 0, `>` ends (optional), other characters are `Corrupt` | upper case, newline every 64 digits, `>` | +| ASCII85 | `<~` prefix, `z`, white space, `~>` (or bare `~`, or none), final partial group; `z` inside a group, a 1-character final group, overflow or bad characters are `Corrupt` | `z` for zero groups, newline every 75 characters, `~>` | +| RunLength | 0–127 literal, 129–255 repeat, 128 ends (optional); a truncated run is `Corrupt` | runs of 2 or more repeat, literals up to 128, 128 | +| CCITT fax | Group 3 (1-D and 2-D) and Group 4 through `hayro-ccitt`, as rows of 1-bit samples, black 0 unless `black_is_1`, at most `max_rows` rows (fewer with `rows`), the rows before any damage, up to `MAX_CCITT_COLUMNS` (1,048,576) samples a row; the rows are paid for from `work` before they are decoded | `encode_g4`: Group 4 with an end-of-block mark, 0 bits coded black; up to `MAX_G4_COLUMNS` (32,767) samples a row | +| JBIG2 | not here: `hayro-jbig2` | `encode_jbig2_generic`: one immediate lossless generic region (template 0, typical prediction, MQ coder). `encode_jbig2`: the smaller of that and symbol coding (exact copies only, so the stream decodes to the same bits). `encode_jbig2_shared`: a group of images sharing one symbol dictionary (the PDF `/JBIG2Globals` data) | + +The limits of the JBIG2 encoder (symbols per plan, instances per text region, marks, runs) are +those `hayro-jbig2` reads back; they are documented on the constants in `src/jbig2/symbols.rs`. + +## Tests + +- Unit tests per codec with hand-computed vectors (the ISO 32000-2 LZW example, PNG and TIFF rows). +- LZW is cross-checked against `weezl` (dev-dependency) in both directions for both early-change settings. +- `ccitt.rs`: decoding gives back the rows `encode_g4` was given; Group 3 layouts over seeded + pictures; Group 4 output decoded by `hayro-ccitt` matches the input bits, with property tests + over bitmaps of noise and of runs; any input returns rather than panics. +- `mq.rs`: the arithmetic coder reproduces Annex H.2's 30 bytes from its 256 decisions. +- `jbig2.rs`, `jbig2/symbols.rs`: output decoded by `hayro-jbig2` matches the input bits for + every coding, plan and strip size, with property tests; size and limit refusals. + +## Not here + +A bounded JBIG2 decoding entry (`decode_jbig2_within` in PdfCraft): it needs the caller-owned +work allowance PdfCraft added to its `hayro-jbig2` (`Image::decode_within`, `charge_reading`), +which the copy vendored here does not have yet. Flate (a container's choice of zlib settings and tolerances), DCT and JPEG 2000, CCITT Group 3 +encoding. diff --git a/crates/stream-codecs/src/ascii85.rs b/crates/stream-codecs/src/ascii85.rs new file mode 100644 index 000000000..beb0098fd --- /dev/null +++ b/crates/stream-codecs/src/ascii85.rs @@ -0,0 +1,156 @@ +//! ASCII85Decode (ISO 32000-2 §7.4.3): groups of five characters `!`..`u` encode four +//! bytes base-85, `z` stands for four zero bytes, `~>` is EOD, white space is ignored and +//! a final partial group of n characters (2–5) yields n − 1 bytes. A leading `<~` and a +//! missing EOD are tolerated. + +use crate::{Failure, Step, is_pdf_whitespace}; + +const NAME: &str = "ASCII85Decode"; + +fn push(out: &mut Vec, bytes: &[u8], max: usize) -> Result<(), Failure> { + if out.len() + bytes.len() > max { + return Err(Failure::limit(max)); + } + out.extend_from_slice(bytes); + Ok(()) +} + +pub fn decode(data: &[u8], max: usize) -> Step { + let start = data.iter().position(|&b| !is_pdf_whitespace(b)).unwrap_or(data.len()); + let body = data[start..].strip_prefix(b"<~").unwrap_or(&data[start..]); + let mut out = Vec::with_capacity((body.len() / 5 * 4 + 4).min(max)); + let mut acc: u64 = 0; + let mut n = 0usize; + for (i, &b) in body.iter().enumerate() { + match b { + b'!'..=b'u' => { + acc = acc * 85 + u64::from(b - b'!'); + n += 1; + if n == 5 { + let Ok(v) = u32::try_from(acc) else { + return Err(Failure::corrupt(NAME, format!("group ending at {i} exceeds 2^32"), out)); + }; + push(&mut out, &v.to_be_bytes(), max)?; + acc = 0; + n = 0; + } + } + b'z' if n == 0 => push(&mut out, &[0; 4], max)?, + b'z' => return Err(Failure::corrupt(NAME, format!("'z' inside a group at {i}"), out)), + // `~` starts the EOD marker; tolerate it without the following `>`. + b'~' => break, + b if is_pdf_whitespace(b) => {} + b => return Err(Failure::corrupt(NAME, format!("invalid character 0x{b:02x} at {i}"), out)), + } + } + match n { + 0 => {} + 1 => return Err(Failure::corrupt(NAME, "final group has a single character", out)), + _ => { + // Pad with `u` (84) to a full group and keep n − 1 bytes. + for _ in n..5 { + acc = acc * 85 + 84; + } + let Ok(v) = u32::try_from(acc) else { + return Err(Failure::corrupt(NAME, "final group exceeds 2^32", out)); + }; + push(&mut out, &v.to_be_bytes()[..n - 1], max)?; + } + } + Ok(out) +} + +/// Encodes with `z` for zero groups, a newline every 75 characters, and a `~>` EOD. +pub fn encode(data: &[u8]) -> Vec { + let mut out = Vec::with_capacity(data.len() * 5 / 4 + data.len() / 60 + 8); + let mut col = 0; + let mut put = |out: &mut Vec, s: &[u8]| { + for &c in s { + if col == 75 { + out.push(b'\n'); + col = 0; + } + out.push(c); + col += 1; + } + }; + for chunk in data.chunks(4) { + let mut word = [0u8; 4]; + word[..chunk.len()].copy_from_slice(chunk); + let mut v = u32::from_be_bytes(word); + if v == 0 && chunk.len() == 4 { + put(&mut out, b"z"); + continue; + } + let mut digits = [0u8; 5]; + for d in digits.iter_mut().rev() { + *d = (v % 85) as u8 + b'!'; + v /= 85; + } + put(&mut out, &digits[..chunk.len() + 1]); + } + out.extend_from_slice(b"~>"); + out +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::CodecError; + + fn dec(d: &[u8]) -> Result, CodecError> { + decode(d, 1000).map_err(|f| f.error) + } + + #[test] + fn vectors() { + // "Man " = 0x4D616E20 = 24·85⁴ + 73·85³ + 80·85² + 78·85 + 61 -> "9jqo^". + assert_eq!(dec(b"9jqo^~>").unwrap(), b"Man "); + assert_eq!(encode(b"Man "), b"9jqo^~>"); + // "M" padded to 0x4D000000 -> "9`" (24, 63); decoding pads with 'u'. + assert_eq!(encode(b"M"), b"9`~>"); + assert_eq!(dec(b"9`~>").unwrap(), b"M"); + assert_eq!(dec(b"z~>").unwrap(), [0; 4]); + assert_eq!(encode(&[0; 4]), b"z~>"); + // A partial zero group is not `z`. + assert_eq!(encode(&[0; 3]), b"!!!!~>"); + assert_eq!(dec(b"!!!!~>").unwrap(), [0; 3]); + assert_eq!(dec(b"s8W-!~>").unwrap(), [0xff; 4]); + } + + #[test] + fn syntax_tolerance() { + assert_eq!(dec(b"<~9jqo^~>").unwrap(), b"Man "); + assert_eq!(dec(b" \n<~9j\r\nq o\t^z~>").unwrap(), b"Man \0\0\0\0"); + assert_eq!(dec(b"9jqo^").unwrap(), b"Man "); // missing EOD + assert_eq!(dec(b"9jqo^~").unwrap(), b"Man "); // bare ~ + assert_eq!(dec(b"9jqo^~>garbage{").unwrap(), b"Man "); + assert_eq!(dec(b"~>").unwrap(), b""); + assert_eq!(dec(b"").unwrap(), b""); + } + + #[test] + fn corrupt() { + assert!(matches!(dec(b"9jz~>"), Err(CodecError::Corrupt { .. }))); + assert!(matches!(dec(b"9jqo^9~>"), Err(CodecError::Corrupt { .. }))); + assert!(matches!(dec(b"uuuuu~>"), Err(CodecError::Corrupt { .. }))); // > 2^32 - 1 + assert!(matches!(dec(b"9jqo^v~>"), Err(CodecError::Corrupt { .. }))); + let f = decode(b"9jqo^{", 100).err().unwrap(); + assert_eq!(f.partial, b"Man "); + } + + #[test] + fn limit() { + assert!(matches!(decode(b"zz", 7).map_err(|f| f.error), Err(CodecError::LimitExceeded(7)))); + assert!(matches!(decode(b"z9`", 4).map_err(|f| f.error), Err(CodecError::LimitExceeded(4)))); + assert_eq!(decode(b"zz", 8).ok().unwrap().len(), 8); + } + + #[test] + fn line_wrapping() { + let data: Vec = (0..=255u8).collect(); + let e = encode(&data); + assert!(e.split(|&b| b == b'\n').all(|l| l.len() <= 77)); + assert_eq!(dec(&e).unwrap_or_default().len(), 256); + } +} diff --git a/crates/stream-codecs/src/asciihex.rs b/crates/stream-codecs/src/asciihex.rs new file mode 100644 index 000000000..60e962216 --- /dev/null +++ b/crates/stream-codecs/src/asciihex.rs @@ -0,0 +1,106 @@ +//! ASCIIHexDecode (ISO 32000-2 §7.4.2): pairs of hex digits, white space ignored, +//! `>` is EOD, an odd final digit is padded with 0. A missing EOD is tolerated. + +use crate::{Failure, Step, is_pdf_whitespace}; + +const NAME: &str = "ASCIIHexDecode"; + +fn nibble(b: u8) -> Option { + match b { + b'0'..=b'9' => Some(b - b'0'), + b'a'..=b'f' => Some(b - b'a' + 10), + b'A'..=b'F' => Some(b - b'A' + 10), + _ => None, + } +} + +pub fn decode(data: &[u8], max: usize) -> Step { + let mut out = Vec::with_capacity((data.len() / 2).min(max)); + let mut hi: Option = None; + for (i, &b) in data.iter().enumerate() { + if is_pdf_whitespace(b) { + continue; + } + if b == b'>' { + break; + } + let Some(n) = nibble(b) else { + return Err(Failure::corrupt(NAME, format!("invalid character 0x{b:02x} at offset {i}"), out)); + }; + match hi.take() { + None => hi = Some(n), + Some(h) => { + if out.len() == max { + return Err(Failure::limit(max)); + } + out.push(h << 4 | n); + } + } + } + if let Some(h) = hi { + if out.len() == max { + return Err(Failure::limit(max)); + } + out.push(h << 4); + } + Ok(out) +} + +/// Upper-case hex, a newline every 64 digits, terminated by `>`. +pub fn encode(data: &[u8]) -> Vec { + const HEX: &[u8; 16] = b"0123456789ABCDEF"; + let mut out = Vec::with_capacity(data.len() * 2 + data.len() / 32 + 1); + for (i, &b) in data.iter().enumerate() { + if i > 0 && i % 32 == 0 { + out.push(b'\n'); + } + out.push(HEX[usize::from(b >> 4)]); + out.push(HEX[usize::from(b & 15)]); + } + out.push(b'>'); + out +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::CodecError; + + fn dec(d: &[u8]) -> Result, CodecError> { + decode(d, 1000).map_err(|f| f.error) + } + + #[test] + fn vectors() { + assert_eq!(dec(b"48656c6C6F>").unwrap(), b"Hello"); + assert_eq!(dec(b" 4 8\n6\r5\t6\x0c c\x006c 6f >").unwrap(), b"Hello"); + assert_eq!(dec(b"901FA>").unwrap(), [0x90, 0x1f, 0xa0]); + assert_eq!(dec(b"901FA").unwrap(), [0x90, 0x1f, 0xa0]); // missing EOD + assert_eq!(dec(b"41>zz garbage after EOD").unwrap(), b"A"); + assert_eq!(dec(b">").unwrap(), b""); + assert_eq!(dec(b"").unwrap(), b""); + } + + #[test] + fn invalid_char() { + let f = decode(b"4142G43>", 1000).err().unwrap(); + assert!(matches!(f.error, CodecError::Corrupt { .. })); + assert_eq!(f.partial, b"AB"); + } + + #[test] + fn limit() { + assert!(matches!(decode(b"414243", 2).map_err(|f| f.error), Err(CodecError::LimitExceeded(2)))); + assert!(matches!(decode(b"41424", 2).map_err(|f| f.error), Err(CodecError::LimitExceeded(2)))); + assert_eq!(decode(b"4142", 2).ok().unwrap(), b"AB"); + } + + #[test] + fn encoding() { + assert_eq!(encode(b"Hello"), b"48656C6C6F>"); + assert_eq!(encode(b""), b">"); + let long = encode(&[0xab; 40]); + assert_eq!(long[64], b'\n'); + assert_eq!(dec(&long).unwrap(), [0xab; 40]); + } +} diff --git a/crates/stream-codecs/src/ccitt.rs b/crates/stream-codecs/src/ccitt.rs new file mode 100644 index 000000000..2733cf98b --- /dev/null +++ b/crates/stream-codecs/src/ccitt.rs @@ -0,0 +1,546 @@ +//! CCITT fax for `CCITTFaxDecode` (ISO 32000-2 §7.4.6): Group 4 (ITU-T T.6) encoding, with `/K -1`, +//! and decoding of Group 3 (T.4) and Group 4. The coding is the `fax` crate's and the decoding the +//! `hayro-ccitt` crate's; this module checks what they are given and bounds what they give. + +use fax::encoder::Encoder; +use fax::{Color, VecWriter}; + +use crate::CodecError; + +/// The widest row [`encode_g4`] encodes. The encoder counts the positions in a row in 16 bits +/// and adds two of them, so a row stays within half that range. +pub const MAX_G4_COLUMNS: u32 = 32_767; + +/// `rows` rows of `columns` 1-bit samples, most significant bit first, each row starting on a new +/// byte (as a PDF image's samples are packed), encoded as CCITT Group 4 with an end-of-block mark. +/// +/// A 0 bit is coded black and a 1 bit white, so the stream decodes to the same bits with +/// `/BlackIs1 false`, the default: an image keeps its colour space and decode array. Bytes after +/// the last row are ignored, as are the bits that pad a row to a whole byte. +/// +/// Errors: no rows or columns, or rows wider than [`MAX_G4_COLUMNS`] ([`CodecError::Unsupported`]); +/// fewer bytes than the rows need ([`CodecError::Corrupt`]). +pub fn encode_g4(data: &[u8], columns: u32, rows: u32) -> Result, CodecError> { + let width = match u16::try_from(columns) { + Ok(w) if w > 0 && columns <= MAX_G4_COLUMNS => w, + _ => return Err(CodecError::Unsupported(format!("CCITT Group 4 encoding of rows {columns} samples wide"))), + }; + if rows == 0 { + return Err(CodecError::Unsupported("CCITT Group 4 encoding of an image without rows".into())); + } + let stride = usize::from(width).div_ceil(8); + let samples = usize::try_from(rows).ok().and_then(|r| r.checked_mul(stride)).and_then(|n| data.get(..n)).ok_or_else(|| CodecError::Corrupt { + filter: "CCITTFaxDecode", + detail: format!("{} bytes for {rows} rows of {columns} samples", data.len()), + })?; + let mut encoder = Encoder::new(VecWriter::new()); + for row in samples.chunks_exact(stride) { + let pels = (0..usize::from(width)).map(|x| { + let white = row.get(x / 8).is_some_and(|b| b & (0x80 >> (x % 8)) != 0); + if white { Color::White } else { Color::Black } + }); + // `VecWriter` writes to memory: its error type has no values. + if let Err(never) = encoder.encode_line(pels, width) { + match never {} + } + } + match encoder.finish() { + Ok(writer) => Ok(writer.finish()), + Err(never) => match never {}, + } +} + +/// The widest row [`decode_ccitt_within`] decodes: its row buffer stays within 128 KiB. +pub const MAX_CCITT_COLUMNS: u32 = 1 << 20; + +/// A `CCITTFaxDecode` stream's `/DecodeParms` (ISO 32000-2 Table 11). [`Default`] gives the +/// table's defaults. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct CcittParams { + /// `/K`: negative for Group 4, 0 for Group 3 one-dimensional, positive for Group 3 mixed. + pub k: i64, + pub columns: i64, + /// `/Rows`: 0 when not given. + pub rows: i64, + pub end_of_line: bool, + pub encoded_byte_align: bool, + pub end_of_block: bool, + pub black_is_1: bool, +} + +impl Default for CcittParams { + fn default() -> Self { + CcittParams { k: 0, columns: 1728, rows: 0, end_of_line: false, encoded_byte_align: false, end_of_block: true, black_is_1: false } + } +} + +/// What [`decode_ccitt_within`] decodes once it is paid for. +fn decode_ccitt(data: &[u8], p: &CcittParams, max_rows: u32) -> Result, CodecError> { + let columns = u32::try_from(p.columns) + .ok() + .filter(|c| (1..=MAX_CCITT_COLUMNS).contains(c)) + .ok_or_else(|| CodecError::Unsupported(format!("CCITT fax rows {} samples wide", p.columns)))?; + let rows = u32::try_from(p.rows).ok().filter(|r| *r > 0).map_or(max_rows, |r| r.min(max_rows)); + if rows == 0 { + return Ok(Vec::new()); + } + let settings = hayro_ccitt::DecodeSettings { + columns, + rows, + end_of_block: p.end_of_block, + end_of_line: p.end_of_line, + // Group 3 rows on bytes may have end-of-line codes whatever `end_of_line` says (the filter + // always accepts them), with the fill starting each code on a byte or ending it on one (T.4's + // fill, as TIFF writes it). Read unaligned, the codes mark the rows either way; without + // codes, the rows are read on bytes. A reading that comes up short isn't tried the other + // way: data cut short would then read as other rows. + rows_are_byte_aligned: p.encoded_byte_align && (p.k < 0 || !starts_with_end_of_line(data)), + encoding: match p.k { + ..0 => hayro_ccitt::EncodingMode::Group4, + 0 => hayro_ccitt::EncodingMode::Group3_1D, + k => hayro_ccitt::EncodingMode::Group3_2D { k: u32::try_from(k).unwrap_or(u32::MAX) }, + }, + invert_black: false, + }; + // u32 to usize: wasm32 and up have at least 32-bit usize. + let mut out = Rows::new(columns as usize, rows as usize, p.black_is_1); + // The rows decoded before damaged data are kept. + let _ = hayro_ccitt::decode(data, &mut out, &mut hayro_ccitt::DecoderContext::new(settings)); + Ok(out.out) +} + +/// The steps of reading a byte of fax code ([`decode_ccitt_within`]): noise takes 114 ns a byte of +/// rows and about two bytes of code to a byte of rows, so that and a step a byte of rows is about +/// what a step of JBIG2 decoding takes. +const CODE_WORK: u64 = 4; + +/// CCITT fax `data` decoded as `CCITTFaxDecode` gives it: rows of `columns` 1-bit samples, most +/// significant bit first, each starting on a new byte, with black as 0 (as 1 with `black_is_1`). +/// +/// At most `max_rows` rows are decoded (fewer when `rows` says so), so the output is bounded by +/// the caller. Damaged data gives the rows decoded before the damage, as viewers show them: a +/// caller that needs a number of rows checks it got them. +/// +/// Group 3 rows on bytes (`k` from 0, `encoded_byte_align`) are read by their end-of-line codes, +/// whether the fill starts each code on a byte or ends it on one, when `data` starts with one (T.4 +/// sends one before the first row when it sends them at all), and on bytes otherwise, whatever +/// `end_of_line` says. Group 4 is read as its parameters say. Data with codes but none before the +/// first row is read on bytes, as hayro's renderer draws it; with `k` 0 and the fill ending each +/// code on a byte, that may be other rows than were coded. +/// +/// Errors: `columns` that aren't from 1 to [`MAX_CCITT_COLUMNS`] ([`CodecError::Unsupported`]). +/// +/// Decoding spends from `work`, what decoding may still cost across a run of images, and there is +/// no way in without it: a +/// byte of rows a step, whether or not the data holds them, and [`CODE_WORK`] a byte of `data`. +/// A blank row takes a bit of code, so the rows are what a few bytes can ask for. It is all paid +/// before anything is decoded, and what `work` can't pay for spends all of it. +pub fn decode_ccitt_within(data: &[u8], p: &CcittParams, max_rows: u32, work: &mut u64) -> Result, CodecError> { + let rows = u32::try_from(p.rows).ok().filter(|r| *r > 0).map_or(max_rows, |r| r.min(max_rows)); + let stride = u64::try_from(p.columns).unwrap_or(0).div_ceil(8); + crate::pay(work, stride.saturating_mul(u64::from(rows)).saturating_add((data.len() as u64).saturating_mul(CODE_WORK)))?; + decode_ccitt(data, p, max_rows) +} + +/// Whether CCITT fax `data` starts with an end-of-line code: a 1 after 11 zeros or more, fill +/// included. T.4 sends one before the first row when it sends them at all, and no other code starts +/// with more than eight zeros. +fn starts_with_end_of_line(data: &[u8]) -> bool { + let Some((i, b)) = data.iter().enumerate().find(|(_, b)| **b != 0) else { return false }; + i.saturating_mul(8).saturating_add(b.leading_zeros() as usize) >= 11 +} + +/// Rows of 1-bit samples filled from a decoder's pixels: `columns` a row, each row on a new byte, at +/// most `rows` rows. Pixels past a row's end and rows past the last are dropped. +pub(crate) struct Rows { + pub(crate) out: Vec, + row: Vec, + x: usize, + columns: usize, + rows: usize, + done: usize, + black_is_1: bool, +} + +impl Rows { + pub(crate) fn new(columns: usize, rows: usize, black_is_1: bool) -> Self { + Rows { out: Vec::new(), row: vec![0; columns.div_ceil(8)], x: 0, columns, rows, done: 0, black_is_1 } + } + + /// `count` pixels, white or black. + pub(crate) fn push(&mut self, white: bool, count: usize) { + let end = self.x.saturating_add(count); + if white != self.black_is_1 { + for x in self.x..end.min(self.columns) { + if let Some(b) = self.row.get_mut(x / 8) { + *b |= 0x80 >> (x % 8); + } + } + } + self.x = end; + } + + /// The row filled so far kept, if fewer than `rows` are, and the next one begun. + pub(crate) fn end_row(&mut self) { + if self.done < self.rows { + self.out.extend_from_slice(&self.row); + self.done += 1; + } + self.row.fill(0); + self.x = 0; + } +} + +impl hayro_ccitt::Decoder for Rows { + fn push_pixel(&mut self, white: bool) { + self.push(white, 1); + } + fn push_pixel_chunk(&mut self, white: bool, chunks: u32) { + self.push(white, (chunks as usize).saturating_mul(8)); + } + fn next_line(&mut self) { + self.end_row(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A run pays for the rows an image asks for and the code it reads, before any of it is + /// decoded: rows the data doesn't hold are paid for too, as a few bytes can ask for any number. + #[test] + fn a_run_of_images_pays_for_the_rows_it_asks_for() { + let (w, h) = (4096u32, 64u32); + let stride = (w as usize).div_ceil(8); + let data = encode_g4(&vec![0xFF; stride * h as usize], w, h).unwrap(); + let p = CcittParams { k: -1, columns: i64::from(w), ..CcittParams::default() }; + let whole = decode_ccitt(&data, &p, h).unwrap(); + let cost = (whole.len() + 4 * data.len()) as u64; + let mut work = cost; + assert_eq!(decode_ccitt_within(&data, &p, h, &mut work).unwrap(), whole, "exactly enough"); + assert_eq!(work, 0); + let mut work = cost + 7; + decode_ccitt_within(&data, &p, h, &mut work).unwrap(); + assert_eq!(work, 7, "only what it took"); + let mut work = cost - 1; + let refused = decode_ccitt_within(&data, &p, h, &mut work).unwrap_err(); + assert!(matches!(refused, CodecError::Spent), "a step short: {refused}"); + assert_eq!(work, 0, "spends all that was left"); + // Nothing left: not read at all, so bad columns aren't reported as bad columns. + let bad = CcittParams { columns: 0, ..p }; + assert!(decode_ccitt_within(&data, &bad, h, &mut 0).is_err_and(|e| matches!(e, CodecError::Spent))); + // No code at all, for the same rows. + let mut work = u64::MAX; + decode_ccitt_within(&[], &p, h, &mut work).unwrap(); + assert_eq!(u64::MAX - work, whole.len() as u64); + // `/Rows` below what is asked for, and no rows. + let mut work = u64::MAX; + decode_ccitt_within(&data, &CcittParams { rows: 3, ..p }, h, &mut work).unwrap(); + assert_eq!(u64::MAX - work, (3 * stride + 4 * data.len()) as u64); + let mut work = u64::MAX; + decode_ccitt_within(&data, &p, 0, &mut work).unwrap(); + assert_eq!(u64::MAX - work, 4 * data.len() as u64); + } + + /// The rows decoded by an independent decoder (`hayro-ccitt`), as 1 for white and 0 for black. + pub(crate) fn decode(data: &[u8], columns: u32, rows: u32) -> Vec> { + struct Rows(Vec>, Vec, usize); + impl hayro_ccitt::Decoder for Rows { + fn push_pixel(&mut self, white: bool) { + self.1.push(white); + } + fn push_pixel_chunk(&mut self, white: bool, chunks: u32) { + self.1.extend(std::iter::repeat_n(white, chunks as usize * 8)); + } + fn next_line(&mut self) { + let mut row = std::mem::take(&mut self.1); + row.truncate(self.2); + self.0.push(row); + } + } + let settings = hayro_ccitt::DecodeSettings { + columns, + rows, + end_of_block: true, + end_of_line: false, + rows_are_byte_aligned: false, + encoding: hayro_ccitt::EncodingMode::Group4, + invert_black: false, + }; + let mut out = Rows(Vec::new(), Vec::new(), columns as usize); + hayro_ccitt::decode(data, &mut out, &mut hayro_ccitt::DecoderContext::new(settings)).unwrap(); + out.0 + } + + /// The rows of packed samples `data`, as 1 for a 1 bit. + pub(crate) fn unpack(data: &[u8], columns: u32, rows: u32) -> Vec> { + let stride = (columns as usize).div_ceil(8); + (0..rows as usize).map(|y| (0..columns as usize).map(|x| data[y * stride + x / 8] & (0x80 >> (x % 8)) != 0).collect()).collect() + } + + #[test] + fn a_page_of_text_like_marks_round_trips_and_shrinks() { + let (w, h) = (1700u32, 220u32); + let stride = (w as usize).div_ceil(8); + let mut data = vec![0xFF; stride * h as usize]; + // Short black strokes in lines, like glyphs on a white page. + for y in (20..h as usize).step_by(40) { + for dy in 0..12 { + for x in (100..1600).step_by(9) { + if (x / 9 + dy) % 5 != 0 { + data[(y + dy) * stride + x / 8] &= !(0x80 >> (x % 8)); + } + } + } + } + let g4 = encode_g4(&data, w, h).unwrap(); + assert_eq!(decode(&g4, w, h), unpack(&data, w, h)); + assert!(g4.len() * 4 < data.len(), "{} bytes from {}", g4.len(), data.len()); + } + + #[test] + fn padding_bits_and_trailing_bytes_are_ignored() { + // 3 columns: the last 5 bits of each byte pad the row; set them to anything. + let data = [0b1010_1111, 0b0101_0000, 0b1110_1010, 0xAB, 0xCD]; + let g4 = encode_g4(&data, 3, 3).unwrap(); + assert_eq!(decode(&g4, 3, 3), [[true, false, true], [false, true, false], [true, true, true]]); + } + + #[test] + fn what_it_cannot_encode_is_an_error() { + assert!(matches!(encode_g4(&[0; 8], 0, 1), Err(CodecError::Unsupported(_)))); + assert!(matches!(encode_g4(&[0; 8], 8, 0), Err(CodecError::Unsupported(_)))); + assert!(matches!(encode_g4(&vec![0; 4097], MAX_G4_COLUMNS + 1, 1), Err(CodecError::Unsupported(_)))); + assert!(matches!(encode_g4(&[0; 3], 16, 2), Err(CodecError::Corrupt { .. })), "two rows of 16 need 4 bytes"); + assert!(matches!(encode_g4(&[], 1, u32::MAX), Err(CodecError::Corrupt { .. }))); + let widest = vec![0x55; (MAX_G4_COLUMNS as usize).div_ceil(8) * 2]; + let g4 = encode_g4(&widest, MAX_G4_COLUMNS, 2).unwrap(); + assert_eq!(decode(&g4, MAX_G4_COLUMNS, 2), unpack(&widest, MAX_G4_COLUMNS, 2)); + } + + /// Rows of `columns` samples built from `runs`: alternating white and black runs one longer + /// than those values (so never empty, even when shrinking makes them all 0), repeated until the + /// rows are full; or, with `noise`, the bytes of `runs` as they are. + fn bitmap(columns: u32, rows: u32, runs: &[u8], noise: bool) -> Vec { + let stride = (columns as usize).div_ceil(8); + let mut data = vec![0u8; stride * rows as usize]; + if runs.is_empty() { + return data; + } + if noise { + data.iter_mut().zip(runs.iter().cycle()).for_each(|(d, r)| *d = *r); + return data; + } + let (mut white, mut left, mut next) = (true, 1 + usize::from(runs[0]), 1); + for y in 0..rows as usize { + for x in 0..columns as usize { + if left == 0 { + white = !white; + left = 1 + usize::from(runs[next % runs.len()]); + next += 1; + } + if white { + data[y * stride + x / 8] |= 0x80 >> (x % 8); + } + left -= 1; + } + } + data + } + + fn g4(columns: u32) -> CcittParams { + CcittParams { k: -1, columns: i64::from(columns), ..CcittParams::default() } + } + + #[test] + fn decoding_gives_the_rows_back() { + // 3 columns, padded with anything: the decoded padding is 0. + let data = [0b1010_1111, 0b0101_0000, 0b1110_1010]; + let coded = encode_g4(&data, 3, 3).unwrap(); + assert_eq!(decode_ccitt(&coded, &g4(3), 3).unwrap(), [0b1010_0000, 0b0100_0000, 0b1110_0000]); + // /BlackIs1 gives black as 1. + let p = CcittParams { black_is_1: true, ..g4(3) }; + assert_eq!(decode_ccitt(&coded, &p, 3).unwrap(), [0b0100_0000, 0b1010_0000, 0b0000_0000]); + // At most /Rows rows, and at most max_rows. + assert_eq!(decode_ccitt(&coded, &CcittParams { rows: 2, ..g4(3) }, 3).unwrap().len(), 2); + assert_eq!(decode_ccitt(&coded, &g4(3), 1).unwrap().len(), 1); + assert_eq!(decode_ccitt(&coded, &CcittParams { rows: 3, ..g4(3) }, 1).unwrap().len(), 1, "max_rows bounds /Rows"); + assert_eq!(decode_ccitt(&coded, &g4(3), 0).unwrap(), [0u8; 0]); + // Damaged data gives the rows before the damage; none here. + assert!(decode_ccitt(&coded[..1], &g4(3), 3).unwrap().len() < 3); + } + + #[test] + fn what_it_cannot_decode_is_an_error() { + for columns in [0, -1, i64::from(MAX_CCITT_COLUMNS) + 1, i64::MAX] { + let p = CcittParams { columns, ..CcittParams::default() }; + assert!(matches!(decode_ccitt(&[0; 8], &p, 4), Err(CodecError::Unsupported(_))), "{columns}"); + } + assert_eq!(CcittParams::default().columns, 1728, "Table 11"); + } + + /// Group 3 data: rows of run lengths below 64, white first, coded one-dimensionally with T.4's + /// terminating codes (ITU-T T.4 Tables 2 and 3). Each row starts with an end-of-line code with + /// `eol`, then with `tag` a 1 bit (the next row one-dimensional, as `k` above 0 has it). Fill + /// bits (0s) go where `fill` says. + fn group_3(rows: &[Vec], eol: bool, tag: bool, fill: Fill) -> Vec { + // The codes for runs 0 to 63, in order. + const WHITE: &str = concat!( + "00110101 000111 0111 1000 1011 1100 1110 1111 10011 10100 00111 01000 001000 000011 110100 110101 101010 101011 ", + "0100111 0001100 0001000 0010111 0000011 0000100 0101000 0101011 0010011 0100100 0011000 00000010 00000011 00011010 ", + "00011011 00010010 00010011 00010100 00010101 00010110 00010111 00101000 00101001 00101010 00101011 00101100 00101101 ", + "00000100 00000101 00001010 00001011 01010010 01010011 01010100 01010101 00100100 00100101 01011000 01011001 01011010 ", + "01011011 01001010 01001011 00110010 00110011 00110100", + ); + const BLACK: &str = concat!( + "0000110111 010 11 10 011 0011 0010 00011 000101 000100 0000100 0000101 0000111 00000100 00000111 000011000 0000010111 ", + "0000011000 0000001000 00001100111 00001101000 00001101100 00000110111 00000101000 00000010111 00000011000 ", + "000011001010 000011001011 000011001100 000011001101 000001101000 000001101001 000001101010 000001101011 000011010010 ", + "000011010011 000011010100 000011010101 000011010110 000011010111 000001101100 000001101101 000011011010 000011011011 ", + "000001010100 000001010101 000001010110 000001010111 000001100100 000001100101 000001010010 000001010011 000000100100 ", + "000000110111 000000111000 000000100111 000000101000 000001011000 000001011001 000000101011 000000101100 000001011010 ", + "000001100110 000001100111", + ); + let code = |white: bool, run: u32| (if white { WHITE } else { BLACK }).split(' ').nth(run as usize).expect("a run below 64"); + let mut bits = String::new(); + for row in rows { + // The end-of-line code is 12 bits. + let ends_code = if fill == Fill::CodeEnd && eol { 12 } else { 0 }; + while fill != Fill::None && !(bits.len() + ends_code).is_multiple_of(8) { + bits.push('0'); + } + if eol { + bits.push_str("000000000001"); + } + if tag { + bits.push('1'); + } + row.iter().enumerate().for_each(|(i, run)| bits.push_str(code(i % 2 == 0, *run))); + } + bits.as_bytes().chunks(8).map(|c| c.iter().enumerate().fold(0u8, |b, (i, d)| b | (u8::from(*d == b'1') << (7 - i)))).collect() + } + + /// Where [`group_3`] puts fill bits: none; so each row, its end-of-line code first, starts on a + /// byte; or so each end-of-line code ends on one (T.4's fill, as TIFF writes it). + #[derive(Clone, Copy, Debug, PartialEq)] + enum Fill { + None, + RowStart, + CodeEnd, + } + + /// `rows` text-like rows of `columns` samples as runs, white first, from `seed`: long white runs + /// and short black ones; a row may start black. + fn text_like(seed: u64, columns: u32, rows: u32) -> Vec> { + let mut seed = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1; + let mut random = |n: u64| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed % n) as u32 + }; + (0..rows) + .map(|_| { + let (mut runs, mut left) = (Vec::new(), columns); + while left > 0 { + let run = match runs.len() % 2 { + 0 if runs.is_empty() => random(31), + 0 => 1 + random(30), + _ => 1 + random(5), + }; + runs.push(run.min(left)); + left -= run.min(left); + } + runs + }) + .collect() + } + + /// Rows of runs, white first, packed as `decode_ccitt` gives them (white 1). + fn pack(runs: &[Vec], columns: u32) -> Vec { + let stride = (columns as usize).div_ceil(8); + let mut out = vec![0u8; stride * runs.len()]; + for (y, row) in runs.iter().enumerate() { + let mut x = 0; + for (i, run) in row.iter().enumerate() { + if i % 2 == 0 { + (x..x + *run as usize).for_each(|x| out[y * stride + x / 8] |= 0x80 >> (x % 8)); + } + x += *run as usize; + } + } + out + } + + /// Group 3 rows on bytes in each layout, over many pictures, as how a misreading comes out + /// depends on the picture: no codes (as TIFF's Modified Huffman has it), and `end_of_line` + /// without them; codes, each starting on a byte or ending on one, `end_of_line` or not. Each + /// decodes to its picture, and a byte short to fewer rows, not to other rows read another way. + /// Group 4 rows on bytes keep their aligned read, `end_of_line` or not. + #[test] + fn group_3_rows_on_bytes_read_by_the_codes_in_the_data() { + for seed in 0..32u64 { + let (columns, rows) = (8 + (seed as u32 * 37) % 200, 2 + seed as u32 % 20); + let runs = text_like(seed, columns, rows); + let picture = pack(&runs, columns); + for k in [0, 1] { + for (codes, fill, end_of_line) in [ + (false, Fill::RowStart, false), + (false, Fill::RowStart, true), + (true, Fill::RowStart, false), + (true, Fill::RowStart, true), + (true, Fill::CodeEnd, false), + (true, Fill::CodeEnd, true), + ] { + let p = CcittParams { k, columns: i64::from(columns), end_of_line, encoded_byte_align: true, ..CcittParams::default() }; + let mut data = group_3(&runs, codes, k > 0, fill); + let case = format!("seed {seed}, {columns} × {rows}, k {k}, codes {codes}, fill {fill:?}, end_of_line {end_of_line}"); + assert_eq!(decode_ccitt(&data, &p, rows).unwrap(), picture, "{case}"); + data.pop(); + assert!(decode_ccitt(&data, &p, rows).unwrap().len() < picture.len(), "{case}, a byte short"); + } + } + } + // White rows, each the one code 1 (vertical, no change) and its fill. + for end_of_line in [false, true] { + let p = CcittParams { k: -1, columns: 64, end_of_line, encoded_byte_align: true, ..CcittParams::default() }; + assert_eq!(decode_ccitt(&[0x80; 16], &p, 16).unwrap(), [0xFF; 8 * 16], "Group 4, end_of_line {end_of_line}"); + } + } + + proptest::proptest! { + /// Any bitmap, of noise or of runs, decodes to its own bits, through an independent decoder + /// and through `decode_ccitt`. + #[test] + fn bitmaps_round_trip( + columns in 1u32..400, + rows in 1u32..24, + runs in proptest::collection::vec(0u8..=255, 1..48), + noise in proptest::bool::ANY, + ) { + let data = bitmap(columns, rows, &runs, noise); + let coded = encode_g4(&data, columns, rows).unwrap(); + proptest::prop_assert_eq!(decode(&coded, columns, rows), unpack(&data, columns, rows)); + let decoded = decode_ccitt(&coded, &g4(columns), rows).unwrap(); + proptest::prop_assert_eq!(unpack(&decoded, columns, rows), unpack(&data, columns, rows)); + } + + /// Whatever it is given, it returns. + #[test] + fn any_input_returns( + data in proptest::collection::vec(0u8..=255, 0..600), + columns in 0u32..40_000, + rows in 0u32..80, + k in -2i64..3, + end_of_line in proptest::bool::ANY, + encoded_byte_align in proptest::bool::ANY, + ) { + let _ = encode_g4(&data, columns, rows); + let p = CcittParams { k, columns: i64::from(columns), end_of_line, encoded_byte_align, ..CcittParams::default() }; + if let Ok(out) = decode_ccitt(&data, &p, rows) { + proptest::prop_assert!(out.len() <= (columns as usize).div_ceil(8) * rows as usize); + } + } + } +} diff --git a/crates/stream-codecs/src/jbig2.rs b/crates/stream-codecs/src/jbig2.rs new file mode 100644 index 000000000..0b8b76720 --- /dev/null +++ b/crates/stream-codecs/src/jbig2.rs @@ -0,0 +1,468 @@ +//! JBIG2 encoding (ITU-T T.88 | ISO/IEC 14492), for `JBIG2Decode` (ISO 32000-2 §7.4.7), lossless: +//! a generic region, template 0 with its nominal adaptive pixels, typical prediction on, MQ +//! arithmetic coding ([`crate::mq`]); or the image's marks as symbols ([`symbols`]), which a group +//! of images may share through `/JBIG2Globals`. Written from the standard's 6.2.5 (coding a generic +//! region), 7.2 (segment headers), 7.4.1, 7.4.6 and 7.4.8 (the segments) and D.3 (the embedded +//! organisation PDF uses). Decoding is in [`decode`]. + +mod symbols; + +/// The largest image the encoders take, in pixels: the largest JBIG2 page `hayro-jbig2` decodes +/// (it decodes a page whole, and takes 20,000 × 10,000), so what they store reads back. +pub const MAX_JBIG2_PIXELS: u64 = 1 << 28; + +use crate::CodecError; +use crate::mq::{Context, MqEncoder}; + +/// The widest row, and the most rows, [`encode_jbig2_generic`] encodes: what `hayro-jbig2`, the +/// decoder behind `JBIG2Decode` here, takes on a side. JBIG2 allows 32-bit sizes, but past this +/// nothing here could read the stream, and the optimizer, which doesn't read back what it stores, +/// replaced such an image with one that draws nothing. +pub const MAX_JBIG2_COLUMNS: u32 = 65_535; + +/// Template 0's pixels (Figure 3), as (x, y) offsets from the pixel being coded, each a bit of the +/// context in this order: the four on the current row, the five fixed ones on the row above, the +/// three on the row above that, then the adaptive pixels A1–A4 at their nominal places (Table 5). +const TEMPLATE: [(i32, i32); 16] = [ + (-1, 0), + (-2, 0), + (-3, 0), + (-4, 0), + (-2, -1), + (-1, -1), + (0, -1), + (1, -1), + (2, -1), + (-1, -2), + (0, -2), + (1, -2), + (3, -1), + (-3, -1), + (2, -2), + (-2, -2), +]; + +/// The context that codes SLTP when template 0 is used (Figure 8): those pixel values, in +/// [`TEMPLATE`]'s order. Figure 8 reads, by row from x = -4: `0 1 0 1` on the current row, +/// `0 1 1 0 0 1 0` (x = -3 to 3) above it, `1 0 0 1 1` (x = -2 to 2) above that. +const SLTP: [u8; 16] = [1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1]; + +/// Columns of padding either side of a row, so every template pixel of a row's pixels is in it. +const PAD: usize = 4; + +/// `rows` rows of `columns` 1-bit PDF samples as lossless JBIG2, the smaller of two codings: +/// [`encode_jbig2_generic`]'s, or one that stores each distinct mark of the image once, as a +/// symbol, and places its copies (see [`symbols`]). Either way the stream is in the embedded +/// organisation and decodes to the same bits without `/JBIG2Globals`. +/// +/// Errors: as [`encode_jbig2_generic`]. +pub fn encode_jbig2(data: &[u8], columns: u32, rows: u32) -> Result, CodecError> { + let generic = encode_jbig2_generic(data, columns, rows)?; + let samples = rows_to_encode(data, columns, rows).ok(); + Ok(match samples.and_then(|s| symbols::encode(s, columns, rows)) { + Some(coded) if coded.len() < generic.len() => coded, + _ => generic, + }) +} + +/// `rows` rows of `columns` 1-bit PDF samples (most significant bit first, each row starting on +/// a new byte), as a JBIG2 stream in the embedded organisation: a page information segment and an +/// immediate lossless generic region segment, which `JBIG2Decode` decodes to the same bits +/// without `/JBIG2Globals`. +/// +/// A 0 sample is coded as a JBIG2 1 (black), since `JBIG2Decode` gives black as 0, as a grey image's +/// samples have it. Bytes after the last row are ignored, as are the bits that pad a row to a whole +/// byte. +/// +/// Errors: no rows or columns, more of either than [`MAX_JBIG2_COLUMNS`], or more samples than +/// [`MAX_JBIG2_PIXELS`], what the decoder takes ([`CodecError::Unsupported`]); fewer bytes than the +/// rows need ([`CodecError::Corrupt`]). +pub fn encode_jbig2_generic(data: &[u8], columns: u32, rows: u32) -> Result, CodecError> { + let samples = rows_to_encode(data, columns, rows)?; + let width = columns as usize; + let stride = width.div_ceil(8); + let mut contexts: Vec = vec![0; 1 << 16]; + let mut mq = MqEncoder::new(); + let height = samples.len() / stride; + code_generic(&mut mq, &mut contexts, width, height, true, |y, row| { + let packed = samples.get(y * stride..).unwrap_or_default(); + for (x, pixel) in row.iter_mut().enumerate() { + let sample = packed.get(x / 8).is_some_and(|b| b & (0x80 >> (x % 8)) != 0); + *pixel = u8::from(!sample); + } + }); + let region = mq.finish(); + let mut out = Vec::with_capacity(region.len() + 64); + segment(&mut out, 0, 48, &[], false, &page_information(columns, rows)); + segment(&mut out, 1, 39, &[], false, &generic_region(columns, rows, 0, 0, ®ion)); + Ok(out) +} + +/// The samples of `rows` rows of `columns` 1-bit samples in `data`, when the encoders here take +/// that size; the errors are [`encode_jbig2_generic`]'s. +fn rows_to_encode(data: &[u8], columns: u32, rows: u32) -> Result<&[u8], CodecError> { + let too_large = columns > MAX_JBIG2_COLUMNS || rows > MAX_JBIG2_COLUMNS || u64::from(columns) * u64::from(rows) > MAX_JBIG2_PIXELS; + if columns == 0 || rows == 0 || too_large { + return Err(CodecError::Unsupported(format!("JBIG2 encoding of {columns} × {rows} samples"))); + } + let stride = (columns as usize).div_ceil(8); + usize::try_from(rows) + .ok() + .and_then(|r| r.checked_mul(stride)) + .and_then(|n| data.get(..n)) + .ok_or_else(|| CodecError::Corrupt { filter: "JBIG2Decode", detail: format!("{} bytes for {rows} rows of {columns} samples", data.len()) }) +} + +/// A group of images coded by [`encode_jbig2_shared`]: the data of the `/JBIG2Globals` stream +/// they share, and each image's own stream, in the order given, or `None` for one that shares +/// nothing with the others. +#[derive(Debug)] +pub struct SharedJbig2 { + pub globals: Vec, + pub images: Vec>>, +} + +/// Images, each `rows` rows of `columns` 1-bit PDF samples, as lossless JBIG2 sharing one symbol +/// dictionary: the marks found in more than one of them are stored once, in a symbol dictionary +/// segment not associated with a page, which is the `/JBIG2Globals` stream's data, and each image's +/// stream, in the embedded organisation, places them from there, with its other marks coded as +/// [`encode_jbig2`]'s symbol coding codes them (see [`symbols`]). With the globals, each decodes to +/// its own bits. An image that holds none of those marks, or that symbol coding doesn't take, has +/// no stream here; it is stored alone. `None` when the images share fewer than two marks. +/// +/// What each image's stream saves is left for the caller to weigh against what the globals take, +/// which the images that use them share. +/// +/// Errors: as [`encode_jbig2_generic`], for the first image it would refuse. +pub fn encode_jbig2_shared(images: &[(&[u8], u32, u32)]) -> Result, CodecError> { + let images = + images.iter().map(|(data, columns, rows)| Ok((rows_to_encode(data, *columns, *rows)?, *columns, *rows))).collect::, _>>()?; + Ok(symbols::encode_shared(&images).map(|(globals, images)| SharedJbig2 { globals, images })) +} + +/// A page information segment's data (7.4.8): the page's size, resolution unknown, eventually +/// lossless, default pixel 0 (white), OR combination, not striped. +fn page_information(columns: u32, rows: u32) -> Vec { + let mut page = Vec::with_capacity(19); + page.extend_from_slice(&columns.to_be_bytes()); + page.extend_from_slice(&rows.to_be_bytes()); + page.extend_from_slice(&[0; 8]); + page.push(0x01); + page.extend_from_slice(&[0, 0]); + page +} + +/// A region segment information field (7.4.1): a `width` × `height` region at (`x`, `y`) on the +/// page, combined with it by OR. +fn region_information(out: &mut Vec, width: u32, height: u32, x: u32, y: u32) { + for v in [width, height, x, y] { + out.extend_from_slice(&v.to_be_bytes()); + } + out.push(0); +} + +/// An immediate lossless generic region segment's data (7.4.6): its region information, flags +/// (MMR 0, template 0, TPGDON 1), the AT pixels at their nominal places, then `coded`. +fn generic_region(width: u32, height: u32, x: u32, y: u32, coded: &[u8]) -> Vec { + let mut data = Vec::with_capacity(26 + coded.len()); + region_information(&mut data, width, height, x, y); + data.push(0x08); + at_pixels(&mut data); + data.extend_from_slice(coded); + data +} + +/// Template 0's four AT pixels at their nominal places, as AT flags (7.4.6.3, 7.4.2.1.2). +fn at_pixels(out: &mut Vec) { + for (x, y) in &TEMPLATE[12..] { + out.push(*x as i8 as u8); + out.push(*y as i8 as u8); + } +} + +/// A segment (7.2) of page 1: its header (number, type, the segments it refers to, the page, data +/// length), then its data. `retained`: a later segment refers to this one. `refers_to` holds each +/// referred-to segment's number and whether a segment after this one refers to it too (its +/// retain bit, 7.2.4). Segment numbers here stay below 256, so each referred-to one takes a byte +/// (7.2.5); a segment refers to at most four, so their count and retain bits take one byte too. +fn segment(out: &mut Vec, number: u32, kind: u8, refers_to: &[(u8, bool)], retained: bool, data: &[u8]) { + segment_of(out, 1, number, kind, refers_to, retained, data); +} + +/// [`segment`], of page `page`: 0 for a segment associated with no page (7.2.6), such as the +/// global ones `/JBIG2Globals` holds, which a page's segments may refer to. +fn segment_of(out: &mut Vec, page: u8, number: u32, kind: u8, refers_to: &[(u8, bool)], retained: bool, data: &[u8]) { + out.extend_from_slice(&number.to_be_bytes()); + out.push(kind); + // At most four, so the count fits its three bits. + let refers_to = refers_to.get(..refers_to.len().min(4)).unwrap_or_default(); + let kept = refers_to.iter().enumerate().fold(0u8, |bits, (k, (_, kept))| bits | u8::from(*kept) << (k + 1)); + out.push((refers_to.len() as u8) << 5 | kept | u8::from(retained)); + out.extend(refers_to.iter().map(|(n, _)| *n)); + out.push(page); + // A segment's data never comes near 4 GiB here: an image is at most 2^28 samples. + out.extend_from_slice(&u32::try_from(data.len()).unwrap_or(u32::MAX).to_be_bytes()); + out.extend_from_slice(data); +} + +/// Codes a `width` × `height` bitmap as a generic region (6.2.5.7) with template 0 and the nominal +/// AT pixels, its decisions in `contexts` (2^16 of them), with typical prediction when `typical` +/// (TPGDON). `fill(y, row)` sets row `y`'s `width` pixels, 1 for black. Pixels outside the bitmap +/// count as 0 (white), as the decoder takes them. +fn code_generic(mq: &mut MqEncoder, contexts: &mut [Context], width: usize, height: usize, typical: bool, mut fill: impl FnMut(usize, &mut [u8])) { + let sltp = SLTP.iter().enumerate().fold(0usize, |cx, (k, v)| cx | usize::from(*v) << k); + // The two rows above the current one and the current one, as JBIG2 pixels (1 black), padded. + let blank = vec![0u8; width + 2 * PAD]; + let (mut above2, mut above1) = (blank.clone(), blank.clone()); + let mut row = blank; + let mut ltp = false; + for y in 0..height { + if let Some(pixels) = row.get_mut(PAD..PAD + width) { + fill(y, pixels); + } + if typical { + // Typical prediction (6.2.5.5): a row the same as the one above is coded as one decision. + let same = row == above1; + if let Some(cx) = contexts.get_mut(sltp) { + mq.encode(cx, same != ltp); + } + ltp = same; + } + if !ltp { + for x in 0..width { + let at = |dx: i32, dy: i32| { + let line = match dy { + 0 => &row, + -1 => &above1, + _ => &above2, + }; + let i = (PAD + x).checked_add_signed(dx as isize); + i.and_then(|i| line.get(i)).copied().unwrap_or(0) + }; + let cx = TEMPLATE.iter().enumerate().fold(0usize, |cx, (k, (dx, dy))| cx | usize::from(at(*dx, *dy)) << k); + if let Some(state) = contexts.get_mut(cx) { + mq.encode(state, row.get(PAD + x) == Some(&1)); + } + } + } + std::mem::swap(&mut above2, &mut above1); + std::mem::swap(&mut above1, &mut row); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The rows an independent decoder (`hayro-jbig2`) reads from `data`, as PDF samples: true + /// for white (a JBIG2 0). + pub(super) fn decode(data: &[u8]) -> Vec> { + decode_with(data, None) + } + + /// [`decode`], with `/JBIG2Globals`. + pub(super) fn decode_with(data: &[u8], globals: Option<&[u8]>) -> Vec> { + struct Rows(Vec>, Vec, usize); + impl hayro_jbig2::Decoder for Rows { + fn push_pixel(&mut self, black: bool) { + self.1.push(!black); + } + fn push_pixel_chunk(&mut self, black: bool, chunks: u32) { + self.1.extend(std::iter::repeat_n(!black, chunks as usize * 8)); + } + fn next_line(&mut self) { + let mut row = std::mem::take(&mut self.1); + row.truncate(self.2); + self.0.push(row); + } + } + let image = hayro_jbig2::Image::new_embedded(data, globals).unwrap(); + let mut rows = Rows(Vec::new(), Vec::new(), image.width() as usize); + image.decode(&mut rows).unwrap(); + rows.0 + } + + /// The rows of packed samples `data`, true for a 1 bit. + pub(super) fn unpack(data: &[u8], columns: u32, rows: u32) -> Vec> { + let stride = (columns as usize).div_ceil(8); + (0..rows as usize).map(|y| (0..columns as usize).map(|x| data[y * stride + x / 8] & (0x80 >> (x % 8)) != 0).collect()).collect() + } + + /// Rows of `columns` samples from `runs`: alternating white and black runs one longer than + /// those values (never empty, even when shrinking makes them all 0), until the rows are full; + /// or, with `noise`, the bytes of `runs` as they are. + fn bitmap(columns: u32, rows: u32, runs: &[u8], noise: bool) -> Vec { + let stride = (columns as usize).div_ceil(8); + let mut data = vec![0u8; stride * rows as usize]; + if runs.is_empty() { + return data; + } + if noise { + data.iter_mut().zip(runs.iter().cycle()).for_each(|(d, r)| *d = *r); + return data; + } + let (mut white, mut left, mut next) = (true, 1 + usize::from(runs[0]), 1); + for y in 0..rows as usize { + for x in 0..columns as usize { + if left == 0 { + white = !white; + left = 1 + usize::from(runs[next % runs.len()]); + next += 1; + } + if white { + data[y * stride + x / 8] |= 0x80 >> (x % 8); + } + left -= 1; + } + } + data + } + + #[test] + fn a_page_of_text_like_marks_round_trips_and_shrinks() { + let (w, h) = (1700u32, 220u32); + let stride = (w as usize).div_ceil(8); + let mut data = vec![0xFF; stride * h as usize]; + for y in (20..h as usize).step_by(40) { + for dy in 0..12 { + for x in (100..1600).step_by(9) { + if (x / 9 + dy) % 5 != 0 { + data[(y + dy) * stride + x / 8] &= !(0x80 >> (x % 8)); + } + } + } + } + let jbig2 = encode_jbig2_generic(&data, w, h).unwrap(); + assert_eq!(decode(&jbig2), unpack(&data, w, h)); + let g4 = crate::encode_g4(&data, w, h).unwrap(); + assert!(jbig2.len() < g4.len(), "JBIG2 {} bytes, Group 4 {}", jbig2.len(), g4.len()); + } + + #[test] + fn edges_padding_and_typical_rows_round_trip() { + // 3 columns: the last 5 bits of each byte pad the row; set them to anything. + let data = [0b1010_1111, 0b0101_0000, 0b1110_1010, 0xAB, 0xCD]; + assert_eq!(decode(&encode_jbig2_generic(&data, 3, 3).unwrap()), [[true, false, true], [false, true, false], [true, true, true]]); + for (w, h, fill) in [(1, 1, 0x00), (1, 1, 0xFF), (13, 9, 0x00), (13, 9, 0xFF), (64, 40, 0x5A)] { + let data = vec![fill; (w as usize).div_ceil(8) * h as usize]; + assert_eq!(decode(&encode_jbig2_generic(&data, w, h).unwrap()), unpack(&data, w, h), "{w} × {h} of {fill:#04x}"); + } + } + + /// Packed samples from JBIG2 pixels (1 black): a 0 sample for each 1. + pub(super) fn samples(pixels: &[Vec]) -> Vec { + let width = pixels.first().map_or(0, Vec::len); + let stride = width.div_ceil(8); + let mut data = vec![0xFF; stride * pixels.len()]; + for (y, row) in pixels.iter().enumerate() { + for (x, p) in row.iter().enumerate() { + if *p == 1 { + data[y * stride + x / 8] &= !(0x80 >> (x % 8)); + } + } + } + data + } + + /// A row the same as the one above is typical (6.2.5.5): one decision, not a row of pixels. + #[test] + fn a_repeated_row_is_one_decision() { + let row: Vec = (0..800u32).map(|x| u8::from((x * 7919 + x / 3) % 5 < 2)).collect(); + let pixels = vec![row; 1000]; + let data = samples(&pixels); + crate::mq::DECISIONS.with(|n| n.set(0)); + let jbig2 = encode_jbig2_generic(&data, 800, 1000).unwrap(); + // The first row's 800 pixels, and one typical-row decision for each of the 1000 rows. + assert_eq!(crate::mq::DECISIONS.with(|n| n.get()), 800 + 1000); + assert_eq!(decode(&jbig2), unpack(&data, 800, 1000)); + } + + /// SLTP is coded in the context Figure 8 shows, which ordinary pixels can share: an image + /// tiled with that very neighbourhood decodes only if both coders use the same context. + #[test] + fn the_typical_row_flag_shares_figure_8s_context() { + // Figure 8, around a pixel at x = 4 of an 8-column tile: two rows up x = 2..6, one row up + // x = 1..7, the pixel's own row x = 0..3. + let tile = [[0, 0, 1, 0, 0, 1, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0], [0, 1, 0, 1, 1, 0, 0, 0]]; + let pixels: Vec> = (0..90).map(|y| (0..400).map(|x| tile[y % 3][x % 8]).collect()).collect(); + let data = samples(&pixels); + assert_eq!(decode(&encode_jbig2_generic(&data, 400, 90).unwrap()), unpack(&data, 400, 90)); + } + + #[test] + fn what_it_cannot_encode_is_an_error() { + assert!(matches!(encode_jbig2_generic(&[0; 8], 0, 1), Err(CodecError::Unsupported(_)))); + assert!(matches!(encode_jbig2_generic(&[0; 8], 8, 0), Err(CodecError::Unsupported(_)))); + assert!(matches!(encode_jbig2_generic(&[0; 3], 16, 2), Err(CodecError::Corrupt { .. })), "two rows of 16 need 4 bytes"); + assert!(matches!(encode_jbig2_generic(&[], 1, 2), Err(CodecError::Corrupt { .. }))); + } + + /// The encoder stops where the decoder does, on each side and in total: past that it would + /// write a stream nothing here can read. 16,384 squared is exactly the bound on samples. + /// `encode_jbig2_shared` refuses what `encode_jbig2_generic` refuses: an image taller than + /// the decoder takes is `Unsupported`, one at the bound isn't refused. (Its one tall mark is + /// too large to be a symbol, so nothing is shared: `Ok(None)`.) + #[test] + fn shared_coding_takes_only_what_the_decoder_takes() { + let past = vec![0x7F; MAX_JBIG2_COLUMNS as usize + 1]; + let tall = (&past[..], 1, MAX_JBIG2_COLUMNS + 1); + assert!(matches!(encode_jbig2_shared(&[tall, tall]), Err(CodecError::Unsupported(_))), "too tall"); + let at = (&past[..MAX_JBIG2_COLUMNS as usize], 1, MAX_JBIG2_COLUMNS); + assert!(matches!(encode_jbig2_shared(&[at, at]), Ok(None)), "at the bound"); + } + + #[test] + fn it_encodes_only_what_the_decoder_takes() { + let unsupported = |columns, rows| matches!(encode_jbig2_generic(&[], columns, rows), Err(CodecError::Unsupported(_))); + assert!(unsupported(MAX_JBIG2_COLUMNS + 1, 1), "too wide"); + assert!(unsupported(1, MAX_JBIG2_COLUMNS + 1), "too tall"); + assert!(unsupported(16_385, 16_384), "too many samples, each side within the bound"); + assert!(unsupported(u32::MAX, u32::MAX), "the product doesn't overflow"); + // At the bounds what it writes reads back, through the decoder that has to read it. + let wide = vec![0x0F; (MAX_JBIG2_COLUMNS as usize).div_ceil(8) * 4]; + assert_eq!(decode(&encode_jbig2_generic(&wide, MAX_JBIG2_COLUMNS, 4).unwrap()), unpack(&wide, MAX_JBIG2_COLUMNS, 4)); + let tall = vec![0x7F; MAX_JBIG2_COLUMNS as usize]; + assert_eq!(decode(&encode_jbig2_generic(&tall, 1, MAX_JBIG2_COLUMNS).unwrap()), unpack(&tall, 1, MAX_JBIG2_COLUMNS)); + struct Rows(u32, u64); + impl hayro_jbig2::Decoder for Rows { + fn push_pixel(&mut self, black: bool) { + self.1 += u64::from(black); + } + fn push_pixel_chunk(&mut self, black: bool, chunks: u32) { + self.1 += u64::from(black) * u64::from(chunks) * 8; + } + fn next_line(&mut self) { + self.0 += 1; + } + } + // One black pixel, so a page that came back blank wouldn't pass. + let mut largest = vec![0xFF; 2_048 * 16_384]; + largest[0] = 0x7F; + let largest = encode_jbig2_generic(&largest, 16_384, 16_384).unwrap(); + let mut rows = Rows(0, 0); + hayro_jbig2::Image::new_embedded(&largest, None).unwrap().decode(&mut rows).unwrap(); + assert_eq!(rows.0, 16_384); + assert_eq!(rows.1, 1); + } + + proptest::proptest! { + /// Any bitmap, of noise or of runs, decodes to its own bits through an independent decoder. + #[test] + fn bitmaps_round_trip( + columns in 1u32..300, + rows in 1u32..24, + runs in proptest::collection::vec(0u8..=255, 1..48), + noise in proptest::bool::ANY, + ) { + let data = bitmap(columns, rows, &runs, noise); + let jbig2 = encode_jbig2_generic(&data, columns, rows).unwrap(); + proptest::prop_assert_eq!(decode(&jbig2), unpack(&data, columns, rows)); + } + + /// Whatever it is given, it returns. + #[test] + fn any_input_returns(data in proptest::collection::vec(0u8..=255, 0..600), columns in 0u32..5000, rows in 0u32..80) { + let _ = encode_jbig2_generic(&data, columns, rows); + } + } +} diff --git a/crates/stream-codecs/src/jbig2/symbols.rs b/crates/stream-codecs/src/jbig2/symbols.rs new file mode 100644 index 000000000..f56503a84 --- /dev/null +++ b/crates/stream-codecs/src/jbig2/symbols.rs @@ -0,0 +1,1329 @@ +//! JBIG2 symbol coding, lossless: each distinct mark of an image (a group of black pixels that +//! touch, side by side or at a corner) is stored once, as a symbol in a symbol dictionary, and +//! text regions place a copy of it wherever it occurs. Marks too large to be symbols (rules, +//! frames, pictures) are left to a generic region beside them, and so, in [`encode`]'s second +//! plan, are marks that occur only once. Only exact copies share a symbol, so the image decodes to the +//! same bits. Written from the committee draft's 6.4 (the text region decoding procedure), 6.5 +//! (the symbol dictionary decoding procedure), 7.4.2 and 7.4.3 (their segments) and Annex A (the +//! arithmetic integer coders). + +use std::collections::HashMap; +use std::hash::{DefaultHasher, Hash, Hasher}; +use std::ops::Range; + +use super::{at_pixels, code_generic, generic_region, page_information, region_information, segment, segment_of}; +use crate::mq::{Context, MqEncoder}; + +/// The longest side of a mark stored as a symbol. A larger one is left to the generic region. +const MAX_SYMBOL_SIDE: u32 = 256; + +/// The most black runs looked at. Past it (halftones, noise) the image is left to generic coding, +/// which suits it better anyway; this bounds the memory the marks take. +const MAX_RUNS: usize = 1 << 20; + +/// The most distinct marks sorted into symbols, and the most pixels their bitmaps may hold +/// together: past either the image is left to generic coding. These bound the memory they take. +const MAX_SYMBOLS: usize = 1 << 16; +const MAX_SYMBOL_PIXELS: usize = 1 << 24; + +/// The most symbols one plan codes. `hayro-jbig2`, the decoder the Craft apps render JBIG2 with, +/// refuses a dictionary whose new and exported symbols pass 65,535 together, and a plan exports +/// every symbol it codes, so it refuses 32,768 or more; others may too, so a plan with more isn't +/// made. +const MAX_CODED_SYMBOLS: usize = (1 << 15) - 1; + +/// The most symbol instances one text region places. `hayro-jbig2`, the decoder the Craft apps render JBIG2 with, +/// refuses a text region of more than 10,000 (a page it then draws blank), so a page with more is +/// placed by several regions, each over a band of rows, all referring to the one dictionary. +const MAX_REGION_INSTANCES: usize = 10_000; + +/// The most symbols a group's shared dictionary holds ([`encode_shared`]): half of what a page may +/// place, so each page keeps room for its own. +const MAX_SHARED_SYMBOLS: usize = MAX_CODED_SYMBOLS / 2; + +// Segment numbers stay at most 256, so a referred-to one takes a byte (7.2.5): the shared +// dictionary, the page, its own dictionary, a text region for each MAX_REGION_INSTANCES of at most +// MAX_RUNS marks, and the generic region. +const _: () = assert!(MAX_RUNS.div_ceil(MAX_REGION_INSTANCES) + 4 <= 256); + +/// The integer coders' ranges (Table A.1): how many 1 bits pick the range, how many bits give the +/// offset into it, and its first value. The last range has no 0 after its prefix. +const RANGES: [(u32, u32, u32); 6] = [(0, 2, 0), (1, 4, 4), (2, 6, 20), (3, 8, 84), (4, 12, 340), (5, 32, 4436)]; + +/// A run of black pixels on row `y`, from `x0` up to but not including `x1`. +#[derive(Clone, Copy, Debug)] +struct Run { + y: u32, + x0: u32, + x1: u32, +} + +/// A mark: its box (`x1` and `y1` just past it) and its runs, as a range of the run order +/// [`marks`] gives. +#[derive(Debug)] +struct Mark { + x0: u32, + x1: u32, + y0: u32, + y1: u32, + runs: Range, +} + +/// A distinct mark's bitmap, a byte a pixel (1 black), and how many marks are exact copies of it. +#[derive(Debug)] +struct Symbol { + width: u32, + height: u32, + pixels: Vec, + uses: u32, +} + +/// An image's marks, sorted out: its black runs, its marks with the run order they index, the +/// distinct marks of at most [`MAX_SYMBOL_SIDE`] a side as symbols, each such mark as (its +/// symbol, the mark), and the marks too large to be symbols. +struct Page { + runs: Vec, + marks: Vec, + order: Vec, + symbols: Vec, + copies: Vec<(u32, u32)>, + large: Vec, +} + +/// A symbol instance (6.4.5): the symbol, and where its bottom-left pixel goes on the page, as S +/// (the column) and T (the row). `width` and `height` are the symbol's. +#[derive(Clone, Copy, Debug)] +struct Instance { + id: u32, + s: u32, + t: u32, + width: u32, + height: u32, +} + +/// A group of images coded by [`encode_shared`]: the globals' data, and each image's stream. +type SharedCoding = (Vec, Vec>>); + +/// The symbols of a group's shared dictionary, as one image's [`plan`] places them: the ID each of +/// the image's symbols has there, if it is there, and how many symbols that dictionary exports. +struct Shared { + ids: Vec>, + count: u32, +} + +/// `rows` rows of `columns` 1-bit PDF samples (black 0), symbol coded: the smaller of two plans, +/// every mark of at most [`MAX_SYMBOL_SIDE`] a side as a symbol, or only those with a copy. +/// `None` when neither plan has from two to [`MAX_CODED_SYMBOLS`] symbols, or the image is too +/// large or too busy to look at. +pub(super) fn encode(samples: &[u8], columns: u32, rows: u32) -> Option> { + let page = page(samples, columns)?; + [1, 2].into_iter().filter_map(|copies| plan(&page, columns, rows, copies, None, None)).min_by_key(Vec::len) +} + +/// Images, each as [`encode`] takes one, symbol coded sharing one dictionary: the globals' data, a +/// symbol dictionary segment associated with no page (number 0) holding the distinct marks found +/// in more than one image, and each image's stream placing them from there, the smaller of +/// [`encode`]'s two plans for its other marks. The group's marks are held within the limits one +/// image's are ([`MAX_SYMBOLS`], [`MAX_SYMBOL_PIXELS`]): past them, later marks aren't shared. Past +/// [`MAX_SHARED_SYMBOLS`] shared marks, those in the most images are kept. +/// +/// An image too large or too busy to look at, or that places none of the shared marks, gets `None`; +/// `None` overall when fewer than two marks are shared. +pub(super) fn encode_shared(images: &[(&[u8], u32, u32)]) -> Option { + // The group's distinct marks, each with how many images hold it (as its uses), looked at an + // image at a time. + let mut table: Vec = Vec::new(); + let mut by_hash: HashMap> = HashMap::new(); + let mut held = 0usize; + for (samples, columns, _) in images { + let Some(page) = page(samples, *columns) else { continue }; + // An image's symbols are distinct, so each counts an image once. + for symbol in page.symbols { + match find(&table, &by_hash, &symbol) { + Some(t) => { + if let Some(s) = table.get_mut(t as usize) { + s.uses = s.uses.saturating_add(1); + } + } + None => { + let Some(more) = held.checked_add(symbol.pixels.len()).filter(|h| *h <= MAX_SYMBOL_PIXELS && table.len() < MAX_SYMBOLS) else { + continue; + }; + held = more; + let t = u32::try_from(table.len()).ok()?; + by_hash.entry(fingerprint(&symbol)).or_default().push(t); + table.push(Symbol { uses: 1, ..symbol }); + } + } + } + } + let uses = |t: &u32| table.get(*t as usize).map_or(0, |s| s.uses); + let mut shared: Vec = (0..u32::try_from(table.len()).ok()?).filter(|t| uses(t) >= 2).collect(); + if shared.len() < 2 { + return None; + } + shared.sort_by_key(|t| std::cmp::Reverse(uses(t))); + shared.truncate(MAX_SHARED_SYMBOLS); + // The dictionary's order, as `plan` keeps it. + shared.sort_by_key(|t| table.get(*t as usize).map(|s| (s.height, s.width))); + let mut global = vec![None; table.len()]; + for (k, t) in shared.iter().enumerate() { + if let Some(slot) = global.get_mut(*t as usize) { + *slot = u32::try_from(k).ok(); + } + } + let symbols: Vec<&Symbol> = shared.iter().filter_map(|t| table.get(*t as usize)).collect(); + let mut globals = Vec::new(); + segment_of(&mut globals, 0, 0, 0, &[], true, &symbol_dictionary(&symbols)?); + let count = u32::try_from(symbols.len()).ok()?; + let streams = images + .iter() + .map(|(samples, columns, rows)| { + let page = page(samples, *columns)?; + let ids = page.symbols.iter().map(|s| find(&table, &by_hash, s).and_then(|t| global.get(t as usize).copied().flatten())).collect(); + let shared = Shared { ids, count }; + [1, 2].into_iter().filter_map(|copies| plan(&page, *columns, *rows, copies, None, Some(&shared))).min_by_key(Vec::len) + }) + .collect(); + Some((globals, streams)) +} + +/// What sorts a symbol's bitmap ([`symbols`] and [`encode_shared`] look exact copies up by it). +fn fingerprint(s: &Symbol) -> u64 { + let mut hasher = DefaultHasher::new(); + (s.width, s.height, &s.pixels).hash(&mut hasher); + hasher.finish() +} + +/// The index in `table` of the symbol with `s`'s bitmap, looked up by its [`fingerprint`]. +fn find(table: &[Symbol], by_hash: &HashMap>, s: &Symbol) -> Option { + by_hash + .get(&fingerprint(s))? + .iter() + .copied() + .find(|t| table.get(*t as usize).is_some_and(|t| (t.width, t.height) == (s.width, s.height) && t.pixels == s.pixels)) +} + +/// The image's marks and symbols; `None` past the limits above. +fn page(samples: &[u8], columns: u32) -> Option { + let width = usize::try_from(columns).ok()?; + let stride = width.div_ceil(8); + if stride == 0 { + return None; + } + let runs = black_runs(samples, width, stride)?; + let (marks, order) = marks(&runs); + let mut page = Page { runs, marks, order, symbols: Vec::new(), copies: Vec::new(), large: Vec::new() }; + symbols(&mut page)?; + Some(page) +} + +/// The black runs of each row (0 samples), row by row and left to right; `None` past [`MAX_RUNS`]. +fn black_runs(samples: &[u8], width: usize, stride: usize) -> Option> { + let black = |row: &[u8], x: usize| row.get(x / 8).is_some_and(|b| b & (0x80 >> (x % 8)) == 0); + let mut runs = Vec::new(); + for (y, row) in samples.chunks_exact(stride).enumerate() { + let y = u32::try_from(y).ok()?; + let mut x = 0; + while x < width { + // Whole white bytes at once. + if x % 8 == 0 && row.get(x / 8) == Some(&0xFF) { + x += 8; + continue; + } + if !black(row, x) { + x += 1; + continue; + } + let start = x; + while x < width && black(row, x) { + x += 1; + } + if runs.len() == MAX_RUNS { + return None; + } + runs.push(Run { y, x0: u32::try_from(start).ok()?, x1: u32::try_from(x).ok()? }); + } + } + Some(runs) +} + +/// The marks the runs make, in the order their first runs come (so from the top), and the run +/// order: the runs' indexes, mark by mark, each mark's in row order. +fn marks(runs: &[Run]) -> (Vec, Vec) { + // Union–find over the runs; a set's root is its first run, as the larger root always joins + // the smaller. Runs on adjacent rows join when they overlap, counting a corner. + let mut parent: Vec = (0..runs.len()).map(|i| i as u32).collect(); + let (mut above, mut above_at, mut above_y): (&[Run], usize, Option) = (&[], 0, None); + let mut at = 0; + for row in runs.chunk_by(|a, b| a.y == b.y) { + let y = row.first().map(|r| r.y); + if above_y.and_then(|a| a.checked_add(1)) == y { + let mut i = 0; + for (j, b) in row.iter().enumerate() { + while above.get(i).is_some_and(|a| a.x1 < b.x0) { + i += 1; + } + let mut k = i; + while above.get(k).is_some_and(|a| a.x0 <= b.x1) { + union(&mut parent, above_at + k, at + j); + k += 1; + } + } + } + (above, above_at, above_y) = (row, at, y); + at += row.len(); + } + let mut mark_of = vec![0u32; runs.len()]; + let mut marks: Vec = Vec::new(); + let mut counts: Vec = Vec::new(); + for (i, r) in runs.iter().enumerate() { + let root = find_root(&mut parent, i); + let m = if root == i { + marks.push(Mark { x0: r.x0, x1: r.x1, y0: r.y, y1: r.y.saturating_add(1), runs: 0..0 }); + counts.push(0); + marks.len() - 1 + } else { + mark_of.get(root).map_or(0, |m| *m as usize) + }; + if let Some(slot) = mark_of.get_mut(i) { + *slot = m as u32; + } + if let (Some(mark), Some(count)) = (marks.get_mut(m), counts.get_mut(m)) { + mark.x0 = mark.x0.min(r.x0); + mark.x1 = mark.x1.max(r.x1); + mark.y1 = mark.y1.max(r.y.saturating_add(1)); + *count += 1; + } + } + let mut next = 0; + for (mark, count) in marks.iter_mut().zip(&counts) { + mark.runs = next..next + count; + next += count; + } + let mut order = vec![0u32; runs.len()]; + let mut fill: Vec = marks.iter().map(|m| m.runs.start).collect(); + for (i, m) in mark_of.iter().enumerate() { + if let Some(n) = fill.get_mut(*m as usize) { + if let Some(slot) = order.get_mut(*n) { + *slot = i as u32; + } + *n += 1; + } + } + (marks, order) +} + +/// The root of `i`'s set, halving the path to it as it goes. Parents are never above their +/// children, so the walk ends. +fn find_root(parent: &mut [u32], mut i: usize) -> usize { + loop { + let Some(&p) = parent.get(i) else { return i }; + let p = p as usize; + if p == i { + return i; + } + let grandparent = parent.get(p).copied().unwrap_or(p as u32); + if let Some(slot) = parent.get_mut(i) { + *slot = grandparent; + } + i = grandparent as usize; + } +} + +fn union(parent: &mut [u32], a: usize, b: usize) { + let (a, b) = (find_root(parent, a), find_root(parent, b)); + if let Some(slot) = parent.get_mut(a.max(b)) { + *slot = a.min(b) as u32; + } +} + +/// Each mark's own pixels in its box: those of its runs, not of other marks the box overlaps. +fn pixels(mark: &Mark, runs: &[Run], order: &[u32]) -> Vec { + // A mark's box holds its runs, so these don't go below 0; a symbol is at most 256 a side. + let width = mark.x1.saturating_sub(mark.x0) as usize; + let height = mark.y1.saturating_sub(mark.y0) as usize; + let mut pixels = vec![0u8; width * height]; + for r in order.get(mark.runs.clone()).unwrap_or_default().iter().filter_map(|i| runs.get(*i as usize)) { + let row = r.y.saturating_sub(mark.y0) as usize * width; + let from = row + r.x0.saturating_sub(mark.x0) as usize; + let to = row + r.x1.saturating_sub(mark.x0) as usize; + pixels.get_mut(from..to).unwrap_or_default().fill(1); + } + pixels +} + +/// Sorts the marks into symbols: exact copies share one. `None` past [`MAX_SYMBOLS`] or +/// [`MAX_SYMBOL_PIXELS`]. +fn symbols(page: &mut Page) -> Option<()> { + let mut by_hash: HashMap> = HashMap::new(); + let mut kept = 0usize; + for (m, mark) in page.marks.iter().enumerate() { + let m = u32::try_from(m).ok()?; + let (width, height) = (mark.x1.saturating_sub(mark.x0), mark.y1.saturating_sub(mark.y0)); + if width > MAX_SYMBOL_SIDE || height > MAX_SYMBOL_SIDE { + page.large.push(m); + continue; + } + let symbol = Symbol { width, height, pixels: pixels(mark, &page.runs, &page.order), uses: 0 }; + let symbols = &mut page.symbols; + let id = match find(symbols, &by_hash, &symbol) { + Some(id) => id, + None => { + kept = kept.checked_add(symbol.pixels.len())?; + if kept > MAX_SYMBOL_PIXELS || symbols.len() == MAX_SYMBOLS { + return None; + } + let id = u32::try_from(symbols.len()).ok()?; + by_hash.entry(fingerprint(&symbol)).or_default().push(id); + symbols.push(symbol); + id + } + }; + if let Some(s) = symbols.get_mut(id as usize) { + s.uses = s.uses.saturating_add(1); + } + page.copies.push((id, m)); + } + Some(()) +} + +/// One plan's stream: the symbols with at least `copies` uses in a symbol dictionary, text +/// regions placing them (in strips of 2^`log_strips` rows, or whichever size codes smallest), and +/// the other marks in a generic region. `None` with fewer than two such symbols (their IDs would +/// take no bits at all, SBSYMCODELEN 0, a corner of the standard left alone here) or more than +/// [`MAX_CODED_SYMBOLS`]. +/// +/// With `shared`, the symbols a group's dictionary holds are placed from there, whatever their uses +/// here, and the page's own dictionary holds only the others (none, when it has none), as +/// [`encode_shared`] writes them; `None` too for a page that places none of the shared ones. +fn plan(page: &Page, columns: u32, rows: u32, copies: u32, log_strips: Option, shared: Option<&Shared>) -> Option> { + let symbol = |s: u32| page.symbols.get(s as usize); + let global = |s: u32| shared.and_then(|g| g.ids.get(s as usize).copied().flatten()); + let globals = shared.map_or(0, |g| g.count); + // The dictionary's order (6.5.5): height classes from the shortest, each narrowest first. + let mut kept: Vec = + (0..page.symbols.len() as u32).filter(|s| global(*s).is_none() && symbol(*s).is_some_and(|s| s.uses >= copies)).collect(); + let count = usize::try_from(globals).ok()?.checked_add(kept.len())?; + if !(2..=MAX_CODED_SYMBOLS).contains(&count) { + return None; + } + kept.sort_by_key(|s| symbol(*s).map(|s| (s.height, s.width))); + // The shared symbols' IDs come first, as their dictionary is referred to first (Table 31's + // SBSYMS). + let mut id: Vec> = (0..page.symbols.len() as u32).map(global).collect(); + for (k, s) in kept.iter().enumerate() { + if let Some(slot) = id.get_mut(*s as usize) { + *slot = u32::try_from(k).ok().and_then(|k| k.checked_add(globals)); + } + } + let mut instances = Vec::new(); + let mut left = page.large.clone(); + for (s, m) in &page.copies { + match (id.get(*s as usize).copied().flatten(), page.marks.get(*m as usize), symbol(*s)) { + (Some(id), Some(mark), Some(symbol)) => { + instances.push(Instance { id, s: mark.x0, t: mark.y1.saturating_sub(1), width: symbol.width, height: symbol.height }) + } + _ => left.push(*m), + } + } + if shared.is_some() && !instances.iter().any(|i| i.id < globals) { + return None; + } + let kept: Vec<&Symbol> = kept.iter().filter_map(|s| symbol(*s)).collect(); + let dictionary = if kept.is_empty() { None } else { Some(symbol_dictionary(&kept)?) }; + let count = u32::try_from(count).ok()?; + // Bands of at most MAX_REGION_INSTANCES, from the top. + instances.sort_by_key(|i| (i.t, i.s)); + let mut regions = Vec::new(); + for band in instances.chunks_mut(MAX_REGION_INSTANCES) { + let top = band.iter().map(|i| i.t.saturating_add(1).saturating_sub(i.height)).min()?; + let height = band.iter().map(|i| i.t).max()?.checked_sub(top)?.checked_add(1)?; + regions.push(match log_strips { + Some(l) => text_region(band, count, columns, top, height, l)?, + None => (0..4).filter_map(|l| text_region(band, count, columns, top, height, l)).min_by_key(Vec::len)?, + }); + } + let mut out = Vec::new(); + // The shared dictionary is segment 0, in the globals, and a segment refers only to lower + // numbers (7.2.5): the page's own follow it. + let mut number = u32::from(shared.is_some()); + segment(&mut out, number, 48, &[], false, &page_information(columns, rows)); + number += 1; + let mut dictionaries: Vec = shared.map(|_| 0).into_iter().collect(); + if let Some(dictionary) = &dictionary { + segment(&mut out, number, 0, &[], true, dictionary); + dictionaries.push(u8::try_from(number).ok()?); + number += 1; + } + for (k, text) in regions.iter().enumerate() { + // The dictionaries stay retained for the next region, until the last (7.2.4). + let refers_to: Vec<(u8, bool)> = dictionaries.iter().map(|n| (*n, k + 1 < regions.len())).collect(); + segment(&mut out, number, 7, &refers_to, false, text); + number += 1; + } + if !left.is_empty() { + segment(&mut out, number, 39, &[], false, &leftover_region(&left, page)?); + } + Some(out) +} + +/// A symbol dictionary segment's data (7.4.2) defining `symbols` (in dictionary order) and +/// exporting them all: arithmetic coded, no refinement or aggregation, each bitmap a generic +/// region with template 0, its nominal AT pixels and no typical prediction (Table 16), the +/// bitmaps sharing one set of contexts. +fn symbol_dictionary(symbols: &[&Symbol]) -> Option> { + let count = u32::try_from(symbols.len()).ok()?; + let mut data = vec![0, 0]; + at_pixels(&mut data); + data.extend_from_slice(&count.to_be_bytes()); + data.extend_from_slice(&count.to_be_bytes()); + let mut mq = MqEncoder::new(); + let mut contexts: Vec = vec![0; 1 << 16]; + let (mut iadh, mut iadw, mut iaex) = (IntCoder::new(), IntCoder::new(), IntCoder::new()); + let mut height = 0; + for class in symbols.chunk_by(|a, b| a.height == b.height) { + let h = class.first()?.height; + iadh.encode(&mut mq, Some(int(i64::from(h) - i64::from(height))?)); + height = h; + let mut width = 0; + for s in class { + iadw.encode(&mut mq, Some(int(i64::from(s.width) - i64::from(width))?)); + width = s.width; + let w = s.width as usize; + code_generic(&mut mq, &mut contexts, w, s.height as usize, false, |y, row| { + let line = s.pixels.get(y * w..).unwrap_or_default(); + row.iter_mut().zip(line).for_each(|(p, v)| *p = *v); + }); + } + iadw.encode(&mut mq, None); + } + // The export flags (6.5.10): a run of no symbols not exported, then a run of all of them. + iaex.encode(&mut mq, Some(0)); + iaex.encode(&mut mq, Some(int(i64::from(count))?)); + data.extend_from_slice(&mq.finish()); + Some(data) +} + +/// An immediate lossless text region segment's data (7.4.3) placing `instances` in the band of +/// `height` rows from row `top` (which holds them): arithmetic coded, no refinement, strips of +/// 2^`log_strips` rows, each symbol's bottom-left pixel at (S, T) (REFCORNER BOTTOMLEFT, so a +/// line's letters share their T), not transposed, OR, default pixel 0, SBDSOFFSET 0. Sorts +/// `instances` into strips, each left to right. +fn text_region(instances: &mut [Instance], symbols: u32, columns: u32, top: u32, height: u32, log_strips: u32) -> Option> { + // LOGSBSTRIPS has two bits: strips of 1, 2, 4 or 8 rows. + if log_strips > 3 { + return None; + } + let strips = 1i64 << log_strips; + // T counts from the band's top row. + let t = |i: &Instance| i.t.saturating_sub(top); + instances.sort_by_key(|i| (t(i) >> log_strips, i.s)); + let mut data = Vec::new(); + region_information(&mut data, columns, height, 0, top); + data.extend_from_slice(&((log_strips as u16) << 2).to_be_bytes()); + data.extend_from_slice(&u32::try_from(instances.len()).ok()?.to_be_bytes()); + // SBSYMCODELEN (Table 31): ⌈log2 SBNUMSYMS⌉. + let length = symbols.checked_next_power_of_two()?.trailing_zeros(); + let mut ids: Vec = vec![0; 1 << length]; + let (mut iadt, mut iafs, mut iads, mut iait) = (IntCoder::new(), IntCoder::new(), IntCoder::new(), IntCoder::new()); + let mut mq = MqEncoder::new(); + // The initial STRIPT (6.4.5 step 2) is 0, so each strip's delta T is its first row over the + // strip size, from the strip before. + iadt.encode(&mut mq, Some(0)); + let (mut strip_t, mut first_s) = (0i64, 0i64); + for strip in instances.chunk_by(|a, b| t(a) >> log_strips == t(b) >> log_strips) { + let first = i64::from(t(strip.first()?) >> log_strips << log_strips); + iadt.encode(&mut mq, Some(int((first - strip_t) / strips)?)); + strip_t = first; + let mut cur_s = 0; + for (k, instance) in strip.iter().enumerate() { + let s = i64::from(instance.s); + if k == 0 { + iafs.encode(&mut mq, Some(int(s - first_s)?)); + first_s = s; + } else { + iads.encode(&mut mq, Some(int(s - cur_s)?)); + } + if strips > 1 { + iait.encode(&mut mq, Some(int(i64::from(t(instance)) - strip_t)?)); + } + encode_id(&mut mq, &mut ids, length, instance.id); + // The next S counts from this symbol's last column (6.4.5 step 3c x). + cur_s = s + i64::from(instance.width) - 1; + } + iads.encode(&mut mq, None); + } + data.extend_from_slice(&mq.finish()); + Some(data) +} + +/// A generic region segment's data holding the marks `left`, over their box. +fn leftover_region(left: &[u32], page: &Page) -> Option> { + let left: Vec<&Mark> = left.iter().filter_map(|m| page.marks.get(*m as usize)).collect(); + let x0 = left.iter().map(|m| m.x0).min()?; + let y0 = left.iter().map(|m| m.y0).min()?; + let x1 = left.iter().map(|m| m.x1).max()?; + let y1 = left.iter().map(|m| m.y1).max()?; + let (width, height) = (x1.saturating_sub(x0) as usize, y1.saturating_sub(y0) as usize); + let stride = width.div_ceil(8); + // Packed, 1 for black: no larger than the image's own samples. + let mut packed = vec![0u8; stride.checked_mul(height)?]; + for mark in &left { + for r in page.order.get(mark.runs.clone()).unwrap_or_default().iter().filter_map(|i| page.runs.get(*i as usize)) { + let row = r.y.saturating_sub(y0) as usize * stride; + for x in r.x0.saturating_sub(x0) as usize..r.x1.saturating_sub(x0) as usize { + if let Some(b) = packed.get_mut(row + x / 8) { + *b |= 0x80 >> (x % 8); + } + } + } + } + let mut mq = MqEncoder::new(); + let mut contexts: Vec = vec![0; 1 << 16]; + code_generic(&mut mq, &mut contexts, width, height, true, |y, row| { + let line = packed.get(y * stride..).unwrap_or_default(); + for (x, p) in row.iter_mut().enumerate() { + *p = u8::from(line.get(x / 8).is_some_and(|b| b & (0x80 >> (x % 8)) != 0)); + } + }); + Some(generic_region(u32::try_from(width).ok()?, u32::try_from(height).ok()?, x0, y0, &mq.finish())) +} + +fn int(v: i64) -> Option { + i32::try_from(v).ok() +} + +/// An arithmetic integer coder other than IAID (A.2): its 512 contexts, indexed by PREV. +struct IntCoder([Context; 512]); + +impl IntCoder { + fn new() -> Self { + IntCoder([0; 512]) + } + + /// Codes `value`, or OOB for `None`. + fn encode(&mut self, mq: &mut MqEncoder, value: Option) { + for (cx, d) in decisions(value) { + if let Some(state) = self.0.get_mut(cx) { + mq.encode(state, d); + } + } + } +} + +/// The decisions that code `value`, or OOB for `None`, with their contexts (A.2, Table A.1): the +/// sign, the prefix that picks the range, then the offset into it, most significant bit first; +/// OOB is a negative 0. Each decision's context is PREV: a leading 1 and the decisions before it, +/// at most the last eight once there are more. +fn decisions(value: Option) -> Vec<(usize, bool)> { + let (negative, magnitude) = value.map_or((true, 0), |v| (v < 0, v.unsigned_abs())); + let (ones, bits, first) = + RANGES.into_iter().find(|(_, bits, first)| *bits == 32 || u64::from(magnitude) < u64::from(*first) + (1 << bits)).unwrap_or(RANGES[5]); + let offset = magnitude - first; + let mut d = vec![negative]; + d.extend(std::iter::repeat_n(true, ones as usize)); + if ones < 5 { + d.push(false); + } + d.extend((0..bits).rev().map(|i| offset >> i & 1 == 1)); + let mut prev = 1usize; + d.into_iter() + .map(|bit| { + let cx = prev; + prev = if prev < 256 { prev << 1 | usize::from(bit) } else { ((prev << 1 | usize::from(bit)) & 511) | 256 }; + (cx, bit) + }) + .collect() +} + +/// Codes symbol ID `id` in `length` bits, most significant first, through IAID (A.3): each +/// decision's context is a leading 1 and all the bits before it. +fn encode_id(mq: &mut MqEncoder, contexts: &mut [Context], length: u32, id: u32) { + let mut prev = 1usize; + for i in (0..length).rev() { + let d = id >> i & 1 == 1; + if let Some(state) = contexts.get_mut(prev) { + mq.encode(state, d); + } + prev = prev << 1 | usize::from(d); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::CodecError; + use crate::jbig2::tests::{decode, decode_with, samples, unpack}; + use crate::jbig2::{encode_jbig2, encode_jbig2_generic, encode_jbig2_shared}; + + type Bitmap = Vec>; + + /// A small linear congruential generator, so the pictures are the same every run. + struct Lcg(u64); + + impl Lcg { + fn below(&mut self, n: usize) -> usize { + self.0 = self.0.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407); + (self.0 >> 33) as usize % n.max(1) + } + } + + /// `count` different letter-like shapes: three strokes two pixels thick in a box of 6 to 18 + /// pixels a side, each after the first starting on one before it, so a shape is one mark; + /// trimmed to its pixels. + fn glyphs(count: usize, seed: u64) -> Vec { + let mut r = Lcg(seed); + let mut out: Vec = Vec::new(); + while out.len() < count { + let (w, h) = (6 + r.below(13), 6 + r.below(13)); + let mut g = vec![vec![0u8; w]; h]; + let (mut x, mut y) = (r.below(w), r.below(h)); + for _ in 0..3 { + let (dx, dy) = [(1, 0), (0, 1), (1, 1)][r.below(3)]; + let mut stroke = Vec::new(); + while x < w && y < h { + g[y][x] = 1; + g[y][(x + 1).min(w - 1)] = 1; + stroke.push((x, y)); + x += dx; + y += dy; + } + (x, y) = stroke[r.below(stroke.len())]; + } + let rows: Vec = (0..h).filter(|y| g[*y].contains(&1)).collect(); + let columns: Vec = (0..w).filter(|x| g.iter().any(|row| row[*x] == 1)).collect(); + let g: Bitmap = g[rows[0]..=rows[rows.len() - 1]].iter().map(|row| row[columns[0]..=columns[columns.len() - 1]].to_vec()).collect(); + if !out.contains(&g) { + out.push(g); + } + } + out + } + + /// Draws `glyph` with its top-left pixel at (`x`, `y`), clipped to the page, by OR. + fn draw(page: &mut Bitmap, glyph: &Bitmap, x: i64, y: i64) { + for (gy, row) in glyph.iter().enumerate() { + for (gx, p) in row.iter().enumerate() { + let (px, py) = (x + gx as i64, y + gy as i64); + if *p == 1 + && px >= 0 + && py >= 0 + && let Some(cell) = page.get_mut(py as usize).and_then(|r| r.get_mut(px as usize)) + { + *cell = 1; + } + } + } + } + + /// Lines of text: glyphs side by side, one to four pixels apart, each line's glyphs on its + /// baseline, the lines 30 rows apart; the first glyphs are the most common, as letters are. + fn text(columns: usize, rows: usize, glyphs: &[Bitmap], seed: u64) -> Bitmap { + let mut page = vec![vec![0u8; columns]; rows]; + let mut r = Lcg(seed); + for baseline in (24..rows).step_by(30) { + let mut x = 4 + r.below(8); + loop { + let common = r.below(glyphs.len()) + 1; + let g = &glyphs[r.below(common)]; + if x + g[0].len() > columns { + break; + } + draw(&mut page, g, x as i64, (baseline + 1 - g.len()) as i64); + x += g[0].len() + 1 + r.below(4); + } + } + page + } + + /// `n` glyphs anywhere, some over the edges, some over each other. + fn scatter(columns: usize, rows: usize, glyphs: &[Bitmap], seed: u64, n: usize) -> Bitmap { + let mut page = vec![vec![0u8; columns]; rows]; + let mut r = Lcg(seed); + for _ in 0..n { + let g = &glyphs[r.below(glyphs.len())]; + draw(&mut page, g, r.below(columns + 12) as i64 - 6, r.below(rows + 12) as i64 - 6); + } + page + } + + /// A segment's number and type, whether it is retained, and the segments it refers to, each + /// with its retain bit. + type Segment = (u32, u8, bool, Vec<(u8, bool)>); + + /// The segments of a stream in the embedded organisation, from their headers (7.2). + fn segments(data: &[u8]) -> Vec { + let (mut segments, mut at) = (Vec::new(), 0); + while at < data.len() { + let flags = data[at + 5]; + let refers = usize::from(flags >> 5); + let length_at = at + 6 + refers + 1; + let length = u32::from_be_bytes(data[length_at..length_at + 4].try_into().unwrap()) as usize; + let referred = data[at + 6..at + 6 + refers].iter().enumerate().map(|(k, n)| (*n, flags >> (k + 1) & 1 == 1)).collect(); + let number = u32::from_be_bytes(data[at..at + 4].try_into().unwrap()); + segments.push((number, data[at + 4] & 0x3F, flags & 1 == 1, referred)); + at = length_at + 4 + length; + } + segments + } + + fn segment_types(data: &[u8]) -> Vec { + segments(data).into_iter().map(|(_, kind, ..)| kind).collect() + } + + /// Every plan, at every strip size, decodes to the picture; returns how many plans there were. + fn every_plan_round_trips(picture: &Bitmap) -> usize { + let (columns, rows) = (picture[0].len() as u32, picture.len() as u32); + let data = samples(picture); + let page = page(&data, columns).unwrap(); + let mut plans = 0; + for copies in [1, 2] { + for log_strips in 0..4 { + if let Some(coded) = plan(&page, columns, rows, copies, Some(log_strips), None) { + assert_eq!(decode(&coded), unpack(&data, columns, rows), "{copies} copies, strips of {}", 1 << log_strips); + plans += 1; + } + } + } + plans + } + + /// Annex A.2's example (12 through IADW: the decisions 0101000, in contexts 1, 10, 101, …) + /// and Table A.1's codes. + #[test] + fn integers_are_coded_as_annex_a_says() { + assert_eq!(decisions(Some(12)), [(1, false), (2, true), (5, false), (10, true), (21, false), (42, false), (84, false)]); + let bits = |v| decisions(v).iter().map(|(_, d)| u8::from(*d)).collect::>(); + assert_eq!(bits(None), [1, 0, 0, 0], "OOB"); + assert_eq!(bits(Some(0)), [0, 0, 0, 0]); + assert_eq!(bits(Some(-1)), [1, 0, 0, 1]); + assert_eq!(bits(Some(-3)), [1, 0, 1, 1]); + assert_eq!(bits(Some(4)), [0, 1, 0, 0, 0, 0, 0]); + assert_eq!(bits(Some(-19)), [1, 1, 0, 1, 1, 1, 1]); + assert_eq!(bits(Some(83)), [0, 1, 1, 0, 1, 1, 1, 1, 1, 1]); + assert_eq!(bits(Some(4435))[..6], [0, 1, 1, 1, 1, 0]); + let big = bits(Some(4436)); + assert_eq!((big.len(), &big[..6]), (6 + 32, &[0, 1, 1, 1, 1, 1][..])); + assert!(big[6..].iter().all(|b| *b == 0)); + // PREV keeps a leading 1 and the last eight decisions, so it stays below 512, also when + // those eight are all 0 (PREV exactly 256), as in 4436's offset of 0. + for value in [Some(i32::MIN), Some(4436), Some(-4436), Some(4436 + 0x100), Some(i32::MAX), None] { + let contexts: Vec = decisions(value).iter().map(|(cx, _)| *cx).collect(); + assert!(contexts.iter().take(8).all(|cx| *cx < 256) && contexts.iter().skip(8).all(|cx| (256..512).contains(cx)), "{value:?}"); + } + assert!(decisions(Some(4436)).iter().any(|(cx, _)| *cx == 256)); + } + + /// Strips and first columns thousands of pixels apart need Annex A's 32-bit range: a mark at + /// the far right of a 12,000-pixel page, then one at the left below it (S back by 11,000), and + /// lines 5,000 rows apart. Each shape occurs twice, so both plans are made. + #[test] + fn coordinates_past_4435_round_trip() { + let shapes = glyphs(3, 4); + let mut picture = vec![vec![0u8; 12_000]; 40]; + for (k, x) in [11_000, 0, 5_000, 11_980].into_iter().enumerate() { + draw(&mut picture, &shapes[k % 2], x, 2 + 9 * k as i64); + } + assert_eq!(every_plan_round_trips(&picture), 8); + let mut tall = vec![vec![0u8; 30]; 10_050]; + for (k, y) in [0, 5_000, 10_030].into_iter().enumerate() { + draw(&mut tall, &shapes[k % 3], 3, y); + draw(&mut tall, &shapes[(k + 1) % 3], 15, y); + } + assert_eq!(every_plan_round_trips(&tall), 8); + } + + /// The padding bits of a row whose width isn't a multiple of 8 are ignored whatever they are: + /// black (0) padding, as many scans have, neither joins the marks nor stops symbol coding. + #[test] + fn black_padding_bits_are_ignored() { + for columns in [13, 241, 901] { + let picture = text(columns, 120, &glyphs(12, 6), 8); + let mut data = samples(&picture); + let stride = columns.div_ceil(8); + for row in data.chunks_mut(stride) { + row[stride - 1] &= 0xFF << (8 * stride - columns); + } + let (c, r) = (columns as u32, 120); + let page = page(&data, c).unwrap(); + assert!(page.marks.iter().all(|m| m.x1 as usize <= columns), "{columns} columns"); + for copies in [1, 2] { + if let Some(coded) = plan(&page, c, r, copies, None, None) { + assert_eq!(decode(&coded), unpack(&data, c, r), "{columns} columns"); + } + } + if columns > 13 { + assert_eq!(segment_types(&encode_jbig2(&data, c, r).unwrap())[1], 0, "{columns} columns: symbol coded"); + } + } + } + + /// Pixels touching only at a corner, down to the right or down to the left, are one mark + /// (8-connected); side by side with a white column between, two. A mark of 256 pixels a side + /// is a symbol; one 257 wide, or 257 tall, is left to the generic region. The diagonal and the + /// dot occur twice, so both plans are made. + #[test] + fn marks_join_at_corners_and_large_ones_are_left_out() { + let mut picture = vec![vec![0u8; 300]; 300]; + for i in 0..6 { + picture[10 + i][10 + i] = 1; + picture[10 + i][100 + i] = 1; + picture[10 + i][60 - i] = 1; + } + for row in &mut picture[0..257] { + row[280] = 1; + } + picture[30][10] = 1; + picture[30][12] = 1; + picture[40][0..256].fill(1); + picture[60][0..257].fill(1); + for row in &mut picture[44..300] { + row[290] = 1; + } + let data = samples(&picture); + let page = page(&data, 300).unwrap(); + let boxes: Vec<(u32, u32, u32, u32)> = page.marks.iter().map(|m| (m.x0, m.y0, m.x1 - m.x0, m.y1 - m.y0)).collect(); + assert_eq!( + boxes, + [ + (280, 0, 1, 257), + (10, 10, 6, 6), + (55, 10, 6, 6), + (100, 10, 6, 6), + (10, 30, 1, 1), + (12, 30, 1, 1), + (0, 40, 256, 1), + (290, 44, 1, 256), + (0, 60, 257, 1) + ] + ); + let large: Vec<(u32, u32)> = page.large.iter().map(|m| (page.marks[*m as usize].x0, page.marks[*m as usize].y0)).collect(); + assert_eq!(large, [(280, 0), (0, 60)]); + assert_eq!(every_plan_round_trips(&picture), 8); + } + + /// Past 2^20 black runs the image is left to generic coding; at the cap it is still looked at. + #[test] + fn busy_images_are_left_to_generic_coding() { + // 0x55: four black pixels a byte, each its own run, under the same ones on the next row. + let columns = 1 << 20; + let row = vec![0x55u8; columns / 8]; + assert!(page(&row.repeat(2), columns as u32).is_some(), "2^20 runs"); + assert!(page(&row.repeat(3), columns as u32).is_none(), "1.5 × 2^20 runs"); + } + + /// A plan codes at most 32,767 symbols, which an independent decoder reads back; with more, + /// the second plan can still code the repeated ones. Past 65,536 distinct marks, or 2^24 + /// pixels of them, the image is left to generic coding; at either cap it is still looked at. + #[test] + fn too_many_symbols_are_left_to_generic_coding() { + // `n` distinct marks of 17 × 3 (full top and bottom rows joined by a code of 1 to n, in + // binary, on the row between), then `again` more with the codes 1 and 2 in turn. + let codes = |n: usize, again: usize| { + let (columns, rows) = (256 * 18, (n + again).div_ceil(256) * 4); + let mut picture = vec![vec![0u8; columns]; rows]; + for k in 0..n + again { + let (x, y) = (k % 256 * 18, k / 256 * 4); + let code = if k < n { k + 1 } else { (k - n) % 2 + 1 }; + picture[y][x..x + 17].fill(1); + picture[y + 2][x..x + 17].fill(1); + for b in (0..17).filter(|b| code >> b & 1 == 1) { + picture[y + 1][x + b] = 1; + } + } + (samples(&picture), columns as u32, rows as u32) + }; + let (data, columns, rows) = codes(MAX_CODED_SYMBOLS, 0); + let at_cap = page(&data, columns).unwrap(); + assert_eq!(decode(&plan(&at_cap, columns, rows, 1, None, None).unwrap()), unpack(&data, columns, rows)); + // One more distinct mark, each once: neither plan is made. + let (data, columns, rows) = codes(MAX_CODED_SYMBOLS + 1, 0); + let past = page(&data, columns).unwrap(); + assert!(plan(&past, columns, rows, 1, None, None).is_none() && plan(&past, columns, rows, 2, None, None).is_none()); + assert_eq!(encode_jbig2(&data, columns, rows).unwrap(), encode_jbig2_generic(&data, columns, rows).unwrap()); + // With two of them repeated, the second plan codes those two. + let (data, columns, rows) = codes(MAX_CODED_SYMBOLS + 1, 200); + let past = page(&data, columns).unwrap(); + assert!(plan(&past, columns, rows, 1, None, None).is_none()); + assert_eq!(decode(&plan(&past, columns, rows, 2, None, None).unwrap()), unpack(&data, columns, rows)); + // Above a page of text whose letters repeat, that second plan is the one `encode_jbig2` + // keeps: the first, of every mark, would code too many symbols. + let mut picture: Bitmap = vec![vec![0u8; 256 * 18]; 512]; + for k in 0..=MAX_CODED_SYMBOLS { + let (x, y) = (k % 256 * 18, k / 256 * 4); + picture[y][x..x + 17].fill(1); + picture[y + 2][x..x + 17].fill(1); + for b in (0..17).filter(|b| (k + 1) >> b & 1 == 1) { + picture[y + 1][x + b] = 1; + } + } + picture.extend(text(256 * 18, 600, &glyphs(40, 7), 11)); + let data = samples(&picture); + let (columns, rows) = (picture[0].len() as u32, picture.len() as u32); + let coded = encode_jbig2(&data, columns, rows).unwrap(); + assert_eq!(segment_types(&coded)[1], 0, "symbol coded"); + assert_eq!(decode(&coded), unpack(&data, columns, rows)); + let (data, columns, _) = codes(MAX_SYMBOLS, 0); + assert_eq!(page(&data, columns).map(|p| p.symbols.len()), Some(MAX_SYMBOLS)); + let (data, columns, _) = codes(MAX_SYMBOLS + 1, 0); + assert!(page(&data, columns).is_none()); + // `n` black squares of 256 × 256, each with a white pixel in a different place: 2^16 + // pixels a symbol, so 256 of them are exactly the cap. + let squares = |n: usize| { + let (columns, rows) = (16 * 258, n.div_ceil(16) * 258); + let mut picture = vec![vec![0u8; columns]; rows]; + for k in 0..n { + let (x, y) = (k % 16 * 258, k / 16 * 258); + for row in &mut picture[y..y + 256] { + row[x..x + 256].fill(1); + } + picture[y + 1 + k / 254][x + 1 + k % 254] = 0; + } + (samples(&picture), columns as u32, rows as u32) + }; + let (data, columns, rows) = squares(256); + let at_cap = page(&data, columns).unwrap(); + assert_eq!(at_cap.symbols.len(), 256); + assert_eq!(decode(&plan(&at_cap, columns, rows, 1, None, None).unwrap()), unpack(&data, columns, rows)); + let (data, columns, _) = squares(257); + assert!(page(&data, columns).is_none()); + } + + /// More than 10,000 marks are placed by several text regions, each of at most 10,000 (more + /// than `hayro-jbig2` accepts in one), over bands of rows, all referring to + /// the dictionary, which each region but the last keeps retained; the generic region comes + /// after them, every segment numbered one more than the one before. + #[test] + fn more_than_10000_marks_are_placed_by_several_regions() { + // 25,000 copies of two 17 × 3 shapes, 256 a line, the lines 5 rows apart, and a bar too + // tall to be a symbol beside them. + let mut picture = vec![vec![0u8; 256 * 18 + 100]; 25_000usize.div_ceil(256) * 5]; + for k in 0..25_000 { + let (x, y) = (k % 256 * 18, k / 256 * 5); + picture[y][x..x + 17].fill(1); + picture[y + 2][x..x + 17].fill(1); + picture[y + 1][x + k % 2] = 1; + } + for row in &mut picture[0..300] { + row[256 * 18 + 50] = 1; + } + let data = samples(&picture); + let (columns, rows) = (picture[0].len() as u32, picture.len() as u32); + let coded = encode_jbig2(&data, columns, rows).unwrap(); + let region = |number, kept| (number, 7, false, vec![(1, kept)]); + assert_eq!( + segments(&coded), + [(0, 48, false, vec![]), (1, 0, true, vec![]), region(2, true), region(3, true), region(4, false), (5, 39, false, vec![])] + ); + assert_eq!(decode(&coded), unpack(&data, columns, rows)); + assert_eq!(every_plan_round_trips(&picture), 8); + } + + /// A page of text whose letters repeat exactly, as rendered text does, keeps each letter once: + /// a symbol dictionary and a text region, much smaller than the generic coding. + #[test] + fn repeated_marks_are_stored_once() { + let picture = text(1700, 400, &glyphs(40, 7), 11); + let data = samples(&picture); + let coded = encode_jbig2(&data, 1700, 400).unwrap(); + let generic = encode_jbig2_generic(&data, 1700, 400).unwrap(); + // The dictionary is retained for the text region, which refers to it (7.2.4, 7.2.5). + assert_eq!(segments(&coded), [(0, 48, false, vec![]), (1, 0, true, vec![]), (2, 7, false, vec![(1, false)])]); + assert_eq!(decode(&coded), unpack(&data, 1700, 400)); + assert!(coded.len() * 2 < generic.len(), "symbols {} bytes, generic {}", coded.len(), generic.len()); + assert_eq!(every_plan_round_trips(&picture), 8); + } + + /// Marks too large to be symbols (a frame, a rule) go to a generic region over their box, and + /// with the second plan so do marks that occur once; the page still decodes to its bits. + #[test] + fn large_and_lone_marks_are_left_to_a_generic_region() { + let mut picture = text(900, 300, &glyphs(25, 3), 5); + picture[10][100..800].fill(1); + picture[290][100..800].fill(1); + for row in &mut picture[10..=290] { + row[100] = 1; + row[799] = 1; + } + // A lone blob, unlike any letter. + for (y, row) in picture[150..170].iter_mut().enumerate() { + row[850 + y % 7] = 1; + } + let data = samples(&picture); + let coded = encode_jbig2(&data, 900, 300).unwrap(); + assert_eq!(segment_types(&coded), [48, 0, 7, 39]); + assert_eq!(decode(&coded), unpack(&data, 900, 300)); + assert_eq!(every_plan_round_trips(&picture), 8); + } + + /// Marks on every edge, marks touching only at a corner (one mark), a mark inside another's + /// box, and marks over each other's boxes. + #[test] + fn marks_at_the_edges_and_corners_round_trip() { + let dot = vec![vec![1u8, 1], vec![1, 1]]; + let ell = vec![vec![1u8, 0, 0], vec![1, 0, 0], vec![1, 1, 1]]; + let mut picture = vec![vec![0u8; 41]; 23]; + for (x, y) in [(0, 0), (39, 0), (0, 21), (39, 21), (20, 0), (0, 10), (39, 10), (20, 21)] { + draw(&mut picture, &dot, x, y); + } + for (x, y) in [(10, 5), (28, 14), (12, 14)] { + draw(&mut picture, &ell, x, y); + } + // Corners touching: one mark (`marks_join_at_corners_and_large_ones_are_left_out` checks it). + picture[3][6] = 1; + picture[4][7] = 1; + picture[5][8] = 1; + // A ring with a dot inside, and an ell over the ring's box. + for i in 0..7 { + picture[8][20 + i] = 1; + picture[14][20 + i] = 1; + picture[8 + i][20] = 1; + picture[8 + i][26] = 1; + } + draw(&mut picture, &dot, 22, 10); + draw(&mut picture, &ell, 25, 15); + assert_eq!(every_plan_round_trips(&picture), 8); + } + + /// One shape, however often, would need symbol IDs of no bits: that image is coded as a generic + /// region, as are a blank page and pure noise. + #[test] + fn one_shape_blank_pages_and_noise_are_coded_generically() { + let letter = &glyphs(1, 9)[..]; + let mut r = Lcg(1); + let noise: Bitmap = (0..60).map(|_| (0..200).map(|_| u8::from(r.below(2) == 1)).collect()).collect(); + for picture in [text(600, 200, letter, 2), vec![vec![0u8; 300]; 40], noise] { + let (columns, rows) = (picture[0].len() as u32, picture.len() as u32); + let data = samples(&picture); + assert_eq!(encode_jbig2(&data, columns, rows).unwrap(), encode_jbig2_generic(&data, columns, rows).unwrap()); + } + } + + /// Each segment's page association (7.2.6): the byte after the numbers it refers to. + fn associations(data: &[u8]) -> Vec { + let (mut pages, mut at) = (Vec::new(), 0); + while at < data.len() { + let refers = usize::from(data[at + 5] >> 5); + pages.push(data[at + 6 + refers]); + let length_at = at + 6 + refers + 1; + at = length_at + 4 + u32::from_be_bytes(data[length_at..length_at + 4].try_into().unwrap()) as usize; + } + pages + } + + /// `pictures`, `columns` × `rows` each, coded sharing a dictionary; each image's stream, which + /// has to be there, decodes with the globals to its picture. + fn shared_round_trip(pictures: &[Bitmap]) -> (Vec, Vec>) { + let (columns, rows) = (pictures[0][0].len() as u32, pictures[0].len() as u32); + let data: Vec> = pictures.iter().map(|p| samples(p)).collect(); + let images: Vec<(&[u8], u32, u32)> = data.iter().map(|d| (&d[..], columns, rows)).collect(); + let shared = encode_jbig2_shared(&images).unwrap().unwrap(); + let (globals, coded) = (shared.globals, shared.images); + let coded: Vec> = coded.into_iter().map(Option::unwrap).collect(); + for (d, c) in data.iter().zip(&coded) { + assert_eq!(decode_with(c, Some(&globals)), unpack(d, columns, rows)); + } + (globals, coded) + } + + /// `font` with a stem 20 pixels tall down each glyph's left side: a font sharing no glyph with + /// any [`glyphs`] gives, whose glyphs are at most 18 pixels tall. + fn stemmed(font: &[Bitmap]) -> Vec { + font.iter() + .map(|g| { + (0..g.len().max(20)).map(|y| std::iter::once(1).chain(g.get(y).cloned().unwrap_or_else(|| vec![0; g[0].len()])).collect()).collect() + }) + .collect() + } + + /// Mark `k` of a set of distinct ones: a stroke 33 pixels long with a pixel above every other + /// column for each 1 bit of `k`, below 2^17. + fn comb(k: usize) -> Bitmap { + let mut g = vec![vec![0u8; 33]; 2]; + g[1].fill(1); + for bit in 0..17 { + g[0][2 * bit] = u8::from(k >> bit & 1 == 1); + } + g + } + + /// The combs `ks`, 120 a row, the rows four pixels apart. + fn combs(ks: impl ExactSizeIterator) -> Bitmap { + let mut page = vec![vec![0u8; 120 * 36]; ks.len().div_ceil(120) * 4]; + for (i, k) in ks.enumerate() { + draw(&mut page, &comb(k), (i % 120 * 36) as i64, (i / 120 * 4) as i64); + } + page + } + + /// The symbols a symbol dictionary segment exports (SDNUMEXSYMS, 7.4.2.1.4), in a stream + /// that is that segment alone: after its header, flags and adaptive pixels. + fn exported(globals: &[u8]) -> usize { + u32::from_be_bytes(globals[21..25].try_into().unwrap()) as usize + } + + /// Pages in one font share its letters: one symbol dictionary, segment 0, associated with no + /// page and retained (7.2.4, 7.2.6), which each page's text region refers to first, its own + /// segments numbered after it (7.2.5). Together they take less than they take alone. + #[test] + fn pages_in_one_font_share_one_dictionary() { + let font = glyphs(40, 7); + let pictures: Vec = (0..4).map(|seed| text(1200, 300, &font, 20 + seed)).collect(); + let (globals, coded) = shared_round_trip(&pictures); + assert_eq!(segments(&globals), [(0, 0, true, vec![])]); + assert_eq!(associations(&globals), [0]); + let (mut shared, mut alone) = (globals.len(), 0); + for (picture, c) in pictures.iter().zip(&coded) { + let segments = segments(c); + assert_eq!(segments.first().map(|s| s.0), Some(1), "the page's own segments follow"); + assert!(segments.iter().filter(|s| s.1 == 7).all(|s| s.3.first() == Some(&(0, false))), "{segments:?}"); + assert!(associations(c).iter().all(|page| *page == 1)); + shared += c.len(); + alone += encode_jbig2(&samples(picture), 1200, 300).unwrap().len(); + } + assert!(shared < alone, "shared {shared} bytes, alone {alone}"); + } + + /// Two pages alike place every mark from the shared dictionary and have none of their own; a + /// page with letters of its own as well keeps them in its own dictionary, referred to after the + /// shared one (its symbols' IDs follow), and the page with nothing shared has no stream. + #[test] + fn what_a_page_has_alone_stays_its_own() { + let (font, other) = (glyphs(30, 7), stemmed(&glyphs(20, 99))); + let same = text(900, 240, &font, 3); + let mut more = text(900, 240, &font, 4); + more.extend(text(900, 120, &other, 5)); + let (globals, coded) = shared_round_trip(&[same.clone(), same.clone()]); + assert!(!globals.is_empty()); + assert!(coded.iter().all(|c| segment_types(c) == [48, 7]), "{:?}", coded.iter().map(|c| segment_types(c)).collect::>()); + let with_more = |p: &Bitmap| p.iter().cloned().chain(std::iter::repeat_n(vec![0u8; 900], 120)).collect::(); + let (_, coded) = shared_round_trip(&[with_more(&same), more]); + assert_eq!(segment_types(&coded[1])[..3], [48, 0, 7]); + assert_eq!(segments(&coded[1])[2].3, [(0, false), (2, false)]); + let alone = text(900, 360, &other, 6); + let data: Vec> = [&same, &same, &alone].iter().map(|p| samples(p)).collect(); + let images: Vec<(&[u8], u32, u32)> = vec![(&data[0], 900, 240), (&data[1], 900, 240), (&data[2], 900, 360)]; + let (_, coded) = encode_shared(&images).unwrap(); + assert!(coded[0].is_some() && coded[1].is_some() && coded[2].is_none()); + } + + /// Images that share fewer than two marks share nothing; two are enough. + #[test] + fn images_with_nothing_in_common_share_nothing() { + let font = glyphs(30, 7); + let (a, b) = (samples(&text(600, 200, &font, 1)), samples(&text(600, 200, &stemmed(&font), 1))); + assert!(encode_shared(&[(&a, 600, 200), (&b, 600, 200)]).is_none()); + assert!(encode_shared(&[(&a, 600, 200)]).is_none(), "one image"); + assert!(encode_shared(&[]).is_none()); + // The two commonest letters of the first page, below the last line of the second. + let mut two = text(600, 200, &stemmed(&font), 1); + draw(&mut two, &font[0], 10, 180); + draw(&mut two, &font[1], 100, 180); + let two = samples(&two); + let (globals, coded) = encode_shared(&[(&a, 600, 200), (&two, 600, 200)]).unwrap(); + assert_eq!(exported(&globals), 2); + assert_eq!(decode_with(coded[1].as_ref().unwrap(), Some(&globals)), unpack(&two, 600, 200)); + } + + /// The images of a group are checked as `encode_jbig2_generic` checks one, and an image it + /// would refuse refuses the group. + #[test] + fn a_group_with_an_image_the_encoder_refuses_is_refused() { + let page = samples(&text(600, 200, &glyphs(30, 7), 1)); + assert!(matches!(encode_jbig2_shared(&[(&page, 600, 200), (&page[..10], 600, 200)]), Err(CodecError::Corrupt { .. }))); + assert!(matches!(encode_jbig2_shared(&[(&page, 600, 200), (&page, 0, 200)]), Err(CodecError::Unsupported(_)))); + assert!(encode_jbig2_shared(&[(&page, 600, 200), (&page, 600, 200)]).unwrap().is_some()); + } + + /// A group holds its distinct marks within what one image's are held to: past 65,536 a mark + /// first seen later isn't held, so isn't shared, while those held count every image they're + /// in. Ten marks of the first image are in two images after it, with 464 first seen there. + #[test] + fn past_the_marks_a_group_holds_later_ones_arent_shared() { + let (a, b) = (samples(&combs(0..40_000)), samples(&combs(40_000..65_536))); + let late = combs((0..10).chain(65_536..66_000).collect::>().into_iter()); + let (rows, late) = (late.len() as u32, samples(&late)); + let (columns, a_rows, b_rows) = (120 * 36, 40_000u32.div_ceil(120) * 4, 25_536u32.div_ceil(120) * 4); + let images = [(&a[..], columns, a_rows), (&b[..], columns, b_rows), (&late[..], columns, rows), (&late[..], columns, rows)]; + let (globals, coded) = encode_shared(&images).unwrap(); + assert_eq!(exported(&globals), 10); + for c in &coded[2..] { + assert_eq!(decode_with(c.as_ref().unwrap(), Some(&globals)), unpack(&late, columns, rows)); + } + } + + /// Past what one dictionary shares, it keeps the marks the most images hold: 20,000 shared, + /// the first 10,000 in all three images and the rest in two. The third image then places all + /// its marks from it, and the other two code the rest themselves (in a dictionary of their own + /// or a generic region, whichever is smaller). + #[test] + fn past_the_shared_limit_the_most_widely_held_marks_are_shared() { + let (both, first) = (combs(0..20_000), combs(0..10_000)); + let rows = both.len(); + let first: Bitmap = first.iter().cloned().chain(std::iter::repeat_n(vec![0u8; 120 * 36], rows - first.len())).collect(); + let (globals, coded) = shared_round_trip(&[both.clone(), both, first]); + assert_eq!(exported(&globals), MAX_SHARED_SYMBOLS); + assert_eq!(segment_types(&coded[2]), [48, 7]); + assert!(segment_types(&coded[0]).iter().any(|t| *t == 0 || *t == 39), "{:?}", segment_types(&coded[0])); + } + + proptest::proptest! { + /// Glyphs scattered over pages of one font: whatever is shared, every page with a stream + /// decodes with the globals to its picture. + #[test] + fn shared_groups_round_trip( + columns in 1usize..200, + rows in 1usize..100, + shapes in 2usize..8, + n in 0usize..60, + pages in 2usize..5, + seed in proptest::num::u64::ANY, + ) { + let font = glyphs(shapes, seed); + let data: Vec> = (0..pages).map(|p| samples(&scatter(columns, rows, &font, seed ^ p as u64, n))).collect(); + let (c, r) = (columns as u32, rows as u32); + let images: Vec<(&[u8], u32, u32)> = data.iter().map(|d| (&d[..], c, r)).collect(); + if let Some((globals, coded)) = encode_shared(&images) { + for (d, coded) in data.iter().zip(coded) { + if let Some(coded) = coded { + proptest::prop_assert_eq!(decode_with(&coded, Some(&globals)), unpack(d, c, r)); + } + } + } + } + + /// Glyphs anywhere, over the edges and each other, with stray pixels: every plan decodes to + /// the picture, and `encode_jbig2` is never larger than the generic coding. + #[test] + fn scattered_marks_round_trip( + columns in 1usize..240, + rows in 1usize..120, + shapes in 2usize..10, + n in 0usize..80, + seed in proptest::num::u64::ANY, + stray in proptest::collection::vec((0usize..240, 0usize..120), 0..20), + ) { + let mut picture = scatter(columns, rows, &glyphs(shapes, seed), seed, n); + for (x, y) in stray { + if let Some(cell) = picture.get_mut(y).and_then(|r| r.get_mut(x)) { + *cell ^= 1; + } + } + every_plan_round_trips(&picture); + let data = samples(&picture); + let (c, r) = (columns as u32, rows as u32); + let coded = encode_jbig2(&data, c, r).unwrap(); + proptest::prop_assert!(coded.len() <= encode_jbig2_generic(&data, c, r).unwrap().len()); + proptest::prop_assert_eq!(decode(&coded), unpack(&data, c, r)); + } + + /// Whatever it is given, it returns. + #[test] + fn any_input_returns(data in proptest::collection::vec(0u8..=255, 0..600), columns in 0u32..5000, rows in 0u32..80) { + let _ = encode_jbig2(&data, columns, rows); + let _ = encode(&data, columns, rows); + let _ = encode_shared(&[(&data, columns, rows), (&data, columns, rows.saturating_sub(1))]); + } + } +} diff --git a/crates/stream-codecs/src/lib.rs b/crates/stream-codecs/src/lib.rs new file mode 100644 index 000000000..04a935333 --- /dev/null +++ b/crates/stream-codecs/src/lib.rs @@ -0,0 +1,117 @@ +//! Byte-stream and bi-level image codecs that PDF, PostScript and TIFF share. +//! +//! - [`lzw`]: variable-width LZW, 9 to 12 bits, most significant bit first (PDF `LZWDecode`, +//! TIFF compression 5), with or without the early code-width change. +//! - [`ascii85`], [`asciihex`], [`runlength`]: the PostScript and PDF text and run-length +//! encodings (`ASCII85Decode`, `ASCIIHexDecode`, `RunLengthDecode`; PackBits). +//! - [`predictor`]: TIFF horizontal differencing and the PNG row filters. +//! - [`ccitt`]: CCITT Group 3 and Group 4 fax decoding, bounded, and Group 4 encoding. +//! - [`jbig2`]: lossless JBIG2 encoding (generic regions, symbol dictionaries, a dictionary +//! shared by a group of images). Decoding is `hayro-jbig2`'s. +//! +//! The crate knows no container: a caller hands it bytes and parameters. A PDF `/Filter` chain +//! (order, limits across stages, Flate) is `craft_pdf::filters`, built on this crate. +//! +//! Every decoder is bounded by a caller-supplied output limit and never panics on malformed +//! input. A decoder that fails returns what it decoded before the damage ([`Failure`]), so a +//! caller can choose to keep it. CCITT decoding spends from an allowance the caller shares +//! across a run of images ([`pay`]); it does not decode without one: +//! +//! ```compile_fail +//! let _ = craft_stream_codecs::decode_ccitt(&[], &craft_stream_codecs::CcittParams::default(), 1); +//! ``` +//! +//! ``` +//! let mut work = 1 << 20; +//! let p = craft_stream_codecs::CcittParams { columns: 8, ..Default::default() }; +//! assert!(craft_stream_codecs::decode_ccitt_within(&[], &p, 1, &mut work).is_ok()); +//! ``` +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod ascii85; +pub mod asciihex; +pub mod ccitt; +pub mod jbig2; +pub mod lzw; +mod mq; +pub mod predictor; +pub mod runlength; + +pub use ccitt::{CcittParams, MAX_CCITT_COLUMNS, MAX_G4_COLUMNS, decode_ccitt_within, encode_g4}; +pub use jbig2::{MAX_JBIG2_COLUMNS, MAX_JBIG2_PIXELS, SharedJbig2, encode_jbig2, encode_jbig2_generic, encode_jbig2_shared}; + +/// Parameters of the predictors and of LZW, as a PDF `/DecodeParms` dictionary gives them +/// (ISO 32000-2 Tables 8–9); TIFF's `Predictor`, `SamplesPerPixel`, `BitsPerSample` and +/// `ImageWidth` map onto the same four. +#[derive(Clone, Debug, PartialEq)] +pub struct Params { + /// 1 = none, 2 = TIFF predictor 2, 10–15 = PNG predictors. + pub predictor: i64, + pub colors: i64, + pub bits_per_component: i64, + pub columns: i64, + /// LZW only: 1 (default) switches code width one code early, 0 does not. + pub early_change: i64, +} + +impl Default for Params { + fn default() -> Self { + Params { predictor: 1, colors: 1, bits_per_component: 8, columns: 1, early_change: 1 } + } +} + +/// Errors from decoding or encoding. +#[derive(Debug, thiserror::Error)] +pub enum CodecError { + /// `filter` names the codec, as PDF names its filter (`LZWDecode`, `CCITTFaxDecode`, …). + #[error("{filter}: corrupt data: {detail}")] + Corrupt { filter: &'static str, detail: String }, + #[error("decoded output exceeds the limit of {0} bytes")] + LimitExceeded(usize), + #[error("unsupported filter: {0}")] + Unsupported(String), + /// A run of images has nothing left to spend ([`pay`]). + #[error("more image decoding than one operation may do")] + Spent, +} + +/// A decoder failure carrying whatever was decoded before the error. +pub struct Failure { + pub error: CodecError, + pub partial: Vec, + /// Bytes decoded on the way that `partial` doesn't hold: they took as long. + pub discarded: usize, +} + +impl Failure { + pub fn corrupt(filter: &'static str, detail: impl Into, partial: Vec) -> Self { + Failure { error: CodecError::Corrupt { filter, detail: detail.into() }, partial, discarded: 0 } + } + + pub fn limit(max: usize) -> Self { + Failure { error: CodecError::LimitExceeded(max), partial: Vec::new(), discarded: 0 } + } +} + +/// What a byte-stream decoder returns: the decoded bytes, or the failure with its partial output. +pub type Step = Result, Failure>; + +/// Takes `steps` from `work`, what a run of images may still cost. What it can't pay for spends +/// all of it. +pub fn pay(work: &mut u64, steps: u64) -> Result<(), CodecError> { + match work.checked_sub(steps) { + Some(left) => { + *work = left; + Ok(()) + } + None => { + *work = 0; + Err(CodecError::Spent) + } + } +} + +/// PDF and PostScript white-space characters (ISO 32000-2 Table 1). +pub fn is_pdf_whitespace(b: u8) -> bool { + matches!(b, 0 | 9 | 10 | 12 | 13 | 32) +} diff --git a/crates/stream-codecs/src/lzw.rs b/crates/stream-codecs/src/lzw.rs new file mode 100644 index 000000000..e16d78288 --- /dev/null +++ b/crates/stream-codecs/src/lzw.rs @@ -0,0 +1,314 @@ +//! LZWDecode (ISO 32000-2 §7.4.4.2): variable-width codes of 9–12 bits, MSB first, +//! 256 = clear-table, 257 = EOD, first free code 258. With `EarlyChange` 1 (the +//! default) the code width grows one code early. +//! +//! Tolerated: data ending without EOD, a missing initial clear-table code, a table that +//! fills up without a clear-table code (entries are then no longer added). + +use std::collections::HashMap; + +use crate::{Failure, Step}; + +const NAME: &str = "LZWDecode"; +const CLEAR: u16 = 256; +const EOD: u16 = 257; +const FIRST: usize = 258; +const TABLE: usize = 4096; +/// The encoder emits clear-table when its next free code reaches this value. +const ENCODER_RESET_AT: usize = TABLE; + +/// The code width a decoder uses when its next free code is `next`. +fn width(next: usize, early: bool) -> u32 { + match next + usize::from(early) { + 0..512 => 9, + 512..1024 => 10, + 1024..2048 => 11, + _ => 12, + } +} + +struct BitReader<'a> { + data: &'a [u8], + pos: usize, + acc: u32, + bits: u32, +} + +impl BitReader<'_> { + fn read(&mut self, n: u32) -> Option { + while self.bits < n { + let &b = self.data.get(self.pos)?; + self.pos += 1; + self.acc = (self.acc << 8) | u32::from(b); + self.bits += 8; + } + self.bits -= n; + let v = (self.acc >> self.bits) & ((1 << n) - 1); + self.acc &= (1 << self.bits) - 1; + Some(v as u16) + } +} + +pub fn decode(data: &[u8], early: bool, max: usize) -> Step { + // Table entry = (prefix code, last byte, first byte, length). + let mut prefix = vec![0u16; TABLE]; + let mut last = vec![0u8; TABLE]; + let mut first = vec![0u8; TABLE]; + let mut len = vec![0u16; TABLE]; + for i in 0..256 { + last[i] = i as u8; + first[i] = i as u8; + len[i] = 1; + } + let mut next = FIRST; + let mut w = 9; + let mut prev: Option = None; + let mut r = BitReader { data, pos: 0, acc: 0, bits: 0 }; + let mut out = Vec::with_capacity(data.len().saturating_mul(2).min(max)); + + while let Some(code) = r.read(w) { + match code { + CLEAR => { + next = FIRST; + w = 9; + prev = None; + continue; + } + EOD => break, + _ => {} + } + let code = usize::from(code); + if let Some(p) = prev { + let head = if code < next { + first[code] + } else if code == next && next < TABLE { + first[p] + } else { + return Err(Failure::corrupt(NAME, format!("code {code} not in table (next {next})"), out)); + }; + if next < TABLE { + prefix[next] = p as u16; + last[next] = head; + first[next] = first[p]; + len[next] = len[p] + 1; + next += 1; + } + } else if code >= 256 { + return Err(Failure::corrupt(NAME, format!("code {code} before any table entry"), out)); + } + // Emit the string for `code`, walking the prefix chain backwards. + let n = usize::from(len[code]); + if out.len() + n > max { + return Err(Failure::limit(max)); + } + let start = out.len(); + out.resize(start + n, 0); + let mut c = code; + for slot in out[start..].iter_mut().rev() { + *slot = last[c]; + c = usize::from(prefix[c]); + } + prev = Some(code); + w = width(next, early); + } + Ok(out) +} + +struct BitWriter { + out: Vec, + acc: u32, + bits: u32, +} + +impl BitWriter { + fn write(&mut self, code: u16, n: u32) { + self.acc = (self.acc << n) | u32::from(code); + self.bits += n; + while self.bits >= 8 { + self.bits -= 8; + self.out.push((self.acc >> self.bits) as u8); + } + self.acc &= (1 << self.bits) - 1; + } + + fn finish(mut self) -> Vec { + if self.bits > 0 { + self.out.push((self.acc << (8 - self.bits)) as u8); + } + self.out + } +} + +/// Greedy LZW with a leading clear-table code and a trailing EOD. +pub fn encode(data: &[u8], early: bool) -> Vec { + let mut wr = BitWriter { out: Vec::with_capacity(data.len() / 2 + 8), acc: 0, bits: 0 }; + let mut dict: HashMap<(u16, u8), u16> = HashMap::new(); + // `next` is the encoder's next free code; `dnext` mirrors the decoder's, which lags + // one entry behind (the decoder adds an entry only once it sees the following code). + let mut next = FIRST; + let mut dnext = FIRST; + let mut codes_since_clear = 0usize; + wr.write(CLEAR, 9); + let emit = |wr: &mut BitWriter, code: u16, dnext: &mut usize, since: &mut usize| { + wr.write(code, width(*dnext, early)); + if *since > 0 && *dnext < TABLE { + *dnext += 1; + } + *since += 1; + }; + let Some((&b0, rest)) = data.split_first() else { + wr.write(EOD, 9); + return wr.finish(); + }; + let mut cur = u16::from(b0); + for &b in rest { + if let Some(&c) = dict.get(&(cur, b)) { + cur = c; + continue; + } + emit(&mut wr, cur, &mut dnext, &mut codes_since_clear); + dict.insert((cur, b), next as u16); + next += 1; + if next == ENCODER_RESET_AT { + wr.write(CLEAR, width(dnext, early)); + dict.clear(); + next = FIRST; + dnext = FIRST; + codes_since_clear = 0; + } + cur = u16::from(b); + } + emit(&mut wr, cur, &mut dnext, &mut codes_since_clear); + wr.write(EOD, width(dnext, early)); + wr.finish() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::CodecError; + + fn dec(d: &[u8], early: bool) -> Result, CodecError> { + decode(d, early, 1 << 26).map_err(|f| f.error) + } + + /// ISO 32000-2 §7.4.4.2 example: codes 256 45 258 258 65 259 66 257. + const SPEC_IN: [u8; 10] = [45, 45, 45, 45, 45, 65, 45, 45, 45, 66]; + const SPEC_OUT: [u8; 9] = [0x80, 0x0B, 0x60, 0x50, 0x22, 0x0C, 0x0C, 0x85, 0x01]; + + #[test] + fn spec_example() { + assert_eq!(dec(&SPEC_OUT, true).unwrap(), SPEC_IN); + assert_eq!(encode(&SPEC_IN, true), SPEC_OUT); + // Below 511 codes the early-change setting makes no difference. + assert_eq!(dec(&SPEC_OUT, false).unwrap(), SPEC_IN); + } + + #[test] + fn missing_eod_and_clear() { + // Drop the EOD (last 9 bits span the final 2 bytes; keep bits for 66 only). + let mut w = BitWriter { out: vec![], acc: 0, bits: 0 }; + for c in [45u16, 258, 258, 65, 259, 66] { + w.write(c, 9); + } + assert_eq!(dec(&w.finish(), true).unwrap(), SPEC_IN); + } + + #[test] + fn invalid_codes() { + let mut w = BitWriter { out: vec![], acc: 0, bits: 0 }; + for c in [256u16, 65, 300] { + w.write(c, 9); + } + let f = decode(&w.finish(), true, 1000).err().unwrap(); + assert!(matches!(f.error, CodecError::Corrupt { .. })); + assert_eq!(f.partial, b"A"); + let mut w = BitWriter { out: vec![], acc: 0, bits: 0 }; + w.write(258, 9); + assert!(matches!(dec(&w.finish(), true), Err(CodecError::Corrupt { .. }))); + } + + #[test] + fn widths() { + assert_eq!((width(510, true), width(511, true), width(511, false), width(512, false)), (9, 10, 9, 10)); + assert_eq!((width(1022, true), width(1023, true), width(2047, true), width(4095, false)), (10, 11, 12, 12)); + } + + fn noisy(n: usize, seed: u32) -> Vec { + let mut s = seed; + (0..n) + .map(|_| { + s = s.wrapping_mul(1_103_515_245).wrapping_add(12_345); + ((s >> 16) % 7) as u8 * 31 + }) + .collect() + } + + #[test] + fn all_widths_and_clears_round_trip() { + for early in [false, true] { + for data in [noisy(200_000, 1), (0..=255u8).cycle().take(70_000).collect(), vec![9u8; 100_000]] { + let e = encode(&data, early); + assert_eq!(dec(&e, early).unwrap(), data); + } + } + } + + #[test] + fn cross_check_with_weezl() { + use weezl::BitOrder::Msb; + for data in [noisy(150_000, 7), SPEC_IN.to_vec(), vec![]] { + // early change 1 is the TIFF size switch; 0 is the plain variant. + let tiff = weezl::encode::Encoder::with_tiff_size_switch(Msb, 8).encode(&data).unwrap(); + assert_eq!(dec(&tiff, true).unwrap(), data); + let plain = weezl::encode::Encoder::new(Msb, 8).encode(&data).unwrap(); + assert_eq!(dec(&plain, false).unwrap(), data); + let ours = encode(&data, true); + assert_eq!(weezl::decode::Decoder::with_tiff_size_switch(Msb, 8).decode(&ours).unwrap(), data); + let ours = encode(&data, false); + assert_eq!(weezl::decode::Decoder::new(Msb, 8).decode(&ours).unwrap(), data); + } + } + + #[test] + fn full_table_without_clear() { + // Hand-roll a stream that fills the table and keeps going at 12 bits. + let data = noisy(40_000, 3); + let mut w = BitWriter { out: vec![], acc: 0, bits: 0 }; + let mut dict: HashMap, u16> = (0..=255u8).map(|b| (vec![b], u16::from(b))).collect(); + let (mut next, mut dnext, mut since) = (FIRST, FIRST, 0usize); + let mut cur: Vec = vec![]; + let mut put = |w: &mut BitWriter, code: u16, dnext: &mut usize| { + w.write(code, width(*dnext, true)); + if since > 0 && *dnext < TABLE { + *dnext += 1; + } + since += 1; + }; + w.write(CLEAR, 9); + for &b in &data { + let mut ext = cur.clone(); + ext.push(b); + if dict.contains_key(&ext) { + cur = ext; + continue; + } + put(&mut w, dict[&cur], &mut dnext); + if next < TABLE { + dict.insert(ext, next as u16); + next += 1; + } + cur = vec![b]; + } + put(&mut w, dict[&cur], &mut dnext); + w.write(EOD, width(dnext, true)); + assert_eq!(dec(&w.finish(), true).unwrap(), data); + } + + #[test] + fn limit() { + let e = encode(&vec![0u8; 100_000], true); + assert!(matches!(decode(&e, true, 50_000).map_err(|f| f.error), Err(CodecError::LimitExceeded(50_000)))); + assert_eq!(decode(&e, true, 100_000).ok().unwrap().len(), 100_000); + } +} diff --git a/crates/stream-codecs/src/mq.rs b/crates/stream-codecs/src/mq.rs new file mode 100644 index 000000000..04dda2bd9 --- /dev/null +++ b/crates/stream-codecs/src/mq.rs @@ -0,0 +1,209 @@ +//! The MQ arithmetic encoder of JBIG2 (ITU-T T.88 | ISO/IEC 14492, Annex E), written from the +//! procedures in its Figures E.2–E.12 and the probability estimates of Table E.1. + +/// Table E.1: for each index, the LPS probability estimate Qe, the next index after an MPS and +/// after an LPS renormalisation, and whether an LPS switches the MPS sense. +const STATES: [(u32, u8, u8, bool); 47] = [ + (0x5601, 1, 1, true), + (0x3401, 2, 6, false), + (0x1801, 3, 9, false), + (0x0AC1, 4, 12, false), + (0x0521, 5, 29, false), + (0x0221, 38, 33, false), + (0x5601, 7, 6, true), + (0x5401, 8, 14, false), + (0x4801, 9, 14, false), + (0x3801, 10, 14, false), + (0x3001, 11, 17, false), + (0x2401, 12, 18, false), + (0x1C01, 13, 20, false), + (0x1601, 29, 21, false), + (0x5601, 15, 14, true), + (0x5401, 16, 14, false), + (0x5101, 17, 15, false), + (0x4801, 18, 16, false), + (0x3801, 19, 17, false), + (0x3401, 20, 18, false), + (0x3001, 21, 19, false), + (0x2801, 22, 19, false), + (0x2401, 23, 20, false), + (0x2201, 24, 21, false), + (0x1C01, 25, 22, false), + (0x1801, 26, 23, false), + (0x1601, 27, 24, false), + (0x1401, 28, 25, false), + (0x1201, 29, 26, false), + (0x1101, 30, 27, false), + (0x0AC1, 31, 28, false), + (0x09C1, 32, 29, false), + (0x08A1, 33, 30, false), + (0x0521, 34, 31, false), + (0x0441, 35, 32, false), + (0x02A1, 36, 33, false), + (0x0221, 37, 34, false), + (0x0141, 38, 35, false), + (0x0111, 39, 36, false), + (0x0085, 40, 37, false), + (0x0049, 41, 38, false), + (0x0025, 42, 39, false), + (0x0015, 43, 40, false), + (0x0009, 44, 41, false), + (0x0005, 45, 42, false), + (0x0001, 45, 43, false), + (0x5601, 46, 46, false), +]; + +#[cfg(test)] +thread_local! { + /// Decisions coded on this thread. + pub(crate) static DECISIONS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// A context's adaptive state: the index into [`STATES`] (low 7 bits) and the MPS sense (bit 7). +/// 0, the initial value, is index 0 with MPS 0 (E.2.8). +pub(crate) type Context = u8; + +/// The encoder's registers (E.2.1) and the bytes written so far. +pub(crate) struct MqEncoder { + a: u32, + c: u32, + ct: u32, + /// The output, after the byte before the first one (B at INITENC), which is 0x00 and is + /// dropped by [`MqEncoder::finish`]: a carry can't reach it before the first byte is out. + out: Vec, +} + +impl MqEncoder { + /// INITENC (Figure E.10). The byte before the first is 0x00, not 0xFF, so CT starts at 12. + pub(crate) fn new() -> Self { + MqEncoder { a: 0x8000, c: 0, ct: 12, out: vec![0] } + } + + /// The byte B, the last one written (or the one before the first). + fn b(&self) -> u8 { + self.out.last().copied().unwrap_or(0) + } + + /// ENCODE (Figures E.3–E.7): code decision `d` in context `cx`, updating its state. + pub(crate) fn encode(&mut self, cx: &mut Context, d: bool) { + #[cfg(test)] + DECISIONS.with(|n| n.set(n.get() + 1)); + let (index, mps) = (usize::from(*cx & 0x7F), *cx >> 7 == 1); + let Some(&(qe, nmps, nlps, switch)) = STATES.get(index) else { return }; + self.a -= qe; + if d == mps { + // CODEMPS. + if self.a & 0x8000 != 0 { + self.c += qe; + return; + } + if self.a < qe { + self.a = qe; + } else { + self.c += qe; + } + *cx = nmps | (*cx & 0x80); + } else { + // CODELPS. + if self.a < qe { + self.c += qe; + } else { + self.a = qe; + } + let mps = mps != switch; + *cx = nlps | (u8::from(mps) << 7); + } + self.renorm(); + } + + /// RENORME (Figure E.8). + fn renorm(&mut self) { + loop { + self.a <<= 1; + self.c <<= 1; + self.ct -= 1; + if self.ct == 0 { + self.byte_out(); + } + if self.a & 0x8000 != 0 { + break; + } + } + } + + /// BYTEOUT (Figure E.9), with bit stuffing after 0xFF and carry into the byte before. + fn byte_out(&mut self) { + if self.b() == 0xFF { + self.stuffed(); + } else if self.c < 0x800_0000 { + self.plain(); + } else { + let b = self.b().wrapping_add(1); + if let Some(last) = self.out.last_mut() { + *last = b; + } + if b == 0xFF { + self.c &= 0x7FF_FFFF; + self.stuffed(); + } else { + self.plain(); + } + } + } + + fn plain(&mut self) { + self.out.push(((self.c >> 19) & 0xFF) as u8); + self.c &= 0x7_FFFF; + self.ct = 8; + } + + fn stuffed(&mut self) { + self.out.push(((self.c >> 20) & 0xFF) as u8); + self.c &= 0xF_FFFF; + self.ct = 7; + } + + /// FLUSH (Figures E.11 and E.12): the final bytes, then the 0xFF 0xAC marker. + pub(crate) fn finish(mut self) -> Vec { + let top = self.c + self.a; + self.c |= 0xFFFF; + if self.c >= top { + self.c -= 0x8000; + } + self.c <<= self.ct; + self.byte_out(); + self.c <<= self.ct; + self.byte_out(); + if self.b() != 0xFF { + self.out.push(0xFF); + } + self.out.push(0xAC); + self.out.split_off(1) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Annex H.2's test sequence: 256 decisions in one context give these 30 bytes. + #[test] + fn the_annex_h_test_sequence_encodes_to_its_bytes() { + let decisions = [ + 0x00, 0x02, 0x00, 0x51, 0x00, 0x00, 0x00, 0xC0, 0x03, 0x52, 0x87, 0x2A, 0xAA, 0xAA, 0xAA, 0xAA, 0x82, 0xC0, 0x20, 0x00, 0xFC, 0xD7, 0x9E, + 0xF6, 0xBF, 0x7F, 0xED, 0x90, 0x4F, 0x46, 0xA3, 0xBF, + ]; + let mut mq = MqEncoder::new(); + let mut cx = 0; + for byte in decisions { + for bit in (0..8).rev() { + mq.encode(&mut cx, byte >> bit & 1 == 1); + } + } + let expected = [ + 0x84, 0xC7, 0x3B, 0xFC, 0xE1, 0xA1, 0x43, 0x04, 0x02, 0x20, 0x00, 0x00, 0x41, 0x0D, 0xBB, 0x86, 0xF4, 0x31, 0x7F, 0xFF, 0x88, 0xFF, 0x37, + 0x47, 0x1A, 0xDB, 0x6A, 0xDF, 0xFF, 0xAC, + ]; + assert_eq!(mq.finish(), expected); + } +} diff --git a/crates/stream-codecs/src/predictor.rs b/crates/stream-codecs/src/predictor.rs new file mode 100644 index 000000000..8e76e3fc2 --- /dev/null +++ b/crates/stream-codecs/src/predictor.rs @@ -0,0 +1,356 @@ +//! Predictor functions for FlateDecode and LZWDecode (ISO 32000-2 §7.4.4.4). +//! +//! - Predictor 2: TIFF 6.0 horizontal differencing (Section 14), per component, for +//! 1, 2, 4, 8 and 16 bits per component. +//! - Predictors 10–15: PNG filters (RFC 2083 §6): each row carries its own filter-type +//! byte (0 None, 1 Sub, 2 Up, 3 Average, 4 Paeth), whatever the predictor value is. +//! +//! A final partial row is decoded as far as its data goes. +//! +//! The samples of an image whose rows carry no filter-type byte, although a PNG predictor is +//! declared, are taken as they are and reported as damage ([`decode`]). + +use crate::{CodecError, Failure, Params, Step}; + +struct Layout { + bpc: usize, + colors: usize, + /// Bytes per complete pixel, at least 1 (the PNG `bpp`). + bpp: usize, + /// Bytes per row (without the PNG tag byte). + row: usize, + /// Samples per row (colors × columns). + samples: usize, +} + +fn layout(p: &Params) -> Result { + let bpc = match p.bits_per_component { + b @ (1 | 2 | 4 | 8 | 16) => b as usize, + b => return Err(format!("invalid BitsPerComponent {b}")), + }; + if !(1..=255).contains(&p.colors) { + return Err(format!("invalid Colors {}", p.colors)); + } + if p.columns < 1 { + return Err(format!("invalid Columns {}", p.columns)); + } + let colors = p.colors as usize; + let samples = usize::try_from(p.columns).ok().and_then(|c| c.checked_mul(colors)).ok_or_else(|| format!("Columns {} too large", p.columns))?; + let bits = samples.checked_mul(bpc).ok_or_else(|| format!("Columns {} too large", p.columns))?; + Ok(Layout { bpc, colors, bpp: (colors * bpc).div_ceil(8), row: bits.div_ceil(8), samples }) +} + +/// Undoes the predictor named by `p.predictor` (1 = identity). `rows` is the height of the image +/// `data` is the samples of, if it is: with a PNG predictor, data of exactly `rows` rows without +/// their filter-type bytes ([`untagged`]) is given back as it is, as a [`Failure`] whose partial +/// output is all of it, so that tolerant decoding takes the samples and reports the repair, and +/// strict decoding refuses them. +pub fn decode(p: &Params, data: Vec, name: &'static str, rows: Option) -> Step { + match p.predictor { + 1 => Ok(data), + 2 | 10..=15 => { + let l = layout(p).map_err(|e| Failure::corrupt(name, e, Vec::new()))?; + if p.predictor == 2 { + return Ok(tiff_decode(&l, data)); + } + if let Some(rows) = rows.filter(|&rows| untagged(&l, data.len(), rows)) { + let detail = + format!("PNG predictor {} declared, but its {rows} rows carry no filter-type bytes: read as unpredicted samples", p.predictor); + return Err(Failure::corrupt(name, detail, data)); + } + png_decode(&l, &data, name) + } + other => Err(Failure::corrupt(name, format!("invalid Predictor {other}"), Vec::new())), + } +} + +/// Whether `len` bytes are exactly `rows` rows of `l` without the filter-type byte that starts each +/// PNG-predicted row (ISO 32000-2 §7.4.4.4), where predicted data would be `rows` × (row + 1): a +/// writer that declared the predictor and didn't apply it. Read with the predictor, every row's +/// first sample was taken for a filter type and the image came out a row short and shifted. +/// PDFium, MuPDF and Poppler read such data as the samples it is. +fn untagged(l: &Layout, len: usize, rows: usize) -> bool { + rows > 0 && l.row > 0 && rows.checked_mul(l.row) == Some(len) +} + +/// Applies the predictor named by `p.predictor` (1 = identity; 15 = per-row optimum). +pub fn encode(p: &Params, data: &[u8]) -> Result, CodecError> { + match p.predictor { + 1 => Ok(data.to_vec()), + 2 | 10..=15 => { + let l = layout(p).map_err(CodecError::Unsupported)?; + Ok(if p.predictor == 2 { tiff_encode(&l, data) } else { png_encode(&l, data, p.predictor) }) + } + other => Err(CodecError::Unsupported(format!("Predictor {other}"))), + } +} + +// ---- TIFF predictor 2 ---- + +fn get(row: &[u8], i: usize, bpc: usize) -> u16 { + match bpc { + 16 => u16::from_be_bytes([row[2 * i], row[2 * i + 1]]), + 8 => u16::from(row[i]), + _ => { + let bit = i * bpc; + let shift = 8 - bpc - bit % 8; + u16::from(row[bit / 8] >> shift) & ((1 << bpc) - 1) + } + } +} + +fn set(row: &mut [u8], i: usize, bpc: usize, v: u16) { + match bpc { + 16 => row[2 * i..2 * i + 2].copy_from_slice(&v.to_be_bytes()), + 8 => row[i] = v as u8, + _ => { + let bit = i * bpc; + let shift = 8 - bpc - bit % 8; + let mask = (((1u16 << bpc) - 1) << shift) as u8; + row[bit / 8] = (row[bit / 8] & !mask) | (((v << shift) as u8) & mask); + } + } +} + +/// Complete samples available in a (possibly partial) row. +fn samples_in(l: &Layout, row_len: usize) -> usize { + (row_len * 8 / l.bpc).min(l.samples) +} + +fn tiff_decode(l: &Layout, mut data: Vec) -> Vec { + let mask = if l.bpc == 16 { u16::MAX } else { (1u16 << l.bpc) - 1 }; + for row in data.chunks_mut(l.row) { + for i in l.colors..samples_in(l, row.len()) { + let v = get(row, i, l.bpc).wrapping_add(get(row, i - l.colors, l.bpc)) & mask; + set(row, i, l.bpc, v); + } + } + data +} + +fn tiff_encode(l: &Layout, data: &[u8]) -> Vec { + let mask = if l.bpc == 16 { u16::MAX } else { (1u16 << l.bpc) - 1 }; + let mut out = data.to_vec(); + for row in out.chunks_mut(l.row) { + // Descending, so the left neighbour still holds its original value. + for i in (l.colors..samples_in(l, row.len())).rev() { + let v = get(row, i, l.bpc).wrapping_sub(get(row, i - l.colors, l.bpc)) & mask; + set(row, i, l.bpc, v); + } + } + out +} + +// ---- PNG predictors ---- + +fn paeth(a: u8, b: u8, c: u8) -> u8 { + let p = i16::from(a) + i16::from(b) - i16::from(c); + let (pa, pb, pc) = ((p - i16::from(a)).abs(), (p - i16::from(b)).abs(), (p - i16::from(c)).abs()); + if pa <= pb && pa <= pc { + a + } else if pb <= pc { + b + } else { + c + } +} + +/// The PNG predictor value for byte `i` given the reconstructed current row `cur` +/// (bytes before `i`) and the prior row `up`. +fn predict(tag: u8, cur: &[u8], up: &[u8], i: usize, bpp: usize) -> u8 { + let a = if i >= bpp { cur[i - bpp] } else { 0 }; + let b = up[i]; + let c = if i >= bpp { up[i - bpp] } else { 0 }; + match tag { + 1 => a, + 2 => b, + 3 => ((u16::from(a) + u16::from(b)) / 2) as u8, + 4 => paeth(a, b, c), + _ => 0, + } +} + +fn png_decode(l: &Layout, data: &[u8], name: &'static str) -> Step { + let mut out = Vec::with_capacity(data.len()); + // The prior row: never longer than the input, even for absurd Columns values. + let mut up = vec![0u8; l.row.min(data.len())]; + for chunk in data.chunks(l.row.saturating_add(1)) { + let (tag, src) = (chunk[0], &chunk[1..]); + if tag > 4 { + return Err(Failure::corrupt(name, format!("invalid PNG filter type {tag}"), out)); + } + let start = out.len(); + for (i, &x) in src.iter().enumerate() { + let pred = predict(tag, &out[start..], &up, i, l.bpp); + out.push(x.wrapping_add(pred)); + } + up[..src.len()].copy_from_slice(&out[start..]); + } + Ok(out) +} + +fn png_filter(tag: u8, cur: &[u8], up: &[u8], bpp: usize, dst: &mut Vec) { + dst.clear(); + dst.push(tag); + dst.extend(cur.iter().enumerate().map(|(i, &x)| x.wrapping_sub(predict(tag, cur, up, i, bpp)))); +} + +/// Sum of residuals as signed bytes: the usual heuristic for choosing a PNG filter. +fn cost(filtered: &[u8]) -> u64 { + filtered[1..].iter().map(|&b| u64::from((b as i8).unsigned_abs())).sum() +} + +fn png_encode(l: &Layout, data: &[u8], predictor: i64) -> Vec { + let rows = data.len().div_ceil(l.row.max(1)); + let mut out = Vec::with_capacity(data.len() + rows); + let zeros = vec![0u8; l.row.min(data.len())]; + let (mut best, mut trial) = (Vec::new(), Vec::new()); + let mut up: &[u8] = &zeros; + for cur in data.chunks(l.row) { + let up_row = &up[..cur.len()]; + if predictor == 15 { + png_filter(0, cur, up_row, l.bpp, &mut best); + let mut best_cost = cost(&best); + for tag in 1..=4 { + png_filter(tag, cur, up_row, l.bpp, &mut trial); + let c = cost(&trial); + if c < best_cost { + best_cost = c; + std::mem::swap(&mut best, &mut trial); + } + } + } else { + png_filter((predictor - 10) as u8, cur, up_row, l.bpp, &mut best); + } + out.extend_from_slice(&best); + up = cur; + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn p(predictor: i64, colors: i64, bpc: i64, columns: i64) -> Params { + Params { predictor, colors, bits_per_component: bpc, columns, early_change: 1 } + } + + fn dec(params: &Params, data: &[u8]) -> Result, CodecError> { + decode(params, data.to_vec(), "FlateDecode", None).map_err(|f| f.error) + } + + #[test] + fn png_hand_vectors() { + // 3 columns, 1 color, 8 bpc. Rows: None, Sub, Up, Average, Paeth. + let enc = [ + 0, 10, 20, 30, // None -> 10 20 30 + 1, 5, 1, 1, // Sub -> 5 6 7 + 2, 1, 1, 1, // Up -> 6 7 8 + 3, 10, 10, 10, // Avg -> 10+3=13, 10+(13+7)/2=20, 10+(20+8)/2=24 + 4, 1, 1, + 1, // Paeth: a=0,b=13,c=0 -> p=13 -> b: 14; a=14,b=20,c=13: p=21 -> pa=7,pb=1,pc=8 -> b: 21; + // a=21,b=24,c=20: p=25 -> pa=4,pb=1,pc=5 -> b: 25 + ]; + let out = dec(&p(15, 1, 8, 3), &enc).unwrap(); + assert_eq!(out, [10, 20, 30, 5, 6, 7, 6, 7, 8, 13, 20, 24, 14, 21, 25]); + } + + #[test] + fn paeth_tie_breaks() { + // RFC 2083 order: a, then b, then c. + assert_eq!(paeth(1, 1, 1), 1); + assert_eq!(paeth(10, 20, 15), 15); // p=15: pa=5 pb=5 pc=0 -> c + assert_eq!(paeth(20, 10, 10), 20); // p=20: pa=0 -> a + assert_eq!(paeth(5, 9, 7), 7); // p=7: pa=2 pb=2 pc=0 -> c + } + + #[test] + fn png_multibyte_pixels() { + // colors=3 bpc=16 -> bpp=6; 1 column; Sub on the first pixel uses a=0 throughout. + let enc = [1, 1, 2, 3, 4, 5, 6, 2, 1, 1, 1, 1, 1, 1]; + let out = dec(&p(11, 3, 16, 1), &enc).unwrap(); + assert_eq!(out, [1, 2, 3, 4, 5, 6, 2, 3, 4, 5, 6, 7]); + // colors=3 bpc=8, 2 columns: Sub looks 3 bytes back. + let enc = [1, 1, 2, 3, 1, 1, 1]; + assert_eq!(dec(&p(11, 3, 8, 2), &enc).unwrap(), [1, 2, 3, 2, 3, 4]); + } + + #[test] + fn png_partial_final_row() { + let enc = [0, 1, 2, 3, 4, 2, 1, 1]; + assert_eq!(dec(&p(12, 1, 8, 4), &enc).unwrap(), [1, 2, 3, 4, 2, 3]); + // A lone tag byte at the end yields nothing. + assert_eq!(dec(&p(12, 1, 8, 4), &[0, 1, 2, 3, 4, 2]).unwrap(), [1, 2, 3, 4]); + } + + #[test] + fn png_invalid_tag() { + let r = decode(&p(10, 1, 8, 2), vec![0, 1, 2, 5, 1, 1], "FlateDecode", None); + let f = r.err().unwrap(); + assert!(matches!(f.error, CodecError::Corrupt { .. })); + assert_eq!(f.partial, [1, 2]); + } + + #[test] + fn tiff_8bit() { + // 2 colors, 3 columns: differences per component. + let enc = [10, 20, 1, 2, 3, 4, /* row 2 */ 5, 5, 250, 10, 1, 1]; + assert_eq!(dec(&p(2, 2, 8, 3), &enc).unwrap(), [10, 20, 11, 22, 14, 26, 5, 5, 255, 15, 0, 16]); + } + + #[test] + fn tiff_16bit() { + let enc = [0x01, 0x00, 0xff, 0xff, 0x00, 0x02]; + assert_eq!(dec(&p(2, 1, 16, 3), &enc).unwrap(), [0x01, 0x00, 0x00, 0xff, 0x01, 0x01]); + } + + #[test] + fn tiff_sub_byte() { + // 1 bpc, 1 color, 10 columns: bits 1 0 0 1 1 0 0 0 | 1 1 (pad 000000) + // Cumulative XOR: 1 1 1 0 1 1 1 1 | 0 1 + let enc = [0b1001_1000, 0b1100_0000]; + assert_eq!(dec(&p(2, 1, 1, 10), &enc).unwrap(), [0b1110_1111, 0b0100_0000]); + // 4 bpc, 1 color, 3 columns: samples 5, 15, 3 -> 5, 4, 7 (mod 16). + assert_eq!(dec(&p(2, 1, 4, 3), &[0x5f, 0x30]).unwrap(), [0x54, 0x70]); + // 2 bpc, 2 colors, 2 columns: samples 1 2 3 3 -> 1 2 0 1. + assert_eq!(dec(&p(2, 2, 2, 2), &[0b0110_1111]).unwrap(), [0b0110_0001]); + // Padding bits are left alone. + assert_eq!(dec(&p(2, 1, 4, 1), &[0x5f]).unwrap(), [0x5f]); + } + + #[test] + fn tiff_partial_rows() { + // 16-bit with an odd trailing byte: the lone byte is left as is. + assert_eq!(dec(&p(2, 1, 16, 4), &[0, 1, 0, 1, 9]).unwrap(), [0, 1, 0, 2, 9]); + assert_eq!(dec(&p(2, 1, 8, 4), &[1, 1, 1, 1, 2, 2]).unwrap(), [1, 2, 3, 4, 2, 4]); + } + + #[test] + fn invalid_params() { + for bad in [p(2, 1, 3, 1), p(12, 0, 8, 1), p(12, 1, 8, 0), p(3, 1, 8, 1), p(12, 1, 8, i64::MAX), p(2, 300, 8, 1)] { + assert!(matches!(dec(&bad, &[0, 1, 2]), Err(CodecError::Corrupt { .. })), "{bad:?}"); + assert!(encode(&bad, &[1, 2]).is_err()); + } + // Huge but representable Columns does not allocate a huge row. + assert_eq!(dec(&p(12, 1, 8, 1 << 40), &[2, 7, 8]).unwrap(), [7, 8]); + } + + #[test] + fn encode_choices() { + let data = [1u8, 2, 3, 4, 5, 6]; + let up = encode(&p(12, 1, 8, 3), &data).unwrap(); + assert_eq!(up, [2, 1, 2, 3, 2, 3, 3, 3]); + let paeth = encode(&p(14, 1, 8, 3), &data).unwrap(); + assert_eq!(paeth[0], 4); + assert_eq!(paeth[4], 4); + // Per-row optimum picks Sub for a ramp. + let ramp: Vec = (0..64).collect(); + let opt = encode(&p(15, 1, 8, 64), &ramp).unwrap(); + assert_eq!(opt[0], 1); + for pred in [10, 11, 12, 13, 14, 15] { + let e = encode(&p(pred, 1, 8, 3), &data).unwrap(); + assert_eq!(dec(&p(pred, 1, 8, 3), &e).unwrap(), data); + } + } +} diff --git a/crates/stream-codecs/src/runlength.rs b/crates/stream-codecs/src/runlength.rs new file mode 100644 index 000000000..a6df9e19d --- /dev/null +++ b/crates/stream-codecs/src/runlength.rs @@ -0,0 +1,117 @@ +//! RunLengthDecode (ISO 32000-2 §7.4.5): a length byte L of 0–127 copies the next +//! L + 1 bytes, 129–255 repeats the next byte 257 − L times, 128 is EOD. +//! A missing EOD is tolerated; a truncated run is corrupt. + +use crate::{Failure, Step}; + +const NAME: &str = "RunLengthDecode"; + +pub fn decode(data: &[u8], max: usize) -> Step { + let mut out = Vec::with_capacity(data.len().saturating_mul(2).min(max)); + let mut i = 0; + while let Some(&l) = data.get(i) { + i += 1; + match l { + 128 => break, + 0..=127 => { + let n = usize::from(l) + 1; + let avail = n.min(data.len() - i); + if out.len() + avail > max { + return Err(Failure::limit(max)); + } + out.extend_from_slice(&data[i..i + avail]); + i += avail; + if avail < n { + return Err(Failure::corrupt(NAME, "truncated literal run", out)); + } + } + _ => { + let Some(&b) = data.get(i) else { + return Err(Failure::corrupt(NAME, "truncated repeat run", out)); + }; + i += 1; + let n = 257 - usize::from(l); + if out.len() + n > max { + return Err(Failure::limit(max)); + } + out.resize(out.len() + n, b); + } + } + } + Ok(out) +} + +pub fn encode(data: &[u8]) -> Vec { + let mut out = Vec::with_capacity(data.len() + data.len() / 128 + 2); + let n = data.len(); + let mut i = 0; + while i < n { + let mut run = 1; + while i + run < n && run < 128 && data[i + run] == data[i] { + run += 1; + } + if run >= 2 { + out.push((257 - run) as u8); + out.push(data[i]); + i += run; + continue; + } + // Literal: extend until a repeat begins or 128 bytes. + let start = i; + let mut j = i + 1; + while j < n && j - start < 128 && !(j + 1 < n && data[j] == data[j + 1]) { + j += 1; + } + out.push((j - start - 1) as u8); + out.extend_from_slice(&data[start..j]); + i = j; + } + out.push(128); + out +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::CodecError; + + fn dec(d: &[u8]) -> Result, CodecError> { + decode(d, 10_000).map_err(|f| f.error) + } + + #[test] + fn vectors() { + assert_eq!(dec(&[2, b'a', b'b', b'c', 254, b'x', 128]).unwrap(), b"abcxxx"); + assert_eq!(dec(&[129, 7, 128]).unwrap(), [7; 128]); + assert!(dec(&[127]).is_err()); + assert_eq!(dec(&[0, b'q']).unwrap(), b"q"); // missing EOD + assert_eq!(dec(&[128, 0, b'q']).unwrap(), b""); // data after EOD ignored + assert_eq!(dec(&[]).unwrap(), b""); + } + + #[test] + fn truncated() { + let f = decode(&[4, b'a', b'b'], 100).err().unwrap(); + assert!(matches!(f.error, CodecError::Corrupt { .. })); + assert_eq!(f.partial, b"ab"); + assert!(matches!(dec(&[0, b'a', 200]), Err(CodecError::Corrupt { .. }))); + } + + #[test] + fn limit() { + assert!(matches!(decode(&[129, 0, 129, 0], 200).map_err(|f| f.error), Err(CodecError::LimitExceeded(200)))); + assert!(matches!(decode(&[3, 1, 2, 3, 4], 3).map_err(|f| f.error), Err(CodecError::LimitExceeded(3)))); + assert_eq!(decode(&[129, 0, 129, 0], 256).ok().unwrap().len(), 256); + } + + #[test] + fn encoding() { + assert_eq!(encode(b"abcxxx"), [2, b'a', b'b', b'c', 254, b'x', 128]); + assert_eq!(encode(b""), [128]); + assert_eq!(encode(&[5; 300]), [129, 5, 129, 5, 213, 5, 128]); + let lit: Vec = (0..200u8).collect(); + let e = encode(&lit); + assert_eq!((e[0], e[129]), (127, 71)); + assert_eq!(dec(&e).unwrap(), lit); + } +} From eb640fd9928b17db5a9b796ca7f16280c60d5b1d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:24:39 -0500 Subject: [PATCH 050/120] Add craft-sheet: the cell positions, values and tables the spreadsheet formats share (#956) craft-xls carried its own CellRef, Range, Value, CellError, Visibility, sheet-name quoting, built-in number format table and indexed palette. craft-xlsx and craft-ods need the same ones, so they move to a crate with no dependencies and craft-xls re-exports them: an application gets one Value and one CellRef whichever file it read. Added on the way: A1 parsing (CellRef::parse, Range::parse), Range::count and a1, CellError::parse and the newer error values SpreadsheetML stores as text, builtin_format_id for writers. --- Cargo.lock | 5 + Cargo.toml | 1 + README.md | 1 + crates/sheet/Cargo.toml | 13 ++ crates/sheet/README.md | 18 +++ crates/sheet/src/a1.rs | 252 +++++++++++++++++++++++++++++++++++++ crates/sheet/src/lib.rs | 18 +++ crates/sheet/src/style.rs | 87 +++++++++++++ crates/sheet/src/tables.rs | 94 ++++++++++++++ crates/sheet/src/value.rs | 133 ++++++++++++++++++++ crates/xls/Cargo.toml | 1 + crates/xls/README.md | 3 +- crates/xls/src/expr.rs | 94 +------------- crates/xls/src/lib.rs | 3 +- crates/xls/src/model.rs | 186 +-------------------------- crates/xls/src/styles.rs | 73 +---------- 16 files changed, 630 insertions(+), 352 deletions(-) create mode 100644 crates/sheet/Cargo.toml create mode 100644 crates/sheet/README.md create mode 100644 crates/sheet/src/a1.rs create mode 100644 crates/sheet/src/lib.rs create mode 100644 crates/sheet/src/style.rs create mode 100644 crates/sheet/src/tables.rs create mode 100644 crates/sheet/src/value.rs diff --git a/Cargo.lock b/Cargo.lock index 2fc2f8292..7f405cb29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1570,6 +1570,10 @@ dependencies = [ "toml_edit", ] +[[package]] +name = "craft-sheet" +version = "0.1.0" + [[package]] name = "craft-silhouette-studio" version = "0.1.0" @@ -1794,6 +1798,7 @@ name = "craft-xls" version = "0.1.0" dependencies = [ "craft-cfb", + "craft-sheet", "thiserror 2.0.21", ] diff --git a/Cargo.toml b/Cargo.toml index 3634d3b8a..a611a202a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -86,6 +86,7 @@ craft-pst = { path = "crates/pst" } craft-quit = { path = "crates/quit" } craft-rtf = { path = "crates/rtf" } craft-raw = { path = "crates/raw" } +craft-sheet = { path = "crates/sheet" } craft-sketch = { path = "crates/sketch" } craft-settings = { path = "crates/settings" } craft-silhouette-studio = { path = "crates/silhouette-studio" } diff --git a/README.md b/README.md index b48364a81..4e8f4e78d 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " Self { + CellRef { row, col } + } + + /// The A1 text (`B3`). + pub fn a1(self) -> String { + let mut s = String::new(); + self.push_a1(&mut s); + s + } + + /// Appends the A1 text. + pub fn push_a1(self, s: &mut String) { + push_col(s, self.col); + let _ = write!(s, "{}", u64::from(self.row) + 1); + } + + /// Reads `B3`, `$B$3`, `b3`: a cell inside the grid, with nothing after it. + pub fn parse(s: &str) -> Option { + match CellRef::parse_prefix(s)? { + (cell, "") => Some(cell), + _ => None, + } + } + + /// Reads an A1 reference at the start of `s` and returns it with the rest of `s`. + pub fn parse_prefix(s: &str) -> Option<(CellRef, &str)> { + let s = s.strip_prefix('$').unwrap_or(s); + let letters = s.bytes().take_while(u8::is_ascii_alphabetic).count(); + let (cols, rest) = s.split_at_checked(letters)?; + let col = col_from_letters(cols)?; + let rest = rest.strip_prefix('$').unwrap_or(rest); + let digits = rest.bytes().take_while(u8::is_ascii_digit).count(); + if digits == 0 || digits > 7 { + return None; + } + let (rows, rest) = rest.split_at_checked(digits)?; + let row: u32 = rows.parse().ok()?; + if row == 0 || row > MAX_ROWS { + return None; + } + Some((CellRef::new(row - 1, col), rest)) + } +} + +impl std::fmt::Display for CellRef { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.a1()) + } +} + +/// A rectangle of cells, both corners included; `first` is the top left one. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct Range { + pub first: CellRef, + pub last: CellRef, +} + +impl Range { + /// The rectangle spanned by two corners, in any order. + pub fn new(a: CellRef, b: CellRef) -> Self { + Range { first: CellRef::new(a.row.min(b.row), a.col.min(b.col)), last: CellRef::new(a.row.max(b.row), a.col.max(b.col)) } + } + + pub fn contains(&self, c: CellRef) -> bool { + (self.first.row..=self.last.row).contains(&c.row) && (self.first.col..=self.last.col).contains(&c.col) + } + + pub fn is_single(&self) -> bool { + self.first == self.last + } + + /// How many cells the rectangle holds. + pub fn count(&self) -> u64 { + (u64::from(self.last.row - self.first.row) + 1) * (u64::from(self.last.col - self.first.col) + 1) + } + + /// The A1 text: `B3` for one cell, `A1:C2` otherwise. + pub fn a1(&self) -> String { + let mut s = self.first.a1(); + if !self.is_single() { + s.push(':'); + self.last.push_a1(&mut s); + } + s + } + + /// Reads `A1:C2`, `$A$1:$C$2`, one cell (`B3`), whole columns (`A:C`) and whole rows + /// (`1:3`), with surrounding space ignored. Whole columns and rows span the grid. + pub fn parse(s: &str) -> Option { + let s = s.trim(); + let Some((a, b)) = s.split_once(':') else { + return CellRef::parse(s).map(|c| Range { first: c, last: c }); + }; + if let (Some(x), Some(y)) = (CellRef::parse(a), CellRef::parse(b)) { + return Some(Range::new(x, y)); + } + let (a, b) = (a.strip_prefix('$').unwrap_or(a), b.strip_prefix('$').unwrap_or(b)); + if let (Some(x), Some(y)) = (col_from_letters(a), col_from_letters(b)) { + return Some(Range::new(CellRef::new(0, x), CellRef::new(MAX_ROWS - 1, y))); + } + let row = |t: &str| -> Option { + if t.is_empty() || t.len() > 7 || !t.bytes().all(|c| c.is_ascii_digit()) { + return None; + } + t.parse::().ok().filter(|r| (1..=MAX_ROWS).contains(r)).map(|r| r - 1) + }; + Some(Range::new(CellRef::new(row(a)?, 0), CellRef::new(row(b)?, MAX_COLS - 1))) + } +} + +impl std::fmt::Display for Range { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.a1()) + } +} + +/// Appends a column's letters: 0 is `A`, 25 is `Z`, 26 is `AA`. +pub fn push_col(s: &mut String, col: u32) { + let mut letters = [0u8; 8]; + let mut i = letters.len(); + let mut n = u64::from(col) + 1; + while n > 0 && i > 0 { + i -= 1; + if let Some(l) = letters.get_mut(i) { + *l = b'A' + ((n - 1) % 26) as u8; + } + n = (n - 1) / 26; + } + for &l in letters.get(i..).unwrap_or_default() { + s.push(char::from(l)); + } +} + +/// A column's letters: 0 is `A`, 16383 is `XFD`. +pub fn col_letters(col: u32) -> String { + let mut s = String::new(); + push_col(&mut s, col); + s +} + +/// `A` is 0, `xfd` is 16383. `None` for empty text, anything but one to three letters, or a +/// column beyond `XFD`. +pub fn col_from_letters(s: &str) -> Option { + if s.is_empty() || s.len() > 3 || !s.bytes().all(|b| b.is_ascii_alphabetic()) { + return None; + } + let n = s.bytes().fold(0u32, |n, b| n * 26 + u32::from(b.to_ascii_uppercase() - b'A') + 1); + (n <= MAX_COLS).then(|| n - 1) +} + +/// Whether `name` reads as an R1C1 reference: `R`, `C`, `RC`, `R1`, `C1`, `R1C`, `RC1`, `R1C1` +/// in any case. +fn is_r1c1_token(name: &str) -> bool { + let mut chars = name.chars().peekable(); + let mut letters = 0; + for letter in ['r', 'c'] { + if chars.peek().is_some_and(|c| c.to_ascii_lowercase() == letter) { + chars.next(); + letters += 1; + while chars.peek().is_some_and(char::is_ascii_digit) { + chars.next(); + } + } + } + letters > 0 && chars.next().is_none() +} + +/// Whether Excel quotes a sheet name in a reference (`'My Sheet'!A1`): it starts with a digit or +/// `.`, holds anything but letters (any script), digits, `_` and `.`, reads as an A1 cell or as +/// an R1C1 reference, or is a boolean. +pub fn sheet_needs_quotes(name: &str) -> bool { + let Some(first) = name.chars().next() else { + return true; + }; + if first.is_ascii_digit() || first == '.' || name.chars().any(|c| !(c.is_alphanumeric() || c == '_' || c == '.')) { + return true; + } + // No `$` can be here (it is not alphanumeric), so this is exactly "an A1 cell in the grid". + CellRef::parse(name).is_some() || is_r1c1_token(name) || name.eq_ignore_ascii_case("TRUE") || name.eq_ignore_ascii_case("FALSE") +} + +/// A sheet name as it is written in a reference: quoted, with `'` doubled, when Excel quotes it. +pub fn quote_sheet(name: &str) -> String { + if sheet_needs_quotes(name) { format!("'{}'", name.replace('\'', "''")) } else { name.to_string() } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a1_text_both_ways() { + assert_eq!(CellRef::new(2, 27).a1(), "AB3"); + assert_eq!(CellRef::parse("AB3"), Some(CellRef::new(2, 27))); + assert_eq!(CellRef::parse("$ab$3"), Some(CellRef::new(2, 27))); + assert_eq!(CellRef::parse("XFD1048576"), Some(CellRef::new(MAX_ROWS - 1, MAX_COLS - 1))); + assert_eq!(CellRef::new(MAX_ROWS - 1, MAX_COLS - 1).a1(), "XFD1048576"); + for bad in ["", "A", "1", "A0", "XFE1", "A1048577", "A1x", "A+1", "A-1", "AAAA1", "A12345678", "É1"] { + assert_eq!(CellRef::parse(bad), None, "{bad}"); + } + assert_eq!(CellRef::parse_prefix("B2:C3"), Some((CellRef::new(1, 1), ":C3"))); + assert_eq!(col_letters(16383), "XFD"); + assert_eq!((col_letters(26), col_letters(u32::MAX).len()), ("AA".to_string(), 7)); + assert_eq!(col_from_letters("xfd"), Some(16383)); + assert_eq!(col_from_letters("XFE"), None); + } + + #[test] + fn ranges() { + let r = Range::parse(" $C$2:A1 "); + assert_eq!(r, Some(Range { first: CellRef::new(0, 0), last: CellRef::new(1, 2) })); + assert_eq!(r.map(|r| (r.a1(), r.count())), Some(("A1:C2".to_string(), 6))); + assert_eq!(Range::parse("B3").map(|r| (r.is_single(), r.a1())), Some((true, "B3".to_string()))); + assert_eq!(Range::parse("B:$A"), Some(Range::new(CellRef::new(0, 0), CellRef::new(MAX_ROWS - 1, 1)))); + assert_eq!(Range::parse("3:2"), Some(Range::new(CellRef::new(1, 0), CellRef::new(2, MAX_COLS - 1)))); + assert_eq!(Range::parse("1:1048577"), None); + assert_eq!(Range::parse("0:1"), None); + assert_eq!(Range::parse("A1:"), None); + assert_eq!(Range::parse("A:1"), None); + assert!(Range::new(CellRef::new(0, 0), CellRef::new(MAX_ROWS - 1, MAX_COLS - 1)).count() == 17_179_869_184); + } + + #[test] + fn sheet_names_are_quoted_when_excel_quotes_them() { + for plain in ["Sheet1", "Data_2", "Übersicht", "a.b", "XFE1", "A1048577", "RC1x"] { + assert!(!sheet_needs_quotes(plain), "{plain}"); + } + for quoted in ["", "My Sheet", "1st", ".x", "A1", "xfd1048576", "R1C1", "rc", "C", "TRUE", "false", "a-b", "It's"] { + assert!(sheet_needs_quotes(quoted), "{quoted}"); + } + assert_eq!(quote_sheet("It's"), "'It''s'"); + assert_eq!(quote_sheet("Sheet1"), "Sheet1"); + } +} diff --git a/crates/sheet/src/lib.rs b/crates/sheet/src/lib.rs new file mode 100644 index 000000000..337a3daf8 --- /dev/null +++ b/crates/sheet/src/lib.rs @@ -0,0 +1,18 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +//! Spreadsheet vocabulary shared by the spreadsheet format crates (`craft-xls`, `craft-xlsx`, +//! `craft-ods`): cell positions and their A1 text, cell values and error values, sheet-name +//! quoting, the cell formatting enumerations, Excel's built-in number formats and its indexed +//! colour palette. +//! +//! Nothing here is a file format; each format crate re-exports what it uses, so an application +//! gets one `Value` and one `CellRef` whichever file it read. + +mod a1; +mod style; +mod tables; +mod value; + +pub use a1::{CellRef, MAX_COLS, MAX_ROWS, Range, col_from_letters, col_letters, push_col, quote_sheet, sheet_needs_quotes}; +pub use style::{BorderStyle, HAlign, Pattern, Script, Underline, VAlign}; +pub use tables::{DEFAULT_PALETTE, builtin_format, builtin_format_id}; +pub use value::{CellError, Value, Visibility}; diff --git a/crates/sheet/src/style.rs b/crates/sheet/src/style.rs new file mode 100644 index 000000000..d3c0576bc --- /dev/null +++ b/crates/sheet/src/style.rs @@ -0,0 +1,87 @@ +//! The cell formatting choices BIFF8 and SpreadsheetML share. Each format has its own spelling +//! for them (a number in an XF record, a name in `styles.xml`). + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum Underline { + #[default] + None, + Single, + Double, + SingleAccounting, + DoubleAccounting, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum Script { + #[default] + Baseline, + Superscript, + Subscript, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum HAlign { + #[default] + General, + Left, + Center, + Right, + Fill, + Justify, + CenterAcross, + Distributed, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum VAlign { + Top, + Center, + #[default] + Bottom, + Justify, + Distributed, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum BorderStyle { + #[default] + None, + Thin, + Medium, + Dashed, + Dotted, + Thick, + Double, + Hair, + MediumDashed, + DashDot, + MediumDashDot, + DashDotDot, + MediumDashDotDot, + SlantDashDot, +} + +/// Fill patterns, named as SpreadsheetML names them. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum Pattern { + #[default] + None, + Solid, + Gray50, + Gray75, + Gray25, + DarkHorizontal, + DarkVertical, + DarkDown, + DarkUp, + DarkGrid, + DarkTrellis, + LightHorizontal, + LightVertical, + LightDown, + LightUp, + LightGrid, + LightTrellis, + Gray125, + Gray0625, +} diff --git a/crates/sheet/src/tables.rs b/crates/sheet/src/tables.rs new file mode 100644 index 000000000..b30935d54 --- /dev/null +++ b/crates/sheet/src/tables.rs @@ -0,0 +1,94 @@ +//! Tables Excel files refer to by number. + +/// Built-in number format codes (en-US) by id. Ids not listed (23–36 are East Asian +/// locale formats) read as General. +const BUILTIN_FORMATS: [(u32, &str); 36] = [ + (0, "General"), + (1, "0"), + (2, "0.00"), + (3, "#,##0"), + (4, "#,##0.00"), + (5, "\"$\"#,##0_);\\(\"$\"#,##0\\)"), + (6, "\"$\"#,##0_);[Red]\\(\"$\"#,##0\\)"), + (7, "\"$\"#,##0.00_);\\(\"$\"#,##0.00\\)"), + (8, "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)"), + (9, "0%"), + (10, "0.00%"), + (11, "0.00E+00"), + (12, "# ?/?"), + (13, "# ??/??"), + (14, "m/d/yyyy"), + (15, "d-mmm-yy"), + (16, "d-mmm"), + (17, "mmm-yy"), + (18, "h:mm AM/PM"), + (19, "h:mm:ss AM/PM"), + (20, "h:mm"), + (21, "h:mm:ss"), + (22, "m/d/yyyy h:mm"), + (37, "#,##0 ;(#,##0)"), + (38, "#,##0 ;[Red](#,##0)"), + (39, "#,##0.00;(#,##0.00)"), + (40, "#,##0.00;[Red](#,##0.00)"), + (41, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)"), + (42, "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)"), + (43, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)"), + (44, "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)"), + (45, "mm:ss"), + (46, "[h]:mm:ss"), + (47, "mmss.0"), + (48, "##0.0E+0"), + (49, "@"), +]; + +pub fn builtin_format(id: u32) -> Option<&'static str> { + BUILTIN_FORMATS.iter().find(|(i, _)| *i == id).map(|(_, c)| *c) +} + +/// Id to write for a format code when it is a locale-independent built-in. The currency formats +/// 5 to 8 and the accounting formats with a currency, 42 and 44, are left out (`None`, to be +/// written as custom formats) because their meaning depends on the reader's locale. +pub fn builtin_format_id(code: &str) -> Option { + BUILTIN_FORMATS.iter().find(|(i, c)| *c == code && !matches!(i, 5..=8 | 42 | 44)).map(|(i, _)| *i) +} + +/// The indexed colour table of a workbook that does not replace it, as `0xRRGGBB`: the eight +/// fixed colours, then the 56 a BIFF8 Palette record ([MS-XLS] 2.4.188) or a SpreadsheetML +/// `indexedColors` list (ECMA-376 Part 1, 18.8.27) replaces. +pub const DEFAULT_PALETTE: [u32; 64] = [ + 0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, + 0xFF00FF, 0x00FFFF, 0x800000, 0x008000, 0x000080, 0x808000, 0x800080, 0x008080, 0xC0C0C0, 0x808080, 0x9999FF, 0x993366, 0xFFFFCC, 0xCCFFFF, + 0x660066, 0xFF8080, 0x0066CC, 0xCCCCFF, 0x000080, 0xFF00FF, 0xFFFF00, 0x00FFFF, 0x800080, 0x800000, 0x008080, 0x0000FF, 0x00CCFF, 0xCCFFFF, + 0xCCFFCC, 0xFFFF99, 0x99CCFF, 0xFF99CC, 0xCC99FF, 0xFFCC99, 0x3366FF, 0x33CCCC, 0x99CC00, 0xFFCC00, 0xFF9900, 0xFF6600, 0x666699, 0x969696, + 0x003366, 0x339966, 0x003300, 0x333300, 0x993300, 0x993366, 0x333399, 0x333333, +]; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn builtin_format_ids() { + assert_eq!((0..=60).filter(|&id| builtin_format(id).is_some()).count(), 36); + assert_eq!(builtin_format(0), Some("General")); + assert_eq!(builtin_format(14), Some("m/d/yyyy")); + assert_eq!(builtin_format(22), Some("m/d/yyyy h:mm")); + assert_eq!(builtin_format(49), Some("@")); + // 23 to 36 are locale formats that are not defined here. + assert_eq!((23..=36).find_map(builtin_format), None); + assert_eq!(builtin_format(50), None); + for id in 0..=60 { + if let Some(code) = builtin_format(id) { + let back = builtin_format_id(code); + assert_eq!(back, (!matches!(id, 5..=8 | 42 | 44)).then_some(id), "{id}"); + } + } + assert_eq!(builtin_format_id("yyyy"), None); + } + + #[test] + fn the_palette_repeats_its_eight_fixed_colours() { + assert_eq!(DEFAULT_PALETTE.get(..8), DEFAULT_PALETTE.get(8..16)); + assert_eq!(DEFAULT_PALETTE.get(63), Some(&0x333333)); + } +} diff --git a/crates/sheet/src/value.rs b/crates/sheet/src/value.rs new file mode 100644 index 000000000..066dc3e95 --- /dev/null +++ b/crates/sheet/src/value.rs @@ -0,0 +1,133 @@ +//! What a cell stores. + +use std::sync::Arc; + +/// The error values a spreadsheet file stores. The first eight are the ones BIFF8 has +/// ([MS-XLS] 2.5.10, BErr); the rest are newer and appear in SpreadsheetML as text. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum CellError { + Null, + Div0, + Value, + Ref, + Name, + Num, + NA, + GettingData, + Spill, + Calc, + Blocked, + Python, + Busy, + Timeout, +} + +impl CellError { + /// Every error value, in the order above. + pub const ALL: [CellError; 14] = [ + CellError::Null, + CellError::Div0, + CellError::Value, + CellError::Ref, + CellError::Name, + CellError::Num, + CellError::NA, + CellError::GettingData, + CellError::Spill, + CellError::Calc, + CellError::Blocked, + CellError::Python, + CellError::Busy, + CellError::Timeout, + ]; + + pub fn as_str(&self) -> &'static str { + match self { + CellError::Null => "#NULL!", + CellError::Div0 => "#DIV/0!", + CellError::Value => "#VALUE!", + CellError::Ref => "#REF!", + CellError::Name => "#NAME?", + CellError::Num => "#NUM!", + CellError::NA => "#N/A", + CellError::GettingData => "#GETTING_DATA", + CellError::Spill => "#SPILL!", + CellError::Calc => "#CALC!", + CellError::Blocked => "#BLOCKED!", + CellError::Python => "#PYTHON!", + CellError::Busy => "#BUSY!", + CellError::Timeout => "#TIMEOUT!", + } + } + + /// The error with this text, in any case. + pub fn parse(s: &str) -> Option { + CellError::ALL.iter().find(|e| e.as_str().eq_ignore_ascii_case(s)).copied() + } +} + +impl std::fmt::Display for CellError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +/// A cell's stored value (for a formula cell, its saved result). +#[derive(Clone, Debug, Default, PartialEq)] +pub enum Value { + #[default] + Empty, + Number(f64), + Text(Arc), + Bool(bool), + Error(CellError), +} + +impl Value { + /// A number, or `#NUM!` when it is NaN or infinite (Excel never stores those). Negative zero + /// becomes zero. + pub fn number(v: f64) -> Value { + if v.is_finite() { Value::Number(if v == 0.0 { 0.0 } else { v }) } else { Value::Error(CellError::Num) } + } + + pub fn text(s: impl Into>) -> Value { + Value::Text(s.into()) + } + + pub fn is_empty(&self) -> bool { + matches!(self, Value::Empty) + } +} + +/// Whether a sheet shows: `VeryHidden` cannot be unhidden from the user interface. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Visibility { + #[default] + Visible, + Hidden, + VeryHidden, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn errors_parse_back_from_their_text() { + for e in CellError::ALL { + assert_eq!(CellError::parse(e.as_str()), Some(e)); + assert_eq!(CellError::parse(&e.as_str().to_ascii_lowercase()), Some(e)); + } + assert_eq!(CellError::parse("#NOPE!"), None); + assert_eq!(CellError::Div0.to_string(), "#DIV/0!"); + } + + #[test] + fn numbers_are_finite() { + assert_eq!(Value::number(f64::NAN), Value::Error(CellError::Num)); + assert_eq!(Value::number(f64::INFINITY), Value::Error(CellError::Num)); + assert!(matches!(Value::number(-0.0), Value::Number(n) if n.is_sign_positive())); + assert!(Value::Empty.is_empty() && !Value::text("").is_empty()); + } +} diff --git a/crates/xls/Cargo.toml b/crates/xls/Cargo.toml index f742798c5..41d3362fb 100644 --- a/crates/xls/Cargo.toml +++ b/crates/xls/Cargo.toml @@ -12,4 +12,5 @@ workspace = true [dependencies] craft-cfb = { workspace = true } +craft-sheet = { workspace = true } thiserror = { workspace = true } diff --git a/crates/xls/README.md b/crates/xls/README.md index c0b6c7489..7fbb93280 100644 --- a/crates/xls/README.md +++ b/crates/xls/README.md @@ -3,7 +3,8 @@ A clean-room reader for Excel 97-2003 workbooks (`.xls`, BIFF8). It turns the bytes of a file into a `Workbook` that keeps the file's own terms: cells, formulas as text, the format table, column and row records. Mapping that onto an application's document is the caller's work. -No `unsafe`; the only dependencies are `craft-cfb` (the compound file container) and `thiserror`. +No `unsafe`; the only dependencies are `craft-cfb` (the compound file container), `craft-sheet` (cell +positions, values and tables shared with the other spreadsheet formats) and `thiserror`. No third-party implementation was consulted. | Document | Used for | diff --git a/crates/xls/src/expr.rs b/crates/xls/src/expr.rs index 704e2a57c..f4e0b114d 100644 --- a/crates/xls/src/expr.rs +++ b/crates/xls/src/expr.rs @@ -3,12 +3,9 @@ use std::fmt::Write; use std::sync::Arc; -use crate::CellError; +use craft_sheet::{push_col, quote_sheet, sheet_needs_quotes}; -/// Rows and columns of the grid a sheet name is tested against when deciding whether it reads -/// as a cell reference (the SpreadsheetML grid, which is the larger one). -const GRID_ROWS: u32 = 1_048_576; -const GRID_COLS: u32 = 16_384; +use crate::CellError; /// One end of a reference, with `$` anchors. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -116,31 +113,6 @@ pub(crate) enum Expr { Paren(Box), } -/// 0 → `A`, 25 → `Z`, 26 → `AA`. -fn push_col(s: &mut String, col: u32) { - let mut letters = [0u8; 8]; - let mut i = letters.len(); - let mut n = u64::from(col) + 1; - while n > 0 && i > 0 { - i -= 1; - if let Some(l) = letters.get_mut(i) { - *l = b'A' + ((n - 1) % 26) as u8; - } - n = (n - 1) / 26; - } - for &l in letters.get(i..).unwrap_or_default() { - s.push(char::from(l)); - } -} - -/// The A1 text of a cell (`B3`), for messages and tests. -pub(crate) fn a1(row: u32, col: u32) -> String { - let mut s = String::new(); - push_col(&mut s, col); - let _ = write!(s, "{}", u64::from(row) + 1); - s -} - fn push_anchor(s: &mut String, a: &Anchor) { if a.col_abs { s.push('$'); @@ -152,53 +124,6 @@ fn push_anchor(s: &mut String, a: &Anchor) { let _ = write!(s, "{}", u64::from(a.row) + 1); } -/// Whether `name` is an A1 cell reference inside the grid (`A1`, `xfd1048576`). -fn is_a1(name: &str) -> bool { - let letters = name.bytes().take_while(u8::is_ascii_alphabetic).count(); - let Some((cols, digits)) = name.split_at_checked(letters) else { return false }; - if cols.is_empty() || cols.len() > 3 || digits.is_empty() || digits.len() > 7 || !digits.bytes().all(|b| b.is_ascii_digit()) { - return false; - } - let col = cols.bytes().fold(0u32, |n, b| n * 26 + u32::from(b.to_ascii_uppercase() - b'A') + 1); - let row: u32 = digits.parse().unwrap_or(0); - (1..=GRID_COLS).contains(&col) && (1..=GRID_ROWS).contains(&row) -} - -/// Whether `name` reads as an R1C1 reference: `R`, `C`, `RC`, `R1`, `C1`, `R1C`, `RC1`, `R1C1` -/// in any case. -fn is_r1c1_token(name: &str) -> bool { - let mut chars = name.chars().peekable(); - let mut letters = 0; - for letter in ['r', 'c'] { - if chars.peek().is_some_and(|c| c.to_ascii_lowercase() == letter) { - chars.next(); - letters += 1; - while chars.peek().is_some_and(char::is_ascii_digit) { - chars.next(); - } - } - } - letters > 0 && chars.next().is_none() -} - -/// Whether Excel quotes a sheet name in a reference (`'My Sheet'!A1`): it starts with a digit or -/// `.`, holds anything but letters (any script), digits, `_` and `.`, reads as an A1 cell or as -/// an R1C1 reference, or is a boolean. -pub fn sheet_needs_quotes(name: &str) -> bool { - let Some(first) = name.chars().next() else { - return true; - }; - if first.is_ascii_digit() || first == '.' || name.chars().any(|c| !(c.is_alphanumeric() || c == '_' || c == '.')) { - return true; - } - is_a1(name) || is_r1c1_token(name) || name.eq_ignore_ascii_case("TRUE") || name.eq_ignore_ascii_case("FALSE") -} - -/// A sheet name as it is written in a reference: quoted, with `'` doubled, when Excel quotes it. -pub fn quote_sheet(name: &str) -> String { - if sheet_needs_quotes(name) { format!("'{}'", name.replace('\'', "''")) } else { name.to_string() } -} - fn push_reference(s: &mut String, r: &Reference) { match &r.sheet { SheetSel::Current => {} @@ -402,19 +327,4 @@ mod tests { assert_eq!(number_to_text(1e-10), "1E-10"); assert!(number(f64::MAX).parse::().is_ok_and(f64::is_finite)); } - - #[test] - fn sheet_names_are_quoted_when_excel_quotes_them() { - for plain in ["Sheet1", "Data_2", "Übersicht", "a.b"] { - assert!(!sheet_needs_quotes(plain), "{plain}"); - } - for quoted in ["", "My Data", "1st", ".x", "A1", "xfd1048576", "R1C1", "rc", "true", "It's"] { - assert!(sheet_needs_quotes(quoted), "{quoted}"); - } - // Outside the grid it is no cell reference. - assert!(!sheet_needs_quotes("XFE1")); - assert!(!sheet_needs_quotes("A1048577")); - assert_eq!(quote_sheet("It's"), "'It''s'"); - assert_eq!(a1(2, 27), "AB3"); - } } diff --git a/crates/xls/src/lib.rs b/crates/xls/src/lib.rs index 20d73ab4c..0dc33f61b 100644 --- a/crates/xls/src/lib.rs +++ b/crates/xls/src/lib.rs @@ -40,13 +40,12 @@ mod styles; #[doc(hidden)] pub mod testing; -pub use expr::{quote_sheet, sheet_needs_quotes}; +pub use craft_sheet::{DEFAULT_PALETTE, quote_sheet, sheet_needs_quotes}; pub use model::{ Alignment, BorderLine, BorderStyle, Borders, Calc, CalcMode, Cell, CellError, CellFormat, CellRef, Color, Column, DefinedName, Fill, Font, Formula, HAlign, Pattern, Range, Row, Script, Sheet, Underline, VAlign, Value, View, Visibility, Workbook, }; pub use read::{is_xls, read, read_with}; -pub use styles::DEFAULT_PALETTE; /// Why a workbook could not be read. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] diff --git a/crates/xls/src/model.rs b/crates/xls/src/model.rs index 3aedf8697..ce88a043d 100644 --- a/crates/xls/src/model.rs +++ b/crates/xls/src/model.rs @@ -2,98 +2,8 @@ //! the format (widths and heights keep the file's units, formulas are text). use std::collections::BTreeMap; -use std::sync::Arc; -/// A cell position, zero-based. Ordered by row, then column. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct CellRef { - pub row: u32, - pub col: u32, -} - -impl CellRef { - pub const fn new(row: u32, col: u32) -> Self { - CellRef { row, col } - } - - /// The A1 text (`B3`). - pub fn a1(self) -> String { - crate::expr::a1(self.row, self.col) - } -} - -/// A rectangle of cells, both corners included; `first` is the top left one. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct Range { - pub first: CellRef, - pub last: CellRef, -} - -impl Range { - /// The rectangle spanned by two corners, in any order. - pub fn new(a: CellRef, b: CellRef) -> Self { - Range { first: CellRef::new(a.row.min(b.row), a.col.min(b.col)), last: CellRef::new(a.row.max(b.row), a.col.max(b.col)) } - } - - pub fn contains(&self, c: CellRef) -> bool { - (self.first.row..=self.last.row).contains(&c.row) && (self.first.col..=self.last.col).contains(&c.col) - } - - pub fn is_single(&self) -> bool { - self.first == self.last - } -} - -/// The error values a BIFF8 file stores ([MS-XLS] 2.5.10, BErr). -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum CellError { - Null, - Div0, - Value, - Ref, - Name, - Num, - NA, - GettingData, -} - -impl CellError { - pub fn as_str(&self) -> &'static str { - match self { - CellError::Null => "#NULL!", - CellError::Div0 => "#DIV/0!", - CellError::Value => "#VALUE!", - CellError::Ref => "#REF!", - CellError::Name => "#NAME?", - CellError::Num => "#NUM!", - CellError::NA => "#N/A", - CellError::GettingData => "#GETTING_DATA", - } - } -} - -/// A cell's stored value (for a formula cell, its saved result). -#[derive(Clone, Debug, Default, PartialEq)] -pub enum Value { - #[default] - Empty, - Number(f64), - Text(Arc), - Bool(bool), - Error(CellError), -} - -impl Value { - /// A number, or `#NUM!` when it is NaN or infinite (Excel never stores those). Negative zero - /// becomes zero. - pub fn number(v: f64) -> Value { - if v.is_finite() { Value::Number(if v == 0.0 { 0.0 } else { v }) } else { Value::Error(CellError::Num) } - } - - pub fn is_empty(&self) -> bool { - matches!(self, Value::Empty) - } -} +pub use craft_sheet::{BorderStyle, CellError, CellRef, HAlign, Pattern, Range, Script, Underline, VAlign, Value, Visibility}; /// A cell's formula. #[derive(Clone, Debug, PartialEq, Eq)] @@ -114,15 +24,6 @@ pub struct Cell { pub formula: Option, } -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub enum Visibility { - #[default] - Visible, - Hidden, - /// Hidden, and not offered by Excel's Unhide dialog. - VeryHidden, -} - /// A column's ColInfo record ([MS-XLS] 2.4.53). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Column { @@ -224,24 +125,6 @@ pub enum Color { Rgb(u32), } -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub enum Underline { - #[default] - None, - Single, - Double, - SingleAccounting, - DoubleAccounting, -} - -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub enum Script { - #[default] - Baseline, - Superscript, - Subscript, -} - /// A Font record ([MS-XLS] 2.4.122). #[derive(Clone, Debug, Default, PartialEq)] pub struct Font { @@ -261,29 +144,6 @@ pub struct Font { pub charset: Option, } -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub enum HAlign { - #[default] - General, - Left, - Center, - Right, - Fill, - Justify, - CenterAcross, - Distributed, -} - -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub enum VAlign { - Top, - Center, - #[default] - Bottom, - Justify, - Distributed, -} - #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct Alignment { pub h: HAlign, @@ -295,25 +155,6 @@ pub struct Alignment { pub shrink: bool, } -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub enum BorderStyle { - #[default] - None, - Thin, - Medium, - Dashed, - Dotted, - Thick, - Double, - Hair, - MediumDashed, - DashDot, - MediumDashDot, - DashDotDot, - MediumDashDotDot, - SlantDashDot, -} - /// One border side. A side without a style has the automatic colour. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct BorderLine { @@ -333,31 +174,6 @@ pub struct Borders { pub diag_up: BorderLine, } -/// Fill patterns, named as SpreadsheetML names them. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub enum Pattern { - #[default] - None, - Solid, - Gray50, - Gray75, - Gray25, - DarkHorizontal, - DarkVertical, - DarkDown, - DarkUp, - DarkGrid, - DarkTrellis, - LightHorizontal, - LightVertical, - LightDown, - LightUp, - LightGrid, - LightTrellis, - Gray125, - Gray0625, -} - /// A cell's fill. Without a pattern both colours are automatic. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct Fill { diff --git a/crates/xls/src/styles.rs b/crates/xls/src/styles.rs index 59d687848..ea0fcae8e 100644 --- a/crates/xls/src/styles.rs +++ b/crates/xls/src/styles.rs @@ -9,61 +9,7 @@ use crate::model::{ Alignment, BorderLine, BorderStyle, Borders, CellFormat, Color, Fill, Font, HAlign, Pattern, Script, Underline, VAlign, Workbook, }; use crate::records::Cursor; - -/// Built-in number format codes (en-US) by id. Ids not listed (23–36 are East Asian -/// locale formats) read as General. -const BUILTIN_FORMATS: [(u32, &str); 36] = [ - (0, "General"), - (1, "0"), - (2, "0.00"), - (3, "#,##0"), - (4, "#,##0.00"), - (5, "\"$\"#,##0_);\\(\"$\"#,##0\\)"), - (6, "\"$\"#,##0_);[Red]\\(\"$\"#,##0\\)"), - (7, "\"$\"#,##0.00_);\\(\"$\"#,##0.00\\)"), - (8, "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)"), - (9, "0%"), - (10, "0.00%"), - (11, "0.00E+00"), - (12, "# ?/?"), - (13, "# ??/??"), - (14, "m/d/yyyy"), - (15, "d-mmm-yy"), - (16, "d-mmm"), - (17, "mmm-yy"), - (18, "h:mm AM/PM"), - (19, "h:mm:ss AM/PM"), - (20, "h:mm"), - (21, "h:mm:ss"), - (22, "m/d/yyyy h:mm"), - (37, "#,##0 ;(#,##0)"), - (38, "#,##0 ;[Red](#,##0)"), - (39, "#,##0.00;(#,##0.00)"), - (40, "#,##0.00;[Red](#,##0.00)"), - (41, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)"), - (42, "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)"), - (43, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)"), - (44, "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)"), - (45, "mm:ss"), - (46, "[h]:mm:ss"), - (47, "mmss.0"), - (48, "##0.0E+0"), - (49, "@"), -]; - -pub(crate) fn builtin_format(id: u32) -> Option<&'static str> { - BUILTIN_FORMATS.iter().find(|(i, _)| *i == id).map(|(_, c)| *c) -} - -/// The colour table a workbook without a Palette record uses, as `0xRRGGBB`: the eight fixed -/// colours, then the 56 a Palette record replaces ([MS-XLS] 2.4.188). -pub const DEFAULT_PALETTE: [u32; 64] = [ - 0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, - 0xFF00FF, 0x00FFFF, 0x800000, 0x008000, 0x000080, 0x808000, 0x800080, 0x008080, 0xC0C0C0, 0x808080, 0x9999FF, 0x993366, 0xFFFFCC, 0xCCFFFF, - 0x660066, 0xFF8080, 0x0066CC, 0xCCCCFF, 0x000080, 0xFF00FF, 0xFFFF00, 0x00FFFF, 0x800080, 0x800000, 0x008080, 0x0000FF, 0x00CCFF, 0xCCFFFF, - 0xCCFFCC, 0xFFFF99, 0x99CCFF, 0xFF99CC, 0xCC99FF, 0xFFCC99, 0x3366FF, 0x33CCCC, 0x99CC00, 0xFFCC00, 0xFF9900, 0xFF6600, 0x666699, 0x969696, - 0x003366, 0x339966, 0x003300, 0x333300, 0x993300, 0x993366, 0x333399, 0x333333, -]; +use craft_sheet::{DEFAULT_PALETTE, builtin_format}; /// Font records kept at most ([MS-XLS] FontIndex allows indexes up to 1022). const MAX_FONTS: usize = 1024; @@ -453,20 +399,3 @@ pub(crate) fn build(r: &StyleRecords, formats: &HashMap, wb: &mut W wb.cell_styles = named; wb.formats.iter().map(|f| *f == plain).collect() } - -#[cfg(test)] -mod tests { - use super::builtin_format; - - #[test] - fn builtin_format_ids() { - assert_eq!((0..=60).filter(|&id| builtin_format(id).is_some()).count(), 36); - assert_eq!(builtin_format(0), Some("General")); - assert_eq!(builtin_format(14), Some("m/d/yyyy")); - assert_eq!(builtin_format(22), Some("m/d/yyyy h:mm")); - assert_eq!(builtin_format(49), Some("@")); - // 23-36 are locale formats, above 49 is custom. - assert_eq!((23..=36).find_map(builtin_format), None); - assert_eq!(builtin_format(50), None); - } -} From 219f63f64cd045d9027c31dd3973922e48af2cc8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:25:01 -0500 Subject: [PATCH 051/120] Add craft-xcf: GIMP XCF reader from PhotoCraft (#960) * Add craft-xcf: GIMP XCF reader from PhotoCraft The format half of PhotoCraft's XCF import (crates/io/src/xcf.rs at abeb16c96): the header, precisions, property lists with their length repairs, parasites, layer structures and item paths, channels, and the uncompressed, RLE and zlib tiles. The mapping onto a document (blend modes, surfaces, profiles, warnings) stays in the app. * craft-xcf: an RLE run of zero length is an error A long run (op 127 or 128) with a 16-bit length of 0 consumed input without producing output, so a tile of them was work the pixel budget never charged. Ported from storytold/photocraft#11138. * craft-xcf: channel names are charged to the metadata budget Layer names already were. Many channel pointers to one channel with a large name made a copy per read, multiplying a small file into gigabytes of retained names. Ported from storytold/photocraft#11138. --- Cargo.lock | 8 + Cargo.toml | 1 + README.md | 1 + crates/xcf/Cargo.toml | 15 + crates/xcf/README.md | 40 ++ crates/xcf/src/lib.rs | 923 ++++++++++++++++++++++++++++++++ crates/xcf/src/tests.rs | 1120 +++++++++++++++++++++++++++++++++++++++ crates/xcf/src/tiles.rs | 70 +++ 8 files changed, 2178 insertions(+) create mode 100644 crates/xcf/Cargo.toml create mode 100644 crates/xcf/README.md create mode 100644 crates/xcf/src/lib.rs create mode 100644 crates/xcf/src/tests.rs create mode 100644 crates/xcf/src/tiles.rs diff --git a/Cargo.lock b/Cargo.lock index 7f405cb29..1be03246b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1793,6 +1793,14 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "craft-xcf" +version = "0.1.0" +dependencies = [ + "flate2", + "thiserror 2.0.21", +] + [[package]] name = "craft-xls" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index a611a202a..3f361200d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -104,6 +104,7 @@ craft-vcard = { path = "crates/vcard" } craft-vectorcraft-format = { path = "crates/vectorcraft-format" } craft-vectordrawable = { path = "crates/vectordrawable" } craft-xls = { path = "crates/xls" } +craft-xcf = { path = "crates/xcf" } craft-xml = { path = "crates/xml" } craft-zip = { path = "crates/zip" } craft-y4m = { path = "crates/y4m" } diff --git a/README.md b/README.md index 4e8f4e78d..5b51e5ebd 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = ", versions 0 to 26) and from files GIMP 3.2 +writes; no GIMP code is used. + +- `is_xcf`, `is_gzipped_xcf`, `gunzip`: recognise a file and inflate a compressed one. +- `Xcf::parse`: the header, the image properties and every layer structure, as an `Image`: + version, canvas, base type, `Precision`, `Compression`, colormap, resolution, guides, the ICC + profile and comment parasites, the path count, and each `Layer` with its properties as GIMP + wrote them (mode, opacity, offsets, locks, colour tag, composite mode and spaces, group flag, + item path, text / vector / link flags, effect count). No pixel is decoded. +- `Xcf::layer_pixels`, `Xcf::layer_mask`, `Xcf::channel`: one drawable's pixels as normalised + `f32` samples (`Pixels`), from uncompressed, RLE or zlib tiles at every precision (8, 16 and + 32-bit integer; 16, 32 and 64-bit float). Indexed pixels are expanded through the colormap. + Each call takes its samples from a byte budget the caller owns, and a `keep_going` callback. +- `Image::tree`: the layers arranged by their item paths, with the ones whose path leads nowhere + or nests too deep reported. + +The reader repairs what GIMP writes wrongly: the length words of `PROP_COLORMAP`, +`PROP_USER_UNIT` and `PROP_LINK_LAYER` are measured, not trusted. + +Layer modes, colour tags and composite settings stay numbers: what they mean to an application +is not decided here. Paths, layer effects and text or vector layer data are not read (their +presence is reported). There is no writer. + +## Limits + +| Limit | Value | +| --- | --- | +| Canvas or drawable side | 300,000 px (`MAX_SIDE`) | +| Canvas area | 1 GiB at 16 bytes per pixel (`MAX_PIXEL_BYTES`) | +| Decoded pixels | the caller's budget, charged 4 bytes a sample before allocation | +| Layers | 8192 (`MAX_LAYERS`); channels 1024 (`MAX_CHANNELS`) | +| Layer effects on one layer | 1024 | +| Properties in one list | 4096; a payload 64 MiB (`MAX_PAYLOAD`) | +| A string | 1 MiB (`MAX_STRING`) | +| Copied layer and channel names and item paths | 32 MiB in all (`MAX_METADATA_BYTES`) | +| Group nesting in `Image::tree` | 64 (`MAX_DEPTH`) | +| A gzipped file, inflated | 1 GiB (`MAX_INFLATED`) | diff --git a/crates/xcf/src/lib.rs b/crates/xcf/src/lib.rs new file mode 100644 index 000000000..fa226d1f1 --- /dev/null +++ b/crates/xcf/src/lib.rs @@ -0,0 +1,923 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +//! GIMP XCF (`.xcf`, `.xcf.gz`), read. Layout from the public specification +//! (, versions 0 to 26): a header, image +//! properties, then pointers to layer, channel and path structures; each drawable's pixels sit in +//! 64×64 tiles, uncompressed, RLE-coded or zlib-coded. Written from the specification and from +//! files GIMP 3.2 writes; no GIMP code. +//! +//! - [`Xcf::parse`] reads the header, the image properties and every layer's structure into an +//! [`Image`]: no pixel is decoded. +//! - [`Xcf::layer_pixels`], [`Xcf::layer_mask`] and [`Xcf::channel`] decode one drawable each +//! into [`Pixels`] (normalised `f32` samples), charging a byte budget the caller owns. +//! - [`Image::tree`] arranges the layers by their item paths. +//! - [`is_xcf`], [`is_gzipped_xcf`] and [`gunzip`] recognise and inflate files. +//! +//! Layer modes, colour tags and composite settings are kept as the numbers GIMP wrote; what +//! they mean to an application is the application's to decide. + +#[cfg(test)] +mod tests; +mod tiles; + +use std::cell::Cell; +use std::collections::HashMap; +use std::io::Read; + +/// Why an XCF file was not read. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The file is not XCF, is damaged, or exceeds a limit. + #[error("invalid XCF file: {0}")] + Invalid(String), + /// The caller's `keep_going` returned `false`. + #[error("cancelled")] + Cancelled, +} + +pub type Result = std::result::Result; + +fn invalid(message: impl Into) -> Error { + Error::Invalid(message.into()) +} + +const MAGIC: &[u8] = b"gimp xcf "; +/// Canvas and drawable side limit. +pub const MAX_SIDE: u32 = 300_000; +/// The canvas may hold at most this many bytes at 16 bytes per pixel (RGBA `f32`). Also the +/// budget a caller would usually give the decoders. +pub const MAX_PIXEL_BYTES: usize = 1 << 30; +/// Layers and groups in one image. +pub const MAX_LAYERS: usize = 8192; +/// Extra channels in one image. +pub const MAX_CHANNELS: usize = 1024; +/// Group nesting [`Image::tree`] builds. +pub const MAX_DEPTH: usize = 64; +/// Total copied layer and channel metadata (names and item paths). A single reused record with a large +/// name is bounded by [`MAX_STRING`], but repeated layer offsets referencing it must not multiply +/// into unbounded retained metadata. +pub const MAX_METADATA_BYTES: usize = 32 << 20; +/// A string field (layer names, parasite names). +pub const MAX_STRING: usize = 1 << 20; +/// A property payload. +pub const MAX_PAYLOAD: usize = 64 << 20; +/// The largest file a gzipped XCF may inflate to. +pub const MAX_INFLATED: usize = 1 << 30; +/// Highest version this reader was written against; newer files still open (unknown properties +/// are skipped by their length). +pub const KNOWN_VERSION: u32 = 26; +/// The side of a pixel tile. +pub const TILE: u32 = 64; + +/// `true` for an XCF file (by its signature). +pub fn is_xcf(bytes: &[u8]) -> bool { + bytes.starts_with(MAGIC) +} + +/// `true` for a gzip stream named `.xcf.gz` / `.xcfgz` (GIMP writes and reads compressed XCF). +pub fn is_gzipped_xcf(name: &str, bytes: &[u8]) -> bool { + let lower = name.rsplit(['/', '\\']).next().unwrap_or(name).to_ascii_lowercase(); + bytes.starts_with(&[0x1f, 0x8b]) && (lower.ends_with(".xcf.gz") || lower.ends_with(".xcfgz")) +} + +/// Inflates a gzipped XCF, refusing one that inflates past [`MAX_INFLATED`]. +pub fn gunzip(bytes: &[u8]) -> Result> { + let mut out = Vec::new(); + let mut dec = flate2::read::GzDecoder::new(bytes).take(MAX_INFLATED as u64 + 1); + dec.read_to_end(&mut out).map_err(|e| invalid(format!("gzip: {e}")))?; + if out.len() > MAX_INFLATED { + return Err(invalid("the compressed file inflates past the 1 GiB limit")); + } + Ok(out) +} + +/// How samples are stored. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Precision { + /// Bytes per sample: 1, 2, 4 or 8. + pub bytes: usize, + /// Floating point (16, 32 or 64 bit) rather than unsigned integer. + pub float: bool, + /// The values are linear light; otherwise gamma-encoded (the profile's own curve). + pub linear: bool, +} + +impl Precision { + /// 8-bit gamma integer: every file before version 4. + pub const GAMMA8: Precision = Precision { bytes: 1, float: false, linear: false }; + + /// The precision a header's code means in a file of `version` (the codes changed twice). + pub fn from_header(version: u32, code: u32) -> Result { + let p = |bytes, float, linear| Ok(Precision { bytes, float, linear }); + match version { + 0..=3 => Ok(Self::GAMMA8), + 4 => match code { + 0 => p(1, false, false), + 1 => p(2, false, false), + 2 => p(4, false, true), + 3 => p(2, true, true), + 4 => p(4, true, true), + c => Err(invalid(format!("unknown precision {c} in an XCF version 4 file"))), + }, + 5 | 6 => match code { + 100 => p(1, false, true), + 150 => p(1, false, false), + 200 => p(2, false, true), + 250 => p(2, false, false), + 300 => p(4, false, true), + 350 => p(4, false, false), + 400 => p(2, true, true), + 450 => p(2, true, false), + 500 => p(4, true, true), + 550 => p(4, true, false), + c => Err(invalid(format!("unknown precision {c} in an XCF version {version} file"))), + }, + _ => match code { + 100 => p(1, false, true), + 150 => p(1, false, false), + 200 => p(2, false, true), + 250 => p(2, false, false), + 300 => p(4, false, true), + 350 => p(4, false, false), + 500 => p(2, true, true), + 550 => p(2, true, false), + 600 => p(4, true, true), + 650 => p(4, true, false), + 700 => p(8, true, true), + 750 => p(8, true, false), + c => Err(invalid(format!("unknown precision {c}"))), + }, + } + } + + /// One sample, big-endian, as a normalised float (integers map to 0..1). + pub fn sample(self, b: &[u8]) -> f32 { + match (self.float, self.bytes, b) { + (false, 1, [v]) => f32::from(*v) / 255.0, + (false, 2, [a, b]) => f32::from(u16::from_be_bytes([*a, *b])) / 65535.0, + (false, 4, [a, b, c, d]) => (u32::from_be_bytes([*a, *b, *c, *d]) as f64 / f64::from(u32::MAX)) as f32, + (true, 2, [a, b]) => half_to_f32(u16::from_be_bytes([*a, *b])), + (true, 4, [a, b, c, d]) => f32::from_be_bytes([*a, *b, *c, *d]), + (true, 8, [a, b, c, d, e, f, g, h]) => f64::from_be_bytes([*a, *b, *c, *d, *e, *f, *g, *h]) as f32, + _ => 0.0, + } + } +} + +/// An IEEE 754 binary16 value as `f32` (exact: every half is representable). +fn half_to_f32(h: u16) -> f32 { + let sign = u32::from(h >> 15) << 31; + let exp = u32::from(h >> 10) & 0x1f; + let frac = u32::from(h) & 0x3ff; + let bits = match (exp, frac) { + (0, 0) => 0, + (0, f) => { + // Subnormal: shift the leading one up to the implicit position. + let shift = f.leading_zeros() - 21; + ((113 - shift) << 23) | ((f << shift) & 0x3ff) << 13 + } + (31, f) => 0x7f80_0000 | f << 13, + (e, f) => ((e + 112) << 23) | f << 13, + }; + f32::from_bits(sign | bits) +} + +/// How tiles are stored (`PROP_COMPRESSION`). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Compression { + None, + Rle, + Zlib, +} + +/// The image's colour model (the header's base type). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BaseType { + Rgb, + Gray, + /// One colormap index per pixel. [`Pixels`] of an indexed image are expanded to RGB. + Indexed, +} + +/// Big-endian reads with bounds checks; every failure is an error, never a panic. +#[derive(Clone, Copy)] +struct Cursor<'a> { + b: &'a [u8], + pos: usize, + /// XCF 11 and later store pointers as 64-bit. + wide: bool, +} + +impl<'a> Cursor<'a> { + fn at(&self, pos: usize) -> Cursor<'a> { + Cursor { b: self.b, pos, wide: self.wide } + } + + fn take(&mut self, n: usize) -> Result<&'a [u8]> { + let end = self.pos.checked_add(n).ok_or_else(|| invalid("offset overflow"))?; + let s = self.b.get(self.pos..end).ok_or_else(|| invalid(format!("the file ends early (wanted {n} bytes at {})", self.pos)))?; + self.pos = end; + Ok(s) + } + + fn u32(&mut self) -> Result { + Ok(be_u32(self.take(4)?).unwrap_or(0)) + } + + fn ptr(&mut self) -> Result { + if self.wide { + let s = self.take(8)?; + let v = s.first_chunk::<8>().map(|b| u64::from_be_bytes(*b)).unwrap_or(0); + usize::try_from(v).map_err(|_| invalid("pointer does not fit in memory")) + } else { + self.u32().map(|v| v as usize) + } + } + + /// A STRING: `n+1` then `n` bytes and a NUL (0 for the empty string). + fn string(&mut self) -> Result { + Ok(String::from_utf8_lossy(self.string_bytes()?).into_owned()) + } + + /// The same bounded STRING read without allocating or decoding an unused field. + fn string_bytes(&mut self) -> Result<&'a [u8]> { + let n = self.u32()? as usize; + if n > MAX_STRING { + return Err(invalid(format!("a string of {n} bytes exceeds the limit"))); + } + let s = self.take(n)?; + Ok(s.strip_suffix(&[0]).unwrap_or(s)) + } + + fn skip(&mut self, n: usize) -> Result<()> { + self.take(n).map(|_| ()) + } +} + +/// One property record: its type and payload. +struct Prop<'a> { + kind: u32, + data: &'a [u8], +} + +/// Reads a property list, measuring known payloads whose length words GIMP writes incorrectly. +/// Ends at PROP_END (type 0). +fn props<'a>(c: &mut Cursor<'a>) -> Result>> { + let mut out = Vec::new(); + loop { + let kind = c.u32()?; + let len = c.u32()? as usize; + if kind == 0 { + return Ok(out); + } + // PROP_COLORMAP's length word was wrong in some releases (n+4 for 3n+4 bytes); its real + // size follows from the count. PROP_USER_UNIT's length is wrong in every release. + // PROP_LINK_LAYER's length can be four bytes short; read all its fields. + let len = match kind { + 1 => { + let n = c.at(c.pos).u32()? as usize; + 4usize.checked_add(n.checked_mul(3).ok_or_else(|| invalid("colormap overflow"))?).ok_or_else(|| invalid("colormap overflow"))? + } + 24 => user_unit_len(c)?, + 48 => link_layer_len(c)?, + _ => len, + }; + if len > MAX_PAYLOAD { + return Err(invalid(format!("property {kind} declares {len} bytes"))); + } + let data = c.take(len)?; + out.push(Prop { kind, data }); + if out.len() > 4096 { + return Err(invalid("too many properties")); + } + } +} + +/// A PROP_LINK_LAYER payload: flags, a path STRING, then the source width and height. +/// The dimensions are present for raster links too, where their values are unused. +fn link_layer_len(c: &Cursor<'_>) -> Result { + let mut p = c.at(c.pos); + p.skip(4)?; + p.string_bytes()?; + p.skip(8)?; + Ok(p.pos - c.pos) +} + +/// The real size of a PROP_USER_UNIT payload: a float, a count and three (XCF 21+) or five +/// strings. Measured without consuming. +fn user_unit_len(c: &Cursor<'_>) -> Result { + let mut p = c.at(c.pos); + p.skip(8)?; + for _ in 0..3 { + p.string_bytes()?; + } + // The two name forms of older files, when present: another string pair before the next + // property. Decide by whether a plausible property header follows. + let three = p.pos - c.pos; + if p.at(p.pos).u32().map(|k| k <= 64).unwrap_or(false) { + return Ok(three); + } + p.string_bytes()?; + p.string_bytes()?; + Ok(p.pos - c.pos) +} + +fn be_u32(b: &[u8]) -> Option { + b.first_chunk::<4>().map(|s| u32::from_be_bytes(*s)) +} + +fn be_i32(b: &[u8]) -> Option { + be_u32(b).map(|v| v as i32) +} + +fn be_f32(b: &[u8]) -> Option { + be_u32(b).map(f32::from_bits) +} + +/// The parasites of a PROP_PARASITES payload as (name, data), in first-seen order; a later +/// duplicate's data wins. A record cut short ends the list. +fn parasites(data: &[u8]) -> Vec<(String, &[u8])> { + let mut c = Cursor { b: data, pos: 0, wide: false }; + let mut out: Vec<(String, &[u8])> = Vec::new(); + // Name to output slot, so a payload of many distinct names is not rescanned per record. + let mut index: HashMap = HashMap::new(); + while c.pos < data.len() { + let Ok(name) = c.string() else { break }; + let (Ok(_flags), Ok(len)) = (c.u32(), c.u32()) else { break }; + let Ok(payload) = c.take(len as usize) else { break }; + match index.get(&name).and_then(|&i| out.get_mut(i)) { + Some(slot) => slot.1 = payload, + None => { + index.insert(name.clone(), out.len()); + out.push((name, payload)); + } + } + } + out +} + +/// A guide's direction. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Orientation { + /// A horizontal line: `position` is a y coordinate. + Horizontal, + /// A vertical line: `position` is an x coordinate. + Vertical, +} + +/// A guide (`PROP_GUIDES`). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Guide { + pub position: i32, + pub orientation: Orientation, +} + +/// A layer structure, with its properties as GIMP wrote them. +#[derive(Debug, Clone, PartialEq)] +pub struct Layer { + /// The size its pixels are declared to have. + pub width: u32, + pub height: u32, + /// The layer type: 0 RGB, 1 RGBA, 2 gray, 3 gray + alpha, 4 indexed, 5 indexed + alpha. + pub layer_type: u32, + pub name: String, + /// `PROP_FLOATING_SELECTION`: the layer is a floating selection, not part of the stack. + pub floating_selection: bool, + /// 0 to 1: `PROP_FLOAT_OPACITY` when present (it wins whatever the order), else + /// `PROP_OPACITY` / 255. + pub opacity: f32, + /// `PROP_MODE`: GIMP's layer mode number (0 to 22 legacy, 23 and up since 2.10; 61 is + /// pass-through). + pub mode: u32, + pub visible: bool, + /// `PROP_LOCK_ALPHA`. + pub lock_alpha: bool, + /// `PROP_LOCK_CONTENT`. + pub lock_content: bool, + /// `PROP_LOCK_POSITION`. + pub lock_position: bool, + /// `PROP_APPLY_MASK`: the layer mask is in effect. `true` when the property is absent. + pub apply_mask: bool, + /// `PROP_OFFSETS`: where the layer's (and its mask's) top-left pixel sits on the canvas. + pub x: i32, + pub y: i32, + /// `PROP_TEXT_LAYER_FLAGS` or a `gimp-text-layer` parasite: a text layer (its pixels are the + /// rendered text). + pub text: bool, + /// `PROP_VECTOR_LAYER`. + pub vector: bool, + /// `PROP_LINK_LAYER`. + pub link: bool, + /// `PROP_GROUP_ITEM`: a group; its members follow it with longer item paths. + pub group: bool, + /// `PROP_GROUP_ITEM_FLAGS` bit 0. `true` when the property is absent. + pub expanded: bool, + /// `PROP_ITEM_PATH`: the layer's index at each level of the tree; empty at the top level of + /// a file without groups. See [`Image::tree`]. + pub item_path: Vec, + /// `PROP_COLOR_TAG`: 0 none, 1 blue, 2 green, 3 yellow, 4 orange, 5 brown, 6 red, 7 violet, + /// 8 gray. + pub color_tag: u32, + /// `PROP_COMPOSITE_MODE`: 0 or ±1 union, ±2 clip to backdrop, ±3 clip to layer, + /// ±4 intersection (negative: GIMP's automatic choice). + pub composite_mode: i32, + /// `PROP_COMPOSITE_SPACE`: ±1 linear, ±2 perceptual. + pub composite_space: i32, + /// `PROP_BLEND_SPACE`: ±1 linear, ±2 perceptual. + pub blend_space: i32, + /// The layer has a `gimphoto-clipped` parasite (a clipping-mask convention). + pub clipped: bool, + /// Non-destructive layer effects (version 20 and later); they are not read. + pub effects: usize, + hierarchy: usize, + mask: usize, +} + +impl Layer { + /// Does the layer type carry an alpha channel? + pub fn has_alpha(&self) -> bool { + matches!(self.layer_type, 1 | 3 | 5) + } + /// Does the layer point at pixels? Groups usually do not. + pub fn has_pixels(&self) -> bool { + self.hierarchy != 0 + } + /// Does the layer have a mask? + pub fn has_mask(&self) -> bool { + self.mask != 0 + } +} + +/// A channel structure: a layer mask, the selection or an extra channel. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Channel { + pub name: String, + /// `PROP_SELECTION`: this channel is the image's selection. + pub selection: bool, +} + +/// Pixel data of a drawable: `channels` normalised samples per pixel, row-major. A layer has 1 +/// (gray), 2 (gray, alpha), 3 (RGB) or 4 (RGBA) channels; a channel has 1. Indexed pixels are +/// expanded to RGB through the colormap. +#[derive(Debug, Clone, PartialEq)] +pub struct Pixels { + pub width: u32, + pub height: u32, + pub channels: usize, + pub data: Vec, +} + +/// A layer in [`Image::tree`]. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct TreeNode { + /// Index into [`Image::layers`]. + pub layer: usize, + /// The group's members, topmost first. + pub children: Vec, +} + +/// The layers arranged by their item paths. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct Tree { + /// The top level, topmost first. + pub roots: Vec, + /// Layers (by index) nested deeper than [`MAX_DEPTH`], moved to the top level. + pub too_deep: Vec, + /// Layers (by index) whose item path leads to no group, put at the top level. + pub misplaced: Vec, +} + +/// The header and structure of an XCF file. +#[derive(Debug, Clone, PartialEq)] +pub struct Image { + /// The file version: 0 to [`KNOWN_VERSION`], or newer. + pub version: u32, + /// Canvas size, 1 to [`MAX_SIDE`]. + pub width: u32, + pub height: u32, + pub base_type: BaseType, + pub precision: Precision, + pub compression: Compression, + /// `PROP_COLORMAP`: at most 256 RGB entries. + pub colormap: Vec<[u8; 3]>, + /// `PROP_RESOLUTION`: horizontal and vertical pixels per inch, as stored. + pub resolution: Option<(f32, f32)>, + /// `PROP_GUIDES`. Files before version 15 could hold stray negative guides; those are + /// left out, as are guides of an unknown orientation. + pub guides: Vec, + /// The `icc-profile` parasite, when not empty. + pub icc_profile: Option>, + /// The `gimp-comment` parasite. + pub comment: Option, + /// Paths the file holds (they are not read). + pub path_count: usize, + /// Every layer structure in file order, topmost first, floating selections included. + pub layers: Vec, + /// The number of channel structures (the selection and the extras); read each with + /// [`Xcf::channel`]. + pub channel_count: usize, +} + +impl Image { + /// Arranges the layers by their item paths, skipping floating selections. A layer without a + /// path sits at the top level in file order; one whose path leads to no group (at any + /// depth) is put at the top level, so later siblings keep their indices. + pub fn tree(&self) -> Tree { + let mut tree = Tree::default(); + let stack = self.layers.iter().enumerate().filter(|(_, l)| !l.floating_selection); + for (position, (index, layer)) in stack.enumerate() { + // Default path: the item's position at the top level, as a file without groups has it. + let default = [position as u32]; + let mut path: &[u32] = if layer.item_path.is_empty() { &default } else { &layer.item_path }; + if path.len() > MAX_DEPTH { + tree.too_deep.push(index); + path = path.get(..1).unwrap_or(path); + } + let (parents, _) = path.split_at(path.len().saturating_sub(1)); + let mut probe = &tree.roots; + let reachable = parents.iter().all(|&i| match probe.get(i as usize) { + Some(n) if self.layers.get(n.layer).is_some_and(|l| l.group) => { + probe = &n.children; + true + } + _ => false, + }); + let group = if reachable { parents } else { &[] }; + if !reachable { + tree.misplaced.push(index); + } + if let Some(level) = group.iter().try_fold(&mut tree.roots, |level, &i| level.get_mut(i as usize).map(|n| &mut n.children)) { + level.push(TreeNode { layer: index, children: Vec::new() }); + } + } + tree + } +} + +/// A parsed XCF file: its [`Image`] and the pixels behind it. +pub struct Xcf<'a> { + c: Cursor<'a>, + image: Image, + channels: Vec, + /// What is left of [`MAX_METADATA_BYTES`] after the layers; channel names are charged too. + meta_budget: Cell, +} + +impl<'a> Xcf<'a> { + /// Reads the header, the image properties and every layer structure of an XCF file + /// (already inflated when it was gzipped). + pub fn parse(bytes: &'a [u8]) -> Result> { + if !is_xcf(bytes) { + return Err(invalid("not an XCF file")); + } + let mut c = Cursor { b: bytes, pos: MAGIC.len(), wide: false }; + let version = match c.take(5)? { + b"file\0" => 0, + [b'v', a, b, d, 0] if a.is_ascii_digit() && b.is_ascii_digit() && d.is_ascii_digit() => { + u32::from(a - b'0') * 100 + u32::from(b - b'0') * 10 + u32::from(d - b'0') + } + _ => return Err(invalid("unknown XCF version tag")), + }; + c.wide = version >= 11; + let (width, height, base_type) = (c.u32()?, c.u32()?, c.u32()?); + if width == 0 || height == 0 || width > MAX_SIDE || height > MAX_SIDE { + return Err(invalid(format!("canvas {width}x{height} must be 1 to {MAX_SIDE} pixels a side"))); + } + if (width as usize).saturating_mul(height as usize).saturating_mul(16) > MAX_PIXEL_BYTES { + return Err(invalid("the canvas exceeds the 1 GiB pixel limit")); + } + let precision = if version >= 4 { Precision::from_header(version, c.u32()?)? } else { Precision::GAMMA8 }; + let base_type = match base_type { + 0 => BaseType::Rgb, + 1 => BaseType::Gray, + 2 => BaseType::Indexed, + t => return Err(invalid(format!("unknown image type {t}"))), + }; + let mut image = Image { + version, + width, + height, + base_type, + precision, + compression: Compression::None, + colormap: Vec::new(), + resolution: None, + guides: Vec::new(), + icc_profile: None, + comment: None, + path_count: 0, + layers: Vec::new(), + channel_count: 0, + }; + image_props(&mut c, &mut image)?; + + let pointers = |c: &mut Cursor<'_>, max: usize, what: &str| -> Result> { + let mut out = Vec::new(); + loop { + let p = c.ptr()?; + if p == 0 { + return Ok(out); + } + out.push(p); + if out.len() > max { + return Err(invalid(format!("more than {max} {what}"))); + } + } + }; + let layer_ptrs = pointers(&mut c, MAX_LAYERS, "layers")?; + let channels = pointers(&mut c, MAX_CHANNELS, "channels")?; + if version >= 18 { + let mut n = 0; + while c.ptr().ok().is_some_and(|p| p != 0) { + n += 1; + if n > MAX_CHANNELS { + break; + } + } + image.path_count += n; + } + image.channel_count = channels.len(); + let c = c.at(0); + let mut meta_budget = MAX_METADATA_BYTES; + for ptr in layer_ptrs { + let layer = layer(c.at(ptr), &image, &mut meta_budget)?; + image.layers.push(layer); + } + Ok(Xcf { c, image, channels, meta_budget: Cell::new(meta_budget) }) + } + + /// The file's header and structure. + pub fn image(&self) -> &Image { + &self.image + } + + /// Decodes the pixels of layer `index` of [`Image::layers`]; `None` for a layer that has + /// none (a group). The decoded samples (4 bytes each) are taken from `budget` before + /// anything is allocated. `keep_going` is asked every 64 tiles. + pub fn layer_pixels(&self, index: usize, budget: &mut usize, keep_going: &dyn Fn() -> bool) -> Result> { + let layer = self.image.layers.get(index).ok_or_else(|| invalid("no such layer"))?; + if layer.hierarchy == 0 { + return Ok(None); + } + let color = if self.image.base_type == BaseType::Rgb { 3 } else { 1 }; + let source_channels = color + usize::from(layer.has_alpha()); + let indexed = self.image.base_type == BaseType::Indexed; + let px = self.hierarchy(layer.hierarchy, source_channels, indexed, budget, keep_going)?; + if (px.width, px.height) != (layer.width, layer.height) { + let (name, lw, lh) = (&layer.name, layer.width, layer.height); + return Err(invalid(format!("layer '{name}' pixels are {}x{} where {lw}x{lh} were declared", px.width, px.height))); + } + Ok(Some(px)) + } + + /// Decodes the mask of layer `index`: one channel, placed at the layer's offset. `None` + /// for a layer without one. + pub fn layer_mask(&self, index: usize, budget: &mut usize, keep_going: &dyn Fn() -> bool) -> Result> { + let layer = self.image.layers.get(index).ok_or_else(|| invalid("no such layer"))?; + if layer.mask == 0 { + return Ok(None); + } + self.channel_at(layer.mask, budget, keep_going).map(Some) + } + + /// Decodes channel `index` (below [`Image::channel_count`]): the selection or an extra + /// channel, the size of the canvas. + pub fn channel(&self, index: usize, budget: &mut usize, keep_going: &dyn Fn() -> bool) -> Result<(Channel, Pixels)> { + let ptr = *self.channels.get(index).ok_or_else(|| invalid("no such channel"))?; + self.channel_at(ptr, budget, keep_going) + } + + fn channel_at(&self, ptr: usize, budget: &mut usize, keep_going: &dyn Fn() -> bool) -> Result<(Channel, Pixels)> { + let mut c = self.c.at(ptr); + let (_w, _h) = (c.u32()?, c.u32()?); + let name = c.string()?; + // As for layers: repeated channel pointers must not retain unbounded copies of one name. + self.meta_budget.set(self.meta_budget.get().checked_sub(name.len()).ok_or_else(|| invalid("channel metadata exceeds the budget"))?); + let selection = props(&mut c)?.iter().any(|p| p.kind == 4); + let hptr = c.ptr()?; + // Channels are always one plain sample per pixel, even in indexed images. + let px = self.hierarchy(hptr, 1, false, budget, keep_going)?; + Ok((Channel { name, selection }, px)) + } + + /// Reads a hierarchy: the first level's tiles into floats, `source_channels` as stored + /// (indexed pixels expand to RGB here). + fn hierarchy(&self, hptr: usize, source_channels: usize, indexed: bool, budget: &mut usize, keep_going: &dyn Fn() -> bool) -> Result { + let precision = self.image.precision; + let mut c = self.c.at(hptr); + let (w, h, bpp) = (c.u32()?, c.u32()?, c.u32()? as usize); + if w == 0 || h == 0 || w > MAX_SIDE || h > MAX_SIDE { + return Err(invalid(format!("drawable size {w}x{h} is out of range"))); + } + let expected_bpp = source_channels.checked_mul(precision.bytes).ok_or_else(|| invalid("bpp overflow"))?; + if bpp != expected_bpp { + return Err(invalid(format!("{bpp} bytes per pixel where {expected_bpp} were expected"))); + } + let channels = if indexed { source_channels + 2 } else { source_channels }; + let count = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(channels)).ok_or_else(|| invalid("drawable overflow"))?; + let bytes = count.checked_mul(4).ok_or_else(|| invalid("drawable overflow"))?; + *budget = budget.checked_sub(bytes).ok_or_else(|| invalid("the layers exceed the pixel limit"))?; + let lptr = c.ptr()?; + let mut l = self.c.at(lptr); + if (l.u32()?, l.u32()?) != (w, h) { + return Err(invalid("the level does not match its hierarchy")); + } + let across = w.div_ceil(TILE) as usize; + let down = h.div_ceil(TILE) as usize; + let mut tiles = Vec::with_capacity(across * down); + for _ in 0..across * down { + let p = l.ptr()?; + if p == 0 { + return Err(invalid("the tile list ends before every tile")); + } + tiles.push(p); + } + let mut data = vec![0.0f32; count]; + let stride = w as usize * channels; + let side = TILE as usize; + for (i, &at) in tiles.iter().enumerate() { + if i % 64 == 0 && !keep_going() { + return Err(Error::Cancelled); + } + let (tx, ty) = (i % across, i / across); + let (tw, th) = ((w as usize - tx * side).min(side), (h as usize - ty * side).min(side)); + let end = tiles.get(i + 1).copied().filter(|&n| n > at).unwrap_or(self.c.b.len()); + let input = self.c.b.get(at..end.min(self.c.b.len())).ok_or_else(|| invalid("a tile pointer is outside the file"))?; + let raw = tiles::decode(input, self.image.compression, bpp, tw * th)?; + for (py, row) in raw.chunks_exact(tw * bpp).enumerate() { + let start = (ty * side + py) * stride + tx * side * channels; + let Some(out) = data.get_mut(start..start + tw * channels) else { continue }; + for (pixel, dst) in row.chunks_exact(bpp).zip(out.chunks_exact_mut(channels)) { + if indexed { + let idx = pixel.first().copied().unwrap_or(0); + let rgb = self.image.colormap.get(usize::from(idx)).copied().unwrap_or([idx; 3]); + let alpha = pixel.get(1).copied(); + for (d, v) in dst.iter_mut().zip(rgb.into_iter().chain(alpha)) { + *d = f32::from(v) / 255.0; + } + } else { + for (d, s) in dst.iter_mut().zip(pixel.chunks_exact(precision.bytes)) { + *d = precision.sample(s); + } + } + } + } + } + Ok(Pixels { width: w, height: h, channels, data }) + } +} + +fn image_props(c: &mut Cursor<'_>, image: &mut Image) -> Result<()> { + for p in &props(c)? { + match p.kind { + 1 => { + let n = be_u32(p.data).unwrap_or(0) as usize; + image.colormap = p.data.get(4..).unwrap_or(&[]).as_chunks::<3>().0.iter().take(n.min(256)).copied().collect(); + } + 17 => { + image.compression = match p.data.first() { + Some(0) => Compression::None, + Some(1) => Compression::Rle, + Some(2) => Compression::Zlib, + Some(k) => return Err(invalid(format!("unknown compression {k}"))), + None => return Err(invalid("empty compression property")), + } + } + 18 => { + for g in p.data.as_chunks::<5>().0 { + let position = i32::from_be_bytes([g[0], g[1], g[2], g[3]]); + let orientation = match g[4] { + 1 => Orientation::Horizontal, + 2 => Orientation::Vertical, + _ => continue, + }; + if position >= 0 || image.version >= 15 { + image.guides.push(Guide { position, orientation }); + } + } + } + 19 => { + if let (Some(x), Some(y)) = (be_f32(p.data), p.data.get(4..).and_then(be_f32)) { + image.resolution = Some((x, y)); + } + } + 21 => { + for (name, data) in parasites(p.data) { + match name.as_str() { + "icc-profile" if !data.is_empty() => image.icc_profile = Some(data.to_vec()), + "gimp-comment" => image.comment = Some(String::from_utf8_lossy(data).trim_end_matches('\0').to_string()), + _ => {} + } + } + } + 23 => image.path_count += be_u32(p.data.get(4..).unwrap_or(&[])).unwrap_or(0) as usize, + 25 => image.path_count += be_u32(p.data.get(8..).unwrap_or(&[])).unwrap_or(0) as usize, + _ => {} + } + } + Ok(()) +} + +/// Reads a layer structure at `c`. +fn layer(mut c: Cursor<'_>, image: &Image, meta_budget: &mut usize) -> Result { + let (width, height, layer_type) = (c.u32()?, c.u32()?, c.u32()?); + let name = c.string()?; + // Charge the owned name against the image-wide metadata budget: repeated layer offsets + // referencing one large name must not retain unbounded copies. + *meta_budget = meta_budget.checked_sub(name.len()).ok_or_else(|| invalid("layer metadata exceeds the budget"))?; + let lprops = props(&mut c)?; + let hierarchy = c.ptr()?; + let mask = c.ptr()?; + let mut effects = 0usize; + if image.version >= 20 { + while c.ptr()? != 0 { + effects += 1; + if effects > 1024 { + return Err(invalid("too many layer effects")); + } + } + } + let type_rgb = match layer_type { + 0 | 1 => true, + 2..=5 => false, + k => return Err(invalid(format!("unknown layer type {k} on '{name}'"))), + }; + if type_rgb != (image.base_type != BaseType::Gray) && image.base_type != BaseType::Indexed { + return Err(invalid(format!("layer '{name}' does not match the image's colour model"))); + } + let mut l = Layer { + width, + height, + layer_type, + name, + floating_selection: false, + opacity: 1.0, + mode: 0, + visible: true, + lock_alpha: false, + lock_content: false, + lock_position: false, + apply_mask: true, + x: 0, + y: 0, + text: false, + vector: false, + link: false, + group: false, + expanded: true, + item_path: Vec::new(), + color_tag: 0, + composite_mode: 0, + composite_space: 0, + blend_space: 0, + clipped: false, + effects, + hierarchy, + mask, + }; + // PROP_FLOAT_OPACITY wins over PROP_OPACITY whatever their order in the file. + let mut float_opacity = None; + for p in &lprops { + let v = be_u32(p.data).unwrap_or(0); + match p.kind { + 5 => l.floating_selection = true, + 6 => l.opacity = (v.min(255) as f32) / 255.0, + 33 => float_opacity = be_f32(p.data).filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0)).or(float_opacity), + 7 => l.mode = v, + 8 => l.visible = v != 0, + 10 => l.lock_alpha = v != 0, + 11 => l.apply_mask = v != 0, + 15 => { + l.x = be_i32(p.data).unwrap_or(0); + l.y = p.data.get(4..).and_then(be_i32).unwrap_or(0); + } + 26 => l.text = true, + 28 => l.lock_content = v != 0, + 29 => l.group = true, + 30 => { + l.item_path = p.data.as_chunks::<4>().0.iter().map(|s| u32::from_be_bytes(*s)).collect(); + *meta_budget = + meta_budget.checked_sub(l.item_path.len().saturating_mul(4)).ok_or_else(|| invalid("layer metadata exceeds the budget"))?; + } + 31 => l.expanded = v & 1 != 0, + 32 => l.lock_position = v != 0, + 34 => l.color_tag = v, + 35 => l.composite_mode = be_i32(p.data).unwrap_or(0), + 36 => l.composite_space = be_i32(p.data).unwrap_or(0), + 37 => l.blend_space = be_i32(p.data).unwrap_or(0), + 47 => l.vector = true, + 48 => l.link = true, + 21 => { + for (pname, _) in parasites(p.data) { + match pname.as_str() { + "gimphoto-clipped" => l.clipped = true, + "gimp-text-layer" => l.text = true, + _ => {} + } + } + } + _ => {} + } + } + l.opacity = float_opacity.unwrap_or(l.opacity); + Ok(l) +} diff --git a/crates/xcf/src/tests.rs b/crates/xcf/src/tests.rs new file mode 100644 index 000000000..5ec45620c --- /dev/null +++ b/crates/xcf/src/tests.rs @@ -0,0 +1,1120 @@ +//! Synthetic XCF files (written here, from the specification) for the reader: structure, +//! every compression, precisions, groups, masks, channels, limits and malformed input. + +use super::tiles::rle_decode; +use super::*; + +/// A small XCF writer for tests: version 11+ layout (64-bit pointers) by default. +pub(super) struct W { + pub out: Vec, + pub wide: bool, +} + +impl W { + pub fn new(version: u32) -> Self { + let mut w = W { out: Vec::new(), wide: version >= 11 }; + w.out.extend_from_slice(b"gimp xcf "); + if version == 0 { + w.out.extend_from_slice(b"file\0"); + } else { + w.out.extend_from_slice(format!("v{version:03}\0").as_bytes()); + } + w + } + pub fn u32(&mut self, v: u32) { + self.out.extend_from_slice(&v.to_be_bytes()); + } + pub fn ptr(&mut self, v: usize) { + if self.wide { + self.out.extend_from_slice(&(v as u64).to_be_bytes()); + } else { + self.u32(v as u32); + } + } + pub fn string(&mut self, s: &str) { + if s.is_empty() { + self.u32(0); + } else { + self.u32(s.len() as u32 + 1); + self.out.extend_from_slice(s.as_bytes()); + self.out.push(0); + } + } + pub fn prop(&mut self, kind: u32, payload: &[u8]) { + self.u32(kind); + self.u32(payload.len() as u32); + self.out.extend_from_slice(payload); + } + pub fn prop_u32(&mut self, kind: u32, v: u32) { + self.prop(kind, &v.to_be_bytes()); + } + pub fn end(&mut self) { + self.u32(0); + self.u32(0); + } + /// Reserves a pointer slot, returning where to patch it. + pub fn slot(&mut self) -> usize { + let at = self.out.len(); + self.ptr(0); + at + } + pub fn patch(&mut self, slot: usize, target: usize) { + if self.wide { + self.out[slot..slot + 8].copy_from_slice(&(target as u64).to_be_bytes()); + } else { + self.out[slot..slot + 4].copy_from_slice(&(target as u32).to_be_bytes()); + } + } +} + +/// RLE-encodes one tile of `bpp`-byte pixels, plane by plane, with runs and literals. +pub(super) fn rle(pixels: &[u8], bpp: usize) -> Vec { + let n = pixels.len() / bpp; + let mut out = Vec::new(); + for b in 0..bpp { + let plane: Vec = (0..n).map(|i| pixels[i * bpp + b]).collect(); + let mut i = 0; + while i < n { + let v = plane[i]; + let mut run = 1; + while i + run < n && plane[i + run] == v && run < 65535 { + run += 1; + } + if run >= 2 { + if run <= 127 { + out.push((run - 1) as u8); + } else { + out.push(127); + out.push((run / 256) as u8); + out.push((run % 256) as u8); + } + out.push(v); + i += run; + } else { + let start = i; + while i < n && (i + 1 >= n || plane[i + 1] != plane[i]) && i - start < 127 { + i += 1; + } + let len = i - start; + out.push((256 - len) as u8); + out.extend_from_slice(&plane[start..i]); + } + } + } + out +} + +/// Writes a hierarchy for `w`×`h` pixels of `bpp` bytes (`data` row-major), returning its offset. +pub(super) fn hierarchy(w: &mut W, width: u32, height: u32, bpp: usize, data: &[u8], compression: Compression) -> usize { + let at = w.out.len(); + w.u32(width); + w.u32(height); + w.u32(bpp as u32); + let level_slot = w.slot(); + w.ptr(0); + let level = w.out.len(); + w.patch(level_slot, level); + w.u32(width); + w.u32(height); + let across = width.div_ceil(TILE) as usize; + let down = height.div_ceil(TILE) as usize; + let slots: Vec = (0..across * down).map(|_| w.slot()).collect(); + w.ptr(0); + for (i, slot) in slots.into_iter().enumerate() { + let (tx, ty) = ((i % across) as u32, (i / across) as u32); + let (tw, th) = ((width - tx * TILE).min(TILE), (height - ty * TILE).min(TILE)); + let mut tile = Vec::new(); + for py in 0..th { + for px in 0..tw { + let src = ((ty * TILE + py) as usize * width as usize + (tx * TILE + px) as usize) * bpp; + tile.extend_from_slice(&data[src..src + bpp]); + } + } + let encoded = match compression { + Compression::None => tile, + Compression::Rle => rle(&tile, bpp), + Compression::Zlib => { + use std::io::Write; + let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + e.write_all(&tile).unwrap(); + e.finish().unwrap() + } + }; + let here = w.out.len(); + w.patch(slot, here); + w.out.extend_from_slice(&encoded); + } + at +} + +/// A layer to write. +pub(super) struct TestLayer { + pub name: &'static str, + pub width: u32, + pub height: u32, + pub kind: u32, + pub x: i32, + pub y: i32, + pub opacity: f32, + pub visible: bool, + pub mode: u32, + pub props: Vec<(u32, Vec)>, + pub data: Vec, + pub mask: Option>, + pub apply_mask: bool, +} + +impl TestLayer { + pub fn rgba(name: &'static str, width: u32, height: u32, data: Vec) -> Self { + TestLayer { + name, + width, + height, + kind: 1, + x: 0, + y: 0, + opacity: 1.0, + visible: true, + mode: 28, + props: Vec::new(), + data, + mask: None, + apply_mask: true, + } + } +} + +pub(super) struct TestImage { + pub version: u32, + pub width: u32, + pub height: u32, + pub base_type: u32, + pub precision: u32, + pub compression: Compression, + pub image_props: Vec<(u32, Vec)>, + pub layers: Vec, + pub channels: Vec<(&'static str, Vec, bool)>, +} + +impl TestImage { + pub fn rgb8(width: u32, height: u32) -> Self { + TestImage { + version: 11, + width, + height, + base_type: 0, + precision: 150, + compression: Compression::Rle, + image_props: Vec::new(), + layers: Vec::new(), + channels: Vec::new(), + } + } + + pub fn write(&self) -> Vec { + let mut w = W::new(self.version); + w.u32(self.width); + w.u32(self.height); + w.u32(self.base_type); + if self.version >= 4 { + w.u32(self.precision); + } + let comp = match self.compression { + Compression::None => 0u8, + Compression::Rle => 1, + Compression::Zlib => 2, + }; + w.prop(17, &[comp]); + for (k, v) in &self.image_props { + w.prop(*k, v); + } + w.end(); + let layer_slots: Vec = self.layers.iter().map(|_| w.slot()).collect(); + w.ptr(0); + let channel_slots: Vec = self.channels.iter().map(|_| w.slot()).collect(); + w.ptr(0); + if self.version >= 18 { + w.ptr(0); + } + let bytes = match self.precision { + 100 | 150 => 1, + 200 | 250 | 500 | 550 => 2, + 300 | 350 | 600 | 650 => 4, + _ => 8, + }; + for (layer, slot) in self.layers.iter().zip(layer_slots) { + let at = w.out.len(); + w.patch(slot, at); + w.u32(layer.width); + w.u32(layer.height); + w.u32(layer.kind); + w.string(layer.name); + w.prop_u32(6, (layer.opacity * 255.0).round() as u32); + w.prop(33, &layer.opacity.to_be_bytes()); + w.prop_u32(8, u32::from(layer.visible)); + w.prop_u32(7, layer.mode); + let mut off = Vec::new(); + off.extend_from_slice(&layer.x.to_be_bytes()); + off.extend_from_slice(&layer.y.to_be_bytes()); + w.prop(15, &off); + if layer.mask.is_some() { + w.prop_u32(11, u32::from(layer.apply_mask)); + } + for (k, v) in &layer.props { + w.prop(*k, v); + } + w.end(); + let hslot = w.slot(); + let mslot = w.slot(); + if self.version >= 20 { + w.ptr(0); + } + let channels = match layer.kind { + 0 => 3, + 1 => 4, + 2 | 4 => 1, + _ => 2, + }; + if !layer.data.is_empty() { + let h = hierarchy(&mut w, layer.width, layer.height, channels * bytes, &layer.data, self.compression); + w.patch(hslot, h); + } + if let Some(mask) = &layer.mask { + let at = w.out.len(); + w.patch(mslot, at); + w.u32(layer.width); + w.u32(layer.height); + w.string("mask"); + w.end(); + let hs = w.slot(); + let h = hierarchy(&mut w, layer.width, layer.height, bytes, mask, self.compression); + w.patch(hs, h); + } + } + for ((name, data, selection), slot) in self.channels.iter().zip(channel_slots) { + let at = w.out.len(); + w.patch(slot, at); + w.u32(self.width); + w.u32(self.height); + w.string(name); + if *selection { + w.prop(4, &[]); + } + w.end(); + let hs = w.slot(); + let h = hierarchy(&mut w, self.width, self.height, bytes, data, self.compression); + w.patch(hs, h); + } + w.out + } +} + +const GO: &dyn Fn() -> bool = &|| true; + +fn open(bytes: &[u8]) -> Result> { + Xcf::parse(bytes) +} + +/// Parses and decodes everything, as an application would. +fn read_all(bytes: &[u8]) -> Result { + let xcf = Xcf::parse(bytes)?; + let mut budget = MAX_PIXEL_BYTES; + let mut samples = 0; + for i in 0..xcf.image().layers.len() { + samples += xcf.layer_pixels(i, &mut budget, GO)?.map_or(0, |p| p.data.len()); + samples += xcf.layer_mask(i, &mut budget, GO)?.map_or(0, |(_, p)| p.data.len()); + } + for i in 0..xcf.image().channel_count { + samples += xcf.channel(i, &mut budget, GO)?.1.data.len(); + } + xcf.image().tree(); + Ok(samples) +} + +fn rgba(w: u32, h: u32, f: impl Fn(u32, u32) -> [u8; 4]) -> Vec { + let f = &f; + (0..h).flat_map(|y| (0..w).flat_map(move |x| f(x, y))).collect() +} + +fn pixels(xcf: &Xcf<'_>, layer: usize) -> Pixels { + xcf.layer_pixels(layer, &mut MAX_PIXEL_BYTES.clone(), GO).unwrap().unwrap() +} + +/// The samples of pixel (x, y), in the drawable's own coordinates. +fn at(px: &Pixels, x: u32, y: u32) -> &[f32] { + let i = (y * px.width + x) as usize * px.channels; + &px.data[i..i + px.channels] +} + +fn path(indices: &[u32]) -> (u32, Vec) { + (30, indices.iter().flat_map(|v| v.to_be_bytes()).collect()) +} + +fn names(image: &Image, nodes: &[TreeNode]) -> Vec { + nodes.iter().map(|n| image.layers[n.layer].name.clone()).collect() +} + +#[test] +fn a_layer_decodes_with_every_compression() { + for compression in [Compression::None, Compression::Rle, Compression::Zlib] { + let mut img = TestImage::rgb8(70, 66); + img.compression = compression; + img.layers.push(TestLayer::rgba("A", 70, 66, rgba(70, 66, |x, y| [x as u8 * 3, y as u8 * 2, (x + y) as u8, 255 - x as u8]))); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + let image = xcf.image(); + assert_eq!((image.width, image.height, image.version, image.compression), (70, 66, 11, compression)); + assert_eq!((image.base_type, image.precision), (BaseType::Rgb, Precision::GAMMA8)); + assert_eq!(image.layers.len(), 1, "{compression:?}"); + let px = pixels(&xcf, 0); + assert_eq!((px.width, px.height, px.channels), (70, 66, 4)); + for (x, y) in [(0, 0), (63, 0), (64, 0), (69, 65), (10, 64)] { + let want = [x as f32 * 3.0, y as f32 * 2.0, (x + y) as f32, 255.0 - x as f32].map(|v| v / 255.0); + assert_eq!(at(&px, x, y), want, "{compression:?} at {x},{y}"); + } + } +} + +#[test] +fn every_truncation_errors_never_panics() { + let mut img = TestImage::rgb8(20, 20); + let mut l = TestLayer::rgba("A", 20, 20, rgba(20, 20, |x, y| [x as u8, y as u8, 0, 255])); + l.mask = Some(vec![9; 400]); + img.layers.push(l); + img.channels.push(("Extra", vec![3; 400], false)); + let bytes = img.write(); + for cut in 0..bytes.len() { + assert!(read_all(&bytes[..cut]).is_err(), "cut at {cut}"); + } + assert_eq!(read_all(&bytes), Ok(1600 + 400 + 400)); +} + +#[test] +fn corrupted_files_never_panic() { + let mut img = TestImage::rgb8(70, 20); + let mut group = TestLayer::rgba("Group", 1, 1, Vec::new()); + group.props = vec![(29, vec![])]; + img.layers.push(group); + let mut l = TestLayer::rgba("A", 70, 20, rgba(70, 20, |x, y| [x as u8, y as u8, 0, 255])); + l.props = vec![path(&[0, 0])]; + l.mask = Some(vec![9; 1400]); + img.layers.push(l); + img.channels.push(("Sel", vec![3; 1400], true)); + for compression in [Compression::None, Compression::Rle, Compression::Zlib] { + img.compression = compression; + let bytes = img.write(); + assert!(read_all(&bytes).is_ok()); + let mut state = 0x9e37_79b9_u32; + for _ in 0..2000 { + let mut b = bytes.clone(); + for _ in 0..3 { + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + let i = state as usize % b.len(); + b[i] ^= (state >> 24) as u8 | 1; + } + let _ = read_all(&b); + } + } +} + +#[test] +fn properties_offsets_opacity_mode_visibility_locks_and_tags() { + let mut img = TestImage::rgb8(30, 20); + let mut l = TestLayer::rgba("Top", 10, 8, rgba(10, 8, |x, y| [x as u8 * 20, y as u8 * 30, 0, 200])); + l.x = -3; + l.y = 15; + l.opacity = 0.4; + l.visible = false; + l.mode = 30; // Multiply + let one = 1u32.to_be_bytes().to_vec(); + l.props = vec![ + (10, one.clone()), + (28, one.clone()), + (32, one.clone()), + (34, 2u32.to_be_bytes().to_vec()), + (35, (-2i32).to_be_bytes().to_vec()), + (36, 1i32.to_be_bytes().to_vec()), + (37, (-1i32).to_be_bytes().to_vec()), + ]; + img.layers.push(l); + img.layers.push(TestLayer::rgba("Bottom", 30, 20, rgba(30, 20, |_, _| [9, 9, 9, 255]))); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + let layers = &xcf.image().layers; + assert_eq!(layers.iter().map(|l| l.name.as_str()).collect::>(), ["Top", "Bottom"], "file order: topmost first"); + let top = &layers[0]; + assert_eq!((top.mode, top.opacity, top.visible, top.x, top.y), (30, 0.4, false, -3, 15)); + assert!(top.lock_alpha && top.lock_content && top.lock_position); + assert_eq!((top.color_tag, top.composite_mode, top.composite_space, top.blend_space), (2, -2, 1, -1)); + assert!(top.has_alpha() && top.has_pixels() && !top.has_mask() && top.apply_mask && top.expanded); + assert!(!(top.group || top.text || top.vector || top.link || top.clipped || top.floating_selection)); + assert_eq!((top.width, top.height, top.layer_type, top.effects), (10, 8, 1, 0)); + // The layer's own pixel (5, 2); the offset is the caller's to apply. + assert_eq!(at(&pixels(&xcf, 0), 5, 2), [100.0 / 255.0, 60.0 / 255.0, 0.0, 200.0 / 255.0]); + let bottom = &layers[1]; + assert_eq!((bottom.mode, bottom.opacity, bottom.visible, bottom.color_tag), (28, 1.0, true, 0)); + assert!(!bottom.lock_alpha && !bottom.lock_content && !bottom.lock_position); +} + +#[test] +fn groups_follow_item_paths() { + // File order: Top, Group (0), child A [1,0], Nested group [1,1], nested child [1,1,0], Bottom. + let mut img = TestImage::rgb8(16, 16); + let px = |v: u8| rgba(4, 4, move |_, _| [v, v, v, 255]); + img.layers.push(TestLayer::rgba("Top", 4, 4, px(1))); + let mut group = TestLayer::rgba("Group", 1, 1, Vec::new()); + group.props = vec![(29, vec![]), (31, 0u32.to_be_bytes().to_vec())]; + img.layers.push(group); + let mut a = TestLayer::rgba("A", 4, 4, px(2)); + a.props = vec![path(&[1, 0])]; + img.layers.push(a); + let mut nested = TestLayer::rgba("Nested", 1, 1, Vec::new()); + nested.props = vec![(29, vec![]), path(&[1, 1])]; + nested.mode = 61; // Pass through + img.layers.push(nested); + let mut b = TestLayer::rgba("B", 4, 4, px(3)); + b.props = vec![path(&[1, 1, 0])]; + img.layers.push(b); + img.layers.push(TestLayer::rgba("Bottom", 16, 16, rgba(16, 16, |_, _| [0, 0, 0, 255]))); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + let image = xcf.image(); + let tree = image.tree(); + assert!(tree.misplaced.is_empty() && tree.too_deep.is_empty()); + assert_eq!(names(image, &tree.roots), ["Top", "Group", "Bottom"]); + let g = &image.layers[tree.roots[1].layer]; + assert!(g.group && !g.expanded && !g.has_pixels() && g.mode == 28); + assert_eq!(xcf.layer_pixels(tree.roots[1].layer, &mut 0, GO), Ok(None), "a group has no pixels and costs nothing"); + let children = &tree.roots[1].children; + assert_eq!(names(image, children), ["A", "Nested"], "topmost first inside the group too"); + assert_eq!((image.layers[children[1].layer].mode, image.layers[children[1].layer].item_path.as_slice()), (61, &[1, 1][..])); + assert_eq!(names(image, &children[1].children), ["B"]); + assert_eq!(at(&pixels(&xcf, children[0].layer), 0, 0)[0], 2.0 / 255.0); +} + +#[test] +fn a_path_into_nothing_lands_at_the_top_level() { + let mut img = TestImage::rgb8(8, 8); + let mut stray = TestLayer::rgba("Stray", 2, 2, rgba(2, 2, |_, _| [5, 5, 5, 255])); + stray.props = vec![path(&[7, 0])]; + img.layers.push(stray); + img.layers.push(TestLayer::rgba("Bottom", 8, 8, rgba(8, 8, |_, _| [0, 0, 0, 255]))); + let bytes = img.write(); + let tree = open(&bytes).unwrap().image().tree(); + assert_eq!((tree.roots.len(), tree.misplaced.as_slice()), (2, &[0][..])); +} + +/// A path that fails below the top level (here at depth 2) still lands at the top level, so the +/// group it would have been pushed into keeps its indices for the siblings placed after it. +#[test] +fn a_path_failing_below_the_top_level_lands_at_the_top_level() { + let mut img = TestImage::rgb8(8, 8); + let px = |v: u8| rgba(2, 2, move |_, _| [v, v, v, 255]); + let mut group = TestLayer::rgba("Group", 1, 1, Vec::new()); + group.props = vec![(29, vec![]), (31, 0u32.to_be_bytes().to_vec())]; + img.layers.push(group); + let mut stray = TestLayer::rgba("Stray", 2, 2, px(5)); + stray.props = vec![path(&[0, 4, 0])]; + img.layers.push(stray); + let mut a = TestLayer::rgba("A", 2, 2, px(6)); + a.props = vec![path(&[0, 0])]; + img.layers.push(a); + img.layers.push(TestLayer::rgba("Bottom", 8, 8, rgba(8, 8, |_, _| [0, 0, 0, 255]))); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + let (image, tree) = (xcf.image(), xcf.image().tree()); + assert_eq!(names(image, &tree.roots), ["Group", "Stray", "Bottom"]); + assert_eq!(names(image, &tree.roots[0].children), ["A"], "the valid sibling placed after the stray one still lands in the group"); + assert_eq!(tree.misplaced, [1]); +} + +#[test] +fn a_path_deeper_than_the_limit_moves_to_the_top_level() { + let mut img = TestImage::rgb8(8, 8); + let mut deep = TestLayer::rgba("Deep", 1, 1, vec![0; 4]); + deep.props = vec![path(&[0; MAX_DEPTH + 1])]; + let mut edge = TestLayer::rgba("Edge", 1, 1, vec![0; 4]); + edge.props = vec![path(&[0; MAX_DEPTH])]; + img.layers.extend([deep, edge]); + let bytes = img.write(); + let tree = open(&bytes).unwrap().image().tree(); + assert_eq!((tree.roots.len(), tree.too_deep.as_slice(), tree.misplaced.as_slice()), (2, &[0][..], &[1][..])); +} + +#[test] +fn masks_are_read_applied_or_not() { + for apply in [true, false] { + let mut img = TestImage::rgb8(20, 20); + let mut l = TestLayer::rgba("Masked", 6, 6, rgba(6, 6, |_, _| [255, 0, 0, 255])); + l.x = 4; + l.y = 5; + l.mask = Some((0..36).map(|i| (i * 7) as u8).collect()); + l.apply_mask = apply; + img.layers.push(l); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + let layer = &xcf.image().layers[0]; + assert!(layer.has_mask()); + assert_eq!((layer.apply_mask, layer.x, layer.y), (apply, 4, 5)); + let (channel, px) = xcf.layer_mask(0, &mut MAX_PIXEL_BYTES.clone(), GO).unwrap().unwrap(); + assert_eq!((channel, px.width, px.height, px.channels), (Channel { name: "mask".into(), selection: false }, 6, 6, 1)); + assert_eq!(at(&px, 3, 2), [(2 * 6 + 3) as f32 * 7.0 / 255.0]); + } +} + +#[test] +fn sixteen_bit_and_float_samples_are_big_endian() { + // 16-bit gamma integer (250): one 2x1 gray+alpha layer. + let mut img = TestImage::rgb8(2, 1); + img.base_type = 1; + img.precision = 250; + let data: Vec = [0x1234u16, 0xffff, 0x8000, 0x0001].into_iter().flat_map(u16::to_be_bytes).collect(); + let mut l = TestLayer::rgba("G", 2, 1, data); + l.kind = 3; // gray with alpha + img.layers.push(l); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + assert_eq!((xcf.image().base_type, xcf.image().precision), (BaseType::Gray, Precision { bytes: 2, float: false, linear: false })); + let px = pixels(&xcf, 0); + assert_eq!(px.data, [0x1234 as f32 / 65535.0, 1.0, 0x8000 as f32 / 65535.0, 1.0 / 65535.0]); + + // 32-bit float linear (600): RGB without alpha, values above 1 kept. + let mut img = TestImage::rgb8(1, 1); + img.precision = 600; + let mut l = TestLayer::rgba("F", 1, 1, [0.25f32, 1.5, -0.5].into_iter().flat_map(f32::to_be_bytes).collect()); + l.kind = 0; + img.layers.push(l); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + assert_eq!(xcf.image().precision, Precision { bytes: 4, float: true, linear: true }); + assert!(!xcf.image().layers[0].has_alpha()); + let px = pixels(&xcf, 0); + assert_eq!((px.channels, px.data.as_slice()), (3, &[0.25, 1.5, -0.5][..])); + + // 16-bit float (500), 64-bit float (700) and 32-bit integer (350) decode too. + for (precision, bytes, want) in + [(500u32, 0x3a00u16.to_be_bytes().to_vec(), 0.75), (700, 0.75f64.to_be_bytes().to_vec(), 0.75), (350, u32::MAX.to_be_bytes().to_vec(), 1.0)] + { + let mut img = TestImage::rgb8(1, 1); + img.base_type = 1; + img.precision = precision; + let mut l = TestLayer::rgba("F", 1, 1, bytes); + l.kind = 2; + img.layers.push(l); + let bytes = img.write(); + assert_eq!(pixels(&open(&bytes).unwrap(), 0).data, [want], "precision {precision}"); + } +} + +#[test] +fn half_floats_convert_exactly() { + for (bits, want) in [ + (0x0000u16, 0.0f32), + (0x3c00, 1.0), + (0xc000, -2.0), + (0x3555, (1024.0 + 341.0) / 4096.0), + (0x7bff, 65504.0), + (0x0400, 2f32.powi(-14)), + (0x0001, 2f32.powi(-24)), + (0x03ff, 1023.0 * 2f32.powi(-24)), + (0x7c00, f32::INFINITY), + (0xfc00, f32::NEG_INFINITY), + ] { + assert_eq!(half_to_f32(bits), want, "{bits:#06x}"); + } + assert!(half_to_f32(0x8000).is_sign_negative() && half_to_f32(0x8000) == 0.0); + assert!(half_to_f32(0x7e00).is_nan()); +} + +#[test] +fn precision_codes_depend_on_the_version() { + let p = |bytes, float, linear| Precision { bytes, float, linear }; + assert_eq!(Precision::from_header(3, 999), Ok(Precision::GAMMA8)); + assert_eq!(Precision::from_header(4, 3), Ok(p(2, true, true))); + assert_eq!(Precision::from_header(5, 500), Ok(p(4, true, true))); + assert_eq!(Precision::from_header(6, 450), Ok(p(2, true, false))); + assert_eq!(Precision::from_header(7, 500), Ok(p(2, true, true))); + assert_eq!(Precision::from_header(12, 750), Ok(p(8, true, false))); + for (version, code) in [(4, 5), (5, 600), (7, 400), (12, 0)] { + assert!(Precision::from_header(version, code).is_err(), "{version} {code}"); + } +} + +#[test] +fn indexed_images_use_the_colormap() { + let mut img = TestImage::rgb8(2, 1); + img.base_type = 2; + let mut cmap = 3u32.to_be_bytes().to_vec(); + cmap.extend_from_slice(&[255, 0, 0, 0, 255, 0, 0, 0, 255]); + img.image_props.push((1, cmap)); + let mut l = TestLayer::rgba("I", 2, 1, vec![2, 255, 1, 128]); + l.kind = 5; // indexed with alpha + img.layers.push(l); + let mut plain = TestLayer::rgba("P", 2, 1, vec![0, 9]); + plain.kind = 4; // indexed, no alpha; index 9 is past the colormap + img.layers.push(plain); + img.channels.push(("C", vec![255, 0], false)); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + assert_eq!((xcf.image().base_type, xcf.image().colormap.len()), (BaseType::Indexed, 3)); + assert_eq!(pixels(&xcf, 0).data, [0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 128.0 / 255.0]); + let p = pixels(&xcf, 1); + assert_eq!((p.channels, p.data.as_slice()), (3, &[1.0, 0.0, 0.0, 9.0 / 255.0, 9.0 / 255.0, 9.0 / 255.0][..])); + // Channels are plain samples even in an indexed image. + assert_eq!(xcf.channel(0, &mut MAX_PIXEL_BYTES.clone(), GO).unwrap().1.data, [1.0, 0.0]); +} + +#[test] +fn channels_are_the_extras_and_the_selection() { + let mut img = TestImage::rgb8(4, 4); + img.layers.push(TestLayer::rgba("L", 4, 4, rgba(4, 4, |_, _| [0, 0, 0, 255]))); + img.channels.push(("Extra", (0..16).map(|i| i as u8 * 16).collect(), false)); + img.channels.push(("Selection Mask", vec![255; 16], true)); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + assert_eq!(xcf.image().channel_count, 2); + let mut budget = 200; + let (extra, px) = xcf.channel(0, &mut budget, GO).unwrap(); + assert_eq!((extra, at(&px, 3, 1)), (Channel { name: "Extra".into(), selection: false }, &[7.0 * 16.0 / 255.0][..])); + let (selection, px) = xcf.channel(1, &mut budget, GO).unwrap(); + assert_eq!((selection.selection, px.data[0], budget), (true, 1.0, 200 - 2 * 64)); + assert!(xcf.channel(2, &mut budget, GO).is_err()); + assert!(xcf.layer_pixels(1, &mut budget, GO).is_err() && xcf.layer_mask(1, &mut budget, GO).is_err()); +} + +#[test] +fn image_parasites_resolution_guides_and_comment() { + let mut img = TestImage::rgb8(10, 10); + img.image_props.push((19, [300.0f32, 299.0].into_iter().flat_map(f32::to_be_bytes).collect())); + let mut guides = Vec::new(); + for (position, orientation) in [(4i32, 1u8), (7, 2), (-3, 1), (5, 9)] { + guides.extend_from_slice(&position.to_be_bytes()); + guides.push(orientation); + } + img.image_props.push((18, guides)); + let mut parasites = Vec::new(); + parasite_record(&mut parasites, "gimp-comment", b"hello\0"); + parasite_record(&mut parasites, "icc-profile", &[1, 2, 3]); + img.image_props.push((21, parasites)); + // An unknown property is skipped by its length. + img.image_props.push((99, vec![1, 2, 3, 4, 5])); + img.layers.push(TestLayer::rgba("L", 10, 10, rgba(10, 10, |_, _| [0, 0, 0, 255]))); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + let image = xcf.image(); + assert_eq!(image.resolution, Some((300.0, 299.0))); + let (h, v) = (Orientation::Horizontal, Orientation::Vertical); + assert_eq!( + image.guides, + [Guide { position: 4, orientation: h }, Guide { position: 7, orientation: v }], + "a negative guide before version 15 is stray" + ); + assert_eq!((image.comment.as_deref(), image.icc_profile.as_deref()), (Some("hello"), Some(&[1u8, 2, 3][..]))); + assert_eq!(image.path_count, 0); + img.version = 15; + assert_eq!(open(&img.write()).unwrap().image().guides.len(), 3, "from version 15 a negative guide is off canvas, not stray"); +} + +#[test] +fn clipping_text_vector_effects_and_floating_selections_are_flagged() { + let mut img = TestImage::rgb8(4, 4); + img.version = 20; + let mut clipped = TestLayer::rgba("Clipped", 4, 4, rgba(4, 4, |_, _| [1, 1, 1, 255])); + let mut parasite = Vec::new(); + parasite_record(&mut parasite, "gimphoto-clipped", &[1]); + clipped.props = vec![(21, parasite), (26, 1u32.to_be_bytes().to_vec()), (47, vec![0; 8])]; + img.layers.push(clipped); + let mut floating = TestLayer::rgba("Floating Selection", 2, 2, rgba(2, 2, |_, _| [2, 2, 2, 255])); + floating.props = vec![(5, vec![0; 8])]; + img.layers.push(floating); + let mut text = TestLayer::rgba("Bottom", 4, 4, rgba(4, 4, |_, _| [0, 0, 0, 255])); + let mut parasite = Vec::new(); + parasite_record(&mut parasite, "gimp-text-layer", b"(text \"x\")"); + text.props = vec![(21, parasite)]; + img.layers.push(text); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + let image = xcf.image(); + let l = &image.layers; + assert!(l[0].clipped && l[0].text && l[0].vector && !l[0].floating_selection); + assert!(l[1].floating_selection && !l[1].clipped); + assert!(l[2].text && !l[2].vector); + assert_eq!(names(image, &image.tree().roots), ["Clipped", "Bottom"], "the floating selection is not part of the stack"); + // Version 20 layers end with a list of effect pointers. + let effects_end = bytes.windows(8).rposition(|w| w == [0; 8]).unwrap(); + assert!(effects_end > 0); +} + +#[test] +fn layer_effect_pointers_are_counted_and_bounded() { + let layer = |effects: usize| { + let mut w = W::new(20); + for v in [1, 1, 0, 150] { + w.u32(v); + } + w.end(); + let slot = w.slot(); + w.ptr(0); + w.ptr(0); + w.ptr(0); // paths + let at = w.out.len(); + w.patch(slot, at); + for v in [1, 1, 1] { + w.u32(v); + } + w.string("fx"); + w.end(); + w.ptr(0); + w.ptr(0); + for _ in 0..effects { + w.ptr(1); + } + w.ptr(0); + w.out + }; + assert_eq!(open(&layer(1024)).unwrap().image().layers[0].effects, 1024); + assert!(open(&layer(1025)).is_err()); +} + +/// GIMP's link-layer length omits one u32. Trusting it shifts the hierarchy pointer by four +/// bytes, even though the cached pixels and every offset in the file are valid. +#[test] +fn linked_layers_open_cached_pixels_with_short_or_correct_property_lengths() { + for (precision, data) in [ + (150, vec![51, 102, 153, 255]), + (250, [13107u16, 26214, 39321, 65535].into_iter().flat_map(u16::to_be_bytes).collect()), + (600, [0.2f32, 0.4, 0.6, 1.0].into_iter().flat_map(f32::to_be_bytes).collect()), + ] { + for (path, width, height) in [("assets/étiquette.svg", 2000, 1270), ("assets/label.png", 0, 0), ("", 0, 0)] { + let mut payload = W { out: Vec::new(), wide: true }; + payload.u32(1); + payload.string(path); + payload.u32(width); + payload.u32(height); + let payload = payload.out; + let marker = [48u32.to_be_bytes(), (payload.len() as u32).to_be_bytes()].concat(); + let mut img = TestImage::rgb8(1, 1); + img.version = 25; + img.precision = precision; + let mut linked = TestLayer::rgba("Linked", 1, 1, data.clone()); + // Later properties must still be read, including unknown ones skipped by length. + linked.props = vec![(48, payload.clone()), (99, vec![1, 2, 3]), (33, 0.375f32.to_be_bytes().to_vec()), (8, 0u32.to_be_bytes().to_vec())]; + img.layers.push(linked); + img.layers.push(TestLayer::rgba("Bottom", 1, 1, data.clone())); + for short in [false, true] { + let mut bytes = img.write(); + let at = bytes.windows(marker.len()).position(|w| w == marker).unwrap(); + if short { + bytes[at + 4..at + 8].copy_from_slice(&((payload.len() - 4) as u32).to_be_bytes()); + } + let xcf = open(&bytes).unwrap_or_else(|e| panic!("precision {precision}, path {path:?}, short {short}: {e}")); + let layers = &xcf.image().layers; + assert_eq!(layers.len(), 2); + assert_eq!((layers[0].link, layers[0].opacity, layers[0].visible, layers[1].link), (true, 0.375, false, false)); + for layer in 0..2 { + for (got, expected) in pixels(&xcf, layer).data.into_iter().zip([0.2, 0.4, 0.6, 1.0]) { + assert!((got - expected).abs() < 1e-5, "precision {precision}: {got} != {expected}"); + } + } + } + } + } +} + +#[test] +fn truncated_and_oversized_link_layer_fields_are_errors() { + let mut payload = W { out: Vec::new(), wide: true }; + payload.u32(1); + payload.string("assets/label.svg"); + payload.u32(2000); + payload.u32(1270); + let payload = payload.out; + let marker = [48u32.to_be_bytes(), (payload.len() as u32).to_be_bytes()].concat(); + let mut img = TestImage::rgb8(1, 1); + img.version = 25; + let mut linked = TestLayer::rgba("Linked", 1, 1, vec![51, 102, 153, 255]); + linked.props.push((48, payload.clone())); + img.layers.push(linked); + let bytes = img.write(); + let start = bytes.windows(marker.len()).position(|w| w == marker).unwrap() + 8; + // Cut within the flags, string length, path and either dimension. + for end in start..start + payload.len() { + assert!(open(&bytes[..end]).is_err(), "truncated at {end}"); + } + for len in [(MAX_STRING + 1) as u32, u32::MAX] { + let mut oversized = bytes.clone(); + oversized[start + 4..start + 8].copy_from_slice(&len.to_be_bytes()); + assert!(open(&oversized).is_err(), "path length {len}"); + } +} + +#[test] +fn version_3_files_use_32_bit_pointers_and_legacy_modes() { + let mut img = TestImage::rgb8(5, 5); + img.version = 3; + let mut l = TestLayer::rgba("Old", 5, 5, rgba(5, 5, |x, _| [x as u8, 0, 0, 255])); + l.mode = 3; // Multiply (legacy) + img.layers.push(l); + let bytes = img.write(); + assert!(bytes.starts_with(b"gimp xcf v003\0")); + let xcf = open(&bytes).unwrap(); + assert_eq!((xcf.image().version, xcf.image().precision, xcf.image().layers[0].mode), (3, Precision::GAMMA8, 3)); + assert_eq!(at(&pixels(&xcf, 0), 4, 0)[0], 4.0 / 255.0); + // Version 0 ("file") as well. + let mut img0 = TestImage::rgb8(2, 2); + img0.version = 0; + img0.layers.push(TestLayer::rgba("Oldest", 2, 2, rgba(2, 2, |_, _| [7, 7, 7, 255]))); + assert_eq!(open(&img0.write()).unwrap().image().version, 0); + // A version newer than the reader knows still opens. + img0.version = KNOWN_VERSION + 1; + assert_eq!(read_all(&img0.write()), Ok(16)); +} + +#[test] +fn rle_long_runs_and_literals_round_trip() { + // A tile with runs over 127 and long literal stretches, in every byte plane. + let w = 64; + let data = rgba(w, 64, |x, y| if y < 40 { [200, 1, 2, 255] } else { [(x * 7 + y) as u8, (x * 3) as u8, y as u8, 255] }); + assert_eq!(rle_decode(&rle(&data, 4), 4, 64 * 64).unwrap(), data); + // Long literal (op 128) and the error cases. + assert_eq!(rle_decode(&[128, 0, 3, 7, 8, 9], 1, 3).unwrap(), [7, 8, 9]); + assert_eq!(rle_decode(&[127, 0, 4, 9], 1, 4).unwrap(), [9, 9, 9, 9]); + assert_eq!(rle_decode(&[254, 1, 2, 0, 5], 1, 3).unwrap(), [1, 2, 5]); + for bad in [&[][..], &[3], &[127, 0], &[128, 0, 3, 7], &[2, 9], &[253, 1]] { + assert!(rle_decode(bad, 1, 2).is_err(), "{bad:?}"); + } +} + +#[test] +fn limits_and_malformed_structures_are_errors() { + // A canvas past the side limit, and one past the area limit. + let mut big = TestImage::rgb8(1, 1); + big.width = MAX_SIDE + 1; + assert!(open(&big.write()).is_err()); + (big.width, big.height) = (MAX_SIDE, 224); + assert!(open(&big.write()).is_err()); + big.height = 223; + assert!(open(&big.write()).is_ok()); + big.height = 0; + assert!(open(&big.write()).is_err()); + // A layer whose declared size has no pixels behind it (hierarchy pointer to garbage). + let mut img = TestImage::rgb8(4, 4); + img.layers.push(TestLayer::rgba("L", 4, 4, rgba(4, 4, |_, _| [0; 4]))); + let mut bytes = img.write(); + let n = bytes.len(); + bytes[n - 10..].fill(0xff); + assert!(read_all(&bytes).is_err()); + // Wrong signature, unknown version tag, unknown compression, image type and precision. + assert!(matches!(open(b"gimp xcf vXYZ\0"), Err(Error::Invalid(_)))); + assert!(open(b"not an xcf").is_err()); + let good = img.write(); + assert!(read_all(&good).is_ok()); + let patched = |at: usize, v: u8| { + let mut b = good.clone(); + b[at] = v; + open(&b).err() + }; + assert!(patched(14 + 12 + 4 + 8, 3).is_some(), "compression"); + assert!(patched(14 + 11, 3).is_some(), "image type"); + assert!(patched(14 + 15, 1).is_some(), "precision"); + // A layer of another colour model, and an unknown layer type. + for kind in [2, 6] { + img.layers[0].kind = kind; + assert!(open(&img.write()).is_err(), "layer type {kind}"); + } + // Pixels of another size than the layer declares. + img.layers[0].kind = 1; + let mut bytes = img.write(); + let at = bytes.windows(12).rposition(|w| w == [0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4]).unwrap(); + bytes[at + 3] = 3; + assert!(open(&bytes).is_ok() && read_all(&bytes).is_err()); +} + +#[test] +fn layer_channel_property_and_metadata_counts_are_bounded() { + let header = |w: &mut W| { + for v in [1, 1, 0, 150] { + w.u32(v); + } + w.end(); + }; + // More layer pointers, then more channel pointers, than the limits. + for (layers, channels, ok) in [(MAX_LAYERS + 1, 0, false), (0, MAX_CHANNELS + 1, false), (0, MAX_CHANNELS, true)] { + let mut w = W::new(11); + header(&mut w); + (0..layers).for_each(|_| w.ptr(1)); + w.ptr(0); + (0..channels).for_each(|_| w.ptr(1)); + w.ptr(0); + assert_eq!(open(&w.out).is_ok(), ok, "{layers} layers, {channels} channels"); + } + // 4097 properties. + let mut w = W::new(11); + for v in [1, 1, 0, 150] { + w.u32(v); + } + (0..4097).for_each(|_| w.prop(99, &[])); + w.end(); + w.ptr(0); + w.ptr(0); + assert_eq!(open(&w.out).err(), Some(Error::Invalid("too many properties".into()))); + // A property declaring more than the payload limit. + let mut w = W::new(11); + for v in [1, 1, 0, 150, 99, MAX_PAYLOAD as u32 + 1] { + w.u32(v); + } + assert!(matches!(open(&w.out), Err(Error::Invalid(m)) if m.contains("declares"))); + // Forty pointers to one layer whose name is a megabyte: its copies pass the metadata budget. + let many = |pointers: usize| { + let mut w = W::new(11); + header(&mut w); + let slots: Vec = (0..pointers).map(|_| w.slot()).collect(); + w.ptr(0); + w.ptr(0); + let at = w.out.len(); + slots.into_iter().for_each(|s| w.patch(s, at)); + for v in [1, 1, 1] { + w.u32(v); + } + w.string(&"n".repeat(MAX_STRING - 1)); + w.end(); + w.ptr(0); + w.ptr(0); + w.out + }; + assert!(open(&many(32)).is_ok()); + assert_eq!(open(&many(33)).err(), Some(Error::Invalid("layer metadata exceeds the budget".into()))); +} + +#[test] +fn the_pixel_budget_is_charged_before_decoding_and_decoding_can_be_cancelled() { + let mut img = TestImage::rgb8(200, 200); + img.layers.push(TestLayer::rgba("L", 200, 200, rgba(200, 200, |_, _| [1, 2, 3, 255]))); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + let cost = 200 * 200 * 4 * 4; + let mut budget = cost - 1; + assert!(matches!(xcf.layer_pixels(0, &mut budget, GO), Err(Error::Invalid(m)) if m.contains("pixel limit"))); + let mut budget = cost + 5; + assert!(xcf.layer_pixels(0, &mut budget, GO).is_ok()); + assert_eq!(budget, 5); + assert_eq!(xcf.layer_pixels(0, &mut MAX_PIXEL_BYTES.clone(), &|| false), Err(Error::Cancelled)); +} + +#[test] +fn gzipped_files_are_recognised_by_name_and_inflated() { + use std::io::Write; + let mut img = TestImage::rgb8(3, 3); + img.layers.push(TestLayer::rgba("L", 3, 3, rgba(3, 3, |_, _| [4, 5, 6, 255]))); + let plain = img.write(); + let mut e = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + e.write_all(&plain).unwrap(); + let gz = e.finish().unwrap(); + assert!(is_xcf(&plain) && !is_xcf(&gz)); + assert!(is_gzipped_xcf("a/b/photo.XCF.GZ", &gz) && is_gzipped_xcf("x.xcfgz", &gz) && is_gzipped_xcf("c:\\dir.xcf.gz\\y.xcf.gz", &gz)); + assert!(!is_gzipped_xcf("photo.xcf", &gz) && !is_gzipped_xcf("photo.xcf.gz", &plain) && !is_gzipped_xcf("dir.xcf.gz/photo.gz", &gz)); + assert_eq!(gunzip(&gz).unwrap(), plain); + assert!(gunzip(&gz[..gz.len() / 2]).is_err()); +} + +/// `PROP_FLOAT_OPACITY` wins over `PROP_OPACITY` whichever comes first in the file. +#[test] +fn float_opacity_wins_over_opacity_in_either_order() { + let mut img = TestImage::rgb8(4, 4); + // The writer emits OPACITY (0.4 → 102) then FLOAT_OPACITY 0.4; an OPACITY 255 after them must not win. + let mut byte_last = TestLayer::rgba("Float then byte", 4, 4, rgba(4, 4, |_, _| [1, 2, 3, 255])); + byte_last.opacity = 0.4; + byte_last.props = vec![(6, 255u32.to_be_bytes().to_vec())]; + // A later FLOAT_OPACITY replaces the earlier one; one that is not finite does not. + let mut float_last = TestLayer::rgba("Byte then float", 4, 4, rgba(4, 4, |_, _| [1, 2, 3, 255])); + float_last.opacity = 0.4; + float_last.props = vec![(33, 0.25f32.to_be_bytes().to_vec()), (33, f32::NAN.to_be_bytes().to_vec())]; + img.layers.push(byte_last); + img.layers.push(float_last); + let bytes = img.write(); + let xcf = open(&bytes).unwrap(); + assert_eq!((xcf.image().layers[0].opacity, xcf.image().layers[1].opacity), (0.4, 0.25)); +} + +/// One PROP_PARASITES record: name, u32 flags, u32 len, then the payload. +fn parasite_record(out: &mut Vec, name: &str, payload: &[u8]) { + out.extend_from_slice(&((name.len() as u32) + 1).to_be_bytes()); + out.extend_from_slice(name.as_bytes()); + out.push(0); + out.extend_from_slice(&0u32.to_be_bytes()); + out.extend_from_slice(&(payload.len() as u32).to_be_bytes()); + out.extend_from_slice(payload); +} + +#[test] +fn duplicate_parasites_keep_order_and_last_value() { + let mut data = Vec::new(); + parasite_record(&mut data, "gimp-comment", b"first"); + parasite_record(&mut data, "gimp-image-grid", b"grid"); + parasite_record(&mut data, "gimp-comment", b"second"); + data.extend_from_slice(&[0, 0, 0, 9, b'c', b'u', b't']); + let out = parasites(&data); + assert_eq!(out, [("gimp-comment".to_string(), &b"second"[..]), ("gimp-image-grid".to_string(), &b"grid"[..])]); +} + +#[test] +fn a_user_unit_property_is_measured_not_trusted() { + // PROP_USER_UNIT's length word is wrong in every release: three strings (XCF 21+) or five. + // Which it is, is decided by whether a property header follows the third: a fourth string + // shorter than 64 bytes would look like one, so the five-string case here has a long one. + let long = "s".repeat(70); + for strings in [3, 5] { + let mut img = TestImage::rgb8(2, 2); + let mut unit = W { out: Vec::new(), wide: true }; + unit.out.extend_from_slice(&25.4f32.to_be_bytes()); + unit.u32(2); + (0..strings).for_each(|i| unit.string(["id", "sym", "abbr", long.as_str(), "plural"][i])); + img.image_props.push((24, unit.out)); + img.image_props.push((19, [96.0f32, 96.0].into_iter().flat_map(f32::to_be_bytes).collect())); + img.layers.push(TestLayer::rgba("L", 2, 2, rgba(2, 2, |_, _| [4, 5, 6, 255]))); + let mut bytes = img.write(); + let marker = 24u32.to_be_bytes(); + let at = bytes.windows(4).position(|w| w == marker).unwrap(); + bytes[at + 4..at + 8].copy_from_slice(&3u32.to_be_bytes()); + let xcf = open(&bytes).unwrap_or_else(|e| panic!("{strings} strings: {e}")); + assert_eq!(xcf.image().resolution, Some((96.0, 96.0)), "{strings} strings"); + assert_eq!(read_all(&bytes), Ok(16)); + } +} + +#[test] +fn an_rle_run_of_zero_length_is_an_error() { + // Long runs (op 127 / 128) carry a 16-bit length; zero consumes input without producing output. + assert!(rle_decode(&[127, 0, 0, 5, 3, 0, 9], 1, 4).is_err()); + assert!(rle_decode(&[128, 0, 0, 3, 0, 9], 1, 4).is_err()); + assert_eq!(rle_decode(&[127, 0, 4, 9], 1, 4).unwrap(), [9, 9, 9, 9]); +} + +#[test] +fn channel_names_are_charged_to_the_metadata_budget() { + // Channel pointers that all name one channel with a megabyte name: each read copies the name. + let mut w = W::new(11); + for v in [1, 1, 0, 150] { + w.u32(v); + } + w.end(); + w.ptr(0); + let slots: Vec = (0..40).map(|_| w.slot()).collect(); + w.ptr(0); + let at = w.out.len(); + slots.into_iter().for_each(|s| w.patch(s, at)); + w.u32(1); + w.u32(1); + w.string(&"n".repeat(MAX_STRING - 1)); + w.end(); + let hs = w.slot(); + let h = hierarchy(&mut w, 1, 1, 1, &[7], Compression::None); + w.patch(hs, h); + let xcf = open(&w.out).unwrap(); + let mut budget = MAX_PIXEL_BYTES; + let results: Vec<_> = (0..40).map(|i| xcf.channel(i, &mut budget, GO).map(|(c, _)| c.name.len())).collect(); + assert!(results[..32].iter().all(|r| *r == Ok(MAX_STRING - 1)), "32 copies fit the 32 MiB budget"); + assert!(results[32..].iter().all(|r| *r == Err(Error::Invalid("channel metadata exceeds the budget".into())))); +} diff --git a/crates/xcf/src/tiles.rs b/crates/xcf/src/tiles.rs new file mode 100644 index 000000000..5a567558a --- /dev/null +++ b/crates/xcf/src/tiles.rs @@ -0,0 +1,70 @@ +//! One tile's bytes: uncompressed, zlib-coded or RLE-coded. + +use std::io::Read; + +use crate::{Compression, Result, invalid}; + +/// What one tile's bytes decode to, for a drawable of `bpp` bytes per pixel: `pixels × bpp` +/// interleaved bytes. +pub(crate) fn decode(input: &[u8], compression: Compression, bpp: usize, pixels: usize) -> Result> { + let expected = pixels.checked_mul(bpp).ok_or_else(|| invalid("tile size overflow"))?; + match compression { + Compression::None => input.get(..expected).map(<[u8]>::to_vec).ok_or_else(|| invalid("an uncompressed tile is cut short")), + Compression::Zlib => { + let mut out = Vec::with_capacity(expected); + flate2::read::ZlibDecoder::new(input).take(expected as u64).read_to_end(&mut out).map_err(|e| invalid(format!("zlib tile: {e}")))?; + if out.len() != expected { + return Err(invalid("a zlib tile is cut short")); + } + Ok(out) + } + Compression::Rle => rle_decode(input, bpp, pixels), + } +} + +/// RLE tile data: one run-length stream per byte position of the pixel, each of `pixels` bytes. +pub(crate) fn rle_decode(input: &[u8], bpp: usize, pixels: usize) -> Result> { + let short = || invalid("an RLE tile is cut short"); + let mut out = vec![0u8; pixels * bpp]; + let mut pos = 0usize; + let mut plane = Vec::with_capacity(pixels); + for b in 0..bpp { + plane.clear(); + while plane.len() < pixels { + let op = *input.get(pos).ok_or_else(short)?; + pos += 1; + let mut long = || -> Result { + let (p, q) = (input.get(pos).copied().ok_or_else(short)?, input.get(pos + 1).copied().ok_or_else(short)?); + pos += 2; + Ok(usize::from(p) * 256 + usize::from(q)) + }; + let (run, verbatim) = match op { + 0..=126 => (usize::from(op) + 1, false), + 127 => (long()?, false), + 128 => (long()?, true), + _ => (256 - usize::from(op), true), + }; + if plane.len() + run > pixels { + return Err(invalid("an RLE run crosses the end of its tile")); + } + // A long run of length 0 consumes input without producing output: a tile of them + // (reachable from many tile pointers) is work the pixel budget never charges. + if run == 0 { + return Err(invalid("an RLE run has no length")); + } + if verbatim { + let data = input.get(pos..pos + run).ok_or_else(short)?; + pos += run; + plane.extend_from_slice(data); + } else { + let v = *input.get(pos).ok_or_else(short)?; + pos += 1; + plane.resize(plane.len() + run, v); + } + } + for (slot, v) in out.iter_mut().skip(b).step_by(bpp.max(1)).zip(&plane) { + *slot = *v; + } + } + Ok(out) +} From 53e1717523a8a4dd24d7bc3be93963aa2b009ae3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:25:27 -0500 Subject: [PATCH 052/120] craft-rtf: Outlook compressed RTF and body text, from MailCraft (#961) The outlook feature adds MailCraft's reader of PidTagRtfCompressed ([MS-OXRTFCP]) and of the text and encapsulated HTML ([MS-OXRTFEX]) of the RTF inside, from crates/pst/src/rtf.rs at a44a450. Font charsets use this crate's charset_code_page table; code pages are decoded by craft-mime. Co-authored-by: Martin Ball <98639962+benandez@users.noreply.github.com> --- Cargo.lock | 2 + crates/rtf/Cargo.toml | 10 + crates/rtf/README.md | 11 + crates/rtf/src/lib.rs | 2 + crates/rtf/src/outlook.rs | 711 ++++++++++++++++++++++++++++++++ crates/rtf/src/outlook_tests.rs | 385 +++++++++++++++++ 6 files changed, 1121 insertions(+) create mode 100644 crates/rtf/src/outlook.rs create mode 100644 crates/rtf/src/outlook_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 1be03246b..0b52a4503 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1556,7 +1556,9 @@ dependencies = [ name = "craft-rtf" version = "0.1.0" dependencies = [ + "craft-mime", "encoding_rs", + "proptest", ] [[package]] diff --git a/crates/rtf/Cargo.toml b/crates/rtf/Cargo.toml index aec1ca5ce..a6724e251 100644 --- a/crates/rtf/Cargo.toml +++ b/crates/rtf/Cargo.toml @@ -10,6 +10,16 @@ repository.workspace = true [lints] workspace = true +[features] +# `outlook`: compressed RTF ([MS-OXRTFCP]) and the text or encapsulated HTML ([MS-OXRTFEX]) of a +# mail item's body. +outlook = ["dep:craft-mime"] + [dependencies] +# Character sets by Windows code page number, for the `outlook` module. +craft-mime = { workspace = true, optional = true } # The East Asian multibyte code pages (932, 936, 949, 950). encoding_rs = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/rtf/README.md b/crates/rtf/README.md index 9ae51fecc..65c378c17 100644 --- a/crates/rtf/README.md +++ b/crates/rtf/README.md @@ -79,6 +79,17 @@ elsewhere are kept. `\sect` ends a paragraph. The reader does not recurse, and what it returns is never larger than a small multiple of the file: each character of text costs the file at least one byte. +## Outlook bodies (feature `outlook`) + +`outlook::decompress_rtf` undoes the compression of `PidTagRtfCompressed` ([MS-OXRTFCP]: `LZFu` +with its CRC checked, or `MELA` stored as is), never allocating past the caller's cap. +`outlook::rtf_text` gives the text a mail body shows (font charsets and `\ansicpg` decoded through +`craft_mime::charset`, `\uN` with its fallback skipped, destinations dropped), and +`outlook::rtf_html` the HTML of an encapsulated-HTML body ([MS-OXRTFEX], `\fromhtml1`). Groups nest +at most `MAX_RTF_DEPTH` (256) deep. From MailCraft `crates/pst/src/rtf.rs` at +[a44a450](https://github.com/UCS-Care-Group/mailcraft/commit/a44a45013d1c6396229c40ab60172d1882b368d5), +by Martin Ball and the MailCraft contributors. + ## Not here yet WordCraft's full reader and writer (styles, lists, fields, bookmarks, pictures, notes, diff --git a/crates/rtf/src/lib.rs b/crates/rtf/src/lib.rs index c6bba82ec..3b22e484b 100644 --- a/crates/rtf/src/lib.rs +++ b/crates/rtf/src/lib.rs @@ -33,6 +33,8 @@ pub mod codepage; mod date; mod lex; mod model; +#[cfg(feature = "outlook")] +pub mod outlook; mod read; mod text; mod write; diff --git a/crates/rtf/src/outlook.rs b/crates/rtf/src/outlook.rs new file mode 100644 index 000000000..7ce886b88 --- /dev/null +++ b/crates/rtf/src/outlook.rs @@ -0,0 +1,711 @@ +//! Rich text as Outlook keeps it in `PidTagRtfCompressed` (0x1009), in .pst and .msg files and +//! TNEF attachments: the compression of +//! [MS-OXRTFCP] ([`decompress_rtf`]) and the text of the RTF that comes out ([`rtf_text`]), +//! written from those two specifications and the public Rich Text Format specification. +//! +//! Both read hostile bytes: output is capped by the caller, a header's size is never trusted +//! for an allocation past that cap, nothing recurses (groups are counted, not nested calls), +//! and every loop ends with its input. + +use std::collections::HashMap; + +use craft_mime::charset::{code_page_label, decode}; + +use crate::codepage; + +/// Why rich text was not read. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Error { + /// The bytes are not what the format says; the text names what was wrong. + Corrupt(&'static str), + /// The compressed bytes do not have the CRC their header gives. + BadChecksum, + /// More would come out than the caller's cap allows. + TooLarge, +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Corrupt(what) => f.write_str(what), + Error::BadChecksum => f.write_str("compressed rich text with a wrong checksum"), + Error::TooLarge => f.write_str("rich text larger than allowed"), + } + } +} + +impl std::error::Error for Error {} + +type Result = std::result::Result; + +/// The CRC of [MS-OXRTFCP] 2.1.3.1 and 3.2: the reflected CRC-32 polynomial, seeded with zero and +/// with no final inversion. +pub(crate) fn crc32(data: &[u8]) -> u32 { + let mut crc = 0u32; + for &b in data { + crc ^= u32::from(b); + for _ in 0..8 { + crc = if crc & 1 != 0 { 0xEDB8_8320 ^ (crc >> 1) } else { crc >> 1 }; + } + } + crc +} + +/// The dictionary a stream starts with ([MS-OXRTFCP] 2.1.2.1), 207 bytes. +const DICT_INIT: &[u8] = b"{\\rtf1\\ansi\\mac\\deff0\\deftab720{\\fonttbl;}{\\f0\\fnil \\froman \\fswiss \\fmodern \\fscript \\fdecor MS Sans SerifSymbolArialTimes New RomanCourier{\\colortbl\\red0\\green0\\blue0\r\n\\par \\pard\\plain\\f0\\fs20\\b\\i\\u\\tab\\tx"; +const _: () = assert!(DICT_INIT.len() == 207); +const DICT_SIZE: usize = 4096; +const HEADER: usize = 16; +const LZFU: u32 = 0x7546_5A4C; +const MELA: u32 = 0x414C_454D; + +fn le32(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(4)?)?; + Some(u32::from_le_bytes(<[u8; 4]>::try_from(s).ok()?)) +} + +/// The RTF inside `PidTagRtfCompressed` bytes: the 16-byte header, then either the text as it +/// is (`MELA`) or the dictionary-compressed runs (`LZFu`) whose CRC is checked. At most +/// `max_out` bytes come out; past that is [`Error::TooLarge`], never a bigger allocation. +pub fn decompress_rtf(data: &[u8], max_out: usize) -> Result> { + let (Some(comp_size), Some(raw_size), Some(kind), Some(crc)) = (le32(data, 0), le32(data, 4), le32(data, 8), le32(data, 12)) else { + return Err(Error::Corrupt("rich text shorter than its header")); + }; + let raw_size = usize::try_from(raw_size).unwrap_or(usize::MAX); + let body = data.get(HEADER..).unwrap_or_default(); + match kind { + MELA => { + // The reader may take the sizes the header gives; never more than is there. + let take = raw_size.min(body.len()); + if take > max_out { + return Err(Error::TooLarge); + } + Ok(body.get(..take).unwrap_or_default().to_vec()) + } + LZFU => { + if raw_size > max_out { + return Err(Error::TooLarge); + } + // COMPSIZE counts the 12 bytes of the header after itself. + let content_len = usize::try_from(comp_size) + .unwrap_or(usize::MAX) + .checked_sub(12) + .ok_or(Error::Corrupt("rich text with a size smaller than its header"))?; + let content = body.get(..content_len).ok_or(Error::Corrupt("rich text cut short"))?; + let out = lzfu(content, max_out, raw_size)?; + if crc32(content) != crc { + return Err(Error::BadChecksum); + } + Ok(out) + } + _ => Err(Error::Corrupt("rich text of a kind not known")), + } +} + +fn lzfu(content: &[u8], max_out: usize, raw_size: usize) -> Result> { + let mut dict = vec![0u8; DICT_SIZE]; + if let Some(head) = dict.get_mut(..DICT_INIT.len()) { + head.copy_from_slice(DICT_INIT); + } + let mut write = DICT_INIT.len(); + let mut out: Vec = Vec::with_capacity(raw_size.min(max_out).min(1 << 20)); + let mut at = 0usize; + let cut = Error::Corrupt("compressed rich text ends before its end mark"); + loop { + let control = *content.get(at).ok_or(cut)?; + at = at.saturating_add(1); + for bit in 0..8u32 { + if control >> bit & 1 == 0 { + let b = *content.get(at).ok_or(cut)?; + at = at.saturating_add(1); + if out.len() >= max_out { + return Err(Error::TooLarge); + } + out.push(b); + if let Some(d) = dict.get_mut(write) { + *d = b; + } + write = write.saturating_add(1) % DICT_SIZE; + } else { + let (Some(&hi), Some(&lo)) = (content.get(at), content.get(at.saturating_add(1))) else { return Err(cut) }; + at = at.saturating_add(2); + let word = usize::from(hi) << 8 | usize::from(lo); + let mut read = word >> 4; + if read == write { + return Ok(out); + } + for _ in 0..(word & 0xF).saturating_add(2) { + let b = dict.get(read).copied().unwrap_or(0); + if out.len() >= max_out { + return Err(Error::TooLarge); + } + out.push(b); + read = read.saturating_add(1) % DICT_SIZE; + if let Some(d) = dict.get_mut(write) { + *d = b; + } + write = write.saturating_add(1) % DICT_SIZE; + } + } + } + } +} + +/// Deepest nesting of groups read. +pub const MAX_RTF_DEPTH: usize = 256; + +/// Destinations whose content is not the document's text. +const SKIPPED: &[&str] = &[ + "fonttbl", + "colortbl", + "stylesheet", + "info", + "pict", + "object", + "header", + "headerl", + "headerr", + "headerf", + "footer", + "footerl", + "footerr", + "footerf", + "themedata", + "colorschememapping", + "latentstyles", + "datastore", + "listtable", + "listoverridetable", + "revtbl", + "rsidtbl", + "generator", + "filetbl", + "fldinst", + "xmlnstbl", + "mmathpr", + "pgdsctbl", + "template", + "private", + "bkmkstart", + "bkmkend", + "wgrffmtfilter", + "shpinst", + "nonshppict", + "pnseclvl", + "listtext", + "blipuid", + "falt", + "panose", + "fchars", + "lchars", + "xe", + "tc", + "tcn", + "ud", + "upr", + "title", + "subject", + "author", + "operator", + "company", + "keywords", + "doccomm", + "comment", + "category", + "manager", + "hlinkbase", + "buptim", + "creatim", + "revtim", + "printim", + "txe", + "rxe", + "pnseclvl", + "protusertbl", + "defchp", + "defpap", + "fontemb", + "fontfile", + "background", + "docvar", + "userprops", + "mhtmltag", + "htmltag", + "factoidname", + "oleclsid", + "objdata", +]; + +#[derive(Clone, Copy)] +struct Group { + /// Skip characters after `\u` (`\ucN`). + uc: u32, + skip: bool, + /// In the font table: `\fN` names the font that `\fcharsetN` is for. + fonttbl: bool, + /// The font being defined (in the font table). + defining: i64, + /// The font text is in (`\fN`), if one was chosen. + font: Option, + /// Encapsulated HTML: under `\htmlrtf`, what isn't part of the HTML. + suppressed: bool, + /// Encapsulated HTML: inside an `\htmltag` group. + in_tag: bool, +} + +impl Group { + fn hidden(&self) -> bool { + self.skip || self.suppressed + } +} + +/// Most fonts whose character set is kept. +const MAX_FONTS: usize = 1024; +/// Most bytes of `\'hh` kept waiting for a character boundary. +const PENDING: usize = 4096; + +struct Out { + text: String, + max: usize, + /// Text was dropped for the cap. + cut: bool, + pending: Vec, + /// The code page of `pending`. + pending_cp: u32, + high: Option, + ansicpg: Option, + mac: bool, + deff: Option, + /// `\fcharset` of each font in the font table. + fonts: HashMap, + /// Reading encapsulated HTML rather than the text a document shows. + html: bool, +} + +/// The code page a font character set stands for ([MSFT-RTF] `\fcharsetN`): `None` for the +/// document's own (0 is Windows ANSI, 1252, which is not the document's when it says +/// `\ansicpg` otherwise: [MS-OXRTFEX] 3.1 marks one font `\fcharset0` in a 1251 document to +/// have 0xA8 read as U+00A8). Symbol (2), Johab (130) and the OEM sets (254, 255) are read in the document's. +fn charset_code_page(charset: i64) -> Option { + match charset { + 0 => Some(1252), + 2 | 254 | 255 => None, + other => i32::try_from(other).ok().and_then(codepage::charset_code_page).map(u32::from), + } +} + +/// Whether `b` begins a two-byte character in code page `cp`. +fn is_lead(cp: u32, b: u8) -> bool { + match cp { + 932 => matches!(b, 0x81..=0x9F | 0xE0..=0xFC), + 936 | 949 | 950 => matches!(b, 0x81..=0xFE), + _ => false, + } +} + +impl Out { + fn new(max: usize) -> Out { + Out { + text: String::new(), + max, + cut: false, + pending: Vec::new(), + pending_cp: 1252, + high: None, + ansicpg: None, + mac: false, + deff: None, + fonts: HashMap::new(), + html: false, + } + } + + /// The code page of the document: `\ansicpg`, else the Macintosh's for `\mac`, else 1252. + fn document_cp(&self) -> u32 { + self.ansicpg.unwrap_or(if self.mac { 10000 } else { 1252 }) + } + + /// The code page of bytes in font `font` (else the default font). + fn code_page(&self, font: Option) -> u32 { + font.or(self.deff).and_then(|f| self.fonts.get(&f)).and_then(|cs| charset_code_page(*cs)).unwrap_or_else(|| self.document_cp()) + } + + fn full(&self) -> bool { + self.text.len() >= self.max + } + + fn flush(&mut self) { + let bytes = std::mem::take(&mut self.pending); + self.decode(&bytes); + } + + fn decode(&mut self, bytes: &[u8]) { + if !bytes.is_empty() { + let decoded = decode(bytes, code_page_label(i64::from(self.pending_cp)).unwrap_or("windows-1252")); + self.push_str(&decoded); + } + } + + /// Decodes the whole characters waiting, and keeps a lead byte whose second is not here yet. + fn flush_whole(&mut self) { + let mut cut = 0usize; + let mut i = 0usize; + while i < self.pending.len() { + let step = if self.pending.get(i).is_some_and(|b| is_lead(self.pending_cp, *b)) { 2 } else { 1 }; + if i.saturating_add(step) > self.pending.len() { + break; + } + i = i.saturating_add(step); + cut = i; + } + let rest = self.pending.split_off(cut); + self.flush(); + self.pending = rest; + } + + fn push_str(&mut self, s: &str) { + for c in s.chars() { + // Controls other than the line end and the tab are not text. + if c.is_control() && c != '\n' && c != '\t' { + continue; + } + if self.full() { + self.cut = true; + return; + } + self.text.push(c); + } + } + + fn char(&mut self, c: char) { + self.flush_high(); + self.flush(); + self.push_str(c.encode_utf8(&mut [0; 4])); + } + + fn flush_high(&mut self) { + if self.high.take().is_some() { + self.push_str("\u{FFFD}"); + } + } + + /// A byte of 8-bit text in code page `cp`. + fn byte(&mut self, b: u8, cp: u32) { + self.flush_high(); + if cp != self.pending_cp { + self.flush(); + self.pending_cp = cp; + } + if self.pending.len() >= PENDING { + self.flush_whole(); + } + // (A buffer that is full of one character's bytes is not a text any code page here + // has; it is let grow by one rather than dropped.) + self.pending.push(b); + } + + fn unit(&mut self, u: u16) { + self.flush(); + match (self.high.take(), u) { + (Some(h), 0xDC00..=0xDFFF) => { + let c = 0x10000u32.saturating_add((u32::from(h).saturating_sub(0xD800)) << 10).saturating_add(u32::from(u).saturating_sub(0xDC00)); + self.push_str(&char::from_u32(c).unwrap_or('\u{FFFD}').to_string()); + } + (Some(_), _) => { + self.push_str("\u{FFFD}"); + self.unit(u); + } + (None, 0xD800..=0xDBFF) => self.high = Some(u), + (None, 0xDC00..=0xDFFF) => self.push_str("\u{FFFD}"), + (None, u) => self.push_str(&char::from_u32(u32::from(u)).unwrap_or('\u{FFFD}').to_string()), + } + } +} + +/// The text of an RTF document: paragraphs and lines as newlines, tabs, `\'hh` bytes in the +/// code page of the font they are set in (its `\fcharset`, else the document's `\ansicpg`, +/// Windows-1252 when it names none), `\uN` with its `\ucN` fallback skipped, and none of the +/// font table, colours, styles, pictures, objects, headers or footers, nor any `{\*…}` +/// destination (for encapsulated HTML, [MS-OXRTFEX], that leaves the text the document shows +/// and not the `\htmltag` HTML, and the text as it is for `\fromtext`). Control characters +/// other than the line end and the tab are dropped. At most `max` bytes of text; whether more +/// was cut is the second part of the answer. Groups nest at most [`MAX_RTF_DEPTH`] deep, else +/// an error; unbalanced braces are read as they come. +pub fn rtf_text(rtf: &[u8], max: usize) -> Result<(String, bool)> { + read(rtf, max, false).map(|(text, cut)| (tidy(&text), cut)) +} + +/// The HTML a document keeps when it is encapsulated HTML ([MS-OXRTFEX] 2.2.3): what its +/// `\htmltag` groups hold and the text no `\htmlrtf` suppresses, under the same caps as +/// [`rtf_text`]. `None` for any other document. The HTML is as hostile as the file: whoever +/// shows it reads it as any other message's. +pub fn rtf_html(rtf: &[u8], max: usize) -> Result> { + if !encapsulates_html(rtf) { + return Ok(None); + } + read(rtf, max, true).map(Some) +} + +/// Whether one of the document's first ten tokens says it was HTML (2.2.3.1). +fn encapsulates_html(rtf: &[u8]) -> bool { + let mut i = rtf.iter().position(|b| !b.is_ascii_whitespace()).unwrap_or(rtf.len()); + if rtf.get(i..i.saturating_add(5)) != Some(b"{\\rtf") { + return false; + } + for _ in 0..10 { + match rtf.get(i) { + Some(b'{') => i = i.saturating_add(1), + Some(b'\\') => { + let from = i.saturating_add(1); + i = from; + while rtf.get(i).is_some_and(u8::is_ascii_alphabetic) { + i = i.saturating_add(1); + } + let word = rtf.get(from..i).unwrap_or_default(); + if word.is_empty() { + return false; + } + let digits = i; + while rtf.get(i).is_some_and(u8::is_ascii_digit) { + i = i.saturating_add(1); + } + match word { + b"fromhtml" => return rtf.get(digits..i) != Some(b"0"), + b"fromtext" => return false, + _ => {} + } + if rtf.get(i) == Some(&b' ') { + i = i.saturating_add(1); + } + } + _ => return false, + } + } + false +} + +fn read(rtf: &[u8], max: usize, html: bool) -> Result<(String, bool)> { + let start = rtf.iter().position(|b| !b.is_ascii_whitespace()).unwrap_or(rtf.len()); + if rtf.get(start..start.saturating_add(5)) != Some(b"{\\rtf") { + return Err(Error::Corrupt("not rich text")); + } + let mut out = Out::new(max); + out.html = html; + let mut stack: Vec = Vec::new(); + let mut cur = Group { uc: 1, skip: false, fonttbl: false, defining: 0, font: None, suppressed: false, in_tag: false }; + let mut star = false; + let mut first = false; + let mut skip_chars = 0u32; + let mut i = start; + while let Some(&b) = rtf.get(i) { + i = i.saturating_add(1); + match b { + b'{' => { + if stack.len() >= MAX_RTF_DEPTH { + return Err(Error::Corrupt("rich text nested too deeply")); + } + stack.push(cur); + first = true; + skip_chars = 0; + } + b'}' => { + out.flush(); + out.flush_high(); + if let Some(g) = stack.pop() { + cur = g; + } + first = false; + skip_chars = 0; + } + b'\r' | b'\n' => {} + b'\\' => { + let Some(&n) = rtf.get(i) else { break }; + i = i.saturating_add(1); + match n { + b'\\' | b'{' | b'}' => { + first = false; + if !cur.hidden() && !take(&mut skip_chars) { + out.char(char::from(n)); + } + } + // A backslash before a line end is a paragraph end. + b'\r' | b'\n' => { + first = false; + if !cur.hidden() { + out.char('\n'); + } + } + b'\'' => { + first = false; + let hex = + rtf.get(i..i.saturating_add(2)).and_then(|h| std::str::from_utf8(h).ok()).and_then(|h| u8::from_str_radix(h, 16).ok()); + if let Some(v) = hex { + i = i.saturating_add(2); + if !cur.hidden() && !take(&mut skip_chars) { + let cp = out.code_page(cur.font); + out.byte(v, cp); + } + } + } + b'*' => { + cur.skip = true; + first = false; + star = true; + } + b'~' => symbol(&mut out, &cur, '\u{A0}', &mut first), + b'_' => { + let hyphen = if out.html { '\u{AD}' } else { '\u{2011}' }; + symbol(&mut out, &cur, hyphen, &mut first); + } + b'-' | b'|' | b':' => first = false, + n if n.is_ascii_alphabetic() => { + let from = i.saturating_sub(1); + while rtf.get(i).is_some_and(u8::is_ascii_alphabetic) { + i = i.saturating_add(1); + } + let word = rtf.get(from..i).and_then(|w| std::str::from_utf8(w).ok()).unwrap_or_default(); + let word = if word.len() > 32 { word.get(..32).unwrap_or(word) } else { word }; + // An optional parameter, then at most one space of delimiter. + let mut neg = false; + if rtf.get(i) == Some(&b'-') { + neg = true; + i = i.saturating_add(1); + } + let mut num: Option = None; + while let Some(d) = rtf.get(i).filter(|d| d.is_ascii_digit()) { + num = Some(num.unwrap_or(0).saturating_mul(10).saturating_add(i64::from(d.saturating_sub(b'0'))).min(1 << 40)); + i = i.saturating_add(1); + } + let num = num.map(|v| if neg { v.saturating_neg() } else { v }); + if rtf.get(i) == Some(&b' ') { + i = i.saturating_add(1); + } + let was_first = std::mem::replace(&mut first, false); + // The HTML of a tag is read whatever suppresses what is round it, by the + // document's own code page (2.2.3.2). + if html && std::mem::take(&mut star) && word == "htmltag" && !stack.last().is_some_and(|g| g.skip) { + cur.skip = false; + cur.in_tag = true; + cur.suppressed = false; + cur.font = None; + } + if was_first && SKIPPED.contains(&word.to_ascii_lowercase().as_str()) { + cur.skip = true; + cur.fonttbl = word.eq_ignore_ascii_case("fonttbl"); + } + skip_chars = 0; + control(word, num, &mut cur, &mut out, &mut skip_chars, &mut i, rtf); + } + _ => first = false, + } + } + b'\t' => { + first = false; + if !cur.hidden() && !take(&mut skip_chars) { + out.char('\t'); + } + } + 0x00..=0x1F => {} + _ => { + first = false; + if cur.hidden() || take(&mut skip_chars) { + continue; + } + if b.is_ascii() { + out.char(char::from(b)); + } else { + let cp = out.code_page(cur.font); + out.byte(b, cp); + } + } + } + } + out.flush(); + out.flush_high(); + let cut = out.cut; + Ok((out.text, cut)) +} + +fn take(skip: &mut u32) -> bool { + if *skip > 0 { + *skip = skip.saturating_sub(1); + true + } else { + false + } +} + +fn symbol(out: &mut Out, cur: &Group, c: char, first: &mut bool) { + *first = false; + if !cur.hidden() { + out.char(c); + } +} + +fn control(word: &str, num: Option, cur: &mut Group, out: &mut Out, skip_chars: &mut u32, i: &mut usize, rtf: &[u8]) { + match word { + "ansicpg" => out.ansicpg = num.and_then(|n| u32::try_from(n).ok()), + "mac" => out.mac = true, + // The fonts: which character set each has, and which one the text is in. + "f" if cur.fonttbl => cur.defining = num.unwrap_or(0), + "fcharset" if cur.fonttbl => { + if let Some(cs) = num + && (out.fonts.len() < MAX_FONTS || out.fonts.contains_key(&cur.defining)) + { + out.fonts.insert(cur.defining, cs); + } + } + "f" if !cur.skip => cur.font = num, + "deff" => out.deff = num, + "plain" if !cur.skip => cur.font = None, + "uc" => cur.uc = num.and_then(|n| u32::try_from(n).ok()).unwrap_or(1).min(16), + "htmlrtf" if out.html && !cur.in_tag => cur.suppressed = num != Some(0), + "u" if !cur.hidden() => { + if let Some(n) = num { + // A negative number is the unit minus 65536; the fallback after it is skipped. + let unit = if n < 0 { n.saturating_add(65536) } else { n }; + out.unit(u16::try_from(unit & 0xFFFF).unwrap_or(0xFFFD)); + *skip_chars = cur.uc; + } + } + "bin" => { + let n = num.and_then(|n| usize::try_from(n).ok()).unwrap_or(0); + *i = i.saturating_add(n).min(rtf.len()); + } + "par" | "line" if !cur.hidden() => out.char('\n'), + "sect" | "page" | "row" if !cur.hidden() && !out.html => out.char('\n'), + "tab" if !cur.hidden() => out.char('\t'), + "cell" if !cur.hidden() && !out.html => out.char('\t'), + "emdash" if !cur.hidden() => out.char('\u{2014}'), + "endash" if !cur.hidden() => out.char('\u{2013}'), + "lquote" if !cur.hidden() => out.char('\u{2018}'), + "rquote" if !cur.hidden() => out.char('\u{2019}'), + "ldblquote" if !cur.hidden() => out.char('\u{201C}'), + "rdblquote" if !cur.hidden() => out.char('\u{201D}'), + "bullet" if !cur.hidden() => out.char('\u{2022}'), + "emspace" | "enspace" | "qmspace" if !cur.hidden() => out.char(' '), + _ => {} + } +} + +/// At most one blank line in a row, and no blanks at the ends. +fn tidy(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + let mut newlines = 0u32; + for c in s.chars() { + if c == '\n' { + newlines = newlines.saturating_add(1); + if newlines > 2 { + continue; + } + } else { + newlines = 0; + } + out.push(c); + } + out.trim().to_string() +} + +#[cfg(test)] +#[path = "outlook_tests.rs"] +mod tests; diff --git a/crates/rtf/src/outlook_tests.rs b/crates/rtf/src/outlook_tests.rs new file mode 100644 index 000000000..7eb41dbd2 --- /dev/null +++ b/crates/rtf/src/outlook_tests.rs @@ -0,0 +1,385 @@ +//! Compressed rich text ([MS-OXRTFCP] section 3 samples) and the text of RTF. + +use proptest::prelude::*; + +use super::{Error, MAX_RTF_DEPTH, crc32, decompress_rtf, rtf_html, rtf_text}; + +/// [MS-OXRTFCP] 3.1.1: a simple compressed RTF. +const EXAMPLE_1: [u8; 49] = [ + 0x2d, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x4c, 0x5a, 0x46, 0x75, 0xf1, 0xc5, 0xc7, 0xa7, 0x03, 0x00, 0x0a, 0x00, 0x72, 0x63, 0x70, 0x67, + 0x31, 0x32, 0x35, 0x42, 0x32, 0x0a, 0xf3, 0x20, 0x68, 0x65, 0x6c, 0x09, 0x00, 0x20, 0x62, 0x77, 0x05, 0xb0, 0x6c, 0x64, 0x7d, 0x0a, 0x80, 0x0f, + 0xa0, +]; + +/// [MS-OXRTFCP] 3.1.2: a reference that reads past the write position. +const EXAMPLE_2: [u8; 30] = [ + 0x1a, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x4c, 0x5a, 0x46, 0x75, 0xe2, 0xd4, 0x4b, 0x51, 0x41, 0x00, 0x04, 0x20, 0x57, 0x58, 0x59, 0x5a, + 0x0d, 0x6e, 0x7d, 0x01, 0x0e, 0xb0, +]; + +fn mela(rtf: &[u8]) -> Vec { + let mut v = Vec::new(); + v.extend_from_slice(&u32::try_from(rtf.len() + 12).unwrap().to_le_bytes()); + v.extend_from_slice(&u32::try_from(rtf.len()).unwrap().to_le_bytes()); + v.extend_from_slice(b"MELA"); + v.extend_from_slice(&[0; 4]); + v.extend_from_slice(rtf); + v +} + +#[test] +fn spec_example_1_decompresses() { + let out = decompress_rtf(&EXAMPLE_1, 1 << 20).unwrap(); + assert_eq!(out, b"{\\rtf1\\ansi\\ansicpg1252\\pard hello world}\r\n"); + assert_eq!(crc32(&EXAMPLE_1[16..]), 0xa7c7_c5f1); + assert_eq!(rtf_text(&out, 1000).unwrap(), ("hello world".to_string(), false)); +} + +#[test] +fn spec_example_2_reads_past_the_write_position() { + let out = decompress_rtf(&EXAMPLE_2, 1 << 20).unwrap(); + assert_eq!(out, b"{\\rtf1 WXYZWXYZWXYZWXYZWXYZ}"); + assert_eq!(out.len(), 0x1c); +} + +#[test] +fn uncompressed_is_taken_as_it_is() { + let rtf = b"{\\rtf1 plain}"; + assert_eq!(decompress_rtf(&mela(rtf), 100).unwrap(), rtf); + // A header that lies about its size is held to what is there. + let mut lying = mela(rtf); + lying.get_mut(4..8).unwrap().copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(decompress_rtf(&lying, 100).unwrap(), rtf); + assert!(matches!(decompress_rtf(&mela(rtf), 3), Err(Error::TooLarge))); +} + +#[test] +fn a_bad_crc_is_an_error() { + let mut bad = EXAMPLE_1; + bad[12] ^= 1; + assert!(matches!(decompress_rtf(&bad, 1 << 20), Err(Error::BadChecksum))); + let mut bad = EXAMPLE_1; + bad[20] ^= 0x40; + assert!(decompress_rtf(&bad, 1 << 20).is_err()); +} + +#[test] +fn truncation_is_an_error_at_every_length() { + for n in 0..EXAMPLE_1.len() { + assert!(decompress_rtf(&EXAMPLE_1[..n], 1 << 20).is_err(), "{n}"); + } + // A header whose size is right but whose runs have no end mark. + let mut v = EXAMPLE_1[..40].to_vec(); + v[..4].copy_from_slice(&28u32.to_le_bytes()); + assert!(decompress_rtf(&v, 1 << 20).is_err()); +} + +#[test] +fn a_header_that_lies_about_the_size_costs_nothing() { + let mut huge = EXAMPLE_1; + huge[4..8].copy_from_slice(&u32::MAX.to_le_bytes()); + assert!(matches!(decompress_rtf(&huge, 1 << 20), Err(Error::TooLarge))); + // Under the cap it is the stream that decides. + let mut small = EXAMPLE_1; + small[4..8].copy_from_slice(&1000u32.to_le_bytes()); + assert_eq!(decompress_rtf(&small, 1 << 20).unwrap().len(), 0x2b); +} + +#[test] +fn output_is_capped_while_it_is_made() { + assert!(matches!(decompress_rtf(&EXAMPLE_1, 10), Err(Error::TooLarge))); + // A long stream (no end mark) is stopped at the cap, not at its end. + let mut body = Vec::new(); + for _ in 0..200_000 { + body.push(0x00); + body.extend_from_slice(b"abcdefgh"); + } + let mut v = Vec::new(); + v.extend_from_slice(&u32::try_from(body.len() + 12).unwrap().to_le_bytes()); + v.extend_from_slice(&0u32.to_le_bytes()); + v.extend_from_slice(b"LZFu"); + v.extend_from_slice(&crc32(&body).to_le_bytes()); + v.extend_from_slice(&body); + assert!(matches!(decompress_rtf(&v, 1 << 16), Err(Error::TooLarge))); +} + +#[test] +fn an_unknown_kind_is_an_error() { + let mut v = EXAMPLE_1; + v[8..12].copy_from_slice(b"XXXX"); + assert!(matches!(decompress_rtf(&v, 100), Err(Error::Corrupt(_)))); +} + +fn text(rtf: &str) -> String { + rtf_text(rtf.as_bytes(), 1 << 20).unwrap().0 +} + +#[test] +fn paragraphs_and_tabs() { + assert_eq!(text("{\\rtf1\\ansi\\deff0 One\\par Two\\line Three\\tab x\\par}"), "One\nTwo\nThree\tx"); + assert_eq!(text("{\\rtf1 a\\par\\par\\par\\par\\par b}"), "a\n\nb"); + assert_eq!(text("{\\rtf1 braces \\{ \\} and \\\\ end}"), "braces { } and \\ end"); + assert_eq!(text("{\\rtf1 line\r\nbreaks in the file are not breaks}"), "linebreaks in the file are not breaks"); +} + +#[test] +fn hex_escapes_use_the_code_page() { + assert_eq!(text("{\\rtf1\\ansi\\ansicpg1252 caf\\'e9 \\'80}"), "caf\u{e9} \u{20ac}"); + assert_eq!(text("{\\rtf1\\ansi\\ansicpg1251 \\'cf\\'f0\\'e8\\'e2\\'e5\\'f2}"), "\u{41f}\u{440}\u{438}\u{432}\u{435}\u{442}"); + // Two bytes of one character (Shift JIS). + assert_eq!(text("{\\rtf1\\ansi\\ansicpg932 \\'82\\'a0}"), "\u{3042}"); + // No code page: Windows-1252. + assert_eq!(text("{\\rtf1 \\'e9}"), "\u{e9}"); +} + +#[test] +fn unicode_escapes_skip_their_fallback() { + assert_eq!(text("{\\rtf1\\uc1\\u8364? x}"), "\u{20ac} x"); + assert_eq!(text("{\\rtf1\\uc2\\u8364\\'80\\'80 x}"), "\u{20ac} x"); + assert_eq!(text("{\\rtf1\\uc0\\u8364 x}"), "\u{20ac}x"); + assert_eq!(text("{\\rtf1\\u-3913 ?}"), "\u{f0b7}"); + assert_eq!(text("{\\rtf1\\u-10179?\\u-8704? x}"), "\u{1f600} x"); + // A lone surrogate is a replacement character, not a panic. + assert_eq!(text("{\\rtf1\\u-10179? x}"), "\u{fffd} x"); + // \ucN belongs to its group. + assert_eq!(text("{\\rtf1{\\uc0}\\u65?B}"), "AB"); +} + +#[test] +fn nested_groups_and_skipped_destinations() { + let rtf = "{\\rtf1\\ansi{\\fonttbl{\\f0 Arial;}}{\\colortbl;\\red0\\green0\\blue0;}{\\stylesheet{\\s1 Heading;}}{\\*\\generator Hidden;}{\\info{\\title T}}\ + {\\pict\\wmetafile8 0102030405}{\\header Head}\\pard{\\b bold {\\i both}} plain{\\*\\htmltag64 }}"; + assert_eq!(text(rtf), "bold both plain"); +} + +#[test] +fn encapsulated_text_and_html_give_what_the_document_shows() { + assert_eq!(text("{\\rtf1\\ansi\\fromtext\\deff0{\\fonttbl{\\f0\\fswiss Arial;}}\\pard Hello\\par}"), "Hello"); + let html = "{\\rtf1\\ansi\\fromhtml1 {\\*\\htmltag19 }\\htmlrtf {\\htmlrtf0 Hello {\\*\\htmltag84 }there\\htmlrtf \\par\\htmlrtf0 }}"; + assert_eq!(text(html), "Hello there"); +} + +#[test] +fn bin_data_is_skipped_by_its_count() { + let mut rtf = b"{\\rtf1 a{\\pict\\bin5 }}{b".to_vec(); + let at = rtf.iter().position(|&b| b == b'5').unwrap() + 2; + rtf.splice(at..at, *b"}{\\par"); + assert_eq!(rtf_text(&rtf, 100).unwrap().0, "ab"); + // A count past the end is the end. + assert_eq!(rtf_text(b"{\\rtf1 a\\bin99999999999 bc", 100).unwrap().0, "a"); +} + +#[test] +fn bad_input_is_an_error_or_what_could_be_read() { + assert!(rtf_text(b"plain text", 100).is_err()); + assert!(rtf_text(b"", 100).is_err()); + assert_eq!(text("{\\rtf1 open {group {never closed"), "open group never closed"); + assert_eq!(text("{\\rtf1 closed}} too} many} text"), "closed too many text"); + assert_eq!(text("{\\rtf1 ends in a slash\\"), "ends in a slash"); + assert_eq!(text("{\\rtf1 \\'zz bad hex}"), "zz bad hex"); +} + +#[test] +fn depth_is_capped() { + let deep = format!("{{\\rtf1 {}x{}", "{".repeat(MAX_RTF_DEPTH + 10), "}".repeat(MAX_RTF_DEPTH + 10)); + assert!(matches!(rtf_text(deep.as_bytes(), 100), Err(Error::Corrupt(_)))); + let ok = format!("{{\\rtf1 {}x{}", "{".repeat(100), "}".repeat(101)); + assert_eq!(text(&ok), "x"); +} + +#[test] +fn text_is_capped() { + let long = format!("{{\\rtf1 {}}}", "word ".repeat(100_000)); + let (t, cut) = rtf_text(long.as_bytes(), 1000).unwrap(); + assert!(t.len() <= 1000 && cut); + assert!(!rtf_text(b"{\\rtf1 short}", 1000).unwrap().1); + // Folded blank lines still say the text was cut. + let folded = format!("{{\\rtf1 {}}}", "x\\par\\par\\par\\par ".repeat(1000)); + let (t, cut) = rtf_text(folded.as_bytes(), 1000).unwrap(); + assert!(cut && t.len() < 1000, "{}", t.len()); + let multibyte = format!("{{\\rtf1 {}}}", "\\u233?".repeat(10_000)); + let t = rtf_text(multibyte.as_bytes(), 101).unwrap().0; + assert!(t.len() <= 102 && t.chars().all(|c| c == '\u{e9}')); +} + +#[test] +fn each_font_has_its_code_page() { + // RichEdit's shape: ANSI document, a font of its own character set. + assert_eq!( + text("{\\rtf1\\ansi\\ansicpg1252{\\fonttbl{\\f0\\fnil\\fcharset128 Yu Gothic;}}\\f0\\'93\\'fa\\'96\\'7b\\'8c\\'ea}"), + "\u{65e5}\u{672c}\u{8a9e}" + ); + assert_eq!(text("{\\rtf1\\ansi\\ansicpg1252{\\fonttbl{\\f0\\fnil\\fcharset204 Calibri;}}\\f0\\'cf\\'f0\\'e8 x}"), "\u{41f}\u{440}\u{438} x"); + // The font goes with its group. + assert_eq!( + text("{\\rtf1\\ansi\\ansicpg1252{\\fonttbl{\\f0\\fcharset204 A;}{\\f1\\fcharset161 B;}}{\\f0 \\'cf}\\'cf{\\f1\\'e1}\\f0\\'cf}"), + "\u{41f}\u{cf}\u{3b1}\u{41f}" + ); + // A change of font in the middle of a run of bytes. + assert_eq!(text("{\\rtf1\\ansi{\\fonttbl{\\f0\\fcharset204 A;}{\\f1\\fcharset0 B;}}\\f0\\'cf\\f1\\'cf\\f0\\'cf}"), "\u{41f}\u{cf}\u{41f}"); + // A flat font table, \deff and no charset at all. + assert_eq!(text("{\\rtf1\\ansi\\deff1{\\fonttbl\\f0\\fcharset0 A;\\f1\\fcharset204 B;}\\'cf}"), "\u{41f}"); + assert_eq!(text("{\\rtf1\\ansi\\ansicpg1251{\\fonttbl{\\f0 A;}}\\f0\\'a8\\f9\\'a8}"), "\u{401}\u{401}"); +} + +#[test] +fn the_sample_of_ms_oxrtfex_reads_its_font_charset() { + // [MS-OXRTFEX] 3.1: a 1251 document, and one font marked \fcharset0 for ¨ (0xA8, U+00A8). + let rtf = "{\\rtf1\\ANSI\\ansicpg1251\\fromhtml1 \\deff0\r\n{\\fonttbl {\\f0\\fmodern Courier New;}{\\f1\\fswiss Arial;}{\\f2\\fswiss\\fcharset0 Arial;}}\ + \\uc1\\pard\\plain\\deftab360 \\f0\\fs24 {\\htmlrtf \\f1 \\htmlrtf0 \\'cf {\\*\\htmltag ¨}\\htmlrtf {\\f2\\'a8}\\htmlrtf0 \\'a8}}"; + assert_eq!(text(rtf), "\u{41f} \u{a8}\u{401}"); +} + +#[test] +fn bytes_past_the_buffer_are_not_dropped() { + let dbcs = format!("{{\\rtf1\\ansi\\ansicpg932 {}}}", "\\'82\\'a0".repeat(3000)); + assert_eq!(text(&dbcs), "\u{3042}".repeat(3000)); + // An odd byte first, so the 4096-byte mark falls inside a pair. + let shifted = format!("{{\\rtf1\\ansi\\ansicpg932 \\'41{}}}", "\\'82\\'a0".repeat(3000)); + assert_eq!(text(&shifted), format!("A{}", "\u{3042}".repeat(3000))); + let single = format!("{{\\rtf1\\ansi\\ansicpg1252 {}}}", "\\'e9".repeat(5000)); + assert_eq!(text(&single), "\u{e9}".repeat(5000)); + let raw: Vec = [b"{\\rtf1\\ansi\\ansicpg1252 ".as_slice(), &[0xE9u8; 5000], b"}"].concat(); + assert_eq!(rtf_text(&raw, 1 << 20).unwrap().0, "\u{e9}".repeat(5000)); +} + +#[test] +fn ansicpg_wins_over_mac_in_either_order() { + assert_eq!(text("{\\rtf1\\ansicpg1251\\mac \\'cf}"), "\u{41f}"); + assert_eq!(text("{\\rtf1\\mac\\ansicpg1251 \\'cf}"), "\u{41f}"); + assert_eq!(text("{\\rtf1\\mac \\'a5}"), "\u{2022}"); +} + +#[test] +fn control_characters_are_not_text() { + assert_eq!(text("{\\rtf1 a\\'00b\\u0? c\\'07\\u1\\u9? d\\u10?e}"), "ab c\t d\ne"); +} + +#[test] +fn a_literal_tab_and_a_backslash_line_end() { + assert_eq!(text("{\\rtf1 a\tb\\\r\nc\\\nd\\\re}"), "a\tb\nc\nd\ne"); +} + +#[test] +fn the_code_pages_of_the_mail_are_the_code_pages_of_the_text() { + // Pages the first version of the text reader lacked: KOI8-R, ISO 8859-2, GB 18030's, EUC-KR's other number. + assert_eq!(text("{\\rtf1\\ansi\\ansicpg20866 \\'d0\\'d2}"), "\u{43f}\u{440}"); + assert_eq!(text("{\\rtf1\\ansi\\ansicpg28592 \\'b1}"), "\u{105}"); + assert_eq!(text("{\\rtf1\\ansi\\ansicpg51949 \\'b0\\'a1}"), "\u{ac00}"); + assert_eq!(craft_mime::charset::code_page_label(20866), Some("koi8-r")); +} + +proptest! { + #![proptest_config(ProptestConfig { cases: 400, ..ProptestConfig::default() })] + + #[test] + fn arbitrary_bytes_never_panic(bytes in proptest::collection::vec(any::(), 0..4096)) { + let _ = decompress_rtf(&bytes, 1 << 16); + let _ = rtf_text(&bytes, 1 << 12); + let mut with_start = b"{\\rtf1".to_vec(); + with_start.extend_from_slice(&bytes); + let _ = rtf_text(&with_start, 1 << 12); + } + + #[test] + fn changed_streams_never_panic(at in 0usize..49, to in any::(), cut in 0usize..49) { + let mut v = EXAMPLE_1.to_vec(); + if let Some(b) = v.get_mut(at) { *b = to; } + v.truncate(cut.max(1)); + if let Ok(rtf) = decompress_rtf(&v, 1 << 12) { let _ = rtf_text(&rtf, 100); } + } + + #[test] + fn rtf_with_valid_start_is_bounded(body in "[\\\\{}a-z0-9 '*-]{0,600}", max in 0usize..300) { + let rtf = format!("{{\\rtf1 {body}"); + if let Ok((t, _)) = rtf_text(rtf.as_bytes(), max) { prop_assert!(t.len() <= max.saturating_add(4)); } + } +} + +/// The document of [MS-OXRTFEX] 2.1.3.1.3's kind: tags in groups, what isn't HTML suppressed. +const ENCAPSULATED: &[u8] = + b"{\\rtf1\\ansi\\ansicpg1252\\fromhtml1 \\deff0{\\fonttbl{\\f0\\fswiss Arial;}{\\f1\\fcharset204 Cyr;}}{\\colortbl;\\red0;}\ +{\\*\\htmltag64

}{\\htmlrtf \\f1 \\htmlrtf0 caf\\'e9 {\\*\\htmltag }{\\htmlrtf \\b \\htmlrtf0 bold{\\*\\htmltag }}\ +\\htmlrtf \\par \\htmlrtf0 {\\*\\htmltag &}\\htmlrtf &\\htmlrtf0 \\u8364?{\\*\\mhtmltag }{\\*\\other no}{\\*\\htmltag

}}}"; + +#[test] +fn encapsulated_html_comes_out_as_it_was() { + let (html, cut) = rtf_html(ENCAPSULATED, 1 << 16).unwrap().unwrap(); + assert!(!cut); + assert_eq!(html, "

caf\u{439} bold&\u{20AC}

"); +} + +#[test] +fn a_tag_is_kept_even_where_the_text_round_it_is_suppressed() { + let rtf = b"{\\rtf1\\fromhtml1 \\htmlrtf {\\*\\htmltag }gone\\htmlrtf0 kept}"; + assert_eq!(rtf_html(rtf, 100).unwrap().unwrap().0, "kept"); +} + +#[test] +fn only_a_document_that_says_so_is_html() { + assert_eq!(rtf_html(b"{\\rtf1\\ansi plain}", 100).unwrap(), None); + assert_eq!(rtf_html(b"{\\rtf1\\fromtext hello}", 100).unwrap(), None); + assert_eq!(rtf_html(b"{\\rtf1 text\\fromhtml1 }", 100).unwrap(), None, "past a token that isn't a control word"); + assert_eq!(rtf_html(b"{\\rtf1\\a\\b\\c\\d\\e\\f\\g\\h\\i\\j\\fromhtml1 }", 100).unwrap(), None, "past the first ten tokens"); + assert_eq!(rtf_html(b"not rich text", 100).unwrap(), None); +} + +#[test] +fn encapsulated_html_is_capped_and_a_deep_nest_refused() { + let mut rtf = b"{\\rtf1\\fromhtml1 ".to_vec(); + rtf.extend(std::iter::repeat_n(b'x', 1000)); + let (html, cut) = rtf_html(&rtf, 10).unwrap().unwrap(); + assert!(cut && html.len() <= 10); + let mut deep = b"{\\rtf1\\fromhtml1 ".to_vec(); + deep.extend(std::iter::repeat_n(b'{', MAX_RTF_DEPTH + 2)); + assert!(rtf_html(&deep, 100).is_err()); +} + +#[test] +fn the_text_of_encapsulated_html_is_still_what_it_shows() { + let rtf = b"{\\rtf1\\fromhtml1 {\\*\\htmltag

}\\htmlrtf x\\htmlrtf0 hello}"; + assert_eq!(rtf_text(rtf, 100).unwrap().0, "xhello"); +} + +proptest! { + #[test] + fn any_bytes_read_as_html_never_panic(data in proptest::collection::vec(any::(), 0..2000)) { + let mut rtf = b"{\\rtf1\\fromhtml1 ".to_vec(); + rtf.extend_from_slice(&data); + let _ = rtf_html(&rtf, 1 << 12); + } +} + +/// The font character sets are the crate's table, but for ANSI (0), which is Windows-1252 +/// whatever the document's code page, and the sets with no text code page here. +#[test] +fn font_character_sets_name_their_code_pages() { + let want = [ + (0, 1252), + (77, 10000), + (128, 932), + (129, 949), + (134, 936), + (136, 950), + (161, 1253), + (162, 1254), + (163, 1258), + (177, 1255), + (178, 1256), + (186, 1257), + (204, 1251), + (222, 874), + (238, 1250), + ]; + for charset in -1..=256 { + let expected = want.iter().find(|(c, _)| *c == charset).map(|(_, cp)| *cp); + assert_eq!(super::charset_code_page(charset), expected, "{charset}"); + } +} + +#[test] +fn the_checksum_is_the_specifications() { + // [MS-OXRTFCP] 3.2.3: the CRC of the compressed bytes of the first example is its header's. + assert_eq!(crc32(&EXAMPLE_1[16..]), u32::from_le_bytes([0xf1, 0xc5, 0xc7, 0xa7])); + assert_eq!(crc32(b""), 0); +} From 2b35d243241bc4c1aaa7a1cc89657c1aad5b3d02 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:25:58 -0500 Subject: [PATCH 053/120] =?UTF-8?q?craft-audio:=20an=20AAC=20program=20con?= =?UTF-8?q?fig=20element=20shaped=20like=203.0=E2=80=935.1=20gets=20those?= =?UTF-8?q?=20speakers=20(#964)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * craft-audio: a program config element in the shape of a channel configuration gets its speakers An AAC stream with channel configuration 0 whose program config element lists a centre, a front pair and then exactly the elements of configuration 3 to 6 decoded as C L R Ls Rs LFE and was reported as an unordered layout, so a caller reading it in WAV order sent the centre to the left speaker. Such a stream now has the configuration's layout and WAV order. Any other element list keeps the decoded order, as before. Ported from storytold/filmcraft#4280 (aac_stream_order in filmcraft-codecs). * craft-audio: the ADTS program config oracle expects 5.1 in WAV order ffmpeg 9 writes its 5.1 program config element centre first, so the stream now has 5.1's speakers: each decoded channel must match ffmpeg's channel of the same index. An element list in another order keeps the old check. --------- Co-authored-by: mpratsaki <110930793+mpratsaki@users.noreply.github.com> --- crates/audio/src/aac/mod.rs | 44 +++++++++++++++++++++ crates/audio/src/track.rs | 5 ++- crates/audio/tests/codecs/routing_oracle.rs | 14 +++++-- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/crates/audio/src/aac/mod.rs b/crates/audio/src/aac/mod.rs index eb1c7d40a..34b081ea6 100644 --- a/crates/audio/src/aac/mod.rs +++ b/crates/audio/src/aac/mod.rs @@ -113,6 +113,26 @@ pub fn wav_order(channel_config: u8) -> Option<&'static [usize]> { }) } +/// The channel configuration whose speakers a stream has: its own, or for configuration 0 the +/// configuration 3–6 that its program config element spells out. A program config element lists +/// front, side, back and LFE elements in that order, so one that opens with a centre and a front +/// pair and goes on with exactly the elements of a configuration has that configuration's speakers +/// (and [`wav_order`]). Any other list has no speaker order that can be told, and gives 0: ffmpeg's +/// 5.1(side), for one, lists the pair first and already decodes in WAV order. +#[cfg(feature = "aac-decode")] +pub fn speaker_config(asc: &AudioSpecificConfig) -> u8 { + if asc.channel_config != 0 { + return asc.channel_config; + } + let centre_then_pair = asc.pce.as_ref().is_some_and(|p| p.front.iter().map(|e| e.0).eq([false, true])); + if !centre_then_pair { + return 0; + } + let elements = asc.layout(); + let same = |config: &u8| config_layout(*config).is_some_and(|l| l.iter().map(|e| e.0).eq(elements.iter().map(|e| e.0))); + (3..=6).find(same).unwrap_or(0) +} + /// The order the [`Encoder`] takes its input in, from WAV order: for each encoder input channel /// (AAC element order, see [`EncoderConfig::channels`]), the WAV-order channel to feed it. The /// inverse of [`wav_order`] for 3 to 7 channels; `None` for mono and stereo (already in order) and @@ -279,6 +299,30 @@ mod tests { mod order_tests { use super::*; + /// A program config element in the shape of a channel configuration has that configuration's + /// speakers, with the centre first. ffmpeg's 5.1(side) lists its pair first and already decodes + /// in WAV order, so it stays as it is. + #[cfg(feature = "aac-decode")] + #[test] + fn a_program_config_element_takes_the_configuration_it_matches() { + let with = |pce| AudioSpecificConfig { pce: Some(pce), ..AudioSpecificConfig::lc(3, 0) }; + let centre_first = ProgramConfig { front: vec![(false, 0), (true, 0)], back: vec![(true, 1)], lfe: vec![0], ..Default::default() }; + assert_eq!(speaker_config(&with(centre_first)), 6); + let three = ProgramConfig { front: vec![(false, 0), (true, 0)], ..Default::default() }; + assert_eq!(speaker_config(&with(three)), 3); + let ffmpeg = ProgramConfig { front: vec![(true, 0), (false, 0)], side: vec![(false, 1)], back: vec![(true, 1)], ..Default::default() }; + assert_eq!(speaker_config(&with(ffmpeg)), 0); + // the same element types with no front pair: the side pair is not left and right + let no_front_pair = + ProgramConfig { front: vec![(false, 0)], side: vec![(true, 0)], back: vec![(true, 1)], lfe: vec![0], ..Default::default() }; + assert_eq!(speaker_config(&with(no_front_pair)), 0); + // 7.1 in a program config element is not one of the unambiguous configurations + let wide = + ProgramConfig { front: vec![(false, 0), (true, 0)], side: vec![(true, 1)], back: vec![(true, 2)], lfe: vec![0], ..Default::default() }; + assert_eq!(speaker_config(&with(wide)), 0); + assert_eq!(speaker_config(&AudioSpecificConfig::lc(3, 6)), 6); + } + /// `aac_order` undoes `wav_order` for each channel count the encoder maps to a configuration /// (3–6 channels: configurations 3–6; 7: configuration 11). #[test] diff --git a/crates/audio/src/track.rs b/crates/audio/src/track.rs index 3d9ed7dfb..af984d718 100644 --- a/crates/audio/src/track.rs +++ b/crates/audio/src/track.rs @@ -156,7 +156,8 @@ impl TrackDecoder { let codec = if up.is_some() { CodecId::HeAac } else { CodecId::Aac }; // Only twice the core rate is upsampled to; any other signalled rate plays the core. let out = if up.is_some() { out } else { dec.sample_rate() }; - let cfg = dec.config().channel_config; + // a program config element that spells out a configuration has its speakers + let cfg = aac::speaker_config(dec.config()); let layout = aac_layout(cfg, dec.channels()); let order = layout.is_ordered().then(|| aac::wav_order(cfg)).flatten(); (Inner::Aac { dec: Box::new(dec), up, order, adts_format }, codec, out, layout, SampleFormat::Compressed) @@ -1079,6 +1080,8 @@ mod tests { let mut dec = TrackDecoder::new(&aac_params(), &[&frame]).unwrap(); assert_eq!(dec.channels(), 3); assert_eq!(dec.decode(&frame).unwrap().channels.len(), 3); + // its element lists a centre and a front pair, as configuration 3 does: L R C, in WAV order + assert_eq!(dec.layout(), ChannelLayout::SURROUND_3_0); // no program config element where one is needed let bare = aac::AdtsHeader::write(2, 3, 0, 1, 0x7FF).unwrap(); let bare = [&bare[..], &[0xE0]].concat(); diff --git a/crates/audio/tests/codecs/routing_oracle.rs b/crates/audio/tests/codecs/routing_oracle.rs index c35a89002..6472979b3 100644 --- a/crates/audio/tests/codecs/routing_oracle.rs +++ b/crates/audio/tests/codecs/routing_oracle.rs @@ -669,10 +669,14 @@ fn adts_with_a_program_config_element_decodes() { assert_eq!(bytes[3] >> 6 | (bytes[2] & 1) << 2, 0, "the fixture signals its layout in a PCE"); let (info, buf) = decode(&bytes, None).unwrap(); assert_eq!((info.container, info.codec, buf.channels.len()), (c.container, c.codec, c.channels)); - assert_eq!(info.layout, c.layout); + // A program config element that lists a centre, a front pair and then the elements of 5.1 has + // 5.1's speakers, in WAV order like ffmpeg's decode; any other list keeps the decoded order, + // which ffmpeg versions write differently. + let ordered = info.layout.is_ordered(); + assert_eq!(info.layout, if ordered { ChannelLayout::SURROUND_5_1_SIDE } else { c.layout }); assert_eq!(buf.frames(), reference(&ff, &path).len() / c.channels); - // A program config's channels come in its own order, and which order ffmpeg writes differs by - // version: each of ours must match a different one of ffmpeg's. + // Each of our channels must match a different one of ffmpeg's: the same one when the layout is + // ordered. let refr = reference(&ff, &path); let mut matched = Vec::new(); for plane in &buf.channels { @@ -686,7 +690,9 @@ fn adts_with_a_program_config_element_decodes() { assert!(s >= c.min_snr, "channel {} matches ffmpeg's {best} at only {s:.1} dB", matched.len()); matched.push(best); } - matched.sort_unstable(); + if !ordered { + matched.sort_unstable(); + } assert_eq!(matched, (0..c.channels).collect::>()); } From 69d812dd7aeff92e94be339a1b1f3c708735a71c Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:29:51 -0500 Subject: [PATCH 054/120] craft-raw: Nikon NEF and NRW (#965) * craft-raw: the file-format layer of camera raw files, with DNG and CRW A new crate for #587 and #778: bytes in, the sensor data out as a neutral RawImage (samples, CFA, levels, active area, crop, orientation, colour tags, opcode lists, profile tables), and back for DNG. One Cargo feature per format: dng (read and write), jxl (JPEG XL DNG), crw (through craft-crw). The base is LightCraft's crates/raw at 5ff567a4: dng, dngwrite, tiffraw, unpack, ljpeg, jxl, semantic, preview, the parsing half of opcodes, profile and gaintable, the model and probe. Demosaicing and everything after it stay with the develop pipeline. Ported from PhotoCraft's and FilmCraft's DNG readers, each with a test: the default crop's per-tag defaults, an unusable crop falling back to the active area, non-finite white levels, the white default of a linearized image, the AsShotWhiteXY range, 8-bit-only lossy JPEG, lossy tiles that must cover their part of the image, and the CinemaDNG frame tags. * craft-raw: Hasselblad 3FR and FFF (feature hasselblad) RawFormat::CfaTiff decodes: the DNG-layout reader plus what these files add. Uncompressed data or Hasselblad's lossless JPEG (selection value 8), RGGB when no CFAPattern is stated, an FFF's colour matrix from the 3FR header it embeds, and the 8-bit RGB preview handed on as a baseline TIFF. Moved from LightCraft's vendor/hasselblad.rs at 5ff567a4 with its tests. Ported from PhotoCraft's reader: four BlackLevel values without a BlackLevelRepeatDim are one per CFA site in a file that is not a DNG. * craft-raw: Minolta MRW, Epson ERF, Mamiya MEF and Kodak DCS520C Four formats only LightCraft decodes, one feature each (mrw, erf, mef, kodak), moved from its vendor modules at 5ff567a4 with their tests; the MRW block container comes from lightcraft-meta. src/shared.rs holds the level helpers the maker decoders share. ERF and MEF state no white balance: the estimate LightCraft made from the preview is develop-side work, so wb_multipliers is None for them here. * craft-raw: shared scan budget for tagged previews, tile-sized lossy JPEG budget, bound on Deflate tile padding Ported from storytold/lightcraft#3695. Candidate preview ranges are deduplicated and charged to one byte budget and one header-walk budget; a lossy JPEG decoded for a tile may hold no more samples than the tile rounded up to whole MCUs (and never more than Limits::max_lossy_tile_samples); a Deflate chunk whose nominal size is mostly padding outside the image is refused before it is inflated. * craft-raw: lossless JPEG sample bound per referenced byte; previews larger than the file are passed over Ported from storytold/lightcraft#3910 (the parts whose code is in craft-raw today). check_image refuses a lossless JPEG image that declares more than 32 samples per referenced byte, counted once per chunk that points at the bytes so identical tiles stored once still decode. The Hasselblad RGB preview search passes over an image whose pixel bytes exceed the file and still takes a smaller complete one. * craft-raw: test for the Deflate padding bound * craft-raw: Pentax PEF and Samsung SRW Moved from LightCraft crates/raw (vendor/pef.rs, vendor/srw.rs) at 5ff567a4. The Huffman difference decoder and bit reader that PEF shares with the Nikon, Panasonic and Sony codings become the huffman module. * craft-raw: Canon CR2, with sRAW and mRAW Moved from LightCraft crates/raw (vendor/cr2.rs, vendor/cr2/sraw.rs) at 5ff567a4. The white-balance presets a CR2 lists come back as data in ColorData::kelvin_presets (the ordering from LightCraft's kelvin_presets.rs; the temperature and tint arithmetic stays with the developer). * craft-raw: an sRAW preview larger than the file is refused before its buffer is reserved Ported from storytold/lightcraft#3910 (cr2::sraw::read_preview). * WIP craft-raw: Nikon NEF/NRW (does not compile yet: limits threading, develop() calls in tests) * craft-raw: Nikon NEF/NRW, with PhotoCraft's maker-note compression routing and 12-bit D4 white level --------- Co-authored-by: radialmonster --- NOTICE | 2 +- README.md | 3 +- crates/raw/Cargo.toml | 6 +- crates/raw/README.md | 10 + crates/raw/docs/nikon-lens-corrections.md | 101 ++ crates/raw/src/lib.rs | 10 + crates/raw/src/nef.rs | 1510 +++++++++++++++++++++ crates/raw/src/nef_lens.rs | 259 ++++ crates/raw/src/nefc.rs | 794 +++++++++++ crates/raw/tests/it/vendor_robust.rs | 2 + 10 files changed, 2692 insertions(+), 5 deletions(-) create mode 100644 crates/raw/docs/nikon-lens-corrections.md create mode 100644 crates/raw/src/nef.rs create mode 100644 crates/raw/src/nef_lens.rs create mode 100644 crates/raw/src/nefc.rs diff --git a/NOTICE b/NOTICE index 3bca3ae0d..eb22134a2 100644 --- a/NOTICE +++ b/NOTICE @@ -93,7 +93,7 @@ crates/raw (craft-raw) is moved from LightCraft, with behaviour ported from two - LightCraft, crates/raw (src/lib.rs, dng.rs, dngwrite.rs, tiffraw.rs, unpack.rs, ljpeg.rs, jxl.rs, opcodes.rs, profile.rs, gaintable.rs, semantic.rs, preview.rs, vendor/crw.rs, vendor/hasselblad.rs, vendor.rs (shared level helpers), vendor/mrw.rs, vendor/erf.rs, vendor/mef.rs, vendor/kodak.rs, vendor/pef.rs, - vendor/srw.rs, vendor/cr2.rs, vendor/cr2/sraw.rs, kelvin_presets.rs (the preset ordering), tests/it/vendor_robust.rs and their tests) and crates/meta/src/mrw.rs (the MRW block container), + vendor/srw.rs, vendor/cr2.rs, vendor/cr2/sraw.rs, vendor/nef.rs, vendor/nefc.rs, vendor/nef_lens.rs, docs/nikon-lens-corrections.md, kelvin_presets.rs (the preset ordering), tests/it/vendor_robust.rs and their tests) and crates/meta/src/mrw.rs (the MRW block container), revision 5ff567a4a2e067bef1b6bce152c11e7f727ccbaf. https://github.com/storytold/lightcraft Copyright (c) 2026 ArtCraft Team and the LightCraft contributors. diff --git a/README.md b/README.md index 5b51e5ebd..951fc1ab9 100644 --- a/README.md +++ b/README.md @@ -134,8 +134,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " Result { RawFormat::Mef => mef::decode(bytes, mode, limits), #[cfg(feature = "cr2")] RawFormat::Cr2 => cr2::decode(bytes, mode, limits), + #[cfg(feature = "nef")] + RawFormat::Nef | RawFormat::Nrw => nef::decode(bytes, mode, limits), #[cfg(feature = "pef")] RawFormat::Pef => pef::decode(bytes, mode, limits), #[cfg(feature = "srw")] diff --git a/crates/raw/src/nef.rs b/crates/raw/src/nef.rs new file mode 100644 index 000000000..86ef0e1b2 --- /dev/null +++ b/crates/raw/src/nef.rs @@ -0,0 +1,1510 @@ +//! Nikon NEF / NRW — uncompressed and Huffman-compressed (lossless, lossy) variants. +//! +//! Sources: TIFF 6.0 (the raw image is a standard CFA SubIFD), Laurent Clévy's NEF structure notes (prose: IFD +//! layout, SubIFDs, maker note header) and the ExifTool Nikon tag-name documentation (`0x000c` WB_RBLevels, +//! `0x003d` BlackLevel, `0x0096` NEFLinearizationTable). The Huffman-compressed data (compression 34713) is decoded +//! by [`crate::nefc`], which documents its clean-room sources; "lossy after split" files are decoded when their strip follows the +//! rule documented there; files it can't decode are reported as [`RawError::Unsupported`] and their embedded previews still work. So are High Efficiency NEFs +//! (HE / HE★, Z 8, Z 9, Z 6III, Z f): they keep compression 34713 but carry a wavelet codestream that starts with +//! the JPEG XS markers SOC + CAP (ISO/IEC 21122-1, `FF10 FF50`) and have no `0x0096` table (storytold/lightcraft#193). +//! Strips labelled 34713 without a `0x0096` +//! linearization table are packed uncompressed data in several measured layouts ([`PackedLayout`]) or, when the strip +//! is exactly three bytes per pixel, Nikon small raw ([`small_raw`]); the D1X's genuinely Huffman-coded strip stays +//! refused. + +use crate::nefc; +use crate::shared::white_from_data; +use crate::tiffraw::{Packing, check_image, read_image}; +use crate::{BlackLevel, Camera, Cfa, ColorData, Limits, Mode, OpcodeLists, Orientation, RawData, RawError, RawFormat, RawImage, Rect, Result}; +use craft_tiff::image::ImageInfo; +use craft_tiff::tags::{self as t, photometric}; +use craft_tiff::{ByteOrder, Ifd, Tiff, makernote}; + +const WB_RB_LEVELS: u16 = 0x000c; +const BLACK_LEVEL: u16 = 0x003d; +const LINEARIZATION_TABLE: u16 = 0x0096; +const CROP_AREA: u16 = 0x0045; +/// Maker note NEFCompression (ExifTool's Nikon tag documentation): `0x0093`, or in newer bodies the `u16` at +/// byte 10 of `0x0051`. 6 is packed 12-bit, 10 packed 14-bit, 13 and 14 High Efficiency and High Efficiency★. +const NEF_COMPRESSION: u16 = 0x0093; +const NEF_COMPRESSION_RECORD: u16 = 0x0051; + +fn nef_compression(mn: Option<&makernote::MakerNote>) -> Option { + let m = mn?; + m.ifd.u64(NEF_COMPRESSION).or_else(|| { + let v: [u8; 2] = m.ifd.bytes(NEF_COMPRESSION_RECORD)?.get(10..12)?.try_into().ok()?; + Some(u64::from(m.order.u16(v))) + }) +} +/// JPEG XS start of codestream (SOC, `FF10`) followed by its mandatory capabilities marker (CAP, `FF50`). +const JPEG_XS_START: [u8; 4] = [0xff, 0x10, 0xff, 0x50]; +/// Why a High Efficiency NEF shows its embedded preview: the UI shows the part before " (". +pub(crate) const HIGH_EFFICIENCY: &str = + "Nikon High Efficiency NEF (HE / HE★, a JPEG XS-based codec with no public specification; shoot RAW Lossless compressed to edit the raw data)"; +/// Maker note `0x0014`, the "NRW" data block of Coolpix raws: `"NRW "` + a 4-character version, then fields. In +/// version `0104` the `u32` at byte `0x20` is the black level in sample units (200 in the four 12-bit files here +/// that have 200 as their darkest samples, 0 in the one whose samples reach 0). +const NRW_DATA: u16 = 0x0014; +const NRW_BLACK_AT: usize = 0x20; + +/// The NRW data block, when the maker note has one. +fn nrw_data(mn: Option<&makernote::MakerNote>) -> Option<(&[u8], ByteOrder)> { + let m = mn?; + m.ifd.bytes(NRW_DATA).filter(|b| b.starts_with(b"NRW ")).map(|b| (b, m.order)) +} + +/// Black level from the NRW data block, bounded to a quarter of the sample range so an unknown version's field +/// can't turn into a large offset. +fn nrw_black(mn: Option<&makernote::MakerNote>, bits: u32) -> Option { + let (b, order) = nrw_data(mn)?; + let v = order.u32(b.get(NRW_BLACK_AT..NRW_BLACK_AT + 4)?.try_into().ok()?); + (u64::from(v) < (1u64 << bits.min(16)) / 4).then_some(v as f32) +} + +/// Nikon CropArea is `[left, top, width, height]` in sensor pixels (maker-note tag documentation). +/// Keep the active area/CFA origin unchanged: the default crop is applied after demosaicing. +fn default_crop(mn: Option<&makernote::MakerNote>, w: usize, h: usize) -> Rect { + let fallback = Rect::new(0, 0, w, h); + let Some(values) = mn.and_then(|m| m.ifd.u64s(CROP_AREA)) else { return fallback }; + let [x, y, width, height] = values.as_slice() else { return fallback }; + if *width == 0 || *height == 0 || x.checked_add(*width).is_none_or(|v| v > w as u64) || y.checked_add(*height).is_none_or(|v| v > h as u64) { + return fallback; + } + Rect::new(*x as usize, *y as usize, *width as usize, *height as usize) +} + +/// A body that records no CropArea, measured on its own files: the image area inside the sensor data (where the +/// camera's full-size JPEG sits when the two are aligned) and, when the data's own top would be wrong, the level +/// where its channels clip. +struct MeasuredBody { + model: &'static str, + /// Sensor data size and sample depth of the measured files. + size: (usize, usize), + bits: u32, + /// Image area in sensor pixels: x, y, width, height. + crop: (usize, usize, usize, usize), + white: Option, +} + +/// * **NIKON D4** (148 uncompressed 14-bit NEFs from one body, 2023–2026, ISO 100–10000): the data is 4992 × 3292, +/// with 2 columns of optical black left of the image and 42 right of it, then 8 empty ones; the camera's +/// 4928 × 3280 JPEG sits at (8, 6). Without this area the optical black shows as purple stripes on both sides. +/// Green saturates at 15546–15672 while red and blue run on to 15687–16383, so the data's own top (~16300) leaves +/// clipped highlights magenta; one level of 15520 for every channel turns them white and keeps the white balance. +/// * **NIKON D7100** (storytold/lightcraft#1248; 14-bit NEFs from the public zjqc/photographing-art repository, with their +/// Lightroom Classic exports): the data holds picture, not masked lines, beyond the camera's frame. In full frame +/// it is 6036 × 4020 and the camera's JPEG is 6000 × 4000; in the 1.3x crop mode it is 4812 × 3212 and the JPEG +/// 4800 × 3200. Fitting renders of the full data to Lightroom's exports (feature matches, homography) puts +/// Lightroom's frame, the camera JPEG's size, at (18, 10) and (6, 6): within 0.5 px mean on DSC_1316, DSC_2763 +/// and DSC_1486. Without it, renders are larger than Lightroom's and its crops land up to 6 px off (18 px in +/// full frame). +/// * **NIKON D90** (550 compressed 12-bit NEFs from one body, 10 measured): the data is 4352 × 2868 and its last 42 +/// columns (from 4310) are filler at a flat ~254 whatever the exposure, above the dark image samples (black 0), so +/// the masked-column test keeps them and they develop as a pink band on the right. The camera's 4288 × 2848 JPEG +/// sits at (10, 10). Green saturates at 3840–3851 while red and blue run on to 4095; one level of 3830 keeps +/// clipped highlights white. +/// * **NIKON D7000** and **NIKON D5100** (and the Coolpix A, the same readout): the data is 4992 × 3280, the picture its +/// first 4948 columns, then 36 masked ones at one level (~600, with a black level of 0) and 8 of 0. The masked +/// columns sit above the dark-column trim's threshold, so without this area they show as a coloured bar on the right +/// edge (the top in portrait; storytold/lightcraft#976). The camera's 4928 × 3264 JPEG sits at (8, 8): in the D5100's 3 CC0 +/// samples, and in the D7000's one clean one, with the D5100 and Coolpix A as siblings (see below). +/// * **The other bodies** below keep masked or dark lines inside the whole data (a survey of every Nikon raw on raw.pixls.us): +/// each crop is its camera JPEG's frame as `cargo xtask raw-frame` measures it on raw.pixls.us CC0 samples (the +/// D200, D3200 and Nikon 1 V1 also on other publicly posted sample files), with at least 3 clean +/// files agreeing: of the model (bit depths pooled: both read out the same area), or, with at least +/// one of its own, together with models of the same readout (data and JPEG size) that agree on the same frame +/// (*siblings*). The D4's 12-bit mode reads out the 14-bit area measured above; the Df, the same readout, agrees on +/// (8, 6) in 6 files. Bodies whose crop holds only picture beyond the camera frame have no entry (no change). +const MEASURED_BODIES: &[MeasuredBody] = &[ + MeasuredBody { model: "NIKON D4", size: (4992, 3292), bits: 14, crop: (8, 6, 4928, 3280), white: Some(15520.0) }, + // the 12-bit samples are the 14-bit ones shifted right by two, and so is the level: 15520 >> 2 + MeasuredBody { model: "NIKON D4", size: (4992, 3292), bits: 12, crop: (8, 6, 4928, 3280), white: Some(3880.0) }, + MeasuredBody { model: "NIKON D7100", size: (6036, 4020), bits: 14, crop: (18, 10, 6000, 4000), white: None }, + MeasuredBody { model: "NIKON D7100", size: (4812, 3212), bits: 14, crop: (6, 6, 4800, 3200), white: None }, + MeasuredBody { model: "NIKON D90", size: (4352, 2868), bits: 12, crop: (10, 10, 4288, 2848), white: Some(3830.0) }, + MeasuredBody { model: "NIKON D7000", size: (4992, 3280), bits: 14, crop: (8, 8, 4928, 3264), white: None }, + MeasuredBody { model: "NIKON D7000", size: (4992, 3280), bits: 12, crop: (8, 8, 4928, 3264), white: None }, + MeasuredBody { model: "NIKON D5100", size: (4992, 3280), bits: 14, crop: (8, 8, 4928, 3264), white: None }, + // siblings: D7000 (4), D5100 (3); masked: right 36 + MeasuredBody { model: "COOLPIX A", size: (4992, 3280), bits: 14, crop: (8, 8, 4928, 3264), white: None }, + // 6 files each; masked: left 2, right 2 + MeasuredBody { model: "NIKON D3", size: (4288, 2844), bits: 12, crop: (15, 6, 4256, 2832), white: None }, + MeasuredBody { model: "NIKON D3", size: (4288, 2844), bits: 14, crop: (15, 6, 4256, 2832), white: None }, + MeasuredBody { model: "NIKON D3S", size: (4288, 2844), bits: 12, crop: (15, 6, 4256, 2832), white: None }, + MeasuredBody { model: "NIKON D3S", size: (4288, 2844), bits: 14, crop: (15, 6, 4256, 2832), white: None }, + MeasuredBody { model: "NIKON D700", size: (4288, 2844), bits: 12, crop: (15, 6, 4256, 2832), white: None }, + MeasuredBody { model: "NIKON D700", size: (4288, 2844), bits: 14, crop: (15, 6, 4256, 2832), white: None }, + // 6 and 4 files; masked: right up to 32 + MeasuredBody { model: "NIKON D300", size: (4352, 2868), bits: 12, crop: (15, 10, 4288, 2848), white: None }, + MeasuredBody { model: "NIKON D300", size: (4352, 2868), bits: 14, crop: (15, 10, 4288, 2848), white: None }, + MeasuredBody { model: "NIKON D300S", size: (4352, 2868), bits: 12, crop: (15, 10, 4288, 2848), white: None }, + MeasuredBody { model: "NIKON D300S", size: (4352, 2868), bits: 14, crop: (15, 10, 4288, 2848), white: None }, + // D2X (1) and D2Xs (2) as siblings; dark: right 1 + MeasuredBody { model: "NIKON D2Xs", size: (4320, 2868), bits: 12, crop: (16, 10, 4288, 2848), white: None }, + // D3000, D40X, D60, D80: one file each, siblings; masked: right 4, bottom 3 + MeasuredBody { model: "NIKON D3000", size: (3904, 2616), bits: 12, crop: (14, 9, 3872, 2592), white: None }, + MeasuredBody { model: "NIKON D40X", size: (3904, 2616), bits: 12, crop: (14, 9, 3872, 2592), white: None }, + MeasuredBody { model: "NIKON D60", size: (3904, 2616), bits: 12, crop: (14, 9, 3872, 2592), white: None }, + MeasuredBody { model: "NIKON D80", size: (3904, 2616), bits: 12, crop: (14, 9, 3872, 2592), white: None }, + // 3 files; masked: left 1, right 3 (the D80's data size, framed elsewhere) + MeasuredBody { model: "NIKON D200", size: (3904, 2616), bits: 12, crop: (16, 12, 3872, 2592), white: None }, + // 22 files; masked: right 46 + MeasuredBody { model: "NIKON D3200", size: (6080, 4012), bits: 12, crop: (8, 6, 6016, 4000), white: None }, + // 8 clean of 19, the rest ~0.3 px between pixels on the same frame; dark: bottom 2 + MeasuredBody { model: "NIKON 1 V1", size: (3904, 2606), bits: 12, crop: (12, 6, 3872, 2592), white: None }, + // Best guesses, short of the evidence rule; every frame below keeps the masked lines out by several pixels. + // D3100: 3 of 4 clean files at (24, 6), the 4th at (22, 7); masked: left 6, right 22 + MeasuredBody { model: "NIKON D3100", size: (4672, 3084), bits: 12, crop: (24, 6, 4608, 3072), white: None }, + // D5000: 4 of 5 clean files at (11, 10), the 5th at (11, 12); the D90's sensor, whose own files also align at + // x = 11; masked: right 42 (the same filler as the D90's) + MeasuredBody { model: "NIKON D5000", size: (4352, 2868), bits: 12, crop: (11, 10, 4288, 2848), white: None }, + // D40: 2 of 2 clean files at (16, 9); masked: right 1. The D50 / D70 / D70s share its data size but not its frame + MeasuredBody { model: "NIKON D40", size: (3040, 2014), bits: 12, crop: (16, 9, 3008, 2000), white: None }, + // D50, D70, D70s (one readout): one clean D50 and one clean D70 file at (15, 7); the D70s's 2 files align + // between pixels next to it, at (15.4, 6.9) and (15.6, 6.4); masked: right 1 + MeasuredBody { model: "NIKON D50", size: (3040, 2014), bits: 12, crop: (15, 7, 3008, 2000), white: None }, + MeasuredBody { model: "NIKON D70", size: (3040, 2014), bits: 12, crop: (15, 7, 3008, 2000), white: None }, + MeasuredBody { model: "NIKON D70s", size: (3040, 2014), bits: 12, crop: (15, 7, 3008, 2000), white: None }, + // 3 files; dark: bottom 4 + MeasuredBody { model: "NIKON D4S", size: (4936, 3288), bits: 12, crop: (4, 4, 4928, 3280), white: None }, + MeasuredBody { model: "NIKON D4S", size: (4936, 3288), bits: 14, crop: (4, 4, 4928, 3280), white: None }, + // 4 files; dark: bottom up to 4 + MeasuredBody { model: "NIKON D5600", size: (6016, 4016), bits: 12, crop: (8, 8, 6000, 4000), white: None }, + MeasuredBody { model: "NIKON D5600", size: (6016, 4016), bits: 14, crop: (8, 8, 6000, 4000), white: None }, + // 4 files each; masked: right 8 + MeasuredBody { model: "NIKON D600", size: (6080, 4028), bits: 12, crop: (8, 6, 6016, 4016), white: None }, + MeasuredBody { model: "NIKON D600", size: (6080, 4028), bits: 14, crop: (8, 6, 6016, 4016), white: None }, + MeasuredBody { model: "NIKON D610", size: (6080, 4028), bits: 12, crop: (8, 6, 6016, 4016), white: None }, + MeasuredBody { model: "NIKON D610", size: (6080, 4028), bits: 14, crop: (8, 6, 6016, 4016), white: None }, + // D800: 6 files; D800E: 2, with the D800 as sibling; masked: right 14 (storytold/lightcraft#914, the top in portrait) + MeasuredBody { model: "NIKON D800", size: (7424, 4924), bits: 12, crop: (8, 6, 7360, 4912), white: None }, + MeasuredBody { model: "NIKON D800", size: (7424, 4924), bits: 14, crop: (8, 6, 7360, 4912), white: None }, + MeasuredBody { model: "NIKON D800E", size: (7424, 4924), bits: 14, crop: (8, 6, 7360, 4912), white: None }, + // 3 files; dark: bottom 1 + MeasuredBody { model: "NIKON D810", size: (7380, 4928), bits: 12, crop: (12, 8, 7360, 4912), white: None }, + MeasuredBody { model: "NIKON D810", size: (7380, 4928), bits: 14, crop: (12, 8, 7360, 4912), white: None }, + // 6 files; masked: left 2, right 42 (the D4's readout) + MeasuredBody { model: "NIKON Df", size: (4992, 3292), bits: 12, crop: (8, 6, 4928, 3280), white: None }, + MeasuredBody { model: "NIKON Df", size: (4992, 3292), bits: 14, crop: (8, 6, 4928, 3280), white: None }, +]; + +fn measured_body(model: &str, w: usize, h: usize, bits: u32) -> Option<&'static MeasuredBody> { + MEASURED_BODIES.iter().find(|b| b.model == model.trim() && b.size == (w, h) && b.bits == bits) +} + +/// Pixel height over width from the raw IFD's resolution tags: the D1X and D1H write 600 x 300 per inch for a +/// 4028 x 1324 frame, rows twice as tall as the columns are wide. Only ratios from 1.5 to 4 count (a frame +/// squashed 3:1 or 2:1), the rest is rounding or a stray tag. +pub(crate) fn pixel_aspect_of(raw: &Ifd) -> f32 { + match (raw.f64(t::X_RESOLUTION), raw.f64(t::Y_RESOLUTION)) { + (Some(x), Some(y)) if x > 0.0 && y > 0.0 && (1.5..=4.0).contains(&(x / y)) => (x / y).round() as f32, + _ => 1.0, + } +} + +fn raw_ifd(tiff: &Tiff) -> Option<&Ifd> { + tiff.all_ifds() + .into_iter() + .filter(|i| i.u16(t::PHOTOMETRIC) == Some(photometric::CFA)) + .max_by_key(|i| i.u64(t::IMAGE_WIDTH).unwrap_or(0).saturating_mul(i.u64(t::IMAGE_LENGTH).unwrap_or(0))) +} + +/// Rows the masked-column test reads, from the top of the frame. The header probe ([`Mode::Header`]) decodes +/// only these (a compressed strip is one sequential Huffman stream, so it can stop here; an uncompressed one reads +/// just its first strips), and the full decode judges the active width from the same rows, so +/// [`crate::probe_info`] equals [`RawImage::info`] of the decoded image (storytold/lightcraft#708). On every corpus NEF the +/// answer is the same for 8 to 512 rows as for the whole frame: the masked columns decode to 0 (compressed) or +/// ~19 (uncompressed) while live columns sit at the black level (~150 at 12 bit, ~600 at 14 bit) or above. +pub(crate) const MASK_ROWS: usize = 128; + +/// Width without the optically masked columns some bodies append on the right (the D5100 and D7000 write 8, with +/// no maker-note tag that says so, in compressed and uncompressed files alike): trailing columns (at most 64) whose +/// mean over the first [`MASK_ROWS`] rows of `d` is below 1% of the nominal white level and a tenth of the image +/// interior. Kept even for CFA phase. +pub(crate) fn trailing_masked_columns(d: &[u16], w: usize, bits: u32) -> usize { + let rows = d.len().checked_div(w).unwrap_or(0).min(MASK_ROWS); + if w < 128 || rows == 0 { + return w; + } + let col_mean = |x: usize| (0..rows).map(|y| d.get(y * w + x).copied().unwrap_or(0) as f64).sum::() / rows as f64; + let interior = (w / 4..w * 3 / 4).step_by(w / 64).map(col_mean).sum::() / 32.0; + let white = ((1u32 << bits.clamp(1, 16)) - 1) as f64; + let dark = (white * 0.01).min(interior * 0.1); + let mut aw = w; + while aw > w - 64 && col_mean(aw - 1) < dark { + aw -= 1; + } + aw & !1 +} + +pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result { + let tiff = Tiff::parse(bytes)?; + let ifd0 = tiff.ifds.first().ok_or_else(|| RawError::Corrupt("TIFF without IFDs".into()))?; + let raw = raw_ifd(&tiff).ok_or_else(|| RawError::Unsupported("NEF without a CFA image IFD".into()))?; + let info = raw.image()?; + let (w, h) = (info.width as usize, info.height as usize); + let bits = info.bits() as u32; + let make = ifd0.string(t::MAKE).unwrap_or_default(); + let mn = + tiff.exif().and_then(|e| e.get(t::MAKER_NOTE)).and_then(|e| makernote::parse_makernote(bytes, e.offset, e.count() as u64, tiff.order, &make)); + if let Some(strip) = small_raw_strip(&info, bytes.len(), mn.as_ref()) { + return small_raw(bytes, &tiff, &info, strip, mode, limits); + } + // the header probe reads only the rows the masked-column test needs + let rows = match mode { + Mode::Full => h, + Mode::Header => h.min(MASK_ROWS), + }; + let data = if info.compression == t::compression::NIKON { + compressed(bytes, &info, mn.as_ref(), rows, limits)? + } else { + uncompressed(bytes, &info, tiff.order, mn.as_ref(), rows, limits)? + }; + let RawData::U16(ref samples) = data else { return Err(RawError::Unsupported("float NEF".into())) }; + + // Maker note 0x003d is in 14-bit units whatever the sample depth: 12-bit files from the D750, D780, D850, + // D7500 and Z 50 store 600 / 1008 / 400 / 400 / 1008 while their darkest samples are 150 / ~252 / 99 / 99 / ~250 + // (our measurements on CC0 raw.pixls.us samples; the same bodies' 14-bit files match the tag as is). + let black_scale = if bits == 12 { 0.25 } else { 1.0 }; + let black = match mn.as_ref().and_then(|m| m.ifd.f64s(BLACK_LEVEL)).as_deref() { + Some([a, b, c, d]) if [a, b, c, d].iter().all(|v| **v < 16384.0) => BlackLevel { + repeat_rows: 2, + repeat_cols: 2, + values: [a, b, c, d].iter().map(|v| (**v * black_scale) as f32).collect(), + ..Default::default() + }, + _ => BlackLevel::uniform(nrw_black(mn.as_ref(), bits).unwrap_or_else(|| be16_black(&info, bytes.len() as u64, mn.as_ref(), samples))), + }; + let wb = mn + .as_ref() + .and_then(|m| m.ifd.f64s(WB_RB_LEVELS)) + .filter(|v| v.len() >= 2 && v[0] > 0.1 && v[1] > 0.1 && v[0] < 10.0 && v[1] < 10.0) + .map(|v| [v[0] as f32, 1.0, v[1] as f32]); + let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + _ => Cfa::bayer_static("RGGB"), + }; + let model = ifd0.string(t::MODEL).unwrap_or_default(); + let body = measured_body(&model, w, h, bits); + let active_w = trailing_masked_columns(samples, w, bits); + // the white level is measured from the whole image (or known for a measured body); the probe (no samples in its + // result) takes the nominal one + let white = match (body.and_then(|b| b.white), mode) { + (Some(white), _) => white, + (None, Mode::Full) => white_from_data(samples, bits), + (None, Mode::Header) => ((1u32 << bits.clamp(1, 16)) - 1) as f32, + }; + let crop = match body { + Some(&MeasuredBody { crop: (x, y, cw, ch), .. }) if x + cw <= active_w && y + ch <= h => Rect::new(x, y, cw, ch), + _ => default_crop(mn.as_ref(), active_w, h), + }; + let pixel_aspect = pixel_aspect_of(raw); + let img = RawImage { + format: RawFormat::Nef, + width: w, + height: h, + cpp: 1, + data: match mode { + Mode::Full => data, + Mode::Header => RawData::U16(Vec::new()), + }, + cfa: Some(cfa), + bits, + black, + white: vec![white], + active_area: Rect::new(0, 0, active_w, h), + crop, + orientation: Orientation::from_exif(ifd0.u16(t::ORIENTATION).unwrap_or(1)), + color: ColorData::default(), + wb_multipliers: wb, + linearized: false, + pixel_aspect, + // Nikon's in-file distortion correction, about the optical centre (which follows the sensor shift) + opcodes: OpcodeLists { + list3: crate::nef_lens::distortion(&model, raw, Rect::new(0, 0, active_w, h), crop).into_iter().collect(), + ..Default::default() + }, + camera: tiff.ifds.first().map(Camera::from_ifd0).unwrap_or_default(), + cinema: Default::default(), + }; + img.validate_for(mode)?; + Ok(img) +} + +/// Uncompressed strips: 16-bit words or 12-bit packed, told apart by the strip size. Packed 12-bit data is an MSB-first +/// byte stream, except in files with an NRW data block (Coolpix), where it is MSB-first inside little-endian 32-bit +/// words (the rows are whole words). Reads the first `rows` rows (the strips that hold them) of the `info.height`. +fn uncompressed( + bytes: &[u8], + info: &ImageInfo, + order: ByteOrder, + mn: Option<&makernote::MakerNote>, + rows: usize, + limits: &Limits, +) -> Result { + let (w, h) = (info.width as usize, info.height as usize); + let bits = info.bits() as u32; + let row_samples = w * info.samples_per_pixel as usize; + let chunks = info.chunks(bytes.len() as u64); + let rows_in_first = chunks.first().map(|c| c.height as usize).unwrap_or(h).max(1); + let bytes_per_row = chunks.first().map(|c| c.len as usize / rows_in_first).unwrap_or(0); + let packing = if bytes_per_row >= row_samples * 2 { + Packing::Word16 + } else if bits == 12 && bytes_per_row * 8 >= row_samples * 12 && bytes_per_row * 8 < row_samples * 13 { + if nrw_data(mn).is_some() && bytes_per_row.is_multiple_of(4) { Packing::Words32Msb } else { Packing::Msb } + } else if info.compression == 1 && bits != 8 && bits != 16 { + return Err(RawError::Unsupported(format!("NEF uncompressed packing ({bytes_per_row} bytes per {row_samples}-sample row)"))); + } else { + Packing::Msb + }; + if rows >= h { + return read_image(bytes, info, order, packing, limits); + } + // the whole image's limits apply either way; then only the strips holding the first `rows` rows are unpacked + check_image(bytes, info, limits)?; + let top = ImageInfo { height: rows as u32, ..info.clone() }; + read_image(bytes, &top, order, packing, limits) +} + +/// Packed layouts of strips that are labelled compression 34713 but have no linearization table (maker note +/// `0x0096`): they hold plain packed samples, not Huffman codes. The layouts were measured from CC0 raw.pixls.us +/// files by a clean-room black-box analysis (strip byte-count identity, same-colour smoothness and correlation with +/// the camera JPEG against at least eight alternative packings). Every layout is a single strip with +/// `stride = StripByteCounts / height`, selected only when `stride * height` equals the strip size exactly. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum PackedLayout { + /// LSB-first bit stream of `bits` per sample (12: 3 bytes per 2 samples, 14: 7 bytes per 4), rows byte aligned. + /// 12-bit rows are `3w/2` bytes (older bodies) or padded to 16 bytes (Z 6, D6); 14-bit rows are `7w/4` bytes + /// padded to 16 bytes. + Lsb { stride: usize }, + /// D100: 16-byte chunks of fifteen data bytes holding ten MSB-first 12-bit samples, plus one zero pad byte. The + /// rows carry more samples than the IFD width (3040 against 3034); the extra columns are not image. + Chunked16 { stride: usize }, + /// Coolpix NRW: one big-endian 16-bit word per pixel, rows `2w` bytes with no header or padding; the 12-bit sample + /// is the upper 12 bits of the word (the low nibble is zero, rarely 8). + Be16, +} + +fn packed_layout(w: usize, h: usize, bits: u32, strip: u64, stated: bool) -> Option { + if w == 0 || h == 0 || !matches!(bits, 12 | 14) { + return None; + } + let strip = usize::try_from(strip).ok()?; + if !strip.is_multiple_of(h) { + return None; + } + let stride = strip / h; + let tight = w.checked_mul(bits as usize)?.div_ceil(8); + let tight = if bits == 12 && w.is_multiple_of(2) || bits == 14 && w.is_multiple_of(4) { tight } else { return None }; + let padded = tight.checked_next_multiple_of(16)?; + let lsb_ok = match bits { + 12 => stride == tight || stride == padded, + _ => stride == padded || stated && stride == tight, + }; + if lsb_ok { + return Some(PackedLayout::Lsb { stride }); + } + None +} + +fn d100_layout(w: usize, h: usize, bits: u32, strip: u64) -> Option { + let strip = usize::try_from(strip).ok()?; + if bits != 12 || h == 0 || !strip.is_multiple_of(h) { + return None; + } + let stride = strip / h; + let per_row = stride / 16 * 10; + (stride.is_multiple_of(16) && per_row >= w && per_row < w.checked_add(10)?).then_some(PackedLayout::Chunked16 { stride }) +} + +/// 12-bit strips of exactly `2 * w * h` bytes: big-endian 16-bit words (Coolpix P330, P7700, P7800 NRW, found by a +/// clean-room black-box analysis of the CC0 files). Tried after the other layouts, whose strides (`1.5w` to +/// `1.5w + 15`, or ten samples per 16 bytes) only reach `2w` for rows narrower than 40 samples, so it can't take a +/// strip they handle. +fn be16_layout(w: usize, h: usize, bits: u32, strip: u64) -> Option { + let want = w.checked_mul(h)?.checked_mul(2)?; + (bits == 12 && w > 0 && h > 0 && usize::try_from(strip).ok()? == want).then_some(PackedLayout::Be16) +} + +/// Black level of the big-endian 16-bit layout, which carries neither `0x003d` nor an NRW block and has no masked +/// pixels (the borders of the frame are image). The darkest samples of the three measured bodies are 214 (P330), +/// 217 (P7700) and 201 (P7800, the only one with a dark enough frame: its per-channel 1st percentile is 209 / 225 / +/// 223), and the other 12-bit Coolpix NRWs state 200 in their NRW block; so 200, never above the darkest sample. +const BE16_BLACK: f32 = 200.0; + +fn be16_black(info: &ImageInfo, file_len: u64, mn: Option<&makernote::MakerNote>, samples: &[u16]) -> f32 { + let has_table = mn.is_some_and(|m| m.ifd.bytes(LINEARIZATION_TABLE).is_some()); + let single = matches!(info.chunks(file_len).as_slice(), [c] if be16_layout(info.width as usize, info.height as usize, info.bits() as u32, c.len).is_some()); + if info.compression != t::compression::NIKON || has_table || info.samples_per_pixel != 1 || !single { + return 0.0; + } + let darkest = samples.iter().copied().min().unwrap_or(0); + BE16_BLACK.min(f32::from(darkest)) +} + +/// Unpack a [`PackedLayout`] strip into `w * h` samples. Short input leaves the missing samples zero. +fn unpack_packed(src: &[u8], w: usize, h: usize, bits: u32, layout: PackedLayout) -> Vec { + use rayon::prelude::*; + let mut out = vec![0u16; w * h]; + let stride = match layout { + PackedLayout::Lsb { stride } | PackedLayout::Chunked16 { stride } => stride, + PackedLayout::Be16 => w.saturating_mul(2), + }; + out.par_chunks_mut(w).enumerate().for_each(|(y, row)| { + let Some(line) = src.get(y * stride..) else { return }; + let line = &line[..stride.min(line.len())]; + match layout { + PackedLayout::Lsb { .. } => crate::unpack::unpack_lsb(line, bits, row), + PackedLayout::Be16 => { + let (words, _) = line.as_chunks::<2>(); + for (dst, word) in row.iter_mut().zip(words) { + *dst = u16::from_be_bytes(*word) >> 4; + } + } + PackedLayout::Chunked16 { .. } => { + let mut samples = vec![0u16; line.len() / 16 * 10]; + for (i, dst) in samples.chunks_mut(10).enumerate() { + if let Some(chunk) = line.get(i * 16..i * 16 + 15) { + crate::unpack::unpack_msb(chunk, 12, dst); + } + } + let n = row.len().min(samples.len()); + row[..n].copy_from_slice(&samples[..n]); + } + } + }); + out +} + +/// Nikon Huffman-compressed strip (see [`crate::nefc`]); the decode table is maker note `0x0096`. Without a table the +/// strip may be packed uncompressed data (see [`PackedLayout`]). +fn compressed(bytes: &[u8], info: &ImageInfo, mn: Option<&makernote::MakerNote>, rows: usize, limits: &Limits) -> Result { + let starts_with = |magic: &[u8]| { + let start = info.chunks(bytes.len() as u64).first().and_then(|c| usize::try_from(c.offset).ok()); + start.and_then(|o| bytes.get(o..o.checked_add(magic.len())?)) == Some(magic) + }; + // the maker note's own compression mode decides first: Z bodies label packed data 34713 and may still carry + // a 0x0096 table + let mode = nef_compression(mn); + if starts_with(&JPEG_XS_START) || matches!(mode, Some(13 | 14)) { + return Err(RawError::Unsupported(HIGH_EFFICIENCY.into())); + } + if let Some(depth) = match mode { + Some(6) => Some(12), + Some(10) => Some(14), + _ => None, + } { + if info.bits() as u32 != depth { + return Err(RawError::Unsupported(format!("Nikon packed {depth}-bit NEF with {}-bit samples", info.bits()))); + } + return packed(bytes, info, rows, true, limits); + } + let Some(table) = mn.and_then(|m| Some((m.ifd.bytes(LINEARIZATION_TABLE)?, m.order))) else { + return packed(bytes, info, rows, false, limits); + }; + if info.samples_per_pixel != 1 { + return Err(RawError::Unsupported(format!("Nikon compressed NEF with {} samples per pixel", info.samples_per_pixel))); + } + let bits = info.bits() as u32; + if u64::from(info.width).saturating_mul(u64::from(info.height)) > limits.samples() as u64 { + return Err(RawError::Limit("image too large")); + } + let table = nefc::parse_table(table.0, table.1, bits)?; + // one strip in every sample; several would be contiguous parts of the same bit stream + let chunks = info.chunks(bytes.len() as u64); + let (Some(first), Some(last)) = (chunks.first(), chunks.last()) else { return Err(RawError::Corrupt("NEF: no image data".into())) }; + let end = last.offset.saturating_add(last.len).min(bytes.len() as u64); + let src = usize::try_from(first.offset).ok().zip(usize::try_from(end).ok()).and_then(|(a, b)| bytes.get(a..b)); + let src = src.ok_or_else(|| RawError::Corrupt("NEF: image data outside the file".into()))?; + Ok(RawData::U16(nefc::decode_rows(src, info.width as usize, info.height as usize, rows, bits, &table)?)) +} + +/// A "compressed" strip of packed samples: the strip size must match a measured layout exactly. `stated` is set +/// when the maker note names the packed mode, which also allows 14-bit rows without padding. +fn packed(bytes: &[u8], info: &ImageInfo, rows: usize, stated: bool, limits: &Limits) -> Result { + let (w, h) = (info.width as usize, info.height as usize); + let bits = info.bits() as u32; + let chunks = info.chunks(bytes.len() as u64); + let layout = match chunks.as_slice() { + [c] if info.samples_per_pixel == 1 => { + packed_layout(w, h, bits, c.len, stated).or_else(|| d100_layout(w, h, bits, c.len)).or_else(|| be16_layout(w, h, bits, c.len)) + } + _ => None, + }; + let (Some(layout), [c]) = (layout, chunks.as_slice()) else { + return Err(RawError::Unsupported("Nikon compressed NEF without a linearization table (maker note 0x96)".into())); + }; + let src = craft_tiff::image::chunk_bytes(bytes, c) + .filter(|src| u64::try_from(src.len()).ok() == Some(c.len)) + .ok_or_else(|| RawError::Corrupt("NEF: incomplete image strip".into()))?; + if (w as u64).saturating_mul(h as u64) > limits.samples() as u64 { + return Err(RawError::Limit("image too large")); + } + Ok(RawData::U16(unpack_packed(src, w, rows.min(h), bits, layout))) +} + +/// Nikon small raw (sRAW), measured clean-room from the CC0 raw.pixls.us D810 (2036) and D4S (2227) samples. The raw SubIFD is labelled compression 34713, +/// 12 bits, one CFA sample per pixel, but it carries no linearization table (maker note `0x0096`) and its single strip +/// is exactly `3 * width * height` bytes. It holds a YCbCr 4:2:2 image of the IFD size, not a CFA mosaic (the CFA +/// tags are dummies): +/// +/// - the strip is an LSB-first 12-bit stream (`s0 = b0 | (b1 & 0x0f) << 8`, `s1 = b1 >> 4 | b2 << 4`), a row is `3 * width` +/// bytes with no padding, i.e. `2 * width` samples; +/// - the samples come in groups of four, `[Y_left, Y_right, Cb, Cr]`, for the pixel pair `(2k, 2k + 1)`: chroma is +/// halved horizontally and not vertically; +/// - luma is linear and already black-subtracted (the darkest samples are near 0) and never uses the top two bits +/// (maxima 873 and 1004 of 4095), so the white level is 1023; chroma is centred on 2048 (fitted neutral 2045-2049). +/// +/// The chroma is a camera colour-difference scale, not JFIF Cb/Cr. In linear terms the stored values behave as +/// `R - Y = R_GAIN * (Cr - 2048)` and `B - Y = B_GAIN * (Cb - 2048)` on the luma scale: regressing the camera JPEG's +/// linear `(R - Y) / Y` and `(B - Y) / Y` on `(Cr - 2048) / Y` and `(Cb - 2048) / Y` over mid-tone pixels gave slopes +/// 4.92 / 5.09 (D810, correlation 0.94 / 0.88) and 3.52 / 3.73 (D4S, 0.98 / 0.97); the gains below are the rounded +/// means. They are approximate (the two bodies differ by about 30 %), the luma is not tone-mapped, and the data is +/// treated as already white balanced (neutral D4S areas have `Cb`, `Cr` within 2 of 2048 and equal JPEG R, G, B). G +/// follows from the luma equation with the BT.601 weights. The chroma is upsampled horizontally with linear +/// interpolation, centred between the two pixels of a pair. Output is linear RGB scaled by 16 (to a 14-bit range), +/// like Sony's downsized ARWs. +const SMALL_RAW_WHITE: f32 = 1023.0; +const SMALL_RAW_SCALE: f32 = 16.0; +const SMALL_RAW_CHROMA_ZERO: f32 = 2048.0; +const SMALL_RAW_R_GAIN: f32 = 4.2; +const SMALL_RAW_B_GAIN: f32 = 4.4; + +/// The strip of a small raw: compression 34713, 12 bits, no maker note `0x0096`, one strip of exactly +/// `3 * width * height` bytes (even width). +fn small_raw_strip(info: &ImageInfo, file_len: usize, mn: Option<&makernote::MakerNote>) -> Option { + if info.compression != t::compression::NIKON || info.bits() != 12 || info.samples_per_pixel != 1 { + return None; + } + if mn.is_some_and(|m| m.ifd.bytes(LINEARIZATION_TABLE).is_some()) { + return None; + } + let (w, h) = (info.width as u64, info.height as u64); + let chunks = info.chunks(file_len as u64); + match chunks.as_slice() { + [c] if w.is_multiple_of(2) && w > 0 && h > 0 && w.checked_mul(h).and_then(|n| n.checked_mul(3)) == Some(c.len) => Some(*c), + _ => None, + } +} + +/// Decode one row of a small raw (`3 * width` bytes) into `3 * width` linear RGB values on the 14-bit scale. +/// Short input reads as zero bytes. +fn small_raw_row(src: &[u8], w: usize, out: &mut [u16]) { + let Some(n) = w.checked_mul(3).filter(|n| *n <= out.len()) else { return }; + if w == 0 || !w.is_multiple_of(2) { + return; + } + let mut s = vec![0u16; 2 * w]; + crate::unpack::unpack_lsb(&src[..src.len().min(n)], 12, &mut s); + let pairs = w / 2; + let chroma = |k: usize| { + let g = &s[4 * k + 2..4 * k + 4]; + (f32::from(g[0]) - SMALL_RAW_CHROMA_ZERO, f32::from(g[1]) - SMALL_RAW_CHROMA_ZERO) + }; + for k in 0..pairs { + let (cb, cr) = chroma(k); + for side in 0..2 { + let (nb, nr) = if side == 0 { chroma(k.saturating_sub(1)) } else { chroma((k + 1).min(pairs - 1)) }; + let (u, v) = (0.75 * cb + 0.25 * nb, 0.75 * cr + 0.25 * nr); + let y = f32::from(s[4 * k + side]); + let r = y + SMALL_RAW_R_GAIN * v; + let b = y + SMALL_RAW_B_GAIN * u; + let g = (y - 0.299 * r - 0.114 * b) / 0.587; + let dst = (2 * k + side) * 3; + for (i, value) in [r, g, b].into_iter().enumerate() { + out[dst + i] = (value * SMALL_RAW_SCALE).round().clamp(0.0, 65535.0) as u16; + } + } + } +} + +fn small_raw(bytes: &[u8], tiff: &Tiff, info: &ImageInfo, strip: craft_tiff::image::Chunk, mode: Mode, limits: &Limits) -> Result { + use rayon::prelude::*; + let (w, h) = (info.width as usize, info.height as usize); + let stride = w.checked_mul(3).ok_or(RawError::Limit("image too large"))?; + let count = stride.checked_mul(h).filter(|n| *n > 0 && *n <= limits.samples()).ok_or(RawError::Limit("image too large"))?; + let src = craft_tiff::image::chunk_bytes(bytes, &strip) + .filter(|src| u64::try_from(src.len()).ok() == Some(strip.len)) + .ok_or_else(|| RawError::Corrupt("NEF: incomplete image strip".into()))?; + let data = match mode { + Mode::Full => { + let mut data = vec![0u16; count]; + data.par_chunks_mut(stride).enumerate().for_each(|(y, out)| { + small_raw_row(src.get(y * stride..).unwrap_or(&[]), w, out); + }); + data + } + Mode::Header => Vec::new(), + }; + let img = RawImage { + format: RawFormat::Nef, + width: w, + height: h, + cpp: 3, + data: RawData::U16(data), + cfa: None, + bits: 14, + black: BlackLevel::uniform(0.0), + white: vec![SMALL_RAW_WHITE * SMALL_RAW_SCALE], + active_area: Rect::new(0, 0, w, h), + crop: Rect::new(0, 0, w, h), + orientation: Orientation::from_exif(tiff.ifds.first().and_then(|i| i.u16(t::ORIENTATION)).unwrap_or(1)), + color: ColorData::default(), + // the stored colour is already white balanced (see above) + wb_multipliers: Some([1.0; 3]), + linearized: false, + pixel_aspect: 1.0, + opcodes: OpcodeLists::default(), + camera: tiff.ifds.first().map(Camera::from_ifd0).unwrap_or_default(), + cinema: Default::default(), + }; + img.validate_for(mode)?; + Ok(img) +} + +#[cfg(test)] +mod tests { + use super::*; + use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; + + fn nef(compression: u16, bits: u16, strips: Vec>, w: u32, h: u32, rps: u32) -> Vec { + nef_with_note(compression, bits, strips, w, h, rps, None) + } + + /// [`nef`] with an optional maker note (`Nikon\0` v2 header + embedded big-endian TIFF holding `note`). + fn nef_with_note(compression: u16, bits: u16, strips: Vec>, w: u32, h: u32, rps: u32, note: Option) -> Vec { + nef_in(ByteOrder::Big, compression, bits, strips, w, h, rps, note) + } + + /// [`nef_with_note`] in either byte order (file and maker note). + #[allow(clippy::too_many_arguments)] + fn nef_in(order: ByteOrder, compression: u16, bits: u16, strips: Vec>, w: u32, h: u32, rps: u32, note: Option) -> Vec { + nef_model_in(order, "NIKON TEST", compression, bits, strips, w, h, rps, note) + } + + /// [`nef_in`] from camera `model`. + #[allow(clippy::too_many_arguments)] + fn nef_model_in( + order: ByteOrder, + model: &str, + compression: u16, + bits: u16, + strips: Vec>, + w: u32, + h: u32, + rps: u32, + note: Option, + ) -> Vec { + let mut raw = IfdBuilder::new(); + raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0])); + raw.set(t::IMAGE_WIDTH, Value::Long(vec![w])); + raw.set(t::IMAGE_LENGTH, Value::Long(vec![h])); + raw.set(t::BITS_PER_SAMPLE, Value::Short(vec![bits])); + raw.set(t::COMPRESSION, Value::Short(vec![compression])); + raw.set(t::PHOTOMETRIC, Value::Short(vec![photometric::CFA])); + raw.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2])); + raw.set(t::CFA_PATTERN_EP, Value::Byte(vec![2, 1, 1, 0])); + raw.set_image(ImageData::Strips { rows_per_strip: rps, strips }); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii("NIKON CORPORATION".into())); + ifd0.set(t::MODEL, Value::Ascii(model.into())); + if let Some(mn) = note { + let mut bytes = b"Nikon\0\x02\x10\0\0".to_vec(); + bytes.extend(TiffWriter::new(order, false).write(&[mn]).unwrap()); + let mut exif = IfdBuilder::new(); + exif.set(t::MAKER_NOTE, Value::Undefined(bytes)); + ifd0.set_child(t::EXIF_IFD, exif); + } + ifd0.add_sub_ifd(raw); + TiffWriter::new(order, false).write(&[ifd0]).unwrap() + } + + /// A Z 6II NEF carrying NikonNEFInfo gets Nikon's distortion correction as one warp, in the header probe and + /// the full decode alike, and keeps it through DNG export; another body with the same data gets none. + #[test] + fn nikon_in_file_distortion_becomes_a_warp() { + use crate::nef_lens::tests::{distortion_block, nef_info_blob, offset_block}; + let (w, h) = (48u32, 32u32); + let file = |model: &str| { + let words: Vec = (0..w * h).flat_map(|i| (600 + (i % 97) as u16 * 50).to_be_bytes()).collect(); + let mut raw = IfdBuilder::new(); + raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0])); + raw.set(t::IMAGE_WIDTH, Value::Long(vec![w])); + raw.set(t::IMAGE_LENGTH, Value::Long(vec![h])); + raw.set(t::BITS_PER_SAMPLE, Value::Short(vec![14])); + raw.set(t::COMPRESSION, Value::Short(vec![1])); + raw.set(t::PHOTOMETRIC, Value::Short(vec![photometric::CFA])); + raw.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2])); + raw.set(t::CFA_PATTERN_EP, Value::Byte(vec![0, 1, 1, 2])); + let info = nef_info_blob(&[ + (0x0005, distortion_block(3, 21.6, 4, [0.039895, 0.019211, -0.127218], -0.025043)), + (0x0008, offset_block(1.5, -0.75)), + ]); + raw.set(0xc7d5, Value::Undefined(info)); + raw.set_image(ImageData::Strips { rows_per_strip: h, strips: vec![words] }); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii("NIKON CORPORATION".into())); + ifd0.set(t::MODEL, Value::Ascii(model.into())); + ifd0.add_sub_ifd(raw); + TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap() + }; + let bytes = file("NIKON Z 6_2"); + let header = decode(&bytes, Mode::Header, &Limits::default()).unwrap(); + let full = decode(&bytes, Mode::Full, &Limits::default()).unwrap(); + assert_eq!(header.info(), full.info()); + assert_eq!(header.opcodes.list3, full.opcodes.list3); + let [crate::Opcode::WarpRectilinear { planes, center }] = full.opcodes.list3.as_slice() else { panic!("{:?}", full.opcodes.list3) }; + assert!((planes[0][0] - 1.0 / (1.0 - 0.025043)).abs() < 1e-5, "{planes:?}"); + assert!((center[0] * 47.0 - (23.5 + 1.5)).abs() < 0.01 && (center[1] * 31.0 - (15.5 - 0.75)).abs() < 0.01, "{center:?}"); + #[cfg(feature = "dng")] + { + let dng = crate::write_dng(&full, &Default::default()).unwrap(); + assert_eq!(crate::decode(&dng).unwrap().opcodes.list3, full.opcodes.list3); + } + assert!(decode(&file("NIKON D850"), Mode::Full, &Limits::default()).unwrap().opcodes.list3.is_empty()); + } + + #[test] + fn preview_color_space_is_read_from_the_nikon_note() { + for (value, expected) in [(1, Some(crate::PreviewColorSpace::Srgb)), (2, Some(crate::PreviewColorSpace::AdobeRgb)), (3, None)] { + let mut mn = IfdBuilder::new(); + mn.set(0x001e, Value::Short(vec![value])); + let bytes = nef_with_note(1, 14, vec![vec![0; 128]], 8, 8, 8, Some(mn)); + assert_eq!(crate::embedded_preview_color_space(&bytes), expected); + } + assert_eq!(crate::embedded_preview_color_space(&nef(1, 14, vec![vec![0; 128]], 8, 8, 8)), None); + assert_eq!(crate::embedded_preview_color_space(b"not a TIFF"), None); + } + + /// storytold/lightcraft#778: any make's raw falls back to its EXIF ColorSpace, with Uncalibrated + InteropIndex "R03" read as + /// Adobe RGB; the Nikon note wins; a DNG is left alone. + #[test] + fn preview_color_space_falls_back_to_the_exif_color_space() { + let file = |make: &str, cs: u16, interop: Option<&str>, note: Option, dng: bool| { + let mut exif = IfdBuilder::new(); + exif.set(0xA001, Value::Short(vec![cs])); + if let Some(i) = interop { + let mut io = IfdBuilder::new(); + io.set(0x0001, Value::Ascii(i.into())); + exif.set_child(t::INTEROP_IFD, io); + } + if let Some(v) = note { + let mut mn = IfdBuilder::new(); + mn.set(0x001e, Value::Short(vec![v])); + let mut bytes = b"Nikon".to_vec(); + bytes.extend(TiffWriter::new(ByteOrder::Little, false).write(&[mn]).unwrap()); + exif.set(t::MAKER_NOTE, Value::Undefined(bytes)); + } + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii(make.into())); + if dng { + ifd0.set(t::DNG_VERSION, Value::Byte(vec![1, 4, 0, 0])); + } + ifd0.set_child(t::EXIF_IFD, exif); + TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap() + }; + use crate::PreviewColorSpace::{AdobeRgb, Srgb}; + let f = crate::embedded_preview_color_space; + assert_eq!(f(&file("SONY", 0xFFFF, Some("R03"), None, false)), Some(AdobeRgb)); + assert_eq!(f(&file("SONY", 1, Some("R98"), None, false)), Some(Srgb)); + assert_eq!(f(&file("SONY", 0xFFFF, Some("R98"), None, false)), None); + assert_eq!(f(&file("SONY", 0xFFFF, None, None, false)), None); + assert_eq!(f(&file("Canon", 0xFFFF, Some("R03"), None, false)), Some(AdobeRgb)); + assert_eq!(f(&file("NIKON CORPORATION", 1, Some("R98"), Some(2), false)), Some(AdobeRgb)); + assert_eq!(f(&file("NIKON CORPORATION", 0xFFFF, Some("R03"), None, false)), Some(AdobeRgb)); + assert_eq!(f(&file("Canon", 0xFFFF, Some("R03"), None, true)), None); + } + + #[test] + fn nikon_crop_area_preserves_sensor_origin_and_rejects_invalid_rectangles() { + let (w, h) = (32, 24); + let words: Vec = (0..w * h).flat_map(|i| (100 + i as u16).to_be_bytes()).collect(); + for (values, expected) in [ + (vec![2, 4, 28, 16], Rect::new(2, 4, 28, 16)), + (vec![31, 4, 28, 16], Rect::new(0, 0, 32, 24)), + (vec![2, 4, 0, 16], Rect::new(0, 0, 32, 24)), + (vec![u32::MAX, 4, 28, 16], Rect::new(0, 0, 32, 24)), + (vec![2, 4, 28], Rect::new(0, 0, 32, 24)), + ] { + let mut mn = IfdBuilder::new(); + mn.set(CROP_AREA, Value::Long(values)); + let bytes = nef_with_note(1, 14, vec![words.clone()], w, h, h, Some(mn)); + let raw = crate::decode(&bytes).unwrap(); + assert_eq!(raw.crop, expected); + assert_eq!(raw.active_area, Rect::new(0, 0, 32, 24)); + assert_eq!((raw.crop.width, raw.crop.height), (expected.width, expected.height)); + assert_eq!(raw.info().developed_size(), (expected.width, expected.height)); + } + } + + /// The D4 records no CropArea: its measured image area and saturation level apply to files of its data size and + /// depth only. + #[test] + fn measured_d4_image_area_and_white_level() { + let (w, h) = (4992usize, 3292usize); + let decode_as = |model: &str, bits: u16| { + let mut nef = nef(1, bits, vec![vec![0x10; w * h * 2]], w as u32, h as u32, h as u32); + let at = nef.windows(10).position(|b| b == b"NIKON TEST").unwrap(); + let name = format!("{model:<10}"); + nef[at..at + 10].copy_from_slice(name.as_bytes()); + crate::decode(&nef).unwrap() + }; + let d4 = decode_as("NIKON D4", 14); + assert_eq!(d4.crop, Rect::new(8, 6, 4928, 3280)); + assert_eq!(d4.white, vec![15520.0]); + assert_eq!((d4.crop.width, d4.crop.height), (4928, 3280)); + assert_eq!(d4.info().developed_size(), (4928, 3280)); + // the 12-bit mode reads out the same area, and its level is the 14-bit one shifted like its samples + let d4_12 = decode_as("NIKON D4", 12); + assert_eq!(d4_12.crop, Rect::new(8, 6, 4928, 3280)); + assert_eq!(d4_12.white, vec![3880.0]); + let other = decode_as("NIKON D5", 14); + assert_eq!(other.crop, Rect::new(0, 0, w, h)); + assert_ne!(other.white, vec![15520.0]); + } + + /// storytold/lightcraft#976: the D7000 and D5100 record no CropArea, and their 36 masked columns at ~600 were developed as a + /// coloured bar. Their measured area applies to the measured models, data sizes and depths only. + #[test] + fn measured_d7000_and_d5100_image_area() { + let (w, h) = (4992usize, 3280usize); + let decode_as = |model: &str, bits: u16| { + let nef = nef_model_in(ByteOrder::Big, model, 1, bits, vec![vec![0x10; w * h * 2]], w as u32, h as u32, h as u32, None); + crate::decode(&nef).unwrap() + }; + for (model, bits) in [("NIKON D7000", 14), ("NIKON D7000", 12), ("NIKON D5100", 14)] { + let raw = decode_as(model, bits); + assert_eq!(raw.crop, Rect::new(8, 8, 4928, 3264), "{model} {bits}-bit"); + assert_eq!((raw.crop.width, raw.crop.height), (4928, 3264)); + assert_eq!(raw.info().developed_size(), (4928, 3264)); + } + // another model, or the D5100's unmeasured 12-bit mode, keeps the whole data + for (model, bits) in [("NIKON D7100", 14), ("NIKON D5100", 12), ("NIKON D7000 ", 16)] { + assert_eq!(decode_as(model, bits).crop, Rect::new(0, 0, w, h), "{model} {bits}-bit"); + } + } + + /// The D200, D3200 and Nikon 1 V1 keep masked or dark lines in their whole data (the D200 shares the D80's data + /// size but frames it elsewhere), measured on public sample files beyond the CC0 ones; the D3100, D5000, D40, D50, + /// D70 and D70s get best-guess frames (the D40 shares the D50 / D70 / D70s data size but not their frame). + #[test] + fn measured_older_dx_bodies_image_area() { + let decode_as = |model: &str, (w, h): (usize, usize)| { + let nef = nef_model_in(ByteOrder::Big, model, 1, 12, vec![vec![0x10; w * h * 2]], w as u32, h as u32, h as u32, None); + crate::decode(&nef).unwrap() + }; + for (model, size, crop) in [ + ("NIKON D200", (3904, 2616), Rect::new(16, 12, 3872, 2592)), + ("NIKON D80", (3904, 2616), Rect::new(14, 9, 3872, 2592)), + ("NIKON D3200", (6080, 4012), Rect::new(8, 6, 6016, 4000)), + ("NIKON 1 V1", (3904, 2606), Rect::new(12, 6, 3872, 2592)), + ("NIKON D3100", (4672, 3084), Rect::new(24, 6, 4608, 3072)), + ("NIKON D5000", (4352, 2868), Rect::new(11, 10, 4288, 2848)), + ("NIKON D40", (3040, 2014), Rect::new(16, 9, 3008, 2000)), + ("NIKON D50", (3040, 2014), Rect::new(15, 7, 3008, 2000)), + ("NIKON D70", (3040, 2014), Rect::new(15, 7, 3008, 2000)), + ("NIKON D70s", (3040, 2014), Rect::new(15, 7, 3008, 2000)), + ] { + assert_eq!(decode_as(model, size).crop, crop, "{model}"); + } + // the V1's area on the V2's data size: no entry, the whole data + assert_eq!(decode_as("NIKON 1 V1", (4620, 3084)).crop, Rect::new(0, 0, 4620, 3084)); + } + + /// Every measured entry is a crop inside its data, once per model, data size and depth, with a white level (if + /// any) inside its bit range. + #[test] + fn measured_bodies_are_consistent() { + let mut seen = std::collections::HashSet::new(); + for b in MEASURED_BODIES { + let (x, y, w, h) = b.crop; + assert!(w > 0 && h > 0 && x + w <= b.size.0 && y + h <= b.size.1, "{}: crop outside its data", b.model); + assert!(seen.insert((b.model, b.size, b.bits)), "{} {:?} {}-bit listed twice", b.model, b.size, b.bits); + assert!(matches!(b.bits, 12 | 14), "{}: {}-bit", b.model, b.bits); + assert!(b.white.is_none_or(|v| v > 0.0 && v < (1u32 << b.bits) as f32), "{}: white level", b.model); + assert_eq!(b.model.trim(), b.model, "{}: model names are matched trimmed", b.model); + } + } + + /// storytold/lightcraft#1248: the D7100 records no CropArea, and its data holds picture beyond Lightroom's frame in both + /// image areas (full frame and 1.3x crop), so renders came out larger and Lightroom crops landed off. + #[test] + fn measured_d7100_image_area() { + let decode_as = |model: &str, (w, h): (usize, usize)| { + let mut nef = nef(1, 14, vec![vec![0x10; w * h * 2]], w as u32, h as u32, h as u32); + // the fixture's model is "NIKON TEST" plus its NUL: 11 bytes to overwrite + let at = nef.windows(11).position(|b| b == b"NIKON TEST\0").unwrap(); + let name = format!("{model:\0<11}"); + nef[at..at + 11].copy_from_slice(name.as_bytes()); + crate::decode(&nef).unwrap() + }; + for (size, crop) in [((6036, 4020), Rect::new(18, 10, 6000, 4000)), ((4812, 3212), Rect::new(6, 6, 4800, 3200))] { + let d7100 = decode_as("NIKON D7100", size); + assert_eq!(d7100.crop, crop); + assert_eq!((d7100.crop.width, d7100.crop.height), (crop.width, crop.height)); + assert_ne!(d7100.white, vec![15520.0]); + assert_eq!(d7100.info().developed_size(), (crop.width, crop.height)); + assert_eq!(decode_as("NIKON D7200", size).crop, Rect::new(0, 0, size.0, size.1)); + } + } + + /// The D90's last 42 columns are filler at a flat ~254, above its dark image samples: without the measured + /// area they stay in the image and develop as a pink band on the right. + #[test] + fn measured_d90_drops_the_filler_columns() { + let (w, h) = (4352usize, 2868usize); + let mut px = vec![40u16; w * h]; + for row in px.chunks_mut(w) { + row[4310..].fill(254); + } + let mut nef = nef(1, 12, vec![px.iter().flat_map(|v| v.to_le_bytes()).collect()], w as u32, h as u32, h as u32); + let at = nef.windows(10).position(|b| b == b"NIKON TEST").unwrap(); + nef[at..at + 10].copy_from_slice(b"NIKON D90 "); + let d90 = crate::decode(&nef).unwrap(); + assert_eq!(d90.crop, Rect::new(10, 10, 4288, 2848)); + assert!(d90.crop.x + d90.crop.width <= 4310, "the filler columns are outside the image"); + assert_eq!(d90.white, vec![3830.0]); + assert_eq!((d90.crop.width, d90.crop.height), (4288, 2848)); + } + + /// storytold/lightcraft#708: the active width is judged from the first [`MASK_ROWS`] rows in both modes, so the header probe + /// (which reads only those rows' strips) and the full decode agree whether the masked columns are there or not. + #[test] + fn masked_columns_are_judged_from_the_top_rows_in_both_modes() { + let (w, h, rps) = (256usize, MASK_ROWS + 64, 8u32); + let file = |masked_rows: std::ops::Range| { + let words: Vec = (0..w * h) + .map(|i| if masked_rows.contains(&(i / w)) && i % w >= w - 8 { 0u16 } else { 1000 + (i % 7) as u16 }) + .flat_map(|v| v.to_be_bytes()) + .collect(); + nef(1, 14, words.chunks(w * 2 * rps as usize).map(|c| c.to_vec()).collect(), w as u32, h as u32, rps) + }; + for (masked_rows, expected_width) in [(0..h, w - 8), (0..MASK_ROWS, w - 8), (MASK_ROWS..h, w), (0..0, w)] { + let bytes = file(masked_rows.clone()); + let full = crate::decode(&bytes).unwrap(); + assert_eq!(full.active_area.width, expected_width, "masked rows {masked_rows:?}"); + assert_eq!(full.crop.width, expected_width); + assert_eq!(crate::probe_info(&bytes).unwrap(), full.info(), "masked rows {masked_rows:?}"); + } + // the shared rule on its own: nominal white, trailing columns at most 64, even width + let d: Vec = (0..w * 4).map(|i| if i % w >= w - 9 { 0 } else { 600 }).collect(); + assert_eq!(trailing_masked_columns(&d, w, 14), w - 10, "nine dark columns trim to an even width"); + assert_eq!(trailing_masked_columns(&d, w, 12), w - 10); + assert_eq!(trailing_masked_columns(&d[..w - 1], w, 14), w, "less than a row of samples leaves the width alone"); + assert_eq!(trailing_masked_columns(&[0; 64], 64, 14), 64, "narrow images are left alone"); + assert_eq!(trailing_masked_columns(&[], 0, 14), 0); + } + + #[test] + fn black_level_tag_is_in_14_bit_units() { + let (w, h) = (16usize, 4usize); + let words: Vec = (0..w * h).flat_map(|i| (100 + i as u16).to_be_bytes()).collect(); + let note = || { + let mut mn = IfdBuilder::new(); + mn.set(BLACK_LEVEL, Value::Short(vec![400, 404, 408, 412])); + mn + }; + let black = |bits| crate::decode(&nef_with_note(1, bits, vec![words.clone()], w as u32, h as u32, h as u32, Some(note()))).unwrap().black; + assert_eq!(black(12).values, vec![100.0, 101.0, 102.0, 103.0]); + assert_eq!(black(14).values, vec![400.0, 404.0, 408.0, 412.0]); + } + + #[test] + fn uncompressed_word16_and_packed12() { + let (w, h) = (16usize, 6usize); + let px: Vec = (0..w * h).map(|i| (i * 131 % 4096) as u16).collect(); + let words: Vec = px.iter().flat_map(|v| v.to_be_bytes()).collect(); + let bytes = nef(1, 12, words.chunks(w * 2 * 3).map(|c| c.to_vec()).collect(), w as u32, h as u32, 3); + assert_eq!(crate::probe(&bytes), Some(RawFormat::Nef)); + let r = crate::decode(&bytes).unwrap(); + assert_eq!(r.data, RawData::U16(px.clone())); + assert_eq!(r.cfa.as_ref().unwrap().name(), "BGGR"); + // 12-bit MSB packed + let mut packed = Vec::new(); + for pair in px.chunks(2) { + let (a, b) = (pair[0] as u32, pair[1] as u32); + let v = (a << 12) | b; + packed.extend_from_slice(&[(v >> 16) as u8, (v >> 8) as u8, v as u8]); + } + let bytes = nef(1, 12, vec![packed], w as u32, h as u32, h as u32); + assert_eq!(crate::decode(&bytes).unwrap().data, RawData::U16(px)); + } + + /// An NRW data block (`"NRW 0104"`, fields from byte 8) with `black` at byte `0x20`, as a maker note. + fn nrw_note(black: u32) -> IfdBuilder { + let mut block = b"NRW 0104".to_vec(); + block.resize(0x20, 0); + block.extend_from_slice(&black.to_le_bytes()); + block.resize(0x40, 0); + let mut mn = IfdBuilder::new(); + mn.set(NRW_DATA, Value::Undefined(block)); + mn + } + + /// 12-bit samples as an MSB-first bit stream inside 32-bit words in `order` (rows are whole words). + fn words32(px: &[u16], w: usize, order: ByteOrder) -> Vec { + let mut out = Vec::new(); + for row in px.chunks(w) { + let stream = row.iter().fold(Vec::new(), |mut bits: Vec, v| { + bits.extend((0..12).rev().map(|b| v >> b & 1 == 1)); + bits + }); + for word in stream.chunks(32) { + let v = word.iter().enumerate().fold(0u32, |a, (i, b)| a | (*b as u32) << (31 - i)); + out.extend_from_slice(&match order { + ByteOrder::Little => v.to_le_bytes(), + ByteOrder::Big => v.to_be_bytes(), + }); + } + } + out + } + + #[test] + fn nrw_block_selects_32_bit_word_packing_and_supplies_the_black_level() { + let (w, h) = (16usize, 6usize); + let px: Vec = (0..w * h).map(|i| (200 + i * 131 % 3800) as u16).collect(); + let packed = words32(&px, w, ByteOrder::Little); + assert_eq!(packed.len(), w * h * 3 / 2); + let decode = |note: Option| { + crate::decode(&nef_in(ByteOrder::Little, 1, 12, vec![packed.clone()], w as u32, h as u32, h as u32, note)).unwrap() + }; + let r = decode(Some(nrw_note(200))); + assert_eq!(r.data, RawData::U16(px.clone())); + assert_eq!(r.black.values, vec![200.0]); + assert_eq!(decode(Some(nrw_note(0))).black.values, vec![0.0]); + // an implausible field is not a black level + assert_eq!(decode(Some(nrw_note(60000))).black.values, vec![0.0]); + // without the block the same bytes are an MSB-first byte stream + let plain = decode(None); + assert_ne!(plain.data, RawData::U16(px)); + assert_eq!(plain.black.values, vec![0.0]); + // the maker-note black level (14-bit units) wins when present + let mut both = nrw_note(200); + both.set(BLACK_LEVEL, Value::Short(vec![400, 404, 408, 412])); + assert_eq!(decode(Some(both)).black.values, vec![100.0, 101.0, 102.0, 103.0]); + } + + #[test] + fn compressed_without_table_is_unsupported() { + let bytes = nef(34713, 14, vec![vec![0; 64]], 8, 8, 8); + let Err(RawError::Unsupported(why)) = crate::decode(&bytes) else { panic!("expected unsupported") }; + assert!(why.contains("linearization table"), "{why}"); + } + + /// storytold/lightcraft#193: a High Efficiency NEF (Z f, Z 8: compression 34713, no `0x0096` table, a JPEG XS codestream) is + /// named as such instead of being blamed on the missing table, and the header probe reports the same reason. + #[test] + fn high_efficiency_nef_is_named() { + let mut strip = vec![0xff, 0x10, 0xff, 0x50, 0x00, 0x22]; + strip.resize(64, 0); + let bytes = nef(34713, 14, vec![strip], 8, 8, 8); + for result in [crate::decode(&bytes).map(|_| ()), crate::probe_info(&bytes).map(|_| ())] { + let Err(RawError::Unsupported(why)) = result else { panic!("expected unsupported") }; + assert_eq!(why, HIGH_EFFICIENCY); + assert!(why.starts_with("Nikon High Efficiency NEF ("), "{why}"); + } + // a strip too short to hold the markers is still the missing-table case, not a panic + let short = nef(34713, 14, vec![vec![0xff, 0x10]], 1, 1, 1); + assert!(matches!(crate::decode(&short), Err(RawError::Unsupported(w)) if w.contains("linearization table"))); + } + + fn samples(n: usize, bits: u32) -> Vec { + (0..n).map(|i| (((i * 2654435761usize) >> 7) as u32 % (1 << bits)) as u16).collect() + } + + fn pack_lsb(row: &[u16], bits: u32) -> Vec { + let (mut out, mut acc, mut n) = (Vec::new(), 0u64, 0u32); + for &v in row { + acc |= (v as u64) << n; + n += bits; + while n >= 8 { + out.push(acc as u8); + acc >>= 8; + n -= 8; + } + } + if n > 0 { + out.push(acc as u8); + } + out + } + + fn pack_msb12(row: &[u16]) -> Vec { + let mut out = Vec::new(); + for pair in row.chunks(2) { + let (a, b) = (pair[0], pair.get(1).copied().unwrap_or(0)); + out.extend([(a >> 4) as u8, ((a & 15) << 4) as u8 | (b >> 8) as u8, b as u8]); + } + out + } + + /// Rows of `samples` packed by `row`, each padded with `pad` bytes of 0xA5 (never decoded). + fn strip(data: &[u16], w: usize, pad: usize, row: impl Fn(&[u16]) -> Vec) -> Vec { + data.chunks(w).flat_map(|r| row(r).into_iter().chain(std::iter::repeat_n(0xA5, pad))).collect() + } + + fn decoded(strip: Vec, bits: u16, w: u32, h: u32) -> Result> { + let bytes = nef(34713, bits, vec![strip], w, h, h); + match crate::decode(&bytes)?.data { + RawData::U16(d) => Ok(d), + _ => Err(RawError::Unsupported("float".into())), + } + } + + #[test] + fn packed_34713_layouts_round_trip() { + let (w, h) = (40usize, 6usize); + let d12 = samples(w * h, 12); + let d14 = samples(w * h, 14); + // group A: 12-bit, 3w/2 bytes per row + assert_eq!(decoded(strip(&d12, w, 0, |r| pack_lsb(r, 12)), 12, 40, 6).unwrap(), d12); + // group B: 12-bit, rows padded to 16 bytes (60 -> 64) + assert_eq!(decoded(strip(&d12, w, 4, |r| pack_lsb(r, 12)), 12, 40, 6).unwrap(), d12); + // group C: 14-bit, 70 bytes -> 80 + assert_eq!(decoded(strip(&d14, w, 10, |r| pack_lsb(r, 14)), 14, 40, 6).unwrap(), d14); + // group E: 16-byte chunks, 15 data bytes (ten samples) + a zero byte; 16 chunks = 160 samples (at 40 wide the + // 64-byte stride would be group B's) + let (w, h) = (160usize, 3usize); + let d12 = samples(w * h, 12); + let e = strip(&d12, w, 0, |r| r.chunks(10).flat_map(|c| pack_msb12(c).into_iter().chain([0u8])).collect()); + assert_eq!(e.len(), 256 * h); + assert_eq!(decoded(e, 12, 160, 3).unwrap(), d12); + } + + /// Z 6II "Packed 14 bits" (maker note 0x93 = 10, storytold/lightcraft#769): 6064x4040, 14-bit, one strip of 4040 rows of 10624 + /// bytes (6064 * 14 / 8 = 10612, padded to a multiple of 16). Four rows keep the real stride. + #[test] + fn packed_14_bit_z6ii_geometry_with_row_padding() { + let (w, h) = (6064usize, 4usize); + let d14 = samples(w * h, 14); + let s = strip(&d14, w, 12, |r| pack_lsb(r, 14)); + assert_eq!(s.len(), 10624 * h); + assert_eq!(decoded(s, 14, w as u32, h as u32).unwrap(), d14); + assert_eq!(packed_layout(6064, 4040, 14, 42_920_960, false), Some(PackedLayout::Lsb { stride: 10624 })); + } + + /// The maker note's compression mode routes the strip: 6 and 10 are packed samples even when a `0x0096` + /// table is present (which the Huffman reader would otherwise take), in the old tag and in the `0x0051` + /// record, in either byte order; a depth that contradicts the mode, and High Efficiency (13, 14), are refused. + #[test] + fn maker_note_compression_mode_routes_packed_and_high_efficiency() { + let (w, h) = (40usize, 6usize); + let note = |order: ByteOrder, modern: bool, mode: u16| { + let mut n = IfdBuilder::new(); + n.set(LINEARIZATION_TABLE, Value::Undefined(vec![0x44, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])); + if modern { + let mut record = b"01000400\0\0".to_vec(); + record.extend(if order == ByteOrder::Big { mode.to_be_bytes() } else { mode.to_le_bytes() }); + n.set(NEF_COMPRESSION_RECORD, Value::Undefined(record)); + } else { + n.set(NEF_COMPRESSION, Value::Short(vec![mode])); + } + n + }; + let file = |order, modern, mode, bits: u16, image: Vec| { + nef_in(order, 34713, bits, vec![image], w as u32, h as u32, h as u32, Some(note(order, modern, mode))) + }; + for (bits, mode) in [(12u32, 6u16), (14, 10)] { + let data = samples(w * h, bits); + // rows padded to 16 bytes, and tight rows (14-bit tight rows only when the mode is stated) + for pad in [(16 - (w * bits as usize / 8) % 16) % 16, 0] { + let image = strip(&data, w, pad, |r| pack_lsb(r, bits)); + for order in [ByteOrder::Big, ByteOrder::Little] { + for modern in [false, true] { + let RawData::U16(got) = crate::decode(&file(order, modern, mode, bits as u16, image.clone())).unwrap().data else { + panic!("integer samples") + }; + assert_eq!(got, data, "{bits}-bit, pad {pad}, {order:?}, modern {modern}"); + } + } + } + } + let d14 = samples(w * h, 14); + let image = strip(&d14, w, 10, |r| pack_lsb(r, 14)); + // tight 14-bit rows without a stated mode stay refused + assert!(decoded(strip(&d14, w, 0, |r| pack_lsb(r, 14)), 14, w as u32, h as u32).is_err()); + // the mode says 12-bit packed, the IFD 14 bits + assert!(matches!(crate::decode(&file(ByteOrder::Big, false, 6, 14, image.clone())), Err(RawError::Unsupported(_)))); + for mode in [13, 14] { + for modern in [false, true] { + let err = crate::decode(&file(ByteOrder::Big, modern, mode, 14, image.clone())).unwrap_err(); + assert!(matches!(&err, RawError::Unsupported(m) if m == HIGH_EFFICIENCY), "{err:?}"); + } + } + } + + #[test] + fn packed_layout_refuses_overflowing_dimensions() { + for bits in [12, 14] { + assert!(packed_layout(usize::MAX - 3, 1, bits, u64::MAX, false).is_none()); + } + assert!(d100_layout(usize::MAX, 1, 12, u64::MAX).is_none()); + } + + /// Header probes inspect only the same top rows that the masked-column test uses. Routing + /// still validates the complete strip's layout, so header and full decode keep the same crop. + #[test] + fn packed_header_probe_reads_only_the_mask_rows() { + let (w, h) = (160usize, MASK_ROWS + 7); + let data = samples(w * h, 12); + for image in [strip(&data, w, 0, |r| pack_lsb(r, 12)), be16(&data)] { + let bytes = nef(34713, 12, vec![image], w as u32, h as u32, h as u32); + let tiff = Tiff::parse(&bytes).unwrap(); + let info = raw_ifd(&tiff).unwrap().image().unwrap(); + let RawData::U16(top) = packed(&bytes, &info, MASK_ROWS, false, &Limits::default()).unwrap() else { panic!("integer samples") }; + assert_eq!(top, data[..w * MASK_ROWS]); + let header = decode(&bytes, Mode::Header, &Limits::default()).unwrap(); + let full = decode(&bytes, Mode::Full, &Limits::default()).unwrap(); + assert_eq!((header.active_area, header.crop), (full.active_area, full.crop)); + assert!(matches!(header.data, RawData::U16(ref samples) if samples.is_empty())); + } + } + + #[test] + fn packed_d100_rows_carry_extra_columns() { + // IFD width 154, rows hold 160 samples (16 chunks); the last 6 are dropped + let (w, wide, h) = (154usize, 160usize, 3usize); + let wide_data = samples(wide * h, 12); + let e = strip(&wide_data, wide, 0, |r| r.chunks(10).flat_map(|c| pack_msb12(c).into_iter().chain([0u8])).collect()); + let got = decoded(e, 12, w as u32, h as u32).unwrap(); + let want: Vec = wide_data.chunks(wide).flat_map(|r| r[..w].iter().copied()).collect(); + assert_eq!(got, want); + } + + #[test] + fn packed_34713_wrong_sizes_stay_refused() { + let d12 = samples(40 * 6, 12); + let good = strip(&d12, 40, 0, |r| pack_lsb(r, 12)); + // one byte more or fewer, a stride that is neither tight nor 16-aligned, a 13-bit file + for len in [good.len() - 1, good.len() + 1, good.len() + 6] { + let mut s = good.clone(); + s.resize(len, 0); + assert!(matches!(decoded(s, 12, 40, 6), Err(RawError::Unsupported(_))), "len {len}"); + } + assert!(matches!(decoded(good.clone(), 13, 40, 6), Err(RawError::Unsupported(_)))); + // 14-bit rows must be padded to 16 bytes + let d14 = samples(40 * 6, 14); + assert!(matches!(decoded(strip(&d14, 40, 0, |r| pack_lsb(r, 14)), 14, 40, 6), Err(RawError::Unsupported(_)))); + } + + /// A small raw strip of `[Y0 Y1 Cb Cr]` groups, rows of `3 * w` bytes, LSB-first 12-bit. + fn small_strip(groups: &[[u16; 4]], w: usize) -> Vec { + let flat: Vec = groups.iter().flatten().copied().collect(); + assert_eq!(flat.len() % (2 * w), 0); + flat.chunks(2 * w).flat_map(|r| pack_lsb(r, 12)).collect() + } + + fn small(strip: Vec, bits: u16, w: u32, h: u32) -> Result { + crate::decode(&nef(34713, bits, vec![strip], w, h, h)) + } + + #[test] + fn small_raw_422_round_trips() { + let (w, h) = (8usize, 3usize); + // flat chroma per row so the interpolation is exact; distinct luma everywhere + let mut groups = Vec::new(); + for y in 0..h { + for k in 0..w / 2 { + let (cb, cr) = (2048 + 10 * y as u16, 2048 - 5 * y as u16); + groups.push([100 + (y * w + 2 * k) as u16, 100 + (y * w + 2 * k + 1) as u16, cb, cr]); + } + } + let raw = small(small_strip(&groups, w), 12, w as u32, h as u32).unwrap(); + assert_eq!((raw.width, raw.height, raw.cpp, raw.cfa.is_none()), (w, h, 3, true)); + assert_eq!(raw.wb_multipliers, Some([1.0; 3])); + let RawData::U16(d) = &raw.data else { panic!() }; + for y in 0..h { + for x in 0..w { + let luma = (100 + y * w + x) as f32; + let (u, v) = (10.0 * y as f32, -5.0 * y as f32); + let (r, b) = (luma + SMALL_RAW_R_GAIN * v, luma + SMALL_RAW_B_GAIN * u); + let g = (luma - 0.299 * r - 0.114 * b) / 0.587; + for (i, want) in [r, g, b].into_iter().enumerate() { + let got = f32::from(d[(y * w + x) * 3 + i]) / SMALL_RAW_SCALE; + assert!((got - want).abs() < 0.1, "({x},{y}) channel {i}: {got} vs {want}"); + } + } + } + } + + #[test] + fn small_raw_neutral_chroma_is_grey_and_chroma_is_interpolated() { + let w = 8usize; + let groups: Vec<[u16; 4]> = (0..w / 2).map(|k| [200 + 40 * k as u16, 200 + 40 * k as u16, 2048, 2048]).collect(); + let raw = small(small_strip(&groups, w), 12, w as u32, 1).unwrap(); + let RawData::U16(d) = &raw.data else { panic!() }; + for x in 0..w { + let px = &d[x * 3..x * 3 + 3]; + assert!(px.iter().all(|&v| v == px[0]), "{px:?}"); + assert_eq!(f32::from(px[0]), (200 + 40 * (x / 2)) as f32 * SMALL_RAW_SCALE); + } + // a step in Cb between the first two pairs is shared 3:1 by the pixels next to the boundary + let mut groups = groups; + groups[1][2] = 2048 + 100; + let raw = small(small_strip(&groups, w), 12, w as u32, 1).unwrap(); + let RawData::U16(d) = &raw.data else { panic!() }; + let blue_minus_luma = |x: usize, luma: f32| f32::from(d[x * 3 + 2]) / SMALL_RAW_SCALE - luma; + assert!((blue_minus_luma(1, 200.0) - SMALL_RAW_B_GAIN * 25.0).abs() < 0.1); + assert!((blue_minus_luma(2, 240.0) - SMALL_RAW_B_GAIN * 75.0).abs() < 0.1); + } + + #[test] + fn small_raw_wrong_sizes_and_depths_are_refused() { + let (w, h) = (8usize, 3usize); + let good = small_strip(&vec![[100, 100, 2048, 2048]; w / 2 * h], w); + assert!(small(good.clone(), 12, w as u32, h as u32).is_ok()); + for len in [good.len() - 1, good.len() + 1] { + let mut s = good.clone(); + s.resize(len, 0); + assert!(matches!(small(s, 12, w as u32, h as u32), Err(RawError::Unsupported(_))), "len {len}"); + } + assert!(matches!(small(good.clone(), 14, w as u32, h as u32), Err(RawError::Unsupported(_)))); + // odd width: 3 bytes per pixel does not split into pixel pairs + assert!(matches!(small(vec![0; 3 * 7 * 3], 12, 7, 3), Err(RawError::Unsupported(_)))); + } + + #[test] + fn small_raw_short_input_does_not_panic() { + for len in [0, 1, 2, 3, 5, 11, 12, 13] { + let mut out = vec![0u16; 12]; + small_raw_row(&vec![0xFF; len], 4, &mut out); + } + let mut bytes = nef(34713, 12, vec![vec![0x55; 3 * 8 * 3]], 8, 3, 3); + bytes.truncate(bytes.len() - 40); + assert!(crate::decode(&bytes).is_err(), "a truncated small-raw strip is corrupt"); + } + + #[test] + fn small_raw_header_has_full_geometry_without_allocating_pixels() { + let (w, h) = (8usize, 3usize); + let bytes = nef(34713, 12, vec![small_strip(&vec![[100, 110, 2048, 2048]; w / 2 * h], w)], w as u32, h as u32, h as u32); + let header = decode(&bytes, Mode::Header, &Limits::default()).unwrap(); + let full = decode(&bytes, Mode::Full, &Limits::default()).unwrap(); + assert_eq!(header.info(), full.info()); + assert!(matches!(header.data, RawData::U16(ref samples) if samples.is_empty())); + assert!(matches!(full.data, RawData::U16(ref samples) if samples.len() == 3 * w * h)); + } + + #[test] + fn small_raw_rejects_excessive_dimensions_before_allocation() { + let bytes = nef(34713, 12, vec![vec![0; 72]], 8, 3, 3); + let tiff = Tiff::parse(&bytes).unwrap(); + let mut info = raw_ifd(&tiff).unwrap().image().unwrap(); + let strip = info.chunks(bytes.len() as u64)[0]; + info.width = u32::MAX - 1; + info.height = u32::MAX; + assert!(matches!(small_raw(&bytes, &tiff, &info, strip, Mode::Header, &Limits::default()), Err(RawError::Limit(_)))); + let mut out = [0; 12]; + small_raw_row(&[], usize::MAX, &mut out); + small_raw_row(&[], 3, &mut out); + assert_eq!(out, [0; 12]); + } + + /// Big-endian 16-bit words holding the 12-bit samples in their upper bits. + fn be16(data: &[u16]) -> Vec { + data.iter().flat_map(|v| (v << 4).to_be_bytes()).collect() + } + + #[test] + fn coolpix_be16_words_decode_and_wrong_sizes_are_refused() { + let (w, h) = (40usize, 6usize); + let d12 = samples(w * h, 12); + let good = be16(&d12); + assert_eq!(good.len(), 2 * w * h); + assert_eq!(decoded(good.clone(), 12, 40, 6).unwrap(), d12); + // a half-LSB in the low nibble does not change the sample + let mut half = good.clone(); + for b in half.iter_mut().skip(1).step_by(2) { + *b |= 8; + } + assert_eq!(decoded(half, 12, 40, 6).unwrap(), d12); + // any other strip size, or another bit depth, is not this layout + for len in [good.len() - 2, good.len() - 1, good.len() + 1, good.len() + 2, good.len() + 16] { + let mut s = good.clone(); + s.resize(len, 0); + assert!(matches!(decoded(s, 12, 40, 6), Err(RawError::Unsupported(_))), "len {len}"); + } + assert!(matches!(decoded(good.clone(), 13, 40, 6), Err(RawError::Unsupported(_)))); + // the black level is 200, but never above the darkest sample + let black = |data: &[u16]| crate::decode(&nef(34713, 12, vec![be16(data)], 40, 6, 6)).unwrap().black.values[0]; + let mut d = d12.clone(); + for v in &mut d { + *v = (*v).max(300); + } + assert_eq!(black(&d), 200.0); + d[5] = 150; + assert_eq!(black(&d), 150.0); + } + + #[test] + fn coolpix_be16_does_not_take_other_layouts() { + // every width where another group's stride equals 2w must still go to that group (the narrow rows where they + // coincide are below the real-world widths; the other groups are tried first) + for w in (2..=400usize).step_by(2) { + let strip = 2 * w as u64 * 6; + let other = packed_layout(w, 6, 12, strip, false).or_else(|| d100_layout(w, 6, 12, strip)); + if other.is_some() { + assert_eq!( + packed_layout(w, 6, 12, strip, false).or_else(|| d100_layout(w, 6, 12, strip)).or_else(|| be16_layout(w, 6, 12, strip)), + other + ); + } + } + assert_eq!(be16_layout(4032, 3024, 12, 24385536), Some(PackedLayout::Be16)); + assert!(packed_layout(4032, 3024, 12, 24385536, false).is_none() && d100_layout(4032, 3024, 12, 24385536).is_none()); + assert!(be16_layout(4032, 3024, 14, 24385536).is_none()); + assert!(be16_layout(usize::MAX, usize::MAX, 12, u64::MAX).is_none()); + } + + #[test] + fn packed_34713_short_input_does_not_panic() { + let (w, h) = (40usize, 6usize); + for layout in [PackedLayout::Lsb { stride: 64 }, PackedLayout::Lsb { stride: 80 }, PackedLayout::Chunked16 { stride: 64 }, PackedLayout::Be16] + { + for len in [0, 1, 15, 16, 17, 63, 64, 65, 200] { + let bits = if matches!(layout, PackedLayout::Lsb { stride: 80 }) { 14 } else { 12 }; + assert_eq!(unpack_packed(&vec![0xFF; len], w, h, bits, layout).len(), w * h); + } + } + // a strip that claims more bytes than the file has + let mut bytes = nef(34713, 12, vec![vec![0x55; 60 * 6]], 40, 6, 6); + bytes.truncate(bytes.len() - 100); + assert!(crate::decode(&bytes).is_err(), "a truncated packed strip is corrupt"); + } + /// Keep every directory intact before a relocated tail strip, then remove its last rows. + fn truncated_tail_strip(mut bytes: Vec, keep: usize) -> Vec { + let tiff = Tiff::parse(&bytes).unwrap(); + let raw = raw_ifd(&tiff).unwrap(); + let info = raw.image().unwrap(); + let chunk = info.chunks(bytes.len() as u64)[0]; + let data = craft_tiff::image::chunk_bytes(&bytes, &chunk).unwrap().to_vec(); + let offset_entry = raw.get(t::STRIP_OFFSETS).unwrap().offset as usize; + let start = bytes.len() as u32; + bytes[offset_entry..offset_entry + 4].copy_from_slice(&start.to_be_bytes()); + bytes.extend_from_slice(&data[..keep]); + bytes + } + + #[test] + fn packed_rejects_tail_truncation_with_intact_directories() { + let bytes = nef(34713, 12, vec![vec![0x55; 360]], 40, 6, 6); + for keep in [0, 60, 359] { + let short = truncated_tail_strip(bytes.clone(), keep); + for mode in [Mode::Header, Mode::Full] { + assert!(matches!(decode(&short, mode, &Limits::default()), Err(RawError::Corrupt(_)))); + } + } + } + + #[test] + fn small_raw_rejects_tail_truncation_with_intact_directories() { + let bytes = nef(34713, 12, vec![vec![0x55; 72]], 8, 3, 3); + for keep in [0, 24, 71] { + let short = truncated_tail_strip(bytes.clone(), keep); + for mode in [Mode::Header, Mode::Full] { + assert!(matches!(decode(&short, mode, &Limits::default()), Err(RawError::Corrupt(_)))); + } + } + } +} diff --git a/crates/raw/src/nef_lens.rs b/crates/raw/src/nef_lens.rs new file mode 100644 index 000000000..9f8dafb0e --- /dev/null +++ b/crates/raw/src/nef_lens.rs @@ -0,0 +1,259 @@ +//! Nikon's in-file distortion correction (raw SubIFD tag `0xc7d5`, NikonNEFInfo), expressed as a DNG warp for +//! the shared optics pipeline. +//! +//! Tag identity: the ExifTool Nikon tag-name documentation (NikonNEFInfo `0xc7d5`; its DistortionInfo `0x0005` +//! with DistortionCorrection and RadialDistortionCoefficient1..3). The model below (which stored value multiplies +//! which power, the normalisation, the fill-frame zoom and the optical-centre offset in entry `0x0008`) was inferred +//! independently by matching the raw against the camera's own corrected JPEG; see docs/nikon-lens-corrections.md. +//! No lens database or external decoder code. + +use crate::{Opcode, Rect}; +use craft_tiff::{ByteOrder, Ifd, ParseOptions, parse_ifd_at}; + +const NEF_INFO: u16 = 0xc7d5; +const DISTORTION_INFO: u16 = 0x0005; +const CENTRE_OFFSET: u16 = 0x0008; +/// Bodies whose files were checked against the camera's JPEG (docs/nikon-lens-corrections.md). Matching tags on +/// another body do not establish its pixel pitch or image area; add a body only after checking its files. +const VALIDATED: &[&str] = &["NIKON Z 6_2", "NIKON Z 8", "NIKON Z f"]; +/// The normalisation radius every checked file stores: the FX half-diagonal in millimetres. +const FX_HALF_DIAGONAL_MM: f64 = 21.6; +const SAMPLES: usize = 256; + +/// `r_src = r_out * (1 + k3 rho^2 + k2 rho^4 + k1 rho^6) / (1 + zoom)` around the optical centre, `rho` the radius +/// over the half-diagonal of the image area (`k1..k3` as ExifTool names them, in the order they are stored). +#[derive(Clone, Copy, Debug, PartialEq)] +struct Distortion { + k: [f64; 3], + zoom: f64, +} + +/// A signed rational at `at` (`i32 / i32`), when it is one. +fn rational(b: &[u8], at: usize, order: ByteOrder) -> Option { + let n = order.u32(b.get(at..at + 4)?.try_into().ok()?) as i32; + let d = order.u32(b.get(at + 4..at + 8)?.try_into().ok()?) as i32; + (d > 0).then(|| f64::from(n) / f64::from(d)).filter(|v| v.is_finite()) +} + +/// The NikonNEFInfo entries: the blob is `"Nikon\0"`, a 4-byte version, then a little TIFF stream. +fn nef_info(raw: &Ifd) -> Option<(Ifd, ByteOrder)> { + let tiff = raw.bytes(NEF_INFO)?.strip_prefix(b"Nikon\0")?.get(4..)?; + let (order, false) = craft_tiff::Tiff::sniff(tiff)? else { return None }; + let first = order.u32(tiff.get(4..8)?.try_into().ok()?); + let opts = ParseOptions { follow_children: false, ..Default::default() }; + let (ifd, _) = parse_ifd_at(tiff, u64::from(first), order, 0, false, &opts).ok()?; + Some((ifd, order)) +} + +/// DistortionInfo: `"0100"`, the correction mode (0 none), the normalisation radius (mm) at 8, the term count (4) +/// at 16, the coefficients at 20 / 28 / 36 and the zoom at 64, each a signed rational. +fn distortion_info(b: &[u8], order: ByteOrder) -> Option { + if b.get(0..4)? != b"0100" || *b.get(4)? == 0 { + return None; + } + let norm = rational(b, 8, order)?; + let terms = order.u32(b.get(16..20)?.try_into().ok()?); + if (norm - FX_HALF_DIAGONAL_MM).abs() > 0.05 || terms != 4 { + return None; + } + let k = [rational(b, 20, order)?, rational(b, 28, order)?, rational(b, 36, order)?]; + let zoom = rational(b, 64, order)?; + // real lenses stay far inside these (the checked files: |k| < 0.13, |zoom| < 0.09) + (k.iter().all(|v| v.abs() < 1.0) && zoom.abs() < 0.5).then_some(Distortion { k, zoom }) +} + +/// Entry `0x0008`: `"0100"`, then the optical centre's offset from the image-area centre in pixels (x right, +/// y down), two signed rationals. Zero when the lens stabilises (the sensor stays centred). +fn centre_offset(b: &[u8], order: ByteOrder) -> Option<(f64, f64)> { + (b.get(0..4)? == b"0100").then_some(())?; + Some((rational(b, 4, order)?, rational(b, 12, order)?)) +} + +/// The correction of a `model` NEF whose raw IFD is `raw`, for the image area `crop` inside `active` (both in +/// sensor pixels), as an `OpcodeList3` warp in active-area coordinates. `None` for unvalidated bodies, other +/// image areas, missing or implausible data, and maps that fold. +pub(super) fn distortion(model: &str, raw: &Ifd, active: Rect, crop: Rect) -> Option { + if !VALIDATED.contains(&model.trim()) { + return None; + } + let crop = crop.clipped(active.width, active.height); + if active.width < 2 || active.height < 2 || crop.width < 2 || crop.height < 2 { + return None; + } + // the stored radius is the FX half-diagonal: only the full 3:2 image area (not a DX or other crop of the + // sensor) is normalised by it + if (crop.width as f64 / crop.height as f64 - 1.5).abs() > 0.005 || (crop.width as f64) < active.width as f64 * 0.95 { + return None; + } + let (info, order) = nef_info(raw)?; + let d = distortion_info(info.bytes(DISTORTION_INFO)?, order)?; + let (ox, oy) = info.bytes(CENTRE_OFFSET).and_then(|b| centre_offset(b, order)).unwrap_or((0.0, 0.0)); + let (cw, ch) = (crop.width as f64 - 1.0, crop.height as f64 - 1.0); + if ox.abs() > cw * 0.05 || oy.abs() > ch * 0.05 { + return None; + } + let half_diagonal = cw.hypot(ch) / 2.0; + let poly = |rho: f64| { + let x = rho * rho; + 1.0 + x * (d.k[2] + x * (d.k[1] + x * d.k[0])) + }; + // DNG normalises by the farthest active-area corner from the centre; the image area reaches at most there + let (ax, ay) = (active.width as f64 - 1.0, active.height as f64 - 1.0); + let (cx, cy) = (crop.x as f64 + cw / 2.0 + ox, crop.y as f64 + ch / 2.0 + oy); + let farthest = cx.max(ax - cx).hypot(cy.max(ay - cy)); + let a = farthest / half_diagonal; + // the source radius must grow with the output radius out to the farthest corner, or the map folds + let mut previous = -1.0; + for j in 0..=SAMPLES { + let rho = a * j as f64 / SAMPLES as f64; + let source = rho * poly(rho) / (1.0 + d.zoom); + if !source.is_finite() || source <= previous { + return None; + } + previous = source; + } + let s = 1.0 / (1.0 + d.zoom); + let a2 = a * a; + let planes = vec![[s, d.k[2] * a2 * s, d.k[1] * a2 * a2 * s, d.k[0] * a2 * a2 * a2 * s, 0.0, 0.0]]; + Some(Opcode::WarpRectilinear { planes, center: [cx / ax, cy / ay] }) +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + use craft_tiff::{Entry, IfdBuilder, TiffWriter, Value}; + + const AREA: Rect = Rect { x: 0, y: 0, width: 6064, height: 4040 }; + const CROP: Rect = Rect { x: 8, y: 8, width: 6048, height: 4024 }; + + fn rat(v: f64, den: i32) -> [u8; 8] { + let mut b = [0u8; 8]; + b[..4].copy_from_slice(&((v * f64::from(den)).round() as i32).to_le_bytes()); + b[4..].copy_from_slice(&den.to_le_bytes()); + b + } + + /// DistortionInfo as the checked files lay it out (84 bytes, little-endian). + pub(crate) fn distortion_block(mode: u8, norm: f64, terms: u32, k: [f64; 3], zoom: f64) -> Vec { + let mut b = vec![0u8; 84]; + b[..4].copy_from_slice(b"0100"); + b[4] = mode; + b[5] = 1; + b[8..16].copy_from_slice(&rat(norm, 4096)); + b[16..20].copy_from_slice(&terms.to_le_bytes()); + for (i, v) in k.iter().enumerate() { + b[20 + 8 * i..28 + 8 * i].copy_from_slice(&rat(*v, 1 << 20)); + } + b[44..52].copy_from_slice(&rat(0.0, 1)); + b[52..60].copy_from_slice(&rat(0.0, 1)); + b[64..72].copy_from_slice(&rat(zoom, 1 << 20)); + b + } + + pub(crate) fn offset_block(x: f64, y: f64) -> Vec { + let mut b = b"0100".to_vec(); + b.extend_from_slice(&rat(x, 256)); + b.extend_from_slice(&rat(y, 256)); + b.extend_from_slice(&[0, 0, 0, 0]); + b + } + + /// A NikonNEFInfo (`0xc7d5`) blob with the given entries. + pub(crate) fn nef_info_blob(entries: &[(u16, Vec)]) -> Vec { + let mut info = IfdBuilder::new(); + for (tag, data) in entries { + info.set(*tag, Value::Undefined(data.clone())); + } + let tiff = TiffWriter::new(ByteOrder::Little, false).write(&[info]).unwrap(); + let mut blob = b"Nikon\0\x01\x03\0\0".to_vec(); + blob.extend_from_slice(&tiff); + blob + } + + /// A raw IFD carrying a NikonNEFInfo blob with the given entries. + pub(super) fn raw_with(entries: &[(u16, Vec)]) -> Ifd { + Ifd { entries: vec![Entry { tag: NEF_INFO, value: Value::Undefined(nef_info_blob(entries)), offset: 0 }], ..Default::default() } + } + + // a Z 24-70 f/4 S at 24 mm and at 70 mm, as stored in two of the checked files + const WIDE: ([f64; 3], f64) = ([0.039895, 0.019211, -0.127218], -0.025043); + const TELE: ([f64; 3], f64) = ([0.01681, -0.010601, 0.056758], 0.088301); + + fn warp(model: &str, raw: &Ifd) -> Option<([f64; 6], [f64; 2])> { + match distortion(model, raw, AREA, CROP)? { + Opcode::WarpRectilinear { planes, center } => Some((planes[0], center)), + _ => None, + } + } + + /// The source radius the warp gives for an output radius `rho` (in image-area half-diagonals) about the centre. + fn source_rho(p: [f64; 6], centre: [f64; 2], rho: f64) -> f64 { + let (ax, ay) = (AREA.width as f64 - 1.0, AREA.height as f64 - 1.0); + let (cx, cy) = (centre[0] * ax, centre[1] * ay); + let m = cx.max(ax - cx).hypot(cy.max(ay - cy)); + let hd = (CROP.width as f64 - 1.0).hypot(CROP.height as f64 - 1.0) / 2.0; + let t2 = (rho * hd / m).powi(2); + rho * (p[0] + t2 * (p[1] + t2 * (p[2] + t2 * p[3]))) + } + + #[test] + fn the_warp_is_the_stored_polynomial_over_the_zoom() { + for (k, zoom) in [WIDE, TELE] { + let raw = raw_with(&[(DISTORTION_INFO, distortion_block(3, 21.6, 4, k, zoom))]); + let (p, c) = warp("NIKON Z 6_2", &raw).unwrap(); + for rho in [0.0, 0.25, 0.5, 0.75, 1.0] { + let x = rho * rho; + let want = rho * (1.0 + k[2] * x + k[1] * x * x + k[0] * x * x * x) / (1.0 + zoom); + assert!((source_rho(p, c, rho) - want).abs() < 2e-6, "{k:?} at {rho}"); + } + // centred on the image area, which sits 8 px in from the sensor's top-left + assert!((c[0] - (8.0 + 6047.0 / 2.0) / 6063.0).abs() < 1e-12 && (c[1] - (8.0 + 4023.0 / 2.0) / 4039.0).abs() < 1e-12); + } + } + + #[test] + fn the_optical_centre_follows_the_sensor_shift() { + let raw = raw_with(&[(DISTORTION_INFO, distortion_block(3, 21.6, 4, TELE.0, TELE.1)), (CENTRE_OFFSET, offset_block(-90.1, 18.59))]); + let (_, c) = warp("NIKON Z 6_2", &raw).unwrap(); + assert!((c[0] * 6063.0 - (8.0 + 6047.0 / 2.0 - 90.1)).abs() < 0.01, "{c:?}"); + assert!((c[1] * 4039.0 - (8.0 + 4023.0 / 2.0 + 18.59)).abs() < 0.01, "{c:?}"); + } + + #[test] + fn refuses_unvalidated_bodies_and_unknown_layouts() { + let good = distortion_block(3, 21.6, 4, WIDE.0, WIDE.1); + assert!(warp("NIKON Z 6_2", &raw_with(&[(DISTORTION_INFO, good.clone())])).is_some()); + assert!(warp("NIKON Z f", &raw_with(&[(DISTORTION_INFO, good.clone())])).is_some()); + assert!(warp("NIKON Z 8", &raw_with(&[(DISTORTION_INFO, good.clone())])).is_some()); + for model in ["", "NIKON Z 6", "NIKON Z 6_3", "NIKON Z 9", "NIKON D850", "NIKON Z 6_2 x"] { + assert!(warp(model, &raw_with(&[(DISTORTION_INFO, good.clone())])).is_none(), "{model}"); + } + // correction off, a DX radius, another term count, a wild coefficient or zoom, no NEFInfo at all + for bad in [ + distortion_block(0, 21.6, 4, WIDE.0, WIDE.1), + distortion_block(3, 14.2, 4, WIDE.0, WIDE.1), + distortion_block(3, 21.6, 3, WIDE.0, WIDE.1), + distortion_block(3, 21.6, 4, [0.0, 0.0, 1.5], 0.0), + distortion_block(3, 21.6, 4, WIDE.0, 0.9), + b"0200".iter().copied().chain([0u8; 80]).collect(), + vec![0u8; 12], + ] { + assert!(warp("NIKON Z 6_2", &raw_with(&[(DISTORTION_INFO, bad)])).is_none()); + } + assert!(warp("NIKON Z 6_2", &Ifd::default()).is_none()); + // a map that folds (the source radius turns back) is refused + assert!(warp("NIKON Z 6_2", &raw_with(&[(DISTORTION_INFO, distortion_block(3, 21.6, 4, [-0.9, 0.0, 0.0], 0.0))])).is_none()); + } + + #[test] + fn only_the_full_3_2_image_area_is_corrected() { + let raw = raw_with(&[(DISTORTION_INFO, distortion_block(3, 21.6, 4, WIDE.0, WIDE.1))]); + // a DX or 1:1 crop of the sensor would need another normalisation + assert!(distortion("NIKON Z 6_2", &raw, AREA, Rect::new(1016, 676, 4032, 2688)).is_none(), "DX"); + assert!(distortion("NIKON Z 6_2", &raw, AREA, Rect::new(1016, 8, 4024, 4024)).is_none()); + assert!(distortion("NIKON Z 6_2", &raw, AREA, Rect::new(0, 0, 1, 1)).is_none()); + // a sensor-shift offset beyond 5 % of the frame is not trusted + let far = raw_with(&[(DISTORTION_INFO, distortion_block(3, 21.6, 4, WIDE.0, WIDE.1)), (CENTRE_OFFSET, offset_block(900.0, 0.0))]); + assert!(warp("NIKON Z 6_2", &far).is_none()); + } +} diff --git a/crates/raw/src/nefc.rs b/crates/raw/src/nefc.rs new file mode 100644 index 000000000..9904f7cca --- /dev/null +++ b/crates/raw/src/nefc.rs @@ -0,0 +1,794 @@ +//! Nikon Huffman-compressed NEF image data (TIFF compression 34713): "lossless compressed" and "lossy compressed" +//! (type 1 and type 2) at 12 and 14 bits. +//! +//! **Clean-room.** No raw-decoder source code (dcraw, LibRaw, rawspeed, rawloader/rawler, darktable, RawTherapee, +//! libopenraw, ExifTool's Perl code, …) was read or consulted. What we used: +//! +//! - Prose format descriptions: Laurent Clévy, "Nikon Electronic File (NEF) format" (), +//! the maker-note tag table and the "0x96 (linearization table) tag format" table (version bytes, four `u16` +//! predictor seeds at offset 2, curve size at 10, curve at 12, split value at 562 for lossy type 2, "lossy type +//! 2 reads an incomplete table and interpolation is required"); Bill Claff, "NEF Compression" +//! (): a lossy encoding curve followed by a +//! non-adaptive (fixed-table) Huffman stage, and the number of encoded values per camera (683, 769, 1025, 2753, +//! 3073, 4097 …), which confirmed our curve interpolation; the libopenraw format notes +//! (, prose only); ExifTool's Nikon tag-name documentation +//! (`0x0093` NEFCompression, `0x0096` NEFLinearizationTable); ITU-T T.81 Annex F/H for the "difference category +//! + additional bits" coding and canonical (BITS/HUFFVAL) Huffman codes. +//! - Our own black-box analysis of CC0 samples from raw.pixls.us. Nikon's Huffman tables are not stored in the +//! files and no prose source lists them, so they were recovered from the data: several cameras (D5100, D300, +//! D700) were shot with the same scene in compressed and uncompressed modes; a beam search assigned code words +//! to difference categories so that the decoded compressed image matched the uncompressed one (likelihood of +//! the decoded values given the reference, minus the bits consumed, plus a description-length penalty per new +//! code word). Each table was then verified on ~45 files from ~35 bodies (D40 … D850, Df, Z 6, Z 50, 1 J1): with +//! the right table the whole strip decodes and ends within the last byte of the data, with every value inside +//! the encoded range. The predictor and difference coding are the same as Pentax PEF (see `pef.rs`). +//! +//! Format, as established: +//! - One strip, MSB-first bit stream, no byte stuffing, continuous across rows. Per pixel a canonical Huffman code +//! gives the category `n`, followed by `n` additional bits (T.81: a leading 0 means a negative difference). +//! - Each pixel is predicted from the same-colour pixel two to the left; the first two pixels of a row from the +//! first two pixels of the previous row of the same parity, starting at the seeds of maker note `0x0096`. +//! - Lossless (`0x0096` version `0x46`): the decoded values are the samples. Lossy (version `0x44`): they index a +//! curve. Type 1 (`0x44 0x10`) stores the full curve; type 2 (`0x44 0x20`, `0x44 0x40`) stores 257 points, +//! one every `2^bits / 256` codes (`0x20`) or `2^bits / 1024` codes (`0x40`), linearly interpolated. The span +//! was established from the files: in every `0x20` file the first interval of the 257 points rises by +//! `2^bits / 256` (16 at 12 bit, 64 at 14 bit) and in every `0x40` file by `2^bits / 1024` (4 and 16), i.e. +//! the curve is the identity in the dark part, one code per sample. Read with a point every `2^bits / 256` +//! codes instead, the 12 `0x40` files that are not split decoded to values that never exceeded 255 (12 bit) or +//! 1023 (14 bit) and whose darkest 0.01 % sat at a quarter of the black level of maker note `0x003d` (63 against +//! 252 in 12-bit units); with the first-interval step they sit at that black level. +//! - Values in the maker note are in the maker note's byte order (newer bodies write little-endian notes). +//! +//! "Lossy after split" (non-zero split row at `0x0096` offset 562, type 2 tables only, found in 11 of the CC0 +//! samples): derived by black-box analysis of those files. Rows below the split row use the lossy Huffman path +//! above unchanged. Row `split` starts at the very next bit (no alignment); from there every pixel is a word of +//! `bits - 4` bits (8 at 12 bit, 10 at 14 bit), MSB first, with no row padding. A word is a complete prefix code +//! for a difference category `n` followed by the top `W - prefix length` bits of the T.81 additional bits (the low +//! bits are dropped); the difference is the centre of the magnitude bin that was kept. The predictor is the same +//! as above and the running value is not clamped (only the sample index is, before the curve lookup). The camera +//! ends the strip with 0 to 7 one-bits after the last full row, which the decoder checks. +//! +//! Not supported (returned as [`RawError::Unsupported`], the embedded preview is used instead): +//! - "Lossy after split" files whose fixed-rate section does not end within the last byte of the strip (see +//! below): refused rather than decoded wrongly. +//! - Other `0x0096` versions, bit depths other than 12/14. +//! - Code words that never occurred in any sample (the lossy 12-bit 8-bit code word `11111110`, and code words +//! longer than 9 bits in the lossy tables) are rejected as corrupt; they cannot code a difference within the +//! lossy value range anyway. + +use crate::huffman::{Bits, Huffman, diff}; +use crate::{MAX_SAMPLES, RawError, Result}; +use craft_tiff::ByteOrder; + +/// A table code word whose symbol was never observed (see the module docs). +const UNSEEN: u8 = u8::MAX; + +/// Canonical Huffman table: number of codes of length 1..=16 and the symbols (difference categories) in code order. +struct Table { + counts: &'static [u8], + symbols: &'static [u8], +} + +const LOSSLESS_12: Table = Table { counts: &[0, 1, 4, 2, 3, 1, 2], symbols: &[5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, 11, 12] }; +const LOSSLESS_14: Table = Table { counts: &[0, 1, 4, 2, 2, 3, 1, 2], symbols: &[7, 6, 8, 5, 9, 4, 10, 3, 11, 12, 2, 0, 1, 13, 14] }; +const LOSSY_12: Table = Table { counts: &[0, 1, 5, 1, 1, 1, 1, 1, 1], symbols: &[5, 4, 3, 6, 2, 7, 1, 0, 8, 9, UNSEEN, 10] }; +const LOSSY_14: Table = Table { counts: &[0, 1, 4, 3, 1, 1, 1, 1, 1], symbols: &[5, 6, 4, 7, 8, 3, 9, 2, 1, 0, 10, 11, 12] }; + +impl Table { + /// `(left-aligned 12-bit code, length)` per symbol, for [`Huffman::new`]. + fn codes(&self) -> Vec<(u16, u8)> { + let mut out = vec![(0u16, 0u8); 17]; + let mut code = 0u32; + let mut symbols = self.symbols.iter(); + for (i, &n) in self.counts.iter().enumerate() { + let len = i as u32 + 1; + for _ in 0..n { + if let Some(&s) = symbols.next() + && let Some(slot) = out.get_mut(s as usize) + { + *slot = ((code << (12 - len)) as u16, len as u8); + } + code += 1; + } + code <<= 1; + } + out + } +} + +/// How the decoded values map to samples. +#[derive(Clone, Debug, PartialEq)] +pub(crate) enum Encoding { + /// The decoded values are the samples (`0..2^bits`). + Lossless, + /// The decoded values index this curve. + Lossy(Vec), +} + +/// Maker note `0x0096` (NEF linearization table), parsed. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct DecodeTable { + pub encoding: Encoding, + /// Predictor seeds `[row parity][column]`. + pub seeds: [[i32; 2]; 2], + /// First row of the fixed-rate section ("lossy after split"), 0 when the whole strip is Huffman coded. + pub split: usize, +} + +/// Parse maker note `0x0096` for a `bits`-bit image. `order` is the maker note's byte order. +pub(crate) fn parse_table(t: &[u8], order: ByteOrder, bits: u32) -> Result { + let u16_at = |o: usize| t.get(o..o + 2).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(|b| order.u16(b)); + let short = || RawError::Corrupt(format!("NEF: linearization table too short ({} bytes)", t.len())); + if bits != 12 && bits != 14 { + return Err(RawError::Unsupported(format!("Nikon compressed NEF with {bits}-bit samples"))); + } + let (v0, v1) = (t.first().copied().ok_or_else(short)?, t.get(1).copied().ok_or_else(short)?); + let mut seeds = [[0i32; 2]; 2]; + for (i, s) in seeds.iter_mut().flatten().enumerate() { + *s = u16_at(2 + 2 * i).ok_or_else(short)? as i32; + } + let mut split = 0; + let encoding = match (v0, v1) { + (0x46, _) => Encoding::Lossless, + (0x44, 0x10 | 0x20 | 0x40) => { + let n = u16_at(10).ok_or_else(short)? as usize; + let points: Vec = (0..n).map(|i| u16_at(12 + 2 * i)).collect::>().ok_or_else(short)?; + if n < 2 { + return Err(RawError::Corrupt(format!("NEF: linearization curve of {n} points"))); + } + if v1 == 0x10 { + Encoding::Lossy(points) + } else { + split = u16_at(562).unwrap_or(0) as usize; + // type 2: `n` points covering `range` codes, one every `range / (n - 1)`; the curve's first interval + // rises by exactly that step (identity in the dark part) and the table version sets the span: + // `0x20` covers all 2^bits codes, `0x40` the first quarter (see the module docs) + let range = if v1 == 0x40 { 1usize << (bits - 2) } else { 1usize << bits }; + if n - 1 > range || !range.is_multiple_of(n - 1) { + return Err(RawError::Unsupported(format!("NEF: {n}-point lossy curve for {bits}-bit data"))); + } + let step = range / (n - 1); + let mut curve = Vec::with_capacity(range + 1); + for pair in points.windows(2) { + let &[a, b] = pair else { continue }; + let (a, b) = (a as u32, b as u32); + for k in 0..step as u32 { + let s = step as u32; + curve.push(((a * (s - k) + b * k) / s) as u16); + } + } + curve.extend(points.last().copied()); + Encoding::Lossy(curve) + } + } + _ => return Err(RawError::Unsupported(format!("Nikon compressed NEF version {v0:#04x} {v1:#04x}"))), + }; + Ok(DecodeTable { encoding, seeds, split }) +} + +/// Difference-category prefix codes of the fixed-rate section: `(prefix bits, prefix length, category n)`. +/// A word of `W` bits is a prefix followed by `W - length` bits of the T.81 additional bits of category `n`. +/// (The 14-bit `010` = category 12 never occurs in the samples; its category is deduced.) +const FIXED_12: &[(u32, u32, u32)] = &[ + (0b00, 2, 9), + (0b010, 3, 10), + (0b011, 3, 8), + (0b100, 3, 7), + (0b101, 3, 6), + (0b110, 3, 5), + (0b1110, 4, 4), + (0b11110, 5, 3), + (0b111110, 6, 2), + (0b1111110, 7, 1), + (0b11111110, 8, 0), +]; +const FIXED_14: &[(u32, u32, u32)] = &[ + (0b00, 2, 8), + (0b010, 3, 12), + (0b011, 3, 11), + (0b100, 3, 10), + (0b101, 3, 9), + (0b110, 3, 7), + (0b1110, 4, 6), + (0b11110, 5, 5), + (0b111110, 6, 4), + (0b1111110, 7, 3), + (0b11111110, 8, 2), + (0b111111110, 9, 1), + (0b1111111110, 10, 0), +]; + +/// Word (`bits - 4` bits) to dequantised difference for the fixed-rate section; `None` marks the unused all-ones word. +fn fixed_word_table(bits: u32) -> Vec> { + let (w, codes) = if bits == 12 { (8, FIXED_12) } else { (10, FIXED_14) }; + let mut lut = vec![None; 1usize << w]; + for &(prefix, len, n) in codes { + let kept = w - len; + for m in 0..1u32 << kept { + let e = n.saturating_sub(kept); + let ex = (m << e) as i32; + let half = (1i32 << e) >> 1; + let d = if n == 0 { + 0 + } else if ex >= 1 << (n - 1) { + ex + half + } else if e > 0 { + ex + half - (1 << n) + } else { + ex - ((1 << n) - 1) + }; + if let Some(slot) = lut.get_mut(((prefix << kept) | m) as usize) { + *slot = Some(d); + } + } + } + lut +} + +/// Decode a whole `w × h` Nikon Huffman-compressed strip `src` with `bits`-bit samples. +#[cfg(test)] +pub(crate) fn decode(src: &[u8], w: usize, h: usize, bits: u32, table: &DecodeTable) -> Result> { + decode_rows(src, w, h, h, bits, table) +} + +/// The first `rows` rows (`w × rows` samples) of a `w × h` strip. The stream is sequential, so a header-only probe +/// decodes just the rows its masked-column test looks at (storytold/lightcraft#708) instead of the whole image. The size and +/// truncation checks still cover the whole image; the "lossy after split" end-of-strip rule (and corruption +/// further down) can only be seen when the whole strip is decoded. +pub(crate) fn decode_rows(src: &[u8], w: usize, h: usize, rows: usize, bits: u32, table: &DecodeTable) -> Result> { + if bits != 12 && bits != 14 { + return Err(RawError::Unsupported(format!("Nikon compressed NEF with {bits}-bit samples"))); + } + let whole = w.checked_mul(h).filter(|&n| n > 0 && n <= MAX_SAMPLES).ok_or(RawError::Limit("NEF image size"))?; + // every code word is at least 2 bits long: a strip that can't hold the image is truncated (also bounds the + // allocation by the file size) + if whole / 4 > src.len() { + return Err(RawError::Corrupt(format!("NEF: {} bytes of compressed data for {w}x{h} pixels", src.len()))); + } + let rows = rows.min(h); + let n = w.saturating_mul(rows); + let huff = Huffman::new( + &match (&table.encoding, bits) { + (Encoding::Lossless, 12) => LOSSLESS_12, + (Encoding::Lossless, _) => LOSSLESS_14, + (Encoding::Lossy(_), 12) => LOSSY_12, + (Encoding::Lossy(_), _) => LOSSY_14, + } + .codes(), + )?; + let (lut, max): (&[u16], i32) = match &table.encoding { + Encoding::Lossless => (&[], (1i32 << bits) - 1), + Encoding::Lossy(curve) => (curve, curve.len() as i32 - 1), + }; + if table.split != 0 && table.split >= h { + return Err(RawError::Corrupt(format!("NEF: split row {} outside the {h} rows", table.split))); + } + let fixed = if table.split != 0 { fixed_word_table(bits) } else { Vec::new() }; + let word = bits - 4; + let mut out = vec![0u16; n]; + let mut stream = Bits::new(src); + let mut vpred = table.seeds; + let mut clipped = 0usize; + for (y, row) in out.chunks_exact_mut(w).enumerate() { + let seeds = vpred.get_mut(y & 1).ok_or(RawError::Limit("NEF row"))?; + let mut hpred = [0i32; 2]; + for (x, o) in row.iter_mut().enumerate() { + let d = if table.split != 0 && y >= table.split { + let code = stream.get(word) as usize; + fixed.get(code).copied().flatten().ok_or_else(|| RawError::Corrupt(format!("NEF: invalid fixed-rate word at row {y}")))? + } else { + diff(&mut stream, &huff).ok_or_else(|| RawError::Corrupt(format!("NEF: invalid Huffman code at row {y}")))? + }; + // a run of large differences in a corrupt stream can drive a predictor past i32 (storytold/lightcraft#1515) + let overflow = || RawError::Corrupt(format!("NEF: predictor out of range at row {y}")); + let p = if x < 2 { + let s = seeds.get_mut(x).ok_or(RawError::Limit("NEF column"))?; + *s = s.checked_add(d).ok_or_else(overflow)?; + hpred[x & 1] = *s; + *s + } else { + let p = hpred[x & 1].checked_add(d).ok_or_else(overflow)?; + hpred[x & 1] = p; + p + }; + let v = p.clamp(0, max); + clipped += (v != p && (table.split == 0 || y < table.split)) as usize; + *o = if lut.is_empty() { v as u16 } else { lut.get(v as usize).copied().unwrap_or(0) }; + } + // the camera pads the strip; reading more than a few bytes past its end means truncated or corrupt data + if stream.consumed_bits() > src.len() * 8 + 64 { + return Err(RawError::Corrupt(format!("NEF: compressed data ends at row {y} of {h}"))); + } + } + if table.split != 0 && rows == h { + // the fixed-rate section must fill the rest of the strip: whole rows of w words and 0..7 bits of padding. + // A strip may hold a few more rows than the image (one body stores 4022 rows for 4020); anything else means + // the file does not follow the rule, so refuse it instead of decoding it wrongly. + let left = (src.len() * 8).checked_sub(stream.consumed_bits()); + let row_bits = w * word as usize; + if !left.is_some_and(|l| l % row_bits < 8 && l / row_bits <= 4) { + return Err(RawError::Unsupported(format!( + "Nikon \"lossy after split\" NEF whose fixed-rate section (from row {}) does not end within the last byte", + table.split + ))); + } + } + // a wrong table / corrupt stream drifts out of range quickly; real files stay inside (a handful of edge pixels) + if clipped > n / 1000 + 16 { + return Err(RawError::Corrupt(format!("NEF: {clipped} decoded values out of range"))); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// MSB-first bit writer for the test encoder. + #[derive(Default)] + struct Writer { + out: Vec, + acc: u64, + n: u32, + } + + impl Writer { + fn put(&mut self, v: u32, len: u32) { + for i in (0..len).rev() { + self.acc = (self.acc << 1) | ((v >> i) & 1) as u64; + self.n += 1; + if self.n == 8 { + self.out.push(self.acc as u8); + self.acc = 0; + self.n = 0; + } + } + } + fn finish(mut self) -> Vec { + if self.n > 0 { + self.out.push((self.acc << (8 - self.n)) as u8); + } + self.out.extend_from_slice(&[0; 4]); + self.out + } + } + + fn category(d: i32) -> u32 { + 32 - d.unsigned_abs().leading_zeros() + } + + /// Encode `values` (decoded domain: samples or curve indices) the way the camera does. + fn encode(values: &[i32], w: usize, table: &Table, seeds: [[i32; 2]; 2]) -> Vec { + let codes = table.codes(); + let mut wr = Writer::default(); + let mut vpred = seeds; + for (y, row) in values.chunks(w).enumerate() { + for (x, &v) in row.iter().enumerate() { + let pred = if x < 2 { vpred[y & 1][x] } else { row[x - 2] }; + if x < 2 { + vpred[y & 1][x] = v; + } + let d = v - pred; + let k = category(d); + let (code, len) = codes[k as usize]; + assert!(len > 0, "no code for category {k}"); + wr.put(code as u32 >> (12 - len), len as u32); + let extra = if d < 0 { d + (1 << k) - 1 } else { d }; + wr.put(extra as u32, k); + } + } + wr.finish() + } + + /// Deterministic test image: smooth gradient + texture + hard edges, within `0..=max`. + fn image(w: usize, h: usize, max: i32, seed: u32) -> Vec { + let mut s = seed; + (0..w * h) + .map(|i| { + let (x, y) = ((i % w) as i32, (i / w) as i32); + s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let noise = (s >> 24) as i32 % 9 - 4; + let base = (x * 37 + y * 11) % (max / 2) + if (x / 7 + y / 5) % 3 == 0 { max / 3 } else { 0 }; + (base + noise).clamp(0, max) + }) + .collect() + } + + fn lossless(bits: u32) -> DecodeTable { + let s = 1 << (bits - 3); + DecodeTable { encoding: Encoding::Lossless, seeds: [[s, s], [s, s]], split: 0 } + } + + #[test] + fn tables_are_complete_prefix_codes() { + for t in [LOSSLESS_12, LOSSLESS_14, LOSSY_12, LOSSY_14] { + assert_eq!(t.counts.iter().map(|&c| c as usize).sum::(), t.symbols.len()); + let kraft: f64 = t.counts.iter().enumerate().map(|(i, &c)| c as f64 / (1u64 << (i + 1)) as f64).sum(); + assert!(kraft <= 1.0); + assert!(Huffman::new(&t.codes()).is_ok()); + } + assert_eq!(LOSSLESS_14.codes()[7], (0b00 << 10, 2)); + assert_eq!(LOSSLESS_14.codes()[0], (0b111110 << 6, 6)); + assert_eq!(LOSSY_12.codes()[10], (0b111111110 << 3, 9)); + } + + #[test] + fn lossless_roundtrip_12_and_14() { + for (bits, table) in [(12, LOSSLESS_12), (14, LOSSLESS_14)] { + let (w, h) = (64, 9); + let max = (1 << bits) - 1; + let mut img = image(w, h, max, bits); + // extreme jumps exercise the longest categories + img[5] = 0; + img[7] = max; + img[w + 9] = max; + let t = lossless(bits); + let src = encode(&img, w, &table, t.seeds); + let out = decode(&src, w, h, bits, &t).unwrap(); + assert_eq!(out, img.iter().map(|&v| v as u16).collect::>(), "{bits}-bit"); + } + } + + fn lossy_table(bits: u32, order: ByteOrder) -> Vec { + // version 0x44 0x20, seeds, 257 points of a concave curve, padding up to the split value (0) + let mut t = vec![0x44, 0x20]; + let put = |t: &mut Vec, v: u16| t.extend_from_slice(&if order == ByteOrder::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + for _ in 0..4 { + put(&mut t, 300); + } + put(&mut t, 257); + let max = ((1u32 << bits) - 1) as f64; + for i in 0..257u32 { + put(&mut t, (max * (i as f64 / 64.0).min(1.0).sqrt()) as u16); + } + t.resize(624, 0); + t + } + + #[test] + fn lossy_type2_curve_and_roundtrip() { + for (bits, table) in [(12u32, LOSSY_12), (14, LOSSY_14)] { + for order in [ByteOrder::Big, ByteOrder::Little] { + let t = parse_table(&lossy_table(bits, order), order, bits).unwrap(); + assert_eq!(t.seeds, [[300, 300], [300, 300]]); + let Encoding::Lossy(curve) = &t.encoding else { panic!() }; + let step = (1usize << bits) / 256; + assert_eq!(curve.len(), (1 << bits) + 1); + assert_eq!(curve[64 * step], (1 << bits) - 1); + assert!(curve.windows(2).all(|p| p[0] <= p[1])); + // the encoded range is the curve's rising part + let (w, h) = (48, 7); + let img = image(w, h, 64 * step as i32 - 1, 7); + let src = encode(&img, w, &table, t.seeds); + let out = decode(&src, w, h, bits, &t).unwrap(); + assert_eq!(out, img.iter().map(|&v| curve[v as usize]).collect::>()); + } + } + } + + /// Maker note `0x0096` as bytes, written out by hand: version `0x44 `, four seeds, 257 points (little-endian + /// as newer bodies write it) of a curve whose first 256 points rise by `step` and whose last is the white + /// level, and a zero split value at offset 562. + fn hand_table(v1: u8, seed: u16, step: u16, white: u16) -> Vec { + let mut t = vec![0x44, v1]; + for _ in 0..4 { + t.extend_from_slice(&seed.to_le_bytes()); + } + t.extend_from_slice(&257u16.to_le_bytes()); + for k in 0..256u16 { + t.extend_from_slice(&(k * step).to_le_bytes()); + } + t.extend_from_slice(&white.to_le_bytes()); + t.resize(624, 0); + t + } + + /// Known answers for the `0x44 0x40` curve, from bytes and bit strings written by hand (nothing here uses the + /// decoder's span rule or the test encoder): the first interval of the points rises by `2^bits / 1024`, so + /// the curve is the identity for one code per sample and spans `2^bits / 4` codes. The `0x20` curve of the + /// same sizes keeps its span of all `2^bits` codes. + #[test] + fn lossy_type2_version_0x40_spans_a_quarter_of_the_range() { + // 12 bit: points 0, 4, 8 … 1020, 4095 + let t = parse_table(&hand_table(0x40, 252, 4, 4095), ByteOrder::Little, 12).unwrap(); + let Encoding::Lossy(curve) = &t.encoding else { panic!() }; + assert_eq!(curve.len(), 1025); + assert_eq!(curve[0..6], [0, 1, 2, 3, 4, 5]); + assert_eq!((curve[252], curve[255], curve[1020]), (252, 255, 1020)); + assert_eq!((curve[1021], curve[1022], curve[1023], curve[1024]), (1788, 2557, 3326, 4095)); + // the pixels p0 = 252 + 4, p1 = 252, p2 = p0 - 1, p3 = p1, coded as 011 100 | 11110 | 1110 0 | 11110 + // (LOSSY_12: category 3 = 011, 0 = 11110, 1 = 1110; extra bits 100 = +4, 0 = -1) + let out = decode(&[0x73, 0xdc, 0xf0], 4, 1, 12, &t).unwrap(); + assert_eq!(out, [256, 252, 255, 252]); + // 14 bit: points 0, 16, 32 … 4080, 16383 + let t = parse_table(&hand_table(0x40, 1008, 16, 16383), ByteOrder::Little, 14).unwrap(); + let Encoding::Lossy(curve) = &t.encoding else { panic!() }; + assert_eq!(curve.len(), 4097); + assert_eq!(curve[0..6], [0, 1, 2, 3, 4, 5]); + assert_eq!((curve[1008], curve[4080], curve[4096]), (1008, 4080, 16383)); + // the same pixels coded as 1100 100 | 111110 | 11110 0 | 111110 + // (LOSSY_14: category 3 = 1100, 0 = 111110, 1 = 11110) + let out = decode(&[0xc9, 0xf7, 0x9f, 0x00], 4, 1, 14, &t).unwrap(); + assert_eq!(out, [1012, 1008, 1011, 1008]); + // version 0x20 with the same points is a curve over all 2^bits codes: the points are 16 codes apart + let t = parse_table(&hand_table(0x20, 252, 16, 4095), ByteOrder::Little, 12).unwrap(); + let Encoding::Lossy(curve) = &t.encoding else { panic!() }; + assert_eq!((curve.len(), curve[16], curve[17], curve[4095]), (4097, 16, 17, 4094)); + // 200 points can't be spaced evenly over the 1024 codes of a 12-bit 0x40 table + let mut odd = hand_table(0x40, 252, 4, 4095); + odd[10..12].copy_from_slice(&200u16.to_le_bytes()); + assert!(parse_table(&odd, ByteOrder::Little, 12).is_err()); + } + + /// Pack a string of `0`/`1` (spaces ignored) MSB first, padding the last byte with one-bits like the camera. + fn pack(bits: &str) -> Vec { + let b: Vec = bits.chars().filter(|c| !c.is_whitespace()).map(|c| c == '1').collect(); + b.chunks(8).map(|c| (0..8).fold(0u8, |a, i| (a << 1) | c.get(i).copied().unwrap_or(true) as u8)).collect() + } + + fn split_table(v1: u8, seed: u16, step: u16, white: u16, split: u16) -> DecodeTable { + let mut t = hand_table(v1, seed, step, white); + t[562..564].copy_from_slice(&split.to_le_bytes()); + parse_table(&t, ByteOrder::Little, if step == 4 { 12 } else { 14 }).unwrap() + } + + /// 12-bit "lossy after split": row 0 Huffman coded, rows 1 and 2 as 8-bit words that follow the last Huffman + /// bit directly. Bit strings written by hand from the rule (prefix, then the kept top bits of the extra bits). + const SPLIT_12: &str = "011 100 11110 1110 0 11110 11111110 11110100 11100111 01110000 00000001 11111110 01111111 11111110"; + + #[test] + fn lossy_after_split_known_answers_12_bit() { + let t = split_table(0x40, 252, 4, 4095, 1); + assert_eq!(t.split, 1); + let out = decode(&pack(SPLIT_12), 4, 3, 12, &t).unwrap(); + // row 1: d = 0 (n0), +4 (n3, exact), -8 (n4, exact), +132 (n8 keeps 5 bits: 16 << 3 plus half a bin, 4); + // row 2: -500 (n9 keeps 6 bits: bin 1 of the negative side, centre -508 + 8), then +252 on the running + // value -244 which is not clamped (8, not 252) + assert_eq!(out, [256, 252, 255, 252, 252, 256, 244, 388, 0, 252, 8, 252]); + } + + #[test] + fn lossy_after_split_known_answers_14_bit() { + let t = split_table(0x40, 1008, 16, 16383, 1); + // row 0 as in the 0x40 test above; row 1 in 10-bit words: 0 (n0), +32 (n6, exact), -254 (n8, exact), + // +510 (n9 keeps 7 bits: 127 << 2 plus 2) + let bits = "1100 100 111110 11110 0 111110 1111111110 1110100000 0000000001 1011111111"; + let out = decode(&pack(bits), 4, 2, 14, &t); + // word 3 is `00` + 8 bits: 00 00000001 -> n8 extra 00000001 = -254 + assert_eq!(out.unwrap(), [1012, 1008, 1011, 1008, 1008, 1040, 754, 1550]); + } + + #[test] + fn lossy_after_split_dequantisation_is_symmetric() { + let lut = fixed_word_table(12); + assert_eq!((lut[0b00_000000], lut[0b00_111111]), (Some(-508), Some(508))); + assert_eq!((lut[0b011_00000], lut[0b011_11111]), (Some(-252), Some(252))); + assert_eq!(lut[0b11111110], Some(0)); + assert_eq!(lut[0xff], None); + let lut = fixed_word_table(14); + assert_eq!((lut[0b00_00000000], lut[0b00_11111111]), (Some(-255), Some(255))); + assert_eq!(lut[0b101_0000000], Some(-510)); + assert_eq!(lut[0b1111111110], Some(0)); + assert_eq!(lut[0x3ff], None); + for bits in [12, 14] { + let lut = fixed_word_table(bits); + assert_eq!(lut.iter().filter(|d| d.is_none()).count(), 1, "{bits}-bit: only the all-ones word is unused"); + } + } + + #[test] + fn lossy_after_split_refuses_strips_that_do_not_follow_the_rule() { + let t = split_table(0x40, 252, 4, 4095, 1); + let mut longer = pack(SPLIT_12); + longer.extend_from_slice(&[0xff; 3]); + assert!(matches!(decode(&longer, 4, 3, 12, &t), Err(RawError::Unsupported(_)))); + let shorter = &pack(SPLIT_12)[..10]; + assert!(decode(shorter, 4, 3, 12, &t).is_err()); + // the unused all-ones word + let bad = pack("011 100 11110 1110 0 11110 11111111 11111110 11111110 11111110"); + assert!(matches!(decode(&bad, 4, 2, 12, &t), Err(RawError::Corrupt(_)))); + // a split row beyond the image + assert!(matches!(decode(&pack(SPLIT_12), 4, 1, 12, &t), Err(RawError::Corrupt(_)))); + // through the container + let mut table = hand_table(0x40, 252, 4, 4095); + table[562..564].copy_from_slice(&1u16.to_le_bytes()); + let ok = crate::decode(&nef_file(pack(SPLIT_12), 4, 3, 12, table.clone(), ByteOrder::Little)).unwrap(); + assert_eq!(ok.data, crate::RawData::U16(vec![256, 252, 255, 252, 252, 256, 244, 388, 0, 252, 8, 252])); + let e = crate::decode(&nef_file(longer, 4, 3, 12, table, ByteOrder::Little)); + assert!(matches!(e, Err(RawError::Unsupported(_))), "{e:?}"); + } + + #[test] + fn lossy_type1_full_curve() { + let mut t = vec![0x44, 0x10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 5]; + for v in [0u16, 100, 400, 900, 1600] { + t.extend_from_slice(&v.to_be_bytes()); + } + let parsed = parse_table(&t, ByteOrder::Big, 12).unwrap(); + assert_eq!(parsed.encoding, Encoding::Lossy(vec![0, 100, 400, 900, 1600])); + let img = vec![0, 4, 1, 3, 2, 2, 4, 0]; + let src = encode(&img, 4, &LOSSY_12, parsed.seeds); + assert_eq!(decode(&src, 4, 2, 12, &parsed).unwrap(), vec![0, 1600, 100, 900, 400, 400, 1600, 0]); + } + + #[test] + fn rejects_unsupported_and_malformed_tables() { + let unsupported = |r: Result| matches!(r, Err(RawError::Unsupported(_))); + let corrupt = |r: Result| matches!(r, Err(RawError::Corrupt(_))); + let mut split = lossy_table(12, ByteOrder::Big); + split[562..564].copy_from_slice(&345u16.to_be_bytes()); + assert_eq!(parse_table(&split, ByteOrder::Big, 12).unwrap().split, 345); + assert!(unsupported(parse_table(&[0x49, 0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ByteOrder::Big, 12))); + assert!(unsupported(parse_table(&lossy_table(12, ByteOrder::Big), ByteOrder::Big, 16))); + assert!(corrupt(parse_table(&[], ByteOrder::Big, 12))); + assert!(corrupt(parse_table(&[0x46, 0x30, 8], ByteOrder::Big, 14))); + assert!(corrupt(parse_table(&lossy_table(12, ByteOrder::Big)[..100], ByteOrder::Big, 12))); + // 300 points don't divide the 12-bit range + let mut odd = lossy_table(12, ByteOrder::Big); + odd[10..12].copy_from_slice(&300u16.to_be_bytes()); + assert!(parse_table(&odd, ByteOrder::Big, 12).is_err()); + assert!(parse_table(&[0x44, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], ByteOrder::Big, 12).is_err()); + } + + /// A minimal NEF: CFA SubIFD with one compressed strip, Exif maker note (`Nikon\0` v2 header + embedded TIFF in + /// `mn_order`) holding the linearization table. + fn nef_file(strip: Vec, w: u32, h: u32, bits: u16, table: Vec, mn_order: ByteOrder) -> Vec { + use craft_tiff::tags::{self as t, photometric}; + use craft_tiff::{IfdBuilder, ImageData, TiffWriter, Value}; + let mut mn = IfdBuilder::new(); + mn.set(0x0096, Value::Undefined(table)); + let mut note = b"Nikon\0\x02\x10\0\0".to_vec(); + note.extend(TiffWriter::new(mn_order, false).write(&[mn]).unwrap()); + let mut exif = IfdBuilder::new(); + exif.set(t::MAKER_NOTE, Value::Undefined(note)); + let mut raw = IfdBuilder::new(); + raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0])); + raw.set(t::IMAGE_WIDTH, Value::Long(vec![w])); + raw.set(t::IMAGE_LENGTH, Value::Long(vec![h])); + raw.set(t::BITS_PER_SAMPLE, Value::Short(vec![bits])); + raw.set(t::COMPRESSION, Value::Short(vec![t::compression::NIKON])); + raw.set(t::PHOTOMETRIC, Value::Short(vec![photometric::CFA])); + raw.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2, 2])); + raw.set(t::CFA_PATTERN_EP, Value::Byte(vec![0, 1, 1, 2])); + raw.set_image(ImageData::Strips { rows_per_strip: h, strips: vec![strip] }); + let mut ifd0 = IfdBuilder::new(); + ifd0.set(t::MAKE, Value::Ascii("NIKON CORPORATION".into())); + ifd0.set(t::MODEL, Value::Ascii("NIKON TEST".into())); + ifd0.set_child(t::EXIF_IFD, exif); + ifd0.add_sub_ifd(raw); + TiffWriter::new(ByteOrder::Big, false).write(&[ifd0]).unwrap() + } + + #[test] + fn decodes_through_the_nef_container() { + // lossless 14-bit, big-endian maker note + let (w, h) = (40usize, 6usize); + let img = image(w, h, 16383, 11); + let mut table = vec![0x46, 0x30]; + for _ in 0..4 { + table.extend_from_slice(&2048u16.to_be_bytes()); + } + table.resize(46, 0); + let src = encode(&img, w, &LOSSLESS_14, [[2048; 2]; 2]); + let bytes = nef_file(src, w as u32, h as u32, 14, table, ByteOrder::Big); + let r = crate::decode(&bytes).unwrap(); + assert_eq!(r.data, crate::RawData::U16(img.iter().map(|&v| v as u16).collect())); + assert_eq!(r.cfa.as_ref().unwrap().name(), "RGGB"); + assert_eq!(crate::probe_info(&bytes).unwrap(), r.info()); + // lossy 12-bit type 2, little-endian maker note (newer bodies) + let table = lossy_table(12, ByteOrder::Little); + let t = parse_table(&table, ByteOrder::Little, 12).unwrap(); + let Encoding::Lossy(curve) = &t.encoding else { panic!() }; + let img = image(w, h, 1023, 5); + let src = encode(&img, w, &LOSSY_12, t.seeds); + let r = crate::decode(&nef_file(src.clone(), w as u32, h as u32, 12, table.clone(), ByteOrder::Little)).unwrap(); + assert_eq!(r.data, crate::RawData::U16(img.iter().map(|&v| curve[v as usize]).collect())); + // a split file whose strip does not follow the fixed-rate rule is refused, never decoded wrongly + let mut split = table; + split[562..564].copy_from_slice(&3u16.to_le_bytes()); + let e = crate::decode(&nef_file(src, w as u32, h as u32, 12, split, ByteOrder::Little)); + assert!(e.is_err(), "{e:?}"); + } + + /// storytold/lightcraft#708: the header probe of a compressed NEF decodes only the first [`crate::nef::MASK_ROWS`] + /// rows (the ones the masked-column test reads) instead of the whole Huffman stream, and still finds the masked + /// columns the full decode finds. A strip cut off after those rows proves it: the probe succeeds with the same + /// description while the full decode reports the truncation. + #[test] + fn header_probe_stops_after_the_masked_column_rows() { + use crate::nef::MASK_ROWS; + let (w, h) = (160usize, MASK_ROWS * 3); + let mut img = image(w, h, 16383, 7); + // eight optically masked columns at 0, as the D5100 / D7000 write them + for row in img.chunks_mut(w) { + row[w - 8..].fill(0); + } + let t = lossless(14); + let table = { + let mut v = vec![0x46, 0x30]; + for s in t.seeds.iter().flatten() { + v.extend_from_slice(&(*s as u16).to_be_bytes()); + } + v.resize(46, 0); + v + }; + let src = encode(&img, w, &LOSSLESS_14, t.seeds); + let whole = nef_file(src.clone(), w as u32, h as u32, 14, table.clone(), ByteOrder::Big); + let full = crate::decode(&whole).unwrap(); + assert_eq!(full.active_area, crate::Rect::new(0, 0, w - 8, h), "the masked columns are found in a compressed file"); + assert_eq!(crate::probe_info(&whole).unwrap(), full.info()); + // the same stream cut off after two thirds of the rows (the first rows' codes are a prefix of the whole) + let cut = encode(&img[..w * (h * 2 / 3)], w, &LOSSLESS_14, t.seeds).len(); + assert!(cut > w * h / 4, "enough bytes to pass the size check"); + let truncated = nef_file(src[..cut].to_vec(), w as u32, h as u32, 14, table, ByteOrder::Big); + assert!(matches!(crate::decode(&truncated), Err(RawError::Corrupt(_))), "the full decode sees the truncation"); + assert_eq!(crate::probe_info(&truncated).unwrap(), full.info(), "the header probe never reads that far"); + // decode_rows itself: a prefix of the whole decode, and the whole image's size check still applies + let top = decode_rows(&src, w, h, MASK_ROWS, 14, &t).unwrap(); + assert_eq!(top.len(), w * MASK_ROWS); + assert_eq!(top[..], decode(&src, w, h, 14, &t).unwrap()[..w * MASK_ROWS]); + assert_eq!(decode_rows(&src, w, h, h + 5, 14, &t).unwrap().len(), w * h, "rows are clamped to the height"); + assert!(decode_rows(&src[..w * h / 4 - 1], w, h, 1, 14, &t).is_err(), "a strip too short for the image is corrupt in any mode"); + } + + /// A long run of the largest difference drove a predictor past i32: a panic in builds with overflow checks, + /// wrapped values otherwise (storytold/lightcraft#1515). Both the row and the column predictors now stop with an error. + #[test] + fn predictor_overflow_is_an_error_not_a_panic() { + let codes = LOSSLESS_14.codes(); + let k = (0..codes.len()).rev().find(|&k| codes[k].1 > 0).unwrap(); + let d = (1u32 << k) - 1; + let stream = |n: usize| { + let mut wr = Writer::default(); + let (code, len) = codes[k]; + for _ in 0..n { + wr.put(code as u32 >> (12 - len), len as u32); + wr.put(d, k as u32); + } + wr.finish() + }; + // differences one predictor needs to pass i32::MAX + let steps = (i32::MAX as u32 / d) as usize + 2; + let table = DecodeTable { encoding: Encoding::Lossless, seeds: [[0; 2]; 2], split: 0 }; + // along a row: every other sample adds to the same predictor + let w = 2 * steps + 2; + let err = decode(&stream(w), w, 1, 14, &table).unwrap_err(); + assert!(matches!(err, RawError::Corrupt(ref m) if m.contains("predictor")), "{err}"); + // down two columns: each row adds to the seed of its parity + let h = 2 * steps + 2; + let err = decode(&stream(2 * h), 2, h, 14, &table).unwrap_err(); + assert!(matches!(err, RawError::Corrupt(ref m) if m.contains("predictor")), "{err}"); + } + + #[test] + fn truncated_and_corrupted_streams_error_never_panic() { + let (w, h) = (64, 16); + let t = lossless(14); + let img = image(w, h, 16383, 3); + let src = encode(&img, w, &LOSSLESS_14, t.seeds); + // truncation: too short for the image, or ends early + for cut in [0, 1, 10, src.len() / 2, src.len() - 40] { + assert!(decode(&src[..cut], w, h, 14, &t).is_err(), "cut {cut}"); + } + // garbage and bit flips: an error or (rarely) some image, never a panic + let mut s = 12345u32; + for i in 0..200 { + let mut bad = src.clone(); + for _ in 0..1 + i % 8 { + s = s.wrapping_mul(1_103_515_245).wrapping_add(12345); + let at = (s >> 8) as usize % bad.len(); + bad[at] ^= 1 << (s % 8); + } + let _ = decode(&bad, w, h, 14, &t); + let noise: Vec = (0..src.len()).map(|k| ((k as u32).wrapping_mul(2_654_435_761) >> (i % 24)) as u8).collect(); + let _ = decode(&noise, w, h, 14, &t); + let _ = decode(&noise, w, h, 12, &lossless(12)); + } + // all-ones never forms a valid lossy 12-bit code + assert!(decode(&[0xff; 64], 8, 8, 12, &parse_table(&lossy_table(12, ByteOrder::Big), ByteOrder::Big, 12).unwrap()).is_err()); + // absurd dimensions are limited before allocating + assert!(decode(&src, usize::MAX, 2, 14, &t).is_err()); + assert!(decode(&src, 1 << 20, 1 << 20, 14, &t).is_err()); + assert!(decode(&src, 0, 2, 14, &t).is_err()); + } +} diff --git a/crates/raw/tests/it/vendor_robust.rs b/crates/raw/tests/it/vendor_robust.rs index 68eccf554..65fb139fb 100644 --- a/crates/raw/tests/it/vendor_robust.rs +++ b/crates/raw/tests/it/vendor_robust.rs @@ -61,6 +61,8 @@ fn cr2() -> Vec { fn samples() -> Vec> { vec![ cr2(), + cfa_tiff("NIKON CORPORATION", 1, 12, ByteOrder::Big), + cfa_tiff("NIKON CORPORATION", 34713, 14, ByteOrder::Big), pef(1), pef(65535), cfa_tiff("SAMSUNG", 1, 12, ByteOrder::Little), From ed16993b3b24dff15105c5230be928e4c03bb3d5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:30:23 -0500 Subject: [PATCH 055/120] Add craft-ical: iCalendar lines, values, rules and VTIMEZONE from MailCraft (#966) MailCraft crates/ical at a44a450: content lines, DATE/DATE-TIME values, durations and text, the common subset of recurrence rules, and VTIMEZONE definitions read and written, with their tests. Times are chrono values; the zone database the writer needs is the caller's, through ZoneRules. The event and to-do mapping onto MailCraft's model stays in MailCraft. Co-authored-by: Martin Ball <98639962+benandez@users.noreply.github.com> --- Cargo.lock | 8 + Cargo.toml | 1 + README.md | 4 + crates/ical/Cargo.toml | 17 + crates/ical/README.md | 56 +++ crates/ical/proptest-regressions/lines.txt | 7 + crates/ical/src/lib.rs | 33 ++ crates/ical/src/lines.rs | 321 +++++++++++++ crates/ical/src/rrule.rs | 516 +++++++++++++++++++++ crates/ical/src/values.rs | 419 +++++++++++++++++ crates/ical/src/zones.rs | 498 ++++++++++++++++++++ crates/ical/src/zones_tests.rs | 293 ++++++++++++ 12 files changed, 2173 insertions(+) create mode 100644 crates/ical/Cargo.toml create mode 100644 crates/ical/README.md create mode 100644 crates/ical/proptest-regressions/lines.txt create mode 100644 crates/ical/src/lib.rs create mode 100644 crates/ical/src/lines.rs create mode 100644 crates/ical/src/rrule.rs create mode 100644 crates/ical/src/values.rs create mode 100644 crates/ical/src/zones.rs create mode 100644 crates/ical/src/zones_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 0b52a4503..857ed807c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1180,6 +1180,14 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-ical" +version = "0.1.0" +dependencies = [ + "chrono", + "proptest", +] + [[package]] name = "craft-idml" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 3f361200d..d7a54cda3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ craft-fig = { path = "crates/fig" } craft-fonts-build = { path = "crates/fonts-build" } craft-i18n = { path = "crates/i18n" } craft-idml = { path = "crates/idml" } +craft-ical = { path = "crates/ical" } craft-jpeg = { path = "crates/jpeg" } craft-kiwi = { path = "crates/kiwi" } craft-keymap = { path = "crates/keymap" } diff --git a/README.md b/README.md index 951fc1ab9..678bf7f1c 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,10 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = "` form on craft-xml elements, value codecs and the package writer; and InDesign Tagged Text, read and written. +- [`craft-ical`](crates/ical/README.md): iCalendar (RFC 5545) building blocks, from MailCraft: content + lines, DATE/DATE-TIME values, durations, TEXT escaping, the common subset of recurrence rules, + and `VTIMEZONE` definitions read and written. No time-zone database; what events and to-dos + mean stays in each app. Bounded and panic-free on hostile input. - [`craft-stream-codecs`](crates/stream-codecs/README.md): byte-stream and bi-level image codecs that PDF, PostScript and TIFF share: LZW, ASCII85, ASCIIHex, RunLength, TIFF and PNG predictors, CCITT Group 3 and 4 decoding and Group 4 encoding, lossless JBIG2 encoding. Every diff --git a/crates/ical/Cargo.toml b/crates/ical/Cargo.toml new file mode 100644 index 000000000..b533c1386 --- /dev/null +++ b/crates/ical/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "craft-ical" +description = "iCalendar (RFC 5545) building blocks: content lines, values, recurrence rules and VTIMEZONE definitions, read and written." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +chrono = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/ical/README.md b/crates/ical/README.md new file mode 100644 index 000000000..1781ffc3b --- /dev/null +++ b/crates/ical/README.md @@ -0,0 +1,56 @@ +# craft-ical + +iCalendar (RFC 5545) building blocks, read and written. Times are `chrono` values; instants are +`i64` seconds since 1970-01-01T00:00:00Z. + +- **`lines`**: `Lines` unfolds a text (CRLF, LF or CR; a byte order mark is skipped); + `ContentLine::parse` splits `NAME;PARAM="quoted",x:value` into an upper-cased name, + parameters (RFC 6868 caret escapes undone) and the value as written; `fold` writes a line in + 75-octet pieces without splitting a character; `param_value` quotes and escapes a parameter. +- **`values`**: `When` (`DATE`, floating or zoned `DATE-TIME`, UTC `DATE-TIME`, with the ISO 8601 + punctuation some writers add), `parse_duration`, `escape_text` / `unescape_text`, `split_list`, + `format_date` / `format_local` / `format_utc`, and the wall-clock helpers they rest on. +- **`rrule`**: `Recurrence`, the subset of RRULE calendars show and edit (daily, weekly, monthly, + yearly; interval; weekdays; the nth or last weekday; count; until), `parse_rrule` and + `format_rrule`, and `format_for` / `format_zoned`, which spell out the start's weekday and + month and give `UNTIL` the start's value type. A rule outside the subset is read as the nearest + it holds. +- **`zones`**: `TimeZones::collect` reads a text's `VTIMEZONE`s (yearly rules by weekday or day + of the month, `UNTIL`, `COUNT`, `RDATE`s); a `VTimezone` answers `offset_at(instant)`, + `offset_for_wall(wall)` and `instant(wall)`, with RFC 5545's reading of skipped and repeated + hours. `write_vtimezone` writes a zone from the caller's `ZoneRules` (offset at an instant, + transitions): one `DAYLIGHT` and one `STANDARD` part with yearly rules where the zone keeps one + rule, else change by change. + +No time-zone database is here: matching a `TZID` to an IANA or Windows zone, and which zone an +event is shown in, are the caller's. So is what events and to-dos mean: an app reads them from +`ContentLine`s with these values and maps them onto its own model. + +## Hostile input + +Nothing panics on any text. + +| Limit | Value | +|---|---| +| unfolded line (`lines::MAX_LINE`) | 1 MiB | +| parameters on a line, values of a parameter | 64 each | +| recurrence rule | 4096 bytes, 64 parts | +| durations | about 10,000 years | +| zones per text (`zones::MAX_ZONES`) | 64 | +| parts per zone, dates per part | 64 each | +| `TZID` kept (`zones::MAX_TZID`) | 128 bytes | +| offsets | at most 18 hours | +| work on zone definitions per text (`zones::WORK_BUDGET`) | 20,000,000 units, spent by the caller through `TimeZones::spend` | + +Every date and time written is in the years 1 to 9999; a value outside them is moved to the +nearest end. + +## Origin + +MailCraft `crates/ical` (`lines.rs`, `values.rs`, `rrule.rs`, `zones.rs`) at +[a44a450](https://github.com/UCS-Care-Group/mailcraft/commit/a44a45013d1c6396229c40ab60172d1882b368d5), +by Martin Ball and the MailCraft contributors (MIT OR Apache-2.0), with their tests. MailCraft's +own date types are replaced by `chrono`, its time-zone database by the `ZoneRules` trait (the +writer's tests use rule zones built in the test), and its recurrence model by `Recurrence`. +MailCraft's event and to-do reader and writer (`read.rs`, `write.rs`), and the matching of a +`TZID` against its zone database, stay in MailCraft as the mapping onto its model. diff --git a/crates/ical/proptest-regressions/lines.txt b/crates/ical/proptest-regressions/lines.txt new file mode 100644 index 000000000..bd55905b3 --- /dev/null +++ b/crates/ical/proptest-regressions/lines.txt @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 960cb45b7c8511f655aea177ca4d647864fc5cf713e5fbdd611ddba6ed077047 # shrinks to line = "\u{feff}\u{feff}" diff --git a/crates/ical/src/lib.rs b/crates/ical/src/lib.rs new file mode 100644 index 000000000..be77be9e7 --- /dev/null +++ b/crates/ical/src/lib.rs @@ -0,0 +1,33 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! iCalendar (RFC 5545): the layers a calendar program reads and writes `.ics` text with. +//! +//! - [`lines`]: content lines. [`Lines`] unfolds a text, [`ContentLine::parse`] splits a line +//! into its name, parameters (RFC 6868 caret escapes undone) and value, [`fold`] and +//! [`param_value`] write them. +//! - [`values`]: DATE and DATE-TIME values ([`When`]), durations, TEXT escaping, lists. +//! - [`rrule`]: the common subset of recurrence rules ([`Recurrence`]), read and written, with the +//! start's weekday and month spelled out where a rule leans on them. +//! - [`zones`]: `VTIMEZONE` definitions a text brings ([`TimeZones`]), which answer the offset at +//! an instant and the instant of a wall-clock time, and [`write_vtimezone`], which writes a +//! definition from the caller's zone rules ([`ZoneRules`]). +//! +//! Times are `chrono` values; instants are seconds since 1970-01-01T00:00:00Z. There is no +//! time-zone database here: what a `TZID` means beyond the text is the caller's. +//! +//! What events and to-dos mean (which properties become which fields, which zone a time is shown +//! in) is the caller's too: it reads them from [`ContentLine`]s with these values. +//! +//! Input is hostile. Nothing here panics on any text: lines, parameters, rules, zones, parts and +//! dates are capped, and work on zone definitions is budgeted ([`TimeZones::spend`]). + +pub mod lines; +pub mod rrule; +pub mod values; +pub mod zones; + +pub use lines::{ContentLine, Lines, Param, fold, param_value}; +pub use rrule::{Freq, Recurrence, format_rrule, parse_rrule}; +pub use values::When; +pub use zones::{TimeZones, Transition, VTimezone, ZoneRules, write_vtimezone}; diff --git a/crates/ical/src/lines.rs b/crates/ical/src/lines.rs new file mode 100644 index 000000000..9c82a7c94 --- /dev/null +++ b/crates/ical/src/lines.rs @@ -0,0 +1,321 @@ +//! Content lines (RFC 5545 §3.1–3.2): unfolding on the way in, folding on the way out, and the +//! `NAME;PARAM=value;PARAM="quoted":value` shape in between. + +/// Longest logical line kept, in bytes; the rest of a longer line is dropped. +pub const MAX_LINE: usize = 1 << 20; +/// Most parameters read from one line, and most values of one parameter. +const MAX_PARAMS: usize = 64; +/// Octets per physical line when writing. +const FOLD_AT: usize = 75; + +/// The longest prefix of `s` of at most `max` bytes that ends on a char boundary. +pub fn prefix(s: &str, max: usize) -> &str { + if s.len() <= max { + return s; + } + let mut end = max; + while end > 0 && !s.is_char_boundary(end) { + end -= 1; + } + s.get(..end).unwrap_or("") +} + +/// The logical (unfolded) lines of a text. Lines end at CRLF, LF or CR; a line that starts with a +/// space or a tab continues the one before. +pub struct Lines<'a> { + rest: &'a str, +} + +impl<'a> Lines<'a> { + pub fn new(text: &'a str) -> Lines<'a> { + Lines { rest: text.strip_prefix('\u{feff}').unwrap_or(text) } + } + + /// The next physical line, without its line break. + fn physical(&mut self) -> Option<&'a str> { + if self.rest.is_empty() { + return None; + } + let bytes = self.rest.as_bytes(); + let end = bytes.iter().position(|b| matches!(b, b'\r' | b'\n')).unwrap_or(bytes.len()); + let skip = match (bytes.get(end), bytes.get(end.saturating_add(1))) { + (Some(b'\r'), Some(b'\n')) => 2, + (Some(_), _) => 1, + (None, _) => 0, + }; + let line = self.rest.get(..end).unwrap_or(""); + self.rest = self.rest.get(end.saturating_add(skip)..).unwrap_or(""); + Some(line) + } +} + +impl Iterator for Lines<'_> { + type Item = String; + + fn next(&mut self) -> Option { + let mut line = String::new(); + push_capped(&mut line, self.physical()?); + while self.rest.starts_with([' ', '\t']) { + let Some(more) = self.physical() else { break }; + push_capped(&mut line, more.get(1..).unwrap_or("")); + } + Some(line) + } +} + +fn push_capped(line: &mut String, more: &str) { + line.push_str(prefix(more, MAX_LINE.saturating_sub(line.len()))); +} + +/// One property parameter: `CN="Ada Lovelace"`, `ROLE=OPT-PARTICIPANT`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Param { + /// Upper-cased. + pub name: String, + pub values: Vec, +} + +/// One property: `ATTENDEE;CN=Ada:mailto:ada@example.org`. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct ContentLine { + /// Upper-cased. + pub name: String, + pub params: Vec, + /// As written: still escaped. + pub value: String, +} + +impl ContentLine { + /// Parse an unfolded line; `None` for a line without a name or without a `:`. + pub fn parse(line: &str) -> Option { + // Every delimiter is ASCII, so the byte positions found here are char boundaries. + let bytes = line.as_bytes(); + let stop = |from: usize, at: &[u8]| { + bytes.get(from..).and_then(|b| b.iter().position(|c| at.contains(c))).map_or(bytes.len(), |i| from.saturating_add(i)) + }; + let mut pos = stop(0, b";:"); + let name = line.get(..pos)?.trim(); + if name.is_empty() { + return None; + } + let mut params = Vec::new(); + while bytes.get(pos) == Some(&b';') { + let start = pos.saturating_add(1); + pos = stop(start, b"=;:"); + let param = line.get(start..pos)?.trim().to_ascii_uppercase(); + let mut values = Vec::new(); + while matches!(bytes.get(pos), Some(b'=' | b',')) { + let from = pos.saturating_add(1); + let value = if bytes.get(from) == Some(&b'"') { + let open = from.saturating_add(1); + let close = stop(open, b"\""); + // A quote that never closes: the line has no value. + bytes.get(close)?; + pos = stop(close, b",;:"); + line.get(open..close)? + } else { + pos = stop(from, b",;:"); + line.get(from..pos)? + }; + if values.len() < MAX_PARAMS { + values.push(caret_decode(value)); + } + } + if !param.is_empty() && params.len() < MAX_PARAMS { + params.push(Param { name: param, values }); + } + } + if bytes.get(pos) != Some(&b':') { + return None; + } + Some(ContentLine { name: name.to_ascii_uppercase(), params, value: line.get(pos.saturating_add(1)..)?.to_string() }) + } + + /// The first value of the parameter `name` (upper case). + pub fn param(&self, name: &str) -> Option<&str> { + self.params.iter().find(|p| p.name == name).and_then(|p| p.values.first()).map(String::as_str) + } +} + +/// Undo RFC 6868: `^n` is a line break, `^'` a double quote, `^^` a caret. +fn caret_decode(s: &str) -> String { + if !s.contains('^') { + return s.to_string(); + } + let mut out = String::with_capacity(s.len()); + let mut chars = s.chars().peekable(); + while let Some(c) = chars.next() { + let decoded = match (c, chars.peek()) { + ('^', Some('n')) => Some('\n'), + ('^', Some('\'')) => Some('"'), + ('^', Some('^')) => Some('^'), + _ => None, + }; + match decoded { + Some(d) => { + out.push(d); + chars.next(); + } + None => out.push(c), + } + } + out +} + +/// A parameter value ready to write: RFC 6868 escapes for what a parameter can't hold, other +/// control characters dropped, and quotes around it when it needs them. +pub fn param_value(s: &str) -> String { + let mut out = String::with_capacity(s.len().saturating_add(2)); + for c in s.chars() { + match c { + '^' => out.push_str("^^"), + '"' => out.push_str("^'"), + '\n' => out.push_str("^n"), + '\t' => out.push(c), + c if c.is_control() => {} + c => out.push(c), + } + } + if out.contains([';', ':', ',']) || out.starts_with(' ') || out.ends_with(' ') { format!("\"{out}\"") } else { out } +} + +/// Append `line` to `out` with CRLF, folded so no physical line is longer than 75 octets and no +/// character is split. +pub fn fold(out: &mut String, line: &str) { + let mut rest = line; + let mut room = FOLD_AT; + while rest.len() > room { + let head = prefix(rest, room); + if head.is_empty() { + break; + } + out.push_str(head); + out.push_str("\r\n "); + rest = rest.get(head.len()..).unwrap_or(""); + // The leading space counts. + room = FOLD_AT - 1; + } + out.push_str(rest); + out.push_str("\r\n"); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn lines(text: &str) -> Vec { + Lines::new(text).collect() + } + + #[test] + fn unfolds_every_kind_of_line_break() { + assert_eq!(lines("A:1\r\nB:2\nC:3\rD:4"), ["A:1", "B:2", "C:3", "D:4"]); + assert_eq!(lines("SUMMARY:Lunch \r\n with Ada\r\n"), ["SUMMARY:Lunch with Ada"]); + assert_eq!(lines("SUMMARY:Lun\n\tch\n twice\n"), ["SUMMARY:Lunch twice"]); + // Only the one space is the fold. + assert_eq!(lines("X:a\r\n b"), ["X:a b"]); + assert_eq!(lines("X:a \r\nY:b"), ["X:a ", "Y:b"]); + assert!(lines("").is_empty()); + } + + #[test] + fn a_byte_order_mark_and_blank_lines_are_harmless() { + assert_eq!(lines("\u{feff}BEGIN:VCALENDAR\r\n\r\nEND:VCALENDAR"), ["BEGIN:VCALENDAR", "", "END:VCALENDAR"]); + assert_eq!(ContentLine::parse(""), None); + } + + #[test] + fn parses_names_parameters_and_values() { + let l = ContentLine::parse("dtstart;tzid=Example/Riverton;Value=DATE-TIME:20261012T090000").unwrap(); + assert_eq!(l.name, "DTSTART"); + assert_eq!(l.param("TZID"), Some("Example/Riverton")); + assert_eq!(l.param("VALUE"), Some("DATE-TIME")); + assert_eq!(l.param("CN"), None); + assert_eq!(l.value, "20261012T090000"); + let plain = ContentLine::parse("SUMMARY:Budget: phase two; hiring").unwrap(); + assert_eq!((plain.name.as_str(), plain.params.len(), plain.value.as_str()), ("SUMMARY", 0, "Budget: phase two; hiring")); + assert_eq!(ContentLine::parse("DESCRIPTION:").unwrap().value, ""); + } + + #[test] + fn quoted_parameters_hide_delimiters() { + let l = ContentLine::parse("ATTENDEE;CN=\"Okafor, Nadia: lead; planning\";ROLE=REQ-PARTICIPANT:mailto:nadia@example.org").unwrap(); + assert_eq!(l.param("CN"), Some("Okafor, Nadia: lead; planning")); + assert_eq!(l.param("ROLE"), Some("REQ-PARTICIPANT")); + assert_eq!(l.value, "mailto:nadia@example.org"); + let several = ContentLine::parse("X-TEST;MEMBER=\"mailto:a@example.org\",\"mailto:b@example.org\",c:v").unwrap(); + assert_eq!(several.params[0].values, ["mailto:a@example.org", "mailto:b@example.org", "c"]); + // A parameter without a value, and an empty one. + let bare = ContentLine::parse("X-TEST;FLAG;EMPTY=:v").unwrap(); + assert_eq!(bare.params.len(), 2); + assert_eq!(bare.param("FLAG"), None); + assert_eq!(bare.param("EMPTY"), Some("")); + } + + #[test] + fn broken_lines_are_refused() { + for bad in ["no colon here", ":value without a name", ";CN=x:y", "ATTENDEE;CN=\"never closed:mailto:a@example.org", "ATTENDEE;CN=x", " "] { + assert_eq!(ContentLine::parse(bad), None, "{bad}"); + } + } + + #[test] + fn parameter_values_escape_and_quote() { + assert_eq!(param_value("Ada Lovelace"), "Ada Lovelace"); + assert_eq!(param_value("Lovelace, Ada"), "\"Lovelace, Ada\""); + assert_eq!(param_value("Ada \"the Countess\" ^1\nLine\r\u{7}"), "Ada ^'the Countess^' ^^1^nLine"); + for name in ["Ada", "Lovelace, Ada", "a\"b^c\nd", "x:y;z", "^n^'^^", "Zoë — 東京"] { + let l = ContentLine::parse(&format!("ORGANIZER;CN={}:mailto:a@example.org", param_value(name))).unwrap(); + assert_eq!(l.param("CN"), Some(name)); + } + } + + #[test] + fn folds_at_75_octets_without_splitting_characters() { + let line = format!("SUMMARY:{}", "é東🎉x".repeat(40)); + let mut out = String::new(); + fold(&mut out, &line); + assert!(out.ends_with("\r\n")); + let physical: Vec<&str> = out.trim_end_matches("\r\n").split("\r\n").collect(); + assert!(physical.len() > 3); + assert!(physical.iter().all(|l| l.len() <= 75), "{physical:?}"); + assert!(physical.iter().skip(1).all(|l| l.starts_with(' '))); + assert_eq!(lines(&out), [line]); + // A short line is left alone, and an exact fit isn't folded. + let mut short = String::new(); + fold(&mut short, &"x".repeat(75)); + assert_eq!(short, format!("{}\r\n", "x".repeat(75))); + let mut one_more = String::new(); + fold(&mut one_more, &"x".repeat(76)); + assert_eq!(one_more, format!("{}\r\n x\r\n", "x".repeat(75))); + } + + #[test] + fn enormous_lines_are_capped() { + let text = format!("SUMMARY:{}\r\n {}\r\nUID:after", "é".repeat(MAX_LINE), "x".repeat(1000)); + let got = lines(&text); + assert_eq!(got.len(), 2); + assert!(got[0].len() <= MAX_LINE); + assert_eq!(got[1], "UID:after"); + assert_eq!(prefix("héllo", 2), "h"); + assert_eq!(prefix("héllo", 0), ""); + assert_eq!(prefix("héllo", 99), "héllo"); + } + + proptest::proptest! { + #[test] + fn folding_then_unfolding_gives_the_line_back(line in "[^\r\n]{0,400}") { + let mut out = String::new(); + fold(&mut out, &line); + proptest::prop_assert!(out.split("\r\n").all(|l| l.len() <= 75)); + // A byte order mark at the very start of a text isn't part of its first line. + proptest::prop_assert_eq!(Lines::new(&out).collect::>(), vec![line.strip_prefix('\u{feff}').unwrap_or(&line).to_string()]); + } + + #[test] + fn any_line_parses_or_is_refused(line in proptest::prelude::any::()) { + let _ = ContentLine::parse(&line); + let _ = Lines::new(&line).count(); + } + } +} diff --git a/crates/ical/src/rrule.rs b/crates/ical/src/rrule.rs new file mode 100644 index 000000000..2833e5a29 --- /dev/null +++ b/crates/ical/src/rrule.rs @@ -0,0 +1,516 @@ +//! Recurrence rules (RFC 5545 §3.3.10): the common subset a calendar keeps. + +use chrono::{Datelike, NaiveDate, NaiveDateTime, Weekday}; + +use crate::lines::prefix; +use crate::values::{When, format_date, valid_time}; + +type Timestamp = i64; + +/// How often a rule repeats. `SECONDLY`, `MINUTELY` and `HOURLY` are not held. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum Freq { + Daily, + #[default] + Weekly, + Monthly, + Yearly, +} + +impl Freq { + /// From `DAILY`, `weekly`…: any letter case. + pub fn parse(s: &str) -> Option { + match s.trim().to_ascii_uppercase().as_str() { + "DAILY" => Some(Freq::Daily), + "WEEKLY" => Some(Freq::Weekly), + "MONTHLY" => Some(Freq::Monthly), + "YEARLY" => Some(Freq::Yearly), + _ => None, + } + } + + /// `DAILY`, `WEEKLY`, `MONTHLY`, `YEARLY`. + pub fn as_str(self) -> &'static str { + match self { + Freq::Daily => "DAILY", + Freq::Weekly => "WEEKLY", + Freq::Monthly => "MONTHLY", + Freq::Yearly => "YEARLY", + } + } +} + +/// How an event repeats: the subset of RRULE that calendars show and edit. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Recurrence { + pub freq: Freq, + /// Every `interval` days/weeks/months/years; 0 reads as 1. + pub interval: u32, + /// Weekly: the days of the week (none: the start's own). Daily: only on these days. Monthly and + /// yearly with `nth`: the weekday(s) counted. + pub by_day: Vec, + /// Monthly and yearly: the `nth` `by_day` weekday of the month (1–5, or -1 for the last) + /// instead of the start's day of the month. + pub nth: Option, + /// Ends after this many occurrences. + pub count: Option, + /// Ends on this day (inclusive). + pub until: Option, +} + +impl Recurrence { + /// Every day, week, month or year. + pub fn every(freq: Freq) -> Recurrence { + Recurrence { freq, interval: 1, ..Default::default() } + } +} + +/// The days of the week, Monday first. +pub const WEEKDAYS: [Weekday; 7] = [Weekday::Mon, Weekday::Tue, Weekday::Wed, Weekday::Thu, Weekday::Fri, Weekday::Sat, Weekday::Sun]; + +/// `MO`, `TU`… as iCalendar writes a weekday. +pub fn weekday_code(day: Weekday) -> &'static str { + match day { + Weekday::Mon => "MO", + Weekday::Tue => "TU", + Weekday::Wed => "WE", + Weekday::Thu => "TH", + Weekday::Fri => "FR", + Weekday::Sat => "SA", + Weekday::Sun => "SU", + } +} + +/// The weekday of `MO`, `tu`…; `None` for anything else. +pub fn weekday_of_code(s: &str) -> Option { + WEEKDAYS.into_iter().find(|d| weekday_code(*d).eq_ignore_ascii_case(s.trim())) +} + +/// Longest rule read, in bytes, and most parts (and most `BYDAY` days) of one rule. +const MAX_RULE: usize = 4096; +const MAX_PARTS: usize = 64; + +/// `FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE` as a [`Recurrence`]; `None` for a rule the model can't hold +/// (`SECONDLY`, `MINUTELY`, `HOURLY`, junk). +/// +/// `UNTIL` gives its date, whether it is written as a date or a date-time. `BYDAY=2TU`, `BYDAY=-1FR` +/// and `BYDAY=TU;BYSETPOS=2` give `nth`. Parts that only restate the start (`BYMONTH`, +/// `BYMONTHDAY`, `WKST`) and parts the model has no place for are ignored. +pub fn parse_rrule(rule: &str) -> Option { + parse_with(rule, None) +} + +/// [`parse_rrule`] for a rule read from a file: with `offset_at` (minutes east of UTC at an +/// instant), a UTC `UNTIL` gives its local day. +pub fn parse_with(rule: &str, offset_at: Option<&dyn Fn(Timestamp) -> i32>) -> Option { + let rule = prefix(rule.trim(), MAX_RULE); + let rule = match rule.get(..6) { + Some(name) if name.eq_ignore_ascii_case("RRULE:") => rule.get(6..).unwrap_or(""), + _ => rule, + }; + let mut freq = None; + let mut interval = 1u32; + let (mut count, mut until, mut set_pos) = (None, None, None); + let mut days: Vec<(Option, Weekday)> = Vec::new(); + // Parts that pick days in a way the model can't: with them, a bare BYDAY means something else. + let mut limited = false; + for part in rule.split(';').take(MAX_PARTS) { + let Some((key, value)) = part.split_once('=') else { continue }; + let value = value.trim(); + match key.trim().to_ascii_uppercase().as_str() { + "FREQ" => freq = Some(Freq::parse(value)?), + "INTERVAL" => interval = value.parse().ok().filter(|n| *n > 0).unwrap_or(1), + "COUNT" => count = value.parse::().ok(), + "UNTIL" => { + until = When::parse(value).map(|w| match offset_at { + Some(offset_at) => w.local(offset_at).date(), + None => w.wall().date(), + }) + } + "BYDAY" => days = value.split(',').take(MAX_PARTS).filter_map(by_day).collect(), + "BYSETPOS" => set_pos = value.parse::().ok(), + "BYMONTH" | "BYMONTHDAY" | "BYYEARDAY" | "BYWEEKNO" => limited = true, + _ => {} + } + } + let mut freq = freq?; + let by_month = matches!(freq, Freq::Monthly | Freq::Yearly); + // `1MO,3MO` isn't one position: the first one is kept. + let ordinal = days.iter().find_map(|(ordinal, _)| *ordinal); + if by_month && ordinal.is_some() { + days.retain(|(o, _)| *o == ordinal); + } + let mut by_day: Vec = Vec::new(); + for (_, day) in &days { + if !by_day.contains(day) { + by_day.push(*day); + } + } + let mut nth = None; + if by_month { + nth = match (ordinal, set_pos) { + (Some(ordinal), _) => position(ordinal), + // A position in a set of several weekdays ("the last working day") isn't a weekday's position. + (None, Some(pos)) if by_day.len() == 1 || (by_day.is_empty() && !limited) => position(pos), + _ => None, + }; + if nth.is_none() { + if freq == Freq::Monthly && interval == 1 && !by_day.is_empty() && ordinal.is_none() && set_pos.is_none() && !limited { + // Every Monday of every month is every Monday. + freq = Freq::Weekly; + } else { + by_day.clear(); + } + } + } + Some(Recurrence { freq, interval, by_day, nth, count, until }) +} + +/// `MO`, `2TU`, `-1FR`: the position, when there is one, and the day. +fn by_day(item: &str) -> Option<(Option, Weekday)> { + let item = item.trim(); + let split = item.len().checked_sub(2)?; + let day = weekday_of_code(item.get(split..)?)?; + let ordinal = item.get(..split)?; + Some((if ordinal.is_empty() { None } else { Some(ordinal.parse::().ok()?) }, day)) +} + +/// A position the model holds: the first to the fifth, or the last. +fn position(n: i32) -> Option { + if (1..=5).contains(&n) || n == -1 { i8::try_from(n).ok() } else { None } +} + +/// A [`Recurrence`] as an RRULE value: `FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE`. +/// +/// An `nth` without a `by_day` means "the start's weekday", which a rule alone can't say: it is +/// written as a bare `BYSETPOS`, which [`parse_rrule`] reads back but other programs may not. +/// [`format_for`] knows the start and writes the weekday (and the month of a yearly rule) out. For monthly and yearly rules without an `nth` the model ignores `by_day`, so it isn't +/// written. +pub fn format_rrule(r: &Recurrence) -> String { + format(r, None, None) +} + +/// The rule of an event that starts at `start`: the start's weekday and month are spelled out +/// where the rule leans on them, and `UNTIL` has the start's value type. +pub fn format_for(r: &Recurrence, start: NaiveDateTime, all_day: bool) -> String { + format(r, Some((valid_time(start), all_day)), None) +} + +/// The rule of a timed event whose start is written in a zone: as [`format_for`], with `UNTIL` +/// as the instant `until` in UTC, which is how RFC 5545 has it beside such a start. +pub fn format_zoned(r: &Recurrence, start: NaiveDateTime, until: Option) -> String { + format(r, Some((valid_time(start), false)), until) +} + +fn format(r: &Recurrence, start: Option<(NaiveDateTime, bool)>, until_at: Option) -> String { + let mut out = format!("FREQ={}", r.freq.as_str()); + if r.interval > 1 { + out.push_str(&format!(";INTERVAL={}", r.interval)); + } + let mut days: Vec = Vec::new(); + for day in &r.by_day { + if !days.contains(day) { + days.push(*day); + } + } + let list = |days: &[Weekday], position: &str| days.iter().map(|d| format!("{position}{}", weekday_code(*d))).collect::>().join(","); + match (r.freq, r.nth.filter(|n| *n != 0)) { + (Freq::Daily | Freq::Weekly, _) => { + if !days.is_empty() { + out.push_str(&format!(";BYDAY={}", list(&days, ""))); + } + } + (_, None) => {} + (freq, Some(nth)) => { + // Any negative position is "the last" to the model. + let nth = nth.max(-1); + if let Some((start, _)) = start { + if days.is_empty() { + days.push(start.date().weekday()); + } + if freq == Freq::Yearly { + out.push_str(&format!(";BYMONTH={}", start.date().month())); + } + } + if days.is_empty() { + out.push_str(&format!(";BYSETPOS={nth}")); + } else { + out.push_str(&format!(";BYDAY={}", list(&days, &nth.to_string()))); + } + } + } + if let Some(count) = r.count { + out.push_str(&format!(";COUNT={count}")); + } + if let (Some(_), Some(at)) = (r.until, until_at) { + out.push_str(&format!(";UNTIL={}", crate::values::format_utc(at))); + } else if let Some(until) = r.until { + out.push_str(&format!(";UNTIL={}", format_date(until))); + // The whole last day of a timed series. + if matches!(start, Some((_, false))) { + out.push_str("T235959"); + } + } + out +} + +#[cfg(test)] +mod tests { + use proptest::prelude::*; + + use super::*; + use crate::values::at_midnight; + + fn d(y: i32, m: u32, day: u32) -> NaiveDate { + NaiveDate::from_ymd_opt(y, m, day).unwrap() + } + + fn rule(freq: Freq) -> Recurrence { + Recurrence::every(freq) + } + + #[test] + fn parses_the_common_rules() { + assert_eq!(parse_rrule("FREQ=DAILY"), Some(rule(Freq::Daily))); + assert_eq!( + parse_rrule("FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE"), + Some(Recurrence { interval: 2, by_day: vec![Weekday::Mon, Weekday::Wed], ..rule(Freq::Weekly) }) + ); + assert_eq!( + parse_rrule("freq=weekly;byday=fr,mo;count=10;wkst=su"), + Some(Recurrence { by_day: vec![Weekday::Fri, Weekday::Mon], count: Some(10), ..rule(Freq::Weekly) }) + ); + assert_eq!( + parse_rrule("FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR"), + Some(Recurrence { by_day: vec![Weekday::Mon, Weekday::Tue, Weekday::Wed, Weekday::Thu, Weekday::Fri], ..rule(Freq::Daily) }) + ); + assert_eq!(parse_rrule("FREQ=MONTHLY;INTERVAL=3"), Some(Recurrence { interval: 3, ..rule(Freq::Monthly) })); + assert_eq!(parse_rrule("FREQ=YEARLY"), Some(rule(Freq::Yearly))); + // The property name and stray spaces are let through. + assert_eq!(parse_rrule(" RRULE:FREQ=DAILY; INTERVAL = 4 ;"), Some(Recurrence { interval: 4, ..rule(Freq::Daily) })); + } + + #[test] + fn positions_come_from_ordinals_or_bysetpos() { + let second_tue = Recurrence { by_day: vec![Weekday::Tue], nth: Some(2), ..rule(Freq::Monthly) }; + assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=2TU"), Some(second_tue.clone())); + assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=+2TU"), Some(second_tue.clone())); + assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=TU;BYSETPOS=2"), Some(second_tue)); + let last_fri = Recurrence { by_day: vec![Weekday::Fri], nth: Some(-1), ..rule(Freq::Monthly) }; + assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=-1FR"), Some(last_fri.clone())); + assert_eq!(parse_rrule("FREQ=MONTHLY;BYSETPOS=-1;BYDAY=FR"), Some(last_fri)); + // The fourth Thursday of November: the month restates the start. + assert_eq!( + parse_rrule("FREQ=YEARLY;BYMONTH=11;BYDAY=4TH"), + Some(Recurrence { by_day: vec![Weekday::Thu], nth: Some(4), ..rule(Freq::Yearly) }) + ); + assert_eq!( + parse_rrule("FREQ=MONTHLY;BYDAY=1MO,1FR"), + Some(Recurrence { by_day: vec![Weekday::Mon, Weekday::Fri], nth: Some(1), ..rule(Freq::Monthly) }) + ); + // Positions mean nothing in a weekly rule. + assert_eq!(parse_rrule("FREQ=WEEKLY;BYDAY=2TU"), Some(Recurrence { by_day: vec![Weekday::Tue], ..rule(Freq::Weekly) })); + } + + #[test] + fn rules_the_model_cannot_hold_fall_back() { + // Two positions: the first is kept. + assert_eq!( + parse_rrule("FREQ=MONTHLY;BYDAY=1MO,3MO,1WE"), + Some(Recurrence { by_day: vec![Weekday::Mon, Weekday::Wed], nth: Some(1), ..rule(Freq::Monthly) }) + ); + // The last working day, the second-to-last Friday, Friday the 13th and the like: plain monthly. + for r in [ + "FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1", + "FREQ=MONTHLY;BYDAY=-2FR", + "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", + "FREQ=MONTHLY;BYMONTHDAY=15", + "FREQ=MONTHLY;BYMONTHDAY=1,2,3;BYSETPOS=1", + ] { + assert_eq!(parse_rrule(r), Some(rule(Freq::Monthly)), "{r}"); + } + // The 20th Monday of the year. + assert_eq!(parse_rrule("FREQ=YEARLY;BYDAY=20MO"), Some(rule(Freq::Yearly))); + // Every Monday and Thursday of every month is weekly; every other month isn't. + assert_eq!(parse_rrule("FREQ=MONTHLY;BYDAY=MO,TH"), Some(Recurrence { by_day: vec![Weekday::Mon, Weekday::Thu], ..rule(Freq::Weekly) })); + assert_eq!(parse_rrule("FREQ=MONTHLY;INTERVAL=2;BYDAY=MO"), Some(Recurrence { interval: 2, ..rule(Freq::Monthly) })); + assert_eq!(parse_rrule("FREQ=YEARLY;BYMONTH=3,6,9"), Some(rule(Freq::Yearly))); + } + + #[test] + fn until_is_a_date_whatever_its_form() { + let until = |r: &str| parse_rrule(r).and_then(|r| r.until); + assert_eq!(until("FREQ=DAILY;UNTIL=20261231"), Some(d(2026, 12, 31))); + assert_eq!(until("FREQ=DAILY;UNTIL=20261231T235959"), Some(d(2026, 12, 31))); + assert_eq!(until("FREQ=DAILY;UNTIL=20261231T030000Z"), Some(d(2026, 12, 31))); + assert_eq!(until("FREQ=DAILY;UNTIL=someday"), None); + // Read from a file, a UTC time is a local day. + let west = parse_with("FREQ=DAILY;UNTIL=20261231T030000Z", Some(&|_| -300)).unwrap(); + assert_eq!(west.until, Some(d(2026, 12, 30))); + let east = parse_with("FREQ=DAILY;UNTIL=20261230T220000Z", Some(&|_| 600)).unwrap(); + assert_eq!(east.until, Some(d(2026, 12, 31))); + assert_eq!(parse_with("FREQ=DAILY;UNTIL=20261231", Some(&|_| -300)).unwrap().until, Some(d(2026, 12, 31))); + } + + #[test] + fn junk_and_fast_rules_are_none() { + for bad in [ + "", + "FREQ=", + "FREQ=HOURLY", + "FREQ=MINUTELY;INTERVAL=5", + "FREQ=SECONDLY", + "FREQ=SOMETIMES", + "INTERVAL=2;BYDAY=MO", + "weekly", + ";;;===", + "RRULE:", + ] { + assert_eq!(parse_rrule(bad), None, "{bad}"); + } + } + + #[test] + fn junk_parts_are_ignored() { + let got = parse_rrule("FREQ=WEEKLY;INTERVAL=0;COUNT=many;BYDAY=MO,XX,,7,MO,é;BYSETPOS=x;X-THING=1;novalue").unwrap(); + assert_eq!(got, Recurrence { by_day: vec![Weekday::Mon], ..rule(Freq::Weekly) }); + assert_eq!(parse_rrule("FREQ=DAILY;INTERVAL=99999999999;COUNT=99999999999").unwrap(), rule(Freq::Daily)); + assert_eq!(parse_rrule("FREQ=DAILY;COUNT=0").unwrap().count, Some(0)); + let long = format!("FREQ=WEEKLY;BYDAY={}", "MO,".repeat(100_000)); + assert_eq!(parse_rrule(&long).unwrap().by_day, [Weekday::Mon]); + } + + #[test] + fn formats_rules() { + assert_eq!(format_rrule(&rule(Freq::Daily)), "FREQ=DAILY"); + assert_eq!( + format_rrule(&Recurrence { interval: 2, by_day: vec![Weekday::Mon, Weekday::Wed, Weekday::Mon], ..rule(Freq::Weekly) }), + "FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE" + ); + assert_eq!(format_rrule(&Recurrence { count: Some(10), ..rule(Freq::Yearly) }), "FREQ=YEARLY;COUNT=10"); + assert_eq!(format_rrule(&Recurrence { until: Some(d(2026, 12, 31)), ..rule(Freq::Monthly) }), "FREQ=MONTHLY;UNTIL=20261231"); + assert_eq!(format_rrule(&Recurrence { by_day: vec![Weekday::Tue], nth: Some(2), ..rule(Freq::Monthly) }), "FREQ=MONTHLY;BYDAY=2TU"); + assert_eq!( + format_rrule(&Recurrence { by_day: vec![Weekday::Mon, Weekday::Fri], nth: Some(-1), ..rule(Freq::Monthly) }), + "FREQ=MONTHLY;BYDAY=-1MO,-1FR" + ); + assert_eq!(format_rrule(&Recurrence { nth: Some(3), ..rule(Freq::Monthly) }), "FREQ=MONTHLY;BYSETPOS=3"); + // Interval 0 reads as 1, and the model ignores the days of a monthly rule without a position. + assert_eq!(format_rrule(&Recurrence { interval: 0, by_day: vec![Weekday::Mon], nth: Some(0), ..rule(Freq::Monthly) }), "FREQ=MONTHLY"); + } + + #[test] + fn an_event_s_rule_spells_out_what_the_start_implies() { + let tuesday = d(2026, 10, 13).and_hms_opt(9, 0, 0).unwrap(); + let r = Recurrence { nth: Some(2), until: Some(d(2027, 6, 30)), ..rule(Freq::Monthly) }; + assert_eq!(format_for(&r, tuesday, false), "FREQ=MONTHLY;BYDAY=2TU;UNTIL=20270630T235959"); + assert_eq!(format_for(&r, tuesday, true), "FREQ=MONTHLY;BYDAY=2TU;UNTIL=20270630"); + let thanksgiving = Recurrence { by_day: vec![Weekday::Thu], nth: Some(4), ..rule(Freq::Yearly) }; + assert_eq!(format_for(&thanksgiving, at_midnight(d(2026, 11, 26)), true), "FREQ=YEARLY;BYMONTH=11;BYDAY=4TH"); + assert_eq!(format_for(&Recurrence { nth: Some(i8::MIN), ..rule(Freq::Yearly) }, tuesday, false), "FREQ=YEARLY;BYMONTH=10;BYDAY=-1TU"); + // What it writes reads back as the same series. + assert_eq!(parse_rrule("FREQ=YEARLY;BYMONTH=11;BYDAY=4TH"), Some(thanksgiving)); + // A start in a year iCalendar can't write is moved into the years it can. + let written = format_for(&Recurrence { nth: Some(1), until: Some(NaiveDate::MAX), ..rule(Freq::Yearly) }, NaiveDateTime::MAX, true); + assert_eq!(written, "FREQ=YEARLY;BYMONTH=12;BYDAY=1FR;UNTIL=99991231"); + } + + /// Every shape the model gives a meaning: weekly and daily rules with any days, monthly and + /// yearly rules with a position (and then any days) or without. + fn shapes() -> Vec { + let day_sets: [&[Weekday]; 5] = [&[], &[Weekday::Tue], &[Weekday::Sun, Weekday::Mon], &[Weekday::Fri, Weekday::Mon, Weekday::Wed], &WEEKDAYS]; + let mut out = Vec::new(); + for freq in [Freq::Daily, Freq::Weekly, Freq::Monthly, Freq::Yearly] { + for interval in [1, 2, 13, u32::MAX] { + for count in [None, Some(0), Some(1), Some(u32::MAX)] { + for until in [None, Some(d(2030, 1, 31)), Some(d(1, 1, 1)), Some(d(9999, 12, 31))] { + for days in day_sets { + let positions: &[Option] = if matches!(freq, Freq::Monthly | Freq::Yearly) { + &[None, Some(1), Some(2), Some(3), Some(4), Some(5), Some(-1)] + } else { + &[None] + }; + for nth in positions { + if nth.is_none() && !days.is_empty() && matches!(freq, Freq::Monthly | Freq::Yearly) { + continue; + } + out.push(Recurrence { freq, interval, by_day: days.to_vec(), nth: *nth, count, until }); + } + } + } + } + } + } + out + } + + #[test] + fn every_shape_the_model_supports_round_trips() { + let shapes = shapes(); + assert!(shapes.len() > 1000); + for r in shapes { + let text = format_rrule(&r); + assert_eq!(parse_rrule(&text), Some(r.clone()), "{text}"); + // And as an event writes it: the start is a Tuesday, so a missing day reads back as Tuesday. + for all_day in [false, true] { + let text = format_for(&r, d(2026, 10, 13).and_hms_opt(9, 30, 0).unwrap(), all_day); + let mut want = r.clone(); + if want.nth.is_some() && want.by_day.is_empty() { + want.by_day = vec![Weekday::Tue]; + } + assert_eq!(parse_rrule(&text), Some(want), "{text}"); + } + } + } + + #[test] + fn shapes_without_a_meaning_are_normalised() { + let normal = |r: &Recurrence| parse_rrule(&format_rrule(r)).unwrap(); + // Interval 0 is 1, duplicate days are one, position 0 is none, any negative position is the last. + assert_eq!(normal(&Recurrence { interval: 0, ..rule(Freq::Weekly) }).interval, 1); + assert_eq!(normal(&Recurrence { by_day: vec![Weekday::Mon; 100], ..rule(Freq::Weekly) }).by_day, [Weekday::Mon]); + assert_eq!(normal(&Recurrence { nth: Some(0), by_day: vec![Weekday::Mon], ..rule(Freq::Monthly) }), rule(Freq::Monthly)); + assert_eq!(normal(&Recurrence { nth: Some(-7), by_day: vec![Weekday::Mon], ..rule(Freq::Monthly) }).nth, Some(-1)); + // A position no month has isn't read back. + assert_eq!(normal(&Recurrence { nth: Some(9), by_day: vec![Weekday::Mon], ..rule(Freq::Monthly) }), rule(Freq::Monthly)); + // A position means nothing in a weekly rule. + assert_eq!(normal(&Recurrence { nth: Some(2), ..rule(Freq::Weekly) }), rule(Freq::Weekly)); + } + + fn recurrence() -> impl Strategy { + let freq = prop_oneof![Just(Freq::Daily), Just(Freq::Weekly), Just(Freq::Monthly), Just(Freq::Yearly)]; + let days = proptest::sample::subsequence(WEEKDAYS.to_vec(), 0..=7).prop_shuffle(); + let nth = proptest::option::of(prop_oneof![1i8..=5, Just(-1i8)]); + let until = proptest::option::of((1i32..=9999, 1u32..=12, 1u32..=28).prop_map(|(y, m, day)| d(y, m, day))); + (freq, 1u32.., days, nth, proptest::option::of(any::()), until).prop_map(|(freq, interval, by_day, nth, count, until)| { + let monthly = matches!(freq, Freq::Monthly | Freq::Yearly); + let nth = if monthly { nth } else { None }; + let by_day = if monthly && nth.is_none() { Vec::new() } else { by_day }; + Recurrence { freq, interval, by_day, nth, count, until } + }) + } + + proptest! { + #[test] + fn any_supported_rule_round_trips(r in recurrence()) { + prop_assert_eq!(parse_rrule(&format_rrule(&r)), Some(r)); + } + + #[test] + fn any_rule_at_all_formats_and_reads_back_stably(freq in 0u8..4, interval in any::(), days in proptest::collection::vec(0usize..7, 0..12), nth in proptest::option::of(any::()), count in proptest::option::of(any::())) { + let freq = [Freq::Daily, Freq::Weekly, Freq::Monthly, Freq::Yearly][usize::from(freq)]; + let r = Recurrence { freq, interval, by_day: days.into_iter().filter_map(|i| WEEKDAYS.get(i).copied()).collect(), nth, count, until: None }; + let text = format_rrule(&r); + let back = parse_rrule(&text).unwrap(); + // Reading normalises; a normal rule then round-trips. + prop_assert_eq!(parse_rrule(&format_rrule(&back)), Some(back)); + } + + #[test] + fn rule_text_never_panics(s in any::(), parts in proptest::collection::vec("(FREQ|INTERVAL|COUNT|UNTIL|BYDAY|BYSETPOS|BYMONTH|X)=[-+A-Z0-9,]{0,12}", 0..8)) { + let _ = parse_rrule(&s); + let _ = parse_rrule(&parts.join(";")); + let _ = parse_with(&parts.join(";"), Some(&|_| i32::MIN)); + } + } +} diff --git a/crates/ical/src/values.rs b/crates/ical/src/values.rs new file mode 100644 index 000000000..1f75a804d --- /dev/null +++ b/crates/ical/src/values.rs @@ -0,0 +1,419 @@ +//! Property values: dates and times, durations and escaped text. +//! +//! Times are `chrono` values. An instant is an `i64` of seconds since 1970-01-01T00:00:00Z, and an +//! offset of a zone is minutes east of UTC where this crate takes one from a caller and seconds +//! where a `VTIMEZONE` gives one. Every date and time this crate makes lies in the years 1 to 9999, +//! the years iCalendar can write. + +use chrono::{DateTime, NaiveDate, NaiveDateTime}; + +/// Seconds in a day. +pub const DAY: i64 = 86_400; + +/// Longest duration read, in seconds (some 10 000 years): sums of it can't overflow. +const MAX_DURATION: i64 = 10_000 * 366 * DAY; + +/// The first and the last second iCalendar can write, as seconds since 1970 on the same clock. +const MIN_CIVIL: i64 = -62_135_596_800; +const MAX_CIVIL: i64 = 253_402_300_799; + +/// Seconds since 1970-01-01T00:00:00 on the same wall clock (no zone applied). +pub fn civil_seconds(t: NaiveDateTime) -> i64 { + t.and_utc().timestamp() +} + +/// The wall-clock time `secs` seconds after 1970-01-01T00:00:00, kept in the years 1 to 9999. +pub fn from_civil_seconds(secs: i64) -> NaiveDateTime { + DateTime::from_timestamp(secs.clamp(MIN_CIVIL, MAX_CIVIL), 0).map(|t| t.naive_utc()).unwrap_or_default() +} + +/// The instant a wall clock shows in a zone `offset_minutes` east of UTC. +pub fn to_timestamp(t: NaiveDateTime, offset_minutes: i32) -> i64 { + civil_seconds(t).saturating_sub(i64::from(offset_minutes).saturating_mul(60)) +} + +/// The wall clock at the instant `ts` in a zone `offset_minutes` east of UTC. +pub fn from_timestamp(ts: i64, offset_minutes: i32) -> NaiveDateTime { + from_civil_seconds(ts.saturating_add(i64::from(offset_minutes).saturating_mul(60))) +} + +/// Midnight at the start of `d`. +pub fn at_midnight(d: NaiveDate) -> NaiveDateTime { + d.and_time(chrono::NaiveTime::MIN) +} + +/// A date and time in the years iCalendar can write: one outside them is moved to the nearest end. +pub(crate) fn valid_time(t: NaiveDateTime) -> NaiveDateTime { + from_civil_seconds(civil_seconds(t)) +} + +/// A date from `YYYY-MM-DD` or `YYYYMMDD`, years 1 to 9999. +fn parse_iso_date(s: &str) -> Option { + let s = s.trim(); + let digits: String = s.chars().filter(char::is_ascii_digit).collect(); + if digits.len() != 8 || s.chars().any(|c| !c.is_ascii_digit() && c != '-') { + return None; + } + let year: i32 = digits.get(0..4)?.parse().ok()?; + if year < 1 { + return None; + } + NaiveDate::from_ymd_opt(year, digits.get(4..6)?.parse().ok()?, digits.get(6..8)?.parse().ok()?) +} + +/// A DATE or DATE-TIME value. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum When { + /// `20261012`: a whole day. + Date(NaiveDate), + /// `20261012T090000`: a wall-clock time, floating or in a named zone. + Local(NaiveDateTime), + /// `20261012T090000Z`. + Utc(NaiveDateTime), +} + +impl When { + /// From `YYYYMMDD`, `YYYYMMDDTHHMMSS` or `YYYYMMDDTHHMMSSZ`; the seconds may be missing, and + /// the ISO 8601 punctuation some writers add (`2026-10-12T09:00:00Z`) is let through. + pub fn parse(s: &str) -> Option { + let s = s.trim(); + if s.len() > 32 || !s.is_ascii() { + return None; + } + let (date, time) = match s.find(['T', 't']) { + Some(i) => (s.get(..i)?, Some(s.get(i.saturating_add(1)..)?)), + None => (s, None), + }; + let date = parse_iso_date(date)?; + let Some(time) = time else { return Some(When::Date(date)) }; + let (clock, utc) = match time.strip_suffix(['Z', 'z']) { + Some(clock) => (clock, true), + None => (time, false), + }; + let digits: String = clock.chars().filter(|c| *c != ':').collect(); + if !matches!(digits.len(), 4 | 6) || !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let part = |at: usize| digits.get(at..at.saturating_add(2)).and_then(|d| d.parse::().ok()); + // A leap second (60) is read as the last second of the minute. + let second = if digits.len() == 6 { part(4)?.min(59) } else { 0 }; + let t = date.and_hms_opt(part(0)?, part(2)?, second)?; + Some(if utc { When::Utc(t) } else { When::Local(t) }) + } + + /// The date and time as written, whatever the zone. + pub fn wall(self) -> NaiveDateTime { + match self { + When::Date(d) => at_midnight(d), + When::Local(t) | When::Utc(t) => t, + } + } + + /// The local wall-clock time: a UTC time is converted, anything else is taken as written. + /// `offset_at(instant)` is minutes east of UTC. + pub fn local(self, offset_at: &dyn Fn(i64) -> i32) -> NaiveDateTime { + match self { + When::Utc(t) => { + let ts = to_timestamp(t, 0); + from_timestamp(ts, offset_at(ts)) + } + other => other.wall(), + } + } + + /// The instant: anything but a UTC time is read in the local zone. + pub fn timestamp(self, offset_at: &dyn Fn(i64) -> i32) -> i64 { + match self { + When::Utc(t) => to_timestamp(t, 0), + other => { + let t = other.wall(); + // Asked twice, so a time near a daylight-saving change still gets its own offset. + let guess = to_timestamp(t, offset_at(to_timestamp(t, 0))); + to_timestamp(t, offset_at(guess)) + } + } + } +} + +/// A duration in seconds from `PT15M`, `-PT1H30M`, `P1D`, `P1DT12H`, `P2W`; `None` for junk. +pub fn parse_duration(s: &str) -> Option { + let s = s.trim(); + let (negative, body) = match s.strip_prefix('-') { + Some(body) => (true, body), + None => (false, s.strip_prefix('+').unwrap_or(s)), + }; + let body = body.strip_prefix(['P', 'p'])?; + let (mut total, mut number, mut in_time, mut any) = (0i64, None::, false, false); + for c in body.chars() { + match c.to_ascii_uppercase() { + '0'..='9' => { + let digit = i64::from(c.to_digit(10)?); + number = Some(number.unwrap_or(0).checked_mul(10)?.checked_add(digit)?); + } + 'T' if number.is_none() => in_time = true, + unit => { + let seconds = match (unit, in_time) { + ('W', false) => 7 * DAY, + ('D', false) => DAY, + ('H', true) => 3600, + ('M', true) => 60, + ('S', true) => 1, + _ => return None, + }; + total = total.checked_add(number.take()?.checked_mul(seconds)?)?; + any = true; + } + } + } + if number.is_some() || !any || total > MAX_DURATION { + return None; + } + Some(if negative { -total } else { total }) +} + +/// `20261012`. +pub fn format_date(d: NaiveDate) -> String { + valid_time(at_midnight(d)).format("%Y%m%d").to_string() +} + +/// `20261012T090000` (floating). +pub fn format_local(t: NaiveDateTime) -> String { + valid_time(t).format("%Y%m%dT%H%M%S").to_string() +} + +/// `20261012T130000Z`. +pub fn format_utc(ts: i64) -> String { + format!("{}Z", format_local(from_timestamp(ts, 0))) +} + +/// A TEXT value ready to write: `\`, `;`, `,` and line breaks escaped (`\r\n` and `\r` count as +/// one line break), other control characters except tab dropped. +pub fn escape_text(s: &str) -> String { + let mut out = String::with_capacity(s.len().saturating_add(8)); + let mut after_cr = false; + for c in s.chars() { + let cr = c == '\r'; + match c { + '\n' if after_cr => {} + '\r' | '\n' => out.push_str("\\n"), + '\\' => out.push_str("\\\\"), + ';' => out.push_str("\\;"), + ',' => out.push_str("\\,"), + '\t' => out.push(c), + c if c.is_control() => {} + c => out.push(c), + } + after_cr = cr; + } + out +} + +/// Undo [`escape_text`]: `\n` and `\N` are line breaks, `\\`, `\;`, `\,` (and the `\:` and `\"` +/// some writers add) their second character; any other backslash is kept. Control characters +/// except tab are dropped. +pub fn unescape_text(s: &str) -> String { + let mut out = String::with_capacity(s.len()); + let mut chars = s.chars(); + while let Some(c) = chars.next() { + match c { + '\\' => match chars.next() { + Some('n' | 'N') => out.push('\n'), + Some(x @ ('\\' | ';' | ',' | ':' | '"')) => out.push(x), + Some(x) => { + out.push('\\'); + if !x.is_control() || x == '\t' { + out.push(x); + } + } + None => out.push('\\'), + }, + '\t' => out.push(c), + c if c.is_control() => {} + c => out.push(c), + } + } + out +} + +/// The first `max` items of a list value, split on its unescaped commas; the items are still +/// escaped. +pub fn split_list(s: &str, max: usize) -> Vec<&str> { + let mut out = Vec::new(); + let (mut start, mut escaped) = (0usize, false); + for (i, c) in s.char_indices() { + if out.len() >= max { + return out; + } + match c { + _ if escaped => escaped = false, + '\\' => escaped = true, + ',' => { + out.push(s.get(start..i).unwrap_or("")); + start = i.saturating_add(1); + } + _ => {} + } + } + if out.len() < max { + out.push(s.get(start..).unwrap_or("")); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn d(y: i32, m: u32, day: u32) -> NaiveDate { + NaiveDate::from_ymd_opt(y, m, day).unwrap() + } + + fn at(y: i32, m: u32, day: u32, h: u32, min: u32, s: u32) -> NaiveDateTime { + d(y, m, day).and_hms_opt(h, min, s).unwrap() + } + + #[test] + fn dates_and_times() { + assert_eq!(When::parse("20261012"), Some(When::Date(d(2026, 10, 12)))); + assert_eq!(When::parse("20261012T090000"), Some(When::Local(at(2026, 10, 12, 9, 0, 0)))); + assert_eq!(When::parse(" 20261012T133015Z "), Some(When::Utc(at(2026, 10, 12, 13, 30, 15)))); + assert_eq!(When::parse("20261012t0900z"), Some(When::Utc(at(2026, 10, 12, 9, 0, 0)))); + assert_eq!(When::parse("2026-10-12T09:00:00Z"), Some(When::Utc(at(2026, 10, 12, 9, 0, 0)))); + // A leap second. + assert_eq!(When::parse("20161231T235960Z"), Some(When::Utc(at(2016, 12, 31, 23, 59, 59)))); + } + + #[test] + fn junk_dates_are_none() { + for bad in [ + "", + "tomorrow", + "2026101", + "202610123", + "20261312", + "20260230", + "00000101", + "20261012T", + "20261012T25", + "20261012T240000", + "20261012T0960", + "20261012T090000+0200", + "20261012", + "20261012T09000000000000000000000000000000000000", + ] { + assert_eq!(When::parse(bad), None, "{bad}"); + } + } + + #[test] + fn utc_times_become_local_and_local_times_become_instants() { + let utc = When::parse("20261012T130000Z").unwrap(); + assert_eq!(utc.local(&|_| -240), at(2026, 10, 12, 9, 0, 0)); + assert_eq!(utc.local(&|_| 330), at(2026, 10, 12, 18, 30, 0)); + assert_eq!(utc.wall(), at(2026, 10, 12, 13, 0, 0)); + // Across midnight. + assert_eq!(When::parse("20261012T020000Z").unwrap().local(&|_| -300).date(), d(2026, 10, 11)); + let local = When::parse("20261012T090000").unwrap(); + assert_eq!(local.local(&|_| -240), at(2026, 10, 12, 9, 0, 0)); + assert_eq!(local.timestamp(&|_| -240), utc.timestamp(&|_| 600)); + assert_eq!(When::Date(d(1970, 1, 2)).timestamp(&|_| 60), DAY - 3600); + // A zone that changes its offset at an instant: each side gets its own. + let change = to_timestamp(at(2026, 11, 1, 6, 0, 0), 0); + let zone = |ts: i64| if ts < change { -240 } else { -300 }; + assert_eq!(When::parse("20261101T000000").unwrap().timestamp(&zone), to_timestamp(at(2026, 11, 1, 4, 0, 0), 0)); + assert_eq!(When::parse("20261101T120000").unwrap().timestamp(&zone), to_timestamp(at(2026, 11, 1, 17, 0, 0), 0)); + // A hostile offset is survived. + let _ = utc.local(&|_| i32::MIN); + let _ = local.timestamp(&|_| i32::MAX); + } + + #[test] + fn durations() { + assert_eq!(parse_duration("PT15M"), Some(900)); + assert_eq!(parse_duration("-PT15M"), Some(-900)); + assert_eq!(parse_duration("+PT1H30M"), Some(5400)); + assert_eq!(parse_duration("P1D"), Some(DAY)); + assert_eq!(parse_duration("P1DT12H"), Some(DAY + 12 * 3600)); + assert_eq!(parse_duration("P2W"), Some(14 * DAY)); + assert_eq!(parse_duration("pt0s"), Some(0)); + assert_eq!(parse_duration("-PT0M"), Some(0)); + assert_eq!(parse_duration("PT1H2M3S"), Some(3723)); + for bad in ["", "P", "PT", "15M", "PT15", "P15M", "PT1D", "PTM", "P1X", "P-1D", "P99999999999999999999D", "P9999999999W", "PT1H T"] { + assert_eq!(parse_duration(bad), None, "{bad}"); + } + } + + #[test] + fn formats_and_keeps_dates_in_writable_years() { + assert_eq!(format_date(d(2026, 1, 2)), "20260102"); + assert_eq!(format_date(d(987, 6, 5)), "09870605"); + assert_eq!(format_local(at(2026, 10, 12, 9, 5, 0)), "20261012T090500"); + assert_eq!(format_utc(0), "19700101T000000Z"); + assert_eq!(format_utc(1_760_082_785), "20251010T075305Z"); + assert_eq!(format_utc(i64::MAX), "99991231T235959Z"); + assert_eq!(format_utc(i64::MIN), "00010101T000000Z"); + // Years iCalendar can't write are moved to the nearest it can. + for hostile in [NaiveDate::MIN, NaiveDate::MAX, d(0, 1, 1), d(-4, 2, 29), d(10_000, 2, 29)] { + assert!(When::parse(&format_date(hostile)).is_some(), "{hostile:?}"); + assert!(When::parse(&format_local(at_midnight(hostile))).is_some(), "{hostile:?}"); + } + assert_eq!(format_date(NaiveDate::MAX), "99991231"); + assert_eq!(format_local(NaiveDateTime::MIN), "00010101T000000"); + // Civil seconds are the same clock read as UTC, and are kept in the years 1 to 9999. + assert_eq!(civil_seconds(at(1970, 1, 2, 0, 0, 1)), DAY + 1); + assert_eq!(from_civil_seconds(DAY + 1), at(1970, 1, 2, 0, 0, 1)); + assert_eq!(from_civil_seconds(i64::MAX), at(9999, 12, 31, 23, 59, 59)); + assert_eq!(from_civil_seconds(i64::MIN), at(1, 1, 1, 0, 0, 0)); + assert_eq!(from_timestamp(0, -300), at(1969, 12, 31, 19, 0, 0)); + assert_eq!(to_timestamp(at(1969, 12, 31, 19, 0, 0), -300), 0); + } + + #[test] + fn text_escapes() { + assert_eq!(escape_text("Budget, phase two; \\ done\nNext"), "Budget\\, phase two\\; \\\\ done\\nNext"); + assert_eq!(escape_text("a\r\nb\rc\n\nd\u{0}\u{7f}\te"), "a\\nb\\nc\\n\\nd\te"); + assert_eq!(unescape_text("Budget\\, phase two\\; \\\\ done\\nNext\\NLast"), "Budget, phase two; \\ done\nNext\nLast"); + // What other writers escape without being asked to, and what isn't an escape at all. + assert_eq!(unescape_text("10\\:30 \\\"quoted\\\" C:\\Users\\x tail\\"), "10:30 \"quoted\" C:\\Users\\x tail\\"); + assert_eq!(unescape_text("a\u{0}b\u{1b}c\td"), "abc\td"); + } + + #[test] + fn lists_split_on_unescaped_commas() { + assert_eq!(split_list("Work,Travel\\, abroad,,Home\\\\,x", 10), ["Work", "Travel\\, abroad", "", "Home\\\\", "x"]); + assert_eq!(split_list("", 10), [""]); + assert_eq!(split_list("a,b,c,d", 2), ["a", "b"]); + assert_eq!(split_list("a,b", 2), ["a", "b"]); + assert!(split_list("a,b", 0).is_empty()); + } + + proptest::proptest! { + #[test] + fn clean_text_survives_escaping(s in "(\\PC|[\n\t;,\\\\:\"]){0,80}") { + proptest::prop_assert_eq!(unescape_text(&escape_text(&s)), s); + } + + #[test] + fn any_text_escapes_to_one_clean_line(s in proptest::prelude::any::()) { + let escaped = escape_text(&s); + proptest::prop_assert!(!escaped.contains(['\r', '\n'])); + // Escaping what was read gives the same line again. + proptest::prop_assert_eq!(escape_text(&unescape_text(&escaped)), escaped); + } + + #[test] + fn values_never_panic(s in proptest::prelude::any::(), max in 0usize..5) { + let _ = When::parse(&s); + let _ = parse_duration(&s); + let _ = unescape_text(&s); + let _ = split_list(&s, max); + } + + #[test] + fn any_civil_second_reads_back_in_writable_years(secs in proptest::prelude::any::()) { + let t = from_civil_seconds(secs); + proptest::prop_assert_eq!(civil_seconds(t), secs.clamp(MIN_CIVIL, MAX_CIVIL)); + proptest::prop_assert!(When::parse(&format_local(t)).is_some()); + } + } +} diff --git a/crates/ical/src/zones.rs b/crates/ical/src/zones.rs new file mode 100644 index 000000000..9d01d93b8 --- /dev/null +++ b/crates/ical/src/zones.rs @@ -0,0 +1,498 @@ +//! Time zones in iCalendar text (RFC 5545 §3.6.5): the `VTIMEZONE` definitions a text brings with +//! it, read ([`TimeZones`], [`VTimezone`]) and written ([`write_vtimezone`]). +//! +//! This crate has no time-zone database. A definition read from a text answers for itself (the +//! offset at an instant, the instant of a wall-clock time); what a `TZID` means beyond the text +//! (an IANA name, a Windows name, which database zone keeps the same clock) is the caller's. To +//! write a definition, the caller hands over its zone's rules through [`ZoneRules`]. +//! +//! Definitions are hostile text: offsets of more than eighteen hours are not offsets, and the +//! zones, their parts and their dates are all capped. [`TimeZones::spend`] keeps a budget of the +//! work a caller does with them. + +use std::cell::Cell; + +use chrono::{Datelike, NaiveDate, NaiveDateTime, Weekday}; + +use crate::lines::{ContentLine, Lines, fold, prefix}; +use crate::rrule::{weekday_code, weekday_of_code}; +use crate::values::{When, at_midnight, civil_seconds, format_local, from_civil_seconds, from_timestamp, to_timestamp}; + +/// Most zones one text defines, most parts (`STANDARD`, `DAYLIGHT`) of one zone, and most dates +/// (`RDATE`) of one part. +pub const MAX_ZONES: usize = 64; +pub const MAX_PARTS: usize = 64; +pub const MAX_DATES: usize = 64; +/// The work one text's times may cost, in units of one look at one part or one date of a +/// definition ([`VTimezone::cost`]). Past it, [`TimeZones::spend`] says no. +pub const WORK_BUDGET: u64 = 20_000_000; +/// The furthest from UTC an offset may be, in seconds. +const MAX_OFFSET: i32 = 18 * 3600; +/// Longest `TZID` (and `X-LIC-LOCATION`) kept, in bytes. +pub const MAX_TZID: usize = 128; + +/// On which day of each year a part of a zone begins. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Yearly { + /// The `nth` (1–5, or negative from the end) `weekday` of `month`. + Nth { month: u32, nth: i8, weekday: Weekday }, + /// Day `day` of `month`. + Day { month: u32, day: u32 }, +} + +/// The `nth` (1–5, or negative from the end) `weekday` of a month, when the month has one. +fn nth_weekday(year: i32, month: u32, weekday: Weekday, nth: i8) -> Option { + let first = NaiveDate::from_ymd_opt(year, month, 1)?; + if nth > 0 { + let offset = (i64::from(weekday.num_days_from_monday()) - i64::from(first.weekday().num_days_from_monday())).rem_euclid(7); + NaiveDate::from_ymd_opt(year, month, u32::try_from(1 + offset + 7 * (i64::from(nth) - 1)).ok()?) + } else { + let last = first.checked_add_months(chrono::Months::new(1))?.pred_opt()?; + let back = (i64::from(last.weekday().num_days_from_monday()) - i64::from(weekday.num_days_from_monday())).rem_euclid(7); + let day = last.checked_sub_days(chrono::Days::new(u64::try_from(back + 7 * (i64::from(nth.saturating_neg()) - 1)).ok()?))?; + (day.month() == month).then_some(day) + } +} + +impl Yearly { + fn date(self, year: i32) -> Option { + match self { + Yearly::Day { month, day } => NaiveDate::from_ymd_opt(year, month, day), + Yearly::Nth { month, nth, weekday } => nth_weekday(year, month, weekday, nth), + } + } +} + +/// One `STANDARD` or `DAYLIGHT` part of a definition: from `start` on (a wall-clock time by the +/// offset `from`), and then again every year or on the listed dates, the offset is `to`. +#[derive(Clone, Debug, PartialEq, Eq)] +struct Part { + start: NaiveDateTime, + /// Seconds east of UTC before and after. + from: i32, + to: i32, + yearly: Option, + /// The rule holds up to this instant, and for this many beginnings. + until: Option, + count: Option, + dates: Vec, +} + +impl Part { + /// The wall-clock second from which `to` holds for a beginning written as `start`: a time + /// the clocks skip is still by the offset before, and so is the first of an hour that comes + /// twice (RFC 5545 §3.3.5). + fn wall_from(&self, start: NaiveDateTime) -> i64 { + civil_seconds(start).saturating_add(i64::from(self.to.saturating_sub(self.from).max(0))) + } + + fn instant_of(&self, start: NaiveDateTime) -> i64 { + civil_seconds(start).saturating_sub(i64::from(self.from)) + } + + /// The beginning of the rule's year `year`, when the rule has one then. + fn in_year(&self, year: i32) -> Option { + let rule = self.yearly?; + let first = self.start.year(); + if year < first || self.count.is_some_and(|count| i64::from(year) - i64::from(first) >= i64::from(count)) { + return None; + } + let begins = rule.date(year)?.and_time(self.start.time()); + (begins >= self.start && self.until.is_none_or(|until| self.instant_of(begins) <= until)).then_some(begins) + } + + /// The last beginning at or before `limit`, as `key` measures beginnings. + fn latest(&self, limit: i64, year: i32, key: &dyn Fn(&Part, NaiveDateTime) -> i64) -> Option { + let mut best: Option = None; + let mut take = |begins: NaiveDateTime| { + let at = key(self, begins); + if at <= limit && best.is_none_or(|have| at > have) { + best = Some(at); + } + }; + take(self.start); + self.dates.iter().copied().for_each(&mut take); + // The year of the time asked about and the years beside it; and where the rule ends. + let last = self.until.map(|until| from_timestamp(until, 0).year()); + let counted = self.count.map(|count| self.start.year().saturating_add(i32::try_from(count).unwrap_or(i32::MAX)).saturating_sub(1)); + for year in [Some(year.saturating_add(1)), Some(year), Some(year.saturating_sub(1)), last, last.map(|y| y.saturating_sub(1)), counted] + .into_iter() + .flatten() + { + if let Some(begins) = self.in_year(year) { + take(begins); + } + } + best + } +} + +/// A zone as a text defines it (`VTIMEZONE`). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct VTimezone { + tzid: String, + /// `X-LIC-LOCATION`: the place the writer says the zone is (an IANA name, as a rule). + place: String, + parts: Vec, +} + +impl VTimezone { + /// The `TZID`, at most [`MAX_TZID`] bytes. + pub fn tzid(&self) -> &str { + &self.tzid + } + + /// `X-LIC-LOCATION`, empty when the text gives none. + pub fn place(&self) -> &str { + &self.place + } + + /// What one look at the definition costs, in units of [`WORK_BUDGET`]. + pub fn cost(&self) -> u64 { + self.parts.iter().fold(0u64, |sum, part| sum.saturating_add(8).saturating_add(part.dates.len() as u64)) + } + + fn offset_by(&self, limit: i64, year: i32, key: &dyn Fn(&Part, NaiveDateTime) -> i64) -> i32 { + let mut best: Option<(i64, i32)> = None; + for part in &self.parts { + if let Some(at) = part.latest(limit, year, key) + && best.is_none_or(|(have, _)| at >= have) + { + best = Some((at, part.to)); + } + } + match best { + Some((_, to)) => to, + // Before the first beginning: the offset the first one changes from. + None => self.parts.iter().min_by_key(|part| part.start).map_or(0, |part| part.from), + } + } + + /// Seconds east of UTC for a wall-clock time written in the zone. A time the clocks skip is + /// read by the offset before the change; a time that comes twice is the first of the two. + pub fn offset_for_wall(&self, wall: NaiveDateTime) -> i32 { + self.offset_by(civil_seconds(wall), wall.year(), &|part, begins| part.wall_from(begins)) + } + + /// Seconds east of UTC at an instant (seconds since 1970-01-01T00:00:00Z). + pub fn offset_at(&self, instant: i64) -> i32 { + self.offset_by(instant, from_timestamp(instant, 0).year(), &|part, begins| part.instant_of(begins)) + } + + /// The instant of a wall-clock time written in the zone. + pub fn instant(&self, wall: NaiveDateTime) -> i64 { + civil_seconds(wall).saturating_sub(i64::from(self.offset_for_wall(wall))) + } +} + +/// `+0100`, `-0500`, `+053000`: seconds east of UTC; `None` for anything else and for more than +/// an offset can be. +pub fn parse_utc_offset(text: &str) -> Option { + let text = text.trim(); + let (sign, digits) = match (text.strip_prefix('+'), text.strip_prefix('-')) { + (Some(rest), _) => (1, rest), + (None, Some(rest)) => (-1, rest), + (None, None) => return None, + }; + let digits: String = digits.chars().filter(|c| *c != ':').collect(); + if !matches!(digits.len(), 4 | 6) || !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let part = |at: usize| digits.get(at..at.saturating_add(2)).and_then(|d| d.parse::().ok()); + let (hours, minutes, seconds) = (part(0)?, part(2)?, if digits.len() == 6 { part(4)? } else { 0 }); + let total = hours * 3600 + minutes * 60 + seconds; + (minutes < 60 && seconds < 60 && total <= MAX_OFFSET).then_some(sign * total) +} + +/// An offset in seconds as `+0100`, `-0930`, `+045602`. +pub fn format_utc_offset(seconds: i32) -> String { + let sign = if seconds < 0 { '-' } else { '+' }; + let a = seconds.unsigned_abs(); + if a.is_multiple_of(60) { + format!("{sign}{:02}{:02}", a / 3600, a / 60 % 60) + } else { + format!("{sign}{:02}{:02}{:02}", a / 3600, a / 60 % 60, a % 60) + } +} + +/// The rule of a part: `FREQ=YEARLY;BYMONTH=3;BYDAY=2SU`, with the day of the month or the +/// part's own day when no weekday is named. `None` for a rule that isn't yearly. +fn yearly(rule: &str, start: NaiveDateTime) -> Option<(Yearly, Option, Option)> { + let (mut yearly, mut month, mut by_day, mut month_day) = (false, None, None, None); + let (mut until, mut count) = (None, None); + for part in prefix(rule, 512).split(';').take(16) { + let Some((key, value)) = part.split_once('=') else { continue }; + let value = value.trim(); + match key.trim().to_ascii_uppercase().as_str() { + "FREQ" => yearly = value.eq_ignore_ascii_case("YEARLY"), + "INTERVAL" if value != "1" => return None, + "BYMONTH" => month = value.parse::().ok().filter(|m| (1..=12).contains(m)), + "BYMONTHDAY" => month_day = value.parse::().ok().filter(|d| (1..=31).contains(d)), + "BYDAY" => { + let split = value.len().checked_sub(2)?; + let weekday = weekday_of_code(value.get(split..)?)?; + let nth = match value.get(..split)? { + "" => return None, + nth => nth.parse::().ok().filter(|n| (1..=5).contains(n) || (-5..=-1).contains(n))?, + }; + by_day = Some((nth, weekday)); + } + "UNTIL" => until = When::parse(value).map(|at| to_timestamp(at.wall(), 0)), + "COUNT" => count = value.parse::().ok(), + _ => {} + } + } + if !yearly { + return None; + } + let month = month.unwrap_or(start.month()); + let rule = match (by_day, month_day) { + (Some((nth, weekday)), _) => Yearly::Nth { month, nth, weekday }, + (None, day) => Yearly::Day { month, day: day.unwrap_or(start.day()) }, + }; + Some((rule, until, count)) +} + +/// A `STANDARD` or `DAYLIGHT` part being read. +#[derive(Default)] +struct PartBuilder { + start: Option, + from: Option, + to: Option, + rule: Option, + dates: Vec, +} + +impl PartBuilder { + fn finish(self) -> Option { + let (start, from, to) = (self.start?, self.from?, self.to?); + let (yearly, until, count) = match self.rule.as_deref().and_then(|rule| yearly(rule, start)) { + Some((rule, until, count)) => (Some(rule), until, count), + None => (None, None, None), + }; + Some(Part { start, from, to, yearly, until, count, dates: self.dates }) + } +} + +/// The zones a text defines, and the budget of work spent on them. +#[derive(Debug, Default)] +pub struct TimeZones { + zones: Vec, + /// Work spent so far (see [`WORK_BUDGET`]), spends granted so far, and whether the budget ran out. + spent: Cell, + granted: Cell, + stopped: Cell, +} + +impl TimeZones { + /// Read the `VTIMEZONE`s of a text: at most [`MAX_ZONES`], the first of each `TZID`. A zone + /// without a `TZID` or without a part that has a start and both offsets is not one. + pub fn collect(text: &str) -> TimeZones { + let mut out = TimeZones::default(); + // Nothing to look for in most texts. + let named = text.as_bytes().windows(9).any(|bytes| bytes.eq_ignore_ascii_case(b"VTIMEZONE")); + if !named { + return out; + } + let mut zone: Option = None; + let mut part: Option = None; + for raw in Lines::new(text) { + let Some(line) = ContentLine::parse(&raw) else { continue }; + let value = prefix(line.value.trim(), MAX_TZID); + match (line.name.as_str(), value.to_ascii_uppercase().as_str()) { + ("BEGIN", "VTIMEZONE") => { + if out.zones.len() >= MAX_ZONES { + break; + } + zone = Some(VTimezone::default()); + part = None; + } + ("END", "VTIMEZONE") => { + part = None; + if let Some(definition) = zone.take() + && !definition.tzid.is_empty() + && !definition.parts.is_empty() + && !out.zones.iter().any(|have| have.tzid == definition.tzid) + { + out.zones.push(definition); + } + } + ("BEGIN", "STANDARD" | "DAYLIGHT") if zone.is_some() => part = Some(PartBuilder::default()), + ("END", "STANDARD" | "DAYLIGHT") => { + if let (Some(definition), Some(built)) = (&mut zone, part.take().and_then(PartBuilder::finish)) + && definition.parts.len() < MAX_PARTS + { + definition.parts.push(built); + } + } + // Anything else that begins inside a zone is not a part of it. + ("BEGIN", _) => part = None, + (name, _) => match (&mut zone, &mut part) { + (Some(_), Some(part)) => match name { + "DTSTART" => part.start = part.start.or_else(|| When::parse(&line.value).map(When::wall)), + "TZOFFSETFROM" => part.from = part.from.or_else(|| parse_utc_offset(&line.value)), + "TZOFFSETTO" => part.to = part.to.or_else(|| parse_utc_offset(&line.value)), + "RRULE" if part.rule.is_none() => part.rule = Some(prefix(&line.value, 512).to_string()), + "RDATE" => { + let room = MAX_DATES.saturating_sub(part.dates.len()); + part.dates.extend(line.value.split(',').take(room).filter_map(When::parse).map(When::wall)); + } + _ => {} + }, + (Some(definition), None) => match name { + "TZID" if definition.tzid.is_empty() => definition.tzid = value.to_string(), + "X-LIC-LOCATION" if definition.place.is_empty() => definition.place = value.to_string(), + _ => {} + }, + _ => {} + }, + } + } + out + } + + /// The definition of `tzid`: the one of that name, else one whose name differs only in letter + /// case. The name is looked at to [`MAX_TZID`] bytes. + pub fn get(&self, tzid: &str) -> Option<&VTimezone> { + let tzid = prefix(tzid.trim(), MAX_TZID); + self.zones.iter().find(|zone| zone.tzid == tzid).or_else(|| self.zones.iter().find(|zone| zone.tzid.eq_ignore_ascii_case(tzid))) + } + + /// Every definition, in the order of the text. + pub fn iter(&self) -> impl Iterator { + self.zones.iter() + } + + pub fn is_empty(&self) -> bool { + self.zones.is_empty() + } + + /// Spend `units` of the text's work budget; `false` (and the budget is out for good) when + /// there is not enough left. + pub fn spend(&self, units: u64) -> bool { + let total = self.spent.get().saturating_add(units); + if self.stopped.get() || total > WORK_BUDGET { + self.stopped.set(true); + return false; + } + self.spent.set(total); + self.granted.set(self.granted.get().saturating_add(1)); + true + } + + /// When the budget ran out: how many spends were granted before it did. + pub fn stopped_after(&self) -> Option { + self.stopped.get().then(|| self.granted.get()) + } +} + +// ---- writing ---- + +/// A change of a zone's offset: at the instant `at` (seconds since 1970-01-01T00:00:00Z), from +/// `before` to `after` seconds east of UTC. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Transition { + pub at: i64, + pub before: i32, + pub after: i32, +} + +/// What [`write_vtimezone`] needs to know of a zone: the caller's time-zone database. +pub trait ZoneRules { + /// Seconds east of UTC at an instant. + fn offset_at(&self, instant: i64) -> i32; + /// The changes of offset from `from` up to `to` (instants), in order. + fn transitions(&self, from: i64, to: i64) -> Vec; +} + +/// The rule a change of clocks follows when it falls on the same weekday of the same month each +/// year at the same time of day: the `nth` weekday (the last, when it is in the month's last week +/// in each of the years). +fn yearly_rule(changes: &[(NaiveDateTime, i32, i32)]) -> Option { + let (first, first_from, first_to) = changes.first().copied()?; + let alike = changes.iter().all(|(at, from, to)| { + at.month() == first.month() && at.weekday() == first.weekday() && at.time() == first.time() && (*from, *to) == (first_from, first_to) + }); + if !alike { + return None; + } + let nth = |at: &NaiveDateTime| (at.day() - 1) / 7 + 1; + let last = |at: &NaiveDateTime| at.date().checked_add_days(chrono::Days::new(7)).is_none_or(|week_on| week_on.month() != at.month()); + let (month, weekday) = (first.month(), first.weekday()); + if changes.iter().all(|(at, _, _)| nth(at) == nth(&first)) && nth(&first) < 5 && !changes.iter().all(|(at, _, _)| last(at)) { + Some(Yearly::Nth { month, nth: i8::try_from(nth(&first)).unwrap_or(1), weekday }) + } else if changes.iter().all(|(at, _, _)| last(at)) { + Some(Yearly::Nth { month, nth: -1, weekday }) + } else { + None + } +} + +fn write_part(out: &mut String, kind: &str, start: NaiveDateTime, from: i32, to: i32, rule: Option) { + fold(out, &format!("BEGIN:{kind}")); + fold(out, &format!("DTSTART:{}", format_local(start))); + fold(out, &format!("TZOFFSETFROM:{}", format_utc_offset(from))); + fold(out, &format!("TZOFFSETTO:{}", format_utc_offset(to))); + if let Some(Yearly::Nth { month, nth, weekday }) = rule { + fold(out, &format!("RRULE:FREQ=YEARLY;BYMONTH={month};BYDAY={nth}{}", weekday_code(weekday))); + } + fold(out, &format!("END:{kind}")); +} + +/// Write the definition of a zone named `tzid` (control characters dropped, at most [`MAX_TZID`] +/// bytes) for times in the years `from`–`to` (at most 120 +/// years are written; the years are kept in 2 to 9990). +/// +/// A zone that changes its clocks twice a year by one rule is written as most programs write it, +/// and all of them read it: one `STANDARD` and one `DAYLIGHT` part, each with its yearly rule. A +/// zone that doesn't change is one `STANDARD` part. Any other (a year in which the rule changed, +/// a zone that follows the moon) is written out change by change for those years. +pub fn write_vtimezone(out: &mut String, tzid: &str, zone: &dyn ZoneRules, from: i32, to: i32) { + let (from, to) = (from.clamp(2, 9_990), to.clamp(2, 9_990).max(from.clamp(2, 9_990))); + // A few hundred changes at most are looked at. + let to = to.min(from.saturating_add(120)); + let instant = |year: i32| NaiveDate::from_ymd_opt(year, 1, 1).map_or(0, |day| to_timestamp(at_midnight(day), 0)); + let before = zone.offset_at(instant(from - 1)); + // Each change as it is written: the wall-clock time by the offset before it. + let changes: Vec<(NaiveDateTime, i32, i32)> = zone + .transitions(instant(from - 1), instant(to + 1)) + .into_iter() + .map(|change| (from_civil_seconds(change.at.saturating_add(i64::from(change.before))), change.before, change.after)) + .collect(); + let midnight = |year: i32| NaiveDate::from_ymd_opt(year, 1, 1).map(at_midnight).unwrap_or_default(); + fold(out, "BEGIN:VTIMEZONE"); + // A name can't start another line. + let tzid: String = prefix(tzid, MAX_TZID).chars().filter(|c| !c.is_control()).collect(); + fold(out, &format!("TZID:{tzid}")); + if changes.is_empty() { + write_part(out, "STANDARD", midnight(1601), before, before, None); + fold(out, "END:VTIMEZONE"); + return; + } + // Clocks forward is the beginning of daylight time, clocks back the end of it. + let (forward, back): (Vec<_>, Vec<_>) = changes.iter().copied().partition(|(_, from, to)| to > from); + let years = usize::try_from(i64::from(to) - i64::from(from) + 2).unwrap_or(0); + let regular = forward.len() == years && back.len() == years; + match (yearly_rule(&forward).filter(|_| regular), yearly_rule(&back).filter(|_| regular)) { + (Some(spring), Some(autumn)) => { + for (kind, list, rule) in [("DAYLIGHT", &forward, spring), ("STANDARD", &back, autumn)] { + if let Some((start, from, to)) = list.first().copied() { + write_part(out, kind, start, from, to, Some(rule)); + } + } + } + _ => { + // What holds before the first change, then each change. + let first = changes.first().map_or(NaiveDateTime::default(), |(at, _, _)| *at); + write_part(out, "STANDARD", midnight(first.year().saturating_sub(1).max(1)), before, before, None); + for (start, from, to) in changes { + write_part(out, if to > from { "DAYLIGHT" } else { "STANDARD" }, start, from, to, None); + } + } + } + fold(out, "END:VTIMEZONE"); +} + +#[cfg(test)] +#[path = "zones_tests.rs"] +mod tests; diff --git a/crates/ical/src/zones_tests.rs b/crates/ical/src/zones_tests.rs new file mode 100644 index 000000000..5c1ec9eba --- /dev/null +++ b/crates/ical/src/zones_tests.rs @@ -0,0 +1,293 @@ +//! `VTIMEZONE` reading and writing. The zones written are rule zones built here (the United States' +//! rules, the European Union's, Sydney's, a zone without changes, the United States' change of +//! rule in 2007), standing in for a caller's time-zone database. + +use proptest::prelude::*; + +use super::*; + +fn d(y: i32, m: u32, day: u32) -> NaiveDate { + NaiveDate::from_ymd_opt(y, m, day).unwrap() +} + +fn at(y: i32, m: u32, day: u32, h: u32, min: u32) -> NaiveDateTime { + d(y, m, day).and_hms_opt(h, min, 0).unwrap() +} + +fn utc(t: NaiveDateTime) -> i64 { + to_timestamp(t, 0) +} + +/// A zone by its list of changes. +struct Rules { + before: i32, + changes: Vec, +} + +impl ZoneRules for Rules { + fn offset_at(&self, instant: i64) -> i32 { + self.changes.iter().rev().find(|c| c.at <= instant).map_or(self.before, |c| c.after) + } + + fn transitions(&self, from: i64, to: i64) -> Vec { + self.changes.iter().copied().filter(|c| c.at >= from && c.at < to).collect() + } +} + +/// The change on the `nth` `weekday` of `month` at `hour` UTC (`day_before`: the evening before). +fn change(year: i32, month: u32, nth: i8, hour: u32, day_before: bool, before: i32, after: i32) -> Transition { + let day = nth_weekday(year, month, Weekday::Sun, nth).unwrap(); + let day = if day_before { day.pred_opt().unwrap() } else { day }; + Transition { at: utc(day.and_hms_opt(hour, 0, 0).unwrap()), before, after } +} + +const H: i32 = 3600; + +/// The United States' rule since 2007 (and, with `history`, the one before it up to 2006). +fn us_eastern(years: std::ops::RangeInclusive, history: bool) -> Rules { + let mut changes = Vec::new(); + for y in years { + if history && y < 2007 { + changes.push(change(y, 4, 1, 7, false, -5 * H, -4 * H)); + changes.push(change(y, 10, -1, 6, false, -4 * H, -5 * H)); + } else { + changes.push(change(y, 3, 2, 7, false, -5 * H, -4 * H)); + changes.push(change(y, 11, 1, 6, false, -4 * H, -5 * H)); + } + } + Rules { before: -5 * H, changes } +} + +fn berlin(years: std::ops::RangeInclusive) -> Rules { + let changes = years.flat_map(|y| [change(y, 3, -1, 1, false, H, 2 * H), change(y, 10, -1, 1, false, 2 * H, H)]).collect(); + Rules { before: H, changes } +} + +fn sydney(years: std::ops::RangeInclusive) -> Rules { + let changes = years.flat_map(|y| [change(y, 4, 1, 16, true, 11 * H, 10 * H), change(y, 10, 1, 16, true, 10 * H, 11 * H)]).collect(); + Rules { before: 11 * H, changes } +} + +fn fixed(seconds: i32) -> Rules { + Rules { before: seconds, changes: Vec::new() } +} + +/// The zone as a big mail program writes it: today's rule, from the year 1601. +const EASTERN: &str = "BEGIN:VTIMEZONE\r\nTZID:Eastern Standard Time\r\nBEGIN:STANDARD\r\nDTSTART:16011104T020000\r\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11\r\n\ +TZOFFSETFROM:-0400\r\nTZOFFSETTO:-0500\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nDTSTART:16010311T020000\r\nRRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3\r\n\ +TZOFFSETFROM:-0500\r\nTZOFFSETTO:-0400\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\n"; + +fn only(text: &str) -> VTimezone { + let zones = TimeZones::collect(text); + assert_eq!(zones.iter().count(), 1, "{text}"); + zones.iter().next().unwrap().clone() +} + +#[test] +fn a_definition_gives_each_time_its_offset() { + let eastern = only(EASTERN); + assert_eq!(eastern.tzid(), "Eastern Standard Time"); + assert_eq!(eastern.offset_for_wall(at(2026, 1, 15, 9, 0)), -5 * 3600); + assert_eq!(eastern.offset_for_wall(at(2026, 7, 15, 9, 0)), -4 * 3600); + // March 8, 2026: 1:59 is winter time, 3:00 summer time, and 2:30 (which no clock shows) + // is still read by the offset before. + assert_eq!(eastern.offset_for_wall(at(2026, 3, 8, 1, 59)), -5 * 3600); + assert_eq!(eastern.offset_for_wall(at(2026, 3, 8, 2, 30)), -5 * 3600); + assert_eq!(eastern.offset_for_wall(at(2026, 3, 8, 3, 0)), -4 * 3600); + // November 1: 1:30 comes twice, and is the first of the two. + assert_eq!(eastern.offset_for_wall(at(2026, 11, 1, 1, 30)), -4 * 3600); + assert_eq!(eastern.offset_for_wall(at(2026, 11, 1, 2, 0)), -5 * 3600); + // By instants, every half hour of two years agrees with the rules. + let rules = us_eastern(2025..=2028, false); + let mut wall = at(2026, 1, 1, 0, 0); + while wall < at(2028, 1, 1, 0, 0) { + let instant = utc(wall); + assert_eq!(eastern.offset_at(instant), rules.offset_at(instant), "{wall}"); + wall += chrono::Duration::minutes(30); + } + assert_eq!(eastern.instant(at(2026, 7, 15, 9, 0)), utc(at(2026, 7, 15, 13, 0))); + // Before there were any rules: what the first change changes from. + assert_eq!(eastern.offset_for_wall(at(1500, 6, 1, 0, 0)), -5 * 3600); +} + +#[test] +fn definitions_of_every_shape_are_read() { + // Listed dates, a rule that ends, a rule by the day of the month, a part with no rule. + let text = "BEGIN:VTIMEZONE\nTZID:Example/Odd\nX-LIC-LOCATION:Europe/Berlin\n\ +BEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\n\ +BEGIN:DAYLIGHT\nDTSTART:20200329T020000\nRDATE:20210328T020000,20220327T020000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0200\nEND:DAYLIGHT\n\ +BEGIN:STANDARD\nDTSTART:20201025T030000\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU;UNTIL=20221030T010000Z\nTZOFFSETFROM:+0200\nTZOFFSETTO:+0100\nEND:STANDARD\n\ +BEGIN:DAYLIGHT\nDTSTART:20240401T000000\nRRULE:FREQ=YEARLY;BYMONTHDAY=1;BYMONTH=4;COUNT=2\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0130\nEND:DAYLIGHT\n\ +BEGIN:STANDARD\nDTSTART:20241001T000000\nRRULE:FREQ=YEARLY\nTZOFFSETFROM:+0130\nTZOFFSETTO:+0100\nEND:STANDARD\n\ +END:VTIMEZONE\n"; + let odd = only(text); + let offset = |wall: NaiveDateTime| odd.offset_for_wall(wall) / 60; + assert_eq!(offset(at(2019, 7, 1, 12, 0)), 60); + assert_eq!(offset(at(2020, 7, 1, 12, 0)), 120); + assert_eq!(offset(at(2021, 1, 1, 12, 0)), 60); + assert_eq!(offset(at(2022, 7, 1, 12, 0)), 120); + assert_eq!(offset(at(2023, 7, 1, 12, 0)), 60); + // Two Aprils of half an hour, and no third. + assert_eq!(offset(at(2024, 7, 1, 12, 0)), 90); + assert_eq!(offset(at(2025, 7, 1, 12, 0)), 90); + assert_eq!(offset(at(2025, 12, 1, 12, 0)), 60); + assert_eq!(offset(at(2026, 7, 1, 12, 0)), 60); + // It says where it is, and is found by its name in any letter case. + let zones = TimeZones::collect(text); + assert_eq!(zones.get("Example/Odd").map(VTimezone::place), Some("Europe/Berlin")); + assert!(zones.get("example/odd").is_some()); + assert!(zones.get("Example/Even").is_none()); +} + +#[test] +fn nonsense_definitions_are_no_definitions() { + for text in [ + "BEGIN:VTIMEZONE\nEND:VTIMEZONE\n", + "BEGIN:VTIMEZONE\nTZID:Empty\nEND:VTIMEZONE\n", + "BEGIN:VTIMEZONE\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n", + // Offsets of days, and no offsets at all. + "BEGIN:VTIMEZONE\nTZID:Far\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+9900\nTZOFFSETTO:-7200\nEND:STANDARD\nEND:VTIMEZONE\n", + "BEGIN:VTIMEZONE\nTZID:Far\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+1801\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n", + "BEGIN:VTIMEZONE\nTZID:None\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:soon\nTZOFFSETTO:later\nEND:STANDARD\nEND:VTIMEZONE\n", + "BEGIN:VTIMEZONE\nTZID:NoStart\nBEGIN:STANDARD\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n", + // A part outside a zone, and a zone that never ends. + "BEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\n", + "BEGIN:VTIMEZONE\nTZID:Open\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\n", + ] { + assert!(TimeZones::collect(text).is_empty(), "{text}"); + } + assert_eq!(parse_utc_offset("+053000"), Some(5 * 3600 + 30 * 60)); + assert_eq!(parse_utc_offset("-00:30"), Some(-30 * 60)); + for bad in ["", "0100", "+1", "+01", "+0160", "+010060", "+2400", "+١٢٠٠", "++0100"] { + assert_eq!(parse_utc_offset(bad), None, "{bad}"); + } + assert_eq!((format_utc_offset(-570 * 60), format_utc_offset(0), format_utc_offset(16_562)), ("-0930".into(), "+0000".into(), "+043602".into())); + // A megabyte of name, hundreds of zones, thousands of parts and dates: all bounded. + let long = format!( + "BEGIN:VTIMEZONE\nTZID:{}\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n", + "Z".repeat(1 << 20) + ); + let zones = TimeZones::collect(&long); + assert!(only(&long).tzid().len() <= MAX_TZID); + assert!(zones.get(&"Z".repeat(1 << 20)).is_some()); + let many: String = (0..500) + .map(|n| format!("BEGIN:VTIMEZONE\nTZID:Zone{n}\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\nEND:VTIMEZONE\n")) + .collect(); + assert_eq!(TimeZones::collect(&many).iter().count(), MAX_ZONES); + let part = format!( + "BEGIN:STANDARD\nDTSTART:19700101T000000\nRDATE:{}\nTZOFFSETFROM:+0100\nTZOFFSETTO:+0100\nEND:STANDARD\n", + "19800101T000000,".repeat(5_000) + ); + let crowded = only(&format!("BEGIN:VTIMEZONE\nTZID:Crowded\n{}END:VTIMEZONE\n", part.repeat(1_000))); + assert_eq!((crowded.parts.len(), crowded.parts[0].dates.len()), (MAX_PARTS, MAX_DATES)); + assert_eq!(crowded.cost(), (MAX_PARTS * (8 + MAX_DATES)) as u64); + let _ = crowded.offset_for_wall(at(2026, 1, 1, 0, 0)); + // Hostile times ask nothing that panics. + let eastern = only(EASTERN); + for wall in [NaiveDateTime::MAX, NaiveDateTime::MIN, NaiveDateTime::default()] { + let _ = (eastern.offset_for_wall(wall), eastern.instant(wall)); + } + let _ = (eastern.offset_at(i64::MIN), eastern.offset_at(i64::MAX)); +} + +#[test] +fn the_budget_runs_out_for_good() { + let zones = TimeZones::collect(EASTERN); + assert_eq!(zones.stopped_after(), None); + assert!(zones.spend(WORK_BUDGET - 10)); + assert!(zones.spend(10)); + assert!(!zones.spend(1)); + // Once out, out: even a spend of nothing is refused. + assert!(!zones.spend(0)); + assert_eq!(zones.stopped_after(), Some(2)); +} + +/// What a written definition says, read back. +fn written(name: &str, zone: &dyn ZoneRules, from: i32, to: i32) -> (String, VTimezone) { + let mut out = String::new(); + write_vtimezone(&mut out, name, zone, from, to); + let read = only(&out); + (out, read) +} + +#[test] +fn written_definitions_are_the_usual_two_rules_where_a_zone_has_them() { + let (text, _) = written("America/New_York", &us_eastern(2025..=2027, false), 2026, 2026); + assert_eq!( + text.replace("\r\n", "\n"), + "BEGIN:VTIMEZONE\nTZID:America/New_York\n\ +BEGIN:DAYLIGHT\nDTSTART:20250309T020000\nTZOFFSETFROM:-0500\nTZOFFSETTO:-0400\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU\nEND:DAYLIGHT\n\ +BEGIN:STANDARD\nDTSTART:20251102T020000\nTZOFFSETFROM:-0400\nTZOFFSETTO:-0500\nRRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU\nEND:STANDARD\n\ +END:VTIMEZONE\n" + ); + let (text, _) = written("Europe/Berlin", &berlin(2025..=2031), 2026, 2030); + assert!(text.contains("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU") && text.contains("RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU"), "{text}"); + assert!(text.contains("DTSTART:20250330T020000") && text.contains("DTSTART:20251026T030000"), "{text}"); + // The south: summer time begins in October. + let (text, _) = written("Australia/Sydney", &sydney(2025..=2027), 2026, 2026); + assert!(text.contains("RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=1SU") && text.contains("RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1SU"), "{text}"); + // No changes: one part. An offset is a zone too. + let (text, _) = written("Asia/Kolkata", &fixed(5 * H + 1800), 2026, 2026); + assert_eq!( + text.replace("\r\n", "\n"), + "BEGIN:VTIMEZONE\nTZID:Asia/Kolkata\nBEGIN:STANDARD\nDTSTART:16010101T000000\nTZOFFSETFROM:+0530\nTZOFFSETTO:+0530\nEND:STANDARD\nEND:VTIMEZONE\n" + ); + let (text, _) = written("UTC-09:30", &fixed(-570 * 60), 2026, 2026); + assert!(text.contains("TZID:UTC-09:30") && text.contains("TZOFFSETTO:-0930"), "{text}"); + // The year the United States changed its rule: written out change by change. + let history = us_eastern(2004..=2009, true); + let (text, read) = written("America/New_York", &history, 2006, 2007); + assert!(!text.contains("RRULE") && text.contains("DTSTART:20060402T020000") && text.contains("DTSTART:20070311T020000"), "{text}"); + assert_eq!(read.instant(at(2006, 3, 20, 9, 0)), utc(at(2006, 3, 20, 14, 0))); + assert_eq!(read.instant(at(2007, 3, 20, 9, 0)), utc(at(2007, 3, 20, 13, 0))); + // Hostile years are years, and a hostile name can't start a line. + for (from, to) in [(i32::MIN, i32::MAX), (9_999, 1), (0, 0)] { + let _ = written("Europe/Berlin", &berlin(2020..=2030), from, to); + } + let (text, read) = written("Evil\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT", &fixed(0), 2026, 2026); + assert_eq!(read.tzid(), "EvilEND:VTIMEZONEBEGIN:VEVENT"); + assert_eq!(text.matches("VTIMEZONE").count(), 3, "{text}"); +} + +fn any_zone() -> impl Strategy { + (0usize..6, -12 * 60i32..=14 * 60) +} + +fn zone_of(which: usize, minutes: i32) -> Rules { + match which { + 0 => us_eastern(1960..=2100, false), + 1 => us_eastern(1960..=2100, true), + 2 => berlin(1960..=2100), + 3 => sydney(1960..=2100), + 4 => fixed(minutes * 60), + _ => fixed(0), + } +} + +proptest! { + #[test] + fn a_written_definition_reads_as_the_zone_it_was_written_for((which, minutes_east) in any_zone(), year in 1971i32..2090, span in 0i32..4, minutes in 0i64..365 * 24 * 60) { + let zone = zone_of(which, minutes_east); + let (text, read) = written("Example/Zone", &zone, year, year + span); + // Any time of those years is the instant the rules say. + let wall = at(year, 1, 1, 0, 0) + chrono::Duration::minutes(minutes) + chrono::Duration::days(i64::from(span) * 365); + let instant = utc(wall); + prop_assert_eq!(read.offset_at(instant), zone.offset_at(instant), "{} at {}\n{}", which, wall, text); + // And a wall-clock time is the instant it shows, where the clock shows it once. + let wall_instant = read.instant(wall); + if read.offset_at(wall_instant) == zone.offset_at(wall_instant) && zone.offset_at(wall_instant - 2 * i64::from(H)) == zone.offset_at(wall_instant + 2 * i64::from(H)) { + prop_assert_eq!(wall_instant + i64::from(zone.offset_at(wall_instant)), civil_seconds(wall)); + } + } + + #[test] + fn any_text_is_definitions_or_none(text in proptest::prelude::any::(), parts in proptest::collection::vec("(BEGIN|END|DTSTART|TZOFFSETFROM|TZOFFSETTO|RRULE|RDATE|TZID):[-+A-Z0-9=;,]{0,24}", 0..40), wall in proptest::num::i64::ANY) { + let _ = TimeZones::collect(&text); + let zones = TimeZones::collect(&format!("BEGIN:VTIMEZONE\nTZID:X\n{}\nEND:VTIMEZONE\n", parts.join("\n"))); + let wall = from_civil_seconds(wall); + if let Some(definition) = zones.get("X") { + let _ = (definition.offset_for_wall(wall), definition.offset_at(utc(wall)), definition.instant(wall)); + } + let _ = zones.get(&text); + } +} From fa26a2a4c6732ded9f86c411ad7158e159a854fe Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:30:55 -0500 Subject: [PATCH 056/120] Add craft-pdn: Paint.NET PDN3 reader and a data-only MS-NRBF reader, from PhotoCraft (#968) The format half of PhotoCraft's Paint.NET import (crates/io/src/pdn.rs and pdn/nrbf.rs at abeb16c96): the PDN3 header, the .NET binary serialization graph, the layer list and blend modes, and the chunked gzip pixel blocks. Surfaces, blend-mode mapping and warnings stay in the app. --- Cargo.lock | 9 + Cargo.toml | 1 + README.md | 1 + crates/pdn/Cargo.toml | 18 ++ crates/pdn/README.md | 33 +++ crates/pdn/src/lib.rs | 381 ++++++++++++++++++++++++++++++++ crates/pdn/src/nrbf.rs | 473 ++++++++++++++++++++++++++++++++++++++++ crates/pdn/src/tests.rs | 444 +++++++++++++++++++++++++++++++++++++ 8 files changed, 1360 insertions(+) create mode 100644 crates/pdn/Cargo.toml create mode 100644 crates/pdn/README.md create mode 100644 crates/pdn/src/lib.rs create mode 100644 crates/pdn/src/nrbf.rs create mode 100644 crates/pdn/src/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 857ed807c..7b227c217 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1409,6 +1409,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "craft-pdn" +version = "0.1.0" +dependencies = [ + "flate2", + "proptest", + "thiserror 2.0.21", +] + [[package]] name = "craft-penpot" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index d7a54cda3..6b5a23328 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ craft-aep = { path = "crates/aep" } craft-affinity = { path = "crates/affinity" } craft-ai-core = { path = "crates/ai-core" } craft-pdf = { path = "crates/pdf", version = "=0.1.0" } +craft-pdn = { path = "crates/pdn" } craft-plate-test = { path = "crates/plate-test" } krilla = { version = "=0.8.2", default-features = false, features = ["raster-images"] } hayro = { version = "=0.7.1", default-features = false } diff --git a/README.md b/README.md index 678bf7f1c..33f37171c 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " = std::result::Result; + +fn invalid(message: impl Into) -> Error { + Error::Invalid(message.into()) +} + +fn check(keep_going: &dyn Fn() -> bool) -> Result<()> { + if keep_going() { Ok(()) } else { Err(Error::Cancelled) } +} + +/// The canvas, and all pixel blocks together, may hold at most this many bytes. +pub const MAX_PIXEL_BYTES: usize = 1 << 30; +/// Layers in one document. +pub const MAX_LAYERS: usize = 1024; +/// Longest layer name, in bytes. +pub const MAX_NAME_BYTES: usize = 64 * 1024; +/// Most bytes of layer names in one document. The graph holds a shared string once; every layer +/// that refers to it gets its own copy. +pub const MAX_TOTAL_NAME_BYTES: usize = 4 * 1024 * 1024; + +/// `true` for a PDN3 document (by its signature). +pub fn is_pdn(bytes: &[u8]) -> bool { + bytes.starts_with(b"PDN3") +} + +/// A layer's blend mode: Paint.NET's `LayerBlendMode`, in its numeric order. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum BlendMode { + Normal, + Multiply, + Additive, + /// Paint.NET's own formula, not Photoshop's Color Burn. + ColorBurn, + /// Paint.NET's own formula, not Photoshop's Color Dodge. + ColorDodge, + Reflect, + Glow, + Overlay, + Difference, + Negation, + Lighten, + Darken, + Screen, + Xor, +} + +impl BlendMode { + /// Every mode, indexed by its `LayerBlendMode` value. + pub const ALL: [BlendMode; 14] = [ + BlendMode::Normal, + BlendMode::Multiply, + BlendMode::Additive, + BlendMode::ColorBurn, + BlendMode::ColorDodge, + BlendMode::Reflect, + BlendMode::Glow, + BlendMode::Overlay, + BlendMode::Difference, + BlendMode::Negation, + BlendMode::Lighten, + BlendMode::Darken, + BlendMode::Screen, + BlendMode::Xor, + ]; + + /// The name older files use in `PaintDotNet.UserBlendOps+BlendOp`. + pub fn name(self) -> &'static str { + match self { + BlendMode::Normal => "Normal", + BlendMode::Multiply => "Multiply", + BlendMode::Additive => "Additive", + BlendMode::ColorBurn => "ColorBurn", + BlendMode::ColorDodge => "ColorDodge", + BlendMode::Reflect => "Reflect", + BlendMode::Glow => "Glow", + BlendMode::Overlay => "Overlay", + BlendMode::Difference => "Difference", + BlendMode::Negation => "Negation", + BlendMode::Lighten => "Lighten", + BlendMode::Darken => "Darken", + BlendMode::Screen => "Screen", + BlendMode::Xor => "Xor", + } + } +} + +/// A bitmap layer, bottom first in [`Document::layers`]. Every layer is the size of the canvas. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Layer { + pub name: String, + pub visible: bool, + /// 0 to 255. + pub opacity: u8, + pub blend: BlendMode, + /// Which surface [`Document::read_surfaces`] hands out holds this layer's pixels. Layers may + /// share one. + pub surface: usize, +} + +/// One decoded pixel block: `height` rows of `stride` bytes, each starting with `width` pixels +/// of B, G, R, A bytes (straight alpha). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Surface { + pub width: usize, + pub stride: usize, + pub data: Vec, +} + +impl Surface { + /// The rows, top first, each cut to its `width × 4` pixel bytes (B, G, R, A). + pub fn rows(&self) -> impl Iterator { + self.data.chunks_exact(self.stride.max(1)).filter_map(|row| row.get(..self.width * 4)) + } +} + +struct Block { + length: usize, + stride: usize, +} + +/// The structure of a PDN3 document. Parse first, then read the pixels. +pub struct Document<'a> { + /// Canvas size in pixels. + pub width: usize, + pub height: usize, + /// The layers, bottom first. + pub layers: Vec, + /// The document carries metadata items (`userMetadataItems`); they are not read. + pub has_user_metadata: bool, + bytes: &'a [u8], + /// Where the pixel blocks start. + pixels_at: usize, + blocks: Vec, +} + +fn count(graph: &Graph, object: &Value, field: &str) -> Result { + usize::try_from(graph.integer(graph.field(object, field)?)?).map_err(|_| invalid(format!("invalid {field}"))) +} + +fn blend(graph: &Graph, layer: &Value, props: &Value) -> Result { + if let Some(mode) = graph.optional_field(props, "blendMode")? { + let mode = match graph.resolve(mode)? { + Value::Int(n) => *n, + _ => graph.integer(graph.field(mode, "value__")?)?, + }; + let mode = usize::try_from(mode).map_err(|_| invalid("invalid blend mode"))?; + return BlendMode::ALL.get(mode).copied().ok_or_else(|| invalid(format!("unsupported blend mode {mode}"))); + } + let props = graph.field(layer, "properties")?; + let mode = graph.class_name(graph.field(props, "blendOp")?)?; + let mode = mode + .strip_prefix("PaintDotNet.UserBlendOps+") + .and_then(|s| s.strip_suffix("BlendOp")) + .ok_or_else(|| invalid(format!("unsupported blend operation {mode}")))?; + BlendMode::ALL.iter().copied().find(|m| m.name() == mode).ok_or_else(|| invalid(format!("unsupported blend operation {mode}"))) +} + +impl<'a> Document<'a> { + /// Reads the header and the serialized document. `keep_going` is asked throughout; `false` + /// ends the read with [`Error::Cancelled`]. + pub fn parse(bytes: &'a [u8], keep_going: &dyn Fn() -> bool) -> Result> { + let mut reader = Reader { bytes, pos: 0 }; + if reader.take(4)? != b"PDN3" { + return Err(invalid("unsupported document signature (expected PDN3)")); + } + let header_size = match reader.take(3)? { + [a, b, c] => u32::from_le_bytes([*a, *b, *c, 0]) as usize, + _ => return Err(invalid("invalid header")), + }; + reader.take(header_size)?; + if reader.take(2)? != [0, 1] { + return Err(invalid("unsupported PDN3 serialization version")); + } + let graph = nrbf::parse_from(&mut reader, keep_going)?; + let root = &graph.root; + if graph.class_name(root)? != "PaintDotNet.Document" { + return Err(invalid("root object is not a Paint.NET document")); + } + let width = count(&graph, root, "width")?; + let height = count(&graph, root, "height")?; + let row_bytes = width.checked_mul(4).ok_or_else(|| invalid("canvas size overflow"))?; + let canvas_bytes = row_bytes.checked_mul(height).ok_or_else(|| invalid("canvas size overflow"))?; + if width == 0 || height == 0 || width > i32::MAX as usize || height > i32::MAX as usize || canvas_bytes > MAX_PIXEL_BYTES { + return Err(invalid("canvas is empty or exceeds the 1 GiB pixel limit")); + } + let list = graph.field(root, "layers")?; + let items = graph.array(graph.field(list, "ArrayList+_items")?)?; + let len = count(&graph, list, "ArrayList+_size")?; + if len == 0 || len > MAX_LAYERS { + return Err(invalid("document must have 1..1024 bitmap layers")); + } + let items = items.get(..len).ok_or_else(|| invalid("layer list count exceeds its array"))?; + // Memory block id to the stride of the surfaces that use it. + let mut layouts = HashMap::new(); + let mut layers = Vec::with_capacity(len); + let mut name_bytes = 0usize; + for layer in items { + check(keep_going)?; + if graph.class_name(layer)? != "PaintDotNet.BitmapLayer" { + return Err(invalid("only PDN3 bitmap layers are supported")); + } + if count(&graph, layer, "Layer+width")? != width || count(&graph, layer, "Layer+height")? != height { + return Err(invalid("layer dimensions do not match the canvas")); + } + let surface = graph.field(layer, "surface")?; + if graph.class_name(surface)? != "PaintDotNet.Surface" + || count(&graph, surface, "width")? != width + || count(&graph, surface, "height")? != height + { + return Err(invalid("invalid bitmap surface")); + } + let stride = count(&graph, surface, "stride")?; + if stride < row_bytes { + return Err(invalid("surface stride is too short for BGRA32 pixels")); + } + let Value::Ref(id) = graph.field(surface, "scan0")? else { return Err(invalid("missing pixel memory reference")) }; + if let Some(old) = layouts.insert(*id, stride) + && old != stride + { + return Err(invalid("shared pixel memory has inconsistent strides")); + } + let props = graph.field(layer, "Layer+properties")?; + let opacity = u8::try_from(count(&graph, props, "opacity")?).map_err(|_| invalid("opacity exceeds 255"))?; + let name = graph.string(graph.field(props, "name")?)?; + if name.len() > MAX_NAME_BYTES { + return Err(invalid("layer name is too long")); + } + name_bytes = name_bytes.saturating_add(name.len()); + if name_bytes > MAX_TOTAL_NAME_BYTES { + return Err(invalid("layer names exceed the total metadata budget")); + } + let visible = graph.boolean(graph.field(props, "visible")?)?; + layers.push((*id, Layer { name: name.to_string(), visible, opacity, blend: blend(&graph, layer, props)?, surface: 0 })); + } + // Validate every allocation before any block is decoded. + let mut blocks = Vec::new(); + let mut index_of = HashMap::new(); + let mut total = 0usize; + for (id, block) in graph.objects_of_class("PaintDotNet.MemoryBlock") { + let length = count(&graph, block, "length64")?; + let stride = *layouts.get(&id).ok_or_else(|| invalid("unreferenced pixel memory block"))?; + if length != stride.checked_mul(height).ok_or_else(|| invalid("pixel length overflow"))? { + return Err(invalid("pixel memory length does not match the surface")); + } + if graph.boolean(graph.field(block, "hasParent")?)? || !graph.boolean(graph.field(block, "deferred")?)? { + return Err(invalid("unsupported non-deferred or parented pixel memory")); + } + total = total.checked_add(length).ok_or_else(|| invalid("pixel length overflow"))?; + if total > MAX_PIXEL_BYTES { + return Err(invalid("decoded pixels exceed the 1 GiB limit")); + } + index_of.insert(id, blocks.len()); + blocks.push(Block { length, stride }); + } + let layers = layers + .into_iter() + .map(|(id, layer)| Ok(Layer { surface: *index_of.get(&id).ok_or_else(|| invalid("missing pixel memory block"))?, ..layer })) + .collect::>>()?; + let has_user_metadata = match graph.optional_field(root, "userMetadataItems")? { + Some(meta) if !matches!(graph.resolve(meta)?, Value::Null) => !graph.array(meta)?.is_empty(), + _ => false, + }; + Ok(Document { width, height, layers, has_user_metadata, bytes, pixels_at: reader.pos, blocks }) + } + + /// How many surfaces [`Self::read_surfaces`] hands out. + pub fn surface_count(&self) -> usize { + self.blocks.len() + } + + /// Bytes all surfaces take once decoded: at most [`MAX_PIXEL_BYTES`]. + pub fn decoded_bytes(&self) -> usize { + self.blocks.iter().map(|b| b.length).sum() + } + + /// Decodes the pixel blocks in file order and hands each to `each` with its index (what + /// [`Layer::surface`] names). Only one decoded surface is held at a time unless `each` keeps + /// them. Data after the last block is an error. + pub fn read_surfaces(&self, keep_going: &dyn Fn() -> bool, each: &mut dyn FnMut(usize, Surface)) -> Result<()> { + let mut reader = Reader { bytes: self.bytes, pos: self.pixels_at }; + for (i, block) in self.blocks.iter().enumerate() { + let data = pixels(&mut reader, block.length, keep_going)?; + each(i, Surface { width: self.width, stride: block.stride, data }); + } + if reader.pos != self.bytes.len() { + return Err(invalid("unexpected trailing data")); + } + Ok(()) + } +} + +/// Reads a memory block's numbered chunks in their on-disk order; each chunk has its own gzip +/// stream and checksum. Inflates directly into the bounded destination, checking its exact size. +fn pixels(reader: &mut Reader<'_>, length: usize, keep_going: &dyn Fn() -> bool) -> Result> { + let version = reader.byte()?; + if version > 1 { + return Err(invalid(format!("unsupported pixel compression {version}"))); + } + let chunk_size = reader.be_u32()?; + if chunk_size == 0 { + return Err(invalid("zero pixel chunk size")); + } + let chunks = length.div_ceil(chunk_size); + if chunks > 1_000_000 { + return Err(invalid("too many pixel chunks")); + } + // Uncompressed chunks hold every pixel byte: a file shorter than that is truncated, and is + // rejected before the buffer is allocated. + if version == 1 && reader.bytes.len().saturating_sub(reader.pos) < length { + return Err(invalid("uncompressed pixel data is truncated")); + } + let mut seen = vec![false; chunks]; + let mut data = vec![0; length]; + for _ in 0..chunks { + check(keep_going)?; + let index = reader.be_u32()?; + let found = seen.get_mut(index).ok_or_else(|| invalid("pixel chunk index out of bounds"))?; + if *found { + return Err(invalid("duplicate pixel chunk")); + } + *found = true; + let size = reader.be_u32()?; + let raw = reader.take(size)?; + let start = index.checked_mul(chunk_size).ok_or_else(|| invalid("pixel offset overflow"))?; + let end = start.checked_add(chunk_size.min(length.saturating_sub(start))).ok_or_else(|| invalid("pixel offset overflow"))?; + let dst = data.get_mut(start..end).ok_or_else(|| invalid("invalid pixel chunk range"))?; + if version == 0 { + let mut decoder = flate2::bufread::GzDecoder::new(raw); + for band in dst.chunks_mut(1 << 20) { + check(keep_going)?; + decoder.read_exact(band).map_err(|e| invalid(format!("invalid gzip pixel chunk: {e}")))?; + } + let mut extra = [0]; + if decoder.read(&mut extra).map_err(|e| invalid(format!("invalid gzip pixel checksum: {e}")))? != 0 || !decoder.into_inner().is_empty() { + return Err(invalid("pixel chunk has excess data")); + } + } else { + if raw.len() != dst.len() { + return Err(invalid("incorrect uncompressed pixel chunk size")); + } + dst.copy_from_slice(raw); + } + } + Ok(data) +} diff --git a/crates/pdn/src/nrbf.rs b/crates/pdn/src/nrbf.rs new file mode 100644 index 000000000..ff06b7b6b --- /dev/null +++ b/crates/pdn/src/nrbf.rs @@ -0,0 +1,473 @@ +//! The data-only subset of the .NET Remoting Binary Format (MS-NRBF) that PDN3 uses, from +//! Microsoft's public specification +//! (). The stream becomes +//! a [`Graph`] of [`Value`]s; references stay as ids and no .NET type is instantiated or run. +//! +//! Limits: [`MAX_ITEMS`] records and members, [`MAX_STRINGS`] bytes of strings, [`MAX_DEPTH`] +//! levels of nesting and of reference chains. + +use std::collections::HashMap; +use std::sync::Arc; + +use crate::{Error, Result, invalid}; + +/// Most records, array items and class members in one stream. +pub const MAX_ITEMS: usize = 1_000_000; +/// Most bytes of strings in one stream. +pub const MAX_STRINGS: usize = 64 << 20; +/// Deepest nesting of records, and longest chain of references [`Graph::resolve`] follows. +pub const MAX_DEPTH: usize = 64; + +pub(crate) struct Reader<'a> { + pub bytes: &'a [u8], + pub pos: usize, +} + +impl<'a> Reader<'a> { + pub fn take(&mut self, len: usize) -> Result<&'a [u8]> { + let end = self.pos.checked_add(len).ok_or_else(|| invalid("length overflow"))?; + let b = self.bytes.get(self.pos..end).ok_or_else(|| invalid("truncated document"))?; + self.pos = end; + Ok(b) + } + + pub fn byte(&mut self) -> Result { + Ok(self.take(1)?.first().copied().unwrap_or_default()) + } + + pub fn le_i32(&mut self) -> Result { + Ok(i32::from_le_bytes(self.take(4)?.try_into().map_err(|_| invalid("invalid integer"))?)) + } + + pub fn be_u32(&mut self) -> Result { + usize::try_from(u32::from_be_bytes(self.take(4)?.try_into().map_err(|_| invalid("invalid integer"))?)).map_err(|_| invalid("length overflow")) + } + + fn count(&mut self) -> Result { + let n = usize::try_from(self.le_i32()?).map_err(|_| invalid("negative count"))?; + if n > MAX_ITEMS { + return Err(invalid("serialized array or class is too large")); + } + Ok(n) + } +} + +/// A serialized class: its name and its member names, in order. +#[derive(Debug)] +pub struct Class { + name: String, + names: Vec, + primitives: Vec>, +} + +impl Class { + /// The class name as serialized (`PaintDotNet.Document`). + pub fn name(&self) -> &str { + &self.name + } + /// The member names, in the order [`Value::Object`] holds their values. + pub fn member_names(&self) -> &[String] { + &self.names + } +} + +/// A value of the graph. +#[derive(Debug)] +pub enum Value { + /// A null reference, and every primitive this reader skips (doubles, decimals, time spans). + Null, + Bool(bool), + /// Every integer type, widened. An unsigned 64-bit value and a date keep their bit pattern. + Int(i64), + /// A string or a single character. + String(String), + /// A reference to the object with this id; [`Graph::resolve`] follows it. + Ref(i32), + /// An array's items. A byte array's content is not kept: its value stays [`Value::Null`]. + Array(Vec), + Object { + class: Arc, + members: Vec, + }, +} + +/// The objects of a stream, by id. +pub struct Graph { + /// A reference to the root object. + pub root: Value, + objects: HashMap, + order: Vec, +} + +impl Graph { + /// Every object of the class `name` with its id, in stream order. + pub fn objects_of_class<'a>(&'a self, name: &'a str) -> impl Iterator { + self.order + .iter() + .filter_map(|id| self.objects.get(id).map(|v| (*id, v))) + .filter(move |(_, v)| matches!(v, Value::Object { class, .. } if class.name == name)) + } + /// Follows references to the value they name. + pub fn resolve<'a>(&'a self, mut value: &'a Value) -> Result<&'a Value> { + for _ in 0..MAX_DEPTH { + match value { + Value::Ref(id) => value = self.objects.get(id).ok_or_else(|| invalid("missing object reference"))?, + _ => return Ok(value), + } + } + Err(invalid("cyclic or excessively deep object reference")) + } + + /// The class name of an object. + pub fn class_name<'a>(&'a self, value: &'a Value) -> Result<&'a str> { + match self.resolve(value)? { + Value::Object { class, .. } => Ok(&class.name), + _ => Err(invalid("expected a serialized object")), + } + } + + /// A member of an object by name; an error when the class has none of that name. + pub fn field<'a>(&'a self, value: &'a Value, name: &str) -> Result<&'a Value> { + self.optional_field(value, name)?.ok_or_else(|| invalid(format!("missing field {name}"))) + } + + /// A member of an object by name; `None` when the class has none of that name. + pub fn optional_field<'a>(&'a self, value: &'a Value, name: &str) -> Result> { + match self.resolve(value)? { + Value::Object { class, members } => Ok(class.names.iter().position(|n| n == name).and_then(|i| members.get(i))), + _ => Err(invalid("expected a serialized object")), + } + } + + /// An integer value. + pub fn integer(&self, value: &Value) -> Result { + match self.resolve(value)? { + Value::Int(n) => Ok(*n), + _ => Err(invalid("expected an integer")), + } + } + + /// A boolean value. + pub fn boolean(&self, value: &Value) -> Result { + match self.resolve(value)? { + Value::Bool(b) => Ok(*b), + _ => Err(invalid("expected a boolean")), + } + } + + /// A string value. + pub fn string<'a>(&'a self, value: &'a Value) -> Result<&'a str> { + match self.resolve(value)? { + Value::String(s) => Ok(s), + _ => Err(invalid("expected a string")), + } + } + + /// An array's items. + pub fn array<'a>(&'a self, value: &'a Value) -> Result<&'a [Value]> { + match self.resolve(value)? { + Value::Array(a) => Ok(a), + _ => Err(invalid("expected an array")), + } + } +} + +struct Parser<'a, 'b> { + reader: &'b mut Reader<'a>, + keep_going: &'b dyn Fn() -> bool, + graph: Graph, + classes: HashMap>, + items: usize, + strings: usize, +} + +/// Reads a stream from its header record to its end record. Returns the graph and the number of +/// bytes the stream took, so whatever follows it can be read. `keep_going` is asked at every +/// record and array item; `false` ends the read with [`Error::Cancelled`]. +pub fn parse(bytes: &[u8], keep_going: &dyn Fn() -> bool) -> Result<(Graph, usize)> { + let mut reader = Reader { bytes, pos: 0 }; + let graph = parse_from(&mut reader, keep_going)?; + Ok((graph, reader.pos)) +} + +pub(crate) fn parse_from(reader: &mut Reader<'_>, keep_going: &dyn Fn() -> bool) -> Result { + if reader.byte()? != 0 { + return Err(invalid("missing NRBF stream header")); + } + let root = reader.le_i32()?; + let _header = reader.le_i32()?; + if root == 0 || reader.le_i32()? != 1 || reader.le_i32()? != 0 { + return Err(invalid("unsupported NRBF stream version")); + } + let mut parser = Parser { + reader, + keep_going, + graph: Graph { root: Value::Ref(root), objects: HashMap::new(), order: Vec::new() }, + classes: HashMap::new(), + items: 0, + strings: 0, + }; + loop { + let tag = parser.reader.byte()?; + if tag == 11 { + break; + } + parser.record(tag, 0)?; + } + parser.graph.resolve(&parser.graph.root)?; + Ok(parser.graph) +} + +impl Parser<'_, '_> { + fn check(&self) -> Result<()> { + if (self.keep_going)() { Ok(()) } else { Err(Error::Cancelled) } + } + + fn budget(&mut self, n: usize) -> Result<()> { + self.items = self.items.checked_add(n).ok_or_else(|| invalid("object count overflow"))?; + if self.items > MAX_ITEMS { + return Err(invalid("too many serialized objects or members")); + } + Ok(()) + } + + fn string(&mut self) -> Result { + let mut len = 0usize; + for shift in (0..35).step_by(7) { + let b = self.reader.byte()?; + if shift == 28 && b > 7 { + return Err(invalid("invalid string length")); + } + len |= usize::from(b & 127) << shift; + if b & 128 == 0 { + self.strings = self.strings.checked_add(len).ok_or_else(|| invalid("string length overflow"))?; + if self.strings > MAX_STRINGS { + return Err(invalid("serialized strings are too large")); + } + return String::from_utf8(self.reader.take(len)?.to_vec()).map_err(|_| invalid("invalid UTF-8 string")); + } + } + Err(invalid("invalid string length")) + } + + fn primitive(&mut self, tag: u8) -> Result { + macro_rules! number { + ($ty:ty, $variant:ident) => { + Value::$variant( + <$ty>::from_le_bytes(self.reader.take(size_of::<$ty>())?.try_into().map_err(|_| invalid("invalid primitive"))?).into(), + ) + }; + } + Ok(match tag { + 1 => match self.reader.byte()? { + 0 => Value::Bool(false), + 1 => Value::Bool(true), + _ => return Err(invalid("invalid boolean")), + }, + 2 => Value::Int(i64::from(self.reader.byte()?)), + 3 => { + let first = self.reader.byte()?; + let len = match first { + 0..=0x7f => 1, + 0xc2..=0xdf => 2, + 0xe0..=0xef => 3, + _ => return Err(invalid("invalid character")), + }; + let mut b = vec![first]; + b.extend_from_slice(self.reader.take(len - 1)?); + Value::String(String::from_utf8(b).map_err(|_| invalid("invalid character"))?) + } + 5 | 18 => Value::String(self.string()?), + 6 => { + self.reader.take(8)?; + Value::Null + } + 7 => number!(i16, Int), + 8 => number!(i32, Int), + 9 | 12 => number!(i64, Int), + 10 => Value::Int(i64::from(self.reader.byte()? as i8)), + 11 => { + self.reader.take(4)?; + Value::Null + } + 13 | 16 => { + let n = u64::from_le_bytes(self.reader.take(8)?.try_into().map_err(|_| invalid("invalid integer"))?); + // Metadata dates may use the high bits; their bit pattern stays intact. + Value::Int(n as i64) + } + 14 => number!(u16, Int), + 15 => number!(u32, Int), + 17 => Value::Null, + _ => return Err(invalid(format!("unsupported primitive type {tag}"))), + }) + } + + fn type_info(&mut self, tag: u8) -> Result> { + match tag { + 0 => Ok(Some(self.reader.byte()?)), + 1 | 2 | 5 | 6 => Ok(None), + 3 => { + self.string()?; + Ok(None) + } + 4 => { + self.string()?; + self.reader.le_i32()?; + Ok(None) + } + 7 => { + self.reader.byte()?; + Ok(None) + } + _ => Err(invalid(format!("unsupported binary type {tag}"))), + } + } + + fn reserve(&mut self, id: i32) -> Result<()> { + if id == 0 || self.graph.objects.insert(id, Value::Null).is_some() { + return Err(invalid("duplicate or zero object id")); + } + self.graph.order.push(id); + Ok(()) + } + + fn value(&mut self, depth: usize) -> Result { + loop { + let tag = self.reader.byte()?; + if tag == 12 { + self.record(tag, depth)?; + } else { + return self.record(tag, depth); + } + } + } + + fn object(&mut self, id: i32, class: Arc, depth: usize) -> Result { + self.reserve(id)?; + self.budget(class.names.len())?; + let mut members = Vec::with_capacity(class.names.len()); + for primitive in &class.primitives { + members.push(match primitive { + Some(p) => self.primitive(*p)?, + None => self.value(depth + 1)?, + }); + } + self.graph.objects.insert(id, Value::Object { class, members }); + Ok(Value::Ref(id)) + } + + fn array(&mut self, id: i32, len: usize, primitive: Option, depth: usize) -> Result { + self.reserve(id)?; + self.budget(len)?; + if primitive == Some(2) { + self.reader.take(len)?; + return Ok(Value::Ref(id)); + } + let mut values = Vec::with_capacity(len); + while values.len() < len { + self.check()?; + if let Some(p) = primitive { + values.push(self.primitive(p)?); + continue; + } + let tag = self.reader.byte()?; + if matches!(tag, 13 | 14) { + let count = if tag == 13 { usize::from(self.reader.byte()?) } else { self.reader.count()? }; + if count == 0 || count > len - values.len() { + return Err(invalid("invalid array null run")); + } + values.resize_with(values.len() + count, || Value::Null); + } else if tag == 12 { + self.record(tag, depth + 1)?; + } else { + values.push(self.record(tag, depth + 1)?); + } + } + self.graph.objects.insert(id, Value::Array(values)); + Ok(Value::Ref(id)) + } + + fn record(&mut self, tag: u8, depth: usize) -> Result { + self.check()?; + self.budget(1)?; + if depth > MAX_DEPTH { + return Err(invalid("serialized objects nested too deeply")); + } + match tag { + 1 => { + let id = self.reader.le_i32()?; + let metadata = self.reader.le_i32()?; + let class = self.classes.get(&metadata).cloned().ok_or_else(|| invalid("missing class metadata"))?; + self.object(id, class, depth) + } + 2..=5 => { + let id = self.reader.le_i32()?; + let name = self.string()?; + let count = self.reader.count()?; + self.budget(count)?; + let names = (0..count).map(|_| self.string()).collect::>>()?; + let primitives = if tag >= 4 { + let types = self.reader.take(count)?.to_vec(); + types.into_iter().map(|t| self.type_info(t)).collect::>>()? + } else { + vec![None; count] + }; + if matches!(tag, 3 | 5) { + self.reader.le_i32()?; + } + let class = Arc::new(Class { name, names, primitives }); + if self.classes.insert(id, class.clone()).is_some() { + return Err(invalid("duplicate class metadata")); + } + self.object(id, class, depth) + } + 6 => { + let id = self.reader.le_i32()?; + self.reserve(id)?; + let value = Value::String(self.string()?); + self.graph.objects.insert(id, value); + Ok(Value::Ref(id)) + } + 7 => { + let id = self.reader.le_i32()?; + let kind = self.reader.byte()?; + let rank = self.reader.count()?; + if kind > 5 || rank == 0 || rank > 32 { + return Err(invalid("invalid array dimensions")); + } + let mut len = 1usize; + for _ in 0..rank { + len = len.checked_mul(self.reader.count()?).ok_or_else(|| invalid("array size overflow"))?; + } + if kind >= 3 { + for _ in 0..rank { + if self.reader.le_i32()? != 0 { + return Err(invalid("nonzero array lower bounds are unsupported")); + } + } + } + let tag = self.reader.byte()?; + let primitive = self.type_info(tag)?; + self.array(id, len, primitive, depth) + } + 8 => { + let primitive = self.reader.byte()?; + self.primitive(primitive) + } + 9 => Ok(Value::Ref(self.reader.le_i32()?)), + 10 => Ok(Value::Null), + 12 => { + self.reader.le_i32()?; + self.string()?; + Ok(Value::Null) + } + 15..=17 => { + let id = self.reader.le_i32()?; + let len = self.reader.count()?; + let primitive = if tag == 15 { Some(self.reader.byte()?) } else { None }; + self.array(id, len, primitive, depth) + } + _ => Err(invalid(format!("unsupported NRBF record {tag}"))), + } + } +} diff --git a/crates/pdn/src/tests.rs b/crates/pdn/src/tests.rs new file mode 100644 index 000000000..bf6c8586b --- /dev/null +++ b/crates/pdn/src/tests.rs @@ -0,0 +1,444 @@ +use super::*; +use std::io::Write; + +enum Field { + Int(i32), + Long(i64), + Byte(u8), + Bool(bool), + Ref(i32), +} + +struct Writer(Vec); + +impl Writer { + fn int(&mut self, n: i32) { + self.0.extend(n.to_le_bytes()); + } + fn string(&mut self, s: &str) { + let mut len = s.len(); + while len >= 128 { + self.0.push((len as u8 & 127) | 128); + len >>= 7; + } + self.0.push(len as u8); + self.0.extend(s.as_bytes()); + } + fn object(&mut self, id: i32, class: &str, fields: &[(&str, Field)]) { + self.0.push(5); + self.int(id); + self.string(class); + self.int(fields.len() as i32); + for (name, _) in fields { + self.string(name); + } + for (_, f) in fields { + self.0.push(if matches!(f, Field::Ref(_)) { 2 } else { 0 }); + } + for (_, f) in fields { + match f { + Field::Int(_) => self.0.push(8), + Field::Long(_) => self.0.push(9), + Field::Byte(_) => self.0.push(2), + Field::Bool(_) => self.0.push(1), + Field::Ref(_) => {} + } + } + self.int(1); + for (_, f) in fields { + match f { + Field::Int(n) => self.int(*n), + Field::Long(n) => self.0.extend(n.to_le_bytes()), + Field::Byte(n) => self.0.push(*n), + Field::Bool(b) => self.0.push(u8::from(*b)), + Field::Ref(id) => { + self.0.push(9); + self.int(*id); + } + } + } + } +} + +const GO: &dyn Fn() -> bool = &|| true; + +/// Parses and decodes everything, as an application would. +fn read_all(bytes: &[u8]) -> Result<(Vec, Vec)> { + let doc = Document::parse(bytes, GO)?; + let mut surfaces = Vec::new(); + doc.read_surfaces(GO, &mut |i, s| { + assert_eq!(i, surfaces.len()); + surfaces.push(s); + })?; + assert_eq!(surfaces.iter().map(|s| s.data.len()).sum::(), doc.decoded_bytes()); + Ok((doc.layers, surfaces)) +} + +/// Independent synthetic PDN writer: padded rows, forward references, a root cycle, +/// spare ArrayList capacity, and reversed gzip/raw chunks. There is no production PDN writer. +fn fixture(modes: &[i32], legacy: bool, compressed: bool) -> Vec { + use Field::*; + let mut w = Writer(b"PDN3\0\0\0\0\x01".to_vec()); + w.0.push(0); + for n in [1, -1, 1, 0] { + w.int(n); + } + w.0.push(12); + w.int(1); + w.string("PaintDotNet.Data"); + w.object(1, "PaintDotNet.Document", &[("width", Int(2)), ("height", Int(2)), ("layers", Ref(2))]); + w.object(2, "PaintDotNet.LayerList", &[("parent", Ref(1)), ("ArrayList+_size", Int(modes.len() as i32)), ("ArrayList+_items", Ref(3))]); + w.0.push(16); + w.int(3); + w.int(modes.len() as i32 + 2); + for i in 0..modes.len() { + w.0.push(9); + w.int(16 + i as i32 * 10); + } + w.0.extend([13, 2]); + for (i, mode) in modes.iter().enumerate() { + let id = 16 + i as i32 * 10; + w.object( + id, + "PaintDotNet.BitmapLayer", + &[ + ("Layer+width", Int(2)), + ("Layer+height", Int(2)), + ("surface", Ref(id + 1)), + ("Layer+properties", Ref(id + 2)), + ("properties", Ref(id + 3)), + ], + ); + w.object(id + 1, "PaintDotNet.Surface", &[("width", Int(2)), ("height", Int(2)), ("stride", Int(12)), ("scan0", Ref(id + 7))]); + let mut fields = + vec![("name", Ref(id + 4)), ("opacity", Byte(if i == 0 { 255 } else { 128 })), ("visible", Bool(i != 1)), ("isBackground", Bool(i == 0))]; + if !legacy { + fields.push(("blendMode", Ref(id + 5))); + } + w.object(id + 2, "PaintDotNet.Layer+LayerProperties", &fields); + w.object(id + 3, "PaintDotNet.BitmapLayer+BitmapLayerProperties", &[("blendOp", Ref(id + 6))]); + w.0.push(6); + w.int(id + 4); + w.string(&format!("Läyer {i}")); + w.object(id + 5, "PaintDotNet.LayerBlendMode", &[("value__", Int(*mode))]); + w.object(id + 6, &format!("PaintDotNet.UserBlendOps+{}BlendOp", BlendMode::ALL.get(*mode as usize).map_or("Unknown", |m| m.name())), &[]); + w.object(id + 7, "PaintDotNet.MemoryBlock", &[("length64", Long(24)), ("hasParent", Bool(false)), ("deferred", Bool(true))]); + } + w.0.push(11); + for _ in modes { + w.0.push(if compressed { 0 } else { 1 }); + w.0.extend(12u32.to_be_bytes()); + for index in [1u32, 0] { + let raw = if index == 0 { + vec![30, 20, 10, 255, 60, 50, 40, 128, 99, 99, 99, 99] + } else { + vec![90, 80, 70, 64, 120, 110, 100, 0, 99, 99, 99, 99] + }; + let data = if compressed { + let mut enc = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + enc.write_all(&raw).unwrap(); + enc.finish().unwrap() + } else { + raw + }; + w.0.extend(index.to_be_bytes()); + w.0.extend((data.len() as u32).to_be_bytes()); + w.0.extend(data); + } + } + w.0 +} + +#[test] +fn all_blend_modes_and_layer_properties_are_read() { + let modes: Vec<_> = (0..14).collect(); + for legacy in [false, true] { + for compressed in [false, true] { + let bytes = fixture(&modes, legacy, compressed); + assert!(is_pdn(&bytes)); + let doc = Document::parse(&bytes, GO).unwrap(); + assert_eq!((doc.width, doc.height, doc.layers.len(), doc.surface_count(), doc.has_user_metadata), (2, 2, 14, 14, false)); + assert_eq!(doc.decoded_bytes(), 14 * 24); + let (layers, surfaces) = read_all(&bytes).unwrap(); + for (i, layer) in layers.iter().enumerate() { + assert_eq!( + layer, + &Layer { + name: format!("Läyer {i}"), + visible: i != 1, + opacity: if i == 0 { 255 } else { 128 }, + blend: BlendMode::ALL[i], + surface: i + } + ); + // Rows are padded to the stride (12 bytes for 2 pixels); the padding is cut off. + let surface = &surfaces[layer.surface]; + assert_eq!((surface.width, surface.stride, surface.data.len()), (2, 12, 24)); + let rows: Vec<&[u8]> = surface.rows().collect(); + assert_eq!(rows, [&[30, 20, 10, 255, 60, 50, 40, 128][..], &[90, 80, 70, 64, 120, 110, 100, 0]], "B, G, R, A"); + } + } + } +} + +#[test] +fn truncation_unknown_modes_and_corrupt_chunks_fail_without_panicking() { + let raw = fixture(&[0], false, false); + for len in 0..raw.len() { + assert!(read_all(&raw[..len]).is_err(), "length {len}"); + } + assert!(read_all(&raw).is_ok()); + let mut trailing = raw.clone(); + trailing.push(0); + assert_eq!(read_all(&trailing).err(), Some(Error::Invalid("unexpected trailing data".into()))); + for legacy in [false, true] { + assert!(Document::parse(&fixture(&[999], legacy, false), GO).is_err()); + assert!(Document::parse(&fixture(&[14], legacy, false), GO).is_err()); + } + let mut corrupt = raw.clone(); + let block = corrupt.len() - 45; + // Zero chunk size. + corrupt[block + 1..block + 5].fill(0); + assert!(read_all(&corrupt).is_err()); + let mut duplicate = raw.clone(); + // Both row chunks claim index 1. + duplicate[block + 25..block + 29].copy_from_slice(&1u32.to_be_bytes()); + assert!(read_all(&duplicate).is_err()); + let mut version = raw.clone(); + version[block] = 2; + assert!(read_all(&version).is_err()); + let mut zipped = fixture(&[0], false, true); + let last = zipped.len() - 1; + zipped[last] ^= 1; + assert!(Document::parse(&zipped, GO).is_ok() && read_all(&zipped).is_err(), "gzip checksum"); +} + +#[test] +fn reading_can_be_cancelled_during_parsing_and_pixel_decode() { + use std::cell::Cell; + let bytes = fixture(&[0, 1], false, true); + for limit in [0, 10] { + let checks = Cell::new(0); + let keep_going = || { + checks.set(checks.get() + 1); + checks.get() <= limit + }; + assert_eq!(Document::parse(&bytes, &keep_going).err(), Some(Error::Cancelled), "{limit}"); + } + let doc = Document::parse(&bytes, GO).unwrap(); + let seen = Cell::new(0); + let result = doc.read_surfaces(&|| seen.get() == 0, &mut |_, _| seen.set(seen.get() + 1)); + assert_eq!((result, seen.get()), (Err(Error::Cancelled), 1), "cancelled after the first surface"); +} + +proptest::proptest! { + #[test] + fn arbitrary_serialized_data_returns_an_error_without_panicking(data in proptest::collection::vec(proptest::prelude::any::(), 0..4096)) { + let mut bytes = b"PDN3\0\0\0\0\x01".to_vec(); + bytes.extend(data); + let _ = read_all(&bytes); + } + + #[test] + fn mutated_layered_documents_never_panic(edits in proptest::collection::vec((proptest::prelude::any::(), proptest::prelude::any::()), 1..8)) { + let mut bytes = fixture(&[0, 5, 13], false, true); + for (offset, value) in edits { + let index = offset % bytes.len(); + bytes[index] = value; + } + let _ = read_all(&bytes); + } +} + +#[test] +fn malformed_headers_are_errors() { + for bytes in [b"".as_slice(), b"PDN3", b"PDN3\xff\xff\xff", b"PDN4\0\0\0", b"PDN3\0\0\0\0\x02"] { + assert!(matches!(Document::parse(bytes, GO), Err(Error::Invalid(_))), "{bytes:?}"); + } + assert!(!is_pdn(b"PDN4\0\0\0") && !is_pdn(b"PD")); + // The XML header between the signature and the stream is skipped by its 24-bit length. + let plain = fixture(&[0], false, true); + let mut with_header = b"PDN3\x05\0\0".to_vec(); + with_header.extend_from_slice(&plain[7..]); + assert_eq!(read_all(&with_header), read_all(&plain)); +} + +/// A stream of `records` after the header record. +fn stream(records: impl FnOnce(&mut Writer)) -> Vec { + let mut w = Writer(vec![0]); + for n in [1, -1, 1, 0] { + w.int(n); + } + records(&mut w); + w.0 +} + +#[test] +fn serialized_counts_depth_and_references_are_bounded() { + let array = |w: &mut Writer, id: i32, len: i32| { + w.0.push(16); + w.int(id); + w.int(len); + }; + let nested = |depth: i32| { + stream(|w| { + (1..=depth).for_each(|id| array(w, id, 1)); + w.0.extend([10, 11]); + }) + }; + let strings = |total: usize| { + stream(|w| { + for id in 1..=(total >> 20) as i32 { + w.0.push(6); + w.int(id); + w.string(&"s".repeat(1 << 20)); + } + w.0.push(11); + }) + }; + let cases: Vec<(&str, Vec)> = vec![ + ("an array over the item limit", stream(|w| array(w, 1, nrbf::MAX_ITEMS as i32 + 1))), + ("nesting past the depth limit", nested(nrbf::MAX_DEPTH as i32 + 2)), + ("strings past the limit", strings(nrbf::MAX_STRINGS + (1 << 20))), + ( + "a duplicate object id", + stream(|w| { + for _ in 0..2 { + w.0.push(6); + w.int(1); + w.string("duplicate"); + } + w.0.push(11); + }), + ), + ( + "a null run longer than its array", + stream(|w| { + array(w, 1, 1); + w.0.extend([13, 2, 11]); + }), + ), + ( + "an object of an undeclared class", + stream(|w| { + w.0.push(1); + w.int(1); + w.int(999); + }), + ), + ("no root object", stream(|w| w.0.push(11))), + ("an unknown record", stream(|w| w.0.push(99))), + ("a negative count", stream(|w| array(w, 1, -1))), + ]; + for (what, bytes) in cases { + let result = nrbf::parse(&bytes, GO).map(|(g, _)| g.array(&g.root).map(|a| a.len())); + assert!(matches!(result, Err(Error::Invalid(_)) | Ok(Err(Error::Invalid(_)))), "{what}"); + } + // An array holding a reference to itself is a legal graph: resolving stops at the array. + let own = stream(|w| { + array(w, 1, 1); + w.0.push(9); + w.int(1); + w.0.push(11); + }); + let (graph, _) = nrbf::parse(&own, GO).unwrap(); + let items = graph.array(&graph.root).unwrap(); + assert_eq!(graph.array(&items[0]).map(<[Value]>::len), Ok(1)); + assert!(nrbf::parse(&nested(nrbf::MAX_DEPTH as i32), GO).is_ok()); + assert!(nrbf::parse(&strings(nrbf::MAX_STRINGS), GO).is_ok()); +} + +#[test] +fn a_graph_is_read_by_class_field_and_reference() { + use Field::*; + let mut bytes = stream(|w| { + w.object( + 1, + "Root", + &[("count", Int(-7)), ("big", Long(1 << 40)), ("flag", Bool(true)), ("byte", Byte(200)), ("child", Ref(2)), ("text", Ref(3))], + ); + w.object(2, "Child", &[("parent", Ref(1))]); + w.0.push(6); + w.int(3); + w.string("héllo"); + w.0.push(11); + }); + let len = bytes.len(); + bytes.extend_from_slice(b"pixels follow"); + let (graph, used) = nrbf::parse(&bytes, GO).unwrap(); + assert_eq!(used, len, "what follows the stream is left for the caller"); + let root = &graph.root; + assert_eq!(graph.class_name(root).unwrap(), "Root"); + assert_eq!(graph.integer(graph.field(root, "count").unwrap()), Ok(-7)); + assert_eq!(graph.integer(graph.field(root, "big").unwrap()), Ok(1 << 40)); + assert_eq!(graph.integer(graph.field(root, "byte").unwrap()), Ok(200)); + assert_eq!(graph.boolean(graph.field(root, "flag").unwrap()), Ok(true)); + assert_eq!(graph.string(graph.field(root, "text").unwrap()), Ok("héllo")); + let child = graph.field(root, "child").unwrap(); + assert_eq!(graph.class_name(graph.field(child, "parent").unwrap()).unwrap(), "Root", "a cycle through objects is fine"); + assert!(graph.optional_field(root, "missing").unwrap().is_none() && graph.field(root, "missing").is_err()); + assert!(graph.integer(graph.field(root, "flag").unwrap()).is_err() && graph.string(root).is_err() && graph.array(root).is_err()); + assert_eq!(graph.objects_of_class("Child").map(|(id, _)| id).collect::>(), [2]); + match graph.resolve(root).unwrap() { + Value::Object { class, members } => assert_eq!((class.name(), class.member_names().len(), members.len()), ("Root", 6, 6)), + other => panic!("{other:?}"), + } + assert_eq!(nrbf::parse(&bytes, &|| false).err(), Some(Error::Cancelled)); +} + +#[test] +fn truncated_uncompressed_pixels_are_rejected_before_allocating() { + // A raw (version 1) block of 256 MiB chunks that declares 1 GiB of pixels but holds a few bytes. + let mut reader = Reader { bytes: &[1, 0x10, 0, 0, 0, 0, 0, 0, 0], pos: 0 }; + assert_eq!(pixels(&mut reader, 1 << 30, GO).err(), Some(Error::Invalid("uncompressed pixel data is truncated".into()))); +} + +/// The fixture with `edit` applied to the serialized text: same-length replacements only. +fn edited(from: &[u8], to: &[u8]) -> Vec { + assert_eq!(from.len(), to.len()); + let mut bytes = fixture(&[0, 1], false, false); + let at = bytes.windows(from.len()).position(|w| w == from).unwrap_or_else(|| panic!("{from:?} not in the fixture")); + bytes[at..at + from.len()].copy_from_slice(to); + bytes +} + +#[test] +fn documents_this_reader_does_not_cover_are_refused() { + let cases: [(&str, &[u8], &[u8]); 6] = [ + ("root object is not a Paint.NET document", b"PaintDotNet.Document", b"PaintDotNet.Documenx"), + ("only PDN3 bitmap layers are supported", b"PaintDotNet.BitmapLayer\x05", b"PaintDotNet.VectorLayer\x05"), + ("invalid bitmap surface", b"PaintDotNet.Surface", b"PaintDotNet.Surfacf"), + ("unsupported non-deferred or parented pixel memory", b"\x18\0\0\0\0\0\0\0\0\x01", b"\x18\0\0\0\0\0\0\0\x01\x01"), + ("pixel memory length does not match the surface", b"\x18\0\0\0\0\0\0\0\0\x01", b"\x19\0\0\0\0\0\0\0\0\x01"), + ("surface stride is too short for BGRA32 pixels", b"\x02\0\0\0\x02\0\0\0\x0c\0\0\0", b"\x02\0\0\0\x02\0\0\0\x07\0\0\0"), + ]; + for (why, from, to) in cases { + assert_eq!(Document::parse(&edited(from, to), GO).err(), Some(Error::Invalid(why.into()))); + } + // The canvas: 2 × 2 in the fixture's Document object, right after its class header. + let canvas = |w: i32, h: i32| { + let mut bytes = fixture(&[0], false, false); + let at = bytes.windows(8).position(|w| w == [2, 0, 0, 0, 2, 0, 0, 0]).unwrap(); + bytes[at..at + 4].copy_from_slice(&w.to_le_bytes()); + bytes[at + 4..at + 8].copy_from_slice(&h.to_le_bytes()); + Document::parse(&bytes, GO).err() + }; + let limit = Some(Error::Invalid("canvas is empty or exceeds the 1 GiB pixel limit".into())); + assert_eq!(canvas(0, 2), limit); + assert_eq!(canvas(16385, 16384), limit); + assert_eq!(canvas(-1, 2), Some(Error::Invalid("invalid width".into()))); + assert_eq!( + canvas(16384, 16384), + Some(Error::Invalid("layer dimensions do not match the canvas".into())), + "1 GiB exactly passes the canvas check" + ); +} + +#[test] +fn layer_counts_and_names_are_bounded() { + assert!(Document::parse(&fixture(&[], false, false), GO).is_err(), "no layers"); + // 1025 layers: over the limit. The fixture's ids step by ten, so this stays far below i32. + let modes = vec![0; MAX_LAYERS + 1]; + assert_eq!(Document::parse(&fixture(&modes, false, false), GO).err(), Some(Error::Invalid("document must have 1..1024 bitmap layers".into()))); + assert_eq!(Document::parse(&fixture(&modes[..MAX_LAYERS], false, false), GO).map(|d| d.layers.len()), Ok(MAX_LAYERS)); +} From 712e6fa5e39e49448e5b705eefadd663beff8fb5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:31:29 -0500 Subject: [PATCH 057/120] craft-odf: Manifest::parse_strict and Package::manifest_strict (#972) A strict read of the manifest for a reader that refuses a damaged package instead of opening what it can, as GridCraft's ODS reader does today (crates/xlsx/src/ods.rs check_manifest): malformed or unbalanced XML, any document type declaration, undefined entities, content outside the root, a root that is not manifest:manifest by namespace, a file entry without a full-path, or more entries than the reader keeps are errors (Error::Manifest). --- crates/odf/README.md | 2 +- crates/odf/src/lib.rs | 4 ++++ crates/odf/src/manifest.rs | 27 +++++++++++++++++++++++- crates/odf/src/package.rs | 7 +++++++ crates/odf/src/tests.rs | 42 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) diff --git a/crates/odf/README.md b/crates/odf/README.md index e91d0d94d..7e82a42ba 100644 --- a/crates/odf/README.md +++ b/crates/odf/README.md @@ -7,7 +7,7 @@ this crate reads and writes that container and nothing inside it. | Module | What it does | | --- | --- | | `Package`, `PackageWriter` | The ZIP container: the `mimetype` file, the manifest, the files, within `Limits`. | -| `Manifest` | `META-INF/manifest.xml`: each file's media type, and how an encrypted file is encrypted. Read by namespace, whatever prefix the file uses. | +| `Manifest` | `META-INF/manifest.xml`: each file's media type, and how an encrypted file is encrypted. Read by namespace, whatever prefix the file uses; `parse_strict` / `Package::manifest_strict` refuse a damaged one instead of reading what they can. | | `crypt` | Password-protected packages: AES-256-CBC, SHA-256 start key, PBKDF2 (HMAC-SHA-1), SHA-256 or SHA-1 checksums. A package in, a package out. | | `flat` | Flat single-file documents (`.fodt`, `.fods`, `.fodp`, `.fodg`): one `office:document` taken apart into a package's files, and a package put together into one. | | `zip_guard` | Refuses a ZIP whose end record declares too many entries, before `zip` reserves room for them. | diff --git a/crates/odf/src/lib.rs b/crates/odf/src/lib.rs index 174eb50cb..ff41b3518 100644 --- a/crates/odf/src/lib.rs +++ b/crates/odf/src/lib.rs @@ -76,6 +76,10 @@ pub enum Error { /// A flat document that is no `office:document`, or not UTF-8. #[error("not a flat OpenDocument file ({0})")] Flat(&'static str), + /// A manifest that [`Manifest::parse_strict`] refuses: malformed XML, a document type + /// declaration, a root that is no `manifest:manifest`, or more entries than it reads. + #[error("the package's manifest is not valid ({0})")] + Manifest(String), } impl Error { diff --git a/crates/odf/src/manifest.rs b/crates/odf/src/manifest.rs index d355c6047..89d4f0631 100644 --- a/crates/odf/src/manifest.rs +++ b/crates/odf/src/manifest.rs @@ -5,7 +5,9 @@ use std::fmt::Write as _; use base64::Engine as _; use base64::engine::general_purpose::STANDARD; -use craft_xml::{Canonical, El, Options, esc_attr}; +use craft_xml::{Canonical, Doctype, El, Options, esc_attr}; + +use crate::Error; /// The manifest's path in a package. pub const MANIFEST_PATH: &str = "META-INF/manifest.xml"; @@ -76,6 +78,29 @@ impl Manifest { Manifest { version: attr(&root, "version").map(str::to_string), entries } } + /// Reads a manifest, refusing one a lenient read would repair: malformed or unbalanced + /// XML, a document type declaration (of any kind), an undefined entity, text or a second + /// element outside the root, a root that is not `manifest` in the manifest namespace, a + /// `file-entry` without a `full-path`, or more than [`MAX_ENTRIES`] entries. For a reader + /// that refuses a damaged package rather than open what it can. + pub fn parse_strict(xml: &[u8]) -> Result { + let bad = |why: String| Error::Manifest(why); + let canonical = Canonical::new(&[("manifest", MANIFEST_NS)]); + let opts = Options { doctype: Doctype::Reject, ..Options::canonical(&canonical).strict() }; + let root = opts.parse(xml).map_err(|e| bad(e.to_string()))?; + if root.name != "manifest:manifest" { + return Err(bad(format!("the root element is {}, not manifest:manifest", root.name))); + } + let mut entries = Vec::new(); + for e in root.elements().filter(|e| e.name == "manifest:file-entry") { + if entries.len() == MAX_ENTRIES { + return Err(bad(format!("more than {MAX_ENTRIES} file entries"))); + } + entries.push(file_entry(e).ok_or_else(|| bad("a file entry has no full-path".into()))?); + } + Ok(Manifest { version: attr(&root, "version").map(str::to_string), entries }) + } + /// The entry for `path`. pub fn entry(&self, path: &str) -> Option<&FileEntry> { self.entries.iter().find(|e| e.path == path) diff --git a/crates/odf/src/package.rs b/crates/odf/src/package.rs index 89977223b..48272e85a 100644 --- a/crates/odf/src/package.rs +++ b/crates/odf/src/package.rs @@ -135,6 +135,13 @@ impl<'a> Package<'a> { self.read(MANIFEST_PATH).ok().flatten().map(|xml| Manifest::parse(&xml)).unwrap_or_default() } + /// The package's manifest read with [`Manifest::parse_strict`]: an error when the package + /// has none, or one a strict read refuses, or it is over a limit. + pub fn manifest_strict(&mut self) -> Result { + let xml = self.read(MANIFEST_PATH)?.ok_or_else(|| Error::Manifest("the package has no manifest".into()))?; + Manifest::parse_strict(&xml) + } + /// Does the manifest say any file is encrypted ([`crate::crypt`])? pub fn is_encrypted(&mut self) -> bool { self.manifest().is_encrypted() diff --git a/crates/odf/src/tests.rs b/crates/odf/src/tests.rs index 0894271ae..7a7d2eaca 100644 --- a/crates/odf/src/tests.rs +++ b/crates/odf/src/tests.rs @@ -68,6 +68,48 @@ fn a_manifest_is_read_by_namespace_and_survives_damage() { assert_eq!(Manifest::parse(many.as_bytes()).entries.len(), crate::manifest::MAX_ENTRIES); } +#[test] +fn a_strict_manifest_refuses_what_a_lenient_one_repairs() { + const NS: &str = r#"xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0""#; + let good = format!( + r#""# + ); + let m = Manifest::parse_strict(good.as_bytes()).unwrap(); + assert_eq!((m.version.as_deref(), m.media_type("/")), (Some("1.3"), Some("a/b"))); + // Another prefix for the same namespace is the same manifest. + let other = r#""#; + assert_eq!(Manifest::parse_strict(other.as_bytes()), Ok(m.clone())); + for bad in [ + format!(r#""#), + format!(r#""#), + format!(r#"]>"#), + format!(r#""#), + format!(r#"trailing"#), + format!(r#""#), + format!(r#""#), + format!(r#""#), + r#""#.to_string(), + r#""#.to_string(), + String::new(), + ] { + assert!(matches!(Manifest::parse_strict(bad.as_bytes()), Err(Error::Manifest(_))), "{bad}"); + let _ = Manifest::parse(bad.as_bytes()); + } + // The lenient read takes what it can of the first. + assert_eq!(Manifest::parse(format!(r#""#).as_bytes()).entries.len(), 1); + // Entries: up to the cap, not past it. + let many = |n: usize| format!(r#"{}"#, r#""#.repeat(n)); + assert_eq!(Manifest::parse_strict(many(crate::manifest::MAX_ENTRIES).as_bytes()).map(|m| m.entries.len()), Ok(crate::manifest::MAX_ENTRIES)); + assert!(Manifest::parse_strict(many(crate::manifest::MAX_ENTRIES + 1).as_bytes()).is_err()); + // Through a package: missing and present. + let mut p_bytes = zip_of(&[("mimetype", mime::SPREADSHEET.as_bytes())]); + assert!(matches!(Package::open(&p_bytes, &Limits::DEFAULT).unwrap().manifest_strict(), Err(Error::Manifest(_)))); + p_bytes = zip_of(&[("mimetype", mime::SPREADSHEET.as_bytes()), ("META-INF/manifest.xml", good.as_bytes())]); + assert_eq!(Package::open(&p_bytes, &Limits::DEFAULT).unwrap().manifest_strict(), Ok(m)); + // A written package passes. + assert!(Package::open(&secret(), &Limits::DEFAULT).unwrap().manifest_strict().is_ok_and(|m| m.entries.len() == 4)); +} + #[test] fn a_package_is_held_to_its_limits() { let bytes = zip_of(&[("mimetype", mime::SPREADSHEET.as_bytes()), ("a.xml", &[b'a'; 100]), ("b.xml", &[b'b'; 100])]); From 119415f46f601e20b9bc6b1b4d6979ceda86d88e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 19:32:04 -0500 Subject: [PATCH 058/120] Add craft-xlsx: XLSX and XLSB part layer (workbook, strings, styles, streamed rows) (#976) * Add craft-sheet: the cell positions, values and tables the spreadsheet formats share craft-xls carried its own CellRef, Range, Value, CellError, Visibility, sheet-name quoting, built-in number format table and indexed palette. craft-xlsx and craft-ods need the same ones, so they move to a crate with no dependencies and craft-xls re-exports them: an application gets one Value and one CellRef whichever file it read. Added on the way: A1 parsing (CellRef::parse, Range::parse), Range::count and a1, CellError::parse and the newer error values SpreadsheetML stores as text, builtin_format_id for writers. * Add craft-xlsx: the part layer of XLSX and XLSB workbooks Package::open finds the workbook part through craft-opc and reads the workbook part, shared strings (runs and phonetic readings), the styles tables as indexed tables, and a worksheet's rows one at a time with the rest of the part as a tree. Binary workbooks give the same workbook, strings, rows and merged cells from their records. Every other part stays reachable through Package::opc. From GridCraft crates/xlsx (7e0d96d7): read.rs, sheet_read.rs, styles.rs, xml.rs (string items, xstring escapes), xlsb.rs and its tests, split from GridCraft's document model. * craft-xlsx: XLSB opens up to 4 million cells, records counted once over the workbook Binary workbooks over 1 million cells were refused, a quarter of what .xls allows, and an allowance of 5 million records per part refused ordinary files: Excel writes four records per non-empty row besides the cells, and one BrtCellBlank for every formatted empty cell. Cells are now 4 million over the workbook (also the most shared strings), text 256 MiB, and records 40 million for every part together, as in craft-xls. Ported from storytold/gridcraft#3807. --------- Co-authored-by: kiwi4boy --- Cargo.lock | 10 + Cargo.toml | 1 + README.md | 1 + crates/xlsx/Cargo.toml | 17 + crates/xlsx/README.md | 41 +++ crates/xlsx/src/lib.rs | 148 +++++++++ crates/xlsx/src/package.rs | 275 ++++++++++++++++ crates/xlsx/src/sheet.rs | 387 ++++++++++++++++++++++ crates/xlsx/src/strings.rs | 196 +++++++++++ crates/xlsx/src/styles.rs | 555 +++++++++++++++++++++++++++++++ crates/xlsx/src/testing.rs | 50 +++ crates/xlsx/src/workbook.rs | 117 +++++++ crates/xlsx/src/xlsb.rs | 563 ++++++++++++++++++++++++++++++++ crates/xlsx/tests/common/mod.rs | 57 ++++ crates/xlsx/tests/xlsb.rs | 444 +++++++++++++++++++++++++ crates/xlsx/tests/xlsx.rs | 177 ++++++++++ 16 files changed, 3039 insertions(+) create mode 100644 crates/xlsx/Cargo.toml create mode 100644 crates/xlsx/README.md create mode 100644 crates/xlsx/src/lib.rs create mode 100644 crates/xlsx/src/package.rs create mode 100644 crates/xlsx/src/sheet.rs create mode 100644 crates/xlsx/src/strings.rs create mode 100644 crates/xlsx/src/styles.rs create mode 100644 crates/xlsx/src/testing.rs create mode 100644 crates/xlsx/src/workbook.rs create mode 100644 crates/xlsx/src/xlsb.rs create mode 100644 crates/xlsx/tests/common/mod.rs create mode 100644 crates/xlsx/tests/xlsb.rs create mode 100644 crates/xlsx/tests/xlsx.rs diff --git a/Cargo.lock b/Cargo.lock index 7b227c217..cb35096ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1829,6 +1829,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xlsx" +version = "0.1.0" +dependencies = [ + "craft-opc", + "craft-sheet", + "craft-xml", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 6b5a23328..9f46a4741 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -106,6 +106,7 @@ craft-vcard = { path = "crates/vcard" } craft-vectorcraft-format = { path = "crates/vectorcraft-format" } craft-vectordrawable = { path = "crates/vectordrawable" } craft-xls = { path = "crates/xls" } +craft-xlsx = { path = "crates/xlsx" } craft-xcf = { path = "crates/xcf" } craft-xml = { path = "crates/xml" } craft-zip = { path = "crates/zip" } diff --git a/README.md b/README.md index 33f37171c..1784d1e47 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,7 @@ craft- = { git = "https://github.com/storytold/craft-libs", rev = " Result<(), craft_xlsx::Error> { +//! # let bytes = craft_xlsx::testing::workbook(&[("Prices", r#"02.5"#)], &["Tea"]); +//! let mut package = craft_xlsx::Package::open(&bytes, &craft_xlsx::Limits::default())?; +//! let workbook = package.workbook()?; +//! let strings = package.shared_strings()?; +//! let mut values = Vec::new(); +//! package.sheet(&workbook.sheets[0], |row| { +//! values.extend(row.cells.iter().map(|c| c.value.resolve(&strings, workbook.date_1904))); +//! Ok(()) +//! })?; +//! assert_eq!(values, [craft_xlsx::Value::text("Tea"), craft_xlsx::Value::Number(2.5)]); +//! # Ok(()) +//! # } +//! ``` + +mod package; +mod sheet; +mod strings; +mod styles; +#[doc(hidden)] +pub mod testing; +mod workbook; +mod xlsb; + +pub use craft_sheet::{ + BorderStyle, CellError, CellRef, DEFAULT_PALETTE, HAlign, MAX_COLS, MAX_ROWS, Pattern, Range, Script, Underline, VAlign, Value, Visibility, + builtin_format, builtin_format_id, quote_sheet, sheet_needs_quotes, +}; +pub use package::{Package, is_xlsb}; +pub use sheet::{Cell, CellFormula, CellValue, Column, FormulaKind, Row, SheetPart, iso_date_serial}; +pub use strings::{PhoneticProps, PhoneticRun, StringItem, TextRun, decode_xstring, encode_xstring, rich_text}; +pub use styles::{ + Alignment, Border, BorderEdge, CellStyle, Color, ColorSource, Dxf, Fill, Font, FontScheme, GradientFill, PatternFill, Protection, Styles, Xf, +}; +pub use workbook::{DefinedName, SheetEntry, SheetKind, Workbook}; + +/// Why a workbook could not be read. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The bytes are not a ZIP package, or an entry is damaged. + #[error("the file is not a valid zip package: {0}")] + Zip(String), + /// A part that must be read holds XML that cannot be. + #[error("the file contains invalid XML: {0}")] + Xml(String), + /// The package is not a workbook, or a binary part is damaged. + #[error("the file is not a valid spreadsheet: {0}")] + Invalid(String), + /// More than [`Limits`] or a fixed cap allows. + #[error("the file is too large: {0}")] + TooLarge(String), +} + +impl From for Error { + fn from(e: craft_opc::Error) -> Self { + match e { + e if e.is_limit() => Error::TooLarge(e.to_string()), + craft_opc::Error::Zip(m) => Error::Zip(m), + craft_opc::Error::Xml { .. } => Error::Xml(e.to_string()), + e => Error::Invalid(e.to_string()), + } + } +} + +impl From for Error { + fn from(e: craft_xml::Error) -> Self { + if e.is_limit() { Error::TooLarge(e.to_string()) } else { Error::Xml(e.to_string()) } + } +} + +/// What a read refuses to exceed. The defaults suit a spreadsheet application; a caller that +/// only wants a table of values can lower them. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Limits { + /// The ZIP package: entries, bytes per part and bytes in all. + pub package: craft_opc::Limits, + /// One XML part. Rows and shared strings are streamed, so `max_nodes` bounds one row or one + /// string and what is left of a part, not a sheet's length. + pub xml: craft_xml::Limits, + /// Records read from a binary workbook, every part together. Records are skipped or decoded + /// without being kept, so this bounds parse time, not memory. Excel writes four records per + /// row besides the cells, and one per formatted empty cell, so a sheet needs many more + /// records than cells. + pub binary_records: usize, + /// Cells with a value read from a binary workbook, over all its sheets; also the most + /// shared-string items. + pub binary_cells: usize, + /// Bytes of decoded text (shared and inline strings) in a binary workbook, over all its parts. + pub binary_text: usize, +} + +impl Default for Limits { + fn default() -> Self { + Limits { + package: craft_opc::Limits::new(200_000, 512 << 20, 1 << 30), + xml: craft_xml::Limits { max_input: 512 << 20, ..craft_xml::Limits::DEFAULT }, + // The limits of `craft-xls`. + binary_records: 40_000_000, + binary_cells: 4_000_000, + binary_text: 256 << 20, + } + } +} + +/// What a read skipped or repaired, each distinct message once and at most 1000 of them. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub(crate) struct Warnings(pub(crate) Vec); + +impl Warnings { + pub(crate) fn warn(&mut self, m: impl Into) { + let m = m.into(); + if self.0.len() < 1000 && !self.0.contains(&m) { + self.0.push(m); + } + } +} diff --git a/crates/xlsx/src/package.rs b/crates/xlsx/src/package.rs new file mode 100644 index 000000000..dc772e46e --- /dev/null +++ b/crates/xlsx/src/package.rs @@ -0,0 +1,275 @@ +//! The OPC package of a workbook and the parts this crate reads from it. + +use craft_opc::{ContentTypes, LazyPackage, Rels}; +use craft_xml::{El, Options, UnknownEntity}; + +use crate::sheet::{Row, RowReader, SheetPart}; +use crate::strings::StringItem; +use crate::styles::Styles; +use crate::workbook::{SheetEntry, Workbook}; +use crate::{Error, Limits, Warnings, xlsb}; + +/// Most bytes of `[Content_Types].xml` read from a binary workbook. +const MAX_CONTENT_TYPES: u64 = 1 << 20; + +/// An open workbook package. Parts are inflated when they are asked for, against the +/// package's [`Limits`]. +pub struct Package<'a> { + opc: LazyPackage<'a>, + limits: Limits, + warnings: Warnings, + workbook_part: String, + /// Content types, for a binary workbook (which checks every part it reads against them). + binary: Option, + workbook_rels: Option, + budget: xlsb::Budget, + /// Shared strings in a binary workbook, once counted: a cell's index is checked against it. + binary_strings: Option, +} + +/// Is this a binary workbook (`.xlsb`): a package whose one workbook relationship points at a +/// part of the XLSB workbook content type? +pub fn is_xlsb(bytes: &[u8]) -> bool { + Package::open(bytes, &Limits::default()).is_ok_and(|p| p.is_binary()) +} + +impl<'a> Package<'a> { + /// Opens the package and finds the workbook part. Nothing else is read yet. + pub fn open(bytes: &'a [u8], limits: &Limits) -> Result, Error> { + let mut opc = LazyPackage::open(bytes, &limits.package)?; + let root = opc.rels("")?; + let documents: Vec<_> = root.all("officeDocument").collect(); + let mut binary = None; + let workbook_part = match documents.first() { + Some(rel) if !rel.external && rel.target.to_ascii_lowercase().ends_with(".bin") => { + let types = opc.read_limited("[Content_Types].xml", MAX_CONTENT_TYPES)?.map(|b| ContentTypes::parse(&b)).unwrap_or_default(); + if documents.len() != 1 { + return Err(xlsb::invalid("expected one workbook relationship")); + } + xlsb::check_type(&types, &rel.target, xlsb::MAIN)?; + binary = Some(types); + rel.target.clone() + } + Some(rel) if !rel.external => rel.target.clone(), + _ => "xl/workbook.xml".to_string(), + }; + Ok(Package { + opc, + limits: *limits, + warnings: Warnings::default(), + workbook_part, + binary, + workbook_rels: None, + budget: xlsb::Budget::default(), + binary_strings: None, + }) + } + + /// A binary workbook (`.xlsb`). + pub fn is_binary(&self) -> bool { + self.binary.is_some() + } + + /// The workbook part's name. + pub fn workbook_part(&self) -> &str { + &self.workbook_part + } + + /// What reading skipped or repaired so far, each message once. + pub fn warnings(&self) -> &[String] { + &self.warnings.0 + } + + /// Adds a warning (a caller reading further parts keeps one list). + pub fn warn(&mut self, m: impl Into) { + self.warnings.warn(m); + } + + /// The package underneath, for the parts this crate does not read. + pub fn opc(&mut self) -> &mut LazyPackage<'a> { + &mut self.opc + } + + /// The XML options parts are read with: local names, an undefined entity kept as written, + /// [`Limits::xml`]. + pub fn xml_options(&self) -> Options<'static> { + Options { unknown_entity: UnknownEntity::Literal, ..Options::local() }.with_limits(self.limits.xml) + } + + /// Part `part` as XML; `Ok(None)` when there is no such part. + pub fn read_xml(&mut self, part: &str) -> Result, Error> { + let options = self.xml_options(); + Ok(self.opc.read_xml(part, &options)?) + } + + /// A part the workbook can do without: missing or unreadable is a warning and `None`; + /// only a limit is an error. + pub fn optional_xml(&mut self, part: &str) -> Result, Error> { + match self.read_xml(part) { + Ok(Some(e)) => Ok(Some(e)), + Ok(None) => { + self.warn(format!("missing part {part}")); + Ok(None) + } + Err(Error::TooLarge(m)) => Err(Error::TooLarge(m)), + Err(e) => { + self.warn(format!("skipped {part}: {e}")); + Ok(None) + } + } + } + + /// The relationships of `part`. + pub fn rels(&mut self, part: &str) -> Result { + Ok(self.opc.rels(part)?) + } + + /// The workbook part's relationships. + pub fn workbook_rels(&mut self) -> Result { + if let Some(r) = &self.workbook_rels { + return Ok(r.clone()); + } + let rels = self.opc.rels(&self.workbook_part)?; + self.workbook_rels = Some(rels.clone()); + Ok(rels) + } + + /// The workbook part. + pub fn workbook(&mut self) -> Result { + if self.binary.is_some() { + let bytes = self.required(&self.workbook_part.clone())?; + let rels = self.workbook_rels()?; + return xlsb::workbook(&bytes, &rels, &self.limits, &self.budget, &mut self.warnings); + } + let options = self.xml_options(); + let root = match self.opc.read_xml(&self.workbook_part, &options)? { + Some(root) => root, + None if self.workbook_part != "xl/workbook.xml" && self.opc.has("xl/workbook.xml") => { + self.workbook_part = "xl/workbook.xml".into(); + self.workbook_rels = None; + self.opc.read_xml("xl/workbook.xml", &options)?.ok_or_else(|| Error::Invalid("no workbook part".into()))? + } + None if self.opc.has("content.xml") => return Err(Error::Invalid("this is an OpenDocument file, not XLSX".into())), + None => return Err(Error::Invalid("no workbook part".into())), + }; + if root.name != "workbook" { + return Err(Error::Invalid(format!("unexpected root element <{}> in workbook part", root.name))); + } + let rels = self.workbook_rels()?; + Ok(Workbook::from_xml(root, &rels)) + } + + /// The shared strings, empty when the workbook has none. + pub fn shared_strings(&mut self) -> Result, Error> { + let rels = self.workbook_rels()?; + if self.binary.is_some() { + let found: Vec<_> = rels.all("sharedStrings").collect(); + let Some(rel) = found.first() else { + self.binary_strings = Some(0); + return Ok(Vec::new()); + }; + if found.len() > 1 { + return Err(xlsb::invalid("multiple shared-string relationships")); + } + if rel.external { + return Err(xlsb::invalid("external shared-string relationship")); + } + if let Some(types) = &self.binary { + xlsb::check_type(types, &rel.target, xlsb::STRINGS)?; + } + let bytes = self.required(&rel.target)?; + let strings = xlsb::shared_strings(&bytes, &self.limits, &self.budget, &mut self.warnings)?; + self.binary_strings = Some(strings.len()); + return Ok(strings); + } + let Some(rel) = rels.first("sharedStrings").filter(|r| !r.external) else { return Ok(Vec::new()) }; + let Some(bytes) = self.opc.read(&rel.target)? else { + self.warn(format!("missing part {}", rel.target)); + return Ok(Vec::new()); + }; + let mut items = Vec::new(); + let (_, err) = self.xml_options().parse_streaming_partial(&bytes, &["si"], |si| { + items.push(StringItem::read(&si)); + Ok::<(), craft_xml::Error>(()) + }); + if let Some(e) = err { + self.warn(format!("shared strings are damaged ({e}); kept {} strings", items.len())); + } + let dropped: usize = items.iter().map(|s| s.dropped_phonetic_runs).sum(); + if dropped > 0 { + self.warn(format!("shared strings: {dropped} phonetic runs were dropped (bad sb/eb or no reading)")); + } + Ok(items) + } + + /// The styles part, `None` when the workbook has none (or is binary: its styles are not read). + pub fn styles(&mut self) -> Result, Error> { + if self.binary.is_some() { + return Ok(None); + } + let rels = self.workbook_rels()?; + let Some(rel) = rels.first("styles").filter(|r| !r.external) else { + self.warn("no styles part"); + return Ok(None); + }; + Ok(self.optional_xml(&rel.target.clone())?.map(Styles::read)) + } + + /// Reads a worksheet, handing each row to `each` in file order; an error from `each` stops the + /// read and is returned. A damaged worksheet gives the rows before the damage and says so + /// in [`SheetPart::damaged`] and in the warnings. + pub fn sheet(&mut self, entry: &SheetEntry, mut each: impl FnMut(Row) -> Result<(), Error>) -> Result { + let part = entry.part.clone().ok_or_else(|| Error::Invalid(format!("sheet \"{}\" has no part", entry.name)))?; + if self.binary.is_some() { + if let Some(types) = &self.binary { + xlsb::check_type(types, &part, xlsb::WORKSHEET)?; + } + let strings = match self.binary_strings { + Some(n) => n, + None => self.shared_strings()?.len(), + }; + let bytes = self.required(&part)?; + let mut sheet = xlsb::worksheet(&bytes, strings, &self.limits, &self.budget, &mut self.warnings, &mut each)?; + sheet.part = part; + return Ok(sheet); + } + let bytes = self.required(&part)?; + let rels = self.opc.rels(&part)?; + let mut rows = RowReader::default(); + let (root, err) = self.xml_options().parse_streaming_partial(&bytes, &["row"], |e| match rows.row(&e) { + Some(row) => each(row).map_err(Stop::Caller), + None => Ok(()), + }); + let damaged = match err { + Some(Stop::Caller(e)) => return Err(e), + Some(Stop::Xml(e)) => { + self.warn(format!("sheet \"{}\" is damaged ({e}); kept {} rows", entry.name, rows.rows)); + Some(e.to_string()) + } + None => None, + }; + if rows.dropped > 0 { + self.warn(format!("sheet \"{}\": {} cells or rows outside the 1,048,576 × 16,384 grid were dropped", entry.name, rows.dropped)); + } + let mut sheet = + SheetPart { part, root: root.unwrap_or_default(), rels, rows: rows.rows, dropped: rows.dropped, damaged, ..SheetPart::default() }; + sheet.lift(); + Ok(sheet) + } + + fn required(&mut self, part: &str) -> Result, Error> { + self.opc.read(part)?.ok_or_else(|| Error::Invalid(format!("missing part {part}"))) + } +} + +/// Why a streamed worksheet stopped: the XML, or the caller. +enum Stop { + Xml(craft_xml::Error), + Caller(Error), +} + +impl From for Stop { + fn from(e: craft_xml::Error) -> Self { + Stop::Xml(e) + } +} diff --git a/crates/xlsx/src/sheet.rs b/crates/xlsx/src/sheet.rs new file mode 100644 index 000000000..5375436e8 --- /dev/null +++ b/crates/xlsx/src/sheet.rs @@ -0,0 +1,387 @@ +//! A worksheet part (`CT_Worksheet`, ECMA-376 Part 1, 18.3): rows and cells as a stream, the +//! rest of the part as a tree. + +use std::sync::Arc; + +use craft_opc::Rels; +use craft_sheet::{CellError, CellRef, MAX_COLS, MAX_ROWS, Range, Value}; +use craft_xml::El; + +use crate::strings::{StringItem, decode_xstring}; + +/// What a cell's `` holds, by its type (`t`, 18.18.11). +#[derive(Clone, Debug, Default, PartialEq)] +pub enum CellValue { + #[default] + Empty, + Number(f64), + /// `t="s"`: an index into the shared strings. + Shared(u32), + /// `t="str"` (a formula's text result), `_xHHHH_` escapes decoded. + Text(Arc), + /// `t="inlineStr"`: the `` item, or the `` text when there is no ``. + Inline(StringItem), + Bool(bool), + /// `t="e"`. An error text no [`CellError`] has reads as `#VALUE!`. + Error(CellError), + /// `t="d"`: an ISO 8601 date, time or both, as written. See [`iso_date_serial`]. + Date(String), + /// A `` that does not read as its type says (a number that is not one, a shared-string + /// index that is not a number). Kept for the caller to report. + Invalid(String), +} + +impl CellValue { + /// The value a cell shows: shared strings looked up, dates as serial numbers in the + /// workbook's date system. A missing shared string, an unreadable date or an invalid value + /// is empty. + pub fn resolve(&self, strings: &[StringItem], date_1904: bool) -> Value { + match self { + CellValue::Empty | CellValue::Invalid(_) => Value::Empty, + CellValue::Number(n) => Value::number(*n), + CellValue::Shared(i) => strings.get(*i as usize).map_or(Value::Empty, |s| Value::Text(s.text.clone())), + CellValue::Text(t) => Value::Text(t.clone()), + CellValue::Inline(s) => Value::Text(s.text.clone()), + CellValue::Bool(b) => Value::Bool(*b), + CellValue::Error(e) => Value::Error(*e), + CellValue::Date(d) => iso_date_serial(d, date_1904).map_or(Value::Empty, Value::number), + } + } +} + +/// ``'s `t` (18.18.6). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum FormulaKind { + #[default] + Normal, + Shared, + Array, + /// A what-if data table: `r1`, `r2` and its flags are in [`CellFormula::attrs`]. + DataTable, +} + +/// A cell's ``. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct CellFormula { + pub kind: FormulaKind, + /// The formula as the file spells it (`_xlfn.` prefixes and all), trimmed. Empty for a + /// cell that shares the formula of its group's first cell. + pub text: String, + /// `si`: the shared formula group. + pub shared_index: Option, + /// `ref`: the range of an array formula or of a shared formula group. + pub range: Option, + /// Every attribute as written. + pub attrs: Vec<(String, String)>, +} + +/// One ``. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Cell { + pub at: CellRef, + /// `s`: an index into [`crate::Styles::cell_xfs`], 0 when absent. + pub style: u32, + pub value: CellValue, + pub formula: Option, + /// `vm`: value metadata (a rich value such as a picture in the cell), 1-based. + pub value_metadata: Option, + /// `cm`: cell metadata (a dynamic array formula), 1-based. + pub cell_metadata: Option, + /// `ph`: show the phonetic reading. + pub phonetic: bool, +} + +/// One `` and its cells. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Row { + /// Zero-based. + pub index: u32, + /// `ht`, points. + pub height: Option, + pub custom_height: bool, + pub hidden: bool, + pub outline_level: u8, + pub collapsed: bool, + /// `s` when `customFormat` is set: the format of the row's empty cells. + pub style: Option, + pub cells: Vec, +} + +/// One ``: columns `first..=last` (zero-based). +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Column { + pub first: u32, + pub last: u32, + /// Characters of the default font's maximum digit width, padding included. + pub width: Option, + pub custom_width: bool, + pub best_fit: bool, + pub hidden: bool, + pub outline_level: u8, + pub collapsed: bool, + pub style: Option, +} + +/// A worksheet after its rows were handed out: the rest of the part and what was lifted out of it. +#[derive(Clone, Debug, Default)] +pub struct SheetPart { + pub part: String, + /// The worksheet element without its rows: properties, views, columns, merged cells, + /// conditional formats, validation, hyperlinks, page setup, extensions. Empty for a binary + /// worksheet. + pub root: El, + pub rels: Rels, + pub columns: Vec, + pub merges: Vec, + /// Rows read. + pub rows: u64, + /// Rows and cells dropped because their position was outside the grid or unreadable. + pub dropped: u64, + /// Why reading stopped early, when the part is damaged; the rows before the damage were + /// handed out. + pub damaged: Option, +} + +impl SheetPart { + pub(crate) fn lift(&mut self) { + if let Some(cols) = self.root.child("cols") { + for c in cols.children("col") { + let (Some(min), Some(max)) = (c.attr_u32("min"), c.attr_u32("max")) else { continue }; + if min == 0 || min > MAX_COLS || max < min { + continue; + } + self.columns.push(Column { + first: min - 1, + last: max.min(MAX_COLS) - 1, + width: c.attr_f64("width"), + custom_width: c.flag("customWidth", false), + best_fit: c.flag("bestFit", false), + hidden: c.flag("hidden", false), + outline_level: c.attr_u32("outlineLevel").unwrap_or(0).min(7) as u8, + collapsed: c.flag("collapsed", false), + style: c.attr_u32("style"), + }); + } + } + if let Some(m) = self.root.child("mergeCells") { + self.merges.extend(m.children("mergeCell").filter_map(|m| m.attr("ref").and_then(Range::parse)).filter(|r| !r.is_single())); + } + } +} + +/// Reads rows one at a time, remembering where the last one ended. +#[derive(Default)] +pub(crate) struct RowReader { + next_row: u32, + pub(crate) rows: u64, + pub(crate) dropped: u64, +} + +impl RowReader { + /// `None` for a row outside the grid (counted in `dropped`). + pub(crate) fn row(&mut self, e: &El) -> Option { + self.rows += 1; + let index = match e.attr_u32("r") { + Some(r) if (1..=MAX_ROWS).contains(&r) => r - 1, + Some(_) => { + self.dropped += 1; + return None; + } + None if self.next_row < MAX_ROWS => self.next_row, + None => { + self.dropped += 1; + return None; + } + }; + self.next_row = index + 1; + let mut row = Row { + index, + height: e.attr_f64("ht"), + custom_height: e.flag("customHeight", false), + hidden: e.flag("hidden", false), + outline_level: e.attr_u32("outlineLevel").unwrap_or(0).min(7) as u8, + collapsed: e.flag("collapsed", false), + style: if e.flag("customFormat", false) { e.attr_u32("s") } else { None }, + cells: Vec::new(), + }; + let mut next_col = 0u32; + for c in e.children("c") { + let at = match c.attr("r") { + Some(a) => match CellRef::parse(a.trim()) { + Some(p) => p, + None => { + self.dropped += 1; + continue; + } + }, + None if next_col < MAX_COLS => CellRef::new(index, next_col), + None => { + self.dropped += 1; + continue; + } + }; + next_col = at.col + 1; + row.cells.push(cell(c, at)); + } + Some(row) + } +} + +fn cell(c: &El, at: CellRef) -> Cell { + let v = c.child("v").map(|v| v.text()); + let v = v.as_deref(); + let value = match c.attr("t").unwrap_or("n") { + "s" => match v.map(str::trim) { + Some(t) => t.parse().map_or_else(|_| CellValue::Invalid(t.to_string()), CellValue::Shared), + None => CellValue::Empty, + }, + "str" => v.map_or(CellValue::Empty, |v| CellValue::Text(decode_xstring(v).into())), + "inlineStr" => match c.child("is") { + Some(is) => CellValue::Inline(StringItem::read(is)), + None => v.map_or(CellValue::Empty, |v| CellValue::Text(decode_xstring(v).into())), + }, + "b" => v.map_or(CellValue::Empty, |v| CellValue::Bool(matches!(v.trim(), "1" | "true" | "TRUE"))), + "e" => v.map_or(CellValue::Empty, |v| CellValue::Error(CellError::parse(v.trim()).unwrap_or(CellError::Value))), + "d" => match v.map(str::trim).filter(|v| !v.is_empty()) { + Some(d) => CellValue::Date(d.to_string()), + None => CellValue::Empty, + }, + _ => match v.map(str::trim).filter(|v| !v.is_empty()) { + Some(t) => match t.parse::() { + Ok(n) if n.is_finite() => CellValue::Number(if n == 0.0 { 0.0 } else { n }), + _ => CellValue::Invalid(t.to_string()), + }, + None => CellValue::Empty, + }, + }; + let formula = c.child("f").map(|f| CellFormula { + kind: match f.attr("t") { + Some("shared") => FormulaKind::Shared, + Some("array") => FormulaKind::Array, + Some("dataTable") => FormulaKind::DataTable, + _ => FormulaKind::Normal, + }, + text: f.text().trim().to_string(), + shared_index: f.attr_u32("si"), + range: f.attr("ref").and_then(Range::parse), + attrs: f.attrs.clone(), + }); + Cell { + at, + style: c.attr_u32("s").unwrap_or(0), + value, + formula, + value_metadata: c.attr_u32("vm"), + cell_metadata: c.attr_u32("cm"), + phonetic: c.flag("ph", false), + } +} + +/// Days from 1970-01-01 to a proleptic Gregorian date (the civil-from-days inverse). +fn days_from_civil(y: i64, m: i64, d: i64) -> i64 { + let y = if m <= 2 { y - 1 } else { y }; + let era = y.div_euclid(400); + let yoe = y - era * 400; + let mp = (m + 9) % 12; + let doy = (153 * mp + 2) / 5 + d - 1; + let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; + era * 146_097 + doe - 719_468 +} + +/// The serial number of a `t="d"` value: `2024-02-29`, `2024-02-29T13:45:30`, `13:45` (a time +/// alone), a trailing `Z` or `+hh:mm` ignored. Years 1900 (1904 in the 1904 system) to 9999; +/// a day past the month's end counts on into the next month, as Excel's `DATE` does. In the +/// 1900 system, dates before 1 March 1900 are one lower than the day count, for the 29 February +/// 1900 that Excel counts. +pub fn iso_date_serial(s: &str, date_1904: bool) -> Option { + let s = s.trim().trim_end_matches('Z'); + let (date, time) = match s.split_once('T') { + Some((d, t)) => (d, Some(t)), + None if s.contains(':') => ("", Some(s)), + None => (s, None), + }; + let mut serial = 0.0; + if !date.is_empty() { + let mut it = date.split('-'); + let y: i64 = it.next()?.parse().ok()?; + let m: i64 = it.next()?.parse().ok()?; + let d: i64 = it.next()?.parse().ok()?; + if it.next().is_some() || !(1..=12).contains(&m) || !(1..=31).contains(&d) || y > 9999 || y < if date_1904 { 1904 } else { 1900 } { + return None; + } + let first = days_from_civil(y, m, 1); + let first = + if date_1904 { first - days_from_civil(1904, 1, 1) } else { first - days_from_civil(1899, 12, 30) - i64::from(y == 1900 && m <= 2) }; + let n = first + d - 1; + let max = days_from_civil(9999, 12, 31) - if date_1904 { days_from_civil(1904, 1, 1) } else { days_from_civil(1899, 12, 30) }; + if !(0..=max).contains(&n) { + return None; + } + serial = n as f64; + } + if let Some(t) = time { + let t = t.split('+').next().unwrap_or(t); + let mut it = t.split(':'); + let h: f64 = it.next()?.parse().ok()?; + let mi: f64 = it.next().unwrap_or("0").parse().ok()?; + let se: f64 = it.next().unwrap_or("0").parse().ok()?; + if !(0.0..24.0).contains(&h) || !(0.0..60.0).contains(&mi) || !(0.0..61.0).contains(&se) { + return None; + } + serial += (h * 3600.0 + mi * 60.0 + se) / 86400.0; + } + Some(serial) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn iso_dates() { + assert_eq!(iso_date_serial("1900-01-01", false), Some(1.0)); + assert_eq!(iso_date_serial("1900-02-28", false), Some(59.0)); + assert_eq!(iso_date_serial("1900-03-01", false), Some(61.0)); + assert_eq!(iso_date_serial("2024-02-29T12:00:00Z", false), Some(45351.5)); + assert_eq!(iso_date_serial("1904-01-02", true), Some(1.0)); + assert_eq!(iso_date_serial("06:00", false), Some(0.25)); + assert_eq!(iso_date_serial("2023-02-31", false), iso_date_serial("2023-03-03", false)); + assert_eq!(iso_date_serial("9999-12-31", false), Some(2_958_465.0)); + for bad in ["1899-12-31", "10000-01-01", "2024-13-01", "2024-01-00", "x", "2024-01-01T25:00", "2024-01-01-01"] { + assert_eq!(iso_date_serial(bad, false), None, "{bad}"); + } + assert_eq!(iso_date_serial("1903-12-31", true), None); + } + + #[test] + fn rows_and_cells() { + let xml = r#"7A1*24 + 1#SPILL!#WHAT1in_x000A_lineabca_x0009_2024-01-01"#; + let mut r = RowReader::default(); + let row = r.row(&craft_xml::Options::local().parse(xml.as_bytes()).unwrap_or_default()).unwrap_or_default(); + assert_eq!((row.index, row.height, row.custom_height, row.style, row.outline_level), (2, Some(20.0), true, Some(4), 7)); + assert_eq!(r.dropped, 1); + let at: Vec = row.cells.iter().map(|c| c.at.a1()).collect(); + assert_eq!(at, ["B3", "C3", "E3", "F3", "G3", "H3", "I3", "J3", "K3", "L3"]); + let values: Vec<&CellValue> = row.cells.iter().map(|c| &c.value).collect(); + assert_eq!(values[0], &CellValue::Shared(7)); + assert_eq!(values[2], &CellValue::Error(CellError::Spill)); + assert_eq!(values[3], &CellValue::Error(CellError::Value)); + assert_eq!(values[4], &CellValue::Bool(true)); + assert!(matches!(values[5], CellValue::Inline(s) if &*s.text == "in\nline")); + assert_eq!(values[6], &CellValue::Invalid("abc".into())); + assert_eq!(values[7], &CellValue::Text("a\t".into())); + assert_eq!(values[8].resolve(&[], false), Value::Number(45292.0)); + let f = row.cells[1].formula.clone().unwrap_or_default(); + assert_eq!((f.kind, f.text.as_str(), f.shared_index, f.range.map(|r| r.a1())), (FormulaKind::Shared, "A1*2", Some(0), Some("C3:C9".into()))); + let last = &row.cells[9]; + assert_eq!((last.value_metadata, last.cell_metadata, last.phonetic, last.style), (Some(1), Some(2), true, 0)); + assert_eq!(row.cells[0].value.resolve(&[], false), Value::Empty); + // Without `r`, the next row follows the last one. + let next = r.row(&craft_xml::El::new("row")).map(|r| r.index); + assert_eq!(next, Some(3)); + assert!(r.row(&craft_xml::El::new("row").with_attr("r", "1048577")).is_none()); + assert!(r.row(&craft_xml::El::new("row").with_attr("r", "0")).is_none()); + assert_eq!((r.rows, r.dropped), (4, 3)); + } +} diff --git a/crates/xlsx/src/strings.rs b/crates/xlsx/src/strings.rs new file mode 100644 index 000000000..641975032 --- /dev/null +++ b/crates/xlsx/src/strings.rs @@ -0,0 +1,196 @@ +//! String items (`CT_Rst`, ECMA-376 Part 1, 18.4): shared strings, inline strings and comment +//! text, with the `_xHHHH_` escapes of `ST_Xstring` (22.9.2.19). + +use std::sync::Arc; + +use craft_xml::El; + +use crate::styles::Font; + +/// A formatted run of a string item (``). +#[derive(Clone, Debug, PartialEq)] +pub struct TextRun { + pub text: String, + /// ``, `None` when the run has none. + pub font: Option, +} + +/// A phonetic reading over part of a string's text (``, 18.4.6). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PhoneticRun { + /// Offsets into the base text in UTF-16 code units, `start < end`, both within the text. + pub start: u32, + pub end: u32, + pub text: String, +} + +/// `` (18.4.7). `kind` and `alignment` are kept only when they are values the +/// schema defines (`ST_PhoneticType`, `ST_PhoneticAlignment`). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct PhoneticProps { + pub font_id: u32, + pub kind: Option, + pub alignment: Option, +} + +/// One string item: a shared string (``), an inline string (``) or comment text. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct StringItem { + /// The whole text, runs joined, phonetic readings left out. + pub text: Arc, + /// The formatted runs, empty for a plain string. + pub runs: Vec, + pub phonetic_runs: Vec, + pub phonetic: Option, + /// `` elements that were dropped: no offsets, `sb >= eb`, past the end of the text, or + /// no reading. + pub dropped_phonetic_runs: usize, +} + +impl StringItem { + pub(crate) fn read(e: &El) -> StringItem { + let text = rich_text(e); + let runs = e + .children("r") + .map(|r| TextRun { text: decode_xstring(&r.children("t").map(|t| t.text()).collect::()), font: r.child("rPr").map(Font::read) }) + .collect(); + // The length in UTF-16 code units: the widest reading of "the total length of the base + // text", so a run is only dropped when it cannot refer to the text at all. + let len = u32::try_from(text.encode_utf16().count()).unwrap_or(u32::MAX); + let mut phonetic_runs = Vec::new(); + let mut seen = 0usize; + for r in e.children("rPh") { + seen += 1; + let (Some(sb), Some(eb)) = (r.attr_u32("sb"), r.attr_u32("eb")) else { continue }; + let (start, end) = (sb.min(len), eb.min(len)); + let reading = r.child("t").map(|t| decode_xstring(&t.text())).unwrap_or_default(); + if start < end && !reading.is_empty() { + phonetic_runs.push(PhoneticRun { start, end, text: reading }); + } + } + const KINDS: &[&str] = &["halfwidthKatakana", "fullwidthKatakana", "Hiragana", "noConversion"]; + const ALIGNMENTS: &[&str] = &["noControl", "left", "center", "distributed"]; + let phonetic = e.child("phoneticPr").map(|p| PhoneticProps { + font_id: p.attr_u32("fontId").unwrap_or(0), + kind: p.attr("type").map(str::trim).filter(|v| KINDS.contains(v)).map(str::to_string), + alignment: p.attr("alignment").map(str::trim).filter(|v| ALIGNMENTS.contains(v)).map(str::to_string), + }); + StringItem { dropped_phonetic_runs: seen - phonetic_runs.len(), text: Arc::from(text), runs, phonetic_runs, phonetic } + } +} + +/// The text of a string item: `` and the `` of each ``, in order, with `_xHHHH_` +/// escapes decoded. Phonetic runs are not part of the text. +pub fn rich_text(e: &El) -> String { + let mut s = String::new(); + for c in e.elements() { + match c.name.as_str() { + "t" => s.push_str(&c.text()), + "r" => c.children("t").for_each(|t| s.push_str(&t.text())), + _ => {} + } + } + decode_xstring(&s) +} + +fn is_escape(chars: &[char], i: usize) -> bool { + chars.get(i) == Some(&'_') + && chars.get(i + 1) == Some(&'x') + && chars.get(i + 6) == Some(&'_') + && (i + 2..i + 6).all(|k| chars.get(k).is_some_and(char::is_ascii_hexdigit)) +} + +fn escape_unit(chars: &[char], i: usize) -> Option { + let hex: String = chars.get(i + 2..i + 6)?.iter().collect(); + u32::from_str_radix(&hex, 16).ok() +} + +/// Text for a `` or ``: characters XML cannot carry and control characters other than tab +/// and line feed become `_xHHHH_`, and a literal `_xHHHH_` is protected with `_x005F_`. The +/// result still needs XML escaping. +pub fn encode_xstring(s: &str) -> String { + let chars: Vec = s.chars().collect(); + let mut o = String::with_capacity(s.len()); + for (i, &c) in chars.iter().enumerate() { + if is_escape(&chars, i) { + o.push_str("_x005F_"); + } else if !craft_xml::is_xml_char(c) || (c.is_control() && !matches!(c, '\t' | '\n')) { + o.push_str(&format!("_x{:04X}_", c as u32 & 0xFFFF)); + } else { + o.push(c); + } + } + o +} + +/// The inverse of [`encode_xstring`]. Two escapes that form a UTF-16 surrogate pair are one +/// character; an escape that is no character on its own stays as written. +pub fn decode_xstring(s: &str) -> String { + if !s.contains("_x") { + return s.to_string(); + } + let chars: Vec = s.chars().collect(); + let mut o = String::with_capacity(s.len()); + let mut i = 0; + while let Some(&c) = chars.get(i) { + if is_escape(&chars, i) { + let unit = escape_unit(&chars, i); + if let Some(ch) = unit.and_then(char::from_u32) { + o.push(ch); + i += 7; + continue; + } + let pair = unit.filter(|h| (0xD800..=0xDBFF).contains(h) && is_escape(&chars, i + 7)).and_then(|high| { + let low = escape_unit(&chars, i + 7).filter(|l| (0xDC00..=0xDFFF).contains(l))?; + char::from_u32(0x10000 + ((high - 0xD800) << 10) + (low - 0xDC00)) + }); + if let Some(ch) = pair { + o.push(ch); + i += 14; + continue; + } + } + o.push(c); + i += 1; + } + o +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn xstring() { + assert_eq!(decode_xstring("a_x000D_b"), "a\rb"); + assert_eq!(encode_xstring("a\rb"), "a_x000D_b"); + assert_eq!(encode_xstring("_x000D_"), "_x005F_x000D_"); + assert_eq!(decode_xstring("_x005F_x000D_"), "_x000D_"); + assert_eq!(decode_xstring("_x12"), "_x12"); + assert_eq!(decode_xstring("_xD83E__xDD80_"), "\u{1F980}"); + // A lone surrogate is no character: kept as written. + assert_eq!(decode_xstring("_xD83E_!"), "_xD83E_!"); + for s in ["plain", "tab\tline\n", "\u{1}\u{1F}", "_x0041_ _xZZZZ_ é"] { + assert_eq!(decode_xstring(&encode_xstring(s)), s); + } + } + + fn item(xml: &str) -> StringItem { + StringItem::read(&craft_xml::Options::local().parse(xml.as_bytes()).unwrap_or_default()) + } + + #[test] + fn runs_and_phonetics() { + let si = item( + r#"東京 トウキョウxx"#, + ); + assert_eq!(&*si.text, "東京 "); + assert_eq!(si.runs.len(), 2); + assert_eq!(si.runs[1].font.as_ref().map(|f| (f.bold, f.name.as_deref(), f.size)), Some((Some(true), Some("Arial"), Some(9.0)))); + assert_eq!(si.phonetic_runs, [PhoneticRun { start: 0, end: 2, text: "トウキョウ".into() }]); + assert_eq!(si.dropped_phonetic_runs, 3); + assert_eq!(si.phonetic, Some(PhoneticProps { font_id: 3, kind: Some("Hiragana".into()), alignment: None })); + let plain = item("a_x000A_b"); + assert_eq!((&*plain.text, plain.runs.len()), ("a\nb", 0)); + } +} diff --git a/crates/xlsx/src/styles.rs b/crates/xlsx/src/styles.rs new file mode 100644 index 000000000..ceb9547f0 --- /dev/null +++ b/crates/xlsx/src/styles.rs @@ -0,0 +1,555 @@ +//! The styles part (`xl/styles.xml`, ECMA-376 Part 1, 18.8) as the file has it: tables that +//! cells refer to by index. Attributes a file may leave out are `Option`s, because a +//! differential format (``) states only what it changes. + +use craft_sheet::{BorderStyle, DEFAULT_PALETTE, HAlign, Pattern, Script, Underline, VAlign, builtin_format}; +use craft_xml::El; + +/// Characters of a font name kept (Excel allows 31; damaged files have far more). +const MAX_FONT_NAME_CHARS: usize = 255; + +/// Where a colour comes from (`CT_Color`, 18.8.9). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum ColorSource { + /// `auto="1"`, or no attribute that names a colour. + #[default] + Auto, + /// `rgb`: `0xAARRGGBB`. Six hex digits read as opaque. + Rgb(u32), + /// `theme`: an index into the theme's colour scheme. + Theme(u32), + /// `indexed`: an index into [`Styles::palette`]. + Indexed(u32), +} + +/// A colour with its tint (`-1.0` darkest to `1.0` lightest, 0 for none). +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Color { + pub source: ColorSource, + pub tint: f64, +} + +impl Color { + pub fn read(e: &El) -> Color { + let source = if e.flag("auto", false) { + ColorSource::Auto + } else if let Some(rgb) = e.attr("rgb") { + parse_argb(rgb).map_or(ColorSource::Auto, ColorSource::Rgb) + } else if let Some(t) = e.attr_u32("theme") { + ColorSource::Theme(t) + } else if let Some(i) = e.attr_u32("indexed") { + ColorSource::Indexed(i) + } else { + ColorSource::Auto + }; + Color { source, tint: e.attr_f64("tint").map_or(0.0, |t| t.clamp(-1.0, 1.0)) } + } + + /// `0xRRGGBB` for an RGB colour, or an indexed one in `palette`; `None` for automatic and + /// theme colours. The tint is not applied. + pub fn rgb(&self, palette: &[u32]) -> Option { + match self.source { + ColorSource::Rgb(v) => Some(v & 0xFF_FFFF), + ColorSource::Indexed(i) => palette.get(i as usize).copied(), + ColorSource::Auto | ColorSource::Theme(_) => None, + } + } +} + +fn parse_argb(s: &str) -> Option { + let h = s.trim().trim_start_matches('#'); + let v = u32::from_str_radix(h, 16).ok().filter(|_| h.bytes().all(|b| b.is_ascii_hexdigit()))?; + match h.len() { + 8 => Some(v), + 6 => Some(0xFF00_0000 | v), + _ => None, + } +} + +/// `scheme` of a font: which theme font it follows. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FontScheme { + None, + Major, + Minor, +} + +/// A font (``, or a run's ``, 18.8.22 and 18.4.7). +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Font { + pub name: Option, + /// Points. + pub size: Option, + pub bold: Option, + pub italic: Option, + pub strike: Option, + pub outline: Option, + pub shadow: Option, + pub condense: Option, + pub extend: Option, + pub underline: Option, + pub vert_align: Option