diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 283af50e1..e40c1b4bc 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -23,5 +23,5 @@ jobs: # 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,craft-image-codecs/sgi + craft-image-codecs/avif,craft-image-codecs/avif-decode,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,craft-image-codecs/sgi,craft-image-codecs/sunraster,craft-jsonrpc/async-lines secrets: inherit diff --git a/Cargo.lock b/Cargo.lock index 0728ab862..3981cb49c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,8 @@ version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad442f58ee04714aaa0ba0a2768c1ea1935b29507bb22ddece8cbbc76db02932" dependencies = [ + "enumn", + "serde", "uuid", ] @@ -115,19 +117,11 @@ checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" dependencies = [ "cfg-if", "once_cell", + "serde", "version_check", "zerocopy", ] -[[package]] -name = "aho-corasick" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" -dependencies = [ - "memchr", -] - [[package]] name = "aligned" version = "0.4.3" @@ -222,66 +216,6 @@ dependencies = [ "libc", ] -[[package]] -name = "annotate-snippets" -version = "0.11.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "710e8eae58854cdc1790fcb56cca04d712a17be849eeb81da2a724bf4bae2bc4" -dependencies = [ - "anstyle", - "unicode-width", -] - -[[package]] -name = "anstream" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" - -[[package]] -name = "anstyle-parse" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" -dependencies = [ - "windows-sys 0.61.2", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" -dependencies = [ - "anstyle", - "once_cell_polyfill", - "windows-sys 0.61.2", -] - [[package]] name = "anyhow" version = "1.0.104" @@ -294,27 +228,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" -[[package]] -name = "arboard" -version = "3.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aeb9553e62312c8756a5979254fc6d7da8bbeb5986a1e17037fab932f7037917" -dependencies = [ - "clipboard-win", - "image", - "log", - "objc2 0.6.5", - "objc2-app-kit 0.3.2", - "objc2-core-foundation", - "objc2-core-graphics", - "objc2-foundation 0.3.2", - "parking_lot", - "percent-encoding", - "windows-sys 0.61.2", - "wl-clipboard-rs", - "x11rb", -] - [[package]] name = "arg_enum_proc_macro" version = "0.3.4" @@ -663,25 +576,6 @@ version = "1.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" -[[package]] -name = "bindgen" -version = "0.72.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895" -dependencies = [ - "annotate-snippets", - "bitflags 2.13.2", - "cexpr", - "clang-sys", - "itertools 0.13.0", - "proc-macro2", - "quote", - "regex", - "rustc-hash 2.1.3", - "shlex 1.3.0", - "syn 2.0.119", -] - [[package]] name = "bit-set" version = "0.10.0" @@ -801,12 +695,6 @@ dependencies = [ "cpufeatures 0.3.1", ] -[[package]] -name = "block" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" - [[package]] name = "block-buffer" version = "0.10.4" @@ -1033,16 +921,6 @@ version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" -[[package]] -name = "calendrical_calculations" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5abbd6eeda6885048d357edc66748eea6e0268e3dd11f326fff5bd248d779c26" -dependencies = [ - "core_maths", - "displaydoc", -] - [[package]] name = "calloop" version = "0.13.0" @@ -1118,16 +996,7 @@ dependencies = [ "find-msvc-tools", "jobserver", "libc", - "shlex 2.0.1", -] - -[[package]] -name = "cexpr" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" -dependencies = [ - "nom 7.1.3", + "shlex", ] [[package]] @@ -1150,16 +1019,6 @@ dependencies = [ "cipher 0.4.4", ] -[[package]] -name = "cfg-expr" -version = "0.20.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ba9e9ec16c447027685b1f897b720e18e9a8afd00bd7332c483537e38086c9f" -dependencies = [ - "smallvec", - "target-lexicon", -] - [[package]] name = "cfg-if" version = "1.0.5" @@ -1237,66 +1096,6 @@ dependencies = [ "inout 0.2.6", ] -[[package]] -name = "clang-sys" -version = "1.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "157a8ba7b480713b56f4c09fd13fc3e0a22a5dfab8097ba61cbc5feef950788a" -dependencies = [ - "glob", - "libc", - "libloading", -] - -[[package]] -name = "clap" -version = "4.6.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" -dependencies = [ - "clap_builder", - "clap_derive", -] - -[[package]] -name = "clap_builder" -version = "4.6.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" -dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_derive" -version = "4.6.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn 3.0.6", -] - -[[package]] -name = "clap_lex" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" - -[[package]] -name = "clipboard-win" -version = "5.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052af7878466feccda12ba7a6a67d019d712f296809559b47c12c8cc196d3a9a" -dependencies = [ - "error-code", -] - [[package]] name = "cmac" version = "0.7.2" @@ -1395,10 +1194,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" [[package]] -name = "colorchoice" -version = "1.0.5" +name = "colored" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" +dependencies = [ + "lazy_static", + "windows-sys 0.59.0", +] [[package]] name = "combine" @@ -1446,12 +1249,6 @@ dependencies = [ "unicode-segmentation", ] -[[package]] -name = "cookie-factory" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9885fa71e26b8ab7855e2ec7cae6e9b380edff76cd052e07c683a0319d51b3a2" - [[package]] name = "core-foundation" version = "0.9.4" @@ -1502,19 +1299,6 @@ dependencies = [ "libc", ] -[[package]] -name = "core-graphics2" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b65858d95344d2711161cd92196732f67d6c5fea238cf12f994584828731a5c" -dependencies = [ - "bitflags 2.13.2", - "block", - "cfg-if", - "core-foundation 0.10.1", - "libc", -] - [[package]] name = "core_detect" version = "1.0.0" @@ -1554,176 +1338,6 @@ dependencies = [ "libc", ] -[[package]] -name = "craft" -version = "0.1.0" -dependencies = [ - "craft-aaf", - "craft-aep", - "craft-affinity", - "craft-ai-agents", - "craft-ai-core", - "craft-anicraft-format", - "craft-assuan", - "craft-atomic", - "craft-audio", - "craft-automation", - "craft-behavior", - "craft-ber", - "craft-browse", - "craft-captions", - "craft-cfb", - "craft-cms", - "craft-codepage", - "craft-color", - "craft-control-protocol", - "craft-crs", - "craft-crw", - "craft-csv", - "craft-dav", - "craft-deckcraft-format", - "craft-designcraft-format", - "craft-dialogs", - "craft-doc", - "craft-doc-binary", - "craft-docpath", - "craft-document-bundle", - "craft-docx", - "craft-dpx", - "craft-draw", - "craft-drawingml", - "craft-dwg", - "craft-dxf", - "craft-ecproj-format", - "craft-eps", - "craft-epub", - "craft-ews", - "craft-exr", - "craft-fcproj-format", - "craft-fig", - "craft-figcraft-format", - "craft-figma-rest", - "craft-file-formats", - "craft-file-import", - "craft-flags", - "craft-flow", - "craft-font-catalog", - "craft-font-manager", - "craft-font-store", - "craft-font-subset", - "craft-gainmap", - "craft-gcode", - "craft-gif", - "craft-gridcraft-format", - "craft-gzip", - "craft-hdr", - "craft-heif", - "craft-host", - "craft-html", - "craft-http-url", - "craft-i18n", - "craft-ical", - "craft-idml", - "craft-image-codecs", - "craft-imaging", - "craft-indd", - "craft-jmap", - "craft-jpeg", - "craft-jsonrpc", - "craft-jxl", - "craft-keymap", - "craft-kiwi", - "craft-kra", - "craft-latex", - "craft-layers", - "craft-lottie", - "craft-markdown", - "craft-materialx", - "craft-math", - "craft-mcmat", - "craft-mcp", - "craft-media", - "craft-menu", - "craft-mesh", - "craft-metadata", - "craft-metafile", - "craft-midi", - "craft-mime", - "craft-odf", - "craft-ods", - "craft-offcrypto", - "craft-opc", - "craft-openpgp", - "craft-ora", - "craft-pages", - "craft-pcraft-bundle", - "craft-pcraft-format", - "craft-pdf 0.1.0", - "craft-pdn", - "craft-penpot", - "craft-pkcs7", - "craft-platform", - "craft-plist", - "craft-plotter", - "craft-pptx", - "craft-prc", - "craft-prepress", - "craft-print", - "craft-proof", - "craft-proto", - "craft-ps", - "craft-psd", - "craft-pst", - "craft-quit", - "craft-raster-codecs", - "craft-raw", - "craft-review", - "craft-rtf", - "craft-scraft-format", - "craft-settings", - "craft-sf2", - "craft-sheet", - "craft-shx", - "craft-silhouette-studio", - "craft-sketch", - "craft-smime", - "craft-stencilcraft-document", - "craft-stencilcraft-format", - "craft-stream-codecs", - "craft-svg", - "craft-table", - "craft-tcraft", - "craft-text", - "craft-texture", - "craft-tga", - "craft-theme", - "craft-theme-store", - "craft-tiff", - "craft-time", - "craft-timeline", - "craft-ui", - "craft-update", - "craft-usd", - "craft-vcard", - "craft-vectorcraft-format", - "craft-vectordrawable", - "craft-video", - "craft-web-api", - "craft-web-host", - "craft-webp", - "craft-window", - "craft-wordmodel", - "craft-xcf", - "craft-xfl", - "craft-xls", - "craft-xlsx", - "craft-xml", - "craft-y4m", - "craft-zip", - "egui", - "toml", -] - [[package]] name = "craft-aaf" version = "0.1.0" @@ -1750,16 +1364,6 @@ dependencies = [ "sha2 0.10.9", ] -[[package]] -name = "craft-ai-agents" -version = "0.1.0" -source = "git+https://github.com/storytold/craft-ai-integrations?rev=35e0352666a117407d62230a6c6c2366f3e21a3b#35e0352666a117407d62230a6c6c2366f3e21a3b" -dependencies = [ - "dirs", - "serde", - "serde_json", -] - [[package]] name = "craft-ai-core" version = "0.1.0" @@ -1808,70 +1412,10 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "craft-automation" -version = "0.1.0" -dependencies = [ - "base64 0.22.1", - "craft-ai-agents", - "craft-atomic", - "craft-behavior", - "craft-flags", - "craft-host", - "craft-keymap", - "craft-platform", - "craft-theme", - "craft-ui", - "egui", - "getrandom 0.3.4", - "hmac 0.12.1", - "log", - "png 0.17.16", - "serde", - "serde_json", - "sha2 0.10.9", - "tempfile", - "thiserror 2.0.21", -] - -[[package]] -name = "craft-behavior" -version = "0.1.0" -dependencies = [ - "accesskit", - "bitflags 2.13.2", - "craft-host", - "craft-keymap", - "craft-theme", - "emath", - "kurbo", - "serde", - "serde_json", - "smallvec", -] - [[package]] name = "craft-ber" version = "0.1.0" -[[package]] -name = "craft-browse" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-docpath", - "craft-host", - "craft-i18n", - "craft-platform", - "craft-settings", - "craft-theme", - "craft-ui", - "egui", - "kurbo", - "serde", - "tempfile", -] - [[package]] name = "craft-captions" version = "0.1.0" @@ -1907,26 +1451,6 @@ dependencies = [ "encoding_rs", ] -[[package]] -name = "craft-color" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-cms", - "craft-host", - "craft-i18n", - "craft-keymap", - "craft-platform", - "craft-settings", - "craft-theme", - "craft-ui", - "craft-window", - "egui", - "egui_kittest", - "serde", - "serde_json", -] - [[package]] name = "craft-control-protocol" version = "0.1.0" @@ -1948,6 +1472,7 @@ dependencies = [ name = "craft-crw" version = "0.1.0" dependencies = [ + "proptest", "serde", "serde_json", "sha2 0.10.9", @@ -1980,19 +1505,6 @@ dependencies = [ "zip", ] -[[package]] -name = "craft-design-lint" -version = "0.1.0" -dependencies = [ - "proc-macro2", - "quote", - "serde", - "serde_json", - "syn 2.0.119", - "toml", - "toml_edit", -] - [[package]] name = "craft-designcraft-format" version = "0.1.0" @@ -2009,21 +1521,11 @@ name = "craft-dialogs" version = "0.1.0" dependencies = [ "block2 0.6.2", - "craft-atomic", - "craft-behavior", - "craft-host", - "craft-i18n", - "craft-platform", - "craft-ui", - "egui", - "futures-lite", - "log", "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-foundation 0.3.2", "raw-window-handle", "rfd", - "zbus", ] [[package]] @@ -2035,6 +1537,7 @@ dependencies = [ "craft-pcraft-format", "craft-vectorcraft-format", "flate2", + "proptest", "ruzstd", "serde", "serde_json", @@ -2062,9 +1565,6 @@ dependencies = [ [[package]] name = "craft-docpath" version = "0.1.0" -dependencies = [ - "serde", -] [[package]] name = "craft-document-bundle" @@ -2107,30 +1607,12 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "craft-draw" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-color", - "craft-host", - "craft-i18n", - "craft-keymap", - "craft-settings", - "craft-theme", - "craft-ui", - "egui", - "kurbo", - "serde", - "serde_json", -] - [[package]] name = "craft-drawingml" version = "0.1.0" dependencies = [ "craft-xml", - "kurbo", + "kurbo 0.13.1", "log", "serde", "serde_json", @@ -2150,7 +1632,7 @@ version = "0.1.0" dependencies = [ "craft-codepage", "encoding_rs", - "kurbo", + "kurbo 0.13.1", "log", "thiserror 2.0.21", ] @@ -2164,11 +1646,20 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-elf" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-eps" version = "0.1.0" dependencies = [ - "kurbo", + "kurbo 0.13.1", "thiserror 2.0.21", "vectorcraft-doc", "vectorcraft-eps", @@ -2220,7 +1711,7 @@ dependencies = [ "craft-kiwi", "craft-zip", "flate2", - "kurbo", + "kurbo 0.13.1", "ruzstd", "serde_json", "thiserror 2.0.21", @@ -2274,7 +1765,7 @@ dependencies = [ "craft-pdf 0.1.0", "craft-raster-codecs", "craft-svg", - "kurbo", + "kurbo 0.13.1", "thiserror 2.0.21", "vectorcraft-geom", ] @@ -2304,7 +1795,7 @@ version = "0.1.0" dependencies = [ "fontique", "harfrust 0.13.3", - "kurbo", + "kurbo 0.13.1", "skrifa 0.47.0", "thiserror 2.0.21", "unicode-bidi", @@ -2317,19 +1808,17 @@ dependencies = [ name = "craft-font-manager" version = "0.1.0" dependencies = [ - "craft-behavior", "craft-font-catalog", - "craft-font-manager", "craft-font-store", - "craft-host", "craft-i18n", - "craft-theme", "craft-ui", "egui", + "egui_kittest", + "egui_system_font_provider", "epaint_default_fonts", + "image", "log", "serde", - "skrifa 0.44.0", ] [[package]] @@ -2353,27 +1842,13 @@ dependencies = [ "write-fonts 0.54.1", ] -[[package]] -name = "craft-font-subset" -version = "0.1.0" -dependencies = [ - "brotli 9.1.1", - "harfbuzz-subset-sys", - "rustybuzz", - "serde_json", - "skrifa 0.49.0", - "thiserror 2.0.21", - "unicode-normalization", - "write-fonts 0.55.0", - "wuff", -] - [[package]] name = "craft-fonts-build" version = "0.1.0" dependencies = [ - "craft-font-subset", "craft-fonts-build-consumer", + "skera", + "skrifa 0.48.0", ] [[package]] @@ -2383,6 +1858,16 @@ dependencies = [ "craft-fonts-build", ] +[[package]] +name = "craft-format" +version = "0.1.0" +dependencies = [ + "craft-zip", + "proptest", + "serde_json", + "sha2 0.10.9", +] + [[package]] name = "craft-gainmap" version = "0.1.0" @@ -2433,21 +1918,10 @@ name = "craft-heif" version = "0.1.0" dependencies = [ "heic-rs", + "proptest", "rayon", ] -[[package]] -name = "craft-host" -version = "0.1.0" -dependencies = [ - "bitflags 2.13.2", - "craft-i18n", - "egui", - "serde", - "toml", - "web-time", -] - [[package]] name = "craft-html" version = "0.1.0" @@ -2469,11 +1943,8 @@ version = "0.1.0" name = "craft-i18n" version = "0.1.0" dependencies = [ - "chrono", "fixed_decimal", - "icu_calendar", "icu_collator", - "icu_datetime", "icu_decimal", "icu_locale_core", "icu_locale_fallback", @@ -2483,7 +1954,6 @@ dependencies = [ "icu_provider", "icu_provider_adapters", "icu_provider_blob", - "icu_time", "log", "objc2-foundation 0.3.2", "sys-locale", @@ -2544,24 +2014,6 @@ dependencies = [ "zune-jpegxl", ] -[[package]] -name = "craft-imaging" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-color", - "craft-host", - "craft-i18n", - "craft-keymap", - "craft-theme", - "craft-ui", - "egui", - "egui_kittest", - "kurbo", - "serde", - "serde_json", -] - [[package]] name = "craft-indd" version = "0.1.0" @@ -2610,13 +2062,15 @@ dependencies = [ name = "craft-keymap" version = "0.1.0" dependencies = [ - "craft-host", "craft-i18n", "craft-settings", - "craft-xml", + "craft-ui", "egui", + "egui_kittest", + "image", "serde", "serde_json", + "sha2 0.10.9", "tempfile", ] @@ -2646,20 +2100,6 @@ dependencies = [ "proptest", ] -[[package]] -name = "craft-layers" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-i18n", - "craft-keymap", - "craft-theme", - "craft-ui", - "egui", - "serde", - "serde_json", -] - [[package]] name = "craft-lottie" version = "0.1.0" @@ -2669,6 +2109,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-macho" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-markdown" version = "0.1.0" @@ -2714,6 +2163,14 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-mcp-server" +version = "0.1.0" +dependencies = [ + "proptest", + "serde_json", +] + [[package]] name = "craft-media" version = "0.1.0" @@ -2734,20 +2191,12 @@ version = "0.1.0" name = "craft-menu" version = "0.1.0" dependencies = [ - "craft-behavior", - "craft-host", - "craft-i18n", "craft-keymap", "craft-quit", - "craft-theme", - "craft-ui", - "craft-window", "egui", + "muda", "objc2 0.6.5", "objc2-app-kit 0.3.2", - "objc2-foundation 0.3.2", - "serde", - "serde_json", ] [[package]] @@ -2756,6 +2205,7 @@ version = "0.1.0" dependencies = [ "craft-opc", "craft-xml", + "craft-zip", "miniz_oxide 0.9.1", "serde_json", ] @@ -2780,7 +2230,7 @@ version = "0.1.0" dependencies = [ "encoding_rs", "image", - "kurbo", + "kurbo 0.13.1", "thiserror 2.0.21", ] @@ -2893,20 +2343,6 @@ dependencies = [ "zip", ] -[[package]] -name = "craft-pages" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-i18n", - "craft-text", - "craft-theme", - "craft-ui", - "egui", - "kurbo", - "serde", -] - [[package]] name = "craft-pcraft-bundle" version = "0.1.0" @@ -2946,11 +2382,10 @@ dependencies = [ "js-sys", "krilla", "krilla-svg", - "kurbo", + "kurbo 0.13.1", "md-5 0.11.0", "proptest", "roxmltree 0.21.2", - "serde", "serde_json", "sha2 0.11.1", "stringprep", @@ -2982,6 +2417,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-pe" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-penpot" version = "0.1.0" @@ -3041,33 +2485,16 @@ dependencies = [ name = "craft-platform" version = "0.1.0" dependencies = [ - "arboard", "async-channel", "async-io", "async-process", - "clipboard-win", - "core-graphics", - "core-graphics2", - "craft-atomic", - "craft-docpath", - "craft-host", - "craft-theme", + "egui", "futures-lite", "js-sys", - "log", - "objc2-app-kit 0.3.2", - "objc2-foundation 0.3.2", - "serde", - "serde_json", - "sysinfo", "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", "web-sys", - "web-time", - "wl-clipboard-rs", - "x11rb", - "xcap", "zbus", ] @@ -3093,7 +2520,7 @@ dependencies = [ "craft-drawingml", "craft-opc", "craft-xml", - "kurbo", + "kurbo 0.13.1", "log", "serde", "serde_json", @@ -3116,7 +2543,7 @@ dependencies = [ "craft-cms", "craft-pdf 0.1.0", "craft-plate-test", - "kurbo", + "kurbo 0.13.1", "linesweeper", "thiserror 2.0.21", "ttf-parser", @@ -3126,14 +2553,10 @@ dependencies = [ name = "craft-print" version = "0.1.0" dependencies = [ - "craft-behavior", - "craft-ui", "craft-winprint-gdi", - "egui", "log", "miniz_oxide 0.9.1", "roxmltree 0.21.2", - "serde_json", "tempfile", "winprint", "winsafe", @@ -3165,7 +2588,8 @@ name = "craft-ps" version = "0.1.0" dependencies = [ "craft-eps", - "kurbo", + "flate2", + "kurbo 0.13.1", "miniz_oxide 0.8.9", "pdf-writer", "skrifa 0.47.0", @@ -3198,6 +2622,7 @@ version = "0.1.0" dependencies = [ "dispatch2", "egui", + "muda", "objc2 0.6.5", "objc2-app-kit 0.3.2", ] @@ -3234,20 +2659,10 @@ dependencies = [ ] [[package]] -name = "craft-review" +name = "craft-render-parity" version = "0.1.0" dependencies = [ - "craft-behavior", - "craft-host", - "craft-i18n", - "craft-keymap", - "craft-theme", - "craft-ui", - "egui", - "image", - "kurbo", - "serde", - "serde_json", + "craft-cms", ] [[package]] @@ -3283,13 +2698,9 @@ name = "craft-settings" version = "0.1.0" dependencies = [ "craft-atomic", - "craft-host", - "craft-settings", "dirs", "serde", - "serde_json", "tempfile", - "toml", "toml_edit", ] @@ -3313,7 +2724,7 @@ version = "0.1.0" name = "craft-silhouette-studio" version = "0.1.0" dependencies = [ - "kurbo", + "kurbo 0.13.1", "thiserror 2.0.21", ] @@ -3323,7 +2734,7 @@ version = "0.1.0" dependencies = [ "craft-figma-rest", "craft-zip", - "kurbo", + "kurbo 0.13.1", "serde_json", "thiserror 2.0.21", "zip", @@ -3360,6 +2771,10 @@ dependencies = [ "zeroize", ] +[[package]] +name = "craft-sniff" +version = "0.1.0" + [[package]] name = "craft-stencilcraft-document" version = "0.1.0" @@ -3367,7 +2782,7 @@ dependencies = [ "craft-atomic", "craft-stencilcraft-format", "craft-svg", - "kurbo", + "kurbo 0.13.1", "serde", "serde_json", "thiserror 2.0.21", @@ -3402,7 +2817,7 @@ dependencies = [ "craft-xml", "data-url", "flate2", - "kurbo", + "kurbo 0.13.1", "quick-xml 0.41.0", "resvg 0.48.1", "roxmltree 0.21.2", @@ -3417,19 +2832,14 @@ name = "craft-svg-paths" version = "0.1.0" dependencies = [ "craft-svg", - "kurbo", + "kurbo 0.13.1", ] [[package]] -name = "craft-table" +name = "craft-tar" version = "0.1.0" dependencies = [ - "craft-behavior", - "craft-keymap", - "craft-table", - "craft-theme", - "craft-ui", - "egui", + "serde", ] [[package]] @@ -3446,20 +2856,10 @@ dependencies = [ name = "craft-text" version = "0.1.0" dependencies = [ - "craft-behavior", - "craft-font-manager", - "craft-host", - "craft-keymap", - "craft-theme", - "craft-ui", - "egui", "hypher", "icu_segmenter", + "kurbo 0.13.1", "miniz_oxide 0.8.9", - "regex", - "regex-syntax", - "serde", - "serde_json", "unicode-bidi", "unicode-bidi-mirroring", "unicode-general-category", @@ -3484,34 +2884,6 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "craft-theme" -version = "0.1.0" -dependencies = [ - "bitflags 2.13.2", - "ecolor", - "log", - "proptest", - "serde", - "serde_json", - "smallvec", -] - -[[package]] -name = "craft-theme-store" -version = "0.1.0" -dependencies = [ - "craft-atomic", - "craft-behavior", - "craft-theme", - "craft-ui", - "dirs", - "egui", - "serde", - "serde_json", - "tempfile", -] - [[package]] name = "craft-tiff" version = "0.1.0" @@ -3535,242 +2907,39 @@ dependencies = [ name = "craft-timeline" version = "0.1.0" dependencies = [ - "craft-behavior", - "craft-keymap", - "craft-theme", "craft-time", - "craft-ui", "craft-xml", - "egui", "proptest", "serde", "serde_json", - "web-time", ] [[package]] name = "craft-ui" version = "0.1.0" dependencies = [ - "craft-behavior", - "craft-i18n", - "craft-ui", - "craft-ui-canvas", - "craft-ui-contract", - "craft-ui-panels", - "craft-ui-patterns", - "craft-ui-shell", - "craft-ui-widgets", - "egui", - "syn 2.0.119", - "toml", -] - -[[package]] -name = "craft-ui-bench" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-host", - "craft-ui", - "craft-window", - "egui", - "serde", - "serde_json", -] - -[[package]] -name = "craft-ui-canvas" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-i18n", - "craft-keymap", - "craft-theme", - "craft-ui-canvas", - "craft-ui-macros", - "craft-ui-panels", - "craft-ui-test-kit", - "egui", - "serde", - "serde_json", -] - -[[package]] -name = "craft-ui-contract" -version = "0.1.0" -dependencies = [ - "accesskit", - "craft-behavior", - "craft-font-catalog", "craft-fonts-build", - "craft-host", "craft-i18n", - "craft-keymap", - "craft-platform", - "craft-quit", - "craft-settings", - "craft-svg", - "craft-theme", - "craft-ui-contract", - "craft-ui-macros", - "craft-ui-test-kit", - "craft-window", + "eframe", "egui", + "egui_extras", "egui_kittest", "epaint", - "epaint_default_fonts", - "flate2", "fontique", "image", - "inventory", - "libtest-mimic", + "js-sys", "log", - "proc-macro2", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-foundation 0.3.2", "serde", + "serde_json", "skrifa 0.44.0", - "smallvec", - "syn 2.0.119", - "toml", - "web-time", -] - -[[package]] -name = "craft-ui-gallery" -version = "0.1.0" -dependencies = [ - "craft-automation", - "craft-behavior", - "craft-browse", - "craft-color", - "craft-dialogs", - "craft-draw", - "craft-font-manager", - "craft-i18n", - "craft-imaging", - "craft-layers", - "craft-menu", - "craft-pages", - "craft-print", - "craft-review", - "craft-settings", - "craft-table", - "craft-text", - "craft-theme", - "craft-theme-store", - "craft-timeline", - "craft-ui", - "craft-update", - "craft-window", - "eframe", - "egui", - "proc-macro2", - "quote", - "serde", - "serde_json", - "syn 2.0.119", + "unicode-script", + "wasm-bindgen", "wasm-bindgen-futures", - "web-time", -] - -[[package]] -name = "craft-ui-macros" -version = "0.1.0" -dependencies = [ - "proc-macro-crate", - "proc-macro2", - "quote", - "syn 2.0.119", -] - -[[package]] -name = "craft-ui-panels" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-host", - "craft-i18n", - "craft-keymap", - "craft-quit", - "craft-theme", - "craft-ui-macros", - "craft-ui-panels", - "craft-ui-widgets", - "egui", - "serde", - "serde_json", - "toml", -] - -[[package]] -name = "craft-ui-patterns" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-host", - "craft-i18n", - "craft-keymap", - "craft-pdf 0.1.0", - "craft-theme", - "craft-ui-macros", - "craft-ui-panels", - "craft-ui-patterns", - "craft-ui-test-kit", - "egui", - "serde", - "serde_json", - "toml", - "toml_edit", - "zeroize", -] - -[[package]] -name = "craft-ui-shell" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-host", - "craft-i18n", - "craft-platform", - "craft-svg", - "craft-theme", - "craft-ui-macros", - "craft-ui-panels", - "craft-ui-shell", - "craft-ui-test-kit", - "craft-window", - "eframe", - "egui", - "serde", - "web-time", -] - -[[package]] -name = "craft-ui-test-kit" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-host", - "craft-ui-contract", - "egui", -] - -[[package]] -name = "craft-ui-widgets" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-host", - "craft-i18n", - "craft-keymap", - "craft-theme", - "craft-ui-contract", - "craft-ui-macros", - "craft-ui-test-kit", - "craft-ui-widgets", - "egui", - "serde", - "smallvec", + "wasm-bindgen-test", + "web-sys", ] [[package]] @@ -3778,12 +2947,12 @@ name = "craft-update" version = "0.1.0" dependencies = [ "chrono", - "craft-host", - "craft-theme", - "craft-ui", "dunce", "egui", + "egui_kittest", + "epaint_default_fonts", "flate2", + "getrandom 0.3.4", "image", "log", "plist", @@ -3834,13 +3003,14 @@ name = "craft-vectordrawable" version = "0.1.0" dependencies = [ "craft-xml", - "kurbo", + "kurbo 0.13.1", ] [[package]] name = "craft-video" version = "0.1.0" dependencies = [ + "craft-jpeg", "craft-media", "craft-media-testkit", "rav1e", @@ -3877,33 +3047,19 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "craft-win32" -version = "0.1.0" -dependencies = [ - "winsafe", -] - [[package]] name = "craft-window" version = "0.1.0" dependencies = [ "craft-atomic", - "craft-behavior", - "craft-host", "craft-settings", - "craft-win32", "egui", - "muda", "objc2 0.6.5", "objc2-app-kit 0.3.2", - "objc2-foundation 0.3.2", - "raw-window-handle", "serde", "serde_json", "tempfile", "wayland-client", - "winsafe", ] [[package]] @@ -4416,6 +3572,19 @@ dependencies = [ "cipher 0.5.2", ] +[[package]] +name = "dify" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ce0fb972943b4e88cd03b8f92953df0c71bb05e0bde8e5b684895d808013cc" +dependencies = [ + "anyhow", + "colored", + "getopts", + "image", + "rayon", +] + [[package]] name = "digest" version = "0.10.7" @@ -4520,61 +3689,6 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" -[[package]] -name = "drm" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80bc8c5c6c2941f70a55c15f8d9f00f9710ebda3ffda98075f996a0e6c92756f" -dependencies = [ - "bitflags 2.13.2", - "bytemuck", - "drm-ffi", - "drm-fourcc", - "libc", - "rustix 0.38.44", -] - -[[package]] -name = "drm" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a41816e58f47f49acfd956651055ddcf137c4882c2098c30c448817af21183a" -dependencies = [ - "bitflags 2.13.2", - "bytemuck", - "bytemuck_derive", - "drm-ffi", - "drm-fourcc", - "libc", - "rustix 1.1.5", -] - -[[package]] -name = "drm-ffi" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51a91c9b32ac4e8105dec255e849e0d66e27d7c34d184364fb93e469db08f690" -dependencies = [ - "drm-sys", - "rustix 1.1.5", -] - -[[package]] -name = "drm-fourcc" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0aafbcdb8afc29c1a7ee5fbe53b5d62f4565b35a042a662ca9fecd0b54dae6f4" - -[[package]] -name = "drm-sys" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecc8e1361066d91f5ffccff060a3c3be9c3ecde15be2959c1937595f7a82a9f8" -dependencies = [ - "libc", - "linux-raw-sys 0.9.4", -] - [[package]] name = "dsa" version = "0.6.3" @@ -4642,10 +3756,11 @@ dependencies = [ [[package]] name = "ecolor" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "bytemuck", "emath", + "serde", ] [[package]] @@ -4698,7 +3813,7 @@ dependencies = [ [[package]] name = "eframe" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "ahash", "bytemuck", @@ -4733,7 +3848,7 @@ dependencies = [ [[package]] name = "egui" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "accesskit", "ahash", @@ -4744,6 +3859,7 @@ dependencies = [ "log", "nohash-hasher", "profiling", + "serde", "smallvec", "unicode-segmentation", "web-sys", @@ -4752,7 +3868,7 @@ dependencies = [ [[package]] name = "egui-wgpu" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "ahash", "bytemuck", @@ -4771,7 +3887,7 @@ dependencies = [ [[package]] name = "egui-winit" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "block2 0.6.2", "bytemuck", @@ -4788,10 +3904,24 @@ dependencies = [ "winit", ] +[[package]] +name = "egui_extras" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" +dependencies = [ + "ahash", + "egui", + "enum-map", + "itertools 0.15.0", + "log", + "profiling", + "resvg 0.45.1", +] + [[package]] name = "egui_glow" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "bytemuck", "egui", @@ -4804,20 +3934,37 @@ dependencies = [ [[package]] name = "egui_kittest" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ + "dify", "eframe", "egui", "egui-wgpu", "image", "kittest", "log", + "open", "pollster 1.0.1", "serde", + "tempfile", "toml", "wgpu", ] +[[package]] +name = "egui_system_font_provider" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" +dependencies = [ + "epaint", + "fontique", + "log", + "poll-promise", + "skrifa 0.44.0", + "sys-locale", + "unicode-script", +] + [[package]] name = "either" version = "1.19.0" @@ -4872,9 +4019,10 @@ dependencies = [ [[package]] name = "emath" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "bytemuck", + "serde", ] [[package]] @@ -4909,6 +4057,26 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" +[[package]] +name = "enum-map" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4532960034c15ba2f55e9eee631040ecc473c32e6c4b200828c759840f13a06" +dependencies = [ + "enum-map-derive", +] + +[[package]] +name = "enum-map-derive" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7127272f64b3e098f7d81f350d672081b879bff896d219c42370dc858b1af9ff" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "enumflags2" version = "0.7.12" @@ -4930,10 +4098,21 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "enumn" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f9ed6b3789237c8a0c1c505af1c7eb2c560df6186f01b098c3a1064ea532f38" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "epaint" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "ahash", "bytemuck", @@ -4947,6 +4126,7 @@ dependencies = [ "parking_lot", "profiling", "self_cell", + "serde", "skrifa 0.44.0", "smallvec", "unicode-bidi", @@ -4959,7 +4139,7 @@ dependencies = [ [[package]] name = "epaint_default_fonts" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" [[package]] name = "equator" @@ -4997,18 +4177,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "error-code" -version = "3.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b5343afd4a8365a643ac588dab4cf234a190c7f6c88c9f6dd6ffe00837661b7" - -[[package]] -name = "escape8259" -version = "0.5.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f1f0d310082fd5de539d4f41601182241147498444a7695c520288a0cdbead6" - [[package]] name = "euclid" version = "0.22.14" @@ -5087,30 +4255,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b97b65636e5b9ef369943878ac74335ba1c55c1cb6adbf1e2c293c624248d693" -[[package]] -name = "feature-root" -version = "0.1.0" -dependencies = [ - "craft-automation", - "craft-browse", - "craft-color", - "craft-dialogs", - "craft-draw", - "craft-font-manager", - "craft-imaging", - "craft-layers", - "craft-menu", - "craft-pages", - "craft-print", - "craft-review", - "craft-table", - "craft-text", - "craft-theme-store", - "craft-timeline", - "craft-ui-gallery", - "craft-update", -] - [[package]] name = "ff" version = "0.13.1" @@ -5171,12 +4315,6 @@ dependencies = [ "writeable", ] -[[package]] -name = "fixedbitset" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" - [[package]] name = "flagset" version = "0.4.7" @@ -5259,6 +4397,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835ae9d781464a79026cde387f9cbc650c9a4909d7cf7e658b127e3c35d2f35d" dependencies = [ "bytemuck", + "serde", ] [[package]] @@ -5439,30 +4578,6 @@ dependencies = [ "slab", ] -[[package]] -name = "gbm" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce852e998d3ca5e4a97014fb31c940dc5ef344ec7d364984525fd11e8a547e6a" -dependencies = [ - "bitflags 2.13.2", - "drm 0.14.1", - "drm-fourcc", - "gbm-sys", - "libc", - "wayland-backend", - "wayland-server", -] - -[[package]] -name = "gbm-sys" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c13a5f2acc785d8fb6bf6b7ab6bfb0ef5dad4f4d97e8e70bb8e470722312f76f" -dependencies = [ - "libc", -] - [[package]] name = "generic-array" version = "0.14.7" @@ -5484,6 +4599,15 @@ dependencies = [ "windows-link", ] +[[package]] +name = "getopts" +version = "0.2.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfe4fbac503b8d1f88e6676011885f34b7174f46e59956bba534ba83abded4df" +dependencies = [ + "unicode-width", +] + [[package]] name = "getrandom" version = "0.2.17" @@ -5545,15 +4669,6 @@ dependencies = [ "weezl 0.1.12", ] -[[package]] -name = "gl" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a94edab108827d67608095e269cf862e60d920f144a5026d3dbcfd8b877fb404" -dependencies = [ - "gl_generator", -] - [[package]] name = "gl_generator" version = "0.14.0" @@ -5605,12 +4720,6 @@ dependencies = [ "vello_common 0.2.0", ] -[[package]] -name = "glob" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" - [[package]] name = "glow" version = "0.17.0" @@ -5764,14 +4873,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "harfbuzz-subset-sys" -version = "0.1.0+harfbuzz-15.0.0" -source = "git+https://github.com/storytold/harfbuzz.git?rev=2659dd807f9a48f75604ee7c76cb709f7b70b950#2659dd807f9a48f75604ee7c76cb709f7b70b950" -dependencies = [ - "cc", -] - [[package]] name = "harfrust" version = "0.12.0" @@ -5833,7 +4934,7 @@ dependencies = [ "bytemuck", "hayro-interpret", "image", - "kurbo", + "kurbo 0.13.1", "log", "pic-scale", "vello_cpu 0.0.8", @@ -5860,7 +4961,7 @@ dependencies = [ "bitflags 2.13.2", "hayro-cmap", "hayro-syntax", - "kurbo", + "kurbo 0.13.1", "moxcms", "phf 0.13.1", "rustc-hash 2.1.3", @@ -6113,21 +5214,6 @@ dependencies = [ "cc", ] -[[package]] -name = "icu_calendar" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58655df2f728e46e4eee80bddebefacf52ec3e77cdb925014b47c5a5905eb55c" -dependencies = [ - "calendrical_calculations", - "displaydoc", - "icu_locale_core", - "icu_provider", - "serde", - "tinystr", - "zerovec", -] - [[package]] name = "icu_collator" version = "2.3.1" @@ -6161,30 +5247,6 @@ dependencies = [ "zerovec", ] -[[package]] -name = "icu_datetime" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c44df99c4c5297b05653d1a1629f13bdc9fd4044acfbbaf30b15194a1eed4cd6" -dependencies = [ - "displaydoc", - "fixed_decimal", - "icu_calendar", - "icu_decimal", - "icu_locale_core", - "icu_pattern", - "icu_plurals", - "icu_provider", - "icu_time", - "potential_utf", - "serde", - "smallvec", - "tinystr", - "writeable", - "zerotrie", - "zerovec", -] - [[package]] name = "icu_decimal" version = "2.3.0" @@ -6373,22 +5435,6 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae293c039020f9ec10710af98d29ce6aa2051486638b49c9a6409f3b4a9e98ad" -[[package]] -name = "icu_time" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af37e1c3b9f3ab564b619fdf4b22de169f6381eb0f7048e3ca454290c438126b" -dependencies = [ - "calendrical_calculations", - "displaydoc", - "icu_calendar", - "icu_locale_core", - "icu_provider", - "serde", - "zerotrie", - "zerovec", -] - [[package]] name = "idea" version = "0.5.1" @@ -6457,6 +5503,12 @@ dependencies = [ "quick-error 2.0.1", ] +[[package]] +name = "imagesize" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edcd27d72f2f071c64249075f42e205ff93c9a4c5f6c6da53e79ed9f9832c285" + [[package]] name = "imagesize" version = "0.14.0" @@ -6520,15 +5572,6 @@ dependencies = [ "syn 2.0.119", ] -[[package]] -name = "inventory" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6928282826c822ad91bf1c9a1cb90a30ba1c26770749929b4656cd6be829cd7c" -dependencies = [ - "rustversion", -] - [[package]] name = "io-extras" version = "0.19.0" @@ -6558,18 +5601,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" [[package]] -name = "is_terminal_polyfill" -version = "1.70.2" +name = "is-docker" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" +checksum = "928bae27f42bc99b60d9ac7334e3a21d10ad8f1835a4e12ec3ec0464765ed1b3" +dependencies = [ + "once_cell", +] [[package]] -name = "itertools" -version = "0.13.0" +name = "is-wsl" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +checksum = "173609498df190136aa7dea1a91db051746d339e18476eed5ca40521f02d7aa5" dependencies = [ - "either", + "is-docker", + "once_cell", ] [[package]] @@ -6971,7 +6018,7 @@ dependencies = [ "skrifa 0.42.1", "smallvec", "subsetter", - "tiny-skia-path", + "tiny-skia-path 0.12.0", "xmp-writer", "yoke", "zune-jpeg", @@ -6989,10 +6036,21 @@ dependencies = [ "resvg 0.47.0", "skrifa 0.42.1", "smallvec", - "tiny-skia", + "tiny-skia 0.12.0", "usvg 0.47.0", ] +[[package]] +name = "kurbo" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62026ae44756f8a599ba21140f350303d4f08dcdcc71b5ad9c9bb8128c13c62" +dependencies = [ + "arrayvec", + "euclid", + "smallvec", +] + [[package]] name = "kurbo" version = "0.13.1" @@ -7065,66 +6123,6 @@ dependencies = [ "redox_syscall 0.9.4", ] -[[package]] -name = "libspa" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "882f7427e7989dcc9d388b7f05c4630390a1d7696f9ffa469cd4a7a48f0b4c40" -dependencies = [ - "bitflags 2.13.2", - "cc", - "cookie-factory", - "libc", - "libspa-sys", - "nom 8.0.0", - "rustix 1.1.5", - "system-deps", -] - -[[package]] -name = "libspa-sys" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b6e17bdaf63ed0d5e4144022624032b41fd9733112e8c74ac26fc9bf1291924" -dependencies = [ - "bindgen", - "cc", - "system-deps", -] - -[[package]] -name = "libtest-mimic" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14e6ba06f0ade6e504aff834d7c34298e5155c6baca353cc6a4aaff2f9fd7f33" -dependencies = [ - "anstream", - "anstyle", - "clap", - "escape8259", -] - -[[package]] -name = "libwayshot-xcap" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea8a46e4d016ef464e386e4970f5415fe0939c2d63abe0c2f539c05ef88c5fe5" -dependencies = [ - "drm 0.15.0", - "gbm", - "gl", - "image", - "khronos-egl", - "memmap2", - "rustix 1.1.5", - "thiserror 2.0.21", - "tracing", - "wayland-backend", - "wayland-client", - "wayland-protocols", - "wayland-protocols-wlr", -] - [[package]] name = "linebender_resource_handle" version = "0.1.1" @@ -7136,7 +6134,7 @@ name = "linesweeper" version = "0.5.0" dependencies = [ "arrayvec", - "kurbo", + "kurbo 0.13.1", "polycool", "rustc-hash 2.1.3", "smallvec", @@ -7148,12 +6146,6 @@ version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" -[[package]] -name = "linux-raw-sys" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" - [[package]] name = "linux-raw-sys" version = "0.12.1" @@ -7224,6 +6216,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" dependencies = [ "cfg-if", + "rayon", ] [[package]] @@ -7505,15 +6498,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" -[[package]] -name = "ntapi" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae" -dependencies = [ - "winapi", -] - [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -7673,8 +6657,8 @@ dependencies = [ "block2 0.5.1", "libc", "objc2 0.5.3", - "objc2-core-data 0.2.2", - "objc2-core-image 0.2.2", + "objc2-core-data", + "objc2-core-image", "objc2-foundation 0.2.2", "objc2-quartz-core 0.2.2", ] @@ -7687,48 +6671,8 @@ checksum = "d49e936b501e5c5bf01fda3a9452ff86dc3ea98ad5f283e1455153142d97518c" dependencies = [ "bitflags 2.13.2", "block2 0.6.2", - "libc", "objc2 0.6.5", - "objc2-cloud-kit 0.3.2", - "objc2-core-data 0.3.2", "objc2-core-foundation", - "objc2-core-graphics", - "objc2-core-image 0.3.2", - "objc2-core-text", - "objc2-core-video", - "objc2-foundation 0.3.2", - "objc2-quartz-core 0.3.2", -] - -[[package]] -name = "objc2-av-foundation" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "478ae33fcac9df0a18db8302387c666b8ef08a3e2d62b510ca4fc278a384b6c0" -dependencies = [ - "bitflags 2.13.2", - "block2 0.6.2", - "dispatch2", - "objc2 0.6.5", - "objc2-avf-audio", - "objc2-core-audio-types", - "objc2-core-foundation", - "objc2-core-graphics", - "objc2-core-image 0.3.2", - "objc2-core-video", - "objc2-foundation 0.3.2", - "objc2-image-io", - "objc2-media-toolbox", - "objc2-quartz-core 0.3.2", -] - -[[package]] -name = "objc2-avf-audio" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13a380031deed8e99db00065c45937da434ca987c034e13b87e4441f9e4090be" -dependencies = [ - "objc2 0.6.5", "objc2-foundation 0.3.2", ] @@ -7745,17 +6689,6 @@ dependencies = [ "objc2-foundation 0.2.2", ] -[[package]] -name = "objc2-cloud-kit" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73ad74d880bb43877038da939b7427bba67e9dd42004a18b809ba7d87cee241c" -dependencies = [ - "bitflags 2.13.2", - "objc2 0.6.5", - "objc2-foundation 0.3.2", -] - [[package]] name = "objc2-contacts" version = "0.2.2" @@ -7767,28 +6700,6 @@ dependencies = [ "objc2-foundation 0.2.2", ] -[[package]] -name = "objc2-core-audio" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1eebcea8b0dbff5f7c8504f3107c68fc061a3eb44932051c8cf8a68d969c3b2" -dependencies = [ - "dispatch2", - "objc2 0.6.5", - "objc2-core-audio-types", - "objc2-core-foundation", -] - -[[package]] -name = "objc2-core-audio-types" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a89f2ec274a0cf4a32642b2991e8b351a404d290da87bb6a9a9d8632490bd1c" -dependencies = [ - "bitflags 2.13.2", - "objc2 0.6.5", -] - [[package]] name = "objc2-core-data" version = "0.2.2" @@ -7801,17 +6712,6 @@ dependencies = [ "objc2-foundation 0.2.2", ] -[[package]] -name = "objc2-core-data" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b402a653efbb5e82ce4df10683b6b28027616a2715e90009947d50b8dd298fa" -dependencies = [ - "bitflags 2.13.2", - "objc2 0.6.5", - "objc2-foundation 0.3.2", -] - [[package]] name = "objc2-core-foundation" version = "0.3.2" @@ -7819,9 +6719,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" dependencies = [ "bitflags 2.13.2", - "block2 0.6.2", "dispatch2", - "libc", "objc2 0.6.5", ] @@ -7831,14 +6729,10 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807" dependencies = [ - "bitflags 2.13.2", - "block2 0.6.2", "dispatch2", - "libc", "objc2 0.6.5", "objc2-core-foundation", "objc2-io-surface", - "objc2-metal 0.3.2", ] [[package]] @@ -7853,16 +6747,6 @@ dependencies = [ "objc2-metal 0.2.2", ] -[[package]] -name = "objc2-core-image" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5d563b38d2b97209f8e861173de434bd0214cf020e3423a52624cd1d989f006" -dependencies = [ - "objc2 0.6.5", - "objc2-foundation 0.3.2", -] - [[package]] name = "objc2-core-location" version = "0.2.2" @@ -7875,22 +6759,6 @@ dependencies = [ "objc2-foundation 0.2.2", ] -[[package]] -name = "objc2-core-media" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05ec576860167a15dd9fce7fbee7512beb4e31f532159d3482d1f9c6caedf31d" -dependencies = [ - "bitflags 2.13.2", - "block2 0.6.2", - "dispatch2", - "objc2 0.6.5", - "objc2-core-audio", - "objc2-core-audio-types", - "objc2-core-foundation", - "objc2-core-video", -] - [[package]] name = "objc2-core-text" version = "0.3.2" @@ -7898,24 +6766,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0cde0dfb48d25d2b4862161a4d5fcc0e3c24367869ad306b0c9ec0073bfed92d" dependencies = [ "bitflags 2.13.2", - "objc2 0.6.5", "objc2-core-foundation", - "objc2-core-graphics", -] - -[[package]] -name = "objc2-core-video" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d425caf1df73233f29fd8a5c3e5edbc30d2d4307870f802d18f00d83dc5141a6" -dependencies = [ - "bitflags 2.13.2", - "block2 0.6.2", - "objc2 0.6.5", - "objc2-core-foundation", - "objc2-core-graphics", - "objc2-io-surface", - "objc2-metal 0.3.2", ] [[package]] @@ -7945,32 +6796,10 @@ checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272" dependencies = [ "bitflags 2.13.2", "block2 0.6.2", - "libc", "objc2 0.6.5", "objc2-core-foundation", ] -[[package]] -name = "objc2-image-io" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b0446e98cf4a784cc7a0177715ff317eeaa8463841c616cfc78aa4f953c4ea" -dependencies = [ - "objc2 0.6.5", - "objc2-core-foundation", - "objc2-core-graphics", -] - -[[package]] -name = "objc2-io-kit" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15" -dependencies = [ - "libc", - "objc2-core-foundation", -] - [[package]] name = "objc2-io-surface" version = "0.3.2" @@ -7994,18 +6823,6 @@ dependencies = [ "objc2-foundation 0.2.2", ] -[[package]] -name = "objc2-media-toolbox" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edd9fdde720df3da7046bb9097811000c1e7ab5cd579fa89d96b27d56781fb30" -dependencies = [ - "objc2 0.6.5", - "objc2-core-audio-types", - "objc2-core-foundation", - "objc2-core-media", -] - [[package]] name = "objc2-metal" version = "0.2.2" @@ -8076,9 +6893,9 @@ dependencies = [ "bitflags 2.13.2", "block2 0.5.1", "objc2 0.5.3", - "objc2-cloud-kit 0.2.2", - "objc2-core-data 0.2.2", - "objc2-core-image 0.2.2", + "objc2-cloud-kit", + "objc2-core-data", + "objc2-core-image", "objc2-core-location", "objc2-foundation 0.2.2", "objc2-link-presentation", @@ -8160,12 +6977,6 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" -[[package]] -name = "once_cell_polyfill" -version = "1.70.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" - [[package]] name = "oorandom" version = "11.1.5" @@ -8178,6 +6989,16 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" +[[package]] +name = "open" +version = "5.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d8c8b8e725ad26404d6896eb2f064294a0a7b4f1d9120210d85544684c4ca95" +dependencies = [ + "is-wsl", + "libc", +] + [[package]] name = "openssl-probe" version = "0.2.1" @@ -8228,16 +7049,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "os_pipe" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" -dependencies = [ - "libc", - "windows-sys 0.61.2", -] - [[package]] name = "p256" version = "0.13.2" @@ -8433,7 +7244,7 @@ checksum = "839c8299360d2e998bdb106dc0a6cd71dcc5f4df51df1b620361bf50e283cca6" dependencies = [ "bytemuck", "color", - "kurbo", + "kurbo 0.13.1", "linebender_resource_handle", "smallvec", ] @@ -8444,17 +7255,6 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" -[[package]] -name = "petgraph" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455" -dependencies = [ - "fixedbitset", - "hashbrown 0.15.5", - "indexmap", -] - [[package]] name = "pgp" version = "0.21.0" @@ -8639,31 +7439,6 @@ dependencies = [ "futures-io", ] -[[package]] -name = "pipewire" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bde71084c4e25959d68f1ea54daa75e5ecdb338e5caf0b5510143b79baa32d5c" -dependencies = [ - "bitflags 2.13.2", - "libc", - "libspa", - "libspa-sys", - "pipewire-sys", - "rustix 1.1.5", -] - -[[package]] -name = "pipewire-sys" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce653f53e63e5b93853218092ee9a8906a5d082c92f3f1db26316955dd63ce0" -dependencies = [ - "bindgen", - "libspa-sys", - "system-deps", -] - [[package]] name = "pkcs1" version = "0.7.5" @@ -8771,6 +7546,16 @@ dependencies = [ "miniz_oxide 0.8.9", ] +[[package]] +name = "poll-promise" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f6a58fecbf9da8965bcdb20ce4fd29788d1acee68ddbb64f0ba1b81bccdb7df" +dependencies = [ + "document-features", + "static_assertions", +] + [[package]] name = "polling" version = "3.11.0" @@ -9269,6 +8054,7 @@ dependencies = [ "loop9", "quick-error 2.0.1", "rav1e", + "rayon", "rgb", ] @@ -9388,20 +8174,6 @@ dependencies = [ "yoke", ] -[[package]] -name = "read-fonts" -version = "0.46.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdaa4fffe9ff3ca2c353fa9729792805b9b3e324695ff5a081f860612e81b330" -dependencies = [ - "bytemuck", - "foldhash 0.2.0", - "font-types 0.12.7", - "once_cell", - "smallvec", - "yoke", -] - [[package]] name = "redox_syscall" version = "0.4.1" @@ -9439,29 +8211,6 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "regex" -version = "1.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - [[package]] name = "regex-lite" version = "0.1.9" @@ -9538,6 +8287,20 @@ dependencies = [ "rgb", ] +[[package]] +name = "resvg" +version = "0.45.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8928798c0a55e03c9ca6c4c6846f76377427d2c1e1f7e6de3c06ae57942df43" +dependencies = [ + "log", + "pico-args", + "rgb", + "svgtypes 0.15.3", + "tiny-skia 0.11.4", + "usvg 0.45.1", +] + [[package]] name = "resvg" version = "0.47.0" @@ -9549,8 +8312,8 @@ dependencies = [ "log", "pico-args", "rgb", - "svgtypes", - "tiny-skia", + "svgtypes 0.16.2", + "tiny-skia 0.12.0", "usvg 0.47.0", "zune-jpeg", ] @@ -9567,8 +8330,8 @@ dependencies = [ "log", "pico-args", "rgb", - "svgtypes", - "tiny-skia", + "svgtypes 0.16.2", + "tiny-skia 0.12.0", "usvg 0.48.1", "zune-jpeg", ] @@ -9843,9 +8606,9 @@ checksum = "eec689c0bc40ff2458a5977b6619cb718087084a18e02a131c599b62d05e1a5f" [[package]] name = "rustls-webpki" -version = "0.103.15" +version = "0.103.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281" dependencies = [ "aws-lc-rs", "ring", @@ -9925,12 +8688,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "scoped-tls" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" - [[package]] name = "scopeguard" version = "1.2.0" @@ -10203,12 +8960,6 @@ dependencies = [ "sponge-cursor", ] -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - [[package]] name = "shlex" version = "2.0.1" @@ -10451,6 +9202,18 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" +[[package]] +name = "skera" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "531045338e36ad05b9652912a47ba38a935d0eaa0293c3e33d8112cfa414c14b" +dependencies = [ + "hashbrown 0.17.1", + "skrifa 0.48.0", + "thiserror 2.0.21", + "write-fonts 0.54.1", +] + [[package]] name = "skrifa" version = "0.42.1" @@ -10483,15 +9246,12 @@ dependencies = [ [[package]] name = "skrifa" -version = "0.49.0" +version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7459fb60888ad9a5f367dd293fa2fb9df142cb451aa6bfd6cd66a7d6a9b739d" +checksum = "a5213b4825bac3587046330052d61bfac7bd3bd0d203c758d7c2ef726fcea034" dependencies = [ "bytemuck", - "once_cell", - "read-fonts 0.46.0", - "smallvec", - "yoke", + "read-fonts 0.45.1", ] [[package]] @@ -10611,17 +9371,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" -[[package]] -name = "story-render" -version = "0.1.0" -dependencies = [ - "craft-behavior", - "craft-ui", - "craft-ui-gallery", - "egui", - "image", -] - [[package]] name = "strict-num" version = "0.1.1" @@ -10685,7 +9434,7 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38803281d1c23166c5ebcb455439a5d2afe711cc909cf88af72448c297756ad6" dependencies = [ - "kurbo", + "kurbo 0.13.1", "rustc-hash 2.1.3", "skrifa 0.42.1", "write-fonts 0.48.1", @@ -10697,13 +9446,23 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" +[[package]] +name = "svgtypes" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68c7541fff44b35860c1a7a47a7cadf3e4a304c457b58f9870d9706ece028afc" +dependencies = [ + "kurbo 0.11.3", + "siphasher", +] + [[package]] name = "svgtypes" version = "0.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f12b12995e3638c70618c1facb1ce65302f6a6f706b675f07c58a5d7391120e" dependencies = [ - "kurbo", + "kurbo 0.13.1", "siphasher", ] @@ -10979,20 +9738,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "sysinfo" -version = "0.39.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2071df9448915b71c4fe6d25deaf1c22f12bd234f01540b77312bb8e41361e6" -dependencies = [ - "libc", - "memchr", - "ntapi", - "objc2-core-foundation", - "objc2-io-kit", - "windows", -] - [[package]] name = "system-configuration" version = "0.7.0" @@ -11014,19 +9759,6 @@ dependencies = [ "libc", ] -[[package]] -name = "system-deps" -version = "7.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "396a35feb67335377e0251fcbc1092fc85c484bd4e3a7a54319399da127796e7" -dependencies = [ - "cfg-expr", - "heck", - "pkg-config", - "toml", - "version-compare", -] - [[package]] name = "tap" version = "1.0.1" @@ -11050,12 +9782,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5" -[[package]] -name = "target-lexicon" -version = "0.13.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca" - [[package]] name = "tempfile" version = "3.27.0" @@ -11171,6 +9897,21 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny-skia" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83d13394d44dae3207b52a326c0c85a8bf87f1541f23b0d143811088497b09ab" +dependencies = [ + "arrayref", + "arrayvec", + "bytemuck", + "cfg-if", + "log", + "png 0.17.16", + "tiny-skia-path 0.11.4", +] + [[package]] name = "tiny-skia" version = "0.12.0" @@ -11183,7 +9924,18 @@ dependencies = [ "cfg-if", "log", "png 0.18.1", - "tiny-skia-path", + "tiny-skia-path 0.12.0", +] + +[[package]] +name = "tiny-skia-path" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c9e7fc0c2e86a30b117d0462aa261b72b7a99b7ebd7deb3a14ceda95c5bdc93" +dependencies = [ + "arrayref", + "bytemuck", + "strict-num", ] [[package]] @@ -11415,17 +10167,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "tree_magic_mini" -version = "3.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8765b90061cba6c22b5831f675da109ae5561588290f9fa2317adab2714d5a6" -dependencies = [ - "memchr", - "nom 8.0.0", - "petgraph", -] - [[package]] name = "try-lock" version = "0.2.5" @@ -11526,9 +10267,9 @@ checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" [[package]] name = "unicode-normalization" -version = "0.1.26" +version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75cd3fb03d5288b3ba9a1ee103ac7e9cbb59f0b03d64f8ddfcc04dd42758a85d" +checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8" dependencies = [ "tinyvec", ] @@ -11633,6 +10374,28 @@ dependencies = [ "serde", ] +[[package]] +name = "usvg" +version = "0.45.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80be9b06fbae3b8b303400ab20778c80bbaf338f563afe567cf3c9eea17b47ef" +dependencies = [ + "base64 0.22.1", + "data-url", + "flate2", + "imagesize 0.13.0", + "kurbo 0.11.3", + "log", + "pico-args", + "roxmltree 0.20.0", + "simplecss", + "siphasher", + "strict-num", + "svgtypes 0.15.3", + "tiny-skia-path 0.11.4", + "xmlwriter", +] + [[package]] name = "usvg" version = "0.47.0" @@ -11644,7 +10407,7 @@ dependencies = [ "flate2", "fontdb 0.23.0", "imagesize 0.14.0", - "kurbo", + "kurbo 0.13.1", "log", "pico-args", "roxmltree 0.21.2", @@ -11652,8 +10415,8 @@ dependencies = [ "simplecss", "siphasher", "strict-num", - "svgtypes", - "tiny-skia-path", + "svgtypes 0.16.2", + "tiny-skia-path 0.12.0", "ttf-parser", "unicode-bidi", "unicode-script", @@ -11672,7 +10435,7 @@ dependencies = [ "fontdb 0.24.0", "harfrust 0.12.0", "imagesize 0.15.0", - "kurbo", + "kurbo 0.13.1", "log", "pico-args", "roxmltree 0.21.2", @@ -11680,8 +10443,8 @@ dependencies = [ "siphasher", "skrifa 0.44.0", "strict-num", - "svgtypes", - "tiny-skia-path", + "svgtypes 0.16.2", + "tiny-skia-path 0.12.0", "unicode-bidi", "unicode-script", "unicode-vo", @@ -11706,12 +10469,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" -[[package]] -name = "utf8parse" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" - [[package]] name = "uuid" version = "1.27.0" @@ -11739,7 +10496,7 @@ name = "vectorcraft-brush" version = "0.18.0" source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" dependencies = [ - "kurbo", + "kurbo 0.13.1", "serde", "serde_json", "vectorcraft-color", @@ -11752,7 +10509,7 @@ name = "vectorcraft-color" version = "0.18.0" source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" dependencies = [ - "kurbo", + "kurbo 0.13.1", "moxcms", "serde", "serde_json", @@ -11767,7 +10524,7 @@ dependencies = [ "chrono", "hayro-ccitt", "image", - "kurbo", + "kurbo 0.13.1", "serde", "serde_json", "thiserror 2.0.21", @@ -11783,7 +10540,7 @@ version = "0.18.0" source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" dependencies = [ "image", - "kurbo", + "kurbo 0.13.1", "serde_json", "vectorcraft-doc", "vectorcraft-geom", @@ -11801,7 +10558,7 @@ dependencies = [ "flate2", "hayro-ccitt", "image", - "kurbo", + "kurbo 0.13.1", "memchr", "ruzstd", "serde_json", @@ -11821,7 +10578,7 @@ name = "vectorcraft-geom" version = "0.18.0" source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" dependencies = [ - "kurbo", + "kurbo 0.13.1", "serde", "thiserror 2.0.21", ] @@ -11832,7 +10589,7 @@ version = "0.18.0" source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" dependencies = [ "image", - "kurbo", + "kurbo 0.13.1", "vectorcraft-brush", "vectorcraft-color", "vectorcraft-doc", @@ -11846,7 +10603,7 @@ name = "vectorcraft-pathops" version = "0.18.0" source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420d69449199cbdf323888d27e#6b3a6b8788ebcb420d69449199cbdf323888d27e" dependencies = [ - "kurbo", + "kurbo 0.13.1", "linesweeper", "thiserror 2.0.21", "vectorcraft-geom", @@ -11864,7 +10621,7 @@ dependencies = [ "getrandom 0.4.3", "image", "jpeg-encoder 0.6.1", - "kurbo", + "kurbo 0.13.1", "log", "md-5 0.10.6", "roxmltree 0.21.2", @@ -11926,7 +10683,7 @@ source = "git+https://github.com/storytold/vectorcraft.git?rev=6b3a6b8788ebcb420 dependencies = [ "harfrust 0.13.3", "icu_segmenter", - "kurbo", + "kurbo 0.13.1", "log", "skrifa 0.47.0", "subsetter", @@ -12039,12 +10796,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "version-compare" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03c2856837ef78f57382f06b2b8563a2f512f7185d732608fd9176cb3b8edf0e" - [[package]] name = "version_check" version = "0.9.5" @@ -12248,7 +10999,6 @@ dependencies = [ "cc", "downcast-rs", "rustix 1.1.5", - "scoped-tls", "smallvec", "wayland-sys", ] @@ -12265,31 +11015,6 @@ dependencies = [ "wayland-scanner", ] -[[package]] -name = "wayland-protocols" -version = "0.32.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23d0c813de3daa2ed6520af85a3bd49b0e722a3078506899aa9686fea58dc4b6" -dependencies = [ - "bitflags 2.13.2", - "wayland-backend", - "wayland-client", - "wayland-scanner", -] - -[[package]] -name = "wayland-protocols-wlr" -version = "0.3.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb04e52f7836d7c7976c78ca0250d61e33873c34156a2a1fc9474828ec268234" -dependencies = [ - "bitflags 2.13.2", - "wayland-backend", - "wayland-client", - "wayland-protocols", - "wayland-scanner", -] - [[package]] name = "wayland-scanner" version = "0.31.11" @@ -12301,19 +11026,6 @@ dependencies = [ "quote", ] -[[package]] -name = "wayland-server" -version = "0.31.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dde9c29be0f723a573977de51ee455bf3dfa03652730a74f9dd3b337e374d75" -dependencies = [ - "bitflags 2.13.2", - "downcast-rs", - "rustix 1.1.5", - "wayland-backend", - "wayland-scanner", -] - [[package]] name = "wayland-sys" version = "0.31.11" @@ -12321,9 +11033,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8eab23fefc9e41f8e841df4a9c707e8a8c4ed26e944ef69297184de2785e3be" dependencies = [ "dlib", - "libc", "log", - "memoffset", "once_cell", "pkg-config", ] @@ -12561,28 +11271,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "widestring" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - [[package]] name = "winapi-util" version = "0.1.11" @@ -12592,12 +11280,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - [[package]] name = "windows" version = "0.62.2" @@ -12916,24 +11598,6 @@ version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" -[[package]] -name = "wl-clipboard-rs" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "959c7ac3b8afde864f7a7448ac16f29b2b83350b7eccbf77fb781c3999aa6848" -dependencies = [ - "libc", - "log", - "os_pipe", - "rustix 1.1.5", - "thiserror 2.0.21", - "tree_magic_mini", - "wayland-backend", - "wayland-client", - "wayland-protocols", - "wayland-protocols-wlr", -] - [[package]] name = "wnaf" version = "0.14.1" @@ -12954,7 +11618,7 @@ checksum = "cb731d4c4d93eacc69a1ad2f270f905788a98e4a3438267bcafbe08d3431c8d8" dependencies = [ "font-types 0.11.3", "indexmap", - "kurbo", + "kurbo 0.13.1", "log", "read-fonts 0.39.2", ] @@ -12967,24 +11631,11 @@ checksum = "55716cfa7e8bcdb90289e07dfac478ccb2e3ec0f9a604fe682c7a03a7984d705" dependencies = [ "font-types 0.12.7", "indexmap", - "kurbo", + "kurbo 0.13.1", "log", "read-fonts 0.45.1", ] -[[package]] -name = "write-fonts" -version = "0.55.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e91ad74b39f48f456560ed1ccc75959efa19bf6f0817d1560751bb49db84f6c" -dependencies = [ - "font-types 0.12.7", - "indexmap", - "kurbo", - "log", - "read-fonts 0.46.0", -] - [[package]] name = "write16" version = "1.0.0" @@ -13000,17 +11651,6 @@ dependencies = [ "either", ] -[[package]] -name = "wuff" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6a992b57b3d1ee972b2e3fd9225e44793104d49818836dd812d5459187712dd" -dependencies = [ - "brotli-decompressor 6.3.1", - "bytes", - "flate2", -] - [[package]] name = "wyz" version = "0.5.1" @@ -13128,48 +11768,6 @@ dependencies = [ "rustix 1.1.5", ] -[[package]] -name = "xcap" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da270fd7c8581d43d731cb690bcc791fc0a7b3bc96e131a4b6552d0379640a9e" -dependencies = [ - "dispatch2", - "image", - "libwayshot-xcap", - "log", - "objc2 0.6.5", - "objc2-app-kit 0.3.2", - "objc2-av-foundation", - "objc2-core-foundation", - "objc2-core-graphics", - "objc2-core-media", - "objc2-core-video", - "objc2-foundation 0.3.2", - "percent-encoding", - "pipewire", - "rand 0.10.3", - "scopeguard", - "serde", - "thiserror 2.0.21", - "url", - "widestring", - "windows", - "xcb", - "zbus", -] - -[[package]] -name = "xcb" -version = "1.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6c2ad15e0e922856ee89afe862b8992334bbe7953adad56cd1199358cb30566" -dependencies = [ - "bitflags 2.13.2", - "libc", - "quick-xml 0.41.0", -] - [[package]] name = "xkbcommon-dl" version = "0.4.2" @@ -13433,9 +12031,9 @@ dependencies = [ [[package]] name = "zip" -version = "9.1.0" +version = "9.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2886b00003a72c4ccee3f42cfc34908d33b2508a74d827dc2a4244706475c704" +checksum = "1156b39d142a34b999c2310a50455e0cb3760bf7efad0122f796d813bee26ccb" dependencies = [ "crc32fast", "flate2", @@ -13536,19 +12134,9 @@ dependencies = [ [[patch.unused]] name = "egui_demo_lib" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" - -[[patch.unused]] -name = "egui_extras" -version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" [[patch.unused]] name = "egui_inspection" version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" - -[[patch.unused]] -name = "egui_system_font_provider" -version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" diff --git a/Cargo.toml b/Cargo.toml index b9d6af2bc..d0c1f75cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ craft-usd = { path = "crates/usd" } craft-smime = { path = "crates/smime" } craft-ber = { path = "crates/ber" } craft-proto = { path = "crates/proto" } +craft-render-parity = { path = "crates/render-parity" } acadrust = "=0.6.6" log = "0.4" # internal: add each crate here as `craft- = { path = "crates/" }` @@ -28,6 +29,7 @@ craft-win32 = { path = "crates/win32" } craft-ai-core = { path = "crates/ai-core" } craft-anicraft-format = { path = "crates/anicraft-format" } craft-pdf = { path = "crates/pdf", version = "=0.1.0" } +craft-pe = { path = "crates/pe" } craft-ps = { path = "crates/ps", version = "=0.1.0" } craft-pdn = { path = "crates/pdn" } craft-plate-test = { path = "crates/plate-test" } @@ -70,6 +72,7 @@ craft-designcraft-format = { path = "crates/designcraft-format" } craft-dxf = { path = "crates/dxf" } craft-drawingml = { path = "crates/drawingml" } craft-eps = { path = "crates/eps" } +craft-elf = { path = "crates/elf" } craft-file-formats = { path = "crates/file-formats" } craft-file-import = { path = "crates/file-import" } vectorcraft-doc = { git = "https://github.com/storytold/vectorcraft.git", rev = "6b3a6b8788ebcb420d69449199cbdf323888d27e" } @@ -124,6 +127,7 @@ craft-layers = { path = "crates/layers" } craft-audio = { path = "crates/audio" } craft-latex = { path = "crates/latex" } craft-markdown = { path = "crates/markdown" } +craft-macho = { path = "crates/macho" } craft-math = { path = "crates/math" } # No default features: craft-audio turns on the craft-media modules each of its features needs. craft-media = { path = "crates/media", default-features = false } @@ -204,6 +208,8 @@ craft-xcf = { path = "crates/xcf" } craft-xml = { path = "crates/xml" } craft-zip = { path = "crates/zip" } craft-y4m = { path = "crates/y4m" } +craft-sniff = { path = "crates/sniff" } +craft-tar = { path = "crates/tar" } craft-epub = { path = "crates/epub" } craft-ews = { path = "crates/ews" } craft-video = { path = "crates/video" } @@ -230,13 +236,11 @@ chrono = { version = "0.4.45", default-features = false, features = ["std"] } [workspace.lints.rust] unsafe_code = "forbid" -unused_workspace_dependencies = "allow" unexpected_cfgs = { level = "warn", check-cfg = ["cfg(craft_pdf_unicode_fixtures)"] } [workspace.lints.clippy] cognitive_complexity = "deny" too_many_arguments = "deny" -unused_workspace_dependencies = "allow" wildcard_imports = "deny" dbg_macro = "warn" todo = "warn" diff --git a/README.md b/README.md index 77a7bd11e..761127975 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit Clean room, from EffectCraft. Mapping onto an app's document stays in the app. - [`craft-ai-core`](crates/ai-core/README.md): the data model and contract for AI in the Craft Apps (connections, models, tools, requests, jobs, consent, history, route spec). Pure, no I/O, wasm-safe. Contract in [docs/ai-shared-layer.md](docs/ai-shared-layer.md). +- [`craft-pe`](crates/pe/src/lib.rs): PE/COFF headers (`.exe`, `.dll`, `.sys`, `.efi`) from untrusted bytes, from Craft Anything: the COFF header, PE32/PE32+ optional header with its data directories bounded by the header's real size, the section table, RVA-to-file mapping and the decoded Rich header. - [`craft-pdf`](crates/pdf/README.md): independently optional PDF parsing, interpretation, rasterization and guarded authoring. Apps retain layout, fonts, editing and export policy; writer-only consumers do not enable the shared reader. - [`craft-pdn`](crates/pdn/README.md): Paint.NET documents (`.pdn`, PDN3), read: the layer list, blend modes and chunked gzip pixel blocks, with a data-only MS-NRBF (.NET binary serialization) reader. From PhotoCraft. - [`craft-aaf`](crates/aaf/README.md): clean-room AAF (Edit Protocol, structured storage) and OMF Interchange 2.0 (Bento) readers and writers over one mob-style composition model with exact times, clip gain, markers, nested compositions and embedded PCM essence. From FilmCraft. @@ -117,6 +118,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit EMU units and `a:xfrm`, colours with theme slots and colour transforms, preset shape outlines (`a:prstGeom`), custom geometry with guide formulas (`a:custGeom`) and preset text warps (`a:prstTxWarp`). From DeckCraft. Takes `craft-xml` trees; packages stay with each format. +- [`craft-elf`](crates/elf/src/lib.rs): ELF program headers and dynamic section entries from untrusted bytes, from Craft Anything: 32- or 64-bit in either byte order, bounded counts and overflow-checked offsets, virtual-address-to-file mapping through `PT_LOAD`, and segment, permission and dynamic tag names. - [`craft-eps`](crates/eps/README.md): Encapsulated PostScript files as a container: the binary preview header (read and write), the DSC header comments and bounding boxes (also `(atend)`), the EPSI preview bitmap, ASCII85 data, and telling EPS, PostScript and Illustrator files @@ -213,6 +215,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit Hasselblad 3FR/FFF, Minolta MRW, Epson ERF, Mamiya MEF, Kodak DCS, Pentax PEF, Samsung SRW, Canon CR2, Nikon NEF/NRW, Panasonic RW2, Fujifilm RAF, Olympus ORF (uncompressed), Sony ARW, Canon CR3 and Canon CRW (through `craft-crw`) so far; demosaicing and everything after stay with the develop pipeline. +- [`craft-render-parity`](crates/render-parity/README.md): test support for the shared develop + pipeline (#587): how far two renders of the same photo are apart (per-channel error, CIEDE2000, + the worst pixel) and whether that is within a stated tolerance. - [`craft-tga`](crates/tga/src/lib.rs): Truevision Targa, from FilmCraft (reader) and PhotoCraft (writer): true-colour, grayscale and colour-mapped pictures, raw or run-length encoded, any origin, to RGBA8, and a run-length writer whose packets end with their scan line. Bounded on @@ -270,6 +275,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit AC-3/E-AC-3, MP1/MP2, MP3 encoding), streaming and seeking, and packet decoding for any demuxer. See its README for formats and features. - [`craft-latex`](crates/latex/README.md): LaTeX documents read (the everyday subset) and written (a standalone article), to and from the craft-flow model. +- [`craft-macho`](crates/macho/src/lib.rs): Mach-O header pieces from untrusted bytes, from Craft Anything: universal (fat and fat64) slice tables checked against the file, CPU and load command names, the fixed size each load command needs before its fields are read, and the thread-state program counter. - [`craft-markdown`](crates/markdown/README.md): Markdown read and written (a CommonMark subset with GitHub tables and strikethrough), to and from the craft-flow model. - [`craft-math`](crates/math/README.md): equations as one tree (the Office Math object model) with its syntaxes, read and written: OMML, presentation MathML, LaTeX math and the UnicodeMath linear format. - [`craft-media`](crates/media/README.md): bit-level I/O (`bitstream`), clean-room MP4/MOV @@ -414,6 +420,8 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit text-format crates read into and write from. WordCraft's model without its editing code. - [`craft-xlsx`](crates/xlsx/README.md): Excel workbooks (`.xlsx`, `.xlsm`, SpreadsheetML, transitional and strict) and binary workbooks (`.xlsb`) on `craft-opc`: the workbook part, shared strings, the styles tables and worksheet rows streamed one at a time, with limits on the package, the XML and the binary records. - [`craft-ora`](crates/ora/README.md): OpenRaster documents (`.ora`): the ZIP archive and the `stack.xml` layer tree as written, read with bounded sizes and written. From PhotoCraft. +- [`craft-sniff`](crates/sniff/src/lib.rs): what a file is from its first bytes: one dependency-free, wasm-safe signature table naming the format, its media type, extension and family for images, audio, video, 3D models, archives, compressed streams, executables and databases. Identifies only; parsing stays with each format's reader. +- [`craft-tar`](crates/tar/src/lib.rs): TAR (v7, ustar, GNU long names and base-256 sizes, local and global pax overrides) and cpio (newc, crc, odc) member walkers for hostile input, from Craft Anything: list each member, its kind, mode, link target and where its data lies, without extracting. Bounded by the input and a member cap; an optional `serde` feature serializes listings. - [`craft-y4m`](crates/y4m/src/lib.rs): YUV4MPEG2 (`.y4m`) reader, from FilmCraft: the stream header, colour space layouts at 8 to 16 bits, a frame index built from the `FRAME` lines without loading the file, and plane access. Bounded on hostile input. diff --git a/crates/aaf/src/aaf.rs b/crates/aaf/src/aaf.rs index 29f6a1024..8e4828f55 100644 --- a/crates/aaf/src/aaf.rs +++ b/crates/aaf/src/aaf.rs @@ -58,13 +58,56 @@ pub struct WriteOptions<'a> { /// 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) + write::write(doc, version, &opts.identity, &ReferenceOptions::default(), report) +} + +/// Optional interpretation choices for applications retaining older interchange behavior. +#[derive(Clone, Copy, Debug)] +pub struct ReadOptions { + /// Interpret a source clip's start using its target slot's edit rate. + pub source_slot_rate: bool, + /// Use the referenced mob name when the clip has no explicit name. + pub infer_clip_name: bool, + /// Decode percent escapes in relative network locators. + pub decode_relative_locator: bool, + /// Report selector and nested-scope flattening. + pub report_flattening: bool, + /// Accept truncated PCM WAVE essence summaries. + pub permissive_pcm: bool, +} +impl Default for ReadOptions { + fn default() -> Self { + Self { source_slot_rate: true, infer_clip_name: true, decode_relative_locator: true, report_flattening: true, permissive_pcm: false } + } +} +/// Read using explicitly selected interpretation choices. +pub fn read_with_options(bytes: &[u8], options: &ReadOptions, report: &mut Report) -> Result { + read::read(bytes, options, report) +} + +/// Optional compatibility choices for writing source references and locators. +#[derive(Clone, Copy, Debug)] +pub struct ReferenceOptions { + /// Write source offsets in target-slot units. + pub source_slot_rate: bool, + /// Percent-encode relative locators. + pub encode_relative_locator: bool, +} +impl Default for ReferenceOptions { + fn default() -> Self { + Self { source_slot_rate: true, encode_relative_locator: true } + } +} +/// Write with explicitly selected reference and locator conventions. +pub fn write_with_references(doc: &Document, opts: &WriteOptions<'_>, references: &ReferenceOptions, 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, references, 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) + read::read(bytes, &ReadOptions::default(), report) } /// Whether `bytes` look like an AAF file (a compound file whose root has the AAF root class or a diff --git a/crates/aaf/src/aaf/read.rs b/crates/aaf/src/aaf/read.rs index 1a8375997..fb5d44539 100644 --- a/crates/aaf/src/aaf/read.rs +++ b/crates/aaf/src/aaf/read.rs @@ -67,6 +67,7 @@ fn indirect_f64(d: &[u8]) -> Option { struct R<'a> { report: &'a mut Report, + options: super::ReadOptions, mobs: HashMap, essence: HashMap, doc: Document, @@ -90,7 +91,7 @@ struct NestClip<'a> { /// Compositions nested deeper than this are read as gaps. const MAX_NESTING: usize = 16; -pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { +pub(crate) fn read(bytes: &[u8], options: &super::ReadOptions, 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() })?; @@ -106,7 +107,8 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { essence.insert(id, d); } } - let mut r = R { report, mobs, essence, doc: Document::default(), sources: HashMap::new(), nests: HashMap::new(), open: Vec::new() }; + let mut r = + R { report, options: *options, 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 @@ -331,7 +333,9 @@ impl R<'_> { items } x if x == cls::SELECTOR => { - self.report.info("a selector was flattened to its selected input"); + if self.options.report_flattening { + self.report.info("a selector was flattened to its selected input"); + } match c.strong(pid::SELECTED) { Some(s) => { let mut items = self.segment(s, rate, kind, depth); @@ -342,7 +346,9 @@ impl R<'_> { } } x if x == cls::NESTED_SCOPE => { - self.report.info("a nested scope was flattened to its final input"); + if self.options.report_flattening { + self.report.info("a nested scope was flattened to its final input"); + } match c.objs(pid::NESTED_SLOTS).last() { Some(s) => { let mut items = self.segment(s, rate, kind, depth); @@ -388,7 +394,12 @@ impl R<'_> { let len = units_to_ticks(len_of(c), rate.0, rate.1); let tagged_name = tagged_values(c, pid::COMPONENT_USER_COMMENTS).into_iter().find(|(k, _)| k == "Clip Name").map(|(_, v)| v); let (Some(id), Some(slot)) = (c.mob_id(pid::SOURCE_ID), c.u32(pid::SOURCE_MOB_SLOT_ID)) else { return CItem::Filler(len) }; - let name = tagged_name.or_else(|| self.mobs.get(&id).and_then(|mob| mob.string(pid::MOB_NAME))).unwrap_or_default(); + let name = if self.options.infer_clip_name { + tagged_name.or_else(|| self.mobs.get(&id).and_then(|mob| mob.string(pid::MOB_NAME))) + } else { + tagged_name + } + .unwrap_or_default(); if id == [0; 32] { return CItem::Filler(len); } @@ -401,6 +412,7 @@ impl R<'_> { .and_then(|mob| mob.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(slot))) .and_then(|s| rate_of(s, pid::EDIT_RATE)) .unwrap_or(rate); + let source_rate = if self.options.source_slot_rate { source_rate } else { rate }; let start = units_to_ticks(c.i64(pid::START_TIME).unwrap_or(0), source_rate.0, source_rate.1); match self.resolve(id, slot, start, kind, master_key, 0) { Some((source, t)) => CItem::Clip(CClip { len, source, start: t, gain: None, name }), @@ -506,6 +518,7 @@ impl R<'_> { .and_then(|m| m.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(next_slot))) .and_then(|s| rate_of(s, pid::EDIT_RATE)) .unwrap_or(rate); + let source_rate = if self.options.source_slot_rate { source_rate } else { rate }; let inner = units_to_ticks(clip.i64(pid::START_TIME).unwrap_or(0), source_rate.0, source_rate.1); if next == [0; 32] { return None; @@ -545,7 +558,11 @@ impl R<'_> { DataKind::Sound => CKind::Sound, _ => kind, }; - let path = d.objs(pid::LOCATOR).iter().find_map(|l| l.string(pid::URL_STRING)).map(|u| locator_path(&u)); + let path = d + .objs(pid::LOCATOR) + .iter() + .find_map(|l| l.string(pid::URL_STRING)) + .map(|u| if self.options.decode_relative_locator { locator_path(&u) } else { 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) @@ -604,7 +621,8 @@ impl R<'_> { 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) { + let pcm = if self.options.permissive_pcm { craft_audio::essence_pcm::parse_wav(data) } else { crate::pcm::parse_wav(data) }; + if let Some((pcm, ch, sr, bits)) = pcm { s.embedded = Some(pcm); s.file_channels = ch as u32; s.channels = if s.channel.is_some() { 1 } else { ch as u32 }; diff --git a/crates/aaf/src/aaf/tests.rs b/crates/aaf/src/aaf/tests.rs index d054793c5..a8b6bbc05 100644 --- a/crates/aaf/src/aaf/tests.rs +++ b/crates/aaf/src/aaf/tests.rs @@ -557,3 +557,32 @@ fn relative_media_locator_round_trips_escaped_characters() { let got = read_clean(&bytes); assert!(got.sources.iter().any(|s| s.path.as_deref() == Some("Audio Files/100%20 #1.wav"))); } + +#[test] +fn compatibility_options_keep_relative_paths_and_parent_rate() { + let mut doc = crate::testdoc::sound_only(None); + doc.compositions[0].sample_rate = 44_100; + doc.sources[0].path = Some("Audio Files/100%20 #1.wav".into()); + let mut report = Report::default(); + let references = ReferenceOptions { source_slot_rate: false, encode_relative_locator: false }; + let bytes = write_with_references(&doc, &WriteOptions { identity: IDENTITY, small_sectors: false }, &references, &mut report).unwrap(); + let root = store::read(&bytes).unwrap(); + let mut all = Vec::new(); + walk(&root, &mut all); + assert!( + all.iter().filter(|o| o.class == cls::NETWORK_LOCATOR).any(|o| o.string(pid::URL_STRING).as_deref() == Some("Audio Files/100%20 #1.wav")) + ); + let starts: Vec = all.iter().filter(|o| o.class == cls::SOURCE_CLIP).filter_map(|o| o.i64(pid::START_TIME)).collect(); + assert!(starts.contains(&17_640), "parent rate start: {starts:?}"); + let opts = ReadOptions { + source_slot_rate: false, + infer_clip_name: false, + decode_relative_locator: false, + report_flattening: false, + permissive_pcm: true, + }; + let back = read_with_options(&bytes, &opts, &mut report).unwrap(); + assert!(back.sources.iter().any(|s| s.path.as_deref() == Some("Audio Files/100%20 #1.wav"))); + let clip = back.compositions[0].tracks[0].items.iter().find_map(|i| if let CItem::Clip(c) = i { Some(c) } else { None }).unwrap(); + assert_eq!(clip.start, FrameRate::FPS_25.tick_of(10)); +} diff --git a/crates/aaf/src/aaf/write.rs b/crates/aaf/src/aaf/write.rs index 06153dc0b..d43d58a8a 100644 --- a/crates/aaf/src/aaf/write.rs +++ b/crates/aaf/src/aaf/write.rs @@ -75,6 +75,7 @@ fn definition(class: Auid, id: Auid, name: &str) -> Obj { struct W<'a> { report: &'a mut Report, inexact: bool, + references: super::ReferenceOptions, } struct IdGen { @@ -350,9 +351,15 @@ fn add_tape_mob(first: &Source, tape_id: [u8; 32], timecode: Option<(i64, FrameR } /// Serialise a composition document. -pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Identity<'_>, report: &mut Report) -> Result> { +pub(crate) fn write( + doc: &Document, + version: craft_cfb::Version, + identity: &Identity<'_>, + references: &super::ReferenceOptions, + report: &mut Report, +) -> Result> { let comp = doc.compositions.first().ok_or(Error::Empty)?; - let mut w = W { report, inexact: false }; + let mut w = W { report, inexact: false, references: *references }; 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(); @@ -360,7 +367,8 @@ pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Iden // ---- 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: doc.sources.iter().enumerate().map(|(i, s)| (i, source_rate(s))).collect() }; + let rates = if references.source_slot_rate { doc.sources.iter().enumerate().map(|(i, s)| (i, source_rate(s))).collect() } else { HashMap::new() }; + let mut refs = Refs { mobs: master_ref, rates }; 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 }; @@ -795,8 +803,10 @@ fn descriptor(w: &mut W, s: &Source) -> Obj { 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 { + } else if w.references.encode_relative_locator { craft_timeline::path::encode_uri_path(p) + } else { + p.clone() }; d.set(pid::LOCATOR, Value::StrongVec(vec![Obj::new(cls::NETWORK_LOCATOR).data_prop(pid::URL_STRING, utf16z(&url))])); } diff --git a/crates/aaf/src/lib.rs b/crates/aaf/src/lib.rs index 5a914f5d8..77a6e190f 100644 --- a/crates/aaf/src/lib.rs +++ b/crates/aaf/src/lib.rs @@ -30,7 +30,11 @@ mod roundtrip_tests; #[cfg(test)] mod testdoc; +/// Diagnostic types used by readers and writers. +pub use craft_timeline::report; pub use craft_timeline::report::Report; +/// Exact time types used by the composition model. +pub use craft_timeline::time; /// Error from a reader or writer. Unsupported *features* never error; they go in the [`Report`]. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] diff --git a/crates/aaf/src/omf/read.rs b/crates/aaf/src/omf/read.rs index 2c446ab43..f825e9e60 100644 --- a/crates/aaf/src/omf/read.rs +++ b/crates/aaf/src/omf/read.rs @@ -12,6 +12,10 @@ type Uid = [u8; 12]; const MAX_REFERENCE_VISITS: usize = 65_536; +/// A source-clip search stopped at its work limit. +#[derive(Clone, Copy)] +struct SearchLimit; + struct R<'a, 'b> { c: &'b Container<'a>, report: &'b mut Report, @@ -21,15 +25,49 @@ struct R<'a, 'b> { media: HashMap>, doc: Document, sources: HashMap<(Uid, u32, String), usize>, + /// Component visits left for [`R::segment`] over the whole document. + budget: usize, + /// Outcome of the source-clip search under each segment: deterministic, so it is made once. + clip_search: HashMap, SearchLimit>>, + /// Visits and queued references left for source-clip searches over the whole document: + /// distinct roots over one shared graph would otherwise each search it in full. + search_budget: usize, } +/// Components one document may expand into timeline items; a small file can reference the same +/// sequence from many places and would otherwise multiply without bound. +const MAX_COMPONENT_VISITS: usize = 1_000_000; + +/// Reported when the shared component budget runs out. +const BUDGET_WARNING: &str = "OMF composition is too large to import completely (the remaining components are dropped)"; +/// Most visited components, and most queued references, one source-clip search may spend. +const MAX_CLIP_SEARCH_VISITS: usize = 100_000; +const MAX_CLIP_SEARCH_PENDING: usize = 1_000_000; +/// Work all source-clip searches in one document may spend together. +const MAX_DOCUMENT_SEARCH_WORK: usize = 2_000_000; + pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { + read_with_budget(bytes, report, MAX_COMPONENT_VISITS) +} + +fn read_with_budget(bytes: &[u8], report: &mut Report, budget: usize) -> 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 mut r = R { + c: &c, + report, + be, + mobs: HashMap::new(), + media: HashMap::new(), + doc: Document::default(), + sources: HashMap::new(), + budget, + clip_search: HashMap::new(), + search_budget: MAX_DOCUMENT_SEARCH_WORK, + }; 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() @@ -66,8 +104,14 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result { cmobs.iter().copied().filter(|m| r.uid(*m, "OMFI:MOBJ:MobID").is_none_or(|id| !referenced.contains(&id))).collect() }; for m in top { + // every composition spends from the budget too: a list of up to 65,535 repeated entries must not yield that many + if r.budget == 0 && !r.doc.compositions.is_empty() { + r.report.warn(BUDGET_WARNING); + break; + } let comp = r.composition(m); r.doc.compositions.push(comp); + r.budget = r.budget.saturating_sub(1); } if r.doc.compositions.is_empty() { return Err(Error::Empty); @@ -166,6 +210,13 @@ impl R<'_, '_> { let slots = self.refs(m, "OMFI:MOBJ:Slots"); let mut na = 0; for s in slots { + if self.budget == 0 { + self.report.warn(BUDGET_WARNING); + break; + } + // Every slot spends a unit, skipped ones too: a list of up to 65,535 repeated slots + // must not cost that much work in each of 65,535 repeated compositions. + self.budget -= 1; 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); @@ -213,13 +264,32 @@ impl R<'_, '_> { fn segment(&mut self, seg: u32, rate: (i64, i64), depth: usize) -> Vec { let len = units_to_ticks(self.len(seg), rate.0, rate.1); + // Depth alone does not bound a graph that references the same component many times at + // every level, so each visit also spends from a per-document budget. + // A visit stopped by depth spends from it too. + if self.budget == 0 { + self.report.warn(BUDGET_WARNING); + return vec![CItem::Filler(len)]; + } + self.budget -= 1; if depth > 32 { return vec![CItem::Filler(len)]; } match &self.class(seg) { b"SEQU" => { let mut out = Vec::new(); + let mut expanded = 0i64; for c in self.refs(seg, "OMFI:SEQU:Components") { + if self.budget == 0 { + // the rest of the sequence keeps its duration as one gap, not a gap per reference + self.report.warn(BUDGET_WARNING); + let rest = self.len(seg).saturating_sub(expanded); + if rest > 0 { + out.push(CItem::Filler(units_to_ticks(rest, rate.0, rate.1))); + } + break; + } + expanded = expanded.saturating_add(self.len(c)); out.extend(self.segment(c, rate, depth + 1)); } out @@ -306,12 +376,66 @@ impl R<'_, '_> { .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, + /// First source clip under `seg`, depth-first; a sequence referencing itself (directly or + /// through a cycle) ends the search rather than the stack. `Err` when the search spent its + /// budget of visited components or queued references before finishing (the answer is unknown). + fn first_clip(&mut self, seg: u32) -> std::result::Result, SearchLimit> { + if let Some(found) = self.clip_search.get(&seg) { + return *found; } + let mut spent = 0; + let found = if self.search_budget == 0 { + Err(SearchLimit) + } else { + let visits = MAX_CLIP_SEARCH_VISITS.min(self.search_budget); + let pending = MAX_CLIP_SEARCH_PENDING.min(self.search_budget); + self.first_clip_within(seg, visits, pending, &mut spent) + }; + self.search_budget = self.search_budget.saturating_sub(spent); + self.clip_search.insert(seg, found); + found + } + + /// `spent` receives the visits plus queued references the search used. + fn first_clip_within(&self, seg: u32, max_visits: usize, max_pending: usize, spent: &mut usize) -> std::result::Result, SearchLimit> { + let mut visited = HashSet::new(); + let mut todo = vec![seg]; + let mut queued = 1usize; + let found = Self::search_from(self, &mut visited, &mut todo, &mut queued, max_visits, max_pending); + *spent = visited.len().saturating_add(queued); + found + } + + fn search_from( + &self, + visited: &mut HashSet, + todo: &mut Vec, + queued: &mut usize, + max_visits: usize, + max_pending: usize, + ) -> std::result::Result, SearchLimit> { + while let Some(seg) = todo.pop() { + if !visited.insert(seg) { + continue; + } + if visited.len() > max_visits { + return Err(SearchLimit); + } + match &self.class(seg) { + b"SCLP" => return Ok(Some(seg)), + b"SEQU" => { + let comps = self.refs(seg, "OMFI:SEQU:Components"); + // sequences may share one component list, so queued references grow with expansions, not visits + *queued = queued.saturating_add(comps.len()); + if *queued > max_pending { + return Err(SearchLimit); + } + todo.extend(comps.into_iter().rev()); + } + _ => {} + } + } + Ok(None) } fn resolve(&mut self, id: Uid, track: u32, start: Tick, master: Option, depth: usize) -> Option<(usize, Tick)> { @@ -328,7 +452,13 @@ impl R<'_, '_> { 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 clip = match self.first_clip(seg) { + Ok(c) => c?, + Err(SearchLimit) => { + self.report.warn("a master mob is too large to search for its source clip (imported as a gap)"); + return None; + } + }; 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")?; @@ -353,7 +483,9 @@ impl R<'_, '_> { 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 fmt = craft_audio::essence_pcm::parse_wav(&summary) + .or_else(|| craft_audio::essence_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) { @@ -373,7 +505,8 @@ impl R<'_, '_> { 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 first = self.first_clip(seg).ok().flatten(); + let tc = first.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| { @@ -415,7 +548,7 @@ impl R<'_, '_> { 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)) { + if let Some((pcm, ch, sr, b)) = craft_audio::essence_pcm::parse_wav(d).or_else(|| craft_audio::essence_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 }; @@ -437,3 +570,200 @@ impl R<'_, '_> { } } } + +#[cfg(test)] +mod tests { + use super::super::bento::Writer; + use super::*; + + fn obj(w: &mut Writer, class: &[u8; 4]) -> u32 { + let id = w.new_object(); + w.set(id, "OMFI:OOBJ:ObjClass", "omfi:ClassID", class); + id + } + fn list(w: &mut Writer, 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()); + } + w.set(o, p, "omfi:ObjRefArray", &v); + } + + fn search(max_visits: usize, max_pending: usize, build: impl FnOnce(&mut Writer) -> u32) -> std::result::Result, SearchLimit> { + let mut w = Writer::new(); + let root = build(&mut w); + let bytes = w.finish(); + let c = Container::open(&bytes).unwrap(); + let mut report = Report::default(); + let r = R { + c: &c, + report: &mut report, + be: true, + mobs: HashMap::new(), + media: HashMap::new(), + doc: Document::default(), + sources: HashMap::new(), + budget: 0, + clip_search: HashMap::new(), + search_budget: MAX_DOCUMENT_SEARCH_WORK, + }; + r.first_clip_within(root, max_visits, max_pending, &mut 0) + } + + #[test] + fn clip_search_reports_its_limits() { + // found within the limits + assert!(matches!( + search(10, 10, |w| { + let (s, c) = (obj(w, b"SEQU"), obj(w, b"SCLP")); + list(w, s, "OMFI:SEQU:Components", &[c]); + s + }), + Ok(Some(_)) + )); + // too many distinct components before any clip + assert!( + search(5, 1000, |w| { + let (s, f) = (obj(w, b"SEQU"), (0..20).map(|_| obj(w, b"FILL")).collect::>()); + list(w, s, "OMFI:SEQU:Components", &f); + s + }) + .is_err() + ); + // few visits, but sequences that each queue a long list of references + assert!( + search(1000, 500, |w| { + let f = obj(w, b"FILL"); + let inner: Vec = (0..20).map(|_| obj(w, b"SEQU")).collect(); + for &i in &inner { + list(w, i, "OMFI:SEQU:Components", &[f; 100]); + } + let s = obj(w, b"SEQU"); + list(w, s, "OMFI:SEQU:Components", &inner); + s + }) + .is_err() + ); + // a search that ends without a clip is not a limit + assert!(matches!(search(10, 10, |w| obj(w, b"FILL")), Ok(None))); + } + + #[test] + fn skipped_slots_spend_the_budget() { + let mut w = Writer::new(); + let head = obj(&mut w, b"HEAD"); + let cm = obj(&mut w, b"CMOB"); + let slot = obj(&mut w, b"MSLT"); + list(&mut w, cm, "OMFI:MOBJ:Slots", &[slot; 200]); + list(&mut w, head, "OMFI:HEAD:Mobs", &[cm]); + list(&mut w, head, "OMFI:HEAD:PrimaryMobs", &[cm]); + let bytes = w.finish(); + let mut report = Report::default(); + read_with_budget(&bytes, &mut report, 50).unwrap(); + assert!(report.mentions(BUDGET_WARNING), "slots with no segment cost nothing before"); + } + + #[test] + fn a_source_clip_search_is_made_once_per_segment() { + let mut w = Writer::new(); + let (s, c) = (obj(&mut w, b"SEQU"), obj(&mut w, b"SCLP")); + list(&mut w, s, "OMFI:SEQU:Components", &[c]); + let bytes = w.finish(); + let c = Container::open(&bytes).unwrap(); + let mut report = Report::default(); + let mut r = R { + c: &c, + report: &mut report, + be: true, + mobs: HashMap::new(), + media: HashMap::new(), + doc: Document::default(), + sources: HashMap::new(), + budget: 0, + clip_search: HashMap::new(), + search_budget: MAX_DOCUMENT_SEARCH_WORK, + }; + let first = r.first_clip(s).ok(); + assert!(matches!(first, Some(Some(_)))); + assert_eq!(r.clip_search.len(), 1); + assert_eq!(r.first_clip(s).ok(), first); + assert_eq!(r.clip_search.len(), 1); + } + + #[test] + fn repeated_slots_and_compositions_spend_the_budget() { + let mut w = Writer::new(); + let head = obj(&mut w, b"HEAD"); + let cm = obj(&mut w, b"CMOB"); + let dd = obj(&mut w, b"DDEF"); + w.set(dd, "OMFI:DDEF:DataKindID", "omfi:UniqueName", b"omfi:data:Sound\0"); + let fill = obj(&mut w, b"FILL"); + w.set(fill, "OMFI:CPNT:DataKind", "omfi:ObjRef", &dd.to_be_bytes()); + let slot = obj(&mut w, b"MSLT"); + w.set(slot, "OMFI:MSLT:Segment", "omfi:ObjRef", &fill.to_be_bytes()); + list(&mut w, cm, "OMFI:MOBJ:Slots", &[slot; 200]); + list(&mut w, head, "OMFI:HEAD:Mobs", &[cm]); + list(&mut w, head, "OMFI:HEAD:PrimaryMobs", &[cm; 200]); + let bytes = w.finish(); + let mut report = Report::default(); + let doc = read_with_budget(&bytes, &mut report, 50).unwrap(); + let tracks: usize = doc.compositions.iter().map(|c| c.tracks.len()).sum(); + assert!(tracks <= 50, "{tracks} tracks from a budget of 50"); + assert!(doc.compositions.len() <= 2, "{} compositions", doc.compositions.len()); + assert!(report.has_warnings()); + } + + fn with_reader(bytes: &[u8], budget: usize, search_budget: usize, f: impl FnOnce(&mut R) -> T) -> (T, Report) { + let c = Container::open(bytes).unwrap(); + let mut report = Report::default(); + let mut r = R { + c: &c, + report: &mut report, + be: true, + mobs: HashMap::new(), + media: HashMap::new(), + doc: Document::default(), + sources: HashMap::new(), + budget, + clip_search: HashMap::new(), + search_budget, + }; + let out = f(&mut r); + (out, report) + } + + #[test] + fn depth_limited_components_spend_the_budget() { + // a chain 33 sequences deep whose last one repeats a depth-limited sequence many times + let mut w = Writer::new(); + let deep = obj(&mut w, b"SEQU"); + let chain: Vec = (0..33).map(|_| obj(&mut w, b"SEQU")).collect(); + for pair in chain.windows(2) { + list(&mut w, pair[0], "OMFI:SEQU:Components", &[pair[1]]); + } + list(&mut w, chain[32], "OMFI:SEQU:Components", &[deep; 1000]); + let bytes = w.finish(); + let (items, report) = with_reader(&bytes, 100, MAX_DOCUMENT_SEARCH_WORK, |r| r.segment(chain[0], (48_000, 1), 0)); + assert!(items.len() <= 100, "{} items from a budget of 100", items.len()); + assert!(report.mentions(BUDGET_WARNING)); + } + + #[test] + fn source_clip_searches_share_a_document_budget() { + // distinct roots over one shared clip-free graph + let mut w = Writer::new(); + let fills: Vec = (0..50).map(|_| obj(&mut w, b"FILL")).collect(); + let shared = obj(&mut w, b"SEQU"); + list(&mut w, shared, "OMFI:SEQU:Components", &fills); + let roots: Vec = (0..30).map(|_| obj(&mut w, b"SEQU")).collect(); + for &root in &roots { + list(&mut w, root, "OMFI:SEQU:Components", &[shared]); + } + let bytes = w.finish(); + let (results, _) = with_reader(&bytes, 0, 500, |r| roots.iter().map(|&root| r.first_clip(root)).collect::>()); + assert!(matches!(results[0], Ok(None)), "the first search fits"); + assert!(results.iter().any(|r| r.is_err()), "later searches run out of the shared budget"); + let (results, _) = with_reader(&bytes, 0, MAX_DOCUMENT_SEARCH_WORK, |r| roots.iter().map(|&root| r.first_clip(root)).collect::>()); + assert!(results.iter().all(|r| matches!(r, Ok(None)))); + } +} diff --git a/crates/aaf/src/pcm.rs b/crates/aaf/src/pcm.rs index 789c976a8..9992b2d3b 100644 --- a/crates/aaf/src/pcm.rs +++ b/crates/aaf/src/pcm.rs @@ -4,8 +4,3 @@ 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/audio/src/essence_pcm.rs b/crates/audio/src/essence_pcm.rs index 593ebb1c2..dfb119b8b 100644 --- a/crates/audio/src/essence_pcm.rs +++ b/crates/audio/src/essence_pcm.rs @@ -63,6 +63,7 @@ pub fn parse_aiff(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { if b.len() < 12 || &b[..4] != b"FORM" || !(&b[8..12] == b"AIFF" || &b[8..12] == b"AIFC") { return None; } + let aifc = &b[8..12] == b"AIFC"; let mut at = 12; let mut comm = None; while at + 8 <= b.len() { @@ -73,7 +74,17 @@ pub fn parse_aiff(b: &[u8]) -> Option<(Vec, u16, u32, u16)> { let ch = u16::from_be_bytes([body[0], body[1]]); let bits = u16::from_be_bytes([body[6], body[7]]); let sr = from_extended(body[8..18].try_into().ok()?).round() as u32; - let little = body.len() >= 22 && &body[18..22] == b"sowt"; + // AIFF has no compression code (trailing COMM bytes mean nothing); AIFF-C must name one + // of the integer PCM codes, or the samples are not integer PCM at all + let little = if aifc { + match body.get(18..22)? { + b"NONE" | b"twos" => false, + b"sowt" => true, + _ => return None, + } + } else { + false + }; comm = Some((ch, sr, bits, little)); } else if id == b"SSND" && body.len() >= 8 { let (ch, sr, bits, little) = comm?; @@ -144,4 +155,35 @@ mod tests { let _ = parse_aiff(&a[..cut.min(a.len())]); } } + + #[test] + fn aifc_needs_an_integer_pcm_compression_code() { + let file = |kind: &[u8; 4], code: &[u8]| { + let mut comm = vec![0, 1, 0, 0, 0, 1, 0, 16]; + comm.extend_from_slice(&[0x40, 0x0E, 0xBB, 0x80, 0, 0, 0, 0, 0, 0]); + comm.extend_from_slice(code); + let mut body = kind.to_vec(); + body.extend_from_slice(b"COMM"); + body.extend_from_slice(&(comm.len() as u32).to_be_bytes()); + body.extend_from_slice(&comm); + if comm.len() & 1 == 1 { + body.push(0); + } + body.extend_from_slice(b"SSND"); + body.extend_from_slice(&10u32.to_be_bytes()); + body.extend_from_slice(&[0; 8]); + body.extend_from_slice(&[0x12, 0x34]); + let mut v = b"FORM".to_vec(); + v.extend_from_slice(&(body.len() as u32).to_be_bytes()); + v.extend_from_slice(&body); + v + }; + assert_eq!(parse_aiff(&file(b"AIFC", b"NONE")).map(|p| p.0), Some(vec![0x34, 0x12])); + assert_eq!(parse_aiff(&file(b"AIFC", b"twos")).map(|p| p.0), Some(vec![0x34, 0x12])); + assert_eq!(parse_aiff(&file(b"AIFC", b"sowt")).map(|p| p.0), Some(vec![0x12, 0x34])); + assert!(parse_aiff(&file(b"AIFC", b"fl32")).is_none(), "float samples are not integer PCM"); + assert!(parse_aiff(&file(b"AIFC", b"")).is_none(), "AIFF-C without a compression code"); + // plain AIFF ignores trailing COMM bytes + assert_eq!(parse_aiff(&file(b"AIFF", b"sowt")).map(|p| p.0), Some(vec![0x34, 0x12])); + } } diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 4f8cb7309..2766be64d 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -79,6 +79,8 @@ mod write; /// PCM WAVE and AIFF wrappers for interchange essence. #[cfg(feature = "pcm-encode")] pub mod essence_pcm; +/// Streaming WAVE and AIFF headers and sample packing. +pub mod pcm_export; /// Interleaved PCM packet decoding. pub mod pcm_interleaved; /// Range-backed PCM WAVE access and a 16-bit PCM writer. @@ -762,3 +764,21 @@ extern crate self as craft_audio; #[cfg(test)] #[path = "../tests/main.rs"] mod integration; + +// An encoder refusing its settings is an encode error, not damaged input. +#[cfg(all(test, feature = "aac-encode"))] +mod tests { + use super::*; + + #[test] + fn aac_encoder_settings_are_encode_errors() { + let e = match aac::Encoder::new(aac::EncoderConfig::cbr(44_000, 2, 128_000)) { + Err(e) => AudioError::from(e), + Ok(_) => panic!("44 kHz is not an AAC rate"), + }; + assert!(matches!(e, AudioError::Encode(_)), "{e}"); + } +} + +/// DVD and Blu-ray LPCM packet decoding. +pub mod lpcm; diff --git a/crates/audio/src/lpcm.rs b/crates/audio/src/lpcm.rs new file mode 100644 index 000000000..5ec13b35d --- /dev/null +++ b/crates/audio/src/lpcm.rs @@ -0,0 +1,118 @@ +//! DVD-Video and Blu-ray LPCM packet grammar and sample decoding. + +/// Blu-ray / AVCHD LPCM packet header (4 bytes before the samples). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct BlurayLpcm { + /// Sample rate in hertz. + pub sample_rate: u32, + /// Number of decoded channels. + pub channels: usize, + /// Significant bits per sample. + pub bits: u32, +} + +/// Channels of each channel_assignment code (mono, stereo, 3/0, 2/1, 3/1, 2/2, 3/2, 3/2+LFE, +/// 3/4, 3/4+LFE). +const BD_LPCM_CHANNELS: [usize; 12] = [0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8]; + +/// Parse and validate the four-byte Blu-ray LPCM header. +pub fn bluray_lpcm_header(p: &[u8]) -> Option { + if p.len() < 4 { + return None; + } + let channels = *BD_LPCM_CHANNELS.get((p[2] >> 4) as usize).filter(|&&c| c > 0)?; + let sample_rate = match p[2] & 15 { + 1 => 48_000, + 4 => 96_000, + 5 => 192_000, + _ => return None, + }; + let bits = match p[3] >> 6 { + 1 => 16, + 2 => 20, + 3 => 24, + _ => return None, + }; + Some(BlurayLpcm { sample_rate, channels, bits }) +} + +/// Decode one Blu-ray LPCM packet (big-endian; channels padded to an even count; 20-bit +/// samples are stored in 24 bits). +pub fn decode_bluray_lpcm(p: &[u8]) -> Option>> { + let h = bluray_lpcm_header(p)?; + let coded = (h.channels + 1) & !1; + let bps = if h.bits == 16 { 2 } else { 3 }; + let data = &p[4..]; + let n = data.len() / (coded * bps); + let mut out = vec![Vec::with_capacity(n); h.channels]; + for k in 0..n { + for (c, o) in out.iter_mut().enumerate() { + let at = (k * coded + c) * bps; + let v = if bps == 2 { + i16::from_be_bytes([data[at], data[at + 1]]) as f32 / 32768.0 + } else { + (i32::from_be_bytes([data[at], data[at + 1], data[at + 2], 0]) >> 8) as f32 / 8_388_608.0 + }; + o.push(v); + } + } + Some(out) +} + +/// Bytes per two sample frames of DVD LPCM. The caller must bound `channels` to the DVD channel range. +pub fn dvd_lpcm_group_bytes(channels: usize, bits: u32) -> usize { + match bits { + 16 => 4 * channels, + 20 => 5 * channels, + _ => 6 * channels, + } +} + +/// Decode DVD-Video LPCM (big-endian). 16-bit samples are interleaved; 20- and 24-bit samples +/// come in groups of two frames: the upper 16 bits of all the group's samples, then their low +/// 4 / 8 bits. The caller must bound `channels` to the DVD channel range. +pub fn decode_dvd_lpcm(data: &[u8], channels: usize, bits: u32) -> Vec> { + let ch = channels.max(1); + let group = dvd_lpcm_group_bytes(ch, bits); + let groups = data.len() / group; + let mut out = vec![Vec::with_capacity(groups * 2); ch]; + for g in 0..groups { + let b = &data[g * group..(g + 1) * group]; + for f in 0..2 { + for (c, o) in out.iter_mut().enumerate() { + let k = f * ch + c; + let hi = i16::from_be_bytes([b[2 * k], b[2 * k + 1]]) as i32; + let v = match bits { + 16 => hi as f32 / 32768.0, + 20 => { + let nib = b[4 * ch + k / 2]; + let lo = if k.is_multiple_of(2) { nib >> 4 } else { nib & 15 } as i32; + ((hi << 4) | lo) as f32 / 524_288.0 + } + _ => ((hi << 8) | b[4 * ch + k] as i32) as f32 / 8_388_608.0, + }; + o.push(v); + } + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn bluray_header_and_even_channel_padding() { + let packet = [0, 0, 0x31, 0x40, 0x40, 0, 0xc0, 0]; + assert_eq!(bluray_lpcm_header(&packet), Some(BlurayLpcm { sample_rate: 48_000, channels: 2, bits: 16 })); + assert_eq!(decode_bluray_lpcm(&packet), Some(vec![vec![0.5], vec![-0.5]])); + assert_eq!(decode_bluray_lpcm(&packet[..3]), None); + } + #[test] + fn dvd_lpcm_16_20_and_24_bit_groups() { + assert_eq!(decode_dvd_lpcm(&[0x40, 0, 0xc0, 0], 1, 16), vec![vec![0.5, -0.5]]); + assert_eq!(decode_dvd_lpcm(&[0x40, 0, 0xc0, 0, 0], 1, 20), vec![vec![0.5, -0.5]]); + assert_eq!(decode_dvd_lpcm(&[0x40, 0, 0xc0, 0, 0, 0], 1, 24), vec![vec![0.5, -0.5]]); + assert_eq!(decode_dvd_lpcm(&[1, 2, 3], 1, 16), vec![Vec::::new()]); + } +} diff --git a/crates/audio/src/pcm_export.rs b/crates/audio/src/pcm_export.rs new file mode 100644 index 000000000..618ba281d --- /dev/null +++ b/crates/audio/src/pcm_export.rs @@ -0,0 +1,287 @@ +//! Audio-only files: WAV (RIFF, little-endian PCM; RF64 past 4 GiB) and AIFF (big-endian PCM), +//! 16, 24 or 32-bit integer or 32-bit float ([`SampleFormat`]), for streaming export. +//! Float WAV is `WAVE_FORMAT_IEEE_FLOAT` with a `fact` chunk; float AIFF is AIFF-C (`fl32`). +//! +//! The header is written first from the known length (`header`), then the samples in chunks +//! (`encode`) and the pad byte (`pad`), so an export streams to disk instead of building the +//! file in memory. + +/// PCM representation in an exported file. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SampleFormat { + /// 16-bit signed integer. + Int16, + /// 24-bit signed integer. + Int24, + /// 32-bit signed integer. + Int32, + /// 32-bit IEEE float. + Float32, +} + +impl SampleFormat { + /// Bits per sample. + pub fn bits(self) -> u16 { + match self { + Self::Int16 => 16, + Self::Int24 => 24, + Self::Int32 | Self::Float32 => 32, + } + } + /// Bytes per sample. + pub fn bytes(self) -> usize { + usize::from(self.bits() / 8) + } + /// Whether this is a floating point format. + pub fn is_float(self) -> bool { + self == Self::Float32 + } + /// Clip and quantize one sample with symmetric integer full scale; NaN becomes silence. + pub fn quantise(self, s: f32) -> i32 { + let s = s.clamp(-1.0, 1.0); + match self { + Self::Int16 => (s * 32_767.0).round() as i32, + Self::Int24 => (s * 8_388_607.0).round() as i32, + Self::Int32 => (f64::from(s) * 2_147_483_647.0).round() as i32, + Self::Float32 => 0, + } + } + /// Sanitize a floating sample: NaN becomes silence, infinity becomes the largest finite value. + pub fn float(s: f32) -> f32 { + if s.is_nan() { 0.0 } else { s.clamp(f32::MIN, f32::MAX) } + } +} + +/// The container of an audio-only export. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PcmContainer { + /// RIFF/WAVE or RF64. + Wav, + /// AIFF or AIFF-C. + Aiff, +} + +/// Bytes of sample data for `frames` frames, or None when that does not fit 64 bits. +fn data_len(frames: u64, channels: u16, format: SampleFormat) -> Option { + frames.checked_mul(u64::from(channels))?.checked_mul(format.bytes() as u64) +} + +/// The pad byte after an odd-sized data chunk (RIFF and AIFF chunks are word-aligned). +pub fn pad(frames: u64, channels: u16, format: SampleFormat) -> &'static [u8] { + if data_len(frames, channels, format).is_some_and(|n| n & 1 == 1) { &[0] } else { &[] } +} + +/// `KSDATAFORMAT_SUBTYPE_PCM` / `_IEEE_FLOAT` after their first two bytes (the format tag). +const SUBTYPE_TAIL: [u8; 14] = [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71]; + +/// AIFF-C format version 1 (`FVER`). +const AIFC_VERSION_1: u32 = 0xA280_5140; + +/// The AIFF-C compression name of `fl32`, as a Pascal string of even length. +const FL32_NAME: &[u8] = b"\x1532-bit floating point"; + +/// Everything before the samples of a file of `frames` frames. A WAV whose RIFF size would not +/// fit 32 bits (4 GiB: 24-bit 5.1 at 96 kHz after about 41 minutes) is written as RF64 (EBU Tech +/// 3306), with the 64-bit sizes in a `ds64` chunk. AIFF has no 64-bit form, so a longer AIFF is +/// an error naming WAV instead. +pub fn header(container: PcmContainer, channels: u16, sample_rate: u32, format: SampleFormat, frames: u64) -> Result, String> { + let too_long = || "the audio is too long to write".to_string(); + if channels == 0 { + return Err("audio needs at least one channel".into()); + } + if sample_rate == 0 { + return Err("audio needs a sample rate".into()); + } + let bits = format.bits(); + let bps = bits / 8; + let float = format.is_float(); + let data = data_len(frames, channels, format).ok_or_else(too_long)?; + let block_align = channels.checked_mul(bps).ok_or("too many audio channels")?; + match container { + PcmContainer::Wav => { + let byte_rate = + u32::try_from(u64::from(sample_rate) * u64::from(block_align)).map_err(|_| "the audio sample rate is too high".to_string())?; + let ext = channels > 2; + // plain PCM has no cbSize; IEEE float has an empty one + let fmt_len: u32 = if ext { + 40 + } else if float { + 18 + } else { + 16 + }; + // every format but integer PCM has a `fact` chunk (the frame count; RF64: in `ds64`) + let fact_len: u64 = if float { 12 } else { 0 }; + let padded = data.checked_add(data & 1).ok_or_else(too_long)?; + // "WAVE" + fmt chunk + fact chunk + data chunk header + samples + let riff = (4 + 8 + u64::from(fmt_len) + fact_len + 8).checked_add(padded).ok_or_else(too_long)?; + let rf64 = riff > u64::from(u32::MAX); + let mut v = Vec::with_capacity(100); + if rf64 { + const DS64: u64 = 8 + 28; + v.extend_from_slice(b"RF64"); + v.extend_from_slice(&u32::MAX.to_le_bytes()); + v.extend_from_slice(b"WAVEds64"); + v.extend_from_slice(&28u32.to_le_bytes()); + v.extend_from_slice(&riff.checked_add(DS64).ok_or_else(too_long)?.to_le_bytes()); + v.extend_from_slice(&data.to_le_bytes()); + v.extend_from_slice(&frames.to_le_bytes()); + v.extend_from_slice(&0u32.to_le_bytes()); // no table entries + } else { + v.extend_from_slice(b"RIFF"); + v.extend_from_slice(&(riff as u32).to_le_bytes()); + v.extend_from_slice(b"WAVE"); + } + let tag: u16 = if float { 3 } else { 1 }; + v.extend_from_slice(b"fmt "); + v.extend_from_slice(&fmt_len.to_le_bytes()); + v.extend_from_slice(&(if ext { 0xFFFE } else { tag }).to_le_bytes()); + v.extend_from_slice(&channels.to_le_bytes()); + v.extend_from_slice(&sample_rate.to_le_bytes()); + v.extend_from_slice(&byte_rate.to_le_bytes()); + v.extend_from_slice(&block_align.to_le_bytes()); + v.extend_from_slice(&bits.to_le_bytes()); + if ext { + // cbSize, valid bits, channel mask, KSDATAFORMAT_SUBTYPE_PCM / _IEEE_FLOAT + v.extend_from_slice(&22u16.to_le_bytes()); + v.extend_from_slice(&bits.to_le_bytes()); + let mask: u32 = if channels == 6 { 0x3F } else { 0 }; + v.extend_from_slice(&mask.to_le_bytes()); + v.extend_from_slice(&tag.to_le_bytes()); + v.extend_from_slice(&SUBTYPE_TAIL); + } else if float { + v.extend_from_slice(&0u16.to_le_bytes()); + } + if float { + v.extend_from_slice(b"fact"); + v.extend_from_slice(&4u32.to_le_bytes()); + v.extend_from_slice(&(if rf64 { u32::MAX } else { u32::try_from(frames).unwrap_or(u32::MAX) }).to_le_bytes()); + } + v.extend_from_slice(b"data"); + v.extend_from_slice(&(if rf64 { u32::MAX } else { data as u32 }).to_le_bytes()); + Ok(v) + } + PcmContainer::Aiff => { + let aiff_too_long = || "AIFF holds at most 4 GB of audio: export WAV, which has no such limit".to_string(); + let frames32 = u32::try_from(frames).map_err(|_| aiff_too_long())?; + // SSND: offset + block size + samples + let ssnd = u32::try_from(data.checked_add(8).ok_or_else(aiff_too_long)?).map_err(|_| aiff_too_long())?; + // floats need AIFF-C: a version chunk, and the compression type and name in COMM + let comm: u32 = if float { 18 + 4 + FL32_NAME.len() as u32 } else { 18 }; + let fver: u32 = if float { 8 + 4 } else { 0 }; + let form = (4u32 + fver + (8 + comm)) + .checked_add(8) + .and_then(|n| n.checked_add(ssnd)) + .and_then(|n| n.checked_add(ssnd & 1)) + .ok_or_else(aiff_too_long)?; + let mut v = Vec::with_capacity(90); + v.extend_from_slice(b"FORM"); + v.extend_from_slice(&form.to_be_bytes()); + if float { + v.extend_from_slice(b"AIFCFVER"); + v.extend_from_slice(&4u32.to_be_bytes()); + v.extend_from_slice(&AIFC_VERSION_1.to_be_bytes()); + } else { + v.extend_from_slice(b"AIFF"); + } + v.extend_from_slice(b"COMM"); + v.extend_from_slice(&comm.to_be_bytes()); + v.extend_from_slice(&channels.to_be_bytes()); + v.extend_from_slice(&frames32.to_be_bytes()); + v.extend_from_slice(&bits.to_be_bytes()); + v.extend_from_slice(&extended(sample_rate)); + if float { + v.extend_from_slice(b"fl32"); + v.extend_from_slice(FL32_NAME); + } + v.extend_from_slice(b"SSND"); + v.extend_from_slice(&ssnd.to_be_bytes()); + v.extend_from_slice(&0u32.to_be_bytes()); // offset + v.extend_from_slice(&0u32.to_be_bytes()); // block size + Ok(v) + } + } +} + +/// Append interleaved samples in `format` (little-endian for WAV, big-endian for AIFF). +pub fn encode(container: PcmContainer, interleaved: &[f32], format: SampleFormat, out: &mut Vec) { + let bps = format.bytes(); + out.reserve(interleaved.len().saturating_mul(bps)); + for &s in interleaved { + let q = if format.is_float() { SampleFormat::float(s).to_bits() as i32 } else { format.quantise(s) }; + match container { + PcmContainer::Wav => out.extend_from_slice(q.to_le_bytes().get(..bps).unwrap_or_default()), + PcmContainer::Aiff => out.extend_from_slice(q.to_be_bytes().get(4 - bps..).unwrap_or_default()), + } + } +} + +/// A whole file in memory (short audio; exports stream with [`header`] and [`encode`]). +fn write_whole(container: PcmContainer, interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + let per_frame = usize::from(channels.max(1)); + if !interleaved.len().is_multiple_of(per_frame) { + return Err("the samples do not make whole frames".into()); + } + let frames = (interleaved.len() / per_frame) as u64; + let mut v = header(container, channels, sample_rate, format, frames)?; + encode(container, interleaved, format, &mut v); + v.extend_from_slice(pad(frames, channels, format)); + Ok(v) +} + +/// A RIFF/WAVE file of interleaved samples. More than two channels are written as +/// `WAVE_FORMAT_EXTENSIBLE` with the speaker mask of the layout (6 channels: L, R, C, LFE, Ls, Rs +/// = `0x3F`). +pub fn write_wav(interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + write_whole(PcmContainer::Wav, interleaved, channels, sample_rate, format) +} + +/// IEEE 754 80-bit extended encoding of a sample rate (AIFF `COMM`). +pub fn extended(rate: u32) -> [u8; 10] { + let mut out = [0u8; 10]; + if rate == 0 { + return out; + } + let shift = rate.leading_zeros(); + let mant = (rate as u64) << (32 + shift); + let exp = 16383 + 31 - shift as u16; + out[..2].copy_from_slice(&exp.to_be_bytes()); + out[2..].copy_from_slice(&mant.to_be_bytes()); + out +} + +/// An AIFF file (`FORM`/`AIFF`, `COMM` + `SSND`; AIFF-C for floats) of interleaved samples. +pub fn write_aiff(interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result, String> { + write_whole(PcmContainer::Aiff, interleaved, channels, sample_rate, format) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn streaming_formats_preserve_samples_and_bounds() { + let samples = [1.0, -1.0, f32::NAN]; + let wav = write_wav(&samples, 1, 48_000, SampleFormat::Int24).unwrap(); + assert_eq!(&wav[..4], b"RIFF"); + assert_eq!(&wav[44..53], &[255, 255, 127, 1, 0, 128, 0, 0, 0]); + let h = header(PcmContainer::Wav, 6, 96_000, SampleFormat::Int24, 96_000 * 3600).unwrap(); + assert_eq!(&h[..4], b"RF64"); + assert!(header(PcmContainer::Aiff, 2, 48_000, SampleFormat::Int24, u64::MAX).is_err()); + let aiff = write_aiff(&[0.25, -3.0], 1, 44_100, SampleFormat::Float32).unwrap(); + assert_eq!(&aiff[8..12], b"AIFC"); + assert_eq!(&aiff[aiff.len() - 8..], [0.25f32.to_be_bytes(), (-3.0f32).to_be_bytes()].concat()); + } + + #[test] + fn zero_channels_rates_and_partial_frames_are_refused() { + for container in [PcmContainer::Wav, PcmContainer::Aiff] { + assert!(header(container, 0, 48_000, SampleFormat::Int16, 1).is_err()); + assert!(header(container, 1, 0, SampleFormat::Int16, 1).is_err()); + assert!(header(container, 2, 48_000, SampleFormat::Int16, 0).is_ok(), "zero frames stay valid"); + } + assert!(write_wav(&[0.5], 0, 48_000, SampleFormat::Int16).is_err()); + assert!(write_wav(&[0.1, 0.2, 0.3], 2, 48_000, SampleFormat::Int16).is_err()); + assert!(write_aiff(&[0.1, 0.2, 0.3], 2, 48_000, SampleFormat::Int16).is_err()); + assert!(write_aiff(&[0.1, 0.2], 2, 48_000, SampleFormat::Int16).is_ok()); + } +} diff --git a/crates/docx/src/write/story/drawing.rs b/crates/docx/src/write/story/drawing.rs index 7c03cca6e..94154f649 100644 --- a/crates/docx/src/write/story/drawing.rs +++ b/crates/docx/src/write/story/drawing.rs @@ -69,9 +69,18 @@ impl Writer<'_> { } /// A shape's or text box's `wps:wsp`, at `off` in its group (or 0, 0). In a group it carries - /// its own `wps:cNvPr` with drawing id `id`. - pub(super) fn wsp(&mut self, w: &mut W, o: &InlineObject, id: Option<&str>, off: (f32, f32), rels: &mut PartRels, depth: usize) { - let InlineObject::Shape { kind, w: sw, h: sh, fill, stroke, stroke_width, story, effects, float, freeform, extra } = o else { return }; + /// its own `wps:cNvPr` with drawing id `id`. Returns where in `w` its `w:txbxContent` was + /// written, for a VML fallback to repeat. + pub(super) fn wsp( + &mut self, + w: &mut W, + o: &InlineObject, + id: Option<&str>, + off: (f32, f32), + rels: &mut PartRels, + depth: usize, + ) -> Option> { + let InlineObject::Shape { kind, w: sw, h: sh, fill, stroke, stroke_width, story, effects, float, freeform, extra } = o else { return None }; let geom = freeform.as_deref().filter(|_| *kind == ShapeKind::Freeform); w.open("wps:wsp", &[]); if let Some(id) = id { @@ -181,6 +190,7 @@ impl Writer<'_> { // its place in it instead. let part = (*story).and_then(|id| self.doc.parts.get(&id).filter(|p| p.kind == craft_wordmodel::PartKind::TextBox).map(|p| (id, p))); let link = part.and_then(|(id, _)| self.txbx_links.get(&id).copied()); + let mut content = None; if let Some((n, seq)) = link.filter(|(_, seq)| *seq > 0) { w.empty("wps:linkedTxbx", &[("id", &n.to_string()), ("seq", &seq.to_string())]); } else if let Some((id, part)) = part @@ -190,6 +200,7 @@ impl Writer<'_> { Some((n, _)) => w.open("wps:txbx", &[("id", &n.to_string())]), None => w.open("wps:txbx", &[]), } + let start = w.s.len(); w.open("w:txbxContent", &[]); if self.written_boxes.insert(id) { let blocks = part.blocks.clone(); @@ -198,6 +209,7 @@ impl Writer<'_> { w.empty("w:p", &[]); } w.close("w:txbxContent"); + content = Some(start..w.s.len()); w.close("wps:txbx"); self.boxes.leave(); } @@ -231,6 +243,7 @@ impl Writer<'_> { w.close("wps:bodyPr"); } w.close("wps:wsp"); + content } /// A `w:drawing` holding `graphic` (an `a:graphic`) in a frame of the object's size and place. diff --git a/crates/docx/src/write/story/object.rs b/crates/docx/src/write/story/object.rs index 6cdd54f46..4b0ae856a 100644 --- a/crates/docx/src/write/story/object.rs +++ b/crates/docx/src/write/story/object.rs @@ -1,7 +1,7 @@ use super::drawing::{DrawingInfo, shape_name, spin_attrs, write_fill, xfrm_spin_only}; use super::field::{FieldData, field_run}; use super::section::comment_reference_style; -use super::{CharProps, Fill, InlineObject, NoteKind, PartRels, TextColor, W, Wrap, Writer, emu, n, rpr}; +use super::{Anchor, CharProps, Fill, Float, InlineObject, NoteKind, PartRels, ShapeKind, TextColor, W, Wrap, Writer, emu, n, rpr}; impl Writer<'_> { /// Write an inline object (`Bookmark`, `Comment`, `Equation`, `Field`, `Shape`, etc.). @@ -362,10 +362,24 @@ impl Writer<'_> { } fn write_shape(&mut self, w: &mut W, o: &InlineObject, props: &CharProps, rels: &mut PartRels, depth: usize) { - let InlineObject::Shape { kind, w: sw, h: sh, float, effects, freeform, extra, .. } = o else { return }; + let InlineObject::Shape { kind, w: sw, h: sh, float, effects, freeform, extra, story, .. } = o else { return }; self.rev_open(w, props); w.open("w:r", &[]); self.rpr(w, props); + // A Word 2010 shape (`wps`) with a VML form goes in `mc:AlternateContent`, as Word writes + // it: a reader that doesn't know `wps` (Word 2007, the Office Compatibility Pack) takes the + // VML fallback instead of refusing the file. A shape without one stays bare: guarded with + // no fallback, that reader would open the file without the shape and drop it on its next + // save, which is worse than refusing the file. + // VML currently has no chain IDs or mso-next-textbox links. Keep every member of a + // linked story bare, including its head: an unlinked fallback would silently lose + // overflow text in older readers. Unlinked boxes still get the compatibility fallback. + let linked = story.is_some_and(|id| self.txbx_links.contains_key(&id)); + let fallback = freeform.is_none() && vml_tag(*kind).is_some() && !linked; + if fallback { + w.open("mc:AlternateContent", &[]); + w.open("mc:Choice", &[("Requires", "wps")]); + } w.open("w:drawing", &[]); let docpr = self.shape_docpr(extra.id); let link = Self::drawing_link(props, rels); @@ -384,18 +398,164 @@ impl Writer<'_> { } let float = &float; self.drawing_open(w, DrawingInfo { float, size: (*sw, *sh), docpr: &docpr, name: &name, alt: o.alt_text(), link: link.as_deref() }); + // This shape's stacking order: floating objects in its text box move `self.z` on. + let z = self.z; w.empty("wp:cNvGraphicFramePr", &[]); w.open("a:graphic", &[]); w.open("a:graphicData", &[("uri", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape")]); - self.wsp(w, o, None, (0.0, 0.0), rels, depth); + let content = self.wsp(w, o, None, (0.0, 0.0), rels, depth); w.close("a:graphicData"); w.close("a:graphic"); w.close(if float.wrap == Wrap::Inline { "wp:inline" } else { "wp:anchor" }); w.close("w:drawing"); + if fallback { + w.close("mc:Choice"); + // The same text again: the fallback is a copy of the choice, not more of the story. + let content = content.and_then(|r| w.s.get(r)).map(str::to_owned); + w.open("mc:Fallback", &[]); + self.vml_shape(w, o, float, z, content.as_deref()); + w.close("mc:Fallback"); + w.close("mc:AlternateContent"); + } w.close("w:r"); self.rev_close(w, props); } + /// A shape as VML (`w:pict`), for readers older than Word 2010 shapes: its frame, place, + /// wrapping, fill, outline and text box. Only for a kind [`vml_tag`] names. + fn vml_shape(&self, w: &mut W, o: &InlineObject, float: &Float, z: u32, content: Option<&str>) { + let InlineObject::Shape { kind, w: sw, h: sh, fill, stroke, stroke_width, story, extra, .. } = o else { return }; + let Some(tag) = vml_tag(*kind) else { return }; + let pt = |v: f32| format!("{:.2}pt", craft_wordmodel::geom::finite(v)); + let mut style = Vec::new(); + if float.wrap != Wrap::Inline { + style.push("position:absolute".to_string()); + style.push(format!("margin-left:{}", pt(float.x))); + style.push(format!("margin-top:{}", pt(float.y))); + } + style.push(format!("width:{}", pt(sw.max(0.0)))); + style.push(format!("height:{}", pt(sh.max(0.0)))); + let spin = float.spin(); + if spin.deg != 0.0 { + style.push(format!("rotation:{}", spin.deg)); + } + match (spin.flip_h, spin.flip_v) { + (true, true) => style.push("flip:x y".into()), + (true, false) => style.push("flip:x".into()), + (false, true) => style.push("flip:y".into()), + (false, false) => {} + } + if float.wrap != Wrap::Inline { + // The stacking order the choice was given, as a signed 32-bit `z-index`. Behind the text + // is negative, shifted down rather than negated, so a higher `z` still stacks higher. + let z = i64::from(z.min(i32::MAX as u32)); + let z = if float.wrap == Wrap::BehindText { z - (i64::from(i32::MAX) + 1) } else { z }; + style.push(format!("z-index:{z}")); + // The same clearances from the text as the choice's `distL`/`distR`/`distT`/`distB`. + let d = pt(float.dist.clamp(0.0, 1584.0)); + style.push(format!("mso-wrap-distance-left:{d}")); + style.push(format!("mso-wrap-distance-right:{d}")); + style.push(format!("mso-wrap-distance-top:{}", pt(float.dist_top.clamp(0.0, 1584.0)))); + style.push(format!("mso-wrap-distance-bottom:{}", pt(float.dist_bottom.clamp(0.0, 1584.0)))); + if let Some(a) = float.h_align { + style.push(format!("mso-position-horizontal:{}", a.ooxml(true))); + } + style.push(format!("mso-position-horizontal-relative:{}", vml_rel(float.h_rel, true))); + if let Some(a) = float.v_align { + style.push(format!("mso-position-vertical:{}", a.ooxml(false))); + } + style.push(format!("mso-position-vertical-relative:{}", vml_rel(float.v_rel, false))); + } + let body = (*story).and_then(|id| self.doc.parts.get(&id)).map(|p| p.body).unwrap_or_default(); + style.push( + match body.anchor { + craft_wordmodel::props::VAlign::Top => "v-text-anchor:top", + craft_wordmodel::props::VAlign::Center => "v-text-anchor:middle", + craft_wordmodel::props::VAlign::Bottom => "v-text-anchor:bottom", + } + .into(), + ); + if float.hidden { + style.push("visibility:hidden".into()); + } + let style = style.join(";"); + let mut attrs: Vec<(&str, String)> = vec![("style", style)]; + if *kind == ShapeKind::RoundedRectangle { + // The same fraction of the shorter side: VML's in 1/65536ths, DrawingML's in 1/100000ths. + let c = match extra.corner { + Some(c) => c.min(craft_wordmodel::connector::MAX_CORNER) as f32 / 100_000.0, + None => craft_wordmodel::connector::DEFAULT_CORNER, + }; + attrs.push(("arcsize", format!("{}f", (c * 65_536.0).round() as u32))); + } + if float.wrap != Wrap::Inline { + attrs.push(("o:allowincell", (if float.outside_cell { "f" } else { "t" }).into())); + } + let opacity = match fill.as_ref() { + Some(f) => { + attrs.push(("fillcolor", format!("#{}", f.main_color().hex()))); + match f { + Fill::Solid { transparency, .. } if *transparency > 0.0 => Some(format!("{:.4}", (1.0 - transparency / 100.0).clamp(0.0, 1.0))), + _ => None, + } + } + None => { + attrs.push(("filled", "f".into())); + None + } + }; + match stroke { + Some(c) => { + attrs.push(("strokecolor", format!("#{}", c.hex()))); + attrs.push(("strokeweight", pt(stroke_width.clamp(0.0, 100.0)))); + } + None => attrs.push(("stroked", "f".into())), + } + let attrs: Vec<(&str, &str)> = attrs.iter().map(|(k, v)| (*k, v.as_str())).collect(); + w.open("w:pict", &[]); + w.open(tag, &attrs); + if let Some(opacity) = &opacity { + w.empty("v:fill", &[("opacity", opacity)]); + } + if let Some(content) = content { + let inset = body.inset.map(|i| i.map(|v| pt(v.min(craft_wordmodel::MAX_TEXT_BOX_INSET) as f32 / 12_700.0)).join(",")); + let mut tb: Vec<(&str, &str)> = Vec::new(); + if let Some(inset) = &inset { + tb.push(("inset", inset)); + } + // Text direction as the VML reader takes it (stacked letters have no VML form). + let mut tb_style = match body.vert { + craft_wordmodel::props::TextVert::Vert => vec!["layout-flow:vertical"], + craft_wordmodel::props::TextVert::Vert270 => vec!["layout-flow:vertical", "mso-layout-flow-alt:bottom-to-top"], + craft_wordmodel::props::TextVert::Horz | craft_wordmodel::props::TextVert::Stacked => Vec::new(), + }; + if body.auto_fit { + tb_style.push("mso-fit-shape-to-text:t"); + } + let tb_style = tb_style.join(";"); + if !tb_style.is_empty() { + tb.push(("style", &tb_style)); + } + w.open("v:textbox", &tb); + w.raw(content); + w.close("v:textbox"); + } + let wrap = match float.wrap { + Wrap::Square => Some("square"), + Wrap::Tight => Some("tight"), + Wrap::Through => Some("through"), + Wrap::TopAndBottom => Some("topAndBottom"), + Wrap::Inline | Wrap::BehindText | Wrap::InFrontOfText => None, + }; + if let Some(wrap) = wrap { + let side = float.wrap_text.ooxml(); + let attrs: &[(&str, &str)] = if float.wrap_text.is_both() { &[("type", wrap)] } else { &[("type", wrap), ("side", side)] }; + w.empty("w10:wrap", attrs); + } + w.close(tag); + w.close("w:pict"); + } + fn write_group(&mut self, w: &mut W, o: &InlineObject, props: &CharProps, rels: &mut PartRels, depth: usize) { let InlineObject::Group { w: gw, h: gh, float, ch_w, ch_h, children, canvas } = o else { return }; self.rev_open(w, props); @@ -487,6 +647,33 @@ impl Writer<'_> { } } +/// The VML element that draws a shape kind, for the kinds VML has one for. +fn vml_tag(kind: ShapeKind) -> Option<&'static str> { + match kind { + ShapeKind::Rectangle | ShapeKind::TextBox => Some("v:rect"), + ShapeKind::RoundedRectangle => Some("v:roundrect"), + ShapeKind::Ellipse => Some("v:oval"), + _ => None, + } +} + +/// A floating object's anchor as VML names it (`mso-position-horizontal-relative`, `-vertical-`). +fn vml_rel(a: Anchor, horiz: bool) -> &'static str { + match (a, horiz) { + (Anchor::Margin, _) => "margin", + (Anchor::Page, _) => "page", + (Anchor::InsideMargin, _) => "inner-margin-area", + (Anchor::OutsideMargin, _) => "outer-margin-area", + (Anchor::LeftMargin, true) => "left-margin-area", + (Anchor::RightMargin, true) => "right-margin-area", + (Anchor::Character, true) => "char", + (Anchor::TopMargin, false) => "top-margin-area", + (Anchor::BottomMargin, false) => "bottom-margin-area", + (Anchor::Line, false) => "line", + _ => "text", + } +} + pub(super) fn ink_name(tool: craft_wordmodel::freeform::InkTool) -> &'static str { match tool { craft_wordmodel::freeform::InkTool::Pen => "Pen", diff --git a/crates/docx/tests/it/main.rs b/crates/docx/tests/it/main.rs index b0bab5833..d1def3f8c 100644 --- a/crates/docx/tests/it/main.rs +++ b/crates/docx/tests/it/main.rs @@ -49,3 +49,4 @@ mod undefined_style; mod vml_shapes; mod word_xml; mod wordml2003_write; +mod wps_fallback; diff --git a/crates/docx/tests/it/roundtrip.rs b/crates/docx/tests/it/roundtrip.rs index 8f3bbdd0b..65a01e6b4 100644 --- a/crates/docx/tests/it/roundtrip.rs +++ b/crates/docx/tests/it/roundtrip.rs @@ -956,7 +956,8 @@ fn toc_field_skips_nested_stories() { let mut xml = String::new(); std::io::Read::read_to_string(&mut zip.by_name("word/document.xml").unwrap(), &mut xml).unwrap(); let boxes: Vec<(usize, usize)> = xml.match_indices("").map(|(a, _)| (a, a + xml[a..].find("").unwrap())).collect(); - assert_eq!(boxes.len(), 2, "{xml}"); + // Each box twice: its shape and its VML fallback. + assert_eq!(boxes.len(), 4, "{xml}"); let in_box = |i: usize| boxes.iter().any(|(a, b)| (*a..*b).contains(&i)); for kind in ["begin", "separate", "end"] { let at: Vec = xml.match_indices(&format!(r#"fldCharType="{kind}""#)).map(|(i, _)| i).collect(); @@ -964,7 +965,7 @@ fn toc_field_skips_nested_stories() { assert!(!in_box(at[0]), "field {kind} written inside a text box: {xml}"); } let (begin, end) = (xml.find(r#"fldCharType="begin""#).unwrap(), xml.find(r#"fldCharType="end""#).unwrap()); - assert!(begin > boxes[0].1 && end > boxes[1].1 && end < xml.find("").unwrap(), "{xml}"); + assert!(begin > boxes[1].1 && end > boxes[3].1 && end < xml.find("").unwrap(), "{xml}"); } #[test] diff --git a/crates/docx/tests/it/text_boxes.rs b/crates/docx/tests/it/text_boxes.rs index 608d22d33..a5824cafc 100644 --- a/crates/docx/tests/it/text_boxes.rs +++ b/crates/docx/tests/it/text_boxes.rs @@ -257,5 +257,6 @@ fn story_shared_by_many_shapes_is_written_once() { } let xml = document_xml(&craft_docx::write(&d).unwrap()); assert!(xml.len() < 2_000_000, "document.xml is {} bytes", xml.len()); - assert_eq!(xml.matches(text.as_str()).count(), 1); + // Once in the first shape and once in its VML fallback; the other shapes don't repeat it. + assert_eq!(xml.matches(text.as_str()).count(), 2); } diff --git a/crates/docx/tests/it/wps_fallback.rs b/crates/docx/tests/it/wps_fallback.rs new file mode 100644 index 000000000..9b209082b --- /dev/null +++ b/crates/docx/tests/it/wps_fallback.rs @@ -0,0 +1,188 @@ +//! Word 2010 shapes (`wps:wsp`) are written inside `mc:AlternateContent` with a VML fallback +//! (ECMA-376 Part 3, Markup Compatibility; Part 4 §14.1, VML), so a reader that doesn't know +//! `wps` — Word 2007, or Word 2000-2003 with the Office Compatibility Pack — takes the fallback +//! instead of refusing the whole file. A shape with no VML form is left bare. + +use std::io::Read; + +use craft_wordmodel::para::{Anchor, FloatAlign, ShapeKind, Wrap}; +use craft_wordmodel::props::TextVert; +use craft_wordmodel::{Document, InlineObject, PartKind, TextBody, para_block}; + +fn document_xml(bytes: &[u8]) -> String { + let mut z = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut s = String::new(); + z.by_name("word/document.xml").unwrap().read_to_string(&mut s).unwrap(); + s +} + +fn shape(kind: ShapeKind, story: Option) -> InlineObject { + let float = craft_wordmodel::para::Float { + wrap: Wrap::Square, + h_rel: Anchor::Margin, + h_align: Some(FloatAlign::End), + v_rel: Anchor::Paragraph, + y: 0.05, + ..Default::default() + }; + InlineObject::Shape { + kind, + w: 10.7, + h: 12.2, + fill: None, + stroke: None, + stroke_width: 0.75, + float, + story, + effects: Default::default(), + freeform: None, + extra: Default::default(), + } +} + +/// A page number in a floating text box, as in a footer: the shape that made the +/// Compatibility Pack refuse the file. +#[test] +fn a_text_box_gets_a_vml_fallback_with_its_text() { + let mut d = Document::from_text("a"); + let id = d.add_part(PartKind::TextBox, vec![para_block(craft_wordmodel::Paragraph::with_text("29", Default::default()))]); + d.parts.get_mut(&id).unwrap().body = TextBody { auto_fit: true, inset: Some([0; 4]), ..Default::default() }; + let mut o = shape(ShapeKind::TextBox, Some(id)); + if let InlineObject::Shape { float, .. } = &mut o { + (float.dist, float.dist_top, float.dist_bottom) = (18.0, 12.0, 6.0); + } + d.insert_object(&craft_wordmodel::Pos::body(0, 1), o, &Default::default()).unwrap(); + let bytes = craft_docx::write(&d).unwrap(); + let xml = document_xml(&bytes); + assert!(xml.contains(r#""#), "{xml}"); + assert!(!xml.contains(""), "no bare Word 2010 shape: {xml}"); + let fallback = &xml[xml.find("").expect("a fallback")..]; + assert!(fallback.starts_with(""#, + r#""#, + ] { + assert!(fallback.contains(want), "{want} in {fallback}"); + } + assert_eq!(xml.matches(">29").count(), 2, "the text in the choice and again in the fallback"); + + // Read back: the choice, once. + let r = craft_docx::read(&bytes).unwrap(); + let mut boxes = Vec::new(); + r.objects_in_reading_order(&mut |o| boxes.extend(o.text_boxes())); + boxes.retain(|id| r.parts.get(id).is_some_and(|p| p.kind == PartKind::TextBox)); + assert_eq!(boxes.len(), 1, "one text box, not the fallback as well"); + assert_eq!(r.plain_text(craft_wordmodel::StoryRef::Part(boxes[0])), "29"); +} + +/// The fallback keeps the box's own stacking order (not that of a floating object inside it), +/// its hidden flag and its text direction. +#[test] +fn the_fallback_keeps_order_visibility_and_direction() { + let mut d = Document::from_text("a"); + let inner = d.add_part(PartKind::TextBox, vec![para_block(craft_wordmodel::Paragraph::with_text("inner", Default::default()))]); + let mut nested = shape(ShapeKind::TextBox, Some(inner)); + if let InlineObject::Shape { float, .. } = &mut nested { + float.z = 100; + } + let mut p = craft_wordmodel::Paragraph::with_text("outer", Default::default()); + p.insert_object(0, nested, &Default::default()).unwrap(); + let outer = d.add_part(PartKind::TextBox, vec![para_block(p)]); + d.parts.get_mut(&outer).unwrap().body = TextBody { vert: TextVert::Vert270, ..Default::default() }; + let mut o = shape(ShapeKind::TextBox, Some(outer)); + if let InlineObject::Shape { float, .. } = &mut o { + float.z = 10; + float.hidden = true; + } + d.insert_object(&craft_wordmodel::Pos::body(0, 1), o, &Default::default()).unwrap(); + let xml = document_xml(&craft_docx::write(&d).unwrap()); + // The outer box's VML: the `v:rect` holding the vertical text box (the inner box's fallback + // is copied into the outer one's text, so it comes after it). + let tb = xml.find(r#""#).expect("the outer box's text, turned"); + let rect = &xml[xml[..tb].rfind(" = xml + .match_indices("z-index:") + .map(|(i, m)| { + let v = &xml[i + m.len()..]; + v[..v.find([';', '"']).unwrap()].parse().unwrap() + }) + .collect(); + got.sort_unstable(); + let low = -(1i64 << 31); + assert_eq!(got, vec![low + 10, low + 20, -1], "{xml}"); +} + +/// A shape VML has no simple form for stays a bare `wps:wsp`: guarded without a fallback, an old +/// reader would open the file without it and drop it on its next save. +#[test] +fn a_shape_without_a_vml_form_is_not_guarded() { + let mut d = Document::from_text("a"); + d.insert_object(&craft_wordmodel::Pos::body(0, 1), shape(ShapeKind::Star, None), &Default::default()).unwrap(); + let bytes = craft_docx::write(&d).unwrap(); + let xml = document_xml(&bytes); + assert!(xml.contains("") && xml.contains(r#""#), "{xml}"); + assert!(!xml.contains(""), "{xml}"); + let r = craft_docx::read(&bytes).unwrap(); + let mut kinds = Vec::new(); + r.objects_in_reading_order(&mut |o| { + if let InlineObject::Shape { kind, .. } = o { + kinds.push(*kind); + } + }); + assert_eq!(kinds, vec![ShapeKind::Star]); +} + +/// A partial fallback must not turn a linked story into independent boxes in older readers. +#[test] +fn linked_boxes_stay_bare_while_unlinked_boxes_keep_their_fallback() { + let mut d = Document::from_text("a\nb\nc\nd"); + let mut ids = Vec::new(); + for (i, text) in ["Flows on", "", "", "Independent"].into_iter().enumerate() { + let id = d.add_part(PartKind::TextBox, vec![para_block(craft_wordmodel::Paragraph::with_text(text, Default::default()))]); + ids.push(id); + d.insert_object(&craft_wordmodel::Pos::body(i, 1), shape(ShapeKind::TextBox, Some(id)), &Default::default()).unwrap(); + } + d.parts.get_mut(&ids[0]).unwrap().body.next = Some(ids[1]); + d.parts.get_mut(&ids[1]).unwrap().body.next = Some(ids[2]); + let bytes = craft_docx::write(&d).unwrap(); + let xml = document_xml(&bytes); + assert_eq!(xml.matches("").count(), 1, "{xml}"); + assert_eq!(xml.matches("").count(), 1, "{xml}"); + let fallback = xml.split("").nth(1).unwrap().split("").next().unwrap(); + assert!(fallback.contains(">Independent"), "{fallback}"); + assert!(!fallback.contains("Flows on"), "the chain head must not get an unlinked fallback"); + assert_eq!(xml.matches(">Flows on").count(), 1); + assert!(xml.contains(r#""#), "{xml}"); + assert!(xml.contains(r#""#), "{xml}"); + assert!(xml.contains(r#""#), "{xml}"); + let read = craft_docx::read(&bytes).unwrap(); + let chains = read.text_box_chains(); + assert_eq!(chains.len(), 1); + assert_eq!(chains[0].len(), 3); + assert_eq!(read.plain_text(craft_wordmodel::StoryRef::Part(chains[0][0])), "Flows on"); +} diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs index 6554a1eb8..8dec304ec 100644 --- a/crates/dpx/src/rgba8_dpx10.rs +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -9,6 +9,14 @@ pub enum Rgba8EncodeError { /// The output buffer could not be allocated. OutOfMemory, } +/// Reserves the DPX output up front; a failed reservation becomes [`Rgba8EncodeError::OutOfMemory`]. +fn output_buffer(size: usize) -> std::result::Result, Rgba8EncodeError> { + let mut buf = Vec::new(); + match buf.try_reserve_exact(size) { + Ok(()) => Ok(buf), + Err(_) => Err(Rgba8EncodeError::OutOfMemory), + } +} fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; if w == 0 || h == 0 || rgba.len() != n { @@ -86,8 +94,7 @@ pub fn encode_rgba8_dpx10(rgba: &[u8], w: u32, h: u32, creator: &str) -> std::re hd[1928] = 0; // The caller already holds the frame; reserve the output fallibly so a large export reports // an error instead of aborting the process. - let mut out = Vec::new(); - out.try_reserve_exact(size).map_err(|_| Rgba8EncodeError::OutOfMemory)?; + let mut out = output_buffer(size)?; out.extend_from_slice(&hd); let ten = |v: u8| (u32::from(v) << 2) | (u32::from(v) >> 6); for &[r, g, b, _] in rgba.as_chunks::<4>().0 { @@ -107,4 +114,9 @@ mod tests { assert_eq!(&bytes[2048..], &((1023u32 << 22) | (514u32 << 2)).to_be_bytes()); assert_eq!(encode_rgba8_dpx10(&[], 0, 1, ""), Err(Rgba8EncodeError::FrameSize)); } + #[test] + fn output_reservation_failure_maps_to_out_of_memory() { + assert!(matches!(output_buffer(usize::MAX), Err(Rgba8EncodeError::OutOfMemory))); + assert_eq!(output_buffer(DPX_HEADER).map(|b| b.capacity() >= DPX_HEADER), Ok(true)); + } } diff --git a/crates/drawingml/src/chart.rs b/crates/drawingml/src/chart.rs index e575d10f2..9475d2054 100644 --- a/crates/drawingml/src/chart.rs +++ b/crates/drawingml/src/chart.rs @@ -354,6 +354,10 @@ pub struct Chart { pub legend: Option, /// `c:legend/c:overlay`: the legend is drawn over the plot instead of shrinking it. pub legend_overlay: bool, + /// `c:legend/c:legendEntry`: entries whose text has its own size, weight or colour, over + /// the legend's. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub legend_entries: Vec, /// 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")] @@ -431,6 +435,21 @@ pub struct Chart { pub kept: ChartKept, } +/// One legend entry's own text (`c:legendEntry/c:txPr`): what it sets wins over the legend's. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct LegendEntry { + /// The entry's place in the legend, from 0 (`c:idx`). + pub idx: u32, + /// Text size in points. + #[serde(skip_serializing_if = "Option::is_none")] + pub size: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub bold: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub color: Option, +} + /// One relationship of a chart part, with the target string as it was written. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -483,6 +502,7 @@ impl Default for Chart { series: vec![], legend: Some("b".into()), legend_overlay: false, + legend_entries: vec![], text_size: None, title_size: None, data_labels: false, diff --git a/crates/drawingml/src/chart/read.rs b/crates/drawingml/src/chart/read.rs index 38ce9f605..3f2be736f 100644 --- a/crates/drawingml/src/chart/read.rs +++ b/crates/drawingml/src/chart/read.rs @@ -143,6 +143,7 @@ fn chart_within(root: &El, mut budget: usize) -> Option { c.legend = ch.child(Local("legend")).map(|l| l.child(Local("legendPos")).and_then(|c| c.attr_local("val")).unwrap_or("r").to_string()); c.legend_overlay = ch.child(Local("legend")).and_then(|l| l.child(Local("overlay"))).and_then(|o| o.attr_local("val").and_then(value::to_bool)).unwrap_or(false); + c.legend_entries = legend_entries(ch.child(Local("legend"))); c.chart_fill = simple_fill(root.child(Local("spPr"))); c.plot_fill = simple_fill(pa.child(Local("spPr"))); @@ -552,6 +553,52 @@ fn text_color(e: Option<&El>) -> Option { rec(e?, 0) } +/// Legend entries read from one chart; a legend lists at most one per series or category. +const MAX_LEGEND_ENTRIES: usize = 1024; + +/// The legend's entries with text of their own (`c:legendEntry/c:txPr`), over the legend's. +fn legend_entries(legend: Option<&El>) -> Vec { + legend + .into_iter() + .flat_map(|l| l.children(Local("legendEntry"))) + .take(MAX_LEGEND_ENTRIES) + .filter_map(|e| { + // The paragraph's own run properties first, then the list style's defaults + // (`a:lstStyle` comes before `a:p`, and is the less specific of the two). + let tx = e.child(Local("txPr")); + let para = tx.and_then(|t| t.child(Local("p"))); + let entry = crate::chart::LegendEntry { + idx: e.child(Local("idx")).and_then(|i| i.attr_local("val")).and_then(|v| v.parse().ok())?, + size: text_size(para).or_else(|| text_size(tx)), + bold: text_bold(para).or_else(|| text_bold(tx)), + color: text_color(para).or_else(|| text_color(tx)), + }; + (entry.size.is_some() || entry.bold.is_some() || entry.color.is_some()).then_some(entry) + }) + .collect() +} + +/// The weight (`b`) of the first `a:defRPr` or `a:rPr` that sets one under `e`. +fn text_bold(e: Option<&El>) -> Option { + fn rec(e: &El, depth: usize) -> Option { + if depth > 8 { + return None; + } + for c in e.elements() { + if (c.is(Local("defRPr")) || c.is(Local("rPr"))) + && let Some(b) = c.attr_local("b").and_then(value::to_bool) + { + return Some(b); + } + if let Some(v) = rec(c, depth + 1) { + return Some(v); + } + } + None + } + rec(e?, 0) +} + /// The size in points of the first `a:defRPr` or `a:rPr` with one under `e` (`sz` in hundredths). fn text_size(e: Option<&El>) -> Option { fn rec(e: &El, depth: usize) -> Option { diff --git a/crates/drawingml/src/chart/write.rs b/crates/drawingml/src/chart/write.rs index b91b262b4..ca1f61acd 100644 --- a/crates/drawingml/src/chart/write.rs +++ b/crates/drawingml/src/chart/write.rs @@ -101,6 +101,38 @@ fn tx_pr(w: &mut Writer, c: &Chart) { w.close(); } +/// `c:legendEntry` with the entry's own text size, weight and colour. +fn legend_entry(w: &mut Writer, e: &crate::chart::LegendEntry) { + w.open("c:legendEntry", &[]); + w.tag("c:idx").attr("val", e.idx).empty(); + w.open("c:txPr", &[]); + w.empty("a:bodyPr", &[]); + w.empty("a:lstStyle", &[]); + w.open("a:p", &[]); + w.open("a:pPr", &[]); + let mut run = w.tag("a:defRPr"); + if let Some(sz) = e.size.filter(|v| v.is_finite() && *v > 0.0) { + // 1 to 400 pt, as the reader takes it, so a size reads back as written. + run = run.attr("sz", (sz * 100.0).round().clamp(100.0, 40_000.0) as u32); + } + if let Some(b) = e.bold { + run = run.attr("b", u8::from(b)); + } + match &e.color { + Some(c) => { + run.open(); + dml::solid(w, c); + w.close(); + } + None => run.empty(), + } + w.close(); + w.tag("a:endParaRPr").attr("lang", "en-US").empty(); + w.close(); + w.close(); + w.close(); +} + fn rich(w: &mut Writer, text: &str, color: Option<&crate::style::ColorRef>) { w.open("c:tx", &[]); w.open("c:rich", &[]); @@ -781,6 +813,9 @@ pub fn chart_xml(c: &Chart) -> (Vec, bool) { }; w.open("c:legend", &[]); w.tag("c:legendPos").attr("val", p).empty(); + for e in &c.legend_entries { + legend_entry(&mut w, e); + } w.tag("c:overlay").attr("val", u8::from(c.legend_overlay)).empty(); tx_pr(&mut w, c); w.close(); @@ -1058,6 +1093,72 @@ mod tests { use crate::color::{ColorTransform, Rgba}; use crate::style::ColorRef; + /// A legend entry's own text (`c:legendEntry/c:txPr`: size, bold, colour) is read, and a + /// rebuilt part writes it back, in schema order before `c:overlay` (DeckCraft #2067). + #[test] + fn legend_entry_text_is_read_and_survives_an_edit() { + let read = |xml: &[u8]| crate::chart::read::read_chart_xml(xml).unwrap(); + let entry = |idx: u32, rgb: &str| { + format!( + r#""# + ) + }; + let xml = format!( + r#"1{}{}"#, + entry(0, "C00000"), + entry(1, "00B050") + ); + let c = read(xml.as_bytes()); + let want = |idx: u32, rgb: (u8, u8, u8)| crate::chart::LegendEntry { + idx, + size: Some(20.0), + bold: Some(true), + color: Some(ColorRef::rgb(Rgba::rgb(rgb.0, rgb.1, rgb.2))), + }; + // The third entry sets nothing of its own and is not kept. + assert_eq!(c.legend_entries, vec![want(0, (0xC0, 0, 0)), want(1, (0, 0xB0, 0x50))]); + assert_eq!(c.text_size, Some(12.0), "the legend's own size stays the legend's"); + let mut e = c.clone(); + e.title = Some("Units".into()); + e.raw = None; + let (out, kept) = super::chart_xml(&e); + assert!(!kept); + let s = String::from_utf8(out.clone()).unwrap(); + let legend = &s[s.find("").unwrap()..]; + let (pos, entry0, overlay) = + (legend.find("").unwrap(), legend.find(""#, + r#""#, + 1, + ); + assert_ne!(listed, xml, "the list style was put in"); + let c = read(listed.as_bytes()); + assert_eq!(c.legend_entries[0], want(0, (0xC0, 0, 0)), "the paragraph's 20 pt, bold, C00000"); + // The list style alone still counts. + let only_list = + listed.replacen(r#""#, "", 1); + let entry = &read(only_list.as_bytes()).legend_entries[0]; + assert_eq!((entry.size, entry.bold), (Some(12.0), Some(false))); + // A size reads back as it was written, within the reader's 1-400 pt. + for (size, back) in [(20.5, 20.5), (400.0, 400.0), (500.0, 400.0)] { + let mut e = c.clone(); + e.raw = None; + e.legend_entries = vec![crate::chart::LegendEntry { idx: 0, size: Some(size), bold: Some(false), color: None }]; + let written = super::chart_xml(&e).0; + let sz = format!(r#"sz="{}""#, (back * 100.0) as u32); + assert!(String::from_utf8_lossy(&written).contains(&sz), "{size} pt is written as {sz}"); + let back_entry = &read(&written).legend_entries[0]; + assert_eq!((back_entry.size, back_entry.bold), (Some(back), Some(false)), "{size} pt"); + } + } + #[test] fn export_cost_counts_the_caches_each_group_writes() { let s = Series { values: vec![Some(1.0); 600], ..Default::default() }; diff --git a/crates/drawingml/src/numfmt.rs b/crates/drawingml/src/numfmt.rs index b5db9667c..7a7b86665 100644 --- a/crates/drawingml/src/numfmt.rs +++ b/crates/drawingml/src/numfmt.rs @@ -234,16 +234,18 @@ fn sci(toks: &[Tok], v: f64, sep: char, dec: char) -> Option { 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); + // Round after forming the engineering mantissa: 999.94 as `##0.0` stays 999.9. + // Carry only when rounding exceeds all of the format's integer placeholders. + let carry = 10f64.powi(step); + if format!("{m:.mant_frac$}").parse::().is_ok_and(|rounded| rounded >= carry) { + m /= carry; + exp += step; + } let mantissa = fixed(mant, mfirst, mlast, m, sep, dec); let sign = if exp < 0 { "-" @@ -404,6 +406,21 @@ mod tests { assert_eq!(format("##0.0E+0", 999.96, None), "1.0E+3"); } + #[test] + fn engineering_formats_round_the_full_mantissa() { + for (code, value, expected) in [ + ("##0.0E+00", 999.94, "999.9E+00"), + ("##0.0E+00", 0.99994, "999.9E-03"), + ("##0.0E+00", -999.94, "-999.9E+00"), + ("#0.0E+00", 99.94, "99.9E+00"), + ("##0.0E+00", 999.96, "1.0E+03"), + ("#0.0E+00", 99.96, "1.0E+02"), + ] { + assert_eq!(format(code, value, None), expected, "{code}, {value}"); + } + assert_eq!(format("##0.0E+00", 999.94, Some("de-DE")), "999,9E+00"); + } + #[test] fn never_panics() { for code in ["", ";", ";;;", "\"", "[", "\\", "0.", ".", "#,", "%", "_", "*", "@", "0E+00", "[$", "\"unterminated"] { diff --git a/crates/elf/Cargo.toml b/crates/elf/Cargo.toml new file mode 100644 index 000000000..e982689e4 --- /dev/null +++ b/crates/elf/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "craft-elf" +description = "ELF program headers and dynamic section entries from hostile bytes, from Craft Anything: 32/64-bit, either byte order, with bounded counts and overflow-checked offsets, plus segment, permission and dynamic tag names. Pure Rust, no I/O." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +thiserror = { workspace = true } +# `serde`: `Serialize` on `ProgramHeader` (addresses as `0x…` strings), for apps that report +# segments as JSON. +serde = { workspace = true, optional = true } + +[features] +serde = ["dep:serde"] + +[dev-dependencies] +serde_json = { workspace = true } diff --git a/crates/elf/src/bytes.rs b/crates/elf/src/bytes.rs new file mode 100644 index 000000000..c78b9b5a8 --- /dev/null +++ b/crates/elf/src/bytes.rs @@ -0,0 +1,37 @@ +//! Bounds-checked reads of fixed-width integers in both byte orders: out of range is `None`. + +#[derive(Debug, Clone, Copy)] +pub(crate) struct Reader<'a> { + buf: &'a [u8], +} + +macro_rules! int { + ($le:ident, $be:ident, $ty:ty) => { + pub(crate) fn $le(&self, at: usize) -> Option<$ty> { + self.take(at).map(<$ty>::from_le_bytes) + } + + pub(crate) fn $be(&self, at: usize) -> Option<$ty> { + self.take(at).map(<$ty>::from_be_bytes) + } + }; +} + +impl<'a> Reader<'a> { + pub(crate) fn new(buf: &'a [u8]) -> Self { + Self { buf } + } + + pub(crate) fn u8(&self, at: usize) -> Option { + self.buf.get(at).copied() + } + + fn take(&self, at: usize) -> Option<[u8; N]> { + let end = at.checked_add(N)?; + self.buf.get(at..end)?.try_into().ok() + } + + int!(u16_le, u16_be, u16); + int!(u32_le, u32_be, u32); + int!(u64_le, u64_be, u64); +} diff --git a/crates/elf/src/hex.rs b/crates/elf/src/hex.rs new file mode 100644 index 000000000..82dfd4bba --- /dev/null +++ b/crates/elf/src/hex.rs @@ -0,0 +1,5 @@ +//! Addresses serialised as `0x…` strings, as Craft Anything reports them. + +pub(crate) fn serialize(value: &u64, serializer: S) -> Result { + serializer.serialize_str(&format!("{value:#x}")) +} diff --git a/crates/elf/src/lib.rs b/crates/elf/src/lib.rs new file mode 100644 index 000000000..b8868f734 --- /dev/null +++ b/crates/elf/src/lib.rs @@ -0,0 +1,244 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! ELF program headers and dynamic section entries from untrusted bytes, 32- or 64-bit in either +//! byte order: [`program_headers`], [`dynamic_entries`], [`va_to_off`], and the segment type, +//! permission and dynamic tag names. +//! +//! From Craft Anything (`crates/formats/src/elf.rs`), where the mitigations, dynamic symbols, +//! symbol versions and core-file notes built on these stay. With the `serde` feature, +//! [`ProgramHeader`] is `Serialize` with `vaddr` as a `0x…` string. + +mod bytes; +#[cfg(feature = "serde")] +mod hex; + +use crate::bytes::Reader; + +/// Program headers read from one table. +pub const MAX_PHDRS: usize = 4096; + +/// Dynamic entries read from one segment. +pub const MAX_DYN: usize = 65_536; + +/// Why a read gave up. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum ElfError { + /// The ELF header ends before the file does. + #[error("the file ends early ({0})")] + Truncated(&'static str), + /// No ELF magic, or an invalid class, encoding or field. + #[error("the file is malformed ({0})")] + Malformed(String), +} + +/// Endian/class-aware reads over an ELF image. +#[derive(Clone, Copy)] +struct E<'a> { + r: Reader<'a>, + is64: bool, + le: bool, +} + +impl<'a> E<'a> { + fn new(data: &'a [u8]) -> Result { + if !data.starts_with(b"\x7fELF") { + return Err(ElfError::Malformed("no ELF magic".into())); + } + let r = Reader::new(data); + let class = r.u8(4).ok_or(ElfError::Truncated("ELF ident"))?; + let enc = r.u8(5).ok_or(ElfError::Truncated("ELF ident"))?; + if !matches!(class, 1 | 2) || !matches!(enc, 1 | 2) { + return Err(ElfError::Malformed("bad ELF class or data encoding".into())); + } + Ok(E { r, is64: class == 2, le: enc == 1 }) + } + + fn u16(&self, off: usize) -> Option { + if self.le { self.r.u16_le(off) } else { self.r.u16_be(off) } + } + + fn u32(&self, off: usize) -> Option { + if self.le { self.r.u32_le(off) } else { self.r.u32_be(off) } + } + + fn u64(&self, off: usize) -> Option { + if self.le { self.r.u64_le(off) } else { self.r.u64_be(off) } + } + + fn word(&self, off: usize) -> Option { + if self.is64 { self.u64(off) } else { self.u32(off).map(u64::from) } + } + + fn word_size(&self) -> usize { + if self.is64 { 8 } else { 4 } + } +} + +/// One program header. +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +pub struct ProgramHeader { + /// [`segment_type_name`] of `p_type`. + pub kind: String, + /// [`permissions`] of `p_flags`. + pub flags: String, + pub offset: u64, + #[cfg_attr(feature = "serde", serde(serialize_with = "hex::serialize"))] + pub vaddr: u64, + pub file_size: u64, + pub mem_size: u64, + pub align: u64, + /// The raw segment type. + #[cfg_attr(feature = "serde", serde(skip))] + pub p_type: u32, + /// The raw `PF_R`/`PF_W`/`PF_X` flags. + #[cfg_attr(feature = "serde", serde(skip))] + pub p_flags: u32, +} + +/// A segment type's `PT_` name without the prefix (`LOAD`, `GNU_RELRO`…), or its number in hex. +pub fn segment_type_name(t: u32) -> String { + match t { + 0 => "NULL".into(), + 1 => "LOAD".into(), + 2 => "DYNAMIC".into(), + 3 => "INTERP".into(), + 4 => "NOTE".into(), + 5 => "SHLIB".into(), + 6 => "PHDR".into(), + 7 => "TLS".into(), + 0x6474_e550 => "GNU_EH_FRAME".into(), + 0x6474_e551 => "GNU_STACK".into(), + 0x6474_e552 => "GNU_RELRO".into(), + 0x6474_e553 => "GNU_PROPERTY".into(), + 0x7000_0001 => "ARM_EXIDX".into(), + other => format!("{other:#x}"), + } +} + +/// `p_flags` as `rwx`, with `-` for each permission missing. +pub fn permissions(f: u32) -> String { + format!("{}{}{}", if f & 4 != 0 { 'r' } else { '-' }, if f & 2 != 0 { 'w' } else { '-' }, if f & 1 != 0 { 'x' } else { '-' }) +} + +/// The program headers of an ELF file, at most [`MAX_PHDRS`]. A table that runs past the file +/// ends at the last complete entry. +pub fn program_headers(data: &[u8]) -> Result, ElfError> { + let e = E::new(data)?; + let (phoff, phentsize, phnum) = if e.is64 { (e.u64(32), e.u16(54), e.u16(56)) } else { (e.u32(28).map(u64::from), e.u16(42), e.u16(44)) }; + let phoff = usize::try_from(phoff.ok_or(ElfError::Truncated("ELF header"))?).map_err(|_| ElfError::Malformed("phoff".into()))?; + let phentsize = usize::from(phentsize.unwrap_or(0)); + let phnum = usize::from(phnum.unwrap_or(0)).min(MAX_PHDRS); + let min = if e.is64 { 56 } else { 32 }; + if phnum > 0 && phentsize < min { + return Err(ElfError::Malformed("program header entry too small".into())); + } + let mut out = Vec::new(); + for i in 0..phnum { + // The fields below sit within `phentsize` bytes of `o`, so their offsets can't overflow either. + let Some(o) = i.checked_mul(phentsize).and_then(|x| x.checked_add(phoff)).filter(|&o| o.checked_add(phentsize).is_some()) else { break }; + let ph = if e.is64 { + (e.u32(o), e.u32(o + 4), e.u64(o + 8), e.u64(o + 16), e.u64(o + 32), e.u64(o + 40), e.u64(o + 48)) + } else { + ( + e.u32(o), + e.u32(o + 24), + e.u32(o + 4).map(u64::from), + e.u32(o + 8).map(u64::from), + e.u32(o + 16).map(u64::from), + e.u32(o + 20).map(u64::from), + e.u32(o + 28).map(u64::from), + ) + }; + let (Some(t), Some(f), Some(off), Some(va), Some(fs), Some(ms), Some(al)) = ph else { break }; + out.push(ProgramHeader { + kind: segment_type_name(t), + flags: permissions(f), + offset: off, + vaddr: va, + file_size: fs, + mem_size: ms, + align: al, + p_type: t, + p_flags: f, + }); + } + Ok(out) +} + +/// A dynamic tag's `DT_` name without the prefix (`NEEDED`, `GNU_HASH`…), or its number in hex. +pub fn dynamic_tag_name(tag: u64) -> String { + match tag { + 0 => "NULL", + 1 => "NEEDED", + 2 => "PLTRELSZ", + 3 => "PLTGOT", + 4 => "HASH", + 5 => "STRTAB", + 6 => "SYMTAB", + 7 => "RELA", + 8 => "RELASZ", + 9 => "RELAENT", + 10 => "STRSZ", + 11 => "SYMENT", + 12 => "INIT", + 13 => "FINI", + 14 => "SONAME", + 15 => "RPATH", + 16 => "SYMBOLIC", + 17 => "REL", + 18 => "RELSZ", + 19 => "RELENT", + 20 => "PLTREL", + 21 => "DEBUG", + 22 => "TEXTREL", + 23 => "JMPREL", + 24 => "BIND_NOW", + 25 => "INIT_ARRAY", + 26 => "FINI_ARRAY", + 27 => "INIT_ARRAYSZ", + 28 => "FINI_ARRAYSZ", + 29 => "RUNPATH", + 30 => "FLAGS", + 0x6fff_fef5 => "GNU_HASH", + 0x6fff_fffb => "FLAGS_1", + 0x6fff_fff0 => "VERSYM", + 0x6fff_fffe => "VERNEED", + 0x6fff_ffff => "VERNEEDNUM", + _ => return format!("{tag:#x}"), + } + .into() +} + +/// Map a virtual address to a file offset using `PT_LOAD` headers. Addresses in a segment's +/// zero-filled tail, or whose offset would overflow, map to nothing. +pub fn va_to_off(phdrs: &[ProgramHeader], va: u64) -> Option { + phdrs.iter().filter(|p| p.p_type == 1).find(|p| va >= p.vaddr && va - p.vaddr < p.file_size).and_then(|p| p.offset.checked_add(va - p.vaddr)) +} + +/// The (tag, value) pairs of the `PT_DYNAMIC` segment in `phdrs`, up to its `DT_NULL`, at most +/// [`MAX_DYN`]. Empty without a dynamic segment. +pub fn dynamic_entries(data: &[u8], phdrs: &[ProgramHeader]) -> Result, ElfError> { + Ok(entries(&E::new(data)?, phdrs)) +} + +fn entries(e: &E<'_>, phdrs: &[ProgramHeader]) -> Vec<(u64, u64)> { + let mut out = Vec::new(); + let Some(d) = phdrs.iter().find(|p| p.p_type == 2) else { return out }; + let ent = 2 * e.word_size(); + let n = usize::try_from(d.file_size).unwrap_or(0) / ent; + let start = usize::try_from(d.offset).unwrap_or(usize::MAX); + for i in 0..n.min(MAX_DYN) { + let Some(o) = i.checked_mul(ent).and_then(|x| start.checked_add(x)) else { break }; + let (Some(tag), Some(val)) = (e.word(o), o.checked_add(e.word_size()).and_then(|v| e.word(v))) else { break }; + if tag == 0 { + break; + } + out.push((tag, val)); + } + out +} + +#[cfg(test)] +mod tests; diff --git a/crates/elf/src/tests.rs b/crates/elf/src/tests.rs new file mode 100644 index 000000000..a5aaf2060 --- /dev/null +++ b/crates/elf/src/tests.rs @@ -0,0 +1,201 @@ +//! Program header and dynamic section tests. `an_address_past_the_last_file_offset_maps_to_nothing` +//! is from Craft Anything's `elf.rs`; the image builder is a small stand-in for its testkit's. + +use super::*; + +/// Field writer in the image's class and byte order. +struct Image { + d: Vec, + is64: bool, + le: bool, +} + +impl Image { + fn put(&mut self, at: usize, value: u64, width: usize) { + let bytes = value.to_le_bytes(); + let mut field = bytes[..width].to_vec(); + if !self.le { + field.reverse(); + } + self.d[at..at + width].copy_from_slice(&field); + } + + fn word(&mut self, at: usize, value: u64) { + self.put(at, value, if self.is64 { 8 } else { 4 }); + } +} + +/// An executable with PT_PHDR, PT_LOAD (r-x, whole file at 0x40_0000), PT_DYNAMIC at file 0x200 +/// and PT_GNU_STACK (rw-). The dynamic section holds NEEDED, STRTAB and a DT_NULL, then junk. +fn elf(is64: bool, le: bool) -> Vec { + let mut img = Image { d: vec![0u8; 0x300], is64, le }; + img.d[..4].copy_from_slice(b"\x7fELF"); + img.d[4] = if is64 { 2 } else { 1 }; + img.d[5] = if le { 1 } else { 2 }; + img.d[6] = 1; + let (phoff_at, entsize_at, num_at, phoff, entsize) = if is64 { (32, 54, 56, 64, 56) } else { (28, 42, 44, 52, 32) }; + img.word(phoff_at, phoff as u64); + img.put(entsize_at, entsize as u64, 2); + img.put(num_at, 4, 2); + let headers = [ + (6u32, 4u32, phoff as u64, 0x40_0040, 4 * entsize as u64), + (1, 5, 0, 0x40_0000, 0x300), + (2, 6, 0x200, 0x40_0200, 0x60), + (0x6474_e551, 6, 0, 0, 0), + ]; + for (i, (t, f, off, va, size)) in headers.into_iter().enumerate() { + let o = phoff + i * entsize; + img.put(o, u64::from(t), 4); + if is64 { + img.put(o + 4, u64::from(f), 4); + img.word(o + 8, off); + img.word(o + 16, va); + img.word(o + 32, size); + img.word(o + 40, size); + img.word(o + 48, 0x1000); + } else { + img.word(o + 4, off); + img.word(o + 8, va); + img.word(o + 16, size); + img.word(o + 20, size); + img.put(o + 24, u64::from(f), 4); + img.word(o + 28, 0x1000); + } + } + let w = if is64 { 8 } else { 4 }; + for (i, (tag, value)) in [(1u64, 1u64), (5, 0x40_0280), (0, 0), (1, 9)].into_iter().enumerate() { + img.word(0x200 + i * 2 * w, tag); + img.word(0x200 + i * 2 * w + w, value); + } + img.d +} + +#[test] +fn program_headers_in_every_class_and_byte_order() { + for is64 in [false, true] { + for le in [false, true] { + let ph = program_headers(&elf(is64, le)).unwrap(); + let kinds: Vec<(&str, &str)> = ph.iter().map(|p| (p.kind.as_str(), p.flags.as_str())).collect(); + assert_eq!(kinds, [("PHDR", "r--"), ("LOAD", "r-x"), ("DYNAMIC", "rw-"), ("GNU_STACK", "rw-")], "is64={is64} le={le}"); + assert_eq!((ph[1].offset, ph[1].vaddr, ph[1].file_size, ph[1].mem_size, ph[1].align), (0, 0x40_0000, 0x300, 0x300, 0x1000)); + assert_eq!((ph[2].p_type, ph[2].p_flags), (2, 6)); + let dynamic = dynamic_entries(&elf(is64, le), &ph).unwrap(); + assert_eq!(dynamic, [(1, 1), (5, 0x40_0280)], "stops at DT_NULL"); + assert_eq!(va_to_off(&ph, 0x40_0280), Some(0x280)); + } + } +} + +#[test] +fn bad_idents_and_headers_are_errors() { + assert_eq!(program_headers(b"\x7fELX\x02\x01"), Err(ElfError::Malformed("no ELF magic".into()))); + assert_eq!(program_headers(b"\x7fELF"), Err(ElfError::Truncated("ELF ident"))); + assert_eq!(program_headers(b"\x7fELF\x02"), Err(ElfError::Truncated("ELF ident"))); + assert_eq!(program_headers(b"\x7fELF\x03\x01"), Err(ElfError::Malformed("bad ELF class or data encoding".into()))); + assert_eq!(program_headers(b"\x7fELF\x01\x00"), Err(ElfError::Malformed("bad ELF class or data encoding".into()))); + assert_eq!(program_headers(&elf(true, true)[..20]), Err(ElfError::Truncated("ELF header"))); + assert_eq!(ElfError::Truncated("ELF header").to_string(), "the file ends early (ELF header)"); + let mut small = elf(true, true); + small[54] = 40; + assert_eq!(program_headers(&small).unwrap_err().to_string(), "the file is malformed (program header entry too small)"); + assert!(dynamic_entries(b"nope", &[]).is_err()); +} + +#[test] +fn a_table_cut_short_keeps_its_complete_entries() { + let d = elf(true, true); + assert_eq!(program_headers(&d[..64 + 56 * 2 + 10]).unwrap().len(), 2); + let mut none = d.clone(); + none[56] = 0; + none[54] = 0; + assert!(program_headers(&none).unwrap().is_empty(), "no headers need no entry size"); + let mut far = d.clone(); + far[32..40].copy_from_slice(&u64::MAX.to_le_bytes()); + assert!(program_headers(&far).unwrap().is_empty(), "offsets that overflow stop the walk"); +} + +#[test] +fn dynamic_entries_need_a_dynamic_segment_inside_the_file() { + let d = elf(false, true); + let mut ph = program_headers(&d).unwrap(); + assert!(dynamic_entries(&d, &ph[..2]).unwrap().is_empty()); + ph[2].offset = 0x2f8; + assert!(dynamic_entries(&d, &ph).unwrap().is_empty(), "an entry cut off by the end of the file"); + ph[2].offset = u64::MAX; + assert!(dynamic_entries(&d, &ph).unwrap().is_empty()); +} + +fn load(offset: u64, vaddr: u64, file_size: u64) -> ProgramHeader { + ProgramHeader { kind: "LOAD".into(), flags: String::new(), offset, vaddr, file_size, mem_size: file_size, align: 0, p_type: 1, p_flags: 0 } +} + +#[test] +fn an_address_past_the_last_file_offset_maps_to_nothing() { + assert_eq!(va_to_off(&[load(0x200, 0x1000, 0x100)], 0x1010), Some(0x210)); + assert_eq!(va_to_off(&[load(u64::MAX, 0x1000, 0x100)], 0x1000), Some(u64::MAX)); + assert_eq!(va_to_off(&[load(u64::MAX, 0x1000, 0x100)], 0x1001), None, "the offset would pass u64::MAX"); + assert_eq!(va_to_off(&[load(0x200, 0x1000, 0x100)], 0x1100), None, "the zero-filled tail has no file bytes"); + let mut dynamic = load(0, 0x1000, 0x100); + dynamic.p_type = 2; + assert_eq!(va_to_off(&[dynamic], 0x1010), None, "only PT_LOAD maps"); +} + +#[test] +fn names() { + let segments: Vec = + [0, 1, 2, 3, 4, 5, 6, 7, 0x6474_e550, 0x6474_e551, 0x6474_e552, 0x6474_e553, 0x7000_0001, 0x99].into_iter().map(segment_type_name).collect(); + assert_eq!( + segments, + [ + "NULL", + "LOAD", + "DYNAMIC", + "INTERP", + "NOTE", + "SHLIB", + "PHDR", + "TLS", + "GNU_EH_FRAME", + "GNU_STACK", + "GNU_RELRO", + "GNU_PROPERTY", + "ARM_EXIDX", + "0x99" + ] + ); + assert_eq!([0, 1, 2, 4, 7].map(permissions), ["---", "--x", "-w-", "r--", "rwx"]); + let tags: Vec = + (0..=30).chain([0x6fff_fef5, 0x6fff_fffb, 0x6fff_fff0, 0x6fff_fffe, 0x6fff_ffff, 0x7000_0000]).map(dynamic_tag_name).collect(); + assert_eq!(tags[..3], ["NULL", "NEEDED", "PLTRELSZ"]); + assert_eq!(tags[29..], ["RUNPATH", "FLAGS", "GNU_HASH", "FLAGS_1", "VERSYM", "VERNEED", "VERNEEDNUM", "0x70000000"]); + assert!(tags.iter().all(|t| !t.is_empty())); +} + +#[cfg(feature = "serde")] +#[test] +fn program_headers_serialize_with_hex_addresses() { + let ph = program_headers(&elf(true, true)).unwrap(); + let value = serde_json::to_value(&ph[1]).unwrap(); + assert_eq!(value["kind"], "LOAD"); + assert_eq!(value["vaddr"], "0x400000"); + assert!(value.get("p_type").is_none()); +} + +#[test] +fn damaged_input_never_panics() { + for sample in [elf(true, true), elf(false, false)] { + for i in 0..sample.len() { + if let Ok(ph) = program_headers(&sample[..i]) { + let _ = dynamic_entries(&sample[..i], &ph); + } + for v in [0x00, 0x7f, 0xff] { + let mut m = sample.clone(); + m[i] = v; + if let Ok(ph) = program_headers(&m) { + let _ = dynamic_entries(&m, &ph); + let _ = va_to_off(&ph, 0x40_0280); + } + } + } + } +} diff --git a/crates/exr/src/decode.rs b/crates/exr/src/decode.rs index 36f55c378..a82495ad1 100644 --- a/crates/exr/src/decode.rs +++ b/crates/exr/src/decode.rs @@ -7,7 +7,7 @@ //! - [`decode_part`] decodes the channels of one flat part that a filter keeps, in their stored //! sample type, over the part's data window. Every compression the `exr` crate reads is read, //! scan lines and tiles alike (the largest resolution level). Other parts' chunks are skipped -//! unread, and channels the filter drops are never allocated. +//! unread, and channels the filter drops are never allocated. Other parts may be deep. //! - [`fn@write`] writes one scan-line part. //! //! The `exr` crate sizes buffers from headers before it reads pixels, so everything that reaches @@ -18,7 +18,7 @@ use std::io::Cursor; use exr::block::UncompressedBlock; -use exr::block::chunk::TileCoordinates; +use exr::block::chunk::{Chunk, CompressedBlock, CompressedScanLineBlock, CompressedTileBlock, TileCoordinates}; use exr::image::read::image::ReadImage; use exr::image::read::layers::{ChannelsReader, ReadAllLayers, ReadChannels}; use exr::meta::MetaData; @@ -245,11 +245,21 @@ fn window_size(size: exr::math::Vec2) -> (u32, u32) { /// Reads the headers and checks every part's declared size, alone and together, before any pixel /// allocation. Headers are read without the crate's validation, which refuses deep parts. -fn read_meta(bytes: &[u8], limits: &Limits) -> Result { +pub(super) fn read_meta(bytes: &[u8], limits: &Limits) -> Result { check_previews(bytes)?; let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(map_exr)?; - let (mut total, mut dwa) = (0u64, 0u64); - for header in meta.headers.iter() { + let (mut total, mut flat, mut dwa) = (0u64, 0u64, 0u64); + for (index, header) in meta.headers.iter().enumerate() { + // The crate sizes a flat chunk's bound as a block's pixels times the bytes a pixel takes, + // without checking the product, before it reads the chunk. + let block_pixels = match header.blocks { + exr::meta::BlockDescription::Tiles(tiles) => (tiles.tile_size.0 as u64).checked_mul(tiles.tile_size.1 as u64), + exr::meta::BlockDescription::ScanLines => (header.compression.scan_lines_per_block() as u64).checked_mul(header.layer_size.0 as u64), + }; + let block_bytes = block_pixels.and_then(|n| n.checked_mul(header.channels.bytes_per_pixel as u64)).and_then(|n| usize::try_from(n).ok()); + if !header.deep && block_bytes.is_none() { + return Err(Error::LimitExceeded(format!("a block of part {index} holds more bytes than this platform can address"))); + } let (w, h) = window_size(header.layer_size); if w > limits.max_width || h > limits.max_height { return Err(Error::LimitExceeded(format!("a {w}x{h} part exceeds {}x{}", limits.max_width, limits.max_height))); @@ -260,23 +270,24 @@ fn read_meta(bytes: &[u8], limits: &Limits) -> Result { } let bytes = pixels.saturating_mul(header.channels.list.len().max(1) as u64 * 4); total = total.saturating_add(bytes); - if matches!(header.compression, ExrCompression::DWAA(_) | ExrCompression::DWAB(_)) { - dwa = dwa.saturating_add(bytes); + if !header.deep { + flat = flat.saturating_add(bytes); + if matches!(header.compression, ExrCompression::DWAA(_) | ExrCompression::DWAB(_)) { + dwa = dwa.saturating_add(bytes); + } } } if total > limits.max_bytes { return Err(Error::LimitExceeded(format!("{total} bytes across {} parts exceed {}", meta.headers.len(), limits.max_bytes))); } - // The crate allocates a flat part's samples before reading its chunks, so the file must be - // able to hold them. DWA's run-length channels are zlib-packed on top and reach about - // 132000:1, so DWA parts are counted at a quarter. Deep parts size their samples from the - // file's own counts. - if !meta.requirements.has_deep_data { - let declared = total.saturating_sub(dwa).saturating_add(dwa / 4); - let capacity = (bytes.len() as u64).saturating_mul(limits.max_ratio).saturating_add(1 << 20); - if declared > capacity { - return Err(Error::LimitExceeded(format!("a {} byte file cannot hold {declared} bytes of samples", bytes.len()))); - } + // A flat part's samples are allocated before its chunks are read, so the file must be able + // to hold the flat parts' samples. DWA's run-length channels are zlib-packed on top and reach + // about 132000:1, so DWA parts are counted at a quarter. Deep parts size their samples from + // the file's own counts and are not counted here. + let declared = flat.saturating_sub(dwa).saturating_add(dwa / 4); + let capacity = (bytes.len() as u64).saturating_mul(limits.max_ratio).saturating_add(1 << 20); + if declared > capacity { + return Err(Error::LimitExceeded(format!("a {} byte file cannot hold {declared} bytes of samples", bytes.len()))); } Ok(meta) } @@ -459,19 +470,24 @@ pub fn decode_display_part( } let block_limit = std::cell::Cell::new(None); let select = DisplaySelect { index, keep, fill, limits, seen: std::cell::Cell::new(0), block_limit: &block_limit }; - let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}) - .from_buffered(Cursor::new(bytes)) - .map_err(|error| block_limit.take().map(Error::LimitExceeded).unwrap_or_else(|| map_exr(error)))?; - let channels = image - .layer_data - .into_iter() - .nth(index) - .ok_or_else(|| malformed("the part was not read"))? - .channel_data - .into_iter() - .flatten() - .map(|(name, samples)| DisplayChannel { name, samples }) - .collect(); + let exr_error = |error| block_limit.take().map(Error::LimitExceeded).unwrap_or_else(|| map_exr(error)); + let planes = if meta.headers.iter().any(|header| header.deep) { + let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?; + select.seen.set(index); + let mut reader = select.create_channels_reader(header).map_err(exr_error)?; + read_flat_part_chunks(bytes, &meta, index, &mut reader, limits)?; + reader.into_channels() + } else { + ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}) + .from_buffered(Cursor::new(bytes)) + .map_err(exr_error)? + .layer_data + .into_iter() + .nth(index) + .ok_or_else(|| malformed("the part was not read"))? + .channel_data + }; + let channels = planes.into_iter().flatten().map(|(name, samples)| DisplayChannel { name, samples }).collect(); Ok(DisplayPart { info, channels }) } @@ -515,18 +531,23 @@ impl<'s> ReadChannels<'s> for DisplaySelect<'_> { u32s: vec![], }); } - // The EXR crate decompresses each accepted block before `read_block` can discard ignored - // channels. Bound that all-channel input separately from the selected output planes. - let Some(block_bytes) = layer_w - .checked_mul(layer_h) - .and_then(|pixels| pixels.checked_mul(header.channels.list.len().max(1))) - .and_then(|samples| samples.checked_mul(std::mem::size_of::())) - else { + // The EXR crate decompresses each accepted block, with every channel, before `read_block` + // can discard the ignored ones, so bound that all-channel block separately from the + // selected output planes. Blocks are decompressed one at a time, so the bound is the + // largest block (a tile within the data window, or a scan-line group across it), not the + // whole part: a part whose full channel set is larger than `max_bytes` still decodes when + // the kept planes and each block fit. + let block_pixels = match header.blocks { + exr::meta::BlockDescription::Tiles(tiles) => tiles.tile_size.0.min(layer_w).checked_mul(tiles.tile_size.1.min(layer_h)), + // a part shorter than its compression's scan-line group has only its own rows + exr::meta::BlockDescription::ScanLines => header.compression.scan_lines_per_block().min(layer_h).checked_mul(layer_w), + }; + let Some(block_bytes) = block_pixels.and_then(|pixels| pixels.checked_mul(header.channels.bytes_per_pixel.max(1))) else { self.block_limit.set(Some(format!("data-window block size exceeds {}", self.limits.max_bytes))); return Err(exr::error::Error::Invalid("data-window block size overflow".into())); }; if u64::try_from(block_bytes).map_or(true, |bytes| bytes > self.limits.max_bytes) || block_bytes > isize::MAX as usize { - self.block_limit.set(Some(format!("{block_bytes} data-window bytes exceed {}", self.limits.max_bytes))); + self.block_limit.set(Some(format!("{block_bytes} bytes in one data-window block exceed {}", self.limits.max_bytes))); return Err(exr::error::Error::Invalid("data-window blocks exceed limit".into())); } let pixels = display_w.checked_mul(display_h).filter(|n| *n > 0).ok_or(exr::error::Error::Invalid("display window".into()))?; @@ -727,22 +748,220 @@ impl ChannelsReader for SelectReader { } } +/// Where the headers end and the offset tables begin: [`MetaData::read_from_buffered`] consumes +/// exactly the version field and the headers. +fn tables_start(bytes: &[u8]) -> Result { + let mut cursor = Cursor::new(bytes); + MetaData::read_from_buffered(&mut cursor, false).map_err(map_exr)?; + Ok(cursor.position()) +} + +/// Stable dense ordinal without materializing every tile. The library's block iterator +/// internally allocates a Vec of all coordinates, which is unsuitable for cropped reads. +pub(super) fn flat_block_ordinal(header: &Header, tile: TileCoordinates) -> Result { + use exr::math::Vec2; + use exr::meta::attribute::LevelMode; + let mut start = 0usize; + let tile_size = header.max_block_pixel_size(); + if tile_size.0 == 0 || tile_size.1 == 0 { + return Err(malformed("flat block has a zero dimension")); + } + let mut visit = |level: Vec2, size: Vec2| -> Result> { + let nx = size.0.div_ceil(tile_size.0); + let ny = size.1.div_ceil(tile_size.1); + if tile.level_index == level { + if tile.tile_index.0 >= nx || tile.tile_index.1 >= ny { + return Err(malformed("flat tile lies outside its level")); + } + return tile + .tile_index + .1 + .checked_mul(nx) + .and_then(|n| n.checked_add(tile.tile_index.0)) + .and_then(|n| start.checked_add(n)) + .map(Some) + .ok_or_else(|| malformed("block ordinal overflow")); + } + start = nx.checked_mul(ny).and_then(|n| start.checked_add(n)).ok_or_else(|| malformed("block count overflow"))?; + Ok(None) + }; + match header.blocks { + exr::meta::BlockDescription::Tiles(tiles) => match tiles.level_mode { + LevelMode::Singular => { + if let Some(i) = visit(Vec2(0, 0), header.layer_size)? { + return Ok(i); + } + } + LevelMode::MipMap => { + for (level, size) in exr::meta::mip_map_levels(tiles.rounding_mode, header.layer_size) { + if let Some(i) = visit(Vec2(level, level), size)? { + return Ok(i); + } + } + } + LevelMode::RipMap => { + for (level, size) in exr::meta::rip_map_levels(tiles.rounding_mode, header.layer_size) { + if let Some(i) = visit(level, size)? { + return Ok(i); + } + } + } + }, + exr::meta::BlockDescription::ScanLines => { + if let Some(i) = visit(Vec2(0, 0), header.layer_size)? { + return Ok(i); + } + } + } + Err(malformed("flat tile names a missing level")) +} + +/// Reads the chunks of flat part `index` of a file that also holds deep parts into `reader`. +/// +/// The `exr` crate's image reader validates every header before it reads a chunk and refuses +/// deep headers, so here only the flat headers are validated, the offset tables (one `u64` per +/// chunk, a table per part in header order) are walked from the end of the headers, and only the +/// selected part's chunks are read. Each chunk must start past the last offset table and inside +/// the file, carry the selected part's number, and hold one of the part's blocks that no other +/// chunk of the part holds, so the part's chunks hold every one of its blocks. A scan-line +/// chunk's y must be the first line of its block, and no chunk may declare more bytes than the +/// file has left after it. +fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &mut impl ChannelsReader, limits: &Limits) -> Result<()> { + let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?; + let multipart = meta.requirements.is_multilayer(); + let mut long_names = false; + for flat in meta.headers.iter().filter(|h| !h.deep) { + flat.validate(multipart, &mut long_names, false).map_err(map_exr)?; + } + let truncated = || malformed("the offset tables run past the end of the file"); + let (mut table, mut tables_end) = (0u64, tables_start(bytes)?); + for (i, h) in meta.headers.iter().enumerate() { + if i == index { + table = tables_end; + } + let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8)).ok_or_else(truncated)?; + tables_end = tables_end.checked_add(len).filter(|end| *end <= bytes.len() as u64).ok_or_else(truncated)?; + } + let offsets = usize::try_from(table) + .ok() + .and_then(|start| bytes.get(start..start.checked_add(header.chunk_count.checked_mul(8)?)?)) + .ok_or_else(truncated)?; + // One bit per block, not a Vec/HashSet of every coordinate. A cropped display must not + // permit unbounded bookkeeping for an enormous data window of tiny tiles. + let tracking_bytes = header.chunk_count.div_ceil(8); + if tracking_bytes as u64 > limits.max_bytes || tracking_bytes > isize::MAX as usize { + return Err(Error::LimitExceeded("block tracking exceeds the byte budget".into())); + } + let mut seen = Vec::new(); + seen.try_reserve_exact(tracking_bytes).map_err(|_| Error::LimitExceeded("cannot allocate block tracking".into()))?; + seen.resize(tracking_bytes, 0u8); + for &entry in offsets.as_chunks::<8>().0 { + let offset = u64::from_le_bytes(entry); + let chunk = usize::try_from(offset) + .ok() + .filter(|_| offset >= tables_end) + .and_then(|start| bytes.get(start..)) + .filter(|rest| !rest.is_empty()) + .ok_or_else(|| malformed(format!("chunk offset {offset} is outside the chunk area of the file")))?; + if multipart { + let part = chunk.get(..4).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(i32::from_le_bytes); + let part = part.ok_or_else(|| malformed(format!("the chunk at offset {offset} is cut off")))?; + if usize::try_from(part).ok() != Some(index) { + return Err(malformed(format!("the chunk at offset {offset} belongs to part {part}, not part {index}"))); + } + } + // The crate allocates a chunk's declared payload before it reads it, so the payload must + // fit in the rest of the file. The length follows the part number and the block's y, or + // its four tile coordinates. + let fields = if matches!(header.blocks, exr::meta::BlockDescription::Tiles(_)) { 16 } else { 4 }; + let size_at = if multipart { 4 } else { 0 } + fields; + let size = chunk.get(size_at..size_at + 4).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(i32::from_le_bytes); + let size = size.ok_or_else(|| malformed(format!("the chunk at offset {offset} is cut off")))?; + let left = chunk.len() - (size_at + 4); + if usize::try_from(size).is_ok_and(|size| size > left) { + return Err(malformed(format!("the chunk at offset {offset} declares {size} bytes, but {left} remain in the file"))); + } + let size = usize::try_from(size).map_err(|_| malformed("negative packed chunk length"))?; + // Read coordinates without allocating the payload. Validate their real clipped block + // and budgets before Chunk::read uses the nominal (possibly huge) tile dimensions. + let coordinate_at = if multipart { 4 } else { 0 }; + let shape = if matches!(header.blocks, exr::meta::BlockDescription::Tiles(_)) { + let coordinates = TileCoordinates::read(&mut Cursor::new(&chunk[coordinate_at..])).map_err(map_exr)?; + CompressedBlock::Tile(CompressedTileBlock { coordinates, compressed_pixels_le: Vec::new() }) + } else { + let y = chunk + .get(coordinate_at..coordinate_at + 4) + .and_then(|b| <[u8; 4]>::try_from(b).ok()) + .ok_or_else(|| malformed("truncated scan-line coordinate"))?; + CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: i32::from_le_bytes(y), compressed_pixels_le: Vec::new() }) + }; + let tile = header.get_block_data_indices(&shape).map_err(map_exr)?; + if let CompressedBlock::ScanLine(block) = &shape { + // The block index is rounded toward zero, so a y that is not the first line of a + // block, or lies just above the data window, would still name a block. + let lines = header.compression.scan_lines_per_block() as i64; + let first = i64::from(header.own_attributes.layer_position.y()) + tile.tile_index.y() as i64 * lines; + if i64::from(block.y_coordinate) != first { + return Err(malformed(format!( + "the scan-line chunk at offset {offset} starts at y {}, which is not the first line of a block", + block.y_coordinate + ))); + } + } + let ordinal = flat_block_ordinal(header, tile)?; + let flag = 1u8 << (ordinal % 8); + let byte = seen.get_mut(ordinal / 8).ok_or_else(|| malformed("block is outside its chunk table"))?; + if *byte & flag != 0 { + return Err(malformed("duplicate flat block")); + } + *byte |= flag; + if reader.filter_block(tile) { + let area = header.get_absolute_block_pixel_coordinates(tile).map_err(map_exr)?.size; + let raw_size = area + .0 + .checked_mul(area.1) + .and_then(|n| n.checked_mul(header.channels.bytes_per_pixel)) + .ok_or_else(|| Error::LimitExceeded("flat block byte size overflows".into()))?; + if raw_size as u64 > limits.max_bytes || raw_size > isize::MAX as usize { + return Err(Error::LimitExceeded("flat block exceeds the byte budget".into())); + } + // Packed size need not be less than raw size for every accepted compression. + // Bound its allocation independently, rather than rejecting valid compressed data + // solely because a clipped edge block has fewer raw bytes. + if size as u64 > limits.max_bytes || size > isize::MAX as usize { + return Err(Error::LimitExceeded("packed flat block exceeds the byte budget".into())); + } + let chunk = Chunk::read(&mut Cursor::new(chunk), meta).map_err(map_exr)?; + let block = UncompressedBlock::decompress_chunk(chunk, meta, false).map_err(map_exr)?; + reader.read_block(header, block).map_err(map_exr)?; + } + } + Ok(()) +} + /// Decodes the channels of flat part `index` that `keep` accepts (by the name as stored), in -/// their stored type. A file with deep parts, or a kept channel that is subsampled, is -/// [`Error::Unsupported`]. +/// their stored type. A deep part, or a kept channel that is subsampled, is +/// [`Error::Unsupported`]; the file's other parts may be deep. pub fn decode_part(bytes: &[u8], index: usize, keep: &dyn Fn(&str) -> bool, limits: &Limits) -> Result { let meta = read_meta(bytes, limits)?; - if meta.requirements.has_deep_data { + let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index} of {}", meta.headers.len())))?; + if header.deep { return Err(Error::Unsupported("deep data (use the deep reader)".into())); } - let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index} of {}", meta.headers.len())))?; if matches!(header.compression, ExrCompression::HTJ2K32 | ExrCompression::HTJ2K256) { return Err(Error::Unsupported("HTJ2K compression".into())); } let info = part_info(index, header); let select = Select { index, keep, seen: std::cell::Cell::new(0) }; - let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}).from_buffered(Cursor::new(bytes)).map_err(map_exr)?; - let planes = image.layer_data.into_iter().nth(index).map(|l| l.channel_data).ok_or_else(|| malformed("the part was not read"))?; + let planes = if meta.headers.iter().any(|header| header.deep) { + select.seen.set(index); + let mut reader = select.create_channels_reader(header).map_err(map_exr)?; + read_flat_part_chunks(bytes, &meta, index, &mut reader, limits)?; + reader.into_channels() + } else { + let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}).from_buffered(Cursor::new(bytes)).map_err(map_exr)?; + image.layer_data.into_iter().nth(index).map(|l| l.channel_data).ok_or_else(|| malformed("the part was not read"))? + }; let channels = info.channels.iter().zip(planes).filter_map(|(c, p)| Some(Channel { name: c.name.clone(), samples: p? })).collect(); Ok(Part { info, channels }) } diff --git a/crates/exr/src/decode_tests.rs b/crates/exr/src/decode_tests.rs index 54c28d9aa..89f44d506 100644 --- a/crates/exr/src/decode_tests.rs +++ b/crates/exr/src/decode_tests.rs @@ -1,4 +1,5 @@ -//! The preview check is PhotoCraft's test; the rest builds files with the `exr` crate. +//! The preview check is PhotoCraft's test; the rest builds files with the `exr` crate, except the +//! files that mix flat and deep parts, which are built by hand. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] use exr::meta::attribute::{Preview, Text}; @@ -129,10 +130,30 @@ fn decoded_display_part_places_selected_channels_and_checks_total_budget() { let limits = Limits { max_bytes: 6 * 4 - 1, ..Limits::default() }; assert!(matches!(decode_display_part(&bytes, 0, &all, &|_| 0.0, &limits), Err(Error::LimitExceeded(_)))); - // One selected display plane fits exactly, but the EXR decoder must decompress both data - // channels in its input block before discarding the ignored one. + // One selected display plane fits exactly. Both data channels together (2x2x2 f32, 32 bytes) + // don't, but they are decompressed one scan-line block (2 pixels x 2 channels) at a time. let block_limits = Limits { max_bytes: 6 * 4, ..Limits::default() }; - assert!(matches!(decode_display_part(&bytes, 0, &|name| name == "R", &|_| 0.0, &block_limits), Err(Error::LimitExceeded(_)))); + let part = decode_display_part(&bytes, 0, &|name| name == "R", &|_| 9.0, &block_limits).unwrap(); + assert_eq!(part.channels[0].samples, [0.0, 1.0, 9.0, 2.0, 3.0, 9.0]); +} + +#[test] +fn decoded_display_part_bounds_each_all_channel_block() { + // The kept R plane is 4 pixels of f32 (16 bytes), but each scan-line block carries all four + // channels (64 bytes) before the ignored ones are discarded. + let bytes = file(vec![layer(None, 4, 1, &["R", "G", "B", "A"], false, exr::prelude::Compression::ZIP1)], (4, 1)); + let r = |name: &str| name == "R"; + assert!(matches!(decode_display_part(&bytes, 0, &r, &|_| 0.0, &Limits { max_bytes: 16, ..Limits::default() }), Err(Error::LimitExceeded(_)))); + assert_eq!(decode_display_part(&bytes, 0, &r, &|_| 0.0, &Limits { max_bytes: 64, ..Limits::default() }).unwrap().channels.len(), 1); + // ZIP groups 16 scan lines, but a one-row part's block is that row: still 64 bytes, not 1024 + let zip16 = file(vec![layer(None, 4, 1, &["R", "G", "B", "A"], false, exr::prelude::Compression::ZIP16)], (4, 1)); + assert!(matches!(decode_display_part(&zip16, 0, &r, &|_| 0.0, &Limits { max_bytes: 63, ..Limits::default() }), Err(Error::LimitExceeded(_)))); + assert_eq!(decode_display_part(&zip16, 0, &r, &|_| 0.0, &Limits { max_bytes: 64, ..Limits::default() }).unwrap().channels.len(), 1); + // A tile is the block of a tiled part: a 4x4 tile of four f32 channels is 256 bytes, while + // the whole 8x8 part is 1024 and the kept R plane of the 2x2 display window only 16. + let tiled = file(vec![layer(None, 8, 8, &["R", "G", "B", "A"], true, exr::prelude::Compression::ZIP1)], (2, 2)); + assert!(matches!(decode_display_part(&tiled, 0, &r, &|_| 0.0, &Limits { max_bytes: 255, ..Limits::default() }), Err(Error::LimitExceeded(_)))); + assert_eq!(decode_display_part(&tiled, 0, &r, &|_| 0.0, &Limits { max_bytes: 256, ..Limits::default() }).unwrap().channels[0].samples.len(), 4); } #[test] @@ -238,3 +259,451 @@ fn hostile_files_never_panic() { } assert!(parts(b"not exr", &limits).is_err()); } + +// A multi-part file with a flat part and a deep scan-line part, built by hand per the OpenEXR file +// layout (the `exr` crate's writer refuses deep headers): version 2 with the deep and multi-part +// flags, the headers and an empty header, one offset table per part, then chunks that each start +// with their part number. Both parts are uncompressed, two chunks a part: the deep part and a +// scan-line flat part hold one scan line a chunk, and a tiled flat part one 2 × 2 tile a chunk. + +fn mixed_attr(out: &mut Vec, name: &str, ty: &str, payload: &[u8]) { + out.extend_from_slice(name.as_bytes()); + out.push(0); + out.extend_from_slice(ty.as_bytes()); + out.push(0); + out.extend_from_slice(&(payload.len() as i32).to_le_bytes()); + out.extend_from_slice(payload); +} + +/// The version field and the headers of a mixed file whose flat part is `w × h` with float +/// `B`, `G`, `R` channels, in scan lines or (with `tiled`) in one level of 2 × 2 tiles, and whose +/// deep part is 3 × 2 with one float `A` channel. Both parts are uncompressed. +fn mixed_headers(deep_first: bool, tiled: bool, (w, h): (usize, usize)) -> Vec { + mixed_headers_with(deep_first, tiled, (w, h), 0, &["B", "G", "R"], 2) +} + +/// [`mixed_headers`] with the flat part's compression code (0 for none, or 3 for ZIP, which +/// holds 16 scan lines a block), its float channels, and the width and height of its tiles. +fn mixed_headers_with(deep_first: bool, tiled: bool, (w, h): (usize, usize), compression: u8, flat_channels: &[&str], tile: u32) -> Vec { + mixed_headers_with_deep_size(deep_first, tiled, (w, h), compression, flat_channels, tile, (3, 2)) +} + +fn mixed_headers_with_deep_size( + deep_first: bool, + tiled: bool, + (w, h): (usize, usize), + compression: u8, + flat_channels: &[&str], + tile: u32, + deep_size: (usize, usize), +) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); + out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); + for deep in if deep_first { [true, false] } else { [false, true] } { + let (w, h, channels): (usize, usize, &[&str]) = if deep { (deep_size.0, deep_size.1, &["A"]) } else { (w, h, flat_channels) }; + let mut chlist = Vec::new(); + for name in channels { + chlist.extend_from_slice(name.as_bytes()); + chlist.push(0); + chlist.extend_from_slice(&2i32.to_le_bytes()); // float + chlist.extend_from_slice(&[0u8; 4]); // pLinear and reserved + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + } + chlist.push(0); + let window: Vec = [0i32, 0, w as i32 - 1, h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + let tiled = tiled && !deep; + let compression = if deep { 0 } else { compression }; + let lines = if compression == 3 { 16 } else { 1 }; + let chunks = if tiled { w.div_ceil(tile as usize) * h.div_ceil(tile as usize) } else { h.div_ceil(lines) }; + mixed_attr(&mut out, "channels", "chlist", &chlist); + mixed_attr(&mut out, "chunkCount", "int", &(chunks as i32).to_le_bytes()); + mixed_attr(&mut out, "compression", "compression", &[compression]); + mixed_attr(&mut out, "dataWindow", "box2i", &window); + mixed_attr(&mut out, "displayWindow", "box2i", &window); + mixed_attr(&mut out, "lineOrder", "lineOrder", &[0]); + if deep { + mixed_attr(&mut out, "maxSamplesPerPixel", "int", &1i32.to_le_bytes()); + } + mixed_attr(&mut out, "name", "string", if deep { b"deep" } else { b"flat" }); + mixed_attr(&mut out, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + mixed_attr(&mut out, "screenWindowCenter", "v2f", &[0u8; 8]); + mixed_attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes()); + if tiled { + // `tile` × `tile` tiles, one level, rounded down. + let mut desc: Vec = [tile, tile].iter().flat_map(|v| v.to_le_bytes()).collect(); + desc.push(0); + mixed_attr(&mut out, "tiles", "tiledesc", &desc); + } + let ty: &[u8] = if deep { + b"deepscanline" + } else if tiled { + b"tiledimage" + } else { + b"scanlineimage" + }; + mixed_attr(&mut out, "type", "string", ty); + if deep { + mixed_attr(&mut out, "version", "int", &1i32.to_le_bytes()); + } + out.push(0); // end of this header + } + out.push(0); // end of the header list + out +} + +/// Sample `i` of a flat channel: `R` is 1, 2, 3, …, `G` 10 more and `B` 100 more, top row first. +fn mixed_flat(channel: f32, i: usize) -> f32 { + channel + 1.0 + i as f32 +} + +/// A mixed file with a 3 × 2 flat part, and its chunk offsets as `offsets[part][chunk]`. The +/// tiled flat part's chunks hold the tiles at (0, 0) and (1, 0). +fn mixed_file(deep_first: bool, tiled: bool) -> (Vec, [[usize; 2]; 2]) { + let (w, h) = (3usize, 2usize); + let mut out = mixed_headers(deep_first, tiled, (w, h)); + let chunks: Vec>> = (0..2i32) + .map(|part| { + let deep = (part == 0) == deep_first; + (0..h) + .map(|y| { + let mut c = Vec::new(); + c.extend_from_slice(&part.to_le_bytes()); + if tiled && !deep { + // Tile (y, 0) at level (0, 0): columns 2y and 2y + 1 that lie inside. + [y as i32, 0, 0, 0].iter().for_each(|v| c.extend_from_slice(&v.to_le_bytes())); + let xs = 2 * y..(2 * y + 2).min(w); + c.extend_from_slice(&((xs.len() * h * 3 * 4) as i32).to_le_bytes()); + for row in 0..h { + for channel in [100.0, 10.0, 0.0] { + xs.clone().for_each(|x| c.extend_from_slice(&mixed_flat(channel, row * w + x).to_le_bytes())); + } + } + return c; + } + c.extend_from_slice(&(y as i32).to_le_bytes()); + if deep { + // Packed count table, packed and unpacked sample data: one sample a pixel. + for size in [w * 4, w * 4, w * 4] { + c.extend_from_slice(&(size as u64).to_le_bytes()); + } + (1..=w as u32).for_each(|n| c.extend_from_slice(&n.to_le_bytes())); + (0..w).for_each(|_| c.extend_from_slice(&0.5f32.to_le_bytes())); + } else { + c.extend_from_slice(&((w * 3 * 4) as i32).to_le_bytes()); + for channel in [100.0, 10.0, 0.0] { + (0..w).for_each(|x| c.extend_from_slice(&mixed_flat(channel, y * w + x).to_le_bytes())); + } + } + c + }) + .collect() + }) + .collect(); + let mut offsets = [[0usize; 2]; 2]; + let mut at = out.len() + 2 * h * 8; + for (part, lines) in chunks.iter().enumerate() { + for (y, c) in lines.iter().enumerate() { + offsets[part][y] = at; + out.extend_from_slice(&(at as u64).to_le_bytes()); + at += c.len(); + } + } + chunks.into_iter().flatten().for_each(|c| out.extend_from_slice(&c)); + (out, offsets) +} + +#[test] +fn a_flat_part_beside_a_deep_part_decodes() { + let limits = Limits::default(); + for (deep_first, tiled) in [(false, false), (true, false), (false, true), (true, true)] { + let (bytes, _) = mixed_file(deep_first, tiled); + let (flat, deep) = if deep_first { (1, 0) } else { (0, 1) }; + let parts = parts(&bytes, &limits).unwrap(); + assert_eq!((parts[flat].deep, parts[deep].deep, parts[flat].name.as_deref()), (false, true, Some("flat"))); + + let part = decode_part(&bytes, flat, &all, &limits).unwrap(); + for (name, channel) in [("R", 0.0), ("G", 10.0), ("B", 100.0)] { + let expected: Vec = (0..6).map(|i| mixed_flat(channel, i)).collect(); + assert_eq!(part.channel(name).unwrap().samples, Samples::F32(expected), "deep first {deep_first} tiled {tiled} {name}"); + } + let only_g = decode_part(&bytes, flat, &|n| n == "G", &limits).unwrap(); + assert_eq!(only_g.channels.iter().map(|c| c.name.as_str()).collect::>(), ["G"]); + + let display = decode_display_part(&bytes, flat, &|n| n == "flat.R", &|_| 9.0, &limits).unwrap(); + assert_eq!(display.channels.len(), 1); + assert_eq!((display.channels[0].name.as_str(), display.channels[0].samples.as_slice()), ("flat.R", &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0][..])); + + assert!(matches!(decode_part(&bytes, deep, &all, &limits), Err(Error::Unsupported(_)))); + assert!(matches!(decode_display_part(&bytes, deep, &all, &|_| 0.0, &limits), Err(Error::Unsupported(_)))); + assert!(matches!(decode_part(&bytes, 2, &all, &limits), Err(Error::Malformed(_)))); + let deep_image = crate::deep::decode_deep(&bytes, &limits).unwrap(); + assert_eq!((deep_image.width, deep_image.height), (3, 2)); + } +} + +#[test] +fn a_flat_part_s_chunks_beside_a_deep_part_are_checked_before_they_are_read() { + let limits = Limits::default(); + for deep_first in [false, true] { + let (good, offsets) = mixed_file(deep_first, false); + let (flat, deep) = if deep_first { (1, 0) } else { (0, 1) }; + let tables = mixed_headers(deep_first, false, (3, 2)).len(); + let entry = tables + flat * 2 * 8; + let with_offset = |slot: usize, value: u64| { + let mut b = good.clone(); + b[entry + slot * 8..entry + slot * 8 + 8].copy_from_slice(&value.to_le_bytes()); + b + }; + let refused = |b: &[u8]| { + for r in [decode_part(b, flat, &all, &limits).map(|_| ()), decode_display_part(b, flat, &all, &|_| 0.0, &limits).map(|_| ())] { + assert!(matches!(&r, Err(Error::Malformed(_))), "deep first {deep_first}: {r:?}"); + } + }; + // The deep part's chunk, the end of the file and past it, the offset tables, the same + // chunk twice. + refused(&with_offset(0, offsets[deep][0] as u64)); + refused(&with_offset(0, good.len() as u64)); + refused(&with_offset(0, u64::MAX)); + refused(&with_offset(0, (tables + 8) as u64)); + refused(&with_offset(1, offsets[flat][0] as u64)); + // A chunk naming another part, and one declaring more bytes than a block holds. + let mut b = good.clone(); + b[offsets[flat][1]..offsets[flat][1] + 4].copy_from_slice(&(deep as i32).to_le_bytes()); + refused(&b); + let mut b = good.clone(); + b[offsets[flat][0] + 8..offsets[flat][0] + 12].copy_from_slice(&i32::MAX.to_le_bytes()); + refused(&b); + // Offset tables longer than the file. + refused(&good[..tables + 20]); + + for n in 0..good.len() { + let _ = decode_part(&good[..n], flat, &all, &limits); + let _ = decode_display_part(&good[..n], flat, &all, &|_| 0.0, &limits); + } + for i in 0..good.len() { + for v in [0u8, 1, 0x7F, 0xFF] { + let mut b = good.clone(); + b[i] = v; + if let Ok(p) = decode_part(&b, flat, &all, &limits) { + let n = p.info.data_window.width as usize * p.info.data_window.height as usize; + assert!(p.channels.iter().all(|c| c.samples.len() == n)); + } + let _ = decode_display_part(&b, flat, &all, &|_| 0.0, &limits); + } + } + } +} + +#[test] +fn a_tiled_flat_part_beside_a_deep_part_must_hold_each_of_its_tiles() { + let limits = Limits::default(); + for deep_first in [false, true] { + let (good, offsets) = mixed_file(deep_first, true); + let flat = usize::from(deep_first); + // The second chunk names tile (0, 0) at level (1, 1), which a one-level part does not + // have, or tile (0, 0) again, so tile (1, 0) has no chunk. + for coordinates in [[0i32, 0, 1, 1], [0, 0, 0, 0]] { + let mut b = good.clone(); + let at = offsets[flat][1] + 4; + coordinates.iter().enumerate().for_each(|(i, v)| b[at + i * 4..at + i * 4 + 4].copy_from_slice(&v.to_le_bytes())); + for r in [decode_part(&b, flat, &all, &limits).map(|_| ()), decode_display_part(&b, flat, &all, &|_| 0.0, &limits).map(|_| ())] { + assert!(matches!(&r, Err(Error::Malformed(_))), "deep first {deep_first} {coordinates:?}: {r:?}"); + } + } + } +} + +#[test] +fn a_scan_line_chunk_beside_a_deep_part_must_start_at_its_block_s_first_line() { + // A ZIP flat part (16 lines a block) holds its 3 × 2 pixels in one chunk, stored raw, as a + // block is when compressing it does not make it smaller; the deep part follows. + let limits = Limits::default(); + let (w, h) = (3usize, 2usize); + let (plain, offsets) = mixed_file(false, false); + let deep = &plain[offsets[1][0]..]; + let with_y = |y: i32| { + let mut out = mixed_headers_with(false, false, (w, h), 3, &["B", "G", "R"], 2); + let mut chunk = Vec::new(); + chunk.extend_from_slice(&0i32.to_le_bytes()); + chunk.extend_from_slice(&y.to_le_bytes()); + chunk.extend_from_slice(&((w * h * 3 * 4) as i32).to_le_bytes()); + for row in 0..h { + for channel in [100.0, 10.0, 0.0] { + (0..w).for_each(|x| chunk.extend_from_slice(&mixed_flat(channel, row * w + x).to_le_bytes())); + } + } + let flat_at = out.len() + 3 * 8; + let deep_at = flat_at + chunk.len(); + for at in [flat_at, deep_at, deep_at + offsets[1][1] - offsets[1][0]] { + out.extend_from_slice(&(at as u64).to_le_bytes()); + } + out.extend_from_slice(&chunk); + out.extend_from_slice(deep); + out + }; + let part = decode_part(&with_y(0), 0, &all, &limits).unwrap(); + assert_eq!(part.channel("R").unwrap().samples, Samples::F32((0..6).map(|i| mixed_flat(0.0, i)).collect())); + // Each of these y values rounds to block 0, but none is that block's first line. + for y in [-15, -1, 1, 15] { + let b = with_y(y); + for r in [decode_part(&b, 0, &all, &limits).map(|_| ()), decode_display_part(&b, 0, &all, &|_| 0.0, &limits).map(|_| ())] { + assert!(matches!(&r, Err(Error::Malformed(_))), "y {y}: {r:?}"); + } + } +} + +/// A mixed file whose flat part, first, is one pixel of these float channels (each 1.0) in one +/// `tile` × `tile` tile, held by a chunk that declares `size` payload bytes. +fn mixed_one_tile(channels: &[&str], tile: u32, size: i32) -> Vec { + let (plain, offsets) = mixed_file(false, false); + let mut out = mixed_headers_with(false, true, (1, 1), 0, channels, tile); + let mut chunk = Vec::new(); + [0i32, 0, 0, 0, 0, size].iter().for_each(|v| chunk.extend_from_slice(&v.to_le_bytes())); + channels.iter().for_each(|_| chunk.extend_from_slice(&1f32.to_le_bytes())); + let flat_at = out.len() + 3 * 8; + let deep_at = flat_at + chunk.len(); + for at in [flat_at, deep_at, deep_at + offsets[1][1] - offsets[1][0]] { + out.extend_from_slice(&(at as u64).to_le_bytes()); + } + out.extend_from_slice(&chunk); + out.extend_from_slice(&plain[offsets[1][0]..]); + out +} + +#[test] +fn a_chunk_beside_a_deep_part_must_fit_the_rest_of_the_file() { + // 2^14 × 2^14 tiles of three floats let a chunk hold up to 3 GiB (also on 32-bit), so the crate would + // allocate the 2 GiB this chunk declares before it found the file too short. + let limits = Limits::default(); + let channels = ["B", "G", "R"]; + let good = mixed_one_tile(&channels, 1 << 14, 12); + assert_eq!(decode_part(&good, 0, &all, &limits).unwrap().channel("R").unwrap().samples, Samples::F32(vec![1.0])); + let b = mixed_one_tile(&channels, 1 << 14, i32::MAX); + for r in [decode_part(&b, 0, &all, &limits).map(|_| ()), decode_display_part(&b, 0, &all, &|_| 0.0, &limits).map(|_| ())] { + assert!(matches!(&r, Err(Error::Malformed(m)) if m.contains("remain in the file")), "{r:?}"); + } +} + +#[test] +fn a_tile_too_large_to_address_is_refused_before_a_chunk_is_read() { + // Five float channels in 10^9 × 10^9 tiles: 20 bytes a pixel times 10^18 pixels does not fit + // in a 64-bit size, though the 1 × 1 part passes every size limit. + let limits = Limits::default(); + let channels = ["A", "B", "C", "D", "E"]; + let tile = 1_000_000_000u32; + let mixed = mixed_one_tile(&channels, tile, 20); + + // A file of flat parts only, read by the crate's own reader, with its 4 × 4 tiles widened. + let mut flat_only = file(vec![layer(None, 1, 1, &channels, true, exr::prelude::Compression::Uncompressed)], (1, 1)); + let at = flat_only.windows(9).position(|w| w == b"tiledesc\0").unwrap() + 9 + 4; + [tile, tile].iter().enumerate().for_each(|(i, v)| flat_only[at + i * 4..at + i * 4 + 4].copy_from_slice(&v.to_le_bytes())); + + for (name, b) in [("mixed", &mixed), ("flat only", &flat_only)] { + let results = [ + parts(b, &limits).map(|_| ()), + decode_part(b, 0, &all, &limits).map(|_| ()), + decode_display_part(b, 0, &all, &|_| 0.0, &limits).map(|_| ()), + ]; + for r in results { + assert!(matches!(&r, Err(Error::LimitExceeded(_))), "{name}: {r:?}"); + } + } +} + +#[test] +fn the_flat_parts_of_a_mixed_file_must_fit_the_file() { + // 1024 × 1024 × 3 float samples (12 MiB) declared in a file of a few hundred bytes: over a + // ratio of 1 plus the 1 MiB slack, though another part is deep. + let headers = mixed_headers(false, false, (1024, 1024)); + assert!(parts(&headers, &Limits::default()).is_ok()); + let tight = Limits { max_ratio: 1, ..Limits::default() }; + for r in [parts(&headers, &tight).map(|_| ()), decode_part(&headers, 0, &all, &tight).map(|_| ())] { + assert!(matches!(&r, Err(Error::LimitExceeded(_))), "{r:?}"); + } + // The deep part's samples are not counted, and a small flat part passes the same ratio. + assert!(parts(&mixed_file(false, false).0, &tight).is_ok()); +} + +#[test] +fn deep_samples_do_not_count_against_the_flat_expansion_ratio() { + // The 4 MiB deep plane would exceed max_ratio=1 plus the 1 MiB slack if counted. + // Header inspection does not allocate those samples; deep decoding bounds its own counts. + let headers = mixed_headers_with_deep_size(false, false, (1, 1), 0, &["R"], 2, (1024, 1024)); + let limits = Limits { max_ratio: 1, max_bytes: 8 * 1024 * 1024, ..Limits::default() }; + assert!(parts(&headers, &limits).is_ok()); +} + +#[test] +fn mixed_display_decode_bounds_each_block_not_the_whole_flat_part() { + for (tiled, block_bytes) in [(false, 36), (true, 48)] { + for deep_first in [false, true] { + let (bytes, _) = mixed_file(deep_first, tiled); + let flat = usize::from(deep_first); + let keep = |name: &str| name == "flat.R"; + // The full flat part is 72 bytes; the selected plane is 24 bytes. + let limits = Limits { max_bytes: block_bytes, ..Limits::default() }; + let part = decode_display_part(&bytes, flat, &keep, &|_| 0.0, &limits).unwrap(); + assert_eq!(part.channels[0].samples, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]); + let tight = Limits { max_bytes: block_bytes - 1, ..limits }; + assert!(matches!(decode_display_part(&bytes, flat, &keep, &|_| 0.0, &tight), Err(Error::LimitExceeded(_)))); + } + } +} + +#[test] +fn present_packed_payload_is_bounded_before_allocation() { + let packed = 1024 * 1024; + let mut bytes = mixed_one_tile(&["R"], 1 << 15, packed); + // Make the declared payload present, not merely a truncated-file error. The nominal + // huge tile allows this size in exr::Chunk::read, but the actual flat image is 1x1. + bytes.resize(bytes.len() + packed as usize, 0); + let limits = Limits { max_bytes: 4, ..Limits::default() }; + assert!(matches!( + decode_display_part(&bytes, 0, &|_| true, &|_| 0.0, &limits), + Err(Error::LimitExceeded(message)) if message.contains("packed flat block") + )); + // decode_part also includes the deep header's 24 bytes in its aggregate header budget. + let limits = Limits { max_bytes: 32, ..limits }; + assert!(matches!(decode_part(&bytes, 0, &all, &limits), Err(Error::LimitExceeded(message)) if message.contains("packed flat block"))); +} + +#[test] +fn cropped_mixed_display_bounds_block_tracking() { + let mut bytes = mixed_headers_with(false, true, (100, 100), 0, &["R"], 1); + // Both display windows are 1x1; the flat data window still has 10,000 one-pixel tiles. + let needle = b"displayWindow\0box2i\0"; + let positions: Vec = bytes.windows(needle.len()).enumerate().filter_map(|(i, w)| (w == needle).then_some(i + needle.len() + 4)).collect(); + for at in positions { + bytes[at..at + 16].fill(0); + } + // Supply all offset-table bytes so validation reaches bookkeeping, not truncation. + bytes.resize(bytes.len() + (10_000 + 2) * 8, 0); + let limits = Limits { max_bytes: 4, ..Limits::default() }; + assert!(matches!( + decode_display_part(&bytes, 0, &|_| true, &|_| 0.0, &limits), + Err(Error::LimitExceeded(message)) if message.contains("block tracking") + )); +} + +#[test] +fn dense_block_ordinals_cover_scan_lines_and_all_tile_levels() { + use exr::block::chunk::TileCoordinates; + use exr::meta::attribute::LevelMode; + for tiled in [false, true] { + let bytes = file(vec![layer(None, 9, 7, &["R"], tiled, exr::prelude::Compression::ZIP1)], (9, 7)); + let mut meta = read_meta(&bytes, &Limits::default()).unwrap(); + let header = &mut meta.headers[0]; + for mode in [LevelMode::Singular, LevelMode::MipMap, LevelMode::RipMap] { + if let exr::meta::BlockDescription::Tiles(tiles) = &mut header.blocks { + tiles.level_mode = mode; + } + for (i, block) in header.blocks_increasing_y_order().enumerate() { + assert_eq!(flat_block_ordinal(header, block.location).unwrap(), i); + } + let absent = TileCoordinates { tile_index: exr::math::Vec2(0, 0), level_index: exr::math::Vec2(100, 100) }; + assert!(flat_block_ordinal(header, absent).is_err()); + } + } +} diff --git a/crates/exr/src/deep.rs b/crates/exr/src/deep.rs index b86d497c8..ad2ab00b5 100644 --- a/crates/exr/src/deep.rs +++ b/crates/exr/src/deep.rs @@ -391,19 +391,14 @@ fn decode_deep_meta(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Result Result { Ok(cur.position()) } +/// Where part `header_index`'s offset table starts and where the last part's table ends. The +/// tables follow the headers, `chunk_count` u64 per header in header order, and the chunks +/// follow the last table. Sizes that overflow, or tables that run past the end of the file, +/// are errors. +fn offset_tables(meta: &MetaData, bytes: &[u8], header_index: usize) -> Result<(u64, u64)> { + let mut table_pos = header_end(bytes)?; + let mut tables_end = table_pos; + for (i, h) in meta.headers.iter().enumerate() { + let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8)); + tables_end = len.and_then(|len| tables_end.checked_add(len)).ok_or_else(|| err("the offset tables are too large"))?; + if i < header_index { + table_pos = tables_end; + } + } + if tables_end > bytes.len() as u64 { + return Err(err(format!("the offset tables end at byte {tables_end}, past the end of the file"))); + } + Ok((table_pos, tables_end)) +} + /// Composite the deep samples into a flat image: per pixel, samples sorted by Z (stable; the /// file is assumed messy), front-to-back `over` with premultiplied colour, un-premultiplied /// into straight alpha at the end. Without any alpha channel the front-most sample wins, diff --git a/crates/exr/src/deep_tests.rs b/crates/exr/src/deep_tests.rs index cb1721809..bdbfa0d68 100644 --- a/crates/exr/src/deep_tests.rs +++ b/crates/exr/src/deep_tests.rs @@ -886,6 +886,106 @@ fn inconsistent_count_table_errors() { assert!(matches!(decode_deep_exr(&bytes, &Limits::none()), Err(CodecError::Malformed(_)))); } +/// A two-part file made from the single-part deep file `deep` and a 1×1 flat scan-line part, +/// the deep part first when `deep_first`: each header gains `name` and `chunkCount`, the +/// version field the multipart bit, and each chunk its part number. The offset tables follow +/// the headers in header order, then the chunks. Returns the file, where the first table +/// starts, where the second table starts and where the last table ends. +fn two_part(deep: &[u8], deep_first: bool) -> (Vec, usize, usize, usize) { + let end = header_end_of(deep); + let first = u64::from_le_bytes(deep[end..end + 8].try_into().expect("8")) as usize; + let offsets: Vec = + (0..(first - end) / 8).map(|i| u64::from_le_bytes(deep[end + 8 * i..end + 8 * i + 8].try_into().expect("8")) as usize).collect(); + let deep_chunks: Vec<&[u8]> = offsets.iter().enumerate().map(|(i, &o)| &deep[o..offsets.get(i + 1).copied().unwrap_or(deep.len())]).collect(); + + let mut deep_header = deep[8..end - 1].to_vec(); + attr(&mut deep_header, "name", "string", b"deep"); + attr(&mut deep_header, "chunkCount", "int", &(deep_chunks.len() as i32).to_le_bytes()); + let mut flat_header = Vec::new(); + let mut chlist = b"R\0".to_vec(); + chlist.extend_from_slice(&1i32.to_le_bytes()); // half + chlist.extend_from_slice(&[0u8; 4]); // pLinear, reserved + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.push(0); + attr(&mut flat_header, "channels", "chlist", &chlist); + attr(&mut flat_header, "compression", "compression", &[0]); + let dw: Vec = [0i32; 4].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut flat_header, "dataWindow", "box2i", &dw); + attr(&mut flat_header, "displayWindow", "box2i", &dw); + attr(&mut flat_header, "lineOrder", "lineOrder", &[0]); + attr(&mut flat_header, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + attr(&mut flat_header, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat()); + attr(&mut flat_header, "screenWindowWidth", "float", &1f32.to_le_bytes()); + attr(&mut flat_header, "name", "string", b"flat"); + attr(&mut flat_header, "type", "string", b"scanlineimage"); + attr(&mut flat_header, "chunkCount", "int", &1i32.to_le_bytes()); + // The flat part's one chunk: y, the data size, one half sample. + let mut flat_chunk = 0i32.to_le_bytes().to_vec(); + flat_chunk.extend_from_slice(&2i32.to_le_bytes()); + flat_chunk.extend_from_slice(&f16::from_f32(0.5).to_le_bytes()); + let flat_chunks: Vec<&[u8]> = vec![&flat_chunk]; + + let parts: [(&[u8], &[&[u8]]); 2] = if deep_first { + [(&deep_header, &deep_chunks), (&flat_header, &flat_chunks)] + } else { + [(&flat_header, &flat_chunks), (&deep_header, &deep_chunks)] + }; + let mut out = deep[..4].to_vec(); + out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); + for (header, _) in &parts { + out.extend_from_slice(header); + out.push(0); // end of this header + } + out.push(0); // end of the headers + let first_table = out.len(); + let second_table = first_table + parts[0].1.len() * 8; + let tables_end = second_table + parts[1].1.len() * 8; + let mut pos = tables_end; + for (_, chunks) in &parts { + for c in chunks.iter() { + out.extend_from_slice(&(pos as u64).to_le_bytes()); + pos += 4 + c.len(); + } + } + for (part, (_, chunks)) in parts.iter().enumerate() { + for c in chunks.iter() { + out.extend_from_slice(&(part as i32).to_le_bytes()); + out.extend_from_slice(c); + } + } + (out, first_table, second_table, tables_end) +} + +/// The deep part of a two-part file decodes as the single-part file does, first or second. +/// Its chunks start after every part's offset table: a chunk offset into the other part's +/// table, or one byte before the end of the last table, is refused as table data, and a file +/// that ends inside the last table is refused even when the deep part's own table is whole. +#[test] +fn multipart_deep_chunks_start_after_every_offset_table() { + let single = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + let want = decode_deep_exr(&single, &Limits::none()).expect("single part"); + for deep_first in [true, false] { + let (bytes, ..) = two_part(&single, deep_first); + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("two parts"); + assert_eq!(deep.counts, want.counts); + assert_eq!(deep.channel("Z").expect("Z").samples, want.channel("Z").expect("Z").samples); + } + + let refused = |bytes: &[u8], what: &str| match decode_deep_exr(bytes, &Limits::none()) { + Err(e @ CodecError::Malformed(_)) => assert!(e.to_string().contains(what), "{e}"), + Err(e) => panic!("{e}, want an error about {what}"), + Ok(_) => panic!("decoded, want an error about {what}"), + }; + let (bytes, first_table, second_table, tables_end) = two_part(&single, true); + for off in [second_table, tables_end - 1] { + let mut into_table = bytes.clone(); + into_table[first_table..first_table + 8].copy_from_slice(&(off as u64).to_le_bytes()); + refused(&into_table, "outside the chunk area"); + } + refused(&bytes[..tables_end - 4], "past the end of the file"); +} + /// `gen_deep` for a cropped render: the data window sits at `(dwx, dwy)` inside a larger /// display window of `sw × sh` (the stage the renderer composed onto). Scanline chunk Y /// coordinates are absolute display rows, so they carry the data-window offset. diff --git a/crates/font-catalog/src/face_data.rs b/crates/font-catalog/src/face_data.rs new file mode 100644 index 000000000..655c2f408 --- /dev/null +++ b/crates/font-catalog/src/face_data.rs @@ -0,0 +1,89 @@ +//! Shared ownership of a font file and a face within it. + +use std::sync::Arc; + +/// Font file bytes and the index of a face in that file. +/// +/// Clones share owned bytes, so collection faces and variable instances do not copy the file. +/// Static bytes let applications use embedded fonts without allocating a copy. +#[derive(Clone, Debug)] +pub struct FaceData { + bytes: Bytes, + index: u32, +} + +#[derive(Clone, Debug)] +enum Bytes { + Static(&'static [u8]), + Owned(Arc>), + Shared(Arc<[u8]>), +} + +impl FaceData { + /// A face in statically embedded font bytes. + pub fn from_static(bytes: &'static [u8], index: u32) -> Self { + Self { bytes: Bytes::Static(bytes), index } + } + + /// A face in a shared vector, without copying its allocation. + pub fn from_owned(bytes: Arc>, index: u32) -> Self { + Self { bytes: Bytes::Owned(bytes), index } + } + + /// A face in bytes shared by a catalog. + pub fn from_shared(bytes: Arc<[u8]>, index: u32) -> Self { + Self { bytes: Bytes::Shared(bytes), index } + } + + /// The entire font file. + pub fn bytes(&self) -> &[u8] { + match &self.bytes { + Bytes::Static(bytes) => bytes, + Bytes::Owned(bytes) => bytes.as_slice(), + Bytes::Shared(bytes) => bytes, + } + } + + /// The index of this face in the file. + pub fn index(&self) -> u32 { + self.index + } + + /// Parse this face for font metadata and outlines. + pub fn skrifa(&self) -> Option> { + skrifa::FontRef::from_index(self.bytes(), self.index).ok() + } + + /// Parse this face for shaping. + pub fn harfrust(&self) -> Option> { + harfrust::FontRef::from_index(self.bytes(), self.index).ok() + } +} + +impl From for FaceData { + fn from(data: crate::FontData) -> Self { + Self::from_shared(data.bytes, data.index) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sources_keep_bytes_and_face_index_without_copying() { + static DATA: &[u8] = b"not a font"; + let owned = Arc::new(DATA.to_vec()); + let shared: Arc<[u8]> = Arc::from(DATA); + for data in [FaceData::from_static(DATA, 2), FaceData::from_owned(owned.clone(), 2), FaceData::from_shared(shared.clone(), 2)] { + let clone = data.clone(); + assert_eq!(clone.bytes(), DATA); + assert_eq!(clone.bytes().as_ptr(), data.bytes().as_ptr()); + assert_eq!(clone.index(), 2); + assert!(clone.skrifa().is_none()); + assert!(clone.harfrust().is_none()); + } + assert_eq!(Arc::strong_count(&owned), 1); + assert_eq!(Arc::strong_count(&shared), 1); + } +} diff --git a/crates/font-catalog/src/lib.rs b/crates/font-catalog/src/lib.rs index 667d7e1c8..8fbcdc491 100644 --- a/crates/font-catalog/src/lib.rs +++ b/crates/font-catalog/src/lib.rs @@ -23,11 +23,13 @@ mod catalog; mod embedding; mod env; +mod face_data; mod fallback; mod fontconfig; mod ligature; mod line_break; mod outline; +mod plan_cache; mod registry; mod scan; mod segments; @@ -41,11 +43,13 @@ mod visual_order; pub use catalog::{Catalog, FontData, FontFileError, Limits, Lister, LoadedFace, Match, Watch}; pub use embedding::{SubsetEmbedding, subset_embedding}; pub use env::{FontEnv, Os, font_dirs, font_store_dir, office_font_dirs}; +pub use face_data::FaceData; pub use fallback::{cluster_face, drawable_characters}; pub use fontconfig::fontconfig_dirs; pub use ligature::ligature_caret_stops; pub use line_break::settle_line_break; pub use outline::{FontOutline, GlyphPen, OutlineError}; +pub use plan_cache::ShapePlanCache; pub use scan::{FaceInfo, LocalizedName, Outlines, Source, is_font_file, style_italic, style_weight, style_width}; pub use segments::SegmentBoundary; pub use shape::{RunGlyph, RunInput, ShapeRunError, shape_run}; diff --git a/crates/font-catalog/src/plan_cache.rs b/crates/font-catalog/src/plan_cache.rs new file mode 100644 index 000000000..d428de4cd --- /dev/null +++ b/crates/font-catalog/src/plan_cache.rs @@ -0,0 +1,52 @@ +//! Per-face bounded shaping plan reuse. + +use std::sync::{Arc, Mutex}; + +/// A cache for one font face and variation instance. Do not share it across faces or instances. +/// Plans are created outside the lock, then checked again to avoid retaining duplicates. +#[derive(Default)] +pub struct ShapePlanCache { + plans: Mutex>>, +} + +impl ShapePlanCache { + /// Maximum number of plans retained; the oldest is evicted when this is reached. + pub const CAPACITY: usize = 32; + + /// Returns a matching plan or builds and retains one for this face. + pub fn get_or_create( + &self, + shaper: &harfrust::Shaper<'_>, + instance: Option<&harfrust::ShaperInstance>, + direction: harfrust::Direction, + script: Option, + language: Option<&harfrust::Language>, + features: &[harfrust::Feature], + ) -> Arc { + let key = harfrust::ShapePlanKey::new(script, direction).language(language).instance(instance).features(features); + let kept = |plans: &[Arc]| plans.iter().rev().find(|p| key.matches(p)).cloned(); + if let Some(plan) = kept(&self.plans.lock().unwrap_or_else(|e| e.into_inner())) { + return plan; + } + let plan = Arc::new(harfrust::ShapePlan::new(shaper, direction, script, language, features)); + let mut plans = self.plans.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(made) = kept(&plans) { + return made; + } + if plans.len() >= Self::CAPACITY { + plans.remove(0); + } + plans.push(plan.clone()); + plan + } + + /// Number of retained plans. + pub fn len(&self) -> usize { + self.plans.lock().unwrap_or_else(|e| e.into_inner()).len() + } + + /// Whether this cache contains no plans. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } +} diff --git a/crates/font-catalog/src/tests.rs b/crates/font-catalog/src/tests.rs index d851d58ae..105662ec5 100644 --- a/crates/font-catalog/src/tests.rs +++ b/crates/font-catalog/src/tests.rs @@ -351,6 +351,25 @@ fn a_read_is_capped_where_the_file_has_grown() { let _ = std::fs::remove_file(&path); } +#[test] +fn shape_plan_cache_reuses_and_evicts_plans() { + let data = font(&spec("Test", "Regular", 400)); + let face = harfrust::FontRef::from_index(&data, 0).unwrap(); + let shaper_data = harfrust::ShaperData::new(&face); + let shaper = shaper_data.shaper(&face).build(); + let cache = ShapePlanCache::default(); + let make = |language: &harfrust::Language| cache.get_or_create(&shaper, None, harfrust::Direction::LeftToRight, None, Some(language), &[]); + let languages: Vec = ('a'..='z').flat_map(|a| ['a', 'b'].map(move |b| format!("{a}{b}").parse().unwrap())).collect(); + let first = make(&languages[0]); + assert!(Arc::ptr_eq(&first, &make(&languages[0]))); + for language in languages.iter().skip(1) { + make(language); + } + assert_eq!(cache.len(), ShapePlanCache::CAPACITY); + assert!(!Arc::ptr_eq(&first, &make(&languages[0]))); + assert_eq!(cache.len(), ShapePlanCache::CAPACITY); +} + #[test] fn bounded_sfnt_reader_preserves_face_names_and_collection_indices() { let mut bold = spec("Example", "Bold Italic", 700); diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml new file mode 100644 index 000000000..943e0b9b6 --- /dev/null +++ b/crates/format/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "craft-format" +description = "Versioned JSON file formats: the format tag, the registry of versions (hand-written steps, then one folder of changes per version, then the pending changes), the upgrade of an older file and the telling of a newer one" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde_json = { workspace = true } +# The container: content-addressed blobs (SHA-256) in a ZIP package. +sha2 = { version = "0.10", default-features = false, features = ["std"] } +craft-zip = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/format/src/build.rs b/crates/format/src/build.rs new file mode 100644 index 000000000..bbffa98ac --- /dev/null +++ b/crates/format/src/build.rs @@ -0,0 +1,237 @@ +//! For a build script: the generated lists of a family's changes. Each change is one `.rs` file in +//! the family's folder, in `v/` once a release fixed its number, else in `pending/`; the lists +//! are generated so that a change adds a file and never edits a line another change also edits. +//! +//! The generated code, for the app to `include!`, is per family a `FOLDED` list (each folded +//! format and its parts) and a `PENDING` list, both prefixed by the family's `list_prefix`, then a +//! module per change file. It names the app's `Part` type, which the including module has in scope +//! (`type Part = craft_format::Part;`), and a change file's step as `step` (a +//! `pub fn step(v: &mut Value) -> Result<(), AppError>`, when the file has one). + +use std::fmt::Write as _; +use std::path::{Path, PathBuf}; + +/// A family's change files: their folder (relative to the crate's manifest), the last format +/// before them, and the prefix of the family's lists. +pub struct Family { + pub folder: &'static str, + pub base: u32, + pub list_prefix: &'static str, +} + +/// What [`lists`] made: the code to write, and the folders the build script reruns on. +pub struct Lists { + pub code: String, + pub watch: Vec, +} + +/// The `FOLDED` and `PENDING` lists of every family, and a module for each change file. Refuses +/// versions that are not contiguous from a family's base, a change file not named with lowercase +/// letters, digits and dashes, and one that doesn't start with a `//!` note. +pub fn lists(manifest: &Path, families: &[Family]) -> Result { + let mut code = String::new(); + let mut mods = String::new(); + let mut watch = Vec::new(); + for family in families { + let root = manifest.join(family.folder); + family_lists(family, &root, &mut code, &mut mods)?; + watch.push(root); + } + code.push_str(&mods); + Ok(Lists { code, watch }) +} + +/// A family's `FOLDED` and `PENDING` lists; its folded formats follow on from its base. +fn family_lists(family: &Family, root: &Path, out: &mut String, mods: &mut String) -> Result<(), String> { + let Family { folder, base, list_prefix } = family; + let mut folded: Vec<(u32, PathBuf)> = Vec::new(); + for entry in std::fs::read_dir(root).map_err(|e| format!("{}: {e}", root.display()))? { + // an entry that can't be read could be a version folder: fail rather than drop it + let path = entry.map_err(|e| format!("{}: {e}", root.display()))?.path(); + let version = path.file_name().and_then(|n| n.to_str()).and_then(|n| n.strip_prefix('v')).and_then(|n| n.parse().ok()); + if let Some(version) = version + && path.is_dir() + { + folded.push((version, path)); + } + } + folded.sort(); + for (i, (version, _)) in folded.iter().enumerate() { + let expected = base + 1 + i as u32; + if *version != expected { + return Err(format!("{folder}/: expected v{expected}/ next, found v{version}/ (versions are contiguous)")); + } + } + // Module names start with the family's (`drawing_pending_…`), so two families' changes of one + // name never clash. + let module = list_prefix.to_ascii_lowercase(); + let _ = writeln!( + out, + "/// The changes of each format after the base, oldest first (`{folder}/v/`).\nconst {list_prefix}FOLDED: &[(u32, &[Part])] = &[" + ); + for (version, dir) in &folded { + let _ = writeln!(out, " ({version}, &["); + parts(dir, &format!("{module}v{version}"), false, out, mods)?; + out.push_str(" ]),\n"); + } + let _ = writeln!(out, "];\n\n/// The changes no release has a number for yet (`{folder}/pending/`).\nconst {list_prefix}PENDING: &[Part] = &["); + parts(&root.join("pending"), &format!("{module}pending"), true, out, mods)?; + out.push_str("];\n\n"); + Ok(()) +} + +/// Whether a change file declares its `step` function (`fn step(`, `pub fn step (` …) on a line of +/// code: a mention in a comment or a note is not a declaration. +fn declares_step(text: &str) -> bool { + let code = without_comments(text); + let ident = |c: char| c.is_alphanumeric() || c == '_'; + // `fn`, whitespace, `step`, then `(` (or generics), across lines + code.match_indices("fn").any(|(at, _)| { + let before_ok = code[..at].chars().next_back().is_none_or(|c| !ident(c)); + let rest = &code[at + 2..]; + before_ok + && rest.starts_with(char::is_whitespace) + && rest + .trim_start() + .strip_prefix("step") + .is_some_and(|rest| matches!(rest.trim_start().chars().next(), Some('(' | '<')) && !rest.starts_with(ident)) + }) +} + +/// `text` as code only: its `//` and (nested) `/* */` comments become a space, and the contents +/// of its string literals (plain, byte and raw) are dropped, line breaks kept throughout. A comment +/// marker inside a string is not a comment, and `fn/* note */step` still reads as `fn step`. +fn without_comments(text: &str) -> String { + let chars: Vec = text.chars().collect(); + let at = |i: usize| chars.get(i).copied(); + let mut out = String::with_capacity(text.len()); + let (mut i, mut depth) = (0usize, 0usize); + while let Some(c) = at(i) { + let next = at(i + 1); + if depth > 0 { + match (c, next) { + ('/', Some('*')) => (depth, i) = (depth + 1, i + 2), + ('*', Some('/')) => (depth, i) = (depth - 1, i + 2), + _ => { + if c == '\n' { + out.push('\n'); + } + i += 1; + } + } + if depth == 0 { + out.push(' '); + } + continue; + } + let ident_before = i > 0 && at(i - 1).is_some_and(|p| p.is_alphanumeric() || p == '_'); + match (c, next) { + ('/', Some('/')) => { + while at(i).is_some_and(|c| c != '\n') { + i += 1; + } + out.push(' '); + } + ('/', Some('*')) => (depth, i) = (1, i + 2), + ('"', _) => { + out.push('"'); + i += 1; + while let Some(c) = at(i) { + i += 1; + match c { + '\\' => i += 1, + '"' => break, + '\n' => out.push('\n'), + _ => {} + } + } + out.push('"'); + } + // a raw string, `r"…"` or `r#"…"#`, after a byte prefix or on its own + ('r', Some('"' | '#')) + if !ident_before || (i > 0 && at(i - 1) == Some('b') && !(i > 1 && at(i - 2).is_some_and(|p| p.is_alphanumeric() || p == '_'))) => + { + let mut j = i + 1; + let mut hashes = 0; + while at(j) == Some('#') { + hashes += 1; + j += 1; + } + if at(j) != Some('"') { + out.push(c); + i += 1; + continue; + } + out.push('"'); + i = j + 1; + while let Some(c) = at(i) { + if c == '"' && (1..=hashes).all(|k| at(i + k) == Some('#')) { + i += 1 + hashes; + break; + } + if c == '\n' { + out.push('\n'); + } + i += 1; + } + out.push('"'); + } + // a character literal (`'"'`, `'\''`), not a lifetime + ('\'', Some('\\')) => { + i += 2; + while at(i).is_some_and(|c| c != '\'') { + i += 1; + } + i += 1; + out.push_str("' '"); + } + ('\'', Some(_)) if at(i + 2) == Some('\'') => { + i += 3; + out.push_str("' '"); + } + _ => { + out.push(c); + i += 1; + } + } + } + out +} + +/// The `Part`s of the change files in `dir`, in file-name order, and a module for each. A missing +/// folder is an empty list only where it may be absent (`optional`: `pending/`); any other error +/// reading it fails the build rather than leave a change out of the lists. +fn parts(dir: &Path, prefix: &str, optional: bool, out: &mut String, mods: &mut String) -> Result<(), String> { + let entries = match std::fs::read_dir(dir) { + Ok(entries) => entries, + Err(e) if optional && e.kind() == std::io::ErrorKind::NotFound => return Ok(()), + Err(e) => return Err(format!("{}: {e}", dir.display())), + }; + let mut files: Vec = Vec::new(); + for entry in entries { + let path = entry.map_err(|e| format!("{}: {e}", dir.display()))?.path(); + if path.extension().is_some_and(|x| x == "rs") { + files.push(path); + } + } + files.sort(); + for path in files { + let name = path.file_stem().and_then(|s| s.to_str()).unwrap_or_default().to_string(); + if name.is_empty() || !name.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') { + return Err(format!("{}: name a format change file with lowercase letters, digits and dashes", path.display())); + } + let text = std::fs::read_to_string(&path).map_err(|e| format!("{}: {e}", path.display()))?; + if !text.trim_start().starts_with("//!") { + return Err(format!("{}: start a format change with a `//!` note", path.display())); + } + let module = format!("{prefix}_{}", name.replace('-', "_")); + let step = if declares_step(&text) { format!("Some({module}::step)") } else { "None".into() }; + let _ = writeln!(out, " Part {{ name: {name:?}, step: {step} }},"); + let _ = writeln!(mods, "#[path = {:?}]\nmod {module};", path.display().to_string()); + } + Ok(()) +} + +#[cfg(test)] +#[path = "build_tests.rs"] +mod tests; diff --git a/crates/format/src/build_tests.rs b/crates/format/src/build_tests.rs new file mode 100644 index 000000000..79d3cb385 --- /dev/null +++ b/crates/format/src/build_tests.rs @@ -0,0 +1,115 @@ +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +use super::*; + +/// A fresh, empty folder for one test. +fn scratch(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("craft-format-build-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +fn write(path: &Path, text: &str) { + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, text).unwrap(); +} + +/// The error of a run that must fail. +fn refused(dir: &Path) -> String { + match lists(dir, &[DRAWING]) { + Ok(_) => panic!("expected an error"), + Err(e) => e, + } +} + +const DRAWING: Family = Family { folder: "drawing", base: 3, list_prefix: "DRAWING_" }; + +#[test] +fn lists_folded_and_pending_changes_with_their_modules() { + let dir = scratch("ok"); + write(&dir.join("drawing/v4/add-layers.rs"), "//! Layers.\npub fn step(v: &mut Value) -> Result<(), E> { Ok(()) }\n"); + write(&dir.join("drawing/v4/notes.txt"), "not a change"); + write(&dir.join("drawing/v5/rename.rs"), "\n//! A rename with no step.\n"); + write(&dir.join("drawing/pending/next-1.rs"), "//! Pending.\nfn step(v: &mut Value) {}\n"); + std::fs::create_dir_all(dir.join("drawing/v6x")).unwrap(); + write(&dir.join("drawing/v7"), "a file, not a version folder"); + let lists = lists(&dir, &[DRAWING]).unwrap(); + assert_eq!(lists.watch, vec![dir.join("drawing")]); + let code = &lists.code; + assert!(code.contains("const DRAWING_FOLDED: &[(u32, &[Part])] = &["), "{code}"); + assert!(code.contains(" (4, &[\n Part { name: \"add-layers\", step: Some(drawing_v4_add_layers::step) },\n ]),"), "{code}"); + assert!(code.contains("Part { name: \"rename\", step: None }"), "{code}"); + assert!( + code.contains("const DRAWING_PENDING: &[Part] = &[\n Part { name: \"next-1\", step: Some(drawing_pending_next_1::step) },"), + "{code}" + ); + assert!(code.contains("mod drawing_v5_rename;"), "{code}"); + assert!(!code.contains("notes"), "{code}"); + assert!(code.find("const DRAWING_PENDING").unwrap() < code.find("#[path").unwrap(), "modules follow the lists"); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn an_empty_family_has_empty_lists() { + let dir = scratch("empty"); + std::fs::create_dir_all(dir.join("drawing")).unwrap(); + let code = lists(&dir, &[DRAWING]).unwrap().code; + assert!(code.contains("const DRAWING_FOLDED: &[(u32, &[Part])] = &[\n];"), "{code}"); + assert!(code.contains("const DRAWING_PENDING: &[Part] = &[\n];"), "{code}"); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn refuses_gaps_bad_names_missing_notes_and_missing_folders() { + let dir = scratch("bad"); + assert!(refused(&dir).contains("drawing")); + write(&dir.join("drawing/v5/a.rs"), "//! a"); + assert!(refused(&dir).contains("expected v4/ next, found v5/")); + std::fs::rename(dir.join("drawing/v5"), dir.join("drawing/v4")).unwrap(); + write(&dir.join("drawing/v4/Bad_Name.rs"), "//! b"); + assert!(refused(&dir).contains("lowercase letters")); + std::fs::remove_file(dir.join("drawing/v4/Bad_Name.rs")).unwrap(); + write(&dir.join("drawing/pending/no-note.rs"), "pub fn step() {}"); + assert!(refused(&dir).contains("`//!` note")); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn a_step_is_a_declaration_not_a_mention() { + assert!(!declares_step("//! No fn step() is needed.\n")); + assert!(!declares_step("//! a note\n// fn step(v) was removed\nconst X: u8 = 1;\n")); + assert!(declares_step("//! a note\npub fn step (v: &mut Value) -> Result<(), E> { Ok(()) }\n")); + // a declaration inside a block comment, nested or not, is not one + assert!(!declares_step("//! Documentation only.\n/*\nfn step() {}\n*/\n")); + assert!(!declares_step("/* outer /* inner */\nfn step() {}\n*/\nconst X: u8 = 1;\n")); + assert!(declares_step("/* a note */\nfn step(v: &mut Value) {}\n")); + // comment markers inside strings and character literals are not comments + assert!(declares_step("//! A change with a step.\nconst MARKER: &str = \"/*\";\npub fn step(_v: &mut Value) -> Result { Ok(()) }\n")); + assert!(declares_step("const A: &str = r#\"/* \"quoted\" \"#;\nconst B: &[u8] = br\"/*\";\nfn step(v: &mut Value) {}\n")); + assert!(declares_step("const Q: char = '\"';\nconst E: char = '\\'';\nfn step(v: &mut Value) { let _: &'static str = \"\"; }\n")); + assert!(declares_step("const S: &str = \"a \\\" /* b\";\nfn step(v: &mut Value) {}\n")); + assert!(!declares_step("const S: &str = \"\nfn step(v) \";\n"), "inside a string"); + // a comment between the words is whitespace + assert!(declares_step("pub fn/* migration */step(v: &mut Value) {}\n")); + // across lines + assert!(declares_step("//! A change with a step.\npub fn step\n (_v: &mut Value) -> Result {\n Ok(())\n}\n")); + assert!(declares_step("pub fn\nstep(v: &mut Value) {}\n")); + assert!(!declares_step("fn stepper(v: &mut Value) {}\nfn prefn step() {}\n"), "other names"); + assert!(declares_step("const X: u8 = 1; // fn step() is below\nfn step(v: &mut Value) {}\n")); + assert!(declares_step("//! a note\npub(crate) fn step(v: &mut Value) -> Result<(), E> { Ok(()) }\n")); + assert!(declares_step("//! a note\n fn step(v: &mut Value) {}\n")); + assert!(!declares_step("//! a note\nfn stepper(v: &mut Value) {}\nfn fnstep() {}\n")); +} + +#[test] +fn a_documentation_only_change_has_no_step_and_a_missing_version_folder_fails() { + let dir = scratch("doc-only"); + write(&dir.join("drawing/v4/doc.rs"), "//! No fn step() is needed.\n"); + let code = lists(&dir, &[DRAWING]).unwrap().code; + assert!(code.contains("Part { name: \"doc\", step: None }"), "{code}"); + // a version folder that can't be listed is an error, not an empty format + let missing = parts(&dir.join("drawing/v9"), "x", false, &mut String::new(), &mut String::new()); + assert!(missing.is_err()); + assert!(parts(&dir.join("drawing/pending"), "x", true, &mut String::new(), &mut String::new()).is_ok()); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/format/src/container.rs b/crates/format/src/container.rs new file mode 100644 index 000000000..00377baf5 --- /dev/null +++ b/crates/format/src/container.rs @@ -0,0 +1,440 @@ +//! The file container: a ZIP package of named parts (the file's JSON, binary payloads, optional +//! caches), never extracted to filesystem paths. Payloads are content-addressed blobs, +//! `blobs/.`, stored once however often they are referred to. Every size is +//! bounded by the app's [`Limits`]: the file, each part, all parts expanded, the payloads resolved, +//! and the number of entries; a part is read no further than its declared size (a decompression +//! bomb stops there). What a part holds, and how a payload is encoded, is the app's. + +use std::collections::{BTreeMap, BTreeSet}; +use std::io::{Read, Seek, SeekFrom}; +use std::sync::Arc; + +use serde_json::Value; +use sha2::{Digest, Sha256}; + +use crate::FormatError; + +/// How hard the payloads are compressed: Fast favours interactive file operations; Compact trades +/// CPU time for space. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Compression { + #[default] + Fast, + Compact, +} + +/// An app's budgets for its containers. The functions are read each time they are needed, so a +/// limit the app lets the user raise takes effect at once. +#[derive(Clone, Copy)] +pub struct Limits { + /// The most entries a container holds. + pub max_entries: usize, + /// The most bytes all parts may expand to, and all payloads may resolve to. + pub max_expanded: fn() -> usize, + /// The most bytes a part of this name may expand to. + pub part_limit: fn(&str) -> usize, + /// The Deflate level of a compressed part of this name and size. + pub level: fn(&str, usize) -> i64, + /// Whether a container of this many bytes may be read, or why not. + pub check_size: fn(u64) -> Result<(), String>, + /// Whether the work is cancelled (a save the person stopped): checked before each part, and + /// a long part is written a piece at a time and stops before the next piece when this says + /// so ([`FormatError::Cancelled`]). `None`: it cannot be cancelled here. + pub cancelled: Option bool>, +} + +/// The most bytes of a part written between two cancellation checks. +const PIECE: usize = 1 << 20; + +fn invalid(message: impl std::fmt::Display) -> FormatError { + FormatError::Invalid(message.to_string()) +} + +/// Whether the bytes start as a container does, whatever the file is called. +pub fn is_container(bytes: &[u8]) -> bool { + bytes.starts_with(b"PK\x03\x04") +} + +/// The SHA-256 of the bytes. +pub fn sha256(bytes: &[u8]) -> [u8; 32] { + Sha256::digest(bytes).into() +} + +fn hex(digest: &[u8; 32]) -> String { + use std::fmt::Write as _; + let mut out = String::with_capacity(64); + for byte in digest { + // Writing to a `String` cannot fail. + let _ = write!(&mut out, "{byte:02x}"); + } + out +} + +/// The SHA-256 of the bytes, in lowercase hexadecimal: a blob's name, and how a cache is keyed to +/// the file it was written for. +pub fn digest(bytes: &[u8]) -> String { + hex(&sha256(bytes)) +} + +/// One part a [`Writer`] wrote, with the SHA-256 of its uncompressed content. A caller that signs +/// a container uses these to bind the parts without reading the file back. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PartDigest { + pub name: String, + pub len: u64, + pub digest: [u8; 32], +} + +/// A container being written. +pub struct Writer { + zip: craft_zip::Writer, + names: BTreeSet, + expanded: usize, + resolved: usize, + limits: Limits, + /// Which written parts to record for a signature, and the records so far. + tracked: Option, +} + +/// A tracking closure and the part digests collected so far. +type TrackedParts = (fn(&str) -> bool, Vec); + +impl Writer { + pub fn new(limits: Limits) -> Writer { + Writer { zip: craft_zip::Writer::new(), names: BTreeSet::new(), expanded: 0, resolved: 0, limits, tracked: None } + } + + /// Record the parts written from now on whose name `include` accepts, with their SHA-256, for + /// [`Writer::take_parts`]. Used by a caller that signs the container. + pub fn track_parts(&mut self, include: fn(&str) -> bool) { + self.tracked = Some((include, Vec::new())); + } + + /// The tracked parts written so far, oldest first, and stop tracking. + pub fn take_parts(&mut self) -> Option> { + self.tracked.take().map(|(_, parts)| parts) + } + + /// The container's bytes. + pub fn finish(self) -> Result, FormatError> { + self.zip.finish().map_err(invalid) + } + + /// Add a part, compressed or stored. A name already written is written once. + pub fn part(&mut self, name: &str, bytes: &[u8], compressed: bool) -> Result<(), FormatError> { + self.part_with_digest(name, bytes, compressed, None) + } + + /// [`Writer::part`] with a digest the caller already worked out (a blob's name is its digest), + /// so a signed save does not hash the same bytes twice. + fn part_with_digest(&mut self, name: &str, bytes: &[u8], compressed: bool, digest: Option<[u8; 32]>) -> Result<(), FormatError> { + if bytes.len() > (self.limits.part_limit)(name) { + return Err(invalid("part too large")); + } + if self.names.contains(name) { + return Ok(()); + } + // Check the counters this part would leave, and commit them only once it is written: a + // refused part is not remembered as written. + let expanded = self.expanded.checked_add(bytes.len()).ok_or_else(|| invalid("expanded size overflow"))?; + if expanded > (self.limits.max_expanded)() || self.names.len().saturating_add(1) > self.limits.max_entries { + return Err(invalid("package too large")); + } + let cancelled = self.limits.cancelled; + if cancelled.is_some_and(|c| c()) { + return Err(FormatError::Cancelled); + } + // the configured level, clamped to the Deflate levels craft-zip writes (1 to 9) + let compression = if compressed { + craft_zip::Compression::Level((self.limits.level)(name, bytes.len()).clamp(1, 9) as u8) + } else { + craft_zip::Compression::Stored + }; + match cancelled { + Some(cancelled) if bytes.len() > PIECE => { + let mut stopped = false; + let written = self.zip.add_with(name, compression, |out| { + for piece in bytes.chunks(PIECE) { + if cancelled() { + stopped = true; + return Err(std::io::Error::other("cancelled")); + } + out.write_all(piece)?; + } + Ok(()) + }); + if stopped { + return Err(FormatError::Cancelled); + } + written.map_err(invalid)?; + } + _ => self.zip.add(name, bytes, compression).map_err(invalid)?, + } + self.names.insert(name.to_owned()); + self.expanded = expanded; + if let Some((include, parts)) = &mut self.tracked + && include(name) + { + let digest = digest.unwrap_or_else(|| sha256(bytes)); + parts.push(PartDigest { name: name.to_owned(), len: bytes.len() as u64, digest }); + } + Ok(()) + } + + /// Would `entries` more optional parts of `extra` bytes, stored uncompressed, still keep the + /// file within `budget` and its expanded size within the reader's budget? The parts already + /// written count at their expanded size (an upper bound on what they take in the file). + pub fn room(&self, entries: usize, extra: usize, budget: usize) -> bool { + self.remaining(entries, budget).is_some_and(|left| extra <= left) + } + + /// The bytes `entries` more optional parts may hold together ([`Writer::room`]). + pub fn space(&self, entries: usize, budget: usize) -> usize { + self.remaining(entries, budget).unwrap_or(0) + } + + /// The bytes left for `entries` more parts, or `None` when the entries don't fit or what is + /// written already is past `budget` or the reader's budget. + fn remaining(&self, entries: usize, budget: usize) -> Option { + let count = self.names.len().checked_add(entries).filter(|&n| n <= self.limits.max_entries)?; + let overhead = count.saturating_mul(256).saturating_add(65_536); + let in_file = budget.checked_sub(overhead.saturating_add(self.expanded))?; + let expanded = (self.limits.max_expanded)().checked_sub(self.expanded)?; + Some(in_file.min(expanded)) + } + + /// Add a payload as a blob, `blobs/.`, and say its name, for the JSON that + /// refers to it. + pub fn blob(&mut self, bytes: &[u8], extension: &str, compressed: bool) -> Result { + let resolved = self + .resolved + .checked_add(bytes.len()) + .filter(|&n| n <= (self.limits.max_expanded)()) + .ok_or_else(|| invalid("resolved payloads too large"))?; + let digest = sha256(bytes); + let name = format!("blobs/{}.{extension}", hex(&digest)); + self.part_with_digest(&name, bytes, compressed, Some(digest))?; + self.resolved = resolved; + Ok(Value::String(name)) + } +} + +/// A container being read. Opening reads all bytes into memory and checks the directory +/// (the entry count, unique and plain names, every part's +/// declared size), not the parts. +pub struct Reader { + archive: craft_zip::Archive<'static>, + blobs: BTreeMap>, + /// The bytes of payloads resolved so far, repeats counted: bounded by the expanded budget. + pub hydrated: usize, + document: Option>, + limits: Limits, + /// Every byte part reads have expanded to, whatever the budget was at the time. + read_total: u64, +} + +impl Reader { + pub fn new(mut source: impl Read + Seek, limits: Limits) -> Result { + let length = source.seek(SeekFrom::End(0)).map_err(invalid)?; + (limits.check_size)(length).map_err(invalid)?; + + // Read all bytes into memory for the archive, no more than the length just approved: a file + // that grew since is refused rather than read past its budget. + source.seek(SeekFrom::Start(0)).map_err(invalid)?; + let capacity = usize::try_from(length).map_err(|_| invalid("file too large"))?; + let mut bytes = Vec::new(); + bytes.try_reserve_exact(capacity).map_err(|_| invalid("file too large"))?; + source.take(length.saturating_add(1)).read_to_end(&mut bytes).map_err(invalid)?; + if bytes.len() as u64 != length { + return Err(invalid("the file changed while it was read")); + } + + let mut archive = craft_zip::Archive::open_owned( + bytes, + craft_zip::Limits { max_entries: limits.max_entries as u64, max_total_bytes: (limits.max_expanded)() as u64 }, + ) + .map_err(invalid)?; + // the directory lists distinct names, so a repeated name is caught here + if archive.has_duplicate_names() { + return Err(invalid("duplicate or invalid archive entry")); + } + + // Validate entry names match format rules: no empty, no backslashes, + // no path components that are "." or "..", and no duplicates; and every part's declared + // size within its configured limit. + let mut name_set = BTreeSet::new(); + for entry_idx in 0..archive.len() { + // an entry whose name can't be read would be left out of the package unnoticed + let Ok(Some(name)) = archive.entry_name(entry_idx) else { + return Err(invalid("duplicate or invalid archive entry")); + }; + { + if name.is_empty() + || name.contains('\\') + || name.split('/').any(|c| c.is_empty() || c == "." || c == "..") + || !name_set.insert(name.clone()) + { + return Err(invalid("duplicate or invalid archive entry")); + } + if archive.declared_size(&name).is_some_and(|size| size > (limits.part_limit)(&name) as u64) { + return Err(invalid("part too large")); + } + } + } + + Ok(Self { archive, blobs: BTreeMap::new(), hydrated: 0, document: None, limits, read_total: 0 }) + } + + /// The file's own part, `document.json`, read once and kept: provisional caches and the + /// authoritative reading are bound to the same bytes, even if another process overwrites the + /// open file in place. Blobs remain hash-checked. + pub fn document(&mut self, limit: usize) -> Result, FormatError> { + if self.document.is_none() { + let bytes = self.limited_part("document.json", limit)?; + self.document = Some(bytes.into()); + } + // a cached document still keeps the current part limit + let limit = limit.min((self.limits.part_limit)("document.json")); + self.document.as_ref().filter(|d| d.len() <= limit).cloned().ok_or_else(|| invalid("metadata budget")) + } + + /// Whether the container has a part of this name. + pub fn has_part(&self, name: &str) -> bool { + self.archive.contains(name) + } + + /// The names of every part, in the container's order. + pub fn part_names(&self) -> Vec { + self.archive.names().map(|n| n.into_owned()).collect() + } + + /// The part of this name, at most `limit` bytes and never more than its configured limit. + pub fn limited_part(&mut self, name: &str, limit: usize) -> Result, FormatError> { + let limit = limit.min((self.limits.part_limit)(name)); + let expected = self.archive.declared_size(name).ok_or_else(|| invalid("part not found"))?; + if expected > limit as u64 { + return Err(invalid("part too large")); + } + // the expanded budget is read each time: a raised (or lowered) one applies at once, the + // bytes already read still counting against it + let budget = self.archive.budget_mut(); + *budget = craft_zip::Budget::new(budget.what(), (self.limits.max_expanded)() as u64); + if budget.charge(self.read_total).is_err() { + return Err(invalid("package too large")); + } + // Read within budget. craft-zip's read() handles decompression bombs via the limit. + let bytes = self.archive.read(name, limit as u64).map_err(invalid)?.ok_or_else(|| invalid("part not found"))?; + if bytes.len() as u64 != expected { + return Err(invalid("part length mismatch")); + } + self.read_total = self.read_total.saturating_add(expected); + Ok(bytes) + } + + /// The blob `value`'s `field` names, of this extension, within its part limit. + pub fn blob(&mut self, value: &Value, field: &str, extension: &str) -> Result<&[u8], FormatError> { + let limit = (self.limits.part_limit)(&format!("blob.{extension}")); + self.limited_blob(value, field, extension, limit) + } + + /// [`Reader::blob`] within `limit` bytes. Its hash is checked when it is first read, and every + /// reference counts against the resolved budget. + pub fn limited_blob(&mut self, value: &Value, field: &str, extension: &str, limit: usize) -> Result<&[u8], FormatError> { + let name = value.get(field).and_then(Value::as_str).ok_or_else(|| invalid(format!("missing `{field}` reference")))?; + // a cached blob still keeps the part's current limit + let limit = limit.min((self.limits.part_limit)(name)); + let hash = name + .strip_prefix("blobs/") + .and_then(|n| n.strip_suffix(&format!(".{extension}"))) + .filter(|h| h.len() == 64 && h.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))) + .ok_or_else(|| invalid("invalid blob reference"))?; + if !self.blobs.contains_key(name) { + let bytes = self.limited_part(name, limit)?; + if digest(&bytes) != hash { + return Err(invalid(format!("blob hash mismatch: {name}"))); + } + self.blobs.insert(name.to_owned(), bytes); + } + let bytes = self.blobs.get(name).ok_or_else(|| invalid("missing blob"))?; + if bytes.len() > limit { + return Err(invalid("blob exceeds its resource budget")); + } + // Count repeated references too: a tiny archive must not expand into unbounded owned + // strings or arrays when the same blob is referenced repeatedly. + self.hydrated = self + .hydrated + .checked_add(bytes.len()) + .filter(|&n| n <= (self.limits.max_expanded)()) + .ok_or_else(|| invalid("resolved payloads too large"))?; + Ok(bytes) + } +} + +/// A small PNG picture of the file, kept in the container as a disposable cache keyed to the +/// exact file it shows: `cache/thumbnail.json` (its key) and `cache/thumbnail.png`. Reading it +/// never resolves a payload; a picture that isn't one, or is stale, is a miss. +#[derive(Clone, Copy, Debug)] +pub struct Thumbnail { + /// The key's format tag. + pub format: &'static str, + /// The build that wrote it: only the same build reads it back. + pub app: &'static str, + /// Its width and height in pixels. + pub size: [usize; 2], + /// The most bytes it may have. + pub max_bytes: usize, +} + +impl Thumbnail { + /// Whether the bytes start as a PNG of this size within the byte budget (not a decode: a + /// consumer decodes with its own dimension and allocation limits). + pub fn valid(&self, bytes: &[u8]) -> bool { + bytes.len() <= self.max_bytes + && bytes.get(..8) == Some(b"\x89PNG\r\n\x1a\n") + && bytes.get(8..16) == Some(b"\0\0\0\rIHDR") + && bytes.get(16..20) == Some((self.size[0] as u32).to_be_bytes().as_slice()) + && bytes.get(20..24) == Some((self.size[1] as u32).to_be_bytes().as_slice()) + } + + /// Add the picture, keyed to `document` (the file's own part), when it is one and there is + /// room for it within `budget`; otherwise leave it out, never failing the save. + pub fn write(&self, writer: &mut Writer, png: &[u8], document: &[u8], budget: usize) -> Result<(), FormatError> { + if !self.valid(png) { + return Ok(()); + } + let key = + serde_json::to_vec(&serde_json::json!({"format": self.format, "app": self.app, "document": digest(document), "payload": digest(png)})) + .map_err(invalid)?; + let fits = |name: &str, len: usize| len <= (writer.limits.part_limit)(name); + if !fits("cache/thumbnail.json", key.len()) + || !fits("cache/thumbnail.png", png.len()) + || !writer.room(2, key.len().saturating_add(png.len()), budget) + { + return Ok(()); + } + writer.part("cache/thumbnail.json", &key, false)?; + writer.part("cache/thumbnail.png", png, false) + } + + /// The picture, when this build wrote it for exactly the file's own part (`document.json`, + /// read within `document_limit`). + pub fn read(&self, reader: &mut Reader, document_limit: usize) -> Result, FormatError> { + let key: Value = serde_json::from_slice(&reader.limited_part("cache/thumbnail.json", 4096)?).map_err(invalid)?; + let document = reader.document(document_limit)?; + if key.get("format").and_then(Value::as_str) != Some(self.format) + || key.get("app").and_then(Value::as_str) != Some(self.app) + || key.get("document").and_then(Value::as_str) != Some(digest(&document).as_str()) + { + return Err(invalid("thumbnail key")); + } + let png = reader.limited_part("cache/thumbnail.png", self.max_bytes)?; + if !self.valid(&png) || key.get("payload").and_then(Value::as_str) != Some(digest(&png).as_str()) { + return Err(invalid("thumbnail payload")); + } + Ok(png) + } +} + +#[cfg(test)] +#[path = "container_tests.rs"] +mod tests; diff --git a/crates/format/src/container_tests.rs b/crates/format/src/container_tests.rs new file mode 100644 index 000000000..6f9c06bbf --- /dev/null +++ b/crates/format/src/container_tests.rs @@ -0,0 +1,260 @@ +use super::*; +use std::io::Cursor; + +const LIMITS: Limits = Limits { + max_entries: 8, + max_expanded: || 1 << 20, + part_limit: |name| if name.ends_with(".big") { 1 << 20 } else { 1024 }, + level: |_, _| 1, + check_size: |length| if length <= 1 << 20 { Ok(()) } else { Err("too large".into()) }, + cancelled: None, +}; + +/// Parts and blobs written are read back as they were; a blob is stored once, named by its hash, +/// and checked against it. +#[test] +fn parts_and_blobs_read_back() { + let mut writer = Writer::new(LIMITS); + writer.part("document.json", br#"{"a":1}"#, true).unwrap(); + let first = writer.blob(b"payload", "bin", false).unwrap(); + let again = writer.blob(b"payload", "bin", true).unwrap(); + assert_eq!(first, again, "the same payload is one blob"); + assert_eq!(first, Value::String(format!("blobs/{}.bin", digest(b"payload")))); + let bytes = writer.finish().unwrap(); + assert!(is_container(&bytes)); + let mut reader = Reader::new(Cursor::new(bytes), LIMITS).unwrap(); + assert_eq!(reader.part_names().len(), 2); + assert!(reader.has_part("document.json") && !reader.has_part("other.json")); + assert_eq!(&*reader.document(64).unwrap(), br#"{"a":1}"#); + let reference = serde_json::json!({"blob": first}); + assert_eq!(reader.blob(&reference, "blob", "bin").unwrap(), b"payload"); + assert!(reader.blob(&reference, "blob", "png").is_err(), "another extension"); + assert_eq!(reader.hydrated, 7); + reader.hydrated = (LIMITS.max_expanded)(); + assert!(reader.blob(&reference, "blob", "bin").unwrap_err().to_string().contains("resolved payloads too large")); +} + +/// A part past its limit, or more entries than the limit, are refused as they are written. +#[test] +fn budgets_are_kept_when_writing() { + let mut writer = Writer::new(LIMITS); + assert_eq!(writer.part("small.json", &[0; 1025], false), Err(FormatError::Invalid("part too large".into()))); + assert!(writer.part("large.big", &[0; 1025], true).is_ok()); + for i in 0..7 { + writer.part(&format!("{i}.json"), b"x", false).unwrap(); + } + assert_eq!(writer.part("one-more.json", b"x", false), Err(FormatError::Invalid("package too large".into()))); + assert!(!writer.room(1, 0, 1 << 20), "no room for an entry past the limit"); +} + +/// A thumbnail is kept only for the file it shows. +#[test] +fn a_thumbnail_is_keyed_to_its_file() { + let picture = Thumbnail { format: "app-thumbnail/1", app: "1.0", size: [2, 1], max_bytes: 1024 }; + let mut png = b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR".to_vec(); + png.extend_from_slice(&2u32.to_be_bytes()); + png.extend_from_slice(&1u32.to_be_bytes()); + assert!(picture.valid(&png)); + let mut writer = Writer::new(LIMITS); + writer.part("document.json", b"{}", false).unwrap(); + picture.write(&mut writer, &png, b"{}", 8 << 20).unwrap(); + let mut reader = Reader::new(Cursor::new(writer.finish().unwrap()), LIMITS).unwrap(); + assert_eq!(picture.read(&mut reader, 64).unwrap(), png); + let mut writer = Writer::new(LIMITS); + writer.part("document.json", b"{\"changed\":1}", false).unwrap(); + picture.write(&mut writer, &png, b"{}", 8 << 20).unwrap(); + let mut reader = Reader::new(Cursor::new(writer.finish().unwrap()), LIMITS).unwrap(); + assert!(picture.read(&mut reader, 64).is_err(), "stale"); +} + +/// A refused part is not remembered as written: once the budget is raised it is written. +#[test] +fn a_refused_part_can_be_written_later() { + static BUDGET: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(4); + let limits = Limits { max_expanded: || BUDGET.load(std::sync::atomic::Ordering::SeqCst), ..LIMITS }; + let mut writer = Writer::new(limits); + assert_eq!(writer.part("a.json", b"12345678", false), Err(FormatError::Invalid("package too large".into()))); + assert!(writer.blob(b"12345678", "bin", false).is_err()); + BUDGET.store(1 << 20, std::sync::atomic::Ordering::SeqCst); + writer.part("a.json", b"12345678", false).unwrap(); + writer.blob(b"12345678", "bin", false).unwrap(); + let reader = Reader::new(Cursor::new(writer.finish().unwrap()), limits).unwrap(); + assert_eq!(reader.part_names().len(), 2, "both parts were written on the retry"); +} + +/// A cancelled save writes no more parts, whether the part is small or written in pieces. +#[test] +fn cancellation_stops_the_save() { + let cancelled = Limits { cancelled: Some(|| true), ..LIMITS }; + let mut writer = Writer::new(cancelled); + assert_eq!(writer.part("a.json", b"a", false), Err(FormatError::Cancelled)); + // cancelled after the first piece of a long part: checks before the part, then per piece + static CHECKS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + let midway = Limits { + cancelled: Some(|| CHECKS.fetch_add(1, std::sync::atomic::Ordering::SeqCst) >= 2), + max_expanded: || 1 << 22, + part_limit: |_| 1 << 22, + ..LIMITS + }; + let mut writer = Writer::new(midway); + assert_eq!(writer.part("big.big", &vec![0; PIECE + 1], true), Err(FormatError::Cancelled)); + assert_eq!(CHECKS.load(std::sync::atomic::Ordering::SeqCst), 3); + let going = Limits { cancelled: Some(|| false), max_expanded: || 1 << 22, part_limit: |_| 1 << 22, ..LIMITS }; + let mut writer = Writer::new(going); + writer.part("big.big", &vec![7; PIECE + 1], true).unwrap(); + let mut reader = Reader::new(Cursor::new(writer.finish().unwrap()), going).unwrap(); + assert_eq!(reader.limited_part("big.big", 1 << 22).unwrap().len(), PIECE + 1); +} + +/// Room counts what is already written, and the reader keeps every part's configured limit. +#[test] +fn room_counts_written_parts_and_reads_keep_part_limits() { + let mut writer = Writer::new(LIMITS); + writer.part("large.big", &[0; 500 * 1024], false).unwrap(); + assert!(!writer.room(1, 200 * 1024, 600 * 1024)); + assert!(writer.room(1, 1024, 1 << 20)); + // what is written already is past the budget: not even an empty part fits + assert!(!writer.room(1, 0, 400 * 1024)); + assert_eq!(writer.space(1, 400 * 1024), 0); + let wide = Limits { part_limit: |_| 1 << 20, ..LIMITS }; + let mut writer = Writer::new(wide); + writer.part("document.json", &[b' '; 2048], false).unwrap(); + let bytes = writer.finish().unwrap(); + assert!(Reader::new(Cursor::new(bytes.clone()), LIMITS).is_err(), "declared past its configured limit"); + let mut reader = Reader::new(Cursor::new(bytes), wide).unwrap(); + assert!(reader.limited_part("document.json", 4096).is_ok()); +} + +/// A thumbnail past a cache part's own limit is left out, never failing the save. +#[test] +fn a_thumbnail_past_its_part_limit_is_left_out() { + let picture = Thumbnail { format: "app-thumbnail/1", app: "1.0", size: [2, 1], max_bytes: 2048 }; + let mut png = b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR".to_vec(); + png.extend_from_slice(&2u32.to_be_bytes()); + png.extend_from_slice(&1u32.to_be_bytes()); + png.resize(1500, 0); + let mut writer = Writer::new(LIMITS); + picture.write(&mut writer, &png, b"{}", 8 << 20).unwrap(); + let reader = Reader::new(Cursor::new(writer.finish().unwrap()), LIMITS).unwrap(); + assert!(!reader.has_part("cache/thumbnail.json") && !reader.has_part("cache/thumbnail.png")); +} + +/// A source that grows after its length was approved is refused. +#[test] +fn a_source_that_grows_while_read_is_refused() { + struct Growing(Cursor>, bool); + impl Read for Growing { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + if !self.1 { + self.1 = true; + let end = self.0.get_ref().len(); + self.0.get_mut().extend_from_slice(&[0; 16]); + self.0.set_position(self.0.position().min(end as u64)); + } + self.0.read(buf) + } + } + impl Seek for Growing { + fn seek(&mut self, to: SeekFrom) -> std::io::Result { + self.0.seek(to) + } + } + let mut writer = Writer::new(LIMITS); + writer.part("document.json", b"{}", false).unwrap(); + let bytes = writer.finish().unwrap(); + assert!(Reader::new(Growing(Cursor::new(bytes), false), LIMITS).err().is_some_and(|e| e.to_string().contains("changed"))); +} + +/// Compressed parts use the configured Deflate level. +#[test] +fn compressed_parts_use_the_configured_level() { + // words in a varied order, so the levels compress them differently + let words = ["alpha ", "beta ", "gamma ", "delta ", "epsilon ", "zeta ", "eta ", "theta "]; + let mut seed = 7u32; + let text: Vec = (0..150) + .flat_map(|_| { + seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12_345); + words[(seed >> 16) as usize % words.len()].bytes() + }) + .take(1000) + .collect(); + let size = |level: fn(&str, usize) -> i64| { + let mut writer = Writer::new(Limits { level, ..LIMITS }); + writer.part("document.json", &text, true).unwrap(); + writer.finish().unwrap().len() + }; + assert_ne!(size(|_, _| 1), size(|_, _| 9), "the level is used"); + assert_eq!(size(|_, _| 42), size(|_, _| 9), "past 9 is clamped"); + assert_eq!(size(|_, _| -3), size(|_, _| 1), "below 1 is clamped"); +} + +/// An archive naming one part twice is refused. +#[test] +fn a_repeated_part_name_is_refused() { + let mut writer = Writer::new(LIMITS); + writer.part("document.json", b"{}", false).unwrap(); + writer.part("documenX.json", b"[]", false).unwrap(); + let mut bytes = writer.finish().unwrap(); + let (from, to) = (b"documenX.json", b"document.json"); + let mut at = 0; + while let Some(i) = bytes[at..].windows(from.len()).position(|w| w == from) { + bytes[at + i..at + i + to.len()].copy_from_slice(to); + at += i + to.len(); + } + assert!(Reader::new(Cursor::new(bytes), LIMITS).is_err()); +} + +/// Cached documents and blobs keep the current part limits. +#[test] +fn cached_parts_keep_the_current_part_limits() { + let wide = Limits { part_limit: |_| 1 << 20, ..LIMITS }; + let mut writer = Writer::new(wide); + writer.part("document.json", &[b' '; 512], false).unwrap(); + let blob = writer.blob(&[7; 512], "bin", false).unwrap(); + let bytes = writer.finish().unwrap(); + let mut reader = Reader::new(Cursor::new(bytes), wide).unwrap(); + let reference = serde_json::json!({"blob": blob}); + assert!(reader.document(4096).is_ok()); + assert!(reader.blob(&reference, "blob", "bin").is_ok()); + reader.limits = Limits { part_limit: |_| 128, ..wide }; + assert!(reader.document(4096).is_err()); + assert!(reader.limited_blob(&reference, "blob", "bin", 4096).is_err()); +} + +/// A raised expanded budget applies to the open reader, and what was read still counts. +#[test] +fn a_raised_expanded_budget_applies_at_once() { + static BUDGET: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(600); + let limits = Limits { max_expanded: || BUDGET.load(std::sync::atomic::Ordering::Relaxed), part_limit: |_| 1 << 20, ..LIMITS }; + let wide = Limits { max_expanded: || 1 << 20, ..limits }; + let mut writer = Writer::new(wide); + writer.part("document.json", &[b' '; 256], false).unwrap(); + writer.part("other.big", &[1; 512], false).unwrap(); + let bytes = writer.finish().unwrap(); + BUDGET.store(1 << 20, std::sync::atomic::Ordering::Relaxed); + let mut reader = Reader::new(Cursor::new(bytes), limits).unwrap(); + BUDGET.store(600, std::sync::atomic::Ordering::Relaxed); + assert!(reader.limited_part("document.json", 4096).is_ok()); + assert!(reader.limited_part("other.big", 4096).is_err(), "256 read + 512 is past 600"); + BUDGET.store(1024, std::sync::atomic::Ordering::Relaxed); + assert!(reader.limited_part("other.big", 4096).is_ok(), "768 fits the raised 1024"); + // a lowered budget does not forget what was read: 768 + 256 is past 1000 after a dip to 100 + BUDGET.store(100, std::sync::atomic::Ordering::Relaxed); + assert!(reader.limited_part("document.json", 4096).is_err()); + BUDGET.store(1000, std::sync::atomic::Ordering::Relaxed); + assert!(reader.limited_part("document.json", 4096).is_err(), "1024 is past 1000"); + BUDGET.store(1024, std::sync::atomic::Ordering::Relaxed); + assert!(reader.limited_part("document.json", 4096).is_ok()); +} + +/// An entry whose local header can't be read is refused at open, not skipped. +#[test] +fn an_unreadable_entry_is_refused() { + let mut writer = Writer::new(LIMITS); + writer.part("document.json", b"{}", false).unwrap(); + writer.part("other.json", b"[]", false).unwrap(); + let mut bytes = writer.finish().unwrap(); + let second = bytes.windows(4).enumerate().filter(|(_, w)| *w == b"PK\x03\x04").map(|(i, _)| i).nth(1).unwrap(); + bytes[second + 2] = 0; + assert!(Reader::new(Cursor::new(bytes), LIMITS).is_err()); +} diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs new file mode 100644 index 000000000..aadb56fd9 --- /dev/null +++ b/crates/format/src/lib.rs @@ -0,0 +1,257 @@ +//! Versioned file formats: what an app needs to read every file it ever wrote, and to open a file +//! a newer build of it wrote. +//! +//! A file starts with `"format": "N"`. A [`Family`] is one kind of file (an app's designs, +//! its drawings): its tag, its own numbers, and how a file of an older number is brought up to +//! this build's, step by step (N → N+1 → … → current). The app's code only knows the current +//! format; all translation lives in the steps: +//! - the steps written out by hand for the first formats, one per format ([`Formats::hand`]); +//! - then one folder per format, `v/`, one file per change in it ([`Part`]), each with a step +//! when files from before the change are rewritten as they open ([`Formats::folded`]); +//! - then the changes no release has a number for yet, in `pending/` ([`Formats::pending`]): +//! while there are any, the build writes the next number, with their names in the file's +//! `format_pending`, so a later build runs just the steps a file is missing. A release moves +//! them into the next `v/`. [`build`] lists the folders for a build script. +//! +//! A file a newer build wrote says so by a number past this build's or a change name this build +//! doesn't have ([`Family::newer`]); the app then reads what it can and keeps the rest +//! ([`newer`]). A file is saved as JSON, or in a [`container`] with its binary payloads. +//! +//! Steps are JSON-level (the file's shape) and return the app's own error type; every error this +//! crate makes is a [`FormatError`], which the app's error type takes `From`. + +#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))] + +use serde_json::Value; + +pub mod build; +pub mod container; +pub mod newer; + +/// Why a file is refused: its text says what is wrong, as the user reads it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum FormatError { + /// The file isn't one of the family's, or it is damaged. + Invalid(String), + /// The work was cancelled (a save the person stopped): nothing more was written. + Cancelled, +} + +impl std::fmt::Display for FormatError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + FormatError::Invalid(why) => f.write_str(why), + FormatError::Cancelled => f.write_str("cancelled"), + } + } +} + +impl std::error::Error for FormatError {} + +/// One JSON-level upgrade step: rewrites a file in place, or says why it can't. +pub type Step = fn(&mut Value) -> Result<(), E>; + +/// One change to a family's file format: a file in its `v/` or `pending/` folder. +pub struct Part { + /// The file's name, which a file written while the change was pending lists. + pub name: &'static str, + /// How a file from before the change is rewritten as it opens, if it is. + pub step: Option>, +} + +/// Another spelling of a change name that a file of one format may carry (a build that named the +/// change otherwise): read as the change it stands for, never written. +#[derive(Clone, Copy, Debug)] +pub struct Alias { + /// The format whose files may say it. + pub format: u32, + /// The spelling in the file. + pub name: &'static str, + /// The change it stands for. + pub known_as: &'static str, +} + +/// A family's upgrade path: the steps written out by hand, the folded formats after them (each a +/// list of changes) and the pending changes, which make the next format. +pub struct Formats<'a, E> { + /// Entry `i` turns format `i + 1` into `i + 2`. + pub hand: &'a [Step], + /// Each format after the hand-written ones, oldest first, and its changes. + pub folded: &'a [(u32, &'a [Part])], + /// The changes no release has a number for yet. + pub pending: &'a [Part], +} + +/// A kind of file whose format is versioned: its own format tag, its own numbers and its own +/// changes. Reading a file's format, telling a file a newer build saved and the upgrade are the +/// same for every family. +pub struct Family<'a, E> { + /// The app, as messages name it. + pub app: &'static str, + /// What a file of the family is called in messages. + pub noun: &'static str, + /// The tag before the number in a file's `format`. + pub prefix: &'static str, + /// The tag files saved under an older name carry: read, never written. + pub legacy_prefix: Option<&'static str>, + /// The format of a file that names none, if such files exist. + pub untagged: Option, + /// Other spellings of change names that files may carry. + pub aliases: &'a [Alias], + /// The upgrade path. + pub formats: Formats<'a, E>, +} + +/// How a file shows a newer build saved it ([`Family::newer`]). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Newer { + /// The number the file says. + pub format: u32, + /// The number this build reads up to. + pub reads_up_to: u32, + /// The changes the file names that this build doesn't have. + pub unknown_changes: Vec, +} + +impl Formats<'_, E> { + /// The format this build writes: the last folded one, or the next while changes are pending. + pub const fn current(&self) -> u32 { + self.hand.len() as u32 + 1 + self.folded.len() as u32 + if self.pending.is_empty() { 0 } else { 1 } + } + + /// The changes format `version` is made of, if it is made of changes. + fn parts(&self, version: u32) -> Option<&[Part]> { + let after = version.checked_sub(self.hand.len() as u32 + 2)? as usize; + match self.folded.get(after) { + Some((_, parts)) => Some(parts), + None if after == self.folded.len() && !self.pending.is_empty() => Some(self.pending), + None => None, + } + } + + /// Whether a change of this name is anywhere in this build's history. + fn knows_part_name(&self, name: &str) -> bool { + self.folded.iter().flat_map(|(_, parts)| *parts).chain(self.pending).any(|part| part.name == name) + } +} + +impl Family<'_, E> { + /// The format this build writes: the last folded one, or the next while changes are pending. + pub const fn current(&self) -> u32 { + self.formats.current() + } + + /// What a file of the family is called in messages. + pub const fn noun(&self) -> &'static str { + self.noun + } + + /// The names of the pending changes this build writes into a file (`format_pending`). + pub fn pending_names(&self) -> Vec { + self.formats.pending.iter().map(|p| p.name.to_string()).collect() + } + + /// The format tag of format `n`. + pub fn format_string(&self, n: u32) -> String { + format!("{}{n}", self.prefix) + } + + /// The version in one of this family's format tags; `None` for any other tag, another + /// family's included. + pub fn version_of(&self, s: &str) -> Option { + // each tag form parses on its own: a legacy prefix may start with the primary one + // (`app/` and `app/design/`) + let parse = |prefix: &str| s.strip_prefix(prefix).and_then(|n| n.parse().ok()); + parse(self.prefix).or_else(|| self.legacy_prefix.and_then(parse)) + } + + /// The change a name a file of format `version` lists stands for: its alias's, or itself. + /// An alias counts only while this build has the change it stands for, so recognising it + /// never grants support to another reader. + fn canonical_applied_name<'name>(&self, version: u32, name: &'name str) -> &'name str { + match self.aliases.iter().find(|a| a.format == version && a.name == name) { + Some(alias) if self.formats.knows_part_name(alias.known_as) => alias.known_as, + _ => name, + } + } + + /// What says a file of format `version` with the changes `applied` came from a newer build: + /// a number past this build's, or a change this build doesn't have anywhere in its history + /// (a branch's pending change may have landed at a later number). A list beside a format that + /// isn't made of changes means nothing (a tool or a test set an older number on a newer file). + /// `None` for a file this build fully knows. + pub fn newer(&self, version: u32, applied: Option<&[String]>) -> Option { + let current = self.current(); + let listed = applied.filter(|_| version > current || self.formats.parts(version).is_some()).unwrap_or_default(); + let unknown_changes: Vec = + listed.iter().filter(|name| !self.formats.knows_part_name(self.canonical_applied_name(version, name))).cloned().collect(); + (version > current || !unknown_changes.is_empty()).then_some(Newer { format: version, reads_up_to: current, unknown_changes }) + } +} + +impl> Family<'_, E> { + /// Read a file's format and take its `format_pending` out of its JSON, before anything else + /// in it is read: a file that isn't this family's is refused here. One a newer build saved is + /// not: [`Family::newer`] says so. + pub fn take_version(&self, v: &mut Value) -> Result<(u32, Option>), E> { + let (app, noun) = (self.app, self.noun); + let not_ours = |what: String| E::from(FormatError::Invalid(format!("not a {app} {noun} ({what})"))); + let Some(obj) = v.as_object() else { return Err(not_ours("expected a JSON object".into())) }; + let version = match obj.get("format") { + None => self.untagged.ok_or_else(|| not_ours("no format".into()))?, + Some(Value::String(f)) => self.version_of(f).ok_or_else(|| not_ours(format!("format `{f}`")))?, + Some(_) => return Err(not_ours("format is not a string".into())), + }; + let applied = take_names(v, "format_pending", noun).map_err(E::from)?; + if version == 0 { + return Err(not_ours("format 0".into())); + } + Ok((version, applied)) + } + + /// Bring a file of format `version` up to this build's; `applied` is its `format_pending` + /// (both from [`Family::take_version`]): the changes of its format it already has, when it + /// was written while they were pending. The others' steps run first, and already-applied + /// changes never run again at their later number. A file past this build's number has no + /// steps to run. + pub fn upgrade(&self, v: &mut Value, version: u32, applied: Option<&[String]>) -> Result<(), E> { + let formats = &self.formats; + if version == 0 { + return Err(E::from(FormatError::Invalid(format!("not a {} {} (format 0)", self.app, self.noun)))); + } + // A list beside a format that isn't made of changes is ignored, like any field an upgrade + // drops. + let applied = applied.filter(|_| formats.parts(version).is_some()); + let already_applied = + |part: &Part| applied.is_some_and(|names| names.iter().any(|name| self.canonical_applied_name(version, name) == part.name)); + if applied.is_some() { + for part in formats.parts(version).unwrap_or_default().iter().filter(|part| !already_applied(part)) { + part.step.map_or(Ok(()), |step| step(v))?; + } + } + for from in version..self.current() { + match formats.hand.get(from as usize - 1) { + Some(step) => step(v)?, + None => { + for part in formats.parts(from + 1).unwrap_or_default().iter().filter(|part| !already_applied(part)) { + part.step.map_or(Ok(()), |step| step(v))?; + } + } + } + } + Ok(()) + } +} + +/// Take a short list of names out of a file's JSON: its `format_pending` (the pending changes it +/// has). +fn take_names(v: &mut Value, key: &str, noun: &str) -> Result>, FormatError> { + let Some(list) = v.as_object_mut().and_then(|o| o.remove(key)) else { return Ok(None) }; + let invalid = || FormatError::Invalid(format!("damaged {noun}: `{key}` is not a short list of names")); + let list = list.as_array().filter(|l| l.len() <= 1000).ok_or_else(invalid)?; + list.iter().map(|n| n.as_str().filter(|n| n.len() <= 128).map(str::to_string).ok_or_else(invalid)).collect::>().map(Some) +} + +#[cfg(test)] +#[path = "lib_tests.rs"] +mod tests; diff --git a/crates/format/src/lib_tests.rs b/crates/format/src/lib_tests.rs new file mode 100644 index 000000000..40c5101fb --- /dev/null +++ b/crates/format/src/lib_tests.rs @@ -0,0 +1,142 @@ +use super::*; +use serde_json::json; + +/// The app's error, as an app has one. +#[derive(Debug, PartialEq)] +struct AppError(String); + +impl From for AppError { + fn from(e: FormatError) -> Self { + AppError(e.to_string()) + } +} + +/// Each step appends its name to the file's `log`, so the order steps ran in can be read back. +fn log(v: &mut Value, step: &str) { + if let Some(list) = v.get_mut("log").and_then(Value::as_array_mut) { + list.push(json!(step)); + } +} + +const HAND: [Step; 2] = [ + |v| { + log(v, "1to2"); + Ok(()) + }, + |v| { + log(v, "2to3"); + Ok(()) + }, +]; +const V4: [Part; 2] = [ + Part { + name: "a", + step: Some(|v| { + log(v, "a"); + Ok(()) + }), + }, + Part { name: "b", step: None }, +]; +const V5: [Part; 1] = [Part { + name: "c", + step: Some(|v| { + log(v, "c"); + Ok(()) + }), +}]; +const FOLDED: [(u32, &[Part]); 2] = [(4, &V4), (5, &V5)]; +const PENDING: [Part; 2] = [ + Part { + name: "d", + step: Some(|v| { + log(v, "d"); + Ok(()) + }), + }, + Part { + name: "e", + step: Some(|v| { + log(v, "e"); + Ok(()) + }), + }, +]; +const ALIASES: [Alias; 1] = [Alias { format: 5, name: "old-c", known_as: "c" }]; + +/// Format 1 untagged; hand steps to 3; folded 4 and 5; pending changes make 6. +const FAMILY: Family<'static, AppError> = Family { + app: "App", + noun: "design", + prefix: "app/", + legacy_prefix: Some("old/"), + untagged: Some(1), + aliases: &ALIASES, + formats: Formats { hand: &HAND, folded: &FOLDED, pending: &PENDING }, +}; + +fn steps_run(version: u32, applied: Option<&[String]>) -> Vec { + let mut v = json!({"log": []}); + FAMILY.upgrade(&mut v, version, applied).unwrap(); + v["log"].as_array().unwrap().iter().map(|s| s.as_str().unwrap().to_string()).collect() +} + +#[test] +fn tags_and_numbers() { + assert_eq!(FAMILY.current(), 6); + assert_eq!(FAMILY.format_string(6), "app/6"); + assert_eq!(FAMILY.version_of("app/4"), Some(4)); + assert_eq!(FAMILY.version_of("old/3"), Some(3), "the legacy tag is read"); + assert_eq!(FAMILY.version_of("other/3"), None); + assert_eq!(FAMILY.pending_names(), vec!["d".to_string(), "e".to_string()]); +} + +#[test] +fn a_file_is_taken_apart_or_refused_with_the_apps_words() { + let mut v = json!({"format": "app/6", "format_pending": ["d"], "x": 1}); + assert_eq!(FAMILY.take_version(&mut v), Ok((6, Some(vec!["d".to_string()])))); + assert!(v.get("format_pending").is_none(), "taken out"); + assert_eq!(FAMILY.take_version(&mut json!({})), Ok((1, None)), "untagged"); + let refused = |mut v: Value| FAMILY.take_version(&mut v).unwrap_err().0; + assert_eq!(refused(json!([])), "not a App design (expected a JSON object)"); + assert_eq!(refused(json!({"format": "other/2"})), "not a App design (format `other/2`)"); + assert_eq!(refused(json!({"format": 2})), "not a App design (format is not a string)"); + assert_eq!(refused(json!({"format": "app/0"})), "not a App design (format 0)"); + assert_eq!(refused(json!({"format": "app/2", "format_pending": [1]})), "damaged design: `format_pending` is not a short list of names"); +} + +#[test] +fn every_step_runs_once_in_order() { + assert_eq!(steps_run(1, None), ["1to2", "2to3", "a", "c", "d", "e"]); + assert_eq!(steps_run(4, None), ["c", "d", "e"]); + assert_eq!(steps_run(6, None), Vec::::new()); + // A file written while `d` was pending has it: only `e` runs. + assert_eq!(steps_run(6, Some(&["d".to_string()])), ["e"]); + // A file of format 5 written while `c` was still pending, without it: `c` runs first. + assert_eq!(steps_run(5, Some(&[])), ["c", "d", "e"]); + // Under its other spelling `c` counts as had. + assert_eq!(steps_run(5, Some(&["old-c".to_string()])), ["d", "e"]); + // A list beside a hand-written format means nothing. + assert_eq!(steps_run(2, Some(&["d".to_string()])), ["2to3", "a", "c", "d", "e"]); + let mut v = json!({}); + assert_eq!(FAMILY.upgrade(&mut v, 0, None), Err(AppError("not a App design (format 0)".into()))); +} + +#[test] +fn a_newer_file_is_told_by_its_number_or_an_unknown_change() { + assert_eq!(FAMILY.newer(6, None), None); + assert_eq!(FAMILY.newer(6, Some(&["d".to_string()])), None); + assert_eq!(FAMILY.newer(7, None), Some(Newer { format: 7, reads_up_to: 6, unknown_changes: Vec::new() })); + assert_eq!(FAMILY.newer(6, Some(&["z".to_string()])), Some(Newer { format: 6, reads_up_to: 6, unknown_changes: vec!["z".to_string()] })); + assert_eq!(FAMILY.newer(5, Some(&["old-c".to_string()])), None, "an alias of a known change"); + assert_eq!(FAMILY.newer(4, Some(&["old-c".to_string()])), Some(Newer { format: 4, reads_up_to: 6, unknown_changes: vec!["old-c".to_string()] })); + assert_eq!(FAMILY.newer(2, Some(&["z".to_string()])), None, "a list beside a hand-written format means nothing"); +} + +#[test] +fn a_legacy_prefix_that_extends_the_primary_one_is_read() { + let nested = Family { prefix: "app/", legacy_prefix: Some("app/design/"), ..FAMILY }; + assert_eq!(nested.version_of("app/design/3"), Some(3)); + assert_eq!(nested.version_of("app/4"), Some(4)); + assert_eq!(nested.version_of("app/design/x"), None); +} diff --git a/crates/format/src/newer.rs b/crates/format/src/newer.rs new file mode 100644 index 000000000..0a3141606 --- /dev/null +++ b/crates/format/src/newer.rs @@ -0,0 +1,470 @@ +//! A file a newer build saved: it opens, with a warning, and saving it loses nothing. +//! +//! A newer format number, or a change the file names that this build doesn't have, says a newer +//! build saved the file ([`crate::Family::newer`]). It may use nothing this build lacks, so the app +//! reads it as usual. When part of it can't be read, that part stays out of the way and the rest +//! opens: the app reads the elements of its lists it can ([`NewerFile::keep_element`]), then any +//! other top-level section it can't read is left out whole ([`leave_out_unreadable`], +//! [`read_around`]), and a field it doesn't know, which typed reading would drop, is noticed by +//! comparing the file with what was read ([`NewerFile::compare`]). +//! +//! All of these are named in [`NewerFile::not_understood`] and go back into the file when it is +//! saved ([`NewerFile::restore_into`]), as they came. What can't be put back because the file was +//! changed around it is what `restore_into` returns. + +use std::collections::{BTreeMap, HashMap}; +use std::sync::Arc; + +use serde_json::{Map, Value, json}; + +use crate::Newer; + +/// The most sections left out one at a time to find the one a file can't be read with. +const MAX_SECTION_TRIES: usize = 64; + +/// What the file says and holds that this build doesn't read. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct NewerFile { + /// The app, as the notice names it. + pub app: &'static str, + /// The number the file says. + pub format: u32, + /// The number this build reads up to. + pub reads_up_to: u32, + /// The changes the file names that this build doesn't have. + pub unknown_changes: Vec, + /// What this build couldn't read, by name. + pub not_understood: Vec, + /// Fields known records lost in typed reading. + kept: Option, + /// Top-level sections left out whole. + sections: Vec
, + /// Elements left out of a top-level list: its key and the element. + elements: Vec<(String, Value)>, + /// The parts of a container this build has no use for: name and bytes. A blob only one of the + /// kept pieces refers to is one of them. + pub parts: Vec<(String, Arc<[u8]>)>, + /// The file's `container.json`, when this build doesn't know what it says. + pub container: Option, +} + +#[derive(Clone, Debug, PartialEq)] +struct Section { + key: String, + value: Value, + /// This build's record of the section, without the file's: while it is unchanged, the file's + /// goes back. + empty: Option, +} + +/// The fields of a record that typed reading dropped. +#[derive(Clone, Debug, PartialEq)] +enum Kept { + Field(Value), + Fields(BTreeMap), + Items(Vec<(Anchor, Kept)>), +} + +/// Which element of a list some kept fields belong to. +#[derive(Clone, Debug, PartialEq)] +enum Anchor { + Id(u64), + Name(String), + /// Its place, good only while the list keeps its length. + Index { + at: usize, + of: usize, + }, +} + +impl NewerFile { + /// A file of `app` that `newer` says a newer build saved, nothing left out of it yet. + pub fn new(app: &'static str, newer: &Newer) -> NewerFile { + NewerFile { + app, + format: newer.format, + reads_up_to: newer.reads_up_to, + unknown_changes: newer.unknown_changes.clone(), + ..NewerFile::default() + } + } + + /// What to tell the user, whatever else is said. + pub fn notice(&self) -> String { + let (app, reads, format) = (self.app, self.reads_up_to, self.format); + match self.unknown_changes.first() { + Some(change) if format <= reads => { + format!("Saved by a newer {app} (format {format} with `{change}`, which this version doesn't know). If something's missing, update.") + } + _ => format!("Saved by a newer {app} (format {format}, this version reads up to {reads}). If something's missing, update."), + } + } + + /// Every line to report: the notice, then what couldn't be read, by name. + pub fn warnings(&self) -> Vec { + let unread = self.not_understood.iter().map(|what| format!("Not read: {what}. Saving keeps it.")); + std::iter::once(self.notice()).chain(unread).collect() + } + + /// What a reply says about it. + pub fn report(&self) -> Value { + json!({ + "format": self.format, + "reads_up_to": self.reads_up_to, + "unknown_changes": self.unknown_changes, + "not_understood": self.not_understood, + "notice": self.notice(), + }) + } + + /// Whether the next save needs a container to keep what the file holds: plain JSON has no room + /// for parts beside the file's own, nor for the container's own `container.json`. + pub fn needs_container(&self) -> bool { + !self.parts.is_empty() || self.container.is_some() + } + + /// Whether anything kept back names this text (a blob's name in a container). + pub fn mentions(&self, text: &str) -> bool { + fn kept(k: &Kept, text: &str) -> bool { + match k { + Kept::Field(v) => value_mentions(v, text), + Kept::Fields(m) => m.values().any(|x| kept(x, text)), + Kept::Items(l) => l.iter().any(|(_, x)| kept(x, text)), + } + } + self.kept.as_ref().is_some_and(|k| kept(k, text)) + || self.sections.iter().any(|s| value_mentions(&s.value, text)) + || self.elements.iter().any(|(_, v)| value_mentions(v, text)) + } + + /// Say that a section the reader couldn't finish reading is left out. `empty` is what the + /// app writes for the section while it holds nothing, if anything. + pub fn keep_section(&mut self, key: &str, value: Value, empty: Option, why: &str) { + self.not_understood.push(format!("section `{key}` ({why})")); + self.sections.push(Section { key: key.into(), value, empty }); + } + + /// Say that an element of the top-level list `key` is left out, as `note` names it; it goes + /// back at the end of the list when the file is saved. + pub fn keep_element(&mut self, key: &str, element: Value, note: String) { + self.not_understood.push(note); + self.elements.push((key.into(), element)); + } + + /// Compare the file as it reads (`file`, after the same upgrade as what was read) with what was + /// read of it (`known`, its typed record written out): what `known` lacks is kept and named. + /// An object whose `type` is `placeholder_tag` (an element the app kept whole) is not + /// compared; nor are these top-level fields: those in `container_only` (what a container adds + /// beside the file's own), `format` and `format_pending`, and the sections kept whole (they go + /// back whole). + pub fn compare(&mut self, file: &Value, known: &Value, placeholder_tag: &str, container_only: &[&str]) { + let mut paths = Vec::new(); + let mut top_only: Vec<&str> = container_only.to_vec(); + top_only.extend(["format", "format_pending"]); + top_only.extend(self.sections.iter().map(|s| s.key.as_str())); + // a list typed writing leaves out holds only elements kept on their own: not kept twice + top_only.extend(self.elements.iter().map(|(key, _)| key.as_str()).filter(|key| known.get(*key).is_none())); + let kept = vanished(file, known, "", &mut paths, placeholder_tag, &top_only); + self.kept = kept; + self.not_understood.extend(paths); + for section in &mut self.sections { + section.empty = known.get(§ion.key).cloned(); + } + } + + /// Write back what was kept of the file into `value`, the file as this build writes it. + /// Returns what couldn't go back, because the file was changed around it. + pub fn restore_into(&self, value: &mut Value) -> Vec { + let mut lost = Vec::new(); + if let Some(kept) = &self.kept { + apply(kept, value, "", &mut lost); + } + if let Some(top) = value.as_object_mut() { + for section in &self.sections { + if top.get(§ion.key) == section.empty.as_ref() { + top.insert(section.key.clone(), section.value.clone()); + } else { + lost.push(format!("section `{}`, replaced by what was edited here", section.key)); + } + } + // each list's records indexed once, by the identity records are matched by + let mut indexes: HashMap<&str, Positions> = HashMap::new(); + for (key, element) in &self.elements { + let name = element.get("name"); + let anchor = anchor_of(element); + match top.entry(key.clone()).or_insert_with(|| json!([])) { + Value::Array(list) => { + let index = indexes.entry(key.as_str()).or_insert_with(|| Positions::of(list)); + if anchor.as_ref().is_some_and(|a| index.find(list.len(), a).is_some()) { + lost.push(format!( + "`{key}` element {}, replaced by what was made here", + name.and_then(Value::as_str).unwrap_or("unnamed") + )); + } else { + index.add(element, list.len()); + list.push(element.clone()); + } + } + _ => lost.push(format!("`{key}` element {}, replaced by what was made here", name.and_then(Value::as_str).unwrap_or("unnamed"))), + } + } + } + lost + } +} + +/// Whether a string anywhere in `v` is `text`; iterative, so no nesting depth overflows the stack. +fn value_mentions(v: &Value, text: &str) -> bool { + let mut todo = vec![v]; + while let Some(v) = todo.pop() { + match v { + Value::String(s) if s == text => return true, + Value::Array(a) => todo.extend(a), + Value::Object(o) => todo.extend(o.values()), + _ => {} + } + } + false +} + +/// The first line of a reader's error, without the list of every kind it knows, and without the +/// `" file: "` its reader starts it with. +pub fn short<'a>(why: &'a str, noun: &str) -> &'a str { + let why = why.split(", expected").next().unwrap_or(why); + why.strip_prefix(&format!("{noun} file: ")).unwrap_or(why) +} + +/// Leave a section out and keep it. A section the file needs is left empty. +fn move_out(top: &mut Map, key: &str, why: &str, noun: &str, file: &mut NewerFile) { + let Some(value) = top.remove(key) else { return }; + file.not_understood.push(format!("section `{key}` ({})", short(why, noun))); + file.sections.push(Section { key: key.into(), value, empty: None }); +} + +/// Every top-level section of `top` that doesn't read by itself (`piece`, the app's reading of a +/// file holding just that section) is left out and kept in `file`. `noun` is what a file of the +/// app's is called, which its reader's errors start with. +pub fn leave_out_unreadable( + top: &mut Map, + file: &mut NewerFile, + noun: &str, + piece: impl Fn(&str, &Value) -> Result<(), E>, +) { + let keys: Vec = top.keys().cloned().collect(); + for key in &keys { + let value = top.get(key).cloned().unwrap_or_default(); + if let Err(why) = piece(key, &value) { + move_out(top, key, &why.to_string(), noun, file); + } + } +} + +/// Read `top` with `attempt`, leaving out the sections it can't be read with: if it doesn't read, +/// each section but the `core` ones (which the file can't be read without) is tried left out alone, +/// and as a last resort every one of them is. What is left out is kept in `file`. The error is the +/// first attempt's, when even the core alone doesn't read. +pub fn read_around( + top: &mut Map, + file: &mut NewerFile, + core: &[&str], + noun: &str, + attempt: impl Fn(&Map) -> Result, +) -> Result { + let first = match attempt(top) { + Ok(doc) => return Ok(doc), + Err(why) => why.to_string(), + }; + // Sections that are fine by themselves but not together with the rest. + let keys: Vec = top.keys().filter(|k| !core.contains(&k.as_str())).cloned().collect(); + // Each try reads the whole file: a file of many sections goes straight to the last resort. + for key in keys.iter().filter(|_| keys.len() <= MAX_SECTION_TRIES) { + let mut without = top.clone(); + without.remove(key); + if let Ok(doc) = attempt(&without) { + move_out(top, key, &first, noun, file); + return Ok(doc); + } + } + let mut bare = top.clone(); + bare.retain(|key, _| core.contains(&key.as_str())); + match attempt(&bare) { + Ok(doc) => { + for key in &keys { + move_out(top, key, &first, noun, file); + } + Ok(doc) + } + Err(_) => Err(first), + } +} + +/// A value a writer leaves out without losing anything: not worth telling anyone about. +pub fn looks_default(v: &Value) -> bool { + match v { + Value::Null => true, + Value::Bool(b) => !b, + Value::Number(n) => n.as_f64() == Some(0.0), + Value::String(s) => s.is_empty(), + Value::Array(a) => a.is_empty(), + Value::Object(o) => o.is_empty(), + } +} + +/// What `raw` has that `known`, what was read of it, doesn't. `paths` gets those fields' names. +/// `top_only` are top-level keys left out of the comparison. +fn vanished(raw: &Value, known: &Value, path: &str, paths: &mut Vec, placeholder_tag: &str, top_only: &[&str]) -> Option { + match (raw, known) { + (Value::Object(raw), Value::Object(known)) => { + if known.get("type").and_then(Value::as_str) == Some(placeholder_tag) { + return None; + } + let mut kept = BTreeMap::new(); + for (key, value) in raw { + if path.is_empty() && top_only.contains(&key.as_str()) { + continue; + } + let inner = if path.is_empty() { key.clone() } else { format!("{path}.{key}") }; + match known.get(key) { + None => { + if !looks_default(value) { + paths.push(format!("field `{inner}`")); + } + kept.insert(key.clone(), Kept::Field(value.clone())); + } + Some(known) => { + if let Some(k) = vanished(value, known, &inner, paths, placeholder_tag, top_only) { + kept.insert(key.clone(), k); + } + } + } + } + (!kept.is_empty()).then_some(Kept::Fields(kept)) + } + (Value::Array(raw), Value::Array(known)) => { + let mut items = Vec::new(); + let positions = Positions::of(known); + for (at, element) in raw.iter().enumerate() { + // an identity typed reading kept: an id it dropped falls back to the name + let anchor = surviving_anchor(element, &positions).unwrap_or(Anchor::Index { at, of: raw.len() }); + let Some(partner) = positions.find(known.len(), &anchor).and_then(|i| known.get(i)) else { continue }; + let label = match (element.get("name").and_then(Value::as_str), &anchor) { + (Some(name), _) => format!("{path}[{name}]"), + (None, Anchor::Id(id)) => format!("{path}[{id}]"), + _ => format!("{path}[{at}]"), + }; + if let Some(k) = vanished(element, partner, &label, paths, placeholder_tag, top_only) { + items.push((anchor, k)); + } + } + (!items.is_empty()).then_some(Kept::Items(items)) + } + _ => None, + } +} + +fn anchor_of(element: &Value) -> Option { + if let Some(id) = element.get("id").and_then(Value::as_u64) { + return Some(Anchor::Id(id)); + } + element.get("name").and_then(Value::as_str).map(|name| Anchor::Name(name.to_string())) +} + +/// The identity of `element` that `known`'s records still carry: its id, or else its name. When +/// typed reading kept neither, there is none, and the record is matched by its place. +fn surviving_anchor(element: &Value, known: &Positions) -> Option { + let id = element.get("id").and_then(Value::as_u64).map(Anchor::Id); + let name = element.get("name").and_then(Value::as_str).map(|n| Anchor::Name(n.to_string())); + [id, name].into_iter().flatten().find(|anchor| known.find(usize::MAX, anchor).is_some()) +} + +/// Where each id and name first appears in a list, so matching records is not a scan per record. +struct Positions { + ids: HashMap, + names: HashMap, +} + +impl Positions { + fn of(list: &[Value]) -> Positions { + let (mut ids, mut names) = (HashMap::new(), HashMap::new()); + for (i, x) in list.iter().enumerate() { + if let Some(id) = x.get("id").and_then(Value::as_u64) { + ids.entry(id).or_insert(i); + } + if let Some(name) = x.get("name").and_then(Value::as_str) { + names.entry(name.to_string()).or_insert(i); + } + } + Positions { ids, names } + } + + /// Count `x`, now at `i`, in. + fn add(&mut self, x: &Value, i: usize) { + if let Some(id) = x.get("id").and_then(Value::as_u64) { + self.ids.entry(id).or_insert(i); + } + if let Some(name) = x.get("name").and_then(Value::as_str) { + self.names.entry(name.to_string()).or_insert(i); + } + } + + /// The position of the record `anchor` names in a list of `len` records (an index anchor + /// only while the list keeps its length). + fn find(&self, len: usize, anchor: &Anchor) -> Option { + match anchor { + Anchor::Id(id) => self.ids.get(id).copied(), + Anchor::Name(name) => self.names.get(name).copied(), + Anchor::Index { at, of } => (len == *of && at < of).then_some(*at), + } + } +} + +/// Put the kept fields back. An element that is gone was deleted here, its fields with it; one +/// whose place can't be told any more, and a field whose record is gone, go into `lost`. +fn apply(kept: &Kept, value: &mut Value, path: &str, lost: &mut Vec) { + match kept { + Kept::Field(_) => {} + Kept::Fields(fields) => { + let Some(object) = value.as_object_mut() else { + lost.push(format!("field `{path}`")); + return; + }; + for (key, sub) in fields { + let inner = if path.is_empty() { key.clone() } else { format!("{path}.{key}") }; + match (sub, object.get_mut(key)) { + (Kept::Field(v), None) => { + object.insert(key.clone(), v.clone()); + } + // the edited value stays; a retained one it replaced is reported + (Kept::Field(v), Some(existing)) => { + if existing != v { + lost.push(format!("field `{inner}`")); + } + } + (sub, Some(inner_value)) => apply(sub, inner_value, &inner, lost), + // The list it was in is empty and not written: its elements were deleted. + (Kept::Items(_), None) => {} + (_, None) => lost.push(format!("field `{inner}`")), + } + } + } + Kept::Items(items) => { + let Some(list) = value.as_array_mut() else { + lost.push(format!("field `{path}`")); + return; + }; + let positions = Positions::of(list); + let len = list.len(); + for (anchor, sub) in items { + match (positions.find(len, anchor).and_then(|i| list.get_mut(i)), anchor) { + (Some(element), _) => apply(sub, element, path, lost), + (None, Anchor::Index { at, .. }) => lost.push(format!("element {at} of `{path}`")), + (None, _) => {} + } + } + } + } +} + +#[cfg(test)] +#[path = "newer_tests.rs"] +mod tests; diff --git a/crates/format/src/newer_tests.rs b/crates/format/src/newer_tests.rs new file mode 100644 index 000000000..ae228405b --- /dev/null +++ b/crates/format/src/newer_tests.rs @@ -0,0 +1,218 @@ +use super::*; + +fn newer() -> Newer { + Newer { format: 9, reads_up_to: 8, unknown_changes: Vec::new() } +} + +/// What typed reading dropped is named and goes back on saving, where it was. +#[test] +fn dropped_fields_are_named_and_put_back() { + let file = json!({"name": "A", "format": "app/9", "items": [{"id": 1, "size": 2, "colour": "red"}], "extra": {"x": 1}}); + let known = json!({"name": "A", "items": [{"id": 1, "size": 2}]}); + let mut said = NewerFile::new("App", &newer()); + said.compare(&file, &known, "placeholder", &[]); + assert_eq!(said.not_understood, ["field `extra`", "field `items[1].colour`"]); + let mut saved = json!({"name": "A", "items": [{"id": 1, "size": 3}]}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"name": "A", "items": [{"id": 1, "size": 3, "colour": "red"}], "extra": {"x": 1}})); + assert_eq!(said.notice(), "Saved by a newer App (format 9, this version reads up to 8). If something's missing, update."); +} + +/// A section that can't be read is left out, kept, and goes back while it is unchanged here. +#[test] +fn unreadable_sections_are_left_out_and_kept() { + let mut top = match json!({"name": "A", "good": 1, "bad": 2}) { + Value::Object(m) => m, + _ => unreachable!(), + }; + let mut said = NewerFile::new("App", &newer()); + leave_out_unreadable(&mut top, &mut said, "design", |key: &str, _: &Value| if key == "bad" { Err("design file: no") } else { Ok(()) }); + assert!(!top.contains_key("bad")); + assert_eq!(said.not_understood, ["section `bad` (no)"]); + let read = + read_around( + &mut top, + &mut said, + &["name"], + "design", + |top: &Map| { + if top.contains_key("good") { Err("design file: clash") } else { Ok(top.len()) } + }, + ); + assert_eq!(read, Ok(1)); + assert_eq!(said.not_understood, ["section `bad` (no)", "section `good` (clash)"]); + let mut saved = json!({"name": "A"}); + said.compare(&saved.clone(), &saved.clone(), "placeholder", &[]); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"name": "A", "good": 1, "bad": 2})); +} + +/// Notices, warnings and reports name what was left out, and kept values can be searched. +#[test] +fn warnings_report_and_mentions_cover_everything_kept() { + let mut said = NewerFile::new("App", &Newer { format: 8, reads_up_to: 8, unknown_changes: vec!["curves".into()] }); + assert_eq!(said.notice(), "Saved by a newer App (format 8 with `curves`, which this version doesn't know). If something's missing, update."); + assert!(!said.needs_container()); + said.keep_section("plugins", json!({"tool": "ruler"}), Some(json!({})), "unknown"); + said.keep_element("shapes", json!({"name": "star", "points": ["tip"]}), "shape `star`".into()); + let file = json!({"shapes": [{"name": "a", "tags": ["deep"], "more": {"x": "nested"}}], "n": 1}); + said.compare(&file, &json!({"shapes": [{"name": "a"}], "n": 1}), "placeholder", &[]); + assert_eq!(said.warnings()[0], said.notice()); + assert!(said.warnings().contains(&"Not read: section `plugins` (unknown). Saving keeps it.".to_string())); + let report = said.report(); + assert_eq!(report["format"], 8); + assert_eq!(report["unknown_changes"], json!(["curves"])); + assert_eq!(report["notice"], json!(said.notice())); + for text in ["ruler", "tip", "deep", "nested"] { + assert!(said.mentions(text), "{text}"); + } + assert!(!said.mentions("absent")); + said.parts.push(("blob".into(), Arc::from(&b"x"[..]))); + assert!(said.needs_container()); +} + +/// A kept section or element that was replaced here is reported lost, not written over. +#[test] +fn replaced_sections_and_elements_are_reported_lost() { + let mut said = NewerFile::new("App", &newer()); + said.keep_section("plugins", json!({"tool": "ruler"}), None, "unknown"); + said.keep_element("shapes", json!({"name": "star"}), "shape `star`".into()); + said.keep_element("shapes", json!({"points": 3}), "an unnamed shape".into()); + let mut saved = json!({"plugins": {"edited": true}, "shapes": [{"name": "star"}]}); + let lost = said.restore_into(&mut saved); + assert_eq!(lost, ["section `plugins`, replaced by what was edited here", "`shapes` element star, replaced by what was made here"]); + assert_eq!(saved["shapes"], json!([{"name": "star"}, {"points": 3}])); + let mut fresh = json!({}); + said.sections.clear(); + assert!(said.restore_into(&mut fresh).is_empty()); + assert_eq!(fresh["shapes"], json!([{"name": "star"}, {"points": 3}])); +} + +/// Retained container metadata alone needs a container to survive the save. +#[test] +fn container_metadata_alone_needs_a_container() { + let mut said = NewerFile::new("App", &newer()); + assert!(!said.needs_container()); + said.container = Some(json!({"layout": 2})); + assert!(said.needs_container()); +} + +/// An edit that replaces a retained field keeps the edit and reports the replaced value. +#[test] +fn a_replaced_retained_field_is_reported() { + let mut said = NewerFile::new("App", &newer()); + said.compare(&json!({"extra": 1}), &json!({}), "placeholder", &[]); + let mut edited = json!({"extra": 2}); + assert_eq!(said.restore_into(&mut edited), ["field `extra`"]); + assert_eq!(edited, json!({"extra": 2})); + let mut same = json!({"extra": 1}); + assert!(said.restore_into(&mut same).is_empty()); +} + +/// Records are matched by id and by name across long lists, and an index anchor only while the +/// list keeps its length. +#[test] +fn records_match_by_id_name_or_unchanged_index() { + let records: Vec = (0..2000).map(|i| json!({"id": i, "extra": i})).collect(); + let known: Vec = (0..2000).map(|i| json!({"id": i})).collect(); + let mut said = NewerFile::new("App", &newer()); + said.compare(&json!({"items": records}), &json!({"items": known}), "placeholder", &[]); + let mut saved = json!({"items": (0..2000).rev().map(|i| json!({"id": i})).collect::>()}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved["items"][0], json!({"id": 1999, "extra": 1999})); + let mut by_name = NewerFile::new("App", &newer()); + by_name.compare(&json!({"l": [{"name": "a", "x": 1}, [1, 2]]}), &json!({"l": [{"name": "a"}, [1]]}), "placeholder", &[]); + let mut renamed = json!({"l": [{"name": "a"}]}); + by_name.restore_into(&mut renamed); + assert_eq!(renamed["l"][0], json!({"name": "a", "x": 1})); +} + +/// A section kept whole goes back whole, even when this build's record of it has fields. +#[test] +fn a_section_kept_whole_is_not_compared_field_by_field() { + let mut said = NewerFile::new("App", &newer()); + said.keep_section("plugins", json!({"mode": "future", "payload": 1}), None, "unreadable"); + let file = json!({"name": "A", "plugins": {"mode": "future", "payload": 1}}); + said.compare(&file, &json!({"name": "A", "plugins": {"mode": "none"}}), "placeholder", &[]); + let mut saved = json!({"name": "A", "plugins": {"mode": "none"}}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, file); +} + +/// A kept element goes back only while no record has its id or name. +#[test] +fn a_kept_element_keeps_record_identity() { + let mut said = NewerFile::new("App", &newer()); + said.keep_element("items", json!({"id": 7, "kind": "future"}), "element 7".into()); + said.keep_element("items", json!({"kind": "anonymous"}), "unnamed".into()); + let mut clash = json!({"items": [{"id": 7, "kind": "made here"}]}); + assert_eq!(said.restore_into(&mut clash).len(), 1); + assert_eq!(clash, json!({"items": [{"id": 7, "kind": "made here"}, {"kind": "anonymous"}]})); + let mut free = json!({"items": []}); + assert!(said.restore_into(&mut free).is_empty()); + assert_eq!(free, json!({"items": [{"id": 7, "kind": "future"}, {"kind": "anonymous"}]})); +} + +/// Container-only fields are left out at the top level only. +#[test] +fn container_only_fields_are_top_level() { + let mut said = NewerFile::new("App", &newer()); + let file = json!({"thumbnail": "t", "layer": {"thumbnail": "nested"}}); + said.compare(&file, &json!({"layer": {}}), "placeholder", &["thumbnail"]); + assert_eq!(said.not_understood, ["field `layer.thumbnail`"]); + let mut saved = json!({"layer": {}}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"layer": {"thumbnail": "nested"}})); +} + +/// An element kept on its own is not kept again with its list when typed writing leaves the list out. +#[test] +fn a_kept_element_is_not_restored_twice() { + let mut said = NewerFile::new("App", &newer()); + said.keep_element("items", json!({"kind": "future"}), "unnamed".into()); + said.compare(&json!({"items": [{"kind": "future"}]}), &json!({}), "placeholder", &[]); + let mut saved = json!({}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"items": [{"kind": "future"}]})); +} + +/// A record whose id typed reading drops is matched by its name, and its fields go back. +#[test] +fn a_dropped_id_falls_back_to_the_name() { + let mut said = NewerFile::new("App", &newer()); + said.compare(&json!({"items": [{"id": 7, "name": "a", "extra": 1}]}), &json!({"items": [{"name": "a"}]}), "placeholder", &[]); + let mut saved = json!({"items": [{"name": "a"}]}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"items": [{"name": "a", "id": 7, "extra": 1}]})); +} + +/// Kept elements sharing a list are checked against one index, the ones put back included. +#[test] +fn kept_elements_check_each_other() { + let mut said = NewerFile::new("App", &newer()); + said.keep_element("items", json!({"id": 1, "v": "first"}), "1".into()); + said.keep_element("items", json!({"id": 1, "v": "again"}), "1 again".into()); + let mut saved = json!({"items": []}); + assert_eq!(said.restore_into(&mut saved).len(), 1); + assert_eq!(saved, json!({"items": [{"id": 1, "v": "first"}]})); + // deep nesting is searched without recursion + let mut deep = json!("needle"); + for _ in 0..20_000 { + deep = Value::Array(vec![deep]); + } + said.keep_section("deep", deep, None, "test"); + assert!(said.mentions("needle")); + // serde_json drops deep values recursively; leak it rather than overflow the test thread + std::mem::forget(said); +} + +/// A record whose whole identity typed reading drops is matched by its place while the list keeps +/// its length, so an unchanged save keeps its fields. +#[test] +fn a_record_without_a_surviving_identity_is_matched_by_place() { + let mut said = NewerFile::new("App", &newer()); + said.compare(&json!({"items": [{"id": 7, "extra": 1}]}), &json!({"items": [{}]}), "placeholder", &[]); + let mut saved = json!({"items": [{}]}); + assert!(said.restore_into(&mut saved).is_empty()); + assert_eq!(saved, json!({"items": [{"id": 7, "extra": 1}]})); +} diff --git a/crates/heif/Cargo.toml b/crates/heif/Cargo.toml index a5734e611..9cd59453f 100644 --- a/crates/heif/Cargo.toml +++ b/crates/heif/Cargo.toml @@ -19,6 +19,9 @@ heic-rs = { version = "=0.1.4", default-features = false, features = ["std"], op heic-rs = { version = "=0.1.4", default-features = false, features = ["std", "parallel"], optional = true } rayon = { workspace = true, optional = true } +[dev-dependencies] +proptest = { workspace = true } + [features] default = ["decode"] # The decoder (probe, decode, decode_thumbnail, read_metadata): heic-rs for the container and HEVC. diff --git a/crates/heif/README.md b/crates/heif/README.md index d5adf6fdd..6842dcc0d 100644 --- a/crates/heif/README.md +++ b/crates/heif/README.md @@ -36,11 +36,24 @@ Errors are `Error::Unsupported` (image sequences, overlays, 4:2:2/4:4:4, …), ` `Error::Malformed`. It never panics: every call into heic-rs runs under `catch_unwind`, and a panic inside it (PhotoCraft's fuzzing found two in 0.1.1) becomes `Error::Malformed`. -Features: `decode` (default) is the decoder above. `metadata` adds `craft_heif::items::read`: the -first `Exif` item and XMP packet from the box structure alone (`iinf`, `iloc`, `idat`; at most 4096 -boxes, 1024 items, 1024 extents per item, 32 MiB per item), without heic-rs. A build with +Features: `decode` (default) is the decoder above. `metadata` adds `craft_heif::items`, which reads +the `Exif` and XMP items from the box structure alone (`pitm`, `iinf`, `iref`, `iloc`, `idat`), +without heic-rs: + +- `items::read` returns the photo's own EXIF and XMP: the items `cdsc` ties to the primary image or + to an image it is made from (`dimg`: grid tiles, `iden`, `iovl`, `tmap`), else those tied to no + image. A thumbnail's or depth map's metadata is not the photo's. +- `items::scan` returns every EXIF and XMP item, whichever image it describes, and says when it + can't be sure it saw them all: `Scan::complete` is false for an incomplete or damaged item + catalog, and an item whose bytes can't be read comes back as `Unreadable`. An app that must + prove a file has no location (before an export that removes it) refuses when unsure. +- `items::exif_tiff` finds the TIFF stream in an `Exif` item, with or without a correct offset + field or an `Exif\0\0` prefix. + +The reader walks at most 65,536 boxes and walks `iloc` once. It gathers at most 16 MiB per item, +and a scan gathers at most 64 MiB in all. A build with `default-features = false, features = ["metadata"]` has no heic-rs dependency, and its -`read_metadata` is that reader. With both features, a test checks that the two readers agree. +`read_metadata` is `items::read`. With both features, a test checks that the two readers agree. Feature `testdata` exposes `craft_heif::testdata`: lossless synthetic HEIF files (PCM-coded 32 × 32 HEVC pictures, grids, every property above) for the tests of the crates above. diff --git a/crates/heif/src/items.rs b/crates/heif/src/items.rs index 7d4ffe0dc..e0b2d2c93 100644 --- a/crates/heif/src/items.rs +++ b/crates/heif/src/items.rs @@ -1,223 +1,688 @@ //! HEIF / HEIC / AVIF metadata from the box structure alone (feature `metadata`): the `Exif` and -//! XMP items of the ISO base media file container (box structure from ISO/IEC 14496-12, item model -//! from ISO/IEC 23008-12). It needs no HEVC decoder and no heic-rs, so a build that only catalogues +//! XMP items of the image file format built on the ISO base media file format (ISO/IEC 23008-12 +//! on ISO/IEC 14496-12). It needs no HEVC decoder and no heic-rs, so a build that only catalogues //! photos can read their capture time, camera and lens. //! -//! The `meta` box holds an item information box (`iinf`, one `infe` per item) naming each item's -//! type, and an item location box (`iloc`) saying where its bytes are: in the file, or in the -//! `idat` box next to it. The `Exif` item is a four-byte offset followed by a TIFF stream; the XMP -//! packet is a `mime` item whose content type is `application/rdf+xml`. The first such item counts -//! (the decoder's [`crate::read_metadata`] takes the primary image's instead). Moved from LightCraft's -//! `lightcraft-meta`. +//! - `ftyp` names a HEIF or AVIF brand ([`is_heif`]). +//! - The top-level `meta` box lists the items (`iinf`, one `infe` each), says where each item's +//! bytes are (`iloc`: extents in the file, construction method 0, or in the `meta` box's own +//! `idat`, method 1), which item is the photo (`pitm`) and how items relate (`iref`). +//! - EXIF is an item of type `Exif`: a 4-byte big-endian offset to the TIFF header, then the +//! payload (ISO/IEC 23008-12 annex A). iPhones write offset 6 and an `Exif\0\0` prefix; libheif +//! and avif-serialize write offset 0 and the TIFF stream ([`exif_tiff`]). +//! - XMP is an item of type `mime` with content type `application/rdf+xml`. +//! - A `cdsc` ("content describes") reference ties a metadata item to the image it describes. The +//! photo's items are those tied to the primary image or to an image it is made from (its `dimg` +//! inputs: a grid's tiles, the picture an `iden` turns, an `iovl` overlay's or a `tmap` tone +//! map's inputs), else those tied to no image (they describe the file). An item tied only to +//! other images (a thumbnail, a depth map, another photo of a collection) is not this photo's. +//! +//! [`read`] returns the photo's own EXIF and XMP, as the decoder's [`crate::read_metadata`] does. +//! [`scan`] returns every EXIF and XMP item instead, whichever image it describes, and says when +//! it can't be sure it saw them all ([`Scan::complete`], [`Unreadable`]). An app that must prove a +//! file holds no location before copying it out (export's "remove location") refuses when unsure. +//! +//! The ICC profile is left to the decoder, which reads it for rendering. Every size and offset is +//! checked against the buffer; the boxes read, the items considered and the bytes gathered are +//! bounded, and `iloc` is walked once. Moved from LightCraft's `lightcraft-meta`. +#![deny(clippy::indexing_slicing, clippy::arithmetic_side_effects, clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use std::collections::{HashMap, HashSet}; use crate::{Error, SEQUENCE}; -/// The Exif and XMP blocks found. -#[derive(Default)] -struct Embedded { - exif: Option>, - xmp: Option, -} +/// The `ftyp` brands of HEIF still images and image sequences, and of AVIF. +const IMAGE_BRANDS: [&[u8; 4]; 12] = [b"mif1", b"msf1", b"heic", b"heix", b"hevc", b"hevx", b"heim", b"heis", b"hevm", b"hevs", b"avif", b"avis"]; +/// Brands of other formats in the same container (Canon CR3, JPEG XL), which win when listed too. +const OTHER_BRANDS: [&[u8; 4]; 2] = [b"crx ", b"jxl "]; -/// The first `Exif` item's TIFF stream (without HEIF's offset header) and the first XMP packet of a -/// HEIF file, as [`crate::read_metadata`] returns them. An error when the file is not an ISO base -/// media still image ([`is_heif`]), or has no `meta` box (an image sequence); an item that can't be -/// located is left out rather than reported. -pub fn read(bytes: &[u8]) -> Result<(Option>, Option), Error> { - if !is_heif(bytes) { - return Err(Error::Malformed("not a HEIF file".into())); - } - let mut budget = MAX_BOXES; - if !boxes(bytes, 0, bytes.len(), &mut budget).iter().any(|b| &b.kind == b"meta") { - return Err(Error::Unsupported(SEQUENCE)); - } - let e = metadata(bytes); - Ok((e.exif, e.xmp)) -} - -/// At most this many boxes are walked in one file. -pub const MAX_BOXES: usize = 4096; - -/// Brands that declare a still image in the ISO base media container. -const IMAGE_BRANDS: [&[u8]; 12] = [b"heic", b"heix", b"hevc", b"hevx", b"heim", b"heis", b"hevm", b"hevs", b"mif1", b"msf1", b"avif", b"avis"]; - -/// Longest `ftyp` box this reads brands from. +/// How far into the `ftyp` box brands are looked for. pub const MAX_FTYP: usize = 4096; -/// Most items one file's `iinf` / `iloc` describes (an iPhone HEIC has a handful). -pub const MAX_ITEMS: usize = 1024; -/// Most extents one item's bytes are split into. -pub const MAX_EXTENTS: u16 = 1024; -/// Largest metadata item this returns; a real photo's Exif and XMP are a few hundred kilobytes. -pub const MAX_ITEM: usize = 32 << 20; +/// Boxes read in one file. An iPhone photo has about a hundred items, a large grid a few hundred. +pub const MAX_BOXES: usize = 1 << 16; +/// Metadata items [`read`] considers for the photo's own EXIF and XMP (a real file has one or two). +pub const MAX_CANDIDATES: usize = 64; +/// Metadata items [`scan`] returns; a file with more is not [`Scan::complete`]. +pub const MAX_SCANNED: usize = 1024; +/// The most bytes one metadata item may gather (EXIF and XMP are kilobytes). +pub const MAX_ITEM: usize = 16 << 20; +/// The most bytes [`scan`] gathers over all items. +pub const MAX_SCANNED_BYTES: usize = 64 << 20; -/// Does this file declare an ISO base media still image (HEIF / HEIC / AVIF)? +/// Does the `ftyp` box name a HEIF or AVIF brand (and not CR3 or JPEG XL)? pub fn is_heif(bytes: &[u8]) -> bool { if bytes.get(4..8) != Some(b"ftyp".as_slice()) { return false; } - let Some(size) = be32(bytes, 0).map(|v| v as usize) else { - return false; - }; - let end = size.min(bytes.len()).min(MAX_FTYP); - let brand = |at: usize| bytes.get(at..at.saturating_add(4)).is_some_and(|b| IMAGE_BRANDS.contains(&b)); - // the major brand, then (after the minor version) the compatible brands - brand(8) || (16..end).step_by(4).any(brand) + // major brand at 8, minor version at 12, compatible brands from 16 to the end of the box + let size = be32(bytes, 0).and_then(|s| usize::try_from(s).ok()).unwrap_or(0); + let end = size.clamp(12, MAX_FTYP).min(bytes.len()); + let compatible = (16..end).step_by(4).filter_map(|at| bytes.get(at..at.checked_add(4).filter(|&e| e <= end)?)); + let mut heif = false; + for brand in bytes.get(8..12).into_iter().chain(compatible) { + if OTHER_BRANDS.iter().any(|b| b.as_slice() == brand) { + return false; + } + heif |= IMAGE_BRANDS.iter().any(|b| b.as_slice() == brand); + } + heif } -/// The Exif and XMP blocks of a HEIF file. Anything malformed is left out rather than reported: -/// a file whose items can't be located simply has no metadata, as before this reader existed. -fn metadata(bytes: &[u8]) -> Embedded { - let mut out = Embedded::default(); - let mut budget = MAX_BOXES; - let Some(meta) = boxes(bytes, 0, bytes.len(), &mut budget).into_iter().find(|b| &b.kind == b"meta") else { - return out; +/// The photo's EXIF (its TIFF stream, see [`exif_tiff`]) and XMP packet, as +/// [`crate::read_metadata`] returns them: the primary image's items first, then those tied to no +/// image; the first that reads well wins. An error when the file is not HEIF ([`is_heif`]) or has +/// no `meta` box (an image sequence); an item that can't be read is left out rather than reported. +pub fn read(bytes: &[u8]) -> Result<(Option>, Option), Error> { + let c = Container::read(bytes, MAX_CANDIDATES).ok_or_else(not_heif)?; + if !c.has_meta { + return Err(Error::Unsupported(SEQUENCE)); + } + let ranked = |kind: Kind| { + let mut order: Vec<(u8, usize)> = + c.items.iter().enumerate().filter(|(_, it)| it.kind == kind).filter_map(|(i, it)| Some((it.rank(c.primary.is_some())?, i))).collect(); + order.sort_by_key(|&(rank, _)| rank); + order.into_iter().filter_map(|(_, i)| match c.gather(i, MAX_ITEM) { + Gathered::Bytes(b) => Some(b), + _ => None, + }) }; - // `meta` is a FullBox: a version and flags before its children - let (mut items, mut locs, mut idat) = (Vec::new(), Vec::new(), None); - for b in boxes(bytes, meta.body.saturating_add(4), meta.end, &mut budget) { - match &b.kind { - b"iinf" => items = item_infos(bytes, &b, &mut budget), - b"iloc" => locs = item_locations(bytes.get(b.body..b.end).unwrap_or_default()), - b"idat" => idat = bytes.get(b.body..b.end), - _ => {} + let exif = ranked(Kind::Exif).find_map(|item| exif_tiff(&item).map(<[u8]>::to_vec)); + let xmp = ranked(Kind::Xmp).next().map(|item| String::from_utf8_lossy(&item).trim_end_matches('\0').to_string()); + Ok((exif, xmp)) +} + +/// Every EXIF and XMP item of a HEIF file, whichever image it describes, in `iinf` order. An error +/// only when the file is not HEIF ([`is_heif`]); a file without a `meta` box has no items. +/// +/// The scan says when it may be missing something: [`Scan::complete`] is false when the item +/// catalog can't be read in full, and an item whose bytes can't be read comes back as +/// [`Unreadable`]. An item a complete `iloc` doesn't place is left out: it has no bytes. +pub fn scan(bytes: &[u8]) -> Result, Error> { + scan_within(bytes, MAX_SCANNED_BYTES) +} + +/// [`scan`], gathering at most `budget` bytes over all items. +fn scan_within(bytes: &[u8], budget: usize) -> Result, Error> { + let container = Container::read(bytes, MAX_SCANNED).ok_or_else(not_heif)?; + Ok(Scan { container, next: 0, budget }) +} + +fn not_heif() -> Error { + Error::Malformed("not a HEIF file".into()) +} + +/// The metadata items of a HEIF file, from [`scan`]: an iterator over [`Item`]s. +pub struct Scan<'a> { + container: Container<'a>, + next: usize, + /// Bytes still to gather, of [`MAX_SCANNED_BYTES`]. + budget: usize, +} + +impl Scan<'_> { + /// Whether the iterator returns every metadata item in the file: the boxes up to and through + /// the item catalog were read to their ends, there was one `meta` box with one `iinf`, every + /// catalog entry was understood, and there were at most [`MAX_SCANNED`] metadata items. A + /// complete file without a `meta` box is complete, with no items. + pub fn complete(&self) -> bool { + self.container.catalog_complete && !self.container.more + } +} + +impl Iterator for Scan<'_> { + type Item = Item; + + fn next(&mut self) -> Option { + loop { + let i = self.next; + let (id, kind) = self.container.items.get(i).map(|c| (c.id, c.kind))?; + self.next = i.saturating_add(1); + let data = match self.container.gather(i, MAX_ITEM.min(self.budget)) { + Gathered::Bytes(b) => { + self.budget = self.budget.saturating_sub(b.len()); + Ok(b) + } + Gathered::NotPlaced => continue, + Gathered::Unreadable => Err(Unreadable::Placement), + Gathered::TooLarge => Err(Unreadable::TooLarge), + Gathered::Protected => Err(Unreadable::Protected), + }; + return Some(Item { id, kind, data }); } } - for (id, kind, content_type) in items { - let want_exif = &kind == b"Exif" && out.exif.is_none(); - let want_xmp = &kind == b"mime" && content_type == "application/rdf+xml" && out.xmp.is_none(); - if !want_exif && !want_xmp { - continue; - } - let Some(loc) = locs.iter().find(|l| l.id == id) else { continue }; - let Some(block) = item_bytes(bytes, idat, loc) else { continue }; - if want_exif { - // ISO/IEC 23008-12 A.2.1: a four-byte offset from the end of itself to the TIFF header - let skip = be32(&block, 0).and_then(|v| usize::try_from(v).ok()).and_then(|v| v.checked_add(4)); - out.exif = skip.and_then(|at| block.get(at..)).filter(|b| !b.is_empty()).map(<[u8]>::to_vec); +} + +/// One metadata item from [`scan`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Item { + /// Its item id. + pub id: u32, + pub kind: Kind, + /// Its bytes as stored (for EXIF, with the offset field: see [`exif_tiff`]), or why there are + /// none. + pub data: Result, Unreadable>, +} + +/// What a metadata item holds. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum Kind { + /// An `Exif` item. + Exif, + /// An XMP packet: a `mime` item of type `application/rdf+xml`. + Xmp, + /// An XMP packet with a content encoding (compressed). Its bytes are returned as stored; this + /// crate doesn't decode them. + EncodedXmp, +} + +/// Why a metadata item's bytes can't be read. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum Unreadable { + /// `iloc` is missing or damaged before the item's entry, or the entry points outside the file + /// (or `idat`), into another file, or builds the item from other items (method 2). + Placement, + /// More than [`MAX_ITEM`] bytes, or more than [`scan`] has left of [`MAX_SCANNED_BYTES`]. + TooLarge, + /// The item is protected (encrypted): its `infe` entry names a protection scheme, so its + /// stored bytes are not the metadata in the clear. + Protected, +} + +/// The TIFF stream of an `Exif` item: past its 4-byte offset field and the bytes that offset +/// skips. An item whose offset is wrong, or that has no offset field, is read too when an +/// `Exif\0\0` prefix or the TIFF header itself shows where the stream starts. `None` when no TIFF +/// header is found. +pub fn exif_tiff(item: &[u8]) -> Option<&[u8]> { + let tiff = |b: &&[u8]| b.starts_with(b"II*\0") || b.starts_with(b"MM\0*"); + let stated = be32(item, 0).and_then(|n| usize::try_from(n).ok()?.checked_add(4)).and_then(|at| item.get(at..)); + let after_field = item.get(4..).map(strip_exif_header); + [stated, after_field, Some(strip_exif_header(item))].into_iter().flatten().find(tiff) +} + +/// `b` without a leading `Exif\0` and the byte after it (the JPEG APP1 prefix), if it has one. +fn strip_exif_header(b: &[u8]) -> &[u8] { + match b.get(..5) { + Some(b"Exif\0") => b.get(6..).unwrap_or(b), + _ => b, + } +} + +/// A metadata item and the images it describes. +#[derive(Debug)] +struct Candidate { + id: u32, + kind: Kind, + /// Tied to some image by a `cdsc` reference. + describes: bool, + /// Tied to the primary image or an image it is made from. + describes_primary: bool, + /// Protected (encrypted): there, but not readable. + protected: bool, +} + +impl Candidate { + /// 0 for the photo's own, 1 for one tied to no image (or any, when the file names no primary + /// image that exists); `None` for one that describes other images only. + fn rank(&self, has_primary: bool) -> Option { + if self.describes_primary { + Some(0) + } else if !self.describes || !has_primary { + Some(1) } else { - out.xmp = Some(String::from_utf8_lossy(&block).trim_end_matches('\0').to_string()); + None } } - out } -/// `iinf` → one `(item_ID, item_type, content_type)` per `infe` entry. Entries of version 0 and 1 -/// name no item type and are skipped; HEIF files use version 2 or 3. -fn item_infos(bytes: &[u8], iinf: &BoxRef, budget: &mut usize) -> Vec<(u32, [u8; 4], String)> { - // FullBox version and flags, then a 16- or 32-bit entry count before the `infe` boxes - let version = *bytes.get(iinf.body).unwrap_or(&0); - let entries_at = iinf.body.saturating_add(if version == 0 { 6 } else { 8 }); - let mut out = Vec::new(); - for b in boxes(bytes, entries_at, iinf.end, budget) { - if &b.kind != b"infe" || out.len() >= MAX_ITEMS { - continue; +/// What the `meta` box says about the metadata items, and where their bytes are. +struct Container<'a> { + file: &'a [u8], + /// A top-level `meta` box was found. + has_meta: bool, + idat: Option<&'a [u8]>, + iloc: Option>, + /// The primary image, when `pitm` names an item `iinf` lists. + primary: Option, + /// The EXIF and XMP items, in `iinf` order (the first `cap` of them). + items: Vec, + /// More metadata items than `cap`. + more: bool, + /// Discovery read the whole item catalog, including each entry's type. + catalog_complete: bool, + /// Where each of `items` is (from one walk of `iloc`). + places: Vec>, + /// A complete `iloc` walk established that any unplaced item has no entry. + iloc_complete: bool, +} + +impl<'a> Container<'a> { + /// `None` when `bytes` is not HEIF; no items when there is no `meta` box. + fn read(bytes: &'a [u8], cap: usize) -> Option> { + if !is_heif(bytes) { + return None; } - let Some(e) = bytes.get(b.body..b.end) else { continue }; - let (id, kind_at) = match *e.first().unwrap_or(&0) { - 2 => (be16(e, 4).map(u32::from), 8), - 3 => (be32(e, 4), 10), + let mut c = Container { + file: bytes, + has_meta: false, + idat: None, + iloc: None, + primary: None, + items: Vec::new(), + more: false, + catalog_complete: false, + places: Vec::new(), + iloc_complete: false, + }; + let mut budget = MAX_BOXES; + let top = boxes(bytes, 0, bytes.len(), &mut budget); + c.catalog_complete = top.last().is_some_and(|b| b.end == bytes.len()); + // A complete file without an item `meta` has no item catalog (e.g. a sequence). + let Some(meta) = top.iter().find(|b| &b.kind == b"meta") else { return Some(c) }; + c.has_meta = true; + // `meta` is a full box: version and flags, then the boxes inside it. + let first = meta.body.saturating_add(4); + let inside = boxes(bytes, first, meta.end, &mut budget); + c.catalog_complete &= version(bytes, meta) == Some(0) + && first <= meta.end + && inside.last().map_or(first, |b| b.end) == meta.end + && top.iter().filter(|b| &b.kind == b"meta").count() == 1 + && inside.iter().filter(|b| &b.kind == b"iinf").count() == 1; + let child = |kind: &[u8; 4]| inside.iter().find(|b| &b.kind == kind); + let payload = |kind: &[u8; 4]| child(kind).and_then(|b| bytes.get(b.body..b.end)); + let mut ids = Vec::new(); + if let Some(iinf) = child(b"iinf") { + let catalog = item_infos(bytes, iinf, cap, &mut budget); + ids = catalog.ids; + c.items = catalog.items; + c.more = catalog.more; + c.catalog_complete &= catalog.complete; + } + c.primary = payload(b"pitm").and_then(primary_item).filter(|p| ids.binary_search(p).is_ok()); + if let Some(iref) = child(b"iref") { + link(bytes, iref, c.primary, &mut c.items, &mut budget); + } + c.idat = payload(b"idat"); + c.iloc = payload(b"iloc").and_then(Iloc::parse); + (c.places, c.iloc_complete) = match &c.iloc { + Some(iloc) => iloc.locate(&c.items), + None => (Vec::new(), false), + }; + Some(c) + } + + /// The bytes of `items[i]`, at most `limit` of them. + fn gather(&self, i: usize, limit: usize) -> Gathered { + if self.items.get(i).is_some_and(|c| c.protected) { + return Gathered::Protected; + } + match (&self.iloc, self.places.get(i)) { + (Some(iloc), Some(Some(place))) => iloc.gather(self.file, self.idat, place, limit), + _ if self.iloc_complete => Gathered::NotPlaced, + _ => Gathered::Unreadable, + } + } +} + +/// An item's bytes, or why there are none. +#[derive(Debug)] +enum Gathered { + Bytes(Vec), + /// A complete `iloc` walk found no entry for the item. + NotPlaced, + /// Placement unresolved, outside the data, in another file, or built from other items (method 2). + Unreadable, + /// More than the limit. + TooLarge, + /// A protected (encrypted) item. + Protected, +} + +/// A full box's version: the first byte of its own payload (`None` for an empty box). +fn version(bytes: &[u8], b: &BoxRef) -> Option { + bytes.get(b.body..b.end)?.first().copied() +} + +/// `pitm`: the primary item's id (16 bits in version 0, else 32). +fn primary_item(pitm: &[u8]) -> Option { + match pitm.first()? { + 0 => be16(pitm, 4).map(u32::from), + _ => be32(pitm, 4), + } +} + +/// What `iinf` lists. Best-effort discovery is kept even when the catalog cannot establish that +/// all items were read. +#[derive(Default)] +struct ItemCatalog { + /// Every item id listed, sorted. + ids: Vec, + items: Vec, + more: bool, + complete: bool, +} + +/// `iinf` → its item ids and its metadata items (at most `cap`). The catalog is complete when the +/// table version is known, its count matches its entries, the entries fill the box, and each is +/// an `infe` this reader understands. +fn item_infos(bytes: &[u8], iinf: &BoxRef, cap: usize, budget: &mut usize) -> ItemCatalog { + let mut catalog = ItemCatalog::default(); + let Some(data) = bytes.get(iinf.body..iinf.end) else { return catalog }; + // Only the supported versions establish the count and where the entries start. + let (count, header) = match version(bytes, iinf) { + Some(0) => (be16(data, 4).map(u32::from), 6), + Some(1) => (be32(data, 4), 8), + _ => return catalog, + }; + let Some(count) = count else { return catalog }; + let first = iinf.body.saturating_add(header); + let entries = boxes(bytes, first, iinf.end, budget); + catalog.complete = entries.last().map_or(first, |b| b.end) == iinf.end && u32::try_from(entries.len()).ok() == Some(count); + for b in &entries { + let info = bytes.get(b.body..b.end).filter(|_| &b.kind == b"infe").and_then(item_info); + let Some((id, kind, protected)) = info else { + catalog.complete = false; + continue; + }; + catalog.ids.push(id); + match kind { + Some(_) if catalog.items.len() >= cap => catalog.more = true, + Some(kind) => catalog.items.push(Candidate { id, kind, describes: false, describes_primary: false, protected }), + None => {} + } + } + catalog.ids.sort_unstable(); + catalog.ids.dedup(); + catalog +} + +/// An `infe` entry's item id, its kind when it is an `Exif` or XMP item (`mime`, +/// `application/rdf+xml`), and whether it is protected (encrypted). A protected entry still names +/// its type in the clear, so protected metadata is found too. `None` when the entry can't be read +/// to its last field. +fn item_info(infe: &[u8]) -> Option<(u32, Option, bool)> { + let version = *infe.first()?; + // version and flags, the item id (32 bits in version 3), the protection index + let (id, at) = match version { + 0..=2 => (u32::from(be16(infe, 4)?), 6usize), + 3 => (be32(infe, 4)?, 8usize), + _ => return None, + }; + let protected = be16(infe, at)? != 0; + let at = at.checked_add(2)?; + // versions 0 and 1 have no item type and describe MIME items; 2 and 3 name the type + let (kind, at) = if version >= 2 { (infe.get(at..at.checked_add(4)?)?, at.checked_add(4)?) } else { (b"mime".as_slice(), at) }; + let (_name, at) = string(infe, at)?; + let kind = match kind { + b"Exif" => Some(Kind::Exif), + b"mime" => { + let (content_type, at) = string(infe, at)?; + // Content encoding may be absent, but a present string must terminate. + let encoding = if at == infe.len() { b"".as_slice() } else { string(infe, at)?.0 }; + match (content_type.trim_ascii().eq_ignore_ascii_case(b"application/rdf+xml"), encoding.trim_ascii().is_empty()) { + (true, true) => Some(Kind::Xmp), + (true, false) => Some(Kind::EncodedXmp), + (false, _) => None, + } + } + b"uri " => { + string(infe, at)?; + None + } + _ => None, + }; + Some((id, kind, protected)) +} + +/// The NUL-terminated string at `at` and where the next field starts. +fn string(b: &[u8], at: usize) -> Option<(&[u8], usize)> { + let rest = b.get(at..)?; + let n = rest.iter().position(|&c| c == 0)?; + Some((rest.get(..n)?, at.checked_add(n)?.checked_add(1)?)) +} + +/// Mark the candidates that `cdsc` references tie to an image, and those tied to the primary image +/// or one it is made from (`dimg`, followed to any depth; each image once, so loops end). +fn link(bytes: &[u8], iref: &BoxRef, primary: Option, items: &mut [Candidate], budget: &mut usize) { + // full box; item ids are 16 bits in version 0, 32 in version 1 + let width = match version(bytes, iref) { + Some(0) => 2usize, + Some(1) => 4, + _ => return, + }; + let index: HashMap = items.iter().enumerate().rev().map(|(i, c)| (c.id, i)).collect(); + let mut inputs: HashMap> = HashMap::new(); + let mut described: Vec<(usize, Vec)> = Vec::new(); + for r in boxes(bytes, iref.body.saturating_add(4), iref.end, budget) { + let cdsc = match &r.kind { + b"cdsc" => true, + b"dimg" => false, _ => continue, }; - let (Some(id), Some(kind)) = (id, e.get(kind_at..kind_at + 4)) else { continue }; - let mut kind4 = [0u8; 4]; - kind4.copy_from_slice(kind); - // the item name, then (for a `mime` item) the content type, both NUL-terminated - let mut strings = e.get(kind_at + 4..).unwrap_or_default().split(|&c| c == 0).skip(1); - let content_type = String::from_utf8_lossy(strings.next().unwrap_or_default()).into_owned(); - out.push((id, kind4, content_type)); + let Some(body) = bytes.get(r.body..r.end) else { continue }; + let Some(from) = id_at(body, 0, width) else { continue }; + let item = index.get(&from).copied(); + if cdsc && item.is_none() { + continue; + } + let count = be16(body, width).unwrap_or(0); + let mut to = Vec::new(); + let mut at = width.saturating_add(2); + for _ in 0..count { + let Some(id) = id_at(body, at, width) else { break }; + to.push(id); + at = at.saturating_add(width); + } + match item { + Some(i) if cdsc => described.push((i, to)), + _ => inputs.entry(from).or_default().extend(to), + } + } + // the primary image and every image it is made from + let mut photo = HashSet::new(); + let mut todo: Vec = primary.into_iter().collect(); + while let Some(id) = todo.pop() { + if photo.insert(id) { + todo.extend(inputs.get(&id).into_iter().flatten().filter(|i| !photo.contains(*i))); + } + } + for (i, to) in described { + if let Some(item) = items.get_mut(i) + && !to.is_empty() + { + item.describes = true; + item.describes_primary |= to.iter().any(|t| photo.contains(t)); + } } - out } -/// Where one item's bytes are. -struct Loc { - id: u32, - /// 0 = offsets into the file, 1 = into the `idat` box, 2 = another item (not read). - construction: u8, +fn id_at(b: &[u8], at: usize, width: usize) -> Option { + if width == 2 { be16(b, at).map(u32::from) } else { be32(b, at) } +} + +/// `iloc`: its field sizes, read once. +struct Iloc<'a> { + data: &'a [u8], + version: u8, + offset_size: usize, + length_size: usize, + base_size: usize, + index_size: usize, +} + +/// Where an item is: how it is built, and its extents in `iloc`. +#[derive(Clone, Copy, Debug)] +struct Place { + method: u16, + data_reference: u16, base: u64, - /// `(offset, length)` per extent, in order. - extents: Vec<(u64, u64)>, + /// Where its first extent starts in `iloc`. + extents_at: usize, + extents: usize, } -/// `iloc` (payload after the box header) → one [`Loc`] per entry, stopping at the first -/// unreadable one. -fn item_locations(p: &[u8]) -> Vec { - let version = *p.first().unwrap_or(&0); - let (Some(sizes), Some(base_index)) = (p.get(4), p.get(5)) else { return Vec::new() }; - let (offset_size, length_size) = ((sizes >> 4) as usize, (sizes & 0xf) as usize); - let (base_size, index_size) = ((base_index >> 4) as usize, if version > 0 { (base_index & 0xf) as usize } else { 0 }); - let (count, mut at) = match version { - 0 | 1 => (be16(p, 6).map(usize::from), 8), - 2 => (be32(p, 6).and_then(|v| usize::try_from(v).ok()), 10), - _ => return Vec::new(), - }; - let mut out = Vec::new(); - for _ in 0..count.unwrap_or(0).min(MAX_ITEMS) { - let (id, after_id) = match version { - 2 => (be32(p, at), at + 4), - _ => (be16(p, at).map(u32::from), at + 2), - }; - // version 1 and 2 put the construction method in the low nibble of a reserved 16-bit field - let construction = if version == 0 { Some(0) } else { be16(p, after_id).map(|v| (v & 0xf) as u8) }; - let after_method = after_id + if version == 0 { 0 } else { 2 }; - // a data reference index of 0 means this file; an external reference has no bytes here - let external = be16(p, after_method).is_some_and(|v| v != 0); - let base = uint(p, after_method + 2, base_size); - let extent_at = after_method + 2 + base_size; - let (Some(id), Some(construction), Some(base), Some(extent_count)) = (id, construction, base, be16(p, extent_at)) else { - break; - }; - let stride = index_size + offset_size + length_size; - let mut extents = Vec::new(); - let mut e = extent_at + 2; - for _ in 0..extent_count.min(MAX_EXTENTS) { - let (Some(offset), Some(length)) = (uint(p, e + index_size, offset_size), uint(p, e + index_size + offset_size, length_size)) else { - return out; - }; - extents.push((offset, length)); - e += stride; +impl<'a> Iloc<'a> { + fn parse(data: &'a [u8]) -> Option> { + let version = *data.first()?; + if version > 2 { + return None; } - at = e; - if !external { - out.push(Loc { id, construction, base, extents }); + // version and flags, then four 4-bit field sizes: offset, length, base offset, extent index + let (sizes, more) = (*data.get(4)?, *data.get(5)?); + let (offset_size, length_size) = (usize::from(sizes >> 4), usize::from(sizes & 15)); + let base_size = usize::from(more >> 4); + let index_size = if version == 0 { 0 } else { usize::from(more & 15) }; // reserved in version 0 + if [offset_size, length_size, base_size, index_size].into_iter().any(|n| !matches!(n, 0 | 4 | 8)) { + return None; } + Some(Iloc { data, version, offset_size, length_size, base_size, index_size }) + } + + /// One walk over the entries: where each of `items` is (the first entry for an id wins). It + /// stops once all are placed, or where the entries stop making sense. The boolean is true + /// only if the walk exhausted the entries, so an absent placement is known to be absent. + fn locate(&self, items: &[Candidate]) -> (Vec>, bool) { + let mut places = vec![None; items.len()]; + if items.is_empty() { + return (places, false); + } + let wanted: HashMap = items.iter().enumerate().rev().map(|(i, c)| (c.id, i)).collect(); + let mut left = wanted.len(); + let complete = self.walk(|id, place| { + if let Some(&i) = wanted.get(&id) + && let Some(slot @ None) = places.get_mut(i) + { + *slot = Some(place); + left = left.saturating_sub(1); + } + left > 0 + }); + (places, complete.unwrap_or(false)) + } + + /// Call `f` with each entry's item id and place. `Some(true)` means every entry was read, + /// `Some(false)` means `f` stopped the walk, and `None` means an entry ran past the box. + fn walk(&self, mut f: impl FnMut(u32, Place) -> bool) -> Option { + let (version, data) = (self.version, self.data); + let id_size = if version == 2 { 4 } else { 2 }; + let (count, mut at) = if version == 2 { (be32(data, 6)?, 10usize) } else { (u32::from(be16(data, 6)?), 8usize) }; + let extent_size = self.index_size.checked_add(self.offset_size)?.checked_add(self.length_size)?; + // (every entry takes at least six bytes, so a count larger than the box ends at its end) + for _ in 0..count { + iloc_entry_visited(); + let id = uint(data, at, id_size)?; + at = at.checked_add(id_size)?; + let method = if version == 0 { + 0 + } else { + let m = be16(data, at)? & 15; + at = at.checked_add(2)?; + m + }; + let data_reference = be16(data, at)?; + let base = uint(data, at.checked_add(2)?, self.base_size)?; + at = at.checked_add(2)?.checked_add(self.base_size)?; + let extents = usize::from(be16(data, at)?); + at = at.checked_add(2)?; + let place = Place { method, data_reference, base, extents_at: at, extents }; + at = at.checked_add(extents.checked_mul(extent_size)?)?; + if at > data.len() { + return None; + } + if let Ok(id) = u32::try_from(id) + && !f(id, place) + { + return Some(false); + } + } + Some(true) + } + + /// An item's bytes, its extents in order from the file (construction method 0) or from the + /// `meta` box's `idat` (method 1), at most `limit` of them. Every extent is checked and the + /// lengths summed before anything is copied, so an item that is too large (say, the same + /// region named by thousands of extents) costs no copying. + fn gather(&self, file: &[u8], idat: Option<&[u8]>, place: &Place, limit: usize) -> Gathered { + // data reference 0 is this file + let source = match (place.data_reference, place.method, idat) { + (0, 0, _) => file, + (0, 1, Some(idat)) => idat, + _ => return Gathered::Unreadable, + }; + let pieces = || { + let mut at = place.extents_at; + (0..place.extents).map(move |_| { + let (piece, next) = self.extent(source, place.base, at)?; + at = next; + Some(piece) + }) + }; + let mut total = 0usize; + for piece in pieces() { + let Some(piece) = piece else { return Gathered::Unreadable }; + total = total.saturating_add(piece.len()); + if total > limit { + return Gathered::TooLarge; + } + } + let mut out = Vec::with_capacity(total); + for piece in pieces().flatten() { + bytes_copied(piece.len()); + out.extend_from_slice(piece); + } + Gathered::Bytes(out) + } + + /// The extent at `at`: its bytes in `source` (a length of 0 runs to the end) and where the + /// next extent starts. + fn extent<'s>(&self, source: &'s [u8], base: u64, at: usize) -> Option<(&'s [u8], usize)> { + let at = at.checked_add(self.index_size)?; + let offset = uint(self.data, at, self.offset_size)?; + let at = at.checked_add(self.offset_size)?; + let length = uint(self.data, at, self.length_size)?; + let at = at.checked_add(self.length_size)?; + let rest = source.get(usize::try_from(base.checked_add(offset)?).ok()?..)?; + let piece = if length == 0 { rest } else { rest.get(..usize::try_from(length).ok()?)? }; + Some((piece, at)) } - out } -/// A big-endian unsigned integer of `size` bytes; `size` 0 is the value 0, and the item boxes -/// allow no other width than 4 or 8. +#[cfg(test)] +thread_local! { + /// `iloc` entries walked on this thread, for the tests that bound the work. + static ILOC_ENTRIES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +fn iloc_entry_visited() { + #[cfg(test)] + ILOC_ENTRIES.with(|n| n.set(n.get().saturating_add(1))); +} + +#[cfg(test)] +thread_local! { + /// Item bytes copied on this thread, for the tests that bound the work. + static COPIED: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[cfg_attr(not(test), allow(unused_variables))] +fn bytes_copied(n: usize) { + #[cfg(test)] + COPIED.with(|c| c.set(c.get().saturating_add(n))); +} + +/// A big-endian unsigned field of `size` bytes (0 reads as 0). fn uint(b: &[u8], at: usize, size: usize) -> Option { match size { 0 => Some(0), + 2 => be16(b, at).map(u64::from), 4 => be32(b, at).map(u64::from), 8 => be64(b, at), _ => None, } } -/// An item's bytes, its extents concatenated. `None` when they don't fit the file (or the `idat` -/// box), when the item is built by referencing another one, or when it is implausibly large. -fn item_bytes(bytes: &[u8], idat: Option<&[u8]>, loc: &Loc) -> Option> { - let src = match loc.construction { - 0 => bytes, - 1 => idat?, - _ => return None, - }; - let mut out = Vec::new(); - for &(offset, length) in &loc.extents { - let at = loc.base.checked_add(offset)?; - let (start, end) = (usize::try_from(at).ok()?, usize::try_from(at.checked_add(length)?).ok()?); - let extent = src.get(start..end)?; - if out.len().checked_add(extent.len())? > MAX_ITEM { - return None; - } - out.extend_from_slice(extent); - } - (!out.is_empty()).then_some(out) -} - -/// A box: its type and where its payload ends. +/// A box: its type, where its payload starts and where it ends. struct BoxRef { kind: [u8; 4], /// Start of the payload (after the header and, for `uuid`, the 16-byte uuid). @@ -225,33 +690,33 @@ struct BoxRef { end: usize, } -/// The boxes directly inside `[from, to)`; stops at the first malformed header. +/// The boxes directly inside `[from, to)`, at most `budget` of them (shared over the calls of one +/// read); stops at the first malformed header. 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), + let mut at = from; + while *budget > 0 { + let Some(header_end) = at.checked_add(8).filter(|&e| e <= to) else { break }; + *budget = budget.saturating_sub(1); + let (Some(size), Some(kind)) = (be32(bytes, at), bytes.get(at.saturating_add(4)..header_end)) else { break }; + let Ok(kind) = <[u8; 4]>::try_from(kind) else { break }; + let (len, header) = match size { + 0 => (to.saturating_sub(at), 8usize), + 1 => match be64(bytes, header_end).and_then(|v| usize::try_from(v).ok()) { + Some(len) => (len, 16), None => break, }, - n => (n as usize, 8), + n => match usize::try_from(n) { + Ok(len) => (len, 8), + Err(_) => break, + }, }; - let Some(end) = at.checked_add(len).filter(|&e| e <= to && len >= hdr) else { break }; - if &kind4 == b"uuid" { - // the 16-byte extended type is part of the header - if at + hdr + 16 > end { - break; - } - hdr += 16; - } - out.push(BoxRef { kind: kind4, body: at + hdr, end }); + // the 16-byte extended type of a `uuid` box is part of its header + let header = if &kind == b"uuid" { header.saturating_add(16) } else { header }; + let Some(end) = at.checked_add(len).filter(|&e| e <= to && len >= header) else { break }; + let Some(body) = at.checked_add(header) else { break }; + out.push(BoxRef { kind, body, end }); at = end; } out @@ -259,231 +724,18 @@ fn boxes(bytes: &[u8], from: usize, to: usize, budget: &mut usize) -> Vec Option { - b.get(at..at.checked_add(2)?).map(|s| u16::from_be_bytes([s[0], s[1]])) + b.get(at..at.checked_add(2)?)?.try_into().ok().map(u16::from_be_bytes) } + /// A big-endian `u32` at `at`, or `None` when the buffer is shorter than that. 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]])) + b.get(at..at.checked_add(4)?)?.try_into().ok().map(u32::from_be_bytes) } + /// A big-endian `u64` at `at`, or `None` when the buffer is shorter than that. 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)) + b.get(at..at.checked_add(8)?)?.try_into().ok().map(u64::from_be_bytes) } #[cfg(test)] -mod tests { - use super::*; - - /// One box: its own size, its four-letter kind, then its payload. - fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec { - let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec(); - v.extend_from_slice(kind); - v.extend_from_slice(payload); - v - } - - /// A FullBox: a box whose payload begins with a version byte and three flag bytes. - fn full_box(kind: &[u8; 4], version: u8, payload: &[u8]) -> Vec { - bx(kind, &[&[version, 0, 0, 0][..], payload].concat()) - } - - struct Item<'a> { - kind: &'a [u8; 4], - content_type: &'a str, - data: Vec, - } - - /// A HEIC-shaped file holding `items`: `ftyp`, a `meta` box with one `infe` per item, and the - /// item bytes in `idat` (`in_idat`, construction method 1) or in `mdat` (method 0). - fn heic(items: &[Item<'_>], iloc_version: u8, in_idat: bool) -> Vec { - let mut iinf = (items.len() as u16).to_be_bytes().to_vec(); - for (i, it) in items.iter().enumerate() { - let mut e = vec![2u8, 0, 0, 0]; // version 2, flags - e.extend_from_slice(&(i as u16 + 1).to_be_bytes()); - e.extend_from_slice(&[0, 0]); // protection index - e.extend_from_slice(it.kind); - e.extend_from_slice(b"an item\0"); - if it.kind == b"mime" { - e.extend_from_slice(it.content_type.as_bytes()); - e.push(0); - } - iinf.extend_from_slice(&bx(b"infe", &e)); - } - let iinf = full_box(b"iinf", 0, &iinf); - let payload: Vec = items.iter().flat_map(|i| i.data.iter().copied()).collect(); - let idat = if in_idat { bx(b"idat", &payload) } else { Vec::new() }; - let iloc = |base: u32| { - // four-byte offsets and lengths, no base offset and no extent index - let mut l = vec![0x44, 0x00]; - match iloc_version { - 2 => l.extend_from_slice(&(items.len() as u32).to_be_bytes()), - _ => l.extend_from_slice(&(items.len() as u16).to_be_bytes()), - } - let mut at = base; - for (i, it) in items.iter().enumerate() { - match iloc_version { - 2 => l.extend_from_slice(&(i as u32 + 1).to_be_bytes()), - _ => l.extend_from_slice(&(i as u16 + 1).to_be_bytes()), - } - if iloc_version > 0 { - l.extend_from_slice(&[0, u8::from(in_idat)]); // construction method - } - l.extend_from_slice(&[0, 0]); // data reference index: this file - l.extend_from_slice(&[0, 1]); // one extent - l.extend_from_slice(&at.to_be_bytes()); - l.extend_from_slice(&(it.data.len() as u32).to_be_bytes()); - at += it.data.len() as u32; - } - full_box(b"iloc", iloc_version, &l) - }; - let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); - let meta = |base| full_box(b"meta", 0, &[iloc(base), iinf.clone(), idat.clone()].concat()); - if in_idat { - return [ftyp, meta(0)].concat(); - } - let base = (ftyp.len() + meta(0).len() + 8) as u32; - [ftyp, meta(base), bx(b"mdat", &payload)].concat() - } - - const TIFF: &[u8] = b"MM\0\x2a\0\0\0\x08\0\0\0\0\0\0"; - - /// An `Exif` item's payload: the offset from the end of this field to the TIFF header, then - /// `skip` bytes of padding, then the TIFF stream. - fn exif_item(tiff: &[u8], skip: usize) -> Vec { - [&(skip as u32).to_be_bytes()[..], &vec![0u8; skip], tiff].concat() - } - - fn exif_only(tiff: &[u8]) -> Vec> { - vec![Item { kind: b"Exif", content_type: "", data: exif_item(tiff, 0) }] - } - - /// Every item location box the format allows: counts and ids are 16-bit before version 2 and - /// 32-bit in it, and the bytes live in `mdat` or in `idat` next to the location box itself. - #[test] - fn items_are_located_in_every_iloc_version() { - for (version, in_idat) in [(0, false), (1, false), (1, true), (2, false), (2, true)] { - let file = heic(&exif_only(TIFF), version, in_idat); - assert_eq!(read(&file).unwrap(), (Some(TIFF.to_vec()), None), "iloc v{version}, idat {in_idat}"); - } - } - - /// The XMP packet is the `mime` item that declares `application/rdf+xml`, not any other one. - #[test] - fn the_xmp_packet_is_the_rdf_mime_item() { - let items = vec![ - Item { kind: b"mime", content_type: "text/plain", data: b"not the packet".to_vec() }, - Item { kind: b"mime", content_type: "application/rdf+xml", data: b"\0".to_vec() }, - Item { kind: b"Exif", content_type: "", data: exif_item(TIFF, 0) }, - ]; - assert_eq!(read(&heic(&items, 1, false)).unwrap(), (Some(TIFF.to_vec()), Some("".into()))); - } - - /// The `Exif` item's leading offset may point past padding, not only straight at the TIFF header. - #[test] - fn the_exif_items_tiff_header_offset_is_honoured() { - let items = vec![Item { kind: b"Exif", content_type: "", data: exif_item(TIFF, 6) }]; - assert_eq!(read(&heic(&items, 1, false)).unwrap().0.as_deref(), Some(TIFF)); - } - - /// Which `ftyp` brands count as a still image in this container, major or compatible. - #[test] - fn brands() { - for brand in ["heic", "heix", "mif1", "avif", "hevc", "msf1"] { - let file = bx(b"ftyp", format!("{brand}\0\0\0\0mif1").as_bytes()); - assert!(is_heif(&file), "{brand}"); - } - assert!(is_heif(&bx(b"ftyp", b"mp41\0\0\0\0isomheic"))); - assert!(!is_heif(&bx(b"ftyp", b"crx \0\0\0\0crx isom"))); - assert!(!is_heif(&bx(b"ftyp", b"jxl \0\0\0\0jxl "))); - assert!(!is_heif(b"\xff\xd8\xff\xe1 a jpeg")); - assert!(!is_heif(b"")); - // a compatible brand past MAX_FTYP bytes is not read - let mut long = b"mp41\0\0\0\0".to_vec(); - long.resize(MAX_FTYP, b'x'); - long.extend_from_slice(b"heic"); - assert!(!is_heif(&bx(b"ftyp", &long))); - } - - /// Not a HEIF file, an image sequence (no `meta`), garbage: errors as the decoder reports them. - #[test] - fn what_is_not_a_still_image_is_an_error() { - assert!(matches!(read(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"), Err(Error::Unsupported(m)) if m.contains("sequence"))); - for bytes in [&b""[..], b"\0\0\0\x18ftypheic\0\0\0\0mif1heic", &[0xFF; 64]] { - assert!(read(bytes).is_err()); - } - } - - /// A truncated or scrambled file never panics, and lies about sizes find nothing. - #[test] - fn broken_files_never_panic() { - let good = heic( - &[ - Item { kind: b"Exif", content_type: "", data: exif_item(TIFF, 0) }, - Item { kind: b"mime", content_type: "application/rdf+xml", data: b"".to_vec() }, - ], - 1, - false, - ); - for cut in 0..good.len() { - let _ = read(&good[..cut]); - } - for at in 0..good.len() { - let mut bad = good.clone(); - bad[at] ^= 0xff; - let _ = read(&bad); - } - let mut lying = good.clone(); - if let Some(at) = lying.windows(4).position(|w| w == b"iloc") { - lying.splice(at + 8..at + 12, u32::MAX.to_be_bytes()); - } - assert_eq!(read(&lying).unwrap().0, None); - } - - /// Past MAX_ITEMS `infe` entries the rest are not read; an item larger than MAX_ITEM is left out. - #[test] - fn item_limits() { - let mut items: Vec> = (0..MAX_ITEMS).map(|_| Item { kind: b"mime", content_type: "text/plain", data: vec![b'x'] }).collect(); - items.push(Item { kind: b"Exif", content_type: "", data: exif_item(TIFF, 0) }); - assert_eq!(read(&heic(&items, 2, false)).unwrap().0, None, "the Exif item is entry {}", MAX_ITEMS + 1); - items.remove(0); - assert_eq!(read(&heic(&items, 2, false)).unwrap().0.as_deref(), Some(TIFF)); - let big = vec![Item { kind: b"Exif", content_type: "", data: exif_item(&vec![0; MAX_ITEM], 0) }]; - assert_eq!(read(&heic(&big, 1, true)).unwrap().0, None); - } - - /// The box walk stops after MAX_BOXES boxes: a `meta` box after that many is not found. - #[test] - fn box_limit() { - let mut file = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); - for _ in 0..MAX_BOXES { - file.extend(bx(b"free", &[])); - } - file.extend(heic(&exif_only(TIFF), 1, true).split_off(bx(b"ftyp", b"heic\0\0\0\0mif1heic").len())); - assert!(matches!(read(&file), Err(Error::Unsupported(_)))); - } - - /// The container writer of this crate's tests: the reader agrees with the layout an encoder writes. - #[test] - fn a_synthetic_heif_file_carries_its_exif() { - let mut spec = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); - spec.exif = Some(TIFF.to_vec()); - assert_eq!(read(&crate::testdata::build(&spec)).unwrap(), (Some(TIFF.to_vec()), None)); - assert_eq!(read(&crate::testdata::build(&crate::testdata::Spec::new(16, 16, &|_, _| [0; 3]))).unwrap(), (None, None)); - } - - /// With the decoder built too, both readers give the same answer on files with one Exif item. - #[cfg(feature = "decode")] - #[test] - fn agrees_with_the_decoders_read_metadata() { - let mut spec = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); - spec.exif = Some(TIFF.to_vec()); - for file in [crate::testdata::build(&spec), crate::testdata::build(&crate::testdata::Spec::new(8, 8, &|_, _| [0; 3]))] { - assert_eq!(read(&file), crate::read_metadata(&file)); - } - let sequence = b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"; - assert_eq!(read(sequence), crate::read_metadata(sequence)); - } -} +mod tests; diff --git a/crates/heif/src/items/tests.rs b/crates/heif/src/items/tests.rs new file mode 100644 index 000000000..35a193613 --- /dev/null +++ b/crates/heif/src/items/tests.rs @@ -0,0 +1,1251 @@ +#![allow(clippy::arithmetic_side_effects)] + +use super::*; + +/// One box: its own size, its four-letter kind, then its payload. +fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec { + let mut v = ((payload.len() + 8) as u32).to_be_bytes().to_vec(); + v.extend_from_slice(kind); + v.extend_from_slice(payload); + v +} + +/// A FullBox: a box whose payload begins with a version byte and three flag bytes. +fn full_box(kind: &[u8; 4], version: u8, payload: &[u8]) -> Vec { + bx(kind, &[&[version, 0, 0, 0][..], payload].concat()) +} + +/// A big-endian field of `size` bytes. +fn put(v: &mut Vec, x: u64, size: u8) { + match size { + 0 => assert_eq!(x, 0, "a value needs a field"), + 2 => v.extend_from_slice(&(x as u16).to_be_bytes()), + 4 => v.extend_from_slice(&(x as u32).to_be_bytes()), + 8 => v.extend_from_slice(&x.to_be_bytes()), + _ => panic!("no {size}-byte fields"), + } +} + +const TIFF: &[u8] = b"MM\0\x2a\0\0\0\x08\0\0\0\0\0\0"; + +/// The 100-byte EXIF of LightCraft's issue sweep `synthetic-metadata.heic`: Make "Synthetic", +/// Model "Issue sweep camera", DateTime 2026:10:12 12:34:56, big-endian. +const SWEEP_TIFF: [u8; 100] = [ + 0x4d, 0x4d, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x01, 0x0f, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, // + 0x00, 0x32, 0x01, 0x10, 0x00, 0x02, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x3c, 0x01, 0x32, 0x00, 0x02, 0x00, 0x00, // + 0x00, 0x14, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x53, 0x79, 0x6e, 0x74, 0x68, 0x65, 0x74, 0x69, 0x63, 0x00, // + 0x49, 0x73, 0x73, 0x75, 0x65, 0x20, 0x73, 0x77, 0x65, 0x65, 0x70, 0x20, 0x63, 0x61, 0x6d, 0x65, 0x72, 0x61, 0x00, 0x00, // + 0x32, 0x30, 0x32, 0x36, 0x3a, 0x31, 0x30, 0x3a, 0x31, 0x32, 0x20, 0x31, 0x32, 0x3a, 0x33, 0x34, 0x3a, 0x35, 0x36, 0x00, +]; + +/// A TIFF stream as far as this reader looks: a big-endian TIFF header, then `marker`. +fn tiff_with(marker: &str) -> Vec { + [b"MM\0*\0\0\0\x08".as_slice(), marker.as_bytes()].concat() +} + +/// An XMP packet holding `title`. +fn xmp_with(title: &str) -> String { + format!("{title}") +} + +/// An EXIF item as HEIF stores it: the 4-byte offset, `skip` (which the offset jumps), the TIFF. +fn exif_item(skip: &[u8], tiff: &[u8]) -> Vec { + [&(skip.len() as u32).to_be_bytes()[..], skip, tiff].concat() +} + +/// The photo's EXIF, as [`read`] gives it. +fn exif(f: &[u8]) -> Option> { + read(f).ok()?.0 +} + +/// The photo's XMP packet, as [`read`] gives it. +fn xmp(f: &[u8]) -> Option { + read(f).ok()?.1 +} + +/// Every metadata item [`scan`] finds, when it is sure it saw them all: `None` when the catalog +/// is incomplete or an item can't be read. +fn sure(f: &[u8]) -> Option)>> { + let s = scan(f).ok()?; + if !s.complete() { + return None; + } + s.map(|it| it.data.ok().map(|d| (it.id, it.kind, d))).collect() +} + +// -- The single-`meta` builder of the first version of this reader -------------------------------- + +struct Entry<'a> { + kind: &'a [u8; 4], + content_type: &'a str, + data: Vec, +} + +/// A HEIC-shaped file holding `items`: `ftyp`, a `meta` box with one `infe` per item, and the +/// item bytes in `idat` (`in_idat`, construction method 1) or in `mdat` (method 0). No `pitm` and +/// no `iref`: every item describes the file. +fn heic(items: &[Entry<'_>], iloc_version: u8, in_idat: bool) -> Vec { + let mut iinf = (items.len() as u16).to_be_bytes().to_vec(); + for (i, it) in items.iter().enumerate() { + let mut e = vec![2u8, 0, 0, 0]; // version 2, flags + e.extend_from_slice(&(i as u16 + 1).to_be_bytes()); + e.extend_from_slice(&[0, 0]); // protection index + e.extend_from_slice(it.kind); + e.extend_from_slice(b"an item\0"); + if it.kind == b"mime" { + e.extend_from_slice(it.content_type.as_bytes()); + e.push(0); + } + iinf.extend_from_slice(&bx(b"infe", &e)); + } + let iinf = full_box(b"iinf", 0, &iinf); + let payload: Vec = items.iter().flat_map(|i| i.data.iter().copied()).collect(); + let idat = if in_idat { bx(b"idat", &payload) } else { Vec::new() }; + let iloc = |base: u32| { + // four-byte offsets and lengths, no base offset and no extent index + let mut l = vec![0x44, 0x00]; + match iloc_version { + 2 => l.extend_from_slice(&(items.len() as u32).to_be_bytes()), + _ => l.extend_from_slice(&(items.len() as u16).to_be_bytes()), + } + let mut at = base; + for (i, it) in items.iter().enumerate() { + match iloc_version { + 2 => l.extend_from_slice(&(i as u32 + 1).to_be_bytes()), + _ => l.extend_from_slice(&(i as u16 + 1).to_be_bytes()), + } + if iloc_version > 0 { + l.extend_from_slice(&[0, u8::from(in_idat)]); // construction method + } + l.extend_from_slice(&[0, 0]); // data reference index: this file + l.extend_from_slice(&[0, 1]); // one extent + l.extend_from_slice(&at.to_be_bytes()); + l.extend_from_slice(&(it.data.len() as u32).to_be_bytes()); + at += it.data.len() as u32; + } + full_box(b"iloc", iloc_version, &l) + }; + let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + let meta = |base| full_box(b"meta", 0, &[iloc(base), iinf.clone(), idat.clone()].concat()); + if in_idat { + return [ftyp, meta(0)].concat(); + } + let base = (ftyp.len() + meta(0).len() + 8) as u32; + [ftyp, meta(base), bx(b"mdat", &payload)].concat() +} + +/// An `Exif` item's payload: the offset from the end of this field to the TIFF header, then +/// `skip` bytes of padding, then the TIFF stream. +fn exif_padded(tiff: &[u8], skip: usize) -> Vec { + [&(skip as u32).to_be_bytes()[..], &vec![0u8; skip], tiff].concat() +} + +fn exif_only(tiff: &[u8]) -> Vec> { + vec![Entry { kind: b"Exif", content_type: "", data: exif_padded(tiff, 0) }] +} + +/// Every item location box the format allows: counts and ids are 16-bit before version 2 and +/// 32-bit in it, and the bytes live in `mdat` or in `idat` next to the location box itself. +#[test] +fn items_are_located_in_every_iloc_version() { + for (version, in_idat) in [(0, false), (1, false), (1, true), (2, false), (2, true)] { + let file = heic(&exif_only(TIFF), version, in_idat); + assert_eq!(read(&file).unwrap(), (Some(TIFF.to_vec()), None), "iloc v{version}, idat {in_idat}"); + } +} + +/// The XMP packet is the `mime` item that declares `application/rdf+xml`, not any other one. +#[test] +fn the_xmp_packet_is_the_rdf_mime_item() { + let items = vec![ + Entry { kind: b"mime", content_type: "text/plain", data: b"not the packet".to_vec() }, + Entry { kind: b"mime", content_type: "application/rdf+xml", data: b"\0".to_vec() }, + Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 0) }, + ]; + assert_eq!(read(&heic(&items, 1, false)).unwrap(), (Some(TIFF.to_vec()), Some("".into()))); +} + +/// The `Exif` item's leading offset may point past padding, not only straight at the TIFF header. +#[test] +fn the_exif_items_tiff_header_offset_is_honoured() { + let items = vec![Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 6) }]; + assert_eq!(read(&heic(&items, 1, false)).unwrap().0.as_deref(), Some(TIFF)); +} + +/// Which `ftyp` brands count as a still image in this container, major or compatible. +#[test] +fn brands() { + for brand in ["heic", "heix", "mif1", "avif", "hevc", "msf1"] { + let file = bx(b"ftyp", format!("{brand}\0\0\0\0mif1").as_bytes()); + assert!(is_heif(&file), "{brand}"); + } + assert!(is_heif(&bx(b"ftyp", b"mp41\0\0\0\0isomheic"))); + assert!(!is_heif(&bx(b"ftyp", b"crx \0\0\0\0crx isom"))); + assert!(!is_heif(&bx(b"ftyp", b"jxl \0\0\0\0jxl "))); + assert!(!is_heif(b"\xff\xd8\xff\xe1 a jpeg")); + assert!(!is_heif(b"")); + // a compatible brand past MAX_FTYP bytes is not read + let mut long = b"mp41\0\0\0\0".to_vec(); + long.resize(MAX_FTYP, b'x'); + long.extend_from_slice(b"heic"); + assert!(!is_heif(&bx(b"ftyp", &long))); +} + +/// Not a HEIF file, an image sequence (no `meta`), garbage: errors as the decoder reports them. +#[test] +fn what_is_not_a_still_image_is_an_error() { + assert!(matches!(read(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"), Err(Error::Unsupported(m)) if m.contains("sequence"))); + for bytes in [&b""[..], b"\0\0\0\x18ftypheic\0\0\0\0mif1heic", &[0xFF; 64]] { + assert!(read(bytes).is_err()); + } + assert!(scan(&[0xFF; 64]).is_err(), "only a file that isn't HEIF is an error to scan"); +} + +/// A truncated or scrambled file never panics, and lies about sizes find nothing. +#[test] +fn broken_files_never_panic() { + let good = heic( + &[ + Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 0) }, + Entry { kind: b"mime", content_type: "application/rdf+xml", data: b"".to_vec() }, + ], + 1, + false, + ); + for cut in 0..good.len() { + let _ = read(&good[..cut]); + let _ = scan(&good[..cut]).map(Iterator::count); + } + for at in 0..good.len() { + let mut bad = good.clone(); + bad[at] ^= 0xff; + let _ = read(&bad); + let _ = scan(&bad).map(Iterator::count); + } + let mut lying = good.clone(); + if let Some(at) = lying.windows(4).position(|w| w == b"iloc") { + lying.splice(at + 8..at + 12, u32::MAX.to_be_bytes()); + } + assert_eq!(read(&lying).unwrap().0, None); + assert_eq!(sure(&lying), None, "an item `iloc` can't place is unreadable, not absent"); +} + +/// [`read`] considers the first MAX_CANDIDATES metadata items; an item larger than MAX_ITEM is +/// left out by [`read`] and too large for [`scan`]. +#[test] +fn item_limits() { + let mut items: Vec> = + (0..MAX_CANDIDATES).map(|_| Entry { kind: b"Exif", content_type: "", data: exif_padded(b"not TIFF", 0) }).collect(); + items.push(Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 0) }); + assert_eq!(read(&heic(&items, 2, false)).unwrap().0, None, "the TIFF is metadata item {}", MAX_CANDIDATES + 1); + items.remove(0); + assert_eq!(read(&heic(&items, 2, false)).unwrap().0.as_deref(), Some(TIFF)); + let big = heic(&[Entry { kind: b"Exif", content_type: "", data: exif_padded(&vec![0; MAX_ITEM], 0) }], 1, true); + assert_eq!(read(&big).unwrap().0, None); + let found: Vec = scan(&big).unwrap().collect(); + assert_eq!(found, [Item { id: 1, kind: Kind::Exif, data: Err(Unreadable::TooLarge) }]); +} + +/// The scan gathers at most its byte budget over all items; past it an item is too large. +#[test] +fn the_scan_byte_budget() { + let items = [ + Entry { kind: b"Exif", content_type: "", data: exif_padded(TIFF, 0) }, + Entry { kind: b"mime", content_type: "application/rdf+xml", data: b"".to_vec() }, + ]; + let f = heic(&items, 1, true); + let first = TIFF.len() + 4; + let found: Vec = scan_within(&f, first + 5).unwrap().collect(); + assert_eq!(found[0].data.as_deref().map(<[u8]>::len), Ok(first)); + assert_eq!(found[1].data, Err(Unreadable::TooLarge)); + let found: Vec = scan_within(&f, first + 12).unwrap().collect(); + assert_eq!(found[1].data.as_deref(), Ok(b"".as_slice())); +} + +/// The box walk stops after MAX_BOXES boxes: a `meta` box after that many is not found. +#[test] +fn box_limit() { + let mut file = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + for _ in 0..MAX_BOXES { + file.extend(bx(b"free", &[])); + } + file.extend(heic(&exif_only(TIFF), 1, true).split_off(bx(b"ftyp", b"heic\0\0\0\0mif1heic").len())); + assert!(matches!(read(&file), Err(Error::Unsupported(_)))); + assert!(!scan(&file).unwrap().complete()); +} + +/// The container writer of this crate's tests: the reader agrees with the layout an encoder writes. +#[test] +fn a_synthetic_heif_file_carries_its_exif() { + let mut spec = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); + spec.exif = Some(TIFF.to_vec()); + let file = crate::testdata::build(&spec); + assert_eq!(read(&file).unwrap(), (Some(TIFF.to_vec()), None)); + let mut exif = 0u32.to_be_bytes().to_vec(); + exif.extend_from_slice(TIFF); + assert!(matches!(sure(&file).as_deref(), Some([(_, Kind::Exif, data)]) if *data == exif)); + assert_eq!(read(&crate::testdata::build(&crate::testdata::Spec::new(16, 16, &|_, _| [0; 3]))).unwrap(), (None, None)); +} + +/// With the decoder built too, both readers give the same answer, also on files with a +/// thumbnail and with several items. +#[cfg(feature = "decode")] +#[test] +fn agrees_with_the_decoders_read_metadata() { + let mut spec = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); + spec.exif = Some(TIFF.to_vec()); + let mut thumbnailed = crate::testdata::Spec::new(16, 16, &|x, _| [x as u16 * 4, 0, 0]); + thumbnailed.exif = Some(TIFF.to_vec()); + thumbnailed.thumbnail = Some(&|_, y| [y as u16 * 4, 0, 0]); + let files = [ + crate::testdata::build(&spec), + crate::testdata::build(&thumbnailed), + crate::testdata::build(&crate::testdata::Spec::new(8, 8, &|_, _| [0; 3])), + ]; + for file in files { + assert_eq!(read(&file), crate::read_metadata(&file)); + } + let sequence = b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"; + assert_eq!(read(sequence), crate::read_metadata(sequence)); +} + +// -- A builder with a primary image, references and every `iloc` layout ---------------------------- + +#[derive(Clone, Copy, PartialEq)] +enum Store { + File, + Idat, +} + +struct Part { + id: u32, + kind: &'static [u8; 4], + /// `mime` items: content type and content encoding. + mime: (&'static str, &'static str), + data: Vec, + store: Store, + /// Extents the data is split into. + extents: usize, +} + +/// A HEIF file for the tests: `ftyp`, `meta` (`hdlr`, `pitm`, `iinf`, `iref`, `iloc`, `idat`) +/// and `mdat`. +struct Heif { + ftyp: Vec, + primary: u32, + items: Vec, + /// `iref` references: type, from, to. + refs: Vec<(&'static [u8; 4], u32, Vec)>, + /// `iloc` version and its offset, length, base-offset and index field sizes. + iloc: (u8, [u8; 4]), + /// 32-bit item ids: `infe` version 3, `pitm` and `iref` version 1 (`iloc` must be version 2). + wide: bool, +} + +impl Heif { + /// A primary HEVC picture, item 1 (its bytes don't matter here). + fn new() -> Heif { + let picture = Part { id: 1, kind: b"hvc1", mime: ("", ""), data: b"coded picture".to_vec(), store: Store::File, extents: 1 }; + Heif { ftyp: b"heic\0\0\0\0mif1heic".to_vec(), primary: 1, items: vec![picture], refs: Vec::new(), iloc: (1, [4, 4, 0, 0]), wide: false } + } + + fn item(mut self, id: u32, kind: &'static [u8; 4], data: Vec, store: Store, extents: usize) -> Heif { + self.items.push(Part { id, kind, mime: ("", ""), data, store, extents }); + self + } + + fn xmp(mut self, id: u32, mime: (&'static str, &'static str), data: Vec) -> Heif { + self.items.push(Part { id, kind: b"mime", mime, data, store: Store::File, extents: 1 }); + self + } + + fn cdsc(self, from: u32, to: &[u32]) -> Heif { + self.iref(b"cdsc", from, to) + } + + fn iref(mut self, kind: &'static [u8; 4], from: u32, to: &[u32]) -> Heif { + self.refs.push((kind, from, to.to_vec())); + self + } + + fn meta(&self, mdat_at: usize) -> Vec { + let id = |v: &mut Vec, id: u32| put(v, u64::from(id), if self.wide { 4 } else { 2 }); + let mut hdlr = vec![0u8; 4]; + hdlr.extend_from_slice(b"pict"); + hdlr.extend_from_slice(&[0; 13]); + let mut pitm = Vec::new(); + id(&mut pitm, self.primary); + let mut iinf = Vec::new(); + put(&mut iinf, self.items.len() as u64, if self.wide { 4 } else { 2 }); + for it in &self.items { + let mut e = Vec::new(); + id(&mut e, it.id); + e.extend_from_slice(&[0, 0]); // not protected + e.extend_from_slice(it.kind); + e.push(0); // no name + if it.kind == b"mime" { + e.extend_from_slice(it.mime.0.as_bytes()); + e.push(0); + e.extend_from_slice(it.mime.1.as_bytes()); + e.push(0); + } + iinf.extend(full_box(b"infe", if self.wide { 3 } else { 2 }, &e)); + } + let mut iref = Vec::new(); + for (kind, from, to) in &self.refs { + let mut r = Vec::new(); + id(&mut r, *from); + put(&mut r, to.len() as u64, 2); + to.iter().for_each(|t| id(&mut r, *t)); + iref.extend(bx(kind, &r)); + } + let (version, [o, l, b, i]) = self.iloc; + let mut iloc = vec![o << 4 | l, b << 4 | i]; + put(&mut iloc, self.items.len() as u64, if version == 2 { 4 } else { 2 }); + let (mut in_file, mut in_idat) = (mdat_at as u64, 0u64); + let mut idat = Vec::new(); + for it in &self.items { + put(&mut iloc, u64::from(it.id), if version == 2 { 4 } else { 2 }); + let at = match it.store { + Store::File => &mut in_file, + Store::Idat => &mut in_idat, + }; + if version > 0 { + put(&mut iloc, if it.store == Store::Idat { 1 } else { 0 }, 2); + } + put(&mut iloc, 0, 2); // this file + let base = if b > 0 { *at } else { 0 }; + put(&mut iloc, base, b); + let chunk = it.data.len().div_ceil(it.extents.max(1)).max(1); + let pieces: Vec<&[u8]> = it.data.chunks(chunk).collect(); + put(&mut iloc, pieces.len() as u64, 2); + for piece in pieces { + if version > 0 { + put(&mut iloc, 0, i); + } + put(&mut iloc, *at - base, o); + put(&mut iloc, piece.len() as u64, l); + *at += piece.len() as u64; + } + if it.store == Store::Idat { + idat.extend_from_slice(&it.data); + } + } + let ref_version = if self.wide { 1 } else { 0 }; + let boxes = [ + full_box(b"hdlr", 0, &hdlr), + full_box(b"pitm", ref_version, &pitm), + full_box(b"iinf", ref_version, &iinf), + full_box(b"iref", ref_version, &iref), + full_box(b"iloc", version, &iloc), + bx(b"idat", &idat), + ]; + full_box(b"meta", 0, &boxes.concat()) + } + + fn build(&self) -> Vec { + let ftyp = bx(b"ftyp", &self.ftyp); + let mdat_at = ftyp.len() + self.meta(0).len() + 8; + let mdat: Vec = self.items.iter().filter(|i| i.store == Store::File).flat_map(|i| i.data.iter().copied()).collect(); + [ftyp, self.meta(mdat_at), bx(b"mdat", &mdat)].concat() + } +} + +/// One item's bytes, as [`read`] gathers them: its place from `iloc`, then its extents. +fn item_bytes(file: &[u8], iloc: &[u8], idat: Option<&[u8]>, id: u32) -> Option> { + let iloc = Iloc::parse(iloc)?; + let mut place = None; + iloc.walk(|i, p| { + if i == id { + place = Some(p); + } + place.is_none() + })?; + match iloc.gather(file, idat, &place?, MAX_ITEM) { + Gathered::Bytes(b) => Some(b), + _ => None, + } +} + +/// LightCraft's issue sweep case, laid out as its HEIF test builder writes it: `iloc` version 1, +/// the EXIF item (offset 0, then the TIFF) tied to the primary image by `cdsc`. +#[test] +fn the_issue_sweep_heic_gives_its_exif() { + let f = Heif::new().item(8, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::File, 1).cdsc(8, &[1]).build(); + assert_eq!(exif(&f).as_deref(), Some(SWEEP_TIFF.as_slice())); +} + +/// The 4-byte offset in front of the TIFF stream: iPhones write 6 and `Exif\0\0`, libheif and +/// avif-serialize 0. Writers that left the field out, or got it wrong, are read when the stream +/// is plain to see; anything else is no EXIF. +#[test] +fn exif_items_with_and_without_their_offset() { + let t = &SWEEP_TIFF[..]; + let tiff = Some(t); + assert_eq!(exif_tiff(&exif_item(b"Exif\0\0", t)), tiff, "iPhone"); + assert_eq!(exif_tiff(&exif_item(&[], t)), tiff, "libheif, avif-serialize"); + assert_eq!(exif_tiff(&exif_item(b"\0\0padding", t)), tiff, "any bytes the offset skips"); + assert_eq!(exif_tiff(&[b"\0\0\0\0Exif\0\0".as_slice(), t].concat()), tiff, "offset 0 though a prefix follows"); + assert_eq!(exif_tiff(&[&1000u32.to_be_bytes()[..], t].concat()), tiff, "offset past the end"); + assert_eq!(exif_tiff(&[b"Exif\0\0".as_slice(), t].concat()), tiff, "no offset field, JPEG style"); + assert_eq!(exif_tiff(t), tiff, "no offset field at all"); + assert_eq!(exif_tiff(&exif_item(&[], b"not a TIFF stream")), None); + assert_eq!(exif_tiff(&[0, 0]), None); + assert_eq!(exif_tiff(&[]), None); + assert_eq!(exif_tiff(b"Exif\0"), None, "a prefix cut short"); + // and through a whole file, the iPhone way + let f = Heif::new().item(2, b"Exif", exif_item(b"Exif\0\0", t), Store::File, 1).cdsc(2, &[1]).build(); + assert_eq!(exif(&f).as_deref(), tiff); +} + +/// Items in several extents, in the file or the `meta` box's `idat`, with every `iloc` version +/// and field size. +#[test] +fn item_extents_and_construction_methods() { + let packet = xmp_with("Harbour at dusk"); + let rdf = ("application/rdf+xml", ""); + // EXIF in `idat` (method 1), XMP in the file in three extents, a 32-bit base offset + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::Idat, 1).cdsc(2, &[1]).xmp(3, rdf, packet.clone().into_bytes()); + h.items[2].extents = 3; + h.iloc = (1, [8, 4, 4, 0]); + let f = h.cdsc(3, &[1]).build(); + assert_eq!(read(&f).unwrap(), (Some(SWEEP_TIFF.to_vec()), Some(packet.clone()))); + // version 2: 32-bit item ids, an extent index, 64-bit lengths; EXIF in two extents + let mut h = Heif::new().item(70_000, b"Exif", exif_item(b"Exif\0\0", &SWEEP_TIFF), Store::File, 2).cdsc(70_000, &[70_001]); + (h.primary, h.items[0].id, h.wide, h.iloc) = (70_001, 70_001, true, (2, [4, 8, 0, 4])); + assert_eq!(exif(&h.build()).as_deref(), Some(SWEEP_TIFF.as_slice())); + // version 0: no construction method, file offsets only + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::File, 1).cdsc(2, &[1]); + h.iloc = (0, [4, 4, 0, 0]); + assert_eq!(exif(&h.build()).as_deref(), Some(SWEEP_TIFF.as_slice())); + // an XMP item in `idat` + let mut h = Heif::new().xmp(5, rdf, packet.clone().into_bytes()); + h.items[1].store = Store::Idat; + assert_eq!(xmp(&h.build()), Some(packet)); +} + +/// `iloc` entries the builder doesn't write: a length of 0 (to the end of the data), an item +/// in another file, an item built from other items, unknown versions and field sizes. +#[test] +fn iloc_entries_that_are_read_or_refused() { + let file = b"0123456789".as_slice(); + let idat = b"abcdefgh".as_slice(); + // version 1, offset and length 4 bytes, no base or index; one item with one extent + let iloc = |version: u8, sizes: [u8; 2], method: u16, data_ref: u16, offset: u32, length: u32| { + let mut l = vec![version, 0, 0, 0]; + l.extend_from_slice(&sizes); + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&7u16.to_be_bytes()); + if version > 0 { + l.extend_from_slice(&method.to_be_bytes()); + } + l.extend_from_slice(&data_ref.to_be_bytes()); + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&offset.to_be_bytes()); + l.extend_from_slice(&length.to_be_bytes()); + l + }; + let get = |l: &[u8]| item_bytes(file, l, Some(idat), 7); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 0, 2, 3)).as_deref(), Some(b"234".as_slice())); + assert_eq!(get(&iloc(1, [0x44, 0], 1, 0, 2, 3)).as_deref(), Some(b"cde".as_slice()), "from idat"); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 0, 6, 0)).as_deref(), Some(b"6789".as_slice()), "length 0: to the end of the file"); + assert_eq!(get(&iloc(1, [0x44, 0], 1, 0, 5, 0)).as_deref(), Some(b"fgh".as_slice()), "length 0: to the end of idat"); + assert_eq!(item_bytes(file, &iloc(1, [0x44, 0], 1, 0, 0, 1), None, 7), None, "method 1 without idat"); + assert_eq!(get(&iloc(1, [0x44, 0], 2, 0, 0, 1)), None, "method 2 (from other items)"); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 1, 0, 1)), None, "in another file"); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 0, 8, 3)), None, "past the end"); + assert_eq!(get(&iloc(1, [0x44, 0], 0, 0, 11, 0)), None, "starts past the end"); + assert_eq!(get(&iloc(1, [0x22, 0], 0, 0, 0, 1)), None, "2-byte fields are not in the standard"); + assert_eq!(get(&iloc(3, [0x44, 0], 0, 0, 0, 1)), None, "unknown version"); + assert_eq!(item_bytes(file, &iloc(1, [0x44, 0], 0, 0, 0, 1), None, 8), None, "no such item"); + // a base offset near u64::MAX plus an extent offset overflows: refused, not wrapped + let mut l = vec![1, 0, 0, 0, 0x44, 0x80]; + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&7u16.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0]); // method 0, data reference 0 + l.extend_from_slice(&u64::MAX.to_be_bytes()); + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&1u32.to_be_bytes()); + l.extend_from_slice(&1u32.to_be_bytes()); + assert_eq!(get(&l), None); +} + +/// The primary image's metadata is read; metadata tied only to another image is not. +#[test] +fn the_primary_images_metadata_is_read() { + let thumb = |h: Heif| h.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1); + // a thumbnail's EXIF comes first in the file; the primary image's is the one read + let f = thumb(Heif::new()) + .item(10, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Primary")), Store::File, 1) + .cdsc(10, &[2]) + .cdsc(11, &[1]) + .build(); + assert_eq!(exif(&f), Some(tiff_with("Primary"))); + // EXIF tied only to another image is not this photo's + let f = thumb(Heif::new()).item(10, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1).cdsc(10, &[2]).build(); + assert_eq!(exif(&f), None); + // tied to no image: it describes the file; tied to both: read + let f = Heif::new().item(10, b"Exif", exif_item(&[], &tiff_with("Whole file")), Store::File, 1).build(); + assert_eq!(exif(&f), Some(tiff_with("Whole file"))); + let f = thumb(Heif::new()).item(10, b"Exif", exif_item(&[], &tiff_with("Both")), Store::File, 1).cdsc(10, &[2, 1]).build(); + assert_eq!(exif(&f), Some(tiff_with("Both"))); + // a primary image's EXIF that can't be read gives way to one tied to no image + let f = Heif::new() + .item(10, b"Exif", exif_item(&[], b"damaged"), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Fallback")), Store::File, 1) + .cdsc(10, &[1]) + .build(); + assert_eq!(exif(&f), Some(tiff_with("Fallback"))); + // XMP: only uncompressed `application/rdf+xml` items, the primary image's first + let f = thumb(Heif::new()) + .xmp(20, ("image/jpeg", ""), b"\xff\xd8 not XMP".to_vec()) + .xmp(21, ("application/rdf+xml", "deflate"), b"compressed".to_vec()) + .xmp(22, ("application/rdf+xml", ""), xmp_with("Thumbnail").into_bytes()) + .xmp(23, (" Application/RDF+XML ", ""), xmp_with("Primary").into_bytes()) + .cdsc(22, &[2]) + .cdsc(23, &[1]) + .build(); + assert_eq!(xmp(&f), Some(xmp_with("Primary"))); + // 32-bit ids in `pitm`, `iinf` and `iref` + let mut h = Heif::new().item(70_000, b"Exif", exif_item(&[], &tiff_with("Wide")), Store::File, 1).cdsc(70_000, &[70_001]); + (h.primary, h.items[0].id, h.wide, h.iloc.0) = (70_001, 70_001, true, 2); + assert_eq!(exif(&h.build()), Some(tiff_with("Wide"))); +} + +/// EXIF tied to an image the primary is derived from (`dimg`: an `iden` turn, an `iovl` overlay, +/// a `tmap` tone map, a grid) is the primary image's, through any depth and despite loops. So is +/// EXIF in a file whose `pitm` names no item. A thumbnail's EXIF still isn't. +#[test] +fn metadata_of_the_images_a_primary_is_made_from_is_read() { + let base = |h: Heif| { + let mut h = h + .item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1) + .item(3, b"hvc1", b"coded picture".to_vec(), Store::File, 1) + .item(10, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Input")), Store::File, 1) + .cdsc(10, &[2]) + .cdsc(11, &[3]); + h.items[0].kind = b"iden"; + h + }; + // the primary (1) turns item 3 + let f = base(Heif::new()).iref(b"dimg", 1, &[3]).build(); + assert_eq!(exif(&f), Some(tiff_with("Input"))); + // two levels (a tone map of a grid of 3), and a loop back to the primary + let f = + base(Heif::new()).item(4, b"grid", Vec::new(), Store::File, 1).iref(b"dimg", 1, &[4]).iref(b"dimg", 4, &[3]).iref(b"dimg", 3, &[1]).build(); + assert_eq!(exif(&f), Some(tiff_with("Input"))); + // `pitm` names an item that doesn't exist: there is no primary image, so tied items count + let mut h = Heif::new().item(11, b"Exif", exif_item(&[], &tiff_with("Orphan")), Store::File, 1).cdsc(11, &[1]); + h.primary = 99; + assert_eq!(exif(&h.build()), Some(tiff_with("Orphan"))); + // the thumbnail is not an input of the primary: without the `dimg`, nothing is the photo's + assert_eq!(exif(&base(Heif::new()).build()), None); +} + +/// HEIF, HEIC and AVIF brands are read; other files built of the same boxes are not HEIF. +#[test] +fn heif_and_avif_brands() { + let with = |ftyp: &[u8]| { + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::File, 1).cdsc(2, &[1]); + h.ftyp = ftyp.to_vec(); + h.build() + }; + for ftyp in [&b"heic\0\0\0\0mif1heic"[..], b"mif1\0\0\0\0mif1", b"heix\0\0\0\0mif1heix", b"avif\0\0\0\0avifmif1miaf", b"msf1\0\0\0\0msf1hevc"] { + assert!(is_heif(&with(ftyp)), "{}", String::from_utf8_lossy(ftyp)); + assert_eq!(exif(&with(ftyp)).as_deref(), Some(SWEEP_TIFF.as_slice()), "{}", String::from_utf8_lossy(ftyp)); + } + // MP4 and QuickTime video, JPEG XL, Canon CR3 + for ftyp in [&b"isom\0\0\x02\0isomiso2mp41"[..], b"qt \0\0\0\0qt ", b"jxl \0\0\0\0jxl ", b"crx \0\0\0\x01crx isom", b"mif1\0\0\0\0crx "] { + assert!(!is_heif(&with(ftyp)), "{}", String::from_utf8_lossy(ftyp)); + assert!(read(&with(ftyp)).is_err() && scan(&with(ftyp)).is_err()); + } + assert!(!is_heif(b"\0\0\0\x0cftyp"), "no brand"); + assert!(!is_heif(&[0xff, 0xd8, 0xff, 0xe0, 0, 0x10, b'J', b'F', b'I', b'F', 0, 1]), "JPEG"); + // a HEIF file without a `meta` box has no metadata + let bare = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + assert!(matches!(read(&bare), Err(Error::Unsupported(_)))); + assert_eq!(sure(&bare), Some(Vec::new())); +} + +/// Damaged structure: an out-of-range box, an `iinf` or `iref` that claims more than it holds, +/// extents that overlap to gather more than [`MAX_ITEM`], an item count far past the box. +#[test] +fn malformed_bounds_are_refused() { + let good = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::File, 1).cdsc(2, &[1]).build(); + // the `meta` box says it is longer than the file: nothing is read + let mut f = good.clone(); + let at = u32::from_be_bytes(f[0..4].try_into().unwrap()) as usize; // `meta` follows `ftyp` + assert_eq!(&f[at + 4..at + 8], b"meta"); + f[at..at + 4].copy_from_slice(&u32::MAX.to_be_bytes()); + assert_eq!(exif(&f), None); + assert_eq!(sure(&f), None); + // an extent past the end of the file + let mut f = good.clone(); + f.truncate(f.len() - 1); + assert_eq!(exif(&f), None); + // 65,535 extents over the same 64 KiB: refused at MAX_ITEM, quickly + let file = vec![0u8; 64 << 10]; + let mut l = vec![1, 0, 0, 0, 0x44, 0]; + l.extend_from_slice(&1u16.to_be_bytes()); + l.extend_from_slice(&7u16.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0]); + l.extend_from_slice(&u16::MAX.to_be_bytes()); + for _ in 0..u16::MAX { + l.extend_from_slice(&0u32.to_be_bytes()); + l.extend_from_slice(&(file.len() as u32).to_be_bytes()); + } + assert_eq!(item_bytes(&file, &l, None, 7), None); + // an item count of 2^32 - 1 in a box holding one entry + let mut l = vec![2, 0, 0, 0, 0x44, 0]; + l.extend_from_slice(&u32::MAX.to_be_bytes()); + l.extend_from_slice(&1u32.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0, 0, 0]); + assert_eq!(item_bytes(&file, &l, None, 7), None); + // a `cdsc` claiming 65,535 targets with none there, an `infe` cut short + let mut iref = vec![0, 0, 0, 0]; + iref.extend(bx(b"cdsc", &[0, 2, 0xff, 0xff])); + let iref = bx(b"iref", &iref); + let mut items = vec![Candidate { id: 2, kind: Kind::Exif, describes: false, describes_primary: false, protected: false }]; + let mut budget = MAX_BOXES; + let b = boxes(&iref, 0, iref.len(), &mut budget); + link(&iref, &b[0], Some(1), &mut items, &mut budget); + assert!(!items[0].describes); + assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 0, b'E', b'x']), None, "cut short: the item type is unknown"); + assert_eq!(item_info(&[4, 0, 0, 0, 0, 2, 0, 0, b'E', b'x', b'i', b'f', 0]), None, "unknown infe version"); + assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x', b'i', b'f', 0]), Some((2, Some(Kind::Exif), true)), "protected"); + assert_eq!(item_info(&[2, 0, 0, 0, 0, 2, 0, 1, b'E', b'x']), None, "protected and cut short"); + // a full box's version is its own first byte, never the next box's + let two = [bx(b"iref", &[]), bx(b"free", &[1, 2, 3])].concat(); + let mut budget = MAX_BOXES; + let b = boxes(&two, 0, two.len(), &mut budget); + assert_eq!((version(&two, &b[0]), version(&two, &b[1])), (None, Some(1))); +} + +/// Box headers that lie: a 64-bit size, a `uuid` box too short for its extended type, a size +/// smaller than the header, a size of 0 (to the end). +#[test] +fn box_headers() { + let mut large = 1u32.to_be_bytes().to_vec(); + large.extend_from_slice(b"free"); + large.extend_from_slice(&20u64.to_be_bytes()); + large.extend_from_slice(b"abcd"); + let mut budget = MAX_BOXES; + let b = boxes(&large, 0, large.len(), &mut budget); + assert_eq!((b.len(), b[0].body, b[0].end), (1, 16, 20)); + large[8..16].copy_from_slice(&u64::MAX.to_be_bytes()); + assert!(boxes(&large, 0, large.len(), &mut budget).is_empty(), "a 64-bit size past the end"); + let short_uuid = bx(b"uuid", &[0; 8]); + assert!(boxes(&short_uuid, 0, short_uuid.len(), &mut budget).is_empty()); + let uuid = bx(b"uuid", &[0; 20]); + assert_eq!(boxes(&uuid, 0, uuid.len(), &mut budget)[0].body, 24); + assert!(boxes(&[0, 0, 0, 4, b'f', b'r', b'e', b'e'], 0, 8, &mut budget).is_empty(), "smaller than its header"); + let to_end = [&[0u8, 0, 0, 0][..], b"mdat", b"payload"].concat(); + assert_eq!(boxes(&to_end, 0, to_end.len(), &mut budget)[0].end, to_end.len()); + assert!(boxes(&to_end, 9, 4, &mut budget).is_empty(), "an empty range"); + let mut none = 0; + assert!(boxes(&to_end, 0, to_end.len(), &mut none).is_empty(), "no budget left"); +} + +/// Every cut-short or damaged file is read without a panic. +#[test] +fn truncated_and_damaged_files_never_panic() { + let mut h = Heif::new() + .item(2, b"Exif", exif_item(b"Exif\0\0", &SWEEP_TIFF), Store::Idat, 2) + .xmp(3, ("application/rdf+xml", ""), xmp_with("t").into_bytes()) + .cdsc(2, &[1]) + .cdsc(3, &[1]); + h.items[2].extents = 3; + h.iloc = (1, [4, 8, 4, 4]); + let f = h.build(); + assert!(matches!(read(&f), Ok((Some(_), Some(_))))); + for n in 0..f.len() { + let _ = read(&f[..n]); + let _ = scan(&f[..n]).map(Iterator::count); + } + for i in 0..f.len() { + for v in [0x00, 0xff, 0x7f, 0x01] { + let mut g = f.clone(); + g[i] = v; + let _ = read(&g); + let _ = scan(&g).map(Iterator::count); + } + } +} + +/// [`scan`] returns every EXIF and XMP item, not only the photo's own first EXIF: a thumbnail's +/// EXIF and XMP, the primary image's second EXIF item, a compressed XMP item. +#[test] +fn the_scan_returns_every_metadata_item() { + let plain = tiff_with("Primary"); + let thumb = |h: Heif| h.item(2, b"hvc1", b"thumbnail".to_vec(), Store::File, 1); + let ids = |f: &[u8]| sure(f).map(|items| items.into_iter().map(|(id, kind, _)| (id, kind)).collect::>()); + // the thumbnail's EXIF + let f = thumb(Heif::new()) + .item(10, b"Exif", exif_item(&[], &plain), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Thumbnail")), Store::File, 1) + .cdsc(10, &[1]) + .cdsc(11, &[2]) + .build(); + assert_eq!(exif(&f), Some(plain.clone()), "the photo's own EXIF"); + let all = sure(&f).unwrap(); + assert_eq!(all[1], (11, Kind::Exif, exif_item(&[], &tiff_with("Thumbnail")))); + // the primary image's second EXIF item + let f = Heif::new() + .item(10, b"Exif", exif_item(&[], &plain), Store::File, 1) + .item(11, b"Exif", exif_item(&[], &tiff_with("Second")), Store::File, 1) + .cdsc(10, &[1]) + .cdsc(11, &[1]) + .build(); + assert_eq!(exif(&f), Some(plain.clone())); + assert_eq!(ids(&f), Some(vec![(10, Kind::Exif), (11, Kind::Exif)])); + // a thumbnail's XMP (with place names, say) is not the photo's, but the scan has it + let f = thumb(Heif::new()) + .item(10, b"Exif", exif_item(&[], &plain), Store::File, 1) + .cdsc(10, &[1]) + .xmp(20, ("application/rdf+xml", ""), xmp_with("Thumbnail").into_bytes()) + .cdsc(20, &[2]) + .build(); + assert_eq!(xmp(&f), None, "the thumbnail's packet is not the primary image's"); + assert_eq!(sure(&f).unwrap()[1], (20, Kind::Xmp, xmp_with("Thumbnail").into_bytes())); + // a compressed XMP item comes back as stored, for the caller to judge + let f = Heif::new().xmp(20, ("application/rdf+xml", "deflate"), b"compressed".to_vec()).build(); + assert_eq!(sure(&f), Some(vec![(20, Kind::EncodedXmp, b"compressed".to_vec())])); + // an EXIF item cut off by the end of the file, one larger than the reader takes, more items + // than it looks at: not sure + let mut f = Heif::new().item(10, b"Exif", exif_item(&[], &plain), Store::File, 1).build(); + f.truncate(f.len() - 1); + assert_eq!(sure(&f), None, "cut off"); + let f = Heif::new().item(10, b"Exif", exif_item(&[], &vec![0; MAX_ITEM]), Store::File, 1).build(); + assert_eq!(scan(&f).unwrap().next().map(|it| it.data), Some(Err(Unreadable::TooLarge)), "too large"); + let mut h = Heif::new(); + for id in 0..(MAX_SCANNED as u32 + 1) { + h = h.item(10 + id, b"Exif", exif_item(&[], &plain), Store::File, 1); + } + let f = h.build(); + assert!(!scan(&f).unwrap().complete(), "too many items"); + assert_eq!(scan(&f).unwrap().count(), MAX_SCANNED); + assert_eq!(exif(&f), Some(plain), "read still finds the photo's EXIF"); +} + +/// Replace only the catalog; the real metadata remains in `idat` with unchanged `iloc`. +fn with_iinf(h: &Heif, iinf: Option<&[u8]>) -> Vec { + assert!(h.items.iter().filter(|it| it.kind == b"Exif" || it.kind == b"mime").all(|it| it.store == Store::Idat)); + let meta = h.meta(0); + let mut budget = MAX_BOXES; + let children: Vec = boxes(&meta, 12, meta.len(), &mut budget) + .iter() + .flat_map(|b| if &b.kind == b"iinf" { iinf.map(|data| bx(b"iinf", data)).unwrap_or_default() } else { bx(&b.kind, &meta[b.body..b.end]) }) + .collect(); + [bx(b"ftyp", &h.ftyp), full_box(b"meta", 0, &children)].concat() +} + +fn catalog(version: u8, count: u32, entries: &[u8]) -> Vec { + let mut data = vec![version, 0, 0, 0]; + put(&mut data, u64::from(count), if version == 0 { 2 } else { 4 }); + data.extend_from_slice(entries); + data +} + +fn catalog_entry(version: u8, id: u32, kind: &[u8; 4], fields: &[u8]) -> Vec { + let mut data = vec![version, 0, 0, 0]; + put(&mut data, u64::from(id), if version == 3 { 4 } else { 2 }); + data.extend_from_slice(&[0, 0]); + if version >= 2 { + data.extend_from_slice(kind); + } + data.extend_from_slice(fields); + data +} + +/// An `infe` cut anywhere, even one that keeps the id and part of the type, makes the scan unsure. +#[test] +fn item_catalog_truncated_type_is_uncertain() { + let tiff = tiff_with("Located"); + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff), Store::Idat, 1); + for version in [2, 3] { + let entry = catalog_entry(version, 2, b"Exif", b"\0"); + let intact = with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry)))); + assert_eq!(exif(&intact), Some(tiff.clone()), "infe v{version}"); + assert_eq!(sure(&intact), Some(vec![(2, Kind::Exif, exif_item(&[], &tiff))]), "infe v{version}"); + for end in 0..entry.len() { + let damaged = with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry[..end])))); + assert_eq!(sure(&damaged), None, "infe v{version}, cut at {end}"); + } + } +} + +/// An XMP entry cut in its fields or strings makes the scan unsure, in every `infe` and `iinf` version. +#[test] +fn item_catalog_truncated_fields_and_strings_are_uncertain() { + let packet = xmp_with("Located").into_bytes(); + let mut h = Heif::new().xmp(2, ("application/rdf+xml", ""), packet.clone()); + h.items[1].store = Store::Idat; + for version in 0..=3 { + // The optional content encoding is absent; name and content type are terminated. + let entry = catalog_entry(version, 2, b"mime", b"name\0application/rdf+xml\0"); + for table_version in [0, 1] { + let intact = with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry)))); + assert_eq!(sure(&intact), Some(vec![(2, Kind::Xmp, packet.clone())]), "infe v{version}, iinf v{table_version}"); + for end in 0..entry.len() { + let damaged = with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry[..end])))); + assert_eq!(sure(&damaged), None, "infe v{version}, iinf v{table_version}, cut at {end}"); + } + } + } +} + +/// A missing or cut catalog, unknown versions, unknown entry boxes, a count that doesn't match +/// the entries and unterminated strings all make the scan unsure. +#[test] +fn item_catalog_incomplete_walks_counts_and_versions_are_uncertain() { + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1); + let entry = catalog_entry(2, 2, b"Exif", b"\0"); + let intact = catalog(0, 1, &bx(b"infe", &entry)); + assert!(sure(&with_iinf(&h, Some(&intact))).is_some()); + let mut cases = vec![("missing catalog".to_string(), None)]; + for end in 0..intact.len() { + cases.push((format!("catalog cut at {end}"), Some(intact[..end].to_vec()))); + } + cases.push(("unsupported table version".into(), Some(catalog(2, 1, &bx(b"infe", &entry))))); + let mut unsupported = entry.clone(); + unsupported[0] = 4; + cases.push(("unsupported entry version".into(), Some(catalog(0, 1, &bx(b"infe", &unsupported))))); + cases.push(("unknown entry box".into(), Some(catalog(0, 1, &bx(b"free", &entry))))); + // A valid image entry cannot explain a declared but absent second entry. + let image = bx(b"infe", &catalog_entry(2, 1, b"hvc1", b"\0")); + for version in [0, 1] { + for count in [0, 2] { + cases.push((format!("iinf v{version}, count {count}"), Some(catalog(version, count, &image)))); + } + } + for (kind, fields) in [(b"hvc1", b"unterminated".as_slice()), (b"uri ", b"name\0unterminated"), (b"mime", b"name\0image/jpeg\0gzip")] { + cases.push((format!("unterminated {kind:?} field"), Some(catalog(0, 1, &bx(b"infe", &catalog_entry(2, 2, kind, fields)))))); + } + for (label, data) in cases { + assert_eq!(sure(&with_iinf(&h, data.as_deref())), None, "{label}"); + } +} + +/// An unreadable catalog entry doesn't stop [`read`] from returning the EXIF it could read. +#[test] +fn item_catalog_uncertainty_keeps_readable_exif() { + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Readable")), Store::Idat, 1).xmp( + 3, + ("application/rdf+xml", ""), + xmp_with("Located").into_bytes(), + ); + h.items[2].store = Store::Idat; + assert!(sure(&h.build()).is_some()); + let exif_entry = bx(b"infe", &catalog_entry(2, 2, b"Exif", b"\0")); + let broken = bx(b"infe", &catalog_entry(2, 3, b"mime", b"name\0application/rdf+")); + for entries in [[exif_entry.clone(), broken.clone()], [broken, exif_entry]] { + let f = with_iinf(&h, Some(&catalog(0, 2, &entries.concat()))); + assert_eq!(exif(&f), Some(tiff_with("Readable"))); + assert_eq!(sure(&f), None, "unreadable XMP after or before readable EXIF"); + } +} + +/// Catalogs without metadata items leave a complete, empty scan. +#[test] +fn item_catalog_benign_entries_and_empty_tables_are_complete() { + let h = Heif::new(); + assert_eq!(sure(&h.build()), Some(Vec::new()), "image only"); + for table_version in [0, 1] { + assert_eq!(sure(&with_iinf(&h, Some(&catalog(table_version, 0, &[])))), Some(Vec::new()), "empty catalog"); + for version in 0..=3 { + for fields in [b"\0image/jpeg\0".as_slice(), b"name\0image/jpeg\0\0", b"name\0image/jpeg\0gzip\0"] { + let entry = catalog_entry(version, 1, b"mime", fields); + assert_eq!(sure(&with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry))))), Some(Vec::new())); + } + } + for kind in [b"hvc1", b"av01", b"zzzz", b"uri "] { + let fields = if kind == b"uri " { b"name\0https://example.invalid/item\0".as_slice() } else { b"name\0" }; + let entry = catalog_entry(2, 1, kind, fields); + assert_eq!(sure(&with_iinf(&h, Some(&catalog(table_version, 1, &bx(b"infe", &entry))))), Some(Vec::new())); + } + } + // A protected (encrypted) image item is no metadata item. + let mut entry = catalog_entry(2, 1, b"hvc1", b"\0"); + entry[7] = 1; + assert_eq!(sure(&with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry))))), Some(Vec::new())); + // Complete top-level files without an item meta box have no item catalog to inspect. + assert_eq!(sure(&bx(b"ftyp", b"msf1\0\0\0\0msf1")), Some(Vec::new())); +} + +/// A protected (encrypted) `Exif` or XMP item is in the scan, as unreadable: the file holds +/// metadata that can't be checked. [`read`] leaves it out. +#[test] +fn protected_metadata_is_unreadable_not_absent() { + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1); + for (kind, fields, expected) in [(b"Exif", b"\0".as_slice(), Kind::Exif), (b"mime", b"name\0application/rdf+xml\0", Kind::Xmp)] { + for version in [2, 3] { + let mut entry = catalog_entry(version, 2, kind, fields); + let protection_at = if version == 3 { 9 } else { 7 }; + entry[protection_at] = 1; + let f = with_iinf(&h, Some(&catalog(0, 1, &bx(b"infe", &entry)))); + let s = scan(&f).unwrap(); + assert!(s.complete(), "{kind:?} v{version}"); + let found: Vec = s.collect(); + assert_eq!(found, [Item { id: 2, kind: expected, data: Err(Unreadable::Protected) }], "{kind:?} v{version}"); + assert_eq!(sure(&f), None, "{kind:?} v{version}"); + assert_eq!(read(&f).unwrap(), (None, None), "{kind:?} v{version}"); + } + } +} + +/// Running out of box budget, or a walk that stops before the end of its box, never makes a +/// scan complete. +#[test] +fn item_catalog_box_budget_and_enclosing_walks_cannot_clear_uncertainty() { + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("Located")), Store::Idat, 1); + let f = h.build(); + let ftyp = bx(b"ftyp", &h.ftyp); + let mut top_cut = f.clone(); + top_cut[ftyp.len()..ftyp.len() + 4].copy_from_slice(&u32::MAX.to_be_bytes()); + assert_eq!(sure(&top_cut), None, "meta box cut short"); + let hidden = [ftyp.clone(), bx(b"free", &[]).repeat(MAX_BOXES), h.meta(0)].concat(); + assert_eq!(sure(&hidden), None, "top-level budget hides meta"); + let hidden = [ftyp.clone(), full_box(b"meta", 0, &[bx(b"free", &[]).repeat(MAX_BOXES), h.meta(0)[12..].to_vec()].concat())].concat(); + assert_eq!(sure(&hidden), None, "meta budget hides catalog"); + let image = bx(b"infe", &catalog_entry(2, 1, b"hvc1", b"\0")); + let data = catalog(1, MAX_BOXES as u32, &image.repeat(MAX_BOXES)); + assert_eq!(sure(&with_iinf(&h, Some(&data))), None, "catalog budget"); + let hidden = [ftyp, full_box(b"meta", 0, &[vec![0; 3], h.meta(0)[12..].to_vec()].concat())].concat(); + assert_eq!(sure(&hidden), None, "incomplete meta child walk"); +} + +/// Replace `iloc` in a builder fixture. Metadata stays in `idat`, so changing the box sizes +/// cannot change its offsets. The coded picture is not used by these metadata tests. +fn with_iloc(h: &Heif, iloc: Option<&[u8]>) -> Vec { + assert!(h.items.iter().filter(|it| it.kind == b"Exif" || it.kind == b"mime").all(|it| it.store == Store::Idat)); + let meta = h.meta(0); + let mut budget = MAX_BOXES; + let children: Vec = boxes(&meta, 12, meta.len(), &mut budget) + .iter() + .flat_map(|b| if &b.kind == b"iloc" { iloc.map(|data| bx(b"iloc", data)).unwrap_or_default() } else { bx(&b.kind, &meta[b.body..b.end]) }) + .collect(); + [bx(b"ftyp", &h.ftyp), full_box(b"meta", 0, &children)].concat() +} + +/// The `iloc` bytes of a builder fixture. +fn iloc_of(f: &[u8]) -> Vec { + Container::read(f, MAX_SCANNED).unwrap().iloc.unwrap().data.to_vec() +} + +/// An `iloc` cut after a readable EXIF entry leaves the later XMP unresolved: [`read`] still +/// returns the EXIF, and the scan reports the XMP as unreadable. +#[test] +fn iloc_truncation_after_readable_exif_leaves_xmp_unreadable() { + let h = Heif::new().item(2, b"Exif", exif_item(&[], &tiff_with("First")), Store::Idat, 1); + assert!(sure(&h.build()).is_some(), "readable EXIF alone"); + let mut h = h.xmp(3, ("application/rdf+xml", ""), xmp_with("Located").into_bytes()); + h.items[2].store = Store::Idat; + let f = h.build(); + assert_eq!(sure(&f).map(|v| v.len()), Some(2), "the intact XMP"); + let iloc = iloc_of(&f); + let cut = with_iloc(&h, Some(&iloc[..iloc.len() - 1])); + assert_eq!(exif(&cut), Some(tiff_with("First")), "the first metadata item still reads"); + let found: Vec = scan(&cut).unwrap().collect(); + assert_eq!(found[1], Item { id: 3, kind: Kind::Xmp, data: Err(Unreadable::Placement) }, "the unresolved XMP isn't skipped"); +} + +/// A missing, cut or unsupported `iloc` leaves every metadata item unreadable. +#[test] +fn iloc_unresolved_metadata_is_unreadable() { + for kind in [b"Exif", b"mime"] { + let mut h = Heif::new().item(2, kind, b"unread metadata".to_vec(), Store::Idat, 1); + h.items[1].mime = ("application/rdf+xml", ""); + let iloc = iloc_of(&h.build()); + assert_eq!(sure(&with_iloc(&h, None)), None, "missing iloc: {kind:?}"); + // Every cut leaves the metadata entry unresolved, including cuts in its last extent. + for end in 0..iloc.len() { + assert_eq!(sure(&with_iloc(&h, Some(&iloc[..end]))), None, "{kind:?}: iloc cut at {end}"); + } + // Unsupported version or any one of the four field sizes. + for (at, value) in [(0, 3), (4, 0x24), (4, 0x42), (5, 0x20), (5, 0x02)] { + let mut bad = iloc.clone(); + bad[at] = value; + assert_eq!(sure(&with_iloc(&h, Some(&bad))), None, "{kind:?}: field {at} = {value}"); + } + } +} + +/// A partial `iloc` walk that placed one item doesn't make another, unplaced item absent. +#[test] +fn iloc_partial_walk_does_not_clear_unresolved_metadata() { + let h = Heif::new().item(2, b"Exif", Vec::new(), Store::Idat, 1).item(3, b"Exif", b"unread metadata".to_vec(), Store::Idat, 1); + let iloc = iloc_of(&h.build()); + // Picture: 16 bytes; empty EXIF: 8 bytes; final EXIF: 16 bytes. Keep the first EXIF + // intact while cutting the second entry at every byte, including its extent fields. + for end in 32..iloc.len() { + let cut = with_iloc(&h, Some(&iloc[..end])); + let partial = Container::read(&cut, MAX_SCANNED).unwrap(); + assert!(matches!(partial.gather(0, MAX_ITEM), Gathered::Bytes(b) if b.is_empty())); + assert_eq!(sure(&cut), None, "iloc cut at {end}"); + } +} + +/// A complete `iloc` that places no entry for an item, or one with no extents, is sure: the +/// item has no bytes. +#[test] +fn iloc_complete_walk_allows_unplaced_and_empty_items() { + let h = Heif::new().item(2, b"Exif", Vec::new(), Store::Idat, 1); + assert_eq!(sure(&h.build()), Some(vec![(2, Kind::Exif, Vec::new())]), "placed EXIF with no extents"); + // A well-formed empty table, or a complete table placing only a different item. + for version in 0..=2 { + let mut iloc = vec![version, 0, 0, 0, 0, 0]; + put(&mut iloc, 0, if version == 2 { 4 } else { 2 }); + assert_eq!(sure(&with_iloc(&h, Some(&iloc))), Some(Vec::new()), "empty iloc v{version}"); + let count_at = iloc.len() - 1; + iloc[count_at] = 1; + put(&mut iloc, 9, if version == 2 { 4 } else { 2 }); + if version > 0 { + put(&mut iloc, 0, 2); // construction method + } + iloc.extend_from_slice(&[0, 0, 0, 0]); // this file, no extents + assert_eq!(sure(&with_iloc(&h, Some(&iloc))), Some(Vec::new()), "absent item in complete iloc v{version}"); + } + // No metadata candidates: there is nothing to resolve, even without a usable iloc. + assert_eq!(sure(&Heif::new().build()), Some(Vec::new())); + for iloc in [None, Some([3, 0, 0, 0, 0, 0].as_slice())] { + ILOC_ENTRIES.with(|n| n.set(0)); + assert_eq!(sure(&with_iloc(&Heif::new(), iloc)), Some(Vec::new())); + assert_eq!(ILOC_ENTRIES.with(std::cell::Cell::get), 0); + } +} + +/// The `iloc` walk ends once every metadata item is placed, before a damaged later entry. +#[test] +fn iloc_stops_after_all_metadata_is_placed() { + let h = Heif::new().item(2, b"Exif", Vec::new(), Store::Idat, 1).item(3, b"hvc1", vec![1], Store::File, 1); + let iloc = iloc_of(&h.build()); + // The last picture entry is truncated. The only metadata entry is already complete. + let cut = with_iloc(&h, Some(&iloc[..32])); + ILOC_ENTRIES.with(|n| n.set(0)); + assert_eq!(sure(&cut), Some(vec![(2, Kind::Exif, Vec::new())])); + assert_eq!(ILOC_ENTRIES.with(std::cell::Cell::get), 2, "stop after the picture and the EXIF entry"); +} + +/// A placed item whose extents leave its data, or pass the limit, can't be read. +#[test] +fn iloc_resolved_extents_still_obey_bounds_and_budget() { + let h = Heif::new().item(2, b"Exif", b"abcd".to_vec(), Store::Idat, 1); + let f = h.build(); + let c = Container::read(&f, MAX_SCANNED).unwrap(); + assert!(matches!(c.gather(0, 4), Gathered::Bytes(b) if b == b"abcd")); + assert!(matches!(c.gather(0, 3), Gathered::TooLarge)); + let iloc = iloc_of(&f); + for (offset, length) in [(0u32, 5u32), (5, 0), (u32::MAX, 1)] { + let mut bad = iloc.clone(); + let end = bad.len(); + bad[end - 8..end - 4].copy_from_slice(&offset.to_be_bytes()); + bad[end - 4..].copy_from_slice(&length.to_be_bytes()); + let f = with_iloc(&h, Some(&bad)); + let c = Container::read(&f, MAX_SCANNED).unwrap(); + assert!(matches!(c.gather(0, MAX_ITEM), Gathered::Unreadable)); + assert_eq!(sure(&f), None); + } +} + +/// A file whose MAX_SCANNED `Exif` items each name one 64 KiB region 257 times: every item is +/// too large, and finding that out copies nothing, for [`scan`] (all 1,024 gathers) and for +/// [`read`] (its MAX_CANDIDATES). Before, each gather copied MAX_ITEM bytes before giving up, +/// about 16 GiB for the scan of a 2 MiB file. +#[test] +fn too_large_items_are_refused_before_copying() { + const REGION: u32 = 64 << 10; + const REPEATS: u16 = 257; + let count = MAX_SCANNED as u16; + let mut iinf = count.to_be_bytes().to_vec(); + for id in 1..=count { + let mut e = vec![2u8, 0, 0, 0]; + e.extend_from_slice(&id.to_be_bytes()); + e.extend_from_slice(&[0, 0]); + e.extend_from_slice(b"Exif\0"); + iinf.extend(bx(b"infe", &e)); + } + let iloc = |region_at: u32| { + let mut l = vec![0x44, 0x00]; + l.extend_from_slice(&count.to_be_bytes()); + for id in 1..=count { + l.extend_from_slice(&id.to_be_bytes()); + l.extend_from_slice(&[0, 0, 0, 0]); // method 0, this file + l.extend_from_slice(&REPEATS.to_be_bytes()); + for _ in 0..REPEATS { + l.extend_from_slice(®ion_at.to_be_bytes()); + l.extend_from_slice(®ION.to_be_bytes()); + } + } + full_box(b"iloc", 1, &l) + }; + let ftyp = bx(b"ftyp", b"heic\0\0\0\0mif1heic"); + let meta = |region_at| full_box(b"meta", 0, &[full_box(b"iinf", 0, &iinf), iloc(region_at)].concat()); + let region_at = (ftyp.len() + meta(0).len() + 8) as u32; + let f = [ftyp, meta(region_at), bx(b"mdat", &vec![0; REGION as usize])].concat(); + + COPIED.with(|c| c.set(0)); + let s = scan(&f).unwrap(); + assert!(s.complete()); + let found: Vec = s.collect(); + assert_eq!(found.len(), MAX_SCANNED); + assert!(found.iter().all(|it| it.data == Err(Unreadable::TooLarge))); + assert_eq!(COPIED.with(std::cell::Cell::get), 0, "scan copied bytes of items it refused"); + + COPIED.with(|c| c.set(0)); + assert_eq!(read(&f).unwrap(), (None, None)); + assert_eq!(COPIED.with(std::cell::Cell::get), 0, "read copied bytes of items it refused"); + + // an item that fits is copied once, extent by extent + let mut h = Heif::new().item(2, b"Exif", exif_item(&[], &SWEEP_TIFF), Store::Idat, 3); + h.iloc = (1, [4, 4, 4, 0]); + let f = h.build(); + COPIED.with(|c| c.set(0)); + assert_eq!(sure(&f), Some(vec![(2, Kind::Exif, exif_item(&[], &SWEEP_TIFF))])); + assert_eq!(COPIED.with(std::cell::Cell::get), SWEEP_TIFF.len() + 4); +} + +/// `iloc` is walked once, however many metadata items there are to place (one walk per item +/// took over a second on a 48 MB `iloc` with 64 items). +#[test] +fn iloc_is_walked_once() { + // 64 metadata items, none placed by `iloc`, and 20,000 entries for other items + let mut iinf = vec![0u8, 0, 0, 0]; + iinf.extend_from_slice(&64u16.to_be_bytes()); + for id in 0..64u16 { + let mut e = vec![2u8, 0, 0, 0]; + e.extend_from_slice(&(1000 + id).to_be_bytes()); + e.extend_from_slice(&[0, 0]); + e.extend_from_slice(if id % 2 == 0 { b"Exif\0" } else { b"mime\0application/rdf+xml\0" }); + iinf.extend(bx(b"infe", &e)); + } + let entries = 20_000usize; + let mut iloc = vec![2u8, 0, 0, 0, 0, 0]; + iloc.extend_from_slice(&(entries as u32).to_be_bytes()); + for _ in 0..entries { + iloc.extend_from_slice(&[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]); // item 1, method 0, this file, no extents + } + let meta = full_box(b"meta", 0, &[bx(b"iinf", &iinf), bx(b"iloc", &iloc)].concat()); + let f = [bx(b"ftyp", b"heic\0\0\0\0mif1heic"), meta].concat(); + let walked = |run: &dyn Fn(&[u8])| { + ILOC_ENTRIES.with(|n| n.set(0)); + run(&f); + ILOC_ENTRIES.with(std::cell::Cell::get) + }; + let n = walked(&|f| assert_eq!(read(f).unwrap(), (None, None))); + assert!(n <= entries, "read walked {n} entries of {entries}"); + let n = walked(&|f| assert_eq!(sure(f), Some(Vec::new()))); + assert!(n <= entries, "scan walked {n} entries of {entries}"); +} + +proptest::proptest! { + /// Arbitrary bytes behind a HEIF `ftyp`, or inside its `meta` box, never panic. + #[test] + fn arbitrary_boxes_never_panic(body in proptest::collection::vec(proptest::prelude::any::(), 0..512)) { + for f in [[bx(b"ftyp", b"heic\0\0\0\0mif1heic"), body.clone()].concat(), [bx(b"ftyp", b"heic\0\0\0\0mif1heic"), full_box(b"meta", 0, &body)].concat()] { + let _ = read(&f); + let _ = scan(&f).map(Iterator::count); + } + } +} diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index 3138d84b3..32d24f92c 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -13,7 +13,10 @@ workspace = true [features] corpus = [] default = [] +# Opt in to threaded AVIF encoding without enabling AVIF (or assembly) by itself. +threads = ["ravif?/threading", "rav1e?/threading"] sgi = [] +sunraster = [] avif = ["image/avif", "dep:ravif", "dep:rav1e", "dep:avif-serialize"] heif = ["dep:craft-heif"] # AVIF decode: craft-video's clean-room pure-Rust AV1 decoder. Native targets only (on wasm32 a diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 0b717fbe2..eb7c37362 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -43,7 +43,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | TIFF | yes | yes | U8, U16, F32 | all six (CMYK, CMYK+A included) | yes | yes (tag 34675) | no | yes (tag 700) | yes | yes (Description, Make, Model, Software, DateTime, Artist, Copyright) | no | `tiff` | | WebP | yes | yes | U8 | RGB, RGBA | yes | yes | yes | yes | no | no | yes (VP8, when `webp_lossless` is off) | `image-webp` / built-in VP8 | | GIF | yes | yes | U8 | RGBA | 1-bit | no | no | no | no | no | yes (256-colour palette) | `image` | -| BMP | yes | yes | U8 | RGB, RGBA | yes | no | no | no | no | no | no | `image` | +| BMP | yes | yes | U8 | RGB, RGBA | yes | no | no | no | yes (header pixels per metre) | no | no | `image` | | TGA | yes | yes | U8 | Gray, GrayA, RGB, RGBA | yes | no | no | no | no | no | no | `image` (read) / built in (RLE write, one scanline per packet) | | ICO | yes | yes | U8 | RGBA (at most 256×256) | yes | no | no | no | no | no | no | `image` | | DDS | yes | yes | U8 | Gray, GrayA, RGB, RGBA | yes | no | no | no | no | no | yes (BC1–BC3, when `EncodeOptions::dds` asks for one) | built in | @@ -57,6 +57,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | | DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | | Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | +| Sun Raster (`.ras`) | with feature `sunraster` (incl. RLE and colour maps) | with feature `sunraster` (uncompressed) | U8 | Gray, RGB, RGBA | yes | no | no | no | no | no | no | `sunraster` (off by default) | | SGI (`.sgi`, `.rgb`, `.bw`) | with feature `sgi` | with feature `sgi` (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | `sgi` (off by default) | | JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | no | no | no | no | no | no | built in | | AVIF | with feature `avif-decode` (native only) | only with feature `avif` | U8, U16 (10/12-bit decodes to U16; PQ/HLG to linear F32) | RGB, RGBA | yes (auxiliary alpha) | read (`colr` prof/rICC) | read | read | no | no | yes | `craft-video` AV1 decoder; `image` + `ravif` to write | @@ -78,7 +79,8 @@ the same. Palette BMP and PNG pixels decode as RGB(A) in `Image`; `bmp_palette` and `png_palette` expose the embedded table (and a PNG's transparent entry) so `photocraft-io` can open them in Indexed Color mode. `bmp_palette` returns the colours the header declares, not the decoder's 256-entry padding. -`encode_bmp_indexed` writes the inverse: indices plus a table as an 8-bit palette BMP. +`encode_bmp_indexed` writes the inverse: indices plus a table as an 8-bit palette BMP; +`encode_bmp_indexed_with_dpi` also writes a resolution in the header. ## Documented asymmetries and limitations @@ -235,6 +237,11 @@ embedded table (and a PNG's transparent entry) so `photocraft-io` can open them the EXIF and XMP resolution are rewritten to the image's (`export_exif`, `export_xmp`): the RATIONAL values in place, ResolutionUnit set to 2, an entry that can't be rewritten removed from IFD0 without moving any other data. IFD1 (the thumbnail's) is left as it is. +* **Resolution without decoding** (module `resolution_probe`): `file_resolution(bytes)` reads + the pixels per inch a JPEG, PNG, WebP, BMP or classic TIFF records from its headers alone, for + placing a picture at its physical size before (or without) decoding it. JPEG, PNG, WebP and + TIFF answer as `decode` would; BMP reads the info header's pixels per metre. Values outside + 1–100 000 ppi count as none. * **JPEG** * 8-bit only. Neither decoder backend supports 12-bit. * A file cut off inside its image data decodes leniently (a baseline JPEG's missing rows come diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index a67321d02..e2faaac4c 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -1,6 +1,7 @@ pub(crate) mod avif; #[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] pub(crate) mod avif_decode; +pub(crate) mod bmp; pub(crate) mod dds; pub(crate) mod dds_bc; pub(crate) mod deep_exr; @@ -22,6 +23,8 @@ pub(crate) mod png; pub(crate) mod pnm; #[cfg(feature = "sgi")] pub(crate) mod sgi; +#[cfg(feature = "sunraster")] +pub(crate) mod sunraster; pub(crate) mod tga; pub(crate) mod tiff; pub(crate) mod tiff_exif; diff --git a/crates/image-codecs/src/codecs/bmp.rs b/crates/image-codecs/src/codecs/bmp.rs new file mode 100644 index 000000000..239794a2c --- /dev/null +++ b/crates/image-codecs/src/codecs/bmp.rs @@ -0,0 +1,106 @@ +//! A BMP's resolution: `biXPelsPerMeter` and `biYPelsPerMeter`, the pixels per metre of the +//! BITMAPINFOHEADER and every later header (V4, V5, OS/2 2.x), at file offsets 38 and 42. The +//! 12-byte BITMAPCOREHEADER of OS/2 1.x has no such fields. The `image` crate reads and writes +//! the pixels only, and writes 0 here, which readers take as "no resolution". + +use super::png::dpi_of_ppm; + +/// Bytes before the fields: the 14-byte BITMAPFILEHEADER, then the DIB header's size, width, +/// height, planes, bit count, compression and image size. +const X_PPM: usize = 38; +const Y_PPM: usize = 42; +/// The shortest DIB header that carries the resolution (BITMAPINFOHEADER). +const INFO_HEADER: u32 = 40; +const METERS_PER_INCH: f64 = 0.0254; + +/// The little-endian 4 bytes at `at`, or `None` past the end of `bytes`. +fn le4(bytes: &[u8], at: usize) -> Option<[u8; 4]> { + bytes.get(at..at.checked_add(4)?)?.try_into().ok() +} + +/// Whether `bytes` start a BMP whose header has the resolution fields. +fn has_resolution_fields(bytes: &[u8]) -> bool { + bytes.starts_with(b"BM") && le4(bytes, Y_PPM).is_some() && le4(bytes, 14).map(u32::from_le_bytes).is_some_and(|dib| dib >= INFO_HEADER) +} + +/// The resolution in pixels per inch a BMP records, or `None` when its header has no fields for it +/// or either is 0 or negative (most writers leave them 0). A whole number of ppi stored rounded to +/// whole pixels per metre (300 ppi = 11811) reads back whole, as a PNG's `pHYs` does. +pub(crate) fn resolution(bytes: &[u8]) -> Option<(f32, f32)> { + if !has_resolution_fields(bytes) { + return None; + } + let field = |at: usize| le4(bytes, at).map(i32::from_le_bytes).and_then(|v| u32::try_from(v).ok()); + let (x, y) = (field(X_PPM)?, field(Y_PPM)?); + (x > 0 && y > 0).then(|| (dpi_of_ppm(x), dpi_of_ppm(y))) +} + +/// Pixels per metre for `ppi`, rounded as Photoshop and the PNG `pHYs` writer do (300 ppi = 11811), +/// or `None` for a resolution that has no positive whole number of pixels per metre. +fn ppm_of_dpi(ppi: f32) -> Option { + let ppm = (f64::from(ppi) / METERS_PER_INCH).round(); + (ppm.is_finite() && ppm >= 1.0).then(|| ppm.min(f64::from(i32::MAX)) as i32) +} + +/// Writes `dpi` into the resolution fields of the BMP in `bytes`. A header without the fields, or +/// a resolution that cannot be stored, leaves the bytes as they are. +pub(crate) fn set_resolution(bytes: &mut [u8], dpi: Option<(f32, f32)>) { + let Some((x, y)) = dpi.and_then(|(x, y)| Some((ppm_of_dpi(x)?, ppm_of_dpi(y)?))) else { return }; + if !has_resolution_fields(bytes) { + return; + } + for (at, ppm) in [(X_PPM, x), (Y_PPM, y)] { + if let Some(field) = bytes.get_mut(at..at + 4) { + field.copy_from_slice(&ppm.to_le_bytes()); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A 46-byte BMP start: file header, a DIB header of `dib` bytes, 11811 px/m each way. + fn start(dib: u32) -> Vec { + let mut b = vec![0u8; Y_PPM + 4]; + b[..2].copy_from_slice(b"BM"); + b[14..18].copy_from_slice(&dib.to_le_bytes()); + b[X_PPM..X_PPM + 4].copy_from_slice(&11811i32.to_le_bytes()); + b[Y_PPM..Y_PPM + 4].copy_from_slice(&11811i32.to_le_bytes()); + b + } + + #[test] + fn only_headers_with_the_fields_are_read_or_written() { + assert_eq!(resolution(&start(40)), Some((300.0, 300.0))); + assert_eq!(resolution(&start(124)), Some((300.0, 300.0)), "BITMAPV5HEADER"); + // OS/2 1.x's 12-byte core header has no resolution: those bytes are pixels or palette. + let mut core = start(12); + assert_eq!(resolution(&core), None); + set_resolution(&mut core, Some((72.0, 72.0))); + assert_eq!(core, start(12), "left as it is"); + // Too short, or not a BMP: nothing read, nothing written, no panic. + let mut short = start(40)[..Y_PPM + 3].to_vec(); + assert_eq!(resolution(&short), None); + set_resolution(&mut short, Some((72.0, 72.0))); + assert_eq!(short, start(40)[..Y_PPM + 3]); + let mut other = start(40); + other[0] = b'X'; + assert_eq!(resolution(&other), None); + set_resolution(&mut other, Some((72.0, 72.0))); + assert_eq!(&other[X_PPM..X_PPM + 4], &11811i32.to_le_bytes(), "not a BMP: left as it is"); + } + + #[test] + fn resolutions_without_a_whole_pixel_per_metre_are_not_written() { + for bad in [0.0, -300.0, 0.01, f32::NAN, f32::INFINITY] { + let mut b = start(40); + set_resolution(&mut b, Some((bad, 300.0))); + assert_eq!(b, start(40), "{bad}"); + } + // A huge one is capped at the field's maximum rather than wrapping negative. + let mut b = start(40); + set_resolution(&mut b, Some((1e12, 300.0))); + assert_eq!(&b[X_PPM..X_PPM + 4], &i32::MAX.to_le_bytes()); + } +} diff --git a/crates/image-codecs/src/codecs/deep_exr.rs b/crates/image-codecs/src/codecs/deep_exr.rs index 0cc69e261..634737816 100644 --- a/crates/image-codecs/src/codecs/deep_exr.rs +++ b/crates/image-codecs/src/codecs/deep_exr.rs @@ -304,19 +304,14 @@ pub(crate) fn decode_deep(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Res let bytes_per_sample: usize = exr_types.iter().map(|t| sample_bytes(*t)).sum(); let multipart = meta.requirements.has_multiple_layers; - // The offset tables follow the headers: `chunk_count` u64 per header, in header order. - // Our part's table starts after the tables of all earlier parts. - let mut table_pos = header_end(bytes)?; - for h_ in meta.headers.iter().take(header_index) { - table_pos = table_pos.saturating_add((h_.chunk_count.min(1 << 24) as u64).saturating_mul(8)); - } + // A chunk offset before the end of the last offset table points into table data. + let (table_pos, tables_end) = offset_tables(meta, bytes, header_index)?; let mut cur = Cursor::new(bytes); seek_to(&mut cur, table_pos)?; let mut offsets = Vec::with_capacity(header.chunk_count.min(1 << 20)); for _ in 0..header.chunk_count { offsets.push(u64(&mut cur)?); } - let tables_end = cur.position(); // Block geometry. let (tiles_x, tile_w, tile_h, slots) = match &header.blocks { @@ -565,6 +560,26 @@ fn header_end(bytes: &[u8]) -> Result { Ok(cur.position()) } +/// Where part `header_index`'s offset table starts and where the last part's table ends. The +/// tables follow the headers, `chunk_count` u64 per header in header order, and the chunks +/// follow the last table. Sizes that overflow, or tables that run past the end of the file, +/// are errors. +fn offset_tables(meta: &MetaData, bytes: &[u8], header_index: usize) -> Result<(u64, u64), CodecError> { + let mut table_pos = header_end(bytes)?; + let mut tables_end = table_pos; + for (i, h) in meta.headers.iter().enumerate() { + let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8)); + tables_end = len.and_then(|len| tables_end.checked_add(len)).ok_or_else(|| err("the offset tables are too large"))?; + if i < header_index { + table_pos = tables_end; + } + } + if tables_end > bytes.len() as u64 { + return Err(err(format!("the offset tables end at byte {tables_end}, past the end of the file"))); + } + Ok((table_pos, tables_end)) +} + /// Composite the deep samples into a flat image: per pixel, samples sorted by Z (stable; the /// file is assumed messy), front-to-back `over` with premultiplied colour, un-premultiplied /// into straight alpha at the end. Without any alpha channel the front-most sample wins, diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index 1b92ea9d9..415a5d717 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -198,6 +198,15 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) { data_end_mode(b, false) } +/// Whether JPEG `b` holds too little scan data for a `w` × `h` frame to be worth decoding: no scan +/// data at all, or under one byte for every [`MAX_PIXELS_PER_BYTE`] pixels (counted on the scan +/// data the marker walk crosses, so APP padding doesn't count). `zune-jpeg` allocates the whole +/// output before it reads a scan, so a few bytes could otherwise ask for a frame of any size. +pub(crate) fn scan_too_short(b: &[u8], w: u32, h: u32) -> bool { + let (end, scan_bytes) = data_end(b); + end == DataEnd::Empty || u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes +} + /// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused. pub(crate) fn reaches_eoi(b: &[u8]) -> bool { data_end_mode(b, true).0 == DataEnd::Complete @@ -322,6 +331,40 @@ fn without_extended_xmp(b: &[u8]) -> Option> { Some(out) } +/// Whether the first frame header of `b` samples its first component below the largest +/// horizontal or vertical sampling factor of the frame. zune-jpeg 0.5 sizes its upsampler's +/// scratch row from the first component and asserts on some such frames (jpeg-encoder writes +/// CMYK that way: C, M and Y at 1×1, K larger). `false` when no frame header can be read. +fn first_component_subsampled(b: &[u8]) -> bool { + if b.get(..2) != Some(&[0xFF, 0xD8]) { + return false; + } + let mut i = 2; + while let (Some(&0xFF), Some(&m)) = (b.get(i), b.get(i + 1)) { + match m { + 0xFF => i += 1, + 0x01 | 0xD0..=0xD8 => i += 2, + 0xD9 | 0xDA => return false, + _ => { + let Some(len) = b.get(i + 2..i + 4).map(|l| usize::from(u16::from_be_bytes([l[0], l[1]]))) else { return false }; + if matches!(m, 0xC0..=0xCF) && !matches!(m, 0xC4 | 0xC8 | 0xCC) { + let Some(seg) = b.get(i + 4..i + 2 + len.max(2)) else { return false }; + let n = usize::from(seg.get(5).copied().unwrap_or(0)); + let factors: Vec<(u8, u8)> = (0..n).filter_map(|c| seg.get(6 + c * 3 + 1)).map(|f| (f >> 4, f & 0x0F)).collect(); + let (Some(&(h0, v0)), Some(hmax), Some(vmax)) = + (factors.first(), factors.iter().map(|f| f.0).max(), factors.iter().map(|f| f.1).max()) + else { + return false; + }; + return h0 < hmax || v0 < vmax; + } + i += 2 + len; + } + } + } + false +} + /// Whether `craft-jpeg` (rather than `zune-jpeg`) decodes the file: arithmetic coding /// (SOF9–SOF11), lossless (SOF3), or a DCT frame whose precision isn't 8 bits (#4934, #5003). /// This includes 12-bit progressive Huffman (SOF2), which `craft-jpeg` supports. @@ -389,6 +432,30 @@ fn decode_craft_jpeg(bytes: &[u8], limits: &Limits, adobe_transform: Option, /// truncated or not, are far above this. const MAX_PIXELS_PER_BYTE: u64 = 2048; +/// Decode with craft-jpeg: the files [`uses_craft_jpeg`] routes away from `zune-jpeg`, and the +/// ones `zune-jpeg` panics on. +fn decode_whole(bytes: &[u8], limits: &Limits, meta: JpegMeta, end: DataEnd, scan_bytes: u64) -> Result { + // craft-jpeg sizes its buffers from the frame header before it reads a scan. + if scan_bytes == 0 { + return Err(err("the file has no scan data")); + } + if let Some((w, h, _)) = meta.frame + && end == DataEnd::Truncated + // Only truncated scans: arithmetic coding (SOF9–SOF11) can spend well under a bit on + // a flat block, so a complete scan may be shorter than this bound. + && u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes + { + return Err(err("the truncated scan is too short for the frame it declares")); + } + let (mut img, damaged) = decode_craft_jpeg(bytes, limits, meta.adobe_transform, meta.adobe_marker)?; + img.icc = meta.icc; + img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() }; + if end == DataEnd::Truncated || damaged { + img.warnings.push(DecodeWarning::Truncated { format: F }); + } + Ok(img) +} + /// The stored size and EXIF block [`decode`] would find, from the marker segments before the /// first scan: the first frame header's size, checked against `limits`, and the APP1 EXIF block. pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option>), CodecError> { @@ -407,26 +474,9 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result if let Some((w, h, nc)) = meta.frame { limits.check_bytes(w, h, u64::from(nc.max(1)))?; } - if uses_craft_jpeg(bytes) { - // craft-jpeg sizes its buffers from the frame header before it reads a scan. - if scan_bytes == 0 { - return Err(err("the file has no scan data")); - } - if let Some((w, h, _)) = meta.frame - && end == DataEnd::Truncated - // Only truncated scans: arithmetic coding (SOF9–SOF11) can spend well under a bit on - // a flat block, so a complete scan may be shorter than this bound. - && u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes - { - return Err(err("the truncated scan is too short for the frame it declares")); - } - let (mut img, damaged) = decode_craft_jpeg(bytes, limits, meta.adobe_transform, meta.adobe_marker)?; - img.icc = meta.icc; - img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() }; - if end == DataEnd::Truncated || damaged { - img.warnings.push(DecodeWarning::Truncated { format: F }); - } - return Ok(img); + // Where a panic aborts (wasm32), the frames zune-jpeg may panic on can't be caught below. + if uses_craft_jpeg(bytes) || (cfg!(panic = "abort") && first_component_subsampled(bytes)) { + return decode_whole(bytes, limits, meta, end, scan_bytes); } let options = DecoderOptions::default().set_strict_mode(false).set_max_width(65535).set_max_height(65535); let stripped = without_extended_xmp(bytes); @@ -452,7 +502,12 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result return Err(err(format!("{what} is too short for the frame it declares"))); } dec.set_options(dec.options().jpeg_set_out_colorspace(out_cs)); - let mut px = dec.decode().map_err(err)?; + // zune-jpeg 0.5 asserts on some subsampled four-component frames whose first component isn't + // the largest (its upsampler's scratch row is sized from that component): craft-jpeg reads them. + let Ok(decoded) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| dec.decode())) else { + return decode_whole(bytes, limits, meta, end, scan_bytes); + }; + let mut px = decoded.map_err(err)?; let expected = w as usize * h as usize * layout.channels(); if px.len() < expected { return Err(err("short pixel buffer")); @@ -1018,6 +1073,25 @@ mod tests { (v * 65535.0 / 4095.0).round() as u16 } + #[test] + fn first_component_subsampling() { + let sof = |comps: &[(u8, u8)]| { + let mut s = vec![0xFF, 0xD8, 0xFF, 0xE0, 0, 4, 0, 0, 0xFF, 0xC2, 0, (8 + 3 * comps.len()) as u8, 8, 0, 8, 0, 8, comps.len() as u8]; + for (i, &(h, v)) in comps.iter().enumerate() { + s.extend([i as u8 + 1, (h << 4) | v, 0]); + } + s + }; + assert!(first_component_subsampled(&sof(&[(1, 1), (1, 1), (1, 1), (2, 2)]))); + assert!(first_component_subsampled(&sof(&[(1, 2), (1, 2), (2, 1)]))); + assert!(!first_component_subsampled(&sof(&[(2, 2), (1, 1), (1, 1)]))); + assert!(!first_component_subsampled(&sof(&[(1, 1); 4]))); + assert!(!first_component_subsampled(&sof(&[]))); + assert!(!first_component_subsampled(&[0xFF, 0xD8, 0xFF, 0xDA, 0, 2])); + assert!(!first_component_subsampled(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 20, 8])); + assert!(!first_component_subsampled(b"not a jpeg")); + } + #[test] fn routing() { assert!(!uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); diff --git a/crates/image-codecs/src/codecs/sunraster.rs b/crates/image-codecs/src/codecs/sunraster.rs new file mode 100644 index 000000000..85c24acc5 --- /dev/null +++ b/crates/image-codecs/src/codecs/sunraster.rs @@ -0,0 +1,281 @@ +//! Sun Raster files (`.ras`, `.sun`): 1-bit, 8-bit (gray or colour-mapped), 24-bit RGB and 32-bit +//! RGBA, uncompressed or byte-encoded (RLE) on read; uncompressed on write. +//! +//! Follows the published rasterfile format: a 32-byte big-endian header, an optional colour map, +//! then rows padded to an even number of bytes. In the "standard" type (1) 24- and 32-bit pixels +//! are stored blue-first; in the "RGB" type (3) red-first. Other types (TIFF, IFF, experimental) +//! are reported as unsupported. + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::SunRaster; +const HEADER: usize = 32; +const MAGIC: u32 = 0x59a6_6a95; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, reason) +} + +fn be32(b: &[u8], i: usize) -> u32 { + u32::from_be_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) +} + +/// Whether `b` starts with a Sun rasterfile header. +pub(crate) fn looks_like_sunraster(b: &[u8]) -> bool { + b.len() >= HEADER && be32(b, 0) == MAGIC && matches!(be32(b, 12), 1 | 8 | 24 | 32) +} + +/// Expand byte-encoded data (`0x80` escapes a run) until `want` bytes are produced. +fn unrle(src: &[u8], want: usize) -> Result, CodecError> { + let mut out = Vec::new(); + out.try_reserve_exact(want).map_err(|_| bad("pixel buffer is too large"))?; + let mut i = 0; + while out.len() < want { + let b = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + if b != 0x80 { + out.push(b); + continue; + } + let n = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + if n == 0 { + out.push(0x80); + continue; + } + let v = *src.get(i).ok_or_else(|| bad("truncated RLE data"))?; + i += 1; + let count = usize::from(n) + 1; + if count > want - out.len() { + return Err(bad("RLE run passes the end of the image")); + } + out.resize(out.len() + count, v); + } + Ok(out) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !looks_like_sunraster(b) { + return Err(bad("invalid Sun raster header")); + } + let (w, h, depth, kind, map_type, map_len) = (be32(b, 4), be32(b, 8), be32(b, 12) as usize, be32(b, 20), be32(b, 24), be32(b, 28) as usize); + if !matches!(kind, 0..=3) { + return Err(CodecError::unsupported(F, format!("Sun raster type {kind} is not supported"))); + } + if map_type > 2 { + return Err(CodecError::unsupported(F, format!("Sun raster map type {map_type} is not supported"))); + } + let layout = match depth { + 1 | 8 if map_type != 1 => ChannelLayout::Gray, + 1 | 8 | 24 => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + }; + limits.check(w, h, layout, SampleType::U8)?; + let (wu, hu) = (w as usize, h as usize); + let stride = wu.checked_mul(depth).and_then(|n| n.checked_add(15)).ok_or_else(|| bad("image size overflow"))? / 16 * 2; + let want = stride.checked_mul(hu).ok_or_else(|| bad("image size overflow"))?; + let data_at = HEADER.checked_add(map_len).ok_or_else(|| bad("colour map size overflow"))?; + let map = b.get(HEADER..data_at).ok_or_else(|| bad("truncated colour map"))?; + let rest = &b[data_at..]; + let raw; + let rows: &[u8] = if kind == 2 { + raw = unrle(rest, want)?; + &raw + } else { + rest.get(..want).ok_or_else(|| bad("the file is too short for the declared image"))? + }; + let channels = layout.channels(); + let mut out = Vec::new(); + out.try_reserve_exact(wu * hu * channels).map_err(|_| bad("pixel buffer is too large"))?; + let entries = map_len / 3; + for y in 0..hu { + let row = &rows[y * stride..(y + 1) * stride]; + for x in 0..wu { + match depth { + 1 => { + let bit = (row[x / 8] >> (7 - x % 8)) & 1; + if map_type == 1 { + // The bit is an index into the (two-entry or longer) colour map. + let i = usize::from(bit); + if i >= entries { + return Err(bad("pixel index is outside the colour map")); + } + out.extend_from_slice(&[map[i], map[entries + i], map[2 * entries + i]]); + } else { + // A set bit is black. + out.push(if bit == 1 { 0 } else { 255 }); + } + } + 8 if map_type == 1 => { + let i = usize::from(row[x]); + if i >= entries { + return Err(bad("pixel index is outside the colour map")); + } + out.extend_from_slice(&[map[i], map[entries + i], map[2 * entries + i]]); + } + 8 => out.push(row[x]), + 24 => { + let p = &row[3 * x..3 * x + 3]; + out.extend_from_slice(&if kind == 3 { [p[0], p[1], p[2]] } else { [p[2], p[1], p[0]] }); + } + _ => { + let p = &row[4 * x..4 * x + 4]; + // In the standard types the first byte (X in XBGR) is unused, not alpha. + out.extend_from_slice(&if kind == 3 { [p[1], p[2], p[3], p[0]] } else { [p[3], p[2], p[1], 255] }); + } + } + } + } + Image::from_u8(w, h, layout, out) +} + +/// A buffer of `cap` bytes, or an error instead of an abort when the allocation cannot be made. +fn reserve(cap: usize) -> std::result::Result, std::collections::TryReserveError> { + let mut v = Vec::new(); + v.try_reserve_exact(cap)?; + Ok(v) +} + +/// Writes an uncompressed rasterfile: 8-bit gray, 24-bit RGB or 32-bit RGBA, in the RGB-ordered type. +pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(F, msg); + let img = src.converted(plan.layout, plan.sample); + if img.sample_type() != SampleType::U8 { + return Err(enc(format!("Sun raster stores 8 bits per channel, not {:?}", img.sample_type()))); + } + let depth: usize = match img.layout() { + ChannelLayout::Gray => 8, + ChannelLayout::Rgb => 24, + ChannelLayout::Rgba => 32, + l => return Err(enc(format!("no Sun raster layout for {l:?}"))), + }; + let (w, h) = img.dimensions(); + let (wu, hu) = (w as usize, h as usize); + let bytes = depth / 8; + let stride = (wu * bytes).next_multiple_of(2); + let len = stride.checked_mul(hu).and_then(|n| u32::try_from(n).ok()).ok_or_else(|| enc("image too large".into()))?; + let data = img.data(); + if data.len() != wu * hu * bytes { + return Err(enc("the image has the wrong number of samples".into())); + } + let mut out = reserve(HEADER.checked_add(len as usize).ok_or_else(|| enc("image too large".into()))?) + .map_err(|_| enc("the image is too large to encode".into()))?; + for v in [MAGIC, w, h, depth as u32, len, 3, 0, 0] { + out.extend_from_slice(&v.to_be_bytes()); + } + for y in 0..hu { + let row = &data[y * wu * bytes..(y + 1) * wu * bytes]; + if depth == 32 { + // Stored alpha first. + for p in row.as_chunks::<4>().0 { + out.extend_from_slice(&[p[3], p[0], p[1], p[2]]); + } + } else { + out.extend_from_slice(row); + } + out.resize(HEADER + (y + 1) * stride, 0); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn pattern(w: u32, h: u32, layout: ChannelLayout) -> Image { + let n = w as usize * h as usize * layout.channels(); + Image::from_u8(w, h, layout, (0..n).map(|i| ((i * 13 + i / 3) % 253) as u8).collect()).unwrap() + } + + #[test] + fn round_trips_gray_rgb_and_rgba_with_odd_widths() { + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let img = pattern(13, 7, layout); + let bytes = encode(&img, Plan { layout, sample: SampleType::U8 }).unwrap(); + assert!(looks_like_sunraster(&bytes)); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!((back.layout(), back.data()), (layout, img.data())); + } + } + + fn file(depth: u32, kind: u32, map: &[u8], map_type: u32, w: u32, h: u32, data: &[u8]) -> Vec { + let mut f = Vec::new(); + for v in [MAGIC, w, h, depth, data.len() as u32, kind, map_type, map.len() as u32] { + f.extend_from_slice(&v.to_be_bytes()); + } + f.extend_from_slice(map); + f.extend_from_slice(data); + f + } + + #[test] + fn standard_type_is_blue_first_and_rle_expands() { + let f = file(24, 1, &[], 0, 1, 1, &[1, 2, 3, 0]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[3, 2, 1]); + // 0x80 0x00 is a literal 0x80; 0x80 n v is n + 1 copies of v. The 2x2 image has stride 2. + let f = file(8, 2, &[], 0, 2, 2, &[0x80, 0x00, 0x80, 0x00, 0x80, 0x01, 7]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[0x80, 0x80, 7, 7]); + // A run that passes the end of the image is an error. + assert!(decode(&file(8, 2, &[], 0, 2, 2, &[0x80, 0x09, 7]), &Limits::default()).is_err()); + } + + #[test] + fn colour_mapped_and_one_bit_files() { + // Two entries: red plane, green plane, blue plane. + let map = [10, 20, 30, 40, 50, 60]; + let f = file(8, 1, &map, 1, 2, 1, &[0, 1]); + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10, 30, 50, 20, 40, 60][..])); + let f = file(8, 1, &map, 1, 1, 1, &[5, 0]); + assert!(decode(&f, &Limits::default()).is_err()); + let f = file(1, 1, &[], 0, 3, 1, &[0b1010_0000, 0]); + assert_eq!(decode(&f, &Limits::default()).unwrap().data(), &[0, 255, 0]); + } + + #[test] + fn one_bit_files_with_a_colour_map_read_as_colour() { + // Two entries: red plane, green plane, blue plane; bit 0 is entry 0. + let map = [10, 20, 30, 40, 50, 60]; + let f = file(1, 1, &map, 1, 3, 1, &[0b0100_0000, 0]); + let img = decode(&f, &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10, 30, 50, 20, 40, 60, 10, 30, 50][..])); + // A one-entry map has no entry for a set bit. + assert!(decode(&file(1, 1, &[1, 2, 3], 1, 1, 1, &[0b1000_0000, 0]), &Limits::default()).is_err()); + } + + #[test] + fn the_unused_first_byte_of_standard_32_bit_pixels_is_not_alpha() { + let img = decode(&file(32, 1, &[], 0, 1, 1, &[0, 30, 20, 10]), &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgba, &[10, 20, 30, 255][..])); + // In the RGB-ordered type the first byte is the alpha the writer stores there. + let img = decode(&file(32, 3, &[], 0, 1, 1, &[7, 10, 20, 30]), &Limits::default()).unwrap(); + assert_eq!(img.data(), &[10, 20, 30, 7]); + } + + #[test] + fn an_output_buffer_too_large_to_allocate_is_an_error() { + assert!(reserve(usize::MAX).is_err()); + assert!(reserve(16).is_ok()); + } + + #[test] + fn hostile_input_is_an_error_not_a_panic() { + let img = pattern(9, 5, ChannelLayout::Rgb); + let good = encode(&img, Plan { layout: ChannelLayout::Rgb, sample: SampleType::U8 }).unwrap(); + for len in 0..good.len() { + let _ = decode(&good[..len], &Limits::default()); + } + let mut huge = good.clone(); + huge[4..8].copy_from_slice(&u32::MAX.to_be_bytes()); + huge[8..12].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&huge, &Limits::default()).is_err()); + let mut bad_map = good; + bad_map[28..32].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(decode(&bad_map, &Limits::default()).is_err()); + assert!(matches!(decode(&file(24, 4, &[], 0, 1, 1, &[0; 4]), &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/codecs/tiff.rs b/crates/image-codecs/src/codecs/tiff.rs index fa279d9f0..bf59157eb 100644 --- a/crates/image-codecs/src/codecs/tiff.rs +++ b/crates/image-codecs/src/codecs/tiff.rs @@ -71,6 +71,18 @@ const TAG_DNG_VERSION: u16 = 50706; /// first IFD is often a reduced preview, so this runs before decoding. /// Reads only bounds-checked header bytes; anything malformed is "not raw". fn camera_raw(b: &[u8]) -> bool { + camera_raw_mode(b, false) +} + +/// The older LightCraft RAW/TIFF classification policy, sharing this bounded directory walk. +pub(crate) fn legacy_camera_raw(b: &[u8]) -> bool { + camera_raw_mode(b, true) +} + +fn camera_raw_mode(b: &[u8], legacy: bool) -> bool { + if legacy && b.get(8..10) == Some(b"CR") { + return true; + } let le = match b.get(0..4) { Some(b"II*\0") => true, Some(b"MM\0*") => false, @@ -92,8 +104,48 @@ fn camera_raw(b: &[u8]) -> bool { let value = |e: usize| match u16_at(e.saturating_add(2))? { 3 => u16_at(e.saturating_add(8)).map(u32::from), 4 | 13 => u32_at(e.saturating_add(8)), + 1 | 7 if legacy => b.get(e.saturating_add(8)).copied().map(u32::from), _ => None, }; + // The compatibility policy recognizes Sony lossless YCbCr only when IFD0 names Sony. + let is_sony = if legacy { + u32_at(4) + .and_then(|v| usize::try_from(v).ok()) + .and_then(|ifd| { + let n = usize::from(u16_at(ifd)?); + if n > 4096 { + return None; + } + for i in 0..n { + let e = ifd.checked_add(2)?.checked_add(i.checked_mul(12)?)?; + let tag = u16_at(e)?; + let ty = u16_at(e + 2)?; + let count = u32_at(e + 4)? as usize; + if tag == 0x010f { + let matches = (|| { + let size = match ty { + 1 | 2 | 6 | 7 => 1usize, + 3 | 8 => 2, + 4 | 9 | 11 | 13 => 4, + 5 | 10 | 12 => 8, + _ => return None, + }; + let len = size.checked_mul(count)?; + let start = if len <= 4 { e + 8 } else { u32_at(e + 8)? as usize }; + Some(b.get(start..start.checked_add(len)?)?.get(..4)?.eq_ignore_ascii_case(b"SONY")) + })() + .unwrap_or(false); + if matches { + return Some(true); + } + } + } + Some(false) + }) + .unwrap_or(false) + } else { + false + }; let mut queue: Vec = u32_at(4).into_iter().collect(); let mut visited = 0; while let Some(ifd) = queue.pop() { @@ -104,6 +156,44 @@ fn camera_raw(b: &[u8]) -> bool { } let ifd = ifd as usize; let Some(n) = u16_at(ifd) else { continue }; + if legacy + && (n > 4096 + || (n > 0 + && ifd + .checked_add(2) + .and_then(|p| usize::from(n - 1).checked_mul(12).and_then(|v| p.checked_add(v))) + .and_then(|p| p.checked_add(8)) + .is_none_or(|end| end > b.len()))) + { + continue; + } + if is_sony { + let mut cfa = false; + let (mut compression, mut photo, mut samples) = (None, None, None); + let (mut saw_compression, mut saw_photo, mut saw_samples) = (false, false, false); + for i in 0..usize::from(n) { + let e = ifd.saturating_add(2 + 12 * i); + match u16_at(e) { + Some(0x828e) => cfa = true, + Some(0x0103) if !saw_compression => { + saw_compression = true; + compression = value(e); + } + Some(0x0106) if !saw_photo => { + saw_photo = true; + photo = value(e); + } + Some(0x0115) if !saw_samples => { + saw_samples = true; + samples = value(e); + } + _ => {} + } + } + if cfa && compression == Some(7) && photo == Some(6) && samples == Some(3) { + return true; + } + } for i in 0..usize::from(n) { let e = ifd.saturating_add(2 + 12 * i); let (Some(tag), Some(count)) = (u16_at(e), u32_at(e.saturating_add(4))) else { @@ -111,6 +201,7 @@ fn camera_raw(b: &[u8]) -> bool { }; match tag { TAG_DNG_VERSION => return true, + 0x0103 if legacy && matches!(value(e), Some(32767 | 32769 | 32770 | 34316 | 34713 | 65000 | 65535)) => return true, TAG_PHOTOMETRIC if matches!(value(e), Some(PHOTOMETRIC_CFA | PHOTOMETRIC_LINEAR_RAW)) => { return true; } @@ -125,7 +216,7 @@ fn camera_raw(b: &[u8]) -> bool { _ => {} } } - if let Some(next) = u32_at(ifd.saturating_add(2 + 12 * usize::from(n))).filter(|&o| o != 0) { + if !legacy && let Some(next) = u32_at(ifd.saturating_add(2 + 12 * usize::from(n))).filter(|&o| o != 0) { queue.push(next); } } diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index 8c811dc08..983dab3dd 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -45,6 +45,8 @@ pub enum Format { Iff, /// JPEG 2000 (JP2 container or raw J2K codestream): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. Jp2, + /// Sun Raster (`.ras`): 1-, 8-, 24- and 32-bit, uncompressed or RLE. + SunRaster, /// SGI image files (`.sgi`, `.rgb`, `.bw`): 8- and 16-bit gray, gray + alpha, RGB and RGBA. Sgi, /// JPEG-LS (`.jls`): lossless and near-lossless gray and RGB. @@ -120,7 +122,7 @@ const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L:: impl Format { /// Every format known to the crate (enabled or not). - pub const ALL: [Format; 26] = [ + pub const ALL: [Format; 27] = [ Format::Png, Format::Jpeg, Format::Jp2, @@ -145,6 +147,7 @@ impl Format { Format::Cineon, Format::Dicom, Format::Iff, + Format::SunRaster, Format::Sgi, Format::JpegLs, ]; @@ -185,6 +188,7 @@ impl Format { Format::Dicom => "DICOM", Format::Iff => "Amiga IFF", Format::Jp2 => "JPEG 2000", + Format::SunRaster => "Sun Raster", Format::Sgi => "SGI", Format::JpegLs => "JPEG-LS", } @@ -218,6 +222,7 @@ impl Format { Format::Dicom => &["dcm", "dicom", "dc3"], Format::Iff => &["iff", "lbm", "ilbm"], Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"], + Format::SunRaster => &["ras", "sun"], Format::Sgi => &["sgi", "rgb", "rgba", "bw"], Format::JpegLs => &["jls"], } @@ -249,6 +254,7 @@ impl Format { Format::Dicom => "application/dicom", Format::Iff => "image/x-iff", Format::Jp2 => "image/jp2", + Format::SunRaster => "image/x-sun-raster", Format::Sgi => "image/x-sgi", Format::JpegLs => "image/jls", } @@ -302,7 +308,8 @@ pub fn caps(format: Format) -> FormatCaps { // Resolution travels in EXIF (WebP has no field of its own). Format::WebP => FormatCaps { icc: true, exif: true, xmp: true, dpi: true, animation: true, ..base }, Format::Gif => FormatCaps { layouts: &[L::Rgba], animation: true, lossy: true, ..base }, - Format::Bmp => base, + // Resolution in the header's pixels per metre (BITMAPINFOHEADER and later). + Format::Bmp => FormatCaps { dpi: true, ..base }, Format::Tga => FormatCaps { layouts: RGB_GRAY, ..base }, Format::Ico => FormatCaps { layouts: &[L::Rgba], ..base }, Format::Pnm => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: ALL_LAYOUTS, ..base }, @@ -327,6 +334,13 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + Format::SunRaster => FormatCaps { + read: cfg!(feature = "sunraster"), + write: cfg!(feature = "sunraster"), + layouts: &[L::Gray, L::Rgb, L::Rgba], + alpha: true, + ..base + }, Format::Sgi => { FormatCaps { read: cfg!(feature = "sgi"), write: cfg!(feature = "sgi"), depths: &[S::U8, S::U16], layouts: RGB_GRAY, alpha: true, ..base } } @@ -412,6 +426,10 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") { return Some(Format::Xpm); } + #[cfg(feature = "sunraster")] + if crate::codecs::sunraster::looks_like_sunraster(b) { + return Some(Format::SunRaster); + } #[cfg(feature = "sgi")] if crate::codecs::sgi::looks_like_sgi(b) { return Some(Format::Sgi); diff --git a/crates/image-codecs/src/jpeg_inks.rs b/crates/image-codecs/src/jpeg_inks.rs new file mode 100644 index 000000000..226703643 --- /dev/null +++ b/crates/image-codecs/src/jpeg_inks.rs @@ -0,0 +1,98 @@ +//! A JPEG's headers and four-component samples read the way `zune-jpeg` and libjpeg-style readers +//! give them, for apps that keep a CMYK JPEG's inks: the component count and ICC profile from the +//! headers alone, and the ink amounts of a CMYK or YCCK JPEG. +//! +//! [`crate::decode`] reads the same files differently, on purpose: it keeps the samples of a +//! four-component JPEG without Adobe's marker as they are, rounds its YCCK conversion, and hands +//! arithmetic-coded, lossless and 12-bit files to its whole-coefficient decoder. The readers here +//! follow the older convention instead, so a document that kept a CMYK JPEG's inks that way reads +//! the same: every four-component file is taken to hold Adobe's inverted samples, YCCK is turned +//! into CMYK as libjpeg does (truncating), and only the 8-bit Huffman-coded frames `zune-jpeg` +//! decodes are read. + +use zune_core::bytestream::ZCursor; +use zune_core::colorspace::ColorSpace; +use zune_core::options::DecoderOptions; +use zune_jpeg::JpegDecoder; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +/// A decoder of `bytes` whose headers have been read, with the default options (16384 pixels a +/// side at most); `None` when they aren't a JPEG or its headers can't be read. +fn headers(bytes: &[u8]) -> Option>> { + if !bytes.starts_with(&[0xFF, 0xD8]) { + return None; + } + let mut d = JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default()); + d.decode_headers().ok()?; + Some(d) +} + +/// The number of colour components the frame header of JPEG `bytes` declares, whatever it is; +/// `None` when they aren't a JPEG whose headers can be read. +pub fn jpeg_components(bytes: &[u8]) -> Option { + Some(usize::from(headers(bytes)?.info()?.components)) +} + +/// The width and height the frame header of JPEG `bytes` declares, for any colour type; `None` +/// when they aren't a JPEG whose headers can be read (which includes sides past 16384 pixels). +pub fn jpeg_dimensions(bytes: &[u8]) -> Option<(u32, u32)> { + let (w, h) = headers(bytes)?.dimensions()?; + Some((u32::try_from(w).ok()?, u32::try_from(h).ok()?)) +} + +/// The ICC profile of JPEG `bytes`, put together from all its numbered `ICC_PROFILE` APP2 +/// segments; `None` without one, when it is empty, and when the headers can't be read. +pub fn jpeg_icc_profile(bytes: &[u8]) -> Option> { + headers(bytes)?.icc_profile().filter(|icc| !icc.is_empty()) +} + +/// The ink amounts of a four-component (CMYK or YCCK) JPEG: C, M, Y and K bytes a pixel, 0 for +/// no ink, as a [`ChannelLayout::Cmyk`] image. The samples are taken to be Adobe's inverted ones +/// whether or not the file has Adobe's marker; YCCK is first turned into CMYK as libjpeg does +/// (the complement of the YCbCr colour's RGB, each value truncated). +/// +/// Errors: bytes that aren't a JPEG with readable headers ([`CodecError::InvalidImage`]); one of +/// other than four components, or a frame `zune-jpeg` doesn't decode, such as an arithmetic-coded +/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; scan data too short for the +/// frame (under a byte for every 2048 pixels, refused before anything is allocated); and data that +/// doesn't decode, +/// including frames `zune-jpeg` panics on, which are caught. +pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result { + let bad = |m: &str| CodecError::InvalidImage(format!("JPEG inks: {m}")); + let mut d = headers(bytes).ok_or_else(|| bad("not a JPEG with readable headers"))?; + let space = d.input_colorspace().ok_or_else(|| bad("no colour space"))?; + let components = d.info().map_or(0, |i| i.components); + if components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) { + return Err(CodecError::unsupported(Format::Jpeg, format!("inks of a {components}-component {space:?} image"))); + } + let (w, h) = d.dimensions().ok_or_else(|| bad("no size"))?; + let (w, h) = (u32::try_from(w).map_err(|_| bad("width"))?, u32::try_from(h).map_err(|_| bad("height"))?); + limits.check(w, h, ChannelLayout::Cmyk, SampleType::U8)?; + if crate::codecs::jpeg::scan_too_short(bytes, w, h) { + return Err(bad("the scan data is too short for the frame it declares")); + } + d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space)); + // zune-jpeg 0.5 asserts on some subsampled four-component frames whose first component isn't + // the largest (a scratch row too short for its upsampler): that is an error here, not a panic. + let decoded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| d.decode())); + let mut data = decoded + .map_err(|_| CodecError::unsupported(Format::Jpeg, "inks: zune-jpeg failed on this file"))? + .map_err(|e| CodecError::unsupported(Format::Jpeg, format!("inks: {e}")))?; + for p in data.as_chunks_mut::<4>().0 { + if space == ColorSpace::YCCK { + let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from); + // libjpeg's YCCK → CMYK: the complement of the YCbCr colour's RGB. + p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8; + p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8; + p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8; + } + for v in p.iter_mut() { + *v = 255 - *v; + } + } + Image::from_u8(w, h, ChannelLayout::Cmyk, data) +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 99f1edb47..dc2ac2933 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -26,15 +26,19 @@ mod fidelity; mod format; mod image; mod inspection; +pub mod jpeg_inks; pub mod jpeg_sef; mod lab; mod options; pub mod orientation; pub mod png_probe; +pub mod raw_metadata; pub mod resolution; +pub mod resolution_probe; pub mod source_settings; pub mod tiff_probe; pub mod web; +pub mod webp_probe; pub use crate::allocation::{probe_allocation_size, probe_stored_size}; pub use crate::cicp::Cicp; pub use crate::cicp::ContentLightLevel; @@ -64,12 +68,15 @@ pub use crate::options::{ }; pub use crate::orientation::{OrientSteps, exif_orientation, upright_exif, upright_xmp}; pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_resolution, xmp_resolution}; +pub use crate::resolution_probe::file_resolution; pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; #[cfg(feature = "sgi")] use crate::codecs::sgi; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +#[cfg(feature = "sunraster")] +use crate::codecs::sunraster; +use crate::codecs::{bmp, dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -108,6 +115,16 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Cineon => dpx::decode_cineon(bytes, l), Format::Dicom => dicom::decode(bytes, l), Format::Iff => iff::decode(bytes, l), + Format::SunRaster => { + #[cfg(feature = "sunraster")] + { + sunraster::decode(bytes, l) + } + #[cfg(not(feature = "sunraster"))] + { + Err(CodecError::unsupported(format, "Sun Raster support requires the sunraster feature")) + } + } Format::Sgi => { #[cfg(feature = "sgi")] { @@ -127,7 +144,11 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Avif => codecs::avif_decode::decode(bytes, l, opts.keep_orientation), #[cfg(not(all(feature = "avif-decode", not(target_arch = "wasm32"))))] Format::Avif => via_image::decode(format, bytes, l), - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::decode(format, bytes, l), + Format::Bmp => via_image::decode(format, bytes, l).map(|mut img| { + img.meta.dpi = bmp::resolution(bytes); + img + }), + Format::Gif | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr => via_image::decode(format, bytes, l), }?; finish_format(format, bytes, img, opts) } @@ -257,6 +278,12 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { Ok(if (5..=8).contains(&o) { (h, w) } else { (w, h) }) } +/// Classify classic TIFF camera RAWs using LightCraft's historical bounded heuristic. +/// This compatibility policy differs from the decoder's own RAW rejection policy. +pub fn legacy_tiff_raw_hint(bytes: &[u8]) -> bool { + tiff::legacy_camera_raw(bytes) +} + /// A JPEG's frame as its header declares it, read without decoding pixels: the stored size, the /// layout [`decode`] returns, whether [`decode`] hands the file to its whole-coefficient /// decoder (arithmetic-coded, lossless or 12-bit frames), whose working set is several times the @@ -304,10 +331,26 @@ pub fn bmp_palette(bytes: &[u8]) -> Result>, CodecError> { /// Encodes `indices` (row-major, one byte per pixel) as an 8-bit palette BMP with the colour /// table `palette` (1 to 256 entries). Used for Indexed Color documents; BMP has no transparency. +/// The header's resolution is left unset; [`encode_bmp_indexed_with_dpi`] writes one. pub fn encode_bmp_indexed(width: u32, height: u32, indices: &[u8], palette: &[[u8; 3]]) -> Result, CodecError> { via_image::encode_bmp_indexed(width, height, indices, palette) } +/// [`encode_bmp_indexed`] with the resolution `dpi` (pixels per inch, horizontal then vertical) +/// in the header's pixels per metre, as [`encode`] writes [`Metadata::dpi`] for a BMP. `None`, or +/// a resolution with no positive whole number of pixels per metre, leaves the fields unset. +pub fn encode_bmp_indexed_with_dpi( + width: u32, + height: u32, + indices: &[u8], + palette: &[[u8; 3]], + dpi: Option<(f32, f32)>, +) -> Result, CodecError> { + let mut bytes = via_image::encode_bmp_indexed(width, height, indices, palette)?; + bmp::set_resolution(&mut bytes, dpi); + Ok(bytes) +} + /// Lists the pages (image directories) of a TIFF or BigTIFF file without decoding pixels: the /// main IFD chain and its SubIFDs, each with its size, kind (page, reduced-resolution copy or /// mask) and storage. [`TiffInfo::default_page`] is the page [`decode`] opens. @@ -500,6 +543,16 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result dpx::encode_cineon(image, plan), Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), Format::Iff => iff::encode(image, plan), + Format::SunRaster => { + #[cfg(feature = "sunraster")] + { + sunraster::encode(image, plan) + } + #[cfg(not(feature = "sunraster"))] + { + Err(CodecError::unsupported(format, "Sun Raster support requires the sunraster feature")) + } + } Format::Sgi => { #[cfg(feature = "sgi")] { @@ -515,8 +568,12 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), Format::Jxl => jxl::encode(image, plan), - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => { - via_image::encode(format, image, plan, opts) - } + // The `image` crate leaves a BMP's resolution fields 0; Photoshop fills them in. They are + // part of the header, not metadata, so they are written whatever `embed_metadata` says. + Format::Bmp => via_image::encode(format, image, plan, opts).map(|mut bytes| { + bmp::set_resolution(&mut bytes, image.meta.dpi); + bytes + }), + Format::Gif | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::encode(format, image, plan, opts), } } diff --git a/crates/image-codecs/src/png_probe.rs b/crates/image-codecs/src/png_probe.rs index 7edda454d..73f1199eb 100644 --- a/crates/image-codecs/src/png_probe.rs +++ b/crates/image-codecs/src/png_probe.rs @@ -88,7 +88,7 @@ pub fn header_with_max_pixels(b: &[u8], max_pixels: u64) -> Result<(u32, u32, u1 if !after_idat { return Err(err("truncated image data")); } - Ok((w, h, exif.map_or(1, crate::exif_orientation))) + Ok((w, h, exif.map_or(1, craft_metadata::compat_orientation::orientation))) } /// Container color declarations preceding image data. Raw values leave color policy to callers. diff --git a/crates/image-codecs/src/raw_metadata.rs b/crates/image-codecs/src/raw_metadata.rs new file mode 100644 index 000000000..916a0078a --- /dev/null +++ b/crates/image-codecs/src/raw_metadata.rs @@ -0,0 +1,320 @@ +//! Explicit verbatim EXIF and JPEG APP metadata helpers for callers that preserve stored orientation. +//! Normal encoder APIs still normalize metadata for upright pixels; callers opt in to these helpers. +use crate::{CodecError, Format}; + +/// Borrowed metadata used to construct JPEG APP segments. +#[derive(Clone, Copy, Default)] +pub struct JpegMetadata<'a> { + /// TIFF-structured EXIF bytes. + pub exif: Option<&'a [u8]>, + /// XMP packet. + pub xmp: Option<&'a str>, + /// ICC profile bytes. + pub icc: Option<&'a [u8]>, + /// Pixels per inch, if nonzero. + pub ppi: Option, +} + +fn error(format: Format, message: &str) -> CodecError { + CodecError::encode(format, message) +} + +/// Build JFIF, EXIF, XMP and ICC APP payloads in that order, preserving bytes and segment limits. +pub fn jpeg_segments(meta: JpegMetadata<'_>) -> Result)>, CodecError> { + let mut segments = Vec::new(); + let density = meta.ppi.filter(|p| *p > 0).map_or([0, 0, 1, 0, 1], |p| { + let [hi, lo] = p.to_be_bytes(); + [1, hi, lo, hi, lo] + }); + segments.push((0xe0, [&b"JFIF\0\x01\x02"[..], &density, &[0, 0]].concat())); + if let Some(exif) = meta.exif { + if exif.len() > 65_527 { + return Err(error(Format::Jpeg, "EXIF larger than one APP1 segment (64 KiB)")); + } + segments.push((0xe1, [&b"Exif\0\0"[..], exif].concat())); + } + if let Some(xmp) = meta.xmp { + segments.push((0xe1, jpeg_xmp(xmp)?)); + } + if let Some(icc) = meta.icc { + let chunks: Vec<_> = icc.chunks(65_519).collect(); + if chunks.len() > 255 { + return Err(error(Format::Jpeg, "ICC profile too large for JPEG")); + } + for (i, chunk) in chunks.iter().enumerate() { + segments.push((0xe2, [&b"ICC_PROFILE\0"[..], &[i as u8 + 1, chunks.len() as u8], chunk].concat())); + } + } + Ok(segments) +} + +/// Build a single standard XMP APP1 payload, rejecting extended-XMP-sized packets. +pub fn jpeg_xmp(xmp: &str) -> Result, CodecError> { + let payload = [&b"http://ns.adobe.com/xap/1.0/\0"[..], xmp.as_bytes()].concat(); + if payload.len() > 65_533 { + return Err(error(Format::Jpeg, "XMP larger than one APP1 segment (extended XMP not supported)")); + } + Ok(payload) +} + +/// Restore bytes in the first existing JPEG EXIF payload when it has room. No segment is inserted. +/// +/// The EXIF APP1 is found by walking the marker segments before the first scan, so an `Exif\0\0` +/// inside another segment (a comment, say) is never taken for it, and the copy must fit inside +/// the APP1's own declared length, not merely inside the file. +pub fn replace_jpeg_exif(jpeg: &mut [u8], exif: &[u8]) { + if let Some(payload) = jpeg_exif_payload(jpeg) + && let Some(dst) = jpeg.get_mut(payload).and_then(|p| p.get_mut(..exif.len())) + { + dst.copy_from_slice(exif); + } +} + +/// The byte range of the TIFF data in the first EXIF APP1 (after `Exif\0\0`), up to the end of +/// that segment; `None` when the header segments end, are damaged, or hold no EXIF APP1. +fn jpeg_exif_payload(jpeg: &[u8]) -> Option> { + if jpeg.get(..2) != Some(&[0xff, 0xd8][..]) { + return None; + } + let mut p = 2usize; + loop { + // fill bytes (0xff) may precede a marker + while jpeg.get(p) == Some(&0xff) && jpeg.get(p + 1) == Some(&0xff) { + p += 1; + } + let (&0xff, &marker) = (jpeg.get(p)?, jpeg.get(p + 1)?) else { return None }; + match marker { + // TEM and RSTn carry no length + 0x01 | 0xd0..=0xd7 => p += 2, + // start of scan, end of image: no header segment follows + 0xda | 0xd9 | 0x00 => return None, + _ => { + let len = usize::from(u16::from_be_bytes([*jpeg.get(p + 2)?, *jpeg.get(p + 3)?])); + let end = p.checked_add(2)?.checked_add(len).filter(|&e| len >= 2 && e <= jpeg.len())?; + if marker == 0xe1 && jpeg.get(p + 4..end)?.starts_with(b"Exif\0\0") { + return Some(p + 10..end); + } + p = end; + } + } + } +} + +/// Replace the existing PNG eXIf chunk, preserving the other chunks and recomputing its CRC. +/// No chunk is inserted when one is absent. +pub fn replace_png_exif(png: &mut Vec, exif: &[u8]) -> Result<(), CodecError> { + let len = u32::try_from(exif.len()).map_err(|_| error(Format::Png, "EXIF too large"))?; + let mut p = 8usize; + while let Some(bytes) = p.checked_add(4).and_then(|end| png.get(p..end)) { + let size = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as usize; + let Some(end) = p.checked_add(12).and_then(|v| v.checked_add(size)).filter(|end| *end <= png.len()) else { break }; + if png.get(p + 4..p + 8) == Some(b"eXIf") { + let mut chunk = Vec::new(); + chunk.extend_from_slice(&len.to_be_bytes()); + chunk.extend_from_slice(b"eXIf"); + chunk.extend_from_slice(exif); + let crc = crc32fast::hash(&chunk[4..]); + chunk.extend_from_slice(&crc.to_be_bytes()); + png.splice(p..end, chunk); + break; + } + p = end; + } + Ok(()) +} + +/// Replace the existing WebP EXIF chunk, including padding and the RIFF size. No chunk is inserted +/// when one is absent. The input must be an encoded WebP as emitted by this crate. +pub fn replace_webp_exif(webp: &mut Vec, exif: &[u8]) -> Result<(), CodecError> { + let len = u32::try_from(exif.len()).map_err(|_| error(Format::WebP, "EXIF too large"))?; + let mut p = 12usize; + while let Some(h) = webp.get(p..p.saturating_add(8)) { + let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize; + let end = p.saturating_add(8).saturating_add(size).saturating_add(size & 1); + if &h[..4] == b"EXIF" && end <= webp.len() { + let new_len = webp + .len() + .checked_sub(end - p) + .and_then(|v| v.checked_add(8)) + .and_then(|v| v.checked_add(exif.len())) + .and_then(|v| v.checked_add(exif.len() & 1)) + .ok_or_else(|| error(Format::WebP, "RIFF too large"))?; + let riff_len = u32::try_from(new_len.saturating_sub(8)).map_err(|_| error(Format::WebP, "RIFF too large"))?; + let mut chunk = Vec::new(); + chunk.extend_from_slice(b"EXIF"); + chunk.extend_from_slice(&len.to_le_bytes()); + chunk.extend_from_slice(exif); + if exif.len() & 1 == 1 { + chunk.push(0); + } + webp.splice(p..end, chunk); + webp[4..8].copy_from_slice(&riff_len.to_le_bytes()); + break; + } + p = end; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + const EXIF: &[u8] = b"II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0"; + #[test] + fn replacements_match_previous_code_on_mutated_containers() { + fn old_png(png: &mut Vec, exif: &[u8]) { + let mut p = 8usize; + while let Some(b) = png.get(p..p + 4) { + let size = u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize; + let end = p.saturating_add(12).saturating_add(size); + if end > png.len() { + break; + } + if png.get(p + 4..p + 8) == Some(b"eXIf") { + let mut chunk = (exif.len() as u32).to_be_bytes().to_vec(); + chunk.extend_from_slice(b"eXIf"); + chunk.extend_from_slice(exif); + let mut crc = 0xffff_ffffu32; + for &v in &chunk[4..] { + crc ^= u32::from(v); + for _ in 0..8 { + crc = if crc & 1 != 0 { (crc >> 1) ^ 0xedb8_8320 } else { crc >> 1 }; + } + } + chunk.extend_from_slice(&(!crc).to_be_bytes()); + png.splice(p..end, chunk); + return; + } + p = end; + } + } + fn old_webp(webp: &mut Vec, exif: &[u8]) { + let mut p = 12usize; + while let Some(h) = webp.get(p..p.saturating_add(8)) { + let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize; + let end = p.saturating_add(8).saturating_add(size).saturating_add(size & 1); + if &h[..4] == b"EXIF" && end <= webp.len() { + let mut chunk = b"EXIF".to_vec(); + chunk.extend_from_slice(&(exif.len() as u32).to_le_bytes()); + chunk.extend_from_slice(exif); + if exif.len() & 1 == 1 { + chunk.push(0); + } + webp.splice(p..end, chunk); + let size = (webp.len().saturating_sub(8)) as u32; + webp[4..8].copy_from_slice(&size.to_le_bytes()); + return; + } + p = end; + } + } + let mut image = crate::Image::from_u8(2, 2, crate::ChannelLayout::Rgb, vec![80; 12]).unwrap(); + image.meta.exif = Some(EXIF.to_vec()); + for format in [Format::Jpeg, Format::Png, Format::WebP] { + let encoded = crate::encode(&image, format, &crate::EncodeOptions::default()).unwrap(); + for at in 0..encoded.len() { + for value in [0, 255] { + let mut original = encoded.clone(); + original[at] = value; + let mut shared = original.clone(); + if format == Format::Jpeg { + // the old byte search; the segment walk writes where it did or, when the + // mutation broke the segment structure or the APP1 length, not at all + let untouched = original.clone(); + if let Some(pos) = original.windows(6).position(|w| w == b"Exif\0\0") + && let Some(dst) = original.get_mut(pos + 6..pos + 6 + EXIF.len()) + { + dst.copy_from_slice(EXIF); + } + replace_jpeg_exif(&mut shared, EXIF); + assert!(shared == original || shared == untouched, "Jpeg: byte {at} value {value}"); + if at == 0 && value == 255 { + // SOI kept: a real replacement + assert_eq!(shared, original); + } + continue; + } else if format == Format::Png { + old_png(&mut original, EXIF); + replace_png_exif(&mut shared, EXIF).unwrap(); + } else { + old_webp(&mut original, EXIF); + replace_webp_exif(&mut shared, EXIF).unwrap(); + } + assert_eq!(shared, original, "{format:?}: byte {at} value {value}"); + } + } + } + } + + #[test] + fn explicit_replacements_preserve_raw_orientation_in_real_encodings() { + let mut image = crate::Image::from_u8(2, 2, crate::ChannelLayout::Rgb, vec![80; 12]).unwrap(); + image.meta.exif = Some(EXIF.to_vec()); + for format in [Format::Jpeg, Format::Png, Format::WebP] { + let mut bytes = crate::encode(&image, format, &crate::EncodeOptions::default()).unwrap(); + let normal = crate::decode_as_with(format, &bytes, &crate::DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!(normal.meta.exif.as_deref().map(crate::exif_orientation), Some(1), "default {format:?}"); + match format { + Format::Jpeg => replace_jpeg_exif(&mut bytes, EXIF), + Format::Png => replace_png_exif(&mut bytes, EXIF).unwrap(), + Format::WebP => replace_webp_exif(&mut bytes, EXIF).unwrap(), + _ => {} + } + let decoded = crate::decode_as_with(format, &bytes, &crate::DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!(decoded.meta.exif.as_deref().map(crate::exif_orientation), Some(6), "{format:?}"); + } + } + #[test] + fn jpeg_exif_is_found_by_segment_not_by_signature() { + fn segment(marker: u8, payload: &[u8]) -> Vec { + [&[0xff, marker][..], &((payload.len() + 2) as u16).to_be_bytes(), payload].concat() + } + let tail = [0xff, 0xda, 0, 2, 0xff, 0xd9]; + let exif_app1 = segment(0xe1, &[&b"Exif\0\0"[..], &[0; 26]].concat()); + // a comment carrying the signature comes first: only the APP1 is rewritten + let comment = segment(0xfe, &[&b"Exif\0\0"[..], &[7; 40]].concat()); + let mut jpeg = [&[0xff, 0xd8, 0xff][..], &comment, &exif_app1, &tail].concat(); + let before = jpeg.clone(); + replace_jpeg_exif(&mut jpeg, &[9; 26]); + let app1_data = 3 + comment.len() + 10; + assert_eq!(&jpeg[..app1_data], &before[..app1_data]); + assert_eq!(&jpeg[app1_data..app1_data + 26], &[9; 26]); + assert_eq!(&jpeg[app1_data + 26..], &before[app1_data + 26..]); + // no EXIF APP1, only the comment: nothing changes + let mut only_comment = [&[0xff, 0xd8][..], &comment, &tail].concat(); + let before = only_comment.clone(); + replace_jpeg_exif(&mut only_comment, &[9; 26]); + assert_eq!(only_comment, before); + // a replacement larger than the APP1 is refused even though the file continues + let mut small = [&[0xff, 0xd8][..], &exif_app1, &segment(0xe2, &[1; 64]), &tail].concat(); + let before = small.clone(); + replace_jpeg_exif(&mut small, &[9; 28]); + assert_eq!(small, before); + // markers without a length, damaged lengths, a scan before any APP1, and a non-JPEG + let mut tem = [&[0xff, 0xd8, 0xff, 0x01][..], &exif_app1, &tail].concat(); + replace_jpeg_exif(&mut tem, &[9; 26]); + assert_eq!(&tem[14..40], &[9; 26]); + for broken in [vec![0xff, 0xd8, 0xff, 0xe1, 0, 1], vec![0xff, 0xd8, 0xff, 0xe1, 0xff, 0xff, 0], tail.to_vec(), vec![0xff, 0xd8, 0xff, 0xda]] { + let mut b = broken.clone(); + replace_jpeg_exif(&mut b, &[9; 2]); + assert_eq!(b, broken); + } + let mut no_marker = [&[0xff, 0xd8, 0x12][..], &exif_app1].concat(); + let before = no_marker.clone(); + replace_jpeg_exif(&mut no_marker, &[9; 26]); + assert_eq!(no_marker, before); + } + + #[test] + fn jpeg_segments_keep_order_and_enforce_limits() { + let parts = jpeg_segments(JpegMetadata { exif: Some(EXIF), xmp: Some(""), icc: Some(&[1, 2]), ppi: Some(300) }).unwrap(); + assert_eq!(parts.iter().map(|v| v.0).collect::>(), [0xe0, 0xe1, 0xe1, 0xe2]); + assert!(jpeg_segments(JpegMetadata { exif: Some(&vec![0; 65_528]), ..Default::default() }).is_err()); + assert!(jpeg_xmp(&"x".repeat(65_533)).is_err()); + let profile = vec![42; 65_520]; + let chunks = jpeg_segments(JpegMetadata { icc: Some(&profile), ..Default::default() }).unwrap(); + assert_eq!(chunks.len(), 3); + assert_eq!(&chunks[1].1[12..14], &[1, 2]); + assert_eq!(&chunks[2].1[12..14], &[2, 2]); + } +} diff --git a/crates/image-codecs/src/resolution_probe.rs b/crates/image-codecs/src/resolution_probe.rs new file mode 100644 index 000000000..f7b9aff83 --- /dev/null +++ b/crates/image-codecs/src/resolution_probe.rs @@ -0,0 +1,126 @@ +//! The resolution a raster file records, read from its headers without decoding any pixels. +//! +//! A layout app placing a picture wants its physical size before (or without) decoding it: a +//! 600-pixel photo saved at 600 ppi places one inch wide. [`file_resolution`] answers from the +//! container alone: +//! +//! - JPEG: the resolution [`crate::decode`] reports, from the same marker scan (stopping at the +//! start of scan): Photoshop ResolutionInfo, then XMP, then EXIF, then the JFIF density. +//! - PNG: the `pHYs` chunk when its unit is the metre (read as the decoder reads it, so a stored +//! 11811 is 300 ppi), else the `eXIf` resolution; the chunk scan stops at `IDAT` or `IEND`. +//! - WebP: the IFD0 resolution of the `EXIF` chunk. +//! - BMP: the pixels-per-metre fields of a `BITMAPINFOHEADER` or later header. +//! - Classic TIFF: the IFD0 resolution tags. +//! +//! EXIF and TIFF resolution tags are read by [`crate::resolution::exif_resolution`]. Every scan +//! is bounded by the input length and a chunk count, and anything malformed, absent, +//! non-positive or past [`MAX_PPI`] gives `None`, so the caller keeps its own default. + +use crate::Limits; +use crate::codecs::{jpeg, png, webp}; +use crate::resolution::exif_resolution; + +/// Resolutions above this many pixels per inch are treated as corrupt. +pub const MAX_PPI: f32 = 100_000.0; + +/// PNG chunks looked at before giving up. +const MAX_CHUNKS: usize = 4096; + +const PNG_SIGNATURE: &[u8] = b"\x89PNG\r\n\x1a\n"; + +/// Pixels per inch (horizontal, vertical) that `bytes` records in its headers, or `None` when +/// the format is not one listed in the [module documentation](self) or carries no usable value. +pub fn file_resolution(bytes: &[u8]) -> Option<(f32, f32)> { + let ppi = if bytes.starts_with(b"\xFF\xD8\xFF") { + jpeg::scan_metadata(bytes).dpi + } else if bytes.starts_with(PNG_SIGNATURE) { + png_phys(bytes).or_else(|| png_exif(bytes)) + } else if bytes.starts_with(b"RIFF") && bytes.get(8..12) == Some(b"WEBP".as_slice()) { + webp_exif(bytes) + } else if bytes.starts_with(b"BM") { + bmp(bytes) + } else if is_classic_tiff(bytes) { + exif_resolution(bytes) + } else { + None + }; + ppi.filter(|&p| usable(p)) +} + +/// Whether both axes are finite and between 1 and [`MAX_PPI`]. +pub fn usable((x, y): (f32, f32)) -> bool { + let ok = |v: f32| v.is_finite() && (1.0..=MAX_PPI).contains(&v); + ok(x) && ok(y) +} + +fn is_classic_tiff(bytes: &[u8]) -> bool { + bytes.starts_with(b"II*\0") || bytes.starts_with(b"MM\0*") +} + +fn be_u32(b: &[u8], at: usize) -> Option { + Some(u32::from_be_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} + +fn le_u32(b: &[u8], at: usize) -> Option { + Some(u32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} + +fn le_i32(b: &[u8], at: usize) -> Option { + Some(i32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} + +fn png_phys(bytes: &[u8]) -> Option<(f32, f32)> { + let mut pos = PNG_SIGNATURE.len(); + for _ in 0..MAX_CHUNKS { + let len = usize::try_from(be_u32(bytes, pos)?).ok()?; + let data = pos.checked_add(8)?; + match bytes.get(pos.checked_add(4)?..data)? { + // A `pHYs` chunk holds exactly nine bytes; the whole chunk, CRC included, must be there. + b"pHYs" if len == 9 => return bytes.get(data..data.checked_add(13)?).and_then(|chunk| phys(chunk.get(..9)?)), + b"pHYs" => return None, + b"IDAT" | b"IEND" => return None, + _ => pos = data.checked_add(len)?.checked_add(4)?, + } + } + None +} + +/// `pHYs`: pixels per unit in x and y, then the unit (1 = metre; 0 = aspect ratio only). +fn phys(data: &[u8]) -> Option<(f32, f32)> { + let (x, y) = (be_u32(data, 0)?, be_u32(data, 4)?); + // As the decoder does: a zero count, or a unit other than the metre, records no resolution, + // so the `eXIf` resolution can still answer. + if *data.get(8)? != 1 || x == 0 || y == 0 { + return None; + } + Some((png::dpi_of_ppm(x), png::dpi_of_ppm(y))) +} + +/// The resolution of a PNG's `eXIf` chunk, read by the decoder's own header probe. +fn png_exif(bytes: &[u8]) -> Option<(f32, f32)> { + let (_, _, exif) = png::probe(bytes, &Limits::default()).ok()?; + exif_resolution(&exif?) +} + +/// The resolution of a WebP's `EXIF` chunk, read by the decoder's own container probe. +fn webp_exif(bytes: &[u8]) -> Option<(f32, f32)> { + let (_, _, exif) = webp::probe(bytes, &Limits::default()).ok()?; + exif_resolution(&exif?) +} + +/// BMP: the info header size at 14 (40 or more for a header with resolution fields), then the +/// signed pixels-per-metre values at 38 and 42. +fn bmp(bytes: &[u8]) -> Option<(f32, f32)> { + if le_u32(bytes, 14)? < 40 { + return None; + } + let (x, y) = (le_i32(bytes, 38)?, le_i32(bytes, 42)?); + if x <= 0 || y <= 0 { + return None; + } + Some((png::dpi_of_ppm(x.unsigned_abs()), png::dpi_of_ppm(y.unsigned_abs()))) +} + +#[cfg(test)] +#[path = "resolution_probe_tests.rs"] +mod tests; diff --git a/crates/image-codecs/src/resolution_probe_tests.rs b/crates/image-codecs/src/resolution_probe_tests.rs new file mode 100644 index 000000000..65a797853 --- /dev/null +++ b/crates/image-codecs/src/resolution_probe_tests.rs @@ -0,0 +1,266 @@ +#![allow(clippy::unwrap_used)] + +use super::{MAX_PPI, file_resolution, usable}; +use crate::{ChannelLayout, EncodeOptions, Format, Image, SampleType}; + +/// A little- or big-endian classic TIFF whose IFD0 holds XResolution and YResolution +/// (`x`/1, `y`/1) and, when given, ResolutionUnit. +fn tiff(le: bool, x: u32, y: u32, unit: Option) -> Vec { + let u16b = |v: u16| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + let u32b = |v: u32| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + let entries: u16 = if unit.is_some() { 3 } else { 2 }; + let rationals = 8 + 2 + 12 * u32::from(entries) + 4; + let mut t = Vec::new(); + t.extend_from_slice(if le { b"II*\0" } else { b"MM\0*" }); + t.extend_from_slice(&u32b(8)); + t.extend_from_slice(&u16b(entries)); + for (tag, offset) in [(0x011Au16, rationals), (0x011B, rationals + 8)] { + t.extend_from_slice(&u16b(tag)); + t.extend_from_slice(&u16b(5)); + t.extend_from_slice(&u32b(1)); + t.extend_from_slice(&u32b(offset)); + } + if let Some(unit) = unit { + t.extend_from_slice(&u16b(0x0128)); + t.extend_from_slice(&u16b(3)); + t.extend_from_slice(&u32b(1)); + t.extend_from_slice(&u16b(unit)); + t.extend_from_slice(&[0, 0]); + } + t.extend_from_slice(&u32b(0)); + for v in [x, 1, y, 1] { + t.extend_from_slice(&u32b(v)); + } + t +} + +fn segment(marker: u8, payload: &[u8]) -> Vec { + let mut s = vec![0xFF, marker]; + s.extend_from_slice(&u16::try_from(payload.len() + 2).unwrap().to_be_bytes()); + s.extend_from_slice(payload); + s +} + +fn jfif(units: u8, x: u16, y: u16) -> Vec { + let mut p = b"JFIF\0\x01\x02".to_vec(); + p.push(units); + p.extend_from_slice(&x.to_be_bytes()); + p.extend_from_slice(&y.to_be_bytes()); + p.extend_from_slice(&[0, 0]); + segment(0xE0, &p) +} + +fn exif_app1(tiff: &[u8]) -> Vec { + let mut p = b"Exif\0\0".to_vec(); + p.extend_from_slice(tiff); + segment(0xE1, &p) +} + +fn jpeg(segments: &[Vec]) -> Vec { + let mut j = vec![0xFF, 0xD8]; + for s in segments { + j.extend_from_slice(s); + } + // Start of scan, then entropy data holding bytes that look like an APP0 marker. + j.extend_from_slice(&segment(0xDA, &[1, 1, 0, 0, 63, 0])); + j.extend_from_slice(&jfif(1, 9, 9)); + j.extend_from_slice(&[0xFF, 0xD9]); + j +} + +fn png_chunk(kind: &[u8; 4], data: &[u8]) -> Vec { + let mut c = u32::try_from(data.len()).unwrap().to_be_bytes().to_vec(); + c.extend_from_slice(kind); + c.extend_from_slice(data); + c.extend_from_slice(&[0, 0, 0, 0]); // the CRC is not checked by a header probe + c +} + +fn png(chunks: &[Vec]) -> Vec { + let mut p = b"\x89PNG\r\n\x1a\n".to_vec(); + p.extend_from_slice(&png_chunk(b"IHDR", &[0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0])); + for c in chunks { + p.extend_from_slice(c); + } + p.extend_from_slice(&png_chunk(b"IEND", &[])); + p +} + +fn phys(x: u32, y: u32, unit: u8) -> Vec { + let mut d = x.to_be_bytes().to_vec(); + d.extend_from_slice(&y.to_be_bytes()); + d.push(unit); + png_chunk(b"pHYs", &d) +} + +fn bmp(header_size: u32, x: i32, y: i32) -> Vec { + let mut b = vec![0u8; 54]; + b[..2].copy_from_slice(b"BM"); + b[14..18].copy_from_slice(&header_size.to_le_bytes()); + b[38..42].copy_from_slice(&x.to_le_bytes()); + b[42..46].copy_from_slice(&y.to_le_bytes()); + b +} + +fn close(got: Option<(f32, f32)>, want: (f32, f32)) { + let (x, y) = got.unwrap(); + assert!((x - want.0).abs() < 0.05 && (y - want.1).abs() < 0.05, "{x} {y} vs {want:?}"); +} + +#[test] +fn jpeg_jfif_density_in_inches_and_centimetres() { + assert_eq!(file_resolution(&jpeg(&[jfif(1, 300, 150)])), Some((300.0, 150.0))); + close(file_resolution(&jpeg(&[jfif(2, 100, 100)])), (254.0, 254.0)); + // Units 0 is an aspect ratio, not a density. + assert_eq!(file_resolution(&jpeg(&[jfif(0, 1, 1)])), None); + // Nothing in the header: the APP0 look-alike after the start of scan is not read. + assert_eq!(file_resolution(&jpeg(&[])), None); +} + +#[test] +fn jpeg_exif_wins_over_jfif_and_non_exif_app1_is_skipped() { + let xmp = segment(0xE1, b"http://ns.adobe.com/xap/1.0/\0"); + let exif = exif_app1(&tiff(false, 600, 600, Some(2))); + assert_eq!(file_resolution(&jpeg(&[jfif(1, 72, 72), xmp.clone(), exif])), Some((600.0, 600.0))); + // An unusable EXIF value falls back to JFIF. + let zero = exif_app1(&tiff(true, 0, 0, None)); + assert_eq!(file_resolution(&jpeg(&[jfif(1, 96, 96), xmp, zero])), Some((96.0, 96.0))); +} + +#[test] +fn jpeg_fill_bytes_and_standalone_markers_are_stepped_over() { + let mut j = vec![0xFF, 0xD8, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xD0]; + j.extend_from_slice(&jfif(1, 200, 200)); + assert_eq!(file_resolution(&j), Some((200.0, 200.0))); +} + +#[test] +fn png_phys_in_metres_only_before_image_data() { + assert_eq!(file_resolution(&png(&[phys(11_811, 11_811, 1)])), Some((300.0, 300.0))); + assert_eq!(file_resolution(&png(&[phys(2, 1, 0)])), None); + assert_eq!(file_resolution(&png(&[png_chunk(b"IDAT", &[0; 4]), phys(11_811, 11_811, 1)])), None); + assert_eq!(file_resolution(&png(&[png_chunk(b"tEXt", b"a\0b"), phys(3_780, 3_780, 1)])).map(|p| p.0.round()), Some(96.0)); + assert_eq!(file_resolution(&png(&[])), None); +} + +/// What the probe reads is what a full decode reports, for each format the encoder writes with +/// a resolution. +#[test] +fn agrees_with_decode_for_encoded_files() { + let mut img = Image::from_raw(2, 1, ChannelLayout::Rgb, SampleType::U8, vec![10, 20, 30, 40, 50, 60]).unwrap(); + img.meta.dpi = Some((300.0, 150.0)); + for format in [Format::Jpeg, Format::Png, Format::WebP, Format::Tiff] { + let bytes = crate::encode(&img, format, &EncodeOptions::default()).unwrap(); + let decoded = crate::decode_as(format, &bytes).unwrap().meta.dpi; + assert_eq!(file_resolution(&bytes), decoded, "{format:?}"); + assert_eq!(decoded, Some((300.0, 150.0)), "{format:?}"); + } +} + +#[test] +fn webp_without_exif_has_none() { + let img = Image::from_raw(1, 1, ChannelLayout::Rgb, SampleType::U8, vec![1, 2, 3]).unwrap(); + let bytes = crate::encode(&img, Format::WebP, &EncodeOptions::default()).unwrap(); + assert_eq!(file_resolution(&bytes), None); + // A RIFF/WEBP container the decoder's probe refuses gives nothing either. + assert_eq!(file_resolution(b"RIFF\x04\0\0\0WEBP"), None); +} + +#[test] +fn bmp_pixels_per_metre() { + close(file_resolution(&bmp(40, 3_780, 7_559)), (96.0, 192.0)); + assert_eq!(file_resolution(&bmp(12, 3_780, 3_780)), None); + assert_eq!(file_resolution(&bmp(40, -3_780, 3_780)), None); + assert_eq!(file_resolution(&bmp(40, 0, 0)), None); +} + +#[test] +fn tiff_tags_in_both_byte_orders_and_units() { + assert_eq!(file_resolution(&tiff(true, 300, 300, None)), Some((300.0, 300.0))); + close(file_resolution(&tiff(false, 100, 50, Some(3))), (254.0, 127.0)); + assert_eq!(file_resolution(&tiff(true, 300, 300, Some(1))), None); +} + +#[test] +fn other_formats_and_absurd_values_give_none() { + assert_eq!(file_resolution(b"GIF89a\x01\0\x01\0"), None); + assert_eq!(file_resolution(b""), None); + assert_eq!(file_resolution(&tiff(true, 1_000_000, 1_000_000, None)), None); + assert!(usable((1.0, MAX_PPI))); + assert!(!usable((0.5, 72.0))); + assert!(!usable((f32::NAN, 72.0))); +} + +#[test] +fn truncated_and_corrupted_headers_never_panic() { + let samples = [ + jpeg(&[jfif(1, 300, 300), exif_app1(&tiff(false, 600, 600, Some(2)))]), + png(&[phys(11_811, 11_811, 1)]), + bmp(40, 3_780, 3_780), + tiff(false, 300, 300, Some(2)), + ]; + for s in &samples { + for cut in 0..=s.len() { + let _ = file_resolution(&s[..cut]); + } + for at in 0..s.len() { + for v in [0u8, 0x7F, 0xFF] { + let mut c = s.clone(); + c[at] = v; + let _ = file_resolution(&c); + } + } + } +} + +#[test] +fn png_phys_must_be_a_whole_nine_byte_chunk() { + // A pHYs that declares no data doesn't borrow the bytes after it. + let mut empty = png_chunk(b"pHYs", &[]); + empty.extend_from_slice(&phys(11_811, 11_811, 1)[8..]); + assert_eq!(file_resolution(&png(&[empty])), None); + // Cut off before its CRC. + let whole = png(&[phys(11_811, 11_811, 1)]); + let crc_at = whole.len() - 12 - 4; + assert_eq!(file_resolution(&whole[..crc_at]), None); + assert_eq!(file_resolution(&whole[..crc_at + 4]), Some((300.0, 300.0))); +} + +/// A PNG chunk with its real CRC, for the decoder's own probe, which checks it. +fn crc_chunk(kind: &[u8; 4], data: &[u8]) -> Vec { + let mut c = png_chunk(kind, data); + let body = c.get(4..c.len() - 4).unwrap().to_vec(); + let mut crc = !0u32; + for b in body { + crc ^= u32::from(b); + for _ in 0..8 { + crc = (crc >> 1) ^ (0xEDB8_8320 & (crc & 1).wrapping_neg()); + } + } + let n = c.len(); + c[n - 4..].copy_from_slice(&(!crc).to_be_bytes()); + c +} + +#[test] +fn png_zero_phys_leaves_the_exif_resolution() { + let exif = crc_chunk(b"eXIf", &tiff(false, 600, 600, Some(2))); + let zero = |x: u32, y: u32| { + let mut d = x.to_be_bytes().to_vec(); + d.extend_from_slice(&y.to_be_bytes()); + d.push(1); + crc_chunk(b"pHYs", &d) + }; + let png = |chunks: &[Vec]| { + let mut p = b"\x89PNG\r\n\x1a\n".to_vec(); + p.extend_from_slice(&crc_chunk(b"IHDR", &[0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0])); + chunks.iter().for_each(|c| p.extend_from_slice(c)); + // The decoder's probe reads up to the image data, so there has to be some. + p.extend_from_slice(&crc_chunk(b"IDAT", &[0x78, 0x9c, 0x63, 0, 0, 0, 1, 0, 1])); + p.extend_from_slice(&crc_chunk(b"IEND", &[])); + p + }; + assert_eq!(file_resolution(&png(std::slice::from_ref(&exif))), Some((600.0, 600.0))); + assert_eq!(file_resolution(&png(&[zero(0, 0), exif.clone()])), Some((600.0, 600.0))); + assert_eq!(file_resolution(&png(&[zero(11_811, 0), exif])), Some((600.0, 600.0))); +} diff --git a/crates/image-codecs/src/webp_probe.rs b/crates/image-codecs/src/webp_probe.rs new file mode 100644 index 000000000..07d6e0269 --- /dev/null +++ b/crates/image-codecs/src/webp_probe.rs @@ -0,0 +1,59 @@ +//! Header-only WebP inspection with image-chunk completeness checks. + +use crate::{CodecError, Format}; + +/// Stored width, height and EXIF orientation without decoding pixels. The image payload must +/// fit in the input, but its compressed contents are not validated. Uses the default decode limits. +pub fn header(bytes: &[u8]) -> Result<(u32, u32, u16), CodecError> { + let err = |s: &str| CodecError::malformed(Format::WebP, s); + let mut p = 12usize; + let mut has_image = false; + let mut exif = None; + while let Some(h) = bytes.get(p..p.saturating_add(8)) { + let size = u32::from_le_bytes([h[4], h[5], h[6], h[7]]) as usize; + let end = p.saturating_add(8).saturating_add(size); + if matches!(&h[..4], b"VP8 " | b"VP8L" | b"ANMF") { + if end > bytes.len() { + return Err(err("truncated image data")); + } + has_image = true; + } else if &h[..4] == b"EXIF" && end <= bytes.len() && exif.is_none() { + exif = bytes.get(p + 8..end); + } + p = end.saturating_add(size & 1); + } + if !has_image { + return Err(err("no image data")); + } + let (w, h) = crate::probe_size(bytes)?; + let orientation = exif.map_or(1, craft_metadata::compat_orientation::orientation); + let (w, h) = if (5..=8).contains(&orientation) { (h, w) } else { (w, h) }; + Ok((w, h, orientation)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn preserves_stored_dimensions_with_exif_orientation() { + let mut image = crate::Image::from_u8(2, 3, crate::ChannelLayout::Rgb, vec![42; 18]).unwrap(); + image.meta.exif = Some(b"II\x2a\x00\x08\x00\x00\x00\x01\x00\x12\x01\x03\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00".to_vec()); + let mut bytes = crate::encode(&image, Format::WebP, &crate::EncodeOptions::default()).unwrap(); + // The writer correctly makes exported pixels upright; simulate a camera file carrying orientation 6. + let exif = bytes.windows(4).position(|v| v == b"EXIF").unwrap(); + let tiff = exif + 8; + bytes[tiff + 18] = 6; + assert_eq!(header(&bytes).unwrap(), (2, 3, 6)); + } + + #[test] + fn rejects_missing_and_truncated_image_chunks() { + let mut bytes = b"RIFF\x00\x00\x00\x00WEBP".to_vec(); + assert!(header(&bytes).is_err()); + bytes.extend_from_slice(b"VP8 "); + bytes.extend_from_slice(&10u32.to_le_bytes()); + bytes.push(0); + assert!(header(&bytes).is_err()); + } +} diff --git a/crates/image-codecs/tests/it/bmp_resolution.rs b/crates/image-codecs/tests/it/bmp_resolution.rs new file mode 100644 index 000000000..2e35f222c --- /dev/null +++ b/crates/image-codecs/tests/it/bmp_resolution.rs @@ -0,0 +1,77 @@ +//! A BMP keeps its resolution in the header's `biXPelsPerMeter` and `biYPelsPerMeter`, which +//! Photoshop writes and reads (storytold/photocraft#13630). BMP exports left both 0, so a 300 ppi +//! document saved as BMP opened at 72 ppi elsewhere, and an opened BMP's resolution was ignored. + +use crate::common::test_image; +use craft_image_codecs::*; + +/// The DIB header's size and its pixels per metre, horizontal then vertical (file offsets 14, 38 +/// and 42, little-endian). +fn header(bytes: &[u8]) -> (u32, i32, i32) { + let at = |i: usize| [bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]; + (u32::from_le_bytes(at(14)), i32::from_le_bytes(at(38)), i32::from_le_bytes(at(42))) +} + +/// A test image of `layout` at resolution `dpi`. +fn image(layout: ChannelLayout, dpi: Option<(f32, f32)>) -> Image { + let mut img = test_image(layout, SampleType::U8); + img.meta.dpi = dpi; + img +} + +#[test] +fn bmp_export_writes_the_resolution_in_pixels_per_metre_and_reads_it_back() { + // RGB is written with a BITMAPINFOHEADER, RGBA with a BITMAPV4HEADER: same field offsets. + for (layout, dib) in [(ChannelLayout::Rgb, 40), (ChannelLayout::Rgba, 108)] { + // The fields are part of the header, not metadata: embed_metadata off keeps them. + for embed_metadata in [true, false] { + let opts = EncodeOptions { embed_metadata, ..Default::default() }; + let img = image(layout, Some((300.0, 150.0))); + let bytes = encode(&img, Format::Bmp, &opts).unwrap(); + // 300 / 0.0254 = 11811.02 and 150 / 0.0254 = 5905.51, rounded. + assert_eq!(header(&bytes), (dib, 11811, 5906), "{layout:?} metadata {embed_metadata}"); + assert_eq!(decode(&bytes).unwrap().meta.dpi, Some((300.0, 150.0)), "{layout:?} metadata {embed_metadata}"); + assert!(!fidelity_warnings_with(&img, Format::Bmp, &opts).contains(&FidelityWarning::DpiDropped), "{layout:?}"); + } + } +} + +#[test] +fn bmp_without_a_resolution_leaves_the_fields_unset() { + let bytes = encode(&image(ChannelLayout::Rgb, None), Format::Bmp, &EncodeOptions::default()).unwrap(); + assert_eq!(header(&bytes), (40, 0, 0)); + assert_eq!(decode(&bytes).unwrap().meta.dpi, None, "0 means no resolution: the caller's default applies"); +} + +#[test] +fn bmp_opens_at_the_resolution_its_header_records() { + let bytes = encode(&image(ChannelLayout::Rgb, None), Format::Bmp, &EncodeOptions::default()).unwrap(); + let with = |x: i32, y: i32| { + let mut b = bytes.clone(); + b[38..42].copy_from_slice(&x.to_le_bytes()); + b[42..46].copy_from_slice(&y.to_le_bytes()); + decode(&b).unwrap().meta.dpi + }; + // What other writers store for 96 and 72 ppi reads back whole. + assert_eq!(with(3780, 3780), Some((96.0, 96.0))); + assert_eq!(with(2835, 2835), Some((72.0, 72.0))); + // 40000 px/m is exactly 1016 ppi; a value no whole ppi rounds to stays as it is (71.98). + let (x, y) = with(40000, 2834).unwrap(); + assert_eq!(x, 1016.0); + assert!((y - 71.9836).abs() < 1e-3, "{y}"); + // An axis that is 0 or negative leaves the resolution unknown. + assert_eq!(with(11811, 0), None); + assert_eq!(with(-11811, 11811), None); +} + +#[test] +fn indexed_bmp_writes_the_resolution_it_is_given() { + let (idx, pal) = ([0u8, 1, 1, 0], [[0, 0, 0], [255, 0, 0]]); + let bytes = encode_bmp_indexed_with_dpi(2, 2, &idx, &pal, Some((300.0, 300.0))).unwrap(); + assert_eq!(header(&bytes), (40, 11811, 11811)); + assert_eq!(decode(&bytes).unwrap().meta.dpi, Some((300.0, 300.0))); + let unset = encode_bmp_indexed_with_dpi(2, 2, &idx, &pal, None).unwrap(); + assert_eq!(header(&unset), (40, 0, 0)); + // The existing function keeps writing no resolution, so its callers' files don't change. + assert_eq!(encode_bmp_indexed(2, 2, &idx, &pal).unwrap(), unset); +} diff --git a/crates/image-codecs/tests/it/deep_exr.rs b/crates/image-codecs/tests/it/deep_exr.rs index e614f865d..2e1105634 100644 --- a/crates/image-codecs/tests/it/deep_exr.rs +++ b/crates/image-codecs/tests/it/deep_exr.rs @@ -799,6 +799,106 @@ fn inconsistent_count_table_errors() { assert!(matches!(decode_deep_exr(&bytes, &Limits::none()), Err(CodecError::Malformed { .. }))); } +/// A two-part file made from the single-part deep file `deep` and a 1×1 flat scan-line part, +/// the deep part first when `deep_first`: each header gains `name` and `chunkCount`, the +/// version field the multipart bit, and each chunk its part number. The offset tables follow +/// the headers in header order, then the chunks. Returns the file, where the first table +/// starts, where the second table starts and where the last table ends. +fn two_part(deep: &[u8], deep_first: bool) -> (Vec, usize, usize, usize) { + let end = header_end_of(deep); + let first = u64::from_le_bytes(deep[end..end + 8].try_into().expect("8")) as usize; + let offsets: Vec = + (0..(first - end) / 8).map(|i| u64::from_le_bytes(deep[end + 8 * i..end + 8 * i + 8].try_into().expect("8")) as usize).collect(); + let deep_chunks: Vec<&[u8]> = offsets.iter().enumerate().map(|(i, &o)| &deep[o..offsets.get(i + 1).copied().unwrap_or(deep.len())]).collect(); + + let mut deep_header = deep[8..end - 1].to_vec(); + attr(&mut deep_header, "name", "string", b"deep"); + attr(&mut deep_header, "chunkCount", "int", &(deep_chunks.len() as i32).to_le_bytes()); + let mut flat_header = Vec::new(); + let mut chlist = b"R\0".to_vec(); + chlist.extend_from_slice(&1i32.to_le_bytes()); // half + chlist.extend_from_slice(&[0u8; 4]); // pLinear, reserved + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.push(0); + attr(&mut flat_header, "channels", "chlist", &chlist); + attr(&mut flat_header, "compression", "compression", &[0]); + let dw: Vec = [0i32; 4].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut flat_header, "dataWindow", "box2i", &dw); + attr(&mut flat_header, "displayWindow", "box2i", &dw); + attr(&mut flat_header, "lineOrder", "lineOrder", &[0]); + attr(&mut flat_header, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + attr(&mut flat_header, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat()); + attr(&mut flat_header, "screenWindowWidth", "float", &1f32.to_le_bytes()); + attr(&mut flat_header, "name", "string", b"flat"); + attr(&mut flat_header, "type", "string", b"scanlineimage"); + attr(&mut flat_header, "chunkCount", "int", &1i32.to_le_bytes()); + // The flat part's one chunk: y, the data size, one half sample. + let mut flat_chunk = 0i32.to_le_bytes().to_vec(); + flat_chunk.extend_from_slice(&2i32.to_le_bytes()); + flat_chunk.extend_from_slice(&f16::from_f32(0.5).to_le_bytes()); + let flat_chunks: Vec<&[u8]> = vec![&flat_chunk]; + + let parts: [(&[u8], &[&[u8]]); 2] = if deep_first { + [(&deep_header, &deep_chunks), (&flat_header, &flat_chunks)] + } else { + [(&flat_header, &flat_chunks), (&deep_header, &deep_chunks)] + }; + let mut out = deep[..4].to_vec(); + out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes()); + for (header, _) in &parts { + out.extend_from_slice(header); + out.push(0); // end of this header + } + out.push(0); // end of the headers + let first_table = out.len(); + let second_table = first_table + parts[0].1.len() * 8; + let tables_end = second_table + parts[1].1.len() * 8; + let mut pos = tables_end; + for (_, chunks) in &parts { + for c in chunks.iter() { + out.extend_from_slice(&(pos as u64).to_le_bytes()); + pos += 4 + c.len(); + } + } + for (part, (_, chunks)) in parts.iter().enumerate() { + for c in chunks.iter() { + out.extend_from_slice(&(part as i32).to_le_bytes()); + out.extend_from_slice(c); + } + } + (out, first_table, second_table, tables_end) +} + +/// The deep part of a two-part file decodes as the single-part file does, first or second. +/// Its chunks start after every part's offset table: a chunk offset into the other part's +/// table, or one byte before the end of the last table, is refused as table data, and a file +/// that ends inside the last table is refused even when the deep part's own table is whole. +#[test] +fn multipart_deep_chunks_start_after_every_offset_table() { + let single = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + let want = decode_deep_exr(&single, &Limits::none()).expect("single part"); + for deep_first in [true, false] { + let (bytes, ..) = two_part(&single, deep_first); + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("two parts"); + assert_eq!(deep.counts, want.counts); + assert_eq!(deep.channel("Z").expect("Z").samples, want.channel("Z").expect("Z").samples); + } + + let refused = |bytes: &[u8], what: &str| match decode_deep_exr(bytes, &Limits::none()) { + Err(CodecError::Malformed { message, .. }) => assert!(message.contains(what), "{message}"), + Err(e) => panic!("{e}, want an error about {what}"), + Ok(_) => panic!("decoded, want an error about {what}"), + }; + let (bytes, first_table, second_table, tables_end) = two_part(&single, true); + for off in [second_table, tables_end - 1] { + let mut into_table = bytes.clone(); + into_table[first_table..first_table + 8].copy_from_slice(&(off as u64).to_le_bytes()); + refused(&into_table, "outside the chunk area"); + } + refused(&bytes[..tables_end - 4], "past the end of the file"); +} + // --------------------------------------------------------------------------------------------- // Real reference files: the deep test images of the OpenEXR repository at commit // `69b2604fc76e370615438bdc8d2cd95b9349c12e`, read from `corpus/exr/` at the repo root with the diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs index c74cff318..bbe518df6 100644 --- a/crates/image-codecs/tests/it/detect_caps.rs +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -62,6 +62,8 @@ fn default_build_formats_all_symmetric() { Format::Dpx, Format::Cineon, Format::Iff, + #[cfg(feature = "sunraster")] + Format::SunRaster, #[cfg(feature = "sgi")] Format::Sgi, Format::JpegLs, @@ -258,6 +260,20 @@ fn detect_magic_jpeg_ls() { assert!(decode_as(Format::JpegLs, &[0xFF, 0xD8, 0xFF, 0xF7]).is_err()); } +#[cfg(feature = "sunraster")] +#[test] +fn detect_magic_sun_raster() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; + let img = Image::from_u8(5, 3, ChannelLayout::Rgb, (0..45).collect()).unwrap(); + let bytes = encode(&img, Format::SunRaster, &EncodeOptions::default()).unwrap(); + assert_eq!(detect(&bytes), Some(Format::SunRaster)); + assert_eq!(from_extension("scan.RAS"), Some(Format::SunRaster)); + assert_eq!(decode(&bytes).unwrap().data(), img.data()); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::SunRaster)); + assert_eq!(detect(&[0x59, 0xa6, 0x6a, 0x95]), None); + assert!(decode_as(Format::SunRaster, &[0x59, 0xa6, 0x6a, 0x95]).is_err()); +} + #[test] fn detect_magic_avif() { assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif)); @@ -360,3 +376,15 @@ fn sgi_requires_explicit_feature() { header[..12].copy_from_slice(&[1, 218, 0, 1, 0, 2, 0, 1, 0, 1, 0, 1]); assert_eq!(detect(&header), None); } + +#[cfg(not(feature = "sunraster"))] +#[test] +fn sun_raster_disabled_by_default() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Image, encode}; + let bytes = [0x59, 0xa6, 0x6a, 0x95, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 42, 0]; + assert_ne!(detect(&bytes), Some(Format::SunRaster)); + assert!(!caps(Format::SunRaster).read && !caps(Format::SunRaster).write); + assert!(decode_as(Format::SunRaster, &bytes).is_err()); + let image = Image::from_u8(1, 1, ChannelLayout::Gray, vec![42]).unwrap(); + assert!(encode(&image, Format::SunRaster, &EncodeOptions::default()).is_err()); +} diff --git a/crates/image-codecs/tests/it/jpeg_inks.rs b/crates/image-codecs/tests/it/jpeg_inks.rs new file mode 100644 index 000000000..b7cdfe699 --- /dev/null +++ b/crates/image-codecs/tests/it/jpeg_inks.rs @@ -0,0 +1,215 @@ +//! `jpeg_inks`: four-component JPEGs read as Adobe-inverted inks, as `zune-jpeg`-based readers +//! kept them, beside `decode`'s own reading. + +use craft_image_codecs::jpeg_inks::{decode_jpeg_inks, jpeg_components, jpeg_dimensions, jpeg_icc_profile}; +use craft_image_codecs::*; + +/// A `w` × `h` four-component JPEG of `samples` at quality 100, written by `jpeg-encoder` as +/// `ty`, with ICC profile `icc` when given. +fn four(w: u16, h: u16, samples: &[u8], ty: jpeg_encoder::ColorType, icc: Option<&[u8]>) -> Vec { + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, 100); + enc.set_sampling_factor(jpeg_encoder::SamplingFactor::R_4_4_4); + if let Some(icc) = icc { + enc.add_icc_profile(icc).unwrap(); + } + enc.encode(samples, w, h, ty).unwrap(); + out +} + +/// Flat 8×8 blocks of ink, so the coding keeps each block's value. +fn blocks(w: usize, h: usize) -> Vec { + (0..w * h) + .flat_map(|i| { + let b = ((i % w) / 8 + (i / w) / 8 * 7) as u8; + [b.wrapping_mul(37), b.wrapping_mul(91), 255 - b.wrapping_mul(13), b.wrapping_mul(53)] + }) + .collect() +} + +/// The segments of `bytes` up to the scan without Adobe's APP14 marker. +fn without_adobe(bytes: &[u8]) -> Vec { + let mut out = bytes[..2].to_vec(); + let mut at = 2; + loop { + let marker = bytes[at + 1]; + if marker == 0xDA { + out.extend_from_slice(&bytes[at..]); + return out; + } + let len = usize::from(u16::from_be_bytes([bytes[at + 2], bytes[at + 3]])); + if !(marker == 0xEE && &bytes[at + 4..at + 9] == b"Adobe") { + out.extend_from_slice(&bytes[at..at + 2 + len]); + } + at += 2 + len; + } +} + +/// Adobe CMYK reads to the inks it was written with, as `decode` reads it; without the marker +/// the samples are still taken as inverted (where `decode` keeps them as they are), and YCCK +/// turns into the same inks closely, truncating where `decode` rounds. +#[test] +fn four_component_jpegs_read_as_inverted_inks() { + let (w, h) = (32u16, 16u16); + let inks = blocks(usize::from(w), usize::from(h)); + let cmyk = four(w, h, &inks, jpeg_encoder::ColorType::Cmyk, None); + let read = decode_jpeg_inks(&cmyk, &Limits::default()).unwrap(); + assert_eq!((read.dimensions(), read.layout()), ((32, 16), ChannelLayout::Cmyk)); + assert!(read.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 2), "Adobe CMYK keeps its inks"); + assert_eq!(read.data(), decode(&cmyk).unwrap().data(), "as decode reads Adobe CMYK"); + // No marker: inverted all the same, where decode keeps the stored samples. + let bare = without_adobe(&cmyk); + assert_ne!(bare, cmyk); + let unmarked = decode_jpeg_inks(&bare, &Limits::default()).unwrap(); + assert_eq!(unmarked.data(), read.data()); + let kept = decode(&bare).unwrap(); + assert!(kept.data().iter().zip(read.data()).all(|(k, r)| *k == 255 - *r), "decode keeps them as stored"); + // YCCK: libjpeg's conversion, truncated. + let ycck = four(w, h, &inks, jpeg_encoder::ColorType::CmykAsYcck, None); + let read = decode_jpeg_inks(&ycck, &Limits::default()).unwrap(); + assert!(read.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 3), "YCCK keeps its inks closely"); + let rounded = decode(&ycck).unwrap(); + let differ = read.data().iter().zip(rounded.data()).filter(|(a, b)| a != b).count(); + assert!(read.data().iter().zip(rounded.data()).all(|(a, b)| a.abs_diff(*b) <= 1), "within a step of decode's rounding"); + assert!(differ > 0, "truncation differs from rounding somewhere"); +} + +/// The headers alone give the component count of any JPEG and its ICC profile, put together from +/// its numbered segments. +#[test] +fn headers_give_components_and_icc() { + let icc: Vec = (0..200_000u32).map(|i| (i * 7 % 251) as u8).collect(); + let cmyk = four(8, 8, &[9; 256], jpeg_encoder::ColorType::Cmyk, Some(&icc)); + assert_eq!((jpeg_components(&cmyk), jpeg_dimensions(&cmyk)), (Some(4), Some((8, 8)))); + assert_eq!(jpeg_icc_profile(&cmyk).as_deref(), Some(icc.as_slice()), "three APP2 segments"); + let gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert_eq!((jpeg_components(&gray), jpeg_icc_profile(&gray)), (Some(1), None)); + for not in [&b""[..], b"\xFF\xD8", b"\x89PNG\r\n\x1a\n"] { + assert_eq!((jpeg_components(not), jpeg_icc_profile(not), jpeg_dimensions(not)), (None, None, None)); + } +} + +/// A frame header past 16384 pixels a side, `zune-jpeg`'s default, isn't read. +#[test] +fn sides_past_zunes_default_are_not_read() { + let mut gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap(); + let sof = gray.windows(2).position(|w| w == [0xFF, 0xC0]).unwrap(); + gray[sof + 5..sof + 9].copy_from_slice(&[0x40, 0x00, 0x40, 0x00]); + assert_eq!(jpeg_dimensions(&gray), Some((16384, 16384))); + gray[sof + 7..sof + 9].copy_from_slice(&[0x40, 0x01]); + assert_eq!((jpeg_dimensions(&gray), jpeg_components(&gray)), (None, None)); +} + +/// What isn't a four-component image `zune-jpeg` decodes is refused: other component counts, an +/// arithmetic-coded or 12-bit frame (`decode` hands those to craft-jpeg), a size past the limits, +/// and data that isn't a JPEG. +#[test] +fn other_jpegs_are_refused() { + let gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert!(matches!(decode_jpeg_inks(&gray, &Limits::default()), Err(CodecError::Unsupported { .. }))); + let cmyk = four(16, 8, &[40; 16 * 8 * 4], jpeg_encoder::ColorType::Cmyk, None); + let sof = cmyk.windows(2).position(|w| w == [0xFF, 0xC0]).unwrap(); + for (marker, precision) in [(0xC9u8, 8u8), (0xC1, 12)] { + let mut other = cmyk.clone(); + other[sof + 1] = marker; + other[sof + 4] = precision; + assert!(decode_jpeg_inks(&other, &Limits::default()).is_err(), "SOF {marker:#x}, {precision} bits"); + } + // A tiny file declaring a large frame is refused before zune-jpeg allocates its output. + let mut huge = cmyk.clone(); + huge[sof + 5..sof + 9].copy_from_slice(&[0x20, 0x00, 0x20, 0x00]); + assert!(matches!(decode_jpeg_inks(&huge, &Limits::default()), Err(CodecError::InvalidImage(m)) if m.contains("too short"))); + let tight = Limits { max_pixels: 100, ..Limits::default() }; + assert!(matches!(decode_jpeg_inks(&cmyk, &tight), Err(CodecError::LimitExceeded(_)))); + assert!(matches!(decode_jpeg_inks(b"not a jpeg", &Limits::default()), Err(CodecError::InvalidImage(_)))); + assert!(decode_jpeg_inks(&cmyk[..12], &Limits::default()).is_err(), "headers cut short"); +} + +/// The ink reader four-component JPEGs were read with before (vectorcraft's `jpeg_inks`, on +/// `zune-jpeg` as this crate uses it), kept as the reference the readers here match. +fn reference_inks(bytes: &[u8]) -> Option<(usize, usize, Vec)> { + use zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions}; + let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default()); + d.decode_headers().ok()?; + let space = d.input_colorspace()?; + if d.info()?.components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) { + return None; + } + let (w, h) = d.dimensions()?; + d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space)); + let mut data = d.decode().ok()?; + for p in data.as_chunks_mut::<4>().0 { + if space == ColorSpace::YCCK { + let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from); + p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8; + p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8; + p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8; + } + for v in p.iter_mut() { + *v = 255 - *v; + } + } + Some((w, h, data)) +} + +/// Over CMYK and YCCK JPEGs of many sizes, samplings, qualities and pictures, with and without +/// Adobe's marker, baseline and progressive, the inks and component counts read the same as the +/// reference reader. +#[test] +fn matches_the_reference_reader() { + use jpeg_encoder::{ColorType, Encoder, SamplingFactor}; + let (mut seen, mut refused) = (0, 0); + for (i, (w, h)) in [(1u16, 1u16), (7, 3), (8, 8), (17, 9), (33, 31), (64, 5), (100, 77)].into_iter().enumerate() { + let (wu, n) = (usize::from(w), usize::from(w) * usize::from(h)); + let noise: Vec = (0..n * 4).map(|k| ((k as u64).wrapping_mul(2_654_435_761).wrapping_add(i as u64 * 97) >> 7) as u8).collect(); + let ramp: Vec = (0..n * 4).map(|k| (k / 4 % wu * 255 / wu.max(2)) as u8).collect(); + for samples in [&noise, &ramp] { + for ty in [ColorType::Cmyk, ColorType::CmykAsYcck] { + for (q, sampling, progressive) in + [(100, SamplingFactor::R_4_4_4, false), (75, SamplingFactor::R_4_2_0, false), (40, SamplingFactor::R_4_2_2, true)] + { + let mut out = Vec::new(); + let mut enc = Encoder::new(&mut out, q); + enc.set_sampling_factor(sampling); + enc.set_progressive(progressive); + enc.encode(samples, w, h, ty).unwrap(); + for file in [without_adobe(&out), out] { + let read = decode_jpeg_inks(&file, &Limits::default()); + assert_eq!(jpeg_components(&file), Some(4)); + let (dw, dh) = jpeg_dimensions(&file).unwrap(); + assert_eq!((usize::from(w), usize::from(h)), (dw as usize, dh as usize)); + let Some((rw, rh, inks)) = reference_inks(&file) else { + eprintln!("refused: {w}x{h} {ty:?} q{q} progressive {progressive}"); + assert!(read.is_err(), "refused by both: {w}x{h} {ty:?} q{q}"); + refused += 1; + continue; + }; + let read = read.unwrap(); + assert_eq!(read.dimensions(), (rw as u32, rh as u32)); + assert_eq!(read.data(), inks.as_slice(), "{w}x{h} {ty:?} q{q}"); + seen += 1; + } + } + } + } + } + eprintln!("read {seen}, refused {refused}"); + assert_eq!(seen + refused, 7 * 2 * 2 * 3 * 2); + assert!(seen > refused); +} + +/// zune-jpeg 0.5 panics on some subsampled CMYK frames whose first component isn't the largest +/// (here `jpeg-encoder`'s 2×2 K with C, M and Y at 1×1, progressive): an error, not a panic. +#[test] +fn frames_zune_panics_on_are_errors() { + for (w, h) in [(1u16, 1u16), (3, 3)] { + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, 70); + enc.set_sampling_factor(jpeg_encoder::SamplingFactor::F_2_2); + enc.set_progressive(true); + enc.encode(&vec![9; usize::from(w * h) * 4], w, h, jpeg_encoder::ColorType::Cmyk).unwrap(); + assert!(std::panic::catch_unwind(|| reference_inks(&out)).is_err(), "the reference reader panics on {w}x{h}"); + assert!(matches!(decode_jpeg_inks(&out, &Limits::default()), Err(CodecError::Unsupported { .. })), "{w}x{h}"); + assert_eq!((jpeg_components(&out), jpeg_dimensions(&out)), (Some(4), Some((u32::from(w), u32::from(h))))); + } +} diff --git a/crates/image-codecs/tests/it/main.rs b/crates/image-codecs/tests/it/main.rs index 4eda74a26..69be177db 100644 --- a/crates/image-codecs/tests/it/main.rs +++ b/crates/image-codecs/tests/it/main.rs @@ -1,5 +1,6 @@ //! The crate's integration tests, linked into one test binary. +mod bmp_resolution; mod camera_raw; mod common; mod dds; @@ -14,6 +15,7 @@ mod exr_stream; mod fidelity; mod gif_frames; mod heif; +mod jpeg_inks; mod jpegls_oracle; mod limits; mod malformed; diff --git a/crates/image-codecs/tests/it/png_jpeg.rs b/crates/image-codecs/tests/it/png_jpeg.rs index 07c57bf24..98a134121 100644 --- a/crates/image-codecs/tests/it/png_jpeg.rs +++ b/crates/image-codecs/tests/it/png_jpeg.rs @@ -759,3 +759,38 @@ fn bounded_png_stream_preserves_late_exif_across_metadata_yields() { assert_eq!(extras.orientation, 6); assert_eq!(extras.meta.text.len(), 150); } + +/// zune-jpeg 0.5 asserts on some subsampled CMYK frames whose first component isn't the largest +/// (jpeg-encoder writes C, M and Y at 1×1 and K larger); craft-jpeg reads those instead, and they +/// decode to the inks they were written with. The others of these files read as before. +#[test] +fn cmyk_frames_zune_jpeg_panics_on_decode() { + use jpeg_encoder::{ColorType, Encoder, SamplingFactor as S}; + let mut seen = 0; + for (s, progressive) in + [(S::F_2_2, true), (S::R_4_2_0, true), (S::F_4_1, false), (S::F_4_2, false), (S::F_2_4, false), (S::R_4_1_1, false), (S::R_4_1_0, false)] + { + for (w, h) in [(1u16, 1u16), (9, 1), (1, 9), (17, 13), (40, 40)] { + let inks: Vec = (0..usize::from(w) * usize::from(h)).flat_map(|_| [30u8, 90, 150, 210]).collect(); + let mut out = Vec::new(); + let mut e = Encoder::new(&mut out, 100); + e.set_sampling_factor(s); + e.set_progressive(progressive); + e.encode(&inks, w, h, ColorType::Cmyk).unwrap(); + let zune = std::panic::catch_unwind(|| { + let mut d = zune_jpeg::JpegDecoder::new(zune_core::bytestream::ZCursor::new(&out[..])); + d.decode().is_ok() + }); + if zune.is_ok() { + // Read or refused by zune-jpeg as before; no panic either way. + let _ = decode(&out); + continue; + } + seen += 1; + let img = decode(&out).unwrap_or_else(|e| panic!("{s:?} {w}x{h}: {e}")); + assert_eq!((img.dimensions(), img.layout()), ((u32::from(w), u32::from(h)), ChannelLayout::Cmyk)); + assert!(img.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 3), "{s:?} progressive {progressive} {w}x{h}"); + } + } + assert!(seen >= 10, "zune-jpeg panicked on {seen} of them"); +} diff --git a/crates/image-codecs/tests/legacy_tiff_raw.rs b/crates/image-codecs/tests/legacy_tiff_raw.rs new file mode 100644 index 000000000..770801734 --- /dev/null +++ b/crates/image-codecs/tests/legacy_tiff_raw.rs @@ -0,0 +1,65 @@ +use craft_image_codecs::legacy_tiff_raw_hint; + +fn tiff(tag: u16, ty: u16, value: u32) -> Vec { + let mut b = b"II\x2a\0\x08\0\0\0\x01\0".to_vec(); + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + b.extend_from_slice(&value.to_le_bytes()); + b.extend_from_slice(&0u32.to_le_bytes()); + b +} + +#[test] +fn recognizes_legacy_raw_compression_and_broad_cr_marker() { + assert!(legacy_tiff_raw_hint(&tiff(0x0103, 3, 34713))); + assert!(!legacy_tiff_raw_hint(&tiff(0x0103, 3, 1))); + assert!(legacy_tiff_raw_hint(b"II*\0\x08\0\0\0CR")); + let mut cut = tiff(0xc612, 1, 1); + cut.truncate(17); + assert!(!legacy_tiff_raw_hint(&cut)); +} + +/// A little-endian TIFF with one IFD of (tag, type, count, value) entries and `tail` after it. +fn ifd(entries: &[(u16, u16, u32, [u8; 4])], tail: &[u8]) -> Vec { + let mut b = b"II\x2a\0\x08\0\0\0".to_vec(); + b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, ty, count, value) in entries { + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + b.extend_from_slice(&count.to_le_bytes()); + b.extend_from_slice(value); + } + b.extend_from_slice(&0u32.to_le_bytes()); + b.extend_from_slice(tail); + b +} + +#[test] +fn sony_lossless_ycbcr_needs_a_sony_make_and_every_layout_tag() { + let short = |v: u16| [v.to_le_bytes()[0], v.to_le_bytes()[1], 0, 0]; + let layout = [(0x828e, 1, 4, [0, 1, 1, 2]), (0x0103, 3, 1, short(7)), (0x0106, 3, 1, short(6)), (0x0115, 3, 1, short(3))]; + // Make stored after the IFD (ASCII, 5 bytes): 8 + 2 + 5 * 12 + 4 = 74. + let with_make = |make: (u16, u16, u32, [u8; 4]), tail: &[u8]| ifd(&[&[make], &layout[..]].concat(), tail); + assert!(legacy_tiff_raw_hint(&with_make((0x010f, 2, 5, 74u32.to_le_bytes()), b"SONY\0"))); + assert!(legacy_tiff_raw_hint(&with_make((0x010f, 7, 4, *b"sony"), b""))); + assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 2, 6, 74u32.to_le_bytes()), b"Nikon\0"))); + assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 99, 4, *b"SONY"), b""))); + assert!(!legacy_tiff_raw_hint(&with_make((0x010f, 2, 5, 9999u32.to_le_bytes()), b""))); + // A Sony file without the full lossless layout is not a RAW by this rule. + let partial = ifd(&[(0x010f, 7, 4, *b"SONY"), layout[0], layout[1], layout[2]], b""); + assert!(!legacy_tiff_raw_hint(&partial)); + // Repeated tags: the first value wins. + let repeated = ifd(&[&[(0x010f, 7, 4, *b"SONY")], &layout[..], &[(0x0103, 3, 1, short(1))]].concat(), b""); + assert!(legacy_tiff_raw_hint(&repeated)); +} + +#[test] +fn oversized_or_cut_directories_are_skipped() { + let mut huge = b"II\x2a\0\x08\0\0\0".to_vec(); + huge.extend_from_slice(&5000u16.to_le_bytes()); + assert!(!legacy_tiff_raw_hint(&huge)); + let mut cut = ifd(&[(0x010f, 7, 4, *b"SONY"), (0x0103, 3, 1, [7, 0, 0, 0])], b""); + cut.truncate(20); + assert!(!legacy_tiff_raw_hint(&cut)); +} diff --git a/crates/image-codecs/tests/proptest-regressions/malformed.txt b/crates/image-codecs/tests/proptest-regressions/malformed.txt new file mode 100644 index 000000000..532484c50 --- /dev/null +++ b/crates/image-codecs/tests/proptest-regressions/malformed.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 f92b7316155ea46006de7467faa6182a90782b7befd08ed047ebeb70be15c3f7 # shrinks to which = 0, pos = 0, val = 0, cut = 0 diff --git a/crates/macho/Cargo.toml b/crates/macho/Cargo.toml new file mode 100644 index 000000000..8fed852ce --- /dev/null +++ b/crates/macho/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "craft-macho" +description = "Mach-O header pieces from hostile bytes, from Craft Anything: universal (fat and fat64) slice tables, CPU and load command names, load command minimum sizes and the thread-state program counter. Pure Rust, no I/O." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +thiserror = { workspace = true } +# `serde`: `Serialize` on `FatSlice`, for apps that report slice tables as JSON. +serde = { workspace = true, optional = true } + +[features] +serde = ["dep:serde"] + +[dev-dependencies] +serde_json = { workspace = true } diff --git a/crates/macho/src/bytes.rs b/crates/macho/src/bytes.rs new file mode 100644 index 000000000..bfe0d9f81 --- /dev/null +++ b/crates/macho/src/bytes.rs @@ -0,0 +1,39 @@ +//! Bounds-checked fixed-width reads in either byte order: out of range is `None`. + +#[derive(Debug, Clone, Copy)] +pub(crate) struct Reader<'a> { + bytes: &'a [u8], +} + +impl<'a> Reader<'a> { + pub(crate) fn new(bytes: &'a [u8]) -> Self { + Self { bytes } + } + + /// `len` bytes starting at `at`. + pub(crate) fn slice(&self, at: usize, len: usize) -> Option<&'a [u8]> { + let end = at.checked_add(len)?; + self.bytes.get(at..end) + } + + fn word(&self, at: usize) -> Option<[u8; N]> { + let (word, _) = self.bytes.get(at..)?.split_first_chunk::()?; + Some(*word) + } + + pub(crate) fn u32_le(&self, at: usize) -> Option { + self.word(at).map(u32::from_le_bytes) + } + + pub(crate) fn u64_le(&self, at: usize) -> Option { + self.word(at).map(u64::from_le_bytes) + } + + pub(crate) fn u32_be(&self, at: usize) -> Option { + self.word(at).map(u32::from_be_bytes) + } + + pub(crate) fn u64_be(&self, at: usize) -> Option { + self.word(at).map(u64::from_be_bytes) + } +} diff --git a/crates/macho/src/lib.rs b/crates/macho/src/lib.rs new file mode 100644 index 000000000..ed146d5e7 --- /dev/null +++ b/crates/macho/src/lib.rs @@ -0,0 +1,192 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! Mach-O header pieces from untrusted bytes: universal (fat and fat64) slice tables +//! ([`fat_slices`]), CPU and load command names, the fixed size each known load command needs +//! before its fields can be read, and the program counter in a thread command. +//! +//! From Craft Anything (`crates/formats/src/macho.rs`), where the load command walker, symbol, +//! stub and Objective-C code built on these stays. With the `serde` feature, [`FatSlice`] is +//! `Serialize`. + +mod bytes; + +use crate::bytes::Reader; + +/// Slices allowed in a universal binary's table. +pub const MAX_FAT_SLICES: usize = 64; + +/// Thread-state records walked in one thread command. +pub const MAX_THREAD_STATES: usize = 4096; + +/// Why [`fat_slices`] gave up. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum MachoError { + /// The header or a slice ends before the file does. + #[error("the file ends early ({0})")] + Truncated(String), + /// Not a universal binary, or an implausible slice count. + #[error("the file is malformed ({0})")] + Malformed(String), +} + +/// One architecture in a universal binary's slice table. +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +pub struct FatSlice { + /// [`cpu_name`] of the slice's CPU type and subtype. + pub arch: String, + pub cputype: u32, + pub cpusubtype: u32, + /// File offset of the thin image. + pub offset: u64, + pub size: u64, + /// Alignment as a power of two. + pub align: u32, +} + +/// A CPU type and subtype as the name `lipo` and `-arch` use (`x86_64h`, `arm64e`…), or +/// `cpu0x…` for others. +pub fn cpu_name(cputype: u32, sub: u32) -> String { + match cputype { + 7 => "i386".into(), + 0x0100_0007 if sub & 0x00ff_ffff == 8 => "x86_64h".into(), + 0x0100_0007 => "x86_64".into(), + 12 => "arm".into(), + 0x0100_000c => { + if sub & 0xff == 2 { + "arm64e".into() + } else { + "arm64".into() + } + } + 0x0200_000c => "arm64_32".into(), + 18 => "ppc".into(), + 0x0100_0012 => "ppc64".into(), + other => format!("cpu{other:#x}"), + } +} + +/// The slices of a universal (fat, or fat64 with magic `0xcafebabf`) binary. Every slice must +/// lie inside `data`. More than [`MAX_FAT_SLICES`] slices is refused: Java class files share the +/// magic and carry their version there. +pub fn fat_slices(data: &[u8]) -> Result, MachoError> { + let r = Reader::new(data); + let magic = r.u32_be(0).ok_or_else(|| MachoError::Truncated("fat header".into()))?; + let wide = match magic { + 0xcafe_babe => false, + 0xcafe_babf => true, + _ => return Err(MachoError::Malformed("not a universal binary".into())), + }; + let n = r.u32_be(4).ok_or_else(|| MachoError::Truncated("fat header".into()))? as usize; + if n == 0 || n > MAX_FAT_SLICES { + return Err(MachoError::Malformed(format!("{n} slices (Java class files share this magic)"))); + } + let mut out = Vec::new(); + for i in 0..n { + let s = if wide { + let o = 8 + i * 32; + (r.u32_be(o), r.u32_be(o + 4), r.u64_be(o + 8), r.u64_be(o + 16), r.u32_be(o + 24)) + } else { + let o = 8 + i * 20; + (r.u32_be(o), r.u32_be(o + 4), r.u32_be(o + 8).map(u64::from), r.u32_be(o + 12).map(u64::from), r.u32_be(o + 16)) + }; + let (Some(cpu), Some(sub), Some(offset), Some(size), Some(align)) = s else { + return Err(MachoError::Truncated("fat arch table".into())); + }; + if offset.checked_add(size).is_none_or(|end| end > data.len() as u64) { + return Err(MachoError::Truncated(format!("slice {i} runs past the end of the file"))); + } + out.push(FatSlice { arch: cpu_name(cpu, sub), cputype: cpu, cpusubtype: sub, offset, size, align }); + } + Ok(out) +} + +/// A load command's `LC_` name, or its number in hex for ones not listed. +pub fn load_command_name(cmd: u32) -> String { + match cmd { + 0x1 => "LC_SEGMENT", + 0x2 => "LC_SYMTAB", + 0x5 => "LC_UNIXTHREAD", + 0xb => "LC_DYSYMTAB", + 0xc => "LC_LOAD_DYLIB", + 0xd => "LC_ID_DYLIB", + 0xe => "LC_LOAD_DYLINKER", + 0x19 => "LC_SEGMENT_64", + 0x1b => "LC_UUID", + 0x1d => "LC_CODE_SIGNATURE", + 0x1e => "LC_SEGMENT_SPLIT_INFO", + 0x20 => "LC_LAZY_LOAD_DYLIB", + 0x21 => "LC_ENCRYPTION_INFO", + 0x22 => "LC_DYLD_INFO", + 0x8000_0022 => "LC_DYLD_INFO_ONLY", + 0x24 => "LC_VERSION_MIN_MACOSX", + 0x25 => "LC_VERSION_MIN_IPHONEOS", + 0x26 => "LC_FUNCTION_STARTS", + 0x29 => "LC_DATA_IN_CODE", + 0x2a => "LC_SOURCE_VERSION", + 0x2c => "LC_ENCRYPTION_INFO_64", + 0x32 => "LC_BUILD_VERSION", + 0x8000_0018 => "LC_LOAD_WEAK_DYLIB", + 0x8000_001c => "LC_RPATH", + 0x8000_001f => "LC_REEXPORT_DYLIB", + 0x8000_0028 => "LC_MAIN", + 0x8000_0033 => "LC_DYLD_EXPORTS_TRIE", + 0x8000_0034 => "LC_DYLD_CHAINED_FIXUPS", + other => return format!("{other:#x}"), + } + .into() +} + +/// Fixed bytes required before decoding a known load command's fields (8, the command header, +/// for unknown ones). +pub fn load_command_min_size(cmd: u32) -> usize { + match cmd { + 0x1 => 56, + 0x2 | 0x1b | 0x2c | 0x32 | 0x8000_0028 => 24, + 0x5 => 16, + 0xb => 80, + 0xc | 0xd | 0x20 | 0x8000_0018 | 0x8000_001f | 0x8000_0023 => 24, + 0xe | 0xf | 0x12 | 0x13 | 0x14 | 0x15 | 0x27 | 0x8000_001c => 12, + 0x19 => 72, + 0x1d | 0x1e | 0x24 | 0x25 | 0x26 | 0x29 | 0x2a | 0x2b | 0x2e | 0x2f | 0x30 | 0x8000_0033 | 0x8000_0034 => 16, + 0x21 => 20, + 0x22 | 0x8000_0022 => 48, + _ => 8, + } +} + +/// An `X.Y.Z` version packed as `xxxx.yy.zz` nibbles (`LC_VERSION_MIN_*`, `LC_BUILD_VERSION`). +pub fn nibble_version(v: u32) -> String { + format!("{}.{}.{}", v >> 16, (v >> 8) & 0xff, v & 0xff) +} + +/// The program counter in an `LC_UNIXTHREAD` / `LC_THREAD` body (the bytes after its 8-byte +/// command header): the first complete x86_64 (flavor 4), arm64 (flavor 6) or i386 (flavor 1) +/// thread state for `cpu`. A command can carry several flavor/count/state records; counts are +/// 32-bit words. At most [`MAX_THREAD_STATES`] records are walked. +pub fn unixthread_pc(data: &[u8], cpu: u32, is64: bool) -> Option { + let r = Reader::new(data); + let mut pos = 0usize; + for _ in 0..MAX_THREAD_STATES { + let flavor = r.u32_le(pos)?; + let count = r.u32_le(pos.checked_add(4)?)?; + let state = pos.checked_add(8)?; + let size = usize::try_from(count).ok()?.checked_mul(4)?; + let record = Reader::new(r.slice(state, size)?); + let pc = match (cpu, is64, flavor) { + (0x0100_0007, true, 4) if count >= 42 => record.u64_le(16 * 8), + (0x0100_000c, true, 6) if count >= 68 => record.u64_le(32 * 8), + (7, false, 1) if count >= 16 => record.u32_le(10 * 4).map(u64::from), + _ => None, + }; + if pc.is_some() { + return pc; + } + pos = state.checked_add(size)?; + } + None +} + +#[cfg(test)] +mod tests; diff --git a/crates/macho/src/tests.rs b/crates/macho/src/tests.rs new file mode 100644 index 000000000..4f3b403dd --- /dev/null +++ b/crates/macho/src/tests.rs @@ -0,0 +1,213 @@ +//! Slice table, name and thread-state tests. The thread-state cases are Craft Anything's +//! `macho_thread_state` integration test, called on the command body directly instead of +//! through the app's program model. + +use super::*; + +const X86_64: u32 = 0x0100_0007; +const ARM64: u32 = 0x0100_000c; + +/// A universal binary with `slices` of (cputype, cpusubtype, body), each at a 0x100 boundary. +fn fat(wide: bool, slices: &[(u32, u32, &[u8])]) -> Vec { + let entry = if wide { 32 } else { 20 }; + let mut d = vec![0u8; 0x100]; + d[0..4].copy_from_slice(&(if wide { 0xcafe_babfu32 } else { 0xcafe_babe }).to_be_bytes()); + d[4..8].copy_from_slice(&(slices.len() as u32).to_be_bytes()); + for (i, &(cpu, sub, body)) in slices.iter().enumerate() { + let offset = d.len(); + let o = 8 + i * entry; + d[o..o + 4].copy_from_slice(&cpu.to_be_bytes()); + d[o + 4..o + 8].copy_from_slice(&sub.to_be_bytes()); + if wide { + d[o + 8..o + 16].copy_from_slice(&(offset as u64).to_be_bytes()); + d[o + 16..o + 24].copy_from_slice(&(body.len() as u64).to_be_bytes()); + d[o + 24..o + 28].copy_from_slice(&8u32.to_be_bytes()); + } else { + d[o + 8..o + 12].copy_from_slice(&(offset as u32).to_be_bytes()); + d[o + 12..o + 16].copy_from_slice(&(body.len() as u32).to_be_bytes()); + d[o + 16..o + 20].copy_from_slice(&8u32.to_be_bytes()); + } + d.extend_from_slice(body); + d.resize(d.len().next_multiple_of(0x100), 0); + } + d +} + +#[test] +fn fat_and_fat64_tables_list_their_slices() { + for wide in [false, true] { + let d = fat(wide, &[(X86_64, 3, &[1; 16]), (ARM64, 2, &[2; 32])]); + let s = fat_slices(&d).unwrap(); + assert_eq!(s.len(), 2, "wide={wide}"); + assert_eq!(s[0], FatSlice { arch: "x86_64".into(), cputype: X86_64, cpusubtype: 3, offset: 0x100, size: 16, align: 8 }); + assert_eq!((s[1].arch.as_str(), s[1].offset, s[1].size), ("arm64e", 0x200, 32)); + } +} + +#[test] +fn damaged_tables_are_errors() { + assert_eq!(fat_slices(b"\xca\xfe"), Err(MachoError::Truncated("fat header".into()))); + assert_eq!(fat_slices(&[0xfe, 0xed, 0xfa, 0xcf, 0, 0, 0, 1]), Err(MachoError::Malformed("not a universal binary".into()))); + assert_eq!(fat_slices(&[0xca, 0xfe, 0xba, 0xbe]), Err(MachoError::Truncated("fat header".into()))); + // A Java 21 class file: minor 0, major 65. + let java = [0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 65]; + assert_eq!(fat_slices(&java).unwrap_err().to_string(), "the file is malformed (65 slices (Java class files share this magic))"); + assert!(matches!(fat_slices(&[0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 0]), Err(MachoError::Malformed(_)))); + let d = fat(false, &[(X86_64, 3, &[1; 16])]); + assert_eq!(fat_slices(&d[..20]), Err(MachoError::Truncated("fat arch table".into()))); + assert_eq!(fat_slices(&d[..0x108]), Err(MachoError::Truncated("slice 0 runs past the end of the file".into()))); + let mut wrap = fat(true, &[(X86_64, 3, &[1; 16])]); + wrap[16..24].copy_from_slice(&u64::MAX.to_be_bytes()); + assert!(matches!(fat_slices(&wrap), Err(MachoError::Truncated(_))), "offset + size must not wrap"); +} + +#[test] +fn cpu_names() { + let names: Vec = [ + (7, 3), + (X86_64, 3), + (X86_64, 8), + (X86_64, 0x8000_0008), + (X86_64, 0x108), + (12, 9), + (ARM64, 0), + (ARM64, 0x8000_0002), + (0x0200_000c, 1), + (18, 0), + (0x0100_0012, 0), + (0x99, 0), + ] + .into_iter() + .map(|(cpu, sub)| cpu_name(cpu, sub)) + .collect(); + assert_eq!(names, ["i386", "x86_64", "x86_64h", "x86_64h", "x86_64", "arm", "arm64", "arm64e", "arm64_32", "ppc", "ppc64", "cpu0x99"]); +} + +#[test] +fn load_command_names_and_sizes() { + let known = [ + (0x1, "LC_SEGMENT", 56), + (0x2, "LC_SYMTAB", 24), + (0x5, "LC_UNIXTHREAD", 16), + (0xb, "LC_DYSYMTAB", 80), + (0xc, "LC_LOAD_DYLIB", 24), + (0xd, "LC_ID_DYLIB", 24), + (0xe, "LC_LOAD_DYLINKER", 12), + (0x19, "LC_SEGMENT_64", 72), + (0x1b, "LC_UUID", 24), + (0x1d, "LC_CODE_SIGNATURE", 16), + (0x1e, "LC_SEGMENT_SPLIT_INFO", 16), + (0x20, "LC_LAZY_LOAD_DYLIB", 24), + (0x21, "LC_ENCRYPTION_INFO", 20), + (0x22, "LC_DYLD_INFO", 48), + (0x8000_0022, "LC_DYLD_INFO_ONLY", 48), + (0x24, "LC_VERSION_MIN_MACOSX", 16), + (0x25, "LC_VERSION_MIN_IPHONEOS", 16), + (0x26, "LC_FUNCTION_STARTS", 16), + (0x29, "LC_DATA_IN_CODE", 16), + (0x2a, "LC_SOURCE_VERSION", 16), + (0x2c, "LC_ENCRYPTION_INFO_64", 24), + (0x32, "LC_BUILD_VERSION", 24), + (0x8000_0018, "LC_LOAD_WEAK_DYLIB", 24), + (0x8000_001c, "LC_RPATH", 12), + (0x8000_001f, "LC_REEXPORT_DYLIB", 24), + (0x8000_0028, "LC_MAIN", 24), + (0x8000_0033, "LC_DYLD_EXPORTS_TRIE", 16), + (0x8000_0034, "LC_DYLD_CHAINED_FIXUPS", 16), + ]; + for (cmd, name, size) in known { + assert_eq!(load_command_name(cmd), name); + assert_eq!(load_command_min_size(cmd), size, "{name}"); + } + assert_eq!(load_command_name(0x8000_0023), "0x80000023"); + assert_eq!(load_command_min_size(0x8000_0023), 24); + assert_eq!(load_command_min_size(0x27), 12); + assert_eq!(load_command_min_size(0x2e), 16); + assert_eq!(load_command_min_size(0x77), 8); +} + +#[test] +fn nibble_versions() { + assert_eq!(nibble_version(0x000e_0500), "14.5.0"); + assert_eq!(nibble_version(0x000a_0f01), "10.15.1"); +} + +fn state(flavor: u32, count: u32, bytes: usize, pc_offset: usize, pc: u64, wide: bool) -> Vec { + let mut b = vec![0u8; 8 + bytes]; + b[0..4].copy_from_slice(&flavor.to_le_bytes()); + b[4..8].copy_from_slice(&count.to_le_bytes()); + if wide { + b[8 + pc_offset..16 + pc_offset].copy_from_slice(&pc.to_le_bytes()); + } else { + b[8 + pc_offset..12 + pc_offset].copy_from_slice(&(pc as u32).to_le_bytes()); + } + b +} + +fn decode(cpu: u32, wide: bool, states: &[Vec]) -> Option { + unixthread_pc(&states.concat(), cpu, wide) +} + +#[test] +fn a_program_counter_requires_a_complete_declared_state() { + for count in [0, 1, 34, 41] { + assert_eq!(decode(X86_64, true, &[state(4, count, 168, 128, 0x1234, true)]), None, "count {count}"); + } + assert_eq!(decode(X86_64, true, &[state(4, 42, 136, 128, 0x1234, true)]), None); + assert_eq!(decode(ARM64, true, &[state(6, 68, 264, 256, 0x1234, true)]), None); +} + +#[test] +fn flavor_numbers_are_interpreted_for_the_image_cpu() { + assert_eq!(decode(ARM64, true, &[state(4, 42, 168, 128, 0x1234, true)]), None); + assert_eq!(decode(X86_64, true, &[state(6, 68, 272, 256, 0x1234, true)]), None); + assert_eq!(decode(12, false, &[state(1, 17, 68, 40, 0x1234, false)]), None); +} + +#[test] +fn a_later_matching_state_is_reached_using_word_counts() { + let unknown = state(99, 2, 8, 0, 0, true); + let matching = state(4, 42, 168, 128, 0x1234, true); + assert_eq!(decode(X86_64, true, &[unknown, matching]), Some(0x1234)); +} + +#[test] +fn complete_supported_states_keep_their_program_counters() { + assert_eq!(decode(X86_64, true, &[state(4, 42, 168, 128, 0x1234, true)]), Some(0x1234)); + assert_eq!(decode(ARM64, true, &[state(6, 68, 272, 256, 0x1234, true)]), Some(0x1234)); + assert_eq!(decode(7, false, &[state(1, 16, 64, 40, 0x1234, false)]), Some(0x1234)); +} + +#[test] +fn endless_empty_states_stop() { + assert_eq!(unixthread_pc(&vec![0; 8 * (MAX_THREAD_STATES + 4)], X86_64, true), None); + assert_eq!(unixthread_pc(&[4, 0, 0, 0, 0xff, 0xff, 0xff, 0xff], X86_64, true), None); +} + +#[cfg(feature = "serde")] +#[test] +fn slices_serialize() { + let s = fat_slices(&fat(false, &[(ARM64, 0, &[0; 4])])).unwrap(); + let value = serde_json::to_value(&s[0]).unwrap(); + assert_eq!(value["arch"], "arm64"); + assert_eq!(value["offset"], 0x100); +} + +#[test] +fn damaged_input_never_panics() { + let sample = fat(true, &[(X86_64, 3, &[1; 16]), (ARM64, 2, &[2; 32])]); + let thread = state(4, 42, 168, 128, 0x1234, true); + for source in [&sample, &thread] { + for i in 0..source.len() { + let _ = fat_slices(&source[..i]); + let _ = unixthread_pc(&source[..i], X86_64, true); + for v in [0x00, 0x7f, 0xff] { + let mut m = source.clone(); + m[i] = v; + let _ = fat_slices(&m); + let _ = unixthread_pc(&m, ARM64, true); + let _ = unixthread_pc(&m, 7, false); + } + } + } +} diff --git a/crates/mcp-server/Cargo.toml b/crates/mcp-server/Cargo.toml new file mode 100644 index 000000000..9b034c7cc --- /dev/null +++ b/crates/mcp-server/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "craft-mcp-server" +description = "MCP server machinery any app may share: JSON-RPC 2.0 over stdio, the MCP lifecycle, tool and resource methods, tool schemas and argument checks, image results" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +serde_json = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/mcp-server/src/content.rs b/crates/mcp-server/src/content.rs new file mode 100644 index 000000000..a2a017dec --- /dev/null +++ b/crates/mcp-server/src/content.rs @@ -0,0 +1,64 @@ +//! The content blocks a tool result is made of (MCP `CallToolResult`), and the contents a resource +//! read sends (MCP `ReadResourceResult`). + +use serde_json::{Value, json}; + +/// What `resources/read` sends for one resource: text of a MIME type, or bytes of a MIME type, +/// which go out base64-encoded. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Contents { + Text { mime_type: String, text: String }, + Blob { mime_type: String, data: Vec }, +} + +impl Contents { + /// `value` as JSON text (`application/json`). + pub fn json(value: &Value) -> Self { + Self::Text { mime_type: "application/json".into(), text: value.to_string() } + } + + /// The MCP contents block of resource `uri`. + pub(crate) fn block(&self, uri: &str) -> Value { + match self { + Self::Text { mime_type, text } => json!({"uri": uri, "mimeType": mime_type, "text": text}), + Self::Blob { mime_type, data } => json!({"uri": uri, "mimeType": mime_type, "blob": base64(data)}), + } + } +} + +/// A tool result of one text block, flagged as an error when `is_error` (the model sees the +/// message; an error is still a result, not a JSON-RPC error). +pub fn text_result(text: impl Into, is_error: bool) -> Value { + let mut r = json!({"content": [{"type": "text", "text": text.into()}]}); + if is_error { + r["isError"] = json!(true); + } + r +} + +/// A tool result of an image (`data` of type `mime`, base64-encoded here) followed by a text +/// block saying what it shows. +pub fn image_result(data: &[u8], mime: &str, text: impl Into) -> Value { + json!({"content": [ + {"type": "image", "data": base64(data), "mimeType": mime}, + {"type": "text", "text": text.into()}, + ]}) +} + +/// Standard base64 with padding (RFC 4648), as MCP image content carries it. +pub fn base64(data: &[u8]) -> String { + const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut out = String::with_capacity(data.len().div_ceil(3) * 4); + for chunk in data.chunks(3) { + let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)]; + let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); + for (i, shift) in [18u32, 12, 6, 0].into_iter().enumerate() { + if i <= chunk.len() { + out.push(char::from(T[((n >> shift) & 63) as usize])); + } else { + out.push('='); + } + } + } + out +} diff --git a/crates/mcp-server/src/lib.rs b/crates/mcp-server/src/lib.rs new file mode 100644 index 000000000..17e6827c4 --- /dev/null +++ b/crates/mcp-server/src/lib.rs @@ -0,0 +1,21 @@ +//! MCP server machinery any app may share: JSON-RPC 2.0 over stdio (newline-delimited), the MCP +//! lifecycle, the tools, resources, prompts and completion methods, tool schemas with the check of +//! a call's arguments against them, and the content blocks tools reply with (text, images). +//! +//! An app implements [`Tools`]: what its tools are, how each one runs, its instructions, and the +//! resources, resource templates and prompts it serves, if any. [`Server`] speaks the protocol +//! around them and tells a client at `initialize` only what the app serves. Nothing here knows any +//! app's design, engine or renderer. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +mod content; +mod schema; +mod server; + +pub use content::{Contents, base64, image_result, text_result}; +pub use schema::{Effect, check_arguments, object, string, tool}; +pub use server::{Incoming, Info, PROTOCOL_VERSION, Reference, SUPPORTED_VERSIONS, Server, Tools, read_message, response}; + +#[cfg(test)] +mod tests; diff --git a/crates/mcp-server/src/schema.rs b/crates/mcp-server/src/schema.rs new file mode 100644 index 000000000..3cc72da34 --- /dev/null +++ b/crates/mcp-server/src/schema.rs @@ -0,0 +1,132 @@ +//! Tool definitions (MCP `Tool`: name, title, description, input schema, annotations) and the +//! check of a call's arguments against the definition it was listed with. + +use serde_json::{Value, json}; + +/// What a tool may do, for the client's approval prompts (MCP tool annotations). Clients that +/// auto-approve on these hints must not be told less than the truth. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Effect { + /// Only looks. + ReadOnly, + /// Changes the app's document in memory, undoably. + Edits, + /// Replaces the app's document in memory (unsaved changes are lost). + Replaces, + /// May read, write, overwrite or delete files on disk. + Files, +} + +/// A tool's definition, its annotations from `effect`. +pub fn tool(name: &str, title: &str, desc: &str, schema: Value, effect: Effect) -> Value { + let (read_only, destructive, open_world) = match effect { + Effect::ReadOnly => (true, false, false), + Effect::Edits => (false, false, false), + Effect::Replaces => (false, true, false), + Effect::Files => (false, true, true), + }; + json!({ + "name": name, + "title": title, + "description": desc, + "inputSchema": schema, + "annotations": {"title": title, "readOnlyHint": read_only, "destructiveHint": destructive, "idempotentHint": read_only, "openWorldHint": open_world}, + }) +} + +/// An object schema with these `props`, the `required` ones, and no other keys. +pub fn object(props: Value, required: &[&str]) -> Value { + let mut o = json!({"type": "object", "properties": props, "additionalProperties": false}); + if !required.is_empty() { + o["required"] = json!(required); + } + o +} + +/// A string property described by `desc`. +pub fn string(desc: &str) -> Value { + json!({"type": "string", "description": desc}) +} + +fn type_matches(t: &str, v: &Value) -> bool { + match t { + "string" => v.is_string(), + "number" => v.is_number(), + "integer" => v.is_i64() || v.is_u64(), + "boolean" => v.is_boolean(), + "array" => v.is_array(), + "object" => v.is_object(), + "null" => v.is_null(), + _ => true, + } +} + +/// Check `args` against tool `name`'s input schema as `defs` list it (top level): unknown tool, +/// missing required arguments, unknown arguments, type and enum mismatches. +pub fn check_arguments(defs: &[Value], name: &str, args: &Value) -> Result<(), String> { + let def = defs.iter().find(|d| d["name"] == name).ok_or_else(|| format!("unknown tool `{name}`"))?; + let schema = &def["inputSchema"]; + let Some(a) = args.as_object() else { return Err(format!("{name}: arguments must be an object")) }; + for r in schema["required"].as_array().into_iter().flatten().filter_map(Value::as_str) { + if a.get(r).is_none_or(Value::is_null) { + return Err(format!("{name}: missing required argument `{r}`")); + } + } + let props = schema["properties"].as_object(); + for (k, v) in a { + let Some(p) = props.and_then(|p| p.get(k)) else { + let valid: Vec<&str> = props.map(|p| p.keys().map(String::as_str).collect()).unwrap_or_default(); + let valid = if valid.is_empty() { "none".to_string() } else { valid.join(", ") }; + let k: String = k.chars().take(64).collect(); + return Err(format!("{name}: unknown argument `{k}`; its arguments are: {valid}")); + }; + if v.is_null() { + continue; + } + // An argument of several types lists them as `anyOf` (Gemini refuses a list of types). + let types: Vec<&Value> = match p["anyOf"].as_array() { + Some(alts) => alts.iter().map(|alt| &alt["type"]).collect(), + None => vec![&p["type"]], + }; + let ok = types.iter().any(|t| match t { + Value::String(t) => type_matches(t, v), + Value::Array(ts) => ts.iter().filter_map(Value::as_str).any(|t| type_matches(t, v)), + _ => true, + }); + if !ok { + let expected = if types.len() == 1 { types[0].clone() } else { Value::Array(types.into_iter().cloned().collect()) }; + return Err(format!("{name}: argument `{k}` must be of type {expected}")); + } + if let Some(allowed) = p["enum"].as_array() + && !allowed.contains(v) + { + return Err(format!("{name}: `{k}` must be one of {}", Value::Array(allowed.clone()))); + } + } + Ok(()) +} + +/// Check `args` against prompt `name` as `defs` list it (MCP `Prompt`): unknown prompt, missing +/// required arguments, unknown arguments, values that are not strings. +pub(crate) fn check_prompt_arguments(defs: &[Value], name: &str, args: &Value) -> Result<(), String> { + let def = defs.iter().find(|d| d["name"] == name).ok_or_else(|| format!("unknown prompt `{name}`"))?; + let declared = def["arguments"].as_array().map(Vec::as_slice).unwrap_or_default(); + let Some(a) = args.as_object() else { return Err(format!("{name}: arguments must be an object")) }; + for r in declared.iter().filter(|d| d["required"] == true).filter_map(|d| d["name"].as_str()) { + if !a.contains_key(r) { + return Err(format!("{name}: missing required argument `{r}`")); + } + } + for (k, v) in a { + if !declared.iter().any(|d| d["name"] == k.as_str()) { + let valid: Vec<&str> = declared.iter().filter_map(|d| d["name"].as_str()).collect(); + let valid = if valid.is_empty() { "none".to_string() } else { valid.join(", ") }; + let k: String = k.chars().take(64).collect(); + return Err(format!("{name}: unknown argument `{k}`; its arguments are: {valid}")); + } + if !v.is_string() { + return Err(format!("{name}: argument `{k}` must be a string")); + } + } + Ok(()) +} diff --git a/crates/mcp-server/src/server.rs b/crates/mcp-server/src/server.rs new file mode 100644 index 000000000..b7ed05027 --- /dev/null +++ b/crates/mcp-server/src/server.rs @@ -0,0 +1,330 @@ +//! JSON-RPC 2.0 framing and the MCP lifecycle / tools / resources / prompts / completion methods. + +use std::io::{BufRead, Write}; + +use serde_json::{Value, json}; + +use crate::content::Contents; +use crate::schema::{check_arguments, check_prompt_arguments}; + +/// The newest protocol revision we speak; offered when the client asks for one we don't know. +pub const PROTOCOL_VERSION: &str = "2025-06-18"; +pub const SUPPORTED_VERSIONS: &[&str] = &[PROTOCOL_VERSION, "2025-03-26", "2024-11-05"]; + +const PARSE_ERROR: i64 = -32700; +const INVALID_REQUEST: i64 = -32600; +const METHOD_NOT_FOUND: i64 = -32601; +const INVALID_PARAMS: i64 = -32602; +const INTERNAL_ERROR: i64 = -32603; +const RESOURCE_NOT_FOUND: i64 = -32002; + +/// Most messages in one JSON-RPC batch. +const MAX_BATCH: usize = 1000; +/// Most suggestions in one `completion/complete` reply (the MCP limit). +const MAX_COMPLETIONS: usize = 100; + +/// What `completion/complete` asks about: a prompt by its name, or a resource by its URI or URI +/// template. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Reference<'a> { + Prompt(&'a str), + Resource(&'a str), +} + +/// An app's tools, served by [`Server`]. +pub trait Tools { + /// What every client is told at `initialize`. + fn instructions(&self) -> String; + /// Every tool's definition ([`crate::tool`]), as `tools/list` serves it now. + fn list(&mut self) -> Vec; + /// Run tool `name`. The server calls it right after the [`Tools::list`] its arguments were + /// checked against ([`crate::check_arguments`]), so it runs the tool as that listing defined + /// it. Failures are tool results with `isError: true` ([`crate::text_result`]); it never + /// panics on hostile arguments. + fn call(&mut self, name: &str, args: &Value) -> Value; + /// The resources `resources/list` serves (MCP `Resource`: uri, name, description, mimeType). + fn resources(&self) -> Vec { + Vec::new() + } + /// The resource templates `resources/templates/list` serves (MCP `ResourceTemplate`: + /// uriTemplate, name, description, mimeType). A URI made from one is read with + /// [`Tools::read_resource`]. + fn resource_templates(&self) -> Vec { + Vec::new() + } + /// The contents of resource `uri`: `None` when there is no such resource, `Err` when it could + /// not be read now. + fn read_resource(&mut self, uri: &str) -> Option> { + let _ = uri; + None + } + /// The prompts `prompts/list` serves (MCP `Prompt`: name, title, description, arguments). + fn prompts(&self) -> Vec { + Vec::new() + } + /// Prompt `name` filled in with `args` (MCP `GetPromptResult`: description, messages). The + /// server calls it only for a listed prompt, with `args` checked against that listing: an + /// object of strings with every required argument and no other. `Err` is an internal error. + fn get_prompt(&mut self, name: &str, args: &Value) -> Result { + let _ = args; + Err(format!("prompt `{name}` cannot be filled in")) + } + /// What argument `argument` of `reference` could be, given what has been typed (`value`). + /// Asked only of an app that serves prompts or resource templates; a prompt reference is + /// always a listed prompt. The reply holds the first 100. + fn complete(&mut self, reference: Reference<'_>, argument: &str, value: &str) -> Vec { + let _ = (reference, argument, value); + Vec::new() + } +} + +/// Who the server says it is at `initialize`, and the longest request line it reads. +#[derive(Clone, Copy, Debug)] +pub struct Info { + pub name: &'static str, + pub title: &'static str, + pub version: &'static str, + /// Longest accepted request line, in bytes; a longer one is refused in bounded space. + pub max_line: usize, +} + +/// An MCP server over the app's [`Tools`]. +pub struct Server { + info: Info, + tools: T, + initialized: bool, +} + +fn error(id: Value, code: i64, message: impl Into) -> Value { + json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message.into()}}) +} + +/// One message read from a newline-delimited stream by [`read_message`]. +#[derive(Debug, PartialEq, Eq)] +pub enum Incoming { + /// A line to handle ([`Server::handle_line`]), without its newline. + Line(String), + /// A line refused unread (too long, not UTF-8): the error reply to send for it. + Refused(String), +} + +/// Read the next line of newline-delimited JSON-RPC from `input`; `None` once it closes. A line +/// longer than `max_line` is consumed in bounded space, so a client cannot exhaust memory by +/// sending an arbitrarily long request without a newline, and an unterminated last line counts. +pub fn read_message(input: &mut impl BufRead, max_line: usize) -> std::io::Result> { + let mut line = Vec::new(); + let mut oversized = false; + let mut received = false; + loop { + let chunk = input.fill_buf()?; + if chunk.is_empty() { + break; + } + let count = chunk.iter().position(|&byte| byte == b'\n').map_or(chunk.len(), |i| i + 1); + let newline = chunk[count - 1] == b'\n'; + let content_len = count - usize::from(newline); + if !oversized { + if line.len().saturating_add(content_len) > max_line { + oversized = true; + line.clear(); + } else { + line.extend_from_slice(&chunk[..content_len]); + } + } + input.consume(count); + received = true; + if newline { + break; + } + } + if !received { + return Ok(None); + } + if oversized { + return Ok(Some(Incoming::Refused(error(Value::Null, INVALID_REQUEST, "request too large").to_string()))); + } + Ok(Some(match String::from_utf8(line) { + Ok(line) => Incoming::Line(line), + // Not UTF-8: answer with a parse error and keep serving. + Err(_) => Incoming::Refused(error(Value::Null, PARSE_ERROR, "parse error: input is not UTF-8").to_string()), + })) +} + +/// The JSON-RPC response to request `id`, as [`Server::handle`] sends it. +pub fn response(id: Value, result: Value) -> Value { + json!({"jsonrpc": "2.0", "id": id, "result": result}) +} + +impl Server { + pub fn new(info: Info, tools: T) -> Self { + Self { info, tools, initialized: false } + } + pub fn tools(&mut self) -> &mut T { + &mut self.tools + } + pub fn is_initialized(&self) -> bool { + self.initialized + } + + /// Serve newline-delimited JSON-RPC until `input` closes. Only protocol messages go to + /// `output`; diagnostics belong on stderr. Consume oversized lines in bounded space so a + /// client cannot exhaust memory by sending an arbitrarily long request without a newline. + pub fn serve(&mut self, mut input: impl BufRead, mut output: impl Write) -> std::io::Result<()> { + while let Some(incoming) = read_message(&mut input, self.info.max_line)? { + let reply = match incoming { + Incoming::Line(line) => self.handle_line(&line), + Incoming::Refused(reply) => Some(reply), + }; + if let Some(reply) = reply { + output.write_all(reply.as_bytes())?; + output.write_all(b"\n")?; + output.flush()?; + } + } + Ok(()) + } + + /// One line in, at most one line out (notifications get no reply). + pub fn handle_line(&mut self, line: &str) -> Option { + // Check the raw input before trimming: a huge whitespace prefix is still a huge line. + if line.len() > self.info.max_line { + return Some(error(Value::Null, INVALID_REQUEST, "request too large").to_string()); + } + let line = line.trim(); + if line.is_empty() { + return None; + } + let reply = match serde_json::from_str::(line) { + Ok(Value::Array(batch)) if batch.is_empty() => Some(error(Value::Null, INVALID_REQUEST, "empty batch")), + Ok(Value::Array(batch)) if batch.len() > MAX_BATCH => Some(error(Value::Null, INVALID_REQUEST, "batch too large")), + Ok(Value::Array(batch)) => { + let replies: Vec = batch.into_iter().filter_map(|m| self.handle(m)).collect(); + (!replies.is_empty()).then_some(Value::Array(replies)) + } + Ok(msg) => self.handle(msg), + Err(e) => Some(error(Value::Null, PARSE_ERROR, format!("parse error: {e}"))), + }; + reply.map(|r| r.to_string()) + } + + pub fn handle(&mut self, msg: Value) -> Option { + let Value::Object(o) = &msg else { return Some(error(Value::Null, INVALID_REQUEST, "message must be an object")) }; + let id = o.get("id").cloned(); + let Some(method) = o.get("method").and_then(Value::as_str) else { + // A response to a request of ours (we send none) or junk. + if o.contains_key("result") || o.contains_key("error") { + return None; + } + return Some(error(id.unwrap_or(Value::Null), INVALID_REQUEST, "missing `method`")); + }; + let params = o.get("params").cloned().unwrap_or(Value::Null); + let Some(id) = id else { + if method == "notifications/initialized" { + self.initialized = true; + } + return None; + }; + Some(match self.request(method, ¶ms) { + Ok(r) => response(id, r), + Err((code, m)) => error(id, code, m), + }) + } + + fn request(&mut self, method: &str, params: &Value) -> Result { + match method { + "initialize" => { + let asked = params.get("protocolVersion").and_then(Value::as_str).unwrap_or(PROTOCOL_VERSION); + let version = if SUPPORTED_VERSIONS.contains(&asked) { asked } else { PROTOCOL_VERSION }; + let Info { name, title, version: server_version, .. } = self.info; + Ok(json!({ + "protocolVersion": version, + "capabilities": self.capabilities(), + "serverInfo": {"name": name, "title": title, "version": server_version}, + "instructions": self.tools.instructions(), + })) + } + "ping" => Ok(json!({})), + "tools/list" => Ok(json!({"tools": self.tools.list()})), + "tools/call" => { + let name = params.get("name").and_then(Value::as_str).ok_or((INVALID_PARAMS, "missing tool `name`".to_string()))?; + let args = match params.get("arguments") { + None | Some(Value::Null) => json!({}), + Some(a) => a.clone(), + }; + check_arguments(&self.tools.list(), name, &args).map_err(|e| (INVALID_PARAMS, e))?; + Ok(self.tools.call(name, &args)) + } + "resources/list" => Ok(json!({"resources": self.tools.resources()})), + "resources/templates/list" => Ok(json!({"resourceTemplates": self.tools.resource_templates()})), + "prompts/list" => Ok(json!({"prompts": self.tools.prompts()})), + "resources/read" => { + let uri = params.get("uri").and_then(Value::as_str).unwrap_or(""); + let contents = self + .tools + .read_resource(uri) + .ok_or_else(|| (RESOURCE_NOT_FOUND, format!("unknown resource `{uri}`")))? + .map_err(|e| (INTERNAL_ERROR, e))?; + Ok(json!({"contents": [contents.block(uri)]})) + } + "prompts/get" if !self.tools.prompts().is_empty() => { + let name = params.get("name").and_then(Value::as_str).ok_or((INVALID_PARAMS, "missing prompt `name`".to_string()))?; + let args = match params.get("arguments") { + None | Some(Value::Null) => json!({}), + Some(a) => a.clone(), + }; + check_prompt_arguments(&self.tools.prompts(), name, &args).map_err(|e| (INVALID_PARAMS, e))?; + self.tools.get_prompt(name, &args).map_err(|e| (INTERNAL_ERROR, e)) + } + "completion/complete" if self.completes() => { + let reference = params.get("ref").unwrap_or(&Value::Null); + let reference = match (reference.get("type").and_then(Value::as_str), reference.get("name"), reference.get("uri")) { + (Some("ref/prompt"), Some(Value::String(name)), _) => { + if !self.tools.prompts().iter().any(|p| p["name"] == name.as_str()) { + return Err((INVALID_PARAMS, format!("unknown prompt `{name}`"))); + } + Reference::Prompt(name) + } + (Some("ref/resource"), _, Some(Value::String(uri))) => Reference::Resource(uri), + _ => { + return Err(( + INVALID_PARAMS, + "`ref` must be a prompt's {type: ref/prompt, name} or a resource's {type: ref/resource, uri}".into(), + )); + } + }; + let argument = params.get("argument"); + let (Some(argument), Some(value)) = + (argument.and_then(|a| a.get("name")).and_then(Value::as_str), argument.and_then(|a| a.get("value")).and_then(Value::as_str)) + else { + return Err((INVALID_PARAMS, "`argument` must have a `name` and a `value`, both strings".into())); + }; + let mut values = self.tools.complete(reference, argument, value); + let total = values.len(); + values.truncate(MAX_COMPLETIONS); + Ok(json!({"completion": {"values": values, "total": total, "hasMore": total > MAX_COMPLETIONS}})) + } + _ => Err((METHOD_NOT_FOUND, format!("method `{method}` not found"))), + } + } + + /// What the server tells a client at `initialize` that it serves: the tools always, and the + /// rest only when the app has some. + fn capabilities(&self) -> Value { + let mut capabilities = json!({"tools": {"listChanged": false}}); + if !self.tools.resources().is_empty() || !self.tools.resource_templates().is_empty() { + capabilities["resources"] = json!({"listChanged": false}); + } + if !self.tools.prompts().is_empty() { + capabilities["prompts"] = json!({"listChanged": false}); + } + if self.completes() { + capabilities["completions"] = json!({}); + } + capabilities + } + + /// Whether the app has arguments to complete: those of its prompts and resource templates. + fn completes(&self) -> bool { + !self.tools.prompts().is_empty() || !self.tools.resource_templates().is_empty() + } +} diff --git a/crates/mcp-server/src/tests.rs b/crates/mcp-server/src/tests.rs new file mode 100644 index 000000000..b687ee2e8 --- /dev/null +++ b/crates/mcp-server/src/tests.rs @@ -0,0 +1,368 @@ +use serde_json::{Value, json}; + +use crate::{ + Contents, Effect, Info, PROTOCOL_VERSION, Reference, SUPPORTED_VERSIONS, Server, Tools, check_arguments, object, string, text_result, tool, +}; + +/// One app's tools: `echo` and `level`, nothing behind them (no engine, no design). `log` records +/// every listing and call in order. +#[derive(Default)] +struct Echo { + calls: Vec, + log: Vec, +} + +impl Tools for Echo { + fn instructions(&self) -> String { + "Echo test tools.".into() + } + fn list(&mut self) -> Vec { + self.log.push("list".into()); + vec![ + tool("echo", "Echo", "Say `text` back.", object(json!({"text": string("What to say")}), &["text"]), Effect::ReadOnly), + tool("level", "Level", "Set the level.", object(json!({"level": {"type": "string", "enum": ["low", "high"]}}), &[]), Effect::Edits), + ] + } + fn call(&mut self, name: &str, args: &Value) -> Value { + self.log.push(format!("call {name}")); + self.calls.push(json!({"name": name, "args": args})); + text_result(args["text"].as_str().unwrap_or_default(), false) + } +} + +fn info() -> Info { + Info { name: "echo", title: "Echo", version: "1.0.0", max_line: 1 << 16 } +} + +fn serve(lines: &[Value]) -> (Server, Vec) { + serve_with(Echo::default(), lines) +} + +fn serve_with(tools: T, lines: &[Value]) -> (Server, Vec) { + let input: String = lines.iter().map(|l| format!("{l}\n")).collect(); + let mut output = Vec::new(); + let mut s = Server::new(info(), tools); + s.serve(std::io::Cursor::new(input), &mut output).unwrap(); + let replies = String::from_utf8(output).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + (s, replies) +} + +/// A registered tool is listed and called over the stdio transport, with no engine behind it. +#[test] +fn a_registered_tool_is_served_over_stdio() { + let (mut s, replies) = serve(&[ + json!({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-03-26"}}), + json!({"jsonrpc": "2.0", "method": "notifications/initialized"}), + json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list"}), + json!({"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": "hi"}}}), + ]); + assert_eq!(replies.len(), 3, "the notification gets no reply: {replies:?}"); + let init = &replies[0]["result"]; + assert_eq!(init["protocolVersion"], "2025-03-26"); + assert_eq!(init["serverInfo"], json!({"name": "echo", "title": "Echo", "version": "1.0.0"})); + assert_eq!(init["instructions"], "Echo test tools."); + assert!(s.is_initialized()); + assert_eq!(replies[1]["result"]["tools"][0]["name"], "echo"); + assert_eq!(replies[1]["result"]["tools"][0]["annotations"]["readOnlyHint"], true); + assert_eq!(replies[2], json!({"jsonrpc": "2.0", "id": 3, "result": {"content": [{"type": "text", "text": "hi"}]}})); + assert_eq!(s.tools().calls, vec![json!({"name": "echo", "args": {"text": "hi"}})]); +} + +/// Arguments are checked against the tool's schema before the tool runs; unknown tools and +/// methods are JSON-RPC errors. +#[test] +fn arguments_break_the_schema_before_the_tool_runs() { + let (mut s, replies) = serve(&[ + json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "echo", "arguments": {}}}), + json!({"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": 3}}}), + json!({"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": "a", "loud": true}}}), + json!({"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": {"name": "shout", "arguments": {}}}), + json!({"jsonrpc": "2.0", "id": 5, "method": "nope"}), + json!({"jsonrpc": "2.0", "id": 6, "method": "resources/read", "params": {"uri": "x://y"}}), + ]); + let codes: Vec<&Value> = replies.iter().map(|r| &r["error"]["code"]).collect(); + assert_eq!(codes, [&json!(-32602), &json!(-32602), &json!(-32602), &json!(-32602), &json!(-32601), &json!(-32002)], "{replies:?}"); + assert_eq!(replies[0]["error"]["message"], "echo: missing required argument `text`"); + assert_eq!(replies[2]["error"]["message"], "echo: unknown argument `loud`; its arguments are: text"); + assert!(s.tools().calls.is_empty()); + let defs = s.tools().list(); + assert!(check_arguments(&defs, "echo", &json!({"text": "a"})).is_ok()); +} + +/// Hostile framing: oversized lines, bad UTF-8 and bad JSON are answered and the server goes on. +#[test] +fn hostile_lines_are_answered_and_serving_goes_on() { + let mut input = vec![b'x'; (1 << 16) + 10]; + input.push(b'\n'); + input.extend_from_slice(b"\xff\xfe\n{not json\n[]\n"); + input.extend_from_slice(json!({"jsonrpc": "2.0", "id": 9, "method": "ping"}).to_string().as_bytes()); + let mut output = Vec::new(); + Server::new(info(), Echo::default()).serve(std::io::Cursor::new(input), &mut output).unwrap(); + let replies: Vec = String::from_utf8(output).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + let codes: Vec<&Value> = replies.iter().map(|r| r.get("error").map_or(&r["result"], |e| &e["code"])).collect(); + assert_eq!(codes, [&json!(-32600), &json!(-32700), &json!(-32700), &json!(-32600), &json!({})], "{replies:?}"); +} + +#[test] +fn base64_matches_rfc4648() { + for (i, o) in [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] { + assert_eq!(crate::base64(i.as_bytes()), o); + } + let r = crate::image_result(b"foo", "image/png", "info"); + assert_eq!(r, json!({"content": [{"type": "image", "data": "Zm9v", "mimeType": "image/png"}, {"type": "text", "text": "info"}]})); +} + +fn ping(id: usize) -> Value { + json!({"jsonrpc": "2.0", "id": id, "method": "ping"}) +} + +fn notification() -> Value { + json!({"jsonrpc": "2.0", "method": "notifications/progress"}) +} + +fn reply(s: &mut Server, msg: &Value) -> Option { + s.handle_line(&msg.to_string()).map(|r| serde_json::from_str(&r).unwrap()) +} + +/// A batch answers each request in it and none of its notifications; a batch of notifications +/// only gets no reply at all; a batch holds at most 1000 messages. +#[test] +fn batches_answer_their_requests_up_to_the_limit() { + let mut s = Server::new(info(), Echo::default()); + let mixed = + json!([ping(1), notification(), {"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": "b"}}}]); + let replies = reply(&mut s, &mixed).unwrap(); + assert_eq!( + replies, + json!([{"jsonrpc": "2.0", "id": 1, "result": {}}, {"jsonrpc": "2.0", "id": 2, "result": {"content": [{"type": "text", "text": "b"}]}}]) + ); + assert_eq!(reply(&mut s, &json!([notification(), notification()])), None); + let full: Vec = (0..1000).map(ping).collect(); + assert_eq!(reply(&mut s, &Value::Array(full)).unwrap().as_array().map(Vec::len), Some(1000)); + let over: Vec = (0..1001).map(ping).collect(); + let r = reply(&mut s, &Value::Array(over)).unwrap(); + assert_eq!(r, json!({"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "batch too large"}})); +} + +/// We send no requests, so a response is never answered, alone or in a batch; a message with +/// neither a method nor a result, or one that is not an object, is refused; a blank line is skipped. +#[test] +fn a_response_we_never_asked_for_is_ignored() { + let mut s = Server::new(info(), Echo::default()); + assert_eq!(reply(&mut s, &json!({"jsonrpc": "2.0", "id": 1, "result": {}})), None); + assert_eq!(reply(&mut s, &json!({"jsonrpc": "2.0", "id": 2, "error": {"code": -1, "message": "no"}})), None); + assert_eq!( + reply(&mut s, &json!([{"jsonrpc": "2.0", "id": 3, "result": {}}, ping(4)])).unwrap(), + json!([{"jsonrpc": "2.0", "id": 4, "result": {}}]) + ); + let r = reply(&mut s, &json!({"jsonrpc": "2.0", "id": 5})).unwrap(); + assert_eq!(r, json!({"jsonrpc": "2.0", "id": 5, "error": {"code": -32600, "message": "missing `method`"}})); + let r = reply(&mut s, &json!(42)).unwrap(); + assert_eq!(r, json!({"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "message must be an object"}})); + assert_eq!(s.handle_line(" "), None); + assert!(s.tools().log.is_empty()); +} + +/// A version we speak is echoed; one we don't, or none, gets ours. +#[test] +fn an_unknown_protocol_version_gets_ours() { + let mut s = Server::new(info(), Echo::default()); + let version = |s: &mut Server, params: Value| { + reply(s, &json!({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": params})).unwrap()["result"]["protocolVersion"].clone() + }; + for v in SUPPORTED_VERSIONS { + assert_eq!(version(&mut s, json!({"protocolVersion": v})), json!(v)); + } + assert_eq!(version(&mut s, json!({"protocolVersion": "1999-01-01"})), json!(PROTOCOL_VERSION)); + assert_eq!(version(&mut s, json!({"protocolVersion": 7})), json!(PROTOCOL_VERSION)); + assert_eq!(version(&mut s, json!({})), json!(PROTOCOL_VERSION)); + assert!(!s.is_initialized(), "initialized only by the client's notification"); +} + +/// `arguments: null` is no arguments; a value outside a property's `enum` is refused before the +/// tool runs. +#[test] +fn null_arguments_are_none_and_enums_are_checked() { + let mut s = Server::new(info(), Echo::default()); + let call = |s: &mut Server, id: u64, args: Value| { + reply(s, &json!({"jsonrpc": "2.0", "id": id, "method": "tools/call", "params": {"name": "level", "arguments": args}})).unwrap() + }; + assert!(call(&mut s, 1, Value::Null).get("result").is_some()); + let none = reply(&mut s, &json!({"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "level"}})).unwrap(); + assert!(none.get("result").is_some(), "{none}"); + assert!(call(&mut s, 3, json!({"level": "high"})).get("result").is_some()); + let r = call(&mut s, 4, json!({"level": "loud"})); + assert_eq!(r["error"], json!({"code": -32602, "message": "level: `level` must be one of [\"low\",\"high\"]"})); + let r = call(&mut s, 5, json!([])); + assert_eq!(r["error"], json!({"code": -32602, "message": "level: arguments must be an object"})); + let args: Vec<&Value> = s.tools().calls.iter().map(|c| &c["args"]).collect(); + assert_eq!(args, [&json!({}), &json!({}), &json!({"level": "high"})]); +} + +/// An app that serves no resources, templates or prompts lists none of each, says it serves only +/// tools, and has no prompt to fill in or argument to complete. +#[test] +fn the_default_lists_are_empty() { + let mut s = Server::new(info(), Echo::default()); + let result = |s: &mut Server, method: &str| reply(s, &json!({"jsonrpc": "2.0", "id": 1, "method": method})).unwrap()["result"].clone(); + assert_eq!(result(&mut s, "resources/list"), json!({"resources": []})); + assert_eq!(result(&mut s, "resources/templates/list"), json!({"resourceTemplates": []})); + assert_eq!(result(&mut s, "prompts/list"), json!({"prompts": []})); + assert_eq!(result(&mut s, "initialize")["capabilities"], json!({"tools": {"listChanged": false}})); + for method in ["prompts/get", "completion/complete"] { + let r = reply(&mut s, &json!({"jsonrpc": "2.0", "id": 2, "method": method, "params": {"name": "describe"}})).unwrap(); + assert_eq!(r["error"]["code"], -32601, "{method}: {r}"); + } +} + +/// An app that serves more than tools: a prompt with a required and an optional argument, a +/// resource template, a Markdown resource and a PNG, and suggestions for any argument. `asked` +/// records every prompt filled in and every completion asked for. +#[derive(Default)] +struct Library { + asked: Vec, +} + +impl Tools for Library { + fn instructions(&self) -> String { + "Library test.".into() + } + fn list(&mut self) -> Vec { + Vec::new() + } + fn call(&mut self, name: &str, _: &Value) -> Value { + text_result(format!("no tool `{name}`"), true) + } + fn resources(&self) -> Vec { + vec![ + json!({"uri": "lib://readme", "name": "Readme", "mimeType": "text/markdown"}), + json!({"uri": "lib://logo", "name": "Logo", "mimeType": "image/png"}), + ] + } + fn resource_templates(&self) -> Vec { + vec![json!({"uriTemplate": "lib://parts/{name}", "name": "Part", "mimeType": "text/markdown"})] + } + fn read_resource(&mut self, uri: &str) -> Option> { + let markdown = |text: String| Contents::Text { mime_type: "text/markdown".into(), text }; + Some(Ok(match uri { + "lib://readme" => markdown("# Parts\n\nA \"quoted\" line.".into()), + "lib://logo" => Contents::Blob { mime_type: "image/png".into(), data: b"foo".to_vec() }, + "lib://parts/broken" => return Some(Err("the part is being rebuilt".into())), + _ => markdown(format!("# {}", uri.strip_prefix("lib://parts/")?)), + })) + } + fn prompts(&self) -> Vec { + vec![json!({"name": "describe", "title": "Describe a part", "arguments": [{"name": "part", "required": true}, {"name": "tone"}]})] + } + fn get_prompt(&mut self, name: &str, args: &Value) -> Result { + self.asked.push(format!("{name} {args}")); + let text = format!("Describe {} in a {} tone.", args["part"].as_str().unwrap_or_default(), args["tone"].as_str().unwrap_or("plain")); + Ok(json!({"messages": [{"role": "user", "content": {"type": "text", "text": text}}]})) + } + fn complete(&mut self, reference: Reference<'_>, argument: &str, value: &str) -> Vec { + self.asked.push(format!("{reference:?} {argument}={value}")); + (0..150).map(|i| format!("{value}{i}")).collect() + } +} + +/// The shared crate serves an app's prompt, resource template and Markdown and binary resources, +/// and tells the client at `initialize` that it does (#13336). +#[test] +fn an_app_serves_prompts_templates_and_markdown_or_binary_resources() { + let request = |id: u64, method: &str, params: Value| json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}); + let (mut s, replies) = serve_with( + Library::default(), + &[ + request(1, "initialize", json!({"protocolVersion": PROTOCOL_VERSION})), + request(2, "prompts/list", json!({})), + request(3, "prompts/get", json!({"name": "describe", "arguments": {"part": "bracket"}})), + request(4, "resources/templates/list", json!({})), + request(5, "resources/read", json!({"uri": "lib://readme"})), + request(6, "resources/read", json!({"uri": "lib://logo"})), + request(7, "resources/read", json!({"uri": "lib://parts/bracket"})), + request(8, "resources/read", json!({"uri": "lib://parts/broken"})), + request(9, "resources/read", json!({"uri": "lib://nope"})), + ], + ); + let result = |i: usize| &replies[i]["result"]; + assert_eq!( + result(0)["capabilities"], + json!({"tools": {"listChanged": false}, "resources": {"listChanged": false}, "prompts": {"listChanged": false}, "completions": {}}) + ); + assert_eq!(result(1)["prompts"][0]["name"], "describe"); + assert_eq!(result(2), &json!({"messages": [{"role": "user", "content": {"type": "text", "text": "Describe bracket in a plain tone."}}]})); + assert_eq!(result(3), &json!({"resourceTemplates": [{"uriTemplate": "lib://parts/{name}", "name": "Part", "mimeType": "text/markdown"}]})); + // The Markdown goes out as it is, not as a JSON string of it. + assert_eq!(result(4), &json!({"contents": [{"uri": "lib://readme", "mimeType": "text/markdown", "text": "# Parts\n\nA \"quoted\" line."}]})); + assert_eq!(result(5), &json!({"contents": [{"uri": "lib://logo", "mimeType": "image/png", "blob": "Zm9v"}]})); + assert_eq!(result(6), &json!({"contents": [{"uri": "lib://parts/bracket", "mimeType": "text/markdown", "text": "# bracket"}]})); + assert_eq!(replies[7]["error"], json!({"code": -32603, "message": "the part is being rebuilt"})); + assert_eq!(replies[8]["error"]["code"], -32002); + assert_eq!(s.tools().asked, [r#"describe {"part":"bracket"}"#]); +} + +/// A prompt is filled in only with the arguments its listing declares, each a string, the +/// required ones present; anything else is invalid params and the app is never asked. +#[test] +fn prompt_arguments_are_checked_before_the_prompt_is_filled_in() { + let mut s = Server::new(info(), Library::default()); + let get = + |s: &mut Server, params: Value| reply(s, &json!({"jsonrpc": "2.0", "id": 1, "method": "prompts/get", "params": params})).unwrap(); + for (params, message) in [ + (json!({"arguments": {"part": "a"}}), "missing prompt `name`"), + (json!({"name": "summarise"}), "unknown prompt `summarise`"), + (json!({"name": "describe"}), "describe: missing required argument `part`"), + (json!({"name": "describe", "arguments": ["a"]}), "describe: arguments must be an object"), + (json!({"name": "describe", "arguments": {"part": 3}}), "describe: argument `part` must be a string"), + (json!({"name": "describe", "arguments": {"part": null}}), "describe: argument `part` must be a string"), + (json!({"name": "describe", "arguments": {"part": "a", "size": "big"}}), "describe: unknown argument `size`; its arguments are: part, tone"), + ] { + assert_eq!(get(&mut s, params.clone())["error"], json!({"code": -32602, "message": message}), "{params}"); + } + assert!(s.tools().asked.is_empty()); + let filled = get(&mut s, json!({"name": "describe", "arguments": {"part": "hinge", "tone": "dry"}})); + assert_eq!(filled["result"]["messages"][0]["content"]["text"], "Describe hinge in a dry tone."); +} + +/// Completion asks the app about a listed prompt or a resource, and sends the first 100 +/// suggestions with how many there are; a bad reference or argument is invalid params. +#[test] +fn completion_suggests_up_to_a_hundred_values() { + let mut s = Server::new(info(), Library::default()); + let complete = |s: &mut Server, params: Value| { + reply(s, &json!({"jsonrpc": "2.0", "id": 1, "method": "completion/complete", "params": params})).unwrap() + }; + let r = complete(&mut s, json!({"ref": {"type": "ref/prompt", "name": "describe"}, "argument": {"name": "part", "value": "br"}})); + let completion = &r["result"]["completion"]; + assert_eq!(completion["values"].as_array().map(Vec::len), Some(100), "{r}"); + assert_eq!((&completion["values"][0], &completion["total"], &completion["hasMore"]), (&json!("br0"), &json!(150), &json!(true))); + let r = complete(&mut s, json!({"ref": {"type": "ref/resource", "uri": "lib://parts/{name}"}, "argument": {"name": "name", "value": ""}})); + assert_eq!(r["result"]["completion"]["values"][99], "99"); + assert_eq!(s.tools().asked, [r#"Prompt("describe") part=br"#, r#"Resource("lib://parts/{name}") name="#]); + let bad_ref = "`ref` must be a prompt's {type: ref/prompt, name} or a resource's {type: ref/resource, uri}"; + let bad_argument = "`argument` must have a `name` and a `value`, both strings"; + for (params, message) in [ + (json!({"ref": {"type": "ref/prompt", "name": "summarise"}, "argument": {"name": "a", "value": ""}}), "unknown prompt `summarise`"), + (json!({"ref": {"type": "ref/tool", "name": "x"}, "argument": {"name": "a", "value": ""}}), bad_ref), + (json!({"ref": {"type": "ref/resource"}, "argument": {"name": "a", "value": ""}}), bad_ref), + (json!({"argument": {"name": "a", "value": ""}}), bad_ref), + (json!({"ref": {"type": "ref/prompt", "name": "describe"}, "argument": {"name": "part"}}), bad_argument), + (json!({"ref": {"type": "ref/prompt", "name": "describe"}}), bad_argument), + ] { + assert_eq!(complete(&mut s, params.clone())["error"], json!({"code": -32602, "message": message}), "{params}"); + } + assert_eq!(s.tools().asked.len(), 2, "a refused completion never reaches the app"); +} + +/// `Tools::call` runs right after the listing its arguments were checked against, every time, +/// whatever was listed before it; a refused call is listed and never run. +#[test] +fn every_call_runs_right_after_its_listing() { + let mut s = Server::new(info(), Echo::default()); + let call = |name: &str, args: Value| json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": name, "arguments": args}}); + reply(&mut s, &json!({"jsonrpc": "2.0", "id": 1, "method": "tools/list"})); + reply(&mut s, &call("echo", json!({"text": "a"}))); + reply(&mut s, &call("echo", json!({}))); + reply(&mut s, &json!([call("level", json!({})), call("echo", json!({"text": "b"}))])); + assert_eq!(s.tools().log, ["list", "list", "call echo", "list", "list", "call level", "list", "call echo"]); +} diff --git a/crates/media/src/isobmff/config_fields.rs b/crates/media/src/isobmff/config_fields.rs new file mode 100644 index 000000000..2821719bf --- /dev/null +++ b/crates/media/src/isobmff/config_fields.rs @@ -0,0 +1,114 @@ +//! Tolerant fields from AVC and HEVC configuration records. + +/// NAL length prefix width from AVC configuration, defaulting to four on a short record. +pub fn avcc_length_size(avcc: &[u8]) -> usize { + avcc.get(4).map_or(4, |b| (b & 3) as usize + 1) +} + +/// NAL length prefix width and highest temporal ID from HEVC configuration. +pub fn hvcc_length_size_and_tid(hvcc: &[u8]) -> (usize, Option) { + let length_size = hvcc.get(21).map_or(4, |b| (b & 3) as usize + 1); + let highest_tid = hvcc.get(21).map(|b| (b >> 3) & 7).filter(|&n| n > 0).map(|n| n - 1); + (length_size, highest_tid) +} + +/// objectTypeIndication of the DecoderConfigDescriptor inside an `esds` box in `raw`. +pub fn esds_object_type(raw: &[u8]) -> Option { + let at = raw.windows(4).position(|w| w == b"esds")? + 8; + let mut p = at; + let size = |p: &mut usize| -> Option { + let mut n = 0usize; + for _ in 0..4 { + let b = *raw.get(*p)?; + *p += 1; + n = (n << 7) | (b & 0x7F) as usize; + if b & 0x80 == 0 { + break; + } + } + Some(n) + }; + if *raw.get(p)? != 0x03 { + return None; + } + p += 1; + size(&mut p)?; + let flags = *raw.get(p + 2)?; + p += 3; + if flags & 0x80 != 0 { + p += 2; + } + if flags & 0x40 != 0 { + p += 1 + *raw.get(p)? as usize; + } + if flags & 0x20 != 0 { + p += 2; + } + if *raw.get(p)? != 0x04 { + return None; + } + p += 1; + size(&mut p)?; + raw.get(p).copied() +} + +/// E-AC-3 `ec-3` entry: the program-layout fields are the 24 bits starting at byte 2 of the +/// descriptor, then `bsmod(5) | acmod(3) | lfeon(1)`. Matches FFmpeg's `dec3` demuxer. +pub fn dec3_layout(payload: &[u8]) -> Option<(u32, u32)> { + if payload.len() < 5 { + return None; + } + let bits = u32::from(payload[2]) << 16 | u32::from(payload[3]) << 8 | u32::from(payload[4]); + let acmod = ((bits >> 9) & 0x7) as u8; + let lfeon = bits & 0x100 != 0; + let channels = dac3_channels(acmod, lfeon)?; + Some((0, channels)) +} + +/// AC-3 `ac-3` entry: `dac3` begins with the syncframe bitstream information: sample rate code, +/// bit stream id, bit stream mode, channel mode and LFE flag. The descriptor has no sample rate, +/// so the caller keeps the sample-entry rate. +pub fn dac3_layout(payload: &[u8]) -> Option<(u32, u32)> { + if payload.len() < 3 { + return None; + } + // The descriptor begins with the bitstream information: fscod(2), bsid(5), bsmod(3), + // acmod(3) and lfeon(1), continuing in the second payload byte. + let bits = u32::from_be_bytes([0, 0, payload[1], payload[2]]); + let acmod = ((bits >> 11) & 0x7) as u8; + let lfeon = bits & 0x400 != 0; + let channels = dac3_channels(acmod, lfeon)?; + Some((0, channels)) +} + +/// Full-bandwidth channels per AC-3 channel mode (Table 5.8) plus the optional LFE channel. +fn dac3_channels(acmod: u8, lfeon: bool) -> Option { + let n = [2u32, 1, 2, 3, 3, 4, 4, 5].get(usize::from(acmod & 7))?.checked_add(u32::from(lfeon))?; + (n > 0).then_some(n) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn audio_layout_and_elementary_descriptor_are_bounded() { + assert_eq!(dac3_layout(&[0, 0, 0]), Some((0, 2))); + assert_eq!(dec3_layout(&[0, 0, 0, 0, 0]), Some((0, 2))); + assert_eq!(dac3_layout(&[0, 0]), None); + assert_eq!(dec3_layout(&[0; 4]), None); + let esds = [b'e', b's', b'd', b's', 0, 0, 0, 0, 0x03, 0x04, 0, 0, 0, 0x04, 0x01, 0x60]; + assert_eq!(esds_object_type(&esds), Some(0x60)); + for end in 0..esds.len() { + assert_eq!(esds_object_type(&esds[..end]), None); + } + } + #[test] + fn short_records_and_temporal_layers() { + assert_eq!(avcc_length_size(&[]), 4); + assert_eq!(avcc_length_size(&[0, 0, 0, 0, 1]), 2); + assert_eq!(hvcc_length_size_and_tid(&[]), (4, None)); + let mut h = [0; 22]; + h[21] = (3 << 3) | 2; + assert_eq!(hvcc_length_size_and_tid(&h), (3, Some(2))); + } +} diff --git a/crates/media/src/isobmff/mod.rs b/crates/media/src/isobmff/mod.rs index 4692ebedd..142a606d6 100644 --- a/crates/media/src/isobmff/mod.rs +++ b/crates/media/src/isobmff/mod.rs @@ -14,6 +14,7 @@ pub mod boxes; mod bytes; pub mod chapters; mod codec; +mod config_fields; mod demux; mod error; mod mux; @@ -25,6 +26,7 @@ pub use codec::{ AacConfig, ApvConfig, AudioParams, Av1Config, AvcConfig, BitRate, CleanAperture, CodecConfig, ColorInfo, FieldInfo, FlacConfig, HevcConfig, HevcNalArray, MasteringDisplay, OpusConfig, PcmConfig, SampleEntry, TimecodeConfig, VideoParams, VpcConfig, parse_asc, }; +pub use config_fields::{avcc_length_size, dac3_layout, dec3_layout, esds_object_type, hvcc_length_size_and_tid}; pub use demux::{Edit, Metadata, Mp4File, Sample, SampleGroup, Track, TrackKind, open}; pub use error::{Error, Result}; pub use mux::{Brand, FragmentedWriter, Mp4Writer, TrackConfig, WriteSample, WriterOptions}; diff --git a/crates/media/src/matroska/mod.rs b/crates/media/src/matroska/mod.rs index 9e60a9600..22d9dd9af 100644 --- a/crates/media/src/matroska/mod.rs +++ b/crates/media/src/matroska/mod.rs @@ -22,6 +22,7 @@ mod mux; #[cfg(test)] mod synthetic_tests; mod track; +mod vp9_features; pub use crate::source::{ByteSource, ReadSeekSource}; pub use codec::Codec; @@ -32,6 +33,7 @@ pub use meta::{ }; pub use mux::{LacingMode, MkvWriter, MuxOptions, TrackSpec}; pub use track::{AudioInfo, Color, ContentEncoding, MasteringMetadata, Projection, Sample, Track, TrackKind, VideoInfo}; +pub use vp9_features::{Vp9Features, vp9_features}; /// Element IDs used by the demuxer (marker bits included), for callers inspecting raw elements. pub mod element_ids { diff --git a/crates/media/src/matroska/vp9_features.rs b/crates/media/src/matroska/vp9_features.rs new file mode 100644 index 000000000..05359cc67 --- /dev/null +++ b/crates/media/src/matroska/vp9_features.rs @@ -0,0 +1,42 @@ +//! VP9 CodecPrivate feature records in Matroska/WebM. + +/// The fields used to configure VP9 decoding and sample descriptions. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Vp9Features { + pub profile: u8, + pub level: u8, + pub bit_depth: u8, + pub chroma_subsampling: u8, +} + +/// Read ID/length/value triples, retaining defaults when a trailing record is truncated. +pub fn vp9_features(private: &[u8]) -> Vp9Features { + let mut result = Vp9Features { profile: 0, level: 0, bit_depth: 8, chroma_subsampling: 0 }; + let mut at = 0; + while at + 2 <= private.len() { + let (id, len) = (private[at], private[at + 1] as usize); + let Some(value) = private.get(at + 2..at + 2 + len) else { break }; + let value = value.first().copied().unwrap_or(0); + match id { + 1 => result.profile = value, + 2 => result.level = value, + 3 => result.bit_depth = value, + 4 => result.chroma_subsampling = value, + _ => {} + } + at += 2 + len; + } + result +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn features_keep_earlier_values_when_a_later_record_is_truncated() { + let f = vp9_features(&[1, 1, 2, 3, 1, 10, 4, 2, 9]); + assert_eq!(f, Vp9Features { profile: 2, level: 0, bit_depth: 10, chroma_subsampling: 0 }); + assert_eq!(vp9_features(&[3, 0]).bit_depth, 0); + assert_eq!(vp9_features(&[]).bit_depth, 8); + } +} diff --git a/crates/mesh/Cargo.toml b/crates/mesh/Cargo.toml index 8c95d3734..861c19239 100644 --- a/crates/mesh/Cargo.toml +++ b/crates/mesh/Cargo.toml @@ -16,13 +16,14 @@ stl = [] obj = [] ply = [] gltf = ["dep:serde_json"] -fbx = ["dep:miniz_oxide"] +fbx = ["dep:miniz_oxide", "dep:craft-zip"] threemf = ["dep:craft-opc", "dep:craft-xml"] [dependencies] serde_json = { workspace = true, optional = true } # zlib-compressed arrays in binary FBX. miniz_oxide = { version = "0.9", optional = true } +craft-zip = { workspace = true, optional = true } # 3MF is an OPC package of XML parts. craft-opc = { workspace = true, optional = true } craft-xml = { workspace = true, optional = true } diff --git a/crates/mesh/src/fbx.rs b/crates/mesh/src/fbx.rs index 3a8115bce..5c7eaab42 100644 --- a/crates/mesh/src/fbx.rs +++ b/crates/mesh/src/fbx.rs @@ -39,21 +39,23 @@ //! name is listed in [`Scene::external`]. The names come from the file: a resolver must not trust //! them as paths. //! -//! Not read: FBX 5 and older, animation stacks, skin deformers, blend shapes, cameras and -//! lights, NURBS and patches. +//! Not read: FBX 5 and older, animation stacks, skin deformers, blend shapes, lights, NURBS and patches. //! //! [`Limits::max_data_bytes`] bounds what one file may keep in memory: its parsed values (an //! array element counts 8 bytes whatever its type in the file), the meshes built from them and //! the image bytes. use std::cell::Cell; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use crate::{ - AlphaMode, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Resolver, Result, Scene, TextureRef, base64_decode, - count_warning, malformed, mat_mul, srgb_to_linear, too_large, + AlphaMode, Camera, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Projection, Resolver, Result, Scene, TextureRef, + base64_decode, count_warning, malformed, mat_mul, srgb_to_linear, too_large, }; +/// Bounded ZIP packages containing an FBX and its texture files. +pub mod package; + const KIND: &str = "FBX"; /// The binary file's magic (`Kaydara FBX Binary`, two spaces, NUL, 0x1A, 0x00). @@ -89,7 +91,8 @@ impl Budget { } /// Bytes the values under `records` keep. Used for ASCII files, which hold array elements as one -/// [`Prop`] each and are parsed before they can be counted (the text itself bounds them). +/// [`Prop`] each. +#[cfg(test)] pub(crate) fn value_bytes(records: &[Record]) -> Option { records.iter().try_fold(0usize, |acc, n| { let own = n.props.iter().try_fold(0usize, |a, p| { @@ -109,7 +112,13 @@ pub(crate) fn value_bytes(records: &[Record]) -> Option { /// Bytes a mesh keeps. pub(crate) fn mesh_bytes(m: &Mesh) -> Option { let uvs = m.uv_sets.iter().try_fold(0usize, |a, s| a.checked_add(s.len().checked_mul(8)?))?; - let pools = m.positions.len().checked_mul(24)?.checked_add(m.normals.len().checked_mul(24)?)?.checked_add(uvs)?; + let pools = m + .positions + .len() + .checked_mul(24)? + .checked_add(m.normals.len().checked_mul(24)?)? + .checked_add(uvs)? + .checked_add(m.colors.len().checked_mul(16)?)?; m.parts.iter().try_fold(pools, |a, p| a.checked_add(p.corners.len().checked_mul(12)?)?.checked_add(p.face_sizes.len().checked_mul(4)?)) } @@ -229,13 +238,29 @@ pub fn is_binary(bytes: &[u8]) -> bool { /// Read an FBX file (binary or ASCII) into its records. pub fn read_document(bytes: &[u8], limits: &Limits) -> Result { + read_document_impl(bytes, limits, false) +} + +/// Read records strictly, checking ASCII delimiters and the binary closing record. +pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result { + read_document_impl(bytes, limits, true) +} + +fn read_document_impl(bytes: &[u8], limits: &Limits, strict: bool) -> Result { let budget = Budget::new(limits.max_data_bytes); let (version, records) = if is_binary(bytes) { - read_binary(bytes, &budget)? + read_binary(bytes, &budget, strict)? } else { - let (version, records) = read_ascii(&String::from_utf8_lossy(bytes))?; - budget.take(value_bytes(&records), "its values")?; - (version, records) + let text = match std::str::from_utf8(bytes) { + Ok(text) => std::borrow::Cow::Borrowed(text), + Err(_) => { + if bytes.len() > limits.max_data_bytes { + return Err(too_large(KIND, "invalid UTF-8 text exceeds the memory limit")); + } + String::from_utf8_lossy(bytes) + } + }; + read_ascii(&text, &budget, strict)? }; Ok(Document { version, records, left: budget.left.get() }) } @@ -254,6 +279,7 @@ struct Bin<'a> { /// 64-bit record offsets (version 7500 and later). wide: bool, budget: &'a Budget, + records_budget: &'a Budget, } impl Bin<'_> { @@ -313,6 +339,8 @@ impl Bin<'_> { if end <= start || end > limit.min(self.d.len()) { return Err(malformed(KIND, format!("bad record end {end} at byte {start}"))); } + // Bound record and property-vector allocations before copying names or reserving storage. + self.records_budget.take(std::mem::size_of::().checked_add(nlen.saturating_mul(3)), "its records")?; let name = String::from_utf8_lossy(self.take(pos, nlen)?).into_owned(); let pstart = *pos; let pend = @@ -321,7 +349,9 @@ impl Bin<'_> { if nprops > plen as u64 { return Err(malformed(KIND, format!("`{name}`: {nprops} properties in {plen} bytes"))); } - let mut props = Vec::with_capacity(nprops as usize); + let count = usize::try_from(nprops).map_err(|_| too_large(KIND, "too many properties"))?; + self.records_budget.take(count.checked_mul(std::mem::size_of::()), "its properties")?; + let mut props = Vec::with_capacity(count); for _ in 0..nprops { props.push(self.prop(pos, pend)?); } @@ -332,7 +362,12 @@ impl Bin<'_> { } fn prop(&self, pos: &mut usize, pend: usize) -> Result { - let sub = Bin { d: self.d.get(..pend).ok_or_else(|| malformed(KIND, "truncated property"))?, wide: self.wide, budget: self.budget }; + let sub = Bin { + d: self.d.get(..pend).ok_or_else(|| malformed(KIND, "truncated property"))?, + wide: self.wide, + budget: self.budget, + records_budget: self.records_budget, + }; let t = sub.arr::<1>(pos)?[0]; Ok(match t { b'Y' => Prop::Int(i64::from(i16::from_le_bytes(sub.arr(pos)?))), @@ -343,10 +378,12 @@ impl Bin<'_> { b'D' => Prop::Float(f64::from_le_bytes(sub.arr(pos)?)), b'S' => { let n = sub.u32(pos)? as usize; + sub.budget.take(n.checked_mul(3), "a string")?; Prop::Str(object_name(sub.take(pos, n)?)) } b'R' => { let n = sub.u32(pos)? as usize; + sub.budget.take(Some(n), "binary data")?; Prop::Bytes(sub.take(pos, n)?.to_vec()) } b'f' | b'd' | b'l' | b'i' | b'b' => { @@ -398,143 +435,178 @@ fn object_name(b: &[u8]) -> String { } } -fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec)> { +fn read_binary(bytes: &[u8], budget: &Budget, strict: bool) -> Result<(u32, Vec)> { let mut pos = BINARY_MAGIC.len(); - let version = Bin { d: bytes, wide: false, budget }.u32(&mut pos)?; - let bin = Bin { d: bytes, wide: version >= 7500, budget }; + let records_budget = Budget::new(budget.limit); + let version = Bin { d: bytes, wide: false, budget, records_budget: &records_budget }.u32(&mut pos)?; + let bin = Bin { d: bytes, wide: version >= 7500, budget, records_budget: &records_budget }; let mut records = vec![]; + let mut closed = false; while pos < bytes.len() { match bin.record(&mut pos, bytes.len(), 0) { Ok(Some(n)) => records.push(n), - Ok(None) => break, + Ok(None) => { + closed = true; + break; + } // A damaged tail (footer, padding) after complete sections is ignored. - Err(crate::Error::Malformed(_)) if records.iter().any(|n| n.name == "Objects") => break, + Err(crate::Error::Malformed(_)) if !strict && records.iter().any(|n| n.name == "Objects") => break, Err(e) => return Err(e), } } + if strict && !closed { + return Err(malformed(KIND, "missing binary closing record")); + } Ok((version, records)) } // --------------------------------------------------------------------------------------------- // ASCII -#[derive(Clone, Debug, PartialEq)] -enum Tok { - Key(String), - Val(Prop), - /// `*N` array length (the values follow in an `a:` child). +#[derive(Clone, Copy, Debug, PartialEq)] +enum Tok<'a> { + Key(&'a str), + Value(&'a str, bool), Count, Comma, Open, Close, } -fn tokenize(text: &str) -> Vec { - let mut out = vec![]; - let mut it = text.char_indices().peekable(); - while let Some(&(i, c)) = it.peek() { - match c { - c if c.is_whitespace() => { - it.next(); +/// Scan on demand so punctuation and ignored input cannot build an unbounded token vector. +struct Ascii<'a> { + text: &'a str, + pos: usize, + lookahead: Option>, + strict: bool, + unterminated: bool, +} + +impl<'a> Ascii<'a> { + fn next_token(&mut self) -> Option> { + loop { + let rest = self.text.get(self.pos..)?; + let c = rest.chars().next()?; + if c.is_whitespace() { + self.pos += c.len_utf8(); + continue; } - ';' => { - for (_, c) in it.by_ref() { - if c == '\n' { - break; - } + if c == ';' { + self.pos += rest.find('\n').map_or(rest.len(), |i| i + 1); + continue; + } + match c { + ',' | '{' | '}' => { + self.pos += 1; + return Some(match c { + ',' => Tok::Comma, + '{' => Tok::Open, + _ => Tok::Close, + }); } - } - ',' => { - it.next(); - out.push(Tok::Comma); - } - '{' => { - it.next(); - out.push(Tok::Open); - } - '}' => { - it.next(); - out.push(Tok::Close); - } - '"' => { - it.next(); - let start = i + 1; - let mut end = text.len(); - for (j, c) in it.by_ref() { - if c == '"' { - end = j; - break; - } + '"' => { + let tail = rest.get(1..).unwrap_or(""); + let end = tail.find('"').unwrap_or(tail.len()); + self.unterminated |= end == tail.len(); + self.pos += 1 + end + usize::from(end < tail.len()); + return Some(Tok::Value(tail.get(..end).unwrap_or(""), true)); } - out.push(Tok::Val(Prop::Str(text.get(start..end).unwrap_or("").replace(""", "\"")))); - } - _ => { - let start = i; - let mut end = text.len(); - while let Some(&(j, c)) = it.peek() { - if c.is_whitespace() || matches!(c, ',' | '{' | '}' | '"' | ';' | ':') { - end = j; - break; + _ => { + let end = rest.find(|c: char| c.is_whitespace() || matches!(c, ',' | '{' | '}' | '"' | ';' | ':')).unwrap_or(rest.len()); + let word = rest.get(..end).unwrap_or(""); + self.pos += end; + if rest.as_bytes().get(end) == Some(&b':') { + self.pos += 1; + return Some(Tok::Key(word)); + } + if word.starts_with('*') { + return Some(Tok::Count); + } + if !word.is_empty() { + return Some(Tok::Value(word, false)); } - it.next(); - } - let word = text.get(start..end).unwrap_or(""); - if it.peek().is_some_and(|(_, c)| *c == ':') { - it.next(); - out.push(Tok::Key(word.to_string())); - } else if word.starts_with('*') { - out.push(Tok::Count); - } else if let Ok(i) = word.parse::() { - out.push(Tok::Val(Prop::Int(i))); - } else if let Ok(f) = word.parse::() { - out.push(Tok::Val(Prop::Float(f))); - } else if !word.is_empty() { - out.push(Tok::Val(Prop::Str(word.to_string()))); - } else { // A stray `:` with no key. - it.next(); + self.pos += 1; } } } } - out + + fn peek(&mut self) -> Option> { + if self.lookahead.is_none() { + self.lookahead = self.next_token(); + } + self.lookahead + } + + fn take(&mut self) -> Option> { + self.lookahead.take().or_else(|| self.next_token()) + } } -fn ascii_list(toks: &[Tok], i: &mut usize, depth: usize) -> Result> { +fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, depth: usize) -> Result> { if depth > MAX_DEPTH { return Err(too_large(KIND, "records nested too deeply")); } let mut out = vec![]; - while let Some(t) = toks.get(*i) { - *i += 1; + while let Some(t) = scan.take() { match t { Tok::Close if depth > 0 => return Ok(out), + Tok::Close if scan.strict => return Err(malformed(KIND, "unbalanced closing brace")), Tok::Key(name) => { - let mut record = Record { name: name.clone(), ..Record::default() }; + records_budget.take(std::mem::size_of::().checked_add(name.len()), "its records")?; + let mut record = Record { name: name.to_string(), ..Record::default() }; + let mut needs_comma = false; loop { - match toks.get(*i) { - Some(Tok::Val(v)) => record.props.push(v.clone()), - Some(Tok::Count | Tok::Comma) => {} + match scan.peek() { + Some(Tok::Value(..) | Tok::Count) if scan.strict && needs_comma => break, + Some(Tok::Value(value, quoted)) => { + let number = if quoted { + None + } else { + value.parse::().ok().map(Prop::Int).or_else(|| value.parse::().ok().map(Prop::Float)) + }; + let size = std::mem::size_of::().checked_add(if number.is_some() { 0 } else { value.len() }); + // Charge before copying a string or growing the property vector. + budget.take(size, "its values")?; + record + .props + .push(number.unwrap_or_else(|| Prop::Str(if quoted { value.replace(""", "\"") } else { value.to_string() }))); + } + Some(Tok::Count) => {} + Some(Tok::Comma) => { + needs_comma = false; + scan.take(); + continue; + } _ => break, } - *i += 1; + needs_comma = true; + scan.take(); } - if toks.get(*i) == Some(&Tok::Open) { - *i += 1; - record.children = ascii_list(toks, i, depth + 1)?; + if scan.peek() == Some(Tok::Open) { + scan.take(); + record.children = ascii_list(scan, budget, records_budget, depth + 1)?; } out.push(record); } + _ if scan.strict => return Err(malformed(KIND, "expected a property name")), _ => {} } } + if scan.strict && depth > 0 { + return Err(malformed(KIND, "missing closing brace")); + } Ok(out) } -fn read_ascii(text: &str) -> Result<(u32, Vec)> { - let toks = tokenize(text); - let mut i = 0; - let records = ascii_list(&toks, &mut i, 0)?; +fn read_ascii(text: &str, budget: &Budget, strict: bool) -> Result<(u32, Vec)> { + let mut scan = Ascii { text, pos: 0, lookahead: None, strict, unterminated: false }; + let records_budget = Budget::new(budget.limit); + let records = ascii_list(&mut scan, budget, &records_budget, 0)?; + if strict && scan.unterminated { + return Err(malformed(KIND, "unterminated string")); + } let version = records .iter() .find(|n| n.name == "FBXHeaderExtension") @@ -739,6 +811,11 @@ impl Document { /// Build a scene with explicitly selected compatibility behavior. pub fn scene_with_options(&self, limits: &Limits, resolve: Resolver, options: &SceneOptions) -> Result { + self.scene_with_placements(limits, resolve, options).map(|(scene, _)| scene) + } + + /// Build a scene and list its mesh nodes in FBX placement order. + pub fn scene_with_placements(&self, limits: &Limits, resolve: Resolver, options: &SceneOptions) -> Result<(Scene, Vec)> { if self.version != 0 && self.version < 6000 { return Err(crate::Error::Unsupported(format!( "FBX {}.{} (only FBX 6.x and 7.x files are read)", @@ -814,50 +891,289 @@ impl Document { } } - // One mesh per Model with Mesh geometry (material slots are per Model). Models instancing - // the same geometry with the same materials share one mesh rather than each building a - // copy. - let mut mesh_of: HashMap<(Key, Vec), usize> = HashMap::new(); - for (id, o) in &models { + // FBX cameras look along +X; a child turns the neutral camera's -Z onto +X. + // Keep this separate from the Model so its children retain their original transforms. + for (id, model) in &models { + let class_index = if matches!(model.props.first(), Some(Prop::Int(_))) { 2 } else { 1 }; + if model.str_at(class_index) != Some("Camera") { + continue; + } + let name = short_name(model.first_str().unwrap_or("")); + if name.starts_with("Producer ") { + continue; + } let Some(ni) = node_of.get(id).copied() else { continue }; - let Some((gid, geom)) = b.geometry_of(*id) else { continue }; - let mut mats: Vec = vec![]; + let lens = b.children_of(*id, "NodeAttribute").last().and_then(|(key, _)| b.by_key.get(key)).copied().unwrap_or(model); + let props = Props::of(lens); + let positive = |key| props.f(key).filter(|v| *v > 0.0); + let projection = if props.f("CameraProjectionType") == Some(1.0) { + let mag = positive("OrthoZoom").unwrap_or(1.0); + Projection::Orthographic { xmag: mag, ymag: mag, znear: 0.01, zfar: 1000.0 } + } else { + let yfov = match positive("FocalLength") { + Some(focal) => 2.0 * (positive("FilmHeight").unwrap_or(0.612) * 25.4 / (2.0 * focal)).atan(), + None => positive("FieldOfViewY").or_else(|| positive("FieldOfView")).unwrap_or(40.0).to_radians(), + }; + Projection::Perspective { yfov, aspect: None, znear: 0.01, zfar: None } + }; + if b.scene.cameras.len() >= limits.max_extras { + return Err(too_large(KIND, "too many cameras")); + } + b.node_room()?; + let ci = b.scene.cameras.len(); + b.scene.cameras.push(Camera { name: name.to_string(), projection }); + let child = b.scene.nodes.len(); + b.scene.nodes.push(Node { name: format!("{name} camera"), transform: rotate(1, -90.0), camera: Some(ci), ..Node::default() }); + if let Some(node) = b.scene.nodes.get_mut(ni) { + node.children.push(child); + } + } + + // One mesh per geometry and material-slot combination, shared by instances. + let mut mesh_of: HashMap<(Key, Vec), usize> = HashMap::new(); + let mut placed = HashSet::new(); + let mut placement_nodes = HashMap::new(); + for (id, model) in &models { + let Some(ni) = node_of.get(id).copied() else { continue }; + let geometries = b.geometries_of(*id); + if geometries.is_empty() { + continue; + } + let mut mats = Vec::new(); for (mid, _) in b.children_of(*id, "Material") { mats.extend(b.material(mid)?); } let name = b.scene.nodes.get(ni).map(|n| n.name.clone()).unwrap_or_default(); - let mesh_index = match mesh_of.get(&(gid, mats.clone())) { - Some(i) => *i, - None => { - let Some(mesh) = b.mesh(&name, geom, &mats)? else { continue }; - b.budget.take(mesh_bytes(&mesh), &format!("mesh `{name}`"))?; - if b.scene.meshes.len() >= limits.max_meshes { - return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes))); - } - b.scene.meshes.push(mesh); - mesh_of.insert((gid, mats), b.scene.meshes.len() - 1); - b.scene.meshes.len() - 1 + for (gid, geom) in geometries { + if !placed.insert((gid, Some(*id))) { + continue; } - }; - match geometric(&Props::of(o)) { - Some(transform) => { + let mesh_index = match mesh_of.get(&(gid, mats.clone())) { + Some(i) => *i, + None => { + let Some(mesh) = b.mesh(&name, geom, &mats)? else { continue }; + b.budget.take(mesh_bytes(&mesh), &format!("mesh `{name}`"))?; + if b.scene.meshes.len() >= limits.max_meshes { + return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes))); + } + b.scene.meshes.push(mesh); + mesh_of.insert((gid, mats.clone()), b.scene.meshes.len() - 1); + b.scene.meshes.len() - 1 + } + }; + let transform = geometric(&Props::of(model)); + let occupied = b.scene.nodes.get(ni).is_some_and(|n| n.mesh.is_some()); + let placement_node = if transform.is_some() || occupied { b.node_room()?; - let gi = b.scene.nodes.len(); - b.scene.nodes.push(Node { name: format!("{name} geometry"), transform, mesh: Some(mesh_index), ..Node::default() }); - if let Some(n) = b.scene.nodes.get_mut(ni) { - n.children.push(gi); + let child = b.scene.nodes.len(); + b.scene.nodes.push(Node { + name: format!("{name} geometry"), + transform: transform.unwrap_or(IDENTITY), + mesh: Some(mesh_index), + ..Node::default() + }); + if let Some(node) = b.scene.nodes.get_mut(ni) { + node.children.push(child); } + child + } else { + if let Some(node) = b.scene.nodes.get_mut(ni) { + node.mesh = Some(mesh_index); + } + ni + }; + placement_nodes.insert((gid, Some(*id)), Placement { node: placement_node, model: Some(ni) }); + } + } + // Geometry with no Model connection still contributes a mesh at the origin. + let placed_ids: HashSet> = placed.iter().map(|(gid, _)| *gid).collect(); + let mut unplaced = Vec::new(); + for geom in objects.children.iter().filter(|o| o.name == "Geometry" && o.child("Vertices").is_some()) { + let Some(gid) = key(geom.props.first()) else { continue }; + if placed_ids.contains(&gid) { + continue; + } + let Some(mesh) = b.mesh("", geom, &[])? else { continue }; + b.budget.take(mesh_bytes(&mesh), "unplaced mesh")?; + if b.scene.meshes.len() >= limits.max_meshes { + return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes))); + } + b.node_room()?; + let mesh_index = b.scene.meshes.len(); + b.scene.meshes.push(mesh); + let node_index = b.scene.nodes.len(); + b.scene.roots.push(node_index); + b.scene.nodes.push(Node { mesh: Some(mesh_index), ..Node::default() }); + unplaced.push((key_text(gid), Placement { node: node_index, model: None })); + } + let placements = ordered_placements(&models, &links, &placement_nodes, unplaced); + let mut scene = b.scene; + count_warning(&mut scene.warnings, b.skipped, "polygons with fewer than three corners or a vertex that is not there were left out"); + Ok((scene, placements)) + } +} + +fn ordered_placements<'a>( + models: &[(Key<'a>, &Record)], + links: &[(Key<'a>, Key<'a>)], + placement_nodes: &HashMap<(Key<'a>, Option>), Placement>, + mut unplaced: Vec<(String, Placement)>, +) -> Vec { + let mut placements = Vec::new(); + let mut seen = HashSet::new(); + let mut add = |key| { + if let Some(placement) = placement_nodes.get(&key).copied() + && seen.insert(placement.node) + { + placements.push(placement); + } + }; + // FBX 6 stores geometry in its model; connected FBX 7 geometry follows connection order. + for (id, model) in models { + if model.child("Vertices").is_some() { + add((*id, Some(*id))); + } + } + for (child, parent) in links { + add((*child, Some(*parent))); + } + unplaced.sort_by(|a, b| a.0.cmp(&b.0)); + placements.extend(unplaced.into_iter().map(|(_, index)| index)); + placements +} + +/// One placed FBX mesh, with its scene node and source model node (if any). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Placement { + /// The scene node holding the mesh. + pub node: usize, + /// The node of the model the mesh is placed by; `None` for geometry no model is connected to. + pub model: Option, +} + +/// Diffuse material values as stored in FBX, before color-space conversion. +#[derive(Clone, Debug, PartialEq)] +pub struct DiffuseMaterial { + pub name: String, + pub factor: [f32; 4], + /// Encoded embedded or resolved image bytes, when available. + pub image: Option>, +} + +impl Document { + /// Read all FBX diffuse materials, including unconnected materials. The resolver receives + /// the relative path followed by the absolute path's leaf, in preference order. + pub fn diffuse_materials(&self, limits: &Limits, resolve: &mut dyn FnMut(&[&str]) -> Option>) -> Result> { + let objects = self.top("Objects").ok_or_else(|| malformed(KIND, "no Objects section"))?; + let mut materials = HashMap::new(); + let mut textures = HashMap::new(); + let mut videos = HashMap::new(); + for record in &objects.children { + let Some(id) = key(record.props.first()) else { continue }; + match record.name.as_str() { + "Material" => { + materials.insert(id, record); } - None => { - if let Some(n) = b.scene.nodes.get_mut(ni) { - n.mesh = Some(mesh_index); + "Texture" => { + textures.insert(id, record); + } + "Video" => { + videos.insert(id, record); + } + _ => {} + } + } + if materials.len() > limits.max_materials { + return Err(too_large(KIND, "too many materials")); + } + let mut video_of = HashMap::new(); + let mut diffuse = HashMap::new(); + for link in self.top("Connections").into_iter().flat_map(|c| c.children.iter()).filter(|c| c.name == "C" || c.name == "Connect") { + let (Some(child), Some(parent)) = (key(link.props.get(1)), key(link.props.get(2))) else { continue }; + if videos.contains_key(&child) && textures.contains_key(&parent) { + video_of.insert(parent, child); + } else if textures.contains_key(&child) && materials.contains_key(&parent) { + match (link.str_at(0), link.str_at(3)) { + (Some("OP"), Some("DiffuseColor" | "Maya|baseColor")) => { + diffuse.insert(parent, child); } + (Some("OO"), _) => { + diffuse.entry(parent).or_insert(child); + } + _ => {} } } } - let mut scene = b.scene; - count_warning(&mut scene.warnings, b.skipped, "polygons with fewer than three corners or a vertex that is not there were left out"); - Ok(scene) + let budget = Budget { left: Cell::new(self.left.min(limits.max_data_bytes)), limit: limits.max_data_bytes }; + let mut out = Vec::new(); + for (id, material) in materials { + let tex = diffuse.get(&id).and_then(|t| textures.get(t)).copied(); + let video = diffuse.get(&id).and_then(|t| video_of.get(t)).and_then(|v| videos.get(v)).copied(); + let mut image = None; + if let Some(content) = video.or(tex).and_then(|n| n.child("Content")) { + match content.props.first() { + Some(Prop::Bytes(bytes)) if !bytes.is_empty() => { + budget.take(Some(bytes.len()), "an embedded image")?; + image = Some(bytes.clone()); + } + Some(Prop::Str(_)) => { + let size = content.props.iter().filter_map(Prop::as_str).try_fold(0usize, |n, s| n.checked_add(s.len())); + // Reserve a conservative bound before concatenating and decoding. + budget.take(size, "an embedded image")?; + let text: String = content.props.iter().filter_map(Prop::as_str).collect(); + image = base64_decode(text.trim()).filter(|b| !b.is_empty()); + } + _ => {} + } + } + if image.is_none() { + for record in [tex, video].into_iter().flatten() { + let relative = record.child("RelativeFilename").and_then(|n| n.str_at(0)).unwrap_or(""); + let absolute = ["FileName", "Filename"] + .into_iter() + .find_map(|n| record.child(n).and_then(|v| v.str_at(0)).filter(|s| !s.is_empty())) + .unwrap_or(""); + let leaf = absolute.rsplit(['/', '\\']).next().unwrap_or(""); + let mut paths = Vec::new(); + for path in [relative, leaf] { + if !path.is_empty() && !paths.contains(&path) { + paths.push(path); + } + } + if !paths.is_empty() + && let Some(bytes) = resolve(&paths) + { + budget.take(Some(bytes.len()), "a resolved image")?; + image = Some(bytes); + break; + } + } + } + let props = Props::of(material); + let values = props.values("DiffuseColor"); + let factor = match values.as_slice() { + [r, g, b, ..] => { + let f = props.f("DiffuseFactor").unwrap_or(1.0); + [(*r * f) as f32, (*g * f) as f32, (*b * f) as f32, 1.0] + } + _ => [1.0; 4], + }; + if factor.iter().any(|v| !v.is_finite()) || (image.is_none() && values.len() < 3) { + continue; + } + let raw_name = short_name(material.first_str().unwrap_or("")); + let name = if raw_name.is_empty() { + match id { + Key::Id(i) => format!("Material_{i}"), + Key::Name(s) => format!("Material_{s}"), + } + } else { + raw_name.to_string() + }; + out.push(DiffuseMaterial { name, factor, image }); + } + out.sort_by(|a, b| a.name.cmp(&b.name)); + Ok(out) } } @@ -868,6 +1184,13 @@ enum Key<'a> { Name(&'a str), } +fn key_text(key: Key<'_>) -> String { + match key { + Key::Id(i) => i.to_string(), + Key::Name(s) => s.to_string(), + } +} + fn key(p: Option<&Prop>) -> Option> { match p? { Prop::Int(i) => Some(Key::Id(*i)), @@ -1011,14 +1334,18 @@ impl<'a> Build<'a, '_> { Ok(()) } - /// The mesh geometry of Model `id`, and what it is shared by. - fn geometry_of(&self, id: Key<'a>) -> Option<(Key<'a>, &'a Record)> { - // 6.x: the Model holds its own mesh. + /// The geometries connected to a Model, including its own FBX 6 mesh. + fn geometries_of(&self, id: Key<'a>) -> Vec<(Key<'a>, &'a Record)> { + let mut geometries = Vec::new(); if let Some(model) = self.by_key.get(&id).copied().filter(|o| o.child("Vertices").is_some()) { - return Some((id, model)); + geometries.push((id, model)); } - let (gid, _) = self.children_of(id, "Geometry").into_iter().next()?; - Some((gid, self.by_key.get(&gid).copied()?)) + for (gid, _) in self.children_of(id, "Geometry") { + if let Some(geometry) = self.by_key.get(&gid).copied().filter(|o| o.child("Vertices").is_some()) { + geometries.push((gid, geometry)); + } + } + geometries } /// The image of Texture `tid`. @@ -1086,7 +1413,18 @@ impl<'a> Build<'a, '_> { return Ok(Some(*m)); } let Some(record) = self.by_key.get(&mid).copied() else { return Ok(None) }; - let mut m = material(short_name(record.first_str().unwrap_or("")), record); + let name = short_name(record.first_str().unwrap_or("")); + let fallback; + let name = if name.is_empty() { + fallback = match mid { + Key::Id(i) => format!("Material_{i}"), + Key::Name(s) => format!("Material_{s}"), + }; + &fallback + } else { + name + }; + let mut m = material(name, record); for (tid, prop) in self.children_of(mid, "Texture") { let slot = match prop { Some("DiffuseColor") => &mut m.base_color_texture, @@ -1128,13 +1466,14 @@ impl<'a> Build<'a, '_> { return Err(malformed(KIND, "mesh array contains a non-numeric, non-finite or fractional index value")); } for (element, names) in [ - ("LayerElementNormal", &["Normals", "NormalsIndex"][..]), + ("LayerElementNormal", &["Normals", "NormalsIndex", "NormalIndex"][..]), ("LayerElementUV", &["UV", "UVIndex"][..]), ("LayerElementMaterial", &["Materials"][..]), - ("LayerElementColor", &["Colors", "ColorIndex"][..]), + ("LayerElementColor", &["Colors", "ColorIndex", "ColorsIndex"][..]), ] { if let Some(layer) = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)) - && names.iter().filter_map(|n| layer.child(n)).any(|r| !valid(r, false)) + // index arrays and material ids are integers; the data arrays are floats + && names.iter().filter_map(|n| layer.child(n).map(|r| (r, n.ends_with("Index") || *n == "Materials"))).any(|(r, integers)| !valid(r, integers)) { return Err(malformed(KIND, "mesh array contains a non-numeric or non-finite value")); } @@ -1143,13 +1482,30 @@ impl<'a> Build<'a, '_> { let pool = |what: &str, n: usize| { if n > limits.max_vertices { Err(too_large(KIND, format!("more than {} {what}", limits.max_vertices))) } else { Ok(()) } }; - let positions: Vec<[f64; 3]> = vertices.floats().as_chunks::<3>().0.iter().map(|c| c.map(finite)).collect(); + let mut positions: Vec<[f64; 3]> = vertices.floats().as_chunks::<3>().0.iter().map(|c| c.map(finite)).collect(); pool("vertices", positions.len())?; + let original_positions = positions.len(); let normals = Layer::of(geom, "LayerElementNormal", "Normals", &["NormalsIndex", "NormalIndex"], 3); let uvs = Layer::of(geom, "LayerElementUV", "UV", &["UVIndex"], 2); - let slots = geom.child("LayerElementMaterial").and_then(|e| { + let colors = Layer::of(geom, "LayerElementColor", "Colors", &["ColorIndex", "ColorsIndex"], 4); + if colors.is_some() && positions.len().checked_mul(16).is_none_or(|n| n > self.budget.left.get()) { + return Err(too_large(KIND, "vertex colors exceed the memory limit")); + } + let mut vertex_colors = colors.as_ref().map(|_| vec![[1.0; 4]; positions.len()]).unwrap_or_default(); + let mut color_indices = HashMap::<(u32, u32), u32>::new(); + let mut first_color = HashMap::::new(); + let mut colors_complete = colors.is_some(); + let slots = geom.children_named("LayerElementMaterial").min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)).and_then(|e| { let mapping = e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("AllSame"); - Some((mapping == "ByPolygon", e.child("Materials")?.ints())) + // a polygon goes to one part, so per-corner and per-vertex IDs use its first corner + let by = match mapping { + "ByPolygon" => Mapping::Polygon, + "ByPolygonVertex" => Mapping::PolygonVertex, + "ByVertex" | "ByVertice" | "ByControlPoint" => Mapping::Vertex, + "AllSame" => Mapping::AllSame, + _ => return None, + }; + Some((by, e.child("Materials")?.ints())) }); let (normal_layer, uv_layer) = (normals.as_ref().map(|l| &l.0), uvs.as_ref().map(|l| &l.0)); @@ -1159,15 +1515,58 @@ impl<'a> Build<'a, '_> { let (mut broken, mut poly) = (false, 0usize); let polygon_indices = pvi.ints(); let index_count = polygon_indices.len(); - for (pv, raw) in polygon_indices.into_iter().enumerate() { + let mut poly_start = 0usize; + for (pv, raw) in polygon_indices.iter().copied().enumerate() { // The last corner of a polygon is stored as `-index - 1`. let (v, last) = if raw < 0 { (!raw, true) } else { (raw, self.strict_geometry && pv + 1 == index_count) }; - match u32::try_from(v).ok().filter(|v| (*v as usize) < positions.len()) { - Some(p) => face.push(Corner { - position: p, - normal: Layer::at(normal_layer, pv, p as usize, poly), - uv: Layer::at(uv_layer, pv, p as usize, poly), - }), + match u32::try_from(v).ok().filter(|v| (*v as usize) < original_positions) { + Some(p) => { + let normal = Layer::at(normal_layer, pv, p as usize, poly); + let uv = Layer::at(uv_layer, pv, p as usize, poly); + let mut position = p; + if let Some((layer, values)) = &colors { + let color = Layer::at(Some(layer), pv, p as usize, poly); + let rgba = usize::try_from(color) + .ok() + .and_then(|i| i.checked_mul(4)) + .and_then(|i| i.checked_add(4).and_then(|end| values.get(i..end))); + if let Some(rgba) = rgba { + if let Some(existing) = color_indices.get(&(p, color)) { + position = *existing; + } else { + // Color seams need two maps as well as the output pools; do not + // grow them past the remaining file budget on hostile corner lists. + let staging = color_indices + .len() + .checked_add(1) + .and_then(|n| n.checked_mul(64)) + .and_then(|n| positions.len().checked_mul(40).and_then(|pool| n.checked_add(pool))); + if staging.is_none_or(|n| n > self.budget.left.get()) { + return Err(too_large(KIND, "color seams exceed the memory limit")); + } + if first_color.insert(p, color).is_some() { + pool("vertices", positions.len().saturating_add(1))?; + if positions.len().saturating_add(1).checked_mul(40).is_none_or(|n| n > self.budget.left.get()) { + return Err(too_large(KIND, "colored vertices exceed the memory limit")); + } + position = u32::try_from(positions.len()).map_err(|_| too_large(KIND, "too many vertices"))?; + let Some(original) = positions.get(p as usize).copied() else { continue }; + positions.push(original); + vertex_colors.push([1.0; 4]); + } + if let Some(dst) = vertex_colors.get_mut(position as usize) + && let [r, g, b, a] = rgba + { + *dst = [finite(*r) as f32, finite(*g) as f32, finite(*b) as f32, finite(*a) as f32]; + } + color_indices.insert((p, color), position); + } + } else { + colors_complete = false; + } + } + face.push(Corner { position, normal, uv }); + } None if self.strict_geometry => return Err(malformed(KIND, format!("polygon vertex index {raw} out of range"))), None => broken = true, } @@ -1178,6 +1577,7 @@ impl<'a> Build<'a, '_> { continue; } let (corners, p_index, was_broken) = (std::mem::take(&mut face), poly, std::mem::take(&mut broken)); + let first_pv = std::mem::replace(&mut poly_start, pv + 1); poly += 1; if was_broken || corners.len() < 3 { self.skipped += 1; @@ -1187,13 +1587,29 @@ impl<'a> Build<'a, '_> { if self.triangles > limits.max_triangles { return Err(too_large(KIND, format!("more than {} triangles", limits.max_triangles))); } - let slot = slots.as_ref().and_then(|(by_poly, ids)| { - let k = if *by_poly { *ids.get(p_index)? } else { *ids.first()? }; - materials.get(usize::try_from(k).ok()?).copied() - }); - let slot = slot.or_else(|| materials.first().copied()); - let at = match part_of.get(&slot) { - Some(at) => *at, + let slot = match &slots { + Some((by, ids)) => { + let k = match by { + Mapping::Polygon => ids.get(p_index), + Mapping::PolygonVertex => ids.get(first_pv), + Mapping::Vertex => polygon_indices + .get(first_pv) + .map(|&r| if r < 0 { !r } else { r }) + .and_then(|v| usize::try_from(v).ok()) + .and_then(|v| ids.get(v)), + Mapping::AllSame => ids.first(), + }; + k.and_then(|k| usize::try_from(*k).ok()).and_then(|k| materials.get(k)).copied() + } + None => materials.first().copied(), + }; + let existing = if self.strict_geometry { + parts.last().filter(|part| part.material == slot).map(|_| parts.len() - 1) + } else { + part_of.get(&slot).copied() + }; + let at = match existing { + Some(at) => at, None => { if parts.len() >= limits.max_parts { return Err(too_large(KIND, format!("more than {} material slots in a mesh", limits.max_parts))); @@ -1221,6 +1637,20 @@ impl<'a> Build<'a, '_> { let uvs: Option> = uvs.map(|(_, d)| d.as_chunks::<2>().0.iter().map(|c| [finite(c[0]) as f32, 1.0 - finite(c[1]) as f32]).collect()); pool("UVs", uvs.as_ref().map_or(0, Vec::len))?; - Ok(Some(Mesh { name: name.to_string(), positions, normals, uv_sets: uvs.into_iter().collect(), parts, ..Mesh::default() })) + if !colors_complete { + vertex_colors.clear(); + if colors.is_some() { + self.scene.warnings.push("FBX: incomplete vertex color layer was omitted".into()); + } + } + Ok(Some(Mesh { + name: name.to_string(), + positions, + normals, + uv_sets: uvs.into_iter().collect(), + colors: vertex_colors, + parts, + ..Mesh::default() + })) } } diff --git a/crates/mesh/src/fbx/package.rs b/crates/mesh/src/fbx/package.rs new file mode 100644 index 000000000..10f7037c2 --- /dev/null +++ b/crates/mesh/src/fbx/package.rs @@ -0,0 +1,301 @@ +//! FBX and referenced texture files in a ZIP container. +use super::{Document, KIND}; +use crate::{Error, Limits, Result, malformed, too_large}; + +const ZIP_MAGIC: &[u8] = b"PK\x03\x04"; + +/// Whether bytes begin with a ZIP local-file header. +pub fn is_package(bytes: &[u8]) -> bool { + bytes.starts_with(ZIP_MAGIC) +} + +/// Limits for an FBX package and each referenced texture. +#[derive(Clone, Copy, Debug)] +pub struct PackageLimits { + pub max_entries: u64, + pub max_model_bytes: u64, + pub max_texture_bytes: u64, + pub max_total_bytes: u64, +} + +/// An open FBX package. Reads are charged to its cumulative decompression budget. +pub struct Package<'a> { + zip: craft_zip::Archive<'a>, + limits: PackageLimits, + name: String, + /// Textures read so far by name: a texture several materials share is read, and charged, once. + textures: std::collections::HashMap>, +} + +impl<'a> Package<'a> { + pub fn open(bytes: &'a [u8], limits: PackageLimits) -> Result { + let zip = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries, max_total_bytes: limits.max_total_bytes }) + .map_err(|e| malformed(KIND, format!("package: {e}")))?; + let name = zip + .names() + .find(|n| n.rsplit('.').next().is_some_and(|ext| ext.eq_ignore_ascii_case("fbx"))) + .map(|n| n.into_owned()) + .ok_or_else(|| malformed(KIND, "the package contains no FBX file"))?; + Ok(Self { zip, limits, name, textures: std::collections::HashMap::new() }) + } + + pub fn model(&mut self) -> Result> { + let bytes = self + .zip + .read(&self.name, self.limits.max_model_bytes) + .map_err(|e| malformed(KIND, format!("package: {e}")))? + .ok_or_else(|| malformed(KIND, "package FBX is missing"))?; + if is_package(&bytes) { + return Err(malformed(KIND, "the package holds another package")); + } + Ok(bytes) + } + + /// Resolve candidate names within the package; outside paths are never read. + pub fn texture(&mut self, paths: &[&str]) -> Option> { + for name in paths.iter().filter_map(|p| below(p)) { + if let Some(data) = self.textures.get(&name) { + return Some(data.clone()); + } + if let Some(data) = self.zip.read(&name, self.limits.max_texture_bytes).ok().flatten() { + self.textures.insert(name, data.clone()); + return Some(data); + } + } + None + } +} + +/// Resolve a safe relative path inside a package. +fn below(path: &str) -> Option { + let path = path.replace('\\', "/"); + if path.starts_with('/') || path.contains(':') { + return None; + } + let mut parts = Vec::new(); + for part in path.split('/') { + match part { + "" | "." => {} + ".." => { + parts.pop()?; + } + part => parts.push(part), + } + } + (!parts.is_empty()).then(|| parts.join("/")) +} + +/// Package the FBX and its externally resolved diffuse textures, if any. Returns unresolved paths. +pub fn write( + doc: &Document, + file_name: &str, + bytes: &[u8], + limits: &Limits, + package_limits: PackageLimits, + found: &mut dyn FnMut(&str) -> Option>, +) -> Result<(Option>, Vec)> { + if is_package(bytes) { + return Ok((None, Vec::new())); + } + let mut files: Vec<(String, Vec)> = Vec::new(); + let mut by_name = std::collections::HashMap::::new(); + let mut too_many = false; + let mut missing = Vec::new(); + let mut missing_seen = std::collections::HashSet::::new(); + let mut total = bytes.len() as u64; + if total > package_limits.max_total_bytes || total > package_limits.max_model_bytes { + return Err(too_large(KIND, "the FBX and its textures exceed the package limit")); + } + let mut exceeded = false; + doc.diffuse_materials(limits, &mut |paths| { + // candidates in order of preference: an earlier one that exists wins over a later cached one + for path in paths { + let Some(name) = below(path) else { continue }; + if let Some(data) = by_name.get(&name).and_then(|&i| files.get(i)).map(|(_, d)| d.clone()) { + return Some(data); + } + // once a resolved texture has not fit, the package fails: stop calling the resolver + if too_many || exceeded { + return None; + } + if let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) { + // the model is an entry too; only a texture that resolves needs a new entry + if files.len() as u64 + 1 >= package_limits.max_entries { + too_many = true; + return None; + } + total = total.saturating_add(data.len() as u64); + if total > package_limits.max_total_bytes { + exceeded = true; + return None; + } + by_name.insert(name.clone(), files.len()); + files.push((name, data.clone())); + return Some(data); + } + } + if let Some(first) = paths.first().filter(|p| missing_seen.insert((**p).to_string())) { + missing.push((*first).to_string()); + } + None + })?; + if exceeded { + return Err(too_large(KIND, "the FBX and its textures exceed the package limit")); + } + if too_many { + return Err(too_large(KIND, "too many package entries")); + } + if files.is_empty() { + return Ok((None, missing)); + } + let leaf = file_name.rsplit(['/', '\\']).next().filter(|n| !n.is_empty()).unwrap_or("mesh.fbx"); + // the package is found by its `.fbx` entry, so the model always carries that extension + let leaf = if leaf.rsplit_once('.').is_some_and(|(_, ext)| ext.eq_ignore_ascii_case("fbx")) { leaf.to_string() } else { format!("{leaf}.fbx") }; + let leaf = leaf.as_str(); + let mut writer = craft_zip::Writer::new(); + for (name, data) in std::iter::once((leaf, bytes)).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))) { + writer.add(name, data, craft_zip::Compression::Stored).map_err(|e| Error::Malformed(format!("FBX package: {e}")))?; + } + Ok((Some(writer.finish().map_err(|e| Error::Malformed(format!("FBX package: {e}")))?), missing)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::fbx::read_document; + + const MODEL: &[u8] = b"FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Material: 1, \"Material::M\", \"\" { } Texture: 2, \"Texture::T\", \"\" { RelativeFilename: \"tex/a.png\" } } Connections: { C: \"OP\",2,1,\"DiffuseColor\" }"; + fn bounds() -> PackageLimits { + PackageLimits { max_entries: 4, max_model_bytes: 4096, max_texture_bytes: 32, max_total_bytes: 8192 } + } + #[test] + fn package_roundtrip_and_limits() { + let limits = Limits::default(); + let doc = read_document(MODEL, &limits).unwrap(); + let (bytes, missing) = write(&doc, "asset.fbx", MODEL, &limits, bounds(), &mut |p| (p == "tex/a.png").then(|| b"image".to_vec())).unwrap(); + assert!(missing.is_empty()); + let bytes = bytes.unwrap(); + let mut package = Package::open(&bytes, bounds()).unwrap(); + assert_eq!(package.model().unwrap(), MODEL); + assert_eq!(package.texture(&["../tex/a.png", "tex/a.png"]), Some(b"image".to_vec())); + assert!(package.texture(&["../tex/a.png"]).is_none()); + let small = PackageLimits { max_total_bytes: MODEL.len() as u64 + 4, ..bounds() }; + assert!(matches!(write(&doc, "a.fbx", MODEL, &limits, small, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_)))); + assert!(Package::open(&bytes, PackageLimits { max_entries: 1, ..bounds() }).is_err()); + } + + #[test] + fn an_earlier_candidate_wins_over_a_cached_later_one() { + let model = b"FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Material: 1, \"Material::A\", \"\" { } Texture: 2, \"Texture::A\", \"\" { RelativeFilename: \"base.png\" } Material: 3, \"Material::B\", \"\" { } Texture: 4, \"Texture::B\", \"\" { RelativeFilename: \"maps/base.png\" FileName: \"C:/art/base.png\" } } Connections: { C: \"OP\",2,1,\"DiffuseColor\" C: \"OP\",4,3,\"DiffuseColor\" }"; + let limits = Limits::default(); + let doc = read_document(model, &limits).unwrap(); + let mut found = |p: &str| match p { + "base.png" => Some(b"flat".to_vec()), + "maps/base.png" => Some(b"maps".to_vec()), + _ => None, + }; + let (bytes, missing) = write(&doc, "asset.fbx", model, &limits, bounds(), &mut found).unwrap(); + assert!(missing.is_empty()); + let bytes = bytes.unwrap(); + let mut package = Package::open(&bytes, bounds()).unwrap(); + assert_eq!(package.texture(&["maps/base.png"]), Some(b"maps".to_vec())); + assert_eq!(package.texture(&["base.png"]), Some(b"flat".to_vec())); + } + + #[test] + fn the_model_entry_always_has_an_fbx_extension() { + let limits = Limits::default(); + let doc = read_document(MODEL, &limits).unwrap(); + let (bytes, _) = write(&doc, "dir/asset", MODEL, &limits, bounds(), &mut |_| Some(b"image".to_vec())).unwrap(); + let bytes = bytes.unwrap(); + let mut package = Package::open(&bytes, bounds()).unwrap(); + assert_eq!(package.model().unwrap(), MODEL); + } + + #[test] + fn texture_resolution_stops_at_the_entry_limit() { + let mut model = String::from("FBXHeaderExtension: { FBXVersion: 7400 } Objects: {"); + let mut links = String::new(); + for i in 0..20 { + model.push_str(&format!( + " Material: {m}, \"Material::M{i}\", \"\" {{ }} Texture: {t}, \"Texture::T{i}\", \"\" {{ RelativeFilename: \"t{i}.png\" }}", + m = 2 * i + 1, + t = 2 * i + 2 + )); + links.push_str(&format!(" C: \"OP\",{t},{m},\"DiffuseColor\"", m = 2 * i + 1, t = 2 * i + 2)); + } + model.push_str(&format!(" }} Connections: {{{links} }}")); + let limits = Limits::default(); + let doc = read_document(model.as_bytes(), &limits).unwrap(); + let mut calls = 0; + let result = write(&doc, "a.fbx", model.as_bytes(), &limits, bounds(), &mut |_| { + calls += 1; + Some(b"image".to_vec()) + }); + assert!(matches!(result, Err(Error::TooLarge(_)))); + assert!(calls <= bounds().max_entries as usize, "{calls} resolver calls past the entry limit"); + } + + #[test] + fn resolution_stops_once_the_byte_limit_is_passed() { + let mut model = String::from("FBXHeaderExtension: { FBXVersion: 7400 } Objects: {"); + let mut links = String::new(); + for i in 0..10 { + model.push_str(&format!( + " Material: {m}, \"Material::M{i}\", \"\" {{ }} Texture: {t}, \"Texture::T{i}\", \"\" {{ RelativeFilename: \"big.png\" }}", + m = 2 * i + 1, + t = 2 * i + 2 + )); + links.push_str(&format!(" C: \"OP\",{t},{m},\"DiffuseColor\"", m = 2 * i + 1, t = 2 * i + 2)); + } + model.push_str(&format!(" }} Connections: {{{links} }}")); + let limits = Limits::default(); + let doc = read_document(model.as_bytes(), &limits).unwrap(); + let tight = PackageLimits { max_total_bytes: model.len() as u64 + 4, max_model_bytes: 1 << 20, ..bounds() }; + let mut calls = 0; + let result = write(&doc, "a.fbx", model.as_bytes(), &limits, tight, &mut |_| { + calls += 1; + Some(b"image".to_vec()) + }); + assert!(matches!(result, Err(Error::TooLarge(_)))); + assert_eq!(calls, 1, "one texture past the limit ends resolution"); + // a model past its own limit is refused before any texture is resolved + let small = PackageLimits { max_model_bytes: 8, ..bounds() }; + let mut calls = 0; + assert!(matches!( + write(&doc, "a.fbx", model.as_bytes(), &limits, small, &mut |_| { + calls += 1; + None + }), + Err(Error::TooLarge(_)) + )); + assert_eq!(calls, 0); + } + + #[test] + fn the_entry_limit_counts_only_textures_that_resolve() { + let limits = Limits::default(); + let doc = read_document(MODEL, &limits).unwrap(); + // the model alone fills a one-entry package, so a resolved texture cannot fit + let one = PackageLimits { max_entries: 1, ..bounds() }; + assert!(matches!(write(&doc, "a.fbx", MODEL, &limits, one, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_)))); + // an unresolved texture adds no entry, so it is reported missing rather than overflowing + let (bytes, missing) = write(&doc, "a.fbx", MODEL, &limits, one, &mut |_| None).unwrap(); + assert!(bytes.is_none()); + assert_eq!(missing, vec!["tex/a.png".to_string()]); + } + + #[test] + fn a_shared_texture_is_read_and_charged_once() { + let limits = Limits::default(); + let doc = read_document(MODEL, &limits).unwrap(); + let (bytes, _) = write(&doc, "asset.fbx", MODEL, &limits, bounds(), &mut |_| Some(b"image".to_vec())).unwrap(); + let bytes = bytes.unwrap(); + // room for the model and one read of the texture + let tight = PackageLimits { max_total_bytes: MODEL.len() as u64 + 5, ..bounds() }; + let mut package = Package::open(&bytes, tight).unwrap(); + package.model().unwrap(); + assert_eq!(package.texture(&["tex/a.png"]), Some(b"image".to_vec())); + assert_eq!(package.texture(&["tex/a.png"]), Some(b"image".to_vec()), "served from the cache"); + } +} diff --git a/crates/mesh/src/tests/fbx.rs b/crates/mesh/src/tests/fbx.rs index 981e81f28..07c823258 100644 --- a/crates/mesh/src/tests/fbx.rs +++ b/crates/mesh/src/tests/fbx.rs @@ -682,3 +682,232 @@ fn object_only_hierarchy_ignores_property_connections() { assert_eq!(scene.roots.len(), 2); assert_eq!(doc.scene(&Limits::default(), &mut no_external).unwrap().roots.len(), 1); } + +#[test] +fn binary_scalar_properties_are_bounded_before_reserving() { + let mut bytes = BINARY_MAGIC.to_vec(); + bytes.extend(7400u32.to_le_bytes()); + let count = 1000u32; + let end = bytes.len() + 13 + 1 + count as usize * 2; + bytes.extend((end as u32).to_le_bytes()); + bytes.extend(count.to_le_bytes()); + bytes.extend((count * 2).to_le_bytes()); + bytes.push(1); + bytes.push(b'A'); + for _ in 0..count { + bytes.extend([b'C', 1]); + } + let limits = Limits { max_data_bytes: 1024, ..Limits::default() }; + assert!(matches!(read_document(&bytes, &limits), Err(Error::TooLarge(_)))); + assert!(read_document(&bytes, &Limits::default()).is_ok()); +} + +#[test] +fn fbx_invalid_material_slot_is_unassigned_and_unnamed_material_has_id() { + let text = ASCII.replace("a: 0,1\n", "a: 0,99\n").replace("\"Material::Red\"", "\"\""); + let scene = parse(text.as_bytes()).unwrap(); + assert!(scene.materials.iter().any(|m| m.name == "Material_400")); + let parts = &scene.meshes[0].parts; + assert!(parts.iter().any(|p| p.material.is_none())); + assert!(parts.iter().any(|p| p.material.is_some())); +} + +#[test] +fn cameras_keep_lens_and_model_hierarchy() { + let text = r#"FBXHeaderExtension: { FBXVersion: 7400 } +Objects: { + Model: 1, "Model::Rig", "Null" { Properties70: { P: "Lcl Translation", "", "", "", 1,2,3 } } + Model: 2, "Model::View", "Camera" { } + NodeAttribute: 3, "NodeAttribute::Lens", "Camera" { Properties70: { + P: "FocalLength", "", "", "", 50 + P: "FilmHeight", "", "", "", 1 + } } + Model: 4, "Model::Ortho", "Camera" { Properties70: { + P: "CameraProjectionType", "", "", "", 1 + P: "OrthoZoom", "", "", "", 3 + } } + Model: 5, "Model::Producer Perspective", "Camera" { } +} +Connections: { + C: "OO",2,1 + C: "OO",3,2 +} +"#; + let scene = parse(text.as_bytes()).unwrap(); + assert_eq!(scene.cameras.len(), 2); + let crate::Projection::Perspective { yfov, .. } = scene.cameras[0].projection else { panic!("perspective") }; + assert!((yfov - 2.0 * (25.4_f64 / 100.0).atan()).abs() < 1e-12); + assert!(matches!(scene.cameras[1].projection, crate::Projection::Orthographic { ymag: 3.0, .. })); + let camera_node = scene.nodes.iter().find(|n| n.camera == Some(0)).unwrap(); + let forward = crate::transform_point(&camera_node.transform, [0.0, 0.0, -1.0]); + assert!((forward[0] - 1.0).abs() < 1e-12); + let rig = scene.nodes.iter().find(|n| n.name == "Rig").unwrap(); + assert!(rig.children.iter().any(|i| scene.nodes[*i].name == "View")); + let limits = Limits { max_extras: 1, ..Limits::default() }; + assert!(matches!(read(text.as_bytes(), &limits, &mut no_external), Err(Error::TooLarge(_)))); +} + +#[test] +fn all_connected_and_unplaced_geometries_are_instances() { + let geometry = |id| format!("Geometry: {id}, \"Geometry::Part{id}\", \"Mesh\" {{ Vertices: 0,0,0,1,0,0,0,1,0 PolygonVertexIndex: 0,1,-3 }}\n"); + let text = format!( + "FBXHeaderExtension: {{ FBXVersion: 7400 }}\nObjects: {{\n{}{}{} Model: 10, \"Model::M\", \"Mesh\" {{}} }}\nConnections: {{ C: \"OO\",1,10 C: \"OO\",2,10 C: \"OO\",2,10 }}", + geometry(1), + geometry(2), + geometry(3) + ); + let scene = parse(text.as_bytes()).unwrap(); + assert_eq!(scene.meshes.len(), 3); + assert_eq!(scene.instances(&Limits::default()).unwrap().len(), 3); + assert_eq!(scene.roots.len(), 2); +} + +#[test] +fn fbx_corner_colors_keep_seams_and_incomplete_layers_are_omitted() { + let layer = r#"LayerElementColor: 0 { + MappingInformationType: "ByPolygonVertex" + ReferenceInformationType: "Direct" + Colors: 1,0,0,1, 0,1,0,1, 0,0,1,1, 1,1,0,1, 0,1,0,1, 0,0,1,1 +}"#; + let text = ASCII.replace("LayerElementNormal: 0 {", &format!("{layer}\n LayerElementNormal: 0 {{")); + // Add a color for every corner of the original quad and triangle. + let text = text.replace("0,0,1,1\n}", "0,0,1,1, 1,0,1,1\n}"); + let scene = parse(text.as_bytes()).unwrap(); + let mesh = &scene.meshes[0]; + let colors: Vec<_> = mesh.parts.iter().flat_map(|part| part.corners.iter()).map(|corner| mesh.colors[corner.position as usize]).collect(); + assert_eq!(colors.len(), 7); + assert!(colors.contains(&[1.0, 0.0, 0.0, 1.0])); + assert!(colors.contains(&[0.0, 1.0, 0.0, 1.0])); + assert!(colors.contains(&[1.0, 0.0, 1.0, 1.0])); + let incomplete = text.replace(", 1,0,1,1\n}", "\n}"); + assert!(parse(incomplete.as_bytes()).unwrap().meshes[0].colors.is_empty()); +} + +#[test] +fn ascii_budget_applies_while_reading_values_and_records() { + let limits = Limits { max_data_bytes: 512, ..Limits::default() }; + let many_values = format!("Objects: {{\n A: {}\n}}", "1,".repeat(1000)); + assert!(matches!(read_document(many_values.as_bytes(), &limits), Err(Error::TooLarge(_)))); + let many_records = format!("Objects: {{\n{}\n}}", "A: {}\n".repeat(1000)); + assert!(matches!(read_document(many_records.as_bytes(), &limits), Err(Error::TooLarge(_)))); + let giant_string = format!("Objects: {{ A: \"{}\" }}", "x".repeat(1024)); + assert!(matches!(read_document(giant_string.as_bytes(), &limits), Err(Error::TooLarge(_)))); + // Empty punctuation and comments do not consume memory in proportion to their length. + let ignored = format!(";{}\nObjects: {{}}", ",".repeat(10_000)); + assert!(read_document(ignored.as_bytes(), &limits).is_ok()); +} + +#[test] +fn strict_ascii_reports_unbalanced_and_unterminated_records() { + let strict = |s: &str| crate::fbx::read_document_strict(s.as_bytes(), &Limits::default()); + assert!(strict(ASCII).is_ok()); + for s in ["Objects: { A: 1", "Objects: { } }", "Objects: { A: \"unterminated", "Objects: { A: 1 2 }"] { + assert!(strict(s).is_err(), "{s}"); + } + assert!(read_document(b"Objects: { A: 1", &Limits::default()).is_ok()); +} + +#[test] +fn strict_binary_requires_a_closing_record() { + let doc = document(ASCII.as_bytes()); + let bytes = write_binary(&doc.records, 7400, false); + assert!(crate::fbx::read_document_strict(&bytes, &Limits::default()).is_ok()); + let truncated = &bytes[..bytes.len() - 29]; + assert!(read_document(truncated, &Limits::default()).is_ok()); + assert!(crate::fbx::read_document_strict(truncated, &Limits::default()).is_err()); +} + +#[test] +fn diffuse_materials_preserve_factors_and_texture_candidates() { + let text = with( + "Texture: 600, \"Texture::Base\", \"\" { RelativeFilename: \"maps/base.png\" FileName: \"C:/a/base.png\" }\n", + "C: \"OP\",600,400,\"Maya|baseColor\"\n", + ); + let doc = document(text.as_bytes()); + let mut asked = Vec::new(); + let materials = doc + .diffuse_materials(&Limits::default(), &mut |paths| { + asked.push(paths.iter().map(|s| s.to_string()).collect::>()); + Some(b"image".to_vec()) + }) + .unwrap(); + assert_eq!(asked, vec![vec!["maps/base.png", "base.png"]]); + let red = materials.iter().find(|m| m.name == "Red").unwrap(); + assert_eq!(red.factor, [1.0, 0.0, 0.0, 1.0]); + assert_eq!(red.image.as_deref(), Some(&b"image"[..])); + let small = Limits { max_data_bytes: 4, ..Limits::default() }; + assert!(matches!(doc.diffuse_materials(&small, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_)))); + let embedded_text = with( + &format!("Texture: 600, \"Texture::Base\", \"\" {{}}\nVideo: 700, \"Video::Base\", \"Clip\" {{ Content: \"{}\" }}\n", b64(b"embedded")), + "C: \"OP\",600,400,\"DiffuseColor\"\nC: \"OO\",700,600\n", + ); + let embedded = document(embedded_text.as_bytes()).diffuse_materials(&Limits::default(), &mut |_| None).unwrap(); + assert_eq!(embedded.iter().find(|m| m.name == "Red").unwrap().image.as_deref(), Some(&b"embedded"[..])); +} + +#[test] +fn per_corner_and_per_vertex_material_ids_use_each_polygons_first_corner() { + let by_corner = ASCII.replace("MappingInformationType: \"ByPolygon\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *2 {\n\t\t\t\ta: 0,1", "MappingInformationType: \"ByPolygonVertex\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *7 {\n\t\t\t\ta: 0,0,0,0,1,1,1"); + assert_ne!(by_corner, ASCII); + let parts = &parse(by_corner.as_bytes()).unwrap().meshes[0].parts; + assert_eq!(parts.len(), 2, "the triangle takes material 1 from its first corner"); + // The triangle starts at control point 4, whose material is 1. + let by_vertex = ASCII.replace("a: 0,1,2,-4,0,1,-5", "a: 0,1,2,-4,4,0,-2").replace( + "MappingInformationType: \"ByPolygon\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *2 {\n\t\t\t\ta: 0,1", + "MappingInformationType: \"ByControlPoint\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *5 {\n\t\t\t\ta: 0,0,0,0,1", + ); + let scene = parse(by_vertex.as_bytes()).unwrap(); + assert_eq!(scene.meshes[0].parts.len(), 2); + assert!(scene.meshes[0].parts.iter().all(|p| p.material.is_some())); +} + +#[test] +fn strict_colors_validate_and_read_the_same_layer() { + let good = "LayerElementColor: 0 { MappingInformationType: \"AllSame\" ReferenceInformationType: \"Direct\" Colors: 0,1,0,1 }\n"; + let bad = "LayerElementColor: 1 { MappingInformationType: \"AllSame\" ReferenceInformationType: \"Direct\" Colors: \"bad\",1,0,0,1 }\n"; + let text = ASCII.replace("LayerElementNormal: 0 {", &format!("{bad} {good} LayerElementNormal: 0 {{")); + let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() }; + let scene = document(text.as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).unwrap(); + assert!(scene.meshes[0].colors.iter().all(|c| *c == [0.0, 1.0, 0.0, 1.0])); + assert!(!scene.meshes[0].colors.is_empty()); + let only_bad = ASCII.replace("LayerElementNormal: 0 {", &format!("{} LayerElementNormal: 0 {{", bad.replace(": 1 {", ": 0 {"))); + assert!(document(only_bad.as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err()); +} + +#[test] +fn strict_geometry_validates_the_colors_index_alias() { + let layer = |index: &str| { + format!( + "LayerElementColor: 0 {{ MappingInformationType: \"AllSame\" ReferenceInformationType: \"IndexToDirect\" Colors: 0,1,0,1 ColorsIndex: {index} }}\n" + ) + }; + let with = |index: &str| ASCII.replace("LayerElementNormal: 0 {", &format!("{} LayerElementNormal: 0 {{", layer(index))); + let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() }; + assert!(document(with("0").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_ok()); + assert!(document(with("\"bad\",0").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err()); + assert!(document(with("0.5").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err(), "a fractional index"); +} + +#[test] +fn placement_and_polygon_order_follow_the_fbx() { + let text = ASCII.replace("a: 0,1,2,-4,0,1,-5", "a: 0,1,2,-4,0,1,-5,0,1,-5").replace("a: 0,1\n", "a: 0,1,0\n"); + let doc = document(text.as_bytes()); + let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() }; + let (scene, placements) = doc.scene_with_placements(&Limits::default(), &mut no_external, &options).unwrap(); + assert_eq!(placements.len(), 1); + let mesh = scene.nodes[placements[0].node].mesh.unwrap(); + let parts = &scene.meshes[mesh].parts; + assert_eq!(parts.len(), 3); + assert_eq!(parts[0].material, parts[2].material); + assert_ne!(parts[0].material, parts[1].material); +} + +#[test] +fn placement_order_follows_connections_not_model_declarations() { + let text = with("Model: 201, \"Model::Later\", \"Mesh\" {}\n", "").replace("C: \"OO\",100,300", "C: \"OO\",100,201\n C: \"OO\",100,300"); + let doc = document(text.as_bytes()); + let (scene, placements) = doc.scene_with_placements(&Limits::default(), &mut no_external, &Default::default()).unwrap(); + let names: Vec<_> = placements.iter().map(|i| scene.nodes[i.node].name.as_str()).collect(); + assert_eq!(names, ["Later", "Shape geometry"]); + assert_eq!(scene.nodes[placements[1].model.unwrap()].name, "Shape"); +} diff --git a/crates/metadata/src/compat_orientation.rs b/crates/metadata/src/compat_orientation.rs new file mode 100644 index 000000000..9a4b90a99 --- /dev/null +++ b/crates/metadata/src/compat_orientation.rs @@ -0,0 +1,237 @@ +//! Compatibility EXIF summary and helpers for legacy clients that accepted loosely formed classic TIFF. +//! The ordinary strict EXIF orientation API remains unchanged. + +/// Read a legacy orientation, with an optional `Exif\0\0` prefix. +pub fn orientation(bytes: &[u8]) -> u16 { + summarize(bytes.strip_prefix(b"Exif\0\0").unwrap_or(bytes)).orientation.unwrap_or(1) +} + +/// Byte-order aware reader over a TIFF-structured blob. +#[derive(Clone, Copy)] +struct Tiff<'a> { + pub data: &'a [u8], + le: bool, +} + +#[derive(Clone, Copy, Debug)] +struct Entry { + pub tag: u16, + pub typ: u16, + pub count: u32, + /// Offset of the 4-byte value/offset field within `data`. + pub value_pos: usize, +} + +impl<'a> Tiff<'a> { + pub fn new(data: &'a [u8]) -> Option> { + let le = match data.get(0..2)? { + b"II" => true, + b"MM" => false, + _ => return None, + }; + Some(Tiff { data, le }) + } + pub fn u16_at(&self, pos: usize) -> Option { + let b = self.data.get(pos..pos.checked_add(2)?)?; + Some(if self.le { u16::from_le_bytes([b[0], b[1]]) } else { u16::from_be_bytes([b[0], b[1]]) }) + } + pub fn u32_at(&self, pos: usize) -> Option { + let b = self.data.get(pos..pos.checked_add(4)?)?; + let a = [b[0], b[1], b[2], b[3]]; + Some(if self.le { u32::from_le_bytes(a) } else { u32::from_be_bytes(a) }) + } + pub fn first_ifd(&self) -> Option { + let magic = self.u16_at(2)?; + if magic != 42 { + return None; + } + Some(self.u32_at(4)? as usize) + } + /// Entries of the IFD at `pos` and the offset of the next IFD (0 = none). + pub fn ifd(&self, pos: usize) -> Option<(Vec, usize)> { + let n = self.u16_at(pos)? as usize; + if n > 4096 { + return None; + } + let mut v = Vec::with_capacity(n); + for i in 0..n { + let e = pos.checked_add(2)?.checked_add(i.checked_mul(12)?)?; + v.push(Entry { tag: self.u16_at(e)?, typ: self.u16_at(e + 2)?, count: self.u32_at(e + 4)?, value_pos: e + 8 }); + } + let next = + pos.checked_add(2).and_then(|p| n.checked_mul(12).and_then(|v| p.checked_add(v))).and_then(|p| self.u32_at(p)).unwrap_or(0) as usize; + Some((v, next)) + } + /// First value of a SHORT/LONG entry. + pub fn uint(&self, e: &Entry) -> Option { + match e.typ { + 3 => self.u16_at(e.value_pos).map(u32::from), + 4 | 13 => self.u32_at(e.value_pos), + 1 | 7 => self.data.get(e.value_pos).map(|b| *b as u32), + _ => None, + } + } + /// Raw bytes of an entry's value (for ASCII/UNDEFINED/BYTE types). + pub fn bytes(&self, e: &Entry) -> Option<&'a [u8]> { + let size = match e.typ { + 1 | 2 | 6 | 7 => 1usize, + 3 | 8 => 2, + 4 | 9 | 11 | 13 => 4, + 5 | 10 | 12 => 8, + _ => return None, + }; + let len = size.checked_mul(e.count as usize)?; + let start = if len <= 4 { e.value_pos } else { self.u32_at(e.value_pos)? as usize }; + self.data.get(start..start.checked_add(len)?) + } +} + +/// What the decoder cares about from an EXIF (TIFF-structured) blob. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct ExifSummary { + /// Valid orientation (1–8), if present. + pub orientation: Option, + /// (offset, length) of the IFD1 JPEG thumbnail within the blob. + pub thumbnail: Option<(usize, usize)>, + /// EXIF ColorSpace = 0xFFFF with InteropIndex "R03" (Adobe RGB (1998) "option" files). + pub adobe_rgb_hint: bool, +} + +pub fn summarize(blob: &[u8]) -> ExifSummary { + let mut s = ExifSummary::default(); + let Some(t) = Tiff::new(blob) else { return s }; + let Some(ifd0) = t.first_ifd() else { return s }; + let Some((entries, next)) = t.ifd(ifd0) else { return s }; + let mut exif_ifd = None; + for e in &entries { + match e.tag { + 0x0112 => s.orientation = t.uint(e).map(|v| v as u16).filter(|v| (1..=8).contains(v)), + 0x8769 => exif_ifd = t.uint(e).map(|v| v as usize), + _ => {} + } + } + if next != 0 + && next != ifd0 + && let Some((e1, _)) = t.ifd(next) + { + let off = e1.iter().find(|e| e.tag == 0x0201).and_then(|e| t.uint(e)); + let len = e1.iter().find(|e| e.tag == 0x0202).and_then(|e| t.uint(e)); + if let (Some(o), Some(l)) = (off, len) { + let (o, l) = (o as usize, l as usize); + if l > 4 && o.checked_add(l).is_some_and(|end| end <= blob.len()) && blob[o..o + 2] == [0xFF, 0xD8] { + s.thumbnail = Some((o, l)); + } + } + } + if let Some(p) = exif_ifd + && let Some((ee, _)) = t.ifd(p) + { + let cs = ee.iter().find(|e| e.tag == 0xA001).and_then(|e| t.uint(e)); + let interop = ee.iter().find(|e| e.tag == 0xA005).and_then(|e| t.uint(e)); + if cs == Some(0xFFFF) + && let Some((ie, _)) = interop.and_then(|p| t.ifd(p as usize)) + { + s.adobe_rgb_hint = ie.iter().any(|e| e.tag == 1 && t.bytes(e).is_some_and(|b| b.starts_with(b"R03"))); + } + } + s +} + +/// Build a tiny EXIF blob (little-endian TIFF) with just an orientation tag. Used by tests and by +/// callers that need to stamp orientation on export when no source EXIF exists. +pub fn minimal_exif(orientation: u16) -> Vec { + let mut v = Vec::with_capacity(26); + v.extend_from_slice(b"II"); + v.extend_from_slice(&42u16.to_le_bytes()); + v.extend_from_slice(&8u32.to_le_bytes()); + v.extend_from_slice(&1u16.to_le_bytes()); + v.extend_from_slice(&0x0112u16.to_le_bytes()); + v.extend_from_slice(&3u16.to_le_bytes()); + v.extend_from_slice(&1u32.to_le_bytes()); + v.extend_from_slice(&orientation.to_le_bytes()); + v.extend_from_slice(&[0, 0]); + v.extend_from_slice(&0u32.to_le_bytes()); + v +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn accepts_legacy_count_zero_but_bounds_the_table() { + let mut bytes = minimal_exif(6); + bytes[14..18].fill(0); + assert_eq!(orientation(&bytes), 6); + bytes.truncate(19); + assert_eq!(orientation(&bytes), 1); + } + + #[test] + fn orientation_roundtrip() { + for o in 1..=8 { + assert_eq!(summarize(&minimal_exif(o)).orientation, Some(o)); + } + } + + #[test] + fn garbage_ok() { + assert_eq!(summarize(b"II*\0\xff\xff\xff\xff"), ExifSummary::default()); + assert_eq!(summarize(&[]), ExifSummary::default()); + } + + /// Big-endian IFD: `entries` are (tag, type, count, value-or-offset), value left-justified. + fn be_ifd(out: &mut Vec, entries: &[(u16, u16, u32, [u8; 4])], next: u32) { + out.extend_from_slice(&(entries.len() as u16).to_be_bytes()); + for (tag, typ, count, value) in entries { + out.extend_from_slice(&tag.to_be_bytes()); + out.extend_from_slice(&typ.to_be_bytes()); + out.extend_from_slice(&count.to_be_bytes()); + out.extend_from_slice(value); + } + out.extend_from_slice(&next.to_be_bytes()); + } + + #[test] + fn big_endian_thumbnail_and_adobe_rgb_interop_hint() { + let (ifd1, exif, interop, rational, thumb) = (38u32, 68u32, 98u32, 164u32, 172u32); + let mut b = b"MM\0\x2a\0\0\0\x08".to_vec(); + be_ifd(&mut b, &[(0x0112, 3, 1, [0, 6, 0, 0]), (0x8769, 4, 1, exif.to_be_bytes())], ifd1); + be_ifd(&mut b, &[(0x0201, 4, 1, thumb.to_be_bytes()), (0x0202, 1, 1, [6, 0, 0, 0])], 0); + be_ifd(&mut b, &[(0xA001, 3, 1, [0xFF, 0xFF, 0, 0]), (0xA005, 13, 1, interop.to_be_bytes())], 0); + // Every tag-1 candidate is read; only the ASCII "R03" one matches. + be_ifd( + &mut b, + &[(1, 99, 1, [0; 4]), (1, 5, 1, rational.to_be_bytes()), (1, 4, 1, *b"R98\0"), (1, 3, 1, *b"R9\0\0"), (1, 2, 4, *b"R03\0")], + 0, + ); + assert_eq!(b.len(), rational as usize); + b.extend_from_slice(&[0, 0, 0, 1, 0, 0, 0, 2]); + b.extend_from_slice(&[0xFF, 0xD8, 0xFF, 0xD9, 0, 0]); + let s = summarize(&b); + assert_eq!(s, ExifSummary { orientation: Some(6), thumbnail: Some((thumb as usize, 6)), adobe_rgb_hint: true }); + // A thumbnail that is not a JPEG, or runs past the blob, is ignored. + let mut not_jpeg = b.clone(); + not_jpeg[thumb as usize] = 0; + assert_eq!(summarize(¬_jpeg).thumbnail, None); + assert_eq!(summarize(&b[..b.len() - 1]).thumbnail, None); + // Without ColorSpace 0xFFFF the interop index is not consulted. + let mut srgb = b.clone(); + srgb[(exif + 10) as usize] = 0; + assert!(!summarize(&srgb).adobe_rgb_hint); + } + + #[test] + fn rejects_bad_headers_types_and_oversized_tables() { + assert_eq!(summarize(b"XX\0\x2a\0\0\0\x08"), ExifSummary::default()); + assert_eq!(summarize(b"MM\0\x2b\0\0\0\x08"), ExifSummary::default()); + let mut huge = b"MM\0\x2a\0\0\0\x08".to_vec(); + huge.extend_from_slice(&5000u16.to_be_bytes()); + assert_eq!(summarize(&huge), ExifSummary::default()); + // Orientation stored with an unsupported type (SRATIONAL) is ignored. + let mut bytes = minimal_exif(6); + bytes[12..14].copy_from_slice(&10u16.to_le_bytes()); + assert_eq!(orientation(&bytes), 1); + assert_eq!(orientation(&[b"Exif\0\0".as_slice(), &minimal_exif(3)].concat()), 3); + } +} diff --git a/crates/metadata/src/exif.rs b/crates/metadata/src/exif.rs index c691b7fb0..8f23b5c7b 100644 --- a/crates/metadata/src/exif.rs +++ b/crates/metadata/src/exif.rs @@ -51,8 +51,18 @@ fn pos(v: Option) -> Option { /// 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 { + ifd0_orientation_value(bytes).filter(|v| (1..=8).contains(v)).map(Orientation::from_exif) +} + +fn ifd0_orientation_value(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) + tiff.ifds.first().and_then(|i| i.u16(t::ORIENTATION)) +} + +/// Read the first IFD's orientation, treating out-of-range values as normal. +/// Missing or malformed values return `None`. +pub fn ifd0_orientation_compat(bytes: &[u8]) -> Option { + ifd0_orientation_value(bytes).map(Orientation::from_exif) } /// The ISO an Exif IFD states beyond the 16-bit ISOSpeedRatings: RecommendedExposureIndex, else diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index ebc608496..81c88de20 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -20,6 +20,7 @@ //! [`Embedded`] and call [`from_embedded`]. pub mod camera_fields; +pub mod compat_orientation; mod container; pub mod cr3; mod datetime; @@ -39,7 +40,7 @@ mod xmp_merge; pub use container::{Embedded, JpegInspection, embedded, inspect_jpeg, jpeg_segments, jpeg_segments_with_all_xmp, png_chunks, webp_chunks}; pub use datetime::{DatePrecision, DateTime}; -pub use exif::{from_tiff, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif}; +pub use exif::{from_tiff, ifd0_orientation_compat, read_exif, strip_exif_header, try_read_exif, try_read_exif_with, write_exif}; pub use focus::FocusPoint; pub use fuji::{FujiRecipe, FujiRow, might_hold_fuji_recipe, read_recipe_file, recipe_from_bytes, recipe_from_exif}; pub use geom::{Orientation, Point, Rect}; diff --git a/crates/metadata/tests/ifd0_orientation_compat.rs b/crates/metadata/tests/ifd0_orientation_compat.rs new file mode 100644 index 000000000..948b2a388 --- /dev/null +++ b/crates/metadata/tests/ifd0_orientation_compat.rs @@ -0,0 +1,8 @@ +#[test] +fn missing_and_invalid_values_remain_distinct() { + let mut data = b"II*\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0".to_vec(); + assert_eq!(craft_metadata::ifd0_orientation_compat(&data).map(|o| o.to_exif()), Some(6)); + data[18] = 0; + assert_eq!(craft_metadata::ifd0_orientation_compat(&data).map(|o| o.to_exif()), Some(1)); + assert_eq!(craft_metadata::ifd0_orientation_compat(b"invalid"), None); +} diff --git a/crates/metafile/README.md b/crates/metafile/README.md index ba255c36d..77dd7233c 100644 --- a/crates/metafile/README.md +++ b/crates/metafile/README.md @@ -53,14 +53,14 @@ decoder is where the size, pixel and work limits have to be enforced. | Group | Records | |---|---| | Headers | Placeable header, memory and disk metafile headers. | -| Mapping | `SETMAPMODE`, `SETWINDOWORG`, `SETWINDOWEXT`, `SETVIEWPORTORG`, `SETVIEWPORTEXT`, all map modes (text, metric, isotropic, anisotropic). A placeable file is played into its box the way Office plays it. | +| Mapping | `SETMAPMODE`, `SETWINDOWORG`, `SETWINDOWEXT`, `SETVIEWPORTORG`, `SETVIEWPORTEXT`, `OFFSETWINDOWORG`, `OFFSETVIEWPORTORG`, `SCALEWINDOWEXT`, `SCALEVIEWPORTEXT`, all map modes (text, metric, isotropic, anisotropic). A placeable file is played into its box the way Office plays it. | | State | `SAVEDC`, `RESTOREDC`, `SETPOLYFILLMODE`, `SETROP2`, `SETBKMODE`, `SETBKCOLOR`, `SETTEXTCOLOR`, `SETTEXTALIGN`. | | Objects | `CREATEPENINDIRECT` (style, cap and join bits), `CREATEBRUSHINDIRECT` (solid, null, hatched), `DIBCREATEPATTERNBRUSH`, `CREATEFONTINDIRECT`, `SELECTOBJECT`, `DELETEOBJECT`, stock objects. `CREATEPALETTE`, `CREATEPATTERNBRUSH` and `CREATEREGION` take their handle slot. | | Drawing | `MOVETO`, `LINETO`, `POLYLINE`, `POLYGON`, `POLYPOLYGON`, `RECTANGLE`, `ROUNDRECT`, `ELLIPSE`, `ARC`, `PIE`, `CHORD`, `PATBLT`. | | Bitmaps | `STRETCHDIB`, `DIBSTRETCHBLT`, `DIBBITBLT` (with or without a source). | | Text | `EXTTEXTOUT` (rectangle, opaque, clipped, dx), `TEXTOUT`, decoded in the selected font's character set: code pages 1250 to 1258 and 874, Shift_JIS, UHC, GBK and Big5 (a double-byte character takes the advances of both its bytes), and the symbol character set (also the Symbol font under `DEFAULT_CHARSET`), whose codes stay in the private-use area at U+F000. Face names are read in the font's code page. Johab pairs are blanks. | | Clipping | `INTERSECTCLIPRECT`, `EXCLUDECLIPRECT`. | -| Not read | `OFFSETWINDOWORG`, `OFFSETVIEWPORTORG`, `SCALEWINDOWEXT`, `SCALEVIEWPORTEXT`, flood fills, `SETPIXEL`, region records (`FILLREGION`, `FRAMEREGION`, `PAINTREGION`, `INVERTREGION`, `SELECTCLIPREGION`, `OFFSETCLIPRGN`), the device-dependent `BITBLT`, `STRETCHBLT` and `SETDIBTODEV`. They are counted in `skipped_records`. | +| Not read | Flood fills, `SETPIXEL`, region records (`FILLREGION`, `FRAMEREGION`, `PAINTREGION`, `INVERTREGION`, `SELECTCLIPREGION`, `OFFSETCLIPRGN`), the device-dependent `BITBLT`, `STRETCHBLT` and `SETDIBTODEV`. They are counted in `skipped_records`. | ### EMF (MS-EMF) diff --git a/crates/metafile/src/canvas.rs b/crates/metafile/src/canvas.rs index 5764fc22c..d95393f65 100644 --- a/crates/metafile/src/canvas.rs +++ b/crates/metafile/src/canvas.rs @@ -817,8 +817,14 @@ impl Canvas { if self.arc_clockwise { (e, s) } else { (s, e) } } - /// SCALEVIEWPORTEXTEX and SCALEWINDOWEXTEX: the extent times `num / den` on each axis. A zero - /// denominator leaves the extent alone. + /// OFFSETWINDOWORG and OFFSETVIEWPORTORG (WMF): the window or viewport origin moved by `d`. + pub fn offset_org(&mut self, window: bool, d: P) { + let o = if window { &mut self.map.wo } else { &mut self.map.vo }; + *o = (o.0 + d.0, o.1 + d.1); + } + + /// SCALEVIEWPORTEXTEX and SCALEWINDOWEXTEX (and WMF's SCALEVIEWPORTEXT and SCALEWINDOWEXT): the + /// extent times `num / den` on each axis. A zero denominator leaves the extent alone. pub fn scale_ext(&mut self, window: bool, x: (f64, f64), y: (f64, f64)) { if x.1 == 0.0 || y.1 == 0.0 { return; diff --git a/crates/metafile/src/tests.rs b/crates/metafile/src/tests.rs index 5b8e1ea78..ef1c0867d 100644 --- a/crates/metafile/src/tests.rs +++ b/crates/metafile/src/tests.rs @@ -2006,3 +2006,107 @@ fn canvas_budget_counts_bytes_held_elsewhere() { assert!(cv.full()); assert!(cv.items.is_empty()); } + +/// The box through a path's segment ends (an ellipse's four cubics end on its box). +fn end_extent(item: &Item) -> (f32, f32, f32, f32) { + let (segs, ..) = path_segs(item); + let (xs, ys): (Vec, Vec) = segs + .iter() + .filter_map(|s| match s { + Seg::Move(x, y) | Seg::Line(x, y) | Seg::Cubic(.., x, y) => Some((*x, *y)), + Seg::Close => None, + }) + .unzip(); + let lo = |v: &[f32]| v.iter().copied().fold(f32::MAX, f32::min); + let hi = |v: &[f32]| v.iter().copied().fold(f32::MIN, f32::max); + (lo(&xs), lo(&ys), hi(&xs), hi(&ys)) +} + +/// Two sub-pictures drawn into halves of the frame, as charts pasted from older Office versions +/// are stored (deckcraft#2069): each saves the DC, halves the viewport with SCALEVIEWPORTEXT, sets +/// its own 400 x 400 window (the second shifted 400 to the left, so it lands in the right half), +/// draws a circle and restores the DC. The left circle stays in the left half and the right one +/// lands in the right half of the 800 x 400 frame. +#[test] +fn wmf_scale_viewport_ext_draws_sub_pictures_into_parts_of_the_frame() { + let sub = |org_x: i32| { + vec![ + wrec(0x001E, &[]), + wrec(0x0103, &h16(8)), + // yDenom, yNum, xDenom, xNum: x * 1/2, y * 1/1. + wrec(0x0412, &cat(vec![h16(1), h16(1), h16(2), h16(1)])), + wrec(0x020B, &cat(vec![h16(0), h16(org_x)])), + wrec(0x020C, &cat(vec![h16(400), h16(400)])), + wrec(0x0418, &cat(vec![h16(380), h16(380), h16(20), h16(20)])), + wrec(0x0127, &h16(-1)), + ] + }; + let mut recs = vec![wrec(0x020B, &cat(vec![h16(0), h16(0)])), wrec(0x020C, &cat(vec![h16(400), h16(800)]))]; + recs.extend(sub(0)); + recs.extend(sub(-400)); + let p = parse(&wmf(&recs, None)).unwrap(); + assert_eq!((p.width, p.height), (800.0, 400.0)); + assert_eq!((p.items.len(), p.skipped_records), (2, 0), "both circles, and every record played"); + for (item, want) in p.items.iter().zip([(20.0, 20.0, 380.0, 380.0), (420.0, 20.0, 780.0, 380.0)]) { + let got = end_extent(item); + assert!(close(got.0, want.0) && close(got.1, want.1) && close(got.2, want.2) && close(got.3, want.3), "{got:?}, want {want:?}"); + } +} + +/// SCALEWINDOWEXT scales the window extent, and OFFSETWINDOWORG and OFFSETVIEWPORTORG move the +/// window and viewport origins by their offsets, on top of what the SET records gave them. The +/// 100 x 100 window of a plain WMF is the frame, and the records after it place a rectangle in it. +#[test] +fn wmf_scale_window_ext_and_offset_origins_change_the_mapping() { + let rect = || wrec(0x041B, &cat(vec![h16(20), h16(20), h16(10), h16(10)])); + let base = || vec![wrec(0x0103, &h16(8)), wrec(0x020B, &cat(vec![h16(0), h16(0)])), wrec(0x020C, &cat(vec![h16(100), h16(100)]))]; + let extent = |extra: Vec>| { + let mut recs = base(); + recs.extend(extra); + recs.push(rect()); + rect_extent(&parse(&wmf(&recs, None)).unwrap().items[0]) + }; + // The window doubled (x * 2/1, y * 4/2): the rectangle shows at half size. + let got = extent(vec![wrec(0x0410, &cat(vec![h16(2), h16(4), h16(1), h16(2)]))]); + assert!(close(got.0, 5.0) && close(got.1, 5.0) && close(got.2, 10.0) && close(got.3, 10.0), "scaled window: {got:?}"); + // The window origin moved by (5, 10): the rectangle moves the other way. + let got = extent(vec![wrec(0x020F, &cat(vec![h16(10), h16(5)]))]); + assert!(close(got.0, 5.0) && close(got.1, 0.0) && close(got.2, 15.0) && close(got.3, 10.0), "window origin: {got:?}"); + // The viewport origin moved by (30, -5): the rectangle moves with it. + let got = extent(vec![wrec(0x0211, &cat(vec![h16(-5), h16(30)]))]); + assert!(close(got.0, 40.0) && close(got.1, 5.0) && close(got.2, 50.0) && close(got.3, 15.0), "viewport origin: {got:?}"); + // A zero denominator leaves the extent alone. + let got = extent(vec![wrec(0x0412, &cat(vec![h16(0), h16(1), h16(0), h16(1)]))]); + assert!(close(got.0, 10.0) && close(got.1, 10.0) && close(got.2, 20.0) && close(got.3, 20.0), "zero denominator: {got:?}"); +} + +#[test] +fn wmf_viewport_scaling_does_not_resize_the_player_frame() { + for numerator in [1, 4] { + let recs = [ + wrec(0x0103, &h16(8)), + wrec(0x020C, &cat(vec![h16(100), h16(100)])), + wrec(0x041B, &cat(vec![h16(100), h16(100), h16(0), h16(0)])), + // Change the mapping after drawing, without a matching RESTOREDC. + wrec(0x0412, &cat(vec![h16(1), h16(1), h16(2), h16(numerator)])), + ]; + let p = parse(&wmf(&recs, None)).unwrap(); + assert_eq!((p.width, p.height), (100.0, 100.0)); + assert_eq!(p.skipped_records, 0); + assert_eq!(rect_extent(&p.items[0]), (0.0, 0.0, 100.0, 100.0)); + } +} + +#[test] +fn wmf_viewport_scaling_before_first_window_is_retained() { + let recs = [ + wrec(0x0103, &h16(8)), + wrec(0x0412, &cat(vec![h16(1), h16(1), h16(2), h16(1)])), + wrec(0x020C, &cat(vec![h16(100), h16(100)])), + wrec(0x041B, &cat(vec![h16(100), h16(100), h16(0), h16(0)])), + ]; + let p = parse(&wmf(&recs, None)).unwrap(); + assert_eq!((p.width, p.height), (100.0, 100.0)); + assert_eq!(p.skipped_records, 0); + assert_eq!(rect_extent(&p.items[0]), (0.0, 0.0, 50.0, 100.0)); +} diff --git a/crates/metafile/src/wmf.rs b/crates/metafile/src/wmf.rs index 9f9db3508..c93f823c6 100644 --- a/crates/metafile/src/wmf.rs +++ b/crates/metafile/src/wmf.rs @@ -50,7 +50,8 @@ pub(crate) fn parse(input: Bytes) -> Result<(Canvas, Option), Error> { cv.set_clip_surface([0.0, 0.0, size.0, size.1]); cv.pattern_px = PT_PER_PX; } - let mut player_viewport = bounds.is_some(); + // Keep the player target independent of subsequent mapping changes. + let mut player_target = bounds.map(|(_, size)| size); let mut parsed = 0usize; while !cv.full() { let (Some(words), Some(func)) = (input.u32(pos), pos.checked_add(4).and_then(|o| input.u16(o))) else { break }; @@ -60,10 +61,14 @@ pub(crate) fn parse(input: Bytes) -> Result<(Canvas, Option), Error> { break; } record(&mut cv, func, Bytes(rec)); - if func == 0x020C && cv.window_set && !player_viewport && !cv.viewport_set { + if func == 0x020C && cv.window_set && player_target.is_none() && !cv.viewport_set { let we = cv.map.window_ext(); - cv.play_into(None, (we.0.abs(), we.1.abs())); - player_viewport = true; + let target = (we.0.abs(), we.1.abs()); + // Before an explicit viewport, its default (1, 1) retains any + // SCALEVIEWPORTEXT factors already applied by the file. + let scale = cv.map.viewport_ext(); + cv.play_into(None, (target.0 * scale.0, target.1 * scale.1)); + player_target = Some(target); } parsed += 1; let Some(next) = pos.checked_add(len) else { break }; @@ -82,9 +87,8 @@ pub(crate) fn parse(input: Bytes) -> Result<(Canvas, Option), Error> { let rect = [a.0.min(b.0), a.1.min(b.1), (b.0 - a.0).abs(), (b.1 - a.1).abs()]; (rect[2] > 0.0 && rect[3] > 0.0).then_some(Frame { rect, size: Some(size) }) } - None if player_viewport && !cv.viewport_set && cv.map.uses_extents() => { - let ve = cv.map.viewport_ext(); - Some(Frame { rect: [0.0, 0.0, ve.0, ve.1], size: None }) + None if !cv.viewport_set && cv.map.uses_extents() && player_target.is_some() => { + player_target.map(|(w, h)| Frame { rect: [0.0, 0.0, w, h], size: None }) } None if cv.window_set => Some(Frame { rect: cv.map.window_rect(), size: None }), None => None, @@ -105,6 +109,15 @@ fn record(cv: &mut Canvas, func: u16, r: Bytes) { 0x020C => xy16(r, 6).map(|p| cv.set_window_ext(p)), 0x020D => xy16(r, 6).map(|p| cv.set_viewport_org(p)), 0x020E => xy16(r, 6).map(|p| cv.set_viewport_ext(p)), + // OFFSETWINDOWORG and OFFSETVIEWPORTORG: yOffset, xOffset, added to the origin. + 0x020F | 0x0211 => xy16(r, 6).map(|d| cv.offset_org(func == 0x020F, d)), + // SCALEWINDOWEXT and SCALEVIEWPORTEXT: yDenom, yNum, xDenom, xNum ([MS-WMF] 2.3.5). A + // sub-picture drawn into part of the frame halves the viewport this way (deckcraft#2069). + 0x0410 | 0x0412 => (|| { + let [y_den, y_num, x_den, x_num] = [r.i16(6)?, r.i16(8)?, r.i16(10)?, r.i16(12)?].map(f64::from); + cv.scale_ext(func == 0x0410, (x_num, x_den), (y_num, y_den)); + Some(()) + })(), 0x001E => { cv.save_dc(); Some(()) diff --git a/crates/midi/src/lib.rs b/crates/midi/src/lib.rs index aa82e3e01..fd703e40a 100644 --- a/crates/midi/src/lib.rs +++ b/crates/midi/src/lib.rs @@ -15,7 +15,10 @@ mod smf; pub mod sync; pub use sequence::{CtrlEvent, CtrlKind, Note, Sequence}; -pub use smf::{LenientRead, MAX_EVENTS, MAX_TRACKS, MAX_WARNINGS, Smf, SmfTrack, read_smf, read_smf_lenient, write_smf}; +pub use smf::{ + LenientRead, MAX_EVENTS, MAX_TRACKS, MAX_WARNINGS, Smf, SmfTrack, TimedEvent, encode_meta_event, read_smf, read_smf_lenient, write_smf, + write_timed_smf, +}; /// Ticks per quarter note used throughout the model. pub const PPQ: u32 = 960; diff --git a/crates/midi/src/smf.rs b/crates/midi/src/smf.rs index fb6683a30..801cdd300 100644 --- a/crates/midi/src/smf.rs +++ b/crates/midi/src/smf.rs @@ -598,10 +598,68 @@ fn push_vlq(out: &mut Vec, v: u64) -> Result<(), MidiError> { } /// An event ready to be serialised: `(tick, order, bytes)`. `order` breaks ties at one tick. -struct WEvent { - tick: u64, - order: u8, - bytes: Vec, +/// An already encoded SMF event at an absolute source tick. +/// +/// `bytes` must contain one complete channel, meta, or SysEx event, including its +/// status and any length, but no delta time. The caller owns payload validity. +/// Do not provide running-status fragments or End-of-Track (added by the writer). +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TimedEvent { + pub tick: u64, + pub order: u8, + pub bytes: Vec, +} + +type WEvent = TimedEvent; + +/// Encode an SMF meta event, without its delta time. +pub fn encode_meta_event(kind: u8, data: &[u8]) -> Result, MidiError> { + meta(kind, data) +} + +/// Serialize source-timed events without normalizing ticks or trimming notes. +/// +/// Tracks and same-tick events retain their input order (with `order` as the tie +/// breaker). Format must be 0 (exactly one track) or 1. Division is PPQ 1..=32767. +/// Channel running-status compression is disabled. `clamp_deltas` is an explicit +/// compatibility option: gaps above the SMF 28-bit maximum are saturated rather +/// than rejected. It can shorten represented time and should normally be false. +/// +/// `max_bytes` bounds the conservative serialized size (four bytes per delta), +/// not caller-owned events or sorting memory. Encoded payloads are trusted; this +/// is a framing primitive, not validation of arbitrary untrusted event bytes. +pub fn write_timed_smf(format: u16, division: u16, tracks: Vec>, clamp_deltas: bool, max_bytes: usize) -> Result, MidiError> { + if format > 1 || (format == 0 && tracks.len() != 1) || division == 0 || division > 0x7fff { + return Err(MidiError::Write("invalid format, track count, or PPQ division".into())); + } + let count = u16::try_from(tracks.len()).map_err(|_| MidiError::Write("more than 65535 tracks".into()))?; + let mut bound = 14usize; + for track in &tracks { + bound = bound.checked_add(12).ok_or_else(|| MidiError::Write("output size overflow".into()))?; + let mut body = 4usize; + for event in track { + let size = event.bytes.len().checked_add(4).ok_or_else(|| MidiError::Write("event size overflow".into()))?; + body = body.checked_add(size).ok_or_else(|| MidiError::Write("track size overflow".into()))?; + bound = bound.checked_add(size).ok_or_else(|| MidiError::Write("output size overflow".into()))?; + } + if body > u32::MAX as usize { + return Err(MidiError::Write("track larger than 4 GiB".into())); + } + } + if bound > max_bytes { + return Err(MidiError::Write("serialized output exceeds byte budget".into())); + } + let mut out = Vec::new(); + out.try_reserve(bound).map_err(|_| MidiError::Write("output allocation failed".into()))?; + out.extend_from_slice(b"MThd"); + out.extend_from_slice(&6u32.to_be_bytes()); + out.extend_from_slice(&format.to_be_bytes()); + out.extend_from_slice(&count.to_be_bytes()); + out.extend_from_slice(&division.to_be_bytes()); + for track in tracks { + write_track_policy(&mut out, track, clamp_deltas, false)?; + } + Ok(out) } fn clamp_tick(t: i64) -> u64 { @@ -615,17 +673,22 @@ fn meta(kind: u8, data: &[u8]) -> Result, MidiError> { Ok(b) } -fn write_track(out: &mut Vec, mut events: Vec) -> Result<(), MidiError> { +fn write_track(out: &mut Vec, events: Vec) -> Result<(), MidiError> { + write_track_policy(out, events, false, true) +} + +fn write_track_policy(out: &mut Vec, mut events: Vec, clamp_deltas: bool, running_status: bool) -> Result<(), MidiError> { events.sort_by_key(|e| (e.tick, e.order)); let mut body = Vec::new(); let mut last_tick = 0u64; let mut running: Option = None; for e in &events { - push_vlq(&mut body, e.tick - last_tick)?; + let delta = e.tick - last_tick; + push_vlq(&mut body, if clamp_deltas { delta.min(MAX_VLQ) } else { delta })?; last_tick = e.tick; let status = e.bytes.first().copied().unwrap_or(0); if (0x80..=0xEF).contains(&status) { - if running == Some(status) { + if running_status && running == Some(status) { body.extend_from_slice(e.bytes.get(1..).unwrap_or(&[])); } else { body.extend_from_slice(&e.bytes); @@ -847,4 +910,32 @@ mod tests { // of two notes starting together only the longer survives. assert_eq!(got, vec![(0, 100), (100, 200), (1000, 100), (2000, 900)]); } + #[test] + fn timed_writer_preserves_source_ticks_payloads_and_stable_order() { + let events = vec![ + TimedEvent { tick: 96, order: 3, bytes: vec![0x90, 60, 100] }, + TimedEvent { tick: 96, order: 1, bytes: vec![0x80, 60, 0] }, + TimedEvent { tick: 96, order: 3, bytes: vec![0x90, 60, 110] }, + ]; + let out = write_timed_smf(0, 96, vec![events], false, 53).unwrap(); + assert_eq!(&out[..14], b"MThd\0\0\0\x06\0\0\0\x01\0\x60"); + assert_eq!(&out[22..], &[96, 0x80, 60, 0, 0, 0x90, 60, 100, 0, 0x90, 60, 110, 0, 0xff, 0x2f, 0]); + } + + #[test] + fn timed_writer_checks_budget_format_division_and_delta_policy() { + let event = TimedEvent { tick: MAX_VLQ + 1, order: 0, bytes: vec![0xc0, 1] }; + assert!(write_timed_smf(0, 480, vec![vec![event.clone()]], false, 32).is_err()); + let out = write_timed_smf(0, 480, vec![vec![event.clone()]], true, 32).unwrap(); + assert_eq!(&out[22..26], &[0xff, 0xff, 0xff, 0x7f]); + assert!(write_timed_smf(0, 480, vec![vec![event]], true, 31).is_err()); + assert!(write_timed_smf(0, 96, vec![], false, 32).is_err()); + assert!(write_timed_smf(2, 96, vec![], false, 32).is_err()); + for division in [0, 0x8000, u16::MAX] { + assert!(write_timed_smf(1, division, vec![], false, 32).is_err()); + } + assert_eq!(write_timed_smf(1, 96, vec![], false, 14).unwrap().len(), 14); + assert!(write_timed_smf(1, 96, vec![], false, 13).is_err()); + assert_eq!(encode_meta_event(4, b"Piano").unwrap(), b"\xff\x04\x05Piano"); + } } diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index 607a0b158..6c00312d3 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -24,6 +24,9 @@ write-raster = ["dep:flate2", "dep:roxmltree"] write-simple-text = ["write", "krilla/simple-text"] filters = ["dep:craft-stream-codecs", "dep:flate2", "dep:thiserror"] cos = ["crypt", "filters", "dep:chrono", "dep:js-sys", "dep:wasm-bindgen"] +# Content streams on the object layer (`cos::content`); inline images end where hayro-syntax's +# tokenizer ends them. +content = ["cos", "dep:hayro-syntax"] crypt = ["dep:aes", "dep:md-5", "dep:sha2", "dep:stringprep", "dep:zeroize", "dep:thiserror"] [dependencies] diff --git a/crates/pdf/README.md b/crates/pdf/README.md index 96281512d..1b324cf9d 100644 --- a/crates/pdf/README.md +++ b/crates/pdf/README.md @@ -16,6 +16,7 @@ No feature is enabled by default. | `filters` | The `/Filter` chain for editing and writing PDF files: decode and encode, with limits and accounting. No reader or writer dependency | | `crypt` | The standard security handler: decrypt, encrypt and create encryption. No reader or writer dependency | | `cos` | `crypt` and `filters` plus the object layer: open, edit and save PDF files (incremental and full saves). No reader or writer dependency | +| `content` | `cos` plus `cos::content`: parse content streams into operators with source spans, write them back, and the matrix arithmetic. Uses hayro-syntax's tokenizer for inline images | ## Export settings @@ -143,6 +144,11 @@ let bytes = write_incremental(&doc, &SaveOptions::default())?; // append a revis let bytes = write_full(&doc, &SaveOptions::default())?; // rewrite what the trailer reaches ``` +With `content`, `cos::content` reads and writes page content streams. `parse` keeps each +operator's byte span and skips (and counts) what it cannot read instead of failing; inline images +stay whole, so `parse` → `serialize_ops` round-trips them. `Matrix` holds the arithmetic content +interpretation needs. + - Reads every cross-reference form (tables, streams, hybrid `/XRefStm`) and object streams. A file whose cross-reference data is wrong or missing is rebuilt by scanning for object headers, and each repair is recorded (`repair_log`). Malformed input gives `CosError`, never a panic. diff --git a/crates/pdf/src/cos.rs b/crates/pdf/src/cos.rs index 1084c8bf4..d67543041 100644 --- a/crates/pdf/src/cos.rs +++ b/crates/pdf/src/cos.rs @@ -13,6 +13,9 @@ mod budget; mod bytes; +/// Content streams: operators with source spans, inline images, the serializer, matrices. +#[cfg(feature = "content")] +pub mod content; mod document; mod object; pub mod page_labels; diff --git a/crates/pdf/src/cos/content.rs b/crates/pdf/src/cos/content.rs new file mode 100644 index 000000000..a3087972d --- /dev/null +++ b/crates/pdf/src/cos/content.rs @@ -0,0 +1,785 @@ +//! Content streams (ISO 32000-2 §7.8.2, §8, §9): parse a stream into operators that keep their +//! source spans, write operators back, and the matrix arithmetic content interpretation needs. +//! +//! Feature `content`. The parser is tolerant: bytes it cannot read are skipped (and counted in +//! [`Parsed::skipped`]) instead of failing the whole stream, and it never panics. Inline images +//! (`BI … ID … EI`) are kept whole, so a parse → serialize round trip preserves them. Where an +//! inline image ends is decided by hayro-syntax's tokenizer, the one the `raster` renderer reads +//! content with ("the renderer" below). + +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +use crate::cos::{Dict, Lexer, Object, PdfString, serialize}; + +#[cfg(test)] +mod tests; + +/// One operator with its operands. +#[derive(Clone, Debug, PartialEq)] +pub struct Op { + /// The operator keyword (`Tj`, `re`, `BI` for inline images…). + pub op: Vec, + pub operands: Vec, + /// Inline images: the image dictionary (`operands` is empty) and the data between `ID` and + /// `EI`. + pub inline: Option<(Dict, Vec)>, + /// The bytes of the source stream this operator came from (operands included). + pub span: std::ops::Range, +} + +impl Op { + pub fn new(op: &str, operands: Vec) -> Self { + Op { op: op.as_bytes().to_vec(), operands, inline: None, span: 0..0 } + } + + pub fn is(&self, op: &str) -> bool { + self.op == op.as_bytes() + } + + /// Operand `i` as a number. + pub fn num(&self, i: usize) -> Option { + self.operands.get(i).and_then(Object::as_f64) + } + + /// The last `n` operands as numbers (operators take their operands from the end). + pub fn nums(&self) -> Option<[f64; N]> { + let k = self.operands.len().checked_sub(N)?; + let mut out = [0.0; N]; + for (o, v) in out.iter_mut().zip(&self.operands[k..]) { + *o = v.as_f64()?; + } + Some(out) + } + + pub fn name(&self, i: usize) -> Option<&[u8]> { + self.operands.get(i).and_then(Object::as_name) + } + + /// The last operand as a name: the one `Do` and the other one-operand operators act on, since + /// the renderer takes operands from the end and ignores any surplus before them. + pub fn last_name(&self) -> Option<&[u8]> { + self.operands.last().and_then(Object::as_name) + } + + /// Operand `k` counted from the end (0 is the last), as the renderer reads an operator's + /// operands: `(x) /OC /MC0 BDC` marks with `/OC /MC0`, and `(x) /F1 10 Tf` selects `/F1`. + pub fn back(&self, k: usize) -> Option<&Object> { + let i = self.operands.len().checked_sub(k)?.checked_sub(1)?; + self.operands.get(i) + } + + /// [`Op::back`] as a name. + pub fn name_back(&self, k: usize) -> Option<&[u8]> { + self.back(k).and_then(Object::as_name) + } + + /// [`Op::back`] as a number. + pub fn num_back(&self, k: usize) -> Option { + self.back(k).and_then(Object::as_f64) + } +} + +/// A parsed stream. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Parsed { + pub ops: Vec, + /// Bytes that could not be read as operands or operators. + pub skipped: usize, + /// Inline images the renderer's tokenizer reads no image for (it stops, or finds no `EI` it + /// accepts), so their end here is a guess and the operators after them may not be the ones + /// the page draws; and images it reads whose parameters this parser can't, which are missing + /// from `ops` although the page draws them. + pub ambiguous_images: usize, + /// For [`Pieces::parse`]: per piece, whether a comment runs into it from the piece before + /// and whether one runs out of it into the next. Only boundaries are recorded, so the size + /// depends on the number of pieces, not of comments. + comment_edges: Vec<(bool, bool)>, +} + +fn is_ws(b: u8) -> bool { + matches!(b, b'\0' | b'\t' | b'\n' | b'\x0C' | b'\r' | b' ') +} + +fn is_delim(b: u8) -> bool { + matches!(b, b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%') +} + +/// Longest unreadable number token retried from its next byte. Real numbers are far shorter. +const MAX_RETRIED_TOKEN: usize = 64; + +/// Parse a (decoded) content stream. +pub fn parse(data: &[u8]) -> Parsed { + parse_pieces(data, &[]) +} + +/// The operators of a (decoded) content stream, read one at a time, as [`parse`] reads them. A +/// caller that stops early or keeps only a summary never holds every operator of the stream. +pub fn ops(data: &[u8]) -> Ops<'_> { + Ops::new(data, &[]) +} + +/// Parse `data`, recording which of the piece boundaries `ends` (as in [`Pieces`]) a comment +/// crosses. +fn parse_pieces(data: &[u8], ends: &[usize]) -> Parsed { + let mut reader = Ops::new(data, ends); + let ops = reader.by_ref().collect(); + Parsed { ops, skipped: reader.skipped, ambiguous_images: reader.ambiguous_images, comment_edges: reader.comment_edges } +} + +/// The iterator [`ops`] returns. +pub struct Ops<'a> { + data: &'a [u8], + lx: Lexer<'a>, + operands: Vec, + start: Option, + skipped: usize, + ambiguous_images: usize, + ends: &'a [usize], + boundary: usize, + comment_edges: Vec<(bool, bool)>, +} + +impl<'a> Ops<'a> { + fn new(data: &'a [u8], ends: &'a [usize]) -> Self { + Ops { + data, + lx: Lexer::new(data, 0), + operands: Vec::new(), + start: None, + skipped: 0, + ambiguous_images: 0, + ends, + boundary: 0, + comment_edges: vec![(false, false); ends.len()], + } + } + + /// Bytes read so far that could not be read as operands or operators. + pub fn skipped(&self) -> usize { + self.skipped + } + + /// Inline images read so far that are ambiguous (as in [`Parsed::ambiguous_images`]): either + /// their end is this parser's guess, or the renderer reads them and this parser cannot. + pub fn ambiguous_images(&self) -> usize { + self.ambiguous_images + } +} + +impl Iterator for Ops<'_> { + type Item = Op; + + fn next(&mut self) -> Option { + let Ops { data, lx, operands, start, skipped, ambiguous_images, ends, boundary, comment_edges: edges } = self; + let (data, ends): (&[u8], &[usize]) = (data, ends); + loop { + let trivia = lx.pos; + lx.skip_ws(); + // Only the whitespace the lexer skips here holds top-level comments: a `%` inside a + // string, a name, an array or dictionary operand or inline image data is not one. + if *boundary < ends.len() { + comment_edges(data, trivia..lx.pos, ends, boundary, edges); + } + let Some(&b) = data.get(lx.pos) else { + // Operands left without an operator. + *skipped += operands.len(); + operands.clear(); + return None; + }; + let at = lx.pos; + let operand_start = matches!(b, b'/' | b'(' | b'<' | b'[' | b'+' | b'-' | b'.') || b.is_ascii_digit(); + if operand_start { + match lx.object() { + Ok(o) => { + start.get_or_insert(at); + operands.push(o); + } + Err(_) if b == b'(' => { + // An unterminated string runs to the end of the stream. + *skipped += data.len() - at; + lx.pos = data.len(); + } + Err(_) => { + let read = lx.pos.saturating_sub(at); + #[cfg(test)] + tests::RESCANNED.with(|c| c.set(c.get() + read)); + let number = matches!(b, b'+' | b'-' | b'.') || b.is_ascii_digit(); + if number && read > MAX_RETRIED_TOKEN { + // A long unreadable number is skipped whole: retrying from each of its + // bytes would read the rest of it again every time. + *skipped += read; + } else { + // Skip the offending byte and carry on. + lx.pos = at + 1; + *skipped += 1; + } + } + } + continue; + } + if is_delim(b) { + // A stray `)`, `>`, `]`, `{`, `}`. + lx.pos += 1; + *skipped += 1; + continue; + } + let kw = lx.token(); + if kw.is_empty() { + lx.pos = at + 1; + *skipped += 1; + continue; + } + match kw { + b"true" => { + start.get_or_insert(at); + operands.push(Object::Bool(true)); + } + b"false" => { + start.get_or_insert(at); + operands.push(Object::Bool(false)); + } + b"null" => { + start.get_or_insert(at); + operands.push(Object::Null); + } + b"BI" => { + let image = inline_image(data, at, lx.pos); + *ambiguous_images += usize::from(image.ambiguous); + let end = image.end; + lx.pos = end; + operands.clear(); + match image.image { + Some((dict, img)) => { + return Some(Op { + op: b"BI".to_vec(), + operands: Vec::new(), + inline: Some((dict, img)), + span: start.take().unwrap_or(at)..end, + }); + } + None => { + *skipped += end.saturating_sub(at); + *start = None; + } + } + } + _ => { + let s = start.take().unwrap_or(at); + return Some(Op { op: kw.to_vec(), operands: std::mem::take(operands), inline: None, span: s..lx.pos }); + } + } + } + } +} + +/// Record the piece boundaries crossed by the comments in `trivia` (skipped whitespace and +/// comments). `boundary` is the first boundary not yet passed; boundaries only move forward. +fn comment_edges(data: &[u8], trivia: std::ops::Range, ends: &[usize], boundary: &mut usize, edges: &mut [(bool, bool)]) { + let mut at = trivia.start; + while at < trivia.end { + if data.get(at) != Some(&b'%') { + at += 1; + continue; + } + let comment = at; + while at < trivia.end && !matches!(data.get(at), Some(b'\r' | b'\n')) { + at += 1; + } + // The separator of piece `boundary` is the last byte before its end. + while let Some(&end) = ends.get(*boundary) { + let separator = end.saturating_sub(1); + if separator >= at { + break; + } + if separator >= comment { + if let Some(edge) = edges.get_mut(*boundary) { + edge.1 = true; + } + // Includes a comment whose end of line is the next piece's first byte. + if let Some(edge) = edges.get_mut(boundary.saturating_add(1)) { + edge.0 = true; + } + } + *boundary += 1; + } + } +} + +/// An inline image as [`inline_image`] reads it. +struct Inline { + /// Its parameters and data; `None` when the parameters can't be read. + image: Option<(Dict, Vec)>, + /// The position after its `EI` (or the end of the stream). + end: usize, + /// The renderer's tokenizer reads no image here, so `end` is this parser's guess; or it reads + /// one whose parameters this parser can't, so `image` is `None` for an image the page draws. + ambiguous: bool, +} + +/// Read the inline image whose `BI` keyword starts at `bi` (its parameters start at `pos`). +/// +/// Filtered image data has no length, and `EI` can occur inside it, so where it ends is a +/// heuristic choice among `EI` candidates. The renderer's choice decides which operators the +/// page draws after the image, and redaction must see the same ones, so the end is taken from +/// the renderer's tokenizer (hayro-syntax) rather than worked out here by another rule. Only +/// when that tokenizer reads no image does this parser fall back to its own rules, and the +/// image is marked ambiguous. +fn inline_image(data: &[u8], bi: usize, pos: usize) -> Inline { + let mut lx = Lexer::new(data, pos); + let mut dict = Dict::new(); + let mut readable = true; + let mut stop = pos; + loop { + lx.skip_ws(); + if lx.eat_keyword(b"ID") { + break; + } + let at = lx.pos; + let entry = match lx.object() { + Ok(Object::Name(k)) => lx.object().ok().map(|v| (k, v)), + _ => None, + }; + let Some((k, v)) = entry else { + // A stream that ends inside the parameters is cut short, not unreadable: keep what was read. + if at >= data.len() { + return Inline { image: Some((dict, Vec::new())), end: data.len(), ambiguous: false }; + } + readable = false; + stop = at; + break; + }; + dict.set(k, v); + } + if let Some((start, end_data, end)) = renderer_image_end(data, bi) { + // Where the renderer ends the data at an `EI` found by its heuristic, one whitespace byte + // before that `EI` separates it from the data. + let mut samples = data.get(start..end_data).unwrap_or_default(); + if data.get(end_data..).is_some_and(|rest| rest.starts_with(b"EI")) + && let Some((last, rest)) = samples.split_last() + && is_ws(*last) + { + samples = rest; + } + // The renderer draws an image whose parameters this parser can't read: it is not + // represented here, so a redaction can't inspect or remove it and must not trust the page. + return Inline { image: readable.then(|| (dict, samples.to_vec())), end, ambiguous: !readable }; + } + if !readable { + // Without an `EI` after it, the image runs to the end of the stream. + return Inline { image: None, end: end_of_image(data, stop, b"").map_or(data.len(), |(_, end)| end), ambiguous: true }; + } + // One whitespace byte separates ID from the data. + let begin = (lx.pos + 1).min(data.len()); + let end = unfiltered_image_end(data, begin, &dict) + .or_else(|| declared_image_end(data, begin, &dict)) + .or_else(|| image_data_end(data, begin, filter_eod(&dict))); + let (image, end) = match end { + Some((end_data, end)) => (data.get(begin..end_data).unwrap_or_default().to_vec(), end), + None => (data.get(begin..).unwrap_or_default().to_vec(), data.len()), + }; + Inline { image: Some((dict, image)), end, ambiguous: true } +} + +/// Where the renderer's tokenizer ends the inline image whose `BI` starts at `bi`: the start and +/// end of its data and the position after its `EI`. `None` when it reads no image there (its +/// parameters don't read, or no `EI` passes its checks). +fn renderer_image_end(data: &[u8], bi: usize) -> Option<(usize, usize, usize)> { + use hayro_syntax::content::TypedIter; + use hayro_syntax::content::ops::{InlineImage, TypedInstruction}; + let tail = data.get(bi..)?; + let mut iter = TypedIter::new(tail); + let (start, len) = match iter.next()? { + TypedInstruction::InlineImage(InlineImage(stream)) => match stream.raw_data() { + // The data is a slice of `tail`: its offset is where it starts. + std::borrow::Cow::Borrowed(samples) => ((samples.as_ptr() as usize).checked_sub(tail.as_ptr() as usize)?, samples.len()), + std::borrow::Cow::Owned(_) => return None, + }, + _ => return None, + }; + let end_data = start.checked_add(len)?; + // `committed` is past the whitespace after `EI`; the image ends at the `EI`. + let mut end = iter.committed(); + while end > end_data && tail.get(end - 1).is_some_and(|b| is_ws(*b)) { + end -= 1; + } + if end < end_data.checked_add(2)? || tail.get(end - 2..end) != Some(b"EI") { + return None; + } + Some((bi.checked_add(start)?, bi.checked_add(end_data)?, bi.checked_add(end)?)) +} + +/// The end-of-data marker (`>` for ASCIIHex, `~>` for ASCII85) of the outer stream filter, if any. +fn filter_eod(dict: &Dict) -> &'static [u8] { + for key in [b"Filter".as_slice(), b"F"] { + if let Some(filter) = dict.get(key) { + let name = match filter { + Object::Name(name) => Some(name.as_slice()), + Object::Array(filters) => filters.first().and_then(Object::as_name), + _ => None, + }; + return match name { + Some(b"ASCIIHexDecode" | b"AHx") => b">", + Some(b"ASCII85Decode" | b"A85") => b"~>", + _ => b"", + }; + } + } + b"" +} + +/// An unfiltered image's samples determine its length, including byte padding per row. +/// Binary samples may themselves contain whitespace + `EI` + whitespace, so use the known +/// length when the actual terminator follows it. Unknown or damaged layouts keep the heuristic. +fn unfiltered_image_end(data: &[u8], begin: usize, dict: &Dict) -> Option<(usize, usize)> { + for key in [b"Filter".as_slice(), b"F"] { + if let Some(filter) = dict.get(key) + && !matches!(filter, Object::Null) + && !matches!(filter, Object::Array(filters) if filters.is_empty()) + { + return None; + } + } + for (full, short) in + [(b"Width".as_slice(), b"W".as_slice()), (b"Height", b"H"), (b"ColorSpace", b"CS"), (b"BitsPerComponent", b"BPC"), (b"ImageMask", b"IM")] + { + if let (Some(full), Some(short)) = (dict.get(full), dict.get(short)) + && full != short + { + return None; + } + } + let get = |full: &[u8], short: &[u8]| dict.get(full).or_else(|| dict.get(short)); + let width = usize::try_from(get(b"Width", b"W")?.as_int()?).ok().filter(|n| *n > 0)?; + let height = usize::try_from(get(b"Height", b"H")?.as_int()?).ok().filter(|n| *n > 0)?; + let mask = match get(b"ImageMask", b"IM") { + None | Some(Object::Null | Object::Bool(false)) => false, + Some(Object::Bool(true)) => true, + _ => return None, + }; + let bits = match get(b"BitsPerComponent", b"BPC") { + None | Some(Object::Null) => None, + Some(value) => Some(value.as_int()?), + }; + let (components, bits) = if mask { + if bits.is_some_and(|bits| bits != 1) { + return None; + } + (1usize, 1usize) + } else { + let (components, max_bits) = match get(b"ColorSpace", b"CS")? { + Object::Name(name) => match name.as_slice() { + b"DeviceGray" | b"G" => (1, 16), + b"DeviceRGB" | b"RGB" => (3, 16), + b"DeviceCMYK" | b"CMYK" => (4, 16), + _ => return None, + }, + Object::Array(space) if space.len() == 4 && matches!(space.first().and_then(Object::as_name), Some(b"Indexed" | b"I")) => (1, 8), + _ => return None, + }; + let bits = match bits? { + bits @ (1 | 2 | 4 | 8 | 16) => bits as usize, + _ => return None, + }; + if bits > max_bits { + return None; + } + (components, bits) + }; + let row = width.checked_mul(components)?.checked_mul(bits)?.div_ceil(8); + let end_data = begin.checked_add(row.checked_mul(height)?)?; + let after = data.get(end_data..)?; + let separator = after.iter().position(|b| !is_ws(*b))?; + if separator == 0 && data.get(end_data.checked_sub(1)?).is_some_and(|b| is_ws(*b)) { + return None; + } + let ei = end_data.checked_add(separator)?; + let end = ei.checked_add(2)?; + if data.get(ei..end) == Some(b"EI") && data.get(end).is_none_or(|b| is_ws(*b) || is_delim(*b)) { Some((end_data, end)) } else { None } +} + +/// The length declared by `/L` (or `/Length`, ISO 32000-2 §8.9.7), used when `EI` follows the +/// declared bytes after optional whitespace. A declared length without that `EI` is ignored. +fn declared_image_end(data: &[u8], begin: usize, dict: &Dict) -> Option<(usize, usize)> { + let length = dict.get(b"L").or_else(|| dict.get(b"Length"))?.as_int()?; + let end_data = begin.checked_add(usize::try_from(length).ok()?)?; + let after = data.get(end_data..)?; + let ei = end_data.checked_add(after.iter().position(|b| !is_ws(*b))?)?; + let end = ei.checked_add(2)?; + if data.get(ei..end) == Some(b"EI") && data.get(end).is_none_or(|b| is_ws(*b) || is_delim(*b)) { Some((end_data, end)) } else { None } +} + +/// Where inline image data of unknown length ends. A candidate `EI` followed by binary bytes before +/// the next `EI` lies inside the (compressed) data, unless another inline image starts first. +fn image_data_end(data: &[u8], begin: usize, eod: &[u8]) -> Option<(usize, usize)> { + let mut candidate = end_of_image(data, begin, eod)?; + loop { + let (end_data, end) = candidate; + let Some(next) = end_of_image(data, end, eod) else { + return Some(candidate); + }; + let between = data.get(end..next.0).unwrap_or_default(); + if !between.iter().any(|b| !b.is_ascii()) || starts_inline_image(between) { + return Some((end_data, end)); + } + candidate = next; + } +} + +/// Whether `bytes` hold a whitespace-delimited `BI` operator. +fn starts_inline_image(bytes: &[u8]) -> bool { + bytes + .windows(2) + .enumerate() + .any(|(i, pair)| pair == b"BI" && (i == 0 || is_ws(bytes[i - 1])) && bytes.get(i + 2).is_none_or(|b| is_ws(*b) || is_delim(*b))) +} + +/// Where inline image data starting at `begin` ends: (end of the data, position after `EI`). The +/// data ends at whitespace (or `eod`: `>` for ASCIIHex, `~>` for ASCII85) + `EI` + (whitespace, +/// a delimiter or the end). +fn end_of_image(data: &[u8], begin: usize, eod: &[u8]) -> Option<(usize, usize)> { + let mut i = begin; + while i + 2 <= data.len() { + if data[i] == b'E' + && data.get(i + 1) == Some(&b'I') + && (i == begin || is_ws(data[i - 1]) || (!eod.is_empty() && data[begin..i].ends_with(eod))) + && data.get(i + 2).is_none_or(|b| is_ws(*b) || is_delim(*b)) + { + let end_data = if i > begin && is_ws(data[i - 1]) { i - 1 } else { i }; + return Some((end_data, i + 2)); + } + i += 1; + } + None +} + +/// Write one operator (with a trailing newline). +pub fn write_op(op: &Op, out: &mut Vec) { + if let Some((dict, img)) = &op.inline { + out.extend_from_slice(b"BI"); + for (k, v) in dict.iter() { + out.push(b' '); + serialize(&Object::Name(k.clone()), out); + out.push(b' '); + serialize(v, out); + } + out.extend_from_slice(b" ID "); + out.extend_from_slice(img); + out.extend_from_slice(b"\nEI\n"); + return; + } + for o in &op.operands { + serialize(o, out); + out.push(b' '); + } + out.extend_from_slice(&op.op); + out.push(b'\n'); +} + +/// Write operators back to a content stream. +pub fn serialize_ops(ops: &[Op]) -> Vec { + let mut out = Vec::new(); + for op in ops { + write_op(op, &mut out); + } + out +} + +/// A page's content streams joined into the one stream they are. Writers may split it between +/// any two tokens (ISO 32000-2 §7.8.2), so an operator's operands can end one piece and its +/// keyword start the next: parse the pieces joined ([`Pieces::parse`]) and map positions back +/// with [`Pieces::piece_of`]. A space separates the pieces, as renderers join them (hayro, +/// MuPDF and Poppler alike): tokens never run together, and a comment at the end of a piece +/// runs on into the next one, up to its first end of line. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Pieces { + data: Vec, + /// Where each piece ends in `data`, after its separator. + ends: Vec, +} + +impl Pieces { + pub fn join>(pieces: &[D]) -> Self { + let mut data = Vec::new(); + let mut ends = Vec::with_capacity(pieces.len()); + for p in pieces { + data.extend_from_slice(p.as_ref()); + data.push(b' '); + ends.push(data.len()); + } + Pieces { data, ends } + } + + /// The joined bytes (the spans of [`Pieces::parse`]'s operators index into them). + pub fn data(&self) -> &[u8] { + &self.data + } + + /// How many pieces there are. + pub fn len(&self) -> usize { + self.ends.len() + } + + pub fn is_empty(&self) -> bool { + self.ends.is_empty() + } + + /// Parse the pieces as one stream. + pub fn parse(&self) -> Parsed { + parse_pieces(&self.data, &self.ends) + } + + /// Per piece, from `parsed` (this piece set's [`Pieces::parse`]): whether a comment runs + /// into it from the piece before, and whether one runs out of it into the next. A writer + /// that rewrites a piece without its comments must keep both: end the incoming comment + /// before its first operator, and leave a comment open at its end. + pub fn comment_edges(&self, parsed: &Parsed) -> Vec<(bool, bool)> { + let mut edges = parsed.comment_edges.clone(); + edges.resize(self.len(), (false, false)); + edges + } + + /// The piece holding joined position `at` (the last piece for positions past the end). + pub fn piece_of(&self, at: usize) -> usize { + self.ends.partition_point(|&e| e <= at).min(self.ends.len().saturating_sub(1)) + } + + /// The pieces an operator spans: (the one its first token is in, the one its keyword is in). + pub fn pieces_of(&self, op: &Op) -> (usize, usize) { + (self.piece_of(op.span.start), self.piece_of(op.span.end.saturating_sub(1))) + } + + /// The joined range of piece `i`, without its separator. + fn range(&self, i: usize) -> std::ops::Range { + let start = match i.checked_sub(1) { + None => 0, + Some(p) => self.ends.get(p).copied().unwrap_or(self.data.len()), + }; + let end = self.ends.get(i).map_or(self.data.len(), |e| e.saturating_sub(1)).max(start); + start..end + } + + /// Rebuild every piece after replacing joined byte ranges: `edits` are (range, bytes) in + /// ascending, non-overlapping order. A replacement goes into the piece its range starts in, + /// and the rest of a range that runs on into later pieces is removed from them, so an + /// operator split across pieces is replaced or removed whole. Everything else is copied byte + /// for byte. Returns every piece's new bytes (unchanged pieces come back equal). + pub fn splice(&self, edits: impl IntoIterator, Vec)>) -> Vec> { + let mut out: Vec> = (0..self.len()).map(|i| Vec::with_capacity(self.range(i).len())).collect(); + // Only the pieces from the one holding `from` up to `to`: one call per edit must not + // visit every piece. + let copy = |out: &mut Vec>, from: usize, to: usize| { + if from >= to { + return; + } + let first = self.piece_of(from); + for (i, piece) in out.iter_mut().enumerate().skip(first) { + #[cfg(test)] + tests::PIECE_VISITS.with(|c| c.set(c.get() + 1)); + let r = self.range(i); + if r.start >= to { + break; + } + let (a, b) = (from.max(r.start), to.min(r.end)); + if a < b { + piece.extend_from_slice(self.data.get(a..b).unwrap_or_default()); + } + } + }; + let mut at = 0; + for (range, bytes) in edits { + let start = range.start.clamp(at, self.data.len()); + copy(&mut out, at, start); + if !bytes.is_empty() + && let Some(piece) = out.get_mut(self.piece_of(start)) + { + piece.extend_from_slice(&bytes); + } + at = range.end.clamp(start, self.data.len()); + } + copy(&mut out, at, self.data.len()); + out + } +} + +/// A number operand, written as an integer when it is one. +pub fn num(v: f64) -> Object { + if v.fract() == 0.0 && v.abs() < 1e15 { Object::Int(v as i64) } else { Object::Real((v * 10_000.0).round() / 10_000.0) } +} + +pub fn string(bytes: Vec) -> Object { + Object::String(PdfString::literal(bytes)) +} + +/// An affine matrix `[a b c d e f]` (PDF order: a point maps to `(a·x + c·y + e, b·x + d·y + f)`). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Matrix(pub [f64; 6]); + +impl Default for Matrix { + fn default() -> Self { + Matrix::IDENTITY + } +} + +impl Matrix { + pub const IDENTITY: Matrix = Matrix([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]); + + pub fn translate(x: f64, y: f64) -> Self { + Matrix([1.0, 0.0, 0.0, 1.0, x, y]) + } + + pub fn from_operands(v: &[Object]) -> Option { + if v.len() != 6 { + return None; + } + let mut m = [0.0; 6]; + for (o, x) in m.iter_mut().zip(v) { + *o = x.as_f64()?; + } + Some(Matrix(m)) + } + + /// `self` then `then` (`self × then` in PDF's row-vector convention). + pub fn then(&self, then: &Matrix) -> Matrix { + let [a, b, c, d, e, f] = self.0; + let [a2, b2, c2, d2, e2, f2] = then.0; + Matrix([a * a2 + b * c2, a * b2 + b * d2, c * a2 + d * c2, c * b2 + d * d2, e * a2 + f * c2 + e2, e * b2 + f * d2 + f2]) + } + + pub fn apply(&self, x: f64, y: f64) -> (f64, f64) { + let [a, b, c, d, e, f] = self.0; + (a * x + c * y + e, b * x + d * y + f) + } + + pub fn invert(&self) -> Option { + let [a, b, c, d, e, f] = self.0; + let det = a * d - b * c; + if det == 0.0 || !det.is_finite() || det.abs() <= 1e-12 * ((a * d).abs() + (b * c).abs()) { + return None; + } + let (ia, ib, ic, id) = (d / det, -b / det, -c / det, a / det); + let inverse = [ia, ib, ic, id, -(e * ia + f * ic), -(e * ib + f * id)]; + // A determinant that underflows toward zero can still pass the relative check above, + // and dividing by it overflows: such a matrix has no usable inverse. + inverse.iter().all(|v| v.is_finite()).then_some(Matrix(inverse)) + } + + /// The bounding box of a rectangle `[x0 y0 x1 y1]` after this transform. + pub fn bbox(&self, r: [f64; 4]) -> [f64; 4] { + let pts = [self.apply(r[0], r[1]), self.apply(r[2], r[1]), self.apply(r[0], r[3]), self.apply(r[2], r[3])]; + let mut b = [f64::MAX, f64::MAX, f64::MIN, f64::MIN]; + for (x, y) in pts { + b = [b[0].min(x), b[1].min(y), b[2].max(x), b[3].max(y)]; + } + b + } +} + +/// Do two rectangles overlap by more than `eps` in both directions? +pub fn overlaps(a: [f64; 4], b: [f64; 4], eps: f64) -> bool { + a[0].max(b[0]) + eps < a[2].min(b[2]) && a[1].max(b[1]) + eps < a[3].min(b[3]) +} + +/// Is `inner` inside `outer` (within `eps`)? +pub fn contains(outer: [f64; 4], inner: [f64; 4], eps: f64) -> bool { + inner[0] >= outer[0] - eps && inner[1] >= outer[1] - eps && inner[2] <= outer[2] + eps && inner[3] <= outer[3] + eps +} diff --git a/crates/pdf/src/cos/content/tests.rs b/crates/pdf/src/cos/content/tests.rs new file mode 100644 index 000000000..6a44d51b1 --- /dev/null +++ b/crates/pdf/src/cos/content/tests.rs @@ -0,0 +1,589 @@ +use super::*; + +thread_local! { + /// Bytes the lexer read before an operand failed, on this thread. + pub(crate) static RESCANNED: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +thread_local! { + /// Pieces [`Pieces::splice`] looked at while copying, on this thread. + pub(crate) static PIECE_VISITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[test] +fn operands_are_counted_from_the_end() { + let op = parse(b"(x) /OC /MC0 BDC").ops.remove(0); + assert_eq!((op.name_back(1), op.name_back(0)), (Some(&b"OC"[..]), Some(&b"MC0"[..]))); + assert_eq!(op.back(2).and_then(Object::as_string).map(|s| s.bytes.clone()), Some(b"x".to_vec())); + assert_eq!((op.back(3), op.back(usize::MAX)), (None, None)); + let op = parse(b"/F1 10 Tf").ops.remove(0); + assert_eq!((op.name_back(1), op.num_back(0), op.num_back(1)), (Some(&b"F1"[..]), Some(10.0), None)); + assert_eq!(Op::new("Q", vec![]).back(0), None); +} + +#[test] +fn splicing_visits_each_piece_a_few_times() { + let n = 5_000; + let pieces: Vec = (0..n).map(|i| format!("({i}) Tj")).collect(); + let joined = Pieces::join(&pieces); + let parsed = joined.parse(); + let edits: Vec<_> = parsed.ops.iter().map(|op| (op.span.clone(), b"(x) Tj".to_vec())).collect(); + assert_eq!(edits.len(), n); + PIECE_VISITS.with(|c| c.set(0)); + let out = joined.splice(edits); + let visits = PIECE_VISITS.with(|c| c.get()); + assert!(visits <= 4 * n, "{visits} piece visits for {n} pieces"); + assert!(out.iter().all(|p| p == b"(x) Tj")); + // A replacement spanning two pieces stays whole in the first and leaves the rest of the second. + let two = Pieces::join(&["q (a)", "Tj Q"]); + let tj = two.parse().ops.into_iter().find(|o| o.is("Tj")).map(|o| o.span).unwrap_or_default(); + assert_eq!(two.splice([(tj, b"(b) Tj".to_vec())]), [b"q (b) Tj".to_vec(), b" Q".to_vec()]); +} + +#[test] +fn a_long_unreadable_number_is_read_once() { + for junk in ["+", "5"] { + let n = 20_000; + let src = format!("{}{} 1 0 0 1 0 0 cm", junk.repeat(n), if junk == "5" { "x" } else { "" }); + RESCANNED.with(|c| c.set(0)); + let p = parse(src.as_bytes()); + let rescanned = RESCANNED.with(|c| c.get()); + assert!(rescanned <= 2 * n, "{rescanned} bytes read for a {n}-byte token"); + assert_eq!(p.ops.last().and_then(|o| o.nums::<6>()), Some([1.0, 0.0, 0.0, 1.0, 0.0, 0.0])); + } + // A short damaged number still recovers byte by byte. + let p = parse(b"-+5 0 Td"); + assert_eq!((p.ops.len(), p.skipped), (1, 1)); + assert_eq!(p.ops[0].nums::<2>(), Some([5.0, 0.0])); +} + +#[test] +fn operators_keep_operands_and_spans() { + let src = b"q 1 0 0 1 72 720 cm BT /F1 12 Tf (Hello \\(world\\)) Tj [(A) -120 (B)] TJ ET Q"; + let p = parse(src); + assert_eq!(p.skipped, 0); + let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"q"[..], b"cm", b"BT", b"Tf", b"Tj", b"TJ", b"ET", b"Q"]); + let cm = &p.ops[1]; + assert_eq!(cm.nums::<6>(), Some([1.0, 0.0, 0.0, 1.0, 72.0, 720.0])); + assert_eq!(&src[cm.span.clone()], b"1 0 0 1 72 720 cm"); + assert_eq!(p.ops[4].operands[0].as_string().unwrap().bytes, b"Hello (world)"); + assert_eq!(p.ops[5].operands[0].as_array().unwrap().len(), 3); + assert_eq!(p.ops[3].name(0), Some(&b"F1"[..])); +} + +#[test] +fn round_trips_through_the_serializer() { + let src = b"0.5 g 10 10 100 50 re f /GS1 gs true false null 3 d0 [3 2] 0 d (x) ' 1 2 (y) \""; + let a = parse(src); + let b = parse(&serialize_ops(&a.ops)); + assert_eq!(a.ops.iter().map(|o| (&o.op, &o.operands)).collect::>(), b.ops.iter().map(|o| (&o.op, &o.operands)).collect::>()); +} + +#[test] +fn inline_images_stay_whole() { + let mut src = b"q 10 0 0 10 0 0 cm BI /W 2 /H 2 /CS /G /BPC 8 ID ".to_vec(); + src.extend_from_slice(&[0, b'E', b'I', 255]); + src.extend_from_slice(b"\nEI Q 1 g"); + let p = parse(&src); + let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"q"[..], b"cm", b"BI", b"Q", b"g"]); + let (d, data) = p.ops[2].inline.clone().unwrap(); + assert_eq!(d.int(b"W"), Some(2)); + assert_eq!(data, [0, b'E', b'I', 255], "an EI inside the data without whitespace before it is data"); + let again = parse(&serialize_ops(&p.ops)); + assert_eq!(again.ops[2].inline, p.ops[2].inline); +} + +#[test] +fn unfiltered_inline_image_keeps_whitespace_bounded_ei_pixels() { + let src = b"q 4 0 0 1 0 0 cm BI /W 4 /H 1 /BPC 8 /CS /G ID EI \nEI Q"; + let parsed = parse(src); + assert_eq!(parsed.ops[2].inline.as_ref().unwrap().1, b" EI "); + assert_eq!(parsed.ops.iter().map(|op| op.op.as_slice()).collect::>(), [b"q".as_slice(), b"cm", b"BI", b"Q"]); + let rewritten = serialize_ops(&parsed.ops); + assert_eq!(parse(&rewritten).ops[2].inline, parsed.ops[2].inline); +} + +fn inline_pixels(parameters: &[u8], pixels: &[u8]) -> Parsed { + let mut src = b"q BI ".to_vec(); + src.extend_from_slice(parameters); + src.extend_from_slice(b" ID "); + src.extend_from_slice(pixels); + src.extend_from_slice(b"\nEI Q"); + parse(&src) +} + +#[test] +fn unfiltered_inline_image_lengths_use_components_and_parameter_aliases() { + for (parameters, pixels) in [ + (b"/W 4 /H 1 /CS /G /BPC 8".as_slice(), b" EI ".as_slice()), + (b"/W 4 /H 1 /CS /G /BPC 8 /Filter null", b" EI "), + (b"/W 4 /H 1 /CS /G /BPC 8 /F []", b" EI "), + (b"/W 4 /Width 4 /H 1 /CS /G /BPC 8", b" EI "), + (b"/Width 4 /Height 1 /ColorSpace /DeviceGray /BitsPerComponent 8", b" EI "), + (b"/W 2 /H 1 /CS /RGB /BPC 8", b"\0 EI \xff"), + (b"/Width 2 /Height 1 /ColorSpace /DeviceRGB /BitsPerComponent 8", b"\0 EI \xff"), + (b"/W 1 /H 1 /CS /CMYK /BPC 8", b" EI "), + (b"/Width 1 /Height 1 /ColorSpace /DeviceCMYK /BitsPerComponent 8", b" EI "), + ] { + let parsed = inline_pixels(parameters, pixels); + assert_eq!(parsed.skipped, 0); + assert_eq!(parsed.ops.iter().map(|op| op.op.as_slice()).collect::>(), [b"q".as_slice(), b"BI", b"Q"]); + assert_eq!(parsed.ops[1].inline.as_ref().unwrap().1, pixels); + assert_eq!(parse(&serialize_ops(&parsed.ops)).ops[1].inline, parsed.ops[1].inline); + } +} + +#[test] +fn unfiltered_inline_image_rows_are_byte_aligned_for_every_sample_depth() { + // Two rows of two bytes each. Padding belongs to each row, not only the final row. + for (bits, width) in [(1, 9), (2, 5), (4, 3), (8, 2), (16, 1)] { + let parameters = format!("/W {width} /H 2 /BPC {bits} /CS /G"); + let parsed = inline_pixels(parameters.as_bytes(), b" EI "); + assert_eq!(parsed.ops[1].inline.as_ref().unwrap().1, b" EI ", "bits={bits}"); + assert_eq!(parsed.ops.len(), 3); + } +} + +#[test] +fn unfiltered_inline_masks_default_to_one_bit_and_indexed_images_have_one_component() { + for parameters in [b"/W 9 /H 2 /IM true".as_slice(), b"/Width 9 /Height 2 /ImageMask true /BitsPerComponent 1"] { + let parsed = inline_pixels(parameters, b" EI "); + assert_eq!(parsed.ops[1].inline.as_ref().unwrap().1, b" EI "); + assert_eq!(parsed.ops.len(), 3); + } + for name in ["Indexed", "I"] { + // A valid 256-entry RGB lookup table; samples are indices regardless of the base space. + let parameters = format!("/W 4 /H 1 /BPC 8 /CS [/{name} /DeviceRGB 255 <{}>]", "00".repeat(768)); + let parsed = inline_pixels(parameters.as_bytes(), b" EI "); + assert_eq!(parsed.ops[1].inline.as_ref().unwrap().1, b" EI "); + assert_eq!(parsed.ops.len(), 3); + // Indexed samples permit at most 8 bits, even though device spaces permit 16, so the + // length is unknown, and the renderer takes the second `EI`. + let invalid = parameters.replace("/W 4", "/W 2").replace("/BPC 8", "/BPC 16"); + let parsed = inline_pixels(invalid.as_bytes(), b" EI "); + assert_eq!(parsed.ops[1].inline.as_ref().unwrap().1, b" EI "); + } +} + +#[test] +fn unfiltered_inline_image_terminator_must_follow_its_samples() { + for separator in [b" ".as_slice(), b"\r\n", b"\0\t\x0c\n"] { + let mut src = b"BI /W 4 /H 1 /BPC 8 /CS /G ID EI ".to_vec(); + src.extend_from_slice(separator); + src.extend_from_slice(b"EI/Q q"); + let parsed = parse(&src); + assert_eq!(parsed.ops[0].inline.as_ref().unwrap().1, b" EI "); + assert_eq!(&src[parsed.ops[0].span.clone()], &src[..src.len() - b"/Q q".len()]); + } + let parsed = parse(b"BI /W 4 /H 1 /BPC 8 /CS /G ID EI \nEI"); + assert_eq!(parsed.ops[0].inline.as_ref().unwrap().1, b" EI "); + // EI is a keyword: a prefix of a larger token is not a terminator. + let parsed = parse(b"BI /W 4 /H 1 /BPC 8 /CS /G ID abcd EIx\nEI Q"); + assert_eq!(parsed.ops[0].inline.as_ref().unwrap().1, b"abcd EIx"); +} + +#[test] +fn filtered_unknown_and_damaged_inline_images_end_where_the_renderer_ends_them() { + for parameters in [ + b"/W 4 /H 1 /BPC 8 /CS /G /F /A85".as_slice(), + b"/W 4 /H 1 /BPC 8 /CS /ResourceColor", + b"/W 0 /H 1 /BPC 8 /CS /G", + b"/W -1 /H 1 /BPC 8 /CS /G", + b"/W 4 /H -1 /BPC 8 /CS /G", + b"/W 4 /H 1 /BPC 3 /CS /G", + b"/W 4 /H 1 /IM true /BPC 8", + b"/W 4 /H 1 /IM true /BPC /Bad", + b"/Width 4 /W 1 /H 1 /BPC 8 /CS /G", + b"/W 4 /H 1 /BPC 8 /CS /G /ImageMask false /IM true", + b"/W 9223372036854775807 /H 9223372036854775807 /BPC 16 /CS /CMYK", + b"/W 9223372036854775807 /H 2 /BPC 8 /CS /G", + ] { + // What follows the first `EI` (`!!~>`) does not read as content, so the renderer takes + // the later one. + let parsed = inline_pixels(parameters, b"! EI !!~>"); + let label = String::from_utf8_lossy(parameters); + assert_eq!(parsed.ops[1].inline.as_ref().unwrap().1, b"! EI !!~>", "{label}"); + assert_eq!(parsed.ops.iter().map(|op| op.op.as_slice()).collect::>(), [b"q".as_slice(), b"BI", b"Q"], "{label}"); + assert_eq!(parsed.ambiguous_images, 0, "{label}"); + } + // A short payload is read whole; without an `EI`, the renderer reads no image and this + // parser's end is a guess. + for (src, ambiguous) in [(b"BI /W 4 /H 1 /BPC 8 /CS /G ID abc\nEI Q".as_slice(), 0), (b"BI /W 4 /H 1 /BPC 8 /CS /G ID abc", 1)] { + let parsed = parse(src); + assert_eq!(parsed.ops[0].inline.as_ref().unwrap().1, b"abc"); + assert_eq!(parsed.ambiguous_images, ambiguous); + let mut reader = ops(src); + reader.by_ref().for_each(drop); + assert_eq!(reader.ambiguous_images(), ambiguous, "the streaming reader counts it too"); + } +} + +/// The renderer reads an image whose parameters this parser can't (a stray operand before `ID`): +/// the page draws an image `ops` doesn't hold, so it is counted as ambiguous rather than dropped +/// silently. +#[test] +fn an_inline_image_read_by_the_renderer_but_not_here_is_ambiguous() { + let parsed = parse(b"q BI /W 1 /H 1 /BPC 8 /CS /G 7 ID \x80 EI Q"); + assert_eq!(parsed.ops.iter().map(|op| op.op.as_slice()).collect::>(), [b"q".as_slice(), b"Q"]); + assert_eq!(parsed.ambiguous_images, 1); +} + +/// A zlib stream (one stored block) for 64 DeviceGray samples whose bytes hold " EI\n" at offset 41, +/// the way a stored deflate block copies incompressible samples verbatim (#3579). +const FLATE_WITH_EI: &str = "789c014000bfffed746159d50788f7ea604984cd08e00b4c8983207f56fe82c77ef8dfb8b9a3a586382045490a3120\ + 31206c2025630aaa4353e6813e233b96539c18bc3a2c5009e9d51b81"; + +#[test] +fn flate_inline_image_is_not_cut_at_an_ei_inside_its_compressed_bytes() { + let flate: Vec = (0..FLATE_WITH_EI.len()).step_by(2).map(|i| u8::from_str_radix(&FLATE_WITH_EI[i..i + 2], 16).unwrap()).collect(); + assert_eq!(flate.len(), 75); + assert_eq!(&flate[41..45], b" EI\n"); + for parameters in [b"/W 64 /H 1 /BPC 8 /CS /G /F /Fl".as_slice(), b"/W 64 /H 1 /BPC 8 /CS /G /F /Fl /L 75"] { + let mut src = b"q 200 0 0 200 100 300 cm BI ".to_vec(); + src.extend_from_slice(parameters); + src.extend_from_slice(b" ID "); + src.extend_from_slice(&flate); + src.extend_from_slice(b"\nEI Q BT /F1 24 Tf 72 200 Td (AFTER) Tj ET"); + let parsed = parse(&src); + let label = String::from_utf8_lossy(parameters); + assert_eq!(parsed.ops[2].inline.as_ref().unwrap().1.len(), 75, "{label}"); + let names: Vec<&[u8]> = parsed.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"q"[..], b"cm", b"BI", b"Q", b"BT", b"Tf", b"Td", b"Tj", b"ET"], "{label}"); + assert_eq!(parsed.skipped, 0, "{label}"); + assert_eq!(parse(&serialize_ops(&parsed.ops)).ops[2].inline, parsed.ops[2].inline, "{label}"); + } +} + +fn inline_pdf(content: &[u8], object_streams: bool) -> Vec { + use crate::cos::{Document, SaveOptions, Stream, write_full}; + + let mut doc = Document::new_empty(); + let pages = doc.get(doc.root().unwrap()).as_dict().unwrap().reference(b"Pages").unwrap(); + let contents = doc.add(Object::Stream(Stream::flate(Dict::new(), content))); + let page = doc.add(Object::Dict( + [ + (b"Type".to_vec(), Object::name("Page")), + (b"Parent".to_vec(), Object::Ref(pages)), + (b"MediaBox".to_vec(), Object::Array([0, 0, 4, 1].map(Object::Int).to_vec())), + (b"Resources".to_vec(), Object::Dict(Dict::new())), + (b"Contents".to_vec(), Object::Ref(contents)), + ] + .into_iter() + .collect(), + )); + doc.update_dict(pages, |dict| { + dict.set(b"Kids".to_vec(), Object::Array(vec![Object::Ref(page)])); + dict.set(b"Count".to_vec(), Object::Int(1)); + }) + .unwrap(); + write_full(&doc, &SaveOptions { object_streams, ..SaveOptions::default() }).unwrap() +} + +#[test] +fn rewritten_inline_image_pixels_survive_saving_and_reopening_a_pdf() { + use crate::cos::{Document, Object}; + use std::sync::Arc; + + let original = b"q 4 0 0 1 0 0 cm BI /W 4 /H 1 /BPC 8 /CS /G ID EI \nEI Q"; + let rewritten = serialize_ops(&parse(original).ops); + for object_streams in [false, true] { + let bytes = inline_pdf(&rewritten, object_streams); + let doc = Document::open(Arc::new(bytes.clone())).unwrap(); + assert!(doc.repair_log().is_empty()); + let pages = doc.get(doc.root().unwrap()).as_dict().unwrap().reference(b"Pages").unwrap(); + let pages = doc.get(pages); + let page = doc.resolve(&pages.as_dict().unwrap().get(b"Kids").unwrap().as_array().unwrap()[0]); + let content = doc.resolve(page.as_dict().unwrap().get(b"Contents").unwrap()); + let Object::Stream(stream) = &*content else { panic!("page content stream") }; + assert_eq!(parse(&stream.decoded_strict().unwrap()).ops[2].inline.as_ref().unwrap().1, b" EI "); + // Opt-in artifacts for independent readers/renderers; synthetic PDFs are never committed. + if let Some(dir) = std::env::var_os("PDFCRAFT_INLINE_FIXTURE_DIR") { + let dir = std::path::PathBuf::from(dir); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("original.pdf"), inline_pdf(original, false)).unwrap(); + std::fs::write(dir.join(if object_streams { "rewritten-packed.pdf" } else { "rewritten-classic.pdf" }), bytes).unwrap(); + } + } +} + +#[test] +fn damage_is_skipped_not_fatal() { + let p = parse(b"1 0 0 RG ) ] 10 20 m 30 40 l S (unterminated"); + let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"RG"[..], b"m", b"l", b"S"]); + assert!(p.skipped >= 2); + assert!(parse(b"").ops.is_empty()); + assert!(parse(b"BI /W 1").ops.len() == 1); +} + +/// Issue #1318: an inline image whose dictionary can't be read is skipped up to its `EI`, counted, +/// and what follows it is parsed. +#[test] +fn an_unreadable_inline_image_is_skipped_to_its_end() { + let src = b"q BI /W 4 H 4 ID aa EI Q BT (After) Tj ET"; + let p = parse(src); + let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"q"[..], b"Q", b"BT", b"Tj", b"ET"]); + assert_eq!(p.skipped, b"BI /W 4 H 4 ID aa EI".len()); + let control = parse(b"q BI /W 4 /H 4 ID aa EI Q BT (After) Tj ET"); + assert_eq!(control.skipped, 0); + assert_eq!(control.ops.len(), 6); +} + +/// Issue #1318, follow-up: without an `EI`, an unreadable inline image runs to the end of the +/// stream, and those bytes are counted. A readable one whose data is cut short is kept, data and +/// all, so nothing of it is skipped. +#[test] +fn an_unreadable_inline_image_without_ei_is_counted() { + let p = parse(b"q BI /W 4 H 4 ID aa"); + let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"q"[..]]); + assert_eq!(p.skipped, b"BI /W 4 H 4 ID aa".len()); + let control = parse(b"q BI /W 4 /H 4 ID aa"); + assert_eq!(control.skipped, 0); + let names: Vec<&[u8]> = control.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"q"[..], b"BI"]); + assert_eq!(control.ops[1].inline.as_ref().map(|(_, data)| data.as_slice()), Some(&b"aa"[..])); +} + +#[test] +fn matrices_compose_and_invert() { + let a = Matrix([2.0, 0.0, 0.0, 3.0, 10.0, 20.0]); + let b = Matrix([0.0, 1.0, -1.0, 0.0, 5.0, 0.0]); + let ab = a.then(&b); + let (x, y) = ab.apply(1.0, 1.0); + let (x1, y1) = a.apply(1.0, 1.0); + assert_eq!((x, y), b.apply(x1, y1)); + let inv = ab.invert().unwrap(); + let (u, v) = inv.apply(x, y); + assert!((u - 1.0).abs() < 1e-9 && (v - 1.0).abs() < 1e-9); + assert_eq!(Matrix([2.0, 0.0, 0.0, 2.0, 1.0, 1.0]).bbox([0.0, 0.0, 1.0, 1.0]), [1.0, 1.0, 3.0, 3.0]); + assert!(overlaps([0.0, 0.0, 10.0, 10.0], [5.0, 5.0, 20.0, 20.0], 0.0)); + assert!(!overlaps([0.0, 0.0, 10.0, 10.0], [10.0, 0.0, 20.0, 10.0], 0.0)); +} + +#[test] +fn pieces_parse_as_one_stream_and_splice_back() { + // An operator's operands end one piece and its keyword starts the next (§7.8.2). + let pieces = Pieces::join(&[&b"BT 72 700 Td [(After) -20 (wards)]"[..], b"TJ ET", b"q Q"]); + assert_eq!(pieces.len(), 3); + let ops = pieces.parse().ops; + let names: Vec<&[u8]> = ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"BT"[..], b"Td", b"TJ", b"ET", b"q", b"Q"]); + assert_eq!(pieces.pieces_of(&ops[2]), (0, 1)); + assert_eq!(pieces.pieces_of(&ops[3]), (1, 1)); + assert_eq!(pieces.piece_of(usize::MAX), 2); + // Nothing changed: the pieces come back byte for byte. + assert_eq!(pieces.splice([]), [&b"BT 72 700 Td [(After) -20 (wards)]"[..], b"TJ ET", b"q Q"]); + // Replacing the split TJ rewrites both pieces it spans; the third is untouched. + let out = pieces.splice([(ops[2].span.clone(), b"(New) Tj".to_vec())]); + assert_eq!(out, [&b"BT 72 700 Td (New) Tj"[..], b" ET", b"q Q"]); + // Removing it leaves no stray operands or keyword. + let out = pieces.splice([(ops[2].span.clone(), Vec::new())]); + assert_eq!(out, [&b"BT 72 700 Td "[..], b" ET", b"q Q"]); + // Out-of-range or reversed edits are clamped, never a panic. + let out = pieces.splice([(std::ops::Range { start: 10, end: 5 }, b"x".to_vec()), (1000..2000, Vec::new())]); + assert_eq!(out.len(), 3); + assert!(Pieces::join::<&[u8]>(&[]).splice([(0..1, b"x".to_vec())]).is_empty()); +} + +#[test] +fn stream_boundary_comment_matches_space_concatenation() { + for eol in [b"\n".as_slice(), b"\r", b"\r\n"] { + let mut tail = b"(HIDDEN) Tj".to_vec(); + tail.extend(eol); + tail.extend(b"(LIVE) Tj"); + let pieces = Pieces::join(&[b"BT % comment".as_slice(), b"(ALSO HIDDEN) Tj", &tail, b"ET"]); + let parsed = pieces.parse(); + let shown: Vec<_> = parsed.ops.iter().filter(|op| op.is("Tj")).collect(); + assert_eq!(shown.len(), 1); + assert_eq!(shown[0].operands[0].as_string().unwrap().bytes, b"LIVE"); + } +} + +#[test] +fn comment_edges_terminate_at_next_piece_byte_zero() { + for eol in [b"\n".as_slice(), b"\r", b"\r\n"] { + let mut next = eol.to_vec(); + next.extend_from_slice(b"q Q"); + let pieces = Pieces::join(&[b"% outgoing".as_slice(), next.as_slice()]); + let parsed = pieces.parse(); + assert_eq!(pieces.comment_edges(&parsed), vec![(false, true), (true, false)], "EOL {eol:?}"); + assert_eq!(parsed.ops.iter().map(|op| op.op.as_slice()).collect::>(), vec![b"q".as_slice(), b"Q"]); + } +} + +#[test] +fn comment_edges_span_empty_pieces_and_end_of_content() { + let pieces = Pieces::join(&[b"q % outgoing".as_slice(), b"", b"", b"ignored\nQ"]); + let parsed = pieces.parse(); + assert_eq!(pieces.comment_edges(&parsed), vec![(false, true), (true, true), (true, true), (true, false)]); + assert_eq!(parsed.ops.iter().map(|op| op.op.as_slice()).collect::>(), vec![b"q".as_slice(), b"Q"]); + + let pieces = Pieces::join(&[b"% outgoing".as_slice(), b"", b"still ignored"]); + let parsed = pieces.parse(); + assert_eq!(pieces.comment_edges(&parsed), vec![(false, true), (true, true), (true, true)]); + assert!(parsed.ops.is_empty()); + + let pieces = Pieces::join::<&[u8]>(&[]); + assert!(pieces.comment_edges(&pieces.parse()).is_empty()); +} + +#[test] +fn comment_edges_ignore_percent_inside_operands_and_image_data() { + for first in [ + b"(literal % not a comment) Tj".as_slice(), + b"/Percent#25Name MP", + b"[(literal % not a comment)] TJ", + b"/P << /ActualText (literal % not a comment) >> BDC EMC", + b"BI /W 1 /H 1 /BPC 8 /CS /G ID % EI", + ] { + let pieces = Pieces::join(&[first, b"q Q"]); + let parsed = pieces.parse(); + assert_eq!(pieces.comment_edges(&parsed), vec![(false, false), (false, false)], "{first:?}"); + assert_eq!(parsed.ops.last().unwrap().op, b"Q"); + assert_eq!(parsed.skipped, 0); + } +} + +#[test] +fn compound_operand_comment_split_is_owned_by_spanning_operator() { + for parts in [[b"[(A) % ignored".as_slice(), b"still ignored\n(B)] TJ"], [b"/P << /MCID 0 % ignored".as_slice(), b"still ignored\n>> BDC"]] { + let pieces = Pieces::join(&parts); + let parsed = pieces.parse(); + // The object lexer consumes this nested comment. Its containing operator spans + // both pieces, which forces both to be rewritten together by the redactor. + assert_eq!(pieces.comment_edges(&parsed), vec![(false, false), (false, false)]); + assert_eq!(parsed.ops.len(), 1); + assert_eq!(pieces.pieces_of(&parsed.ops[0]), (0, 1)); + assert_eq!(parsed.skipped, 0); + } +} + +#[test] +fn comment_edges_do_not_confuse_local_comments_with_boundary_comments() { + let pieces = Pieces::join(&[b"% local\nq % crossing".as_slice(), b"ignored\n% local again\nQ\n% final local\n", b"q Q"]); + let parsed = pieces.parse(); + assert_eq!(pieces.comment_edges(&parsed), vec![(false, true), (true, false), (false, false)]); + assert_eq!(parsed.ops.iter().map(|op| op.op.as_slice()).collect::>(), vec![b"q".as_slice(), b"Q", b"q", b"Q"]); +} + +#[test] +fn comment_edge_storage_depends_on_piece_count_not_comment_count() { + let mut first = b"%\n".repeat(16_384); + first.extend_from_slice(b"q % crossing"); + let pieces = Pieces::join(&[first.as_slice(), b"ignored\nQ"]); + let parsed = pieces.parse(); + assert_eq!(pieces.comment_edges(&parsed), vec![(false, true), (true, false)]); + assert_eq!(parsed.ops.len(), 2); + // Also exercise public parsing without piece-boundary metadata. + assert_eq!(parse(&first).ops.len(), 1); +} + +#[test] +fn piece_lookup_preserves_exact_boundary_and_empty_piece_mapping() { + // Joined layout: qQ; separator belongs to prior piece. + let pieces = Pieces::join(&[b"q".as_slice(), b"", b"Q"]); + for (at, expected) in [(0, 0), (1, 0), (2, 1), (3, 2), (4, 2), (5, 2), (usize::MAX, 2)] { + assert_eq!(pieces.piece_of(at), expected, "position {at}"); + } + let empty = Pieces::join::<&[u8]>(&[]); + assert_eq!(empty.piece_of(0), 0); + assert_eq!(empty.piece_of(usize::MAX), 0); +} + +#[test] +fn inline_image_terminator_without_preceding_whitespace() { + let cases: &[(&[u8], &[u8])] = &[ + (b"BI /W 4 /H 1 /BPC 8 /CS /G /F /AHx ID 2060A0E0>EI Q BT /F1 18 Tf (After) Tj ET", b"2060A0E0>"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G /Filter /ASCIIHexDecode ID 2060A0E0>EI Q BT /F1 18 Tf (After) Tj ET", b"2060A0E0>"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G /F [/AHx] ID 2060A0E0>EI Q BT /F1 18 Tf (After) Tj ET", b"2060A0E0>"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G /F [/AHx /RL] ID 2060A0E0>EI Q BT /F1 18 Tf (After) Tj ET", b"2060A0E0>"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G /F /A85 ID 5s,^g~>EI Q BT /F1 18 Tf (After) Tj ET", b"5s,^g~>"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G /Filter /ASCII85Decode ID 5s,^g~>EI Q BT /F1 18 Tf (After) Tj ET", b"5s,^g~>"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G /F [/A85 /RL] ID 5s,^g~>EI Q BT /F1 18 Tf (After) Tj ET", b"5s,^g~>"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G ID ab()EI Q BT /F1 18 Tf (After) Tj ET", b"ab()"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G ID ab[]EI Q BT /F1 18 Tf (After) Tj ET", b"ab[]"), + (b"BI /W 4 /H 1 /BPC 8 /CS /G ID \x20\x60\xa0\xe0EI Q BT /F1 18 Tf (After) Tj ET", b"\x20\x60\xa0\xe0"), + ]; + + for (src, expected_data) in cases { + let p = parse(src); + assert_eq!(p.skipped, 0, "stream should have 0 skipped bytes: {}", String::from_utf8_lossy(src)); + let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!( + names, + [&b"BI"[..], b"Q", b"BT", b"Tf", b"Tj", b"ET"], + "operators after inline image must be preserved: {}", + String::from_utf8_lossy(src) + ); + let (_, img) = p.ops[0].inline.as_ref().unwrap(); + assert_eq!(img, expected_data, "inline image data must match: {}", String::from_utf8_lossy(src)); + } +} + +#[test] +fn ascii85_inline_image_does_not_terminate_at_gt_ei_inside_payload() { + for src in [ + b"BI /W 4 /H 1 /BPC 8 /CS /G /F /A85 ID !!!!>EI !!~>\nEI Q BT /F1 18 Tf (After) Tj ET".as_slice(), + b"BI /W 4 /H 1 /BPC 8 /CS /G /F [/A85 /AHx] ID !!!!>EI !!~>\nEI Q BT /F1 18 Tf (After) Tj ET".as_slice(), + ] { + let p = parse(src); + assert_eq!(p.skipped, 0); + let names: Vec<&[u8]> = p.ops.iter().map(|o| o.op.as_slice()).collect(); + assert_eq!(names, [&b"BI"[..], b"Q", b"BT", b"Tf", b"Tj", b"ET"]); + let (_, img) = p.ops[0].inline.as_ref().unwrap(); + assert_eq!(img, b"!!!!>EI !!~>"); + } +} + +/// The operators the renderer's tokenizer reads from `data`. +fn renderer_operators(data: &[u8]) -> Vec> { + let mut iter = hayro_syntax::content::UntypedIter::new(data); + let mut out = Vec::new(); + while let Some(instruction) = iter.next() { + out.push(instruction.operator.as_ref().to_vec()); + } + out +} + +/// Inline images end where the renderer ends them, so both read the same operators after one: +/// an `EI` without whitespace before it in the data, upper-case text ending in "EI" after the +/// image, and an `EI` inside compressed bytes (#3579). +#[test] +fn inline_images_end_where_the_renderer_ends_them() { + let flate: Vec = (0..FLATE_WITH_EI.len()).step_by(2).map(|i| u8::from_str_radix(&FLATE_WITH_EI[i..i + 2], 16).unwrap()).collect(); + let mut stored = b"q BI /W 64 /H 1 /BPC 8 /CS /G /F /Fl ID ".to_vec(); + stored.extend_from_slice(&flate); + stored.extend_from_slice(b"\nEI Q BT /F1 24 Tf 72 200 Td (AFTER) Tj ET"); + let fixtures: [&[u8]; 6] = [ + b"BI /W 1 /H 1 /CS /G /BPC 8 /F /AHx ID 7FEI 1 0 0 1 200 0 cm EI BT /F1 10 Tf 10 100 Td (SECRET) Tj ET", + b"BI /W 1 /H 1 /CS /G /BPC 8 /F /AHx ID 7F> EI BT /F1 10 Tf 10 100 Td (ZWEI) Tj (EIN FREI BEI) Tj ET", + b"q BI /W 2 /H 1 /CS /G /BPC 8 /F /AHx ID 7F7F> EI Q BT (DREI) Tj ET BI /W 1 /H 1 /CS /G /BPC 8 /F /AHx ID 00> EI", + b"BI /W 4 /H 1 /BPC 8 /CS /G /F /A85 ID ! EI !!~>\nEI Q", + b"q BI /W 4 /H 1 /BPC 8 /CS /G ID EI \nEI Q", + &stored, + ]; + for data in fixtures { + let parsed = parse(data); + let ours: Vec> = parsed.ops.iter().map(|op| op.op.clone()).collect(); + assert_eq!(ours, renderer_operators(data), "{}", String::from_utf8_lossy(data)); + assert_eq!((parsed.skipped, parsed.ambiguous_images), (0, 0), "{}", String::from_utf8_lossy(data)); + } +} + +#[test] +fn invert_accepts_small_scale_and_rejects_singular() { + let m = Matrix([1e-7, 0., 0., 1e-7, 0., 0.]); + let inv = m.invert().expect("a uniform 1e-7 scale is invertible"); + let id = m.then(&inv).0; + for (got, want) in id.iter().zip(Matrix::IDENTITY.0) { + assert!((got - want).abs() < 1e-9, "{id:?}"); + } + assert!(Matrix([1., 2., 2., 4., 0., 0.]).invert().is_none()); + assert!(Matrix([f64::NAN, 0., 0., 1., 0., 0.]).invert().is_none()); + // A subnormal scale passes the relative check but its inverse overflows (CodeRabbit on #5703). + assert!(Matrix([1e-310, 0., 0., 1., 0., 0.]).invert().is_none()); +} diff --git a/crates/pe/Cargo.toml b/crates/pe/Cargo.toml new file mode 100644 index 000000000..513c39055 --- /dev/null +++ b/crates/pe/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "craft-pe" +description = "PE/COFF headers from hostile bytes, from Craft Anything: the DOS stub pointer, COFF header, PE32/PE32+ optional header, data directories, section table, RVA-to-file mapping and the Rich header. Pure Rust, no I/O." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +thiserror = { workspace = true } +# `serde`: `Serialize` on `PeSection`, for apps that report section tables as JSON. +serde = { workspace = true, optional = true } + +[features] +serde = ["dep:serde"] + +[dev-dependencies] +serde_json = { workspace = true } diff --git a/crates/pe/src/bytes.rs b/crates/pe/src/bytes.rs new file mode 100644 index 000000000..63aaeed12 --- /dev/null +++ b/crates/pe/src/bytes.rs @@ -0,0 +1,35 @@ +//! Bounds-checked little-endian field reads for the header parser: out of range is `None`. + +#[derive(Debug, Clone, Copy)] +pub(crate) struct Reader<'a>(&'a [u8]); + +impl<'a> Reader<'a> { + pub(crate) fn new(data: &'a [u8]) -> Self { + Self(data) + } + + /// `len` bytes at `off`. + pub(crate) fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> { + self.0.get(off..off.checked_add(len)?) + } + + fn field(&self, off: usize) -> Option<[u8; N]> { + self.0.get(off..)?.first_chunk::().copied() + } + + pub(crate) fn u8(&self, off: usize) -> Option { + self.field::<1>(off).map(|[b]| b) + } + + pub(crate) fn u16_le(&self, off: usize) -> Option { + Some(u16::from_le_bytes(self.field(off)?)) + } + + pub(crate) fn u32_le(&self, off: usize) -> Option { + Some(u32::from_le_bytes(self.field(off)?)) + } + + pub(crate) fn u64_le(&self, off: usize) -> Option { + Some(u64::from_le_bytes(self.field(off)?)) + } +} diff --git a/crates/pe/src/lib.rs b/crates/pe/src/lib.rs new file mode 100644 index 000000000..f2294359b --- /dev/null +++ b/crates/pe/src/lib.rs @@ -0,0 +1,265 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! PE/COFF headers (`.exe`, `.dll`, `.sys`, `.efi`…) from untrusted bytes. +//! +//! [`headers`] reads the DOS stub pointer, the COFF header, the PE32 or PE32+ optional header, +//! its data directories and the section table; [`PeHeaders::rva_to_offset`] maps addresses +//! through it. [`rich_header`] decodes the linker's Rich header. Nothing here panics on hostile +//! input: required fields that are missing are errors, optional ones read as zero, the +//! directory count is bounded by the complete entries in the optional header, and at most +//! [`MAX_SECTIONS`] sections are read. +//! +//! From Craft Anything (`crates/formats/src/pe.rs`), where the import, export, resource and +//! program-model code built on these headers stays. With the `serde` feature, [`PeSection`] is +//! `Serialize`. + +mod bytes; + +use crate::bytes::Reader; + +/// Sections read from the section table; the rest are ignored (the loader allows 96). +pub const MAX_SECTIONS: usize = 96; + +/// The names of the sixteen optional-header data directories, by index. +pub const DATA_DIRECTORY_NAMES: [&str; 16] = [ + "export", + "import", + "resource", + "exception", + "security", + "basereloc", + "debug", + "architecture", + "globalptr", + "tls", + "load_config", + "bound_import", + "iat", + "delay_import", + "clr_runtime", + "reserved", +]; + +/// Why [`headers`] gave up. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum PeError { + /// A required header ends before the file does. + #[error("the file ends early ({0})")] + Truncated(&'static str), + /// A signature or magic number is wrong. + #[error("the file is malformed ({0})")] + Malformed(String), +} + +/// One section table entry. +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +pub struct PeSection { + /// The 8-byte name up to its first NUL, decoded lossily. + pub name: String, + pub virtual_size: u32, + pub rva: u32, + pub raw_size: u32, + pub raw_offset: u32, + pub characteristics: u32, +} + +/// The COFF and optional header fields and the section table. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PeHeaders { + /// File offset of the `PE\0\0` signature (`e_lfanew`). + pub pe_offset: usize, + pub machine: u16, + pub timestamp: u32, + pub characteristics: u16, + pub pe32_plus: bool, + pub image_base: u64, + pub entry_rva: u32, + pub size_of_headers: u32, + pub subsystem: u16, + pub dll_characteristics: u16, + /// Major and minor linker version. + pub linker: (u8, u8), + /// `(rva, size)` for each data directory present, at most 16. + pub data_dirs: Vec<(u32, u32)>, + pub sections: Vec, +} + +impl PeHeaders { + /// RVA → file offset through the section table (headers map 1:1). + pub fn rva_to_offset(&self, rva: u32) -> Option { + for s in &self.sections { + let size = s.virtual_size.max(s.raw_size); + if rva >= s.rva && rva - s.rva < size { + let delta = rva - s.rva; + if delta >= s.raw_size { + return None; + } + return usize::try_from(s.raw_offset.checked_add(delta)?).ok(); + } + } + if rva < self.size_of_headers { usize::try_from(rva).ok() } else { None } + } + + /// The virtual address of `rva` in the loaded image, or `None` when it overflows (or, for + /// PE32, leaves the 32-bit address space). + pub fn image_va(&self, rva: u64) -> Option { + let rva = u32::try_from(rva).ok()?; + let address = self.image_base.checked_add(u64::from(rva))?; + (self.pe32_plus || address <= u64::from(u32::MAX)).then_some(address) + } + + /// Bytes remaining in the selected RVA mapping before file offsets stop being contiguous. + pub fn rva_file_len(&self, rva: u32) -> Option { + let base = self.rva_to_offset(rva)?; + let next_section_distance = |count: usize| { + self.sections + .iter() + .take(count) + .filter(|s| s.virtual_size.max(s.raw_size) > 0) + .filter_map(|s| s.rva.checked_sub(rva)) + .filter(|&distance| distance > 0) + .min() + .unwrap_or(u32::MAX) + }; + match self.sections.iter().enumerate().find(|(_, s)| rva.checked_sub(s.rva).is_some_and(|delta| delta < s.virtual_size.max(s.raw_size))) { + Some((index, s)) => { + rva.checked_sub(s.rva).and_then(|delta| s.raw_size.checked_sub(delta)).map(|len| len.min(next_section_distance(index))) + } + None => self.size_of_headers.checked_sub(rva).map(|len| len.min(next_section_distance(self.sections.len()))), + } + .map(u64::from) + .and_then(|len| { + let address_end = u64::from(u32::MAX) + 1; + let remaining_rvas = address_end.checked_sub(u64::from(rva))?; + let remaining_offsets = address_end.checked_sub(u64::try_from(base).ok()?)?; + usize::try_from(len.min(remaining_rvas).min(remaining_offsets)).ok() + }) + } + + /// Data directory `i` (see [`DATA_DIRECTORY_NAMES`]) when present and non-empty. + pub fn dir(&self, i: usize) -> Option<(u32, u32)> { + self.data_dirs.get(i).copied().filter(|(rva, size)| *rva != 0 && *size != 0) + } +} + +/// True when bytes start with MZ and point at a `PE\0\0` signature. +pub fn has_pe_header(data: &[u8]) -> bool { + let r = Reader::new(data); + r.u32_le(0x3c).and_then(|o| r.slice(o as usize, 4)) == Some(b"PE\0\0") +} + +/// Parse the DOS stub pointer, COFF header, optional header and section table. +pub fn headers(data: &[u8]) -> Result { + let r = Reader::new(data); + if !data.starts_with(b"MZ") { + return Err(PeError::Malformed("no MZ signature".into())); + } + let pe = r.u32_le(0x3c).ok_or(PeError::Truncated("DOS header"))? as usize; + if r.slice(pe, 4) != Some(b"PE\0\0") { + return Err(PeError::Malformed("no PE signature".into())); + } + let coff = pe + 4; + let t = |o: Option| o.ok_or(PeError::Truncated("COFF header")); + let machine = t(r.u16_le(coff))?; + let nsections = usize::from(t(r.u16_le(coff + 2))?); + let timestamp = r.u32_le(coff + 4).unwrap_or(0); + let opt_size = usize::from(t(r.u16_le(coff + 16))?); + let characteristics = t(r.u16_le(coff + 18))?; + let opt = coff + 20; + let optional = Reader::new(r.slice(opt, opt_size).ok_or(PeError::Truncated("optional header"))?); + let magic = optional.u16_le(0).ok_or(PeError::Truncated("optional header"))?; + let pe32_plus = match magic { + 0x20b => true, + 0x10b => false, + m => return Err(PeError::Malformed(format!("optional header magic {m:#x}"))), + }; + let (dd, count_off) = if pe32_plus { (112, 108) } else { (96, 92) }; + if opt_size < dd { + return Err(PeError::Truncated("fixed optional header fields")); + } + let image_base = if pe32_plus { optional.u64_le(24) } else { optional.u32_le(28).map(u64::from) }.unwrap_or(0); + let count = (optional.u32_le(count_off).unwrap_or(0) as usize).min(16).min((opt_size - dd) / 8); + let data_dirs = (0..count).map(|i| (optional.u32_le(dd + i * 8).unwrap_or(0), optional.u32_le(dd + i * 8 + 4).unwrap_or(0))).collect(); + let sh = opt + opt_size; + let mut sections = Vec::new(); + for i in 0..nsections.min(MAX_SECTIONS) { + let o = sh + i * 40; + let Some(name) = r.slice(o, 8) else { break }; + let name = String::from_utf8_lossy(name.split(|&c| c == 0).next().unwrap_or(&[])).into_owned(); + sections.push(PeSection { + name, + virtual_size: r.u32_le(o + 8).unwrap_or(0), + rva: r.u32_le(o + 12).unwrap_or(0), + raw_size: r.u32_le(o + 16).unwrap_or(0), + raw_offset: r.u32_le(o + 20).unwrap_or(0), + characteristics: r.u32_le(o + 36).unwrap_or(0), + }); + } + Ok(PeHeaders { + pe_offset: pe, + machine, + timestamp, + characteristics, + pe32_plus, + image_base, + entry_rva: optional.u32_le(16).unwrap_or(0), + size_of_headers: optional.u32_le(60).unwrap_or(0), + subsystem: optional.u16_le(68).unwrap_or(0), + dll_characteristics: optional.u16_le(70).unwrap_or(0), + linker: (optional.u8(2).unwrap_or(0), optional.u8(3).unwrap_or(0)), + data_dirs, + sections, + }) +} + +/// The optional header's subsystem as a short name (`windows-gui`, `efi-application`…), or +/// `unknown`. +pub fn subsystem_name(s: u16) -> &'static str { + match s { + 1 => "native", + 2 => "windows-gui", + 3 => "windows-console", + 7 => "posix-console", + 9 => "windows-ce", + 10 => "efi-application", + 11 => "efi-boot-driver", + 12 => "efi-runtime-driver", + 14 => "xbox", + 16 => "windows-boot", + _ => "unknown", + } +} + +/// Decoded Rich header entries between the DOS stub and `pe_offset`: (product id, build, count). +/// At most 256 entries. +pub fn rich_header(data: &[u8], pe_offset: usize) -> Option> { + let r = Reader::new(data); + let region = data.get(0x40..pe_offset.min(data.len()))?; + let rich = region.windows(4).position(|w| w == b"Rich")? + 0x40; + let key = r.u32_le(rich + 4)?; + // Walk back to "DanS". + let mut pos = rich; + let mut start = None; + while pos >= 0x44 { + pos -= 4; + if r.u32_le(pos)? ^ key == 0x536e_6144 { + start = Some(pos); + break; + } + } + let start = start?; + let mut out = Vec::new(); + let mut o = start + 16; + while o + 8 <= rich && out.len() < 256 { + let id = r.u32_le(o)? ^ key; + let count = r.u32_le(o + 4)? ^ key; + out.push(((id >> 16) as u16, (id & 0xffff) as u16, count)); + o += 8; + } + Some(out) +} + +#[cfg(test)] +mod tests; diff --git a/crates/pe/src/tests.rs b/crates/pe/src/tests.rs new file mode 100644 index 000000000..237d8fa30 --- /dev/null +++ b/crates/pe/src/tests.rs @@ -0,0 +1,263 @@ +//! Header, mapping and Rich header tests. `header_rvas_stop_before_file_padding` is from Craft +//! Anything's `pe.rs`; the image builder here is a small stand-in for its testkit's `pe64`. + +use super::*; + +const PE_OFFSET: usize = 0xc0; +const OPT: usize = PE_OFFSET + 24; +const OPT_SIZE: usize = 112 + 16 * 8; +const SECTIONS: usize = OPT + OPT_SIZE; +const BASE: u64 = 0x1_4000_0000; + +fn put(data: &mut [u8], at: usize, bytes: &[u8]) { + data[at..at + bytes.len()].copy_from_slice(bytes); +} + +/// A PE32+ console image with `.text` at RVA 0x1000 (file 0x400) and `.rdata` at RVA 0x2000 +/// (file 0x600), an import directory, and a Rich header with two entries. +fn pe64() -> Vec { + let mut d = vec![0u8; 0x800]; + put(&mut d, 0, b"MZ"); + put(&mut d, 0x3c, &(PE_OFFSET as u32).to_le_bytes()); + rich(&mut d, 0x5a5a_1234, &[(0x0104, 30_729, 7), (0x00ff, 40_000, 1)]); + put(&mut d, PE_OFFSET, b"PE\0\0"); + let coff = PE_OFFSET + 4; + put(&mut d, coff, &0x8664u16.to_le_bytes()); + put(&mut d, coff + 2, &2u16.to_le_bytes()); + put(&mut d, coff + 4, &0x6000_0000u32.to_le_bytes()); + put(&mut d, coff + 16, &(OPT_SIZE as u16).to_le_bytes()); + put(&mut d, coff + 18, &0x22u16.to_le_bytes()); + put(&mut d, OPT, &0x20bu16.to_le_bytes()); + d[OPT + 2] = 14; + d[OPT + 3] = 36; + put(&mut d, OPT + 16, &0x1010u32.to_le_bytes()); + put(&mut d, OPT + 24, &BASE.to_le_bytes()); + put(&mut d, OPT + 60, &0x400u32.to_le_bytes()); + put(&mut d, OPT + 68, &3u16.to_le_bytes()); + put(&mut d, OPT + 70, &0x8160u16.to_le_bytes()); + put(&mut d, OPT + 108, &16u32.to_le_bytes()); + put(&mut d, OPT + 112 + 8, &0x2000u32.to_le_bytes()); + put(&mut d, OPT + 112 + 12, &0x28u32.to_le_bytes()); + for (i, (name, rva, raw)) in [(&b".text"[..], 0x1000u32, 0x400u32), (&b".rdata"[..], 0x2000, 0x600)].into_iter().enumerate() { + let o = SECTIONS + i * 40; + put(&mut d, o, name); + put(&mut d, o + 8, &0x180u32.to_le_bytes()); + put(&mut d, o + 12, &rva.to_le_bytes()); + put(&mut d, o + 16, &0x200u32.to_le_bytes()); + put(&mut d, o + 20, &raw.to_le_bytes()); + put(&mut d, o + 36, &0x6000_0020u32.to_le_bytes()); + } + d +} + +/// Write a Rich header (`DanS`, three zero pads, entries, `Rich`, key) at 0x80, XOR-masked. +fn rich(d: &mut [u8], key: u32, entries: &[(u16, u16, u32)]) { + let mut o = 0x80; + for word in [0x536e_6144u32, 0, 0, 0] { + put(d, o, &(word ^ key).to_le_bytes()); + o += 4; + } + for &(product, build, count) in entries { + put(d, o, &(((u32::from(product)) << 16 | u32::from(build)) ^ key).to_le_bytes()); + put(d, o + 4, &(count ^ key).to_le_bytes()); + o += 8; + } + put(d, o, b"Rich"); + put(d, o + 4, &key.to_le_bytes()); +} + +/// Every truncation and a spread of single-byte corruptions. +fn mutations(sample: &[u8]) -> Vec> { + let mut out: Vec> = (0..sample.len()).step_by(7).map(|len| sample[..len].to_vec()).collect(); + for i in (0..sample.len()).step_by(3) { + for v in [0x00, 0x7f, 0xff] { + let mut m = sample.to_vec(); + m[i] = v; + out.push(m); + } + } + out +} + +#[test] +fn reads_coff_optional_header_and_sections() { + let d = pe64(); + assert!(has_pe_header(&d)); + let h = headers(&d).unwrap(); + assert_eq!(h.pe_offset, PE_OFFSET); + assert_eq!(h.machine, 0x8664); + assert_eq!(h.timestamp, 0x6000_0000); + assert_eq!(h.characteristics, 0x22); + assert!(h.pe32_plus); + assert_eq!(h.image_base, BASE); + assert_eq!(h.entry_rva, 0x1010); + assert_eq!(h.size_of_headers, 0x400); + assert_eq!(subsystem_name(h.subsystem), "windows-console"); + assert_eq!(h.dll_characteristics, 0x8160); + assert_eq!(h.linker, (14, 36)); + assert_eq!(h.data_dirs.len(), 16); + assert_eq!(h.dir(1), Some((0x2000, 0x28))); + assert_eq!(DATA_DIRECTORY_NAMES[1], "import"); + assert_eq!(h.dir(0), None); + assert_eq!(h.dir(16), None); + let names: Vec<&str> = h.sections.iter().map(|s| s.name.as_str()).collect(); + assert_eq!(names, [".text", ".rdata"]); + assert_eq!(h.sections[1].characteristics, 0x6000_0020); +} + +#[test] +fn pe32_reads_a_32_bit_image_base() { + let mut d = pe64(); + put(&mut d, OPT, &0x10bu16.to_le_bytes()); + put(&mut d, OPT + 28, &0x0040_0000u32.to_le_bytes()); + put(&mut d, OPT + 92, &2u32.to_le_bytes()); + let h = headers(&d).unwrap(); + assert!(!h.pe32_plus); + assert_eq!(h.image_base, 0x0040_0000); + assert_eq!(h.data_dirs.len(), 2); + assert_eq!(h.image_va(0x1000), Some(0x0040_1000)); + let mut high = h.clone(); + high.image_base = 0xffff_f000; + assert_eq!(high.image_va(0x1000), None, "a PE32 address cannot leave 32 bits"); +} + +#[test] +fn bad_signatures_and_short_headers_are_errors() { + let d = pe64(); + assert_eq!(headers(b"ZM"), Err(PeError::Malformed("no MZ signature".into()))); + assert_eq!(headers(b"MZ"), Err(PeError::Truncated("DOS header"))); + let mut no_pe = d.clone(); + no_pe[PE_OFFSET] = b'X'; + assert!(!has_pe_header(&no_pe)); + assert_eq!(headers(&no_pe), Err(PeError::Malformed("no PE signature".into()))); + assert_eq!(headers(&d[..PE_OFFSET + 6]), Err(PeError::Truncated("COFF header"))); + assert_eq!(headers(&d[..OPT + 10]), Err(PeError::Truncated("optional header"))); + let mut magic = d.clone(); + put(&mut magic, OPT, &0x107u16.to_le_bytes()); + assert_eq!(headers(&magic), Err(PeError::Malformed("optional header magic 0x107".into()))); + assert_eq!(headers(&magic).unwrap_err().to_string(), "the file is malformed (optional header magic 0x107)"); + assert_eq!(PeError::Truncated("COFF header").to_string(), "the file ends early (COFF header)"); + let mut empty_optional = d.clone(); + put(&mut empty_optional, PE_OFFSET + 20, &0u16.to_le_bytes()); + assert_eq!(headers(&empty_optional), Err(PeError::Truncated("optional header"))); +} + +#[test] +fn a_section_table_cut_short_keeps_the_complete_entries() { + let d = pe64(); + let h = headers(&d[..SECTIONS + 40 + 7]).unwrap(); + assert_eq!(h.sections.len(), 1); + let mut many = d.clone(); + put(&mut many, PE_OFFSET + 6, &u16::MAX.to_le_bytes()); + many.resize(SECTIONS + 40 * 200, 0); + assert_eq!(headers(&many).unwrap().sections.len(), MAX_SECTIONS); +} + +#[test] +fn header_rvas_stop_before_file_padding() { + let mut pe = pe64(); + let first = headers(&pe).unwrap().sections[0].raw_offset; + let opt = Reader::new(&pe).u32_le(0x3c).unwrap() as usize + 24; + let size_headers = 0x200_u32; + pe[opt + 60..opt + 64].copy_from_slice(&size_headers.to_le_bytes()); + let coff = opt - 20; + let section = opt + usize::from(Reader::new(&pe).u16_le(coff + 16).unwrap()); + pe[section + 20..section + 24].copy_from_slice(&(first + 0x400).to_le_bytes()); + let h = headers(&pe).unwrap(); + assert_eq!(h.rva_to_offset(size_headers - 1), Some(0x1ff)); + assert_eq!(h.rva_to_offset(size_headers), None); + assert_eq!(h.rva_to_offset(h.sections[0].rva), Some((first + 0x400) as usize)); +} + +#[test] +fn rvas_map_through_sections_and_stop_at_raw_data() { + let h = headers(&pe64()).unwrap(); + assert_eq!(h.rva_to_offset(0x10), Some(0x10)); + assert_eq!(h.rva_to_offset(0x1010), Some(0x410)); + assert_eq!(h.rva_to_offset(0x2000), Some(0x600)); + assert_eq!(h.rva_to_offset(0x1000 + 0x200), None, "past the raw data in the virtual tail"); + assert_eq!(h.rva_to_offset(0x9000), None); + assert_eq!(h.rva_file_len(0x1010), Some(0x1f0)); + assert_eq!(h.rva_file_len(0x2100), Some(0x100)); + assert_eq!(h.rva_file_len(0x10), Some(0x3f0)); + assert_eq!(h.rva_file_len(0x9000), None); + assert_eq!(h.image_va(0x1010), Some(BASE + 0x1010)); + assert_eq!(h.image_va(u64::from(u32::MAX) + 1), None); +} + +#[test] +fn contiguous_file_length_stops_at_the_next_section() { + let mut h = headers(&pe64()).unwrap(); + // `.text` claims raw bytes that run into `.rdata`'s RVA range. + h.sections[0].raw_size = 0x2000; + h.sections[0].virtual_size = 0x2000; + assert_eq!(h.rva_file_len(0x1800), Some(0x1800)); + // Headers end where the first section begins. + h.size_of_headers = 0x1800; + assert_eq!(h.rva_file_len(0x0f00), Some(0x100)); +} + +#[test] +fn rich_header_entries_are_unmasked() { + let d = pe64(); + assert_eq!(rich_header(&d, PE_OFFSET), Some(vec![(0x0104, 30_729, 7), (0x00ff, 40_000, 1)])); + assert_eq!(rich_header(&d, 0x80), None, "Rich lies past the stub"); + let mut no_dans = d.clone(); + no_dans[0x80] ^= 1; + assert_eq!(rich_header(&no_dans, PE_OFFSET), None); + assert_eq!(rich_header(&d[..0x30], PE_OFFSET), None); +} + +#[test] +fn rich_header_is_capped_at_256_entries() { + let mut d = vec![0u8; 0x80 + 16 + 300 * 8 + 8 + 16]; + let entries: Vec<(u16, u16, u32)> = (0..300).map(|i| (1, i, 1)).collect(); + rich(&mut d, 7, &entries); + assert_eq!(rich_header(&d, d.len()).map(|e| e.len()), Some(256)); +} + +#[test] +fn subsystem_names() { + let names: Vec<&str> = [1, 2, 3, 7, 9, 10, 11, 12, 14, 16, 99].into_iter().map(subsystem_name).collect(); + assert_eq!( + names, + [ + "native", + "windows-gui", + "windows-console", + "posix-console", + "windows-ce", + "efi-application", + "efi-boot-driver", + "efi-runtime-driver", + "xbox", + "windows-boot", + "unknown" + ] + ); +} + +#[cfg(feature = "serde")] +#[test] +fn sections_serialize() { + let h = headers(&pe64()).unwrap(); + let value = serde_json::to_value(&h.sections[0]).unwrap(); + assert_eq!(value["name"], ".text"); + assert_eq!(value["rva"], 0x1000); +} + +#[test] +fn damaged_headers_never_panic() { + for m in mutations(&pe64()) { + let _ = has_pe_header(&m); + if let Ok(h) = headers(&m) { + for rva in [0, 0x10, 0x1010, 0x2000, 0x2100, u32::MAX] { + let _ = h.rva_to_offset(rva); + let _ = h.rva_file_len(rva); + let _ = h.image_va(u64::from(rva)); + } + let _ = rich_header(&m, h.pe_offset); + } + let _ = rich_header(&m, usize::MAX); + } +} diff --git a/crates/pe/tests/pe_optional_header_framing.rs b/crates/pe/tests/pe_optional_header_framing.rs new file mode 100644 index 000000000..2cdaa8593 --- /dev/null +++ b/crates/pe/tests/pe_optional_header_framing.rs @@ -0,0 +1,62 @@ +// From Craft Anything crates/formats/tests/pe_optional_header_framing.rs. +use craft_pe as pe; + +fn image(plus: bool, optional_size: usize, declared_dirs: u32) -> Vec { + let opt = 0x98; + let fixed = if plus { 112 } else { 96 }; + let mut data = vec![0; opt + optional_size.max(fixed) + 64]; + data[..2].copy_from_slice(b"MZ"); + data[0x3c..0x40].copy_from_slice(&0x80u32.to_le_bytes()); + data[0x80..0x84].copy_from_slice(b"PE\0\0"); + data[0x94..0x96].copy_from_slice(&(optional_size as u16).to_le_bytes()); + data[opt..opt + 2].copy_from_slice(&(if plus { 0x20bu16 } else { 0x10b }).to_le_bytes()); + data[opt + 68..opt + 70].copy_from_slice(&3u16.to_le_bytes()); + data[opt + fixed - 4..opt + fixed].copy_from_slice(&declared_dirs.to_le_bytes()); + data +} + +#[test] +fn required_optional_header_fields_cannot_be_borrowed_from_the_section_table() { + for plus in [false, true] { + for size in [0, 2, 70, if plus { 111 } else { 95 }] { + assert!(pe::headers(&image(plus, size, 16)).is_err(), "plus={plus}, size={size}"); + } + } +} + +#[test] +fn directory_count_is_bounded_by_complete_entries_in_the_optional_header() { + for plus in [false, true] { + let fixed = if plus { 112 } else { 96 }; + for available in [0, 7, 8, 15] { + let mut data = image(plus, fixed + available, 16); + data[0x98 + fixed..0x98 + fixed + 16].fill(0x55); + let parsed = pe::headers(&data).unwrap(); + assert_eq!(parsed.data_dirs.len(), available / 8, "plus={plus}, available={available}"); + if available >= 8 { + assert_eq!(parsed.data_dirs[0], (0x5555_5555, 0x5555_5555), "plus={plus}, available={available}"); + } + } + } +} + +#[test] +fn zero_or_one_directory_and_unused_optional_bytes_are_valid() { + for plus in [false, true] { + let fixed = if plus { 112 } else { 96 }; + for dirs in [0u32, 1] { + for padding in [0, 12] { + let size = fixed + dirs as usize * 8 + padding; + let mut data = image(plus, size, dirs); + data[0x86..0x88].copy_from_slice(&1u16.to_le_bytes()); + let section = 0x98 + size; + data[section..section + 5].copy_from_slice(b".test"); + let parsed = pe::headers(&data).unwrap(); + assert_eq!(parsed.pe32_plus, plus); + assert_eq!(parsed.subsystem, 3); + assert_eq!(parsed.data_dirs.len(), dirs as usize); + assert_eq!(parsed.sections[0].name, ".test"); + } + } + } +} diff --git a/crates/print/src/cups/mod.rs b/crates/print/src/cups/mod.rs index 9a8d7407e..94df2070b 100644 --- a/crates/print/src/cups/mod.rs +++ b/crates/print/src/cups/mod.rs @@ -34,6 +34,8 @@ const CUPS_SOCKET: &str = "/run/cups/cups.sock"; const MAX_BODY: usize = 1 << 20; const MAX_HEADERS: usize = 8 << 10; const MAX_PRINTERS: usize = 256; +// Even opted-in fleet discovery has a fixed record ceiling in addition to MAX_BODY. +const MAX_DISCOVERY_QUEUES: usize = 65_536; const MAX_NAME: usize = 256; /// A job's title on the wire; IPP values are u16-length, and no real title is this long. const MAX_JOB_NAME: usize = 255; @@ -344,9 +346,24 @@ pub fn destinations_at(endpoints: &[Server]) -> Result, Error> { /// explicitly choose whether an optional class/default query failure should hide otherwise /// usable printer queues. No error is swallowed here except CUPS's absent/unsupported list. pub fn queues_at(endpoints: &[Server], is_class: bool) -> Result, Error> { + queues_at_with_limit(endpoints, is_class, MAX_PRINTERS) +} + +/// List queues or classes with an explicit fleet-size budget (1 through 65,536). +/// +/// The existing [`queues_at`] API keeps its 256-queue default. Increasing this budget does +/// not increase the one-MiB response, HTTP header, name-length or exchange-time limits. +/// An excessive response is refused in full, never silently truncated. This changes only +/// local parsing: it does not invent pagination or retry a possibly submitted print job. +/// +/// # Errors +/// Returns an error for an invalid budget, an over-budget or malformed response, or a +/// transport/server error, with the same absent-list handling as [`queues_at`]. +pub fn queues_at_with_limit(endpoints: &[Server], is_class: bool, max_queues: usize) -> Result, Error> { + check_queue_limit(max_queues)?; let operation = if is_class { 0x4005 } else { OPERATION_CUPS_GET_PRINTERS }; let reply = post(endpoints, "/", &discovery_request(operation), DISCOVERY_TIMEOUT, true)?; - let parsed = parse_reply(&reply)?; + let parsed = parse_reply_with_limit(&reply, max_queues)?; if !status_ok(parsed.status) { if parsed.status == 0x0406 || (is_class && parsed.status == 0x0501) { return Ok(Vec::new()); @@ -846,7 +863,22 @@ impl<'a> Reader<'a> { } } +fn check_queue_limit(max_queues: usize) -> Result<(), Error> { + if !(1..=MAX_DISCOVERY_QUEUES).contains(&max_queues) { + return Err(Error::Spool("CUPS discovery queue limit must be 1 through 65536".into())); + } + Ok(()) +} + fn parse_reply(reply: &[u8]) -> Result { + parse_reply_with_limit(reply, MAX_PRINTERS) +} + +fn parse_reply_with_limit(reply: &[u8], max_queues: usize) -> Result { + check_queue_limit(max_queues)?; + if reply.len() > MAX_BODY { + return Err(Error::Spool("CUPS reply exceeds limit".into())); + } let malformed = || Error::Spool("the print server sent a malformed reply".into()); let mut r = Reader { bytes: reply, pos: 0 }; let _version = r.array::<2>().ok_or_else(malformed)?; @@ -866,7 +898,7 @@ fn parse_reply(reply: &[u8]) -> Result { } GROUP_OPERATION | GROUP_JOB | GROUP_PRINTER | 0x05 => { if !queue.name.is_empty() { - if queues.len() >= MAX_PRINTERS { + if queues.len() >= max_queues { return Err(malformed()); } queues.push(std::mem::take(&mut queue)); @@ -891,7 +923,7 @@ fn parse_reply(reply: &[u8]) -> Result { match (group, name, tag) { (GROUP_PRINTER, "printer-name", TAG_NAME) => { if let Some(printer) = std::str::from_utf8(value).ok().filter(|n| !n.is_empty() && n.len() <= MAX_NAME) { - if names.len() >= MAX_PRINTERS { + if names.len() >= max_queues { return Err(Error::Spool("too many CUPS destinations".into())); } if !queue.name.is_empty() { diff --git a/crates/print/src/cups/tests.rs b/crates/print/src/cups/tests.rs index ee1ef7020..e7df135bf 100644 --- a/crates/print/src/cups/tests.rs +++ b/crates/print/src/cups/tests.rs @@ -592,3 +592,37 @@ fn stalled_unix_peer_still_fails_within_the_absolute_read_deadline() { assert!(matches!(error.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock)); assert!(start.elapsed() < Duration::from_secs(3)); } + +#[test] +fn fleet_discovery_opt_in_preserves_all_257_queues_and_default_limit() { + let mut body = vec![1, 1, 0, 0, 0, 0, 0, 1]; + for index in 0..257 { + body.push(4); + body.extend(printer_name(&format!("Printer{index}"))); + } + body.push(3); + assert!(parse_reply(&body).is_err()); + assert_eq!(parse_reply_with_limit(&body, 257).unwrap().queues.len(), 257); + assert!(parse_reply_with_limit(&body, 256).is_err()); + for is_class in [false, true] { + let server = FakeCups::on_tcp(vec![http_reply(&body)]).unwrap(); + let queues = queues_at_with_limit(server.endpoint(), is_class, 257).unwrap(); + assert_eq!(queues.len(), 257); + assert_eq!(queues[256].name, "Printer256"); + assert!(queues.iter().all(|q| q.is_class == is_class)); + let request = server.request().unwrap(); + assert!(has(&request, b"requested-attributes")); + } +} + +#[test] +fn fleet_discovery_keeps_absolute_resource_limits() { + for limit in [0, MAX_DISCOVERY_QUEUES + 1, usize::MAX] { + assert!(queues_at_with_limit(&[], false, limit).is_err()); + } + assert!(parse_reply_with_limit(&vec![0; MAX_BODY + 1], MAX_DISCOVERY_QUEUES).is_err()); + let mut body = vec![1, 1, 0, 0, 0, 0, 0, 1, 4]; + body.extend(attr(0x42, &"x".repeat(MAX_NAME + 1), b"value")); + body.push(3); + assert!(parse_reply_with_limit(&body, MAX_DISCOVERY_QUEUES).is_err()); +} diff --git a/crates/ps/Cargo.toml b/crates/ps/Cargo.toml index baa9050e4..2f0316e59 100644 --- a/crates/ps/Cargo.toml +++ b/crates/ps/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "craft-ps" -description = "PostScript and EPS for Craft apps: an EPS interpreter that paints into PDF" +description = "PostScript and EPS for Craft apps: writing primitives, and an EPS interpreter that paints into PDF" version.workspace = true edition.workspace = true license.workspace = true @@ -13,6 +13,7 @@ workspace = true [features] default = [] read = ["dep:craft-eps", "dep:pdf-writer", "dep:miniz_oxide", "dep:weezl", "dep:skrifa", "dep:kurbo"] +write = ["dep:kurbo", "dep:flate2"] [dependencies] craft-eps = { workspace = true, optional = true } @@ -21,6 +22,7 @@ miniz_oxide = { workspace = true, optional = true } weezl = { workspace = true, optional = true } skrifa = { workspace = true, optional = true } kurbo = { workspace = true, optional = true } +flate2 = { version = "1.1.10", default-features = false, features = ["rust_backend"], optional = true } [[example]] name = "eps2pdf" diff --git a/crates/ps/README.md b/crates/ps/README.md index 51deedcc2..2ccc83247 100644 --- a/crates/ps/README.md +++ b/crates/ps/README.md @@ -4,9 +4,17 @@ PostScript and EPS for the Craft apps. No feature is enabled by default. | Feature | Provides | | --- | --- | +| `write` | PostScript writing primitives: numbers, strings, matrices, path data and Flate | | `read` | An EPS interpreter: runs the program and returns what it paints as a one-page PDF | -Writing PostScript and EPS (craft-libs #115) is planned as a separate feature next to `read`. +## Writing PostScript + +`write` emits the tokens of a PostScript or EPS program. The caller writes the page program and +any short operator names a prolog binds; this module formats numbers (non-finite values as zero), +escapes string and byte literals, writes matrices, raises quadratic segments to cubics, and +compresses or inflates Flate (`FlateDecode`) payloads. Path and paint operators can use the +Language Reference names or the one-letter names a bound prolog defines. Container concerns (DSC +comments, a binary preview header, ASCII85) stay in `craft-eps`. ## Reading EPS diff --git a/crates/ps/src/lib.rs b/crates/ps/src/lib.rs index 4480dae16..6643a823e 100644 --- a/crates/ps/src/lib.rs +++ b/crates/ps/src/lib.rs @@ -1,5 +1,7 @@ //! PostScript and EPS for the Craft apps. //! +//! - `write` (feature `write`): numbers, strings, matrices, path data and Flate, so an app can +//! emit a PostScript or EPS program. //! - `read` (feature `read`): runs an EPS and gives what it paints as a one-page vector PDF, //! for apps that place EPS files as graphics. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] @@ -7,3 +9,5 @@ #[cfg(feature = "read")] pub mod read; +#[cfg(feature = "write")] +pub mod write; diff --git a/crates/ps/src/write.rs b/crates/ps/src/write.rs new file mode 100644 index 000000000..97201771b --- /dev/null +++ b/crates/ps/src/write.rs @@ -0,0 +1,341 @@ +//! PostScript writing primitives: numbers, strings, matrices, path data and Flate. +//! +//! The caller decides the page program and any short operator names a prolog binds; this module +//! makes what it writes valid PostScript tokens. Numbers that are not finite are written as zero, +//! because PostScript has no spelling for them. Strings and byte strings are escaped so no value +//! can break out of a literal. +//! +//! ``` +//! use craft_ps::write::{num, push_path, string, PathOps}; +//! use kurbo::BezPath; +//! +//! assert_eq!(num(1.5), "1.5"); +//! assert_eq!(string("a (b)"), "(a \\(b\\))"); +//! +//! let mut path = BezPath::new(); +//! path.move_to((1.0, 2.0)); +//! path.line_to((3.0, 4.0)); +//! path.close_path(); +//! let mut out = String::new(); +//! push_path(&mut out, &path, PathOps::SHORT); +//! assert_eq!(out, "1 2 m\n3 4 l\nh\n"); +//! ``` + +use std::fmt::Write; +use std::io::{Read, Write as _}; + +use kurbo::{Affine, BezPath, PathEl}; + +/// `v` as PostScript writes it: at most four decimals, no trailing zeros (0 when not finite). +pub fn num(v: f64) -> String { + let mut s = String::new(); + push_num(&mut s, v); + s +} + +/// Append [`num`]`(v)` to `s`. +pub fn push_num(s: &mut String, v: f64) { + push_num_places(s, v, 4); +} + +/// The value [`num`]`(v)` stands for, as a reader parses it back. +pub fn written(v: f64) -> f64 { + num(v).parse().unwrap_or(0.0) +} + +/// Append `v` to `s` with at most `places` decimals (up to 9), no trailing zeros (0 when not +/// finite). +pub fn push_num_places(s: &mut String, v: f64, places: u32) { + let places = places.min(9); + let scale = 10f64.powi(places as i32); + // Past where the scaling overflows, values have no fractional digits to round. + let v = if !v.is_finite() { + 0.0 + } else if (v * scale).is_finite() { + (v * scale).round() / scale + } else { + v + }; + if v == v.trunc() && v.abs() < 1e15 { + let _ = write!(s, "{}", v as i64); + return; + } + let start = s.len(); + let _ = write!(s, "{v:.*}", places as usize); + // Only trim fractional zeros. With `places == 0` the token has no decimal point, and + // trimming would turn 1e15 into "1". + if s[start..].contains('.') { + let keep = s[start..].trim_end_matches('0').trim_end_matches('.').len(); + s.truncate(start + keep); + } +} + +/// `vs` as numbers separated by spaces, then a space. +pub fn push_nums(s: &mut String, vs: &[f64]) { + for v in vs { + push_num(s, *v); + s.push(' '); + } +} + +/// `text` as a PostScript string: `(` `)` `\` escaped, anything outside printable ASCII as octal +/// escapes of its UTF-8 bytes. +pub fn string(text: &str) -> String { + ps_literal(text.bytes()) +} + +/// `bytes` as a PostScript string: `(` `)` `\` escaped, carriage returns, line feeds and tabs as +/// `\r` `\n` `\t`, other bytes outside printable ASCII as octal escapes. +pub fn bytes(bytes: &[u8]) -> String { + ps_literal_bytes(bytes, true) +} + +fn ps_literal(bytes: impl Iterator) -> String { + ps_literal_bytes_iter(bytes, false) +} + +fn ps_literal_bytes(bytes: &[u8], escape_whitespace: bool) -> String { + ps_literal_bytes_iter(bytes.iter().copied(), escape_whitespace) +} + +fn ps_literal_bytes_iter(bytes: impl Iterator, escape_whitespace: bool) -> String { + let mut s = String::from("("); + for b in bytes { + match b { + b'(' | b')' | b'\\' => { + s.push('\\'); + s.push(char::from(b)); + } + b'\r' if escape_whitespace => s.push_str("\\r"), + b'\n' if escape_whitespace => s.push_str("\\n"), + b'\t' if escape_whitespace => s.push_str("\\t"), + 0x20..=0x7e => s.push(char::from(b)), + _ => { + let _ = write!(s, "\\{b:03o}"); + } + } + } + s.push(')'); + s +} + +/// A matrix operand `[a b c d e f]`. +pub fn matrix(a: Affine) -> String { + let mut s = String::from("["); + push_nums(&mut s, &a.as_coeffs()); + s.pop(); + s.push(']'); + s +} + +/// Names of the path operators written after each segment. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct PathOps { + pub moveto: &'static str, + pub lineto: &'static str, + pub curveto: &'static str, + pub closepath: &'static str, +} + +impl PathOps { + /// The PostScript Language Reference names. + pub const STANDARD: Self = Self { moveto: "moveto", lineto: "lineto", curveto: "curveto", closepath: "closepath" }; + /// One-letter names a bound prolog can define (`m`, `l`, `c`, `h`). + pub const SHORT: Self = Self { moveto: "m", lineto: "l", curveto: "c", closepath: "h" }; +} + +/// Append path `path` (`moveto`, `lineto`, `curveto`, `closepath` by default; quadratics raised to +/// cubics), one segment a line. Pass [`PathOps::SHORT`] for the one-letter names a prolog binds. +pub fn push_path(s: &mut String, path: &BezPath, ops: PathOps) { + let mut last = kurbo::Point::ZERO; + let mut start = kurbo::Point::ZERO; + for el in path.elements() { + match *el { + PathEl::MoveTo(p) => { + push_nums(s, &[p.x, p.y]); + s.push_str(ops.moveto); + s.push('\n'); + (last, start) = (p, p); + } + PathEl::LineTo(p) => { + push_nums(s, &[p.x, p.y]); + s.push_str(ops.lineto); + s.push('\n'); + last = p; + } + PathEl::QuadTo(a, p) => { + let c1 = last + (a - last) * (2.0 / 3.0); + let c2 = p + (a - p) * (2.0 / 3.0); + push_nums(s, &[c1.x, c1.y, c2.x, c2.y, p.x, p.y]); + s.push_str(ops.curveto); + s.push('\n'); + last = p; + } + PathEl::CurveTo(a, b, p) => { + push_nums(s, &[a.x, a.y, b.x, b.y, p.x, p.y]); + s.push_str(ops.curveto); + s.push('\n'); + last = p; + } + PathEl::ClosePath => { + s.push_str(ops.closepath); + s.push('\n'); + last = start; + } + } + } +} + +/// The winding rule a fill or clip uses. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FillRule { + NonZero, + EvenOdd, +} + +/// Operator names written for a fill or a clip (which also ends the path). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct PaintOps { + pub fill: &'static str, + pub eofill: &'static str, + pub clip: &'static str, + pub eoclip: &'static str, +} + +impl PaintOps { + /// The PostScript Language Reference names. The clip operators also start a new path. + pub const STANDARD: Self = Self { fill: "fill\n", eofill: "eofill\n", clip: "clip newpath\n", eoclip: "eoclip newpath\n" }; + /// Short names a bound prolog can define (`f`, `f*`, `W`, `W*`). + pub const SHORT: Self = Self { fill: "f\n", eofill: "f*\n", clip: "W\n", eoclip: "W*\n" }; +} + +/// The fill operator for `rule`. +pub fn fill_op(rule: FillRule, ops: PaintOps) -> &'static str { + match rule { + FillRule::NonZero => ops.fill, + FillRule::EvenOdd => ops.eofill, + } +} + +/// The clip operator for `rule` (it also ends the path). +pub fn clip_op(rule: FillRule, ops: PaintOps) -> &'static str { + match rule { + FillRule::NonZero => ops.clip, + FillRule::EvenOdd => ops.eoclip, + } +} + +/// `data` compressed for `FlateDecode` (zlib). +pub fn deflate(data: &[u8]) -> Vec { + let mut encoder = flate2::write::ZlibEncoder::new(Vec::with_capacity(data.len() / 2), flate2::Compression::default()); + // Writing to a Vec doesn't fail. + let _ = encoder.write_all(data); + encoder.finish().unwrap_or_default() +} + +/// zlib `data` inflated, when it is valid and at most `max` bytes. +pub fn inflate(data: &[u8], max: u64) -> Option> { + let mut out = vec![]; + flate2::read::ZlibDecoder::new(data).take(max.saturating_add(1)).read_to_end(&mut out).ok()?; + (out.len() as u64 <= max).then_some(out) +} + +#[cfg(test)] +mod tests { + use super::{ + FillRule, PaintOps, PathOps, bytes, clip_op, deflate, fill_op, inflate, matrix, num, push_num, push_num_places, push_nums, push_path, string, + written, + }; + use kurbo::{Affine, BezPath}; + + #[test] + fn huge_finite_numbers_are_valid_postscript() { + for v in [1.5e305, -1.5e305, f64::MAX, f64::MIN] { + let s = num(v); + assert!(!s.is_empty() && s.chars().all(|c| c.is_ascii_digit() || c == '-' || c == '.'), "{v}: got {s:?}"); + } + } + + #[test] + fn numbers_keep_the_decimals_asked_for() { + let places = |v: f64, n: u32| { + let mut s = String::new(); + push_num_places(&mut s, v, n); + s + }; + assert_eq!(num(0.123_456_78), "0.1235"); + assert_eq!(places(0.123_456_78, 7), "0.1234568"); + assert_eq!(places(f64::from(0.4f32), 7), "0.4"); + assert_eq!(places(2.5, 7), "2.5"); + assert_eq!(places(f64::NAN, 7), "0"); + assert_eq!(places(f64::INFINITY, 2), "0"); + assert_eq!(places(f64::NEG_INFINITY, 2), "0"); + assert_eq!(num(-2.0), "-2"); + assert_eq!(num(0.0), "0"); + assert_eq!(written(0.123_456_78), 0.1235); + let mut s = String::new(); + push_num(&mut s, 3.0); + assert_eq!(s, "3"); + let mut nums = String::new(); + push_nums(&mut nums, &[1.0, 2.5]); + assert_eq!(nums, "1 2.5 "); + assert_eq!(places(1.0, 20), "1"); + assert_eq!(places(1.23456789, 9), "1.23456789"); + assert_eq!(places(1e15, 0), "1000000000000000"); + assert_eq!(places(1e15 - 1.0, 0), "999999999999999"); + } + + #[test] + fn strings_and_bytes_are_escaped() { + assert_eq!(string("ok"), "(ok)"); + assert_eq!(string("a (b) \\c"), "(a \\(b\\) \\\\c)"); + assert_eq!(string("ä"), "(\\303\\244)"); + assert_eq!(bytes(b"ok"), "(ok)"); + assert_eq!(bytes(b"(\r\n\t)"), "(\\(\\r\\n\\t\\))"); + assert_eq!(bytes(&[0x01, 0xff]), "(\\001\\377)"); + assert_eq!(string("a\nb"), "(a\\012b)"); + } + + #[test] + fn matrix_and_paths_use_the_operator_names_asked_for() { + assert_eq!(matrix(Affine::new([1.0, 0.0, 0.0, 1.0, 10.0, 20.0])), "[1 0 0 1 10 20]"); + let mut path = BezPath::new(); + path.move_to((1.0, 2.0)); + path.line_to((3.0, 4.0)); + path.quad_to((5.0, 6.0), (7.0, 8.0)); + path.curve_to((9.0, 10.0), (11.0, 12.0), (13.0, 14.0)); + path.close_path(); + let mut short = String::new(); + push_path(&mut short, &path, PathOps::SHORT); + assert_eq!(short, "1 2 m\n3 4 l\n4.3333 5.3333 5.6667 6.6667 7 8 c\n9 10 11 12 13 14 c\nh\n"); + let mut standard = String::new(); + push_path(&mut standard, &path, PathOps::STANDARD); + assert!( + standard.contains("moveto\n") && standard.contains("lineto\n") && standard.contains("curveto\n") && standard.ends_with("closepath\n") + ); + let mut empty = String::new(); + push_path(&mut empty, &BezPath::new(), PathOps::STANDARD); + assert!(empty.is_empty()); + assert_eq!(fill_op(FillRule::NonZero, PaintOps::SHORT), "f\n"); + assert_eq!(fill_op(FillRule::EvenOdd, PaintOps::SHORT), "f*\n"); + assert_eq!(clip_op(FillRule::NonZero, PaintOps::SHORT), "W\n"); + assert_eq!(clip_op(FillRule::EvenOdd, PaintOps::SHORT), "W*\n"); + assert_eq!(fill_op(FillRule::NonZero, PaintOps::STANDARD), "fill\n"); + assert_eq!(fill_op(FillRule::EvenOdd, PaintOps::STANDARD), "eofill\n"); + assert_eq!(clip_op(FillRule::NonZero, PaintOps::STANDARD), "clip newpath\n"); + assert_eq!(clip_op(FillRule::EvenOdd, PaintOps::STANDARD), "eoclip newpath\n"); + } + + #[test] + fn flate_round_trips_and_honours_the_size_limit() { + let data = b"PostScript FlateDecode payload \x00\xff".repeat(32); + let compressed = deflate(&data); + assert_ne!(compressed, data); + assert_eq!(inflate(&compressed, data.len() as u64).as_deref(), Some(data.as_slice())); + assert_eq!(inflate(&compressed, (data.len() as u64) - 1), None); + assert_eq!(inflate(b"not zlib", 64), None); + assert_eq!(inflate(&deflate(b""), 0).as_deref(), Some(&b""[..])); + let hello = deflate(b"hello"); + assert_eq!(inflate(&hello, u64::MAX).as_deref(), Some(&b"hello"[..])); + } +} diff --git a/crates/psd/src/layer.rs b/crates/psd/src/layer.rs index e84b87e1d..eb1399f21 100644 --- a/crates/psd/src/layer.rs +++ b/crates/psd/src/layer.rs @@ -8,7 +8,7 @@ use crate::error::{PsdError, Result}; use crate::header::{MAX_DIMENSION, Version}; use crate::io::{Reader, Sink, WriteExt, append_with, decode_legacy_name, read_pascal, write_pascal}; use crate::source::Stored; -use crate::tagged::{BlockData, SectionDivider, SectionType, TaggedBlock, read_blocks, uses_long_length, write_blocks}; +use crate::tagged::{BlockData, SectionDivider, SectionType, TaggedBlock, read_record_blocks, uses_long_length, write_blocks}; /// Channel id of the transparency mask. pub const CHANNEL_TRANSPARENCY: i16 = -1; @@ -651,7 +651,7 @@ impl LayerRecord { let br_len = x.u32()?; let blending_ranges = BlendingRanges { data: x.bytes_u64(u64::from(br_len))?.to_vec() }; let name = read_pascal(&mut x, 4)?; - let (blocks, extra_trailing) = read_blocks(&mut x, version)?; + let (blocks, extra_trailing) = read_record_blocks(&mut x, version)?; Ok((LayerRecord { rect, channels, blend_mode, opacity, clipping, flags, filler, mask, blending_ranges, name, blocks, extra_trailing }, lens)) } diff --git a/crates/psd/src/resources.rs b/crates/psd/src/resources.rs index 75b52b199..d0ac3e83f 100644 --- a/crates/psd/src/resources.rs +++ b/crates/psd/src/resources.rs @@ -369,6 +369,22 @@ fn layer_selection_ids(data: &[u8]) -> Result> { (0..count).map(|_| r.u32()).collect() } +/// Read the unique EXIF and XMP image resources, rejecting duplicates and invalid XMP UTF-8. +/// EXIF bytes retain any optional container prefix; trailing XMP NULs are removed. +pub fn unique_exif_xmp(resources: &[ImageResource]) -> Option<(Option<&[u8]>, Option<&str>)> { + let (mut exif, mut xmp) = (None, None); + for resource in resources { + match resource.id { + ids::EXIF if exif.is_some() => return None, + ids::EXIF => exif = Some(resource.data.as_slice()), + ids::XMP if xmp.is_some() => return None, + ids::XMP => xmp = Some(std::str::from_utf8(&resource.data).ok()?.trim_end_matches('\0')), + _ => {} + } + } + Some((exif, xmp)) +} + impl ImageResource { /// A new `8BIM` resource with an empty name. pub fn new(id: u16, data: Vec) -> Self { diff --git a/crates/psd/src/tagged.rs b/crates/psd/src/tagged.rs index a934dad01..c8ec1789f 100644 --- a/crates/psd/src/tagged.rs +++ b/crates/psd/src/tagged.rs @@ -533,11 +533,28 @@ pub(crate) fn read_blocks(r: &mut Reader<'_>, version: Version) -> Result<(Vec, version: Version) -> Result<(Vec, Vec)> { + read_blocks_impl(r, version, 2, true, |_, _, _, _, _| false) +} + /// The callback consumes selected payloads before they are copied into owned blocks. pub(crate) fn read_blocks_with( r: &mut Reader<'_>, version: Version, alignment: usize, + consume: impl FnMut(usize, [u8; 4], [u8; 4], &[u8], &Option>) -> bool, +) -> Result<(Vec, Vec)> { + read_blocks_impl(r, version, alignment, false, consume) +} + +fn read_blocks_impl( + r: &mut Reader<'_>, + version: Version, + alignment: usize, + overlong_is_trailing: bool, mut consume: impl FnMut(usize, [u8; 4], [u8; 4], &[u8], &Option>) -> bool, ) -> Result<(Vec, Vec)> { let mut blocks = Vec::new(); @@ -548,6 +565,11 @@ pub(crate) fn read_blocks_with( r.skip(rest.len())?; return Ok((blocks, trailing)); } + if overlong_is_trailing && !block_fits(r, version) { + let trailing = rest.to_vec(); + r.skip(rest.len())?; + return Ok((blocks, trailing)); + } let signature = r.array::<4>()?; let key = r.array::<4>()?; let len = r.len_field(uses_long_length(version, &key))?; @@ -577,6 +599,13 @@ pub(crate) fn read_blocks_with( } } +/// Whether the block at `r` (signature and key already checked present) fits in what remains. +fn block_fits(r: &Reader<'_>, version: Version) -> bool { + let mut probe = r.clone(); + let Ok(key) = probe.skip(4).and_then(|()| probe.array::<4>()) else { return false }; + probe.len_field(uses_long_length(version, &key)).is_ok_and(|len| len <= probe.remaining() as u64) +} + pub(crate) fn write_blocks(out: &mut Vec, blocks: &[TaggedBlock], version: Version, alignment: usize) -> Result<()> { for b in blocks { b.write(out, version, alignment)?; @@ -766,4 +795,18 @@ mod tests { out.put(&[0; 4]); assert!(read_blocks(&mut Reader::new(&out), Version::Psd).is_err()); } + + #[test] + fn overlong_length_ends_a_records_blocks() { + let mut out = Vec::new(); + write_blocks(&mut out, &[TaggedBlock::layer_id(1)], Version::Psd, 2).unwrap(); + let damaged = out.len(); + out.put(b"8BIMabcd"); + out.put_u32(100); + out.put(&[0; 4]); + // The blocks before the damage survive; the damaged one and what follows stay as bytes. + let (blocks, trailing) = read_record_blocks(&mut Reader::new(&out), Version::Psd).unwrap(); + assert_eq!(blocks, vec![TaggedBlock::layer_id(1)]); + assert_eq!(trailing, out[damaged..]); + } } diff --git a/crates/psd/tests/metadata_resources.rs b/crates/psd/tests/metadata_resources.rs new file mode 100644 index 000000000..28df6e93c --- /dev/null +++ b/crates/psd/tests/metadata_resources.rs @@ -0,0 +1,14 @@ +use craft_psd::ImageResource; +use craft_psd::resources::{ids, unique_exif_xmp}; + +#[test] +fn metadata_resources_reject_duplicates_and_invalid_xmp() { + let exif = ImageResource::new(ids::EXIF, b"Exif\0\0II*\0".to_vec()); + let xmp = ImageResource::new(ids::XMP, b"\0".to_vec()); + let resources = [exif.clone(), xmp.clone()]; + assert_eq!(unique_exif_xmp(&resources), Some((Some(b"Exif\0\0II*\0".as_slice()), Some("")))); + assert!(unique_exif_xmp(&[exif.clone(), exif]).is_none()); + assert!(unique_exif_xmp(&[xmp.clone(), xmp]).is_none()); + assert!(unique_exif_xmp(&[ImageResource::new(ids::XMP, vec![255])]).is_none()); + assert_eq!(unique_exif_xmp(&[]), Some((None, None))); +} diff --git a/crates/raw/Cargo.toml b/crates/raw/Cargo.toml index 5e5a021c3..ddb1dbf2e 100644 --- a/crates/raw/Cargo.toml +++ b/crates/raw/Cargo.toml @@ -11,6 +11,10 @@ repository.workspace = true workspace = true [features] +# Bounded DNG strip/tile reader (`dng_lines`): the `dng` reader's unpackers and JPEG decoder. +line-dng = ["dng"] +# Decode DNG tile bands in parallel. +line-dng-threads = ["line-dng"] default = ["dng", "jxl", "crw", "hasselblad", "mrw", "erf", "mef", "kodak", "pef", "srw", "cr2", "nef", "rw2", "raf", "orf", "arw", "cr3"] ## Adobe DNG, reading and writing: uncompressed, lossless JPEG, lossy JPEG, Deflate. dng = ["dep:miniz_oxide", "dep:zune-jpeg", "dep:zune-core"] diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index 13bf252f3..efa1ec4f5 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -500,6 +500,12 @@ const FRAME_RATE: u16 = 51044; /// CinemaDNG `ReelName` (tag 51081). const REEL_NAME: u16 = 51081; +/// A CinemaDNG `FrameRate` of `n / d` frames a second between 1/1000 and 1000 (shared with the +/// bounded `dng_metadata` reader). +pub(crate) fn plausible_frame_rate(n: i64, d: i64) -> bool { + n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d +} + /// The CinemaDNG tags of a frame, from IFD 0 (where the specification puts them), else the raw IFD. A frame rate /// outside 1/1000 to 1000 frames a second and a time code that is not valid BCD are ignored. fn cinema_tags(ifd0: &Ifd, raw: &Ifd) -> CinemaTags { @@ -510,7 +516,7 @@ fn cinema_tags(ifd0: &Ifd, raw: &Ifd) -> CinemaTags { _ => None, }); CinemaTags { - frame_rate: frame_rate.filter(|&(n, d)| n > 0 && d > 0 && n <= 1000 * d && n * 1000 >= d), + frame_rate: frame_rate.filter(|&(n, d)| plausible_frame_rate(n, d)), timecode: pick(TIME_CODES).bytes(TIME_CODES).and_then(Timecode::from_bytes), reel_name: pick(REEL_NAME).string(REEL_NAME).map(|s| s.trim().to_owned()).filter(|s| !s.is_empty()), } diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs new file mode 100644 index 000000000..83877492e --- /dev/null +++ b/crates/raw/src/dng_lines.rs @@ -0,0 +1,446 @@ +//! The raw image data of a DNG: finding the main raw IFD, and reading its strips or tiles +//! (uncompressed or lossless JPEG) into one array of 16-bit samples. + +#[cfg(feature = "line-dng-threads")] +use rayon::prelude::*; + +use crate::ljpeg_lines as ljpeg; +use craft_tiff::classic::{Ifd, Tiff, tags}; + +/// Why DNG raw layout or sample data could not be read. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The file is not classic TIFF. + #[error("not a TIFF")] + NotTiff, + /// Required bytes are missing. + #[error("truncated raw data")] + Truncated, + /// A limit on samples or allocation was exceeded. + #[error("raw image too large")] + TooLarge, + /// Invalid structure. + #[error("{0}")] + Malformed(&'static str), + /// A valid unsupported variation. + #[error("{0}")] + Unsupported(String), +} +/// Result of the bounded raw reader. +pub type Result = std::result::Result; +impl From for Error { + fn from(e: craft_tiff::classic::Error) -> Self { + match e { + craft_tiff::classic::Error::NotTiff => Self::NotTiff, + craft_tiff::classic::Error::Malformed(s) => Self::Malformed(s), + } + } +} +impl From for Error { + fn from(e: crate::ljpeg_lines::Error) -> Self { + match e { + crate::ljpeg_lines::Error::Truncated => Self::Truncated, + crate::ljpeg_lines::Error::TooLarge => Self::TooLarge, + crate::ljpeg_lines::Error::Malformed(s) => Self::Malformed(s), + crate::ljpeg_lines::Error::Unsupported(s) => Self::Unsupported(s), + } + } +} + +/// Largest raw image decoded, in samples (width × height × samples per pixel). +pub const MAX_SAMPLES: usize = 1 << 27; +/// Decoded tiles are padded to tile boundaries, and a JPEG may be coded a little larger than its +/// chunk ([`decode_budget`]), so the samples decoding may allocate can exceed the useful image. +/// Keep that temporary work within a quarter more than a complete decoded raw frame. +const MAX_DECODED_TILE_SAMPLES: usize = MAX_SAMPLES + MAX_SAMPLES / 4; + +/// Photometric interpretations of DNG raw data. +pub const PHOTOMETRIC_CFA: u64 = 32803; +pub const PHOTOMETRIC_LINEAR_RAW: u64 = 34892; + +/// The layout of a raw IFD's image data. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RawLayout { + pub width: usize, + pub height: usize, + pub samples_per_pixel: usize, + pub bits: u32, + /// 1 uncompressed, 7 lossless JPEG, 34892 lossy JPEG. + pub compression: u64, + pub photometric: u64, +} + +/// Raw samples: `width` × `height` pixels of `samples_per_pixel` interleaved 16-bit samples. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RawImage { + pub width: usize, + pub height: usize, + pub samples_per_pixel: usize, + pub bits: u32, + pub data: Vec, +} + +fn uint_or(t: &Tiff, ifd: &Ifd, tag: u16, default: u64) -> Result { + match ifd.get(tag) { + Some(e) => Ok(t.uint(e)?), + None => Ok(default), + } +} + +/// The main raw image IFD: the largest full-resolution (NewSubFileType 0) IFD whose photometric +/// interpretation is CFA or LinearRaw. +pub fn find_raw_ifd<'i>(t: &Tiff, ifds: &'i [Ifd]) -> Result<&'i Ifd> { + let mut best: Option<(&Ifd, u64)> = None; + for ifd in ifds { + let Ok(kind) = uint_or(t, ifd, tags::NEW_SUBFILE_TYPE, 0) else { continue }; + let Ok(photometric) = uint_or(t, ifd, tags::PHOTOMETRIC, 0) else { continue }; + if kind != 0 || !matches!(photometric, PHOTOMETRIC_CFA | PHOTOMETRIC_LINEAR_RAW) { + continue; + } + let w = uint_or(t, ifd, tags::IMAGE_WIDTH, 0).unwrap_or(0); + let h = uint_or(t, ifd, tags::IMAGE_LENGTH, 0).unwrap_or(0); + let area = w.saturating_mul(h); + if best.is_none_or(|(_, a)| area > a) { + best = Some((ifd, area)); + } + } + best.map(|(i, _)| i).ok_or(Error::Unsupported("no raw image in this DNG".into())) +} + +/// Read and check the layout tags of a raw IFD. +pub fn layout(t: &Tiff, ifd: &Ifd) -> Result { + let width = uint_or(t, ifd, tags::IMAGE_WIDTH, 0)?; + let height = uint_or(t, ifd, tags::IMAGE_LENGTH, 0)?; + let spp = uint_or(t, ifd, tags::SAMPLES_PER_PIXEL, 1)?; + let photometric = uint_or(t, ifd, tags::PHOTOMETRIC, 0)?; + let compression = uint_or(t, ifd, tags::COMPRESSION, 1)?; + let bits = match ifd.get(tags::BITS_PER_SAMPLE) { + Some(e) => { + let b = t.uints(e)?; + let first = *b.first().ok_or(Error::Malformed("empty BitsPerSample"))?; + if b.iter().any(|&x| x != first) { + return Err(Error::Unsupported("different bit depths per sample".into())); + } + first + } + None => 1, + }; + if width == 0 || height == 0 || width > 65_535 || height > 65_535 { + return Err(Error::Malformed("raw image size")); + } + if !(1..=4).contains(&spp) { + return Err(Error::Unsupported(format!("{spp} samples per pixel"))); + } + if photometric == PHOTOMETRIC_CFA && spp != 1 { + return Err(Error::Malformed("a CFA image with more than one sample per pixel")); + } + if !(1..=16).contains(&bits) { + return Err(Error::Unsupported(format!("{bits}-bit raw samples"))); + } + if uint_or(t, ifd, tags::SAMPLE_FORMAT, 1)? != 1 { + return Err(Error::Unsupported("floating-point raw data".into())); + } + if spp > 1 && uint_or(t, ifd, tags::PLANAR_CONFIGURATION, 1)? != 1 { + return Err(Error::Unsupported("planar raw data".into())); + } + match compression { + 1 => { + if uint_or(t, ifd, tags::PREDICTOR, 1)? != 1 { + return Err(Error::Unsupported("a TIFF predictor on uncompressed raw data".into())); + } + } + 7 => {} + 8 => return Err(Error::Unsupported("deflate-compressed DNG".into())), + // DNG 1.4: lossy JPEG only for 8-bit LinearRaw (or mask) data + 34892 if photometric != PHOTOMETRIC_LINEAR_RAW => { + return Err(Error::Unsupported("lossy JPEG DNG of CFA data (DNG allows it only for 8-bit LinearRaw)".into())); + } + 34892 if bits != 8 => return Err(Error::Unsupported(format!("lossy JPEG DNG with {bits}-bit samples (DNG allows only 8)"))), + 34892 => {} + 52546 => return Err(Error::Unsupported("JPEG XL DNG".into())), + c => return Err(Error::Unsupported(format!("DNG compression {c}"))), + } + Ok(RawLayout { width: width as usize, height: height as usize, samples_per_pixel: spp as usize, bits: bits as u32, compression, photometric }) +} + +/// One strip or tile: where it sits in the image and its bytes. +struct Chunk<'a> { + x: usize, + y: usize, + /// Pixel size of the chunk inside the image's rows (tiles are whole even at the image edge; + /// a last strip ends at the image). + w: usize, + h: usize, + /// Rows a coder may still have written (a last strip coded at full RowsPerStrip). + coded_h: usize, + bytes: &'a [u8], +} + +fn chunks<'a>(t: &Tiff<'a>, ifd: &Ifd, l: &RawLayout) -> Result>> { + let (offs, counts, tw, th) = if let (Some(o), Some(c)) = (ifd.get(tags::TILE_OFFSETS), ifd.get(tags::TILE_BYTE_COUNTS)) { + let tw = uint_or(t, ifd, tags::TILE_WIDTH, 0)? as usize; + let th = uint_or(t, ifd, tags::TILE_LENGTH, 0)? as usize; + if tw == 0 || th == 0 || tw > 65_535 || th > 65_535 { + return Err(Error::Malformed("tile size")); + } + (t.uints(o)?, t.uints(c)?, tw, th) + } else if let (Some(o), Some(c)) = (ifd.get(tags::STRIP_OFFSETS), ifd.get(tags::STRIP_BYTE_COUNTS)) { + let rows = (uint_or(t, ifd, tags::ROWS_PER_STRIP, l.height as u64)? as usize).clamp(1, l.height); + (t.uints(o)?, t.uints(c)?, l.width, rows) + } else { + return Err(Error::Malformed("no strips or tiles")); + }; + let across = l.width.div_ceil(tw); + let down = l.height.div_ceil(th); + let n = across * down; + if offs.len() < n || counts.len() < n { + return Err(Error::Malformed("fewer strips or tiles than the image needs")); + } + let mut out = Vec::with_capacity(n); + let mut ranges = Vec::with_capacity(n); + for i in 0..n { + let (x, y) = ((i % across) * tw, (i / across) * th); + let start = usize::try_from(offs[i]).map_err(|_| Error::Truncated)?; + let len = usize::try_from(counts[i]).map_err(|_| Error::Truncated)?; + let end = start.checked_add(len).ok_or(Error::Truncated)?; + let bytes = t.data.get(start..end).ok_or(Error::Truncated)?; + // strips end at the image; tiles are coded whole + let h = if ifd.get(tags::TILE_OFFSETS).is_some() { th } else { th.min(l.height - y) }; + out.push(Chunk { x, y, w: tw, h, coded_h: th, bytes }); + if len != 0 { + ranges.push((start, end)); + } + } + // chunks that share bytes would decode the same data again and again + ranges.sort_unstable(); + if ranges.windows(2).any(|w| w[1].0 < w[0].1) { + return Err(Error::Malformed("strips or tiles overlap")); + } + Ok(out) +} + +/// Unpack `w` × `h` × `spp` uncompressed samples of `bits` bits: 8 and 16-bit samples as bytes / +/// words in the file's byte order, other depths packed most significant bit first with each row +/// starting on a byte boundary (TIFF 6.0 §7, DNG BitsPerSample). +pub fn unpack(bytes: &[u8], w: usize, h: usize, spp: usize, bits: u32, big_endian: bool) -> Result> { + // zero-bit samples need no input, so a header could ask for any number of them + if !(1..=16).contains(&bits) { + return Err(Error::Malformed("unsupported bits per sample")); + } + let per_row = w.checked_mul(spp).ok_or(Error::TooLarge)?; + let samples = per_row.checked_mul(h).filter(|&n| n <= MAX_SAMPLES).ok_or(Error::TooLarge)?; + let row_bytes = per_row.checked_mul(bits as usize).ok_or(Error::TooLarge)?.div_ceil(8); + let need = row_bytes.checked_mul(h).ok_or(Error::TooLarge)?; + if bytes.len() < need { + return Err(Error::Truncated); + } + let mut out = vec![0u16; samples]; + let order = if big_endian { craft_tiff::ByteOrder::Big } else { craft_tiff::ByteOrder::Little }; + // the same unpackers the full DNG reader uses (`crate::unpack`) + for (row, dst) in bytes.chunks_exact(row_bytes.max(1)).zip(out.chunks_exact_mut(per_row.max(1))) { + match bits { + 8 => dst.iter_mut().zip(row).for_each(|(o, &b)| *o = u16::from(b)), + 16 => crate::unpack::read_u16s(row, order, dst), + _ => crate::unpack::unpack_msb(row, bits, dst), + } + } + Ok(out) +} + +/// The most samples decoding chunk `c` may allocate, charged against the aggregate budget before +/// anything is decoded: the samples themselves when uncompressed; for lossless JPEG the coded rows +/// with an eighth of slack (the frame may be coded a little larger than its chunk); for lossy JPEG +/// the frame its header declares when that fits the chunk rounded up to the 32-pixel MCU padding +/// `tiffraw` accepts (a 16-row strip is charged 16 rows, not 32), else that padded chunk (decoding +/// then refuses the frame). +fn decode_budget(c: &Chunk, l: &RawLayout) -> Option { + let spp = l.samples_per_pixel; + match l.compression { + 1 => c.w.checked_mul(c.h)?.checked_mul(spp), + 34892 => { + let (max_w, max_h) = (c.w.checked_next_multiple_of(32)?, c.coded_h.checked_next_multiple_of(32)?); + let padded = max_w.checked_mul(max_h)?.checked_mul(spp)?; + match crate::tiffraw::lossy_jpeg_dimensions(c.bytes) { + Some((w, h)) if w <= max_w && h <= max_h => Some(w.checked_mul(h)?.checked_mul(spp)?.min(padded)), + _ => Some(padded), + } + } + _ => { + let coded = c.w.checked_mul(c.coded_h)?.checked_mul(spp)?; + coded.checked_add(coded / 8)?.checked_add(64) + } + } +} + +/// Decode one chunk into `w` × `h` × `spp` samples, allocating at most `budget` samples. +fn decode_chunk(c: &Chunk, l: &RawLayout, big_endian: bool, budget: usize) -> Result> { + let need = c.w * c.h * l.samples_per_pixel; + match l.compression { + 1 => unpack(c.bytes, c.w, c.h, l.samples_per_pixel, l.bits, big_endian), + 34892 => lossy_jpeg(c, l, budget), + _ => { + // the JPEG's samples fill the chunk in raster order (its own width × components may + // differ from the chunk's, e.g. two components of half the width); the samples past + // `need` (a last strip coded at full RowsPerStrip) are dropped, and so is their capacity + let d = ljpeg::decode(c.bytes, budget)?; + if d.samples.len() < need { + return Err(Error::Malformed("lossless JPEG smaller than its tile or strip")); + } + let mut s = d.samples; + s.truncate(need); + s.shrink_to_fit(); + Ok(s) + } + } +} + +/// Decode a baseline (lossy) JPEG tile or strip (Compression 34892) into the chunk's 8-bit +/// samples: one component (greyscale) or three (colour, converted from YCbCr as the JPEG says), in +/// raster order. It is the full DNG reader's decoder (`tiffraw`, zune-jpeg), with the same check +/// that the JPEG covers the chunk's visible part and is at most MCU-padded beyond it. +fn lossy_jpeg(c: &Chunk, l: &RawLayout, budget: usize) -> Result> { + let spp = l.samples_per_pixel; + let need = c.w * c.h * spp; + let visible = (c.w.min(l.width.saturating_sub(c.x)), c.h.min(l.height.saturating_sub(c.y))); + let (px, w, h, _) = + crate::tiffraw::lossy_jpeg_with_tile(c.bytes, spp, budget, Some((c.w, c.coded_h, visible.0, visible.1))).map_err(|e| match e { + crate::RawError::Unsupported(m) => Error::Unsupported(m), + _ => Error::Malformed("lossy JPEG tile"), + })?; + // the JPEG's rows are its own width; the chunk keeps its `c.w` x `c.h` top-left corner, and + // the part of a coded chunk past a smaller JPEG (outside the image) stays zero + let mut out = vec![0u16; need]; + let n = c.w.min(w) * spp; + for (y, dst) in out.chunks_exact_mut(c.w * spp).take(h).enumerate() { + let src = px.get(y * w * spp..y * w * spp + n).ok_or(Error::Truncated)?; + dst[..n].iter_mut().zip(src).for_each(|(o, &v)| *o = u16::from(v)); + } + Ok(out) +} + +/// Read the raw image of `ifd`. +pub fn read(t: &Tiff, ifd: &Ifd) -> Result { + let l = layout(t, ifd)?; + let spp = l.samples_per_pixel; + let total = l.width.checked_mul(l.height).and_then(|v| v.checked_mul(spp)).ok_or(Error::TooLarge)?; + if total > MAX_SAMPLES { + return Err(Error::TooLarge); + } + let chunks = chunks(t, ifd, &l)?; + // every coded sample takes at least one bit of file (a Huffman code is at least one bit long); + // a lossy JPEG block at least two bits (a DC code and an end of block), and one luma block of + // 64 pixels carries at most 64 × spp samples however far the chroma is subsampled, so a tiny + // file cannot claim a huge picture + let per_byte = if l.compression == 34892 { 256 * spp } else { 8 }; + let coded: usize = chunks.iter().map(|c| c.bytes.len()).fold(0usize, usize::saturating_add); + if total.div_ceil(per_byte) > coded { + return Err(Error::Truncated); + } + // what decoding may allocate, chunk by chunk, within one decoded frame's worth of samples + let mut budgets = Vec::with_capacity(chunks.len()); + let mut decoded_samples = 0usize; + for c in &chunks { + let n = c.w.checked_mul(c.h).and_then(|v| v.checked_mul(spp)).ok_or(Error::TooLarge)?; + let budget = decode_budget(c, &l).filter(|&b| b <= MAX_SAMPLES).ok_or(Error::TooLarge)?; + decoded_samples = decoded_samples.checked_add(budget).ok_or(Error::TooLarge)?; + if decoded_samples > MAX_DECODED_TILE_SAMPLES { + return Err(Error::TooLarge); + } + if n.div_ceil(per_byte) > c.bytes.len() { + return Err(Error::Truncated); + } + budgets.push(budget); + } + let raw = |data| RawImage { width: l.width, height: l.height, samples_per_pixel: spp, bits: l.bits, data }; + // one chunk covering the image (a single strip): its samples are the image, no copy + if let [c] = chunks.as_slice() + && (c.x, c.y, c.w) == (0, 0, l.width) + && c.h >= l.height + { + let mut d = decode_chunk(c, &l, t.big_endian, budgets[0])?; + d.truncate(total); + if d.len() == total { + return Ok(raw(d)); + } + return Err(Error::Truncated); + } + // bands of one row of tiles (or one strip): decoded in parallel, each band copying its own + // chunks into place + let row_len = l.width * spp; + let (Some(first), true) = (chunks.first(), row_len > 0) else { return Err(Error::Malformed("no strips or tiles")) }; + let (band_rows, across) = (first.h.max(1), l.width.div_ceil(first.w.max(1))); + let mut data = vec![0u16; total]; + let band = |k: usize, out: &mut [u16]| -> Result<()> { + let range = k * across..((k + 1) * across).min(chunks.len()); + let mine = chunks.get(range.clone()).unwrap_or(&[]); + let budget = budgets.get(range).unwrap_or(&[]); + #[cfg(feature = "line-dng-threads")] + let decoded: Vec>> = mine.par_iter().zip(budget).map(|(c, &b)| decode_chunk(c, &l, t.big_endian, b)).collect(); + #[cfg(not(feature = "line-dng-threads"))] + let decoded: Vec>> = mine.iter().zip(budget).map(|(c, &b)| decode_chunk(c, &l, t.big_endian, b)).collect(); + for (c, d) in mine.iter().zip(decoded) { + let d = d?; + let cw = c.w.min(l.width.saturating_sub(c.x)); + let ch = c.h.min(l.height.saturating_sub(c.y)); + for r in 0..ch { + let src = d.get(r * c.w * spp..(r * c.w + cw) * spp).ok_or(Error::Truncated)?; + let at = r * row_len + c.x * spp; + out.get_mut(at..at + cw * spp).ok_or(Error::Truncated)?.copy_from_slice(src); + } + } + Ok(()) + }; + #[cfg(feature = "line-dng-threads")] + data.par_chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?; + #[cfg(not(feature = "line-dng-threads"))] + data.chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?; + Ok(raw(data)) +} + +#[cfg(test)] +mod unpack_tests { + use super::*; + + #[test] + fn reads_a_complete_uncompressed_cfa_strip() { + let mut bytes = b"II*\0".to_vec(); + bytes.extend_from_slice(&8u32.to_le_bytes()); + let entries: [(u16, u32); 9] = [ + (tags::IMAGE_WIDTH, 2), + (tags::IMAGE_LENGTH, 2), + (tags::BITS_PER_SAMPLE, 8), + (tags::COMPRESSION, 1), + (tags::PHOTOMETRIC, PHOTOMETRIC_CFA as u32), + (tags::STRIP_OFFSETS, 122), + (tags::SAMPLES_PER_PIXEL, 1), + (tags::ROWS_PER_STRIP, 2), + (tags::STRIP_BYTE_COUNTS, 4), + ]; + bytes.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, value) in entries { + bytes.extend_from_slice(&tag.to_le_bytes()); + bytes.extend_from_slice(&4u16.to_le_bytes()); + bytes.extend_from_slice(&1u32.to_le_bytes()); + bytes.extend_from_slice(&value.to_le_bytes()); + } + bytes.extend_from_slice(&0u32.to_le_bytes()); + bytes.extend_from_slice(&[1, 2, 3, 4]); + let (tiff, first) = Tiff::new(&bytes).unwrap(); + let ifds = tiff.all_ifds(first).unwrap(); + let raw = read(&tiff, find_raw_ifd(&tiff, &ifds).unwrap()).unwrap(); + assert_eq!((raw.width, raw.height, raw.bits), (2, 2, 8)); + assert_eq!(raw.data, [1, 2, 3, 4]); + } + + #[test] + fn zero_or_oversized_bit_depths_are_rejected_before_allocating() { + // zero-bit samples consume no input: one byte used to yield a whole picture of zeros + assert!(matches!(unpack(&[0], 1 << 20, 1 << 10, 1, 0, false), Err(Error::Malformed(_)))); + assert!(matches!(unpack(&[0; 64], 4, 1, 1, 17, false), Err(Error::Malformed(_)))); + assert_eq!(unpack(&[0x12, 0x34, 0x50], 2, 1, 1, 12, true).unwrap(), vec![0x123, 0x450]); + // dimensions whose products overflow, or exceed the sample limit, fail before allocating + assert_eq!(unpack(&[], isize::MAX as usize + 1, 1, 1, 2, false), Err(Error::TooLarge)); + assert_eq!(unpack(&[], usize::MAX, 2, 1, 16, false), Err(Error::TooLarge)); + assert_eq!(unpack(&[], 1 << 20, 1 << 8, 1, 1, false), Err(Error::TooLarge)); + } +} diff --git a/crates/raw/src/dng_metadata.rs b/crates/raw/src/dng_metadata.rs new file mode 100644 index 000000000..1d3a02b16 --- /dev/null +++ b/crates/raw/src/dng_metadata.rs @@ -0,0 +1,259 @@ +//! Bounded DNG processing and camera metadata, before application rendering. +//! +//! This is the [`crate::dng_lines`] companion on the same `craft_tiff::classic` IFDs. The full +//! reader's `dng::color_data` works on `craft_tiff::Tiff` and returns the values it will render +//! with (raw IFD first, implausible or singular values dropped, `BaselineExposureOffset` added). +//! This reader returns the tags as written, IFD 0 first, and fails on a value outside the file, +//! so a damaged file does not silently lose its white balance. For a well-formed file both read the +//! same matrices and neutral (see `tests/it/dng_metadata.rs`). +use crate::dng_lines::{self, Error, PHOTOMETRIC_CFA, Result}; +use craft_tiff::classic::{Entry, Ifd, Tiff, tags}; +type M3 = [[f64; 3]; 3]; +type V3 = [f64; 3]; +/// Camera-plane colour filter pattern. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Pattern { + pub rows: usize, + pub cols: usize, + pub colors: Vec, +} +/// Parsed image processing tags. +#[derive(Clone, Debug, Default)] +pub struct Params { + pub active_area: Option<[usize; 4]>, + pub linearization: Option>, + pub black_repeat: (usize, usize), + pub black: Vec, + pub black_delta_h: Vec, + pub black_delta_v: Vec, + pub white: Vec, + pub crop_origin: Option<(f64, f64)>, + pub crop_size: Option<(f64, f64)>, + pub cfa: Option, +} +/// Parsed camera colour calibration. +#[derive(Clone, Debug, Default)] +pub struct Calibration { + pub color_matrix: [Option; 2], + pub camera_calibration: [Option; 2], + pub forward_matrix: [Option; 2], + pub illuminant: [Option; 2], + pub analog_balance: Option, + pub as_shot_neutral: Option, + pub as_shot_white_xy: Option<(f64, f64)>, + pub baseline_exposure: f64, +} +/// SMPTE ST 12-1 time code of a CinemaDNG frame: the crate's [`crate::Timecode`], which the full +/// reader's `CinemaTags` uses too. +pub use crate::Timecode; + +/// File-level capture fields, without image geometry or rendering policy. +#[derive(Clone, Debug, PartialEq)] +pub struct CaptureInfo { + pub dng_version: [u8; 4], + pub frame_rate: Option<(i64, i64)>, + pub timecode: Option, + pub camera: Option, + pub reel: Option, +} +/// Metadata reader with the file's main and raw IFDs. +pub struct Metadata<'a, 'i> { + pub tiff: Tiff<'a>, + pub main: &'i Ifd, + pub raw: &'i Ifd, +} +impl Metadata<'_, '_> { + /// A raw-image tag: from the raw IFD, else IFD 0. + fn raw_tag(&self, tag: u16) -> Option<&Entry> { + self.raw.get(tag).or_else(|| self.main.get(tag)) + } + + /// A camera-wide tag: from IFD 0, else the raw IFD. + fn main_tag(&self, tag: u16) -> Option<&Entry> { + self.main.get(tag).or_else(|| self.raw.get(tag)) + } + + /// A numeric tag's values; a value outside the file (or a zero denominator) is an error, so a + /// damaged file does not silently lose its white balance. + fn floats(&self, e: Option<&Entry>) -> Result>> { + Ok(e.map(|e| self.tiff.floats(e)).transpose()?) + } + + /// A 3 × 3 matrix tag (`None` when absent or not 3 × 3, e.g. for other colour counts). + fn matrix(&self, tag: u16) -> Result> { + let Some(v) = self.floats(self.main_tag(tag))? else { return Ok(None) }; + let Ok(v) = <[f64; 9]>::try_from(v.as_slice()) else { return Ok(None) }; + Ok(Some([[v[0], v[1], v[2]], [v[3], v[4], v[5]], [v[6], v[7], v[8]]])) + } + + fn vec3(&self, tag: u16) -> Result> { + Ok(self.floats(self.main_tag(tag))?.and_then(|v| <[f64; 3]>::try_from(v.as_slice()).ok())) + } + + /// The CFA pattern in camera planes (`None` for LinearRaw). + pub fn cfa(&self) -> Result> { + let l = dng_lines::layout(&self.tiff, self.raw)?; + if l.photometric != PHOTOMETRIC_CFA { + return Ok(None); + } + let dims = self.raw_tag(tags::CFA_REPEAT_PATTERN_DIM).map(|e| self.tiff.uints(e)).transpose()?.unwrap_or_else(|| vec![2, 2]); + let (rows, cols) = match dims[..] { + [r, c, ..] => (r as usize, c as usize), + _ => return Err(Error::Malformed("CFARepeatPatternDim")), + }; + if !(1..=8).contains(&rows) || !(1..=8).contains(&cols) { + return Err(Error::Unsupported(format!("{rows} × {cols} CFA pattern"))); + } + let pattern = self.tiff.uints(self.raw_tag(tags::CFA_PATTERN).ok_or(Error::Malformed("no CFAPattern"))?)?; + if pattern.len() < rows * cols { + return Err(Error::Malformed("CFAPattern shorter than its repeat")); + } + let planes = match self.raw_tag(tags::CFA_PLANE_COLOR) { + Some(e) => self.tiff.uints(e)?, + None => vec![0, 1, 2], + }; + let mut sorted = planes.clone(); + sorted.sort_unstable(); + if sorted != [0, 1, 2] { + return Err(Error::Unsupported("CFA colours other than red, green and blue".into())); + } + if let Some(e) = self.raw_tag(tags::CFA_LAYOUT) + && self.tiff.uint(e)? != 1 + { + return Err(Error::Unsupported("a non-rectangular CFA layout".into())); + } + let colors = pattern[..rows * cols] + .iter() + .map(|&v| planes.iter().position(|&p| p == v).map(|i| i as u8).ok_or(Error::Malformed("CFAPattern colour not in CFAPlaneColor"))) + .collect::>>()?; + Ok(Some(Pattern { rows, cols, colors })) + } + + pub fn params(&self) -> Result { + let uint_pair = |tag| -> Result> { + match self.raw_tag(tag) { + Some(e) => match self.tiff.uints(e)?[..] { + [a, b, ..] => Ok(Some((a as usize, b as usize))), + _ => Err(Error::Malformed("short field")), + }, + None => Ok(None), + } + }; + let active_area = match self.raw_tag(tags::ACTIVE_AREA) { + Some(e) => match self.tiff.uints(e)?[..] { + [t, l, b, r, ..] => Some([t, l, b, r].map(|v| usize::try_from(v).unwrap_or(usize::MAX))), + _ => return Err(Error::Malformed("ActiveArea")), + }, + None => None, + }; + let linearization = match self.raw_tag(tags::LINEARIZATION_TABLE) { + Some(e) => { + // checked before `uints` allocates a u64 per entry + if e.count > 65_536 { + return Err(Error::Malformed("LinearizationTable longer than 65536")); + } + let v = self.tiff.uints(e)?; + Some(v.into_iter().map(|x| x.min(65_535) as u16).collect()) + } + None => None, + }; + let pair = |tag| -> Result> { + match self.raw_tag(tag) { + Some(e) => match self.tiff.floats(e)?[..] { + [a, b, ..] => Ok(Some((a, b))), + _ => Err(Error::Malformed("short field")), + }, + None => Ok(None), + } + }; + let list = |tag| -> Result> { Ok(self.raw_tag(tag).map(|e| self.tiff.floats(e)).transpose()?.unwrap_or_default()) }; + Ok(Params { + active_area, + linearization, + black_repeat: uint_pair(tags::BLACK_LEVEL_REPEAT_DIM)?.unwrap_or((1, 1)), + black: list(tags::BLACK_LEVEL)?, + black_delta_h: list(tags::BLACK_LEVEL_DELTA_H)?, + black_delta_v: list(tags::BLACK_LEVEL_DELTA_V)?, + white: list(tags::WHITE_LEVEL)?, + crop_origin: pair(tags::DEFAULT_CROP_ORIGIN)?, + crop_size: pair(tags::DEFAULT_CROP_SIZE)?, + cfa: self.cfa()?, + }) + } + + fn text(&self, tag: u16) -> Option { + self.main_tag(tag).and_then(|e| self.tiff.ascii(e).ok()).filter(|s| !s.is_empty()) + } + /// Read file-level CinemaDNG and camera fields. + pub fn capture_info(&self) -> CaptureInfo { + let dng_version = self + .main_tag(tags::DNG_VERSION) + .and_then(|e| self.tiff.uints(e).ok()) + .and_then(|v| v.get(..4).map(|v| [0, 1, 2, 3].map(|i| v[i] as u8))) + .unwrap_or_default(); + let frame_rate = self.main_tag(tags::FRAME_RATE).and_then(|e| { + let b = self.tiff.value_bytes(e).ok()?; + if !matches!(e.ty, craft_tiff::classic::Type::SRational | craft_tiff::classic::Type::Rational) { + return None; + } + let n = i64::from(self.tiff.u32_of(b.get(0..4)?.try_into().ok()?) as i32); + let d = i64::from(self.tiff.u32_of(b.get(4..8)?.try_into().ok()?) as i32); + crate::dng::plausible_frame_rate(n, d).then_some((n, d)) + }); + let timecode = self.main_tag(tags::TIME_CODES).and_then(|e| self.tiff.value_bytes(e).ok()).and_then(Timecode::from_bytes); + let camera = self.text(tags::UNIQUE_CAMERA_MODEL).or_else(|| { + let mm = [self.text(tags::MAKE), self.text(tags::MODEL)].into_iter().flatten().collect::>().join(" "); + (!mm.is_empty()).then_some(mm) + }); + CaptureInfo { dng_version, frame_rate, timecode, camera, reel: self.text(tags::REEL_NAME) } + } + + /// The colour tags. + pub fn calibration(&self) -> Result { + let illum = |tag| self.main_tag(tag).map(|e| self.tiff.uint(e)).transpose(); + let white_xy = self.floats(self.main_tag(tags::AS_SHOT_WHITE_XY))?.and_then(|v| match v[..] { + [x, y] => Some((x, y)), + _ => None, + }); + Ok(Calibration { + color_matrix: [self.matrix(tags::COLOR_MATRIX_1)?, self.matrix(tags::COLOR_MATRIX_2)?], + camera_calibration: [self.matrix(tags::CAMERA_CALIBRATION_1)?, self.matrix(tags::CAMERA_CALIBRATION_2)?], + forward_matrix: [self.matrix(tags::FORWARD_MATRIX_1)?, self.matrix(tags::FORWARD_MATRIX_2)?], + illuminant: [illum(tags::CALIBRATION_ILLUMINANT_1)?, illum(tags::CALIBRATION_ILLUMINANT_2)?], + analog_balance: self.vec3(tags::ANALOG_BALANCE)?, + as_shot_neutral: self.vec3(tags::AS_SHOT_NEUTRAL)?, + as_shot_white_xy: white_xy, + baseline_exposure: self.floats(self.main_tag(tags::BASELINE_EXPOSURE))?.and_then(|v| v.first().copied()).unwrap_or(0.0), + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn timecode_is_the_crate_timecode() { + let tc = Timecode { frames: 12, seconds: 34, minutes: 12, hours: 9, drop_frame: true }; + assert_eq!(Timecode::from_bytes(&[0x52, 0x34, 0x12, 0x09]), Some(tc)); + let shared: crate::Timecode = tc; + assert_eq!(Timecode::from_bytes(&shared.to_bytes()), Some(tc)); + } + + #[test] + fn calibration_defaults_and_rejects_bad_rational() -> Result<()> { + let empty = Ifd::default(); + let tiff = Tiff { data: &[], big_endian: false }; + let metadata = Metadata { tiff, main: &empty, raw: &empty }; + let calibration = metadata.calibration()?; + assert_eq!(calibration.baseline_exposure, 0.0); + assert_eq!(calibration.color_matrix, [None, None]); + + // IFD with one BaselineExposure SRational pointing past the end of the file. + let data = [b'I', b'I', 42, 0, 8, 0, 0, 0, 1, 0, 42, 198, 10, 0, 1, 0, 0, 0, 200, 0, 0, 0, 0, 0, 0, 0]; + let (tiff, first) = Tiff::new(&data)?; + let (ifd, _) = tiff.read_ifd(first, 0)?; + assert!(Metadata { tiff, main: &ifd, raw: &ifd }.calibration().is_err()); + Ok(()) + } +} diff --git a/crates/raw/src/dngwrite.rs b/crates/raw/src/dngwrite.rs index 384152e66..2b8061949 100644 --- a/crates/raw/src/dngwrite.rs +++ b/crates/raw/src/dngwrite.rs @@ -119,8 +119,184 @@ fn rat_vec(v: &[f64]) -> Value { Value::Rational(v.iter().map(|&x| rational(x)).collect()) } +/// Colour tags copied by value, with potentially large profile tables borrowed. +pub struct DngColor<'a> { + pub illuminant: [u16; 2], + pub color_matrix: [Option; 2], + pub forward_matrix: [Option; 2], + pub camera_calibration: [Option; 2], + pub analog_balance: Option<[f64; 3]>, + pub as_shot_neutral: Option<[f64; 3]>, + pub as_shot_white_xy: Option<[f64; 2]>, + pub baseline_exposure: f64, + pub baseline_sharpness: Option, + pub profile: &'a crate::profile::ProfileLook, +} + +impl<'a> From<&'a crate::ColorData> for DngColor<'a> { + fn from(color: &'a crate::ColorData) -> Self { + Self { + illuminant: color.illuminant, + color_matrix: color.color_matrix, + forward_matrix: color.forward_matrix, + camera_calibration: color.camera_calibration, + analog_balance: color.analog_balance, + as_shot_neutral: color.as_shot_neutral, + as_shot_white_xy: color.as_shot_white_xy, + baseline_exposure: color.baseline_exposure, + baseline_sharpness: color.baseline_sharpness, + profile: &color.profile, + } + } +} + +/// Borrowed wire-format input for callers with their own application image type. +/// Pixel buffers and large profile/opcode tables are not copied. +pub struct DngImage<'a> { + pub width: usize, + pub height: usize, + pub cpp: usize, + pub data: &'a RawData, + pub cfa: Option<&'a crate::Cfa>, + pub bits: u32, + pub black: &'a crate::BlackLevel, + pub white: &'a [f32], + pub active_area: crate::Rect, + pub crop: crate::Rect, + pub orientation: crate::Orientation, + pub color: DngColor<'a>, + pub wb_multipliers: Option<[f32; 3]>, + pub opcodes: &'a crate::OpcodeLists, + pub make: Option<&'a str>, + pub model: Option<&'a str>, + pub unique_model: Option<&'a str>, + pub cinema: &'a crate::CinemaTags, + /// Optional application-supplied UniqueCameraModel fallback when make/model are absent. + pub unknown_camera_model: Option<&'a str>, + /// Whether `data` has already been mapped through `linearization_table` (see + /// [`RawImage::linearized`]). + pub linearized: bool, + /// A LinearizationTable still to be applied by the reader; written only when `linearized` is false. + pub linearization_table: Option<&'a [u16]>, +} + +impl<'a> From<&'a RawImage> for DngImage<'a> { + fn from(raw: &'a RawImage) -> Self { + Self { + width: raw.width, + height: raw.height, + cpp: raw.cpp, + data: &raw.data, + cfa: raw.cfa.as_ref(), + bits: raw.bits, + black: &raw.black, + white: &raw.white, + active_area: raw.active_area, + crop: raw.crop, + orientation: raw.orientation, + color: DngColor::from(&raw.color), + wb_multipliers: raw.wb_multipliers, + opcodes: &raw.opcodes, + make: raw.camera.make.as_deref(), + model: raw.camera.model.as_deref(), + unique_model: raw.camera.unique_model.as_deref(), + cinema: &raw.cinema, + unknown_camera_model: None, + linearized: raw.linearized, + linearization_table: raw.linearization_table.as_deref(), + } + } +} + +impl DngImage<'_> { + fn validate(&self) -> Result<()> { + crate::validate_image_layout(self.width, self.height, self.cpp, Some(self.data.len()), self.cfa, self.active_area)?; + if self.width > u32::MAX as usize || self.height > u32::MAX as usize { + return Err(RawError::Limit("DNG dimensions exceed TIFF fields")); + } + Ok(()) + } + + fn white_at(&self, sample: usize) -> f32 { + self.white.get(sample).or_else(|| self.white.first()).copied().unwrap_or(65535.0) + } +} + +/// Write an image without taking ownership of its pixels or application metadata. +pub fn write_dng_view(raw: &DngImage<'_>, opts: &DngWriteOptions) -> Result> { + write_image(raw, opts, None) +} + +/// Encode prepared 8-bit RGB codes as lossy DNG JPEG tiles. Rendering, scaling and the +/// transfer curve are application policy; `linearization` supplies exactly 256 code values. +/// The callback receives one bounded RGB tile and must return its JPEG encoding or an error. +/// It can be invoked concurrently. The callback is a trusted encoder: it owns its output +/// allocation and JPEG-validity contract; this API does not impose an aggregate output budget. +/// Only the byte order, XMP and Software fields of `opts` +/// apply here; `tile` and `quality` select the compression instead of `opts.compression`. +pub fn write_lossy_dng( + raw: &DngImage<'_>, + opts: &DngWriteOptions, + tile: u32, + quality: u8, + linearization: &[u16], + encode: &(impl Fn(Vec, u32, u8) -> Result> + Sync), +) -> Result> { + write_image(raw, opts, Some(LossyTiles { tile, quality, linearization, encode })) +} + +type JpegTileEncoder<'a> = dyn Fn(Vec, u32, u8) -> Result> + Sync + 'a; +struct LossyTiles<'a> { + tile: u32, + quality: u8, + linearization: &'a [u16], + encode: &'a JpegTileEncoder<'a>, +} + +fn lossy_tile_edge(tile: u32) -> usize { + (tile.clamp(16, 4096) & !15) as usize +} + +fn set_lossy_tiles(ifd: &mut IfdBuilder, raw: &DngImage<'_>, options: LossyTiles<'_>) -> Result<()> { + let RawData::U16(samples) = raw.data else { return Err(RawError::Unsupported("lossy DNG requires prepared 8-bit RGB codes".into())) }; + if raw.cpp != 3 || raw.cfa.is_some() || options.linearization.len() != 256 || samples.iter().any(|&v| v > 255) { + return Err(RawError::Unsupported("lossy DNG requires prepared 8-bit RGB codes and a 256-entry linearization table".into())); + } + // Preserve the existing profile-export limit: <=4096² RGB bytes per worker, never + // a caller-supplied unbounded square. Existing codec output/worker budgets still apply. + let tile = lossy_tile_edge(options.tile); + let (across, down) = (raw.width.div_ceil(tile), raw.height.div_ceil(tile)); + let count = across.checked_mul(down).ok_or(RawError::Limit("too many DNG tiles"))?; + let mut tiles = Vec::new(); + tiles.try_reserve_exact(count).map_err(|_| RawError::Limit("DNG tile table allocation failed"))?; + tiles.resize_with(count, Vec::new); + tiles.par_iter_mut().enumerate().try_for_each(|(i, output)| -> Result<()> { + let (tx, ty) = (i % across * tile, i / across * tile); + let mut pixels = Vec::new(); + pixels.try_reserve_exact(tile * tile * 3).map_err(|_| RawError::Limit("DNG tile allocation failed"))?; + for y in 0..tile { + let sy = (ty + y).min(raw.height - 1); + for x in 0..tile { + let sx = (tx + x).min(raw.width - 1); + pixels.extend(samples[(sy * raw.width + sx) * 3..][..3].iter().map(|&v| v as u8)); + } + } + *output = (options.encode)(pixels, tile as u32, options.quality.clamp(1, 100))?; + Ok(()) + })?; + ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![8; 3])); + ifd.set(t::COMPRESSION, Value::Short(vec![compression::LOSSY_JPEG])); + ifd.set(t::LINEARIZATION_TABLE, Value::Short(options.linearization.to_vec())); + ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); + Ok(()) +} + /// Serialise `raw` as a DNG. pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result> { + write_dng_view(&DngImage::from(raw), opts) +} + +fn write_image(raw: &DngImage<'_>, opts: &DngWriteOptions, lossy: Option>) -> Result> { raw.validate()?; if opts.software.trim() == "LightCraft" { return Err(RawError::Unsupported("the Software tag `LightCraft` marks files with a sensor-anchored CFAPattern".into())); @@ -137,7 +313,7 @@ pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result> { ifd.set(t::DNG_BACKWARD_VERSION, Value::Byte(vec![1, 3, 0, 0])); write_identity_tags(&mut ifd, raw, opts); - if let Some(cfa) = &raw.cfa { + if let Some(cfa) = raw.cfa { ifd.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![cfa.height as u16, cfa.width as u16])); // the tag is anchored at the ActiveArea's top-left, `raw.cfa` at the sensor origin let a = raw.active_area; @@ -145,7 +321,7 @@ pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result> { ifd.set(t::CFA_PLANE_COLOR, Value::Byte(vec![0, 1, 2])); ifd.set(t::CFA_LAYOUT, Value::Short(vec![1])); } - let b = &raw.black; + let b = raw.black; ifd.set(t::BLACK_LEVEL_REPEAT_DIM, Value::Short(vec![b.repeat_rows as u16, b.repeat_cols as u16])); let mut bl: Vec = b.values.iter().map(|&v| v as f64).collect(); if bl.len() == 1 && cpp > 1 { @@ -169,7 +345,7 @@ pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result> { // with the samples. Once the samples have been mapped, writing the tag would apply it twice // on the next read of a file without list 1. if !raw.linearized - && let Some(table) = raw.linearization_table.as_deref().filter(|t| !t.is_empty()) + && let Some(table) = raw.linearization_table.filter(|t| !t.is_empty()) { ifd.set(t::LINEARIZATION_TABLE, Value::Short(table.to_vec())); } @@ -182,135 +358,139 @@ pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result> { write_profile_tags(&mut ifd, raw, opts.order); write_opcode_tags(&mut ifd, raw); - match (&raw.data, opts.compression) { - (RawData::F32(v), DngCompression::Deflate { tile, half }) => { - let tile = deflate_tile(tile); - let bp = if half { 2 } else { 4 }; - ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![(bp * 8) as u16; cpp])); - ifd.set(t::SAMPLE_FORMAT, Value::Short(vec![3; cpp])); - ifd.set(t::COMPRESSION, Value::Short(vec![compression::ADOBE_DEFLATE])); - ifd.set(t::PREDICTOR, Value::Short(vec![3])); - let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); - let tiles: Vec> = (0..ta * td) - .into_par_iter() - .map(|i| { - let (tx, ty) = (i % ta * tile, i / ta * tile); - let mut buf = Vec::with_capacity(tile * tile * cpp * bp); - let mut row = Vec::with_capacity(tile * cpp * bp); - for y in 0..tile { - let sy = (ty + y).min(h - 1); - row.clear(); - for x in 0..tile { - let sx = (tx + x).min(w - 1); - for s in 0..cpp { - let f = v[(sy * w + sx) * cpp + s]; - if half { - row.extend_from_slice(&f32_to_f16(f).to_be_bytes()); - } else { - row.extend_from_slice(&f.to_bits().to_be_bytes()); + if let Some(lossy) = lossy { + set_lossy_tiles(&mut ifd, raw, lossy)?; + } else { + match (raw.data, opts.compression) { + (RawData::F32(v), DngCompression::Deflate { tile, half }) => { + let tile = deflate_tile(tile); + let bp = if half { 2 } else { 4 }; + ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![(bp * 8) as u16; cpp])); + ifd.set(t::SAMPLE_FORMAT, Value::Short(vec![3; cpp])); + ifd.set(t::COMPRESSION, Value::Short(vec![compression::ADOBE_DEFLATE])); + ifd.set(t::PREDICTOR, Value::Short(vec![3])); + let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); + let tiles: Vec> = (0..ta * td) + .into_par_iter() + .map(|i| { + let (tx, ty) = (i % ta * tile, i / ta * tile); + let mut buf = Vec::with_capacity(tile * tile * cpp * bp); + let mut row = Vec::with_capacity(tile * cpp * bp); + for y in 0..tile { + let sy = (ty + y).min(h - 1); + row.clear(); + for x in 0..tile { + let sx = (tx + x).min(w - 1); + for s in 0..cpp { + let f = v[(sy * w + sx) * cpp + s]; + if half { + row.extend_from_slice(&f32_to_f16(f).to_be_bytes()); + } else { + row.extend_from_slice(&f.to_bits().to_be_bytes()); + } } } + buf.extend_from_slice(&float_predict(&row, tile * cpp, bp, cpp)); } - buf.extend_from_slice(&float_predict(&row, tile * cpp, bp, cpp)); - } - miniz_oxide::deflate::compress_to_vec_zlib(&buf, DEFLATE_LEVEL) - }) - .collect(); - ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); - } - (RawData::F32(v), _) => { - ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![32; cpp])); - ifd.set(t::SAMPLE_FORMAT, Value::Short(vec![3; cpp])); - ifd.set(t::COMPRESSION, Value::Short(vec![compression::NONE])); - let mut bytes = Vec::with_capacity(v.len() * 4); - for &x in v { - let mut tmp = Vec::with_capacity(4); - opts.order.put_u32(&mut tmp, x.to_bits()); - bytes.extend_from_slice(&tmp); + miniz_oxide::deflate::compress_to_vec_zlib(&buf, DEFLATE_LEVEL) + }) + .collect(); + ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); } - ifd.set_image(ImageData::Strips { rows_per_strip: h as u32, strips: vec![bytes] }); - } - (RawData::U16(v), _) => { - // Dark frames may never reach the sensor's white level, which must still fit. - let maxv = u32::from(v.iter().copied().max().unwrap_or(0)).max(max_white); - let bits = (32 - maxv.leading_zeros()).max(raw.bits).clamp(2, 16) as u8; - ifd.set( - t::BITS_PER_SAMPLE, - Value::Short(vec![if matches!(opts.compression, DngCompression::Uncompressed) { 16 } else { bits as u16 }; cpp]), - ); - match opts.compression { - DngCompression::Uncompressed => { - ifd.set(t::COMPRESSION, Value::Short(vec![compression::NONE])); - let rows = (65536 / (w * cpp * 2)).clamp(1, h); - let strips = v - .chunks(rows * w * cpp) - .map(|s| { - let mut b = Vec::with_capacity(s.len() * 2); - s.iter().for_each(|&x| opts.order.put_u16(&mut b, x)); - b - }) - .collect(); - ifd.set_image(ImageData::Strips { rows_per_strip: rows as u32, strips }); + (RawData::F32(v), _) => { + ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![32; cpp])); + ifd.set(t::SAMPLE_FORMAT, Value::Short(vec![3; cpp])); + ifd.set(t::COMPRESSION, Value::Short(vec![compression::NONE])); + let mut bytes = Vec::with_capacity(v.len() * 4); + for &x in v { + let mut tmp = Vec::with_capacity(4); + opts.order.put_u32(&mut tmp, x.to_bits()); + bytes.extend_from_slice(&tmp); } - DngCompression::Deflate { tile, .. } => { - let tile = deflate_tile(tile); - ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![16; cpp])); - ifd.set(t::COMPRESSION, Value::Short(vec![compression::ADOBE_DEFLATE])); - ifd.set(t::PREDICTOR, Value::Short(vec![2])); - let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); - let tiles: Vec> = (0..ta * td) - .into_par_iter() - .map(|i| { - let (tx, ty) = (i % ta * tile, i / ta * tile); - let mut buf = Vec::with_capacity(tile * tile * cpp * 2); - let mut row = vec![0u16; tile * cpp]; - for y in 0..tile { - let sy = (ty + y).min(h - 1); - for x in 0..tile { - let sx = (tx + x).min(w - 1); - for s in 0..cpp { - row[x * cpp + s] = v[(sy * w + sx) * cpp + s]; - } - } - for i in (cpp..row.len()).rev() { - row[i] = row[i].wrapping_sub(row[i - cpp]); - } - row.iter().for_each(|&x| opts.order.put_u16(&mut buf, x)); - } - miniz_oxide::deflate::compress_to_vec_zlib(&buf, DEFLATE_LEVEL) - }) - .collect(); - ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); - } - DngCompression::Lj92 { tile } => { - let tile = (tile.max(2) & !1) as usize; - if tile > MAX_LJ92_TILE { - return Err(RawError::Limit("tile too large")); + ifd.set_image(ImageData::Strips { rows_per_strip: h as u32, strips: vec![bytes] }); + } + (RawData::U16(v), _) => { + // Dark frames may never reach the sensor's white level, which must still fit. + let maxv = u32::from(v.iter().copied().max().unwrap_or(0)).max(max_white); + let bits = (32 - maxv.leading_zeros()).max(raw.bits).clamp(2, 16) as u8; + ifd.set( + t::BITS_PER_SAMPLE, + Value::Short(vec![if matches!(opts.compression, DngCompression::Uncompressed) { 16 } else { bits as u16 }; cpp]), + ); + match opts.compression { + DngCompression::Uncompressed => { + ifd.set(t::COMPRESSION, Value::Short(vec![compression::NONE])); + let rows = (65536 / (w * cpp * 2)).clamp(1, h); + let strips = v + .chunks(rows * w * cpp) + .map(|s| { + let mut b = Vec::with_capacity(s.len() * 2); + s.iter().for_each(|&x| opts.order.put_u16(&mut b, x)); + b + }) + .collect(); + ifd.set_image(ImageData::Strips { rows_per_strip: rows as u32, strips }); } - ifd.set(t::COMPRESSION, Value::Short(vec![compression::JPEG])); - let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); - let tiles: Vec> = (0..ta * td) - .into_par_iter() - .map(|i| { - let (tx, ty) = (i % ta * tile, i / ta * tile); - let mut buf = Vec::with_capacity(tile * tile * cpp); - for y in 0..tile { - let sy = (ty + y).min(h - 1); - for x in 0..tile { - let sx = (tx + x).min(w - 1); - for s in 0..cpp { - buf.push(v[(sy * w + sx) * cpp + s]); + DngCompression::Deflate { tile, .. } => { + let tile = deflate_tile(tile); + ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![16; cpp])); + ifd.set(t::COMPRESSION, Value::Short(vec![compression::ADOBE_DEFLATE])); + ifd.set(t::PREDICTOR, Value::Short(vec![2])); + let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); + let tiles: Vec> = (0..ta * td) + .into_par_iter() + .map(|i| { + let (tx, ty) = (i % ta * tile, i / ta * tile); + let mut buf = Vec::with_capacity(tile * tile * cpp * 2); + let mut row = vec![0u16; tile * cpp]; + for y in 0..tile { + let sy = (ty + y).min(h - 1); + for x in 0..tile { + let sx = (tx + x).min(w - 1); + for s in 0..cpp { + row[x * cpp + s] = v[(sy * w + sx) * cpp + s]; + } + } + for i in (cpp..row.len()).rev() { + row[i] = row[i].wrapping_sub(row[i - cpp]); + } + row.iter().for_each(|&x| opts.order.put_u16(&mut buf, x)); + } + miniz_oxide::deflate::compress_to_vec_zlib(&buf, DEFLATE_LEVEL) + }) + .collect(); + ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); + } + DngCompression::Lj92 { tile } => { + let tile = (tile.max(2) & !1) as usize; + if tile > MAX_LJ92_TILE { + return Err(RawError::Limit("tile too large")); + } + ifd.set(t::COMPRESSION, Value::Short(vec![compression::JPEG])); + let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); + let tiles: Vec> = (0..ta * td) + .into_par_iter() + .map(|i| { + let (tx, ty) = (i % ta * tile, i / ta * tile); + let mut buf = Vec::with_capacity(tile * tile * cpp); + for y in 0..tile { + let sy = (ty + y).min(h - 1); + for x in 0..tile { + let sx = (tx + x).min(w - 1); + for s in 0..cpp { + buf.push(v[(sy * w + sx) * cpp + s]); + } } } - } - if cpp == 1 { - ljpeg::encode(&buf, tile / 2, tile, 2, bits, 1, 0) - } else { - ljpeg::encode(&buf, tile, tile, cpp, bits, 1, 0) - } - }) - .collect(); - ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); + if cpp == 1 { + ljpeg::encode(&buf, tile / 2, tile, 2, bits, 1, 0) + } else { + ljpeg::encode(&buf, tile, tile, cpp, bits, 1, 0) + } + }) + .collect(); + ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); + } } } } @@ -319,9 +499,9 @@ pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result> { } /// Make, Model, UniqueCameraModel, Orientation, Software and the XMP packet. -fn write_identity_tags(ifd: &mut IfdBuilder, raw: &RawImage, opts: &DngWriteOptions) { - let make = raw.camera.make.as_deref().unwrap_or_default(); - let model = raw.camera.model.as_deref().unwrap_or_default(); +fn write_identity_tags(ifd: &mut IfdBuilder, raw: &DngImage<'_>, opts: &DngWriteOptions) { + let make = raw.make.unwrap_or_default(); + let model = raw.model.unwrap_or_default(); if !make.is_empty() { ifd.set(t::MAKE, Value::Ascii(make.to_owned())); } @@ -331,7 +511,7 @@ fn write_identity_tags(ifd: &mut IfdBuilder, raw: &RawImage, opts: &DngWriteOpti let model_includes_make = !make.is_empty() && model.get(..make.len()).is_some_and(|prefix| prefix.eq_ignore_ascii_case(make)) && model.get(make.len()..).is_some_and(|suffix| suffix.chars().next().is_none_or(|c| !c.is_alphanumeric() && c != '_')); - let unique = if let Some(unique) = raw.camera.unique_model.as_deref().filter(|u| !u.is_empty()) { + let unique = if let Some(unique) = raw.unique_model.filter(|u| !u.is_empty()) { unique.to_owned() } else if model_includes_make || make.is_empty() { model.to_owned() @@ -340,7 +520,7 @@ fn write_identity_tags(ifd: &mut IfdBuilder, raw: &RawImage, opts: &DngWriteOpti } else { format!("{make} {model}") }; - ifd.set(t::UNIQUE_CAMERA_MODEL, Value::Ascii(if unique.is_empty() { "Unknown".into() } else { unique })); + ifd.set(t::UNIQUE_CAMERA_MODEL, Value::Ascii(if unique.is_empty() { raw.unknown_camera_model.unwrap_or("Unknown").into() } else { unique })); ifd.set(t::ORIENTATION, Value::Short(vec![raw.orientation.to_exif()])); ifd.set(t::SOFTWARE, Value::Ascii(opts.software.clone())); if let Some(xmp) = &opts.xmp { @@ -349,7 +529,7 @@ fn write_identity_tags(ifd: &mut IfdBuilder, raw: &RawImage, opts: &DngWriteOpti } /// The colour tags (matrices, white balance, baseline exposure and sharpness) and the CinemaDNG frame tags. -fn write_color_tags(ifd: &mut IfdBuilder, raw: &RawImage) { +fn write_color_tags(ifd: &mut IfdBuilder, raw: &DngImage<'_>) { let c = &raw.color; for (i, tag) in [t::CALIBRATION_ILLUMINANT_1, t::CALIBRATION_ILLUMINANT_2].into_iter().enumerate() { if c.color_matrix[i].is_some() { @@ -393,9 +573,9 @@ fn write_color_tags(ifd: &mut IfdBuilder, raw: &RawImage) { } } -fn write_profile_tags(ifd: &mut IfdBuilder, raw: &RawImage, order: ByteOrder) { +fn write_profile_tags(ifd: &mut IfdBuilder, raw: &DngImage<'_>, order: ByteOrder) { // The source's own profile look travels with its data (conversions, smart previews, merges). - let profile = &raw.color.profile; + let profile = raw.color.profile; let table_tags = |ifd: &mut IfdBuilder, table: &crate::profile::HsvTable, dims: u16, enc: u16| { ifd.set(dims, Value::Long(vec![table.hue_divisions as u32, table.sat_divisions as u32, table.val_divisions as u32])); if table.srgb_value { @@ -433,7 +613,7 @@ fn write_profile_tags(ifd: &mut IfdBuilder, raw: &RawImage, order: ByteOrder) { } } -fn write_opcode_tags(ifd: &mut IfdBuilder, raw: &RawImage) { +fn write_opcode_tags(ifd: &mut IfdBuilder, raw: &DngImage<'_>) { for (list, tag) in [(&raw.opcodes.list1, t::OPCODE_LIST_1), (&raw.opcodes.list2, t::OPCODE_LIST_2), (&raw.opcodes.list3, t::OPCODE_LIST_3)] { if !list.is_empty() { ifd.set(tag, Value::Undefined(opcodes::write_list(list))); @@ -441,6 +621,10 @@ fn write_opcode_tags(ifd: &mut IfdBuilder, raw: &RawImage) { } } +#[cfg(test)] +#[path = "dngwrite_legacy_tests.rs"] +mod legacy; + #[cfg(test)] mod tests { use super::*; @@ -476,6 +660,80 @@ mod tests { } } + #[test] + fn borrowed_writer_preserves_owned_output() { + let mut raw = tiny_raw(); + raw.camera.unique_model = Some("Exact camera model".into()); + for floating in [false, true] { + if floating { + raw.data = RawData::F32((0..64).map(|v| v as f32 / 64.0).collect()); + raw.white = vec![1.0]; + } + for order in [ByteOrder::Little, ByteOrder::Big] { + for (compression, old_compression) in [ + (DngCompression::Uncompressed, legacy::DngCompression::Uncompressed), + (DngCompression::Lj92 { tile: 16 }, legacy::DngCompression::Lj92 { tile: 16 }), + (DngCompression::Deflate { tile: 16, half: false }, legacy::DngCompression::Deflate { tile: 16, half: false }), + (DngCompression::Deflate { tile: 16, half: true }, legacy::DngCompression::Deflate { tile: 16, half: true }), + ] { + let xmp = Some("retained".to_string()); + let options = DngWriteOptions { compression, order, xmp: xmp.clone(), software: "Test writer".into() }; + let old_options = legacy::DngWriteOptions { compression: old_compression, order, xmp, software: "Test writer".into() }; + let expected = legacy::write_dng(&raw, &old_options).unwrap(); + assert_eq!(expected, write_dng_view(&DngImage::from(&raw), &options).unwrap()); + assert_eq!(expected, write_dng(&raw, &options).unwrap()); + } + } + } + } + + #[test] + fn lossy_tiles_preserve_prepared_codes_padding_and_tags() { + let mut raw = tiny_raw(); + raw.width = 2; + raw.height = 1; + raw.cpp = 3; + raw.bits = 8; + raw.data = RawData::U16(vec![1, 2, 3, 250, 251, 252]); + raw.active_area = Rect::new(0, 0, 2, 1); + raw.crop = raw.active_area; + let linearization: Vec = (0..256).map(|v| v * 257).collect(); + let called = std::sync::atomic::AtomicUsize::new(0); + let encoder = |pixels: Vec, tile: u32, quality: u8| { + called.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + assert_eq!((tile, quality), (16, 100)); + for row in pixels.as_chunks::<{ 16 * 3 }>().0 { + assert_eq!(&row[..3], &[1, 2, 3]); + assert!(row[3..].as_chunks::<3>().0.iter().all(|p| *p == [250, 251, 252])); + } + let mut output = Vec::new(); + jpeg_encoder::Encoder::new(&mut output, quality) + .encode(&pixels, tile as u16, tile as u16, jpeg_encoder::ColorType::Rgb) + .map_err(|e| RawError::Corrupt(e.to_string()))?; + Ok(output) + }; + let bytes = write_lossy_dng(&DngImage::from(&raw), &DngWriteOptions::default(), 17, 255, &linearization, &encoder).unwrap(); + assert_eq!(called.load(std::sync::atomic::Ordering::Relaxed), 1); + let tiff = craft_tiff::Tiff::parse(&bytes).unwrap(); + let ifd = tiff.all_ifds()[0]; + assert_eq!(ifd.u16(t::COMPRESSION), Some(compression::LOSSY_JPEG)); + assert_eq!(ifd.u64s(t::LINEARIZATION_TABLE).unwrap(), linearization.iter().map(|&v| u64::from(v)).collect::>()); + assert_eq!((crate::decode(&bytes).unwrap().width, crate::decode(&bytes).unwrap().height), (2, 1)); + assert_eq!(lossy_tile_edge(u32::MAX), 4096); + assert_eq!(lossy_tile_edge(0), 16); + let failing = |_: Vec, _: u32, _: u8| Err(RawError::Limit("test encoder failure")); + assert!(matches!( + write_lossy_dng(&DngImage::from(&raw), &DngWriteOptions::default(), 16, 90, &linearization, &failing), + Err(RawError::Limit("test encoder failure")) + )); + let never = |_: Vec, _: u32, _: u8| -> Result> { panic!("malformed input reached encoder") }; + assert!(write_lossy_dng(&DngImage::from(&raw), &DngWriteOptions::default(), 16, 90, &linearization[..255], &never).is_err()); + raw.data = RawData::U16(vec![256; 6]); + assert!(write_lossy_dng(&DngImage::from(&raw), &DngWriteOptions::default(), 16, 90, &linearization, &never).is_err()); + raw.data = RawData::U16(vec![1; 5]); + assert!(write_lossy_dng(&DngImage::from(&raw), &DngWriteOptions::default(), 16, 90, &linearization, &never).is_err()); + } + #[test] fn oversized_lj92_padding_is_rejected() { let raw = tiny_raw(); diff --git a/crates/raw/src/dngwrite_legacy_tests.rs b/crates/raw/src/dngwrite_legacy_tests.rs new file mode 100644 index 000000000..f0eda677a --- /dev/null +++ b/crates/raw/src/dngwrite_legacy_tests.rs @@ -0,0 +1,435 @@ +// Frozen pre-extraction writer: test-only byte-parity oracle. +//! Writing DNG files from a [`RawImage`] (uncompressed strips, lossless-JPEG tiles or Deflate tiles), used for DNG +//! export and for bit-exact decoder round-trip tests. Lossy DNG and DNGs of rendered pictures start from a developed +//! image, so the develop pipeline builds those on top of this writer. The raw image is stored in IFD0 (allowed by DNG 1.7 §"File Structure"). + +use crate::{Matrix3, RawData, RawError, RawImage, Result, ljpeg, opcodes}; +use craft_tiff::tags::{self as t, compression, photometric}; +use craft_tiff::writer::{rational, srational}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value}; +use rayon::prelude::*; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum DngCompression { + Uncompressed, + /// Lossless JPEG tiles of `tile × tile` pixels (`tile` even). CFA tiles are encoded as two interleaved + /// components of half the tile width (the common DNG layout), linear data with one component per sample. + /// Edges above 2048 (after even alignment) are rejected before padding the tile. + Lj92 { + tile: u32, + }, + /// Deflate (zlib) tiles of `tile × tile` pixels. Floating-point data is stored as 16-bit (`half`) or + /// 32-bit IEEE floats with the floating-point predictor (TIFF predictor 3, Adobe TIFF Technical Note 3); + /// integer data with horizontal differencing (predictor 2). 16-bit float is what HDR merges usually use. + Deflate { + tile: u32, + half: bool, + }, +} + +/// zlib level of Deflate tiles. On a 67 MP focus-stack merge (half floats) level 6 took 7× as long as level 1 +/// (3.9 s vs 0.55 s on 4 cores) to save 3 % of the file (266 vs 275 MB); 5–12 % on raw and rendered DNGs. +const DEFLATE_LEVEL: u8 = 1; + +/// Largest Deflate tile edge the writer pads to (`tile` is caller-supplied in [`DngCompression::Deflate`]): +/// square-tile buffers grow with the square, so an unclamped edge lets a request pad a tiny valid image into +/// a multi-gigabyte buffer and billions of edge-replicate iterations. 2048² samples (≤ 24 MB of f32) is far +/// above the 256 default and every real tile this writer makes. +const MAX_DEFLATE_TILE: u32 = 2048; + +// Match the existing padded Deflate edge budget. JPEG's 16-bit dimension limit alone would permit +// a 65534²-sample allocation even for a tiny source image. At most four validated channels make +// this bound at most 32 MiB of u16 padding per tile, before encoder scratch/output. +const MAX_LJ92_TILE: usize = MAX_DEFLATE_TILE as usize; + +/// `tile` aligned down to a multiple of 16 and clamped to [`MAX_DEFLATE_TILE`]. +fn deflate_tile(tile: u32) -> usize { + (tile.max(16) & !15).min(MAX_DEFLATE_TILE) as usize +} + +/// IEEE 754 binary32 → binary16 (round to nearest even; overflow → ±inf, NaN kept). +pub fn f32_to_f16(v: f32) -> u16 { + let b = v.to_bits(); + let sign = ((b >> 16) & 0x8000) as u16; + let exp = ((b >> 23) & 0xff) as i32; + let man = b & 0x7f_ffff; + if exp == 0xff { + return sign | 0x7c00 | if man != 0 { 0x200 } else { 0 }; + } + let e = exp - 127 + 15; + if e >= 0x1f { + return sign | 0x7c00; + } + if e <= 0 { + if e < -10 { + return sign; + } + // subnormal half + let m = man | 0x80_0000; + let shift = (14 - e) as u32; + let half = m >> shift; + let rem = m & ((1 << shift) - 1); + let mid = 1 << (shift - 1); + let r = if rem > mid || (rem == mid && half & 1 == 1) { half + 1 } else { half }; + return sign | r as u16; + } + let half = ((e as u32) << 10) | (man >> 13); + let rem = man & 0x1fff; + let r = if rem > 0x1000 || (rem == 0x1000 && half & 1 == 1) { half + 1 } else { half }; + sign | r as u16 +} + +/// Apply the floating-point predictor (inverse of `unpack::undo_float_predictor`) to one row of +/// `n` big-endian samples of `bytes_per` bytes: split into byte planes (most significant first), then +/// byte-difference with `stride` samples. +fn float_predict(row: &[u8], n: usize, bytes_per: usize, stride: usize) -> Vec { + let mut planes = vec![0u8; row.len()]; + for i in 0..n { + for b in 0..bytes_per { + planes[b * n + i] = row[i * bytes_per + b]; + } + } + for i in (stride..planes.len()).rev() { + planes[i] = planes[i].wrapping_sub(planes[i - stride]); + } + planes +} + +#[derive(Clone, Debug, PartialEq)] +pub struct DngWriteOptions { + pub compression: DngCompression, + pub order: ByteOrder, + /// XMP packet to embed (tag 700); none when `None`. + pub xmp: Option, + /// The `Software` tag: the writing application. It must not be the bare string `LightCraft`, which the reader + /// takes for that app's early files (their `CFAPattern` is anchored at the sensor origin, not the ActiveArea). + pub software: String, +} + +impl Default for DngWriteOptions { + fn default() -> Self { + Self { compression: DngCompression::Lj92 { tile: 256 }, order: ByteOrder::Little, xmp: None, software: "craft-raw".into() } + } +} + +fn srat_mat(m: &Matrix3) -> Value { + Value::SRational(m.iter().flatten().map(|&v| srational(v)).collect()) +} + +fn rat_vec(v: &[f64]) -> Value { + Value::Rational(v.iter().map(|&x| rational(x)).collect()) +} + +/// Serialise `raw` as a DNG. +pub fn write_dng(raw: &RawImage, opts: &DngWriteOptions) -> Result> { + raw.validate()?; + if opts.software.trim() == "LightCraft" { + return Err(RawError::Unsupported("the Software tag `LightCraft` marks files with a sensor-anchored CFAPattern".into())); + } + let (w, h, cpp) = (raw.width, raw.height, raw.cpp); + let mut ifd = IfdBuilder::new(); + ifd.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0])); + ifd.set(t::IMAGE_WIDTH, Value::Long(vec![w as u32])); + ifd.set(t::IMAGE_LENGTH, Value::Long(vec![h as u32])); + ifd.set(t::SAMPLES_PER_PIXEL, Value::Short(vec![cpp as u16])); + ifd.set(t::PLANAR_CONFIGURATION, Value::Short(vec![1])); + ifd.set(t::PHOTOMETRIC, Value::Short(vec![if raw.cfa.is_some() { photometric::CFA } else { photometric::LINEAR_RAW }])); + ifd.set(t::DNG_VERSION, Value::Byte(vec![1, 4, 0, 0])); + ifd.set(t::DNG_BACKWARD_VERSION, Value::Byte(vec![1, 3, 0, 0])); + write_identity_tags(&mut ifd, raw, opts); + + if let Some(cfa) = &raw.cfa { + ifd.set(t::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![cfa.height as u16, cfa.width as u16])); + // the tag is anchored at the ActiveArea's top-left, `raw.cfa` at the sensor origin + let a = raw.active_area; + ifd.set(t::CFA_PATTERN_EP, Value::Byte(cfa.shifted(a.x, a.y).pattern)); + ifd.set(t::CFA_PLANE_COLOR, Value::Byte(vec![0, 1, 2])); + ifd.set(t::CFA_LAYOUT, Value::Short(vec![1])); + } + let b = &raw.black; + ifd.set(t::BLACK_LEVEL_REPEAT_DIM, Value::Short(vec![b.repeat_rows as u16, b.repeat_cols as u16])); + let mut bl: Vec = b.values.iter().map(|&v| v as f64).collect(); + if bl.len() == 1 && cpp > 1 { + bl = vec![bl[0]; cpp]; + } + ifd.set(t::BLACK_LEVEL, rat_vec(&bl)); + if !b.delta_h.is_empty() { + ifd.set(t::BLACK_LEVEL_DELTA_H, Value::SRational(b.delta_h.iter().map(|&v| srational(v as f64)).collect())); + } + if !b.delta_v.is_empty() { + ifd.set(t::BLACK_LEVEL_DELTA_V, Value::SRational(b.delta_v.iter().map(|&v| srational(v as f64)).collect())); + } + let white: Vec = (0..cpp).map(|s| raw.white_at(s).round().clamp(0.0, u32::MAX as f32) as u32).collect(); + let max_white = white.iter().copied().max().unwrap_or(0); + if matches!(raw.data, RawData::F32(_)) { + ifd.set(t::WHITE_LEVEL, rat_vec(&(0..cpp).map(|s| raw.white_at(s) as f64).collect::>())); + } else { + ifd.set(t::WHITE_LEVEL, Value::Long(white)); + } + let a = raw.active_area; + ifd.set(t::ACTIVE_AREA, Value::Long(vec![a.y as u32, a.x as u32, (a.y + a.height) as u32, (a.x + a.width) as u32])); + ifd.set(t::DEFAULT_CROP_ORIGIN, Value::Long(vec![raw.crop.x as u32, raw.crop.y as u32])); + ifd.set(t::DEFAULT_CROP_SIZE, Value::Long(vec![raw.crop.width as u32, raw.crop.height as u32])); + + write_color_tags(&mut ifd, raw); + write_profile_tags(&mut ifd, raw, opts.order); + write_opcode_tags(&mut ifd, raw); + + match (&raw.data, opts.compression) { + (RawData::F32(v), DngCompression::Deflate { tile, half }) => { + let tile = deflate_tile(tile); + let bp = if half { 2 } else { 4 }; + ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![(bp * 8) as u16; cpp])); + ifd.set(t::SAMPLE_FORMAT, Value::Short(vec![3; cpp])); + ifd.set(t::COMPRESSION, Value::Short(vec![compression::ADOBE_DEFLATE])); + ifd.set(t::PREDICTOR, Value::Short(vec![3])); + let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); + let tiles: Vec> = (0..ta * td) + .into_par_iter() + .map(|i| { + let (tx, ty) = (i % ta * tile, i / ta * tile); + let mut buf = Vec::with_capacity(tile * tile * cpp * bp); + let mut row = Vec::with_capacity(tile * cpp * bp); + for y in 0..tile { + let sy = (ty + y).min(h - 1); + row.clear(); + for x in 0..tile { + let sx = (tx + x).min(w - 1); + for s in 0..cpp { + let f = v[(sy * w + sx) * cpp + s]; + if half { + row.extend_from_slice(&f32_to_f16(f).to_be_bytes()); + } else { + row.extend_from_slice(&f.to_bits().to_be_bytes()); + } + } + } + buf.extend_from_slice(&float_predict(&row, tile * cpp, bp, cpp)); + } + miniz_oxide::deflate::compress_to_vec_zlib(&buf, DEFLATE_LEVEL) + }) + .collect(); + ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); + } + (RawData::F32(v), _) => { + ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![32; cpp])); + ifd.set(t::SAMPLE_FORMAT, Value::Short(vec![3; cpp])); + ifd.set(t::COMPRESSION, Value::Short(vec![compression::NONE])); + let mut bytes = Vec::with_capacity(v.len() * 4); + for &x in v { + let mut tmp = Vec::with_capacity(4); + opts.order.put_u32(&mut tmp, x.to_bits()); + bytes.extend_from_slice(&tmp); + } + ifd.set_image(ImageData::Strips { rows_per_strip: h as u32, strips: vec![bytes] }); + } + (RawData::U16(v), _) => { + // Dark frames may never reach the sensor's white level, which must still fit. + let maxv = u32::from(v.iter().copied().max().unwrap_or(0)).max(max_white); + let bits = (32 - maxv.leading_zeros()).max(raw.bits).clamp(2, 16) as u8; + ifd.set( + t::BITS_PER_SAMPLE, + Value::Short(vec![if matches!(opts.compression, DngCompression::Uncompressed) { 16 } else { bits as u16 }; cpp]), + ); + match opts.compression { + DngCompression::Uncompressed => { + ifd.set(t::COMPRESSION, Value::Short(vec![compression::NONE])); + let rows = (65536 / (w * cpp * 2)).clamp(1, h); + let strips = v + .chunks(rows * w * cpp) + .map(|s| { + let mut b = Vec::with_capacity(s.len() * 2); + s.iter().for_each(|&x| opts.order.put_u16(&mut b, x)); + b + }) + .collect(); + ifd.set_image(ImageData::Strips { rows_per_strip: rows as u32, strips }); + } + DngCompression::Deflate { tile, .. } => { + let tile = deflate_tile(tile); + ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![16; cpp])); + ifd.set(t::COMPRESSION, Value::Short(vec![compression::ADOBE_DEFLATE])); + ifd.set(t::PREDICTOR, Value::Short(vec![2])); + let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); + let tiles: Vec> = (0..ta * td) + .into_par_iter() + .map(|i| { + let (tx, ty) = (i % ta * tile, i / ta * tile); + let mut buf = Vec::with_capacity(tile * tile * cpp * 2); + let mut row = vec![0u16; tile * cpp]; + for y in 0..tile { + let sy = (ty + y).min(h - 1); + for x in 0..tile { + let sx = (tx + x).min(w - 1); + for s in 0..cpp { + row[x * cpp + s] = v[(sy * w + sx) * cpp + s]; + } + } + for i in (cpp..row.len()).rev() { + row[i] = row[i].wrapping_sub(row[i - cpp]); + } + row.iter().for_each(|&x| opts.order.put_u16(&mut buf, x)); + } + miniz_oxide::deflate::compress_to_vec_zlib(&buf, DEFLATE_LEVEL) + }) + .collect(); + ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); + } + DngCompression::Lj92 { tile } => { + let tile = (tile.max(2) & !1) as usize; + if tile > MAX_LJ92_TILE { + return Err(RawError::Limit("tile too large")); + } + ifd.set(t::COMPRESSION, Value::Short(vec![compression::JPEG])); + let (ta, td) = (w.div_ceil(tile), h.div_ceil(tile)); + let tiles: Vec> = (0..ta * td) + .into_par_iter() + .map(|i| { + let (tx, ty) = (i % ta * tile, i / ta * tile); + let mut buf = Vec::with_capacity(tile * tile * cpp); + for y in 0..tile { + let sy = (ty + y).min(h - 1); + for x in 0..tile { + let sx = (tx + x).min(w - 1); + for s in 0..cpp { + buf.push(v[(sy * w + sx) * cpp + s]); + } + } + } + if cpp == 1 { + ljpeg::encode(&buf, tile / 2, tile, 2, bits, 1, 0) + } else { + ljpeg::encode(&buf, tile, tile, cpp, bits, 1, 0) + } + }) + .collect(); + ifd.set_image(ImageData::Tiles { tile_width: tile as u32, tile_height: tile as u32, tiles }); + } + } + } + } + Ok(TiffWriter::new(opts.order, false).write(&[ifd])?) +} + +/// Make, Model, UniqueCameraModel, Orientation, Software and the XMP packet. +fn write_identity_tags(ifd: &mut IfdBuilder, raw: &RawImage, opts: &DngWriteOptions) { + let make = raw.camera.make.as_deref().unwrap_or_default(); + let model = raw.camera.model.as_deref().unwrap_or_default(); + if !make.is_empty() { + ifd.set(t::MAKE, Value::Ascii(make.to_owned())); + } + if !model.is_empty() { + ifd.set(t::MODEL, Value::Ascii(model.to_owned())); + } + let model_includes_make = !make.is_empty() + && model.get(..make.len()).is_some_and(|prefix| prefix.eq_ignore_ascii_case(make)) + && model.get(make.len()..).is_some_and(|suffix| suffix.chars().next().is_none_or(|c| !c.is_alphanumeric() && c != '_')); + let unique = if let Some(unique) = raw.camera.unique_model.as_deref().filter(|u| !u.is_empty()) { + unique.to_owned() + } else if model_includes_make || make.is_empty() { + model.to_owned() + } else if model.is_empty() { + make.to_owned() + } else { + format!("{make} {model}") + }; + ifd.set(t::UNIQUE_CAMERA_MODEL, Value::Ascii(if unique.is_empty() { "Unknown".into() } else { unique })); + ifd.set(t::ORIENTATION, Value::Short(vec![raw.orientation.to_exif()])); + ifd.set(t::SOFTWARE, Value::Ascii(opts.software.clone())); + if let Some(xmp) = &opts.xmp { + ifd.set(t::XMP, Value::Byte(xmp.clone().into_bytes())); + } +} + +/// The colour tags (matrices, white balance, baseline exposure and sharpness) and the CinemaDNG frame tags. +fn write_color_tags(ifd: &mut IfdBuilder, raw: &RawImage) { + let c = &raw.color; + for (i, tag) in [t::CALIBRATION_ILLUMINANT_1, t::CALIBRATION_ILLUMINANT_2].into_iter().enumerate() { + if c.color_matrix[i].is_some() { + ifd.set(tag, Value::Short(vec![c.illuminant[i]])); + } + } + for (pair, tags) in [ + (&c.color_matrix, [t::COLOR_MATRIX_1, t::COLOR_MATRIX_2]), + (&c.forward_matrix, [t::FORWARD_MATRIX_1, t::FORWARD_MATRIX_2]), + (&c.camera_calibration, [t::CAMERA_CALIBRATION_1, t::CAMERA_CALIBRATION_2]), + ] { + for i in 0..2 { + if let Some(m) = &pair[i] { + ifd.set(tags[i], srat_mat(m)); + } + } + } + if let Some(v) = c.analog_balance { + ifd.set(t::ANALOG_BALANCE, rat_vec(&v)); + } + if let Some(v) = c.as_shot_neutral { + ifd.set(t::AS_SHOT_NEUTRAL, rat_vec(&v)); + } else if let Some(xy) = c.as_shot_white_xy { + ifd.set(t::AS_SHOT_WHITE_XY, rat_vec(&xy)); + } else if let Some(m) = raw.wb_multipliers.filter(|m| m.iter().all(|v| *v > 0.0 && v.is_finite())) { + // vendor raws (NEF, ARW…) carry white balance as multipliers: the DNG neutral is their reciprocal + ifd.set(t::AS_SHOT_NEUTRAL, rat_vec(&m.map(|v| 1.0 / v as f64))); + } + ifd.set(t::BASELINE_EXPOSURE, Value::SRational(vec![srational(c.baseline_exposure)])); + if let Some(v) = c.baseline_sharpness { + ifd.set(t::BASELINE_SHARPNESS, rat_vec(&[v])); + } + if let Some((n, d)) = raw.cinema.frame_rate.and_then(|(n, d)| Some((i32::try_from(n).ok()?, i32::try_from(d).ok()?))) { + ifd.set(51044, Value::SRational(vec![(n, d)])); + } + if let Some(tc) = raw.cinema.timecode { + ifd.set(51043, Value::Byte(tc.to_bytes().to_vec())); + } + if let Some(reel) = raw.cinema.reel_name.as_deref().filter(|r| !r.is_empty()) { + ifd.set(51081, Value::Ascii(reel.to_owned())); + } +} + +fn write_profile_tags(ifd: &mut IfdBuilder, raw: &RawImage, order: ByteOrder) { + // The source's own profile look travels with its data (conversions, smart previews, merges). + let profile = &raw.color.profile; + let table_tags = |ifd: &mut IfdBuilder, table: &crate::profile::HsvTable, dims: u16, enc: u16| { + ifd.set(dims, Value::Long(vec![table.hue_divisions as u32, table.sat_divisions as u32, table.val_divisions as u32])); + if table.srgb_value { + ifd.set(enc, Value::Long(vec![1])); + } + }; + let flat = |table: &crate::profile::HsvTable| Value::Float(table.data.iter().flatten().copied().collect()); + match &profile.hue_sat_map { + [Some(first), second] => { + table_tags(ifd, first, t::PROFILE_HUE_SAT_MAP_DIMS, t::PROFILE_HUE_SAT_MAP_ENCODING); + ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_1, flat(first)); + if let Some(second) = second.as_ref().filter(|second| { + (second.hue_divisions, second.sat_divisions, second.val_divisions) == (first.hue_divisions, first.sat_divisions, first.val_divisions) + }) { + ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_2, flat(second)); + } + } + [None, Some(table)] => { + table_tags(ifd, table, t::PROFILE_HUE_SAT_MAP_DIMS, t::PROFILE_HUE_SAT_MAP_ENCODING); + ifd.set(t::PROFILE_HUE_SAT_MAP_DATA_1, flat(table)); + } + [None, None] => {} + } + if let Some(table) = &profile.look_table { + table_tags(ifd, table, t::PROFILE_LOOK_TABLE_DIMS, t::PROFILE_LOOK_TABLE_ENCODING); + ifd.set(t::PROFILE_LOOK_TABLE_DATA, flat(table)); + } + if let Some(curve) = &profile.tone_curve { + ifd.set(t::PROFILE_TONE_CURVE, Value::Float(curve.points.iter().flatten().copied().collect())); + } + if let Some(map) = &profile.gain_table_map { + // The raw IFD is IFD 0 here, where both tag versions are valid. + let (version2, bytes) = map.to_bytes(order); + ifd.set(if version2 { t::PROFILE_GAIN_TABLE_MAP_2 } else { t::PROFILE_GAIN_TABLE_MAP }, Value::Undefined(bytes)); + } +} + +fn write_opcode_tags(ifd: &mut IfdBuilder, raw: &RawImage) { + for (list, tag) in [(&raw.opcodes.list1, t::OPCODE_LIST_1), (&raw.opcodes.list2, t::OPCODE_LIST_2), (&raw.opcodes.list3, t::OPCODE_LIST_3)] { + if !list.is_empty() { + ifd.set(tag, Value::Undefined(opcodes::write_list(list))); + } + } +} diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 4f7959a66..83a1165ca 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -93,6 +93,12 @@ mod arw6_testenc; mod arw_lens; #[cfg(feature = "arw")] mod arw_lens_rm6; +/// Bounded DNG strip and tile sample reader. +#[cfg(feature = "line-dng")] +pub mod dng_lines; +/// Bounded DNG camera and processing metadata. +#[cfg(feature = "line-dng")] +pub mod dng_metadata; #[allow(dead_code)] mod huffman; #[cfg(feature = "jxl")] @@ -116,6 +122,9 @@ mod nef; mod nef_lens; #[cfg(feature = "nef")] mod nefc; +pub mod opcode_params; +/// Bounded raw DNG opcode-list envelopes. +pub mod opcode_raw; pub mod opcodes; #[cfg(feature = "orf")] mod orf; @@ -1076,6 +1085,45 @@ pub struct RawImage { pub notices: RawNotices, } +pub(crate) fn validate_image_layout( + width: usize, + height: usize, + cpp: usize, + data_len: Option, + cfa: Option<&Cfa>, + active_area: Rect, +) -> Result<()> { + if width == 0 || height == 0 || !(1..=4).contains(&cpp) { + return Err(RawError::Corrupt("bad dimensions".into())); + } + // two samples a pixel are neither one colour nor RGB: nothing downstream can develop them (storytold/lightcraft#1511) + if cpp == 2 { + return Err(RawError::Unsupported("2 samples per pixel".into())); + } + if let Some(data_len) = data_len { + let expected = crate::checked_pixel_count(width as u64, height as u64) + .and_then(|n| n.checked_mul(cpp as u64)) + .ok_or_else(|| RawError::Corrupt("data length mismatch".into()))?; + let actual = u64::try_from(data_len).map_err(|_| RawError::Corrupt("data length mismatch".into()))?; + if actual != expected { + return Err(RawError::Corrupt("data length mismatch".into())); + } + } + if let Some(c) = cfa + && (!c.valid() || c.pattern.iter().any(|&p| p > 2)) + { + return Err(RawError::Unsupported(format!("CFA pattern {}", c.name()))); + } + let a = active_area; + // checked: a crop origin from a maker note can be `u64::MAX`, and `x + width` would wrap + // back inside the image here and again in `normalized`'s index arithmetic. + let fits = |x: usize, len: usize, limit: usize| x.checked_add(len).is_some_and(|end| end <= limit); + if a.width == 0 || a.height == 0 || !fits(a.x, a.width, width) || !fits(a.y, a.height, height) { + return Err(RawError::Corrupt("active area outside image".into())); + } + Ok(()) +} + impl RawImage { /// The description without the samples (what [`probe_info`] returns for the same file). pub fn info(&self) -> RawInfo { @@ -1150,35 +1198,7 @@ impl RawImage { } fn check(&self, samples: bool) -> Result<()> { - if self.width == 0 || self.height == 0 || !(1..=4).contains(&self.cpp) { - return Err(RawError::Corrupt("bad dimensions".into())); - } - // two samples a pixel are neither one colour nor RGB: nothing downstream can develop them (storytold/lightcraft#1511) - if self.cpp == 2 { - return Err(RawError::Unsupported("2 samples per pixel".into())); - } - if samples { - let expected = crate::checked_pixel_count(self.width as u64, self.height as u64) - .and_then(|n| n.checked_mul(self.cpp as u64)) - .ok_or_else(|| RawError::Corrupt("data length mismatch".into()))?; - let actual = u64::try_from(self.data.len()).map_err(|_| RawError::Corrupt("data length mismatch".into()))?; - if actual != expected { - return Err(RawError::Corrupt("data length mismatch".into())); - } - } - if let Some(c) = &self.cfa - && (!c.valid() || c.pattern.iter().any(|&p| p > 2)) - { - return Err(RawError::Unsupported(format!("CFA pattern {}", c.name()))); - } - let a = self.active_area; - // checked: a crop origin from a maker note can be `u64::MAX`, and `x + width` would wrap - // back inside the image here and again in `normalized`'s index arithmetic. - let fits = |x: usize, len: usize, limit: usize| x.checked_add(len).is_some_and(|end| end <= limit); - if a.width == 0 || a.height == 0 || !fits(a.x, a.width, self.width) || !fits(a.y, a.height, self.height) { - return Err(RawError::Corrupt("active area outside image".into())); - } - Ok(()) + validate_image_layout(self.width, self.height, self.cpp, samples.then_some(self.data.len()), self.cfa.as_ref(), self.active_area) } /// White level for sample `s`. diff --git a/crates/raw/src/opcode_params.rs b/crates/raw/src/opcode_params.rs new file mode 100644 index 000000000..4118d6cef --- /dev/null +++ b/crates/raw/src/opcode_params.rs @@ -0,0 +1,418 @@ +//! Context-aware parsing of DNG opcode parameters. Image processing remains the caller's job. +//! +//! The typed [`crate::opcodes`] model is the file's own view (owned, serialisable, lenient: a +//! malformed opcode becomes `Unknown`, a zero pitch reads as 1, coordinates stay as written). This +//! module answers a different question for a caller about to apply an opcode to a given image: +//! parameters borrowed from the record (bad-pixel lists are not copied), checked strictly (a zero +//! pitch, a count larger than its bytes or a warp on a mosaic is an error) and areas clamped to the +//! image. The record envelope comes from [`crate::opcode_raw`]; the rectangle on the wire is +//! [`crate::opcodes::Area`], which [`Area::clamp`] turns into image coordinates. + +/// Image properties used to validate and clamp opcode parameters. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Context { + pub width: usize, + pub height: usize, + pub planes: usize, + pub mosaic: bool, + pub allow_trim: bool, +} +/// A parameter failure, retaining the diagnostic used by the caller. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ParseError { + Malformed(&'static str), + Unsupported(&'static str), +} +/// A clamped image area and its sample pitches. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Area { + pub top: usize, + pub left: usize, + pub bottom: usize, + pub right: usize, + pub plane: usize, + pub planes: usize, + pub row_pitch: usize, + pub col_pitch: usize, +} +impl Area { + /// Iterate sampled rows, rejecting a zero pitch even for caller-constructed areas. + pub fn rows(&self) -> Result + use<>, ParseError> { + if self.row_pitch == 0 { + return Err(ParseError::Malformed("opcode area with a zero pitch")); + } + Ok((self.top..self.bottom.max(self.top)).step_by(self.row_pitch)) + } + /// Iterate sampled columns, rejecting a zero pitch even for caller-constructed areas. + pub fn cols(&self) -> Result + use<>, ParseError> { + if self.col_pitch == 0 { + return Err(ParseError::Malformed("opcode area with a zero pitch")); + } + Ok((self.left..self.right.max(self.left)).step_by(self.col_pitch)) + } +} +/// Borrowed bad-pixel coordinates, decoded lazily without copying the payload. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct PointRecords<'a>(&'a [u8]); +impl PointRecords<'_> { + pub fn iter(&self) -> impl Iterator + '_ { + self.0.as_chunks::<8>().0.iter().map(|b| (u32::from_be_bytes([b[0], b[1], b[2], b[3]]), u32::from_be_bytes([b[4], b[5], b[6], b[7]]))) + } +} +/// Borrowed bad-pixel rectangles, decoded lazily without copying the payload. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct RectangleRecords<'a>(&'a [u8]); +impl RectangleRecords<'_> { + pub fn iter(&self) -> impl Iterator + '_ { + self.0.as_chunks::<16>().0.iter().map(|b| { + [ + u32::from_be_bytes([b[0], b[1], b[2], b[3]]), + u32::from_be_bytes([b[4], b[5], b[6], b[7]]), + u32::from_be_bytes([b[8], b[9], b[10], b[11]]), + u32::from_be_bytes([b[12], b[13], b[14], b[15]]), + ] + }) + } +} +/// Parameters for the fourteen defined opcodes. +#[derive(Clone, Debug, PartialEq)] +pub enum Params<'a> { + WarpRectilinear { planes: Vec<[f64; 6]>, center: [f64; 2] }, + WarpFisheye { planes: Vec<[f64; 4]>, center: [f64; 2] }, + FixVignetteRadial { k: [f64; 5], center: [f64; 2] }, + FixBadPixelsConstant { constant: u32, bayer_phase: u32 }, + FixBadPixelsList { bayer_phase: u32, points: PointRecords<'a>, rects: RectangleRecords<'a> }, + TrimBounds { top: usize, left: usize, bottom: usize, right: usize }, + MapTable { area: Area, table: Vec }, + MapPolynomial { area: Area, coefficients: Vec }, + GainMap { area: Area, points_v: usize, points_h: usize, spacing: [f64; 2], origin: [f64; 2], map_planes: usize, gains: Vec }, + DeltaPerRow { area: Area, deltas: Vec }, + DeltaPerColumn { area: Area, deltas: Vec }, + ScalePerRow { area: Area, scales: Vec }, + ScalePerColumn { area: Area, scales: Vec }, + WarpRectilinear2 { planes: Vec<[f64; 19]>, center: [f64; 2], reciprocal: bool }, +} +struct Reader<'a> { + bytes: &'a [u8], + pos: usize, +} +impl<'a> Reader<'a> { + fn take(&mut self, n: usize) -> Result<&'a [u8], ParseError> { + let end = self.pos.checked_add(n).ok_or(ParseError::Malformed("opcode parameters"))?; + let b = self.bytes.get(self.pos..end).ok_or(ParseError::Malformed("opcode parameters too short"))?; + self.pos = end; + Ok(b) + } + fn u32(&mut self) -> Result { + let b = self.take(4)?; + Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]])) + } + fn f32(&mut self) -> Result { + let v = f32::from_bits(self.u32()?); + if v.is_finite() { Ok(v) } else { Err(ParseError::Malformed("non-finite opcode parameter")) } + } + fn f64(&mut self) -> Result { + let b = self.take(8)?; + let v = f64::from_be_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]); + if v.is_finite() { Ok(v) } else { Err(ParseError::Malformed("non-finite opcode parameter")) } + } + fn remaining(&self) -> usize { + self.bytes.len().saturating_sub(self.pos) + } + fn count(&mut self, size: usize) -> Result { + let n = self.u32()? as usize; + if n.saturating_mul(size) > self.remaining() { Err(ParseError::Malformed("opcode count larger than its parameters")) } else { Ok(n) } + } + fn array(&mut self) -> Result<[f64; N], ParseError> { + let mut a = [0.0; N]; + for x in &mut a { + *x = self.f64()?; + } + Ok(a) + } + fn area(&mut self, c: Context) -> Result { + let wire = crate::opcodes::Area { + top: self.u32()?, + left: self.u32()?, + bottom: self.u32()?, + right: self.u32()?, + plane: self.u32()?, + planes: self.u32()?, + row_pitch: self.u32()?, + col_pitch: self.u32()?, + }; + Area::clamp(wire, c) + } +} +impl Area { + /// The area of an opcode as written ([`crate::opcodes::Area`]) clamped to the image of `c`: + /// coordinates to its size, the plane range to its planes. A zero pitch is an error here (the + /// typed reader reads it as 1). + pub fn clamp(wire: crate::opcodes::Area, c: Context) -> Result { + if wire.row_pitch == 0 || wire.col_pitch == 0 { + return Err(ParseError::Malformed("opcode area with a zero pitch")); + } + let px = |v: u32| v as usize; + let plane = px(wire.plane).min(c.planes); + Ok(Area { + top: px(wire.top).min(c.height), + left: px(wire.left).min(c.width), + bottom: px(wire.bottom).min(c.height), + right: px(wire.right).min(c.width), + plane, + planes: px(wire.planes).min(c.planes - plane), + row_pitch: px(wire.row_pitch), + col_pitch: px(wire.col_pitch), + }) + } +} +/// Parse a known opcode's parameters. Unknown identifiers return an unsupported error. +pub fn parse(id: u32, bytes: &[u8], context: Context) -> Result, ParseError> { + let mut r = Reader { bytes, pos: 0 }; + match id { + 1 | 2 | 14 => { + if context.mosaic { + return Err(ParseError::Unsupported("a warp opcode on a CFA mosaic")); + } + let per = match id { + 1 => 6, + 2 => 4, + _ => 19, + }; + let n = r.count(per * 8)?; + if n != 1 && n != context.planes { + return Err(ParseError::Malformed("warp opcode coefficient sets")); + } + match id { + 1 => { + let planes = (0..n).map(|_| r.array()).collect::>()?; + let center = r.array()?; + Ok(Params::WarpRectilinear { planes, center }) + } + 2 => { + let planes = (0..n).map(|_| r.array()).collect::>()?; + let center = r.array()?; + Ok(Params::WarpFisheye { planes, center }) + } + _ => { + let mut planes = Vec::with_capacity(n); + for _ in 0..n { + let mut p: [f64; 19] = r.array()?; + p[17] = p[17].clamp(0.0, 1.0); + p[18] = p[18].clamp(0.0, 1.0); + if p[17] >= p[18] { + return Err(ParseError::Malformed("WarpRectilinear2 valid radius range")); + } + planes.push(p); + } + let center = r.array()?; + let reciprocal = r.u32()? != 0; + Ok(Params::WarpRectilinear2 { planes, center, reciprocal }) + } + } + } + 3 => Ok(Params::FixVignetteRadial { k: r.array()?, center: r.array()? }), + 4 => Ok(Params::FixBadPixelsConstant { constant: r.u32()?, bayer_phase: r.u32()? }), + 5 => { + let bayer_phase = r.u32()?; + let points = r.u32()? as usize; + let rects = r.u32()? as usize; + if points.saturating_mul(8).saturating_add(rects.saturating_mul(16)) > r.remaining() { + return Err(ParseError::Malformed("FixBadPixelsList counts larger than its parameters")); + } + let points = + PointRecords(r.take(points.checked_mul(8).ok_or(ParseError::Malformed("FixBadPixelsList counts larger than its parameters"))?)?); + let rects = + RectangleRecords(r.take(rects.checked_mul(16).ok_or(ParseError::Malformed("FixBadPixelsList counts larger than its parameters"))?)?); + Ok(Params::FixBadPixelsList { bayer_phase, points, rects }) + } + 6 => { + let top = r.u32()? as usize; + let left = r.u32()? as usize; + let bottom = r.u32()? as usize; + let right = r.u32()? as usize; + if !context.allow_trim { + return Err(ParseError::Unsupported("TrimBounds before the demosaic")); + } + let bottom = bottom.min(context.height); + let right = right.min(context.width); + if top >= bottom || left >= right { + return Err(ParseError::Malformed("TrimBounds outside the picture")); + } + Ok(Params::TrimBounds { top, left, bottom, right }) + } + 7..=13 => { + let area = r.area(context)?; + match id { + 7 => { + let n = r.count(2)?; + if n == 0 { + return Err(ParseError::Malformed("MapTable without entries")); + } + let table = (0..n) + .map(|_| { + let b = r.take(2)?; + Ok(u16::from_be_bytes([b[0], b[1]])) + }) + .collect::>()?; + Ok(Params::MapTable { area, table }) + } + 8 => { + let degree = r.u32()? as usize; + if degree > 8 { + return Err(ParseError::Malformed("MapPolynomial degree above 8")); + } + let coefficients = (0..=degree).map(|_| r.f64()).collect::>()?; + Ok(Params::MapPolynomial { area, coefficients }) + } + 9 => { + let points_v = r.u32()? as usize; + let points_h = r.u32()? as usize; + let spacing = r.array()?; + let origin = r.array()?; + let map_planes = r.u32()? as usize; + let n = points_v.saturating_mul(points_h).saturating_mul(map_planes); + if points_v == 0 || points_h == 0 || map_planes == 0 || n.saturating_mul(4) > r.remaining() { + return Err(ParseError::Malformed("GainMap size")); + } + if (points_v > 1 && spacing[0] <= 0.0) || (points_h > 1 && spacing[1] <= 0.0) { + return Err(ParseError::Malformed("GainMap spacing")); + } + let gains = (0..n).map(|_| r.f32()).collect::>()?; + Ok(Params::GainMap { area, points_v, points_h, spacing, origin, map_planes, gains }) + } + _ => { + let n = r.count(4)?; + let vals = (0..n).map(|_| r.f32()).collect::, _>>()?; + let need = if matches!(id, 10 | 12) { + area.bottom.saturating_sub(area.top).div_ceil(area.row_pitch) + } else { + area.right.saturating_sub(area.left).div_ceil(area.col_pitch) + }; + if vals.len() < need { + return Err(ParseError::Malformed("opcode with fewer values than rows or columns")); + } + Ok(match id { + 10 => Params::DeltaPerRow { area, deltas: vals }, + 11 => Params::DeltaPerColumn { area, deltas: vals }, + 12 => Params::ScalePerRow { area, scales: vals }, + _ => Params::ScalePerColumn { area, scales: vals }, + }) + } + } + } + _ => Err(ParseError::Unsupported("unknown DNG opcode")), + } +} + +#[cfg(test)] +mod tests { + use super::*; + fn u(v: &[u32]) -> Vec { + v.iter().flat_map(|x| x.to_be_bytes()).collect() + } + fn d(v: &[f64]) -> Vec { + v.iter().flat_map(|x| x.to_be_bytes()).collect() + } + fn f(v: &[f32]) -> Vec { + v.iter().flat_map(|x| x.to_be_bytes()).collect() + } + fn ctx() -> Context { + Context { width: 4, height: 3, planes: 2, mosaic: false, allow_trim: true } + } + #[test] + fn caller_constructed_areas_reject_zero_pitch() -> Result<(), ParseError> { + let mut area = Area { top: 1, left: 2, bottom: 7, right: 9, plane: 0, planes: 1, row_pitch: 2, col_pitch: 3 }; + assert_eq!(area.rows()?.collect::>(), vec![1, 3, 5]); + assert_eq!(area.cols()?.collect::>(), vec![2, 5, 8]); + area.row_pitch = 0; + area.col_pitch = 0; + assert!(area.rows().is_err()); + assert!(area.cols().is_err()); + // Empty/reversed bounds must not allow an invalid pitch through either. + area.bottom = 0; + area.right = 0; + assert!(area.rows().is_err()); + assert!(area.cols().is_err()); + Ok(()) + } + + #[test] + fn parses_all_variants_and_clamps_areas() -> Result<(), ParseError> { + let area = u(&[0, 1, 8, 9, 1, 5, 1, 2]); + let cases = vec![ + (1, [u(&[1]), d(&[1., 0., 0., 0., 0., 0., 0.5, 0.5])].concat()), + (2, [u(&[1]), d(&[1., 0., 0., 0., 0.5, 0.5])].concat()), + (3, d(&[0., 0., 0., 0., 0., 0.5, 0.5])), + (4, u(&[0, 0])), + (5, u(&[0, 1, 1, 1, 2, 0, 0, 1, 1])), + (6, u(&[0, 1, 8, 9])), + (7, [area.clone(), u(&[1]), vec![0, 1]].concat()), + (8, [area.clone(), u(&[0]), d(&[1.])].concat()), + (9, [area.clone(), u(&[1, 1]), d(&[0., 0., 0., 0.]), u(&[1]), f(&[1.])].concat()), + (10, [area.clone(), u(&[3]), f(&[1., 2., 3.])].concat()), + (11, [area.clone(), u(&[2]), f(&[1., 2.])].concat()), + (12, [area.clone(), u(&[3]), f(&[1., 2., 3.])].concat()), + (13, [area.clone(), u(&[2]), f(&[1., 2.])].concat()), + (14, [u(&[1]), d(&[0.; 17]), d(&[-1., 2., 0.5, 0.5]), u(&[1])].concat()), + ]; + for (id, bytes) in cases { + let p = parse(id, &bytes, ctx())?; + assert!(matches!( + (id, p), + (1, Params::WarpRectilinear { .. }) + | (2, Params::WarpFisheye { .. }) + | (3, Params::FixVignetteRadial { .. }) + | (4, Params::FixBadPixelsConstant { .. }) + | (5, Params::FixBadPixelsList { .. }) + | (6, Params::TrimBounds { .. }) + | (7, Params::MapTable { .. }) + | (8, Params::MapPolynomial { .. }) + | (9, Params::GainMap { .. }) + | (10, Params::DeltaPerRow { .. }) + | (11, Params::DeltaPerColumn { .. }) + | (12, Params::ScalePerRow { .. }) + | (13, Params::ScalePerColumn { .. }) + | (14, Params::WarpRectilinear2 { .. }) + )); + } + assert!(matches!( + parse(7, &[area, u(&[1]), vec![0, 1]].concat(), ctx()), + Ok(Params::MapTable { area: Area { bottom: 3, right: 4, plane: 1, planes: 1, .. }, .. }) + )); + Ok(()) + } + #[test] + fn validates_in_application_order() { + assert_eq!(parse(1, &[], Context { mosaic: true, ..ctx() }), Err(ParseError::Unsupported("a warp opcode on a CFA mosaic"))); + assert_eq!(parse(6, &[], Context { allow_trim: false, ..ctx() }), Err(ParseError::Malformed("opcode parameters too short"))); + assert_eq!( + parse(6, &u(&[0, 0, 1, 1]), Context { allow_trim: false, ..ctx() }), + Err(ParseError::Unsupported("TrimBounds before the demosaic")) + ); + assert_eq!(parse(5, &u(&[0, u32::MAX, u32::MAX]), ctx()), Err(ParseError::Malformed("FixBadPixelsList counts larger than its parameters"))); + let a = u(&[0, 0, 3, 4, 0, 1, 1, 1]); + assert_eq!(parse(10, &[a, u(&[1]), f(&[f32::NAN])].concat(), ctx()), Err(ParseError::Malformed("non-finite opcode parameter"))); + } + + #[test] + fn typed_areas_clamp_to_the_image() { + let c = ctx(); + let wire = crate::opcodes::Area { top: 1, left: 2, bottom: 9_999, right: 9_999, plane: 9, planes: 9, row_pitch: 2, col_pitch: 1 }; + let a = Area::clamp(wire, c).unwrap(); + assert_eq!((a.bottom, a.right), (c.height, c.width)); + assert_eq!((a.plane, a.planes), (c.planes, 0)); + assert!(Area::clamp(crate::opcodes::Area { row_pitch: 0, ..wire }, c).is_err()); + // the typed reader and this one agree on a well-formed area + let typed = crate::opcodes::Opcode::MapTable { area: crate::opcodes::Area { col_pitch: 1, ..wire }, table: vec![1, 2] }; + let bytes = crate::opcodes::write_list(&[typed]); + let record = &crate::opcode_raw::parse(&bytes, 1).unwrap()[0]; + match parse(record.id, &record.params, c).unwrap() { + Params::MapTable { area, table } => { + assert_eq!(area, a); + assert_eq!(table, [1, 2]); + } + other => panic!("MapTable expected, got {other:?}"), + } + } +} diff --git a/crates/raw/src/opcode_raw.rs b/crates/raw/src/opcode_raw.rs new file mode 100644 index 000000000..5b04ca895 --- /dev/null +++ b/crates/raw/src/opcode_raw.rs @@ -0,0 +1,146 @@ +//! DNG opcode-list envelopes, retaining unrecognized records and their parameter bytes. + +/// An opcode with its version, flags and unmodified parameter bytes. +#[derive(Clone, Debug, PartialEq)] +pub struct Opcode { + /// Opcode identifier. + pub id: u32, + /// Four-byte DNG version, stored as a big-endian integer. + pub version: u32, + /// Opcode flags. + pub flags: u32, + /// Unmodified parameter bytes. + pub params: Vec, +} +impl Opcode { + /// Unknown or unsupported optional opcodes may be skipped. + pub fn optional(&self) -> bool { + self.flags & 1 != 0 + } + /// The opcode may be skipped for preview quality. + pub fn preview_skippable(&self) -> bool { + self.flags & 2 != 0 + } +} + +/// A damaged opcode list or one exceeding the caller's count limit. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Malformed; + +/// The records of a big-endian opcode list in order, as `(id, version, flags, params)`, ending at +/// the declared count or at the first truncated record. The envelope walk shared by [`parse`] and +/// the typed [`crate::opcodes::parse_list`]. +pub(crate) struct Records<'a> { + bytes: &'a [u8], + at: usize, + /// Records the list header still promises. + pub(crate) left: u32, +} + +impl<'a> Records<'a> { + /// The record walker of a list; `None` when even the count is missing. + pub(crate) fn new(bytes: &'a [u8]) -> Option { + let left = u32::from_be_bytes(bytes.get(..4)?.try_into().ok()?); + Some(Self { bytes, at: 4, left }) + } + fn u32(&mut self) -> Option { + let end = self.at.checked_add(4)?; + let value = u32::from_be_bytes(self.bytes.get(self.at..end)?.try_into().ok()?); + self.at = end; + Some(value) + } +} + +impl<'a> Iterator for Records<'a> { + type Item = (u32, u32, u32, &'a [u8]); + fn next(&mut self) -> Option { + if self.left == 0 { + return None; + } + let (id, version, flags, len) = (self.u32()?, self.u32()?, self.u32()?, self.u32()? as usize); + let end = self.at.checked_add(len)?; + let params = self.bytes.get(self.at..end)?; + self.at = end; + self.left -= 1; + Some((id, version, flags, params)) + } +} + +/// Parse a big-endian opcode list with an explicit maximum record count. +pub fn parse(bytes: &[u8], max_opcodes: usize) -> Result, Malformed> { + let records = Records::new(bytes).ok_or(Malformed)?; + let count = records.left as usize; + if count > bytes.len() / 16 || count > max_opcodes { + return Err(Malformed); + } + let out: Vec = records.map(|(id, version, flags, params)| Opcode { id, version, flags, params: params.to_vec() }).collect(); + if out.len() == count { Ok(out) } else { Err(Malformed) } +} + +/// An opcode list cannot be represented in the DNG wire format or allocated. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct EncodeError; + +fn wire_length(length: usize) -> Result { + u32::try_from(length).map_err(|_| EncodeError) +} + +/// Encode records, rejecting unrepresentable counts and parameter lengths before allocation. +pub fn encode(list: &[Opcode]) -> Result, EncodeError> { + let count = wire_length(list.len())?; + let mut total = 4usize; + for op in list { + wire_length(op.params.len())?; + total = total.checked_add(16).and_then(|n| n.checked_add(op.params.len())).ok_or(EncodeError)?; + } + let mut out = Vec::new(); + out.try_reserve_exact(total).map_err(|_| EncodeError)?; + out.extend_from_slice(&count.to_be_bytes()); + for op in list { + for value in [op.id, op.version, op.flags, wire_length(op.params.len())?] { + out.extend(value.to_be_bytes()); + } + out.extend_from_slice(&op.params); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn wire_lengths_are_checked_without_large_allocations() { + assert_eq!(wire_length(0), Ok(0)); + assert_eq!(wire_length(u32::MAX as usize), Ok(u32::MAX)); + if let Some(too_large) = (u32::MAX as usize).checked_add(1) { + assert_eq!(wire_length(too_large), Err(EncodeError)); + } + assert_eq!(encode(&[]).unwrap(), vec![0, 0, 0, 0]); + } + + #[test] + fn unknown_records_round_trip_and_limits_are_enforced() { + let records = vec![Opcode { id: 900, version: 0x0107_0100, flags: 3, params: vec![1, 2, 3] }]; + let bytes = encode(&records).unwrap(); + assert_eq!(parse(&bytes, 1).unwrap(), records); + assert!(parse(&bytes, 0).is_err()); + assert!(parse(&bytes[..bytes.len() - 1], 1).is_err()); + assert!(records[0].optional() && records[0].preview_skippable()); + } + + #[test] + fn typed_lists_share_the_envelope() { + use crate::opcodes::{self, Opcode as Typed}; + let typed = vec![Typed::TrimBounds { top: 1, left: 2, bottom: 3, right: 4 }, Typed::Unknown { id: 900, flags: 1, params: vec![5, 6] }]; + let bytes = opcodes::write_list(&typed); + let raw = parse(&bytes, 8).unwrap(); + assert_eq!(raw.iter().map(|o| (o.id, o.version)).collect::>(), [(6, 0x0103_0000), (900, 0x0103_0000)]); + assert_eq!(encode(&raw).unwrap(), bytes); + assert_eq!(opcodes::parse_list(&bytes), typed); + // a truncated last record: the typed reader keeps what came before, the raw one refuses + assert_eq!(opcodes::parse_list(&bytes[..bytes.len() - 1]), typed[..1]); + assert!(parse(&bytes[..bytes.len() - 1], 8).is_err()); + assert!(opcodes::parse_list(&[0, 0]).is_empty()); + assert!(parse(&[0, 0], 8).is_err()); + } +} diff --git a/crates/raw/src/opcodes.rs b/crates/raw/src/opcodes.rs index 2143adcf1..f585dfd51 100644 --- a/crates/raw/src/opcodes.rs +++ b/crates/raw/src/opcodes.rs @@ -480,31 +480,27 @@ fn params(op: &Opcode) -> (u32, u32, Vec) { (id, 0, o) } -/// Serialise an opcode list (inverse of [`parse_list`]). +/// Serialise an opcode list (inverse of [`parse_list`]) through the shared envelope encoder +/// ([`crate::opcode_raw::encode`]). A list the wire format cannot hold (a count or parameter block +/// past 4 GiB) yields an empty blob rather than a truncated one. pub fn write_list(list: &[Opcode]) -> Vec { - let mut out = (list.len() as u32).to_be_bytes().to_vec(); - for op in list { - let (id, flags, p) = params(op); - for v in [id, if id == 14 { 0x0106_0000 } else { 0x0103_0000 }, flags, p.len() as u32] { - out.extend_from_slice(&v.to_be_bytes()); - } - out.extend_from_slice(&p); - } - out + let records: Vec = list + .iter() + .map(|op| { + let (id, flags, params) = params(op); + crate::opcode_raw::Opcode { id, version: if id == 14 { 0x0106_0000 } else { 0x0103_0000 }, flags, params } + }) + .collect(); + crate::opcode_raw::encode(&records).unwrap_or_default() } /// Parse an opcode list blob. Malformed opcodes become [`Opcode::Unknown`]; a truncated list stops early. pub fn parse_list(d: &[u8]) -> Vec { - let mut r = Rd { d, p: 0 }; - let Some(count) = r.u32() else { return vec![] }; - let mut out = Vec::new(); - for _ in 0..count.min(4096) { - let (Some(id), Some(_ver), Some(flags), Some(size)) = (r.u32(), r.u32(), r.u32(), r.u32()) else { break }; - let Some(params) = d.get(r.p..r.p.saturating_add(size as usize)) else { break }; - r.p += size as usize; - out.push(parse_one(id, flags, params).unwrap_or(Opcode::Unknown { id, flags, params: params.to_vec() })); - } - out + let Some(records) = crate::opcode_raw::Records::new(d) else { return vec![] }; + records + .take(4096) + .map(|(id, _version, flags, params)| parse_one(id, flags, params).unwrap_or(Opcode::Unknown { id, flags, params: params.to_vec() })) + .collect() } /// Radius normaliser: distance from the centre to the farthest image corner. diff --git a/crates/raw/src/tiffraw.rs b/crates/raw/src/tiffraw.rs index e4c9a26b4..b7291f9d0 100644 --- a/crates/raw/src/tiffraw.rs +++ b/crates/raw/src/tiffraw.rs @@ -458,10 +458,23 @@ pub(crate) fn lossy_jpeg(src: &[u8], cpp: usize, max_samples: usize) -> Result<( lossy_jpeg_with_tile(src, cpp, max_samples, None) } +/// The width and height a baseline (lossy) JPEG declares in its frame header, read without +/// decoding any scan; `None` when the header cannot be read. +#[cfg(feature = "line-dng")] +pub(crate) fn lossy_jpeg_dimensions(src: &[u8]) -> Option<(usize, usize)> { + use zune_core::bytestream::ZCursor; + use zune_core::options::DecoderOptions; + let opts = DecoderOptions::default().set_max_width(1 << 16).set_max_height(1 << 16).set_strict_mode(false); + let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(src), opts); + d.decode_headers().ok()?; + let info = d.info()?; + Some((usize::from(info.width), usize::from(info.height))) +} + /// Validate the actual frame before decode allocates its output. Edge tiles may store /// a smaller frame covering all visible samples or bounded whole-MCU padding. #[cfg(feature = "dng")] -fn lossy_jpeg_with_tile( +pub(crate) fn lossy_jpeg_with_tile( src: &[u8], cpp: usize, max_samples: usize, diff --git a/crates/raw/tests/it/dng.rs b/crates/raw/tests/it/dng.rs index cc0747830..21074a828 100644 --- a/crates/raw/tests/it/dng.rs +++ b/crates/raw/tests/it/dng.rs @@ -15,7 +15,7 @@ fn scene_value(x: usize, y: usize, c: u8, max: f32) -> f32 { v * max } -fn synthetic(w: usize, h: usize, cfa: Option, cpp: usize) -> RawImage { +pub(super) fn synthetic(w: usize, h: usize, cfa: Option, cpp: usize) -> RawImage { let black = 256.0; let white = 16000.0; let mut data = Vec::with_capacity(w * h * cpp); @@ -194,7 +194,7 @@ fn deflate_roundtrips_integer_and_float() { // ---------------------------------------------------------------- hand-built variants -fn base_ifd(w: usize, h: usize, bits: u16, cpp: usize, cfa: bool) -> IfdBuilder { +pub(super) fn base_ifd(w: usize, h: usize, bits: u16, cpp: usize, cfa: bool) -> IfdBuilder { let mut ifd = IfdBuilder::new(); ifd.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0])); ifd.set(t::IMAGE_WIDTH, Value::Long(vec![w as u32])); @@ -209,12 +209,12 @@ fn base_ifd(w: usize, h: usize, bits: u16, cpp: usize, cfa: bool) -> IfdBuilder ifd } -fn dng(raw: IfdBuilder, order: ByteOrder) -> Vec { +pub(super) fn dng(raw: IfdBuilder, order: ByteOrder) -> Vec { dng_with(raw, order, |_| {}) } /// [`dng`] with extra IFD 0 tags (where DNG writers put camera-profile tags). -fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilder)) -> Vec { +pub(super) fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilder)) -> Vec { let mut ifd0 = IfdBuilder::new(); ifd0.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![1])); ifd0.set(t::DNG_VERSION, Value::Byte(vec![1, 6, 0, 0])); @@ -237,12 +237,12 @@ fn dng_with(raw: IfdBuilder, order: ByteOrder, extra: impl FnOnce(&mut IfdBuilde TiffWriter::new(order, false).write(&[ifd0]).unwrap() } -fn pattern(w: usize, h: usize, cpp: usize, bits: u32) -> Vec { +pub(super) fn pattern(w: usize, h: usize, cpp: usize, bits: u32) -> Vec { let max = (1u32 << bits) - 1; (0..w * h * cpp).map(|i| ((i as u32).wrapping_mul(2654435761u32) >> 7) % (max + 1)).map(|v| v as u16).collect() } -fn pack_msb(v: &[u16], bits: u32, w: usize) -> Vec { +pub(super) fn pack_msb(v: &[u16], bits: u32, w: usize) -> Vec { let mut out = Vec::new(); for row in v.chunks(w) { let mut acc = 0u64; @@ -262,7 +262,7 @@ fn pack_msb(v: &[u16], bits: u32, w: usize) -> Vec { out } -fn u16_bytes(v: &[u16], order: ByteOrder) -> Vec { +pub(super) fn u16_bytes(v: &[u16], order: ByteOrder) -> Vec { let mut b = Vec::new(); v.iter().for_each(|&x| order.put_u16(&mut b, x)); b @@ -407,7 +407,7 @@ fn floating_point_dngs() { /// The chunks (tiles of `tw` x `th`, or strips when `tw == w`) of an image of `cpp` interleaved samples per pixel, each /// nominal-sized (strips: clipped to the image height) with zero padding past the image edge: one chunk per tile, and for `planar` one set of chunks per /// plane (one sample per pixel), plane after plane. -fn chunk_samples(px: &[T], w: usize, h: usize, cpp: usize, tw: usize, th: usize, planar: bool) -> Vec> { +pub(super) fn chunk_samples(px: &[T], w: usize, h: usize, cpp: usize, tw: usize, th: usize, planar: bool) -> Vec> { let planes = if planar { cpp } else { 1 }; let ccpp = if planar { 1 } else { cpp }; let mut out = Vec::new(); @@ -432,7 +432,7 @@ fn chunk_samples(px: &[T], w: usize, h: usize, cpp: usize, tw out } -fn layout_image(raw: &mut IfdBuilder, chunks: Vec>, w: usize, tile: (usize, usize)) { +pub(super) fn layout_image(raw: &mut IfdBuilder, chunks: Vec>, w: usize, tile: (usize, usize)) { raw.set_image(if tile.0 == w { ImageData::Strips { rows_per_strip: tile.1 as u32, strips: chunks } } else { diff --git a/crates/raw/tests/it/dng_lines.rs b/crates/raw/tests/it/dng_lines.rs new file mode 100644 index 000000000..1cf266d67 --- /dev/null +++ b/crates/raw/tests/it/dng_lines.rs @@ -0,0 +1,279 @@ +//! The bounded strip/tile reader (`dng_lines`) against the full DNG reader: every layout the writer +//! and the hand-built variants produce reads to the same samples, and the layouts the bounded reader +//! does not take are refused with a reason. + +use super::dng::{base_ifd, chunk_samples, dng, layout_image, pack_msb, pattern, synthetic, u16_bytes}; +use craft_raw::dng_lines::{self, Error}; +use craft_raw::dngwrite::{DngImage, write_lossy_dng}; +use craft_raw::{Cfa, DngCompression, DngWriteOptions, RawData, RawError, decode, write_dng}; +use craft_tiff::classic::Tiff; +use craft_tiff::tags as t; +use craft_tiff::{ByteOrder, ImageData, Value}; + +fn lines(bytes: &[u8]) -> Result { + let (tiff, first) = Tiff::new(bytes)?; + let ifds = tiff.all_ifds(first)?; + let ifd = dng_lines::find_raw_ifd(&tiff, &ifds)?; + dng_lines::read(&tiff, ifd) +} + +fn full(bytes: &[u8]) -> Vec { + match decode(bytes).unwrap().data { + RawData::U16(v) => v, + other => panic!("integer samples expected, got {other:?}"), + } +} + +#[test] +fn uncompressed_layouts_match_the_full_reader() { + let (w, h) = (37, 23); + for cpp in [1usize, 3] { + for bits in [8u32, 12, 16] { + let px = pattern(w, h, cpp, bits); + for tile in [(16, 16), (w, 5), (w, h)] { + for order in [ByteOrder::Little, ByteOrder::Big] { + let chunks = chunk_samples(&px, w, h, cpp, tile.0, tile.1, false) + .iter() + .map(|c| match bits { + 8 => c.iter().map(|&v| v as u8).collect(), + 16 => u16_bytes(c, order), + _ => pack_msb(c, bits, tile.0 * cpp), + }) + .collect(); + let mut raw = base_ifd(w, h, bits as u16, cpp, cpp == 1); + raw.set(t::COMPRESSION, Value::Short(vec![1])); + layout_image(&mut raw, chunks, w, tile); + let bytes = dng(raw, order); + let img = lines(&bytes).unwrap(); + let what = format!("cpp {cpp} bits {bits} tile {tile:?} {order:?}"); + assert_eq!((img.width, img.height, img.samples_per_pixel, img.bits), (w, h, cpp, bits), "{what}"); + assert_eq!(img.data, px, "{what}"); + assert_eq!(img.data, full(&bytes), "{what}"); + } + } + } + } +} + +#[test] +fn written_dngs_read_back_in_every_compression() { + for (w, h) in [(64, 48), (33, 17)] { + for cfa in [Some(Cfa::bayer("RGGB").unwrap()), None] { + let cpp = if cfa.is_some() { 1 } else { 3 }; + let raw = synthetic(w, h, cfa, cpp); + for comp in [DngCompression::Uncompressed, DngCompression::Lj92 { tile: 16 }, DngCompression::Lj92 { tile: 256 }] { + for order in [ByteOrder::Little, ByteOrder::Big] { + let bytes = write_dng(&raw, &DngWriteOptions { compression: comp, order, xmp: None, ..Default::default() }).unwrap(); + let img = lines(&bytes).unwrap(); + assert_eq!(RawData::U16(img.data.clone()), raw.data, "{w}x{h} cpp {cpp} {comp:?} {order:?}"); + assert_eq!(img.data, full(&bytes)); + } + } + } + } +} + +#[test] +fn lossy_jpeg_tiles_read_like_the_full_reader() { + let (w, h) = (40, 21); + let mut raw = synthetic(w, h, None, 3); + raw.bits = 8; + raw.data = RawData::U16(pattern(w, h, 3, 8)); + raw.white = vec![255.0]; + raw.black = craft_raw::BlackLevel::uniform(0.0); + let linearization: Vec = (0..256).map(|v| v * 257).collect(); + let encode = |pixels: Vec, tile: u32, quality: u8| { + let mut out = Vec::new(); + jpeg_encoder::Encoder::new(&mut out, quality) + .encode(&pixels, tile as u16, tile as u16, jpeg_encoder::ColorType::Rgb) + .map_err(|e| RawError::Corrupt(e.to_string()))?; + Ok(out) + }; + for tile in [16, 64] { + let bytes = write_lossy_dng(&DngImage::from(&raw), &DngWriteOptions::default(), tile, 90, &linearization, &encode).unwrap(); + let img = lines(&bytes).unwrap(); + assert_eq!((img.width, img.height, img.samples_per_pixel, img.bits), (w, h, 3, 8)); + // the same decoder as the full reader, so the same 8-bit codes; the full reader also + // applies the LinearizationTable, the bounded reader leaves that to its caller + let codes: Vec = img.data.iter().map(|&v| linearization[usize::from(v)]).collect(); + assert_eq!(codes, full(&bytes), "tile {tile}"); + } + // a lossy tile that is not a JPEG + let mut bad = base_ifd(16, 16, 8, 3, false); + bad.set(t::COMPRESSION, Value::Short(vec![34892])); + bad.set_image(ImageData::Tiles { tile_width: 16, tile_height: 16, tiles: vec![vec![0x55; 512]] }); + assert!(matches!(lines(&dng(bad, ByteOrder::Little)), Err(Error::Malformed(_) | Error::Unsupported(_)))); +} + +fn refused(edit: impl FnOnce(&mut craft_tiff::IfdBuilder), cpp: usize, cfa: bool) -> Error { + let (w, h) = (8, 4); + let mut raw = base_ifd(w, h, 16, cpp, cfa); + raw.set(t::COMPRESSION, Value::Short(vec![1])); + raw.set_image(ImageData::Strips { rows_per_strip: h as u32, strips: vec![vec![0; w * h * cpp * 2]] }); + edit(&mut raw); + lines(&dng(raw, ByteOrder::Little)).unwrap_err() +} + +#[test] +fn unsupported_and_damaged_layouts_are_refused() { + let unsupported = |e: Error| matches!(e, Error::Unsupported(_)); + assert!(unsupported(refused( + |r| { + r.set(t::COMPRESSION, Value::Short(vec![8])); + }, + 1, + true + ))); + assert!(unsupported(refused( + |r| { + r.set(t::COMPRESSION, Value::Short(vec![52546])); + }, + 1, + true + ))); + assert!(unsupported(refused( + |r| { + r.set(t::COMPRESSION, Value::Short(vec![5])); + }, + 1, + true + ))); + assert!(unsupported(refused( + |r| { + r.set(t::COMPRESSION, Value::Short(vec![34892])); + }, + 1, + true + ))); + assert!(unsupported(refused( + |r| { + r.set(t::COMPRESSION, Value::Short(vec![34892])); + }, + 3, + false + ))); + assert!(unsupported(refused( + |r| { + r.set(t::PREDICTOR, Value::Short(vec![2])); + }, + 1, + true + ))); + assert!(unsupported(refused( + |r| { + r.set(t::PLANAR_CONFIGURATION, Value::Short(vec![2])); + }, + 3, + false + ))); + assert!(unsupported(refused( + |r| { + r.set(t::SAMPLE_FORMAT, Value::Short(vec![3; 3])); + }, + 3, + false + ))); + assert!(unsupported(refused( + |r| { + r.set(t::BITS_PER_SAMPLE, Value::Short(vec![16, 12, 16])); + }, + 3, + false + ))); + assert!(unsupported(refused( + |r| { + r.set(t::BITS_PER_SAMPLE, Value::Short(vec![32])); + }, + 1, + true + ))); + assert!(unsupported(refused( + |r| { + r.set(t::SAMPLES_PER_PIXEL, Value::Short(vec![5])); + }, + 1, + false + ))); + assert!(matches!( + refused( + |r| { + r.set(t::SAMPLES_PER_PIXEL, Value::Short(vec![3])); + }, + 1, + true + ), + Error::Malformed(_) + )); + assert!(matches!( + refused( + |r| { + r.set(t::IMAGE_WIDTH, Value::Long(vec![0])); + }, + 1, + true + ), + Error::Malformed(_) + )); + // a strip shorter than its rows, and fewer strips than the image needs + let short = |r: &mut craft_tiff::IfdBuilder| { + r.set_image(ImageData::Strips { rows_per_strip: 4, strips: vec![vec![0; 10]] }); + }; + assert_eq!(refused(short, 1, true), Error::Truncated); + let few = |r: &mut craft_tiff::IfdBuilder| { + r.set_image(ImageData::Strips { rows_per_strip: 1, strips: vec![vec![0; 16]] }); + }; + assert!(matches!(refused(few, 1, true), Error::Malformed(_))); + // no full-resolution CFA or LinearRaw IFD at all + let none = |r: &mut craft_tiff::IfdBuilder| { + r.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![1])); + }; + assert!(unsupported(refused(none, 1, true))); + assert_eq!(lines(b"not a tiff at all").unwrap_err(), Error::NotTiff); +} + +#[test] +fn unpack_reads_every_depth_in_both_byte_orders() { + assert_eq!(dng_lines::unpack(&[1, 2, 3, 4], 2, 1, 1, 16, false).unwrap(), [0x0201, 0x0403]); + assert_eq!(dng_lines::unpack(&[1, 2, 3, 4], 2, 1, 1, 16, true).unwrap(), [0x0102, 0x0304]); + assert_eq!(dng_lines::unpack(&[7, 9], 1, 2, 1, 8, false).unwrap(), [7, 9]); + // rows start on a byte boundary: 3 x 4 bits per row is two bytes + assert_eq!(dng_lines::unpack(&[0x12, 0x30, 0x45, 0x60], 3, 2, 1, 4, true).unwrap(), [1, 2, 3, 4, 5, 6]); + assert_eq!(dng_lines::unpack(&[0x12], 3, 1, 1, 4, true).unwrap_err(), Error::Truncated); +} + +#[test] +fn a_lossless_jpeg_larger_than_its_tile_is_refused_before_decoding_it_all() { + // 16 x 16 tiles each holding a 512 x 512 lossless JPEG: the decoder may allocate only the + // tile's samples (and a little slack), not the frame the JPEG claims + let big = craft_raw::ljpeg::encode(&vec![7u16; 512 * 512], 512, 512, 1, 16, 1, 0); + let mut raw = base_ifd(32, 16, 16, 1, true); + raw.set(t::COMPRESSION, Value::Short(vec![7])); + raw.set_image(ImageData::Tiles { tile_width: 16, tile_height: 16, tiles: vec![big.clone(), big] }); + assert!(matches!(lines(&dng(raw, ByteOrder::Little)), Err(Error::TooLarge | Error::Malformed(_)))); + // the same tiles coded at their own size decode + let fit = craft_raw::ljpeg::encode(&vec![7u16; 16 * 16], 16, 16, 1, 16, 1, 0); + let mut raw = base_ifd(32, 16, 16, 1, true); + raw.set(t::COMPRESSION, Value::Short(vec![7])); + raw.set_image(ImageData::Tiles { tile_width: 16, tile_height: 16, tiles: vec![fit.clone(), fit] }); + assert_eq!(lines(&dng(raw, ByteOrder::Little)).unwrap().data, vec![7; 32 * 16]); +} + +#[test] +fn short_lossy_jpeg_strips_are_charged_their_own_rows() { + // 8192 x 4096 RGB (below MAX_SAMPLES) in 16-row lossy JPEG strips: each strip's JPEG is exactly + // 16 rows, so the decode budget charges 16 rows, not the 32 of the MCU padding allowance (which + // would put the image over the aggregate budget and refuse it) + let (w, h, rows) = (8192usize, 4096usize, 16usize); + let strip: Vec = (0..w * rows).flat_map(|i| [(i % 251) as u8, 90, 160]).collect(); + let mut jpeg = Vec::new(); + jpeg_encoder::Encoder::new(&mut jpeg, 90).encode(&strip, w as u16, rows as u16, jpeg_encoder::ColorType::Rgb).unwrap(); + let mut raw = base_ifd(w, h, 8, 3, false); + raw.set(t::COMPRESSION, Value::Short(vec![34892])); + raw.set_image(ImageData::Strips { rows_per_strip: rows as u32, strips: vec![jpeg; h / rows] }); + let img = lines(&dng(raw, ByteOrder::Little)).unwrap(); + assert_eq!((img.width, img.height, img.samples_per_pixel), (w, h, 3)); + assert_eq!(img.data.len(), w * h * 3); + // every strip holds the same picture + let band = w * rows * 3; + assert_eq!(&img.data[..band], &img.data[img.data.len() - band..]); +} diff --git a/crates/raw/tests/it/dng_metadata.rs b/crates/raw/tests/it/dng_metadata.rs new file mode 100644 index 000000000..a2f5e92ef --- /dev/null +++ b/crates/raw/tests/it/dng_metadata.rs @@ -0,0 +1,150 @@ +//! The bounded DNG metadata reader (`dng_metadata`) on hand-built DNGs, checked against the tags +//! written and, for the colour tags, against the full reader. + +use super::dng::{base_ifd, dng, dng_with}; +use craft_raw::dng_lines::{self, Error}; +use craft_raw::dng_metadata::{Metadata, Pattern}; +use craft_tiff::classic::{Tiff, tags as ct}; +use craft_tiff::{ByteOrder, IfdBuilder, ImageData, Value}; + +fn cfa_raw(edit: impl FnOnce(&mut IfdBuilder)) -> IfdBuilder { + let mut raw = base_ifd(8, 4, 16, 1, true); + raw.set(ct::COMPRESSION, Value::Short(vec![1])); + raw.set_image(ImageData::Strips { rows_per_strip: 4, strips: vec![vec![0; 64]] }); + edit(&mut raw); + raw +} + +fn with_metadata(bytes: &[u8], f: impl FnOnce(&Metadata) -> T) -> T { + let (tiff, first) = Tiff::new(bytes).unwrap(); + let ifds = tiff.all_ifds(first).unwrap(); + let raw = dng_lines::find_raw_ifd(&tiff, &ifds).unwrap(); + f(&Metadata { tiff, main: &ifds[0], raw }) +} + +#[test] +fn processing_tags_are_read_from_the_raw_ifd() { + let raw = cfa_raw(|r| { + r.set(ct::ACTIVE_AREA, Value::Long(vec![1, 2, 3, 7])); + r.set(ct::LINEARIZATION_TABLE, Value::Short(vec![0, 10, 20])); + r.set(ct::BLACK_LEVEL_REPEAT_DIM, Value::Short(vec![2, 2])); + r.set(ct::BLACK_LEVEL, Value::Rational(vec![(64, 1), (65, 1), (66, 1), (67, 1)])); + r.set(ct::BLACK_LEVEL_DELTA_H, Value::SRational(vec![(1, 2); 8])); + r.set(ct::BLACK_LEVEL_DELTA_V, Value::SRational(vec![(-1, 2); 4])); + r.set(ct::WHITE_LEVEL, Value::Long(vec![4000])); + r.set(ct::DEFAULT_CROP_ORIGIN, Value::Rational(vec![(1, 2), (1, 1)])); + r.set(ct::DEFAULT_CROP_SIZE, Value::Long(vec![6, 2])); + }); + for order in [ByteOrder::Little, ByteOrder::Big] { + let p = with_metadata(&dng(raw.clone(), order), |m| m.params().unwrap()); + assert_eq!(p.active_area, Some([1, 2, 3, 7])); + assert_eq!(p.linearization, Some(vec![0, 10, 20])); + assert_eq!(p.black_repeat, (2, 2)); + assert_eq!(p.black, [64.0, 65.0, 66.0, 67.0]); + assert_eq!(p.black_delta_h, [0.5; 8]); + assert_eq!(p.black_delta_v, [-0.5; 4]); + assert_eq!(p.white, [4000.0]); + assert_eq!((p.crop_origin, p.crop_size), (Some((0.5, 1.0)), Some((6.0, 2.0)))); + // base_ifd writes CFAPattern 1 0 2 1 (GRBG) + assert_eq!(p.cfa, Some(Pattern { rows: 2, cols: 2, colors: vec![1, 0, 2, 1] })); + } + // a LinearizationTable longer than 65536 entries is refused + let long = cfa_raw(|r| { + r.set(ct::LINEARIZATION_TABLE, Value::Short(vec![1; 65_537])); + }); + assert!(matches!(with_metadata(&dng(long, ByteOrder::Little), |m| m.params()), Err(Error::Malformed(_)))); + // absent tags take the DNG defaults + let p = with_metadata(&dng(cfa_raw(|_| {}), ByteOrder::Little), |m| m.params().unwrap()); + assert_eq!((p.active_area, p.linearization, p.black_repeat), (None, None, (1, 1))); + assert!(p.black.is_empty() && p.white.is_empty() && p.crop_origin.is_none()); +} + +#[test] +fn cfa_patterns_map_plane_colours_and_refuse_odd_layouts() { + let pattern = |edit: fn(&mut IfdBuilder)| with_metadata(&dng(cfa_raw(edit), ByteOrder::Little), |m| m.cfa()); + // CFAPlaneColor reorders the planes: file colour 2 is plane 0 + let swapped = pattern(|r| { + r.set(ct::CFA_PLANE_COLOR, Value::Byte(vec![2, 1, 0])); + r.set(ct::CFA_LAYOUT, Value::Short(vec![1])); + }); + assert_eq!(swapped.unwrap(), Some(Pattern { rows: 2, cols: 2, colors: vec![1, 2, 0, 1] })); + let unsupported = |r: Result, Error>| matches!(r, Err(Error::Unsupported(_))); + let malformed = |r: Result, Error>| matches!(r, Err(Error::Malformed(_))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_LAYOUT, Value::Short(vec![2])); + }))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_PLANE_COLOR, Value::Byte(vec![0, 1, 3])); + }))); + assert!(unsupported(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![9, 2])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![2])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_REPEAT_PATTERN_DIM, Value::Short(vec![4, 4])); + }))); + assert!(malformed(pattern(|r| { + r.set(ct::CFA_PATTERN, Value::Byte(vec![0, 1, 1, 3])); + }))); + // LinearRaw has no pattern + let mut linear = base_ifd(4, 2, 16, 3, false); + linear.set(ct::COMPRESSION, Value::Short(vec![1])); + linear.set_image(ImageData::Strips { rows_per_strip: 2, strips: vec![vec![0; 48]] }); + assert_eq!(with_metadata(&dng(linear, ByteOrder::Big), |m| m.cfa()).unwrap(), None); +} + +#[test] +fn calibration_matches_the_full_reader() { + let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Little, |ifd0| { + ifd0.set(ct::COLOR_MATRIX_2, Value::SRational(vec![(2, 1), (0, 1), (0, 1), (0, 1), (2, 1), (0, 1), (0, 1), (0, 1), (2, 1)])); + ifd0.set(ct::CALIBRATION_ILLUMINANT_2, Value::Short(vec![17])); + ifd0.set(ct::ANALOG_BALANCE, Value::Rational(vec![(1, 1); 3])); + ifd0.set(ct::AS_SHOT_NEUTRAL, Value::Rational(vec![(1, 2), (1, 1), (3, 4)])); + ifd0.set(ct::BASELINE_EXPOSURE, Value::SRational(vec![(1, 4)])); + // not 3 x 3 (a four-colour camera): not a matrix here + ifd0.set(ct::FORWARD_MATRIX_1, Value::SRational(vec![(1, 1); 12])); + }); + let c = with_metadata(&bytes, |m| m.calibration().unwrap()); + let full = craft_raw::decode(&bytes).unwrap().color; + assert_eq!(c.color_matrix, full.color_matrix); + assert_eq!(c.illuminant, [Some(21), Some(17)]); + assert_eq!(c.as_shot_neutral, full.as_shot_neutral); + assert_eq!(c.analog_balance, Some([1.0; 3])); + assert_eq!(c.as_shot_white_xy, Some((0.3127, 0.329))); + assert_eq!(c.baseline_exposure, 0.25); + assert_eq!(c.forward_matrix, [None, None]); + assert_eq!(c.camera_calibration, [None, None]); +} + +#[test] +fn capture_info_matches_the_full_readers_cinema_tags() { + use craft_raw::dng_metadata::Timecode; + let tc = Timecode { hours: 10, minutes: 59, seconds: 7, frames: 23, drop_frame: true }; + let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Big, |ifd0| { + ifd0.set(ct::FRAME_RATE, Value::SRational(vec![(24000, 1001)])); + ifd0.set(ct::TIME_CODES, Value::Byte(tc.to_bytes().to_vec())); + ifd0.set(ct::REEL_NAME, Value::Ascii("A001".into())); + ifd0.set(ct::MODEL, Value::Ascii("Cam".into())); + }); + let info = with_metadata(&bytes, |m| m.capture_info()); + let cinema = craft_raw::decode(&bytes).unwrap().cinema; + assert_eq!(info.dng_version, [1, 6, 0, 0]); + assert_eq!(info.frame_rate, Some((24000, 1001))); + assert_eq!((info.frame_rate, info.timecode), (cinema.frame_rate, cinema.timecode)); + assert_eq!(info.timecode, Some(tc)); + assert_eq!(info.reel.as_deref(), cinema.reel_name.as_deref()); + assert_eq!(info.camera.as_deref(), Some("Hand Cam")); + // an implausible rate and a broken time code are dropped by both; UniqueCameraModel wins + let bytes = dng_with(cfa_raw(|_| {}), ByteOrder::Little, |ifd0| { + ifd0.set(ct::FRAME_RATE, Value::Rational(vec![(5000, 1)])); + ifd0.set(ct::TIME_CODES, Value::Byte(vec![0x0a, 0, 0, 0, 0, 0, 0, 0])); + ifd0.set(ct::UNIQUE_CAMERA_MODEL, Value::Ascii("Hand Cam Mk II".into())); + }); + let info = with_metadata(&bytes, |m| m.capture_info()); + assert_eq!((info.frame_rate, info.timecode, info.reel), (None, None, None)); + assert_eq!(info.camera.as_deref(), Some("Hand Cam Mk II")); + let cinema = craft_raw::decode(&bytes).unwrap().cinema; + assert_eq!((cinema.frame_rate, cinema.timecode), (None, None)); +} diff --git a/crates/raw/tests/it/main.rs b/crates/raw/tests/it/main.rs index 3ed7a0b82..5a9386eb0 100644 --- a/crates/raw/tests/it/main.rs +++ b/crates/raw/tests/it/main.rs @@ -2,6 +2,10 @@ #[cfg(feature = "dng")] mod dng; +#[cfg(feature = "line-dng")] +mod dng_lines; +#[cfg(feature = "line-dng")] +mod dng_metadata; #[cfg(feature = "dng")] mod robust; mod vendor_robust; diff --git a/crates/render-parity/Cargo.toml b/crates/render-parity/Cargo.toml new file mode 100644 index 000000000..087ff748b --- /dev/null +++ b/crates/render-parity/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "craft-render-parity" +description = "Compare two renders of the same photo (per-channel error and CIEDE2000) against a stated tolerance: the parity check for the shared develop pipeline. Test support only." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true +publish = false + +[lints] +workspace = true + +[dependencies] +craft-cms = { workspace = true } diff --git a/crates/render-parity/README.md b/crates/render-parity/README.md new file mode 100644 index 000000000..0924d891a --- /dev/null +++ b/crates/render-parity/README.md @@ -0,0 +1,37 @@ +# craft-render-parity + +Test support for the shared develop pipeline ([#587](https://github.com/storytold/craft-libs/issues/587)): +how far two renders of the same photo are apart, and whether that is within a stated tolerance. +A develop stage that moves from an app, or an app that switches to the shared pipeline, renders +the same fixture both ways and compares the two here. + +```toml +[dev-dependencies] +craft-render-parity = { git = "https://github.com/storytold/craft-libs", rev = "" } +``` + +```rust +use craft_render_parity::{Image, Tolerance, compare}; + +let expected = Image::new(width, height, &app_render)?; +let actual = Image::new(width, height, &shared_render)?; +let report = compare(&expected, &actual)?; +assert!(report.within(&Tolerance::BASELINE), "{report}: {:?}", report.exceeded(&Tolerance::BASELINE)); +``` + +## Input + +Scene-linear RGB with Rec. 709 / sRGB primaries and a D65 white, three interleaved `f32` samples +per pixel, row by row, with 1.0 as diffuse white. Values above 1.0 are compared as they are. A NaN +or infinite sample is an error, never a pass. + +## Measures + +| Measure | Meaning | +|---|---| +| `max_delta_e`, `mean_delta_e` | CIEDE2000 (kL = kC = kH = 1) in CIELAB relative to D65, from `craft_cms::math` | +| `max_channel`, `mean_channel` | Absolute difference of the linear samples, overall and per channel | +| `worst` | The first pixel with the largest CIEDE2000 difference, or the first that differs at all | + +`Tolerance::BASELINE`, the acceptance bar of #587, is CIEDE2000 at most 0.5 at any pixel and 0.05 +on average (1.0 is about the smallest visible difference), and at most 1/1024 in any linear sample. diff --git a/crates/render-parity/src/lib.rs b/crates/render-parity/src/lib.rs new file mode 100644 index 000000000..aedac7f0f --- /dev/null +++ b/crates/render-parity/src/lib.rs @@ -0,0 +1,205 @@ +//! Render parity: how far two renders of the same photo are apart, and whether that is within a +//! stated [`Tolerance`]. +//! +//! The shared develop pipeline (craft-libs#587) has to give LightCraft and PhotoCraft the same +//! picture. A stage moved from an app, or an app switched to the shared crate, is checked by +//! rendering the same fixture both ways and comparing them here. The [`Report`] names the worst +//! pixel, so a failure points at where to look. +//! +//! Renders are scene-linear RGB with Rec. 709 / sRGB primaries and a D65 white, interleaved, with +//! 1.0 as diffuse white. Colour difference is CIEDE2000 in CIELAB relative to D65 (from +//! [`craft_cms::math`]); values above 1.0 are allowed and compared as they are. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +use std::fmt; + +use craft_cms::math::{delta_e2000, xyz_to_lab}; + +/// Linear Rec. 709 RGB → CIE XYZ (D65), IEC 61966-2-1. +const RGB_TO_XYZ: [[f64; 3]; 3] = [[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722], [0.0193, 0.1192, 0.9505]]; +/// The D65 white in XYZ, from the matrix above applied to RGB (1, 1, 1). +const D65: [f64; 3] = [0.9505, 1.0, 1.089]; + +/// A render to compare: `width` × `height` interleaved RGB samples, row by row. +#[derive(Clone, Copy, Debug)] +pub struct Image<'a> { + width: u32, + height: u32, + rgb: &'a [f32], +} + +impl<'a> Image<'a> { + /// A render of `width` × `height` pixels whose `rgb` holds exactly three samples per pixel. + pub fn new(width: u32, height: u32, rgb: &'a [f32]) -> Result { + let expected = (width as usize).checked_mul(height as usize).and_then(|n| n.checked_mul(3)); + if expected != Some(rgb.len()) { + return Err(ParityError::Length { width, height, samples: rgb.len() }); + } + Ok(Self { width, height, rgb }) + } + + pub fn width(&self) -> u32 { + self.width + } + + pub fn height(&self) -> u32 { + self.height + } +} + +/// Why two renders could not be compared. +#[derive(Clone, Debug, PartialEq)] +pub enum ParityError { + /// The sample count is not three per pixel. + Length { width: u32, height: u32, samples: usize }, + /// The renders differ in size, so a pixel of one has no counterpart in the other. + Size { expected: (u32, u32), actual: (u32, u32) }, + /// A sample is NaN or infinite: no difference can be stated for it. + NotFinite { x: u32, y: u32 }, +} + +impl fmt::Display for ParityError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Length { width, height, samples } => write!(f, "{samples} samples is not RGB for {width} × {height} pixels"), + Self::Size { expected, actual } => write!(f, "render is {} × {}, expected {} × {}", actual.0, actual.1, expected.0, expected.1), + Self::NotFinite { x, y } => write!(f, "pixel ({x}, {y}) is not a finite colour"), + } + } +} + +impl std::error::Error for ParityError {} + +/// How far two renders may be apart and still count as the same picture. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Tolerance { + /// The largest CIEDE2000 difference at any pixel. + pub max_delta_e: f64, + /// The largest mean CIEDE2000 difference over the picture. + pub mean_delta_e: f64, + /// The largest difference in any one linear channel sample. + pub max_channel: f32, +} + +impl Tolerance { + /// The baseline parity of craft-libs#587: well below a visible difference (CIEDE2000 1.0), and + /// within a 10-bit code value in any channel. + pub const BASELINE: Self = Self { max_delta_e: 0.5, mean_delta_e: 0.05, max_channel: 1.0 / 1024.0 }; +} + +impl Default for Tolerance { + fn default() -> Self { + Self::BASELINE + } +} + +/// The difference between two renders of the same size. +#[derive(Clone, Debug, PartialEq)] +pub struct Report { + /// Pixels compared. + pub pixels: u64, + /// The largest CIEDE2000 difference, at [`Self::worst`]. + pub max_delta_e: f64, + /// The mean CIEDE2000 difference. + pub mean_delta_e: f64, + /// The largest absolute difference in any channel sample. + pub max_channel: f32, + /// The mean absolute difference per channel (R, G, B). + pub mean_channel: [f64; 3], + /// The first pixel with the largest CIEDE2000 difference, when any pixel differs. + pub worst: Option<(u32, u32)>, +} + +impl Report { + /// Whether every measure is within `tolerance`. A NaN limit is never met. + pub fn within(&self, tolerance: &Tolerance) -> bool { + self.exceeded(tolerance).is_empty() + } + + /// The measures over `tolerance`, each with its value and limit, for a test failure message. + pub fn exceeded(&self, tolerance: &Tolerance) -> Vec { + let mut over = Vec::new(); + if exceeds(self.max_delta_e, tolerance.max_delta_e) { + over.push(format!("max ΔE2000 {:.4} > {}", self.max_delta_e, tolerance.max_delta_e)); + } + if exceeds(self.mean_delta_e, tolerance.mean_delta_e) { + over.push(format!("mean ΔE2000 {:.4} > {}", self.mean_delta_e, tolerance.mean_delta_e)); + } + if exceeds(f64::from(self.max_channel), f64::from(tolerance.max_channel)) { + over.push(format!("max channel difference {:.6} > {}", self.max_channel, tolerance.max_channel)); + } + over + } +} + +impl fmt::Display for Report { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} px: ΔE2000 max {:.4} mean {:.4}, channel max {:.6}", self.pixels, self.max_delta_e, self.mean_delta_e, self.max_channel)?; + match self.worst { + Some((x, y)) => write!(f, ", worst at ({x}, {y})"), + None => write!(f, ", identical"), + } + } +} + +/// Compare `actual` with `expected`, pixel by pixel. +pub fn compare(expected: &Image<'_>, actual: &Image<'_>) -> Result { + if (expected.width, expected.height) != (actual.width, actual.height) { + return Err(ParityError::Size { expected: (expected.width, expected.height), actual: (actual.width, actual.height) }); + } + let width = expected.width.max(1) as usize; + let mut report = Report { pixels: 0, max_delta_e: 0.0, mean_delta_e: 0.0, max_channel: 0.0, mean_channel: [0.0; 3], worst: None }; + let mut sum_delta_e = 0.0; + for (i, (e, a)) in expected.rgb.as_chunks::<3>().0.iter().zip(actual.rgb.as_chunks::<3>().0).enumerate() { + let (x, y) = ((i % width) as u32, (i / width) as u32); + let (Some(e), Some(a)) = (finite(*e), finite(*a)) else { return Err(ParityError::NotFinite { x, y }) }; + for c in 0..3 { + let d = (e[c] - a[c]).abs(); + report.max_channel = report.max_channel.max(d); + report.mean_channel[c] += f64::from(d); + } + let delta_e = delta_e2000(lab(e), lab(a)); + sum_delta_e += delta_e; + if delta_e > report.max_delta_e { + report.max_delta_e = delta_e; + report.worst = Some((x, y)); + } + report.pixels += 1; + } + if report.pixels > 0 { + let n = report.pixels as f64; + report.mean_delta_e = sum_delta_e / n; + report.mean_channel = report.mean_channel.map(|s| s / n); + } + if report.worst.is_none() && report.max_channel > 0.0 { + // channel differences too small to move CIELAB still name a pixel + report.worst = first_difference(expected, actual); + } + Ok(report) +} + +/// Whether `value` exceeds `limit`. A NaN limit (or value) exceeds it: an unordered comparison never +/// passes, so an invalid tolerance cannot switch a check off. +fn exceeds(value: f64, limit: f64) -> bool { + !matches!(value.partial_cmp(&limit), Some(std::cmp::Ordering::Less | std::cmp::Ordering::Equal)) +} + +fn finite(px: [f32; 3]) -> Option<[f32; 3]> { + px.iter().all(|v| v.is_finite()).then_some(px) +} + +fn first_difference(expected: &Image<'_>, actual: &Image<'_>) -> Option<(u32, u32)> { + let width = expected.width.max(1) as usize; + let i = expected.rgb.as_chunks::<3>().0.iter().zip(actual.rgb.as_chunks::<3>().0).position(|(e, a)| e != a)?; + Some(((i % width) as u32, (i / width) as u32)) +} + +fn lab(rgb: [f32; 3]) -> [f64; 3] { + let rgb = rgb.map(f64::from); + let xyz = RGB_TO_XYZ.map(|row| row[0] * rgb[0] + row[1] * rgb[1] + row[2] * rgb[2]); + xyz_to_lab(xyz, D65) +} + +#[cfg(test)] +mod tests; diff --git a/crates/render-parity/src/tests.rs b/crates/render-parity/src/tests.rs new file mode 100644 index 000000000..a5c81f24f --- /dev/null +++ b/crates/render-parity/src/tests.rs @@ -0,0 +1,100 @@ +use super::*; + +fn gray_ramp(width: u32, height: u32) -> Vec { + (0..width * height).flat_map(|i| [i as f32 / (width * height) as f32; 3]).collect() +} + +#[test] +fn identical_renders_are_within_any_tolerance() { + let rgb = gray_ramp(4, 3); + let image = Image::new(4, 3, &rgb).unwrap(); + let report = compare(&image, &image).unwrap(); + assert_eq!((report.pixels, report.max_delta_e, report.max_channel, report.worst), (12, 0.0, 0.0, None)); + assert!(report.within(&Tolerance { max_delta_e: 0.0, mean_delta_e: 0.0, max_channel: 0.0 })); + assert_eq!(report.to_string(), "12 px: ΔE2000 max 0.0000 mean 0.0000, channel max 0.000000, identical"); +} + +#[test] +fn white_in_the_matrix_is_neutral() { + // RGB (1, 1, 1) is the D65 white: L* 100, a* = b* = 0 + let [l, a, b] = lab([1.0; 3]); + assert!((l - 100.0).abs() < 1e-9 && a.abs() < 1e-9 && b.abs() < 1e-9, "{l} {a} {b}"); +} + +#[test] +fn the_worst_pixel_and_the_means_are_reported() { + let expected = gray_ramp(3, 2); + let mut actual = expected.clone(); + // pixel (1, 1): red up by 0.1, a visible difference + actual[4 * 3] += 0.1; + let report = compare(&Image::new(3, 2, &expected).unwrap(), &Image::new(3, 2, &actual).unwrap()).unwrap(); + assert_eq!(report.worst, Some((1, 1))); + assert!(report.max_delta_e > 1.0, "{report}"); + assert!((report.mean_delta_e - report.max_delta_e / 6.0).abs() < 1e-9); + assert!((report.max_channel - 0.1).abs() < 1e-6); + assert!((report.mean_channel[0] - 0.1 / 6.0).abs() < 1e-6 && report.mean_channel[1] == 0.0); + let over = report.exceeded(&Tolerance::BASELINE); + assert_eq!(over.len(), 3, "{over:?}"); + assert!(over[0].starts_with("max ΔE2000") && over[1].starts_with("mean ΔE2000") && over[2].starts_with("max channel")); + assert!(!report.within(&Tolerance::default())); + assert!(report.to_string().ends_with("worst at (1, 1)")); +} + +#[test] +fn a_tenth_of_a_10_bit_code_value_passes_the_baseline() { + let expected = vec![0.18f32; 2 * 2 * 3]; + let actual: Vec = expected.iter().map(|v| v + 0.1 / 1024.0).collect(); + let report = compare(&Image::new(2, 2, &expected).unwrap(), &Image::new(2, 2, &actual).unwrap()).unwrap(); + assert!(report.within(&Tolerance::BASELINE), "{report}"); + assert!(report.worst.is_some()); +} + +#[test] +fn a_channel_difference_too_small_for_cielab_still_names_a_pixel() { + let expected = vec![0.5f32; 6]; + let mut actual = expected.clone(); + actual[5] = f32::from_bits(0.5f32.to_bits() + 1); + let report = compare(&Image::new(2, 1, &expected).unwrap(), &Image::new(2, 1, &actual).unwrap()).unwrap(); + assert!(report.max_channel > 0.0); + assert_eq!(report.worst, Some((1, 0))); +} + +#[test] +fn an_empty_render_compares_as_identical() { + let image = Image::new(0, 5, &[]).unwrap(); + let report = compare(&image, &image).unwrap(); + assert_eq!((report.pixels, report.mean_delta_e, report.worst), (0, 0.0, None)); + assert_eq!((image.width(), image.height()), (0, 5)); +} + +#[test] +fn bad_inputs_are_errors_not_panics() { + let e = Image::new(2, 2, &[0.0; 11]).unwrap_err(); + assert_eq!(e, ParityError::Length { width: 2, height: 2, samples: 11 }); + assert_eq!(e.to_string(), "11 samples is not RGB for 2 × 2 pixels"); + assert!(Image::new(u32::MAX, u32::MAX, &[]).is_err()); + + let a = vec![0.0f32; 6]; + let b = vec![0.0f32; 6]; + let e = compare(&Image::new(2, 1, &a).unwrap(), &Image::new(1, 2, &b).unwrap()).unwrap_err(); + assert_eq!(e.to_string(), "render is 1 × 2, expected 2 × 1"); + + let mut nan = a.clone(); + nan[4] = f32::NAN; + let e = compare(&Image::new(2, 1, &a).unwrap(), &Image::new(2, 1, &nan).unwrap()).unwrap_err(); + assert_eq!(e, ParityError::NotFinite { x: 1, y: 0 }); + assert_eq!(e.to_string(), "pixel (1, 0) is not a finite colour"); + let e: Box = Box::new(e); + assert!(e.source().is_none()); +} + +#[test] +fn a_nan_tolerance_fails_every_check() { + let rgb = gray_ramp(2, 2); + let image = Image::new(2, 2, &rgb).unwrap(); + let report = compare(&image, &image).unwrap(); + let nan = Tolerance { max_delta_e: f64::NAN, mean_delta_e: f64::NAN, max_channel: f32::NAN }; + assert_eq!(report.exceeded(&nan).len(), 3); + assert!(!report.within(&nan)); + assert!(report.within(&Tolerance { max_delta_e: f64::INFINITY, ..Tolerance::BASELINE })); +} diff --git a/crates/sniff/Cargo.toml b/crates/sniff/Cargo.toml new file mode 100644 index 000000000..5fd0955d5 --- /dev/null +++ b/crates/sniff/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "craft-sniff" +description = "What a file is from its first bytes: a dependency-free signature table naming the format, its media type, extension and family, for images, audio, video, 3D models, archives, executables and databases." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true diff --git a/crates/sniff/src/detect.rs b/crates/sniff/src/detect.rs new file mode 100644 index 000000000..d97baf126 --- /dev/null +++ b/crates/sniff/src/detect.rs @@ -0,0 +1,264 @@ +//! The signature checks behind [`identify`]. + +use crate::Kind; + +/// How many leading bytes [`identify`] reads at most. Every signature it knows lies within them. +pub const HEAD: usize = 4096; + +/// The largest universal Mach-O architecture count taken as Mach-O rather than a Java class. +/// Both start `CAFEBABE`; a class file's next four bytes are its minor and major version, and +/// every released class version (major 45, JDK 1.0, onwards) reads as at least 45 there, while +/// real universal binaries hold a handful of architectures. +const MAX_FAT_ARCHES: u32 = 44; + +/// The ISO BMFF brands read from an `ftyp` box: the major brand and up to this many compatible ones. +const MAX_BRANDS: usize = 32; + +/// The MPEG transport stream packet size and the sync byte each packet starts with. +const TS_PACKET: usize = 188; +const TS_SYNC: u8 = 0x47; + +/// Name the format of a file from its leading bytes, or `None` when no signature matches. +/// +/// Only the first [`HEAD`] bytes are read. Signatures at the start of the file are checked +/// before the weaker ones further in or built from few bits (TAR at byte 257, MP3 and AAC frame +/// headers, MPEG transport stream packets), so a file is never named by a weak signature when a +/// strong one matches. +/// +/// `CAFEBABE` starts both a universal Mach-O and a Java class file. The next big-endian 32-bit +/// value is a Mach-O architecture count or a class file's version: up to 44 is taken as +/// [`Kind::MachOFat`], 45 and over as [`Kind::JavaClass`]. A caller that knows the file name +/// (`.class`) may prefer that. +pub fn identify(bytes: &[u8]) -> Option { + let head = bytes.get(..HEAD).unwrap_or(bytes); + leading(head).or_else(|| container(head)).or_else(|| trailing(head)) +} + +/// Signatures at byte 0 that need nothing more than a fixed prefix. +fn leading(b: &[u8]) -> Option { + const PREFIXES: &[(&[u8], Kind)] = &[ + (b"\x89PNG\r\n\x1a\n", Kind::Png), + (b"\xff\xd8\xff", Kind::Jpeg), + (b"GIF87a", Kind::Gif), + (b"GIF89a", Kind::Gif), + (b"\xff\x0a", Kind::Jxl), + (b"\x00\x00\x00\x0cJXL \r\n\x87\n", Kind::Jxl), + (b"qoif", Kind::Qoi), + (b"\x76\x2f\x31\x01", Kind::OpenExr), + (b"fLaC", Kind::Flac), + (b"MThd\x00\x00\x00\x06", Kind::Midi), + (b"#!AMR", Kind::Amr), + (b"FLV\x01", Kind::Flv), + (b"\x00\x00\x01\xba", Kind::MpegPs), + (b"glTF", Kind::Glb), + (b"Kaydara FBX Binary ", Kind::Fbx), + (b"ply\n", Kind::Ply), + (b"ply\r", Kind::Ply), + (b"PK\x03\x04", Kind::Zip), + (b"PK\x05\x06", Kind::Zip), + (b"7z\xbc\xaf\x27\x1c", Kind::SevenZip), + (b"Rar!\x1a\x07\x00", Kind::Rar), + (b"Rar!\x1a\x07\x01\x00", Kind::Rar), + (b"\x1f\x8b\x08", Kind::Gzip), + (b"\xfd7zXZ\x00", Kind::Xz), + (b"\x28\xb5\x2f\xfd", Kind::Zstd), + (b"070701", Kind::Cpio), + (b"070702", Kind::Cpio), + (b"070707", Kind::Cpio), + (b"\xc7\x71", Kind::Cpio), + (b"\x71\xc7", Kind::Cpio), + (b"\x7fELF", Kind::Elf), + (b"\xfe\xed\xfa\xce", Kind::MachO), + (b"\xfe\xed\xfa\xcf", Kind::MachO), + (b"\xce\xfa\xed\xfe", Kind::MachO), + (b"\xcf\xfa\xed\xfe", Kind::MachO), + (b"\xca\xfe\xba\xbf", Kind::MachOFat), + (b"\0asm", Kind::Wasm), + (b"dex\n", Kind::Dex), + (b"SQLite format 3\0", Kind::Sqlite), + (b"bplist00", Kind::BinaryPlist), + (b"%PDF-", Kind::Pdf), + (b"ID3", Kind::Mp3), + ]; + PREFIXES.iter().find(|(magic, _)| b.starts_with(magic)).map(|&(_, kind)| kind).or_else(|| checked(b)) +} + +/// Signatures at byte 0 whose prefix is short or shared, confirmed by a field after it. +fn checked(b: &[u8]) -> Option { + if b.starts_with(b"MZ") { + return Some(dos(b)); + } + match b.get(..4)? { + [0xca, 0xfe, 0xba, 0xbe] => Some(if be32(b, 4)? <= MAX_FAT_ARCHES { Kind::MachOFat } else { Kind::JavaClass }), + b"II*\0" | b"MM\0*" | b"II+\0" | b"MM\0+" => Some(Kind::Tiff), + b"8BPS" => matches!(be16(b, 4)?, 1 | 2).then_some(Kind::Psd), + [0, 0, 1, 0] => (le16(b, 4)? > 0 && b.get(9).copied()? == 0).then_some(Kind::Ico), + [b'B', b'Z', b'h', level] => (b'1'..=b'9').contains(level).then_some(Kind::Bzip2), + [b'B', b'M', ..] => bmp(b), + _ => None, + } +} + +/// `MZ`: a PE file when `e_lfanew` (at 0x3C) points at `PE\0\0` within the bytes given. +fn dos(b: &[u8]) -> Kind { + let pe = le32(b, 0x3c) + .and_then(|offset| usize::try_from(offset).ok()) + .and_then(|offset| b.get(offset..offset.checked_add(4)?)) + .is_some_and(|sig| sig == b"PE\0\0"); + if pe { Kind::Pe } else { Kind::DosExecutable } +} + +/// `BM` alone is two letters of text, so a BMP also needs zero reserved fields and a known +/// DIB header size. +fn bmp(b: &[u8]) -> Option { + let reserved = b.get(6..10)? == [0, 0, 0, 0]; + let dib = matches!(le32(b, 14)?, 12 | 16 | 40 | 52 | 56 | 64 | 108 | 124); + (reserved && dib).then_some(Kind::Bmp) +} + +/// Container formats named by what the container says it holds. +fn container(b: &[u8]) -> Option { + match (b.get(..4)?, b.get(8..12)) { + (b"RIFF", Some(b"WAVE")) => Some(Kind::Wav), + (b"RIFF", Some(b"WEBP")) => Some(Kind::WebP), + (b"RIFF", Some(b"AVI ")) => Some(Kind::Avi), + (b"FORM", Some(b"AIFF" | b"AIFC")) => Some(Kind::Aiff), + (b"OggS", _) => ogg(b), + ([0x1a, 0x45, 0xdf, 0xa3], _) => Some(ebml(b)), + _ => iso_bmff(b), + } +} + +/// An Ogg stream is named by its first packet: Opus and Theora have their own types. +fn ogg(b: &[u8]) -> Option { + let segments = usize::from(*b.get(26)?); + let packet = b.get(27usize.checked_add(segments)?..).unwrap_or_default(); + Some(if packet.starts_with(b"OpusHead") { + Kind::Opus + } else if packet.starts_with(b"\x80theora") { + Kind::OggVideo + } else { + Kind::Ogg + }) +} + +/// EBML: WebM and Matroska differ by the `DocType` element (ID 0x4282) in the EBML header. +fn ebml(b: &[u8]) -> Kind { + let header = b.get(..64).unwrap_or(b); + let doc_type = header.windows(2).position(|w| w == [0x42, 0x82]).and_then(|at| { + let size = header.get(at.checked_add(2)?).copied()?; + // A one-byte EBML size has its top bit set; the value is the remaining seven bits. + let len = usize::from(size & 0x7f); + let start = at.checked_add(3)?; + if size & 0x80 == 0 { + return None; + } + header.get(start..start.checked_add(len)?) + }); + if doc_type == Some(b"webm") { Kind::WebM } else { Kind::Matroska } +} + +/// ISO base media files: an `ftyp` box whose brands say what it holds, or an old QuickTime +/// movie starting with one of its own top-level atoms. +fn iso_bmff(b: &[u8]) -> Option { + let box_type = b.get(4..8)?; + let size = usize::try_from(be32(b, 0)?).ok()?; + if box_type != b"ftyp" { + let atom = matches!(box_type, b"moov" | b"mdat" | b"wide") && size >= 8; + return atom.then_some(Kind::QuickTime); + } + if size < 16 { + return None; + } + let end = size.min(b.len()).min(16usize.saturating_add(MAX_BRANDS.saturating_mul(4))); + let major: &[u8; 4] = b.get(8..12)?.try_into().ok()?; + let compatible = b.get(16..end).unwrap_or_default().as_chunks::<4>().0; + let brands = core::iter::once(major).chain(compatible.iter()); + // A still image also lists generic brands, so image brands win wherever they are listed: + // AVIF and JPEG XL over HEVC-coded HEIF, over the bare HEIF structure brands, over video. + let mut best: Option<(u8, Kind)> = None; + for kind in brands.filter_map(brand_kind) { + let rank = brand_rank(kind); + if best.is_none_or(|(top, _)| rank > top) { + best = Some((rank, kind)); + } + } + best.map(|(_, kind)| kind) +} + +fn brand_rank(kind: Kind) -> u8 { + match kind { + Kind::Avif | Kind::Jxl => 3, + Kind::Heif => 2, + _ => 0, + } +} + +fn brand_kind(brand: &[u8; 4]) -> Option { + Some(match brand { + b"avif" | b"avis" => Kind::Avif, + b"heic" | b"heix" | b"heim" | b"heis" | b"hevc" | b"hevx" => Kind::Heif, + // The HEIF structure brands. AVIF files list them too, and AVIF outranks them. + b"mif1" | b"msf1" => Kind::Heif, + b"jxl " => Kind::Jxl, + b"qt " => Kind::QuickTime, + b"M4A " | b"M4B " | b"M4P " => Kind::M4a, + b"3gp4" | b"3gp5" | b"3gp6" | b"3gp7" | b"3gg6" | b"3g2a" | b"3g2b" | b"3g2c" => Kind::ThreeGp, + b"isom" | b"iso2" | b"iso3" | b"iso4" | b"iso5" | b"iso6" | b"mp41" | b"mp42" | b"avc1" | b"dash" | b"mmp4" | b"M4V " | b"M4VH" | b"M4VP" + | b"MSNV" | b"f4v " => Kind::Mp4, + _ => return None, + }) +} + +/// Signatures that sit past byte 0 or are made of a few bits, checked last. +fn trailing(b: &[u8]) -> Option { + if let Some(magic) = b.get(257..263) + && (magic == b"ustar\0" || magic == b"ustar ") + { + return Some(Kind::Tar); + } + if transport_stream(b) { + return Some(Kind::MpegTs); + } + frame_sync(b) +} + +/// Three transport stream packets in a row, each starting with the sync byte. +fn transport_stream(b: &[u8]) -> bool { + (0..3).all(|n| b.get(n * TS_PACKET).copied() == Some(TS_SYNC)) +} + +/// An MPEG audio frame header: 11 sync bits, then the layer bits tell MP3 (layer III) from AAC +/// in ADTS (layer 00). Bitrate and sampling indexes must not be the reserved ones. +fn frame_sync(b: &[u8]) -> Option { + let [0xff, second, third, ..] = *b else { + return None; + }; + if second & 0xe0 != 0xe0 { + return None; + } + let version = (second >> 3) & 0b11; + let layer = (second >> 1) & 0b11; + match layer { + // ADTS uses only the MPEG-4 (0) and MPEG-2 (1) id bit, so the second byte is 0xFx. + 0b00 if second & 0xf0 == 0xf0 && (third >> 2) & 0x0f < 13 => Some(Kind::Aac), + 0b01 if version != 0b01 && third >> 4 != 0x0f && (third >> 2) & 0b11 != 0b11 => Some(Kind::Mp3), + _ => None, + } +} + +fn be16(b: &[u8], at: usize) -> Option { + Some(u16::from_be_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?)) +} + +fn le16(b: &[u8], at: usize) -> Option { + Some(u16::from_le_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?)) +} + +fn be32(b: &[u8], at: usize) -> Option { + Some(u32::from_be_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} + +fn le32(b: &[u8], at: usize) -> Option { + Some(u32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?)) +} diff --git a/crates/sniff/src/kind.rs b/crates/sniff/src/kind.rs new file mode 100644 index 000000000..da98b15dc --- /dev/null +++ b/crates/sniff/src/kind.rs @@ -0,0 +1,270 @@ +//! The kinds [`identify`](crate::identify) names, and what each one is. + +/// The broad kind of content a file holds. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Family { + /// Still or layered pictures. + Image, + /// Sound with no pictures. + Audio, + /// Moving pictures, with or without sound. Container formats that may hold only sound + /// (Matroska, MP4, Ogg Theora) are named by what their signature promises. + Video, + /// 3D scenes and meshes. + Model, + /// Several files packed together. + Archive, + /// One compressed stream. + Compressed, + /// Machine code or bytecode a runtime loads. + Executable, + /// Structured stores: databases and binary property lists. + Database, + /// Page-description documents. + Document, +} + +/// A file format named by its signature. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Kind { + // Images. + Png, + Jpeg, + Gif, + WebP, + Bmp, + Tiff, + /// Windows icon (`.ico`). + Ico, + /// HEIF still images, HEVC-coded (`.heic`) or generic. + Heif, + Avif, + /// JPEG XL, as a bare codestream or in its ISO BMFF container. + Jxl, + /// Photoshop document or large document (`.psd`, `.psb`). + Psd, + Qoi, + OpenExr, + // Audio. + /// MPEG-1/2 Audio Layer III, with or without an ID3v2 tag. + Mp3, + /// AAC in ADTS framing. + Aac, + Flac, + /// RIFF WAVE. + Wav, + /// AIFF or AIFF-C. + Aiff, + /// Ogg holding anything but Opus or Theora (usually Vorbis). + Ogg, + /// Ogg Opus. + Opus, + /// MPEG-4 audio (`.m4a`, `.m4b`). + M4a, + /// Standard MIDI file. + Midi, + /// AMR narrowband or wideband speech. + Amr, + // Video. + /// MPEG-4 Part 14 and the ISO base media brands it shares. + Mp4, + /// QuickTime movie. + QuickTime, + /// 3GPP or 3GPP2 movie. + ThreeGp, + WebM, + Matroska, + /// Ogg holding Theora video. + OggVideo, + /// RIFF AVI. + Avi, + /// Flash video. + Flv, + /// MPEG program stream. + MpegPs, + /// MPEG transport stream (188-byte packets). + MpegTs, + // 3D models. + /// Binary glTF. + Glb, + /// Binary FBX. ASCII FBX has no signature. + Fbx, + /// PLY, ASCII or binary. + Ply, + // Archives and compressed streams. + Zip, + /// POSIX or GNU tar. + Tar, + /// cpio, binary or ASCII ("newc", CRC and "odc"). + Cpio, + SevenZip, + Rar, + Gzip, + Bzip2, + Xz, + Zstd, + // Executables. + Elf, + /// Portable Executable: a DOS header whose `e_lfanew` reaches a `PE\0\0` signature. + Pe, + /// A DOS `MZ` header with no PE signature in the bytes given (a DOS program, or a PE file + /// whose header lies past them). + DosExecutable, + /// A single-architecture Mach-O file. + MachO, + /// A universal ("fat") Mach-O file. + MachOFat, + /// A Java class file. It shares `CAFEBABE` with universal Mach-O; see [`identify`](crate::identify). + JavaClass, + /// WebAssembly binary module. + Wasm, + /// Dalvik executable. + Dex, + // Databases. + Sqlite, + /// Apple binary property list. + BinaryPlist, + // Documents. + Pdf, +} + +impl Kind { + /// Every kind, in the order [`identify`](crate::identify) documents them. + pub const ALL: [Kind; 56] = [ + Kind::Png, + Kind::Jpeg, + Kind::Gif, + Kind::WebP, + Kind::Bmp, + Kind::Tiff, + Kind::Ico, + Kind::Heif, + Kind::Avif, + Kind::Jxl, + Kind::Psd, + Kind::Qoi, + Kind::OpenExr, + Kind::Mp3, + Kind::Aac, + Kind::Flac, + Kind::Wav, + Kind::Aiff, + Kind::Ogg, + Kind::Opus, + Kind::M4a, + Kind::Midi, + Kind::Amr, + Kind::Mp4, + Kind::QuickTime, + Kind::ThreeGp, + Kind::WebM, + Kind::Matroska, + Kind::OggVideo, + Kind::Avi, + Kind::Flv, + Kind::MpegPs, + Kind::MpegTs, + Kind::Glb, + Kind::Fbx, + Kind::Ply, + Kind::Zip, + Kind::Tar, + Kind::Cpio, + Kind::SevenZip, + Kind::Rar, + Kind::Gzip, + Kind::Bzip2, + Kind::Xz, + Kind::Zstd, + Kind::Elf, + Kind::Pe, + Kind::DosExecutable, + Kind::MachO, + Kind::MachOFat, + Kind::JavaClass, + Kind::Wasm, + Kind::Dex, + Kind::Sqlite, + Kind::BinaryPlist, + Kind::Pdf, + ]; + + /// The media type to send or store with the file: the IANA registration where one exists, + /// otherwise the name in common use (`audio/wav`, `video/x-matroska`, `model/fbx`). + pub fn mime_type(self) -> &'static str { + self.info().0 + } + + /// The usual file extension, lower case, without the leading dot. + pub fn extension(self) -> &'static str { + self.info().1 + } + + /// The broad kind of content. + pub fn family(self) -> Family { + self.info().2 + } + + fn info(self) -> (&'static str, &'static str, Family) { + use Family::{Archive, Audio, Compressed, Database, Document, Executable, Image, Model, Video}; + match self { + Kind::Png => ("image/png", "png", Image), + Kind::Jpeg => ("image/jpeg", "jpg", Image), + Kind::Gif => ("image/gif", "gif", Image), + Kind::WebP => ("image/webp", "webp", Image), + Kind::Bmp => ("image/bmp", "bmp", Image), + Kind::Tiff => ("image/tiff", "tif", Image), + Kind::Ico => ("image/vnd.microsoft.icon", "ico", Image), + Kind::Heif => ("image/heif", "heic", Image), + Kind::Avif => ("image/avif", "avif", Image), + Kind::Jxl => ("image/jxl", "jxl", Image), + Kind::Psd => ("image/vnd.adobe.photoshop", "psd", Image), + Kind::Qoi => ("image/qoi", "qoi", Image), + Kind::OpenExr => ("image/x-exr", "exr", Image), + Kind::Mp3 => ("audio/mpeg", "mp3", Audio), + Kind::Aac => ("audio/aac", "aac", Audio), + Kind::Flac => ("audio/flac", "flac", Audio), + Kind::Wav => ("audio/wav", "wav", Audio), + Kind::Aiff => ("audio/aiff", "aiff", Audio), + Kind::Ogg => ("audio/ogg", "ogg", Audio), + Kind::Opus => ("audio/opus", "opus", Audio), + Kind::M4a => ("audio/mp4", "m4a", Audio), + Kind::Midi => ("audio/midi", "mid", Audio), + Kind::Amr => ("audio/amr", "amr", Audio), + Kind::Mp4 => ("video/mp4", "mp4", Video), + Kind::QuickTime => ("video/quicktime", "mov", Video), + Kind::ThreeGp => ("video/3gpp", "3gp", Video), + Kind::WebM => ("video/webm", "webm", Video), + Kind::Matroska => ("video/x-matroska", "mkv", Video), + Kind::OggVideo => ("video/ogg", "ogv", Video), + Kind::Avi => ("video/x-msvideo", "avi", Video), + Kind::Flv => ("video/x-flv", "flv", Video), + Kind::MpegPs => ("video/mpeg", "mpg", Video), + Kind::MpegTs => ("video/mp2t", "ts", Video), + Kind::Glb => ("model/gltf-binary", "glb", Model), + Kind::Fbx => ("model/fbx", "fbx", Model), + Kind::Ply => ("model/ply", "ply", Model), + Kind::Zip => ("application/zip", "zip", Archive), + Kind::Tar => ("application/x-tar", "tar", Archive), + Kind::Cpio => ("application/x-cpio", "cpio", Archive), + Kind::SevenZip => ("application/x-7z-compressed", "7z", Archive), + Kind::Rar => ("application/vnd.rar", "rar", Archive), + Kind::Gzip => ("application/gzip", "gz", Compressed), + Kind::Bzip2 => ("application/x-bzip2", "bz2", Compressed), + Kind::Xz => ("application/x-xz", "xz", Compressed), + Kind::Zstd => ("application/zstd", "zst", Compressed), + Kind::Elf => ("application/x-elf", "elf", Executable), + Kind::Pe => ("application/vnd.microsoft.portable-executable", "exe", Executable), + Kind::DosExecutable => ("application/x-msdownload", "exe", Executable), + Kind::MachO | Kind::MachOFat => ("application/x-mach-binary", "macho", Executable), + Kind::JavaClass => ("application/java-vm", "class", Executable), + Kind::Wasm => ("application/wasm", "wasm", Executable), + Kind::Dex => ("application/vnd.android.dex", "dex", Executable), + Kind::Sqlite => ("application/vnd.sqlite3", "sqlite", Database), + Kind::BinaryPlist => ("application/x-bplist", "plist", Database), + Kind::Pdf => ("application/pdf", "pdf", Document), + } + } +} diff --git a/crates/sniff/src/lib.rs b/crates/sniff/src/lib.rs new file mode 100644 index 000000000..b95e11591 --- /dev/null +++ b/crates/sniff/src/lib.rs @@ -0,0 +1,32 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![deny(clippy::indexing_slicing)] +#![forbid(unsafe_code)] + +//! What a file is, from its first bytes. +//! +//! [`identify`] reads a file's leading bytes and names its [`Kind`]: the format, with its media +//! type ([`Kind::mime_type`]), usual extension ([`Kind::extension`]) and [`Family`]. It covers the +//! formats apps most often have to tell apart before they open anything: images, audio, video, +//! 3D models, archives and compressed streams, executables and databases. +//! +//! Only signatures are read. Nothing is parsed, validated or decoded, so a file that `identify` +//! names can still be damaged, and a file it doesn't name may still be readable by its own +//! reader. Formats with no signature (plain text, CSV, ASCII FBX, binary STL) are never named. +//! Which kinds an app supports, and what it does when the name and the bytes disagree, stay +//! with the app. +//! +//! [`HEAD`] bytes are enough for every signature; passing more changes nothing, passing fewer +//! only loses the signatures that sit further in (TAR's at byte 257, MPEG transport streams' +//! third packet). Any input is fine: nothing here allocates, panics or reads past the slice. +//! +//! Written from the formats' public specifications and registrations (IANA media types, ISO/IEC +//! 14496-12 brands, the EBML and Matroska specifications, RIFF, the ELF, PE and Mach-O headers). + +mod detect; +mod kind; + +pub use detect::{HEAD, identify}; +pub use kind::{Family, Kind}; + +#[cfg(test)] +mod tests; diff --git a/crates/sniff/src/tests.rs b/crates/sniff/src/tests.rs new file mode 100644 index 000000000..2c5d2da8d --- /dev/null +++ b/crates/sniff/src/tests.rs @@ -0,0 +1,311 @@ +use crate::{Family, HEAD, Kind, identify}; + +/// `prefix` followed by zeros up to `len` bytes. +fn padded(prefix: &[u8], len: usize) -> Vec { + let mut bytes = prefix.to_vec(); + bytes.resize(len.max(prefix.len()), 0); + bytes +} + +/// An ISO BMFF `ftyp` box with the given major and compatible brands. +fn ftyp(major: &[u8; 4], compatible: &[&[u8; 4]]) -> Vec { + let size = u32::try_from(16 + compatible.len() * 4).unwrap(); + let mut bytes = size.to_be_bytes().to_vec(); + bytes.extend_from_slice(b"ftyp"); + bytes.extend_from_slice(major); + bytes.extend_from_slice(&[0, 0, 0, 0]); + for brand in compatible { + bytes.extend_from_slice(*brand); + } + bytes +} + +/// A RIFF or FORM container header of the given form type. +fn riff(chunk: &[u8; 4], form: &[u8; 4]) -> Vec { + let mut bytes = chunk.to_vec(); + bytes.extend_from_slice(&[0x24, 0, 0, 0]); + bytes.extend_from_slice(form); + bytes +} + +/// An Ogg page whose first packet starts with `packet`. +fn ogg(packet: &[u8]) -> Vec { + let mut bytes = b"OggS\0\x02".to_vec(); + bytes.resize(26, 0); + bytes.push(1); + bytes.push(u8::try_from(packet.len()).unwrap()); + bytes.extend_from_slice(packet); + bytes +} + +/// An EBML header naming `doc_type`. +fn ebml(doc_type: &[u8]) -> Vec { + let mut bytes = vec![0x1a, 0x45, 0xdf, 0xa3, 0x9f, 0x42, 0x86, 0x81, 0x01, 0x42, 0x82]; + bytes.push(0x80 | u8::try_from(doc_type.len()).unwrap()); + bytes.extend_from_slice(doc_type); + bytes +} + +#[test] +fn names_fixed_prefixes() { + let cases: &[(&[u8], Kind)] = &[ + (b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR", Kind::Png), + (b"\xff\xd8\xff\xe0\0\x10JFIF", Kind::Jpeg), + (b"GIF89a\x01\0\x01\0", Kind::Gif), + (b"GIF87a", Kind::Gif), + (b"\xff\x0a\xfa", Kind::Jxl), + (b"\x00\x00\x00\x0cJXL \r\n\x87\n", Kind::Jxl), + (b"qoif\0\0\0\x01", Kind::Qoi), + (b"\x76\x2f\x31\x01\x02\0\0\0", Kind::OpenExr), + (b"fLaC\0\0\0\x22", Kind::Flac), + (b"MThd\0\0\0\x06\0\x01", Kind::Midi), + (b"#!AMR\n", Kind::Amr), + (b"#!AMR-WB\n", Kind::Amr), + (b"FLV\x01\x05", Kind::Flv), + (b"\x00\x00\x01\xba\x44", Kind::MpegPs), + (b"glTF\x02\0\0\0", Kind::Glb), + (b"Kaydara FBX Binary \0\x1a\0", Kind::Fbx), + (b"ply\nformat ascii 1.0\n", Kind::Ply), + (b"ply\r\nformat binary_little_endian 1.0\r\n", Kind::Ply), + (b"PK\x03\x04\x14\0", Kind::Zip), + (b"PK\x05\x06\0\0\0\0", Kind::Zip), + (b"7z\xbc\xaf\x27\x1c\0\x04", Kind::SevenZip), + (b"Rar!\x1a\x07\x00", Kind::Rar), + (b"Rar!\x1a\x07\x01\x00", Kind::Rar), + (b"\x1f\x8b\x08\0", Kind::Gzip), + (b"\xfd7zXZ\x00\x00", Kind::Xz), + (b"\x28\xb5\x2f\xfd\x24", Kind::Zstd), + (b"070701000000", Kind::Cpio), + (b"070707000000", Kind::Cpio), + (b"\xc7\x71\0\0", Kind::Cpio), + (b"\x7fELF\x02\x01\x01", Kind::Elf), + (b"\xcf\xfa\xed\xfe\x07\0\0\x01", Kind::MachO), + (b"\xfe\xed\xfa\xce", Kind::MachO), + (b"\xca\xfe\xba\xbf\0\0\0\x02", Kind::MachOFat), + (b"\0asm\x01\0\0\0", Kind::Wasm), + (b"dex\n035\0", Kind::Dex), + (b"SQLite format 3\0\x10\0", Kind::Sqlite), + (b"bplist00\xd1", Kind::BinaryPlist), + (b"%PDF-1.7\n", Kind::Pdf), + (b"ID3\x04\0\0\0\0\0\0", Kind::Mp3), + ]; + for &(bytes, kind) in cases { + assert_eq!(identify(bytes), Some(kind), "{bytes:02x?}"); + } +} + +#[test] +fn java_class_and_universal_mach_o_share_cafebabe() { + assert_eq!(identify(b"\xca\xfe\xba\xbe\0\0\0\x02"), Some(Kind::MachOFat)); + assert_eq!(identify(b"\xca\xfe\xba\xbe\0\0\0\x2c"), Some(Kind::MachOFat)); + // JDK 1.1 (45.3) and Java 21 (65.0). + assert_eq!(identify(b"\xca\xfe\xba\xbe\0\x03\0\x2d"), Some(Kind::JavaClass)); + assert_eq!(identify(b"\xca\xfe\xba\xbe\0\0\0\x41"), Some(Kind::JavaClass)); + assert_eq!(identify(b"\xca\xfe\xba\xbe"), None); +} + +#[test] +fn checks_fields_after_short_prefixes() { + assert_eq!(identify(b"II*\0\x08\0\0\0"), Some(Kind::Tiff)); + assert_eq!(identify(b"MM\0*\0\0\0\x08"), Some(Kind::Tiff)); + assert_eq!(identify(b"II+\0\x08\0\0\0"), Some(Kind::Tiff)); + assert_eq!(identify(b"8BPS\0\x01\0\0"), Some(Kind::Psd)); + assert_eq!(identify(b"8BPS\0\x02\0\0"), Some(Kind::Psd)); + assert_eq!(identify(b"8BPS\0\x03\0\0"), None); + assert_eq!(identify(b"\0\0\x01\0\x01\0\x10\x10\0\0"), Some(Kind::Ico)); + assert_eq!(identify(b"\0\0\x01\0\0\0\x10\x10\0\0"), None); + assert_eq!(identify(b"BZh91AY&SY"), Some(Kind::Bzip2)); + assert_eq!(identify(b"BZh0"), None); +} + +#[test] +fn tells_pe_from_dos_programs() { + let mut pe = padded(b"MZ\x90\0", 0x84); + pe[0x3c] = 0x80; + pe[0x80..0x84].copy_from_slice(b"PE\0\0"); + assert_eq!(identify(&pe), Some(Kind::Pe)); + // The PE header lies past the bytes given, or isn't there at all. + pe[0x3c] = 0xf0; + assert_eq!(identify(&pe), Some(Kind::DosExecutable)); + let mut far = padded(b"MZ", 0x40); + far[0x3c..0x40].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(identify(&far), Some(Kind::DosExecutable)); + assert_eq!(identify(b"MZ"), Some(Kind::DosExecutable)); +} + +#[test] +fn bmp_needs_reserved_zeros_and_a_known_dib_header() { + let mut bmp = padded(b"BM\x36\0\0\0", 54); + bmp[10] = 54; + bmp[14] = 40; + assert_eq!(identify(&bmp), Some(Kind::Bmp)); + bmp[14] = 41; + assert_eq!(identify(&bmp), None); + assert_eq!(identify(b"BMW is a car brand, not a bitmap."), None); +} + +#[test] +fn names_riff_and_form_containers_by_form_type() { + assert_eq!(identify(&riff(b"RIFF", b"WAVE")), Some(Kind::Wav)); + assert_eq!(identify(&riff(b"RIFF", b"WEBP")), Some(Kind::WebP)); + assert_eq!(identify(&riff(b"RIFF", b"AVI ")), Some(Kind::Avi)); + assert_eq!(identify(&riff(b"FORM", b"AIFF")), Some(Kind::Aiff)); + assert_eq!(identify(&riff(b"FORM", b"AIFC")), Some(Kind::Aiff)); + assert_eq!(identify(&riff(b"RIFF", b"CDXA")), None); +} + +#[test] +fn names_ogg_streams_by_first_packet() { + assert_eq!(identify(&ogg(b"OpusHead\x01\x02")), Some(Kind::Opus)); + assert_eq!(identify(&ogg(b"\x80theora\x03\x02")), Some(Kind::OggVideo)); + assert_eq!(identify(&ogg(b"\x01vorbis\0\0")), Some(Kind::Ogg)); + // A page cut off before its segment table is still Ogg. + assert_eq!(identify(&ogg(b"")[..27]), Some(Kind::Ogg)); + assert_eq!(identify(b"OggS\0\x02"), None); +} + +#[test] +fn names_ebml_by_doc_type() { + assert_eq!(identify(&ebml(b"webm")), Some(Kind::WebM)); + assert_eq!(identify(&ebml(b"matroska")), Some(Kind::Matroska)); + // No readable DocType: still Matroska's container. + assert_eq!(identify(&[0x1a, 0x45, 0xdf, 0xa3, 0x42, 0x82, 0x04]), Some(Kind::Matroska)); + assert_eq!(identify(&[0x1a, 0x45, 0xdf, 0xa3, 0x42, 0x82, 0x84, b'w']), Some(Kind::Matroska)); +} + +#[test] +fn names_iso_bmff_by_brand() { + assert_eq!(identify(&ftyp(b"isom", &[b"isom", b"iso2", b"avc1", b"mp41"])), Some(Kind::Mp4)); + assert_eq!(identify(&ftyp(b"mp42", &[])), Some(Kind::Mp4)); + assert_eq!(identify(&ftyp(b"qt ", &[b"qt "])), Some(Kind::QuickTime)); + assert_eq!(identify(&ftyp(b"M4A ", &[b"isom"])), Some(Kind::M4a)); + assert_eq!(identify(&ftyp(b"3gp5", &[b"isom"])), Some(Kind::ThreeGp)); + assert_eq!(identify(&ftyp(b"heic", &[b"mif1"])), Some(Kind::Heif)); + assert_eq!(identify(&ftyp(b"jxl ", &[])), Some(Kind::Jxl)); + // A still image lists generic brands too; the image brand wins wherever it is. + assert_eq!(identify(&ftyp(b"mif1", &[b"avif", b"miaf"])), Some(Kind::Avif)); + assert_eq!(identify(&ftyp(b"isom", &[b"heic"])), Some(Kind::Heif)); + // Unknown brands (a camera's raw container, say) are not guessed at. + assert_eq!(identify(&ftyp(b"crx ", &[b"isom"])), Some(Kind::Mp4)); + assert_eq!(identify(&ftyp(b"crx ", &[])), None); + let mut short = ftyp(b"isom", &[]); + short[3] = 12; + assert_eq!(identify(&short), None); +} + +#[test] +fn names_old_quicktime_atoms() { + assert_eq!(identify(b"\0\0\0\x08wide\0\0\0\0mdat"), Some(Kind::QuickTime)); + assert_eq!(identify(b"\0\0\x10\0moov"), Some(Kind::QuickTime)); + assert_eq!(identify(b"\0\0\0\x04moov"), None); + assert_eq!(identify(b"\0\0\0\x10free"), None); +} + +#[test] +fn names_tar_by_its_ustar_magic() { + let mut tar = padded(b"hello.txt", 512); + tar[257..263].copy_from_slice(b"ustar\0"); + assert_eq!(identify(&tar), Some(Kind::Tar)); + tar[257..263].copy_from_slice(b"ustar "); + assert_eq!(identify(&tar), Some(Kind::Tar)); + tar[257..263].copy_from_slice(b"ustaR "); + assert_eq!(identify(&tar), None); +} + +#[test] +fn transport_streams_need_three_packets() { + let mut ts = vec![0u8; 3 * 188]; + for n in 0..3 { + ts[n * 188] = 0x47; + } + assert_eq!(identify(&ts), Some(Kind::MpegTs)); + assert_eq!(identify(&ts[..377]), Some(Kind::MpegTs)); + assert_eq!(identify(&ts[..376]), None); +} + +#[test] +fn reads_mpeg_audio_frame_headers() { + // MPEG-1, MPEG-2 and MPEG-2.5 layer III. + assert_eq!(identify(b"\xff\xfb\x90\x64"), Some(Kind::Mp3)); + assert_eq!(identify(b"\xff\xf3\x48\xc4"), Some(Kind::Mp3)); + assert_eq!(identify(b"\xff\xe3\x18\xc4"), Some(Kind::Mp3)); + // Reserved version, bad bitrate, reserved sampling rate, layers I and II. + assert_eq!(identify(b"\xff\xeb\x90\x64"), None); + assert_eq!(identify(b"\xff\xfb\xf0\x64"), None); + assert_eq!(identify(b"\xff\xfb\x9c\x64"), None); + assert_eq!(identify(b"\xff\xff\x90\x64"), None); + assert_eq!(identify(b"\xff\xfd\x90\x64"), None); + // ADTS AAC, MPEG-4 and MPEG-2 ids; a reserved sampling index is not AAC. + assert_eq!(identify(b"\xff\xf1\x50\x80"), Some(Kind::Aac)); + assert_eq!(identify(b"\xff\xf9\x4c\x80"), Some(Kind::Aac)); + assert_eq!(identify(b"\xff\xf1\x7c\x80"), None); + assert_eq!(identify(b"\xff\x81\x50"), None); + assert_eq!(identify(b"\xff\xf1"), None); +} + +#[test] +fn names_nothing_without_a_signature() { + for bytes in [&b""[..], b"\0", b"hello, world\n", b"; FBX 7.3.0 project file", b"plywood", b"name,value\n1,2\n", b""] { + assert_eq!(identify(bytes), None, "{bytes:?}"); + } +} + +#[test] +fn reads_only_the_head() { + let mut late = vec![0u8; HEAD + 600]; + late[HEAD + 257..HEAD + 263].copy_from_slice(b"ustar\0"); + assert_eq!(identify(&late), None); + let mut png = b"\x89PNG\r\n\x1a\n".to_vec(); + png.resize(HEAD * 4, 0xff); + assert_eq!(identify(&png), Some(Kind::Png)); +} + +#[test] +fn every_prefix_of_every_sample_is_safe() { + let mut samples = vec![ftyp(b"mif1", &[b"avif"]), ebml(b"webm"), ogg(b"OpusHead"), riff(b"RIFF", b"WAVE")]; + samples.push(padded(b"MZ", 0x100)); + samples.push((0..=255).collect()); + for sample in &samples { + for end in 0..=sample.len() { + let _ = identify(&sample[..end]); + } + } +} + +#[test] +fn every_kind_has_a_type_extension_and_family() { + let mut seen = std::collections::HashSet::new(); + for kind in Kind::ALL { + assert!(seen.insert(kind), "{kind:?} listed twice"); + let mime = kind.mime_type(); + let (top, sub) = mime.split_once('/').unwrap(); + assert!(!top.is_empty() && !sub.is_empty(), "{kind:?}: {mime}"); + assert!(!kind.extension().is_empty() && !kind.extension().starts_with('.')); + assert_eq!(kind.extension(), kind.extension().to_ascii_lowercase()); + let expected = match top { + "image" => Family::Image, + "audio" => Family::Audio, + "video" => Family::Video, + "model" => Family::Model, + _ => kind.family(), + }; + assert_eq!(kind.family(), expected, "{kind:?}"); + } +} + +#[test] +fn media_types_apps_send() { + assert_eq!(Kind::Png.mime_type(), "image/png"); + assert_eq!(Kind::Jpeg.extension(), "jpg"); + assert_eq!(Kind::Wav.mime_type(), "audio/wav"); + assert_eq!(Kind::Flac.mime_type(), "audio/flac"); + assert_eq!(Kind::Glb.mime_type(), "model/gltf-binary"); + assert_eq!(Kind::Fbx.mime_type(), "model/fbx"); + assert_eq!(Kind::Ply.mime_type(), "model/ply"); + assert_eq!(Kind::Mp4.family(), Family::Video); + assert_eq!(Kind::Zip.family(), Family::Archive); + assert_eq!(Kind::Gzip.family(), Family::Compressed); + assert_eq!(Kind::Elf.family(), Family::Executable); + assert_eq!(Kind::Sqlite.family(), Family::Database); + assert_eq!(Kind::Pdf.family(), Family::Document); +} diff --git a/crates/svg/Cargo.toml b/crates/svg/Cargo.toml index a1d1e3437..90bad0e18 100644 --- a/crates/svg/Cargo.toml +++ b/crates/svg/Cargo.toml @@ -13,14 +13,16 @@ workspace = true [dependencies] craft-xml = { workspace = true } quick-xml = { version = "0.41", default-features = false } +# Probing and bounding raster images embedded in a parsed tree (`tree`). craft-image-codecs = { workspace = true, optional = true } # SVGZ (gzip), pure-Rust backend. flate2 = { version = "1", default-features = false, features = ["rust_backend"] } kurbo = { workspace = true } roxmltree = "0.21.2" thiserror = { workspace = true } -vectorcraft-geom = { workspace = true } -vectorcraft-pathops = { workspace = true } +# The geometry reader (`paths`) builds and cleans its outlines with these. +vectorcraft-geom = { workspace = true, optional = true } +vectorcraft-pathops = { workspace = true, optional = 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 } @@ -31,9 +33,14 @@ resvg = { version = "=0.48.1", default-features = false, optional = true } data-url = { version = "0.3.2", optional = true } [features] +default = ["paths"] +# `craft_svg::paths`: the geometry reader (Bézier paths in millimetres) for cutters, plotters and +# CAD import. Off, the crate is SVG text, guards, path data, colours and the writer only, with no +# app crates (vectorcraft) among its dependencies. +paths = ["dep:vectorcraft-geom", "dep:vectorcraft-pathops"] # `craft_svg::tree` without images: usvg behind the bounded entry point, the preflight, the # limits and the draw check. For a caller that parses SVG it trusts no `` in (UI icons). -tree-core = ["dep:usvg"] +tree-core = ["paths", "dep:usvg"] # `craft_svg::tree`: usvg behind the bounded entry point, with the data-URL image resolver. tree = ["tree-core", "dep:data-url", "dep:craft-image-codecs"] # Text converted to paths (needs a font database in the options). diff --git a/crates/svg/src/color.rs b/crates/svg/src/color.rs index 14588bc90..610ca6aa1 100644 --- a/crates/svg/src/color.rs +++ b/crates/svg/src/color.rs @@ -65,12 +65,20 @@ pub struct ColorParseError { /// and SVG 2 paint both defer to): the 148 named colours, `#rgb`, `#rgba`, `#rrggbb`, /// `#rrggbbaa`, `rgb()`/`rgba()` with integer 0–255 or percentage channels, and /// `hsl()`/`hsla()`, in comma or space syntax with an optional alpha (`/ a` or a fourth -/// comma value). Alpha is checked and then dropped. +/// comma value). Alpha is checked and then dropped; [`parse_color_alpha`] keeps it. /// /// Anything else — `transparent`, `currentColor`, `color()`, out-of-range channels — /// is an error, not a guess: silent color loss is worse than a reported failure. /// Callers that understand `none`/`currentColor` (SVG paint) resolve them first. pub fn parse_color(input: &str) -> Result { + parse_color_alpha(input).map(|(color, _)| color) +} + +/// [`parse_color`], with the colour's alpha in `[0, 1]`: from `#rgba` and `#rrggbbaa` (in +/// 255ths), from the fourth value of `rgb()`/`hsl()` (a number or a percentage), else 1. A +/// colour with an alpha of its own (a fill of `rgba(…)`) multiplies with `fill-opacity`; that +/// is the caller's to do. +pub fn parse_color_alpha(input: &str) -> Result<(Color, f32), ColorParseError> { let err = || ColorParseError { input: input.to_string() }; // Cap input length before work: a caller feeding untrusted text cannot // make this quadratic. @@ -84,30 +92,27 @@ pub fn parse_color(input: &str) -> Result { if let Some((name, rest)) = lower.split_once('(') { return parse_function(name.trim(), rest).ok_or_else(err); } - NAMED.iter().find(|(name, _)| *name == lower).map(|(_, rgb)| Color::new((rgb >> 16) as u8, (rgb >> 8) as u8, *rgb as u8)).ok_or_else(err) + NAMED.iter().find(|(name, _)| *name == lower).map(|(_, rgb)| (Color::new((rgb >> 16) as u8, (rgb >> 8) as u8, *rgb as u8), 1.0)).ok_or_else(err) } -/// Hex digits after `#`: 3 or 4 (each doubled), 6 or 8; the alpha digits are dropped. -fn parse_hex(digits: &str) -> Option { +/// Hex digits after `#`: 3 or 4 (each doubled), 6 or 8; the fourth (or fourth pair) is alpha. +fn parse_hex(digits: &str) -> Option<(Color, f32)> { if !digits.bytes().all(|b| b.is_ascii_hexdigit()) { return None; } let value = |s: &str| u8::from_str_radix(s, 16).ok(); - match digits.len() { - 3 | 4 => { - let d = |i: usize| value(digits.get(i..=i)?).map(|v| v * 17); - Some(Color::new(d(0)?, d(1)?, d(2)?)) - } - 6 | 8 => { - let d = |i: usize| value(digits.get(i..i + 2)?); - Some(Color::new(d(0)?, d(2)?, d(4)?)) - } - _ => None, - } + let channels: Vec = match digits.len() { + 3 | 4 => (0..digits.len()).map(|i| value(digits.get(i..=i)?).map(|v| v * 17)).collect::>()?, + 6 | 8 => (0..digits.len() / 2).map(|i| value(digits.get(2 * i..2 * i + 2)?)).collect::>()?, + _ => return None, + }; + let (&r, &g, &b) = (channels.first()?, channels.get(1)?, channels.get(2)?); + let alpha = channels.get(3).map_or(1.0, |a| f32::from(*a) / 255.0); + Some((Color::new(r, g, b), alpha)) } /// `rgb(…)`, `rgba(…)`, `hsl(…)`, `hsla(…)` after the `(`, which must close with `)`. -fn parse_function(name: &str, rest: &str) -> Option { +fn parse_function(name: &str, rest: &str) -> Option<(Color, f32)> { let body = rest.trim().strip_suffix(')')?; // Legacy comma syntax, or modern space syntax with `/ alpha`. let parts: Vec<&str> = if body.contains(',') { @@ -127,26 +132,29 @@ fn parse_function(name: &str, rest: &str) -> Option { if !(parts.len() == 3 || parts.len() == 4) { return None; } - if let Some(alpha) = parts.get(3) { - let a = match alpha.strip_suffix('%') { + let mut alpha = 1.0; + if let Some(text) = parts.get(3) { + let a = match text.strip_suffix('%') { Some(p) => number(p)? / 100.0, - None => number(alpha)?, + None => number(text)?, }; if !(0.0..=1.0).contains(&a) { return None; } + alpha = a as f32; } let (Some(a), Some(b), Some(c)) = (parts.first(), parts.get(1), parts.get(2)) else { return None; }; - match name { - "rgb" | "rgba" => Some(Color::new(rgb_channel(a)?, rgb_channel(b)?, rgb_channel(c)?)), + let color = match name { + "rgb" | "rgba" => Color::new(rgb_channel(a)?, rgb_channel(b)?, rgb_channel(c)?), "hsl" | "hsla" => { let hue = number(a.strip_suffix("deg").unwrap_or(a))?; - Some(hsl_to_rgb(hue.rem_euclid(360.0), percent(b)?, percent(c)?)) + hsl_to_rgb(hue.rem_euclid(360.0), percent(b)?, percent(c)?) } - _ => None, - } + _ => return None, + }; + Some((color, alpha)) } /// A finite plain decimal number. @@ -372,6 +380,24 @@ mod tests { assert_eq!(c("#abc"), Color::new(0xaa, 0xbb, 0xcc)); } + #[test] + fn alpha_is_kept_by_parse_color_alpha() { + let a = |input: &str| parse_color_alpha(input).ok().expect("test input should parse"); + assert_eq!(a("#ff000080"), (Color::new(255, 0, 0), 128.0 / 255.0)); + assert_eq!(a("#f008"), (Color::new(255, 0, 0), 136.0 / 255.0)); + assert_eq!(a("#f00"), (Color::new(255, 0, 0), 1.0)); + assert_eq!(a("rgba(255, 0, 0, 0.25)"), (Color::new(255, 0, 0), 0.25)); + assert_eq!(a("rgb(0 0 255 / 50%)"), (Color::new(0, 0, 255), 0.5)); + assert_eq!(a("hsla(120, 100%, 50%, 0)"), (Color::new(0, 255, 0), 0.0)); + assert_eq!(a("SteelBlue"), (Color::new(0x46, 0x82, 0xb4), 1.0)); + // parse_color keeps dropping it, and both refuse the same input. + assert_eq!(c("#ff000080"), Color::new(255, 0, 0)); + for bad in ["rgba(255, 0, 0, 1.5)", "#ff00000", "rgb(0 0 0 / -1)", "transparent", "#ggg"] { + assert!(parse_color_alpha(bad).is_err(), "{bad}"); + assert!(parse_color(bad).is_err(), "{bad}"); + } + } + #[test] fn parses_css_keywords() { assert_eq!(c("black"), Color::new(0, 0, 0)); diff --git a/crates/svg/src/guard.rs b/crates/svg/src/guard.rs index f3573e443..9601b2a13 100644 --- a/crates/svg/src/guard.rs +++ b/crates/svg/src/guard.rs @@ -21,22 +21,56 @@ const MAX_NESTING: u32 = 16; const MAX_NAME: usize = 256; /// The internal general entities declared in `svg`: name → every value declared for it. Text that -/// only looks like a declaration (in a comment, or in another entity's value) is kept too: the -/// largest value counts, so it can only make the estimate larger. +/// only looks like a declaration in another entity's value is kept too: the largest value counts, +/// so it can only make the estimate larger. Comments and CDATA are skipped, so markers there are +/// not treated as declarations. fn declared(svg: &str) -> HashMap<&str, Vec<&str>> { let mut map: HashMap<&str, Vec<&str>> = HashMap::new(); - for (at, _) in svg.match_indices("") { + Some(n) => at + 1 + 3 + n + 3, + None => svg.len(), + }; continue; } - let Some(end) = rest.find(char::is_whitespace) else { continue }; - let (name, rest) = rest.split_at(end); - let rest = rest.trim_start(); - let Some(quote) = rest.chars().next().filter(|q| matches!(q, '"' | '\'')) else { continue }; - let Some(value) = rest.get(1..).and_then(|r| r.split_once(quote)).map(|(v, _)| v) else { continue }; - map.entry(name).or_default().push(value); + if let Some(body) = after.strip_prefix("![CDATA[") { + i = match body.find("]]>") { + Some(n) => at + 1 + 8 + n + 3, + None => svg.len(), + }; + continue; + } + if let Some(rest) = after.strip_prefix("!ENTITY") { + let rest = rest.trim_start(); + // A parameter entity (`%`) isn't referenced as `&name;`. + if rest.starts_with('%') { + i = at + 1; + continue; + } + // Bound the name scan: a comment full of ` MAX_NAME { + // A name longer than MAX_NAME cannot be referenced, but it must still be + // recorded so `entities_expand_past` can refuse the document. + map.entry(window).or_default().push(""); + } + i = at + 1; + continue; + } + i = at + 1; } map } @@ -99,7 +133,8 @@ pub fn entities_expand_past(svg: &str, most: usize) -> bool { /// 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. +/// only looks like a declaration in another entity's value counts too; comments are skipped. +/// `<` is text, not markup. pub fn entity_holds_markup(svg: &str) -> bool { declared(svg).values().flatten().any(|value| value.contains('<')) } @@ -396,6 +431,16 @@ mod tests { assert!(expansion_too_large(&long_name)); } + #[test] + fn entity_markers_in_a_comment_are_scanned_in_linear_time() { + // 100,000 `"); + assert!(!entities_expand_past(&svg, MAX_ENTITY_EXPANSION)); + } + fn svg(body: &str) -> String { format!(r#"{body}"#) } diff --git a/crates/svg/src/lib.rs b/crates/svg/src/lib.rs index 1acdb7a9a..16538781b 100644 --- a/crates/svg/src/lib.rs +++ b/crates/svg/src/lib.rs @@ -5,8 +5,8 @@ //! UTF-16 and ISO 8859-1 decoded; and SVGZ compression. //! - [`guard`]: checks on the text before any XML reader or SVG converter sees it: how deep the //! elements nest, and what the document type's entities expand to. -//! - [`paths`]: the geometry of a document as Bézier paths in millimetres, for cutters, plotters -//! and CAD import. +//! - `paths` (feature `paths`, on by default): the geometry of a document as Bézier paths in +//! millimetres, for cutters, plotters and CAD import. //! - [`color`]: CSS colours. //! - [`write`](mod@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, @@ -22,6 +22,7 @@ pub mod drawing_write; pub mod guard; pub mod neutral_document; pub mod path_data; +#[cfg(feature = "paths")] pub mod paths; pub mod preflight; pub mod sniff; diff --git a/crates/svg/src/path_data.rs b/crates/svg/src/path_data.rs index 96d0db771..fc31fbaf2 100644 --- a/crates/svg/src/path_data.rs +++ b/crates/svg/src/path_data.rs @@ -214,6 +214,10 @@ pub fn parse_path(d: &str) -> Result> { } cur = start; cmd = 0; + // Z is not a cubic or quadratic, so a following S or T starts from the + // current point rather than reflecting the pre-close control. + prev_cubic = None; + prev_quad = None; continue; } cmd = c; @@ -434,6 +438,15 @@ mod tests { assert_eq!(paths[0].segs[1], Segment::Cubic(Point::new(8.0, -2.0), Point::new(10.0, -2.0), Point::new(12.0, 0.0))); } + #[test] + fn close_resets_smooth_control_history() { + // After Z, S must not reflect the preceding C's control (20,20) → (0,0). + let paths = parse_path("M10 10 C10 20 20 20 20 10 Z S30 0 30 10").unwrap(); + assert_eq!(paths.len(), 2); + assert!(paths[0].closed); + assert_eq!(paths[1].segs[0], Segment::Cubic(Point::new(10.0, 10.0), Point::new(30.0, 0.0), Point::new(30.0, 10.0))); + } + #[test] fn arc_centers_keep_radius_correction_and_sweep() { let (center, delta) = arc_center_circular(Point::new(0.0, 0.0), Point::new(10.0, 0.0), 2.0, false, true).unwrap(); diff --git a/crates/svg/src/tree.rs b/crates/svg/src/tree.rs index 4e2e74516..f2e1a7963 100644 --- a/crates/svg/src/tree.rs +++ b/crates/svg/src/tree.rs @@ -38,6 +38,7 @@ use crate::{guard, svgz}; pub mod document; pub mod draw; +pub mod primitive; /// What one import may use. The defaults suit a desktop app that parses on a thread with a /// large stack (see the module text). diff --git a/crates/svg/src/tree/document.rs b/crates/svg/src/tree/document.rs index f77d5a8c0..838f62e98 100644 --- a/crates/svg/src/tree/document.rs +++ b/crates/svg/src/tree/document.rs @@ -89,6 +89,14 @@ pub struct Path { pub source_path: NodePath, } +impl Path { + /// The basic shape (rectangle, rounded rectangle or ellipse) this path draws, if any + /// ([`super::primitive::recognize`]). + pub fn primitive(&self) -> Option { + super::primitive::recognize(&self.segments) + } +} + /// A path segment in CSS pixels after its absolute transform. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Segment { diff --git a/crates/svg/src/tree/primitive.rs b/crates/svg/src/tree/primitive.rs new file mode 100644 index 000000000..a43933ec3 --- /dev/null +++ b/crates/svg/src/tree/primitive.rs @@ -0,0 +1,164 @@ +//! Basic shapes recovered from a scene path. usvg turns `rect`, `circle` and `ellipse` into +//! paths; an app that edits them as shapes (a rectangle with corner radii, an ellipse) gets them +//! back here from the segments alone. +//! +//! A path is a primitive when it is one closed contour whose points all lie on the outline of +//! an axis-aligned (rounded) rectangle or ellipse and that encloses its area. The check works on +//! the segments as given, so use it on a [`Coordinates::Local`](super::document::Coordinates) +//! scene, where a rotated or skewed shape keeps its own axes. + +use super::document::Segment; + +/// An axis-aligned basic shape. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Primitive { + /// A rectangle, with elliptical corners of radii `rx`, `ry` (both 0 for square corners). + Rect { x: f64, y: f64, width: f64, height: f64, rx: f64, ry: f64 }, + /// An ellipse (a circle when `rx == ry`). + Ellipse { cx: f64, cy: f64, rx: f64, ry: f64 }, +} + +/// Most segments a primitive's contour has: usvg writes a rounded rectangle as 4 lines and up to +/// 4 curves per corner. +const MAX_SEGMENTS: usize = 32; +/// Points sampled on each curve, besides its ends: enough that the polygon through them +/// encloses a curved outline's area to well within the area check's 1%. +const SAMPLES: usize = 31; + +/// The curve parameters sampled between a curve's ends. +fn samples() -> impl Iterator { + (1..=SAMPLES).map(|i| i as f64 / (SAMPLES + 1) as f64) +} + +/// The basic shape `segments` draw, if they draw one. +pub fn recognize(segments: &[Segment]) -> Option { + let (ends, points) = contour(segments)?; + let (mut min, mut max) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]); + for p in &points { + for axis in 0..2 { + min[axis] = min[axis].min(p[axis]); + max[axis] = max[axis].max(p[axis]); + } + } + let (width, height) = (max[0] - min[0], max[1] - min[1]); + if !(width.is_finite() && height.is_finite()) || width <= 0.0 || height <= 0.0 { + return None; + } + let tol = 1e-3 * width.max(height); + // The straight part of the top edge starts `rx` in; of the left edge, `ry` down. + let rx = ends.iter().filter(|p| (p[1] - min[1]).abs() <= tol).map(|p| p[0] - min[0]).fold(f64::INFINITY, f64::min); + let ry = ends.iter().filter(|p| (p[0] - min[0]).abs() <= tol).map(|p| p[1] - min[1]).fold(f64::INFINITY, f64::min); + let rx = snap(rx, width / 2.0, tol)?; + let ry = snap(ry, height / 2.0, tol)?; + if (rx == 0.0) != (ry == 0.0) { + return None; + } + let inner = ([min[0] + rx, min[1] + ry], [max[0] - rx, max[1] - ry]); + let on_outline = |p: &[f64; 2]| { + let q = [p[0].clamp(inner.0[0], inner.1[0]), p[1].clamp(inner.0[1], inner.1[1])]; + let d = [p[0] - q[0], p[1] - q[1]]; + if rx == 0.0 { + // Square corners: on an edge of the box. + (p[0] - min[0]).abs() <= tol || (p[0] - max[0]).abs() <= tol || (p[1] - min[1]).abs() <= tol || (p[1] - max[1]).abs() <= tol + } else if d == [0.0, 0.0] { + false + } else { + let e = (d[0] / rx).hypot(d[1] / ry); + (e - 1.0).abs() * rx.min(ry) <= tol || (d[0] == 0.0 && (d[1].abs() - ry).abs() <= tol) || (d[1] == 0.0 && (d[0].abs() - rx).abs() <= tol) + } + }; + if !points.iter().all(on_outline) { + return None; + } + // Every point is on the outline; it also has to go all the way round once. + let expected = width * height - (4.0 - std::f64::consts::PI) * rx * ry; + if (area(&points).abs() - expected).abs() > 0.01 * expected { + return None; + } + if (rx - width / 2.0).abs() <= tol && (ry - height / 2.0).abs() <= tol { + return Some(Primitive::Ellipse { cx: min[0] + width / 2.0, cy: min[1] + height / 2.0, rx: width / 2.0, ry: height / 2.0 }); + } + Some(Primitive::Rect { x: min[0], y: min[1], width, height, rx, ry }) +} + +/// `value` within `tol` of 0 or `half` is snapped there; past `half` it isn't a corner radius. +fn snap(value: f64, half: f64, tol: f64) -> Option { + if !value.is_finite() || value < -tol || value > half + tol { + return None; + } + Some(if value <= tol { + 0.0 + } else if (value - half).abs() <= tol { + half + } else { + value + }) +} + +/// Points of a contour. +type Points = Vec<[f64; 2]>; + +/// The segment ends of one closed contour, and all its points (ends and curve samples), or +/// `None` for anything else: several contours, an open one, or too many segments. +fn contour(segments: &[Segment]) -> Option<(Points, Points)> { + if segments.len() > MAX_SEGMENTS + 2 { + return None; + } + let (first, rest) = segments.split_first()?; + let Segment::MoveTo(start) = *first else { return None }; + let mut points = vec![start]; + let mut ends = vec![start]; + let mut at = start; + let mut closed = false; + for segment in rest { + if closed { + return None; + } + match *segment { + Segment::MoveTo(_) => return None, + Segment::LineTo(p) => { + points.push(p); + ends.push(p); + at = p; + } + Segment::QuadTo(c, p) => { + for t in samples() { + let u = 1.0 - t; + points.push([u * u * at[0] + 2.0 * u * t * c[0] + t * t * p[0], u * u * at[1] + 2.0 * u * t * c[1] + t * t * p[1]]); + } + points.push(p); + ends.push(p); + at = p; + } + Segment::CubicTo(c1, c2, p) => { + for t in samples() { + let u = 1.0 - t; + let (a, b, c, d) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t); + points.push([a * at[0] + b * c1[0] + c * c2[0] + d * p[0], a * at[1] + b * c1[1] + c * c2[1] + d * p[1]]); + } + points.push(p); + ends.push(p); + at = p; + } + Segment::Close => closed = true, + } + } + let ends_at_start = (at[0] - start[0]).abs() < 1e-9 && (at[1] - start[1]).abs() < 1e-9; + (closed || ends_at_start).then_some((ends, points)) +} + +/// The signed area of the polygon through `points`. +fn area(points: &[[f64; 2]]) -> f64 { + let n = points.len(); + (0..n) + .map(|i| { + let (a, b) = (points[i], points[(i + 1) % n]); + a[0] * b[1] - b[0] * a[1] + }) + .sum::() + / 2.0 +} + +#[cfg(test)] +#[path = "primitive_tests.rs"] +mod tests; diff --git a/crates/svg/src/tree/primitive_tests.rs b/crates/svg/src/tree/primitive_tests.rs new file mode 100644 index 000000000..81f4c0979 --- /dev/null +++ b/crates/svg/src/tree/primitive_tests.rs @@ -0,0 +1,116 @@ +#![allow(clippy::unwrap_used, clippy::panic, clippy::indexing_slicing, clippy::float_cmp)] + +use super::*; +use crate::tree::document::{Document, Node}; +use crate::tree::{Limits, parse, usvg}; + +/// The segments usvg makes of `element`, the only shape in a document. +fn segments(element: &str) -> Vec { + let svg = format!(r#"{element}"#); + let tree = parse(svg.as_bytes(), &Limits::default(), &usvg::Options::default()).unwrap(); + let document = Document::from_tree(&tree, 16); + fn first(nodes: &[Node]) -> Option> { + nodes.iter().find_map(|n| match n { + Node::Path(p) => Some(p.segments.clone()), + Node::Group { children, .. } => first(children), + Node::Image(_) => None, + }) + } + first(&document.nodes).unwrap() +} + +fn near(a: f64, b: f64) -> bool { + (a - b).abs() < 1e-3 +} + +#[test] +fn a_rect_is_a_rect() { + let Some(Primitive::Rect { x, y, width, height, rx, ry }) = recognize(&segments(r#""#)) else { + panic!() + }; + assert_eq!((x, y, width, height, rx, ry), (10.0, 20.0, 30.0, 40.0, 0.0, 0.0)); +} + +#[test] +fn a_rounded_rect_keeps_its_radii() { + for (attrs, want) in [(r#"rx="5""#, (5.0, 5.0)), (r#"rx="6" ry="3""#, (6.0, 3.0)), (r#"ry="4""#, (4.0, 4.0))] { + let element = format!(r#""#); + let Some(Primitive::Rect { x, y, width, height, rx, ry }) = recognize(&segments(&element)) else { panic!("{element}") }; + assert!(near(x, 10.0) && near(y, 20.0) && near(width, 30.0) && near(height, 40.0), "{element}"); + assert!(near(rx, want.0) && near(ry, want.1), "{element}: {rx} {ry}"); + } +} + +#[test] +fn circles_and_ellipses_are_ellipses() { + for (element, want) in [ + (r#""#, (50.0, 60.0, 25.0, 25.0)), + (r#""#, (50.0, 60.0, 40.0, 10.0)), + // A rectangle whose corners meet in the middle is an ellipse too. + (r#""#, (10.0, 5.0, 10.0, 5.0)), + ] { + let Some(Primitive::Ellipse { cx, cy, rx, ry }) = recognize(&segments(element)) else { panic!("{element}") }; + assert!(near(cx, want.0) && near(cy, want.1) && near(rx, want.2) && near(ry, want.3), "{element}: {cx} {cy} {rx} {ry}"); + } +} + +#[test] +fn other_paths_are_not_primitives() { + for element in [ + // A triangle on the edges of its box encloses half of it. + r#""#, + // Open, or two contours. + r#""#, + r#""#, + // A rectangle drawn twice, a diamond, a star. + r#""#, + r#""#, + r#""#, + // A curve that bulges past a circle's outline. + r#""#, + ] { + assert_eq!(recognize(&segments(element)), None, "{element}"); + } +} + +#[test] +fn degenerate_and_oversized_input_is_refused() { + assert_eq!(recognize(&[]), None); + assert_eq!(recognize(&[Segment::LineTo([1.0, 1.0])]), None); + assert_eq!(recognize(&[Segment::MoveTo([0.0, 0.0]), Segment::LineTo([10.0, 0.0]), Segment::Close]), None, "no height"); + let nan = [Segment::MoveTo([f64::NAN, 0.0]), Segment::LineTo([1.0, 0.0]), Segment::LineTo([1.0, 1.0]), Segment::Close]; + assert_eq!(recognize(&nan), None); + let mut long = vec![Segment::MoveTo([0.0, 0.0])]; + long.extend((0..100).map(|i| Segment::LineTo([f64::from(i % 2) * 10.0, 0.0]))); + long.push(Segment::Close); + assert_eq!(recognize(&long), None); +} + +#[test] +fn hand_written_segments_are_recognised_too() { + // Square corners written clockwise from the bottom left, closed by returning to the start. + let square = [ + Segment::MoveTo([0.0, 10.0]), + Segment::LineTo([0.0, 0.0]), + Segment::LineTo([10.0, 0.0]), + Segment::LineTo([10.0, 10.0]), + Segment::LineTo([0.0, 10.0]), + ]; + assert_eq!(recognize(&square), Some(Primitive::Rect { x: 0.0, y: 0.0, width: 10.0, height: 10.0, rx: 0.0, ry: 0.0 })); + // A circle from four cubic quarters, through Path::primitive. + let k = 0.552_284_749_830_793_6 * 5.0; + let path = crate::tree::document::Path { + segments: vec![ + Segment::MoveTo([10.0, 5.0]), + Segment::CubicTo([10.0, 5.0 + k], [5.0 + k, 10.0], [5.0, 10.0]), + Segment::CubicTo([5.0 - k, 10.0], [0.0, 5.0 + k], [0.0, 5.0]), + Segment::CubicTo([0.0, 5.0 - k], [5.0 - k, 0.0], [5.0, 0.0]), + Segment::CubicTo([5.0 + k, 0.0], [10.0, 5.0 - k], [10.0, 5.0]), + Segment::Close, + ], + fill: None, + stroke: None, + source_path: crate::tree::document::NodePath::default(), + }; + assert_eq!(path.primitive(), Some(Primitive::Ellipse { cx: 5.0, cy: 5.0, rx: 5.0, ry: 5.0 })); +} diff --git a/crates/svg/src/write.rs b/crates/svg/src/write.rs index 66cb1d6a0..658454ab2 100644 --- a/crates/svg/src/write.rs +++ b/crates/svg/src/write.rs @@ -269,6 +269,38 @@ impl SvgWriter { pub fn fragment() -> SvgWriter { SvgWriter::default() } + /// A document that starts with an XML declaration naming `encoding` (for example `UTF-8` or + /// `ISO-8859-1`) and a newline. Nothing is written if `encoding` is not an XML name. + pub fn with_encoding(encoding: &str) -> SvgWriter { + let mut w = SvgWriter::fragment(); + w.decl(encoding, "\n"); + w + } + /// Writes `` followed by `newline`. Nothing is + /// written if `encoding` is not an XML name. + pub fn decl(&mut self, encoding: &str, newline: &str) { + if craft_xml::is_name(encoding) { + self.0.raw(&format!("{newline}")); + } + } + /// Writes `` + `newline`. When + /// `internal` is empty, the inner newline is omitted, so a minified document can be + /// `` with no extra breaks. Nothing is written if `name` is not an XML name + /// or `internal` contains `]]>`. + pub fn doctype_internal(&mut self, name: &str, internal: &str, newline: &str) { + if !craft_xml::is_name(name) || internal.contains("]]>") { + return; + } + let mut out = format!(""); + out.push_str(newline); + self.0.raw(&out); + } /// Starts the root tag, ` Tag<'_> { @@ -295,9 +327,13 @@ impl SvgWriter { pub fn raw(&mut self, svg: &str) { self.0.raw(svg); } + /// What has been written so far, including any element that is still open. + pub fn as_str(&self) -> &str { + self.0.as_str() + } /// How many bytes have been written so far. pub fn len(&self) -> usize { - self.0.as_str().len() + self.as_str().len() } /// Has nothing been written yet? pub fn is_empty(&self) -> bool { @@ -362,6 +398,7 @@ mod tests { } #[test] + #[cfg(feature = "paths")] fn writes_a_document_the_reader_reads() { let mut w = SvgWriter::new(); assert!(!w.is_empty()); @@ -412,4 +449,37 @@ mod tests { assert_eq!(w.len(), before + inner.len()); assert_eq!(w.finish(), ""); } + + #[test] + fn declaration_names_the_encoding_and_rejects_a_bad_name() { + assert_eq!(SvgWriter::with_encoding("UTF-8").finish(), "\n"); + assert_eq!(SvgWriter::with_encoding("ISO-8859-1").finish(), "\n"); + assert!(SvgWriter::with_encoding("UTF-8\"").is_empty()); + assert!(SvgWriter::with_encoding("").is_empty()); + let mut compact = SvgWriter::fragment(); + compact.decl("ISO-8859-1", ""); + compact.root().open(); + assert_eq!(compact.as_str(), ""); + compact.close(); + assert_eq!(compact.finish(), ""); + } + + #[test] + fn doctype_internal_subset_keeps_caller_newlines() { + let mut pretty = SvgWriter::fragment(); + pretty.doctype_internal("svg", " ", "\n"); + pretty.root().open(); + pretty.close(); + assert_eq!(pretty.finish(), "\n]>\n"); + let mut compact = SvgWriter::fragment(); + compact.doctype_internal("svg", "", ""); + assert_eq!(compact.finish(), "]>"); + let mut empty = SvgWriter::fragment(); + empty.doctype_internal("svg", "", "\n"); + assert_eq!(empty.finish(), "\n"); + let mut refused = SvgWriter::fragment(); + refused.doctype_internal("svg", "]]>", "\n"); + refused.doctype_internal("not a name", "", "\n"); + assert!(refused.is_empty()); + } } diff --git a/crates/tar/Cargo.toml b/crates/tar/Cargo.toml new file mode 100644 index 000000000..e0628a35a --- /dev/null +++ b/crates/tar/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "craft-tar" +description = "TAR (v7, ustar, GNU long names and base-256 sizes, pax overrides) and cpio (newc, crc, odc) member walkers for hostile input: list members and where their data lies, without extracting. From Craft Anything." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +# `serde`: `Serialize` on the listing types, for apps that report listings as JSON. +serde = { workspace = true, optional = true } + +[features] +serde = ["dep:serde"] diff --git a/crates/tar/src/lib.rs b/crates/tar/src/lib.rs new file mode 100644 index 000000000..7712fc2aa --- /dev/null +++ b/crates/tar/src/lib.rs @@ -0,0 +1,911 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +#![forbid(unsafe_code)] + +//! Member walkers for Unix archives: tar (v7, ustar, GNU long names and base-256 sizes, pax path +//! local/global overrides) and cpio (newc, crc and portable odc). They list members and where their data +//! lies; nothing is extracted here, and no input can make them panic or loop forever. + +use std::collections::BTreeSet; + +/// Bounds-checked reads from the archive bytes. +struct Reader<'a> { + data: &'a [u8], +} + +impl<'a> Reader<'a> { + fn new(data: &'a [u8]) -> Self { + Reader { data } + } + + /// `len` bytes at `off`, or `None` when they don't all lie inside the data. + fn slice(&self, off: usize, len: usize) -> Option<&'a [u8]> { + self.data.get(off..off.checked_add(len)?) + } +} + +/// What a member is. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] +pub enum MemberKind { + File, + Dir, + Symlink, + Hardlink, + Device, + Fifo, + Other, +} + +/// One archive member. +#[derive(Debug, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +pub struct Member { + pub name: String, + pub kind: MemberKind, + pub size: u64, + /// Where the member's data starts, relative to the archive start. + pub data_offset: u64, + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] + pub link_target: Option, + pub mode: u32, +} + +/// Why tar name metadata stopped a walk that still retains earlier members. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] +pub enum StopReason { + GnuMetadataTooLong, + GnuMetadataTruncated, + GlobalNameBudget, +} + +/// The members of an archive. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +pub struct Listing { + /// `tar`, `cpio-newc`, `cpio-crc` or `cpio-odc`. + pub format: &'static str, + pub members: Vec, + /// Bytes from the archive start to the end of its last structure (end marker included). + pub length: u64, + /// True when the end-of-archive marker was reached. + pub complete: bool, + /// True when `max_members` stopped the walk. + pub truncated: bool, + /// Name metadata that prevented reading the remaining member identities. + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))] + pub stop_reason: Option, + pub warnings: Vec, +} + +const BLOCK: usize = 512; +const MAX_NAME: usize = 4096; +const MAX_GLOBAL_NAME_COPIES: usize = 64 << 20; + +fn trim_field(f: &[u8]) -> &[u8] { + let end = f.iter().position(|&b| b == 0).unwrap_or(f.len()); + f.get(..end).unwrap_or(&[]) +} + +fn text(f: &[u8]) -> String { + String::from_utf8_lossy(trim_field(f)).into_owned() +} + +/// An octal number field (spaces and NULs around it are ignored); GNU base-256 when the high bit +/// of the first byte is set. +fn octal(f: &[u8]) -> Option { + if let Some((&first, rest)) = f.split_first() + && first & 0x80 != 0 + { + if first & 0x40 != 0 { + return None; // This field holds an unsigned value. + } + let mut v = u64::from(first & 0x7f); + for &b in rest { + v = v.checked_mul(256)?.checked_add(u64::from(b))?; + } + return Some(v); + } + let padding = |&b: &u8| b == 0 || b == b' '; + let mut digits = f.iter().copied().skip_while(padding).peekable(); + let mut v = 0u64; + while let Some(b) = digits.next_if(|b| !padding(b)) { + if !(b'0'..=b'7').contains(&b) { + return None; + } + v = v.checked_mul(8)?.checked_add(u64::from(b - b'0'))?; + } + digits.all(|b| padding(&b)).then_some(v) +} + +/// True when a 512-byte block is a tar header with a correct checksum. +pub fn tar_header_ok(h: &[u8]) -> bool { + let Some(h) = h.get(..BLOCK) else { return false }; + let Some(stored) = h.get(148..156).and_then(octal) else { return false }; + let mut unsigned = 0u64; + let mut signed = 0i64; + for (i, &b) in h.iter().enumerate() { + let v = if (148..156).contains(&i) { b' ' } else { b }; + unsigned += u64::from(v); + signed += i64::from(v as i8); + } + // An all-zero block sums to 256 (the blank checksum field) and is the end marker, not a header. + unsigned != 256 && (stored == unsigned || i64::try_from(stored).is_ok_and(|s| s == signed)) +} + +fn round_up(v: u64, to: u64) -> Option { + let r = v % to; + if r == 0 { Some(v) } else { v.checked_add(to - r) } +} + +/// Walk a tar archive. `None` when the first block is not a valid header. +pub fn tar(data: &[u8], max_members: usize) -> Option { + tar_with_name_copy_limit(data, max_members, MAX_GLOBAL_NAME_COPIES) +} + +fn tar_with_name_copy_limit(data: &[u8], max_members: usize, mut name_bytes_left: usize) -> Option { + let r = Reader::new(data); + if !tar_header_ok(r.slice(0, BLOCK)?) { + return None; + } + let mut out = Listing { format: "tar", ..Listing::default() }; + let mut pos = 0usize; + let mut long_name: Option = None; + let mut long_link: Option = None; + let mut pax = PaxOverrides::default(); + // Every iteration advances by at least one block, so the walk is bounded by the input size. + 'headers: loop { + let Some(h) = r.slice(pos, BLOCK) else { + out.warnings.push(format!("archive ends inside a header at {pos:#x}")); + out.length = data.len() as u64; + break; + }; + if h.iter().all(|&b| b == 0) { + // End of archive: one or two zero blocks. + let second = r.slice(pos + BLOCK, BLOCK).is_some_and(|b| b.iter().all(|&x| x == 0)); + out.length = (pos + if second { 2 * BLOCK } else { BLOCK }) as u64; + out.complete = true; + break; + } + if !tar_header_ok(h) { + out.warnings.push(format!("invalid header checksum at {pos:#x}; stopping")); + out.length = pos as u64; + break; + } + let typeflag = h.get(156).copied().unwrap_or(0); + let Some(mut size) = h.get(124..136).and_then(octal) else { + out.warnings.push(format!("unreadable size at {pos:#x}")); + out.length = pos as u64; + break; + }; + if !matches!(typeflag, b'x' | b'g' | b'L' | b'K') + && let Some(s) = pax.size.take().or(pax.global_size) + { + size = s; + } + let data_off = pos + BLOCK; + let Some(next) = + round_up(size, BLOCK as u64).and_then(|s| u64::try_from(data_off).ok()?.checked_add(s)).and_then(|n| usize::try_from(n).ok()) + else { + if matches!(typeflag, b'L' | b'K') { + out.stop_reason = Some(StopReason::GnuMetadataTruncated); + } + out.warnings.push(format!("size overflows at {pos:#x}")); + out.length = pos as u64; + break; + }; + let body = r.slice(data_off, usize::try_from(size).unwrap_or(usize::MAX)); + match typeflag { + b'L' | b'K' => { + let Some(body) = body.filter(|_| next <= data.len()) else { + out.stop_reason = Some(StopReason::GnuMetadataTruncated); + out.warnings.push(format!("GNU long-name record ends early at {pos:#x}")); + out.length = data.len() as u64; + break; + }; + let end = body.iter().take(MAX_NAME + 1).position(|&byte| byte == 0).unwrap_or(body.len()); + if end > MAX_NAME { + out.stop_reason = Some(StopReason::GnuMetadataTooLong); + out.warnings.push(format!("GNU long name or link exceeds {MAX_NAME} bytes at {pos:#x}")); + out.length = pos as u64; + break; + } + let value = Some(String::from_utf8_lossy(&body[..end]).into_owned()); + if typeflag == b'L' { + long_name = value; + } else { + long_link = value; + } + } + b'x' | b'g' => { + if let Some(records) = body + && let Err(warning) = pax.apply(records, typeflag == b'g', pos) + { + out.warnings.push(warning); + out.length = pos as u64; + break 'headers; + } + } + _ => { + if out.members.len() >= max_members { + out.truncated = true; + out.length = pos as u64; + break; + } + let linked = matches!(typeflag, b'1' | b'2'); + let copied_path = if pax.path.is_none() { pax.global_path.as_ref().map_or(0, String::len) } else { 0 }; + let copied_link = if linked && pax.link.is_none() { pax.global_link.as_ref().map_or(0, String::len) } else { 0 }; + let Some(remaining) = name_bytes_left.checked_sub(copied_path.saturating_add(copied_link)) else { + out.stop_reason = Some(StopReason::GlobalNameBudget); + out.warnings.push("the retained global pax name-byte budget was reached; later members were omitted".into()); + out.length = pos as u64; + break; + }; + name_bytes_left = remaining; + // Old GNU headers use these bytes for timestamps and sparse-file metadata. + let ustar = h.get(257..263) == Some(b"ustar\0"); + let base = text(h.get(0..100).unwrap_or(&[])); + let prefix = if ustar { text(h.get(345..500).unwrap_or(&[])) } else { String::new() }; + let joined = if prefix.is_empty() { base } else { format!("{prefix}/{base}") }; + // PAX values override header names, including GNU's long-name replacement. + // Consume the one-member GNU records even when a PAX value takes precedence. + let gnu_name = long_name.take(); + let gnu_link = long_link.take(); + let name = pax.path.take().or_else(|| pax.global_path.clone()).or(gnu_name).unwrap_or(joined); + let link = pax + .link + .take() + .or_else(|| if linked { pax.global_link.clone() } else { None }) + .or(gnu_link) + .or_else(|| Some(text(h.get(157..257).unwrap_or(&[])))) + .filter(|l| !l.is_empty()); + let mut kind = tar_kind(typeflag, &name); + if std::mem::take(&mut pax.sparse) || !pax.global_sparse.is_empty() { + kind = MemberKind::Other; + out.warnings.push(format!("{name}: GNU sparse reconstruction is unsupported; stored extents are not the complete file")); + } + let mode = h.get(100..108).and_then(octal).and_then(|m| u32::try_from(m).ok()).unwrap_or(0); + if body.is_none() && kind == MemberKind::File { + out.warnings.push(format!("{name}: data runs past the end of the input")); + } + out.members.push(Member { + name, + kind, + size, + data_offset: data_off as u64, + link_target: if matches!(kind, MemberKind::Symlink | MemberKind::Hardlink) { link } else { None }, + mode, + }); + } + } + if next > data.len() { + out.length = data.len() as u64; + out.warnings.push("archive ends early (no end-of-archive marker)".into()); + break; + } + pos = next; + } + Some(out) +} + +/// What a tar header's typeflag says the member is. +fn tar_kind(typeflag: u8, name: &str) -> MemberKind { + match typeflag { + b'0' | 0 | b'7' => { + if name.ends_with('/') { + MemberKind::Dir + } else { + MemberKind::File + } + } + b'1' => MemberKind::Hardlink, + b'2' => MemberKind::Symlink, + b'3' | b'4' => MemberKind::Device, + b'5' => MemberKind::Dir, + b'6' => MemberKind::Fifo, + _ => MemberKind::Other, + } +} + +/// The pax values that override the next member (local) or every later one (global). +#[derive(Default)] +struct PaxOverrides<'a> { + path: Option, + link: Option, + size: Option, + sparse: bool, + global_path: Option, + global_link: Option, + global_size: Option, + /// Borrow keys from the input: global sparse attributes persist until their own deletion. + global_sparse: BTreeSet<&'a [u8]>, +} + +impl<'a> PaxOverrides<'a> { + /// Apply one extended header's records. `Err` is the warning that stops the walk. + fn apply(&mut self, mut remaining: &'a [u8], global: bool, pos: usize) -> Result<(), String> { + for _ in 0..256 { + if remaining.is_empty() { + break; + } + let Some((key, value, rest)) = pax_record(remaining) else { + return Err(format!("invalid pax record at {pos:#x}")); + }; + if key.starts_with(b"GNU.sparse.") { + if !global { + self.sparse = true; + } else if value.is_empty() { + self.global_sparse.remove(key); + } else { + self.global_sparse.insert(key); + } + } + match key { + b"path" | b"linkpath" | b"GNU.sparse.name" => self.set_name(key, value, global, pos)?, + b"size" => self.set_size(value, global, pos)?, + _ => {} + } + remaining = rest; + } + if remaining.is_empty() { Ok(()) } else { Err(format!("pax record limit reached at {pos:#x}")) } + } + + fn set_name(&mut self, key: &[u8], value: &[u8], global: bool, pos: usize) -> Result<(), String> { + if value.len() > MAX_NAME { + return Err(format!("pax name exceeds {MAX_NAME} bytes at {pos:#x}")); + } + let slot = match (global, key == b"linkpath") { + (true, true) => &mut self.global_link, + (true, false) => &mut self.global_path, + (false, true) => &mut self.link, + (false, false) => &mut self.path, + }; + *slot = if global && value.is_empty() { None } else { Some(String::from_utf8_lossy(value).into_owned()) }; + Ok(()) + } + + fn set_size(&mut self, value: &[u8], global: bool, pos: usize) -> Result<(), String> { + let size = std::str::from_utf8(value).ok().and_then(|s| s.parse().ok()); + if !value.is_empty() && (size.is_none() || !value.iter().all(u8::is_ascii_digit)) { + let scope = if global { "global" } else { "local" }; + return Err(format!("invalid {scope} pax size at {pos:#x}")); + } + if global { + self.global_size = size; + } else { + self.size = size; + } + Ok(()) + } +} + +/// Read one length-prefixed pax record, borrowing even unknown keys and values from the header. +fn pax_record(data: &[u8]) -> Option<(&[u8], &[u8], &[u8])> { + let sp = data.iter().position(|&byte| byte == b' ')?; + let digits = data.get(..sp)?; + if !digits.iter().all(u8::is_ascii_digit) { + return None; + } + let len: usize = std::str::from_utf8(digits).ok()?.parse().ok()?; + let field = data.get(sp + 1..len)?.strip_suffix(b"\n")?; + let eq = field.iter().position(|&byte| byte == b'=')?; + if eq == 0 { + return None; + } + Some((field.get(..eq)?, field.get(eq + 1..)?, data.get(len..)?)) +} + +fn hex_field(f: &[u8]) -> Option { + if !f.iter().all(u8::is_ascii_hexdigit) { + return None; + } + let s = std::str::from_utf8(f).ok()?; + u64::from_str_radix(s, 16).ok() +} + +fn oct_field(f: &[u8]) -> Option { + if !f.iter().all(|b| (b'0'..=b'7').contains(b)) { + return None; + } + let s = std::str::from_utf8(f).ok()?; + u64::from_str_radix(s, 8).ok() +} + +fn mode_kind(mode: u32) -> MemberKind { + match mode & 0o170_000 { + 0o100_000 => MemberKind::File, + 0o040_000 => MemberKind::Dir, + 0o120_000 => MemberKind::Symlink, + 0o020_000 | 0o060_000 => MemberKind::Device, + 0o010_000 => MemberKind::Fifo, + _ => MemberKind::Other, + } +} + +/// Walk a cpio archive (newc `070701`, crc `070702`, odc `070707`). `None` when the first header +/// is not one of these. +pub fn cpio(data: &[u8], max_members: usize) -> Option { + cpio_with_checksum(data, max_members, &mut |body| CpioChecksum::Value(body.iter().fold(0u32, |sum, &byte| sum.wrapping_add(u32::from(byte))))) +} + +/// A checksum sum, or the reason a caller stopped verification before completing it. +#[derive(Clone, Copy)] +pub enum CpioChecksum { + Value(u32), + Skipped, + Cancelled, +} + +/// Let a scanner share its checksum work allowance and cancellation across overlapping probes. +pub fn cpio_with_checksum(data: &[u8], max_members: usize, checksum: &mut dyn FnMut(&[u8]) -> CpioChecksum) -> Option { + let r = Reader::new(data); + let format = match r.slice(0, 6)? { + b"070701" => "cpio-newc", + b"070702" => "cpio-crc", + b"070707" => "cpio-odc", + _ => return None, + }; + let odc = format == "cpio-odc"; + let mut hard_links = std::collections::HashMap::<(u64, u64, u64), Vec>::new(); + let mut out = Listing { format, ..Listing::default() }; + let mut pos = 0usize; + let align = |v: usize| if odc { Some(v) } else { v.checked_add(3).map(|x| x & !3) }; + loop { + let hdr_len = if odc { 76 } else { 110 }; + let Some(h) = r.slice(pos, hdr_len) else { + out.warnings.push(format!("archive ends inside a header at {pos:#x}")); + out.length = data.len() as u64; + break; + }; + if h.get(..6) != r.slice(0, 6) { + out.warnings.push(format!("bad header magic at {pos:#x}; stopping")); + out.length = pos as u64; + break; + } + let fields = if odc { + (h.get(18..24).and_then(oct_field), h.get(59..65).and_then(oct_field), h.get(65..76).and_then(oct_field)) + } else { + (h.get(14..22).and_then(hex_field), h.get(94..102).and_then(hex_field), h.get(54..62).and_then(hex_field)) + }; + let (Some(mode), Some(namesize), Some(filesize)) = fields else { + out.warnings.push(format!("unreadable header fields at {pos:#x}")); + out.length = pos as u64; + break; + }; + let namesize = usize::try_from(namesize).unwrap_or(usize::MAX); + if namesize == 0 || namesize > MAX_NAME { + out.warnings.push(format!("implausible name size at {pos:#x}")); + out.length = pos as u64; + break; + } + let Some(name_bytes) = r.slice(pos + hdr_len, namesize) else { + out.warnings.push(format!("name runs past the end at {pos:#x}")); + out.length = data.len() as u64; + break; + }; + // newc permits extra trailing NULs in namesize for alignment. + let Some(end) = name_bytes.iter().position(|&b| b == 0) else { + out.warnings.push(format!("unterminated member name at {pos:#x}")); + out.length = pos as u64; + break; + }; + if end == 0 || name_bytes.get(end..).is_some_and(|pad| pad.iter().any(|&b| b != 0)) { + out.warnings.push(format!("invalid member name at {pos:#x}")); + out.length = pos as u64; + break; + } + let name = text(name_bytes); + let Some(data_off) = (pos + hdr_len).checked_add(namesize).and_then(align) else { break }; + let size = usize::try_from(filesize).unwrap_or(usize::MAX); + let Some(next) = data_off.checked_add(size).and_then(align) else { + out.warnings.push(format!("size overflows at {pos:#x}")); + out.length = pos as u64; + break; + }; + if name == "TRAILER!!!" { + out.length = next.min(data.len()) as u64; + if size == 0 && next <= data.len() { + out.complete = true; + } else { + out.warnings.push(format!("invalid or truncated trailer at {pos:#x}")); + } + break; + } + let mode = u32::try_from(mode).unwrap_or(0); + let kind = mode_kind(mode); + let link_target = if kind == MemberKind::Symlink { r.slice(data_off, size.min(MAX_NAME)).map(text) } else { None }; + if out.members.len() >= max_members { + out.truncated = true; + out.length = pos as u64; + break; + } + if format == "cpio-crc" { + match h.get(102..110).filter(|field| field.iter().all(u8::is_ascii_hexdigit)).and_then(hex_field).and_then(|n| u32::try_from(n).ok()) { + Some(expected) => { + if let Some(body) = r.slice(data_off, size) { + match checksum(body) { + CpioChecksum::Value(actual) => { + if actual != expected { + out.warnings.push(format!("{name}: data checksum mismatch (stored {expected:08x}, computed {actual:08x})")); + } + } + CpioChecksum::Skipped => out.warnings.push(format!("{name}: data checksum not verified (scan byte budget exhausted)")), + CpioChecksum::Cancelled => return None, + } + } + } + None => out.warnings.push(format!("{name}: unreadable data checksum")), + } + } + // newc stores hard-linked files by device and inode. GNU cpio may put the + // payload on the last name, so identify aliases after scanning the group. + if !odc + && kind == MemberKind::File + && h.get(38..46).and_then(hex_field).is_some_and(|n| n > 1) + && let (Some(ino), Some(major), Some(minor)) = + (h.get(6..14).and_then(hex_field), h.get(62..70).and_then(hex_field), h.get(70..78).and_then(hex_field)) + { + hard_links.entry((major, minor, ino)).or_default().push(out.members.len()); + } + out.members.push(Member { name, kind, size: filesize, data_offset: data_off as u64, link_target, mode }); + if next > data.len() { + out.warnings.push("archive ends early (no trailer)".into()); + out.length = data.len() as u64; + break; + } + pos = next; + } + for group in hard_links.values().filter(|group| group.len() > 1) { + let source = group.iter().rev().copied().find(|&i| out.members.get(i).is_some_and(|m| m.size > 0)).or_else(|| group.first().copied()); + let Some(source) = source else { continue }; + let Some(name) = out.members.get(source).map(|m| m.name.clone()) else { continue }; + for &index in group { + if index != source + && let Some(alias) = out.members.get_mut(index) + && alias.size == 0 + { + alias.kind = MemberKind::Hardlink; + alias.link_target = Some(name.clone()); + } + } + } + Some(out) +} + +/// The data of a member, when it lies inside `data`. +pub fn member_data<'a>(data: &'a [u8], m: &Member) -> Option<&'a [u8]> { + Reader::new(data).slice(usize::try_from(m.data_offset).ok()?, usize::try_from(m.size).ok()?) +} + +#[cfg(test)] +mod tar_tests; +#[cfg(test)] +mod testkit; + +#[cfg(test)] +mod tests { + use super::*; + use crate::testkit as fx; + + #[test] + fn plus_sign_in_cpio_fields_is_rejected() { + assert_eq!(hex_field(b"+000000A"), None); + assert_eq!(oct_field(b"+00012"), None); + assert_eq!(hex_field(b"0000000A"), Some(10)); + assert_eq!(oct_field(b"00012"), Some(10)); + } + + fn global_record(key: &str, value: &str) -> String { + let tail = format!(" {key}={value}\n"); + let mut len = tail.len() + 1; + loop { + let record = format!("{len}{tail}"); + if record.len() == len { + return record; + } + len = record.len(); + } + } + + #[test] + fn global_name_copies_stop_before_spending_the_budget() { + let global = global_record("path", "shared"); + let data = + fx::tar_entries(&[("global", b'g', global.as_bytes(), ""), ("one", b'0', b"", ""), ("two", b'0', b"", ""), ("three", b'0', b"", "")]); + let listing = tar_with_name_copy_limit(&data, 10, 12).unwrap(); + assert_eq!(listing.members.len(), 2); + assert!(listing.members.iter().all(|m| m.name == "shared")); + assert_eq!(listing.stop_reason, Some(StopReason::GlobalNameBudget)); + assert!(!listing.complete && !listing.truncated); + assert!(listing.warnings.iter().any(|w| w.contains("name-byte budget"))); + assert_eq!(listing.length, 2048); + assert!(tar_with_name_copy_limit(&data, 10, 18).unwrap().complete); + } + + #[test] + fn local_names_and_unused_global_links_do_not_spend_copy_bytes() { + let global = global_record("path", "name") + &global_record("linkpath", "unused-link"); + let local = global_record("path", "local-name"); + let data = fx::tar_entries(&[ + ("global", b'g', global.as_bytes(), ""), + ("local", b'x', local.as_bytes(), ""), + ("one", b'0', b"", ""), + ("two", b'0', b"", ""), + ]); + let listing = tar_with_name_copy_limit(&data, 10, 4).unwrap(); + assert!(listing.complete); + assert_eq!(listing.members.iter().map(|m| m.name.as_str()).collect::>(), ["local-name", "name"]); + assert!(listing.members.iter().all(|m| m.link_target.is_none())); + } + + #[test] + fn global_names_and_links_share_the_decoded_byte_budget() { + let global = global_record("path", "é") + &global_record("linkpath", "中"); + let data = fx::tar_entries(&[("global", b'g', global.as_bytes(), ""), ("one", b'2', b"", "fallback"), ("two", b'1', b"", "fallback")]); + let listing = tar_with_name_copy_limit(&data, 10, 5).unwrap(); + assert_eq!(listing.members.len(), 1); + assert_eq!(listing.members[0].link_target.as_deref(), Some("中")); + assert_eq!(listing.stop_reason, Some(StopReason::GlobalNameBudget)); + assert!(tar_with_name_copy_limit(&data, 10, 10).unwrap().complete); + } + + #[test] + fn oversized_gnu_names_do_not_alias_their_truncated_prefix() { + for kind in *b"LK" { + let mut long = vec![b'n'; MAX_NAME + 1]; + long.push(0); + let data = fx::tar_entries(&[ + ("before", b'0', b"kept", ""), + ("././@LongLink", kind, &long, ""), + ("short", if kind == b'K' { b'2' } else { b'0' }, b"", "fallback"), + ]); + let listing = tar(&data, 10).unwrap(); + assert!(!listing.complete); + assert_eq!(listing.stop_reason, Some(StopReason::GnuMetadataTooLong)); + assert_eq!(listing.members.len(), 1); + assert_eq!(listing.members[0].name, "before"); + assert_eq!(member_data(&data, &listing.members[0]).unwrap(), b"kept"); + assert!(listing.warnings.iter().any(|warning| warning.contains("GNU long name or link exceeds"))); + } + } + + #[test] + fn gnu_long_names_keep_exact_limits_and_short_terminated_values() { + for kind in *b"LK" { + for terminated in [false, true] { + let mut long = vec![b'n'; MAX_NAME]; + if terminated { + long.push(0); + } + let data = fx::tar_entries(&[("././@LongLink", kind, &long, ""), ("short", if kind == b'K' { b'2' } else { b'0' }, b"", "fallback")]); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete, "{:?}", listing.warnings); + let member = &listing.members[0]; + let name = if kind == b'L' { member.name.as_str() } else { member.link_target.as_deref().unwrap() }; + assert_eq!(name, "n".repeat(MAX_NAME)); + } + } + let mut padded = b"short\0".to_vec(); + padded.resize(MAX_NAME + 100, 0); + let data = fx::tar_entries(&[("././@LongLink", b'L', &padded, ""), ("fallback", b'0', b"payload", "")]); + assert_eq!(tar(&data, 10).unwrap().members[0].name, "short"); + } + + fn sparse_pax_record(key: &str, value: &str) -> Vec { + let field = format!(" {key}={value}\n"); + let mut length = field.len() + 1; + loop { + let actual = field.len() + length.to_string().len(); + if actual == length { + return format!("{length}{field}").into_bytes(); + } + length = actual; + } + } + + #[test] + fn gnu_sparse_pax_members_are_not_regular_file_bytes() { + for fields in [ + vec![("GNU.sparse.size", "100"), ("GNU.sparse.numblocks", "1"), ("GNU.sparse.offset", "50"), ("GNU.sparse.numbytes", "3")], + vec![("GNU.sparse.size", "100"), ("GNU.sparse.map", "50,3"), ("GNU.sparse.name", "original.bin")], + vec![("GNU.sparse.major", "1"), ("GNU.sparse.minor", "0"), ("GNU.sparse.realsize", "100"), ("GNU.sparse.name", "original.bin")], + ] { + let metadata: Vec = fields.iter().flat_map(|(key, value)| sparse_pax_record(key, value)).collect(); + let version_one = fields.iter().any(|(key, _)| *key == "GNU.sparse.major"); + let mut sparse_body = if version_one { b"1\n50\n3\n".to_vec() } else { Vec::new() }; + if version_one { + sparse_body.resize(512, 0); + } + sparse_body.extend_from_slice(b"abc"); + let data = + fx::tar_entries(&[("metadata", b'x', &metadata, ""), ("stored-name", b'0', &sparse_body, ""), ("normal", b'0', b"ordinary", "")]); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete, "{:?}", listing.warnings); + assert_eq!(listing.members.len(), 2); + assert_eq!(listing.members[0].kind, MemberKind::Other); + let named = fields.iter().any(|(key, _)| *key == "GNU.sparse.name"); + assert_eq!(listing.members[0].name, if named { "original.bin" } else { "stored-name" }); + assert_eq!(listing.members[1].kind, MemberKind::File); + assert_eq!(member_data(&data, &listing.members[1]).unwrap(), b"ordinary"); + assert_eq!(listing.warnings.len(), 1); + assert!(listing.warnings[0].contains("sparse reconstruction is unsupported")); + } + } + + #[test] + fn sparse_metadata_survives_other_extension_headers() { + let sparse = sparse_pax_record("GNU.sparse.map", "0,3"); + let path = sparse_pax_record("path", "renamed"); + let data = fx::tar_entries(&[ + ("sparse", b'x', &sparse, ""), + ("name", b'L', b"long-name\0", ""), + ("path", b'x', &path, ""), + ("header", b'0', b"abc", ""), + ("next", b'0', b"ok", ""), + ]); + let listing = tar(&data, 10).unwrap(); + assert_eq!(listing.members[0].name, "renamed"); + assert_eq!(listing.members[0].kind, MemberKind::Other); + assert_eq!(listing.members[1].kind, MemberKind::File); + } + + #[test] + fn gnu_metadata_stop_reasons_keep_partial_listings_and_marker_compatibility() { + for kind in *b"LK" { + let mut data = + fx::tar_entries(&[("before", b'0', b"kept", ""), ("././@LongLink", kind, b"long\0", ""), ("fallback", b'0', b"later", "")]); + data.truncate(3 * BLOCK + 2); + let listing = tar(&data, 10).unwrap(); + assert_eq!(listing.stop_reason, Some(StopReason::GnuMetadataTruncated)); + assert!(!listing.complete); + assert!(!listing.truncated); + assert_eq!(listing.members.len(), 1); + assert_eq!(member_data(&data, &listing.members[0]).unwrap(), b"kept"); + } + let mut data = fx::tar_entries(&[("file", b'0', b"kept", "")]); + data.truncate(2 * BLOCK); + let listing = tar(&data, 10).unwrap(); + assert!(!listing.complete); + assert_eq!(listing.stop_reason, None); + assert_eq!(member_data(&data, &listing.members[0]).unwrap(), b"kept"); + let cpio_listing = cpio(&fx::cpio_newc(&[("file", b"kept", 0o100644)]), 10).unwrap(); + assert_eq!(cpio_listing.stop_reason, None); + } + + #[test] + fn gnu_metadata_requires_its_complete_padded_record() { + let body = b"next\0"; + for kind in *b"LK" { + let data = fx::tar_entries(&[ + ("before", b'0', b"kept", ""), + ("././@LongLink", kind, body, ""), + ("fallback", if kind == b'L' { b'0' } else { b'2' }, b"", "fallback"), + ]); + for padding in [0, 1, BLOCK - body.len() - 1] { + let short = &data[..3 * BLOCK + body.len() + padding]; + let listing = tar(short, 10).unwrap(); + assert_eq!(listing.stop_reason, Some(StopReason::GnuMetadataTruncated)); + assert!(!listing.complete); + assert_eq!(listing.members.len(), 1); + assert_eq!(member_data(short, &listing.members[0]).unwrap(), b"kept"); + } + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete); + assert_eq!(listing.stop_reason, None); + assert_eq!(listing.members.len(), 2); + } + } + + #[test] + fn walks_tar() { + let t = fx::tar(&[("etc/passwd", b"root:x:0:0\n"), ("bin/", b""), ("long/".repeat(30).as_str(), b"deep")]); + let l = tar(&t, 100).unwrap(); + assert!(l.complete, "{:?}", l.warnings); + assert_eq!(l.members.len(), 3); + assert_eq!(l.members[0].name, "etc/passwd"); + assert_eq!(member_data(&t, &l.members[0]).unwrap(), b"root:x:0:0\n"); + assert_eq!(l.members[1].kind, MemberKind::Dir); + assert!(l.members[2].name.starts_with("long/long/")); + assert_eq!(l.length, t.len() as u64); + let l2 = tar(&t, 1).unwrap(); + assert!(l2.truncated); + assert_eq!(l2.members.len(), 1); + } + + #[test] + fn cpio_names_and_trailers_must_be_complete() { + let bytes = fx::cpio_newc(&[("abc", b"x", 0o100644)]); + let trailer = bytes.windows(6).rposition(|b| b == b"070701").unwrap(); + for (offset, byte) in [(113, b'x'), (111, 0)] { + let mut damaged = bytes.clone(); + damaged[offset] = byte; + let listed = cpio(&damaged, 10).unwrap(); + assert!(!listed.complete); + assert!(listed.members.is_empty()); + } + let mut padded_name = bytes.clone(); + padded_name[112] = 0; + assert_eq!(cpio(&padded_name, 10).unwrap().members[0].name, "ab"); + let mut with_payload = bytes.clone(); + with_payload[trailer + 61] = b'1'; + assert!(!cpio(&with_payload, 10).unwrap().complete); + assert!(!cpio(&bytes[..bytes.len() - 1], 10).unwrap().complete); + } + + #[test] + fn cpio_hard_link_aliases_do_not_appear_as_empty_files() { + for content_first in [true, false] { + let data = if content_first { [b"abc".as_slice(), b""] } else { [b"".as_slice(), b"abc"] }; + let mut archive = fx::cpio_newc(&[("one", data[0], 0o100644), ("two", data[1], 0o100644), ("separate", b"", 0o100644)]); + let starts: Vec<_> = archive.windows(6).enumerate().filter(|(_, bytes)| *bytes == b"070701").map(|(i, _)| i).collect(); + for &start in &starts[..3] { + archive[start + 6..start + 14].copy_from_slice(b"00000015"); + archive[start + 38..start + 46].copy_from_slice(b"00000002"); + } + archive[starts[2] + 70..starts[2] + 78].copy_from_slice(b"00000001"); + let (file, alias) = if content_first { (0, 1) } else { (1, 0) }; + for crc in [false, true] { + if crc { + for &start in &starts { + archive[start..start + 6].copy_from_slice(b"070702"); + } + archive[starts[file] + 102..starts[file] + 110].copy_from_slice(b"00000126"); + } + let listing = cpio(&archive, 10).unwrap(); + assert_eq!(listing.members[file].kind, MemberKind::File); + assert_eq!(member_data(&archive, &listing.members[file]), Some(b"abc".as_slice())); + assert_eq!(listing.members[alias].kind, MemberKind::Hardlink); + assert_eq!(listing.members[alias].link_target.as_deref(), Some(listing.members[file].name.as_str())); + assert_eq!(listing.members[2].kind, MemberKind::File); + assert!(listing.warnings.is_empty()); + } + } + } + + #[test] + fn walks_cpio() { + let c = fx::cpio_newc(&[("init", b"#!/bin/sh\n", 0o100755), ("dev", b"", 0o040755), ("sh", b"busybox", 0o120777)]); + let l = cpio(&c, 100).unwrap(); + assert!(l.complete); + assert_eq!(l.members.len(), 3); + assert_eq!(member_data(&c, &l.members[0]).unwrap(), b"#!/bin/sh\n"); + assert_eq!(l.members[2].kind, MemberKind::Symlink); + assert_eq!(l.members[2].link_target.as_deref(), Some("busybox")); + assert_eq!(l.length, c.len() as u64); + } + + #[test] + fn octal_fields() { + assert_eq!(octal(b"0000644 \0"), Some(0o644)); + assert_eq!(octal(b" \0"), Some(0)); + assert_eq!(octal(b"9"), None); + assert_eq!(octal(&[0x80, 0, 0, 1, 0]), Some(256)); + assert_eq!(octal(&[0xff; 8]), None); + assert_eq!(octal(&[0xff; 12]), None); + } + + #[test] + fn hostile_archives_do_not_panic() { + let t = fx::tar(&[("a", b"hello"), ("b/c", b"world")]); + let c = fx::cpio_newc(&[("a", b"hello", 0o100644)]); + for s in [&t, &c] { + for m in fx::mutations(s) { + let _ = tar(&m, 10); + let _ = cpio(&m, 10); + } + for n in 0..s.len().min(800) { + let _ = tar(&s[..n], 10); + let _ = cpio(&s[..n], 10); + } + } + for seed in 0..50 { + let n = fx::noise(seed, 2048); + let _ = tar(&n, 10); + let _ = cpio(&n, 10); + } + assert!(pax_record(b"12 path=abc\n").is_some()); + assert!(pax_record(b"9999999999999999999999 x").is_none()); + } +} diff --git a/crates/tar/src/tar_tests.rs b/crates/tar/src/tar_tests.rs new file mode 100644 index 000000000..38f72abd9 --- /dev/null +++ b/crates/tar/src/tar_tests.rs @@ -0,0 +1,404 @@ +// ---- from Craft Anything crates/formats/tests/tar_gnu_names.rs +mod tar_gnu_names { + use crate::testkit as fx; + use crate::{member_data, tar}; + + fn checksum(header: &mut [u8]) { + header[148..156].fill(b' '); + let sum: u32 = header.iter().map(|&b| u32::from(b)).sum(); + header[148..156].copy_from_slice(format!("{sum:06o}\0 ").as_bytes()); + } + + #[test] + fn old_gnu_timestamps_are_not_posix_path_prefixes() { + let mut archive = fx::tar(&[("program", b"payload")]); + let header = &mut archive[..512]; + header[257..265].copy_from_slice(b"ustar \0"); + header[345..357].copy_from_slice(b"00000000123\0"); + header[357..369].copy_from_slice(b"00000000456\0"); + checksum(header); + let listing = tar(&archive, 10).unwrap(); + assert!(listing.complete); + assert_eq!(listing.members[0].name, "program"); + assert_eq!(member_data(&archive, &listing.members[0]), Some(b"payload".as_slice())); + } + + #[test] + fn posix_ustar_prefixes_are_still_joined() { + let mut archive = fx::tar(&[("program", b"payload")]); + let header = &mut archive[..512]; + header[345..352].copy_from_slice(b"usr/bin"); + checksum(header); + assert_eq!(tar(&archive, 10).unwrap().members[0].name, "usr/bin/program"); + } +} + +// ---- from Craft Anything crates/formats/tests/tar_local_pax_size.rs +mod tar_local_pax_size { + use crate::testkit::tar_entries; + use crate::{member_data, tar}; + + /// Encode the decimal byte count that includes the PAX record itself. + fn record(value: &str) -> String { + let body = format!(" size={value}\n"); + let mut size = body.len() + 1; + loop { + let result = format!("{size}{body}"); + if result.len() == size { + return result; + } + size = result.len(); + } + } + + /// Invalid local sizes cannot silently fall back to a plausible member header. + #[test] + fn malformed_local_sizes_stop_before_the_affected_member() { + for value in ["no", "-1", "+3", " 3", "3 ", "3.0", "18446744073709551616"] { + let pax = record(value); + let bytes = tar_entries(&[("keep", b'0', b"ok", ""), ("PaxHeader", b'x', pax.as_bytes(), ""), ("affected", b'0', b"abc", "")]); + let report = tar(&bytes, 10).unwrap(); + assert!(!report.complete, "size={value}"); + assert_eq!(report.members.len(), 1); + assert_eq!(report.members[0].name, "keep"); + assert_eq!(member_data(&bytes, &report.members[0]), Some(b"ok".as_slice())); + assert!(report.warnings.iter().any(|w| w.contains("pax size"))); + } + } + + /// Decimal sizes, zero, and the existing empty override behavior still work. + #[test] + fn valid_local_sizes_remain_readable() { + for (size, body) in [("3", b"abc".as_slice()), ("0003", b"abc"), ("0", b""), ("", b"abc")] { + let pax = record(size); + let bytes = tar_entries(&[("PaxHeader", b'x', pax.as_bytes(), ""), ("file", b'0', body, "")]); + let report = tar(&bytes, 10).unwrap(); + assert!(report.complete, "{:?}", report.warnings); + assert_eq!(member_data(&bytes, &report.members[0]), Some(body)); + } + } +} + +// ---- from Craft Anything crates/formats/tests/tar_pax_size.rs +mod tar_pax_size { + use crate::testkit as fx; + use crate::{member_data, tar}; + + fn clear_member_size(archive: &mut [u8], offset: usize) { + let header = &mut archive[offset..offset + 512]; + header[124..136].copy_from_slice(b"00000000000\0"); + header[148..156].fill(b' '); + let sum: u32 = header.iter().map(|&b| u32::from(b)).sum(); + header[148..156].copy_from_slice(format!("{sum:06o}\0 ").as_bytes()); + } + + #[test] + fn pax_size_survives_a_gnu_long_name_record() { + let mut archive = fx::tar_entries(&[ + ("PaxHeader", b'x', b"10 size=3\n", ""), + ("././@LongLink", b'L', b"renamed\0", ""), + ("short", b'0', b"abc", ""), + ("next", b'0', b"later", ""), + ]); + clear_member_size(&mut archive, 2048); + let listing = tar(&archive, 10).unwrap(); + assert!(listing.complete, "{:?}", listing.warnings); + assert_eq!(listing.members.len(), 2); + assert_eq!(listing.members[0].name, "renamed"); + assert_eq!(listing.members[0].size, 3); + assert_eq!(member_data(&archive, &listing.members[0]), Some(b"abc".as_slice())); + assert_eq!(listing.members[1].size, 5); + assert_eq!(member_data(&archive, &listing.members[1]), Some(b"later".as_slice())); + } + + #[test] + fn pax_size_without_intervening_metadata_still_applies_once() { + let mut archive = fx::tar_entries(&[("PaxHeader", b'x', b"10 size=3\n", ""), ("short", b'0', b"abc", ""), ("next", b'0', b"later", "")]); + clear_member_size(&mut archive, 1024); + let listing = tar(&archive, 10).unwrap(); + assert!(listing.complete); + assert_eq!(listing.members.iter().map(|m| m.size).collect::>(), vec![3, 5]); + } + + fn pax_record(key: &str, value: &str) -> String { + let content = format!(" {key}={value}\n"); + let mut len = content.len() + 1; + loop { + let record = format!("{len}{content}"); + if record.len() == len { + return record; + } + len = record.len(); + } + } + + #[test] + fn malformed_pax_framing_stops_the_listing() { + for invalid in [b"10 size=3!".as_slice(), b"10 size=3!\n", b"20 size=3\n", b"+12 path=no\n", b"5 =x\n", b"12 path=a=b\n11 path=bad"] { + let archive = fx::tar_entries(&[("keep", b'0', b"one", ""), ("PaxHeader", b'x', invalid, ""), ("after", b'0', b"two", "")]); + let listing = tar(&archive, 10).unwrap(); + assert!(!listing.complete); + assert_eq!(listing.members.len(), 1); + assert_eq!(listing.members[0].name, "keep"); + assert!(listing.warnings.iter().any(|s| s.contains("pax"))); + } + } + + #[test] + fn pax_path_can_contain_an_equals_sign() { + let archive = fx::tar_entries(&[("PaxHeader", b'x', b"12 path=a=b\n", ""), ("short", b'0', b"ok", "")]); + let listing = tar(&archive, 10).unwrap(); + assert!(listing.complete); + assert_eq!(listing.members[0].name, "a=b"); + } + + #[test] + fn the_name_limit_applies_only_to_pax_fields_kept_as_names() { + let exact = "a".repeat(4096); + let large = "b".repeat(100_000); + let pax = pax_record("comment", &large) + &pax_record("path", &exact); + let archive = fx::tar_entries(&[("PaxHeader", b'x', pax.as_bytes(), ""), ("short", b'0', b"ok", "")]); + let listing = tar(&archive, 10).unwrap(); + assert!(listing.complete); + assert_eq!(listing.members[0].name, exact); + for key in ["path", "linkpath"] { + let pax = pax_record(key, &large); + let archive = fx::tar_entries(&[("PaxHeader", b'x', pax.as_bytes(), ""), ("short", b'2', b"", "link")]); + let listing = tar(&archive, 10).unwrap(); + assert!(!listing.complete); + assert!(listing.members.is_empty()); + assert!(listing.warnings.iter().any(|s| s.contains("4096"))); + } + } + + #[test] + fn empty_pax_values_remove_an_earlier_override() { + let pax = pax_record("path", "other") + &pax_record("path", "") + &pax_record("linkpath", "first") + &pax_record("linkpath", ""); + let archive = fx::tar_entries(&[("PaxHeader", b'x', pax.as_bytes(), ""), ("original", b'2', b"", "target")]); + let listing = tar(&archive, 10).unwrap(); + assert!(listing.complete); + assert!(listing.members[0].name.is_empty()); + assert!(listing.members[0].link_target.is_none()); + } + + #[test] + fn tar_sizes_allow_outer_padding_but_do_not_join_separated_digits() { + for (field, valid) in [(b" 000000001\0 ", true), (b" 0000 0001\0 ", false), (b" 0000\x000001\0 ", false)] { + let mut archive = fx::tar_entries(&[("one", b'0', b"1", ""), ("two", b'0', b"2", "")]); + let header = &mut archive[..512]; + header[124..136].copy_from_slice(field); + header[148..156].fill(b' '); + let sum: u32 = header.iter().map(|&b| u32::from(b)).sum(); + header[148..156].copy_from_slice(format!("{sum:06o}\0 ").as_bytes()); + let listing = tar(&archive, 10).unwrap(); + assert_eq!(listing.complete, valid); + assert_eq!(listing.members.len(), if valid { 2 } else { 0 }); + } + } + + #[test] + fn excess_pax_records_do_not_hide_a_later_override() { + let mut records = pax_record("comment", "").repeat(256); + for excess in [false, true] { + if excess { + records.push_str(&pax_record("size", "9999")); + } + let archive = fx::tar_entries(&[("PaxHeader", b'x', records.as_bytes(), ""), ("member", b'0', b"ok", "")]); + let listing = tar(&archive, 10).unwrap(); + assert_eq!(listing.complete, !excess); + assert_eq!(listing.members.len(), usize::from(!excess)); + } + } +} + +// ---- from Craft Anything crates/formats/tests/tar_global_pax.rs +mod tar_global_pax { + use crate::testkit as fx; + use crate::{MemberKind, member_data, tar}; + + fn record(key: &str, value: &str) -> String { + let tail = format!(" {key}={value}\n"); + let mut len = tail.len() + 1; + loop { + let record = format!("{len}{tail}"); + if record.len() == len { + return record; + } + len = record.len(); + } + } + + fn clear_size(data: &mut [u8], at: usize) { + let header = &mut data[at..at + 512]; + header[124..136].copy_from_slice(b"00000000000\0"); + header[148..156].fill(b' '); + let sum: u32 = header.iter().map(|&b| u32::from(b)).sum(); + header[148..156].copy_from_slice(format!("{sum:06o}\0 ").as_bytes()); + } + + #[test] + fn global_path_and_size_persist_across_members_and_metadata() { + let global = record("path", "global-name") + &record("size", "3"); + let local = record("comment", "unrelated"); + let mut data = fx::tar_entries(&[ + ("global", b'g', global.as_bytes(), ""), + ("first", b'0', b"abc", ""), + ("metadata", b'x', local.as_bytes(), ""), + ("second", b'0', b"def", ""), + ]); + clear_size(&mut data, 1024); + clear_size(&mut data, 3072); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete, "{:?}", listing.warnings); + assert_eq!(listing.members.len(), 2); + assert!(listing.members.iter().all(|member| member.name == "global-name" && member.size == 3)); + assert_eq!(member_data(&data, &listing.members[0]), Some(b"abc".as_slice())); + assert_eq!(member_data(&data, &listing.members[1]), Some(b"def".as_slice())); + } + + #[test] + fn local_attributes_override_only_one_member() { + let global = record("path", "inherited") + &record("size", "3"); + let local = record("path", "one-member") + &record("size", "0"); + let data = fx::tar_entries(&[ + ("global", b'g', global.as_bytes(), ""), + ("local", b'x', local.as_bytes(), ""), + ("first", b'0', b"", ""), + ("second", b'0', b"abc", ""), + ]); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete); + assert_eq!(listing.members.iter().map(|m| (m.name.as_str(), m.size)).collect::>(), [("one-member", 0), ("inherited", 3)]); + } + + #[test] + fn later_global_records_update_and_delete_only_their_keys() { + let first = record("path", "first-global") + &record("size", "3"); + let second = record("path", "second-global"); + let remove = record("path", "") + &record("size", ""); + let data = fx::tar_entries(&[ + ("global", b'g', first.as_bytes(), ""), + ("one", b'0', b"abc", ""), + ("global", b'g', second.as_bytes(), ""), + ("two", b'0', b"def", ""), + ("global", b'g', remove.as_bytes(), ""), + ("plain", b'0', b"normal", ""), + ]); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete); + assert_eq!( + listing.members.iter().map(|m| (m.name.as_str(), m.size)).collect::>(), + [("first-global", 3), ("second-global", 3), ("plain", 6)] + ); + } + + #[test] + fn global_link_names_persist_until_deleted() { + let global = record("linkpath", "shared-target"); + let local = record("linkpath", "local-target"); + let remove = record("linkpath", ""); + let data = fx::tar_entries(&[ + ("global", b'g', global.as_bytes(), ""), + ("one", b'2', b"", "fallback"), + ("local", b'x', local.as_bytes(), ""), + ("two", b'1', b"", "fallback"), + ("three", b'2', b"", "fallback"), + ("global", b'g', remove.as_bytes(), ""), + ("four", b'2', b"", "original"), + ]); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete); + assert_eq!( + listing.members.iter().map(|m| m.link_target.as_deref()).collect::>(), + [Some("shared-target"), Some("local-target"), Some("shared-target"), Some("original")] + ); + } + + #[test] + fn malformed_or_excess_global_records_do_not_claim_complete_members() { + for metadata in [String::from("10 size=3!"), record("comment", "").repeat(257), record("path", &"a".repeat(4097))] { + let data = fx::tar_entries(&[("keep", b'0', b"ok", ""), ("global", b'g', metadata.as_bytes(), ""), ("after", b'0', b"later", "")]); + let listing = tar(&data, 10).unwrap(); + assert!(!listing.complete); + assert_eq!(listing.members.len(), 1); + assert_eq!(listing.members[0].name, "keep"); + assert!(listing.warnings.iter().any(|warning| warning.contains("pax"))); + } + } + + #[test] + fn global_sparse_metadata_keeps_stored_extents_out_of_regular_files() { + let global = record("GNU.sparse.map", "0,3"); + let remove = record("GNU.sparse.map", ""); + let data = fx::tar_entries(&[ + ("global", b'g', global.as_bytes(), ""), + ("one", b'0', b"abc", ""), + ("two", b'0', b"def", ""), + ("global", b'g', remove.as_bytes(), ""), + ("plain", b'0', b"ghi", ""), + ]); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete); + assert_eq!(listing.members.iter().map(|m| m.kind).collect::>(), [MemberKind::Other, MemberKind::Other, MemberKind::File]); + assert_eq!(listing.warnings.iter().filter(|warning| warning.contains("sparse")).count(), 2); + } + + #[test] + fn unrelated_global_fields_preserve_local_and_gnu_names() { + let global = record("comment", "not a member name"); + let local = record("path", "local"); + let data = fx::tar_entries(&[ + ("local", b'x', local.as_bytes(), ""), + ("global", b'g', global.as_bytes(), ""), + ("first", b'0', b"one", ""), + ("././@LongLink", b'L', b"gnu-name\0", ""), + ("second", b'0', b"two", ""), + ]); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete && listing.warnings.is_empty()); + assert_eq!(listing.members.iter().map(|m| m.name.as_str()).collect::>(), ["local", "gnu-name"]); + } + + #[test] + fn global_names_obey_the_existing_member_limit() { + let path = "a".repeat(4096); + let global = record("path", &path); + let data = fx::tar_entries(&[("global", b'g', global.as_bytes(), ""), ("one", b'0', b"abc", ""), ("two", b'0', b"def", "")]); + let listing = tar(&data, 1).unwrap(); + assert!(listing.truncated && !listing.complete); + assert_eq!(listing.members.len(), 1); + assert_eq!(listing.members[0].name, path); + } + + #[test] + fn global_pax_names_override_and_consume_gnu_name_records() { + let global = record("path", "global") + &record("linkpath", "global-target"); + let remove = record("path", "") + &record("linkpath", ""); + let data = fx::tar_entries(&[ + ("global", b'g', global.as_bytes(), ""), + ("LongLink", b'L', b"gnu-name\0", ""), + ("LongLink", b'K', b"gnu-target\0", ""), + ("first", b'2', b"", "fallback"), + ("global", b'g', remove.as_bytes(), ""), + ("plain", b'2', b"", "original"), + ]); + let listing = tar(&data, 10).unwrap(); + assert!(listing.complete); + assert_eq!( + listing.members.iter().map(|m| (m.name.as_str(), m.link_target.as_deref())).collect::>(), + [("global", Some("global-target")), ("plain", Some("original"))] + ); + } + + #[test] + fn nonempty_global_sizes_must_be_unsigned_decimal_numbers() { + for size in ["+3", "-3", "3x", " 3", "18446744073709551616"] { + let global = record("size", size); + let data = fx::tar_entries(&[("keep", b'0', b"abc", ""), ("global", b'g', global.as_bytes(), ""), ("after", b'0', b"def", "")]); + let listing = tar(&data, 10).unwrap(); + assert!(!listing.complete); + assert_eq!(listing.members.len(), 1); + assert!(listing.warnings.iter().any(|warning| warning.contains("global pax size"))); + } + } +} diff --git a/crates/tar/src/testkit.rs b/crates/tar/src/testkit.rs new file mode 100644 index 000000000..828644a4b --- /dev/null +++ b/crates/tar/src/testkit.rs @@ -0,0 +1,135 @@ +//! Archive builders and damage generators for the tests, from Craft Anything's testkit. + +#![allow(clippy::indexing_slicing, clippy::cast_possible_truncation)] + +fn tar_header(name: &str, typeflag: u8, size: usize, link: &str) -> Vec { + let mut h = vec![0u8; 512]; + let n = name.as_bytes(); + h[..n.len().min(100)].copy_from_slice(&n[..n.len().min(100)]); + h[100..108].copy_from_slice(b"0000644\0"); + h[108..116].copy_from_slice(b"0000000\0"); + h[116..124].copy_from_slice(b"0000000\0"); + h[124..136].copy_from_slice(format!("{size:011o}\0").as_bytes()); + h[136..148].copy_from_slice(b"00000000000\0"); + h[156] = typeflag; + let l = link.as_bytes(); + h[157..157 + l.len().min(100)].copy_from_slice(&l[..l.len().min(100)]); + h[257..263].copy_from_slice(b"ustar\0"); + h[263..265].copy_from_slice(b"00"); + h[148..156].copy_from_slice(b" "); + let sum: u32 = h.iter().map(|&b| u32::from(b)).sum(); + h[148..156].copy_from_slice(format!("{sum:06o}\0 ").as_bytes()); + h +} + +fn pad512(v: &mut Vec) { + while !v.len().is_multiple_of(512) { + v.push(0); + } +} + +/// A tar entry for [`tar_entries`]: (name, typeflag, data, link target). +pub type TarEntry<'a> = (&'a str, u8, &'a [u8], &'a str); + +/// A ustar archive from raw entries; names longer than 100 bytes get a GNU long-name record. +pub fn tar_entries(entries: &[TarEntry<'_>]) -> Vec { + let mut out = Vec::new(); + for (name, flag, data, link) in entries { + if name.len() > 100 { + let mut long = name.as_bytes().to_vec(); + long.push(0); + out.extend(tar_header("././@LongLink", b'L', long.len(), "")); + out.extend_from_slice(&long); + pad512(&mut out); + } + out.extend(tar_header(name, *flag, data.len(), link)); + out.extend_from_slice(data); + pad512(&mut out); + } + out.extend(vec![0u8; 1024]); + out +} + +/// A ustar archive of files (names ending in `/` become folders). +pub fn tar(files: &[(&str, &[u8])]) -> Vec { + let e: Vec> = files.iter().map(|(n, d)| (*n, if n.ends_with('/') { b'5' } else { b'0' }, *d, "")).collect(); + tar_entries(&e) +} + +/// A cpio newc archive from (name, data, mode). Symlinks (mode 0o120000) store the target as data. +pub fn cpio_newc(files: &[(&str, &[u8], u32)]) -> Vec { + let mut out = Vec::new(); + let mut push = |name: &str, data: &[u8], mode: u32, ino: u32| { + let hdr = format!( + "070701{ino:08x}{mode:08x}{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}{:08x}", + 0, + 0, + 1, + 0, + data.len(), + 0, + 0, + 0, + 0, + name.len() + 1, + 0 + ); + out.extend_from_slice(hdr.as_bytes()); + out.extend_from_slice(name.as_bytes()); + out.push(0); + while !out.len().is_multiple_of(4) { + out.push(0); + } + out.extend_from_slice(data); + while !out.len().is_multiple_of(4) { + out.push(0); + } + }; + for (i, (n, d, m)) in files.iter().enumerate() { + push(n, d, *m, i as u32 + 1); + } + push("TRAILER!!!", b"", 0, 0); + out +} + +/// Deterministic pseudo-random bytes (xorshift) for fuzz-like loops. +pub fn noise(seed: u64, len: usize) -> Vec { + let mut x = seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1; + (0..len) + .map(|_| { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + x as u8 + }) + .collect() +} + +/// Every way to damage a sample used by robustness tests: truncations at many lengths and +/// single-byte corruptions spread across the file. +pub fn mutations(sample: &[u8]) -> Vec> { + let mut out = Vec::new(); + let n = sample.len(); + let step = (n / 64).max(1); + let mut len = 0; + while len < n { + out.push(sample[..len].to_vec()); + len += step; + } + for (i, seed) in (0..n).step_by(step).zip(1u64..) { + let mut m = sample.to_vec(); + let noise = noise(seed, 8); + for (j, v) in noise.iter().enumerate() { + if let Some(b) = m.get_mut(i + j) { + *b = *v; + } + } + out.push(m); + let mut m = sample.to_vec(); + if let Some(b) = m.get_mut(i) { + *b = 0xff; + } + out.push(m); + } + out +} diff --git a/crates/text/Cargo.toml b/crates/text/Cargo.toml index 280ee99ca..39be25683 100644 --- a/crates/text/Cargo.toml +++ b/crates/text/Cargo.toml @@ -17,6 +17,7 @@ harness = false workspace = true [dependencies] +kurbo = { workspace = true, optional = true } hypher = { version = "0.1.8", default-features = false, features = ["alloc"], optional = true } miniz_oxide = { version = "0.8", optional = true } unicode-general-category = { version = "1.1", optional = true } @@ -51,6 +52,7 @@ name = "hyphgen" required-features = ["generator"] [features] +positioned = ["dep:kurbo"] default = ["hyphen", "bidi", "segment"] # Hyphenation points; English (bundled dictionary and patterns) needs no further feature. hyphen = ["dep:hypher", "dep:miniz_oxide", "dep:unicode-general-category"] diff --git a/crates/text/src/composer.rs b/crates/text/src/composer.rs new file mode 100644 index 000000000..fc2067f3f --- /dev/null +++ b/crates/text/src/composer.rs @@ -0,0 +1,529 @@ +//! Every-line composer: a Knuth–Plass style total-fit line breaker. +//! +//! Boxes are the paragraph's glyphs, glue is its spaces, and penalties are break opportunities +//! after hyphens/dashes/CJK and at hyphenation points. The breaks minimise the sum of squared +//! (badness + penalty) demerits over the whole paragraph, plus a demerit when neighbouring lines +//! fall in fitness classes more than one apart (a tight line next to a loose one). +//! +//! - **Justified** lines: word spacing, letter spacing and glyph scaling may stretch and shrink +//! within the paragraph's Justification options (each glyph's [`Flexibility`]); the badness comes from +//! the stretch/shrink ratio, so loose lines are traded for evenly spaced ones. +//! - **Ragged** lines (left/centre/right aligned): spaces keep their width, so a line never +//! shrinks (it must fit). The badness comes from the line's leftover space measured against a +//! rag zone of a sixth of the line width, so the rag is even rather than full lines followed by +//! a short one, and hyphenating costs less. The last line costs nothing unless it is shorter +//! than a third of the width (a word or two left dangling). + +/// Glyph measurements needed by the paragraph composer. +pub trait Glyph { + fn advance(&self) -> f64; + fn is_space(&self) -> bool; + fn is_soft_hyphen(&self) -> bool; +} + +/// Stretch and shrink available for a glyph. +pub trait Flexibility { + fn stretch(&self) -> f64; + fn shrink(&self) -> f64; +} + +const HYPHEN_PENALTY: f64 = 50.0; +const DOUBLE_HYPHEN_DEMERITS: f64 = 3000.0; +const LINE_PENALTY: f64 = 10.0; +/// Demerits for adjacent lines whose fitness classes differ by more than one. +const ADJ_DEMERITS_JUSTIFIED: f64 = 10_000.0; +const ADJ_DEMERITS_RAGGED: f64 = 1_000.0; +/// Ragged lines only gain from hyphenation by filling the rag, so it costs less than in +/// justified text (where it also evens the word spacing). +const HYPHEN_PENALTY_RAGGED: f64 = 25.0; +/// Ragged text: leftover space is measured in units of this fraction of the line width. +const RAG_ZONE: f64 = 1.0 / 6.0; +/// Ragged text: a last line shorter than this fraction of the width is penalised, by how much +/// shorter it is in units of `SHORT_LAST_ZONE` of the width. +const SHORT_LAST: f64 = 1.0 / 3.0; +const SHORT_LAST_ZONE: f64 = 1.0 / 3.0; +/// Give up (fall back to the single-line composer) beyond this many active breakpoints. +const MAX_ACTIVE: usize = 4096; +const EPS: f64 = 1e-6; + +/// A place a line may end: glyph index `end` (exclusive), plus the width of the hyphen that is +/// added when breaking there (0 = no hyphen). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Breakpoint { + pub end: usize, + pub hyphen: f64, +} + +/// How a paragraph is composed. +#[derive(Clone, Copy, Debug)] +pub struct Params { + /// The last line is justified too (Justify All). + pub justify_last: bool, + /// Left/centre/right aligned text: spaces keep their width and lines are scored by their rag. + pub ragged: bool, + /// Maximum stretch ratio (justified) or leftover space in rag zones (ragged) of a line. + pub tolerance: f64, + /// Lines from this index on all have the same width, so their line numbers are interchangeable + /// (keeps the number of active breakpoints small). + pub uniform_from: usize, + /// Hyphen Limit: the most consecutive lines that may end with a hyphen before a hyphenation + /// point is no longer a break; 0 sets no limit. A soft hyphen stays a break and counts. + pub hyphen_limit: usize, +} + +/// The search gave up: it ran past its budget ([`Measure::line`]). +#[derive(Debug)] +pub struct Exhausted; + +/// Where each line of a paragraph goes and how wide it is: the measure a line gets may depend on +/// the lines set above it (their heights move it down a shaped frame, or past a wrap object). +pub trait Measure { + /// What the measure of the next line depends on; breaks reached with equal states are + /// interchangeable from there on. + type State: Clone + PartialEq; + /// The state of the paragraph's first line. + fn first(&mut self) -> Self::State; + /// The width of the line of glyphs `pos..end` set from `state`, and the state of the line + /// after it; `Ok(None)` when the line can't be set (the frame is full), and [`Exhausted`] when + /// the search is to stop. + fn line(&mut self, state: &Self::State, pos: usize, end: usize) -> Result, Exhausted>; + /// Whether a line from `state` longer than `pos..end`, which [`Measure::line`] just measured, + /// may get more room than it. While it may, a line from `state` that is too long, or that + /// can't be set, does not end the reach of `state`. Lines from a state are all as wide by + /// default. + fn may_widen(&mut self, _state: &Self::State, _pos: usize, _end: usize) -> bool { + false + } +} + +/// Line widths by line number: `width(k)` for line `k`, lines from `uniform_from` on all as wide. +struct ByLine<'a> { + width: &'a dyn Fn(usize) -> f64, + uniform_from: usize, +} + +impl Measure for ByLine<'_> { + type State = usize; + fn first(&mut self) -> usize { + 0 + } + fn line(&mut self, &line: &usize, _: usize, _: usize) -> Result, Exhausted> { + Ok(Some(((self.width)(line), (line + 1).min(self.uniform_from)))) + } +} + +struct Node { + pos: usize, + /// The measure of the next line (its line number, clamped to `uniform_from`, for [`compose`]). + state: S, + fitness: u8, + demerits: f64, + /// Lines in a row ending with a hyphen up to this break (at most `hyphen_limit`, or 1 with no + /// limit); 0 when this break has no hyphen. + hyphens: usize, + prev: Option, +} + +/// Fitness class 0 (tight) … 3 (very loose) of a line with adjustment ratio `r`. +fn fitness(r: f64, ragged: bool) -> u8 { + let bounds = if ragged { [0.15, 0.4, 0.75] } else { [-0.5, 0.5, 1.0] }; + if r < bounds[0] { + 0 + } else if r <= bounds[1] { + 1 + } else if r <= bounds[2] { + 2 + } else { + 3 + } +} + +/// Adjustment ratio for a candidate line; `None` means a ragged line is already too long. +fn adjustment_ratio(natural: f64, width: f64, stretch: f64, shrink: f64, last: bool, p: &Params) -> Option { + if p.ragged { + if natural > width + EPS { + return None; + } + return Some(if last && !p.justify_last { + ((width * SHORT_LAST - natural) / (width * SHORT_LAST_ZONE).max(1.0)).max(0.0) + } else { + (width - natural).max(0.0) / (width * RAG_ZONE).max(1.0) + }); + } + Some(if natural < width { + if last && !p.justify_last { + 0.0 + } else if stretch > EPS { + (width - natural) / stretch + } else { + f64::INFINITY + } + } else if natural > width { + if shrink > EPS { (width - natural) / shrink } else { f64::NEG_INFINITY } + } else { + 0.0 + }) +} + +/// Compose the paragraph `g` into lines. `width(k)` is the available width of line `k`; +/// `start_credit(i)` is the width a wrapped line starting at glyph `i` gives back (Mojikumi sets +/// an opening bracket there flush with the line's start); `end_credit(t)` is the width a line whose +/// last character is glyph `t - 1` gives back (Mojikumi sets a closing mark there half width), at +/// most that glyph's advance, and nothing at a hyphenation point; `cands` are the break opportunities +/// in increasing order (the paragraph end is added); `flex` holds how far each glyph may stretch +/// and shrink in a justified line. +/// Returns `(end, hyphen)` per line, or `None` if no set of breaks fits within the tolerance +/// (the caller then falls back to single-line breaking). +pub fn compose( + g: &[G], + width: &dyn Fn(usize) -> f64, + start_credit: &dyn Fn(usize) -> f64, + end_credit: &dyn Fn(usize) -> f64, + cands: &[Breakpoint], + flex: &[F], + p: &Params, +) -> Option> { + compose_with(g, &mut ByLine { width, uniform_from: p.uniform_from }, start_credit, end_credit, cands, flex, p) +} + +/// [`compose`] with each line's width from `measure`, which follows where the lines above it went. +pub fn compose_with( + g: &[G], + measure: &mut M, + start_credit: &dyn Fn(usize) -> f64, + end_credit: &dyn Fn(usize) -> f64, + cands: &[Breakpoint], + flex: &[F], + p: &Params, +) -> Option> { + let n = g.len(); + // The search assumes increasing break positions when indexing its prefix sums. + // Reject malformed candidates before they can make a later line run backwards. + if cands.windows(2).any(|pair| pair[0].end > pair[1].end) { + return None; + } + if n == 0 { + return Some(vec![]); + } + // Prefix sums of advances and of how far the glyphs may stretch and shrink. + let mut w = vec![0.0; n + 1]; + let mut st = vec![0.0; n + 1]; + let mut sh = vec![0.0; n + 1]; + for (i, gl) in g.iter().enumerate() { + let f = flex.get(i); + w[i + 1] = w[i] + gl.advance(); + st[i + 1] = st[i] + f.map_or(0.0, Flexibility::stretch); + sh[i + 1] = sh[i] + f.map_or(0.0, Flexibility::shrink); + } + if !w[n].is_finite() { + return None; + } + // Trailing spaces are dropped at a break. + let trim = |mut b: usize, a: usize| { + while b > a && g.get(b - 1).is_some_and(Glyph::is_space) { + b -= 1; + } + b + }; + let line_end_credit = |t: usize| { + let credit = end_credit(t); + let most = t.checked_sub(1).and_then(|k| g.get(k)).map_or(0.0, |gl| gl.advance().max(0.0)); + if credit.is_finite() { credit.clamp(0.0, most) } else { 0.0 } + }; + let mut all: Vec = cands.iter().copied().filter(|c| c.end > 0 && c.end < n).collect(); + all.push(Breakpoint { end: n, hyphen: 0.0 }); + let (adj_demerits, hyphen_penalty) = + if p.ragged { (ADJ_DEMERITS_RAGGED, HYPHEN_PENALTY_RAGGED) } else { (ADJ_DEMERITS_JUSTIFIED, HYPHEN_PENALTY) }; + let mut nodes = vec![Node { pos: 0, state: measure.first(), fitness: 1, demerits: 0.0, hyphens: 0, prev: None }]; + let mut active: Vec = vec![0]; + for bp in &all { + let last = bp.end == n; + let hyphen = bp.hyphen > 0.0; + // `automatic` is true for a hyphenation point and false for a soft hyphen typed in the text. + let automatic = hyphen && !bp.end.checked_sub(1).and_then(|k| g.get(k)).is_some_and(Glyph::is_soft_hyphen); + let mut best: Vec<(M::State, u8, usize, f64, usize)> = vec![]; // (state, fitness, hyphens, demerits, from) + let mut keep = Vec::with_capacity(active.len()); + for &ai in &active { + let Some(a) = nodes.get(ai) else { continue }; + let t = trim(bp.end, a.pos); + let credit = if a.pos > 0 { start_credit(a.pos) } else { 0.0 }; + // A line that ends with a hyphen ends with it, not with its last character. At most the + // last glyph's advance, so a longer line from the same node is never narrower. + let end = if hyphen || t <= a.pos { 0.0 } else { line_end_credit(t) }; + let natural = w[t] - w[a.pos] - credit - end + bp.hyphen; + // A line that can't be set (no room left in the frame) ends this node's reach, unless a + // longer line may be placed elsewhere (a taller line steps down the frame by its own + // leading). + let Some((lw, next)) = measure.line(&a.state, a.pos, bp.end).ok()? else { + if measure.may_widen(&a.state, a.pos, bp.end) { + keep.push(ai); + } + continue; + }; + // Adjustment ratio, badness and whether the line ends the active node's reach. + let ratio = adjustment_ratio(natural, lw, st[t] - st[a.pos], sh[t] - sh[a.pos], last, p); + let Some(ratio) = ratio.filter(|&r| r >= -1.0 || r.is_nan()) else { + // Too long already; longer lines from this node only get worse, unless they may + // get more room (a taller line set lower down a shaped frame). + if measure.may_widen(&a.state, a.pos, bp.end) { + keep.push(ai); + } + continue; + }; + keep.push(ai); + if automatic && p.hyphen_limit > 0 && a.hyphens >= p.hyphen_limit { + continue; + } + let free_last = last && (p.ragged || !p.justify_last); + // A line that can't stretch at all (no word spaces, nor letter spacing or glyph scaling + // to vary) never fills its measure, however loose the tolerance. + if (ratio > p.tolerance || ratio == f64::INFINITY) && !(last && (p.ragged || (ratio.is_infinite() && p.justify_last))) { + continue; + } + // Justified lines aren't capped: past a ratio of 4.6 (only reached in the unbounded pass) + // a looser line must still score worse than a less loose one. + let cap = if p.ragged { 10_000.0 } else { f64::INFINITY }; + let badness = if ratio.is_finite() { (100.0 * ratio.abs().powi(3)).min(cap) } else { 10_000.0 }; + let penalty = if hyphen { hyphen_penalty } else { 0.0 }; + let mut d = (LINE_PENALTY + badness).powi(2) + penalty * penalty; + if hyphen && a.hyphens > 0 { + d += DOUBLE_HYPHEN_DEMERITS; + } + // A ragged last line's length says nothing about the rag: it keeps its neighbour's class. + let fit = if p.ragged && free_last { a.fitness } else { fitness(ratio, p.ragged) }; + if a.prev.is_some() && fit.abs_diff(a.fitness) > 1 { + d += adj_demerits; + } + let total = a.demerits + d; + // With a limit, breaks reached with different runs of hyphenated lines stay apart. + let hyphens = match (hyphen, p.hyphen_limit) { + (false, _) => 0, + (true, 0) => 1, + (true, limit) => (a.hyphens + 1).min(limit), + }; + match best.iter_mut().find(|b| b.0 == next && b.1 == fit && b.2 == hyphens) { + Some(b) if b.3 <= total => {} + Some(b) => *b = (next, fit, hyphens, total, ai), + None => best.push((next, fit, hyphens, total, ai)), + } + } + active = keep; + for (state, fitness, hyphens, demerits, from) in best { + nodes.push(Node { pos: bp.end, state, fitness, demerits, hyphens, prev: Some(from) }); + active.push(nodes.len() - 1); + } + if active.is_empty() || active.len() > MAX_ACTIVE { + return None; + } + } + let end = nodes.iter().enumerate().filter(|(_, nd)| nd.pos == n && nd.prev.is_some()).min_by(|a, b| a.1.demerits.total_cmp(&b.1.demerits))?.0; + let mut out = vec![]; + let mut cur = Some(end); + while let Some(i) = cur { + let nd = nodes.get(i)?; + if nd.prev.is_some() { + out.push((nd.pos, nd.hyphens > 0)); + } + cur = nd.prev; + if out.len() > n { + return None; + } + } + out.reverse(); + Some(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + struct Unit(char); + impl Glyph for Unit { + fn advance(&self) -> f64 { + 1.0 + } + fn is_space(&self) -> bool { + self.0 == ' ' + } + fn is_soft_hyphen(&self) -> bool { + self.0 == '\u{ad}' + } + } + struct Rigid; + impl Flexibility for Rigid { + fn stretch(&self) -> f64 { + 0.0 + } + fn shrink(&self) -> f64 { + 0.0 + } + } + #[test] + fn rejects_breaks_that_move_backwards() { + let glyphs: Vec<_> = "abcd".chars().map(Unit).collect(); + let candidates = [Breakpoint { end: 3, hyphen: 0.0 }, Breakpoint { end: 1, hyphen: 0.0 }]; + let params = Params { justify_last: false, ragged: true, tolerance: 100.0, uniform_from: 0, hyphen_limit: 0 }; + assert_eq!(compose(&glyphs, &|_| 3.0, &|_| 0.0, &|_| 0.0, &candidates, &[] as &[Rigid], ¶ms), None); + } + + #[test] + fn composes_ragged_paragraph_and_trims_spaces() { + let glyphs: Vec<_> = "aa bb cc".chars().map(Unit).collect(); + let candidates = [Breakpoint { end: 3, hyphen: 0.0 }, Breakpoint { end: 6, hyphen: 0.0 }]; + let params = Params { justify_last: false, ragged: true, tolerance: 100.0, uniform_from: 0, hyphen_limit: 0 }; + let result = compose(&glyphs, &|_| 3.0, &|_| 0.0, &|_| 0.0, &candidates, &[] as &[Rigid], ¶ms); + assert_eq!(result, Some(vec![(3, false), (6, false), (8, false)])); + } + struct Flex(f64, f64); + impl Flexibility for Flex { + fn stretch(&self) -> f64 { + self.0 + } + fn shrink(&self) -> f64 { + self.1 + } + } + struct Wide(f64); + impl Glyph for Wide { + fn advance(&self) -> f64 { + self.0 + } + fn is_space(&self) -> bool { + false + } + fn is_soft_hyphen(&self) -> bool { + false + } + } + fn justified() -> Params { + Params { justify_last: false, ragged: false, tolerance: 10.0, uniform_from: 0, hyphen_limit: 0 } + } + fn units(text: &str) -> Vec { + text.chars().map(Unit).collect() + } + fn brk(end: usize, hyphen: f64) -> Breakpoint { + Breakpoint { end, hyphen } + } + + #[test] + fn fitness_classes_and_adjustment_ratios() { + assert_eq!([-0.6, 0.0, 0.7, 2.0].map(|r| fitness(r, false)), [0, 1, 2, 3]); + assert_eq!([0.1, 0.3, 0.5, 0.9].map(|r| fitness(r, true)), [0, 1, 2, 3]); + let j = justified(); + assert_eq!(adjustment_ratio(8.0, 10.0, 4.0, 0.0, false, &j), Some(0.5)); + assert_eq!(adjustment_ratio(8.0, 10.0, 0.0, 0.0, false, &j), Some(f64::INFINITY)); + assert_eq!(adjustment_ratio(8.0, 10.0, 4.0, 0.0, true, &j), Some(0.0)); + assert_eq!(adjustment_ratio(12.0, 10.0, 0.0, 4.0, false, &j), Some(-0.5)); + assert_eq!(adjustment_ratio(12.0, 10.0, 0.0, 0.0, false, &j), Some(f64::NEG_INFINITY)); + assert_eq!(adjustment_ratio(10.0, 10.0, 0.0, 0.0, false, &j), Some(0.0)); + let all = Params { justify_last: true, ..j }; + assert_eq!(adjustment_ratio(8.0, 10.0, 4.0, 0.0, true, &all), Some(0.5)); + let ragged = Params { ragged: true, ..j }; + assert_eq!(adjustment_ratio(11.0, 10.0, 0.0, 0.0, false, &ragged), None); + } + + #[test] + fn empty_and_unmeasurable_paragraphs() { + let none: [Unit; 0] = []; + assert_eq!(compose(&none, &|_| 3.0, &|_| 0.0, &|_| 0.0, &[], &[] as &[Rigid], &justified()), Some(vec![])); + let huge = [Wide(f64::INFINITY)]; + assert_eq!(compose(&huge, &|_| 3.0, &|_| 0.0, &|_| 0.0, &[], &[] as &[Rigid], &justified()), None); + } + + #[test] + fn justified_lines_stretch_and_shrink_within_tolerance() { + let glyphs = units("a a b b"); + let flex: Vec<_> = glyphs.iter().map(|g| if g.0 == ' ' { Flex(1.0, 0.5) } else { Flex(0.0, 0.0) }).collect(); + let cands = [brk(4, 0.0)]; + // "a a" (the trailing space is dropped) stretches its inner space to fill 4 units... + let lines = compose(&glyphs, &|_| 4.0, &|_| 0.0, &|_| 0.0, &cands, &flex, &justified()); + assert_eq!(lines, Some(vec![(4, false), (7, false)])); + // ...or shrinks it into 2.75. + let lines = compose(&glyphs, &|_| 2.75, &|_| 0.0, &|_| 0.0, &cands, &flex, &justified()); + assert_eq!(lines, Some(vec![(4, false), (7, false)])); + // Rigid glyphs can't fill a wider line except the last, so there is no fit. + let rigid: Vec<_> = glyphs.iter().map(|_| Rigid).collect(); + assert_eq!(compose(&glyphs, &|_| 4.0, &|_| 0.0, &|_| 0.0, &cands, &rigid, &justified()), None); + // Justify All lets an unstretchable last line through. + let all = Params { justify_last: true, ..justified() }; + let word = units("ab"); + assert_eq!(compose(&word, &|_| 4.0, &|_| 0.0, &|_| 0.0, &[], &[Rigid, Rigid], &all), Some(vec![(2, false)])); + } + + #[test] + fn hyphen_limit_and_soft_hyphens() { + // "ab-cd-ef" with hyphenation points after each pair: every line wants a hyphen. + let glyphs = units("abcdef"); + let cands = [brk(2, 1.0), brk(4, 1.0)]; + let ragged = Params { justify_last: false, ragged: true, tolerance: 100.0, uniform_from: 0, hyphen_limit: 0 }; + let free = compose(&glyphs, &|_| 3.0, &|_| 0.0, &|_| 0.0, &cands, &[] as &[Rigid], &ragged).unwrap(); + assert_eq!(free, vec![(2, true), (4, true), (6, false)]); + // A limit of one hyphenated line in a row forbids the second automatic hyphen. + let limited = Params { hyphen_limit: 1, ..ragged }; + assert_eq!(compose(&glyphs, &|_| 3.0, &|_| 0.0, &|_| 0.0, &cands, &[] as &[Rigid], &limited), None); + // A typed soft hyphen stays a break regardless of the limit. + let soft = units("ab\u{ad}cd\u{ad}ef"); + let cands = [brk(3, 1.0), brk(6, 1.0)]; + let out = compose(&soft, &|_| 4.0, &|_| 0.0, &|_| 0.0, &cands, &[] as &[Rigid], &limited).unwrap(); + assert_eq!(out, vec![(3, true), (6, true), (8, false)]); + } + + #[test] + fn credits_are_clamped_to_the_glyph_and_ignored_when_not_finite() { + let glyphs = units("aa bb"); + let cands = [brk(3, 0.0)]; + let ragged = Params { justify_last: false, ragged: true, tolerance: 100.0, uniform_from: 0, hyphen_limit: 0 }; + for credit in [f64::NAN, 5.0, -1.0] { + let out = compose(&glyphs, &|_| 2.0, &|_| 0.0, &|_| credit, &cands, &[] as &[Rigid], &ragged); + assert_eq!(out, Some(vec![(3, false), (5, false)]), "{credit}"); + } + // A start credit gives a wrapped line room back. + let out = compose(&units("aa bbb"), &|_| 2.0, &|_| 1.0, &|_| 0.0, &cands, &[] as &[Rigid], &ragged); + assert_eq!(out, Some(vec![(3, false), (6, false)])); + } + + /// Two lines of width 2, then the frame is full; a line may widen when the frame says so. + struct Frame { + widen: bool, + calls: usize, + } + impl Measure for Frame { + type State = usize; + fn first(&mut self) -> usize { + 0 + } + fn line(&mut self, &k: &usize, _: usize, _: usize) -> Result, Exhausted> { + self.calls += 1; + if self.calls > 1_000 { + return Err(Exhausted); + } + Ok((k < 2).then_some((2.0, k + 1))) + } + fn may_widen(&mut self, _: &usize, _: usize, _: usize) -> bool { + self.widen + } + } + + #[test] + fn measure_can_end_reach_widen_or_exhaust() { + let ragged = Params { justify_last: false, ragged: true, tolerance: 100.0, uniform_from: 0, hyphen_limit: 0 }; + let two = units("aa bb"); + let cands = [brk(3, 0.0)]; + let mut frame = Frame { widen: false, calls: 0 }; + assert_eq!(compose_with(&two, &mut frame, &|_| 0.0, &|_| 0.0, &cands, &[] as &[Rigid], &ragged), Some(vec![(3, false), (5, false)])); + // A third line can't be set: no fit, whether or not the frame may widen. + let three = units("aa bb cc"); + let cands = [brk(3, 0.0), brk(6, 0.0)]; + for widen in [false, true] { + let mut frame = Frame { widen, calls: 0 }; + assert_eq!(compose_with(&three, &mut frame, &|_| 0.0, &|_| 0.0, &cands, &[] as &[Rigid], &ragged), None); + } + // Too long for the line, but the frame may widen: the node stays active. + let long = units("aaa b"); + let mut frame = Frame { widen: true, calls: 0 }; + assert_eq!(compose_with(&long, &mut frame, &|_| 0.0, &|_| 0.0, &[brk(4, 0.0)], &[] as &[Rigid], &ragged), None); + // The measure may stop the search. + let mut frame = Frame { widen: false, calls: 1_000 }; + assert_eq!(compose_with(&two, &mut frame, &|_| 0.0, &|_| 0.0, &[brk(3, 0.0)], &[] as &[Rigid], &ragged), None); + } +} diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index ade6753d6..8b1230a65 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -29,6 +29,9 @@ //! turn on the modules that use it. #![warn(missing_docs)] +#[cfg(feature = "positioned")] +pub mod positioned; + #[cfg(feature = "bidi")] pub mod bidi; #[cfg(feature = "find")] @@ -68,3 +71,6 @@ mod integration; fn main() { craft_ui::testing::run(module_path!(), env!("CARGO_PKG_NAME"), &craft_ui::testing::registered_in(env!("CARGO_PKG_NAME"))); } + +/// Paragraph-wide line composition with stateful line measurement. +pub mod composer; diff --git a/crates/text/src/positioned.rs b/crates/text/src/positioned.rs new file mode 100644 index 000000000..cb749cac5 --- /dev/null +++ b/crates/text/src/positioned.rs @@ -0,0 +1,212 @@ +//! A placed glyph with source mapping, geometry and optional application-owned artwork. + +use kurbo::{Affine, BezPath, Point}; + +/// Application-owned payloads and conversion to document artwork. +pub trait Artwork { + /// Bitmap glyph data. + type Image: Clone + std::fmt::Debug; + /// Colour glyph data. + type Colour: Clone + std::fmt::Debug; + /// Curved-baseline metadata. + type Bend: Clone + std::fmt::Debug; + /// Application document node. + type Node; + /// Transform from image pixels to font space. + fn image_transform(image: &Self::Image) -> Affine; + /// Place a bitmap in document space. + fn image_node(image: &Self::Image, transform: Affine) -> Self::Node; + /// Place colour artwork in document space. + fn colour_node(colour: &Self::Colour, transform: Affine) -> Self::Node; +} + +/// A glyph after shaping and composition. Coordinates are in the composer's text space; +/// `xf`, `pivot` and `unturned` refer to font space. Image, colour artwork and bend metadata +/// stay generic so the model does not own an application's document or rendering types. +#[derive(Clone, Debug)] +pub struct PositionedGlyph { + /// Outline in text space. + pub outline: BezPath, + /// Source style run index. + pub run: usize, + /// Source cluster's byte offset. + pub byte: usize, + /// Pen position on the baseline. + pub origin: Point, + /// Advance including composition spacing. + pub advance: f64, + /// Number of source bytes in the cluster. + pub len: usize, + /// Baseline angle in radians. + pub angle: f64, + /// Composed line index. + pub line: usize, + /// Identity of the resolved face. + pub font_id: u32, + /// Glyph identifier in that face. + pub gid: u32, + /// Transform from glyph font space to text space. + pub xf: Affine, + /// Whether the logical leading edge is on the right. + pub rtl: bool, + /// Paragraph glyph scaling factor. + pub scale: f64, + /// Optional bitmap artwork. + pub image: Option, + /// Optional colour artwork. + pub colour: Option, + /// Optional curved-baseline geometry. + pub bend: Option, + /// Transform restoring the glyph before character rotation. + pub unturned: Affine, + /// Character rotation pivot in font space. + pub pivot: Point, + /// Source-byte caret stops and their fractions of the glyph width. + pub carets: Vec<(usize, f64)>, + /// Glyph width excluding composition spacing. + pub width: f64, + /// Offset from the left end of the advance to the glyph width. + pub width_start: f64, + /// Source byte identifying a warichu block, when present. + pub warichu: Option, + /// Warichu block baseline offset. + pub warichu_line: f64, + /// Glyph lies before an open path and is retained for caret mapping only. + pub off_path: bool, +} + +impl PositionedGlyph { + /// Source caret stops as fractions of glyph width. + pub fn width_stops(&self) -> impl Iterator + '_ { + std::iter::once((self.byte, 0.0)).chain(self.carets.iter().copied()).chain(std::iter::once((self.byte + self.len, 1.0))) + } + + /// Source caret stops measured from the logical leading edge. + pub fn stops_along(&self, extent: f64) -> impl Iterator + '_ { + let (width, start) = if self.width > 0.0 && self.width.is_finite() { + (self.width, if self.width_start.is_finite() { self.width_start } else { 0.0 }) + } else { + (extent, 0.0) + }; + let ends = (self.byte, self.byte + self.len); + self.width_stops().map(move |(b, f)| { + if b == ends.0 { + (b, 0.0) + } else if b == ends.1 { + (b, extent) + } else if self.rtl { + (b, extent - start - (1.0 - f) * width) + } else { + (b, start + f * width) + } + }) + } + + /// Distance from the logical leading edge to the source byte's caret. + pub fn caret_offset(&self, byte: usize, extent: f64) -> f64 { + if byte <= self.byte || self.len == 0 { + return 0.0; + } + let mut from = (self.byte, 0.0); + for (b, d) in self.stops_along(extent).skip(1) { + if byte <= b { + let span = b.saturating_sub(from.0).max(1) as f64; + return from.1 + (d - from.1) * (byte - from.0) as f64 / span; + } + from = (b, d); + } + extent + } + + /// Bitmap data and its transform into text space. + pub fn placed_image(&self) -> Option<(&A::Image, Affine)> { + self.image.as_ref().map(|image| (image, self.xf * A::image_transform(image))) + } + + /// Bitmap artwork placed in document space. + pub fn placed_image_node(&self, to_doc: Affine) -> Option { + let (image, to_text) = self.placed_image()?; + Some(A::image_node(image, to_doc * to_text)) + } + + /// Colour artwork placed in document space. + pub fn placed_colour(&self, to_doc: Affine) -> Option { + Some(A::colour_node(self.colour.as_ref()?, to_doc * self.xf)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[derive(Clone, Debug)] + struct TestArt; + impl Artwork for TestArt { + type Image = Affine; + type Colour = (); + type Bend = (); + type Node = Affine; + fn image_transform(image: &Affine) -> Affine { + *image + } + fn image_node(_: &Affine, transform: Affine) -> Affine { + transform + } + fn colour_node(_: &(), transform: Affine) -> Affine { + transform + } + } + fn glyph() -> PositionedGlyph { + PositionedGlyph { + outline: BezPath::new(), + run: 0, + byte: 3, + origin: Point::ZERO, + advance: 12.0, + len: 4, + angle: 0.0, + line: 0, + font_id: 1, + gid: 4, + xf: Affine::translate((3.0, 5.0)), + rtl: false, + scale: 1.0, + image: Some(Affine::scale(2.0)), + colour: Some(()), + bend: None, + unturned: Affine::IDENTITY, + pivot: Point::ZERO, + carets: vec![(5, 0.5)], + width: 10.0, + width_start: 1.0, + warichu: None, + warichu_line: 0.0, + off_path: false, + } + } + #[test] + fn caret_mapping_tracks_ligatures_rtl_and_kerning() { + let mut g = glyph(); + assert_eq!((g.caret_offset(3, 12.0), g.caret_offset(5, 12.0), g.caret_offset(7, 12.0)), (0.0, 6.0, 12.0)); + assert_eq!(g.caret_offset(4, 12.0), 3.0); + g.rtl = true; + assert_eq!(g.caret_offset(5, 12.0), 6.0); + assert_eq!(g.caret_offset(5, 8.0), 2.0); + g.width = f64::NAN; + assert_eq!(g.caret_offset(5, 8.0), 4.0); + } + #[test] + fn artwork_uses_the_same_composition_order() { + let g = glyph(); + let doc = Affine::translate((10.0, 20.0)); + let (_, image) = g.placed_image().unwrap(); + assert_eq!(image, g.xf * Affine::scale(2.0)); + assert_eq!(g.placed_image_node(doc), Some(doc * image)); + assert_eq!(g.placed_colour(doc), Some(doc * g.xf)); + let mut empty = g; + empty.image = None; + empty.colour = None; + assert!(empty.placed_image_node(doc).is_none()); + assert!(empty.placed_colour(doc).is_none()); + } +} diff --git a/crates/tga/src/rgba8_raw.rs b/crates/tga/src/rgba8_raw.rs index 40cc256c7..a671462c9 100644 --- a/crates/tga/src/rgba8_raw.rs +++ b/crates/tga/src/rgba8_raw.rs @@ -9,6 +9,13 @@ pub enum Rgba8EncodeError { /// The output buffer could not be allocated. OutOfMemory, } +/// An empty buffer with room for `len` bytes, or [`Rgba8EncodeError::OutOfMemory`] when the +/// allocation fails (instead of aborting the process). +fn output_buffer(len: usize) -> std::result::Result, Rgba8EncodeError> { + let mut out = Vec::new(); + out.try_reserve_exact(len).map_err(|_| Rgba8EncodeError::OutOfMemory)?; + Ok(out) +} fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; if w == 0 || h == 0 || rgba.len() != n { @@ -29,8 +36,7 @@ pub fn encode_rgba8_raw(rgba: &[u8], w: u32, h: u32, alpha: bool) -> std::result // The caller already holds the frame; reserve the second, output-sized buffer fallibly so a // large export reports an error instead of aborting the process. let len = (w as usize * h as usize).checked_mul(bpp).and_then(|n| n.checked_add(18 + 26)).ok_or(Rgba8EncodeError::FrameSize)?; - let mut out = Vec::new(); - out.try_reserve_exact(len).map_err(|_| Rgba8EncodeError::OutOfMemory)?; + let mut out = output_buffer(len)?; out.extend_from_slice(&[0, 0, 2]); // no ID, no colour map, uncompressed true-colour out.extend_from_slice(&[0; 5]); // colour map specification (unused) out.extend_from_slice(&[0, 0, 0, 0]); // x / y origin @@ -63,4 +69,9 @@ mod tests { assert_eq!(&bytes[bytes.len() - 18..], b"TRUEVISION-XFILE.\0"); assert_eq!(encode_rgba8_raw(&[], 0, 1, false), Err(Rgba8EncodeError::FrameSize)); } + #[test] + fn a_failed_output_allocation_is_an_error() { + assert_eq!(output_buffer(usize::MAX), Err(Rgba8EncodeError::OutOfMemory)); + assert!(output_buffer(16).is_ok_and(|b| b.is_empty() && b.capacity() >= 16)); + } } diff --git a/crates/tiff/src/lib.rs b/crates/tiff/src/lib.rs index 0e9d595c7..5d6d78a21 100644 --- a/crates/tiff/src/lib.rs +++ b/crates/tiff/src/lib.rs @@ -28,6 +28,7 @@ pub mod classic; pub mod image; pub mod makernote; pub mod packbits; +pub mod probe; mod reader; pub mod tags; pub mod writer; diff --git a/crates/tiff/src/probe.rs b/crates/tiff/src/probe.rs new file mode 100644 index 000000000..9ed4f3bb1 --- /dev/null +++ b/crates/tiff/src/probe.rs @@ -0,0 +1,111 @@ +//! Allocation-free classic TIFF compression inspection. + +/// Return the first image's compression, or 1 when its tag is absent. +/// +/// Only classic TIFF is accepted. A duplicate Compression tag, non-scalar value, +/// unsupported field type, out-of-range LONG, or unreadable required field returns +/// `None`. Duplicate tags are refused rather than choosing one: decoders may use +/// a different copy, bypassing a caller's compression/resource policy. +/// +/// This inspects only compression, not image validity. Unrelated tag payloads, +/// next-IFD pointers and image data are not read. The caller chooses allowed +/// compression values and must still validate/decode the image with its limits. +/// No allocation occurs; at most 65,535 first-directory entries are inspected. +pub fn first_image_compression(bytes: &[u8]) -> Option { + let le = match bytes.get(..4)? { + b"II*\0" => true, + b"MM\0*" => false, + _ => return None, + }; + let u16_at = |at: usize| -> Option { + let b = [*bytes.get(at)?, *bytes.get(at.checked_add(1)?)?]; + Some(if le { u16::from_le_bytes(b) } else { u16::from_be_bytes(b) }) + }; + let u32_at = |at: usize| -> Option { + let b: [u8; 4] = bytes.get(at..at.checked_add(4)?)?.try_into().ok()?; + Some(if le { u32::from_le_bytes(b) } else { u32::from_be_bytes(b) }) + }; + let ifd = usize::try_from(u32_at(4)?).ok()?; + let mut found = None; + for i in 0..usize::from(u16_at(ifd)?) { + let entry = ifd.checked_add(2)?.checked_add(i.checked_mul(12)?)?; + if u16_at(entry)? != 259 { + continue; + } + if found.is_some() || u32_at(entry.checked_add(4)?)? != 1 { + return None; + } + found = Some(match u16_at(entry.checked_add(2)?)? { + 3 => u16_at(entry.checked_add(8)?)?, + 4 => u16::try_from(u32_at(entry.checked_add(8)?)?).ok()?, + _ => return None, + }); + } + Some(found.unwrap_or(1)) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + use crate::ByteOrder; + + fn fixture(order: ByteOrder, entries: &[(u16, u16, u32, u32)]) -> Vec { + let mut bytes = match order { + ByteOrder::Little => b"II*\0".to_vec(), + ByteOrder::Big => b"MM\0*".to_vec(), + }; + order.put_u32(&mut bytes, 8); + order.put_u16(&mut bytes, u16::try_from(entries.len()).unwrap()); + for &(tag, ty, count, value) in entries { + order.put_u16(&mut bytes, tag); + order.put_u16(&mut bytes, ty); + order.put_u32(&mut bytes, count); + if ty == 3 { + order.put_u16(&mut bytes, u16::try_from(value).unwrap()); + order.put_u16(&mut bytes, 0); + } else { + order.put_u32(&mut bytes, value); + } + } + bytes + } + + #[test] + fn both_byte_orders_and_scalar_types() { + for order in [ByteOrder::Little, ByteOrder::Big] { + assert_eq!(first_image_compression(&fixture(order, &[])), Some(1)); + for ty in [3, 4] { + for value in [1, 5, 6, 7, 32773] { + let bytes = fixture(order, &[(259, ty, 1, value)]); + assert_eq!(first_image_compression(&bytes), u16::try_from(value).ok()); + // Every prefix missing a required byte is refused; SHORT padding and + // the next-IFD pointer are intentionally unnecessary for this probe. + let needed = if ty == 3 { 20 } else { 22 }; + for end in 0..needed { + assert_eq!(first_image_compression(&bytes[..end]), None); + } + } + } + } + } + + #[test] + fn ambiguous_compression_is_refused() { + for order in [ByteOrder::Little, ByteOrder::Big] { + for pair in [(1, 7), (7, 1), (1, 1)] { + assert_eq!(first_image_compression(&fixture(order, &[(259, 3, 1, pair.0), (259, 3, 1, pair.1)])), None); + } + for entry in [(259, 3, 0, 1), (259, 3, 2, 1), (259, 1, 1, 1), (259, 4, 1, 65536)] { + assert_eq!(first_image_compression(&fixture(order, &[entry])), None); + } + // Unrelated values are not decoded or allocated, including unknown types. + assert_eq!(first_image_compression(&fixture(order, &[(999, 65535, u32::MAX, u32::MAX)])), Some(1)); + let mut absent = fixture(order, &[(999, 4, 1, 0)]); + absent.truncate(12); // Only the unrelated tag is needed, matching Word's probe. + assert_eq!(first_image_compression(&absent), Some(1)); + } + assert_eq!(first_image_compression(b"II+\0\x08\0\0\0"), None); + assert_eq!(first_image_compression(b"II*\0\xff\xff\xff\xff"), None); + } +} diff --git a/crates/timeline/src/edl.rs b/crates/timeline/src/edl.rs index 66e837506..1d794dc21 100644 --- a/crates/timeline/src/edl.rs +++ b/crates/timeline/src/edl.rs @@ -158,6 +158,10 @@ pub struct Edl { /// Read a list. Never fails: lines that cannot be read are named in `report` and skipped. pub fn parse(text: &str, report: &mut Report) -> Edl { + parse_with_report_limit(text, report, None) +} +/// Parse an EDL with an optional limit on distinct warning messages. +pub fn parse_with_report_limit(text: &str, report: &mut Report, report_limit: Option) -> Edl { let mut p = Edl { title: None, events: Vec::new(), any_df: false, max_ff: 0 }; let mut df = false; let mut skip_metadata = false; @@ -226,7 +230,7 @@ pub fn parse(text: &str, report: &mut Report) -> Edl { if (1..=99).contains(&n) { extra.push(Channel::A(n)); } else { - report.warn(format!("EDL AUD channel {n} is outside 1..99; ignored")); + report.warn_limited(report_limit, format!("EDL AUD channel {n} is outside 1..99; ignored")); } } if let Some(line) = p.events.last_mut().and_then(|e| e.lines.last_mut()) { @@ -235,7 +239,7 @@ pub fn parse(text: &str, report: &mut Report) -> Edl { continue; } if upper.starts_with("SPLIT:") { - report.warn("EDL split (L-cut/J-cut) lines are not supported; the split was ignored"); + report.warn_limited(report_limit, "EDL split (L-cut/J-cut) lines are not supported; the split was ignored"); continue; } if let Some(line) = parse_event_line(l) { @@ -258,7 +262,7 @@ pub fn parse(text: &str, report: &mut Report) -> Edl { // Comments and auxiliary lines after a rejected event do not belong to the previous one. skip_metadata = true; } - report.warn(format!("unrecognised EDL line ignored: {}", l.chars().take(60).collect::())); + report.warn_limited(report_limit, format!("unrecognised EDL line ignored: {}", l.chars().take(60).collect::())); } p } @@ -589,3 +593,20 @@ M2 A001 -012.0 00;00;00;00\n\ assert_eq!(channel_name(Channel::A(7)), "A7"); } } + +#[cfg(test)] +mod report_limit_tests { + use super::*; + + #[test] + fn caps_distinct_unrecognized_lines_without_changing_default() { + let text = (0..10).map(|i| format!("unrecognized-{i}\n")).collect::(); + let mut limited = Report::default(); + let _ = parse_with_report_limit(&text, &mut limited, Some(2)); + assert_eq!(limited.entries.len(), 3); + assert!(limited.mentions("further diagnostics omitted")); + let mut ordinary = Report::default(); + let _ = parse(&text, &mut ordinary); + assert_eq!(ordinary.entries.len(), 10); + } +} diff --git a/crates/timeline/src/fcp7.rs b/crates/timeline/src/fcp7.rs index 04841f320..436f932a7 100644 --- a/crates/timeline/src/fcp7.rs +++ b/crates/timeline/src/fcp7.rs @@ -72,7 +72,7 @@ const BLEND_XML: &[(&str, u32)] = &[ /// The generator of a `generatoritem`: an application's own payload (`:` as /// the effect id), one of the generators every editor has by name, or one the catalog knows. -fn generator_from(name: &str, id: &str, opts: &ImportOptions, report: &mut Report) -> Generator { +fn generator_from(name: &str, id: &str, opts: &ImportOptions, report: &mut Report, limit: Option) -> Generator { if let Some(data) = id.strip_prefix(opts.namespace).and_then(|rest| rest.strip_prefix(':')) && !opts.namespace.is_empty() { @@ -88,7 +88,7 @@ fn generator_from(name: &str, id: &str, opts: &ImportOptions, report: &mut Repor if let Some(g) = opts.catalog.generator_by_name(n) { return g; } - report.warn(format!("generator \"{name}\" is not supported; imported as Black Video")); + report.warn_limited(limit, format!("generator \"{name}\" is not supported; imported as Black Video")); Generator::BlackVideo } } @@ -127,6 +127,7 @@ struct Imp<'a, 'c, 'r> { b: Builder<'c>, opts: &'a ImportOptions<'a>, report: &'r mut Report, + report_limit: Option, files: HashMap>, seq_defs: HashMap>, file_items: HashMap, @@ -163,6 +164,10 @@ fn node_key(n: Node) -> String { /// Read an `xmeml` document. Media paths are resolved against `opts.base_dir`; effects the format /// does not name itself are looked up in `opts.catalog`. What cannot be represented is in `report`. pub fn import(text_in: &str, opts: &ImportOptions, report: &mut Report) -> Result { + import_with_report_limit(text_in, opts, report, None) +} +/// Read FCP7 XML with an optional distinct-diagnostic limit. +pub fn import_with_report_limit(text_in: &str, opts: &ImportOptions, report: &mut Report, report_limit: Option) -> Result { let doc = crate::xml::parse(text_in).map_err(|e| Error::parse(Format::Fcp7Xml, e))?; let root = Node(&doc); if root.name() != "xmeml" { @@ -172,7 +177,7 @@ pub fn import(text_in: &str, opts: &ImportOptions, report: &mut Report) -> Resul .descendants() .any(|n| n.has_tag_name("timebase") && n.text().and_then(|t| t.trim().parse::().ok()).is_some_and(|tb| tb > MAX_RATE as f64)) { - report.warn(format!("a timebase above {MAX_RATE} per second is not a frame rate; it was limited to {MAX_RATE}")); + report.warn_limited(report_limit, format!("a timebase above {MAX_RATE} per second is not a frame rate; it was limited to {MAX_RATE}")); } let mut imp = Imp { b: Builder::new( @@ -181,6 +186,7 @@ pub fn import(text_in: &str, opts: &ImportOptions, report: &mut Report) -> Resul ), opts, report, + report_limit, files: HashMap::new(), seq_defs: HashMap::new(), file_items: HashMap::new(), @@ -227,7 +233,7 @@ pub fn import(text_in: &str, opts: &ImportOptions, report: &mut Report) -> Resul return Err(Error::Empty); } let mut imported = imp.b.finish(); - imported.bound(report); + imported.bound_with_report_limit(report, report_limit); Ok(imported) } @@ -345,7 +351,7 @@ impl<'a> Imp<'a, '_, '_> { self.b.file_media(&format!("file:{p}"), &name, &p, &spec, bin) } None => { - self.report.warn(format!("file \"{name}\" has no pathurl; imported offline")); + self.report.warn_limited(self.report_limit, format!("file \"{name}\" has no pathurl; imported offline")); let p = resolve_path(&name, self.opts.base_dir); let id = self.b.file_media(&format!("fileid:{key}"), &name, &p, &spec, bin); if let Some(m) = self.b.p.item_mut(id).and_then(|i| i.as_media_mut()) { @@ -369,7 +375,7 @@ impl<'a> Imp<'a, '_, '_> { return Some(*id); } if self.reimports_left == 0 { - self.report.warn("truncated nested sequence kept incomplete after the re-import limit was reached"); + self.report.warn_limited(self.report_limit, "truncated nested sequence kept incomplete after the re-import limit was reached"); return Some(*id); } self.reimports_left -= 1; @@ -378,12 +384,12 @@ impl<'a> Imp<'a, '_, '_> { let def = if child(n, "media").is_some() { n } else { *self.seq_defs.get(&key)? }; if depth >= MAX_NESTING_DEPTH { self.depth_hits += 1; - self.report.warn("nested sequence is nested too deeply; imported as a gap"); + self.report.warn_limited(self.report_limit, "nested sequence is nested too deeply; imported as a gap"); return None; } let hits_before = self.depth_hits; if !self.in_progress.insert(key.clone()) { - self.report.warn("recursive nested sequence ignored"); + self.report.warn_limited(self.report_limit, "recursive nested sequence ignored"); return None; } let name = child_text(def, "name").unwrap_or("Sequence").to_string(); @@ -424,7 +430,7 @@ impl<'a> Imp<'a, '_, '_> { let Some(m) = media.and_then(|m| child(m, tag)) else { continue }; for (ti, tn) in children(m, "track").enumerate() { if !self.b.ensure_tracks(&mut seq, kind, ti + 1) { - self.report.warn(format!("FCP7 {kind:?} tracks beyond {MAX_IMPORTED_TRACKS} were skipped")); + self.report.warn_limited(self.report_limit, format!("FCP7 {kind:?} tracks beyond {MAX_IMPORTED_TRACKS} were skipped")); break; } self.track(&mut seq, id, kind, ti, tn, TrackFormat { rate, frame: (w, h) }); @@ -465,10 +471,13 @@ impl<'a> Imp<'a, '_, '_> { } let t = &mut seq.tracks_mut(kind)[ti]; if t.items.iter().any(|o| o.start < ti_item.end() && ti_item.start < o.end()) { - self.report.warn("overlapping clip items on one track; the later one was moved to a new track"); + self.report + .warn_limited(self.report_limit, "overlapping clip items on one track; the later one was moved to a new track"); if self.b.place(seq, kind, ti + 1, ti_item).is_none() { - self.report - .warn(format!("FCP7 {kind:?} track exceeds the {MAX_IMPORTED_TRACKS}-track import limit; clip was skipped")); + self.report.warn_limited( + self.report_limit, + format!("FCP7 {kind:?} track exceeds the {MAX_IMPORTED_TRACKS}-track import limit; clip was skipped"), + ); } } else { t.items.push(ti_item); @@ -510,7 +519,7 @@ impl<'a> Imp<'a, '_, '_> { (seq_rate.tick_of(end) - d, d) } (false, false) => { - self.report.warn(format!("clip \"{name}\" has neither start nor end; skipped")); + self.report.warn_limited(self.report_limit, format!("clip \"{name}\" has neither start nor end; skipped")); return None; } }; @@ -535,7 +544,7 @@ impl<'a> Imp<'a, '_, '_> { match BLEND_XML.iter().find(|(n, _)| n.eq_ignore_ascii_case(mode)) { Some((_, v)) if *v != 0 => set_param(self.b.catalog, &mut ti, "opacity", "blend", Param::new(ParamValue::Choice(*v))), Some(_) => {} - None => self.report.warn(format!("composite mode \"{mode}\" is not supported")), + None => self.report.warn_limited(self.report_limit, format!("composite mode \"{mode}\" is not supported")), } } ti.markers = read_markers(c, rate, &mut self.b); @@ -552,7 +561,7 @@ impl<'a> Imp<'a, '_, '_> { let eff = child(c, "effect"); let gname = eff.and_then(|e| child_text(e, "name")).unwrap_or(name).to_string(); let gid = eff.and_then(|e| child_text(e, "effectid")).unwrap_or("").to_string(); - let mut g = generator_from(&gname, &gid, self.opts, self.report); + let mut g = generator_from(&gname, &gid, self.opts, self.report, self.report_limit); if let (Generator::ColorMatte { color }, Some(e)) = (&mut g, eff) && let Some(v) = e.descendants().find(|d| d.has_tag_name("value") && child(*d, "red").is_some()) { @@ -566,7 +575,7 @@ impl<'a> Imp<'a, '_, '_> { match self.file(f, Some(name), None) { Some(item) => Some(item), None => { - self.report.warn(format!("clip \"{name}\" refers to a file the document does not define; skipped")); + self.report.warn_limited(self.report_limit, format!("clip \"{name}\" refers to a file the document does not define; skipped")); None } } @@ -574,10 +583,10 @@ impl<'a> Imp<'a, '_, '_> { if let Some(id) = self.sequence(s, None) { return Some(id); } - self.report.warn(format!("clip \"{name}\" refers to a nested sequence that was left out; imported offline")); + self.report.warn_limited(self.report_limit, format!("clip \"{name}\" refers to a nested sequence that was left out; imported offline")); Some(self.offline_media(kind, seq_rate, frame, name)) } else { - self.report.warn(format!("clip \"{name}\" has no media reference; imported offline")); + self.report.warn_limited(self.report_limit, format!("clip \"{name}\" has no media reference; imported offline")); Some(self.offline_media(kind, seq_rate, frame, name)) } } @@ -615,12 +624,16 @@ impl<'a> Imp<'a, '_, '_> { remap.reverse |= v < 0.0; } if child(p, "keyframe").is_some() { - self.report.warn("variable speed (time remap keyframes) is not supported; a constant speed was used"); + self.report.warn_limited( + self.report_limit, + "variable speed (time remap keyframes) is not supported; a constant speed was used", + ); } } Some("reverse") => remap.reverse |= child_bool(p, "value").unwrap_or(false), Some("variablespeed") if child_bool(p, "value") == Some(true) || child_f64(p, "value").is_some_and(|v| v != 0.0) => { - self.report.warn("variable speed (time remap keyframes) is not supported; a constant speed was used"); + self.report + .warn_limited(self.report_limit, "variable speed (time remap keyframes) is not supported; a constant speed was used"); } _ => {} } @@ -721,7 +734,7 @@ impl<'a> Imp<'a, '_, '_> { let catalog = self.b.catalog; let def = catalog.effect(id).filter(|d| !d.intrinsic).or_else(|| catalog.effect_by_name(name).filter(|d| !d.intrinsic)); let Some(def) = def else { - self.report.warn(format!("effect \"{name}\" is not supported and was skipped")); + self.report.warn_limited(self.report_limit, format!("effect \"{name}\" is not supported and was skipped")); return; }; let mut inst = def.instance(); @@ -753,7 +766,13 @@ impl<'a> Imp<'a, '_, '_> { } let eff = child(c, "effect"); let name = eff.and_then(|x| child_text(x, "name")).or_else(|| eff.and_then(|x| child_text(x, "effectid"))).unwrap_or("Cross Dissolve"); - let effect = transition_effect(self.b.catalog, name, kind == TrackKind::Audio, self.report); + let mut temporary = Report::default(); + let effect = transition_effect(self.b.catalog, name, kind == TrackKind::Audio, &mut temporary); + for e in temporary.entries { + for _ in 0..e.count { + self.report.warn_limited(self.report_limit, &e.message); + } + } let align_s = child_text(c, "alignment").unwrap_or("").to_ascii_lowercase(); let track = &mut seq.tracks_mut(kind)[ti]; let (mut from, mut to) = (None, None); @@ -774,7 +793,7 @@ impl<'a> Imp<'a, '_, '_> { } } if from.is_none() && to.is_none() { - self.report.warn(format!("transition \"{name}\" at frame {start} has no adjacent clip; skipped")); + self.report.warn_limited(self.report_limit, format!("transition \"{name}\" at frame {start} has no adjacent clip; skipped")); return; } let cut = from.map(|f| f.1).or(to.map(|t| t.1)).unwrap_or(s); @@ -914,6 +933,7 @@ struct Exp<'a, 'r> { p: &'a Project, opts: &'a ExportOptions<'a>, report: &'r mut Report, + report_limit: Option, w: XmlWriter, file_ids: HashMap, seq_ids: HashMap, @@ -924,11 +944,22 @@ struct Exp<'a, 'r> { } pub fn export(p: &Project, seq_id: ItemId, opts: &ExportOptions, report: &mut Report) -> Result { + export_with_report_limit(p, seq_id, opts, report, None) +} +/// Write FCP7 XML with an optional distinct-diagnostic limit. +pub fn export_with_report_limit( + p: &Project, + seq_id: ItemId, + opts: &ExportOptions, + report: &mut Report, + report_limit: Option, +) -> Result { p.sequence(seq_id).ok_or(Error::NoSequence(seq_id))?; let mut x = Exp { p, opts, report, + report_limit, w: XmlWriter::new(Some("xmeml")), file_ids: HashMap::new(), seq_ids: HashMap::new(), @@ -1003,11 +1034,11 @@ impl Exp<'_, '_> { } // Each clip lists every member of its group, so a group of n clips writes n * n links. if let Some(n) = link_members.values().map(Vec::len).filter(|&n| n > MAX_LINK_MEMBERS).max() { - self.report.warn(format!("a link group of {n} clips was exported with its first {MAX_LINK_MEMBERS} members")); + self.report.warn_limited(self.report_limit, format!("a link group of {n} clips was exported with its first {MAX_LINK_MEMBERS} members")); link_members.values_mut().for_each(|m| m.truncate(MAX_LINK_MEMBERS)); } if seq.all_tracks().any(|t| t.items.iter().any(|i| i.group.is_some())) { - self.report.info("clip groups have no FCP7 XML equivalent and were not exported"); + self.report.info_limited(self.report_limit, "clip groups have no FCP7 XML equivalent and were not exported"); } for kind in [TrackKind::Video, TrackKind::Audio] { let tag = if kind == TrackKind::Video { "video" } else { "audio" }; @@ -1064,7 +1095,7 @@ impl Exp<'_, '_> { fn write_transition(&mut self, tr: &Transition, kind: TrackKind, rate: FrameRate, t: &crate::interchange::Track) { if !on_frame(rate, tr.start) || !on_frame(rate, tr.duration) { - self.report.warn("sub-frame transition positions were rounded to frames"); + self.report.warn_limited(self.report_limit, "sub-frame transition positions were rounded to frames"); } let align = match (tr.from, tr.to, tr.align) { (None, Some(_), _) => "start-black", @@ -1150,12 +1181,13 @@ impl Exp<'_, '_> { fn write_clip(&mut self, c: &TrackItem, kind: TrackKind, seq: &Sequence, xid: &str, links: &[(String, TrackKind, usize, usize)]) { let rate = seq.settings.frame_rate; if !on_frame(rate, c.start) || !on_frame(rate, c.duration) { - self.report.warn("sub-frame clip positions were rounded to frames"); + self.report.warn_limited(self.report_limit, "sub-frame clip positions were rounded to frames"); } if let Some(clip) = crate::build::uncarried_clip(self.p, c, rate) { - self.report.warn(format!( - "{clip} has no Final Cut Pro 7 XML equivalent: it is written as a clip item without media and is not read back on import" - )); + self.report.warn_limited( + self.report_limit, + format!("{clip} has no Final Cut Pro 7 XML equivalent: it is written as a clip item without media and is not read back on import"), + ); } let base = base_item(self.p, c.item); let generator = crate::build::generator_of(self.p, base).cloned(); @@ -1201,7 +1233,7 @@ impl Exp<'_, '_> { if let Some(blend) = param(c, "opacity", "blend").and_then(|p| p.value.as_f64()).map(|v| v as u32).filter(|v| *v != 0) { match BLEND_XML.iter().find(|(_, v)| *v == blend) { Some((n, _)) => self.w.text("compositemode", n), - None => self.report.warn("blend mode not representable in FCP7 XML; Normal used"), + None => self.report.warn_limited(self.report_limit, "blend mode not representable in FCP7 XML; Normal used"), } } self.write_filters(c, kind, seq, base); @@ -1247,7 +1279,8 @@ impl Exp<'_, '_> { self.w.text("effectid", g.label()); } Generator::Other { name, data } => { - self.report.info(format!("generator \"{name}\" is written with an effect id only {} reads", self.opts.namespace)); + self.report + .info_limited(self.report_limit, format!("generator \"{name}\" is written with an effect id only {} reads", self.opts.namespace)); self.w.text("name", name); self.w.text("effectid", format!("{}:{data}", self.opts.namespace)); } @@ -1326,7 +1359,7 @@ impl Exp<'_, '_> { if param_modified(self.opts.catalog, c, "motion", "scale_width") || param(c, "motion", "uniform_scale").and_then(|p| p.value.as_bool()) == Some(false) { - self.report.warn("non-uniform scale is not supported by FCP7 Basic Motion; uniform scale written"); + self.report.warn_limited(self.report_limit, "non-uniform scale is not supported by FCP7 Basic Motion; uniform scale written"); } let enabled = c.effect("motion").is_none_or(|e| e.enabled); self.open_effect("Basic Motion", "basic", "motion", "motion", "video", enabled); @@ -1373,7 +1406,7 @@ impl Exp<'_, '_> { } if c.speed != 1.0 || c.reverse || c.frame_hold.is_some() { if param(c, "time_remap", "speed").is_some_and(|p| p.is_animated()) { - self.report.warn("speed ramps (time remapping keyframes) are not exported to FCP7 XML"); + self.report.warn_limited(self.report_limit, "speed ramps (time remapping keyframes) are not exported to FCP7 XML"); } self.open_effect("Time Remap", "timeremap", "motion", "motion", "video", true); let sp = if c.frame_hold.is_some() { 0.0 } else { c.speed * 100.0 }; @@ -1385,7 +1418,7 @@ impl Exp<'_, '_> { } } else { if c.gain_db != 0.0 { - self.report.info("clip gain was folded into Audio Levels"); + self.report.info_limited(self.report_limit, "clip gain was folded into Audio Levels"); } if param_modified(self.opts.catalog, c, "volume", "level") || c.gain_db != 0.0 { let g = c.gain_db; @@ -1408,7 +1441,7 @@ impl Exp<'_, '_> { self.close_effect(); } if param_modified(self.opts.catalog, c, "channel_volume", "left") || param_modified(self.opts.catalog, c, "channel_volume", "right") { - self.report.warn("Channel Volume is not supported by FCP7 XML and was not exported"); + self.report.warn_limited(self.report_limit, "Channel Volume is not supported by FCP7 XML and was not exported"); } } for e in standard_effects(self.opts.catalog, c) { @@ -1492,7 +1525,7 @@ impl Exp<'_, '_> { self.w.close(); } _ => { - self.report.info("Bezier keyframe easing is written as FCP curve keyframes (handles not preserved)"); + self.report.info_limited(self.report_limit, "Bezier keyframe easing is written as FCP curve keyframes (handles not preserved)"); self.w.open("interpolation", &[]); self.w.text("name", "FCPCurve"); self.w.close(); @@ -1510,7 +1543,7 @@ impl Exp<'_, '_> { for k in &p.keyframes { let Some(v) = val(&k.value) else { continue }; if !on_frame(rate, k.time) { - self.report.info("sub-frame keyframes were rounded to frames"); + self.report.info_limited(self.report_limit, "sub-frame keyframes were rounded to frames"); } self.w.open("keyframe", &[]); self.w.text("when", frames_round(rate, k.time)); @@ -1522,7 +1555,7 @@ impl Exp<'_, '_> { fn generic_effect(&mut self, e: &EffectInstance, kind: TrackKind, rate: FrameRate) { let Some(def) = self.opts.catalog.effect(&e.effect) else { - self.report.warn(format!("unknown effect \"{}\" was not exported", e.effect)); + self.report.warn_limited(self.report_limit, format!("unknown effect \"{}\" was not exported", e.effect)); return; }; let media = if kind == TrackKind::Video { "video" } else { "audio" }; @@ -1550,7 +1583,7 @@ impl Exp<'_, '_> { self.w.close(); self.w.close(); if p.is_animated() { - self.report.warn(format!("point keyframes of \"{}\" were not exported", def.name)); + self.report.warn_limited(self.report_limit, format!("point keyframes of \"{}\" were not exported", def.name)); } } ParamValue::Bool(b) => self.simple_param(&pd.id, &pd.label, bool_str(*b)), @@ -1564,7 +1597,7 @@ impl Exp<'_, '_> { self.w.close(); } ParamValue::Opaque(_) => { - self.report.warn(format!("curve parameters of \"{}\" are not supported by FCP7 XML", def.name)); + self.report.warn_limited(self.report_limit, format!("curve parameters of \"{}\" are not supported by FCP7 XML", def.name)); } } } diff --git a/crates/timeline/src/fcpxml.rs b/crates/timeline/src/fcpxml.rs index e123f2997..83bb63597 100644 --- a/crates/timeline/src/fcpxml.rs +++ b/crates/timeline/src/fcpxml.rs @@ -141,6 +141,7 @@ struct Imp<'a, 'c, 'r> { b: Builder<'c>, opts: &'a ImportOptions<'a>, report: &'r mut Report, + report_limit: Option, formats: HashMap, asset_nodes: HashMap>, assets: HashMap, @@ -183,6 +184,10 @@ struct Ctx { /// Read an FCPXML document. Media paths are resolved against `opts.base_dir`; filters are looked /// up in `opts.catalog` by name. What cannot be represented is in `report`. pub fn import(text: &str, opts: &ImportOptions, report: &mut Report) -> Result { + import_with_report_limit(text, opts, report, None) +} +/// Read FCPXML with an optional distinct-diagnostic limit. +pub fn import_with_report_limit(text: &str, opts: &ImportOptions, report: &mut Report, report_limit: Option) -> Result { let doc = crate::xml::parse(text).map_err(|e| Error::parse(Format::Fcpxml, e))?; let root = Node(&doc); if root.name() != "fcpxml" { @@ -191,7 +196,7 @@ pub fn import(text: &str, opts: &ImportOptions, report: &mut Report) -> Result().ok()).unwrap_or(0); if !v.starts_with("1.") || minor > 11 { - report.info(format!("FCPXML version {v} is newer than 1.11; unknown elements were ignored")); + report.info_limited(report_limit, format!("FCPXML version {v} is newer than 1.11; unknown elements were ignored")); } } let lib_name = child(root, "library").and_then(|l| child(l, "event")).and_then(|e| e.attribute("name")).unwrap_or("Imported FCPXML"); @@ -199,6 +204,7 @@ pub fn import(text: &str, opts: &ImportOptions, report: &mut Report) -> Result Result { let rate = match r.attribute("frameDuration") { Some(fd) => rate_from_frame_duration(fd).unwrap_or_else(|| { - imp.report.warn(format!("format \"{id}\" has an invalid frameDuration \"{fd}\"; the default frame rate was used")); + imp.report.warn_limited( + imp.report_limit, + format!("format \"{id}\" has an invalid frameDuration \"{fd}\"; the default frame rate was used"), + ); FrameRate::default() }), None => FrameRate::default(), @@ -276,7 +285,7 @@ pub fn import(text: &str, opts: &ImportOptions, report: &mut Report) -> Result Imp<'a, '_, '_> { self.b.file_media(&format!("file:{p}"), &name, &p, &spec, bin) } None => { - self.report.warn(format!("asset \"{name}\" has no media; imported offline")); + self.report.warn_limited(self.report_limit, format!("asset \"{name}\" has no media; imported offline")); let p = resolve_path(&name, self.opts.base_dir); let it = self.b.file_media(&format!("asset:{id}"), &name, &p, &spec, bin); if let Some(m) = self.b.p.item_mut(it).and_then(|i| i.as_media_mut()) { @@ -367,7 +376,7 @@ impl<'a> Imp<'a, '_, '_> { // Check before the cache: a truncated resource may already have an item while it // is being rebuilt, but a back-edge must never turn that item into a self-cycle. if self.media_active.contains(id) { - self.report.warn(format!("recursive FCPXML media resource {id:?} ignored")); + self.report.warn_limited(self.report_limit, format!("recursive FCPXML media resource {id:?} ignored")); return None; } let depth = self.media_active.len(); @@ -383,9 +392,10 @@ impl<'a> Imp<'a, '_, '_> { if !shallower || self.reimports_left == 0 { if self.cache_reaches_active(i) { self.depth_hits += 1; - self.report.warn(format!( - "FCPXML cached media {id:?} could form a recursive graph or exceeds the cache traversal budget; imported as a gap" - )); + self.report.warn_limited( + self.report_limit, + format!("FCPXML cached media {id:?} could form a recursive graph or exceeds the cache traversal budget; imported as a gap"), + ); return None; } return Some(i); @@ -399,7 +409,7 @@ impl<'a> Imp<'a, '_, '_> { // would otherwise recurse once per resource. if depth >= MAX_MEDIA_DEPTH { self.depth_hits += 1; - self.report.warn(format!("FCPXML media resource {id:?} is nested too deeply; imported as a gap")); + self.report.warn_limited(self.report_limit, format!("FCPXML media resource {id:?} is nested too deeply; imported as a gap")); return None; } self.media_active.insert(id.into()); @@ -447,7 +457,10 @@ impl<'a> Imp<'a, '_, '_> { fn storyline(&mut self, seq: &mut Sequence, ctx: &Ctx, spine: Node<'a>, origin: Tick, lane: i32, primary: bool) { if self.depth >= MAX_XML_DEPTH { self.depth_hits += 1; - self.report.warn(format!("XML storylines are nested too deeply (more than {MAX_XML_DEPTH}); deeper clips were skipped")); + self.report.warn_limited( + self.report_limit, + format!("XML storylines are nested too deeply (more than {MAX_XML_DEPTH}); deeper clips were skipped"), + ); return; } self.depth += 1; @@ -473,7 +486,7 @@ impl<'a> Imp<'a, '_, '_> { let vidx = if lane >= 0 { lane as usize } else { usize::MAX }; for (pos, el) in transitions { if vidx == usize::MAX { - self.report.warn("transitions on audio storylines are not supported"); + self.report.warn_limited(self.report_limit, "transitions on audio storylines are not supported"); continue; } let start = origin + attr_time(el, "offset").unwrap_or(Tick::ZERO); @@ -501,7 +514,13 @@ impl<'a> Imp<'a, '_, '_> { } else { TransitionAlign::CenterAtCut }; - let effect = transition_effect(self.b.catalog, &name, false, self.report); + let mut temporary = Report::default(); + let effect = transition_effect(self.b.catalog, &name, false, &mut temporary); + for e in temporary.entries { + for _ in 0..e.count { + self.report.push_limited(e.level, self.report_limit, &e.message); + } + } let id = TransitionId(self.b.alloc()); // The clips may have been moved to another track by placement; find the track holding them. let clip = from.or(to).map(|c| c.0); @@ -527,7 +546,10 @@ impl<'a> Imp<'a, '_, '_> { fn element(&mut self, seq: &mut Sequence, ctx: &Ctx, el: Node<'a>, t0: Tick, lane: i32, primary: bool) -> (Tick, Option) { if self.depth >= MAX_XML_DEPTH { self.depth_hits += 1; - self.report.warn(format!("XML elements are nested too deeply (more than {MAX_XML_DEPTH}); deeper clips were skipped")); + self.report.warn_limited( + self.report_limit, + format!("XML elements are nested too deeply (more than {MAX_XML_DEPTH}); deeper clips were skipped"), + ); return (t0, None); } self.depth += 1; @@ -556,7 +578,8 @@ impl<'a> Imp<'a, '_, '_> { .flatten() .filter(|a| a.has_video); if asset.is_none() { - self.report.warn(format!("FCPXML